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">
|
2022-01-24 02:43:09 +01:00
|
|
|
import IconButton from "../../components/IconButton.svelte";
|
|
|
|
import Shortcut from "../../components/Shortcut.svelte";
|
|
|
|
import WithState from "../../components/WithState.svelte";
|
2022-02-22 13:17:22 +01:00
|
|
|
import type { MatchType } from "../../domlib/surround";
|
2022-02-04 09:36:34 +01:00
|
|
|
import * as tr from "../../lib/ftl";
|
2022-01-24 02:43:09 +01:00
|
|
|
import { getPlatformString } from "../../lib/shortcuts";
|
2022-02-03 05:52:11 +01:00
|
|
|
import { context as noteEditorContext } from "../NoteEditor.svelte";
|
|
|
|
import { editingInputIsRichText } from "../rich-text-input";
|
2022-02-22 13:17:22 +01:00
|
|
|
import { removeEmptyStyle, Surrounder } from "../surround";
|
|
|
|
import { context as editorToolbarContext } from "./EditorToolbar.svelte";
|
2021-11-18 10:18:39 +01:00
|
|
|
import { boldIcon } from "./icons";
|
|
|
|
|
2022-02-22 13:17:22 +01:00
|
|
|
const surroundElement = document.createElement("strong");
|
2021-11-18 10:18:39 +01:00
|
|
|
|
2022-02-22 13:17:22 +01:00
|
|
|
function matcher(element: HTMLElement | SVGElement, match: MatchType): void {
|
2021-11-18 10:18:39 +01:00
|
|
|
if (element.tagName === "B" || element.tagName === "STRONG") {
|
2022-02-22 13:17:22 +01:00
|
|
|
return match.remove();
|
2021-11-18 10:18:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const fontWeight = element.style.fontWeight;
|
|
|
|
if (fontWeight === "bold" || Number(fontWeight) >= 400) {
|
2022-02-22 13:17:22 +01:00
|
|
|
return match.clear((): void => {
|
|
|
|
element.style.removeProperty("font-weight");
|
2021-11-18 10:18:39 +01:00
|
|
|
|
2022-02-22 13:17:22 +01:00
|
|
|
if (removeEmptyStyle(element) && element.className.length === 0) {
|
|
|
|
match.remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-11-18 10:18:39 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 13:17:22 +01:00
|
|
|
const format = {
|
|
|
|
surroundElement,
|
|
|
|
matcher,
|
|
|
|
};
|
2021-11-18 10:18:39 +01:00
|
|
|
|
2022-02-22 13:17:22 +01:00
|
|
|
const namedFormat = {
|
|
|
|
name: tr.editingBoldText(),
|
|
|
|
show: true,
|
|
|
|
active: true,
|
|
|
|
format,
|
|
|
|
};
|
2021-11-18 10:18:39 +01:00
|
|
|
|
2022-02-22 13:17:22 +01:00
|
|
|
const { removeFormats } = editorToolbarContext.get();
|
|
|
|
removeFormats.update((formats) => [...formats, namedFormat]);
|
2021-11-18 10:18:39 +01:00
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
const { focusedInput } = noteEditorContext.get();
|
2022-02-22 13:17:22 +01:00
|
|
|
const surrounder = Surrounder.make();
|
|
|
|
let disabled: boolean;
|
2021-11-18 10:18:39 +01:00
|
|
|
|
2022-02-22 13:17:22 +01:00
|
|
|
$: if (editingInputIsRichText($focusedInput)) {
|
|
|
|
surrounder.richText = $focusedInput;
|
|
|
|
disabled = false;
|
|
|
|
} else {
|
|
|
|
surrounder.disable();
|
|
|
|
disabled = true;
|
|
|
|
}
|
2021-11-18 10:18:39 +01:00
|
|
|
|
|
|
|
function updateStateFromActiveInput(): Promise<boolean> {
|
2022-02-22 13:17:22 +01:00
|
|
|
return disabled ? Promise.resolve(false) : surrounder.isSurrounded(format);
|
2021-11-18 10:18:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeBold(): void {
|
2022-02-22 13:17:22 +01:00
|
|
|
surrounder.surround(format);
|
2021-11-18 10:18:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const keyCombination = "Control+B";
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<WithState
|
|
|
|
key="bold"
|
|
|
|
update={updateStateFromActiveInput}
|
2022-02-22 13:17:22 +01:00
|
|
|
let:state={active}
|
2021-11-18 10:18:39 +01:00
|
|
|
let:updateState
|
|
|
|
>
|
|
|
|
<IconButton
|
|
|
|
tooltip="{tr.editingBoldText()} ({getPlatformString(keyCombination)})"
|
2022-02-22 13:17:22 +01:00
|
|
|
{active}
|
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>
|