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 type { DynamicSvelteComponent } from "sveltelib/dynamicComponent";
|
|
|
|
import type { ToolbarItem } from "./types";
|
|
|
|
|
2021-04-22 01:14:38 +02:00
|
|
|
import { onDestroy } from "svelte";
|
2021-04-22 13:04:24 +02:00
|
|
|
import { registerShortcut, getPlatformString } from "anki/shortcuts";
|
2021-04-22 01:14:38 +02:00
|
|
|
|
2021-04-21 16:05:08 +02:00
|
|
|
export let button: ToolbarItem;
|
2021-04-22 03:25:31 +02:00
|
|
|
export let shortcuts: string[];
|
2021-04-21 16:05:08 +02:00
|
|
|
|
2021-04-22 14:18:48 +02:00
|
|
|
function extend({ ...rest }: DynamicSvelteComponent): DynamicSvelteComponent {
|
|
|
|
const shortcutLabel = getPlatformString(shortcuts[0]);
|
2021-04-22 13:04:24 +02:00
|
|
|
|
2021-04-21 16:05:08 +02:00
|
|
|
return {
|
2021-04-22 14:18:48 +02:00
|
|
|
shortcutLabel,
|
2021-04-21 16:05:08 +02:00
|
|
|
...rest,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-22 14:54:29 +02:00
|
|
|
let deregisters: (() => void)[];
|
2021-04-22 01:14:38 +02:00
|
|
|
|
2021-04-21 16:05:08 +02:00
|
|
|
function createShortcut({ detail }: CustomEvent): void {
|
2021-04-22 02:36:54 +02:00
|
|
|
const mounted: HTMLButtonElement = detail.button;
|
2021-04-22 03:25:31 +02:00
|
|
|
deregisters = shortcuts.map((shortcut: string): (() => void) =>
|
|
|
|
registerShortcut((event) => {
|
|
|
|
mounted.dispatchEvent(new MouseEvent("click"));
|
|
|
|
event.preventDefault();
|
|
|
|
}, shortcut)
|
|
|
|
);
|
2021-04-21 16:05:08 +02:00
|
|
|
}
|
2021-04-22 01:14:38 +02:00
|
|
|
|
2021-04-22 14:54:29 +02:00
|
|
|
onDestroy(() => deregisters.forEach((dereg: () => void): void => dereg()));
|
2021-04-21 16:05:08 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<svelte:component
|
|
|
|
this={button.component}
|
|
|
|
{...extend(button)}
|
|
|
|
on:mount={createShortcut} />
|