anki/ts/editor/editor-toolbar/SuperscriptButton.svelte
Henrik Giesel 9ca13ca3bc
Refactor how toolbar buttons get to surround within editor fields (#1931)
* Export surrounder directly from RichTextInput

* Change wording in editor/surround

* Remove empty line

* Change wording

* Fix interfaces

* Add field description directly in NoteEditor

* Strip description logic from ContentEditable

* Make RichTextInput position: relative

* Make attachToShadow an async function

* Apply field styling to field description

* Show FieldDescription only if content empty

* Remove descriptionStore and descriptionKey

* Revert "Make attachToShadow an async function"

This reverts commit b62705eadf7335c7ee0c6c8797047e1f1ccdbf44.

SvelteActionReturnType does not accept Promise<void>

* Fix mess after merge commit

* Require registering surround formats
2022-08-15 13:34:16 +10:00

100 lines
2.9 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 { onMount } from "svelte";
import IconButton from "../../components/IconButton.svelte";
import Shortcut from "../../components/Shortcut.svelte";
import WithState from "../../components/WithState.svelte";
import { updateStateByKey } from "../../components/WithState.svelte";
import type { MatchType } from "../../domlib/surround";
import * as tr from "../../lib/ftl";
import { getPlatformString } from "../../lib/shortcuts";
import { removeStyleProperties } from "../../lib/styling";
import { singleCallback } from "../../lib/typing";
import { surrounder } from "../rich-text-input";
import { context as editorToolbarContext } from "./EditorToolbar.svelte";
import { superscriptIcon } from "./icons";
const surroundElement = document.createElement("sup");
export function matcher(element: HTMLElement | SVGElement, match: MatchType): void {
if (element.tagName === "SUP") {
return match.remove();
}
if (element.style.verticalAlign === "super") {
return match.clear((): void => {
if (
removeStyleProperties(element, "vertical-align") &&
element.className.length === 0
) {
return match.remove();
}
});
}
}
const key = "superscript";
export const format = {
surroundElement,
matcher,
};
const namedFormat = {
key,
name: tr.editingSuperscript(),
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 makeSuper(): void {
surrounder.surround(key, ["subscript"]);
}
const keyCombination = "Control+=";
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="{tr.editingSuperscript()} ({getPlatformString(keyCombination)})"
{active}
{disabled}
on:click={(event) => {
makeSuper();
updateState(event);
updateStateByKey("subscript", event);
}}
>
{@html superscriptIcon}
</IconButton>
<Shortcut
{keyCombination}
on:action={(event) => {
makeSuper();
updateState(event);
updateStateByKey("subscript", event);
}}
/>
</WithState>