2021-11-23 01:27:32 +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-02-25 02:14:26 +01:00
|
|
|
import type CodeMirrorLib from "codemirror";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { createEventDispatcher, onMount } from "svelte";
|
2021-11-23 01:27:32 +01:00
|
|
|
import type { Writable } from "svelte/store";
|
2022-02-04 09:36:34 +01:00
|
|
|
|
2021-11-23 01:27:32 +01:00
|
|
|
import * as tr from "../../lib/ftl";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { noop } from "../../lib/functional";
|
|
|
|
import { getPlatformString } from "../../lib/shortcuts";
|
|
|
|
import { baseOptions, focusAndCaretAfter, latex } from "../code-mirror";
|
|
|
|
import type { CodeMirrorAPI } from "../CodeMirror.svelte";
|
|
|
|
import CodeMirror from "../CodeMirror.svelte";
|
2021-11-23 01:27:32 +01:00
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
export let code: Writable<string>;
|
2021-11-23 01:27:32 +01:00
|
|
|
export let acceptShortcut: string;
|
|
|
|
export let newlineShortcut: string;
|
|
|
|
|
|
|
|
const configuration = {
|
|
|
|
...Object.assign({}, baseOptions, {
|
|
|
|
extraKeys: {
|
2022-02-25 02:14:26 +01:00
|
|
|
...(baseOptions.extraKeys as CodeMirrorLib.KeyMap),
|
2021-11-23 01:27:32 +01:00
|
|
|
[acceptShortcut]: noop,
|
|
|
|
[newlineShortcut]: noop,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
placeholder: tr.editingMathjaxPlaceholder({
|
|
|
|
accept: getPlatformString(acceptShortcut),
|
|
|
|
newline: getPlatformString(newlineShortcut),
|
|
|
|
}),
|
|
|
|
mode: latex,
|
|
|
|
};
|
2022-01-08 02:46:01 +01:00
|
|
|
|
|
|
|
export let selectAll: boolean;
|
|
|
|
|
|
|
|
const dispatch = createEventDispatcher();
|
|
|
|
|
2022-02-25 02:14:26 +01:00
|
|
|
let codeMirror = {} as CodeMirrorAPI;
|
2022-01-08 02:46:01 +01:00
|
|
|
|
2022-02-25 02:14:26 +01:00
|
|
|
onMount(() =>
|
|
|
|
codeMirror.editor.then((editor) => {
|
|
|
|
focusAndCaretAfter(editor);
|
2022-01-08 02:46:01 +01:00
|
|
|
|
2022-02-25 02:14:26 +01:00
|
|
|
if (selectAll) {
|
|
|
|
editor.execCommand("selectAll");
|
2022-01-08 02:46:01 +01:00
|
|
|
}
|
|
|
|
|
2022-02-25 02:14:26 +01:00
|
|
|
let direction: "start" | "end" | undefined = undefined;
|
2022-01-08 02:46:01 +01:00
|
|
|
|
2022-02-25 02:14:26 +01:00
|
|
|
editor.on(
|
|
|
|
"keydown",
|
|
|
|
(_instance: CodeMirrorLib.Editor, event: KeyboardEvent): void => {
|
|
|
|
if (event.key === "ArrowLeft") {
|
|
|
|
direction = "start";
|
|
|
|
} else if (event.key === "ArrowRight") {
|
|
|
|
direction = "end";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
editor.on(
|
|
|
|
"beforeSelectionChange",
|
|
|
|
(
|
|
|
|
instance: CodeMirrorLib.Editor,
|
|
|
|
obj: CodeMirrorLib.EditorSelectionChange,
|
|
|
|
): void => {
|
|
|
|
const { anchor } = obj.ranges[0];
|
|
|
|
|
|
|
|
if (anchor["hitSide"]) {
|
|
|
|
if (instance.getValue().length === 0) {
|
|
|
|
if (direction) {
|
|
|
|
dispatch(`moveout${direction}`);
|
|
|
|
}
|
|
|
|
} else if (anchor.line === 0 && anchor.ch === 0) {
|
|
|
|
dispatch("moveoutstart");
|
|
|
|
} else {
|
|
|
|
dispatch("moveoutend");
|
2022-01-08 02:46:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 02:14:26 +01:00
|
|
|
direction = undefined;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape characters which are technically legal in Mathjax, but confuse HTML.
|
|
|
|
*/
|
|
|
|
export function escapeSomeEntities(value: string): string {
|
|
|
|
return value.replace(/</g, "{\\lt}").replace(/>/g, "{\\gt}");
|
|
|
|
}
|
2021-11-23 01:27:32 +01:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<div class="mathjax-editor">
|
|
|
|
<CodeMirror
|
|
|
|
{code}
|
|
|
|
{configuration}
|
2022-01-08 02:46:01 +01:00
|
|
|
bind:api={codeMirror}
|
2022-02-25 02:14:26 +01:00
|
|
|
on:change={({ detail }) => code.set(escapeSomeEntities(detail))}
|
2021-11-23 01:27:32 +01:00
|
|
|
on:blur
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.mathjax-editor {
|
|
|
|
:global(.CodeMirror) {
|
|
|
|
max-width: 28rem;
|
|
|
|
min-width: 14rem;
|
|
|
|
margin-bottom: 0.25rem;
|
|
|
|
}
|
|
|
|
|
|
|
|
:global(.CodeMirror-placeholder) {
|
|
|
|
font-family: sans-serif;
|
|
|
|
font-size: 55%;
|
|
|
|
text-align: center;
|
|
|
|
color: var(--slightly-grey-text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|