anki/ts/editor/MathjaxHandleEditor.svelte

76 lines
2.0 KiB
Svelte
Raw Normal View History

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="typescript">
import { onMount, createEventDispatcher } from "svelte";
import { ChangeTimer } from "./change-timer";
import { CodeMirror, latex, baseOptions } from "./codeMirror";
export let initialValue: string;
const codeMirrorOptions = {
mode: latex,
...baseOptions,
};
2021-08-08 00:13:16 +02:00
let codeMirror: CodeMirror.EditorFromTextArea;
const changeTimer = new ChangeTimer();
const dispatch = createEventDispatcher();
2021-08-06 18:21:23 +02:00
function onInput() {
changeTimer.schedule(
() => dispatch("update", { mathjax: codeMirror.getValue() }),
400
);
2021-08-06 18:21:23 +02:00
}
function onBlur() {
changeTimer.fireImmediately();
}
function openCodemirror(textarea: HTMLTextAreaElement): void {
codeMirror = CodeMirror.fromTextArea(textarea, codeMirrorOptions);
2021-08-06 18:21:23 +02:00
codeMirror.on("change", onInput);
codeMirror.on("blur", onBlur);
}
let textarea: HTMLTextAreaElement;
2021-08-06 18:21:23 +02:00
onMount(() => {
codeMirror.focus();
codeMirror.setCursor(codeMirror.lineCount(), 0);
const codeMirrorElement = textarea.nextElementSibling!;
codeMirrorElement.classList.add("mathjax-editor");
2021-08-06 18:21:23 +02:00
});
</script>
2021-08-06 18:21:23 +02:00
<div
on:click|stopPropagation
on:focus|stopPropagation
on:focusin|stopPropagation
2021-08-06 18:21:23 +02:00
on:keydown|stopPropagation
on:keyup|stopPropagation
on:mousedown|preventDefault|stopPropagation
2021-08-06 18:21:23 +02:00
on:mouseup|stopPropagation
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 -->
<textarea
bind:this={textarea}
value={initialValue}
on:input={onInput}
2021-08-06 18:21:23 +02:00
use:openCodemirror
/>
</div>
<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>