Use registerShortcut within WithShortcut

This commit is contained in:
Henrik Giesel 2021-04-22 01:14:38 +02:00
parent b57810556d
commit dc5b13eeab
3 changed files with 13 additions and 15 deletions

View File

@ -4,4 +4,5 @@ import type { ToolbarItem } from "./types";
export interface WithShortcutProps { export interface WithShortcutProps {
button: ToolbarItem; button: ToolbarItem;
shortcut: string;
} }

View File

@ -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 { DynamicSvelteComponent } from "sveltelib/dynamicComponent";
import type { ToolbarItem } from "./types"; import type { ToolbarItem } from "./types";
export let button: ToolbarItem; import { onDestroy } from "svelte";
/* export let event: string; */ import { registerShortcut } from "anki/shortcuts";
function extend({ export let button: ToolbarItem;
className, export let shortcut: string;
...rest
}: DynamicSvelteComponent): DynamicSvelteComponent { function extend({ ...rest }: DynamicSvelteComponent): DynamicSvelteComponent {
return { return {
...rest, ...rest,
}; };
} }
let deregister;
function createShortcut({ detail }: CustomEvent): void { function createShortcut({ detail }: CustomEvent): void {
const button: HTMLButtonElement = detail.button; const button: HTMLButtonElement = detail.button;
deregister = registerShortcut(() => button.click(), shortcut);
document.addEventListener("click", () => {
const event = new MouseEvent("click");
if (code === "KeyP") {
button.dispatchEvent(event);
}
});
} }
onDestroy(() => deregister());
</script> </script>
<svelte:component <svelte:component

View File

@ -58,7 +58,7 @@ function innerShortcut(
} }
} }
export function shortcut( export function registerShortcut(
callback: (event: KeyboardEvent) => void, callback: (event: KeyboardEvent) => void,
shortcutString: string shortcutString: string
): () => void { ): () => void {