anki/ts/components/WithShortcut.svelte

26 lines
740 B
Svelte
Raw Normal View History

2021-04-21 16:05:08 +02:00
<!--
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";
2021-04-23 02:16:40 +02:00
import { registerShortcut, getPlatformString } from "lib/shortcuts";
export let shortcut: string;
2021-04-21 16:05:08 +02:00
const shortcutLabel = getPlatformString(shortcut);
2021-04-21 16:05:08 +02:00
let deregister: () => void;
function createShortcut(mounted: HTMLElement): void {
deregister = registerShortcut((event: KeyboardEvent) => {
mounted.dispatchEvent(new MouseEvent("click", event));
event.preventDefault();
}, shortcut);
2021-04-21 16:05:08 +02:00
}
onDestroy(() => deregister());
2021-04-21 16:05:08 +02:00
</script>
<slot {createShortcut} {shortcutLabel} />