anki/ts/editor/editor-toolbar/BoldButton.svelte
Damien Elmes 856d4763a9 Switch strong/em back to b/i
In the absence of strong arguments either way, and given the fact that
strong/em has broken a few (perhaps rare) use-cases, reverting seems
prudent. But happy to revisit this again in the future if someone has
a convincing argument for why we should be doing it differently.

Closes #1807
2022-04-19 16:28:44 +10:00

101 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 IconButton from "../../components/IconButton.svelte";
import Shortcut from "../../components/Shortcut.svelte";
import WithState from "../../components/WithState.svelte";
import type { MatchType } from "../../domlib/surround";
import * as tr from "../../lib/ftl";
import { getPlatformString } from "../../lib/shortcuts";
import { context as noteEditorContext } from "../NoteEditor.svelte";
import { editingInputIsRichText } from "../rich-text-input";
import { removeEmptyStyle, Surrounder } from "../surround";
import { context as editorToolbarContext } from "./EditorToolbar.svelte";
import { boldIcon } from "./icons";
const surroundElement = document.createElement("b");
function matcher(element: HTMLElement | SVGElement, match: MatchType): void {
if (element.tagName === "B" || element.tagName === "STRONG") {
return match.remove();
}
const fontWeight = element.style.fontWeight;
if (fontWeight === "bold" || Number(fontWeight) >= 400) {
return match.clear((): void => {
element.style.removeProperty("font-weight");
if (removeEmptyStyle(element) && element.className.length === 0) {
match.remove();
}
});
}
}
const format = {
surroundElement,
matcher,
};
const namedFormat = {
name: tr.editingBoldText(),
show: true,
active: true,
format,
};
const { removeFormats } = editorToolbarContext.get();
removeFormats.update((formats) => [...formats, namedFormat]);
const { focusedInput } = noteEditorContext.get();
const surrounder = Surrounder.make();
let disabled: boolean;
$: if (editingInputIsRichText($focusedInput)) {
surrounder.richText = $focusedInput;
disabled = false;
} else {
surrounder.disable();
disabled = true;
}
function updateStateFromActiveInput(): Promise<boolean> {
return disabled ? Promise.resolve(false) : surrounder.isSurrounded(format);
}
function makeBold(): void {
surrounder.surround(format);
}
const keyCombination = "Control+B";
</script>
<WithState
key="bold"
update={updateStateFromActiveInput}
let:state={active}
let:updateState
>
<IconButton
tooltip="{tr.editingBoldText()} ({getPlatformString(keyCombination)})"
{active}
{disabled}
on:click={(event) => {
makeBold();
updateState(event);
}}
>
{@html boldIcon}
</IconButton>
<Shortcut
{keyCombination}
on:action={(event) => {
makeBold();
updateState(event);
}}
/>
</WithState>