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 { underlineIcon } from "./icons";
|
|
|
|
import { getNoteEditor } from "./OldEditorAdapter.svelte";
|
|
|
|
import type { RichTextInputAPI } from "./RichTextInput.svelte";
|
|
|
|
|
2021-11-24 01:33:14 +01:00
|
|
|
function matchUnderline(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 === "U") {
|
|
|
|
return MatchResult.MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MatchResult.NO_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
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> {
|
|
|
|
return !input || input.name === "plain-text"
|
|
|
|
? Promise.resolve(false)
|
2021-11-24 01:33:14 +01:00
|
|
|
: surrounder!.isSurrounded(matchUnderline);
|
2021-11-18 10:18:39 +01:00
|
|
|
}
|
|
|
|
|
2021-11-24 01:33:14 +01:00
|
|
|
const element = document.createElement("u");
|
2021-11-18 10:18:39 +01:00
|
|
|
function makeUnderline(): void {
|
2021-11-24 01:33:14 +01:00
|
|
|
surrounder!.surroundCommand(element, matchUnderline);
|
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>
|