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">
|
2022-01-08 02:46:01 +01:00
|
|
|
import { createEventDispatcher, getContext } from "svelte";
|
2021-10-18 14:01:15 +02:00
|
|
|
import type { Writable } from "svelte/store";
|
2022-02-04 09:36:34 +01:00
|
|
|
|
2021-10-23 03:06:15 +02:00
|
|
|
import { directionKey } from "../lib/context-keys";
|
2022-02-04 09:36:34 +01:00
|
|
|
import storeSubscribe from "../sveltelib/store-subscribe";
|
2022-01-10 03:51:50 +01:00
|
|
|
import { pageTheme } from "../sveltelib/theme";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { darkCodeMirrorTheme, lightCodeMirrorTheme } from "./code-mirror";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
2022-02-04 09:36:34 +01:00
|
|
|
export let configuration: CodeMirrorLib.EditorConfiguration;
|
2021-10-18 14:01:15 +02:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2022-02-04 09:36:34 +01:00
|
|
|
let codeMirror: CodeMirrorLib.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;
|
|
|
|
|
2021-11-05 02:29:02 +01:00
|
|
|
let ranges: CodeMirrorLib.Range[] | null = null;
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
codeMirror.on("change", () => dispatch("change", codeMirror.getValue()));
|
2021-11-05 02:29:02 +01:00
|
|
|
codeMirror.on("mousedown", () => {
|
|
|
|
ranges = null;
|
|
|
|
});
|
2021-10-18 14:01:15 +02:00
|
|
|
codeMirror.on("focus", () => {
|
2021-11-05 02:29:02 +01:00
|
|
|
if (ranges) {
|
2021-11-23 01:27:32 +01:00
|
|
|
try {
|
|
|
|
codeMirror.setSelections(ranges);
|
|
|
|
} catch {
|
|
|
|
ranges = null;
|
2021-12-03 12:41:12 +01:00
|
|
|
codeMirror.setCursor(codeMirror.lineCount(), 0);
|
2021-11-23 01:27:32 +01:00
|
|
|
}
|
2021-11-05 02:29:02 +01:00
|
|
|
}
|
2021-10-18 14:01:15 +02:00
|
|
|
unsubscribe();
|
2021-11-23 01:27:32 +01:00
|
|
|
dispatch("focus");
|
2021-10-18 14:01:15 +02:00
|
|
|
});
|
|
|
|
codeMirror.on("blur", () => {
|
2021-11-05 02:29:02 +01:00
|
|
|
ranges = codeMirror.listSelections();
|
2021-10-18 14:01:15 +02:00
|
|
|
subscribe();
|
2021-11-23 01:27:32 +01:00
|
|
|
dispatch("blur");
|
2021-10-18 14:01:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
subscribe();
|
|
|
|
}
|
|
|
|
|
2022-01-10 03:51:50 +01:00
|
|
|
$: codeMirror?.setOption(
|
|
|
|
"theme",
|
|
|
|
$pageTheme.isDark ? darkCodeMirrorTheme : lightCodeMirrorTheme,
|
|
|
|
);
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
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>
|
2021-10-31 00:29:22 +02:00
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.code-mirror :global(.CodeMirror) {
|
|
|
|
height: auto;
|
|
|
|
}
|
|
|
|
</style>
|