@tinyvolt/pie 0.8.2 → 0.10.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.
Files changed (139) hide show
  1. package/README.md +18 -1
  2. package/dist/components/SplitPane.d.ts +21 -0
  3. package/dist/components/SplitPane.jsx +113 -0
  4. package/dist/components/icons.d.ts +12 -0
  5. package/dist/components/icons.jsx +45 -0
  6. package/dist/lib/autodiff/index.d.ts +28 -0
  7. package/dist/lib/autodiff/index.js +39 -0
  8. package/dist/lib/customModules.d.ts +13 -0
  9. package/dist/lib/customModules.js +17 -0
  10. package/dist/lib/markdown/compile.d.ts +27 -0
  11. package/dist/lib/markdown/compile.js +26 -0
  12. package/dist/lib/markdown/components/CanvasPage.d.ts +9 -0
  13. package/dist/lib/markdown/components/CanvasPage.jsx +16 -0
  14. package/dist/lib/markdown/components/MarkdownBody.d.ts +11 -0
  15. package/dist/lib/markdown/components/MarkdownBody.jsx +37 -0
  16. package/dist/lib/markdown/components/PiePage.d.ts +14 -0
  17. package/dist/lib/markdown/components/PiePage.jsx +41 -0
  18. package/dist/lib/markdown/components/PythonEditor.d.ts +10 -0
  19. package/dist/lib/markdown/components/PythonEditor.jsx +209 -0
  20. package/dist/lib/markdown/features/build.d.ts +60 -0
  21. package/dist/lib/markdown/features/build.js +36 -0
  22. package/dist/lib/markdown/features/glossary/View.d.ts +9 -0
  23. package/dist/lib/markdown/features/glossary/View.jsx +62 -0
  24. package/dist/lib/markdown/features/glossary/build.d.ts +6 -0
  25. package/dist/lib/markdown/features/glossary/build.js +81 -0
  26. package/dist/lib/markdown/features/sidebar/View.d.ts +11 -0
  27. package/dist/lib/markdown/features/sidebar/View.jsx +45 -0
  28. package/dist/lib/markdown/features/sidebar/build.d.ts +3 -0
  29. package/dist/lib/markdown/features/sidebar/build.js +7 -0
  30. package/dist/lib/markdown/features/tags/build.d.ts +7 -0
  31. package/dist/lib/markdown/features/tags/build.js +22 -0
  32. package/dist/lib/markdown/features/tags/url.d.ts +6 -0
  33. package/dist/lib/markdown/features/tags/url.js +10 -0
  34. package/dist/lib/markdown/features/toc/build.d.ts +7 -0
  35. package/dist/lib/markdown/features/toc/build.js +62 -0
  36. package/dist/lib/markdown/features/views.d.ts +4 -0
  37. package/dist/lib/markdown/features/views.jsx +11 -0
  38. package/dist/lib/markdown/frontmatter.d.ts +44 -0
  39. package/dist/lib/markdown/frontmatter.js +109 -0
  40. package/dist/lib/markdown/render.d.ts +6 -0
  41. package/dist/lib/markdown/render.js +95 -0
  42. package/dist/lib/nodes/animation.d.ts +14 -0
  43. package/dist/lib/nodes/animation.js +34 -0
  44. package/dist/lib/nodes/components/CenteredGrid.d.ts +20 -0
  45. package/dist/lib/nodes/components/CenteredGrid.jsx +44 -0
  46. package/dist/lib/nodes/components/NodeCanvas.d.ts +7 -0
  47. package/dist/lib/nodes/components/NodeCanvas.jsx +50 -0
  48. package/dist/lib/nodes/execute.d.ts +27 -0
  49. package/dist/lib/nodes/execute.js +159 -0
  50. package/dist/lib/nodes/functions/Node2NodeFunc.d.ts +70 -0
  51. package/dist/lib/nodes/functions/Node2NodeFunc.js +223 -0
  52. package/dist/lib/nodes/functions/array.d.ts +12 -0
  53. package/dist/lib/nodes/functions/array.js +31 -0
  54. package/dist/lib/nodes/functions/autodiff.d.ts +12 -0
  55. package/dist/lib/nodes/functions/autodiff.js +60 -0
  56. package/dist/lib/nodes/functions/basic.d.ts +111 -0
  57. package/dist/lib/nodes/functions/basic.js +275 -0
  58. package/dist/lib/nodes/functions/registry.d.ts +9 -0
  59. package/dist/lib/nodes/functions/registry.js +45 -0
  60. package/dist/lib/nodes/functions/sideEffects.d.ts +78 -0
  61. package/dist/lib/nodes/functions/sideEffects.js +201 -0
  62. package/dist/lib/nodes/hooks/useCanvasGestures.d.ts +9 -0
  63. package/dist/lib/nodes/hooks/useCanvasGestures.js +120 -0
  64. package/dist/lib/nodes/hooks/useCanvasResize.d.ts +2 -0
  65. package/dist/lib/nodes/hooks/useCanvasResize.js +33 -0
  66. package/dist/lib/nodes/jax.d.ts +46 -0
  67. package/dist/lib/nodes/jax.js +120 -0
  68. package/dist/lib/nodes/render/renderLogic.d.ts +13 -0
  69. package/dist/lib/nodes/render/renderLogic.jsx +234 -0
  70. package/dist/lib/nodes/state/NameGenerator.d.ts +10 -0
  71. package/dist/lib/nodes/state/NameGenerator.js +29 -0
  72. package/dist/lib/nodes/state/Node.d.ts +134 -0
  73. package/dist/lib/nodes/state/Node.js +57 -0
  74. package/dist/lib/nodes/state/NodeStore.d.ts +49 -0
  75. package/dist/lib/nodes/state/NodeStore.js +138 -0
  76. package/dist/lib/nodes/tensors/broadcast.d.ts +18 -0
  77. package/dist/lib/nodes/tensors/broadcast.js +92 -0
  78. package/dist/lib/nodes/tensors/tensor.d.ts +13 -0
  79. package/dist/lib/nodes/tensors/tensor.js +34 -0
  80. package/dist/lib/python/lift.d.ts +51 -0
  81. package/dist/lib/python/lift.js +155 -0
  82. package/dist/lib/python/py/pie.py +303 -0
  83. package/dist/lib/python/py/pie_mpl.py +15 -0
  84. package/dist/lib/python/py/show_figures.py +5 -0
  85. package/dist/lib/python/runner.d.ts +36 -0
  86. package/dist/lib/python/runner.js +221 -0
  87. package/dist/lib/python/worker.d.ts +1 -0
  88. package/dist/lib/python/worker.js +111 -0
  89. package/dist/lib/ui/UiLayer.d.ts +2 -0
  90. package/dist/lib/ui/UiLayer.jsx +15 -0
  91. package/dist/lib/ui/UiView.d.ts +23 -0
  92. package/dist/lib/ui/UiView.jsx +56 -0
  93. package/dist/lib/ui/components/checkboxes.d.ts +10 -0
  94. package/dist/lib/ui/components/checkboxes.jsx +30 -0
  95. package/dist/lib/ui/components/latex.d.ts +8 -0
  96. package/dist/lib/ui/components/latex.jsx +41 -0
  97. package/dist/lib/ui/components/radio.d.ts +6 -0
  98. package/dist/lib/ui/components/radio.jsx +19 -0
  99. package/dist/lib/ui/components/slider.d.ts +8 -0
  100. package/dist/lib/ui/components/slider.jsx +7 -0
  101. package/dist/lib/ui/components/textbox.d.ts +5 -0
  102. package/dist/lib/ui/components/textbox.jsx +21 -0
  103. package/dist/lib/ui/dragUi.d.ts +10 -0
  104. package/dist/lib/ui/dragUi.js +129 -0
  105. package/dist/lib/ui/nodeHandle.d.ts +15 -0
  106. package/dist/lib/ui/nodeHandle.js +37 -0
  107. package/dist/lib/ui/registry.d.ts +10 -0
  108. package/dist/lib/ui/registry.js +42 -0
  109. package/dist/runtime.css +696 -0
  110. package/dist/runtime.d.ts +9 -0
  111. package/dist/runtime.jsx +6 -0
  112. package/init/index.js +2 -2
  113. package/init/site-agents.md +7 -3
  114. package/package.json +15 -6
  115. package/pages/about.md +4 -0
  116. package/runtime-agents.md +146 -0
  117. package/src/app.css +7 -665
  118. package/src/components/SplitPane.tsx +11 -9
  119. package/src/lib/customModules.ts +30 -0
  120. package/src/lib/markdown/compile.ts +49 -0
  121. package/src/lib/markdown/components/CanvasPage.tsx +9 -4
  122. package/src/lib/markdown/components/PiePage.tsx +48 -0
  123. package/src/lib/markdown/features/build.ts +15 -6
  124. package/src/lib/markdown/features/sidebar/View.tsx +4 -2
  125. package/src/lib/markdown/features/tags/build.ts +6 -3
  126. package/src/lib/markdown/pages.ts +1 -9
  127. package/src/lib/markdown/vitePlugin.ts +4 -8
  128. package/src/lib/nodes/functions/registry.ts +30 -21
  129. package/src/lib/nodes/functions/sideEffects.ts +48 -8
  130. package/src/lib/nodes/functions/vitePlugin.ts +8 -10
  131. package/src/lib/nodes/state/Node.ts +27 -19
  132. package/src/lib/nodes/state/NodeStore.ts +12 -1
  133. package/src/lib/siteModules.ts +13 -0
  134. package/src/lib/ui/dragUi.ts +4 -2
  135. package/src/lib/ui/registry.ts +35 -14
  136. package/src/page.css +664 -0
  137. package/src/routes/[...slug].tsx +2 -3
  138. package/src/runtime.css +29 -0
  139. package/src/runtime.tsx +15 -0
@@ -0,0 +1,129 @@
1
+ /** Class of the layer over the canvas that holds the UIs dropped there. */
2
+ export const UI_LAYER_CLASS = 'canvas-ui-layer';
3
+ /** Pixels the pointer must move before a press on the grip becomes a drag. */
4
+ const DRAG_THRESHOLD = 4;
5
+ /** Within this many pixels of the text's visible top or bottom, a drag scrolls the text. */
6
+ const SCROLL_EDGE = 40;
7
+ const SCROLL_STEP = 10;
8
+ const inside = (r, p) => p.x >= r.left && p.x <= r.right && p.y >= r.top && p.y <= r.bottom;
9
+ /** Keeps `box` inside `layer`, with its top left corner at (`left`, `top`) if that fits. */
10
+ export function clampToLayer(box, layer, left = box.offsetLeft, top = box.offsetTop) {
11
+ const maxLeft = Math.max(0, layer.clientWidth - box.offsetWidth);
12
+ const maxTop = Math.max(0, layer.clientHeight - box.offsetHeight);
13
+ box.style.left = `${Math.min(Math.max(0, left), maxLeft)}px`;
14
+ box.style.top = `${Math.min(Math.max(0, top), maxTop)}px`;
15
+ }
16
+ /** The gap between the blocks of `text` nearest to height `y`: the block it sits before (null: the end) and its height. */
17
+ function nearestGap(text, box, y) {
18
+ // Hidden blocks have no box, so their position means nothing.
19
+ const blocks = [...text.children].filter((block) => block !== box && block.getClientRects().length > 0);
20
+ const rects = blocks.map((block) => block.getBoundingClientRect());
21
+ if (rects.length === 0)
22
+ return { before: null, y: text.getBoundingClientRect().top };
23
+ let best = { before: blocks[0], y: rects[0].top };
24
+ for (let i = 1; i <= rects.length; i++) {
25
+ const gapY = i === rects.length ? rects[i - 1].bottom : (rects[i - 1].bottom + rects[i].top) / 2;
26
+ if (Math.abs(gapY - y) < Math.abs(best.y - y))
27
+ best = { before: blocks[i] ?? null, y: gapY };
28
+ }
29
+ return best;
30
+ }
31
+ /** The nearest ancestor of `el` that scrolls, or the page. */
32
+ function scrollParent(el) {
33
+ for (let p = el.parentElement; p; p = p.parentElement) {
34
+ const { overflowY } = getComputedStyle(p);
35
+ if (overflowY === 'auto' || overflowY === 'scroll')
36
+ return p;
37
+ }
38
+ return document.scrollingElement;
39
+ }
40
+ /**
41
+ * Drags `box`, a UI, from a press `e` on its grip. Dropped over the canvas, it stays at that spot on the screen.
42
+ * Dropped anywhere else, it goes into the nearest gap between the blocks of `text`, the page's text.
43
+ * Near the text's top or bottom edge, the text scrolls.
44
+ */
45
+ export function dragUi(e, box, text) {
46
+ if (e.button !== 0)
47
+ return;
48
+ e.preventDefault();
49
+ const layer = document.querySelector(`.${UI_LAYER_CLASS}`);
50
+ const scroller = scrollParent(text);
51
+ const rect = box.getBoundingClientRect();
52
+ // Where the pointer holds the box, so the box doesn't jump under it.
53
+ const offset = { x: e.clientX - rect.left, y: e.clientY - rect.top };
54
+ const start = { x: e.clientX, y: e.clientY };
55
+ let pointer = start;
56
+ let dragging = false;
57
+ let frame = 0;
58
+ const line = document.createElement('div');
59
+ line.className = 'python-editor-ui-drop-line';
60
+ // Holds the box and the line while dragging. Inside the page, so styles limited to it apply; `fixed` keeps them unclipped.
61
+ const floating = text.parentElement;
62
+ const overLayer = () => layer !== null && inside(layer.getBoundingClientRect(), pointer);
63
+ const update = () => {
64
+ box.style.left = `${pointer.x - offset.x}px`;
65
+ box.style.top = `${pointer.y - offset.y}px`;
66
+ if (overLayer())
67
+ return line.remove();
68
+ const textRect = text.getBoundingClientRect();
69
+ line.style.left = `${textRect.left}px`;
70
+ line.style.width = `${textRect.width}px`;
71
+ line.style.top = `${nearestGap(text, box, pointer.y).y}px`;
72
+ if (!line.isConnected)
73
+ floating.append(line);
74
+ };
75
+ const autoScroll = () => {
76
+ if (!overLayer()) {
77
+ const edges = scroller === document.scrollingElement ? { top: 0, bottom: window.innerHeight } : scroller.getBoundingClientRect();
78
+ const step = pointer.y < edges.top + SCROLL_EDGE ? -SCROLL_STEP : pointer.y > edges.bottom - SCROLL_EDGE ? SCROLL_STEP : 0;
79
+ if (step !== 0) {
80
+ scroller.scrollTop += step;
81
+ update();
82
+ }
83
+ }
84
+ frame = requestAnimationFrame(autoScroll);
85
+ };
86
+ // The box floats under the pointer, above everything, until it is dropped.
87
+ const begin = () => {
88
+ dragging = true;
89
+ document.body.style.userSelect = 'none';
90
+ box.classList.add('python-editor-ui-dragging');
91
+ box.style.position = 'fixed';
92
+ floating.append(box);
93
+ frame = requestAnimationFrame(autoScroll);
94
+ };
95
+ const move = (ev) => {
96
+ pointer = { x: ev.clientX, y: ev.clientY };
97
+ if (!dragging) {
98
+ if (Math.hypot(pointer.x - start.x, pointer.y - start.y) < DRAG_THRESHOLD)
99
+ return;
100
+ begin();
101
+ }
102
+ update();
103
+ };
104
+ const drop = () => {
105
+ window.removeEventListener('pointermove', move);
106
+ window.removeEventListener('pointerup', drop);
107
+ window.removeEventListener('pointercancel', drop);
108
+ if (!dragging)
109
+ return;
110
+ cancelAnimationFrame(frame);
111
+ document.body.style.userSelect = '';
112
+ line.remove();
113
+ box.classList.remove('python-editor-ui-dragging');
114
+ if (layer && overLayer()) {
115
+ const boxRect = box.getBoundingClientRect();
116
+ const layerRect = layer.getBoundingClientRect();
117
+ layer.append(box);
118
+ box.style.position = 'absolute';
119
+ clampToLayer(box, layer, boxRect.left - layerRect.left, boxRect.top - layerRect.top);
120
+ }
121
+ else {
122
+ box.style.position = box.style.left = box.style.top = '';
123
+ text.insertBefore(box, nearestGap(text, box, pointer.y).before);
124
+ }
125
+ };
126
+ window.addEventListener('pointermove', move);
127
+ window.addEventListener('pointerup', drop);
128
+ window.addEventListener('pointercancel', drop);
129
+ }
@@ -0,0 +1,15 @@
1
+ import { Accessor } from 'solid-js';
2
+ import type { AnyNode } from '../nodes/state/Node';
3
+ import type { NodeStore } from '../nodes/state/NodeStore';
4
+ /** A node passed to a UI, as a Solid signal pair: read it with `n()`, write it with `setN(changes)`. */
5
+ export type NodeHandle<N extends AnyNode = AnyNode> = [Accessor<N>, (changes: Partial<N>) => void];
6
+ /** A positional argument of `pie.ui.<name>(...)`: a node, or a list of them at any depth. */
7
+ export type UiArg = NodeHandle | UiArg[];
8
+ export declare const isHandle: (arg: UiArg) => arg is NodeHandle;
9
+ /** Reads node `id` from the store each time, so it follows the node when it is replaced. */
10
+ export declare function nodeHandle<N extends AnyNode>(store: NodeStore, id: string): NodeHandle<N>;
11
+ export declare const StoreContext: import("solid-js").Context<NodeStore | undefined>;
12
+ /** Inside a UI: writes a node it did not get as an argument, e.g. an Array's item. */
13
+ export declare function useSetNode(): <N extends AnyNode>(node: N, changes: Partial<N>) => void;
14
+ /** `arg` as a node of one of `types`. Throws otherwise; the UI shows the error. */
15
+ export declare function expectNode<N extends AnyNode>(arg: UiArg | undefined, types: N['type'][]): NodeHandle<N>;
@@ -0,0 +1,37 @@
1
+ import { createContext, untrack, useContext } from 'solid-js';
2
+ import { replaceNode } from '../nodes/functions/sideEffects';
3
+ export const isHandle = (arg) => typeof arg[0] === 'function';
4
+ /**
5
+ * Saves `node` with `changes`, like dragging a point: it no longer follows its inputs.
6
+ * To change an Array, write its items: replacing the Array removes its item nodes.
7
+ */
8
+ function setNode(store, node, changes) {
9
+ replaceNode(store, node, changes, false);
10
+ }
11
+ /** Reads node `id` from the store each time, so it follows the node when it is replaced. */
12
+ export function nodeHandle(store, id) {
13
+ const get = () => store.getNodeValue(id);
14
+ const set = (changes) => {
15
+ const node = untrack(get);
16
+ if (node && node.type !== 'Dummy')
17
+ setNode(store, node, changes);
18
+ };
19
+ return [get, set];
20
+ }
21
+ export const StoreContext = createContext();
22
+ /** Inside a UI: writes a node it did not get as an argument, e.g. an Array's item. */
23
+ export function useSetNode() {
24
+ const store = useContext(StoreContext);
25
+ if (!store)
26
+ throw new Error('useSetNode works only inside a UI');
27
+ return (node, changes) => setNode(store, node, changes);
28
+ }
29
+ /** `arg` as a node of one of `types`. Throws otherwise; the UI shows the error. */
30
+ export function expectNode(arg, types) {
31
+ if (arg === undefined || !isHandle(arg))
32
+ throw new Error(`expects a ${types.join(' or ')} node, got ${arg === undefined ? 'nothing' : 'a list'}`);
33
+ const type = untrack(arg[0]).type;
34
+ if (!types.includes(type))
35
+ throw new Error(`expects a ${types.join(' or ')} node, got a ${type}`);
36
+ return arg;
37
+ }
@@ -0,0 +1,10 @@
1
+ import type { Component } from 'solid-js';
2
+ import type { CustomModule } from '../customModules';
3
+ /** A UI: a Solid component. `args` holds the call's positional arguments; its keyword arguments are the other props. */
4
+ export type UiComponent = Component<any>;
5
+ /**
6
+ * Replaces the custom UIs with the PascalCase exports of `modules`, e.g. a site's `ui/**\/*.{ts,tsx}`.
7
+ * Throws on a name used twice, leaving the UIs as they were.
8
+ */
9
+ export declare function setCustomUis(modules: CustomModule[]): void;
10
+ export declare function getUi(name: string): UiComponent | undefined;
@@ -0,0 +1,42 @@
1
+ import * as checkboxes from './components/checkboxes';
2
+ import * as latex from './components/latex';
3
+ import * as radio from './components/radio';
4
+ import * as slider from './components/slider';
5
+ import * as textbox from './components/textbox';
6
+ const BUILTINS = [
7
+ { file: 'src/lib/ui/components/checkboxes.tsx', exports: checkboxes },
8
+ { file: 'src/lib/ui/components/latex.tsx', exports: latex },
9
+ { file: 'src/lib/ui/components/radio.tsx', exports: radio },
10
+ { file: 'src/lib/ui/components/slider.tsx', exports: slider },
11
+ { file: 'src/lib/ui/components/textbox.tsx', exports: textbox },
12
+ ];
13
+ const isComponent = (name, value) => /^[A-Z][A-Za-z0-9]*$/.test(name) && typeof value === 'function';
14
+ /** Its name in Python: `Slider` gives `pie.ui.slider`, `MatrixProduct` gives `pie.ui.matrix_product`. */
15
+ const pythonName = (exportName) => exportName.replace(/(?<!^)([A-Z])/g, '_$1').toLowerCase();
16
+ let components = new Map();
17
+ /**
18
+ * Replaces the custom UIs with the PascalCase exports of `modules`, e.g. a site's `ui/**\/*.{ts,tsx}`.
19
+ * Throws on a name used twice, leaving the UIs as they were.
20
+ */
21
+ export function setCustomUis(modules) {
22
+ const uis = new Map();
23
+ const sourceFile = new Map();
24
+ for (const { file, exports } of [...BUILTINS, ...modules]) {
25
+ for (const [exportName, value] of Object.entries(exports)) {
26
+ if (!isComponent(exportName, value))
27
+ continue;
28
+ const name = pythonName(exportName);
29
+ const other = sourceFile.get(name);
30
+ if (other !== undefined) {
31
+ throw new Error(`Duplicate UI 'pie.ui.${name}': ${file} and ${other}`);
32
+ }
33
+ uis.set(name, value);
34
+ sourceFile.set(name, file);
35
+ }
36
+ }
37
+ components = uis;
38
+ }
39
+ setCustomUis([]);
40
+ export function getUi(name) {
41
+ return components.get(name);
42
+ }