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-22 13:17:22 +01:00
|
|
|
import { context as noteEditorContext } from "../NoteEditor.svelte";
|
2022-02-03 05:52:11 +01:00
|
|
|
import { editingInputIsRichText } from "../rich-text-input";
|
2022-02-22 13:17:22 +01:00
|
|
|
import { Surrounder } from "../surround";
|
|
|
|
import { context as editorToolbarContext } from "./EditorToolbar.svelte";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { underlineIcon } from "./icons";
|
2021-11-18 10:18:39 +01:00
|
|
|
|
2022-02-22 13:17:22 +01:00
|
|
|
const surroundElement = document.createElement("u");
|
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 === "U") {
|
2022-02-22 13:17:22 +01:00
|
|
|
return match.remove();
|
2021-11-18 10:18:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 13:17:22 +01:00
|
|
|
const clearer = () => false;
|
|
|
|
|
|
|
|
const format = {
|
|
|
|
surroundElement,
|
|
|
|
matcher,
|
|
|
|
clearer,
|
|
|
|
};
|
|
|
|
|
|
|
|
const namedFormat = {
|
|
|
|
name: tr.editingUnderlineText(),
|
|
|
|
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]);
|
|
|
|
|
|
|
|
const { focusedInput } = noteEditorContext.get();
|
|
|
|
const surrounder = Surrounder.make();
|
|
|
|
let disabled: boolean;
|
|
|
|
|
|
|
|
$: 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 makeUnderline(): void {
|
2022-02-22 13:17:22 +01:00
|
|
|
surrounder.surround(format);
|
2021-11-18 10:18:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const keyCombination = "Control+U";
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<WithState
|
|
|
|
key="underline"
|
|
|
|
update={updateStateFromActiveInput}
|
|
|
|
let:state={active}
|
|
|
|
let:updateState
|
|
|
|
>
|
|
|
|
<IconButton
|
|
|
|
tooltip="{tr.editingUnderlineText()} ({getPlatformString(keyCombination)})"
|
|
|
|
{active}
|
|
|
|
{disabled}
|
|
|
|
on:click={(event) => {
|
|
|
|
makeUnderline();
|
|
|
|
updateState(event);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{@html underlineIcon}
|
|
|
|
</IconButton>
|
|
|
|
|
|
|
|
<Shortcut
|
|
|
|
{keyCombination}
|
|
|
|
on:action={(event) => {
|
|
|
|
makeUnderline();
|
|
|
|
updateState(event);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</WithState>
|