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
|
|
|
|
-->
|
2021-10-26 00:43:02 +02:00
|
|
|
<script context="module" lang="ts">
|
2021-10-18 14:01:15 +02:00
|
|
|
import { CodeMirror as CodeMirrorLib } from "./code-mirror";
|
|
|
|
|
|
|
|
export interface CodeMirrorAPI {
|
|
|
|
readonly editor: CodeMirrorLib.EditorFromTextArea;
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2021-10-26 00:43:02 +02:00
|
|
|
<script lang="ts">
|
2021-10-23 03:06:15 +02:00
|
|
|
import { createEventDispatcher, getContext } from "svelte";
|
2021-10-18 14:01:15 +02:00
|
|
|
import type { Writable } from "svelte/store";
|
|
|
|
import storeSubscribe from "../sveltelib/store-subscribe";
|
2021-10-23 03:06:15 +02:00
|
|
|
import { directionKey } from "../lib/context-keys";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
export let configuration: CodeMirror.EditorConfiguration;
|
|
|
|
export let code: Writable<string>;
|
|
|
|
|
2021-10-23 03:06:15 +02:00
|
|
|
const direction = getContext<Writable<"ltr" | "rtl">>(directionKey);
|
|
|
|
const defaultConfiguration = {
|
|
|
|
direction: $direction,
|
|
|
|
rtlMoveVisually: true,
|
|
|
|
};
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
let codeMirror: CodeMirror.EditorFromTextArea;
|
2021-10-23 03:06:15 +02:00
|
|
|
$: codeMirror?.setOption("direction", $direction);
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
function setValue(content: string): void {
|
|
|
|
codeMirror.setValue(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { subscribe, unsubscribe } = storeSubscribe(code, setValue, false);
|
|
|
|
const dispatch = createEventDispatcher();
|
|
|
|
|
|
|
|
function openCodeMirror(textarea: HTMLTextAreaElement): void {
|
2021-10-23 03:06:15 +02:00
|
|
|
codeMirror = CodeMirrorLib.fromTextArea(textarea, {
|
|
|
|
...defaultConfiguration,
|
|
|
|
...configuration,
|
|
|
|
});
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
// TODO passing in the tabindex option does not do anything: bug?
|
|
|
|
codeMirror.getInputField().tabIndex = 0;
|
|
|
|
|
|
|
|
codeMirror.on("change", () => dispatch("change", codeMirror.getValue()));
|
|
|
|
codeMirror.on("focus", () => {
|
|
|
|
unsubscribe();
|
|
|
|
dispatch("focus");
|
|
|
|
});
|
|
|
|
codeMirror.on("blur", () => {
|
|
|
|
subscribe();
|
|
|
|
dispatch("blur");
|
|
|
|
});
|
|
|
|
|
|
|
|
subscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
export const api = Object.create(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
editor: { get: () => codeMirror },
|
2021-10-19 01:06:00 +02:00
|
|
|
},
|
2021-10-18 14:01:15 +02:00
|
|
|
) as CodeMirrorAPI;
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<div class="code-mirror">
|
|
|
|
<textarea tabindex="-1" hidden use:openCodeMirror />
|
|
|
|
</div>
|