anki/ts/components/WithShortcut.svelte
2021-09-09 15:37:34 +02:00

26 lines
740 B
Svelte

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="typescript">
import { onDestroy } from "svelte";
import { registerShortcut, getPlatformString } from "lib/shortcuts";
export let shortcut: string;
const shortcutLabel = getPlatformString(shortcut);
let deregister: () => void;
function createShortcut(element: HTMLElement): void {
deregister = registerShortcut((event: KeyboardEvent) => {
element.dispatchEvent(new MouseEvent("click", event));
event.preventDefault();
}, shortcut);
}
onDestroy(() => deregister());
</script>
<slot {createShortcut} {shortcutLabel} />