@zikojs/notebook 0.15.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/package.json +1 -1
- package/src/code-editor/index.js +302 -302
- package/src/compiler/evaluate.js +129 -129
- package/src/compiler/imports.js +189 -189
- package/src/compiler/index.js +11 -11
- package/src/compiler/scope.js +94 -94
- package/src/compiler/transform.js +115 -115
- package/src/components/Button/index.css +54 -54
- package/src/components/Button/index.js +4 -4
- package/src/components/CellItem/index.js +180 -180
- package/src/components/ErrorOutput/index.css +9 -9
- package/src/components/ErrorOutput/index.js +1 -1
- package/src/components/Header/index.css +46 -46
- package/src/components/Header/index.js +47 -47
- package/src/components/MarkdownView/index.css +91 -91
- package/src/components/MarkdownView/index.js +134 -134
- package/src/components/icons/index.js +31 -31
- package/src/components/index.css +22 -22
- package/src/components/index.js +3 -3
- package/src/index.css +176 -176
- package/src/index.js +1 -1
- package/src/index.ts +96 -96
- package/src/notebook.js +816 -816
- package/src/store.js +37 -37
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 ZAKARIA ELALAOUI
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ZAKARIA ELALAOUI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zikojs/notebook",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.1",
|
|
4
4
|
"description": "A lightweight, extensible JavaScript notebook component with Markdown, executable cells, persistent state, and pluggable runtimes.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
package/src/code-editor/index.js
CHANGED
|
@@ -1,302 +1,302 @@
|
|
|
1
|
-
import {
|
|
2
|
-
EditorView,
|
|
3
|
-
keymap,
|
|
4
|
-
lineNumbers,
|
|
5
|
-
highlightActiveLine,
|
|
6
|
-
} from "@codemirror/view";
|
|
7
|
-
import { EditorState, Compartment } from "@codemirror/state";
|
|
8
|
-
import {
|
|
9
|
-
defaultKeymap,
|
|
10
|
-
history,
|
|
11
|
-
historyKeymap,
|
|
12
|
-
indentWithTab,
|
|
13
|
-
} from "@codemirror/commands";
|
|
14
|
-
import {
|
|
15
|
-
bracketMatching,
|
|
16
|
-
indentUnit,
|
|
17
|
-
syntaxHighlighting,
|
|
18
|
-
defaultHighlightStyle,
|
|
19
|
-
} from "@codemirror/language";
|
|
20
|
-
import { closeBrackets, closeBracketsKeymap } from "@codemirror/autocomplete";
|
|
21
|
-
import { javascript } from "@codemirror/lang-javascript";
|
|
22
|
-
import { markdown } from "@codemirror/lang-markdown";
|
|
23
|
-
import { tags } from "ziko/dom";
|
|
24
|
-
|
|
25
|
-
const { div } = tags;
|
|
26
|
-
|
|
27
|
-
const getRuntimePlugin = (runtime, runtimePlugins = []) =>
|
|
28
|
-
runtimePlugins.find((plugin) => plugin?.name === runtime);
|
|
29
|
-
|
|
30
|
-
const languageFor = (type, runtime, runtimePlugins = []) => {
|
|
31
|
-
if (type === "markdown") return markdown();
|
|
32
|
-
|
|
33
|
-
const plugin = getRuntimePlugin(runtime, runtimePlugins);
|
|
34
|
-
|
|
35
|
-
return plugin?.language ?? javascript();
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const themeFor = (runtime, runtimePlugins = []) => {
|
|
39
|
-
const plugin = getRuntimePlugin(runtime, runtimePlugins);
|
|
40
|
-
|
|
41
|
-
return plugin?.theme ?? [];
|
|
42
|
-
};
|
|
43
|
-
export function createCodeEditor(app, cellData, actions) {
|
|
44
|
-
const editorDom = div({ class: "input-wrapper" });
|
|
45
|
-
|
|
46
|
-
const languageCompartment = new Compartment();
|
|
47
|
-
const themeCompartment = new Compartment();
|
|
48
|
-
const readOnlyCompartment = new Compartment();
|
|
49
|
-
const editableCompartment = new Compartment();
|
|
50
|
-
|
|
51
|
-
let view = null;
|
|
52
|
-
|
|
53
|
-
const buildPluginKeymap = () => {
|
|
54
|
-
const bindings = [];
|
|
55
|
-
|
|
56
|
-
app.codeMirrorPlugins.forEach((plugin) => {
|
|
57
|
-
if (plugin && typeof plugin === "object" && plugin.extraKeys) {
|
|
58
|
-
Object.entries(plugin.extraKeys).forEach(([key, run]) => {
|
|
59
|
-
bindings.push({
|
|
60
|
-
key,
|
|
61
|
-
run: () => {
|
|
62
|
-
run();
|
|
63
|
-
return true;
|
|
64
|
-
},
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
return bindings;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const initEditor = () => {
|
|
74
|
-
if (view) return;
|
|
75
|
-
|
|
76
|
-
const shortcutKeymap = keymap.of([
|
|
77
|
-
{
|
|
78
|
-
key: "Ctrl-ArrowDown",
|
|
79
|
-
run: () => actions.moveToAdjacentCell(1),
|
|
80
|
-
},
|
|
81
|
-
{
|
|
82
|
-
key: "Ctrl-ArrowUp",
|
|
83
|
-
run: () => actions.moveToAdjacentCell(-1),
|
|
84
|
-
},
|
|
85
|
-
{
|
|
86
|
-
key: "ArrowDown",
|
|
87
|
-
run: (editorView) => {
|
|
88
|
-
const selection = editorView.state.selection.main;
|
|
89
|
-
const line = editorView.state.doc.lineAt(selection.head);
|
|
90
|
-
|
|
91
|
-
if (
|
|
92
|
-
selection.empty &&
|
|
93
|
-
line.number === editorView.state.doc.lines
|
|
94
|
-
) {
|
|
95
|
-
return actions.moveToAdjacentCell(1);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return false;
|
|
99
|
-
},
|
|
100
|
-
},
|
|
101
|
-
{
|
|
102
|
-
key: "ArrowUp",
|
|
103
|
-
run: (editorView) => {
|
|
104
|
-
const selection = editorView.state.selection.main;
|
|
105
|
-
const line = editorView.state.doc.lineAt(selection.head);
|
|
106
|
-
|
|
107
|
-
if (selection.empty && line.number === 1) {
|
|
108
|
-
return actions.moveToAdjacentCell(-1);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return false;
|
|
112
|
-
},
|
|
113
|
-
},
|
|
114
|
-
{
|
|
115
|
-
key: "Shift-Enter",
|
|
116
|
-
run: () => {
|
|
117
|
-
actions.runCode(true);
|
|
118
|
-
return true;
|
|
119
|
-
},
|
|
120
|
-
},
|
|
121
|
-
{
|
|
122
|
-
key: "Ctrl-Enter",
|
|
123
|
-
mac: "Cmd-Enter",
|
|
124
|
-
run: () => {
|
|
125
|
-
actions.runCode(false);
|
|
126
|
-
return true;
|
|
127
|
-
},
|
|
128
|
-
},
|
|
129
|
-
{
|
|
130
|
-
key: "Ctrl-Shift-d",
|
|
131
|
-
mac: "Cmd-Shift-d",
|
|
132
|
-
run: () => {
|
|
133
|
-
app.notebookGlobalShortcuts.deleteActive();
|
|
134
|
-
return true;
|
|
135
|
-
},
|
|
136
|
-
},
|
|
137
|
-
{
|
|
138
|
-
key: "Ctrl-Shift-m",
|
|
139
|
-
mac: "Cmd-Shift-m",
|
|
140
|
-
run: () => {
|
|
141
|
-
app.notebookGlobalShortcuts.addMdBelow();
|
|
142
|
-
return true;
|
|
143
|
-
},
|
|
144
|
-
},
|
|
145
|
-
{
|
|
146
|
-
key: "Ctrl-Shift-y",
|
|
147
|
-
mac: "Cmd-Shift-y",
|
|
148
|
-
run: () => {
|
|
149
|
-
app.notebookGlobalShortcuts.addCodeBelow();
|
|
150
|
-
return true;
|
|
151
|
-
},
|
|
152
|
-
},
|
|
153
|
-
indentWithTab,
|
|
154
|
-
...buildPluginKeymap(),
|
|
155
|
-
...closeBracketsKeymap,
|
|
156
|
-
...defaultKeymap,
|
|
157
|
-
...historyKeymap,
|
|
158
|
-
]);
|
|
159
|
-
|
|
160
|
-
const extraExtensions = Array.isArray(app.codeMirrorConfig)
|
|
161
|
-
? app.codeMirrorConfig
|
|
162
|
-
: [];
|
|
163
|
-
|
|
164
|
-
const state = EditorState.create({
|
|
165
|
-
doc: cellData.code,
|
|
166
|
-
extensions: [
|
|
167
|
-
// lineNumbers(),
|
|
168
|
-
highlightActiveLine(),
|
|
169
|
-
// EditorView.theme({
|
|
170
|
-
// "&": {
|
|
171
|
-
// fontFamily: '"Fira Code", SFMono-Regular, Consolas, monospace',
|
|
172
|
-
// fontSize: "13px",
|
|
173
|
-
// lineHeight: "17px",
|
|
174
|
-
// },
|
|
175
|
-
// ".cm-scroller, .cm-content, .cm-line, .cm-gutterElement": {
|
|
176
|
-
// lineHeight: "17px",
|
|
177
|
-
// },
|
|
178
|
-
// }),
|
|
179
|
-
|
|
180
|
-
history(),
|
|
181
|
-
bracketMatching(),
|
|
182
|
-
closeBrackets(),
|
|
183
|
-
syntaxHighlighting(defaultHighlightStyle),
|
|
184
|
-
shortcutKeymap,
|
|
185
|
-
EditorView.lineWrapping,
|
|
186
|
-
indentUnit.of(" "),
|
|
187
|
-
EditorState.tabSize.of(2),
|
|
188
|
-
languageCompartment.of(
|
|
189
|
-
languageFor(cellData.type, cellData.runtime, app.runtimePlugins),
|
|
190
|
-
),
|
|
191
|
-
|
|
192
|
-
themeCompartment.of(themeFor(cellData.runtime, app.runtimePlugins)),
|
|
193
|
-
|
|
194
|
-
readOnlyCompartment.of(EditorState.readOnly.of(cellData.readonly)),
|
|
195
|
-
|
|
196
|
-
editableCompartment.of(EditorView.editable.of(!cellData.readonly)),
|
|
197
|
-
|
|
198
|
-
EditorView.updateListener.of((update) => {
|
|
199
|
-
if (update.docChanged) {
|
|
200
|
-
cellData.code = update.state.doc.toString();
|
|
201
|
-
}
|
|
202
|
-
}),
|
|
203
|
-
|
|
204
|
-
EditorView.domEventHandlers({
|
|
205
|
-
focus: () => {
|
|
206
|
-
actions.setActive();
|
|
207
|
-
},
|
|
208
|
-
}),
|
|
209
|
-
|
|
210
|
-
...extraExtensions,
|
|
211
|
-
],
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
view = new EditorView({
|
|
215
|
-
state,
|
|
216
|
-
parent: editorDom.element,
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
const lineHeight = view.defaultLineHeight || 19;
|
|
220
|
-
|
|
221
|
-
view.dom.style.minHeight = `${Math.ceil(app.minLines * lineHeight + 8)}px`;
|
|
222
|
-
|
|
223
|
-
app.codeMirrorPlugins.forEach((plugin) => {
|
|
224
|
-
if (typeof plugin === "function") {
|
|
225
|
-
plugin(view);
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
return {
|
|
231
|
-
dom: editorDom.element,
|
|
232
|
-
|
|
233
|
-
getValue: () => (view ? view.state.doc.toString() : cellData.code),
|
|
234
|
-
|
|
235
|
-
focusAt: (position) => {
|
|
236
|
-
requestAnimationFrame(() => {
|
|
237
|
-
if (!view) return;
|
|
238
|
-
|
|
239
|
-
const cursor = Math.max(
|
|
240
|
-
0,
|
|
241
|
-
Math.min(position, view.state.doc.length),
|
|
242
|
-
);
|
|
243
|
-
|
|
244
|
-
view.dispatch({
|
|
245
|
-
selection: { anchor: cursor },
|
|
246
|
-
});
|
|
247
|
-
view.focus();
|
|
248
|
-
});
|
|
249
|
-
},
|
|
250
|
-
|
|
251
|
-
sync: (isActive, isMarkdownView) => {
|
|
252
|
-
if (!isMarkdownView) {
|
|
253
|
-
if (!view) {
|
|
254
|
-
requestAnimationFrame(() => initEditor());
|
|
255
|
-
} else {
|
|
256
|
-
view.dispatch({
|
|
257
|
-
effects: [
|
|
258
|
-
languageCompartment.reconfigure(
|
|
259
|
-
languageFor(
|
|
260
|
-
cellData.type,
|
|
261
|
-
cellData.runtime,
|
|
262
|
-
app.runtimePlugins,
|
|
263
|
-
),
|
|
264
|
-
),
|
|
265
|
-
|
|
266
|
-
themeCompartment.reconfigure(
|
|
267
|
-
themeFor(cellData.runtime, app.runtimePlugins),
|
|
268
|
-
),
|
|
269
|
-
|
|
270
|
-
readOnlyCompartment.reconfigure(
|
|
271
|
-
EditorState.readOnly.of(cellData.readonly),
|
|
272
|
-
),
|
|
273
|
-
|
|
274
|
-
editableCompartment.reconfigure(
|
|
275
|
-
EditorView.editable.of(!cellData.readonly),
|
|
276
|
-
),
|
|
277
|
-
],
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
const currentValue = view.state.doc.toString();
|
|
281
|
-
|
|
282
|
-
if (currentValue !== cellData.code) {
|
|
283
|
-
view.dispatch({
|
|
284
|
-
changes: {
|
|
285
|
-
from: 0,
|
|
286
|
-
to: currentValue.length,
|
|
287
|
-
insert: cellData.code,
|
|
288
|
-
},
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
if (isActive && view && !cellData.readonly) {
|
|
294
|
-
requestAnimationFrame(() => view.focus());
|
|
295
|
-
}
|
|
296
|
-
} else if (view) {
|
|
297
|
-
view.destroy();
|
|
298
|
-
view = null;
|
|
299
|
-
}
|
|
300
|
-
},
|
|
301
|
-
};
|
|
302
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
EditorView,
|
|
3
|
+
keymap,
|
|
4
|
+
lineNumbers,
|
|
5
|
+
highlightActiveLine,
|
|
6
|
+
} from "@codemirror/view";
|
|
7
|
+
import { EditorState, Compartment } from "@codemirror/state";
|
|
8
|
+
import {
|
|
9
|
+
defaultKeymap,
|
|
10
|
+
history,
|
|
11
|
+
historyKeymap,
|
|
12
|
+
indentWithTab,
|
|
13
|
+
} from "@codemirror/commands";
|
|
14
|
+
import {
|
|
15
|
+
bracketMatching,
|
|
16
|
+
indentUnit,
|
|
17
|
+
syntaxHighlighting,
|
|
18
|
+
defaultHighlightStyle,
|
|
19
|
+
} from "@codemirror/language";
|
|
20
|
+
import { closeBrackets, closeBracketsKeymap } from "@codemirror/autocomplete";
|
|
21
|
+
import { javascript } from "@codemirror/lang-javascript";
|
|
22
|
+
import { markdown } from "@codemirror/lang-markdown";
|
|
23
|
+
import { tags } from "ziko/dom";
|
|
24
|
+
|
|
25
|
+
const { div } = tags;
|
|
26
|
+
|
|
27
|
+
const getRuntimePlugin = (runtime, runtimePlugins = []) =>
|
|
28
|
+
runtimePlugins.find((plugin) => plugin?.name === runtime);
|
|
29
|
+
|
|
30
|
+
const languageFor = (type, runtime, runtimePlugins = []) => {
|
|
31
|
+
if (type === "markdown") return markdown();
|
|
32
|
+
|
|
33
|
+
const plugin = getRuntimePlugin(runtime, runtimePlugins);
|
|
34
|
+
|
|
35
|
+
return plugin?.language ?? javascript();
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const themeFor = (runtime, runtimePlugins = []) => {
|
|
39
|
+
const plugin = getRuntimePlugin(runtime, runtimePlugins);
|
|
40
|
+
|
|
41
|
+
return plugin?.theme ?? [];
|
|
42
|
+
};
|
|
43
|
+
export function createCodeEditor(app, cellData, actions) {
|
|
44
|
+
const editorDom = div({ class: "input-wrapper" });
|
|
45
|
+
|
|
46
|
+
const languageCompartment = new Compartment();
|
|
47
|
+
const themeCompartment = new Compartment();
|
|
48
|
+
const readOnlyCompartment = new Compartment();
|
|
49
|
+
const editableCompartment = new Compartment();
|
|
50
|
+
|
|
51
|
+
let view = null;
|
|
52
|
+
|
|
53
|
+
const buildPluginKeymap = () => {
|
|
54
|
+
const bindings = [];
|
|
55
|
+
|
|
56
|
+
app.codeMirrorPlugins.forEach((plugin) => {
|
|
57
|
+
if (plugin && typeof plugin === "object" && plugin.extraKeys) {
|
|
58
|
+
Object.entries(plugin.extraKeys).forEach(([key, run]) => {
|
|
59
|
+
bindings.push({
|
|
60
|
+
key,
|
|
61
|
+
run: () => {
|
|
62
|
+
run();
|
|
63
|
+
return true;
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return bindings;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const initEditor = () => {
|
|
74
|
+
if (view) return;
|
|
75
|
+
|
|
76
|
+
const shortcutKeymap = keymap.of([
|
|
77
|
+
{
|
|
78
|
+
key: "Ctrl-ArrowDown",
|
|
79
|
+
run: () => actions.moveToAdjacentCell(1),
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
key: "Ctrl-ArrowUp",
|
|
83
|
+
run: () => actions.moveToAdjacentCell(-1),
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
key: "ArrowDown",
|
|
87
|
+
run: (editorView) => {
|
|
88
|
+
const selection = editorView.state.selection.main;
|
|
89
|
+
const line = editorView.state.doc.lineAt(selection.head);
|
|
90
|
+
|
|
91
|
+
if (
|
|
92
|
+
selection.empty &&
|
|
93
|
+
line.number === editorView.state.doc.lines
|
|
94
|
+
) {
|
|
95
|
+
return actions.moveToAdjacentCell(1);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return false;
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
key: "ArrowUp",
|
|
103
|
+
run: (editorView) => {
|
|
104
|
+
const selection = editorView.state.selection.main;
|
|
105
|
+
const line = editorView.state.doc.lineAt(selection.head);
|
|
106
|
+
|
|
107
|
+
if (selection.empty && line.number === 1) {
|
|
108
|
+
return actions.moveToAdjacentCell(-1);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return false;
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
key: "Shift-Enter",
|
|
116
|
+
run: () => {
|
|
117
|
+
actions.runCode(true);
|
|
118
|
+
return true;
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
key: "Ctrl-Enter",
|
|
123
|
+
mac: "Cmd-Enter",
|
|
124
|
+
run: () => {
|
|
125
|
+
actions.runCode(false);
|
|
126
|
+
return true;
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
key: "Ctrl-Shift-d",
|
|
131
|
+
mac: "Cmd-Shift-d",
|
|
132
|
+
run: () => {
|
|
133
|
+
app.notebookGlobalShortcuts.deleteActive();
|
|
134
|
+
return true;
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
key: "Ctrl-Shift-m",
|
|
139
|
+
mac: "Cmd-Shift-m",
|
|
140
|
+
run: () => {
|
|
141
|
+
app.notebookGlobalShortcuts.addMdBelow();
|
|
142
|
+
return true;
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
key: "Ctrl-Shift-y",
|
|
147
|
+
mac: "Cmd-Shift-y",
|
|
148
|
+
run: () => {
|
|
149
|
+
app.notebookGlobalShortcuts.addCodeBelow();
|
|
150
|
+
return true;
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
indentWithTab,
|
|
154
|
+
...buildPluginKeymap(),
|
|
155
|
+
...closeBracketsKeymap,
|
|
156
|
+
...defaultKeymap,
|
|
157
|
+
...historyKeymap,
|
|
158
|
+
]);
|
|
159
|
+
|
|
160
|
+
const extraExtensions = Array.isArray(app.codeMirrorConfig)
|
|
161
|
+
? app.codeMirrorConfig
|
|
162
|
+
: [];
|
|
163
|
+
|
|
164
|
+
const state = EditorState.create({
|
|
165
|
+
doc: cellData.code,
|
|
166
|
+
extensions: [
|
|
167
|
+
// lineNumbers(),
|
|
168
|
+
highlightActiveLine(),
|
|
169
|
+
// EditorView.theme({
|
|
170
|
+
// "&": {
|
|
171
|
+
// fontFamily: '"Fira Code", SFMono-Regular, Consolas, monospace',
|
|
172
|
+
// fontSize: "13px",
|
|
173
|
+
// lineHeight: "17px",
|
|
174
|
+
// },
|
|
175
|
+
// ".cm-scroller, .cm-content, .cm-line, .cm-gutterElement": {
|
|
176
|
+
// lineHeight: "17px",
|
|
177
|
+
// },
|
|
178
|
+
// }),
|
|
179
|
+
|
|
180
|
+
history(),
|
|
181
|
+
bracketMatching(),
|
|
182
|
+
closeBrackets(),
|
|
183
|
+
syntaxHighlighting(defaultHighlightStyle),
|
|
184
|
+
shortcutKeymap,
|
|
185
|
+
EditorView.lineWrapping,
|
|
186
|
+
indentUnit.of(" "),
|
|
187
|
+
EditorState.tabSize.of(2),
|
|
188
|
+
languageCompartment.of(
|
|
189
|
+
languageFor(cellData.type, cellData.runtime, app.runtimePlugins),
|
|
190
|
+
),
|
|
191
|
+
|
|
192
|
+
themeCompartment.of(themeFor(cellData.runtime, app.runtimePlugins)),
|
|
193
|
+
|
|
194
|
+
readOnlyCompartment.of(EditorState.readOnly.of(cellData.readonly)),
|
|
195
|
+
|
|
196
|
+
editableCompartment.of(EditorView.editable.of(!cellData.readonly)),
|
|
197
|
+
|
|
198
|
+
EditorView.updateListener.of((update) => {
|
|
199
|
+
if (update.docChanged) {
|
|
200
|
+
cellData.code = update.state.doc.toString();
|
|
201
|
+
}
|
|
202
|
+
}),
|
|
203
|
+
|
|
204
|
+
EditorView.domEventHandlers({
|
|
205
|
+
focus: () => {
|
|
206
|
+
actions.setActive();
|
|
207
|
+
},
|
|
208
|
+
}),
|
|
209
|
+
|
|
210
|
+
...extraExtensions,
|
|
211
|
+
],
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
view = new EditorView({
|
|
215
|
+
state,
|
|
216
|
+
parent: editorDom.element,
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const lineHeight = view.defaultLineHeight || 19;
|
|
220
|
+
|
|
221
|
+
view.dom.style.minHeight = `${Math.ceil(app.minLines * lineHeight + 8)}px`;
|
|
222
|
+
|
|
223
|
+
app.codeMirrorPlugins.forEach((plugin) => {
|
|
224
|
+
if (typeof plugin === "function") {
|
|
225
|
+
plugin(view);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
dom: editorDom.element,
|
|
232
|
+
|
|
233
|
+
getValue: () => (view ? view.state.doc.toString() : cellData.code),
|
|
234
|
+
|
|
235
|
+
focusAt: (position) => {
|
|
236
|
+
requestAnimationFrame(() => {
|
|
237
|
+
if (!view) return;
|
|
238
|
+
|
|
239
|
+
const cursor = Math.max(
|
|
240
|
+
0,
|
|
241
|
+
Math.min(position, view.state.doc.length),
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
view.dispatch({
|
|
245
|
+
selection: { anchor: cursor },
|
|
246
|
+
});
|
|
247
|
+
view.focus();
|
|
248
|
+
});
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
sync: (isActive, isMarkdownView) => {
|
|
252
|
+
if (!isMarkdownView) {
|
|
253
|
+
if (!view) {
|
|
254
|
+
requestAnimationFrame(() => initEditor());
|
|
255
|
+
} else {
|
|
256
|
+
view.dispatch({
|
|
257
|
+
effects: [
|
|
258
|
+
languageCompartment.reconfigure(
|
|
259
|
+
languageFor(
|
|
260
|
+
cellData.type,
|
|
261
|
+
cellData.runtime,
|
|
262
|
+
app.runtimePlugins,
|
|
263
|
+
),
|
|
264
|
+
),
|
|
265
|
+
|
|
266
|
+
themeCompartment.reconfigure(
|
|
267
|
+
themeFor(cellData.runtime, app.runtimePlugins),
|
|
268
|
+
),
|
|
269
|
+
|
|
270
|
+
readOnlyCompartment.reconfigure(
|
|
271
|
+
EditorState.readOnly.of(cellData.readonly),
|
|
272
|
+
),
|
|
273
|
+
|
|
274
|
+
editableCompartment.reconfigure(
|
|
275
|
+
EditorView.editable.of(!cellData.readonly),
|
|
276
|
+
),
|
|
277
|
+
],
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
const currentValue = view.state.doc.toString();
|
|
281
|
+
|
|
282
|
+
if (currentValue !== cellData.code) {
|
|
283
|
+
view.dispatch({
|
|
284
|
+
changes: {
|
|
285
|
+
from: 0,
|
|
286
|
+
to: currentValue.length,
|
|
287
|
+
insert: cellData.code,
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (isActive && view && !cellData.readonly) {
|
|
294
|
+
requestAnimationFrame(() => view.focus());
|
|
295
|
+
}
|
|
296
|
+
} else if (view) {
|
|
297
|
+
view.destroy();
|
|
298
|
+
view = null;
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
};
|
|
302
|
+
}
|