forked from frappe/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateCodeMirrorState.ts
More file actions
226 lines (218 loc) · 5.44 KB
/
createCodeMirrorState.ts
File metadata and controls
226 lines (218 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import {
autocompletion,
closeBrackets,
closeBracketsKeymap,
completionKeymap,
} from "@codemirror/autocomplete";
import {
defaultKeymap,
history,
historyKeymap,
} from "@codemirror/commands";
import {
bracketMatching,
defaultHighlightStyle,
foldGutter,
foldKeymap,
indentOnInput,
syntaxHighlighting,
} from "@codemirror/language";
import { highlightSelectionMatches, searchKeymap, search } from "@codemirror/search";
import { EditorState, Extension } from "@codemirror/state";
import {
crosshairCursor,
drawSelection,
dropCursor,
highlightActiveLine,
highlightActiveLineGutter,
highlightSpecialChars,
keymap,
lineNumbers,
rectangularSelection,
ViewUpdate,
} from "@codemirror/view";
import { EditorView } from "codemirror";
import jsCompletionsFromGlobalScope from "./jsGlobalCompletion";
import customPythonCompletions from "./pythonCustomCompletion";
import { createApp } from "vue";
import CustomSearchPanel from "@/components/Controls/CodeMirror/CustomSearchPanel.vue";
import { indentationMarkers } from '@replit/codemirror-indentation-markers';
interface CreateStateParams {
props: any;
extraExtensions?: Extension[];
pythonCompletions: any;
onSaveCallback: any;
onChangeCallback: any;
onBlurCallback?: any;
initialValue?: string;
}
export const createStartingState = async ({
props,
extraExtensions = [], // to add extra extensions without recreating state (eg: linting)
pythonCompletions,
onSaveCallback,
onChangeCallback,
onBlurCallback,
initialValue = "", // to override initial value without recreating state (eg: when resetting)
}: CreateStateParams) => {
const updateEmitter = EditorView.updateListener.of((update: ViewUpdate) => {
if (update.docChanged) onChangeCallback();
});
// Create blur event listener if callback is provided
const blurListener = onBlurCallback
? EditorView.domEventHandlers({
blur: (event, view) => {
onBlurCallback(view.state.doc.toString());
return false; // Don't prevent default
},
})
: [];
// collection of basic extensions: https://github.com/codemirror/basic-setup/blob/main/src/codemirror.ts
const basicSetup: Extension = (() => [
props.showLineNumbers ? lineNumbers() : [],
props.showLineNumbers ? EditorView.lineWrapping : [],
props.readonly ? highlightActiveLineGutter() : [],
highlightSpecialChars(),
history(),
foldGutter(),
drawSelection(),
dropCursor(),
EditorState.allowMultipleSelections.of(true),
indentOnInput(),
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
bracketMatching(),
closeBrackets(),
autocompletion({
activateOnTyping: true,
}),
rectangularSelection(),
crosshairCursor(),
highlightActiveLine(),
highlightSelectionMatches(),
keymap.of([
...closeBracketsKeymap,
...defaultKeymap,
...searchKeymap,
...historyKeymap,
...foldKeymap,
...completionKeymap,
]),
])();
const extensions = [
basicSetup,
EditorState.readOnly.of(props.readonly),
// EditorView.editable.of(!props.readonly), // removes cursor but also disables search // TODO: use https://codemirror.net/docs/ref/#search.openSearchPanel
updateEmitter,
blurListener,
...extraExtensions,
keymap.of([
{
key: "Tab",
run: (view) => {
const spaces = " ";
view.dispatch({
changes: {
from: view.state.selection.main.from,
to: view.state.selection.main.to,
insert: spaces,
},
selection: {
anchor: view.state.selection.main.from + spaces.length,
},
});
return true;
},
},
]),
EditorView.domEventHandlers({
// to avoid interfering with builder clipboard events
cut: (event, view) => {
event.stopPropagation();
},
copy: (event, view) => {
event.stopPropagation();
},
paste: (event, view) => {
event.stopPropagation();
},
}),
search({
createPanel(view) {
const dom = document.createElement("div");
dom.classList.add("@container");
const app = createApp(CustomSearchPanel);
app.provide("view", view);
app.provide("enableReplace", !props.readonly);
app.mount(dom);
return {
dom,
top: true,
};
},
}),
indentationMarkers(),
];
if (props.allowSave || !props.readOnly) {
extensions.push(
keymap.of([
{
key: "Ctrl-s",
mac: "Cmd-s",
run: () => {
onSaveCallback();
return true;
},
stopPropagation: true,
},
]),
);
}
// TODO: reconfigure with Compartments instead of switch...case
switch (props.type) {
case "JavaScript": {
const { javascript, javascriptLanguage } = await import(
"@codemirror/lang-javascript"
);
extensions.push(
javascript(),
javascriptLanguage.data.of({
autocomplete: jsCompletionsFromGlobalScope,
}),
);
break;
}
case "Python": {
const { python, pythonLanguage } = await import(
"@codemirror/lang-python"
);
extensions.push(
python(),
pythonLanguage.data.of({
autocomplete: (context: any) =>
customPythonCompletions(context, pythonCompletions),
}),
);
break;
}
case "HTML": {
const { html } = await import("@codemirror/lang-html");
extensions.push(html());
break;
}
case "CSS": {
const { css } = await import("@codemirror/lang-css");
extensions.push(css());
break;
}
case "JSON": {
const { json } = await import("@codemirror/lang-json");
extensions.push(json());
break;
}
}
let startState = EditorState.create({
doc: props.initialValue || initialValue || "",
extensions,
});
return { startState };
};