2021-08-08 00:39:40 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2021-10-31 00:29:22 +02:00
|
|
|
import "codemirror/lib/codemirror.css";
|
|
|
|
import "codemirror/addon/fold/foldgutter.css";
|
2022-01-10 03:51:50 +01:00
|
|
|
import "codemirror/theme/monokai.css";
|
2021-08-08 00:39:40 +02:00
|
|
|
import "codemirror/mode/htmlmixed/htmlmixed";
|
|
|
|
import "codemirror/mode/stex/stex";
|
|
|
|
import "codemirror/addon/fold/foldcode";
|
|
|
|
import "codemirror/addon/fold/foldgutter";
|
|
|
|
import "codemirror/addon/fold/xml-fold";
|
2021-11-23 01:27:32 +01:00
|
|
|
import "codemirror/addon/edit/matchtags";
|
2022-10-03 05:14:57 +02:00
|
|
|
import "codemirror/addon/edit/closetag";
|
2021-11-23 01:27:32 +01:00
|
|
|
import "codemirror/addon/display/placeholder";
|
2021-08-08 00:39:40 +02:00
|
|
|
|
2022-02-04 09:36:34 +01:00
|
|
|
import CodeMirror from "codemirror";
|
2022-02-25 02:14:26 +01:00
|
|
|
import type { Readable } from "svelte/store";
|
|
|
|
|
|
|
|
import storeSubscribe from "../sveltelib/store-subscribe";
|
2022-02-04 09:36:34 +01:00
|
|
|
|
2021-08-08 00:39:40 +02:00
|
|
|
export { CodeMirror };
|
|
|
|
|
|
|
|
export const latex = {
|
|
|
|
name: "stex",
|
|
|
|
inMathMode: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const htmlanki = {
|
|
|
|
name: "htmlmixed",
|
|
|
|
tags: {
|
|
|
|
"anki-mathjax": [[null, null, latex]],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-02-25 02:14:26 +01:00
|
|
|
export const lightTheme = "default";
|
|
|
|
export const darkTheme = "monokai";
|
2022-01-10 03:51:50 +01:00
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
export const baseOptions: CodeMirror.EditorConfiguration = {
|
2022-02-25 02:14:26 +01:00
|
|
|
theme: lightTheme,
|
2021-08-08 00:39:40 +02:00
|
|
|
lineWrapping: true,
|
|
|
|
matchTags: { bothTags: true },
|
2021-10-18 14:01:15 +02:00
|
|
|
extraKeys: { Tab: false, "Shift-Tab": false },
|
|
|
|
tabindex: 0,
|
2021-08-08 00:39:40 +02:00
|
|
|
viewportMargin: Infinity,
|
|
|
|
lineWiseCopyCut: false,
|
|
|
|
};
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
export const gutterOptions: CodeMirror.EditorConfiguration = {
|
2021-08-08 00:39:40 +02:00
|
|
|
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
|
|
|
|
lineNumbers: true,
|
|
|
|
foldGutter: true,
|
|
|
|
};
|
2022-01-08 02:46:01 +01:00
|
|
|
|
2022-03-31 15:39:49 +02:00
|
|
|
export function focusAndSetCaret(
|
|
|
|
editor: CodeMirror.Editor,
|
|
|
|
position: CodeMirror.Position = { line: editor.lineCount(), ch: 0 },
|
|
|
|
): void {
|
2022-01-08 02:46:01 +01:00
|
|
|
editor.focus();
|
2022-03-31 15:39:49 +02:00
|
|
|
editor.setCursor(position);
|
2022-01-08 02:46:01 +01:00
|
|
|
}
|
2022-02-25 02:14:26 +01:00
|
|
|
|
|
|
|
interface OpenCodeMirrorOptions {
|
|
|
|
configuration: CodeMirror.EditorConfiguration;
|
|
|
|
resolve(editor: CodeMirror.EditorFromTextArea): void;
|
2022-04-09 05:25:54 +02:00
|
|
|
hidden: boolean;
|
2022-02-25 02:14:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function openCodeMirror(
|
|
|
|
textarea: HTMLTextAreaElement,
|
2022-04-09 05:25:54 +02:00
|
|
|
options: Partial<OpenCodeMirrorOptions>,
|
2022-02-25 02:14:26 +01:00
|
|
|
): { update: (options: Partial<OpenCodeMirrorOptions>) => void; destroy: () => void } {
|
2022-04-09 05:25:54 +02:00
|
|
|
let editor: CodeMirror.EditorFromTextArea | null = null;
|
2022-02-25 02:14:26 +01:00
|
|
|
|
2022-04-09 05:25:54 +02:00
|
|
|
function update({
|
|
|
|
configuration,
|
|
|
|
resolve,
|
|
|
|
hidden,
|
|
|
|
}: Partial<OpenCodeMirrorOptions>): void {
|
|
|
|
if (editor) {
|
2022-02-25 02:14:26 +01:00
|
|
|
for (const key in configuration) {
|
|
|
|
editor.setOption(
|
|
|
|
key as keyof CodeMirror.EditorConfiguration,
|
|
|
|
configuration[key],
|
|
|
|
);
|
|
|
|
}
|
2022-04-09 05:25:54 +02:00
|
|
|
} else if (!hidden) {
|
|
|
|
editor = CodeMirror.fromTextArea(textarea, configuration);
|
|
|
|
resolve?.(editor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update(options);
|
|
|
|
|
|
|
|
return {
|
|
|
|
update,
|
2022-02-25 02:14:26 +01:00
|
|
|
destroy(): void {
|
2022-04-09 05:25:54 +02:00
|
|
|
editor?.toTextArea();
|
|
|
|
editor = null;
|
2022-02-25 02:14:26 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up the contract with the code store and location restoration.
|
|
|
|
*/
|
|
|
|
export function setupCodeMirror(
|
|
|
|
editor: CodeMirror.Editor,
|
|
|
|
code: Readable<string>,
|
|
|
|
): void {
|
|
|
|
const { subscribe, unsubscribe } = storeSubscribe(
|
|
|
|
code,
|
|
|
|
(value: string): void => editor.setValue(value),
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO passing in the tabindex option does not do anything: bug?
|
|
|
|
editor.getInputField().tabIndex = 0;
|
|
|
|
|
|
|
|
let ranges: CodeMirror.Range[] | null = null;
|
|
|
|
|
|
|
|
editor.on("focus", () => {
|
|
|
|
if (ranges) {
|
|
|
|
try {
|
|
|
|
editor.setSelections(ranges);
|
|
|
|
} catch {
|
|
|
|
ranges = null;
|
|
|
|
editor.setCursor(editor.lineCount(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsubscribe();
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.on("mousedown", () => {
|
|
|
|
// Prevent focus restoring location
|
|
|
|
ranges = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
editor.on("blur", () => {
|
|
|
|
ranges = editor.listSelections();
|
|
|
|
subscribe();
|
|
|
|
});
|
|
|
|
|
|
|
|
subscribe();
|
|
|
|
}
|