anki/ts/editor-toolbar/CommandIconButton.svelte

77 lines
1.8 KiB
Svelte
Raw Normal View History

2021-03-29 21:05:30 +02:00
<script lang="typescript" context="module">
import { writable } from "svelte/store";
const commandMap = writable(new Map<string, boolean>());
2021-03-29 21:05:30 +02:00
function updateButton(key: string): void {
2021-03-29 21:05:30 +02:00
commandMap.update(
(map: Map<string, boolean>): Map<string, boolean> =>
new Map([...map, [key, document.queryCommandState(key)]])
);
}
function updateButtons(callback: (key: string) => boolean): void {
2021-03-29 21:05:30 +02:00
commandMap.update(
(map: Map<string, boolean>): Map<string, boolean> => {
const newMap = new Map<string, boolean>();
for (const key of map.keys()) {
newMap.set(key, callback(key));
2021-03-29 21:05:30 +02:00
}
return newMap;
}
);
}
export function updateActiveButtons() {
updateButtons((key: string): boolean => document.queryCommandState(key));
}
export function clearActiveButtons() {
updateButtons((): boolean => false);
}
2021-03-29 21:05:30 +02:00
</script>
<script lang="typescript">
import SquareButton from "./SquareButton.svelte";
2021-03-29 21:05:30 +02:00
export let id;
2021-03-29 21:05:30 +02:00
export let className = "";
export let tooltip: string;
export let icon;
2021-03-29 21:05:30 +02:00
export let command: string;
export let activatable = true;
export let disables = true;
export let dropdownToggle = false;
2021-03-29 21:05:30 +02:00
let active = false;
if (activatable) {
updateButton(command);
2021-03-29 21:05:30 +02:00
2021-04-01 18:55:34 +02:00
commandMap.subscribe((map: Map<string, boolean>): void => {
2021-03-29 21:05:30 +02:00
active = map.get(command);
return () => map.delete(command);
});
}
2021-04-01 18:55:34 +02:00
function onClick(): void {
2021-03-29 21:05:30 +02:00
document.execCommand(command);
updateButton(command);
2021-03-29 21:05:30 +02:00
}
</script>
<SquareButton
{id}
{className}
{tooltip}
{active}
{disables}
{dropdownToggle}
{onClick}
on:mount>
2021-03-30 00:51:44 +02:00
{@html icon}
</SquareButton>