@tinyvolt/pie 0.1.0 → 0.3.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 +12 -0
- package/init/site-agents.md +10 -3
- package/package.json +6 -1
- package/pages/about.md +5 -0
- package/pages/python-editor.md +46 -15
- package/public/logo.svg +1 -1
- package/src/app.css +64 -4
- package/src/components/PageMeta.tsx +40 -0
- package/src/components/icons.tsx +72 -0
- package/src/lib/markdown/components/MarkdownBody.tsx +2 -1
- package/src/lib/markdown/components/PythonEditor.tsx +95 -7
- package/src/lib/markdown/frontmatter.ts +2 -0
- package/src/lib/markdown/render.ts +37 -1
- package/src/lib/nodes/animation.ts +47 -0
- package/src/lib/nodes/components/CenteredGrid.tsx +15 -2
- package/src/lib/nodes/components/NodeCanvas.tsx +7 -1
- package/src/lib/nodes/execute.ts +5 -2
- package/src/lib/nodes/functions/Node2NodeFunc.ts +3 -2
- package/src/lib/nodes/functions/sideEffects.ts +11 -22
- package/src/lib/nodes/state/NodeStore.ts +27 -1
- package/src/lib/python/lift.ts +87 -0
- package/src/lib/python/py/pie.py +24 -1
- package/src/lib/python/runner.ts +22 -3
- package/src/lib/python/worker.ts +8 -1
- package/src/lib/site/vitePlugin.ts +17 -2
- package/src/routes/[...slug].tsx +2 -1
- package/src/routes/index.tsx +11 -1
- package/src/routes/pages/index.tsx +2 -2
- package/vite.ts +37 -2
- package/src/routes/about.tsx +0 -10
package/README.md
CHANGED
|
@@ -55,3 +55,15 @@ pnpm build # build to .output/
|
|
|
55
55
|
pnpm start # run the built server
|
|
56
56
|
pnpm preview # preview the build with vite
|
|
57
57
|
```
|
|
58
|
+
|
|
59
|
+
## Publishing
|
|
60
|
+
|
|
61
|
+
Bump `version` in `package.json`, then at the repo root:
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
npm whoami
|
|
65
|
+
npm login # if npm whoami fails
|
|
66
|
+
npm publish --dry-run # list the files without publishing
|
|
67
|
+
npm publish
|
|
68
|
+
git tag pie@<version> && git push origin pie@<version>
|
|
69
|
+
```
|
package/init/site-agents.md
CHANGED
|
@@ -32,6 +32,8 @@ Do the work in the folders above. Don't edit `node_modules/@tinyvolt/pie/`: inst
|
|
|
32
32
|
Flat `key: value` lines. Values may be quoted. A key may appear only once.
|
|
33
33
|
|
|
34
34
|
- `title` (required): shown in the header and in every browser tab title.
|
|
35
|
+
- `url`: where the site is deployed, e.g. `https://example.com`. Needed for `sitemap.xml`, `robots.txt`, canonical links and `og:url`; without it the build skips them and warns.
|
|
36
|
+
- `description`: shown in search results and link previews for a page with no `description` of its own.
|
|
35
37
|
|
|
36
38
|
## Pages
|
|
37
39
|
|
|
@@ -47,13 +49,14 @@ canvas: left
|
|
|
47
49
|
```
|
|
48
50
|
|
|
49
51
|
- `title`: the tab title. Empty means the tab shows the site title alone.
|
|
52
|
+
- `description`: shown in search results and link previews. Empty means `site.yml`'s `description`.
|
|
50
53
|
- `canvas`: `left`, `right`, `top`, `bottom` or `none` (default). Adds a canvas on that side of the page.
|
|
51
54
|
|
|
52
55
|
Other keys are ignored. A bad value fails the build, and shows an error screen in dev.
|
|
53
56
|
|
|
54
57
|
Math uses KaTeX: `$…$` inline, and `$$…$$` on its own lines for a display block.
|
|
55
58
|
|
|
56
|
-
A page's URL must not match a route in `node_modules/@tinyvolt/pie/src/routes/` (`/`, `/
|
|
59
|
+
A page's URL must not match a route in `node_modules/@tinyvolt/pie/src/routes/` (`/`, `/pages`). For example, `pages/pages.md` fails the build.
|
|
57
60
|
|
|
58
61
|
## Python editors
|
|
59
62
|
|
|
@@ -71,6 +74,7 @@ pie.circle(a, r)
|
|
|
71
74
|
- All editors on one page share the same Python variables and the same canvas.
|
|
72
75
|
- `import numpy`, `scipy`, `matplotlib` and other [Pyodide packages](https://pyodide.org/en/0.28.2/usage/packages-in-pyodide.html) load on first use.
|
|
73
76
|
- `plt.show()` shows matplotlib figures under the editor.
|
|
77
|
+
- `` ```python-editor collapsed `` starts the editor collapsed: only its toolbar shows until the reader expands it. Other words after `python-editor` are ignored.
|
|
74
78
|
|
|
75
79
|
## Drawing on the canvas: `pie.*`
|
|
76
80
|
|
|
@@ -90,7 +94,10 @@ Common mistake: `center = pie.point(1, 2)` does not give the node the id `center
|
|
|
90
94
|
- Every node the call makes gets the colors, including extra nodes such as the center point of `ellipse_from_foci`.
|
|
91
95
|
- A node shows only the colors its render function draws with. A point uses only `fill`, a line only `stroke`. For `partial`'s arrow, `stroke` colors the line and `fill` the head.
|
|
92
96
|
|
|
93
|
-
|
|
97
|
+
There are three system nodes:
|
|
98
|
+
- `unit`: a scalar holding the pixels per grid unit (default 50). Changing it changes the grid unit, e.g. `pie.scalar(80, out='unit')`.
|
|
99
|
+
- `gridOpacity`: a scalar that multiplies the opacity of the grid lines and axes (default 1; 0 hides them), e.g. `pie.scalar(0.5, out='gridOpacity')`.
|
|
100
|
+
- `canvasColor`: a text holding a CSS color for the canvas background (default `''`: no background), e.g. `pie.text('#111', out='canvasColor')`.
|
|
94
101
|
|
|
95
102
|
Built-in functions:
|
|
96
103
|
|
|
@@ -118,7 +125,7 @@ These change existing nodes instead of making a new one. They run once, when cal
|
|
|
118
125
|
|
|
119
126
|
| Call | Does |
|
|
120
127
|
| --- | --- |
|
|
121
|
-
| `pie.clear()` | removes every node and resets `unit` (pixels per grid unit) to 50 |
|
|
128
|
+
| `pie.clear()` | removes every node and resets the system nodes: `unit` (pixels per grid unit) to 50, `gridOpacity` to 1, `canvasColor` to `''` |
|
|
122
129
|
| `pie.translate_point_to(p, x, y)` | moves point `p` to (x, y) |
|
|
123
130
|
| `pie.translate_point_by(p, dx, dy)` | moves point `p` by (dx, dy) |
|
|
124
131
|
| `pie.translate_scalar_to(s, value)` | sets scalar `s` to `value` |
|
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinyvolt/pie",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A Markdown-based SSG for interactive science explainers. Create a site with: npx @tinyvolt/pie init my-site",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/TinyVolt/pie-ssg.git"
|
|
9
|
+
},
|
|
6
10
|
"exports": {
|
|
7
11
|
"./vite": "./vite.ts"
|
|
8
12
|
},
|
|
@@ -36,6 +40,7 @@
|
|
|
36
40
|
"@solidjs/router": "1.0.0",
|
|
37
41
|
"@solidjs/start": "2.0.6",
|
|
38
42
|
"codemirror": "6.0.2",
|
|
43
|
+
"@lezer/highlight": "1.2.5",
|
|
39
44
|
"katex": "0.19.0",
|
|
40
45
|
"marked": "18.1.1",
|
|
41
46
|
"nitro": "3.0.260610-beta"
|
package/pages/python-editor.md
CHANGED
|
@@ -1,29 +1,60 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
canvas: left
|
|
2
|
+
canvas: right
|
|
4
3
|
---
|
|
5
|
-
# Python editor
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
# Examples of animating lifted functions
|
|
6
|
+
|
|
7
|
+
1. One lifted function. The point should trace a sine curve, not jump to the end.
|
|
8
|
+
|
|
9
|
+
```python-editor
|
|
10
|
+
import math
|
|
11
|
+
def wave(t):
|
|
12
|
+
return (t, 2 * math.sin(t))
|
|
13
|
+
wave_point = pie.lift(wave, pie.Point)
|
|
14
|
+
t = pie.scalar(0, out='t')
|
|
15
|
+
wave_point(t)
|
|
16
|
+
pie.translate_scalar_to(t, 6, animate=True)
|
|
11
17
|
```
|
|
12
18
|
|
|
13
|
-
|
|
19
|
+
2. Change `t` after the animation. The point should jump to (1, 1.68) through a normal rerun.
|
|
20
|
+
|
|
21
|
+
```python-editor
|
|
22
|
+
pie.scalar(1, out='t')
|
|
23
|
+
```
|
|
14
24
|
|
|
15
|
-
|
|
25
|
+
3. Two lifted functions, one feeding the other. The point should slide from (0, 1) to (6, 1).
|
|
16
26
|
|
|
17
27
|
```python-editor
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
28
|
+
def double(x):
|
|
29
|
+
return 2 * x
|
|
30
|
+
def on_line(x):
|
|
31
|
+
return (x, 1)
|
|
32
|
+
double_l = pie.lift(double, pie.Scalar)
|
|
33
|
+
on_line_l = pie.lift(on_line, pie.Point)
|
|
34
|
+
s = pie.scalar(0, out='s')
|
|
35
|
+
on_line_l(double_l(s))
|
|
36
|
+
pie.translate_scalar_to(s, 3, animate=True)
|
|
21
37
|
```
|
|
22
38
|
|
|
39
|
+
4. A lifted function that returns an array. Five points should spread out smoothly from [0, 1] to [0, 5].
|
|
40
|
+
|
|
23
41
|
```python-editor
|
|
24
|
-
|
|
42
|
+
import numpy as np
|
|
43
|
+
linspace = pie.lift(np.linspace, pie.Array[pie.Scalar])
|
|
44
|
+
n = pie.scalar(1, out='n')
|
|
45
|
+
pie.point(linspace(0, n, 5), -2)
|
|
46
|
+
pie.translate_scalar_to(n, 5, animate=True)
|
|
25
47
|
```
|
|
26
48
|
|
|
49
|
+
5. A lifted function that fails partway. The point should move until x = 2 and then stop. The console should show one error: `fails_above_2: ValueError: too big`.
|
|
50
|
+
|
|
27
51
|
```python-editor
|
|
28
|
-
|
|
29
|
-
|
|
52
|
+
def fails_above_2(x):
|
|
53
|
+
if x > 2:
|
|
54
|
+
raise ValueError("too big")
|
|
55
|
+
return (x, -1)
|
|
56
|
+
fails_l = pie.lift(fails_above_2, pie.Point)
|
|
57
|
+
u = pie.scalar(0, out='u')
|
|
58
|
+
fails_l(u)
|
|
59
|
+
pie.translate_scalar_to(u, 4, animate=True)
|
|
60
|
+
```
|
package/public/logo.svg
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 21 64 45" width="256" height="180">
|
|
2
2
|
<path d="M32 29 L8 29 A24 24 0 0 0 20 49.78 Z" fill="#41DBC9"/>
|
|
3
3
|
<path d="M32 29 L44 49.78 A24 24 0 0 0 56 29 Z" fill="#41DBC9"/>
|
|
4
4
|
<path d="M32 34 L20 54.78 A24 24 0 0 0 44 54.78 Z" fill="#0B6B73"/>
|
package/src/app.css
CHANGED
|
@@ -128,6 +128,22 @@ main pre code {
|
|
|
128
128
|
background: none;
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
+
/* ```python tokens. Same colors as the editor's default theme. */
|
|
132
|
+
.tok-keyword { color: #708; }
|
|
133
|
+
.tok-atom, .tok-bool { color: #219; }
|
|
134
|
+
.tok-literal, .tok-number { color: #164; }
|
|
135
|
+
.tok-string { color: #a11; }
|
|
136
|
+
.tok-string2 { color: #e40; }
|
|
137
|
+
.tok-variableName.tok-definition { color: #00f; }
|
|
138
|
+
.tok-variableName.tok-local { color: #30a; }
|
|
139
|
+
.tok-typeName, .tok-namespace { color: #085; }
|
|
140
|
+
.tok-className { color: #167; }
|
|
141
|
+
.tok-variableName2, .tok-macroName { color: #256; }
|
|
142
|
+
.tok-propertyName.tok-definition { color: #00c; }
|
|
143
|
+
.tok-comment { color: #940; }
|
|
144
|
+
.tok-meta { color: #404740; }
|
|
145
|
+
.tok-invalid { color: #f00; }
|
|
146
|
+
|
|
131
147
|
main table {
|
|
132
148
|
border-collapse: collapse;
|
|
133
149
|
}
|
|
@@ -256,6 +272,8 @@ main .katex-display {
|
|
|
256
272
|
/* Hidden until MarkdownBody swaps it for the editor; keeps its space so the text below doesn't jump. */
|
|
257
273
|
pre:has(> code.language-python-editor) {
|
|
258
274
|
visibility: hidden;
|
|
275
|
+
max-height: 20rem;
|
|
276
|
+
overflow: hidden;
|
|
259
277
|
}
|
|
260
278
|
|
|
261
279
|
.python-editor {
|
|
@@ -265,8 +283,24 @@ pre:has(> code.language-python-editor) {
|
|
|
265
283
|
font-family: var(--font-ui, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif);
|
|
266
284
|
}
|
|
267
285
|
|
|
286
|
+
/* Default cap on the code's height. Dragging the resize bar replaces it with a fixed height. */
|
|
268
287
|
.python-editor .cm-editor {
|
|
269
288
|
font-size: 0.875rem;
|
|
289
|
+
max-height: 20rem;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
.python-editor .cm-scroller {
|
|
293
|
+
overflow-y: auto;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
.python-editor-resize {
|
|
297
|
+
height: 6px;
|
|
298
|
+
cursor: row-resize;
|
|
299
|
+
touch-action: none;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
.python-editor-resize:hover {
|
|
303
|
+
background: var(--color-border, #ddd);
|
|
270
304
|
}
|
|
271
305
|
|
|
272
306
|
.python-editor-toolbar {
|
|
@@ -278,6 +312,9 @@ pre:has(> code.language-python-editor) {
|
|
|
278
312
|
}
|
|
279
313
|
|
|
280
314
|
.python-editor-button {
|
|
315
|
+
display: inline-flex;
|
|
316
|
+
align-items: center;
|
|
317
|
+
justify-content: center;
|
|
281
318
|
width: 1.75rem;
|
|
282
319
|
height: 1.75rem;
|
|
283
320
|
padding: 0;
|
|
@@ -296,9 +333,23 @@ pre:has(> code.language-python-editor) {
|
|
|
296
333
|
font-size: 0.875rem;
|
|
297
334
|
}
|
|
298
335
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
336
|
+
.python-editor-result {
|
|
337
|
+
display: inline-flex;
|
|
338
|
+
align-items: center;
|
|
339
|
+
gap: 0.25rem;
|
|
340
|
+
font-size: 0.875rem;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
.python-editor-result-success {
|
|
344
|
+
color: var(--color-success, #1a7f37);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
.python-editor-result-error {
|
|
348
|
+
color: var(--color-error, #c00);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
.python-editor-result-stopped {
|
|
352
|
+
color: var(--color-warning, #9a6700);
|
|
302
353
|
}
|
|
303
354
|
|
|
304
355
|
.python-editor-collapse {
|
|
@@ -307,6 +358,7 @@ pre:has(> code.language-python-editor) {
|
|
|
307
358
|
|
|
308
359
|
/* Collapsed: only the toolbar shows. Hidden with CSS so the editor keeps its code. */
|
|
309
360
|
.python-editor-collapsed .python-editor-code,
|
|
361
|
+
.python-editor-collapsed .python-editor-resize,
|
|
310
362
|
.python-editor-collapsed .python-editor-output {
|
|
311
363
|
display: none;
|
|
312
364
|
}
|
|
@@ -315,12 +367,20 @@ pre:has(> code.language-python-editor) {
|
|
|
315
367
|
border-top: none;
|
|
316
368
|
}
|
|
317
369
|
|
|
370
|
+
/* Right padding leaves room for the copy button so it doesn't cover the text. */
|
|
318
371
|
.python-editor-output {
|
|
319
|
-
|
|
372
|
+
position: relative;
|
|
373
|
+
padding: 0.5rem 2.75rem 0.5rem 0.5rem;
|
|
320
374
|
border-top: 1px solid var(--color-border, #ddd);
|
|
321
375
|
font-size: 0.875rem;
|
|
322
376
|
}
|
|
323
377
|
|
|
378
|
+
.python-editor-copy {
|
|
379
|
+
position: absolute;
|
|
380
|
+
top: 0.5rem;
|
|
381
|
+
right: 0.5rem;
|
|
382
|
+
}
|
|
383
|
+
|
|
324
384
|
.python-editor-text {
|
|
325
385
|
margin: 0;
|
|
326
386
|
max-height: 20rem;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Link, Meta } from "@solidjs/meta";
|
|
2
|
+
import { useLocation } from "@solidjs/router";
|
|
3
|
+
import { Show } from "solid-js";
|
|
4
|
+
import site from "/site.yml";
|
|
5
|
+
import { PageTitle } from "./PageTitle";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The tab title plus the tags for search results and link previews. Each route renders it once:
|
|
9
|
+
* `@solidjs/meta` replaces a `<meta>` only when its content matches too, so a page's tag can't override a default.
|
|
10
|
+
* Empty `title` or `description` falls back to `site.yml`. The canonical link and `og:url` need `site.yml`'s `url`.
|
|
11
|
+
*/
|
|
12
|
+
export function PageMeta(props: { title?: string; description?: string }) {
|
|
13
|
+
const location = useLocation();
|
|
14
|
+
const ogTitle = () => props.title || site.title;
|
|
15
|
+
const description = () => props.description || site.description;
|
|
16
|
+
return (
|
|
17
|
+
<>
|
|
18
|
+
<PageTitle>{props.title}</PageTitle>
|
|
19
|
+
<Meta property="og:type" content="website" />
|
|
20
|
+
<Meta property="og:site_name" content={site.title} />
|
|
21
|
+
<Meta property="og:title" content={ogTitle()} />
|
|
22
|
+
<Show when={description()}>
|
|
23
|
+
{description => (
|
|
24
|
+
<>
|
|
25
|
+
<Meta name="description" content={description()} />
|
|
26
|
+
<Meta property="og:description" content={description()} />
|
|
27
|
+
</>
|
|
28
|
+
)}
|
|
29
|
+
</Show>
|
|
30
|
+
<Show when={site.url}>
|
|
31
|
+
{url => (
|
|
32
|
+
<>
|
|
33
|
+
<Link rel="canonical" href={url() + location.pathname} />
|
|
34
|
+
<Meta property="og:url" content={url() + location.pathname} />
|
|
35
|
+
</>
|
|
36
|
+
)}
|
|
37
|
+
</Show>
|
|
38
|
+
</>
|
|
39
|
+
);
|
|
40
|
+
}
|
package/src/components/icons.tsx
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { JSX } from 'solid-js';
|
|
2
|
+
|
|
1
3
|
export type Direction = 'left' | 'right' | 'up' | 'down';
|
|
2
4
|
|
|
3
5
|
const CHEVRON_TURN: Record<Direction, number> = { left: 0, up: 90, right: 180, down: 270 };
|
|
@@ -20,3 +22,73 @@ export function Chevron(props: { direction: Direction }) {
|
|
|
20
22
|
</svg>
|
|
21
23
|
);
|
|
22
24
|
}
|
|
25
|
+
|
|
26
|
+
/** The shared frame for the icons below: 24×24 drawing, lines in the text color. */
|
|
27
|
+
function Icon(props: { children: JSX.Element }) {
|
|
28
|
+
return (
|
|
29
|
+
<svg
|
|
30
|
+
class="icon"
|
|
31
|
+
width="14"
|
|
32
|
+
height="14"
|
|
33
|
+
viewBox="0 0 24 24"
|
|
34
|
+
fill="none"
|
|
35
|
+
stroke="currentColor"
|
|
36
|
+
stroke-width="2"
|
|
37
|
+
stroke-linecap="round"
|
|
38
|
+
stroke-linejoin="round"
|
|
39
|
+
aria-hidden="true"
|
|
40
|
+
>
|
|
41
|
+
{props.children}
|
|
42
|
+
</svg>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function PlayIcon() {
|
|
47
|
+
return (
|
|
48
|
+
<Icon>
|
|
49
|
+
<path d="M6 4l14 8-14 8z" fill="currentColor" />
|
|
50
|
+
</Icon>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function StopIcon() {
|
|
55
|
+
return (
|
|
56
|
+
<Icon>
|
|
57
|
+
<rect x="5" y="5" width="14" height="14" rx="1" fill="currentColor" />
|
|
58
|
+
</Icon>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function ResetIcon() {
|
|
63
|
+
return (
|
|
64
|
+
<Icon>
|
|
65
|
+
<path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8" />
|
|
66
|
+
<path d="M3 3v5h5" />
|
|
67
|
+
</Icon>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function CopyIcon() {
|
|
72
|
+
return (
|
|
73
|
+
<Icon>
|
|
74
|
+
<rect x="8" y="8" width="14" height="14" rx="2" />
|
|
75
|
+
<path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" />
|
|
76
|
+
</Icon>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function CheckIcon() {
|
|
81
|
+
return (
|
|
82
|
+
<Icon>
|
|
83
|
+
<path d="M20 6L9 17l-5-5" />
|
|
84
|
+
</Icon>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function CrossIcon() {
|
|
89
|
+
return (
|
|
90
|
+
<Icon>
|
|
91
|
+
<path d="M18 6L6 18M6 6l12 12" />
|
|
92
|
+
</Icon>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
@@ -30,9 +30,10 @@ export function MarkdownBody(props: { html: string; store?: NodeStore; class?: s
|
|
|
30
30
|
const pre = codeEl.parentElement!;
|
|
31
31
|
if (!pre.isConnected) return;
|
|
32
32
|
const code = codeEl.textContent ?? '';
|
|
33
|
+
const collapsed = codeEl.hasAttribute('data-collapsed');
|
|
33
34
|
const host = document.createElement('div');
|
|
34
35
|
pre.replaceWith(host);
|
|
35
|
-
disposers.push(render(() => <PythonEditor code={code} scope={scope} />, host));
|
|
36
|
+
disposers.push(render(() => <PythonEditor code={code} collapsed={collapsed} scope={scope} />, host));
|
|
36
37
|
});
|
|
37
38
|
});
|
|
38
39
|
});
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { Accessor, createSignal, For, onCleanup, onMount, Setter, Show } from 'solid-js';
|
|
1
|
+
import { Accessor, Component, createSignal, For, onCleanup, onMount, Setter, Show } from 'solid-js';
|
|
2
|
+
import { Dynamic } from 'solid-js/web';
|
|
2
3
|
import { basicSetup, EditorView } from 'codemirror';
|
|
3
4
|
import { keymap } from '@codemirror/view';
|
|
4
5
|
import { python } from '@codemirror/lang-python';
|
|
6
|
+
import { CheckIcon, Chevron, CopyIcon, CrossIcon, PlayIcon, ResetIcon, StopIcon } from '~/components/icons';
|
|
5
7
|
import type { PythonScope, RunStatus } from '~/lib/python/runner';
|
|
6
8
|
|
|
7
9
|
const STATUS_TEXT: Record<RunStatus | 'idle', string> = {
|
|
@@ -19,6 +21,15 @@ const END_NOTE: Partial<Record<RunStatus, string>> = {
|
|
|
19
21
|
killed: 'Stopped. Python restarted, so variables from earlier blocks are gone.',
|
|
20
22
|
};
|
|
21
23
|
|
|
24
|
+
/** How the last run ended, shown in the toolbar. */
|
|
25
|
+
type RunResult = 'success' | 'error' | 'stopped';
|
|
26
|
+
|
|
27
|
+
const RESULT: Record<RunResult, { icon: Component; text: string }> = {
|
|
28
|
+
success: { icon: CheckIcon, text: 'Ran successfully' },
|
|
29
|
+
error: { icon: CrossIcon, text: 'Error' },
|
|
30
|
+
stopped: { icon: StopIcon, text: 'Stopped' },
|
|
31
|
+
};
|
|
32
|
+
|
|
22
33
|
const isActive = (status: RunStatus | 'idle') =>
|
|
23
34
|
status === 'waiting' || status === 'loading' || status === 'running';
|
|
24
35
|
|
|
@@ -27,8 +38,11 @@ type OutputEntry =
|
|
|
27
38
|
| { type: 'text'; text: Accessor<string>; setText: Setter<string> }
|
|
28
39
|
| { type: 'plot'; png: string };
|
|
29
40
|
|
|
30
|
-
/**
|
|
31
|
-
|
|
41
|
+
/**
|
|
42
|
+
* An editable block of Python. Run draws its `pie.*` calls on the scope's canvas.
|
|
43
|
+
* `collapsed`: start with only the toolbar showing.
|
|
44
|
+
*/
|
|
45
|
+
export function PythonEditor(props: { code: string; collapsed?: boolean; scope: PythonScope }) {
|
|
32
46
|
let host!: HTMLDivElement;
|
|
33
47
|
let view: EditorView | undefined;
|
|
34
48
|
let stop: (() => void) | undefined;
|
|
@@ -37,8 +51,13 @@ export function PythonEditor(props: { code: string; scope: PythonScope }) {
|
|
|
37
51
|
const [output, setOutput] = createSignal<OutputEntry[]>([]);
|
|
38
52
|
const [error, setError] = createSignal('');
|
|
39
53
|
const [note, setNote] = createSignal('');
|
|
40
|
-
const [collapsed, setCollapsed] = createSignal(false);
|
|
54
|
+
const [collapsed, setCollapsed] = createSignal(props.collapsed ?? false);
|
|
55
|
+
const [copied, setCopied] = createSignal(false);
|
|
41
56
|
const active = () => isActive(status());
|
|
57
|
+
const result = (): RunResult | undefined => {
|
|
58
|
+
if (status() === 'finished') return error() ? 'error' : 'success';
|
|
59
|
+
if (status() === 'stopped' || status() === 'killed') return 'stopped';
|
|
60
|
+
};
|
|
42
61
|
const initialCode = props.code.replace(/\n$/, '');
|
|
43
62
|
|
|
44
63
|
const addText = (text: string) => {
|
|
@@ -70,6 +89,28 @@ export function PythonEditor(props: { code: string; scope: PythonScope }) {
|
|
|
70
89
|
);
|
|
71
90
|
};
|
|
72
91
|
|
|
92
|
+
/** Printed text and the error, without plots or the stop note. */
|
|
93
|
+
const copyText = () =>
|
|
94
|
+
[
|
|
95
|
+
...output().flatMap((entry) => (entry.type === 'text' ? [entry.text()] : [])),
|
|
96
|
+
error(),
|
|
97
|
+
]
|
|
98
|
+
.map((part) => part.replace(/\n+$/, ''))
|
|
99
|
+
.filter(Boolean)
|
|
100
|
+
.join('\n');
|
|
101
|
+
|
|
102
|
+
let copiedTimer: ReturnType<typeof setTimeout> | undefined;
|
|
103
|
+
const copy = async () => {
|
|
104
|
+
try {
|
|
105
|
+
await navigator.clipboard.writeText(copyText());
|
|
106
|
+
} catch {
|
|
107
|
+
return; // Clipboard is only available on https or localhost.
|
|
108
|
+
}
|
|
109
|
+
setCopied(true);
|
|
110
|
+
clearTimeout(copiedTimer);
|
|
111
|
+
copiedTimer = setTimeout(() => setCopied(false), 1500);
|
|
112
|
+
};
|
|
113
|
+
|
|
73
114
|
/** Puts the original code back. Ctrl/Cmd+Z undoes it. */
|
|
74
115
|
const reset = () => {
|
|
75
116
|
if (!view) return;
|
|
@@ -82,6 +123,32 @@ export function PythonEditor(props: { code: string; scope: PythonScope }) {
|
|
|
82
123
|
if (!collapsed()) view?.requestMeasure();
|
|
83
124
|
};
|
|
84
125
|
|
|
126
|
+
/** Drag the bar under the code to set the editor's height. */
|
|
127
|
+
const startResize = (e: PointerEvent) => {
|
|
128
|
+
if (!view || e.button !== 0) return;
|
|
129
|
+
e.preventDefault(); // no text selection while dragging
|
|
130
|
+
const handle = e.currentTarget as HTMLElement;
|
|
131
|
+
const editor = view.dom;
|
|
132
|
+
const startY = e.clientY;
|
|
133
|
+
const startHeight = editor.getBoundingClientRect().height;
|
|
134
|
+
// About one line, plus the 4px top and bottom padding CodeMirror puts on its content.
|
|
135
|
+
const minHeight = view.defaultLineHeight + 8;
|
|
136
|
+
|
|
137
|
+
const move = (ev: PointerEvent) => {
|
|
138
|
+
editor.style.maxHeight = 'none';
|
|
139
|
+
editor.style.height = `${Math.max(minHeight, startHeight + ev.clientY - startY)}px`;
|
|
140
|
+
};
|
|
141
|
+
const end = () => {
|
|
142
|
+
handle.removeEventListener('pointermove', move);
|
|
143
|
+
handle.removeEventListener('pointerup', end);
|
|
144
|
+
handle.removeEventListener('pointercancel', end);
|
|
145
|
+
};
|
|
146
|
+
handle.setPointerCapture(e.pointerId);
|
|
147
|
+
handle.addEventListener('pointermove', move);
|
|
148
|
+
handle.addEventListener('pointerup', end);
|
|
149
|
+
handle.addEventListener('pointercancel', end);
|
|
150
|
+
};
|
|
151
|
+
|
|
85
152
|
const runFromKeyboard = () => {
|
|
86
153
|
run();
|
|
87
154
|
return true;
|
|
@@ -105,12 +172,14 @@ export function PythonEditor(props: { code: string; scope: PythonScope }) {
|
|
|
105
172
|
|
|
106
173
|
onCleanup(() => {
|
|
107
174
|
stop?.();
|
|
175
|
+
clearTimeout(copiedTimer);
|
|
108
176
|
view?.destroy();
|
|
109
177
|
});
|
|
110
178
|
|
|
111
179
|
return (
|
|
112
180
|
<div class="python-editor" classList={{ 'python-editor-collapsed': collapsed() }}>
|
|
113
181
|
<div class="python-editor-code" ref={host} />
|
|
182
|
+
<div class="python-editor-resize" title="Drag to resize" onPointerDown={startResize} />
|
|
114
183
|
<div class="python-editor-toolbar">
|
|
115
184
|
<button
|
|
116
185
|
type="button"
|
|
@@ -119,7 +188,7 @@ export function PythonEditor(props: { code: string; scope: PythonScope }) {
|
|
|
119
188
|
aria-label={active() ? 'Stop' : 'Run'}
|
|
120
189
|
onClick={() => (active() ? stop?.() : run())}
|
|
121
190
|
>
|
|
122
|
-
{active() ?
|
|
191
|
+
{active() ? <StopIcon /> : <PlayIcon />}
|
|
123
192
|
</button>
|
|
124
193
|
<button
|
|
125
194
|
type="button"
|
|
@@ -128,9 +197,17 @@ export function PythonEditor(props: { code: string; scope: PythonScope }) {
|
|
|
128
197
|
aria-label="Reset code"
|
|
129
198
|
onClick={reset}
|
|
130
199
|
>
|
|
131
|
-
|
|
200
|
+
<ResetIcon />
|
|
132
201
|
</button>
|
|
133
202
|
<span class="python-editor-status">{STATUS_TEXT[status()]}</span>
|
|
203
|
+
<Show when={result()}>
|
|
204
|
+
{(r) => (
|
|
205
|
+
<span class={`python-editor-result python-editor-result-${r()}`}>
|
|
206
|
+
<Dynamic component={RESULT[r()].icon} />
|
|
207
|
+
{RESULT[r()].text}
|
|
208
|
+
</span>
|
|
209
|
+
)}
|
|
210
|
+
</Show>
|
|
134
211
|
<button
|
|
135
212
|
type="button"
|
|
136
213
|
class="python-editor-button python-editor-collapse"
|
|
@@ -139,11 +216,22 @@ export function PythonEditor(props: { code: string; scope: PythonScope }) {
|
|
|
139
216
|
aria-expanded={!collapsed()}
|
|
140
217
|
onClick={toggleCollapsed}
|
|
141
218
|
>
|
|
142
|
-
{collapsed() ? '
|
|
219
|
+
<Chevron direction={collapsed() ? 'down' : 'up'} />
|
|
143
220
|
</button>
|
|
144
221
|
</div>
|
|
145
222
|
<Show when={output().length > 0 || error() || note()}>
|
|
146
223
|
<div class="python-editor-output">
|
|
224
|
+
<Show when={copyText()}>
|
|
225
|
+
<button
|
|
226
|
+
type="button"
|
|
227
|
+
class="python-editor-button python-editor-copy"
|
|
228
|
+
title={copied() ? 'Copied' : 'Copy output'}
|
|
229
|
+
aria-label={copied() ? 'Copied' : 'Copy output'}
|
|
230
|
+
onClick={copy}
|
|
231
|
+
>
|
|
232
|
+
{copied() ? <CheckIcon /> : <CopyIcon />}
|
|
233
|
+
</button>
|
|
234
|
+
</Show>
|
|
147
235
|
<For each={output()}>
|
|
148
236
|
{(entry) =>
|
|
149
237
|
entry.type === 'text' ? (
|
|
@@ -28,6 +28,8 @@ const FIELDS = {
|
|
|
28
28
|
canvas: oneOf(['left', 'right', 'top', 'bottom', 'none'], 'none'),
|
|
29
29
|
// Empty: the tab shows the site title alone.
|
|
30
30
|
title: text(''),
|
|
31
|
+
// Empty: `site.yml`'s `description`.
|
|
32
|
+
description: text(''),
|
|
31
33
|
};
|
|
32
34
|
|
|
33
35
|
export type Frontmatter = Values<typeof FIELDS>;
|