anki/ts/components/WithShortcut.svelte

27 lines
801 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;
2021-04-21 16:05:08 +02:00
function createShortcut({ detail }: CustomEvent): void {
const mounted: HTMLButtonElement = detail.button;
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} />