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-02-25 02:14:26 +01:00
|
|
|
import { registerPackage } from "../../lib/runtime-require";
|
|
|
|
import lifecycleHooks from "../../sveltelib/lifecycle-hooks";
|
|
|
|
import type { CodeMirrorAPI } from "../CodeMirror.svelte";
|
2022-02-04 09:36:34 +01:00
|
|
|
import type { EditingInputAPI } from "../EditingArea.svelte";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
export interface PlainTextInputAPI extends EditingInputAPI {
|
|
|
|
name: "plain-text";
|
|
|
|
moveCaretToEnd(): void;
|
|
|
|
toggle(): boolean;
|
2022-02-25 02:14:26 +01:00
|
|
|
codeMirror: CodeMirrorAPI;
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
2022-01-08 02:46:01 +01:00
|
|
|
|
|
|
|
export const parsingInstructions: string[] = [];
|
2022-02-25 02:14:26 +01:00
|
|
|
|
|
|
|
const [lifecycle, instances, setupLifecycleHooks] =
|
|
|
|
lifecycleHooks<PlainTextInputAPI>();
|
|
|
|
|
|
|
|
registerPackage("anki/PlainTextInput", {
|
|
|
|
lifecycle,
|
|
|
|
instances,
|
|
|
|
});
|
2021-10-18 14:01:15 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2022-02-04 09:36:34 +01:00
|
|
|
import { onMount, tick } from "svelte";
|
2021-10-18 14:01:15 +02:00
|
|
|
import { writable } from "svelte/store";
|
2022-02-04 09:36:34 +01:00
|
|
|
|
2022-01-24 02:43:09 +01:00
|
|
|
import { pageTheme } from "../../sveltelib/theme";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { baseOptions, gutterOptions, htmlanki } from "../code-mirror";
|
|
|
|
import CodeMirror from "../CodeMirror.svelte";
|
|
|
|
import { context as decoratedElementsContext } from "../DecoratedElements.svelte";
|
|
|
|
import { context as editingAreaContext } from "../EditingArea.svelte";
|
2022-02-25 01:59:06 +01:00
|
|
|
import { context as noteEditorContext } from "../NoteEditor.svelte";
|
2022-02-25 02:14:26 +01:00
|
|
|
import removeProhibitedTags from "./remove-prohibited";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
export let hidden = false;
|
|
|
|
|
|
|
|
const configuration = {
|
|
|
|
mode: htmlanki,
|
|
|
|
...baseOptions,
|
|
|
|
...gutterOptions,
|
|
|
|
};
|
|
|
|
|
2022-02-25 01:59:06 +01:00
|
|
|
const { focusedInput } = noteEditorContext.get();
|
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
const { editingInputs, content } = editingAreaContext.get();
|
|
|
|
const decoratedElements = decoratedElementsContext.get();
|
2021-10-18 14:01:15 +02:00
|
|
|
const code = writable($content);
|
|
|
|
|
|
|
|
function focus(): void {
|
2022-02-25 02:14:26 +01:00
|
|
|
codeMirror.editor.then((editor) => editor.focus());
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
2022-02-05 06:58:31 +01:00
|
|
|
function moveCaretToEnd(): void {
|
2022-02-25 02:14:26 +01:00
|
|
|
codeMirror.editor.then((editor) => editor.setCursor(editor.lineCount(), 0));
|
2022-02-05 06:58:31 +01:00
|
|
|
}
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
function refocus(): void {
|
2022-02-25 02:14:26 +01:00
|
|
|
codeMirror.editor.then((editor) => (editor as any).display.input.blur());
|
2021-10-18 14:01:15 +02:00
|
|
|
focus();
|
2022-02-05 06:58:31 +01:00
|
|
|
moveCaretToEnd();
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
2021-12-13 05:00:35 +01:00
|
|
|
function toggle(): boolean {
|
|
|
|
hidden = !hidden;
|
|
|
|
return hidden;
|
|
|
|
}
|
|
|
|
|
2022-02-25 02:14:26 +01:00
|
|
|
let codeMirror = {} as CodeMirrorAPI;
|
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,
|
2022-02-25 02:14:26 +01:00
|
|
|
codeMirror,
|
2021-10-18 14:01:15 +02:00
|
|
|
} as PlainTextInputAPI;
|
|
|
|
|
|
|
|
function pushUpdate(): void {
|
|
|
|
api.focusable = !hidden;
|
|
|
|
$editingInputs = $editingInputs;
|
|
|
|
}
|
|
|
|
|
2022-02-25 02:14:26 +01:00
|
|
|
function refresh(): void {
|
|
|
|
codeMirror.editor.then((editor) => editor.refresh());
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$: {
|
|
|
|
hidden;
|
|
|
|
tick().then(refresh);
|
|
|
|
pushUpdate();
|
|
|
|
}
|
|
|
|
|
2022-02-25 02:14:26 +01:00
|
|
|
function storedToUndecorated(html: string): string {
|
|
|
|
return decoratedElements.toUndecorated(html);
|
|
|
|
}
|
|
|
|
|
|
|
|
function undecoratedToStored(html: string): string {
|
|
|
|
return decoratedElements.toStored(html);
|
|
|
|
}
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
onMount(() => {
|
|
|
|
$editingInputs.push(api);
|
|
|
|
$editingInputs = $editingInputs;
|
|
|
|
|
|
|
|
const unsubscribeFromEditingArea = content.subscribe((value: string): void => {
|
2022-02-25 02:14:26 +01:00
|
|
|
code.set(storedToUndecorated(value));
|
2021-10-18 14:01:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const unsubscribeToEditingArea = code.subscribe((value: string): void => {
|
2022-02-25 02:14:26 +01:00
|
|
|
content.set(removeProhibitedTags(undecoratedToStored(value)));
|
2021-10-18 14:01:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
unsubscribeFromEditingArea();
|
|
|
|
unsubscribeToEditingArea();
|
|
|
|
};
|
|
|
|
});
|
2022-02-25 02:14:26 +01:00
|
|
|
|
|
|
|
setupLifecycleHooks(api);
|
2021-10-18 14:01:15 +02:00
|
|
|
</script>
|
|
|
|
|
2022-01-10 03:51:50 +01:00
|
|
|
<div
|
|
|
|
class="plain-text-input"
|
|
|
|
class:light-theme={!$pageTheme.isDark}
|
|
|
|
class:hidden
|
2022-02-25 01:59:06 +01:00
|
|
|
on:focusin={() => ($focusedInput = api)}
|
2022-01-10 03:51:50 +01:00
|
|
|
>
|
2021-10-18 14:01:15 +02:00
|
|
|
<CodeMirror
|
|
|
|
{configuration}
|
|
|
|
{code}
|
|
|
|
bind:api={codeMirror}
|
2022-02-25 02:14:26 +01:00
|
|
|
on:change={({ detail: html }) => code.set(removeProhibitedTags(html))}
|
2021-10-18 14:01:15 +02:00
|
|
|
/>
|
|
|
|
</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>
|