2023-07-27 00:31:52 -05:00
|
|
|
/*
|
2024-02-13 09:50:11 -06:00
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
2023-07-27 00:31:52 -05:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-09-19 02:37:43 -05:00
|
|
|
import keyCode from './keycode.js';
|
2020-12-27 02:04:41 -06:00
|
|
|
|
2022-07-04 09:46:48 -05:00
|
|
|
type Callback = (ev: KeyboardEvent) => void;
|
|
|
|
|
|
|
|
type Keymap = Record<string, Callback>;
|
2020-12-27 02:04:41 -06:00
|
|
|
|
|
|
|
type Pattern = {
|
|
|
|
which: string[];
|
|
|
|
ctrl?: boolean;
|
|
|
|
shift?: boolean;
|
|
|
|
alt?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
type Action = {
|
|
|
|
patterns: Pattern[];
|
2022-07-04 09:46:48 -05:00
|
|
|
callback: Callback;
|
2020-12-27 02:04:41 -06:00
|
|
|
allowRepeat: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
const parseKeymap = (keymap: Keymap) => Object.entries(keymap).map(([patterns, callback]): Action => {
|
|
|
|
const result = {
|
|
|
|
patterns: [],
|
2022-07-04 09:46:48 -05:00
|
|
|
callback,
|
2022-12-22 01:01:59 -06:00
|
|
|
allowRepeat: true,
|
2020-12-27 02:04:41 -06:00
|
|
|
} as Action;
|
|
|
|
|
|
|
|
if (patterns.match(/^\(.*\)$/) !== null) {
|
|
|
|
result.allowRepeat = false;
|
|
|
|
patterns = patterns.slice(1, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.patterns = patterns.split('|').map(part => {
|
|
|
|
const pattern = {
|
|
|
|
which: [],
|
|
|
|
ctrl: false,
|
|
|
|
alt: false,
|
2022-12-22 01:01:59 -06:00
|
|
|
shift: false,
|
2020-12-27 02:04:41 -06:00
|
|
|
} as Pattern;
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pattern;
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
2023-03-01 00:24:09 -06:00
|
|
|
const ignoreElements = ['input', 'textarea'];
|
2020-12-27 02:04:41 -06:00
|
|
|
|
2022-05-07 00:19:15 -05:00
|
|
|
function match(ev: KeyboardEvent, patterns: Action['patterns']): boolean {
|
2023-03-01 00:24:09 -06:00
|
|
|
const key = ev.key.toLowerCase();
|
2020-12-27 02:04:41 -06:00
|
|
|
return patterns.some(pattern => pattern.which.includes(key) &&
|
2022-05-07 00:19:15 -05:00
|
|
|
pattern.ctrl === ev.ctrlKey &&
|
|
|
|
pattern.shift === ev.shiftKey &&
|
|
|
|
pattern.alt === ev.altKey &&
|
2022-12-22 01:01:59 -06:00
|
|
|
!ev.metaKey,
|
2020-12-27 02:04:41 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const makeHotkey = (keymap: Keymap) => {
|
|
|
|
const actions = parseKeymap(keymap);
|
|
|
|
|
2022-05-07 00:19:15 -05:00
|
|
|
return (ev: KeyboardEvent) => {
|
2020-12-27 02:04:41 -06:00
|
|
|
if (document.activeElement) {
|
2023-03-01 00:24:09 -06:00
|
|
|
if (ignoreElements.some(el => document.activeElement!.matches(el))) return;
|
2020-12-27 02:04:41 -06:00
|
|
|
if (document.activeElement.attributes['contenteditable']) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const action of actions) {
|
2022-05-07 00:19:15 -05:00
|
|
|
const matched = match(ev, action.patterns);
|
2020-12-27 02:04:41 -06:00
|
|
|
|
|
|
|
if (matched) {
|
2022-05-07 00:19:15 -05:00
|
|
|
if (!action.allowRepeat && ev.repeat) return;
|
2020-12-27 02:04:41 -06:00
|
|
|
|
2022-05-07 00:19:15 -05:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
action.callback(ev);
|
2020-12-27 02:04:41 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|