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-05-13 04:57:07 +02:00
|
|
|
import { removeStyleProperties } from "../../lib/styling";
|
2022-02-03 05:52:11 +01:00
|
|
|
import { context as noteEditorContext } from "../NoteEditor.svelte";
|
|
|
|
import { editingInputIsRichText } from "../rich-text-input";
|
2022-05-13 04:57:07 +02:00
|
|
|
import { Surrounder } from "../surround";
|
2022-02-22 13:17:22 +01:00
|
|
|
import { context as editorToolbarContext } from "./EditorToolbar.svelte";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { italicIcon } from "./icons";
|
2021-11-18 10:18:39 +01:00
|
|
|
|
2022-04-19 08:28:44 +02:00
|
|
|
const surroundElement = document.createElement("i");
|
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 === "I" || element.tagName === "EM") {
|
2022-02-22 13:17:22 +01:00
|
|
|
return match.remove();
|
2021-11-18 10:18:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (["italic", "oblique"].includes(element.style.fontStyle)) {
|
2022-02-22 13:17:22 +01:00
|
|
|
return match.clear((): void => {
|
2022-05-13 04:57:07 +02:00
|
|
|
if (
|
|
|
|
removeStyleProperties(element, "font-style") &&
|
|
|
|
element.className.length === 0
|
|
|
|
) {
|
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 format = {
|
|
|
|
surroundElement,
|
|
|
|
matcher,
|
|
|
|
};
|
2021-11-18 10:18:39 +01:00
|
|
|
|
2022-02-22 13:17:22 +01:00
|
|
|
const namedFormat = {
|
|
|
|
name: tr.editingItalicText(),
|
|
|
|
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 makeItalic(): void {
|
2022-02-22 13:17:22 +01:00
|
|
|
surrounder.surround(format);
|
2021-11-18 10:18:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const keyCombination = "Control+I";
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<WithState
|
|
|
|
key="italic"
|
|
|
|
update={updateStateFromActiveInput}
|
|
|
|
let:state={active}
|
|
|
|
let:updateState
|
|
|
|
>
|
|
|
|
<IconButton
|
|
|
|
tooltip="{tr.editingItalicText()} ({getPlatformString(keyCombination)})"
|
|
|
|
{active}
|
|
|
|
{disabled}
|
|
|
|
on:click={(event) => {
|
|
|
|
makeItalic();
|
|
|
|
updateState(event);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{@html italicIcon}
|
|
|
|
</IconButton>
|
|
|
|
|
|
|
|
<Shortcut
|
|
|
|
{keyCombination}
|
|
|
|
on:action={(event) => {
|
|
|
|
makeItalic();
|
|
|
|
updateState(event);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</WithState>
|