2021-03-29 21:05:30 +02:00
|
|
|
<script lang="typescript" context="module">
|
|
|
|
import { writable } from "svelte/store";
|
|
|
|
|
2021-04-03 03:24:22 +02:00
|
|
|
const commandMap = writable(new Map<string, boolean>());
|
2021-03-29 21:05:30 +02:00
|
|
|
|
2021-04-03 03:24:22 +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)]])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-30 04:38:02 +02:00
|
|
|
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()) {
|
2021-03-30 04:38:02 +02:00
|
|
|
newMap.set(key, callback(key));
|
2021-03-29 21:05:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return newMap;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2021-03-30 04:38:02 +02:00
|
|
|
|
2021-03-30 06:14:00 +02:00
|
|
|
export function updateActiveButtons() {
|
2021-03-30 04:38:02 +02:00
|
|
|
updateButtons((key: string): boolean => document.queryCommandState(key));
|
|
|
|
}
|
|
|
|
|
2021-03-30 06:14:00 +02:00
|
|
|
export function clearActiveButtons() {
|
2021-03-30 04:38:02 +02:00
|
|
|
updateButtons((): boolean => false);
|
|
|
|
}
|
2021-03-29 21:05:30 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<script lang="typescript">
|
2021-03-30 18:57:50 +02:00
|
|
|
import SquareButton from "./SquareButton.svelte";
|
2021-03-29 21:05:30 +02:00
|
|
|
|
2021-04-09 22:02:34 +02:00
|
|
|
export let id;
|
2021-03-29 21:05:30 +02:00
|
|
|
export let className = "";
|
2021-04-08 16:40:42 +02:00
|
|
|
export let tooltip: string;
|
2021-03-30 18:57:50 +02:00
|
|
|
|
2021-04-08 16:29:28 +02:00
|
|
|
export let icon;
|
2021-03-29 21:05:30 +02:00
|
|
|
export let command: string;
|
|
|
|
export let activatable = true;
|
2021-04-09 22:02:34 +02:00
|
|
|
export let disables = true;
|
|
|
|
export let dropdownToggle = false;
|
2021-03-29 21:05:30 +02:00
|
|
|
|
|
|
|
let active = false;
|
|
|
|
|
|
|
|
if (activatable) {
|
2021-04-03 03:24:22 +02:00
|
|
|
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);
|
2021-04-03 03:24:22 +02:00
|
|
|
updateButton(command);
|
2021-03-29 21:05:30 +02:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2021-04-09 22:02:34 +02:00
|
|
|
<SquareButton
|
|
|
|
{id}
|
|
|
|
{className}
|
|
|
|
{tooltip}
|
|
|
|
{active}
|
|
|
|
{disables}
|
|
|
|
{dropdownToggle}
|
|
|
|
{onClick}
|
|
|
|
on:mount>
|
2021-03-30 00:51:44 +02:00
|
|
|
{@html icon}
|
2021-03-30 18:57:50 +02:00
|
|
|
</SquareButton>
|