@tinyvolt/pie 0.9.0 → 0.11.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/README.md +18 -1
- package/dist/components/SplitPane.d.ts +21 -0
- package/dist/components/SplitPane.jsx +113 -0
- package/dist/components/icons.d.ts +12 -0
- package/dist/components/icons.jsx +45 -0
- package/dist/lib/autodiff/index.d.ts +28 -0
- package/dist/lib/autodiff/index.js +39 -0
- package/dist/lib/customModules.d.ts +19 -0
- package/dist/lib/customModules.js +52 -0
- package/dist/lib/markdown/compile.d.ts +27 -0
- package/dist/lib/markdown/compile.js +26 -0
- package/dist/lib/markdown/components/CanvasPage.d.ts +9 -0
- package/dist/lib/markdown/components/CanvasPage.jsx +16 -0
- package/dist/lib/markdown/components/MarkdownBody.d.ts +11 -0
- package/dist/lib/markdown/components/MarkdownBody.jsx +37 -0
- package/dist/lib/markdown/components/PiePage.d.ts +14 -0
- package/dist/lib/markdown/components/PiePage.jsx +42 -0
- package/dist/lib/markdown/components/PythonEditor.d.ts +10 -0
- package/dist/lib/markdown/components/PythonEditor.jsx +209 -0
- package/dist/lib/markdown/features/build.d.ts +60 -0
- package/dist/lib/markdown/features/build.js +36 -0
- package/dist/lib/markdown/features/glossary/View.d.ts +9 -0
- package/dist/lib/markdown/features/glossary/View.jsx +62 -0
- package/dist/lib/markdown/features/glossary/build.d.ts +6 -0
- package/dist/lib/markdown/features/glossary/build.js +81 -0
- package/dist/lib/markdown/features/sidebar/View.d.ts +11 -0
- package/dist/lib/markdown/features/sidebar/View.jsx +45 -0
- package/dist/lib/markdown/features/sidebar/build.d.ts +3 -0
- package/dist/lib/markdown/features/sidebar/build.js +7 -0
- package/dist/lib/markdown/features/tags/build.d.ts +7 -0
- package/dist/lib/markdown/features/tags/build.js +22 -0
- package/dist/lib/markdown/features/tags/url.d.ts +6 -0
- package/dist/lib/markdown/features/tags/url.js +10 -0
- package/dist/lib/markdown/features/toc/build.d.ts +7 -0
- package/dist/lib/markdown/features/toc/build.js +62 -0
- package/dist/lib/markdown/features/views.d.ts +4 -0
- package/dist/lib/markdown/features/views.jsx +11 -0
- package/dist/lib/markdown/frontmatter.d.ts +44 -0
- package/dist/lib/markdown/frontmatter.js +109 -0
- package/dist/lib/markdown/render.d.ts +6 -0
- package/dist/lib/markdown/render.js +95 -0
- package/dist/lib/nodes/animation.d.ts +14 -0
- package/dist/lib/nodes/animation.js +34 -0
- package/dist/lib/nodes/components/CenteredGrid.d.ts +20 -0
- package/dist/lib/nodes/components/CenteredGrid.jsx +44 -0
- package/dist/lib/nodes/components/NodeCanvas.d.ts +7 -0
- package/dist/lib/nodes/components/NodeCanvas.jsx +50 -0
- package/dist/lib/nodes/execute.d.ts +27 -0
- package/dist/lib/nodes/execute.js +159 -0
- package/dist/lib/nodes/functions/Node2NodeFunc.d.ts +70 -0
- package/dist/lib/nodes/functions/Node2NodeFunc.js +223 -0
- package/dist/lib/nodes/functions/array.d.ts +12 -0
- package/dist/lib/nodes/functions/array.js +31 -0
- package/dist/lib/nodes/functions/autodiff.d.ts +12 -0
- package/dist/lib/nodes/functions/autodiff.js +60 -0
- package/dist/lib/nodes/functions/basic.d.ts +111 -0
- package/dist/lib/nodes/functions/basic.js +275 -0
- package/dist/lib/nodes/functions/registry.d.ts +9 -0
- package/dist/lib/nodes/functions/registry.js +45 -0
- package/dist/lib/nodes/functions/sideEffects.d.ts +78 -0
- package/dist/lib/nodes/functions/sideEffects.js +201 -0
- package/dist/lib/nodes/hooks/useCanvasGestures.d.ts +9 -0
- package/dist/lib/nodes/hooks/useCanvasGestures.js +120 -0
- package/dist/lib/nodes/hooks/useCanvasResize.d.ts +2 -0
- package/dist/lib/nodes/hooks/useCanvasResize.js +33 -0
- package/dist/lib/nodes/jax.d.ts +46 -0
- package/dist/lib/nodes/jax.js +120 -0
- package/dist/lib/nodes/render/renderLogic.d.ts +13 -0
- package/dist/lib/nodes/render/renderLogic.jsx +234 -0
- package/dist/lib/nodes/state/NameGenerator.d.ts +10 -0
- package/dist/lib/nodes/state/NameGenerator.js +29 -0
- package/dist/lib/nodes/state/Node.d.ts +134 -0
- package/dist/lib/nodes/state/Node.js +57 -0
- package/dist/lib/nodes/state/NodeStore.d.ts +49 -0
- package/dist/lib/nodes/state/NodeStore.js +138 -0
- package/dist/lib/nodes/tensors/broadcast.d.ts +18 -0
- package/dist/lib/nodes/tensors/broadcast.js +92 -0
- package/dist/lib/nodes/tensors/tensor.d.ts +13 -0
- package/dist/lib/nodes/tensors/tensor.js +34 -0
- package/dist/lib/python/lift.d.ts +51 -0
- package/dist/lib/python/lift.js +155 -0
- package/dist/lib/python/py/pie.py +303 -0
- package/dist/lib/python/py/pie_mpl.py +15 -0
- package/dist/lib/python/py/show_figures.py +5 -0
- package/dist/lib/python/runPage.d.ts +21 -0
- package/dist/lib/python/runPage.js +60 -0
- package/dist/lib/python/runner.d.ts +68 -0
- package/dist/lib/python/runner.js +243 -0
- package/dist/lib/python/worker.d.ts +1 -0
- package/dist/lib/python/worker.js +111 -0
- package/dist/lib/ui/UiLayer.d.ts +2 -0
- package/dist/lib/ui/UiLayer.jsx +15 -0
- package/dist/lib/ui/UiView.d.ts +23 -0
- package/dist/lib/ui/UiView.jsx +56 -0
- package/dist/lib/ui/components/checkboxes.d.ts +10 -0
- package/dist/lib/ui/components/checkboxes.jsx +30 -0
- package/dist/lib/ui/components/latex.d.ts +8 -0
- package/dist/lib/ui/components/latex.jsx +41 -0
- package/dist/lib/ui/components/radio.d.ts +6 -0
- package/dist/lib/ui/components/radio.jsx +19 -0
- package/dist/lib/ui/components/slider.d.ts +8 -0
- package/dist/lib/ui/components/slider.jsx +7 -0
- package/dist/lib/ui/components/textbox.d.ts +5 -0
- package/dist/lib/ui/components/textbox.jsx +21 -0
- package/dist/lib/ui/dragUi.d.ts +10 -0
- package/dist/lib/ui/dragUi.js +129 -0
- package/dist/lib/ui/nodeHandle.d.ts +15 -0
- package/dist/lib/ui/nodeHandle.js +37 -0
- package/dist/lib/ui/registry.d.ts +10 -0
- package/dist/lib/ui/registry.js +42 -0
- package/dist/runtime.css +696 -0
- package/dist/runtime.d.ts +10 -0
- package/dist/runtime.jsx +7 -0
- package/init/index.js +2 -2
- package/init/site-agents.md +2 -1
- package/package.json +15 -6
- package/runtime-agents.md +168 -0
- package/src/app.css +7 -665
- package/src/lib/customModules.ts +67 -0
- package/src/lib/markdown/compile.ts +49 -0
- package/src/lib/markdown/components/PiePage.tsx +49 -0
- package/src/lib/markdown/features/build.ts +15 -6
- package/src/lib/markdown/features/sidebar/View.tsx +4 -2
- package/src/lib/markdown/features/tags/build.ts +6 -3
- package/src/lib/markdown/pages.ts +1 -9
- package/src/lib/markdown/vitePlugin.ts +4 -8
- package/src/lib/nodes/functions/registry.ts +30 -21
- package/src/lib/nodes/functions/sideEffects.ts +14 -8
- package/src/lib/nodes/functions/vitePlugin.ts +8 -10
- package/src/lib/nodes/state/Node.ts +27 -19
- package/src/lib/python/runPage.ts +78 -0
- package/src/lib/python/runner.ts +189 -164
- package/src/lib/siteModules.ts +13 -0
- package/src/lib/ui/dragUi.ts +4 -2
- package/src/lib/ui/registry.ts +35 -14
- package/src/page.css +664 -0
- package/src/routes/[...slug].tsx +1 -0
- package/src/runtime.css +29 -0
- package/src/runtime.tsx +16 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { num } from "../jax";
|
|
2
|
+
import { For, Show } from "solid-js";
|
|
3
|
+
// Sizes are in pixels. Strokes use `non-scaling-stroke` so their width ignores zoom;
|
|
4
|
+
// the point radius is divided by `unit` (pixels per grid unit) instead.
|
|
5
|
+
const COLOR = 'currentColor';
|
|
6
|
+
const STROKE_WIDTH = 2;
|
|
7
|
+
const POINT_RADIUS = 4;
|
|
8
|
+
const ARROW_HEAD = 10;
|
|
9
|
+
// Each render function runs once per element. It reads the node accessor inside
|
|
10
|
+
// the attributes, so a node change updates those attributes in place.
|
|
11
|
+
const renderPoint = (point, unit) => {
|
|
12
|
+
return (<circle cx={num(point().x)} cy={num(point().y)} r={POINT_RADIUS / unit()} fill={point().fill ?? COLOR}/>);
|
|
13
|
+
};
|
|
14
|
+
const renderLine = (line) => {
|
|
15
|
+
return (<Show when={line().p1 && line().p2}>
|
|
16
|
+
<line x1={num(line().p1.x)} y1={num(line().p1.y)} x2={num(line().p2.x)} y2={num(line().p2.y)} stroke={line().stroke ?? COLOR} stroke-width={STROKE_WIDTH} vector-effect="non-scaling-stroke"/>
|
|
17
|
+
</Show>);
|
|
18
|
+
};
|
|
19
|
+
const renderTriangle = (triangle) => {
|
|
20
|
+
const pointsStr = () => {
|
|
21
|
+
const [p1, p2, p3] = triangle().vertices;
|
|
22
|
+
return `${num(p1.x)},${num(p1.y)} ${num(p2.x)},${num(p2.y)} ${num(p3.x)},${num(p3.y)}`;
|
|
23
|
+
};
|
|
24
|
+
return (<Show when={triangle().vertices[0] && triangle().vertices[1] && triangle().vertices[2]}>
|
|
25
|
+
<polygon points={pointsStr()} fill={triangle().fill ?? 'none'} stroke={triangle().stroke ?? COLOR} stroke-width={STROKE_WIDTH} vector-effect="non-scaling-stroke"/>
|
|
26
|
+
</Show>);
|
|
27
|
+
};
|
|
28
|
+
const renderCircle = (circle) => {
|
|
29
|
+
return (<Show when={circle().center}>
|
|
30
|
+
<circle cx={num(circle().center.x)} cy={num(circle().center.y)} r={num(circle().radius.value)} fill={circle().fill ?? 'none'} stroke={circle().stroke ?? COLOR} stroke-width={STROKE_WIDTH} vector-effect="non-scaling-stroke"/>
|
|
31
|
+
</Show>);
|
|
32
|
+
};
|
|
33
|
+
const renderEllipse = (ellipse) => {
|
|
34
|
+
const cx = () => num(ellipse().center.x);
|
|
35
|
+
const cy = () => num(ellipse().center.y);
|
|
36
|
+
const rx = () => num(ellipse().radiusX.value);
|
|
37
|
+
const ry = () => num(ellipse().radiusY.value);
|
|
38
|
+
const rotation = () => num(ellipse().rotation.value);
|
|
39
|
+
return (<Show when={ellipse().center}>
|
|
40
|
+
<ellipse cx={cx()} cy={cy()} rx={rx()} ry={ry()} transform={`rotate(${rotation()} ${cx()} ${cy()})`} fill={ellipse().fill ?? 'none'} stroke={ellipse().stroke ?? COLOR} stroke-width={STROKE_WIDTH} vector-effect="non-scaling-stroke"/>
|
|
41
|
+
</Show>);
|
|
42
|
+
};
|
|
43
|
+
const renderArc = (arc) => {
|
|
44
|
+
const pathData = () => {
|
|
45
|
+
const center = arc().center;
|
|
46
|
+
const startAngleRad = (num(arc().startAngle.value) * Math.PI) / 180;
|
|
47
|
+
const endAngleRad = (num(arc().endAngle.value) * Math.PI) / 180;
|
|
48
|
+
const arcRadius = num(arc().radius.value);
|
|
49
|
+
const cx = num(center.x), cy = num(center.y);
|
|
50
|
+
const startX = cx + arcRadius * Math.cos(startAngleRad);
|
|
51
|
+
const startY = cy + arcRadius * Math.sin(startAngleRad);
|
|
52
|
+
const endX = cx + arcRadius * Math.cos(endAngleRad);
|
|
53
|
+
const endY = cy + arcRadius * Math.sin(endAngleRad);
|
|
54
|
+
const largeArcFlag = Math.abs(num(arc().endAngle.value) - num(arc().startAngle.value)) > 180 ? 1 : 0;
|
|
55
|
+
return `M ${startX},${startY} A ${arcRadius},${arcRadius} 0 ${largeArcFlag} 1 ${endX},${endY}`;
|
|
56
|
+
};
|
|
57
|
+
return (<Show when={arc().center}>
|
|
58
|
+
<path d={pathData()} fill={arc().fill ?? 'none'} stroke={arc().stroke ?? COLOR} stroke-width={STROKE_WIDTH} vector-effect="non-scaling-stroke"/>
|
|
59
|
+
</Show>);
|
|
60
|
+
};
|
|
61
|
+
const renderBezierQuadratic = (bezier) => {
|
|
62
|
+
const pathData = () => {
|
|
63
|
+
const { p1, control, p2 } = bezier();
|
|
64
|
+
return `M ${num(p1.x)},${num(p1.y)} Q ${num(control.x)},${num(control.y)} ${num(p2.x)},${num(p2.y)}`;
|
|
65
|
+
};
|
|
66
|
+
return (<Show when={bezier().p1 && bezier().control && bezier().p2}>
|
|
67
|
+
<path d={pathData()} fill={bezier().fill ?? 'none'} stroke={bezier().stroke ?? COLOR} stroke-width={STROKE_WIDTH} vector-effect="non-scaling-stroke"/>
|
|
68
|
+
</Show>);
|
|
69
|
+
};
|
|
70
|
+
const renderBezierCubic = (bezier) => {
|
|
71
|
+
const pathData = () => {
|
|
72
|
+
const { p1, control1, control2, p2 } = bezier();
|
|
73
|
+
return `M ${num(p1.x)},${num(p1.y)} C ${num(control1.x)},${num(control1.y)} ${num(control2.x)},${num(control2.y)} ${num(p2.x)},${num(p2.y)}`;
|
|
74
|
+
};
|
|
75
|
+
return (<Show when={bezier().p1 && bezier().control1 && bezier().control2 && bezier().p2}>
|
|
76
|
+
<path d={pathData()} fill={bezier().fill ?? 'none'} stroke={bezier().stroke ?? COLOR} stroke-width={STROKE_WIDTH} vector-effect="non-scaling-stroke"/>
|
|
77
|
+
</Show>);
|
|
78
|
+
};
|
|
79
|
+
// Draws (dx, dy) in grid units, starting at the point. No arrow when it is zero or NaN.
|
|
80
|
+
const renderPointGrad = (grad, unit) => {
|
|
81
|
+
const arrow = () => {
|
|
82
|
+
const g = grad();
|
|
83
|
+
const x = num(g.point.x), y = num(g.point.y), dx = num(g.dx), dy = num(g.dy);
|
|
84
|
+
const length = Math.hypot(dx, dy);
|
|
85
|
+
if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(length) || length === 0)
|
|
86
|
+
return null;
|
|
87
|
+
const ux = dx / length, uy = dy / length;
|
|
88
|
+
// The head shrinks to fit an arrow shorter than it.
|
|
89
|
+
const head = Math.min(ARROW_HEAD / unit(), length);
|
|
90
|
+
const tipX = x + dx, tipY = y + dy;
|
|
91
|
+
const baseX = tipX - head * ux, baseY = tipY - head * uy;
|
|
92
|
+
const half = head / 2;
|
|
93
|
+
return {
|
|
94
|
+
x, y, baseX, baseY,
|
|
95
|
+
head: `${tipX},${tipY} ${baseX - half * uy},${baseY + half * ux} ${baseX + half * uy},${baseY - half * ux}`,
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
return (<Show when={arrow()}>
|
|
99
|
+
{(a) => (<>
|
|
100
|
+
<line x1={a().x} y1={a().y} x2={a().baseX} y2={a().baseY} stroke={grad().stroke ?? COLOR} stroke-width={STROKE_WIDTH} vector-effect="non-scaling-stroke"/>
|
|
101
|
+
<polygon points={a().head} fill={grad().fill ?? COLOR}/>
|
|
102
|
+
</>)}
|
|
103
|
+
</Show>);
|
|
104
|
+
};
|
|
105
|
+
// A box's size in grid units when its width or height fits the text.
|
|
106
|
+
const TEXT_MAX_AUTO = 5;
|
|
107
|
+
// CSS variables don't work in SVG attributes, only in `style`.
|
|
108
|
+
const PAGE_BACKGROUND = 'var(--color-bg, #fff)';
|
|
109
|
+
/** Sizes in grid units. `unit` is pixels per grid unit. */
|
|
110
|
+
function textLayout(t, unit) {
|
|
111
|
+
const text = t.value;
|
|
112
|
+
const fs = (t.fontSize ? num(t.fontSize.value) : 12) / unit;
|
|
113
|
+
const lineHeight = fs * 1.4;
|
|
114
|
+
const rawW = t.w ? num(t.w.value) : -1;
|
|
115
|
+
const rawH = t.h ? num(t.h.value) : -1;
|
|
116
|
+
if (rawW !== 0 || rawH !== 0) {
|
|
117
|
+
const charW = 0.55 * fs;
|
|
118
|
+
// Space between the box's edge and the text.
|
|
119
|
+
const bgPad = 10 / unit;
|
|
120
|
+
// 0 means auto here too, when the other one isn't 0.
|
|
121
|
+
const wAuto = rawW <= 0;
|
|
122
|
+
const hAuto = rawH <= 0;
|
|
123
|
+
// Breaks at whitespace and after hyphens. A token longer than a line is cut.
|
|
124
|
+
const wrap = (charsPerLine) => {
|
|
125
|
+
const cpl = Math.max(1, charsPerLine);
|
|
126
|
+
const tokens = [];
|
|
127
|
+
for (const word of text.split(/\s+/).filter(Boolean)) {
|
|
128
|
+
for (const part of word.split(/(?<=-)/)) {
|
|
129
|
+
if (part.length <= cpl) {
|
|
130
|
+
tokens.push(part);
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
for (let i = 0; i < part.length; i += cpl)
|
|
134
|
+
tokens.push(part.slice(i, i + cpl));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const lines = [];
|
|
139
|
+
let current = '';
|
|
140
|
+
for (const token of tokens) {
|
|
141
|
+
const sep = current && !current.endsWith('-') ? ' ' : '';
|
|
142
|
+
const candidate = current + sep + token;
|
|
143
|
+
if (candidate.length <= cpl) {
|
|
144
|
+
current = candidate;
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
if (current)
|
|
148
|
+
lines.push(current);
|
|
149
|
+
current = token;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (current)
|
|
153
|
+
lines.push(current);
|
|
154
|
+
return lines;
|
|
155
|
+
};
|
|
156
|
+
// w and h are the outer box; the text fits in the box less its padding.
|
|
157
|
+
const innerFromOuter = (outer) => Math.max(0, outer - 2 * bgPad);
|
|
158
|
+
const innerCap = TEXT_MAX_AUTO - 2 * bgPad;
|
|
159
|
+
let innerW;
|
|
160
|
+
let lines;
|
|
161
|
+
if (!wAuto) {
|
|
162
|
+
innerW = innerFromOuter(rawW);
|
|
163
|
+
lines = wrap(Math.floor(innerW / charW));
|
|
164
|
+
}
|
|
165
|
+
else if (!hAuto) {
|
|
166
|
+
// Fixed height: widen until the text fits in the lines that fit.
|
|
167
|
+
const maxLines = Math.max(1, Math.floor(innerFromOuter(rawH) / lineHeight));
|
|
168
|
+
let cpl = Math.max(1, Math.ceil(text.length / maxLines));
|
|
169
|
+
lines = wrap(cpl);
|
|
170
|
+
while (lines.length > maxLines && cpl < text.length) {
|
|
171
|
+
cpl += 1;
|
|
172
|
+
lines = wrap(cpl);
|
|
173
|
+
}
|
|
174
|
+
innerW = lines.reduce((m, l) => Math.max(m, l.length), 1) * charW;
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
// Both auto: at most `TEXT_MAX_AUTO` wide, shrunk to the longest line.
|
|
178
|
+
lines = wrap(Math.floor(innerCap / charW));
|
|
179
|
+
innerW = Math.min(innerCap, lines.reduce((m, l) => Math.max(m, l.length), 1) * charW);
|
|
180
|
+
}
|
|
181
|
+
// Lines that don't fit a fixed or capped height are dropped.
|
|
182
|
+
let innerH;
|
|
183
|
+
if (!hAuto) {
|
|
184
|
+
innerH = innerFromOuter(rawH);
|
|
185
|
+
lines = lines.slice(0, Math.max(1, Math.floor(innerH / lineHeight)));
|
|
186
|
+
}
|
|
187
|
+
else if (!wAuto) {
|
|
188
|
+
innerH = lines.length * lineHeight;
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
lines = lines.slice(0, Math.max(1, Math.floor(innerCap / lineHeight)));
|
|
192
|
+
innerH = lines.length * lineHeight;
|
|
193
|
+
}
|
|
194
|
+
const outerW = wAuto ? innerW + 2 * bgPad : rawW;
|
|
195
|
+
const outerH = hAuto ? innerH + 2 * bgPad : rawH;
|
|
196
|
+
return { boxed: true, fs, lineHeight, lines, outerW, outerH };
|
|
197
|
+
}
|
|
198
|
+
const bgPad = 4 / unit;
|
|
199
|
+
return {
|
|
200
|
+
boxed: false, fs, lineHeight, lines: [text],
|
|
201
|
+
outerW: text.length * 0.55 * fs + 2 * bgPad,
|
|
202
|
+
outerH: lineHeight + 2 * bgPad,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
// Centered at (x, y). Not drawn without both. A box gets the page background and a border in the text's color.
|
|
206
|
+
const renderText = (text, unit) => {
|
|
207
|
+
const box = () => {
|
|
208
|
+
const t = text();
|
|
209
|
+
if (!t.x || !t.y)
|
|
210
|
+
return null;
|
|
211
|
+
const layout = textLayout(t, unit());
|
|
212
|
+
const cx = num(t.x.value), cy = num(t.y.value);
|
|
213
|
+
// y grows upward: the first line is the highest.
|
|
214
|
+
const firstY = cy + (layout.lines.length * layout.lineHeight) / 2 - layout.lineHeight / 2;
|
|
215
|
+
// `stroke` colors the text and the border; `fill` isn't used.
|
|
216
|
+
return { ...layout, cx, cy, firstY, color: t.stroke ?? COLOR };
|
|
217
|
+
};
|
|
218
|
+
return (<Show when={box()}>
|
|
219
|
+
{(b) => (<>
|
|
220
|
+
{/* Unboxed, it is invisible: it only catches the hover. */}
|
|
221
|
+
<rect x={b().cx - b().outerW / 2} y={b().cy - b().outerH / 2} width={b().outerW} height={b().outerH} rx={(b().boxed ? 6 : 2) / unit()} style={{ fill: PAGE_BACKGROUND, 'fill-opacity': b().boxed ? 1 : 0 }} stroke={b().boxed ? b().color : 'none'} stroke-width={1} vector-effect="non-scaling-stroke"/>
|
|
222
|
+
<For each={b().lines}>
|
|
223
|
+
{(line, i) => (
|
|
224
|
+
// scale(1, -1) undoes CenteredGrid's flip, so the text is upright.
|
|
225
|
+
<g transform={`translate(${b().cx}, ${b().firstY - i() * b().lineHeight}) scale(1, -1)`}>
|
|
226
|
+
<text x={0} y={0} font-size={`${b().fs}`} text-anchor="middle" dominant-baseline="central" fill={b().color}>
|
|
227
|
+
{line}
|
|
228
|
+
</text>
|
|
229
|
+
</g>)}
|
|
230
|
+
</For>
|
|
231
|
+
</>)}
|
|
232
|
+
</Show>);
|
|
233
|
+
};
|
|
234
|
+
export { renderPoint, renderLine, renderTriangle, renderCircle, renderEllipse, renderArc, renderBezierQuadratic, renderBezierCubic, renderPointGrad, renderText, };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { NodeType } from './Node';
|
|
2
|
+
export declare function getPrefixForType(type: NodeType | string): string;
|
|
3
|
+
export declare class NameGenerator {
|
|
4
|
+
private isTaken;
|
|
5
|
+
private counters;
|
|
6
|
+
constructor(isTaken?: (name: string) => boolean);
|
|
7
|
+
getPrefixForType(type: string): string;
|
|
8
|
+
generateAutoName(prefix: string): string;
|
|
9
|
+
resetCounters(): void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { getNodeDefinition } from './Node';
|
|
2
|
+
export function getPrefixForType(type) {
|
|
3
|
+
return getNodeDefinition(type)?.prefix ?? type.toLowerCase();
|
|
4
|
+
}
|
|
5
|
+
export class NameGenerator {
|
|
6
|
+
isTaken;
|
|
7
|
+
counters = {};
|
|
8
|
+
constructor(isTaken = () => false) {
|
|
9
|
+
this.isTaken = isTaken;
|
|
10
|
+
}
|
|
11
|
+
getPrefixForType(type) {
|
|
12
|
+
return getPrefixForType(type);
|
|
13
|
+
}
|
|
14
|
+
generateAutoName(prefix) {
|
|
15
|
+
/** An empty prefix (Dummy) always gives ''. */
|
|
16
|
+
if (prefix === '')
|
|
17
|
+
return '';
|
|
18
|
+
let name;
|
|
19
|
+
do {
|
|
20
|
+
const current = this.counters[prefix] || 0;
|
|
21
|
+
this.counters[prefix] = current + 1;
|
|
22
|
+
name = `${prefix}${current}`;
|
|
23
|
+
} while (this.isTaken(name));
|
|
24
|
+
return name;
|
|
25
|
+
}
|
|
26
|
+
resetCounters() {
|
|
27
|
+
this.counters = {};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { Accessor, JSXElement } from 'solid-js';
|
|
2
|
+
import type { CustomModule } from '../../customModules';
|
|
3
|
+
import type { Dtype } from '../jax';
|
|
4
|
+
export interface Node {
|
|
5
|
+
id: string;
|
|
6
|
+
type: string;
|
|
7
|
+
hidden?: boolean;
|
|
8
|
+
/** A CSS color or 'none'. */
|
|
9
|
+
fill?: string;
|
|
10
|
+
/** A CSS color or 'none'. */
|
|
11
|
+
stroke?: string;
|
|
12
|
+
__graph_deps?: {
|
|
13
|
+
funcKeyword: string;
|
|
14
|
+
inputNodes: AnyNode[];
|
|
15
|
+
blueprintKey?: string;
|
|
16
|
+
parentId?: string;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export declare function checkColor(keyword: string, name: string, value?: string): void;
|
|
20
|
+
export interface NodeDefinition<N extends Node = AnyNode> {
|
|
21
|
+
type: N['type'];
|
|
22
|
+
/** Start of generated ids: 'p' gives p0, p1, …. Default: `type` in lower case. */
|
|
23
|
+
prefix?: string;
|
|
24
|
+
/** Draws the node, in grid units. `unit` is pixels per grid unit. Without it the node is not drawn. */
|
|
25
|
+
render?(node: Accessor<N>, unit: Accessor<number>): JSXElement | null;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Drawn only when it has both `x` and `y`, the center of its box.
|
|
29
|
+
* `w`, `h`: the box size in grid units. 0 and 0: one line, no box. Above 0: fixed. -1: fits the text, at most 5.
|
|
30
|
+
*/
|
|
31
|
+
export interface TextNode extends Node {
|
|
32
|
+
type: 'Text';
|
|
33
|
+
value: string;
|
|
34
|
+
x?: ScalarNode;
|
|
35
|
+
y?: ScalarNode;
|
|
36
|
+
/** In pixels. */
|
|
37
|
+
fontSize?: ScalarNode;
|
|
38
|
+
w?: ScalarNode;
|
|
39
|
+
h?: ScalarNode;
|
|
40
|
+
}
|
|
41
|
+
export interface PointNode extends Node {
|
|
42
|
+
type: 'Point';
|
|
43
|
+
x: Dtype;
|
|
44
|
+
y: Dtype;
|
|
45
|
+
}
|
|
46
|
+
export interface ScalarNode extends Node {
|
|
47
|
+
type: 'Scalar';
|
|
48
|
+
value: Dtype;
|
|
49
|
+
}
|
|
50
|
+
export interface TriangleNode extends Node {
|
|
51
|
+
type: 'Triangle';
|
|
52
|
+
vertices: PointNode[];
|
|
53
|
+
}
|
|
54
|
+
export interface LineNode extends Node {
|
|
55
|
+
type: 'Line';
|
|
56
|
+
p1: PointNode;
|
|
57
|
+
p2: PointNode;
|
|
58
|
+
}
|
|
59
|
+
export interface CircleNode extends Node {
|
|
60
|
+
type: 'Circle';
|
|
61
|
+
center: PointNode;
|
|
62
|
+
radius: ScalarNode;
|
|
63
|
+
}
|
|
64
|
+
export interface EllipseNode extends Node {
|
|
65
|
+
type: 'Ellipse';
|
|
66
|
+
center: PointNode;
|
|
67
|
+
radiusX: ScalarNode;
|
|
68
|
+
radiusY: ScalarNode;
|
|
69
|
+
rotation: ScalarNode;
|
|
70
|
+
}
|
|
71
|
+
export interface BezierQuadraticNode extends Node {
|
|
72
|
+
type: 'BezierQuadratic';
|
|
73
|
+
p1: PointNode;
|
|
74
|
+
control: PointNode;
|
|
75
|
+
p2: PointNode;
|
|
76
|
+
}
|
|
77
|
+
export interface BezierCubicNode extends Node {
|
|
78
|
+
type: 'BezierCubic';
|
|
79
|
+
p1: PointNode;
|
|
80
|
+
control1: PointNode;
|
|
81
|
+
control2: PointNode;
|
|
82
|
+
p2: PointNode;
|
|
83
|
+
}
|
|
84
|
+
export interface ArcNode extends Node {
|
|
85
|
+
type: 'Arc';
|
|
86
|
+
center: PointNode;
|
|
87
|
+
radius: ScalarNode;
|
|
88
|
+
startAngle: ScalarNode;
|
|
89
|
+
endAngle: ScalarNode;
|
|
90
|
+
}
|
|
91
|
+
/** d target / d point, drawn as an arrow (dx, dy) starting at `point`. */
|
|
92
|
+
export interface PointGradNode extends Node {
|
|
93
|
+
type: 'PointGrad';
|
|
94
|
+
point: PointNode;
|
|
95
|
+
target: ScalarNode;
|
|
96
|
+
dx: Dtype;
|
|
97
|
+
dy: Dtype;
|
|
98
|
+
}
|
|
99
|
+
/** `items` in row-major order: shape [2, 3] holds row 0's three items, then row 1's. */
|
|
100
|
+
export interface ArrayNode extends Node {
|
|
101
|
+
type: 'Array';
|
|
102
|
+
/** 'Mixed' when the items are not all one type. */
|
|
103
|
+
itemType: NodeType | 'Mixed';
|
|
104
|
+
shape: number[];
|
|
105
|
+
items: AnyNode[];
|
|
106
|
+
}
|
|
107
|
+
export interface DummyNode extends Node {
|
|
108
|
+
type: 'Dummy';
|
|
109
|
+
}
|
|
110
|
+
export interface NodeTypeMap {
|
|
111
|
+
Text: TextNode;
|
|
112
|
+
Point: PointNode;
|
|
113
|
+
Scalar: ScalarNode;
|
|
114
|
+
Triangle: TriangleNode;
|
|
115
|
+
Line: LineNode;
|
|
116
|
+
Circle: CircleNode;
|
|
117
|
+
Ellipse: EllipseNode;
|
|
118
|
+
BezierQuadratic: BezierQuadraticNode;
|
|
119
|
+
BezierCubic: BezierCubicNode;
|
|
120
|
+
Arc: ArcNode;
|
|
121
|
+
PointGrad: PointGradNode;
|
|
122
|
+
Array: ArrayNode;
|
|
123
|
+
Dummy: DummyNode;
|
|
124
|
+
}
|
|
125
|
+
export type AnyNode = NodeTypeMap[keyof NodeTypeMap];
|
|
126
|
+
export type NodeType = keyof NodeTypeMap;
|
|
127
|
+
/**
|
|
128
|
+
* Replaces the custom node types with the `NodeDefinition`s that `modules` export, e.g. a site's `nodes/**\/*.{ts,tsx}`.
|
|
129
|
+
* Throws on a type used twice, leaving the types as they were.
|
|
130
|
+
*/
|
|
131
|
+
export declare function setCustomNodes(modules: CustomModule[]): void;
|
|
132
|
+
export declare function getNodeDefinition(type: string): NodeDefinition | undefined;
|
|
133
|
+
/** Every node type, built-in and the site's own. */
|
|
134
|
+
export declare function getNodeTypes(): NodeType[];
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { renderPoint, renderLine, renderTriangle, renderCircle, renderEllipse, renderArc, renderBezierQuadratic, renderBezierCubic, renderPointGrad, renderText, } from '../render/renderLogic';
|
|
2
|
+
export function checkColor(keyword, name, value) {
|
|
3
|
+
if (value !== undefined && value !== 'none' && !CSS.supports('color', value)) {
|
|
4
|
+
throw new Error(`${keyword}: ${name} '${value}' is not a valid color`);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
const TextDefinition = { type: 'Text', prefix: 'text', render: renderText };
|
|
8
|
+
const PointDefinition = { type: 'Point', prefix: 'p', render: renderPoint };
|
|
9
|
+
const ScalarDefinition = { type: 'Scalar', prefix: 'num' };
|
|
10
|
+
const TriangleDefinition = { type: 'Triangle', prefix: 'tr', render: renderTriangle };
|
|
11
|
+
const LineDefinition = { type: 'Line', prefix: 'line', render: renderLine };
|
|
12
|
+
const CircleDefinition = { type: 'Circle', prefix: 'circ', render: renderCircle };
|
|
13
|
+
const EllipseDefinition = { type: 'Ellipse', prefix: 'ell', render: renderEllipse };
|
|
14
|
+
const BezierQuadraticDefinition = { type: 'BezierQuadratic', prefix: 'bq', render: renderBezierQuadratic };
|
|
15
|
+
const BezierCubicDefinition = { type: 'BezierCubic', prefix: 'bc', render: renderBezierCubic };
|
|
16
|
+
const ArcDefinition = { type: 'Arc', prefix: 'arc', render: renderArc };
|
|
17
|
+
const PointGradDefinition = { type: 'PointGrad', prefix: 'pointgrad', render: renderPointGrad };
|
|
18
|
+
const ArrayDefinition = { type: 'Array', prefix: 'arr' };
|
|
19
|
+
/** An empty prefix gives an empty id. */
|
|
20
|
+
const DummyDefinition = { type: 'Dummy', prefix: '' };
|
|
21
|
+
const isNodeDefinition = (def) => typeof def === 'object' && def !== null && typeof def.type === 'string';
|
|
22
|
+
const BUILTINS = [{
|
|
23
|
+
file: 'src/lib/nodes/state/Node.ts',
|
|
24
|
+
exports: {
|
|
25
|
+
TextDefinition, PointDefinition, ScalarDefinition, TriangleDefinition, LineDefinition, CircleDefinition,
|
|
26
|
+
EllipseDefinition, BezierQuadraticDefinition, BezierCubicDefinition, ArcDefinition, PointGradDefinition,
|
|
27
|
+
ArrayDefinition, DummyDefinition,
|
|
28
|
+
},
|
|
29
|
+
}];
|
|
30
|
+
let nodeDefinitions = new Map();
|
|
31
|
+
/**
|
|
32
|
+
* Replaces the custom node types with the `NodeDefinition`s that `modules` export, e.g. a site's `nodes/**\/*.{ts,tsx}`.
|
|
33
|
+
* Throws on a type used twice, leaving the types as they were.
|
|
34
|
+
*/
|
|
35
|
+
export function setCustomNodes(modules) {
|
|
36
|
+
const definitions = new Map();
|
|
37
|
+
const sourceFile = new Map();
|
|
38
|
+
for (const { file, exports } of [...BUILTINS, ...modules]) {
|
|
39
|
+
for (const def of Object.values(exports).filter(isNodeDefinition)) {
|
|
40
|
+
const other = sourceFile.get(def.type);
|
|
41
|
+
if (other !== undefined) {
|
|
42
|
+
throw new Error(`Duplicate node type '${def.type}': ${file} and ${other}`);
|
|
43
|
+
}
|
|
44
|
+
definitions.set(def.type, def);
|
|
45
|
+
sourceFile.set(def.type, file);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
nodeDefinitions = definitions;
|
|
49
|
+
}
|
|
50
|
+
setCustomNodes([]);
|
|
51
|
+
export function getNodeDefinition(type) {
|
|
52
|
+
return nodeDefinitions.get(type);
|
|
53
|
+
}
|
|
54
|
+
/** Every node type, built-in and the site's own. */
|
|
55
|
+
export function getNodeTypes() {
|
|
56
|
+
return [...nodeDefinitions.keys()];
|
|
57
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Accessor } from 'solid-js';
|
|
2
|
+
import { AnyNode } from './Node';
|
|
3
|
+
import { NameGenerator } from './NameGenerator';
|
|
4
|
+
import type { Collapsed, SplitSide } from '../../../components/SplitPane';
|
|
5
|
+
/** Below this many pixels per unit, the grid hides its lines and keeps only the axes. */
|
|
6
|
+
export declare const MIN_GRID_UNIT = 20;
|
|
7
|
+
export type NodeSignal = {
|
|
8
|
+
signal: Accessor<AnyNode>;
|
|
9
|
+
dispose?: () => void;
|
|
10
|
+
};
|
|
11
|
+
export declare class NodeStore {
|
|
12
|
+
nodes: Record<string, NodeSignal>;
|
|
13
|
+
setNodes: (id: string, value: NodeSignal | undefined) => void;
|
|
14
|
+
reconcileNodes: (value: Record<string, NodeSignal>) => void;
|
|
15
|
+
canvasBounds: Accessor<{
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
}>;
|
|
19
|
+
setCanvasBounds: (bounds: {
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
}) => void;
|
|
23
|
+
/** The side of the page the canvas sits on. */
|
|
24
|
+
canvasSide: Accessor<SplitSide>;
|
|
25
|
+
setCanvasSide: (side: SplitSide) => void;
|
|
26
|
+
/** The page's collapsed pane: `first` is the canvas, `second` the text. */
|
|
27
|
+
collapsedPane: Accessor<Collapsed>;
|
|
28
|
+
setCollapsedPane: (c: Collapsed) => void;
|
|
29
|
+
/** Pixels per grid unit, read from the `unit` node. */
|
|
30
|
+
unit: Accessor<number>;
|
|
31
|
+
/** Opacity multiplier for the grid lines and axes, read from the `gridOpacity` node. */
|
|
32
|
+
gridOpacity: Accessor<number>;
|
|
33
|
+
/** Canvas background color, read from the `canvasColor` node. `undefined` when empty: no background. */
|
|
34
|
+
canvasColor: Accessor<string | undefined>;
|
|
35
|
+
/** Where the grid origin sits in the viewBox, read from the `originX` and `originY` nodes. */
|
|
36
|
+
origin: Accessor<{
|
|
37
|
+
x: number;
|
|
38
|
+
y: number;
|
|
39
|
+
}>;
|
|
40
|
+
nameGenerator: NameGenerator;
|
|
41
|
+
constructor(canvasSide: SplitSide, collapsedPane: Collapsed);
|
|
42
|
+
initSystemNodes(): void;
|
|
43
|
+
clearNodes(): void;
|
|
44
|
+
saveNode(id: string, signal: Accessor<AnyNode>, dispose?: () => void): void;
|
|
45
|
+
/** Sets `props` on node `id`. Unlike `saveNode`, the node keeps following its inputs. */
|
|
46
|
+
updateNode(id: string, props: Partial<AnyNode>): void;
|
|
47
|
+
getNodeValue(id: string): AnyNode | undefined;
|
|
48
|
+
removeNode(id: string): void;
|
|
49
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { batch, createMemo, createRoot, createSignal, untrack } from 'solid-js';
|
|
2
|
+
import { createStore, reconcile } from 'solid-js/store';
|
|
3
|
+
import { NameGenerator } from './NameGenerator';
|
|
4
|
+
import { num, scalar } from '../jax';
|
|
5
|
+
/** Below this many pixels per unit, the grid hides its lines and keeps only the axes. */
|
|
6
|
+
export const MIN_GRID_UNIT = 20;
|
|
7
|
+
export class NodeStore {
|
|
8
|
+
nodes;
|
|
9
|
+
setNodes;
|
|
10
|
+
reconcileNodes;
|
|
11
|
+
canvasBounds;
|
|
12
|
+
setCanvasBounds;
|
|
13
|
+
/** The side of the page the canvas sits on. */
|
|
14
|
+
canvasSide;
|
|
15
|
+
setCanvasSide;
|
|
16
|
+
/** The page's collapsed pane: `first` is the canvas, `second` the text. */
|
|
17
|
+
collapsedPane;
|
|
18
|
+
setCollapsedPane;
|
|
19
|
+
/** Pixels per grid unit, read from the `unit` node. */
|
|
20
|
+
unit;
|
|
21
|
+
/** Opacity multiplier for the grid lines and axes, read from the `gridOpacity` node. */
|
|
22
|
+
gridOpacity;
|
|
23
|
+
/** Canvas background color, read from the `canvasColor` node. `undefined` when empty: no background. */
|
|
24
|
+
canvasColor;
|
|
25
|
+
/** Where the grid origin sits in the viewBox, read from the `originX` and `originY` nodes. */
|
|
26
|
+
origin;
|
|
27
|
+
nameGenerator;
|
|
28
|
+
constructor(canvasSide, collapsedPane) {
|
|
29
|
+
const [nodesState, setNodesState] = createStore({});
|
|
30
|
+
this.nodes = nodesState;
|
|
31
|
+
this.setNodes = (id, value) => { setNodesState(id, value); };
|
|
32
|
+
this.reconcileNodes = (value) => { setNodesState(reconcile(value)); };
|
|
33
|
+
const [canvasBounds, setCanvasBounds] = createSignal({ width: 500, height: 500 });
|
|
34
|
+
this.canvasBounds = canvasBounds;
|
|
35
|
+
this.setCanvasBounds = setCanvasBounds;
|
|
36
|
+
[this.canvasSide, this.setCanvasSide] = createSignal(canvasSide);
|
|
37
|
+
[this.collapsedPane, this.setCollapsedPane] = createSignal(collapsedPane);
|
|
38
|
+
createRoot(() => {
|
|
39
|
+
this.unit = createMemo(() => {
|
|
40
|
+
const node = this.nodes['unit']?.signal();
|
|
41
|
+
// max(1, ...): a zero, negative or NaN unit would break the viewBox.
|
|
42
|
+
return node ? Math.max(1, num(node.value)) : 50;
|
|
43
|
+
});
|
|
44
|
+
this.gridOpacity = createMemo(() => {
|
|
45
|
+
const node = this.nodes['gridOpacity']?.signal();
|
|
46
|
+
return node ? num(node.value) : 1;
|
|
47
|
+
});
|
|
48
|
+
this.canvasColor = createMemo(() => {
|
|
49
|
+
const node = this.nodes['canvasColor']?.signal();
|
|
50
|
+
return node?.value?.trim() || undefined;
|
|
51
|
+
});
|
|
52
|
+
this.origin = createMemo(() => {
|
|
53
|
+
const xNode = this.nodes['originX']?.signal();
|
|
54
|
+
const yNode = this.nodes['originY']?.signal();
|
|
55
|
+
const viewWidth = this.canvasBounds().width / this.unit();
|
|
56
|
+
const viewHeight = this.canvasBounds().height / this.unit();
|
|
57
|
+
// The offset is in grid units from the canvas center. The viewBox is y-down, hence `- y`.
|
|
58
|
+
const x = xNode ? num(xNode.value) : 0;
|
|
59
|
+
const y = yNode ? num(yNode.value) : 0;
|
|
60
|
+
return { x: viewWidth / 2 + x, y: viewHeight / 2 - y };
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
// untrack: names are generated inside reactive computations, which must not rerun when the checked node changes.
|
|
64
|
+
this.nameGenerator = new NameGenerator((name) => untrack(() => this.nodes[name] !== undefined));
|
|
65
|
+
this.initSystemNodes();
|
|
66
|
+
}
|
|
67
|
+
initSystemNodes() {
|
|
68
|
+
const unitId = 'unit';
|
|
69
|
+
createRoot((dispose) => {
|
|
70
|
+
const signal = createMemo(() => ({ id: unitId, type: 'Scalar', value: scalar(50) }));
|
|
71
|
+
this.saveNode(unitId, signal, dispose);
|
|
72
|
+
});
|
|
73
|
+
const gridOpacityId = 'gridOpacity';
|
|
74
|
+
createRoot((dispose) => {
|
|
75
|
+
const signal = createMemo(() => ({ id: gridOpacityId, type: 'Scalar', value: scalar(1) }));
|
|
76
|
+
this.saveNode(gridOpacityId, signal, dispose);
|
|
77
|
+
});
|
|
78
|
+
const canvasColorId = 'canvasColor';
|
|
79
|
+
createRoot((dispose) => {
|
|
80
|
+
const signal = createMemo(() => ({ id: canvasColorId, type: 'Text', value: '' }));
|
|
81
|
+
this.saveNode(canvasColorId, signal, dispose);
|
|
82
|
+
});
|
|
83
|
+
const originXId = 'originX';
|
|
84
|
+
createRoot((dispose) => {
|
|
85
|
+
const signal = createMemo(() => ({ id: originXId, type: 'Scalar', value: scalar(0) }));
|
|
86
|
+
this.saveNode(originXId, signal, dispose);
|
|
87
|
+
});
|
|
88
|
+
const originYId = 'originY';
|
|
89
|
+
createRoot((dispose) => {
|
|
90
|
+
const signal = createMemo(() => ({ id: originYId, type: 'Scalar', value: scalar(0) }));
|
|
91
|
+
this.saveNode(originYId, signal, dispose);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
clearNodes() {
|
|
95
|
+
batch(() => {
|
|
96
|
+
for (const key in this.nodes) {
|
|
97
|
+
if (this.nodes[key] && typeof this.nodes[key].dispose === 'function') {
|
|
98
|
+
this.nodes[key].dispose();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
this.reconcileNodes({});
|
|
102
|
+
this.nameGenerator.resetCounters();
|
|
103
|
+
});
|
|
104
|
+
this.initSystemNodes();
|
|
105
|
+
}
|
|
106
|
+
saveNode(id, signal, dispose) {
|
|
107
|
+
if (this.nodes[id] && typeof this.nodes[id].dispose === 'function') {
|
|
108
|
+
this.nodes[id].dispose();
|
|
109
|
+
}
|
|
110
|
+
this.setNodes(id, { signal, dispose });
|
|
111
|
+
}
|
|
112
|
+
/** Sets `props` on node `id`. Unlike `saveNode`, the node keeps following its inputs. */
|
|
113
|
+
updateNode(id, props) {
|
|
114
|
+
// Read out now: `setNodes` merges into the stored entry, so it would overwrite them.
|
|
115
|
+
const { signal: oldSignal, dispose: oldDispose } = this.nodes[id];
|
|
116
|
+
createRoot((dispose) => {
|
|
117
|
+
const signal = createMemo(() => ({ ...oldSignal(), ...props }));
|
|
118
|
+
// Not `saveNode`: that would dispose the old node, which the new one reads.
|
|
119
|
+
this.setNodes(id, { signal, dispose: () => { dispose(); oldDispose?.(); } });
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
getNodeValue(id) {
|
|
123
|
+
return this.nodes[id]?.signal();
|
|
124
|
+
}
|
|
125
|
+
removeNode(id) {
|
|
126
|
+
const nodeSignal = this.nodes[id];
|
|
127
|
+
if (nodeSignal) {
|
|
128
|
+
this.setNodes(id, undefined);
|
|
129
|
+
if (typeof nodeSignal.dispose === 'function') {
|
|
130
|
+
queueMicrotask(() => {
|
|
131
|
+
if (nodeSignal.dispose) {
|
|
132
|
+
nodeSignal.dispose();
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { BlueprintNode, Node2NodeFunc, ParameterDefinition } from '../functions/Node2NodeFunc';
|
|
2
|
+
import { AnyNode } from '../state/Node';
|
|
3
|
+
/** Whether an input of type `type` is looped over: an array going to a parameter that doesn't take whole arrays. */
|
|
4
|
+
export declare function isBroadcast(type: string, param: ParameterDefinition): boolean;
|
|
5
|
+
/** numpy broadcasting: shapes are lined up from the right, and each dimension must match or be 1. */
|
|
6
|
+
export declare function broadcastShapes(shapes: number[][]): number[];
|
|
7
|
+
/**
|
|
8
|
+
* The inputs of each item when an input is broadcast (see `isBroadcast`), or just `inputs`
|
|
9
|
+
* when none is. Like `computeBlueprint`, an item with a Dummy input is skipped.
|
|
10
|
+
*/
|
|
11
|
+
export declare function broadcastItems(parameters: ParameterDefinition[], inputs: AnyNode[]): AnyNode[][];
|
|
12
|
+
/**
|
|
13
|
+
* `func.compute`, run once per item when an input is broadcast (see `isBroadcast`).
|
|
14
|
+
* Item k's nodes are saved under keys `k.main`, `k.l_0`, ..., and `main` is the array of
|
|
15
|
+
* every item's main node. An item is Dummy, and not saved, when one of its inputs is
|
|
16
|
+
* Dummy or its own main node is.
|
|
17
|
+
*/
|
|
18
|
+
export declare function computeBlueprint(func: Node2NodeFunc, inputs: AnyNode[]): Record<string, BlueprintNode>;
|