anki/ts/components/WithShortcut.svelte

34 lines
996 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">
2021-04-23 02:16:40 +02:00
import type { Modifier } from "lib/shortcuts";
2021-04-21 16:05:08 +02:00
import { onDestroy } from "svelte";
2021-04-23 02:16:40 +02:00
import { registerShortcut, getPlatformString } from "lib/shortcuts";
export let shortcut: string;
export let optionalModifiers: Modifier[] | undefined = [];
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 KeyboardEvent("click", event));
2021-04-22 03:25:31 +02:00
event.preventDefault();
},
shortcut,
optionalModifiers
2021-04-22 03:25:31 +02:00
);
2021-04-21 16:05:08 +02:00
}
onDestroy(() => deregister());
2021-04-21 16:05:08 +02:00
</script>
<slot {createShortcut} {shortcutLabel} />