mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00

* Factor out common code in bold/italic/underline/sub/sup buttons * Update exclusiveNames' states on click * Update exclusiveNames' states on keyboard shortcut
81 lines
2.4 KiB
Svelte
81 lines
2.4 KiB
Svelte
<!--
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
-->
|
|
<script lang="ts">
|
|
import { getPlatformString } from "@tslib/shortcuts";
|
|
import { singleCallback } from "@tslib/typing";
|
|
import { onMount } from "svelte";
|
|
|
|
import IconButton from "../../components/IconButton.svelte";
|
|
import Shortcut from "../../components/Shortcut.svelte";
|
|
import WithState, { updateStateByKey } from "../../components/WithState.svelte";
|
|
import type { MatchType } from "../../domlib/surround";
|
|
import { surrounder } from "../rich-text-input";
|
|
import { context as editorToolbarContext } from "./EditorToolbar.svelte";
|
|
|
|
export let tagName;
|
|
export let matcher: (element: HTMLElement | SVGElement, match: MatchType) => void;
|
|
export let key: string;
|
|
export let tooltip: string;
|
|
export let keyCombination: string;
|
|
export let exclusiveNames: string[] = [];
|
|
|
|
const surroundElement = document.createElement(tagName);
|
|
|
|
const format = {
|
|
surroundElement,
|
|
matcher,
|
|
};
|
|
|
|
const namedFormat = {
|
|
key,
|
|
name: tooltip,
|
|
show: true,
|
|
active: true,
|
|
};
|
|
|
|
const { removeFormats } = editorToolbarContext.get();
|
|
removeFormats.update((formats) => [...formats, namedFormat]);
|
|
|
|
async function updateStateFromActiveInput(): Promise<boolean> {
|
|
return disabled ? false : surrounder.isSurrounded(key);
|
|
}
|
|
|
|
function applyAttribute(): void {
|
|
surrounder.surround(key, exclusiveNames);
|
|
}
|
|
|
|
let disabled: boolean;
|
|
|
|
onMount(() =>
|
|
singleCallback(
|
|
surrounder.active.subscribe((value) => (disabled = !value)),
|
|
surrounder.registerFormat(key, format),
|
|
),
|
|
);
|
|
</script>
|
|
|
|
<WithState {key} update={updateStateFromActiveInput} let:state={active} let:updateState>
|
|
<IconButton
|
|
tooltip="{tooltip} ({getPlatformString(keyCombination)})"
|
|
{active}
|
|
{disabled}
|
|
on:click={(event) => {
|
|
applyAttribute();
|
|
updateState(event);
|
|
exclusiveNames.map((name) => updateStateByKey(name, event));
|
|
}}
|
|
>
|
|
<slot />
|
|
</IconButton>
|
|
|
|
<Shortcut
|
|
{keyCombination}
|
|
on:action={(event) => {
|
|
applyAttribute();
|
|
updateState(event);
|
|
exclusiveNames.map((name) => updateStateByKey(name, event));
|
|
}}
|
|
/>
|
|
</WithState>
|