@zikojs/notebook 0.14.2 → 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 +3 -4
- package/src/code-editor/index.js +302 -249
- package/src/compiler/evaluate.js +129 -120
- package/src/compiler/imports.js +189 -126
- package/src/compiler/index.js +11 -11
- package/src/compiler/scope.js +94 -94
- package/src/compiler/transform.js +115 -111
- 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 -132
- 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 -172
- package/src/index.js +1 -1
- package/src/index.ts +96 -96
- package/src/notebook.js +816 -792
- package/src/store.js +37 -37
package/src/compiler/scope.js
CHANGED
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
export const rewriteVariableDeclaration = (
|
|
2
|
-
node,
|
|
3
|
-
code,
|
|
4
|
-
) => {
|
|
5
|
-
let replacementCode = "";
|
|
6
|
-
|
|
7
|
-
node.declarations.forEach((decl) => {
|
|
8
|
-
const initCode = decl.init
|
|
9
|
-
? code.slice(decl.init.start, decl.init.end)
|
|
10
|
-
: "undefined";
|
|
11
|
-
|
|
12
|
-
if (decl.id.type === "Identifier") {
|
|
13
|
-
const varName = decl.id.name;
|
|
14
|
-
|
|
15
|
-
replacementCode +=
|
|
16
|
-
`__scope__.${varName} = ${initCode};\n`;
|
|
17
|
-
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (decl.id.type === "ObjectPattern") {
|
|
22
|
-
const tempVar = `__destruct_obj_${decl.start}`;
|
|
23
|
-
|
|
24
|
-
replacementCode +=
|
|
25
|
-
`(() => { const ${tempVar} = ${initCode}; `;
|
|
26
|
-
|
|
27
|
-
decl.id.properties.forEach((prop) => {
|
|
28
|
-
if (
|
|
29
|
-
prop.value &&
|
|
30
|
-
prop.value.type === "Identifier"
|
|
31
|
-
) {
|
|
32
|
-
const localName = prop.value.name;
|
|
33
|
-
|
|
34
|
-
const keyName =
|
|
35
|
-
prop.key.type === "Identifier"
|
|
36
|
-
? prop.key.name
|
|
37
|
-
: prop.key.value;
|
|
38
|
-
|
|
39
|
-
replacementCode +=
|
|
40
|
-
`__scope__.${localName} = ` +
|
|
41
|
-
`${tempVar}.${keyName}; `;
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
replacementCode += `})();\n`;
|
|
46
|
-
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (decl.id.type === "ArrayPattern") {
|
|
51
|
-
const tempVar = `__destruct_arr_${decl.start}`;
|
|
52
|
-
|
|
53
|
-
replacementCode +=
|
|
54
|
-
`(() => { const ${tempVar} = ${initCode}; `;
|
|
55
|
-
|
|
56
|
-
decl.id.elements.forEach((elem, index) => {
|
|
57
|
-
if (elem && elem.type === "Identifier") {
|
|
58
|
-
replacementCode +=
|
|
59
|
-
`__scope__.${elem.name} = ` +
|
|
60
|
-
`${tempVar}[${index}]; `;
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
replacementCode += `})();\n`;
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
return replacementCode;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
export const rewriteFunctionDeclaration = (
|
|
72
|
-
node,
|
|
73
|
-
code,
|
|
74
|
-
) => {
|
|
75
|
-
const name = node.id.name;
|
|
76
|
-
const body = code.slice(node.start, node.end);
|
|
77
|
-
|
|
78
|
-
return (
|
|
79
|
-
`__scope__.${name} = ` +
|
|
80
|
-
body.replace(/^function\s+\w+/, "function")
|
|
81
|
-
);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
export const rewriteClassDeclaration = (
|
|
85
|
-
node,
|
|
86
|
-
code,
|
|
87
|
-
) => {
|
|
88
|
-
const name = node.id.name;
|
|
89
|
-
const body = code.slice(node.start, node.end);
|
|
90
|
-
|
|
91
|
-
return (
|
|
92
|
-
`__scope__.${name} = ` +
|
|
93
|
-
body.replace(/^class\s+\w+/, "class")
|
|
94
|
-
);
|
|
1
|
+
export const rewriteVariableDeclaration = (
|
|
2
|
+
node,
|
|
3
|
+
code,
|
|
4
|
+
) => {
|
|
5
|
+
let replacementCode = "";
|
|
6
|
+
|
|
7
|
+
node.declarations.forEach((decl) => {
|
|
8
|
+
const initCode = decl.init
|
|
9
|
+
? code.slice(decl.init.start, decl.init.end)
|
|
10
|
+
: "undefined";
|
|
11
|
+
|
|
12
|
+
if (decl.id.type === "Identifier") {
|
|
13
|
+
const varName = decl.id.name;
|
|
14
|
+
|
|
15
|
+
replacementCode +=
|
|
16
|
+
`__scope__.${varName} = ${initCode};\n`;
|
|
17
|
+
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (decl.id.type === "ObjectPattern") {
|
|
22
|
+
const tempVar = `__destruct_obj_${decl.start}`;
|
|
23
|
+
|
|
24
|
+
replacementCode +=
|
|
25
|
+
`(() => { const ${tempVar} = ${initCode}; `;
|
|
26
|
+
|
|
27
|
+
decl.id.properties.forEach((prop) => {
|
|
28
|
+
if (
|
|
29
|
+
prop.value &&
|
|
30
|
+
prop.value.type === "Identifier"
|
|
31
|
+
) {
|
|
32
|
+
const localName = prop.value.name;
|
|
33
|
+
|
|
34
|
+
const keyName =
|
|
35
|
+
prop.key.type === "Identifier"
|
|
36
|
+
? prop.key.name
|
|
37
|
+
: prop.key.value;
|
|
38
|
+
|
|
39
|
+
replacementCode +=
|
|
40
|
+
`__scope__.${localName} = ` +
|
|
41
|
+
`${tempVar}.${keyName}; `;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
replacementCode += `})();\n`;
|
|
46
|
+
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (decl.id.type === "ArrayPattern") {
|
|
51
|
+
const tempVar = `__destruct_arr_${decl.start}`;
|
|
52
|
+
|
|
53
|
+
replacementCode +=
|
|
54
|
+
`(() => { const ${tempVar} = ${initCode}; `;
|
|
55
|
+
|
|
56
|
+
decl.id.elements.forEach((elem, index) => {
|
|
57
|
+
if (elem && elem.type === "Identifier") {
|
|
58
|
+
replacementCode +=
|
|
59
|
+
`__scope__.${elem.name} = ` +
|
|
60
|
+
`${tempVar}[${index}]; `;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
replacementCode += `})();\n`;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return replacementCode;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const rewriteFunctionDeclaration = (
|
|
72
|
+
node,
|
|
73
|
+
code,
|
|
74
|
+
) => {
|
|
75
|
+
const name = node.id.name;
|
|
76
|
+
const body = code.slice(node.start, node.end);
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
`__scope__.${name} = ` +
|
|
80
|
+
body.replace(/^function\s+\w+/, "function")
|
|
81
|
+
);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export const rewriteClassDeclaration = (
|
|
85
|
+
node,
|
|
86
|
+
code,
|
|
87
|
+
) => {
|
|
88
|
+
const name = node.id.name;
|
|
89
|
+
const body = code.slice(node.start, node.end);
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
`__scope__.${name} = ` +
|
|
93
|
+
body.replace(/^class\s+\w+/, "class")
|
|
94
|
+
);
|
|
95
95
|
};
|
|
@@ -1,112 +1,116 @@
|
|
|
1
|
-
import * as acorn from "acorn";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
rewriteImportNode,
|
|
5
|
-
} from "./imports.js";
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
rewriteVariableDeclaration,
|
|
9
|
-
rewriteFunctionDeclaration,
|
|
10
|
-
rewriteClassDeclaration,
|
|
11
|
-
} from "./scope.js";
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export const transformImportsAndScope = async (
|
|
15
|
-
code,
|
|
16
|
-
{
|
|
17
|
-
importMap = {},
|
|
18
|
-
plugins = [],
|
|
19
|
-
resolverEndpoint,
|
|
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
|
-
transformedCode.slice(modification.
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
1
|
+
import * as acorn from "acorn";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
rewriteImportNode,
|
|
5
|
+
} from "./imports.js";
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
rewriteVariableDeclaration,
|
|
9
|
+
rewriteFunctionDeclaration,
|
|
10
|
+
rewriteClassDeclaration,
|
|
11
|
+
} from "./scope.js";
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export const transformImportsAndScope = async (
|
|
15
|
+
code,
|
|
16
|
+
{
|
|
17
|
+
importMap = {},
|
|
18
|
+
plugins = [],
|
|
19
|
+
resolverEndpoint,
|
|
20
|
+
importScopeIdentifier = "__scope__",
|
|
21
|
+
} = {},
|
|
22
|
+
) => {
|
|
23
|
+
try {
|
|
24
|
+
const ast = acorn.parse(code, {
|
|
25
|
+
ecmaVersion: "latest",
|
|
26
|
+
sourceType: "module",
|
|
27
|
+
allowReturnOutsideFunction: true,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const context = {
|
|
31
|
+
importMap,
|
|
32
|
+
plugins,
|
|
33
|
+
resolverEndpoint,
|
|
34
|
+
importScopeIdentifier,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const modifications = [];
|
|
38
|
+
|
|
39
|
+
for (const node of ast.body) {
|
|
40
|
+
if (node.type === "ImportDeclaration") {
|
|
41
|
+
modifications.push({
|
|
42
|
+
start: node.start,
|
|
43
|
+
end: node.end,
|
|
44
|
+
replacement: await rewriteImportNode(
|
|
45
|
+
node,
|
|
46
|
+
code,
|
|
47
|
+
context,
|
|
48
|
+
),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (node.type === "VariableDeclaration") {
|
|
55
|
+
modifications.push({
|
|
56
|
+
start: node.start,
|
|
57
|
+
end: node.end,
|
|
58
|
+
replacement: rewriteVariableDeclaration(node, code),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (
|
|
65
|
+
node.type === "FunctionDeclaration" &&
|
|
66
|
+
node.id
|
|
67
|
+
) {
|
|
68
|
+
modifications.push({
|
|
69
|
+
start: node.start,
|
|
70
|
+
end: node.end,
|
|
71
|
+
replacement: rewriteFunctionDeclaration(node, code),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (
|
|
78
|
+
node.type === "ClassDeclaration" &&
|
|
79
|
+
node.id
|
|
80
|
+
) {
|
|
81
|
+
modifications.push({
|
|
82
|
+
start: node.start,
|
|
83
|
+
end: node.end,
|
|
84
|
+
replacement: rewriteClassDeclaration(node, code),
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
modifications.sort((a, b) => b.start - a.start);
|
|
90
|
+
|
|
91
|
+
let transformedCode = code;
|
|
92
|
+
|
|
93
|
+
for (const modification of modifications) {
|
|
94
|
+
transformedCode =
|
|
95
|
+
transformedCode.slice(0, modification.start) +
|
|
96
|
+
modification.replacement +
|
|
97
|
+
transformedCode.slice(modification.end);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
console.log(transformedCode)
|
|
101
|
+
|
|
102
|
+
return transformedCode;
|
|
103
|
+
} catch (error) {
|
|
104
|
+
if (
|
|
105
|
+
code.trim().startsWith("import ") ||
|
|
106
|
+
code.includes("\nimport ")
|
|
107
|
+
) {
|
|
108
|
+
throw new SyntaxError(
|
|
109
|
+
"Invalid code module layout syntax.",
|
|
110
|
+
{ cause: error },
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return code;
|
|
115
|
+
}
|
|
112
116
|
};
|
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
/* --- BUTTONS --- */
|
|
2
|
-
|
|
3
|
-
.ziko-notebook .btn {
|
|
4
|
-
display: inline-flex;
|
|
5
|
-
align-items: center;
|
|
6
|
-
justify-content: center;
|
|
7
|
-
gap: 6px;
|
|
8
|
-
background: #ffffff;
|
|
9
|
-
border: 1px solid var(--border-color);
|
|
10
|
-
padding: 6px 10px;
|
|
11
|
-
border-radius: 4px;
|
|
12
|
-
font-size: 12px;
|
|
13
|
-
font-weight: 500;
|
|
14
|
-
color: #334155;
|
|
15
|
-
cursor: pointer;
|
|
16
|
-
line-height: 1;
|
|
17
|
-
white-space: nowrap;
|
|
18
|
-
transition: background 0.12s ease, border-color 0.12s ease;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
.ziko-notebook .btn:hover {
|
|
22
|
-
background: #f1f5f9;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
.ziko-notebook .btn:disabled {
|
|
26
|
-
opacity: 0.5;
|
|
27
|
-
cursor: not-allowed;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
.ziko-notebook .btn-icon {
|
|
31
|
-
padding: 6px;
|
|
32
|
-
border-radius: 4px;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
.ziko-notebook .btn-primary {
|
|
36
|
-
background: var(--primary);
|
|
37
|
-
color: #ffffff;
|
|
38
|
-
border-color: var(--primary);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
.ziko-notebook .btn-primary:hover {
|
|
42
|
-
background: var(--primary-hover);
|
|
43
|
-
border-color: var(--primary-hover);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
.ziko-notebook .btn-active {
|
|
47
|
-
background: #e2e8f0;
|
|
48
|
-
border-color: #cbd5e1;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
.ziko-notebook .icon {
|
|
52
|
-
width: 14px;
|
|
53
|
-
height: 14px;
|
|
54
|
-
stroke-width: 2;
|
|
1
|
+
/* --- BUTTONS --- */
|
|
2
|
+
|
|
3
|
+
.ziko-notebook .btn {
|
|
4
|
+
display: inline-flex;
|
|
5
|
+
align-items: center;
|
|
6
|
+
justify-content: center;
|
|
7
|
+
gap: 6px;
|
|
8
|
+
background: #ffffff;
|
|
9
|
+
border: 1px solid var(--border-color);
|
|
10
|
+
padding: 6px 10px;
|
|
11
|
+
border-radius: 4px;
|
|
12
|
+
font-size: 12px;
|
|
13
|
+
font-weight: 500;
|
|
14
|
+
color: #334155;
|
|
15
|
+
cursor: pointer;
|
|
16
|
+
line-height: 1;
|
|
17
|
+
white-space: nowrap;
|
|
18
|
+
transition: background 0.12s ease, border-color 0.12s ease;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.ziko-notebook .btn:hover {
|
|
22
|
+
background: #f1f5f9;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.ziko-notebook .btn:disabled {
|
|
26
|
+
opacity: 0.5;
|
|
27
|
+
cursor: not-allowed;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
.ziko-notebook .btn-icon {
|
|
31
|
+
padding: 6px;
|
|
32
|
+
border-radius: 4px;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
.ziko-notebook .btn-primary {
|
|
36
|
+
background: var(--primary);
|
|
37
|
+
color: #ffffff;
|
|
38
|
+
border-color: var(--primary);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.ziko-notebook .btn-primary:hover {
|
|
42
|
+
background: var(--primary-hover);
|
|
43
|
+
border-color: var(--primary-hover);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.ziko-notebook .btn-active {
|
|
47
|
+
background: #e2e8f0;
|
|
48
|
+
border-color: #cbd5e1;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.ziko-notebook .icon {
|
|
52
|
+
width: 14px;
|
|
53
|
+
height: 14px;
|
|
54
|
+
stroke-width: 2;
|
|
55
55
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import van from "vanjs-core";
|
|
2
|
-
|
|
3
|
-
export const Button = ({ class: className = "", title = "", disabled = false, onclick }, ...children) =>
|
|
4
|
-
van.tags.button({ class: `btn ${className}`, title, disabled, onclick }, ...children);
|
|
1
|
+
import van from "vanjs-core";
|
|
2
|
+
|
|
3
|
+
export const Button = ({ class: className = "", title = "", disabled = false, onclick }, ...children) =>
|
|
4
|
+
van.tags.button({ class: `btn ${className}`, title, disabled, onclick }, ...children);
|