f534dbb8e5
* Put PlainTextInput into its own directory * Create a directory for RichTextInput * Create editor-toolbar directory * Move PreviewButton into editor-toolbar * The time to refactor this is not quite yet here * Create tag-editor directory * Remove some of the uses of WithShortcut * Remove all uses of WithShortcut from editor package * Remove last uses of WithShortcut * Fix typo
89 lines
2.7 KiB
Svelte
89 lines
2.7 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 * as tr from "../../lib/ftl";
|
|
import IconButton from "../../components/IconButton.svelte";
|
|
import Shortcut from "../../components/Shortcut.svelte";
|
|
import WithState from "../../components/WithState.svelte";
|
|
import { MatchResult } from "../../domlib/surround";
|
|
import { getPlatformString } from "../../lib/shortcuts";
|
|
import { getSurrounder } from "../surround";
|
|
import { getNoteEditor } from "../OldEditorAdapter.svelte";
|
|
import type { RichTextInputAPI } from "../rich-text-input";
|
|
import { boldIcon } from "./icons";
|
|
|
|
function matchBold(element: Element): Exclude<MatchResult, MatchResult.ALONG> {
|
|
if (!(element instanceof HTMLElement) && !(element instanceof SVGElement)) {
|
|
return MatchResult.NO_MATCH;
|
|
}
|
|
|
|
if (element.tagName === "B" || element.tagName === "STRONG") {
|
|
return MatchResult.MATCH;
|
|
}
|
|
|
|
const fontWeight = element.style.fontWeight;
|
|
if (fontWeight === "bold" || Number(fontWeight) >= 400) {
|
|
return MatchResult.KEEP;
|
|
}
|
|
|
|
return MatchResult.NO_MATCH;
|
|
}
|
|
|
|
function clearBold(element: Element): boolean {
|
|
const htmlElement = element as HTMLElement | SVGElement;
|
|
htmlElement.style.removeProperty("font-weight");
|
|
|
|
if (htmlElement.style.cssText.length === 0) {
|
|
htmlElement.removeAttribute("style");
|
|
}
|
|
|
|
return !htmlElement.hasAttribute("style") && element.className.length === 0;
|
|
}
|
|
|
|
const { focusInRichText, activeInput } = getNoteEditor();
|
|
|
|
$: input = $activeInput;
|
|
$: disabled = !$focusInRichText;
|
|
$: surrounder = disabled ? null : getSurrounder(input as RichTextInputAPI);
|
|
|
|
function updateStateFromActiveInput(): Promise<boolean> {
|
|
return disabled ? Promise.resolve(false) : surrounder!.isSurrounded(matchBold);
|
|
}
|
|
|
|
const element = document.createElement("strong");
|
|
function makeBold(): void {
|
|
surrounder?.surroundCommand(element, matchBold, clearBold);
|
|
}
|
|
|
|
const keyCombination = "Control+B";
|
|
</script>
|
|
|
|
<WithState
|
|
key="bold"
|
|
update={updateStateFromActiveInput}
|
|
let:state={isBold}
|
|
let:updateState
|
|
>
|
|
<IconButton
|
|
tooltip="{tr.editingBoldText()} ({getPlatformString(keyCombination)})"
|
|
active={isBold}
|
|
{disabled}
|
|
on:click={(event) => {
|
|
makeBold();
|
|
updateState(event);
|
|
}}
|
|
>
|
|
{@html boldIcon}
|
|
</IconButton>
|
|
|
|
<Shortcut
|
|
{keyCombination}
|
|
on:action={(event) => {
|
|
makeBold();
|
|
updateState(event);
|
|
}}
|
|
/>
|
|
</WithState>
|