summaryrefslogtreecommitdiff
path: root/packages/frontend/src/scripts/hotkey.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/frontend/src/scripts/hotkey.ts')
-rw-r--r--packages/frontend/src/scripts/hotkey.ts201
1 files changed, 139 insertions, 62 deletions
diff --git a/packages/frontend/src/scripts/hotkey.ts b/packages/frontend/src/scripts/hotkey.ts
index 0600bff893..04fb235694 100644
--- a/packages/frontend/src/scripts/hotkey.ts
+++ b/packages/frontend/src/scripts/hotkey.ts
@@ -2,94 +2,171 @@
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
+import { getHTMLElementOrNull } from "@/scripts/get-dom-node-or-null.js";
-import keyCode from './keycode.js';
+//#region types
+export type Keymap = Record<string, CallbackFunction | CallbackObject>;
-type Callback = (ev: KeyboardEvent) => void;
+type CallbackFunction = (ev: KeyboardEvent) => unknown;
-type Keymap = Record<string, Callback>;
+type CallbackObject = {
+ callback: CallbackFunction;
+ allowRepeat?: boolean;
+};
type Pattern = {
which: string[];
- ctrl?: boolean;
- shift?: boolean;
- alt?: boolean;
+ ctrl: boolean;
+ alt: boolean;
+ shift: boolean;
};
type Action = {
patterns: Pattern[];
- callback: Callback;
- allowRepeat: boolean;
+ callback: CallbackFunction;
+ options: Required<Omit<CallbackObject, 'callback'>>;
};
+//#endregion
-const parseKeymap = (keymap: Keymap) => Object.entries(keymap).map(([patterns, callback]): Action => {
- const result = {
- patterns: [],
- callback,
- allowRepeat: true,
- } as Action;
+//#region consts
+const KEY_ALIASES = {
+ 'esc': 'Escape',
+ 'enter': 'Enter',
+ 'space': ' ',
+ 'up': 'ArrowUp',
+ 'down': 'ArrowDown',
+ 'left': 'ArrowLeft',
+ 'right': 'ArrowRight',
+ 'plus': ['+', ';'],
+};
- if (patterns.match(/^\(.*\)$/) !== null) {
- result.allowRepeat = false;
- patterns = patterns.slice(1, -1);
- }
+const MODIFIER_KEYS = ['ctrl', 'alt', 'shift'];
+
+const IGNORE_ELEMENTS = ['input', 'textarea'];
+//#endregion
- result.patterns = patterns.split('|').map(part => {
- const pattern = {
- which: [],
- ctrl: false,
- alt: false,
- shift: false,
- } as Pattern;
+//#region store
+let latestHotkey: Pattern & { callback: CallbackFunction } | null = null;
+//#endregion
- const keys = part.trim().split('+').map(x => x.trim().toLowerCase());
- for (const key of keys) {
- switch (key) {
- case 'ctrl': pattern.ctrl = true; break;
- case 'alt': pattern.alt = true; break;
- case 'shift': pattern.shift = true; break;
- default: pattern.which = keyCode(key).map(k => k.toLowerCase());
+//#region impl
+export const makeHotkey = (keymap: Keymap) => {
+ const actions = parseKeymap(keymap);
+ return (ev: KeyboardEvent) => {
+ if ('pswp' in window && window.pswp != null) return;
+ if (document.activeElement != null) {
+ if (IGNORE_ELEMENTS.includes(document.activeElement.tagName.toLowerCase())) return;
+ if (getHTMLElementOrNull(document.activeElement)?.isContentEditable) return;
+ }
+ for (const action of actions) {
+ if (matchPatterns(ev, action)) {
+ ev.preventDefault();
+ ev.stopPropagation();
+ action.callback(ev);
+ storePattern(ev, action.callback);
}
}
+ };
+};
- return pattern;
+const parseKeymap = (keymap: Keymap) => {
+ return Object.entries(keymap).map(([rawPatterns, rawCallback]) => {
+ const patterns = parsePatterns(rawPatterns);
+ const callback = parseCallback(rawCallback);
+ const options = parseOptions(rawCallback);
+ return { patterns, callback, options } as const satisfies Action;
});
+};
- return result;
-});
-
-const ignoreElements = ['input', 'textarea'];
+const parsePatterns = (rawPatterns: keyof Keymap) => {
+ return rawPatterns.split('|').map(part => {
+ const keys = part.split('+').map(trimLower);
+ const which = parseKeyCode(keys.findLast(x => !MODIFIER_KEYS.includes(x)));
+ const ctrl = keys.includes('ctrl');
+ const alt = keys.includes('alt');
+ const shift = keys.includes('shift');
+ return { which, ctrl, alt, shift } as const satisfies Pattern;
+ });
+};
-function match(ev: KeyboardEvent, patterns: Action['patterns']): boolean {
- const key = ev.key.toLowerCase();
- return patterns.some(pattern => pattern.which.includes(key) &&
- pattern.ctrl === ev.ctrlKey &&
- pattern.shift === ev.shiftKey &&
- pattern.alt === ev.altKey &&
- !ev.metaKey,
- );
-}
+const parseCallback = (rawCallback: Keymap[keyof Keymap]) => {
+ if (typeof rawCallback === 'object') {
+ return rawCallback.callback;
+ }
+ return rawCallback;
+};
-export const makeHotkey = (keymap: Keymap) => {
- const actions = parseKeymap(keymap);
+const parseOptions = (rawCallback: Keymap[keyof Keymap]) => {
+ const defaultOptions = {
+ allowRepeat: false,
+ } as const satisfies Action['options'];
+ if (typeof rawCallback === 'object') {
+ const { callback, ...rawOptions } = rawCallback;
+ const options = { ...defaultOptions, ...rawOptions };
+ return { ...options } as const satisfies Action['options'];
+ }
+ return { ...defaultOptions } as const satisfies Action['options'];
+};
- return (ev: KeyboardEvent) => {
- if (document.activeElement) {
- if (ignoreElements.some(el => document.activeElement!.matches(el))) return;
- if (document.activeElement.attributes['contenteditable']) return;
+const matchPatterns = (ev: KeyboardEvent, action: Action) => {
+ const { patterns, options, callback } = action;
+ if (ev.repeat && !options.allowRepeat) return false;
+ const key = ev.key.toLowerCase();
+ return patterns.some(({ which, ctrl, shift, alt }) => {
+ if (
+ options.allowRepeat === false &&
+ latestHotkey != null &&
+ latestHotkey.which.includes(key) &&
+ latestHotkey.ctrl === ctrl &&
+ latestHotkey.alt === alt &&
+ latestHotkey.shift === shift &&
+ latestHotkey.callback === callback
+ ) {
+ return false;
}
+ if (!which.includes(key)) return false;
+ if (ctrl !== (ev.ctrlKey || ev.metaKey)) return false;
+ if (alt !== ev.altKey) return false;
+ if (shift !== ev.shiftKey) return false;
+ return true;
+ });
+};
- for (const action of actions) {
- const matched = match(ev, action.patterns);
+let lastHotKeyStoreTimer: number | null = null;
- if (matched) {
- if (!action.allowRepeat && ev.repeat) return;
+const storePattern = (ev: KeyboardEvent, callback: CallbackFunction) => {
+ if (lastHotKeyStoreTimer != null) {
+ clearTimeout(lastHotKeyStoreTimer);
+ }
- ev.preventDefault();
- ev.stopPropagation();
- action.callback(ev);
- break;
- }
- }
+ latestHotkey = {
+ which: [ev.key.toLowerCase()],
+ ctrl: ev.ctrlKey || ev.metaKey,
+ alt: ev.altKey,
+ shift: ev.shiftKey,
+ callback,
};
+
+ lastHotKeyStoreTimer = window.setTimeout(() => {
+ latestHotkey = null;
+ }, 500);
};
+
+const parseKeyCode = (input?: string | null) => {
+ if (input == null) return [];
+ const raw = getValueByKey(KEY_ALIASES, input);
+ if (raw == null) return [input];
+ if (typeof raw === 'string') return [trimLower(raw)];
+ return raw.map(trimLower);
+};
+
+const getValueByKey = <
+ T extends Record<keyof any, unknown>,
+ K extends keyof T | keyof any,
+ R extends K extends keyof T ? T[K] : T[keyof T] | undefined,
+>(obj: T, key: K) => {
+ return obj[key] as R;
+};
+
+const trimLower = (str: string) => str.trim().toLowerCase();
+//#endregion