| import { dynamicImports } from "./DynamicImports"; |
|
|
| const hotkeysUsed: Set<string> = new Set(); |
|
|
| let hotkeys: any = undefined; |
|
|
| |
| export let shiftKeyDown = false; |
| export let controlKeyDown = false; |
|
|
| |
| |
| |
| |
| |
| function isTextSelected(): boolean { |
| const selection = window.getSelection(); |
| if (selection === null) { |
| return false; |
| } |
| return selection.toString().length > 0; |
| } |
|
|
| |
| |
| |
| |
| |
| function onInitialClick() { |
| hotkeyslibPromise() |
| .then((hotkeys) => { |
| hotkeys("*", { keyup: true }, (event: any) => { |
| if (hotkeys.shift) { |
| shiftKeyDown = event.type === "keydown"; |
| } |
| if (hotkeys.ctrl) { |
| controlKeyDown = event.type === "keydown"; |
| } |
| if (hotkeys.command) { |
| controlKeyDown = event.type === "keydown"; |
| } |
| }); |
| |
| document.removeEventListener("click", onInitialClick); |
| return; |
| }) |
| .catch((err) => { |
| throw err; |
| }); |
| } |
|
|
| |
| |
| |
| function onWindowBlur() { |
| shiftKeyDown = false; |
| controlKeyDown = false; |
| } |
|
|
| |
| |
| |
| |
| export function setupGlobalKeyListeners() { |
| document.addEventListener("click", onInitialClick); |
| window.addEventListener("blur", onWindowBlur); |
| } |
| |
| |
| |
| |
| |
| function hotkeyslibPromise(): Promise<any> { |
| return hotkeys === undefined |
| ? dynamicImports.hotkeys.module.then((mod) => { |
| hotkeys = mod; |
| return mod; |
| }) |
| : Promise.resolve(hotkeys); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function registerHotkeys( |
| hotkeys: string | string[], |
| pluginId: string, |
| callback: (e: KeyboardEvent) => void |
| ) { |
| const callBackWrapper = (e: KeyboardEvent) => { |
| if (isTextSelected()) { |
| return; |
| } |
| callback(e); |
| }; |
|
|
| |
| if (typeof hotkeys === "string") { |
| hotkeys = [hotkeys]; |
| } |
|
|
| |
| for (let i = 0; i < hotkeys.length; i++) { |
| const hotkey = hotkeys[i]; |
| if (hotkeysUsed.has(hotkey)) { |
| const msg = `Two plugins are trying to use the same hotkey: ${hotkey}`; |
| throw new Error(msg); |
| } |
|
|
| |
| if (hotkey.indexOf("+") !== -1) { |
| const msg = `Plugin ${pluginId} has a hotkey with "+" in it. This is not allowed. Use only the letter.`; |
| throw new Error(msg); |
| } |
|
|
| let key = hotkey.toLowerCase(); |
| if (key.length === 1) { |
| key = `ctrl+${key}, command+${key}`; |
| } |
|
|
| hotkeys[i] = key; |
| } |
|
|
| |
| hotkeys = hotkeys.join(", "); |
|
|
| hotkeyslibPromise() |
| .then((hotkeysLib) => { |
| hotkeysUsed.add(hotkeys as string); |
| hotkeysLib(hotkeys, callBackWrapper); |
| return; |
| }) |
| .catch((err) => { |
| throw err; |
| }); |
| } |
|
|