2021-10-18 14:01:15 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
|
|
|
<script context="module" lang="ts">
|
2022-01-24 02:43:09 +01:00
|
|
|
import type { EditingInputAPI } from "../EditingArea.svelte";
|
|
|
|
import type { CodeMirror as CodeMirrorType } from "../code-mirror";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
export interface PlainTextInputAPI extends EditingInputAPI {
|
|
|
|
name: "plain-text";
|
|
|
|
moveCaretToEnd(): void;
|
|
|
|
toggle(): boolean;
|
2022-01-12 05:51:43 +01:00
|
|
|
getEditor(): CodeMirrorType.Editor;
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
2022-01-08 02:46:01 +01:00
|
|
|
|
|
|
|
export const parsingInstructions: string[] = [];
|
2021-10-18 14:01:15 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import { tick, onMount } from "svelte";
|
|
|
|
import { writable } from "svelte/store";
|
2022-01-24 02:43:09 +01:00
|
|
|
import { pageTheme } from "../../sveltelib/theme";
|
|
|
|
import { getDecoratedElements } from "../DecoratedElements.svelte";
|
|
|
|
import { getEditingArea } from "../EditingArea.svelte";
|
|
|
|
import CodeMirror from "../CodeMirror.svelte";
|
|
|
|
import type { CodeMirrorAPI } from "../CodeMirror.svelte";
|
|
|
|
import { htmlanki, baseOptions, gutterOptions } from "../code-mirror";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
export let hidden = false;
|
|
|
|
|
|
|
|
const configuration = {
|
|
|
|
mode: htmlanki,
|
|
|
|
...baseOptions,
|
|
|
|
...gutterOptions,
|
|
|
|
};
|
|
|
|
|
|
|
|
const { editingInputs, content } = getEditingArea();
|
|
|
|
const decoratedElements = getDecoratedElements();
|
|
|
|
const code = writable($content);
|
|
|
|
|
|
|
|
function adjustInputHTML(html: string): string {
|
|
|
|
for (const component of decoratedElements) {
|
|
|
|
html = component.toUndecorated(html);
|
|
|
|
}
|
|
|
|
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parser = new DOMParser();
|
|
|
|
|
2022-01-10 03:51:50 +01:00
|
|
|
function removeTag(element: HTMLElement, tagName: string): void {
|
|
|
|
for (const elem of element.getElementsByTagName(tagName)) {
|
|
|
|
elem.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
function parseAsHTML(html: string): string {
|
2022-01-08 02:46:01 +01:00
|
|
|
const doc = parser.parseFromString(
|
|
|
|
parsingInstructions.join("") + html,
|
|
|
|
"text/html",
|
|
|
|
);
|
2021-10-18 14:01:15 +02:00
|
|
|
const body = doc.body;
|
|
|
|
|
2022-01-10 03:51:50 +01:00
|
|
|
removeTag(body, "script");
|
|
|
|
removeTag(body, "link");
|
|
|
|
removeTag(body, "style");
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
return doc.body.innerHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
function adjustOutputHTML(html: string): string {
|
|
|
|
for (const component of decoratedElements) {
|
|
|
|
html = component.toStored(html);
|
|
|
|
}
|
|
|
|
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
|
|
|
|
let codeMirror: CodeMirrorAPI;
|
|
|
|
|
|
|
|
function focus(): void {
|
|
|
|
codeMirror?.editor.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
function refocus(): void {
|
|
|
|
(codeMirror?.editor as any).display.input.blur();
|
|
|
|
focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
function moveCaretToEnd(): void {
|
|
|
|
codeMirror?.editor.setCursor(codeMirror.editor.lineCount(), 0);
|
|
|
|
}
|
|
|
|
|
2021-12-13 05:00:35 +01:00
|
|
|
function toggle(): boolean {
|
|
|
|
hidden = !hidden;
|
|
|
|
return hidden;
|
|
|
|
}
|
|
|
|
|
2022-01-12 05:51:43 +01:00
|
|
|
function getEditor(): CodeMirrorType.Editor {
|
2021-12-13 05:00:35 +01:00
|
|
|
return codeMirror?.editor;
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const api = {
|
|
|
|
name: "plain-text",
|
|
|
|
focus,
|
|
|
|
focusable: !hidden,
|
|
|
|
moveCaretToEnd,
|
|
|
|
refocus,
|
2021-12-13 05:00:35 +01:00
|
|
|
toggle,
|
|
|
|
getEditor,
|
2021-10-18 14:01:15 +02:00
|
|
|
} as PlainTextInputAPI;
|
|
|
|
|
|
|
|
function pushUpdate(): void {
|
|
|
|
api.focusable = !hidden;
|
|
|
|
$editingInputs = $editingInputs;
|
|
|
|
}
|
|
|
|
|
|
|
|
function refresh() {
|
|
|
|
codeMirror.editor.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
$: {
|
|
|
|
hidden;
|
|
|
|
tick().then(refresh);
|
|
|
|
pushUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
$editingInputs.push(api);
|
|
|
|
$editingInputs = $editingInputs;
|
|
|
|
|
|
|
|
const unsubscribeFromEditingArea = content.subscribe((value: string): void => {
|
|
|
|
const adjusted = adjustInputHTML(value);
|
|
|
|
code.set(adjusted);
|
|
|
|
});
|
|
|
|
|
|
|
|
const unsubscribeToEditingArea = code.subscribe((value: string): void => {
|
|
|
|
const parsed = parseAsHTML(value);
|
|
|
|
content.set(adjustOutputHTML(parsed));
|
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
unsubscribeFromEditingArea();
|
|
|
|
unsubscribeToEditingArea();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2022-01-10 03:51:50 +01:00
|
|
|
<div
|
|
|
|
class="plain-text-input"
|
|
|
|
class:light-theme={!$pageTheme.isDark}
|
|
|
|
class:hidden
|
|
|
|
on:focusin
|
|
|
|
on:focusout
|
|
|
|
>
|
2021-10-18 14:01:15 +02:00
|
|
|
<CodeMirror
|
|
|
|
{configuration}
|
|
|
|
{code}
|
|
|
|
bind:api={codeMirror}
|
|
|
|
on:change={({ detail: html }) => code.set(parseAsHTML(html))}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
2022-01-08 02:46:01 +01:00
|
|
|
.plain-text-input {
|
|
|
|
overflow-x: hidden;
|
2021-11-23 01:27:32 +01:00
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
:global(.CodeMirror) {
|
|
|
|
border-radius: 0 0 5px 5px;
|
|
|
|
}
|
|
|
|
|
2022-01-10 03:51:50 +01:00
|
|
|
:global(.CodeMirror-lines) {
|
|
|
|
padding: 6px 0;
|
|
|
|
}
|
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
&.hidden {
|
|
|
|
display: none;
|
|
|
|
}
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
2022-01-10 03:51:50 +01:00
|
|
|
|
|
|
|
.light-theme :global(.CodeMirror) {
|
|
|
|
border-top: 1px solid #ddd;
|
|
|
|
}
|
2021-10-18 14:01:15 +02:00
|
|
|
</style>
|