diff --git a/ts/editor-toolbar/WithShortcut.d.ts b/ts/editor-toolbar/WithShortcut.d.ts index 2792bb7ae..624622767 100644 --- a/ts/editor-toolbar/WithShortcut.d.ts +++ b/ts/editor-toolbar/WithShortcut.d.ts @@ -4,4 +4,5 @@ import type { ToolbarItem } from "./types"; export interface WithShortcutProps { button: ToolbarItem; + shortcut: string; } diff --git a/ts/editor-toolbar/WithShortcut.svelte b/ts/editor-toolbar/WithShortcut.svelte index f3293e761..bbc5e9cba 100644 --- a/ts/editor-toolbar/WithShortcut.svelte +++ b/ts/editor-toolbar/WithShortcut.svelte @@ -6,29 +6,26 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import type { DynamicSvelteComponent } from "sveltelib/dynamicComponent"; import type { ToolbarItem } from "./types"; - export let button: ToolbarItem; - /* export let event: string; */ + import { onDestroy } from "svelte"; + import { registerShortcut } from "anki/shortcuts"; - function extend({ - className, - ...rest - }: DynamicSvelteComponent): DynamicSvelteComponent { + export let button: ToolbarItem; + export let shortcut: string; + + function extend({ ...rest }: DynamicSvelteComponent): DynamicSvelteComponent { return { ...rest, }; } + let deregister; + function createShortcut({ detail }: CustomEvent): void { const button: HTMLButtonElement = detail.button; - - document.addEventListener("click", () => { - const event = new MouseEvent("click"); - - if (code === "KeyP") { - button.dispatchEvent(event); - } - }); + deregister = registerShortcut(() => button.click(), shortcut); } + + onDestroy(() => deregister()); void, shortcutString: string ): () => void {