2021-08-06 16:52:34 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
|
|
|
<script lang="typescript">
|
2021-08-06 17:40:27 +02:00
|
|
|
import { onMount, createEventDispatcher } from "svelte";
|
2021-08-07 01:59:28 +02:00
|
|
|
import { ChangeTimer } from "./change-timer";
|
2021-08-08 00:39:40 +02:00
|
|
|
import { CodeMirror, latex, baseOptions } from "./codeMirror";
|
2021-08-06 16:52:34 +02:00
|
|
|
|
|
|
|
export let initialValue: string;
|
|
|
|
|
|
|
|
const codeMirrorOptions = {
|
|
|
|
mode: latex,
|
2021-08-08 00:39:40 +02:00
|
|
|
...baseOptions,
|
2021-08-06 16:52:34 +02:00
|
|
|
};
|
|
|
|
|
2021-08-08 00:13:16 +02:00
|
|
|
let codeMirror: CodeMirror.EditorFromTextArea;
|
2021-08-07 01:59:28 +02:00
|
|
|
const changeTimer = new ChangeTimer();
|
2021-09-16 14:47:05 +02:00
|
|
|
const dispatch = createEventDispatcher();
|
2021-08-06 16:52:34 +02:00
|
|
|
|
2021-08-06 18:21:23 +02:00
|
|
|
function onInput() {
|
2021-08-07 01:59:28 +02:00
|
|
|
changeTimer.schedule(
|
|
|
|
() => dispatch("update", { mathjax: codeMirror.getValue() }),
|
|
|
|
400
|
|
|
|
);
|
2021-08-06 18:21:23 +02:00
|
|
|
}
|
|
|
|
|
2021-09-16 14:47:05 +02:00
|
|
|
function onBlur() {
|
|
|
|
changeTimer.fireImmediately();
|
|
|
|
}
|
|
|
|
|
2021-08-06 16:52:34 +02:00
|
|
|
function openCodemirror(textarea: HTMLTextAreaElement): void {
|
|
|
|
codeMirror = CodeMirror.fromTextArea(textarea, codeMirrorOptions);
|
2021-08-06 18:21:23 +02:00
|
|
|
codeMirror.on("change", onInput);
|
2021-09-16 14:47:05 +02:00
|
|
|
codeMirror.on("blur", onBlur);
|
2021-08-06 16:52:34 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 17:40:27 +02:00
|
|
|
let textarea: HTMLTextAreaElement;
|
|
|
|
|
2021-08-06 18:21:23 +02:00
|
|
|
onMount(() => {
|
|
|
|
codeMirror.focus();
|
|
|
|
codeMirror.setCursor(codeMirror.lineCount(), 0);
|
2021-09-09 15:06:07 +02:00
|
|
|
|
|
|
|
const codeMirrorElement = textarea.nextElementSibling!;
|
|
|
|
codeMirrorElement.classList.add("mathjax-editor");
|
2021-08-06 18:21:23 +02:00
|
|
|
});
|
2021-08-06 16:52:34 +02:00
|
|
|
</script>
|
|
|
|
|
2021-08-06 18:21:23 +02:00
|
|
|
<div
|
|
|
|
on:click|stopPropagation
|
|
|
|
on:focus|stopPropagation
|
2021-09-15 22:14:38 +02:00
|
|
|
on:focusin|stopPropagation
|
2021-08-06 18:21:23 +02:00
|
|
|
on:keydown|stopPropagation
|
|
|
|
on:keyup|stopPropagation
|
2021-09-15 17:56:56 +02:00
|
|
|
on:mousedown|preventDefault|stopPropagation
|
2021-08-06 18:21:23 +02:00
|
|
|
on:mouseup|stopPropagation
|
2021-09-15 22:14:38 +02:00
|
|
|
on:paste|stopPropagation
|
2021-08-06 18:21:23 +02:00
|
|
|
>
|
2021-08-06 17:21:47 +02:00
|
|
|
<!-- TODO no focusin for now, as EditingArea will defer to Editable/Codable -->
|
2021-08-06 16:52:34 +02:00
|
|
|
<textarea
|
2021-08-06 17:40:27 +02:00
|
|
|
bind:this={textarea}
|
2021-08-06 16:52:34 +02:00
|
|
|
value={initialValue}
|
2021-08-06 17:40:27 +02:00
|
|
|
on:input={onInput}
|
2021-08-06 18:21:23 +02:00
|
|
|
use:openCodemirror
|
2021-08-06 16:52:34 +02:00
|
|
|
/>
|
|
|
|
</div>
|
2021-09-09 15:06:07 +02:00
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
/* TODO there is global CSS in fields.scss */
|
|
|
|
div :global(.mathjax-editor) {
|
|
|
|
border-radius: 0;
|
|
|
|
border-width: 0 1px;
|
|
|
|
border-color: var(--medium-border);
|
|
|
|
}
|
|
|
|
</style>
|