From 92d511e313d5049fd859160902ed7b735e4f0f3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesper=20M=C3=B8ller=20Jensen?= <26099018+JesmoDev@users.noreply.github.com> Date: Wed, 22 Feb 2023 12:35:22 +1300 Subject: [PATCH] added shortcut service --- .../src/backoffice/backoffice.element.ts | 2 + .../src/core/shortcuts/shortcut.service.ts | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/Umbraco.Web.UI.Client/src/core/shortcuts/shortcut.service.ts diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/backoffice.element.ts b/src/Umbraco.Web.UI.Client/src/backoffice/backoffice.element.ts index dae0f7ba21..51873f3759 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/backoffice.element.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/backoffice.element.ts @@ -55,6 +55,7 @@ import './search'; import './templating'; import './shared'; import { UmbLitElement } from '@umbraco-cms/element'; +import UmbShortcutService from 'src/core/shortcuts/shortcut.service'; @defineElement('umb-backoffice') export class UmbBackofficeElement extends UmbLitElement { @@ -107,6 +108,7 @@ export class UmbBackofficeElement extends UmbLitElement { new UmbTemplateTreeStore(this); new UmbTemplateDetailStore(this); new UmbLanguageStore(this); + new UmbShortcutService(); this.provideContext(UMB_BACKOFFICE_CONTEXT_TOKEN, new UmbBackofficeContext()); this.provideContext(UMB_CURRENT_USER_HISTORY_STORE_CONTEXT_TOKEN, new UmbCurrentUserHistoryStore()); diff --git a/src/Umbraco.Web.UI.Client/src/core/shortcuts/shortcut.service.ts b/src/Umbraco.Web.UI.Client/src/core/shortcuts/shortcut.service.ts new file mode 100644 index 0000000000..334506cb14 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/core/shortcuts/shortcut.service.ts @@ -0,0 +1,44 @@ +export type Shortcut = { + key: string; + altKey?: boolean; + ctrlKey?: boolean; + shiftKey?: boolean; + metaKey?: boolean; + callback(): void; +}; + +export class UmbShortcutService { + #shortcuts: Array = [ + { + key: 'k', + metaKey: true, + callback: () => console.log('Open search'), + }, + ]; + + constructor() { + addEventListener('keydown', (event: KeyboardEvent) => { + if (!event.altKey && !event.ctrlKey && !event.shiftKey && !event.metaKey) return; + if (event.key === 'Shift' || event.key === 'Control' || event.key === 'Alt' || event.key === 'Meta') return; + + const shortcut = this.#shortcuts.find((x) => { + if (x.key !== event.key) return false; + if ((x.altKey ? true : false) !== event.altKey) return false; + if ((x.ctrlKey ? true : false) !== event.ctrlKey) return false; + if ((x.shiftKey ? true : false) !== event.shiftKey) return false; + if ((x.metaKey ? true : false) !== event.metaKey) return false; + return true; + }); + + shortcut?.callback(); + }); + } +} + +export default UmbShortcutService; + +declare global { + interface HTMLElementTagNameMap { + 'umb-shortcut': UmbShortcutService; + } +}