2021-11-18 10:18:39 +01:00
|
|
|
<!--
|
|
|
|
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";
|
2021-11-24 01:33:14 +01:00
|
|
|
import { getSurrounder } from "./surround";
|
2021-11-18 10:18:39 +01:00
|
|
|
import { boldIcon } from "./icons";
|
|
|
|
import { getNoteEditor } from "./OldEditorAdapter.svelte";
|
|
|
|
import type { RichTextInputAPI } from "./RichTextInput.svelte";
|
|
|
|
|
2021-11-24 01:33:14 +01:00
|
|
|
function matchBold(element: Element): Exclude<MatchResult, MatchResult.ALONG> {
|
2021-11-18 10:18:39 +01:00
|
|
|
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;
|
2021-11-24 01:33:14 +01:00
|
|
|
$: surrounder = disabled ? null : getSurrounder(input as RichTextInputAPI);
|
2021-11-18 10:18:39 +01:00
|
|
|
|
|
|
|
function updateStateFromActiveInput(): Promise<boolean> {
|
2021-11-24 01:33:14 +01:00
|
|
|
return disabled ? Promise.resolve(false) : surrounder!.isSurrounded(matchBold);
|
2021-11-18 10:18:39 +01:00
|
|
|
}
|
|
|
|
|
2021-11-24 01:33:14 +01:00
|
|
|
const element = document.createElement("strong");
|
2021-11-18 10:18:39 +01:00
|
|
|
function makeBold(): void {
|
2021-11-24 01:33:14 +01:00
|
|
|
surrounder?.surroundCommand(element, matchBold, clearBold);
|
2021-11-18 10:18:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const keyCombination = "Control+B";
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<WithState
|
|
|
|
key="bold"
|
|
|
|
update={updateStateFromActiveInput}
|
2021-11-24 01:33:14 +01:00
|
|
|
let:state={isBold}
|
2021-11-18 10:18:39 +01:00
|
|
|
let:updateState
|
|
|
|
>
|
|
|
|
<IconButton
|
|
|
|
tooltip="{tr.editingBoldText()} ({getPlatformString(keyCombination)})"
|
2021-11-24 01:33:14 +01:00
|
|
|
active={isBold}
|
2021-11-18 10:18:39 +01:00
|
|
|
{disabled}
|
|
|
|
on:click={(event) => {
|
|
|
|
makeBold();
|
|
|
|
updateState(event);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{@html boldIcon}
|
|
|
|
</IconButton>
|
|
|
|
|
|
|
|
<Shortcut
|
|
|
|
{keyCombination}
|
|
|
|
on:action={(event) => {
|
|
|
|
makeBold();
|
|
|
|
updateState(event);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</WithState>
|