@zikojs/notebook 0.1.0
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 -0
- package/README.md +1 -0
- package/package.json +55 -0
- package/src/codeEditor.js +212 -0
- package/src/icons.js +29 -0
- package/src/index.css +374 -0
- package/src/index.js +500 -0
- package/src/transform.js +119 -0
- package/src/ui.js +6 -0
package/LICENSE
ADDED
|
@@ -0,0 +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.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @zikojs/notebook
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zikojs/notebook",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"author": "zakaria elalaoui",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/zikojs/notebook.git",
|
|
13
|
+
"directory": "packages/notebook"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"zikojs",
|
|
17
|
+
"notebook"
|
|
18
|
+
],
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"import": "./src/index.js",
|
|
22
|
+
"types": "./src/index.d.ts"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"src",
|
|
27
|
+
"dist",
|
|
28
|
+
"LICENCE"
|
|
29
|
+
],
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@codemirror/autocomplete": "^6.20.3",
|
|
33
|
+
"@codemirror/commands": "^6.11.0",
|
|
34
|
+
"@codemirror/lang-javascript": "^6.2.5",
|
|
35
|
+
"@codemirror/lang-markdown": "^6.5.2",
|
|
36
|
+
"@codemirror/language": "^6.12.4",
|
|
37
|
+
"@codemirror/state": "^6.7.4",
|
|
38
|
+
"@codemirror/view": "^6.43.11",
|
|
39
|
+
"@zikojs/lucide": "^0.2.2",
|
|
40
|
+
"acorn": "^8.18.0",
|
|
41
|
+
"canvas-confetti": "^1.9.4",
|
|
42
|
+
"codemirror": "^6.0.2",
|
|
43
|
+
"lucide": "^1.44.0",
|
|
44
|
+
"marked": "^18.0.12",
|
|
45
|
+
"vanjs-core": "^1.6.1",
|
|
46
|
+
"zextra": "^1.15.0",
|
|
47
|
+
"ziko": "latest"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
51
|
+
"dev": "cross-env NODE_ENV=development rollup --config rollup.config.js",
|
|
52
|
+
"watch": "cross-env NODE_ENV=development rollup --config rollup.config.js -w",
|
|
53
|
+
"build": "cross-env NODE_ENV=production rollup --config rollup.config.js"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { EditorView, keymap, lineNumbers, highlightActiveLine } from "@codemirror/view";
|
|
2
|
+
import { EditorState, Compartment } from "@codemirror/state";
|
|
3
|
+
import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";
|
|
4
|
+
import { bracketMatching, indentUnit, syntaxHighlighting, defaultHighlightStyle } from "@codemirror/language";
|
|
5
|
+
import { closeBrackets, closeBracketsKeymap } from "@codemirror/autocomplete";
|
|
6
|
+
import { javascript } from "@codemirror/lang-javascript";
|
|
7
|
+
import { markdown } from "@codemirror/lang-markdown";
|
|
8
|
+
import van from "vanjs-core";
|
|
9
|
+
|
|
10
|
+
const { div } = van.tags;
|
|
11
|
+
|
|
12
|
+
const languageFor = (type) =>
|
|
13
|
+
type === "markdown" ? markdown() : javascript();
|
|
14
|
+
|
|
15
|
+
export function createCodeEditor(app, cellData, actions) {
|
|
16
|
+
const editorDom = div({ class: "input-wrapper" });
|
|
17
|
+
|
|
18
|
+
const languageCompartment = new Compartment();
|
|
19
|
+
const readOnlyCompartment = new Compartment();
|
|
20
|
+
const editableCompartment = new Compartment();
|
|
21
|
+
|
|
22
|
+
let view = null;
|
|
23
|
+
|
|
24
|
+
const buildPluginKeymap = () => {
|
|
25
|
+
const bindings = [];
|
|
26
|
+
|
|
27
|
+
app.codeMirrorPlugins.forEach(plugin => {
|
|
28
|
+
if (plugin && typeof plugin === "object" && plugin.extraKeys) {
|
|
29
|
+
Object.entries(plugin.extraKeys).forEach(([key, run]) => {
|
|
30
|
+
bindings.push({
|
|
31
|
+
key,
|
|
32
|
+
run: () => {
|
|
33
|
+
run();
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return bindings;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const initEditor = () => {
|
|
45
|
+
if (view) return;
|
|
46
|
+
|
|
47
|
+
const shortcutKeymap = keymap.of([
|
|
48
|
+
{
|
|
49
|
+
key: "Shift-Enter",
|
|
50
|
+
run: () => {
|
|
51
|
+
actions.runCode(true);
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
key: "Ctrl-Enter",
|
|
57
|
+
mac: "Cmd-Enter",
|
|
58
|
+
run: () => {
|
|
59
|
+
actions.runCode(false);
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
key: "Ctrl-Shift-d",
|
|
65
|
+
mac: "Cmd-Shift-d",
|
|
66
|
+
run: () => {
|
|
67
|
+
app.notebookGlobalShortcuts.deleteActive();
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
key: "Ctrl-Shift-m",
|
|
73
|
+
mac: "Cmd-Shift-m",
|
|
74
|
+
run: () => {
|
|
75
|
+
app.notebookGlobalShortcuts.addMdBelow();
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
key: "Ctrl-Shift-y",
|
|
81
|
+
mac: "Cmd-Shift-y",
|
|
82
|
+
run: () => {
|
|
83
|
+
app.notebookGlobalShortcuts.addCodeBelow();
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
...buildPluginKeymap(),
|
|
88
|
+
...closeBracketsKeymap,
|
|
89
|
+
...defaultKeymap,
|
|
90
|
+
...historyKeymap
|
|
91
|
+
]);
|
|
92
|
+
|
|
93
|
+
const extraExtensions = Array.isArray(app.codeMirrorConfig)
|
|
94
|
+
? app.codeMirrorConfig
|
|
95
|
+
: [];
|
|
96
|
+
|
|
97
|
+
const state = EditorState.create({
|
|
98
|
+
doc: cellData.code,
|
|
99
|
+
extensions: [
|
|
100
|
+
lineNumbers(),
|
|
101
|
+
highlightActiveLine(),
|
|
102
|
+
|
|
103
|
+
history(),
|
|
104
|
+
bracketMatching(),
|
|
105
|
+
closeBrackets(),
|
|
106
|
+
|
|
107
|
+
syntaxHighlighting(defaultHighlightStyle),
|
|
108
|
+
|
|
109
|
+
shortcutKeymap,
|
|
110
|
+
|
|
111
|
+
EditorView.lineWrapping,
|
|
112
|
+
|
|
113
|
+
indentUnit.of(" "),
|
|
114
|
+
EditorState.tabSize.of(2),
|
|
115
|
+
|
|
116
|
+
languageCompartment.of(
|
|
117
|
+
languageFor(cellData.type)
|
|
118
|
+
),
|
|
119
|
+
|
|
120
|
+
readOnlyCompartment.of(
|
|
121
|
+
EditorState.readOnly.of(cellData.readonly)
|
|
122
|
+
),
|
|
123
|
+
|
|
124
|
+
editableCompartment.of(
|
|
125
|
+
EditorView.editable.of(!cellData.readonly)
|
|
126
|
+
),
|
|
127
|
+
|
|
128
|
+
EditorView.updateListener.of(update => {
|
|
129
|
+
if (update.docChanged) {
|
|
130
|
+
cellData.code = update.state.doc.toString();
|
|
131
|
+
}
|
|
132
|
+
}),
|
|
133
|
+
|
|
134
|
+
EditorView.domEventHandlers({
|
|
135
|
+
focus: () => {
|
|
136
|
+
actions.setActive();
|
|
137
|
+
}
|
|
138
|
+
}),
|
|
139
|
+
|
|
140
|
+
...extraExtensions
|
|
141
|
+
]
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
view = new EditorView({
|
|
145
|
+
state,
|
|
146
|
+
parent: editorDom
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const lineHeight = view.defaultLineHeight || 19;
|
|
150
|
+
|
|
151
|
+
view.dom.style.minHeight =
|
|
152
|
+
`${Math.ceil(app.minLines * lineHeight + 8)}px`;
|
|
153
|
+
|
|
154
|
+
app.codeMirrorPlugins.forEach(plugin => {
|
|
155
|
+
if (typeof plugin === "function") {
|
|
156
|
+
plugin(view);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
dom: editorDom,
|
|
163
|
+
|
|
164
|
+
getValue: () =>
|
|
165
|
+
view
|
|
166
|
+
? view.state.doc.toString()
|
|
167
|
+
: cellData.code,
|
|
168
|
+
|
|
169
|
+
sync: (isActive, isMarkdownView) => {
|
|
170
|
+
if (!isMarkdownView) {
|
|
171
|
+
if (!view) {
|
|
172
|
+
requestAnimationFrame(() => initEditor());
|
|
173
|
+
} else {
|
|
174
|
+
view.dispatch({
|
|
175
|
+
effects: [
|
|
176
|
+
languageCompartment.reconfigure(
|
|
177
|
+
languageFor(cellData.type)
|
|
178
|
+
),
|
|
179
|
+
|
|
180
|
+
readOnlyCompartment.reconfigure(
|
|
181
|
+
EditorState.readOnly.of(cellData.readonly)
|
|
182
|
+
),
|
|
183
|
+
|
|
184
|
+
editableCompartment.reconfigure(
|
|
185
|
+
EditorView.editable.of(!cellData.readonly)
|
|
186
|
+
)
|
|
187
|
+
]
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const currentValue = view.state.doc.toString();
|
|
191
|
+
|
|
192
|
+
if (currentValue !== cellData.code) {
|
|
193
|
+
view.dispatch({
|
|
194
|
+
changes: {
|
|
195
|
+
from: 0,
|
|
196
|
+
to: currentValue.length,
|
|
197
|
+
insert: cellData.code
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (isActive && view && !cellData.readonly) {
|
|
204
|
+
requestAnimationFrame(() => view.focus());
|
|
205
|
+
}
|
|
206
|
+
} else if (view) {
|
|
207
|
+
view.destroy();
|
|
208
|
+
view = null;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
}
|
package/src/icons.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import _Check from '@zikojs/lucide/Check'
|
|
2
|
+
import _Copy from '@zikojs/lucide/Copy'
|
|
3
|
+
import _Lock from '@zikojs/lucide/Lock'
|
|
4
|
+
import _Unlock from '@zikojs/lucide/Unlock'
|
|
5
|
+
import _Code2 from '@zikojs/lucide/Code2'
|
|
6
|
+
import _FileText from '@zikojs/lucide/FileText'
|
|
7
|
+
import _Pencil from '@zikojs/lucide/Pencil'
|
|
8
|
+
import _Play from '@zikojs/lucide/Play'
|
|
9
|
+
import _Plus from '@zikojs/lucide/Plus'
|
|
10
|
+
import _Trash2 from '@zikojs/lucide/Trash2'
|
|
11
|
+
import _PlayForward from '@zikojs/lucide/StepForward'
|
|
12
|
+
import _Eraser from '@zikojs/lucide/Eraser'
|
|
13
|
+
import _Download from '@zikojs/lucide/Download'
|
|
14
|
+
import _Loader2 from '@zikojs/lucide/Loader2'
|
|
15
|
+
|
|
16
|
+
export const Check = () => _Check({ class: 'icon', stroke : 'currentColor' }).element
|
|
17
|
+
export const Copy = () => _Copy({ class: 'icon', stroke : 'currentColor' }).element
|
|
18
|
+
export const Lock = () => _Lock({ class: 'icon', stroke : 'currentColor' }).element
|
|
19
|
+
export const Unlock = () => _Unlock({ class: 'icon', stroke : 'currentColor' }).element
|
|
20
|
+
export const Code2 = () => _Code2({ class: 'icon', stroke : 'currentColor' }).element
|
|
21
|
+
export const FileText = () => _FileText({ class: 'icon', stroke : 'currentColor' }).element
|
|
22
|
+
export const Pencil = () => _Pencil({ class: 'icon', stroke : 'currentColor' }).element
|
|
23
|
+
export const Play = () => _Play({ class: 'icon', stroke : 'currentColor' }).element
|
|
24
|
+
export const Plus = () => _Plus({ class: 'icon', stroke : 'currentColor' }).element
|
|
25
|
+
export const Trash2 = () => _Trash2({ class: 'icon', stroke : 'currentColor' }).element
|
|
26
|
+
export const PlayForward = () => _PlayForward({ class: 'icon', stroke : 'currentColor' }).element
|
|
27
|
+
export const Eraser = () => _Eraser({ class: 'icon', stroke : 'currentColor' }).element
|
|
28
|
+
export const Download = () => _Download({ class: 'icon', stroke : 'currentColor' }).element
|
|
29
|
+
export const Loader2 = () => _Loader2({ class: 'icon', stroke : 'currentColor' }).element
|
package/src/index.css
ADDED
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
:root {
|
|
2
|
+
--primary: #0284c7;
|
|
3
|
+
--primary-hover: #0369a1;
|
|
4
|
+
--border-color: #e2e8f0;
|
|
5
|
+
--prompt-in: #303f9f;
|
|
6
|
+
--prompt-out: #d62728;
|
|
7
|
+
--bg-editor: #f8fafc;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
* {
|
|
11
|
+
box-sizing: border-box;
|
|
12
|
+
margin: 0;
|
|
13
|
+
padding: 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
body {
|
|
17
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
|
18
|
+
background: #ffffff;
|
|
19
|
+
color: #1e293b;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/* --- HEADER & TOOLBAR --- */
|
|
23
|
+
|
|
24
|
+
.header {
|
|
25
|
+
border-bottom: 1px solid var(--border-color);
|
|
26
|
+
background: #ffffff;
|
|
27
|
+
padding: 10px 16px;
|
|
28
|
+
display: flex;
|
|
29
|
+
align-items: center;
|
|
30
|
+
justify-content: space-between;
|
|
31
|
+
gap: 12px;
|
|
32
|
+
flex-wrap: wrap;
|
|
33
|
+
position: sticky;
|
|
34
|
+
top: 0;
|
|
35
|
+
z-index: 100;
|
|
36
|
+
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
|
|
37
|
+
width: 100%;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.brand {
|
|
41
|
+
font-weight: 600;
|
|
42
|
+
font-size: 16px;
|
|
43
|
+
color: #0f172a;
|
|
44
|
+
display: flex;
|
|
45
|
+
align-items: center;
|
|
46
|
+
gap: 12px;
|
|
47
|
+
flex-shrink: 0;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
.toolbar {
|
|
51
|
+
display: flex;
|
|
52
|
+
gap: 6px;
|
|
53
|
+
align-items: center;
|
|
54
|
+
flex-wrap: wrap;
|
|
55
|
+
max-width: 100%;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.status-badge {
|
|
59
|
+
font-size: 12px;
|
|
60
|
+
font-weight: 500;
|
|
61
|
+
color: #0284c7;
|
|
62
|
+
background: #e0f2fe;
|
|
63
|
+
padding: 4px 8px;
|
|
64
|
+
border-radius: 4px;
|
|
65
|
+
display: inline-flex;
|
|
66
|
+
align-items: center;
|
|
67
|
+
gap: 6px;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/* --- NOTEBOOK & CELLS --- */
|
|
71
|
+
|
|
72
|
+
.notebook {
|
|
73
|
+
max-width: 900px;
|
|
74
|
+
margin: 16px auto;
|
|
75
|
+
padding: 0 8px;
|
|
76
|
+
display: flex;
|
|
77
|
+
flex-direction: column;
|
|
78
|
+
gap: 12px;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
.cell {
|
|
82
|
+
display: flex;
|
|
83
|
+
flex-direction: column;
|
|
84
|
+
border: 1px solid var(--border-color);
|
|
85
|
+
border-left: 4px solid transparent;
|
|
86
|
+
border-radius: 6px;
|
|
87
|
+
padding: 8px;
|
|
88
|
+
gap: 8px;
|
|
89
|
+
background: #ffffff;
|
|
90
|
+
transition: border-color 0.15s ease, box-shadow 0.15s ease;
|
|
91
|
+
position: relative;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
.cell.active {
|
|
95
|
+
border-left-color: var(--primary);
|
|
96
|
+
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
.cell-input-grid {
|
|
100
|
+
display: grid;
|
|
101
|
+
grid-template-columns: 56px 1fr auto;
|
|
102
|
+
align-items: flex-start;
|
|
103
|
+
gap: 8px;
|
|
104
|
+
width: 100%;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
.cell-output-grid {
|
|
108
|
+
display: grid;
|
|
109
|
+
grid-template-columns: 56px 1fr;
|
|
110
|
+
align-items: flex-start;
|
|
111
|
+
gap: 8px;
|
|
112
|
+
width: 100%;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
.prompt {
|
|
116
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
117
|
+
font-size: 11px;
|
|
118
|
+
color: var(--prompt-in);
|
|
119
|
+
text-align: right;
|
|
120
|
+
padding-top: 8px;
|
|
121
|
+
white-space: nowrap;
|
|
122
|
+
user-select: none;
|
|
123
|
+
font-weight: 700;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
.prompt.out {
|
|
127
|
+
color: var(--prompt-out);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* --- CODEMIRROR / INPUT --- */
|
|
131
|
+
|
|
132
|
+
.input-wrapper {
|
|
133
|
+
border: 1px solid var(--border-color);
|
|
134
|
+
border-radius: 4px;
|
|
135
|
+
background: var(--bg-editor);
|
|
136
|
+
overflow: hidden;
|
|
137
|
+
width: 100%;
|
|
138
|
+
transition: border-color 0.15s ease;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
.cell.active .input-wrapper {
|
|
142
|
+
border-color: #cbd5e1;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
.CodeMirror {
|
|
146
|
+
font-family: "Fira Code", SFMono-Regular, Consolas, monospace;
|
|
147
|
+
font-size: 13px;
|
|
148
|
+
height: auto !important;
|
|
149
|
+
background: transparent;
|
|
150
|
+
border-radius: 4px;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
.CodeMirror-scroll {
|
|
154
|
+
/* Overflow reset ensures height auto-fits custom minLines properly */
|
|
155
|
+
overflow-y: hidden !important;
|
|
156
|
+
overflow-x: auto !important;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
.CodeMirror-activeline-background {
|
|
160
|
+
background: rgba(2, 132, 199, 0.05);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
.cell-controls {
|
|
164
|
+
display: flex;
|
|
165
|
+
align-items: center;
|
|
166
|
+
gap: 4px;
|
|
167
|
+
flex-shrink: 0;
|
|
168
|
+
flex-wrap: wrap;
|
|
169
|
+
max-width: 120px;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/* --- OUTPUTS & MARKDOWN --- */
|
|
173
|
+
|
|
174
|
+
.output-wrapper {
|
|
175
|
+
width: 100%;
|
|
176
|
+
padding: 4px 0;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
.output-area {
|
|
180
|
+
font-size: 14px;
|
|
181
|
+
word-break: break-word;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
.error-output {
|
|
185
|
+
color: #e11d48;
|
|
186
|
+
font-family: ui-monospace, SFMono-Regular, monospace;
|
|
187
|
+
white-space: pre-wrap;
|
|
188
|
+
font-size: 12px;
|
|
189
|
+
background: #fff1f2;
|
|
190
|
+
padding: 8px;
|
|
191
|
+
border-radius: 4px;
|
|
192
|
+
border: 1px solid #fecdd3;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
.markdown-rendered-cell {
|
|
196
|
+
grid-column: 1 / 3;
|
|
197
|
+
padding: 8px 12px;
|
|
198
|
+
cursor: pointer;
|
|
199
|
+
min-height: 38px;
|
|
200
|
+
border: 1px dashed transparent;
|
|
201
|
+
border-radius: 4px;
|
|
202
|
+
transition: background 0.15s ease, border-color 0.15s ease;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
.markdown-rendered-cell:hover {
|
|
206
|
+
background: #f8fafc;
|
|
207
|
+
border-color: #cbd5e1;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
.cell.active .markdown-rendered-cell {
|
|
211
|
+
border-color: #94a3b8;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
.markdown-body {
|
|
215
|
+
line-height: 1.6;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
.markdown-body h1,
|
|
219
|
+
.markdown-body h2,
|
|
220
|
+
.markdown-body h3 {
|
|
221
|
+
margin-top: 8px;
|
|
222
|
+
margin-bottom: 8px;
|
|
223
|
+
font-weight: 600;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
.markdown-body h1 {
|
|
227
|
+
font-size: 1.5em;
|
|
228
|
+
border-bottom: 1px solid var(--border-color);
|
|
229
|
+
padding-bottom: 4px;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
.markdown-body h2 {
|
|
233
|
+
font-size: 1.25em;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
.markdown-body h3 {
|
|
237
|
+
font-size: 1.1em;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
.markdown-body p {
|
|
241
|
+
margin-bottom: 8px;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
.markdown-body p:last-child {
|
|
245
|
+
margin-bottom: 0;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
.markdown-body ul,
|
|
249
|
+
.markdown-body ol {
|
|
250
|
+
padding-left: 20px;
|
|
251
|
+
margin-bottom: 8px;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
.markdown-body code {
|
|
255
|
+
background: #f1f5f9;
|
|
256
|
+
padding: 2px 4px;
|
|
257
|
+
border-radius: 4px;
|
|
258
|
+
font-family: ui-monospace, SFMono-Regular, monospace;
|
|
259
|
+
font-size: 0.9em;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
.markdown-body pre {
|
|
263
|
+
background: #1e293b;
|
|
264
|
+
color: #f8fafc;
|
|
265
|
+
padding: 10px;
|
|
266
|
+
border-radius: 6px;
|
|
267
|
+
overflow-x: auto;
|
|
268
|
+
margin: 8px 0;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
.markdown-body pre code {
|
|
272
|
+
background: transparent;
|
|
273
|
+
color: inherit;
|
|
274
|
+
padding: 0;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
.markdown-body blockquote {
|
|
278
|
+
border-left: 4px solid var(--primary);
|
|
279
|
+
padding-left: 10px;
|
|
280
|
+
color: #64748b;
|
|
281
|
+
margin: 8px 0;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/* --- BUTTONS & ICONS --- */
|
|
285
|
+
|
|
286
|
+
.btn {
|
|
287
|
+
display: inline-flex;
|
|
288
|
+
align-items: center;
|
|
289
|
+
justify-content: center;
|
|
290
|
+
gap: 6px;
|
|
291
|
+
background: #ffffff;
|
|
292
|
+
border: 1px solid var(--border-color);
|
|
293
|
+
padding: 6px 10px;
|
|
294
|
+
border-radius: 4px;
|
|
295
|
+
font-size: 12px;
|
|
296
|
+
font-weight: 500;
|
|
297
|
+
color: #334155;
|
|
298
|
+
cursor: pointer;
|
|
299
|
+
line-height: 1;
|
|
300
|
+
white-space: nowrap;
|
|
301
|
+
transition: background 0.12s ease, border-color 0.12s ease;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
.btn:hover {
|
|
305
|
+
background: #f1f5f9;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
.btn:disabled {
|
|
309
|
+
opacity: 0.5;
|
|
310
|
+
cursor: not-allowed;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
.btn-icon {
|
|
314
|
+
padding: 6px;
|
|
315
|
+
border-radius: 4px;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
.btn-primary {
|
|
319
|
+
background: var(--primary);
|
|
320
|
+
color: #ffffff;
|
|
321
|
+
border-color: var(--primary);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
.btn-primary:hover {
|
|
325
|
+
background: var(--primary-hover);
|
|
326
|
+
border-color: var(--primary-hover);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
.btn-active {
|
|
330
|
+
background: #e2e8f0;
|
|
331
|
+
border-color: #cbd5e1;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
.icon {
|
|
335
|
+
width: 14px;
|
|
336
|
+
height: 14px;
|
|
337
|
+
stroke-width: 2;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/* --- MOBILE RESPONSIVE --- */
|
|
341
|
+
|
|
342
|
+
@media (max-width: 640px) {
|
|
343
|
+
.header {
|
|
344
|
+
padding: 8px 12px;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
.toolbar {
|
|
348
|
+
width: 100%;
|
|
349
|
+
justify-content: flex-start;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
.cell-input-grid {
|
|
353
|
+
grid-template-columns: 42px 1fr;
|
|
354
|
+
grid-template-rows: auto auto;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
.cell-controls {
|
|
358
|
+
grid-column: 1 / -1;
|
|
359
|
+
justify-content: flex-end;
|
|
360
|
+
border-bottom: 1px solid #f1f5f9;
|
|
361
|
+
padding-bottom: 6px;
|
|
362
|
+
margin-bottom: 2px;
|
|
363
|
+
max-width: 100%;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
.cell-output-grid {
|
|
367
|
+
grid-template-columns: 42px 1fr;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
.prompt {
|
|
371
|
+
font-size: 10px;
|
|
372
|
+
padding-top: 6px;
|
|
373
|
+
}
|
|
374
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
import './index.css'
|
|
2
|
+
import van from "vanjs-core";
|
|
3
|
+
import { marked } from "marked";
|
|
4
|
+
import { Button } from "./ui.js";
|
|
5
|
+
import { createCodeEditor } from "./codeEditor.js";
|
|
6
|
+
import { evaluateCodeAsync } from "./transform.js";
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
Check,
|
|
10
|
+
Copy,
|
|
11
|
+
Lock,
|
|
12
|
+
Unlock,
|
|
13
|
+
Code2,
|
|
14
|
+
FileText,
|
|
15
|
+
Pencil,
|
|
16
|
+
Play,
|
|
17
|
+
Plus,
|
|
18
|
+
Trash2,
|
|
19
|
+
PlayForward,
|
|
20
|
+
Eraser,
|
|
21
|
+
Download,
|
|
22
|
+
Loader2
|
|
23
|
+
} from './icons.js'
|
|
24
|
+
|
|
25
|
+
const { div, span } = van.tags;
|
|
26
|
+
|
|
27
|
+
export function Header({ state, actions }) {
|
|
28
|
+
const { runAllBtn, clearBtn, addCodeBtn, addMdBtn, deleteActiveBtn, exportBtn, statusBadge, statusText } = state;
|
|
29
|
+
|
|
30
|
+
return div({ class: "header" },
|
|
31
|
+
div({ class: "brand" }, "Jupyter VanJS Notebook", statusBadge),
|
|
32
|
+
div({ class: "toolbar" },
|
|
33
|
+
runAllBtn, clearBtn, addCodeBtn, addMdBtn, deleteActiveBtn, exportBtn
|
|
34
|
+
)
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function MarkdownView({ cellData, actions }) {
|
|
39
|
+
const content = div();
|
|
40
|
+
content.innerHTML = marked.parse(cellData.code || "*Empty Markdown Cell*");
|
|
41
|
+
|
|
42
|
+
const wrap = div({ class: "markdown-rendered-cell markdown-body" }, content);
|
|
43
|
+
wrap.onclick = () => actions.editMarkdown();
|
|
44
|
+
return wrap;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function CellControls({ cellData, state, actions }) {
|
|
48
|
+
const isQueueRunning = state.executionQueueState_state.isRunning;
|
|
49
|
+
const isMaxCellsReached = state.cells_state.length >= state.maxCells;
|
|
50
|
+
const isCopied = cellData._copiedFlag_state === true;
|
|
51
|
+
|
|
52
|
+
const handleCopy = async () => {
|
|
53
|
+
await actions.copyCode();
|
|
54
|
+
cellData._copiedFlag_state = true;
|
|
55
|
+
state.refreshControls(cellData);
|
|
56
|
+
setTimeout(() => {
|
|
57
|
+
cellData._copiedFlag_state = false;
|
|
58
|
+
state.refreshControls(cellData);
|
|
59
|
+
}, 1500);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
return [
|
|
63
|
+
Button({
|
|
64
|
+
class: `btn-icon ${isCopied ? "btn-active" : ""}`,
|
|
65
|
+
title: isCopied ? "Copied!" : "Copy Cell Content",
|
|
66
|
+
onclick: handleCopy
|
|
67
|
+
},
|
|
68
|
+
isCopied ? Check() : Copy()
|
|
69
|
+
),
|
|
70
|
+
Button({
|
|
71
|
+
class: `btn-icon ${cellData.readonly ? "btn-active" : ""}`,
|
|
72
|
+
title: cellData.readonly ? "Make Editable" : "Make Read-Only",
|
|
73
|
+
disabled: isQueueRunning,
|
|
74
|
+
onclick: actions.toggleReadonly,
|
|
75
|
+
},
|
|
76
|
+
cellData.readonly ? Lock() : Unlock()
|
|
77
|
+
),
|
|
78
|
+
Button({
|
|
79
|
+
class: `btn-icon ${cellData.type === "markdown" ? "btn-active" : ""}`,
|
|
80
|
+
title: `Switch to ${cellData.type === "code" ? "Markdown" : "JS Code"}`,
|
|
81
|
+
disabled: isQueueRunning || cellData.readonly,
|
|
82
|
+
onclick: actions.toggleType,
|
|
83
|
+
},
|
|
84
|
+
cellData.type === "code" ? FileText() : Code2()
|
|
85
|
+
),
|
|
86
|
+
(cellData.type === "markdown" && !cellData.isEditingMarkdown)
|
|
87
|
+
? Button(
|
|
88
|
+
{
|
|
89
|
+
class: "btn-icon",
|
|
90
|
+
title: "Edit Markdown",
|
|
91
|
+
disabled: isQueueRunning || cellData.readonly,
|
|
92
|
+
onclick: actions.editMarkdown
|
|
93
|
+
},
|
|
94
|
+
Pencil()
|
|
95
|
+
)
|
|
96
|
+
: Button({
|
|
97
|
+
class: "btn-icon btn-primary",
|
|
98
|
+
title: "Run & Advance (Shift+Enter)",
|
|
99
|
+
disabled: isQueueRunning,
|
|
100
|
+
onclick: () => actions.runCode(true)
|
|
101
|
+
},
|
|
102
|
+
Play()
|
|
103
|
+
),
|
|
104
|
+
Button({
|
|
105
|
+
class: "btn-icon",
|
|
106
|
+
title: "Add Cell Below",
|
|
107
|
+
disabled: isQueueRunning || isMaxCellsReached,
|
|
108
|
+
onclick: actions.addBelow
|
|
109
|
+
}, Plus()),
|
|
110
|
+
Button({
|
|
111
|
+
class: "btn-icon",
|
|
112
|
+
title: "Delete Cell",
|
|
113
|
+
disabled: isQueueRunning,
|
|
114
|
+
onclick: actions.deleteCell
|
|
115
|
+
}, Trash2())
|
|
116
|
+
];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function CellItem({ cellData, app }) {
|
|
120
|
+
const outputRef = div({ class: "output-area" });
|
|
121
|
+
if (cellData.outputNode) outputRef.appendChild(cellData.outputNode);
|
|
122
|
+
cellData._outputRef = outputRef;
|
|
123
|
+
|
|
124
|
+
const actions = app._cellActions(cellData);
|
|
125
|
+
cellData._actions = actions;
|
|
126
|
+
cellData._editor = createCodeEditor(app, cellData, actions);
|
|
127
|
+
|
|
128
|
+
const inPrompt = span({ class: "prompt" });
|
|
129
|
+
const outPrompt = span({ class: "prompt out" });
|
|
130
|
+
const bodySlot = div({ style: "display:contents;" });
|
|
131
|
+
const controlsEl = div({ class: "cell-controls" });
|
|
132
|
+
|
|
133
|
+
const outputGrid = div({ class: "cell-output-grid" }, outPrompt, div({ class: "output-wrapper" }, outputRef));
|
|
134
|
+
const inputGrid = div({ class: "cell-input-grid" }, inPrompt, bodySlot, controlsEl);
|
|
135
|
+
const root = div({ class: "cell" }, inputGrid, outputGrid);
|
|
136
|
+
root.onclick = () => app.setActiveCell(cellData.id);
|
|
137
|
+
|
|
138
|
+
cellData._dom = root;
|
|
139
|
+
cellData._inPrompt = inPrompt;
|
|
140
|
+
cellData._outPrompt = outPrompt;
|
|
141
|
+
cellData._bodySlot = bodySlot;
|
|
142
|
+
cellData._controlsEl = controlsEl;
|
|
143
|
+
cellData._outputGrid = outputGrid;
|
|
144
|
+
|
|
145
|
+
app._refreshCell(cellData);
|
|
146
|
+
return root;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// --- MAIN NOTEBOOK APP CLASS ---
|
|
150
|
+
|
|
151
|
+
export class NotebookApp {
|
|
152
|
+
constructor({
|
|
153
|
+
cells: initialCells = [],
|
|
154
|
+
importMap = {},
|
|
155
|
+
runCells = true,
|
|
156
|
+
codeMirrorConfig = {},
|
|
157
|
+
markedPlugins = [],
|
|
158
|
+
codeMirrorPlugins = [],
|
|
159
|
+
minLines = 3,
|
|
160
|
+
maxCells = Infinity
|
|
161
|
+
} = {}) {
|
|
162
|
+
this.importMap = importMap;
|
|
163
|
+
this.codeMirrorConfig = codeMirrorConfig;
|
|
164
|
+
this.codeMirrorPlugins = codeMirrorPlugins;
|
|
165
|
+
this.minLines = minLines;
|
|
166
|
+
this.maxCells = maxCells;
|
|
167
|
+
|
|
168
|
+
if (Array.isArray(markedPlugins) && markedPlugins.length > 0) {
|
|
169
|
+
markedPlugins.forEach(plugin => marked.use(plugin));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const startingCells = maxCells !== Infinity ? initialCells.slice(0, maxCells) : initialCells;
|
|
173
|
+
|
|
174
|
+
this.nextCellId_state = startingCells.length ? Math.max(...startingCells.map(c => c.id || 0)) + 1 : 1;
|
|
175
|
+
|
|
176
|
+
this.cells_state = startingCells.map(cell => ({
|
|
177
|
+
id: cell.id || this.nextCellId_state++,
|
|
178
|
+
type: cell.type || "code",
|
|
179
|
+
code: cell.code || "",
|
|
180
|
+
readonly: cell.readonly ?? false,
|
|
181
|
+
isEditingMarkdown: cell.readonly ? false : (cell.isEditingMarkdown ?? true),
|
|
182
|
+
execCount: cell.execCount ?? null,
|
|
183
|
+
outputNode: cell.outputNode || null,
|
|
184
|
+
hasDomOutput: cell.hasDomOutput ?? false,
|
|
185
|
+
_copiedFlag_state: false,
|
|
186
|
+
_dom: null, _editor: null, _actions: null,
|
|
187
|
+
_inPrompt: null, _outPrompt: null, _bodySlot: null, _controlsEl: null, _outputGrid: null, _outputRef: null
|
|
188
|
+
}));
|
|
189
|
+
|
|
190
|
+
this.activeCellId_state = this.cells_state.length ? this.cells_state[0].id : null;
|
|
191
|
+
this.executionCounter_state = 1;
|
|
192
|
+
this.executionQueueState_state = { isRunning: false, currentIndex: 0, total: 0 };
|
|
193
|
+
|
|
194
|
+
this.notebookGlobalShortcuts = {
|
|
195
|
+
deleteActive: () => this.deleteCell(this.activeCellId_state),
|
|
196
|
+
addMdBelow: () => this.addCell("markdown", "", this.activeCellId_state),
|
|
197
|
+
addCodeBelow: () => this.addCell("code", "", this.activeCellId_state)
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
this.cellsContainer = div({ class: "cells-container" });
|
|
201
|
+
this.header = this._buildHeader();
|
|
202
|
+
this.element = div(this.header, div({ class: "notebook" }, this.cellsContainer));
|
|
203
|
+
|
|
204
|
+
this._renderAllCells();
|
|
205
|
+
|
|
206
|
+
if (runCells) {
|
|
207
|
+
setTimeout(() => { this.runAllCells(); }, 200);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
_isMaxCellsReached() { return this.cells_state.length >= this.maxCells; }
|
|
212
|
+
|
|
213
|
+
// --- HEADER BUILDER ---
|
|
214
|
+
|
|
215
|
+
_buildHeader() {
|
|
216
|
+
this.runAllBtn = Button({
|
|
217
|
+
class: "btn-primary",
|
|
218
|
+
onclick: () => this.runAllCells(),
|
|
219
|
+
}, PlayForward(), "Run All"
|
|
220
|
+
);
|
|
221
|
+
this.clearBtn = Button({
|
|
222
|
+
onclick: () => this.clearOutputs()
|
|
223
|
+
}, Eraser(), "Clear Outputs");
|
|
224
|
+
this.addCodeBtn = Button({
|
|
225
|
+
onclick: () => this.addCell("code", "", this.activeCellId_state),
|
|
226
|
+
}, Play(), "Add Code");
|
|
227
|
+
this.addMdBtn = Button({
|
|
228
|
+
onclick: () => this.addCell("markdown", "", this.activeCellId_state),
|
|
229
|
+
}, FileText(), "Add Markdown");
|
|
230
|
+
this.deleteActiveBtn = Button({
|
|
231
|
+
onclick: () => this.deleteCell(this.activeCellId_state)
|
|
232
|
+
}, Trash2(), "Delete Active");
|
|
233
|
+
this.exportBtn = Button({
|
|
234
|
+
onclick: () => console.log("Notebook Data Export:", this.getNotebookData())
|
|
235
|
+
}, Download(), "Export Data");
|
|
236
|
+
|
|
237
|
+
this.statusText = span();
|
|
238
|
+
this.statusBadge = span({
|
|
239
|
+
class: "status-badge",
|
|
240
|
+
style: "display:none;"
|
|
241
|
+
}, Loader2(), this.statusText);
|
|
242
|
+
|
|
243
|
+
const header = Header({
|
|
244
|
+
state: {
|
|
245
|
+
runAllBtn: this.runAllBtn,
|
|
246
|
+
clearBtn: this.clearBtn,
|
|
247
|
+
addCodeBtn: this.addCodeBtn,
|
|
248
|
+
addMdBtn: this.addMdBtn,
|
|
249
|
+
deleteActiveBtn: this.deleteActiveBtn,
|
|
250
|
+
exportBtn: this.exportBtn,
|
|
251
|
+
statusBadge: this.statusBadge,
|
|
252
|
+
statusText: this.statusText
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
this._updateHeaderUI();
|
|
257
|
+
return header;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
_updateHeaderUI() {
|
|
261
|
+
const q = this.executionQueueState_state;
|
|
262
|
+
this.statusBadge.style.display = q.isRunning ? "inline-flex" : "none";
|
|
263
|
+
this.statusText.textContent = `Executing Queue (${q.currentIndex + 1}/${q.total})`;
|
|
264
|
+
[this.runAllBtn, this.clearBtn, this.deleteActiveBtn].forEach(b => { b.disabled = q.isRunning; });
|
|
265
|
+
const maxReached = this._isMaxCellsReached();
|
|
266
|
+
this.addCodeBtn.disabled = q.isRunning || maxReached;
|
|
267
|
+
this.addMdBtn.disabled = q.isRunning || maxReached;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// --- CELL ACTIONS & DOM ---
|
|
271
|
+
|
|
272
|
+
_cellActions(cellData) {
|
|
273
|
+
const app = this;
|
|
274
|
+
return {
|
|
275
|
+
setActive: () => app.setActiveCell(cellData.id),
|
|
276
|
+
runCode: async (autoNext = true) => {
|
|
277
|
+
const code = cellData._editor.getValue();
|
|
278
|
+
cellData.code = code;
|
|
279
|
+
|
|
280
|
+
if (cellData.type === "markdown") {
|
|
281
|
+
cellData.isEditingMarkdown = false;
|
|
282
|
+
if (autoNext) app.nextCell(cellData.id, "markdown");
|
|
283
|
+
else app._refreshCell(cellData);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (!code.trim()) return;
|
|
288
|
+
|
|
289
|
+
cellData._outputRef.innerHTML = "";
|
|
290
|
+
let hasError = false;
|
|
291
|
+
|
|
292
|
+
try {
|
|
293
|
+
await evaluateCodeAsync(code, cellData._outputRef, app.importMap);
|
|
294
|
+
} catch (err) {
|
|
295
|
+
hasError = true;
|
|
296
|
+
van.add(cellData._outputRef, div({ class: "error-output" }, err.toString()));
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
cellData.execCount = app.executionCounter_state++;
|
|
300
|
+
cellData.hasDomOutput = cellData._outputRef.childNodes.length > 0 || hasError;
|
|
301
|
+
|
|
302
|
+
const container = div({ class: "output-area" });
|
|
303
|
+
while (cellData._outputRef.firstChild) container.appendChild(cellData._outputRef.firstChild);
|
|
304
|
+
cellData.outputNode = container;
|
|
305
|
+
cellData._outputRef.appendChild(container);
|
|
306
|
+
|
|
307
|
+
if (autoNext && !hasError) app.nextCell(cellData.id, "code");
|
|
308
|
+
else app._refreshCell(cellData);
|
|
309
|
+
},
|
|
310
|
+
copyCode: async () => {
|
|
311
|
+
const textToCopy = cellData._editor.getValue() || cellData.code;
|
|
312
|
+
try {
|
|
313
|
+
await navigator.clipboard.writeText(textToCopy);
|
|
314
|
+
} catch (err) {
|
|
315
|
+
const textArea = document.createElement("textarea");
|
|
316
|
+
textArea.value = textToCopy;
|
|
317
|
+
document.body.appendChild(textArea);
|
|
318
|
+
textArea.select();
|
|
319
|
+
document.execCommand("copy");
|
|
320
|
+
document.body.removeChild(textArea);
|
|
321
|
+
}
|
|
322
|
+
},
|
|
323
|
+
toggleReadonly: () => {
|
|
324
|
+
cellData.readonly = !cellData.readonly;
|
|
325
|
+
if (cellData.readonly && cellData.type === "markdown") cellData.isEditingMarkdown = false;
|
|
326
|
+
app._refreshCell(cellData);
|
|
327
|
+
},
|
|
328
|
+
toggleType: () => {
|
|
329
|
+
if (cellData.readonly) return;
|
|
330
|
+
cellData.type = cellData.type === "code" ? "markdown" : "code";
|
|
331
|
+
cellData.isEditingMarkdown = true;
|
|
332
|
+
cellData.hasDomOutput = false;
|
|
333
|
+
app._refreshCell(cellData);
|
|
334
|
+
},
|
|
335
|
+
editMarkdown: () => {
|
|
336
|
+
if (cellData.readonly) return;
|
|
337
|
+
cellData.isEditingMarkdown = true;
|
|
338
|
+
app._refreshCell(cellData);
|
|
339
|
+
},
|
|
340
|
+
addBelow: () => app.addCell(cellData.type, null, cellData.id),
|
|
341
|
+
deleteCell: () => app.deleteCell(cellData.id)
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
_buildCellDom(cellData) {
|
|
346
|
+
return CellItem({ cellData, app: this });
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
_refreshCell(cellData) {
|
|
350
|
+
const isActive = this.activeCellId_state === cellData.id;
|
|
351
|
+
cellData._dom.className = `cell ${isActive ? "active" : ""} ${cellData.readonly ? "readonly" : ""}`.trim();
|
|
352
|
+
|
|
353
|
+
const countText = cellData.execCount ? `[${cellData.execCount}]` : "[ ]";
|
|
354
|
+
const isMarkdownView = cellData.type === "markdown" && !cellData.isEditingMarkdown;
|
|
355
|
+
|
|
356
|
+
cellData._inPrompt.textContent = cellData.type === "code" ? `In ${countText}:` : "";
|
|
357
|
+
|
|
358
|
+
cellData._editor.sync(isActive, isMarkdownView);
|
|
359
|
+
cellData._bodySlot.replaceChildren(
|
|
360
|
+
isMarkdownView ? MarkdownView({ cellData, actions: cellData._actions }) : cellData._editor.dom
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
this._refreshCellControls(cellData);
|
|
364
|
+
|
|
365
|
+
const showOutput = cellData.type === "code" && cellData.hasDomOutput;
|
|
366
|
+
cellData._outputGrid.style.display = showOutput ? "grid" : "none";
|
|
367
|
+
cellData._outPrompt.textContent = `Out ${countText}:`;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
_refreshCellControls(cellData) {
|
|
371
|
+
const buttons = CellControls({
|
|
372
|
+
cellData,
|
|
373
|
+
state: this,
|
|
374
|
+
actions: cellData._actions
|
|
375
|
+
});
|
|
376
|
+
cellData._controlsEl.replaceChildren(...buttons);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// --- STATE MUTATORS ---
|
|
380
|
+
|
|
381
|
+
_renderAllCells() {
|
|
382
|
+
this.cellsContainer.replaceChildren(...this.cells_state.map(c => c._dom || this._buildCellDom(c)));
|
|
383
|
+
this._updateHeaderUI();
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
setActiveCell(id) {
|
|
387
|
+
if (this.activeCellId_state === id) return;
|
|
388
|
+
const current = this.cells_state.find(c => c.id === this.activeCellId_state);
|
|
389
|
+
if (current && current.type === "markdown" && current.isEditingMarkdown) current.isEditingMarkdown = false;
|
|
390
|
+
this.activeCellId_state = id;
|
|
391
|
+
if (current) this._refreshCell(current);
|
|
392
|
+
const next = this.cells_state.find(c => c.id === id);
|
|
393
|
+
if (next) this._refreshCell(next);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
focusCell(targetId) {
|
|
397
|
+
const prevId = this.activeCellId_state;
|
|
398
|
+
this.activeCellId_state = targetId;
|
|
399
|
+
const target = this.cells_state.find(c => c.id === targetId);
|
|
400
|
+
if (target && target.type === "markdown" && !target.readonly) target.isEditingMarkdown = true;
|
|
401
|
+
if (prevId !== targetId) {
|
|
402
|
+
const prevCell = this.cells_state.find(c => c.id === prevId);
|
|
403
|
+
if (prevCell) this._refreshCell(prevCell);
|
|
404
|
+
}
|
|
405
|
+
if (target) this._refreshCell(target);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
addCell(type = "code", initialCode = null, afterId = null, readonly = false) {
|
|
409
|
+
if (this.cells_state.length >= this.maxCells) {
|
|
410
|
+
console.warn(`Cannot create cell: Maximum cell limit (${this.maxCells}) reached.`);
|
|
411
|
+
return null;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const newId = this.nextCellId_state++;
|
|
415
|
+
const defaultText = type === "markdown" ? "### New Markdown Cell\nClick to edit..." : "";
|
|
416
|
+
const newCell = {
|
|
417
|
+
id: newId, type, code: initialCode !== null ? initialCode : defaultText, readonly,
|
|
418
|
+
isEditingMarkdown: !readonly && type === "markdown" && initialCode === null,
|
|
419
|
+
execCount: null, outputNode: null, hasDomOutput: false, _copiedFlag_state: false,
|
|
420
|
+
_dom: null, _editor: null, _actions: null,
|
|
421
|
+
_inPrompt: null, _outPrompt: null, _bodySlot: null, _controlsEl: null, _outputGrid: null, _outputRef: null
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
if (afterId === null) {
|
|
425
|
+
this.cells_state.push(newCell);
|
|
426
|
+
} else {
|
|
427
|
+
const index = this.cells_state.findIndex(c => c.id === afterId);
|
|
428
|
+
this.cells_state.splice(index + 1, 0, newCell);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
this._renderAllCells();
|
|
432
|
+
this.focusCell(newId);
|
|
433
|
+
return newId;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
deleteCell(id) {
|
|
437
|
+
if (this.cells_state.length <= 1) return;
|
|
438
|
+
const index = this.cells_state.findIndex(c => c.id === id);
|
|
439
|
+
this.cells_state = this.cells_state.filter(c => c.id !== id);
|
|
440
|
+
this._renderAllCells();
|
|
441
|
+
const nextActiveIndex = Math.min(index, this.cells_state.length - 1);
|
|
442
|
+
this.focusCell(this.cells_state[nextActiveIndex].id);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
nextCell(currentId, defaultType) {
|
|
446
|
+
const idx = this.cells_state.findIndex(c => c.id === currentId);
|
|
447
|
+
if (idx === this.cells_state.length - 1) this.addCell(defaultType, null, currentId);
|
|
448
|
+
else this.focusCell(this.cells_state[idx + 1].id);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
async runCell(id) {
|
|
452
|
+
const cell = this.cells_state.find(c => c.id === id);
|
|
453
|
+
if (cell && cell._actions) await cell._actions.runCode(false);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
async runAllCells() {
|
|
457
|
+
const codeCells = this.cells_state.filter(c => c.type === "code");
|
|
458
|
+
if (codeCells.length === 0) return;
|
|
459
|
+
|
|
460
|
+
this.executionQueueState_state = { isRunning: true, currentIndex: 0, total: codeCells.length };
|
|
461
|
+
this._updateHeaderUI();
|
|
462
|
+
this.cells_state.forEach(c => this._refreshCellControls(c));
|
|
463
|
+
|
|
464
|
+
for (const cell of this.cells_state) {
|
|
465
|
+
if (cell.type === "code" && cell._actions) {
|
|
466
|
+
this.executionQueueState_state = { ...this.executionQueueState_state, currentIndex: codeCells.indexOf(cell) };
|
|
467
|
+
this._updateHeaderUI();
|
|
468
|
+
await cell._actions.runCode(false);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
this.executionQueueState_state = { isRunning: false, currentIndex: 0, total: 0 };
|
|
473
|
+
this._updateHeaderUI();
|
|
474
|
+
this.cells_state.forEach(c => this._refreshCellControls(c));
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
clearOutputs() {
|
|
478
|
+
this.cells_state.forEach(cell => {
|
|
479
|
+
cell.execCount = null;
|
|
480
|
+
cell.outputNode = null;
|
|
481
|
+
cell.hasDomOutput = false;
|
|
482
|
+
if (cell._outputRef) cell._outputRef.innerHTML = "";
|
|
483
|
+
});
|
|
484
|
+
this.executionCounter_state = 1;
|
|
485
|
+
this.cells_state.forEach(c => this._refreshCell(c));
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
resetScope() {
|
|
489
|
+
window.__notebook_scope = Object.create(null);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
getNotebookData({ inputs = true, outputs = true } = {}) {
|
|
493
|
+
return this.cells_state.map(cell => {
|
|
494
|
+
const exportedCell = { id: cell.id, type: cell.type, readonly: cell.readonly, order: cell.execCount };
|
|
495
|
+
if (inputs) exportedCell.code = cell.code;
|
|
496
|
+
if (outputs) exportedCell.outputHtml = cell.outputNode ? cell.outputNode.innerHTML : null;
|
|
497
|
+
return exportedCell;
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
}
|
package/src/transform.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import van from "vanjs-core";
|
|
2
|
+
import * as acorn from "acorn";
|
|
3
|
+
|
|
4
|
+
export const rewriteImportNode = (node, code, importMapConfig) => {
|
|
5
|
+
let source = node.source.value;
|
|
6
|
+
if (importMapConfig && importMapConfig[source]) {
|
|
7
|
+
source = importMapConfig[source];
|
|
8
|
+
} else if (!source.startsWith("http://") && !source.startsWith("https://") && !source.startsWith("./") && !source.startsWith("../")) {
|
|
9
|
+
source = `https://esm.sh/${source}`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (node.specifiers.length === 0) return `await import("${source}");`;
|
|
13
|
+
|
|
14
|
+
let defaultImportName = null;
|
|
15
|
+
let namespaceId = null;
|
|
16
|
+
let namedImports = [];
|
|
17
|
+
|
|
18
|
+
node.specifiers.forEach(spec => {
|
|
19
|
+
if (spec.type === "ImportNamespaceSpecifier") namespaceId = spec.local.name;
|
|
20
|
+
else if (spec.type === "ImportDefaultSpecifier") defaultImportName = spec.local.name;
|
|
21
|
+
else if (spec.type === "ImportSpecifier") namedImports.push({ imported: spec.imported.name, local: spec.local.name });
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
if (namespaceId) return `window.__notebook_scope.${namespaceId} = await import("${source}");`;
|
|
25
|
+
|
|
26
|
+
const tmp = `__mod_${node.start}`;
|
|
27
|
+
let lines = [`const ${tmp} = await import("${source}");`];
|
|
28
|
+
|
|
29
|
+
if (defaultImportName && namedImports.length === 0) {
|
|
30
|
+
lines.push(`window.__notebook_scope.${defaultImportName} = ${tmp}.default ?? ${tmp};`);
|
|
31
|
+
return lines.join("\n");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (defaultImportName) lines.push(`window.__notebook_scope.${defaultImportName} = ${tmp}.default ?? ${tmp};`);
|
|
35
|
+
namedImports.forEach(({ imported, local }) => {
|
|
36
|
+
lines.push(`window.__notebook_scope.${local} = ${tmp}.${imported} !== undefined ? ${tmp}.${imported} : (${tmp}.default ?? ${tmp});`);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
return lines.join("\n");
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const transformImportsAndScope = (code, importMapConfig) => {
|
|
43
|
+
try {
|
|
44
|
+
const ast = acorn.parse(code, { ecmaVersion: "latest", sourceType: "module", allowReturnOutsideFunction: true });
|
|
45
|
+
let modifications = [];
|
|
46
|
+
|
|
47
|
+
ast.body.forEach(node => {
|
|
48
|
+
if (node.type === "ImportDeclaration") {
|
|
49
|
+
modifications.push({ start: node.start, end: node.end, replacement: rewriteImportNode(node, code, importMapConfig) });
|
|
50
|
+
} else if (node.type === "VariableDeclaration") {
|
|
51
|
+
let replacementCode = "";
|
|
52
|
+
node.declarations.forEach(decl => {
|
|
53
|
+
if (decl.id.type === "Identifier") {
|
|
54
|
+
const varName = decl.id.name;
|
|
55
|
+
const initCode = decl.init ? code.slice(decl.init.start, decl.init.end) : "undefined";
|
|
56
|
+
replacementCode += `window.__notebook_scope.${varName} = ${initCode};\n`;
|
|
57
|
+
} else if (decl.id.type === "ObjectPattern") {
|
|
58
|
+
const initCode = decl.init ? code.slice(decl.init.start, decl.init.end) : "undefined";
|
|
59
|
+
const tempVar = `__destruct_obj_${decl.start}`;
|
|
60
|
+
replacementCode += `(() => { const ${tempVar} = ${initCode}; `;
|
|
61
|
+
decl.id.properties.forEach(prop => {
|
|
62
|
+
if (prop.value && prop.value.type === "Identifier") {
|
|
63
|
+
const localName = prop.value.name;
|
|
64
|
+
const keyName = prop.key.type === "Identifier" ? prop.key.name : prop.key.value;
|
|
65
|
+
replacementCode += `window.__notebook_scope.${localName} = ${tempVar}.${keyName}; `;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
replacementCode += `})();\n`;
|
|
69
|
+
} else if (decl.id.type === "ArrayPattern") {
|
|
70
|
+
const initCode = decl.init ? code.slice(decl.init.start, decl.init.end) : "undefined";
|
|
71
|
+
const tempVar = `__destruct_arr_${decl.start}`;
|
|
72
|
+
replacementCode += `(() => { const ${tempVar} = ${initCode}; `;
|
|
73
|
+
decl.id.elements.forEach((elem, index) => {
|
|
74
|
+
if (elem && elem.type === "Identifier") {
|
|
75
|
+
const localName = elem.name;
|
|
76
|
+
replacementCode += `window.__notebook_scope.${localName} = ${tempVar}[${index}]; `;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
replacementCode += `})();\n`;
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
modifications.push({ start: node.start, end: node.end, replacement: replacementCode });
|
|
83
|
+
} else if (node.type === "FunctionDeclaration" && node.id) {
|
|
84
|
+
const funcName = node.id.name;
|
|
85
|
+
const funcBody = code.slice(node.start, node.end);
|
|
86
|
+
modifications.push({ start: node.start, end: node.end, replacement: `window.__notebook_scope.${funcName} = ${funcBody.replace(/^function\s+\w+/, "function")}` });
|
|
87
|
+
} else if (node.type === "ClassDeclaration" && node.id) {
|
|
88
|
+
const className = node.id.name;
|
|
89
|
+
const classBody = code.slice(node.start, node.end);
|
|
90
|
+
modifications.push({ start: node.start, end: node.end, replacement: `window.__notebook_scope.${className} = ${classBody.replace(/^class\s+\w+/, "class")}` });
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
modifications.sort((a, b) => b.start - a.start);
|
|
95
|
+
let transformedCode = code;
|
|
96
|
+
modifications.forEach(mod => { transformedCode = transformedCode.slice(0, mod.start) + mod.replacement + transformedCode.slice(mod.end); });
|
|
97
|
+
return transformedCode;
|
|
98
|
+
} catch (e) {
|
|
99
|
+
if (code.trim().startsWith("import ") || code.includes("\nimport ")) throw new SyntaxError("Invalid code module layout syntax.");
|
|
100
|
+
return code;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export const evaluateCodeAsync = async (code, TARGET, importMapConfig) => {
|
|
105
|
+
const compiledCode = transformImportsAndScope(code, importMapConfig);
|
|
106
|
+
|
|
107
|
+
const runCell = new Function("TARGET", "van", `
|
|
108
|
+
return (async () => {
|
|
109
|
+
with (window.__notebook_scope) {
|
|
110
|
+
${compiledCode}
|
|
111
|
+
}
|
|
112
|
+
})();
|
|
113
|
+
`);
|
|
114
|
+
|
|
115
|
+
const result = await runCell(TARGET, van);
|
|
116
|
+
if (result !== undefined && result !== null) {
|
|
117
|
+
van.add(TARGET, result);
|
|
118
|
+
}
|
|
119
|
+
};
|
package/src/ui.js
ADDED