Debian Codegraph @

frontend/src/codemirror/setup.ts

raw
1 import {
2 autocompletion,
3 closeBrackets,
4 closeBracketsKeymap,
5 completionKeymap,
6 } from "@codemirror/autocomplete";
7 import {
8 defaultKeymap,
9 history,
10 historyKeymap,
11 indentWithTab,
12 } from "@codemirror/commands";
13 import type { LanguageSupport } from "@codemirror/language";
14 import {
15 bracketMatching,
16 defaultHighlightStyle,
17 foldGutter,
18 foldKeymap,
19 indentOnInput,
20 indentUnit,
21 syntaxHighlighting,
22 } from "@codemirror/language";
23 import { lintGutter, lintKeymap } from "@codemirror/lint";
24 import { highlightSelectionMatches, searchKeymap } from "@codemirror/search";
25 import type { Extension } from "@codemirror/state";
26 import { EditorState } from "@codemirror/state";
27 import type { KeyBinding } from "@codemirror/view";
28 import {
29 drawSelection,
30 EditorView,
31 highlightActiveLine,
32 highlightSpecialChars,
33 keymap,
34 lineNumbers,
35 placeholder,
36 rectangularSelection,
37 } from "@codemirror/view";
38 import type { Action } from "svelte/action";
39 import { get as store_get } from "svelte/store";
40
41 import { log_error } from "../log";
42 import { fava_options } from "../stores";
43 import { getBeancountLanguageSupport } from "./beancount";
44 import {
45 beancountEditorHighlight,
46 beancountQueryHighlight,
47 } from "./beancount-highlight";
48 import { bql } from "./bql";
49 import { rulerPlugin } from "./ruler";
50
51 const baseExtensions = [
52 lineNumbers(),
53 highlightSpecialChars(),
54 history(),
55 foldGutter(),
56 drawSelection(),
57 EditorState.allowMultipleSelections.of(true),
58 indentOnInput(),
59 bracketMatching(),
60 closeBrackets(),
61 autocompletion(),
62 rectangularSelection(),
63 highlightActiveLine(),
64 highlightSelectionMatches(),
65 lintGutter(),
66 keymap.of([
67 ...closeBracketsKeymap,
68 ...defaultKeymap,
69 ...searchKeymap,
70 ...historyKeymap,
71 ...foldKeymap,
72 ...completionKeymap,
73 ...lintKeymap,
74 indentWithTab,
75 ]),
76 ];
77
78 /** An editor and a function to attach it to a DOM element. */
79 interface EditorAndAction {
80 editor: EditorView;
81 renderEditor: Action<HTMLDivElement | HTMLPreElement>;
82 }
83
84 function setup(
85 value: string | undefined,
86 extensions: Extension[],
87 ): EditorAndAction {
88 const view = new EditorView({
89 state: EditorState.create({ doc: value, extensions }),
90 });
91 return {
92 editor: view,
93 renderEditor: (el) => {
94 el.appendChild(view.dom);
95 },
96 };
97 }
98
99 /**
100 * A basic readonly editor for an asynchronously loaded document.
101 */
102 export function initDocumentPreviewEditor(value: string): EditorAndAction {
103 return setup(value, [
104 baseExtensions,
105 EditorState.readOnly.of(true),
106 placeholder("Loading..."),
107 ]);
108 }
109
110 /**
111 * Read-only editors in the help pages.
112 */
113 export class BeancountTextarea extends HTMLTextAreaElement {
114 constructor() {
115 super();
116 getBeancountLanguageSupport()
117 .then((beancount) => {
118 const { editor } = setup(this.value, [
119 beancount,
120 syntaxHighlighting(defaultHighlightStyle),
121 EditorView.editable.of(false),
122 ]);
123 this.parentNode?.insertBefore(editor.dom, this);
124 this.style.display = "none";
125 })
126 .catch(log_error);
127 }
128 }
129
130 /**
131 * A Beancount editor.
132 */
133 export function initBeancountEditor(
134 value: string,
135 onDocChanges: (s: EditorState) => void,
136 commands: KeyBinding[],
137 beancount: LanguageSupport,
138 ): EditorAndAction {
139 const { indent, currency_column } = store_get(fava_options);
140 return setup(value, [
141 beancount,
142 indentUnit.of(" ".repeat(indent)),
143 ...(currency_column ? [rulerPlugin(currency_column - 1)] : []),
144 keymap.of(commands),
145 EditorView.updateListener.of((update) => {
146 if (update.docChanged) {
147 onDocChanges(update.state);
148 }
149 }),
150 baseExtensions,
151 syntaxHighlighting(beancountEditorHighlight),
152 ]);
153 }
154
155 /**
156 * A basic readonly BQL editor that only does syntax highlighting.
157 */
158 export function initReadonlyQueryEditor(value: string): EditorAndAction {
159 return setup(value, [
160 bql,
161 syntaxHighlighting(beancountQueryHighlight),
162 EditorView.editable.of(false),
163 ]);
164 }
165
166 /**
167 * The main BQL editor.
168 */
169 export function initQueryEditor(
170 value: string | undefined,
171 onDocChanges: (s: EditorState) => void,
172 _placeholder: string,
173 submit: () => void,
174 ): EditorAndAction {
175 return setup(value, [
176 bql,
177 EditorView.updateListener.of((update) => {
178 if (update.docChanged) {
179 onDocChanges(update.state);
180 }
181 }),
182 keymap.of([
183 {
184 key: "Control-Enter",
185 mac: "Meta-Enter",
186 run: () => {
187 submit();
188 return true;
189 },
190 },
191 ]),
192 placeholder(_placeholder),
193 baseExtensions,
194 syntaxHighlighting(beancountQueryHighlight),
195 ]);
196 }