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-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-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-08-06 16:52:34 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 17:40:27 +02:00
|
|
|
const dispatch = createEventDispatcher();
|
|
|
|
let textarea: HTMLTextAreaElement;
|
|
|
|
|
2021-08-06 18:21:23 +02:00
|
|
|
onMount(() => {
|
|
|
|
codeMirror.focus();
|
|
|
|
codeMirror.setCursor(codeMirror.lineCount(), 0);
|
|
|
|
});
|
2021-08-06 16:52:34 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<!-- <textarea bind:value use:openCodemirror /> -->
|
2021-08-06 18:21:23 +02:00
|
|
|
<div
|
|
|
|
on:click|stopPropagation
|
|
|
|
on:focus|stopPropagation
|
|
|
|
on:keydown|stopPropagation
|
|
|
|
on:focusin|stopPropagation
|
|
|
|
on:keyup|stopPropagation
|
|
|
|
on:mouseup|stopPropagation
|
|
|
|
>
|
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}
|
|
|
|
on:mouseup|preventDefault|stopPropagation
|
2021-08-06 17:40:27 +02:00
|
|
|
on:keyup|stopPropagation
|
2021-08-06 16:52:34 +02:00
|
|
|
on:click|stopPropagation
|
|
|
|
on:focusin|stopPropagation
|
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>
|