@tinyvolt/pie 0.9.0 → 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 (135) 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 +2 -1
  114. package/package.json +15 -6
  115. package/runtime-agents.md +146 -0
  116. package/src/app.css +7 -665
  117. package/src/lib/customModules.ts +30 -0
  118. package/src/lib/markdown/compile.ts +49 -0
  119. package/src/lib/markdown/components/PiePage.tsx +48 -0
  120. package/src/lib/markdown/features/build.ts +15 -6
  121. package/src/lib/markdown/features/sidebar/View.tsx +4 -2
  122. package/src/lib/markdown/features/tags/build.ts +6 -3
  123. package/src/lib/markdown/pages.ts +1 -9
  124. package/src/lib/markdown/vitePlugin.ts +4 -8
  125. package/src/lib/nodes/functions/registry.ts +30 -21
  126. package/src/lib/nodes/functions/sideEffects.ts +14 -8
  127. package/src/lib/nodes/functions/vitePlugin.ts +8 -10
  128. package/src/lib/nodes/state/Node.ts +27 -19
  129. package/src/lib/siteModules.ts +13 -0
  130. package/src/lib/ui/dragUi.ts +4 -2
  131. package/src/lib/ui/registry.ts +35 -14
  132. package/src/page.css +664 -0
  133. package/src/routes/[...slug].tsx +1 -0
  134. package/src/runtime.css +29 -0
  135. package/src/runtime.tsx +15 -0
@@ -0,0 +1,49 @@
1
+ import { buildFeatures, type FeatureData, type FeatureValues } from './features/build';
2
+ import { parseFrontmatter, type Frontmatter } from './frontmatter';
3
+ import { renderMarkdown } from './render';
4
+
5
+ /** One page as HTML, with its frontmatter and its features' data. */
6
+ export interface Page {
7
+ frontmatter: Frontmatter;
8
+ html: string;
9
+ features: FeatureData;
10
+ }
11
+
12
+ /**
13
+ * Compiles the markdown of the page `file`. Throws, naming the file, on bad frontmatter or a feature's bad value.
14
+ * `site`: each feature's value for a page that leaves its key out.
15
+ * `readFile(from, path)`: the markdown, without its frontmatter, of the file at `path`, set in `from`.
16
+ */
17
+ export function compilePage(
18
+ file: string,
19
+ markdown: string,
20
+ site: Partial<FeatureValues>,
21
+ readFile: (from: string, path: string) => string,
22
+ tagLinks: boolean,
23
+ ): Page {
24
+ const { frontmatter, raw, body } = parseFrontmatter(file, markdown);
25
+ const { data, extensions } = buildFeatures(file, raw, site, readFile, tagLinks);
26
+ return { frontmatter, html: renderMarkdown(body, extensions), features: data };
27
+ }
28
+
29
+ export interface CompileOptions {
30
+ /** The page's name in error messages. Default: `page.md`. */
31
+ file?: string;
32
+ /** What a site's `site.yml` gives: each feature's value for a page that leaves its key out. */
33
+ site?: Partial<FeatureValues>;
34
+ /** The text of another `.md` file, e.g. `_topics.md` for `sidebar: _topics.md`. `path` is as the page wrote it. */
35
+ readFile?(path: string): string;
36
+ }
37
+
38
+ /**
39
+ * Compiles pie markdown into a `Page` for `PiePage`, as a site build would, in the browser or on a server.
40
+ * Throws with the messages a site build gives. Tags are plain text: there are no tag pages to link to.
41
+ */
42
+ export function compilePie(markdown: string, options: CompileOptions = {}): Page {
43
+ const { file = 'page.md', site = {}, readFile } = options;
44
+ const read = (_from: string, path: string) => {
45
+ if (!readFile) throw new Error(`file ${path} does not exist`);
46
+ return parseFrontmatter(path, readFile(path)).body;
47
+ };
48
+ return compilePage(file, markdown, site, read, false);
49
+ }
@@ -0,0 +1,48 @@
1
+ import { createMemo, onCleanup, Show } from 'solid-js';
2
+ import { isServer } from 'solid-js/web';
3
+ import 'katex/dist/katex.min.css';
4
+ import { setCustomModules, type CustomModules } from '~/lib/customModules';
5
+ import type { Page } from '../compile';
6
+ import { wrapPage } from '../features/views';
7
+ import { CanvasPage } from './CanvasPage';
8
+
9
+ // Custom modules are global, so only one PiePage can use them at a time.
10
+ let mounted = false;
11
+
12
+ /** Sets `modules` until the calling component is cleaned up. */
13
+ function useModules(modules: CustomModules) {
14
+ if (mounted) throw new Error('Only one PiePage can be mounted at a time');
15
+ setCustomModules(modules);
16
+ mounted = true;
17
+ onCleanup(() => {
18
+ setCustomModules({});
19
+ mounted = false;
20
+ });
21
+ }
22
+
23
+ /**
24
+ * A page from `compilePie`, shown as a pie site shows it, in a `.pie-page` box that fills its parent.
25
+ * `modules`: the host's own functions, node types and UIs. `css`: author CSS, limited to the page.
26
+ * A new `page`, `modules` or `css` starts a fresh canvas and fresh Python variables.
27
+ * Throws on a name used twice in `modules`, or when another PiePage is mounted.
28
+ */
29
+ export function PiePage(props: { page: Page; modules?: CustomModules; css?: string }) {
30
+ const current = createMemo(() => ({ page: props.page, modules: props.modules, css: props.css }));
31
+
32
+ return (
33
+ <div class="pie-page">
34
+ <Show when={current()} keyed>
35
+ {({ page, modules, css }) => {
36
+ // Only in the browser: the server draws no nodes and runs no Python, and every request would share its modules.
37
+ if (!isServer) useModules(modules ?? {});
38
+ return (
39
+ <>
40
+ {css && <style>{`.pie-page {\n${css}\n}`}</style>}
41
+ {wrapPage(page.features, <CanvasPage side={page.frontmatter.canvas} html={page.html} />)}
42
+ </>
43
+ );
44
+ }}
45
+ </Show>
46
+ </div>
47
+ );
48
+ }
@@ -12,7 +12,7 @@
12
12
  * A feature that only changes the page's HTML, like `toc`, has no view.
13
13
  */
14
14
  import type { MarkedExtension } from 'marked';
15
- import type { Field } from '../frontmatter';
15
+ import type { Field, Values } from '../frontmatter';
16
16
  import { glossary } from './glossary/build';
17
17
  import { sidebar } from './sidebar/build';
18
18
  import { tags } from './tags/build';
@@ -26,6 +26,8 @@ export interface BuildContext {
26
26
  from: string;
27
27
  /** The markdown of the `.md` file at `path`, relative to the folder of `from`, without its frontmatter. Throws if it's missing or outside the project. */
28
28
  readFile(path: string): string;
29
+ /** False inside another app (`compilePie`), which has no tag pages to link to. */
30
+ tagLinks: boolean;
29
31
  }
30
32
 
31
33
  /** A feature's build half. `T`: its frontmatter value. `D`: the data its view gets. */
@@ -53,20 +55,27 @@ export const FEATURE_FIELDS = Object.fromEntries(
53
55
  Object.entries(FEATURES).map(([key, feature]) => [key, feature.field]),
54
56
  ) as { [K in keyof typeof FEATURES]: (typeof FEATURES)[K]['field'] };
55
57
 
56
- /** Runs every feature's `build` for the page `file`. Throws, naming the file and key, on a bad value. */
58
+ /** Each feature's value, as `site.yml` gives it. */
59
+ export type FeatureValues = Values<typeof FEATURE_FIELDS>;
60
+
61
+ /**
62
+ * Runs every feature's `build` for the page `file`. Throws, naming the file and key, on a bad value.
63
+ * A key that neither the page nor `site` sets takes its field's default.
64
+ */
57
65
  export function buildFeatures(
58
66
  file: string,
59
67
  raw: Record<string, string>,
60
- site: Record<string, unknown>,
68
+ site: Partial<FeatureValues>,
61
69
  readFile: (from: string, path: string) => string,
70
+ tagLinks: boolean,
62
71
  ): { data: FeatureData; extensions: MarkedExtension[] } {
63
72
  const data: Record<string, unknown> = {};
64
73
  const extensions: MarkedExtension[] = [];
65
- for (const [key, feature] of Object.entries(FEATURES) as [string, FeatureBuild<unknown, unknown>][]) {
74
+ for (const [key, feature] of Object.entries(FEATURES) as [keyof FeatureValues, FeatureBuild<unknown, unknown>][]) {
66
75
  const from = Object.hasOwn(raw, key) ? file : 'site.yml';
67
76
  try {
68
- const value = from === file ? feature.field.parse(raw[key]) : site[key];
69
- const ctx: BuildContext = { file, from, readFile: path => readFile(from, path) };
77
+ const value = from === file ? feature.field.parse(raw[key]) : site[key] ?? feature.field.default;
78
+ const ctx: BuildContext = { file, from, readFile: path => readFile(from, path), tagLinks };
70
79
  const built = feature.build(value, ctx);
71
80
  if (built === undefined) continue;
72
81
  data[key] = built;
@@ -14,9 +14,11 @@ const DRAWER_BELOW = '768px';
14
14
  export function SidebarView(props: { data: string; children: JSX.Element }) {
15
15
  const [open, setOpen] = createSignal(false);
16
16
  const [drawer, setDrawer] = createSignal(false);
17
+ let page!: HTMLDivElement;
17
18
 
18
19
  onMount(() => {
19
- const below = getComputedStyle(document.documentElement).getPropertyValue('--sidebar-drawer-below').trim() || DRAWER_BELOW;
20
+ // Read here, not on `:root`, so a host can set it on its `.pie-page`.
21
+ const below = getComputedStyle(page).getPropertyValue('--sidebar-drawer-below').trim() || DRAWER_BELOW;
20
22
  const query = matchMedia(`(width < ${below})`);
21
23
  const update = () => setDrawer(query.matches);
22
24
  update();
@@ -31,7 +33,7 @@ export function SidebarView(props: { data: string; children: JSX.Element }) {
31
33
  const label = () => (open() ? 'Hide sidebar' : 'Show sidebar');
32
34
 
33
35
  return (
34
- <div class="sidebar-page" classList={{ 'sidebar-page--open': open(), 'sidebar-page--drawer': drawer() }}>
36
+ <div ref={page} class="sidebar-page" classList={{ 'sidebar-page--open': open(), 'sidebar-page--drawer': drawer() }}>
35
37
  <aside id="sidebar" class="sidebar" onClick={closeOnLink}>
36
38
  <MarkdownBody class="sidebar-text" html={props.data} />
37
39
  </aside>
@@ -7,15 +7,18 @@ import { tagUrl } from './url';
7
7
  /**
8
8
  * `tags: #math #linear-algebra`: the page's tags, as links at the end of its text. Each link opens the page
9
9
  * listing the pages with that tag, in `src/routes/_tags/`. Only a page's own frontmatter counts, not `site.yml`.
10
+ * Without `tagLinks`, the tags are plain text.
10
11
  */
11
12
  export const tags: FeatureBuild<string[], true> = {
12
13
  field: hashtags(),
13
14
  build: (tags, ctx) => (ctx.from === ctx.file && tags.length > 0 ? true : undefined),
14
- markdown(tags) {
15
- const links = tags.map(tag => `<a href="${escapeHtml(tagUrl(tag))}">#${escapeHtml(tag)}</a>`).join('');
15
+ markdown(tags, ctx) {
16
+ const items = tags.map(tag => (ctx.tagLinks
17
+ ? `<a href="${escapeHtml(tagUrl(tag))}">#${escapeHtml(tag)}</a>`
18
+ : `<span>#${escapeHtml(tag)}</span>`)).join('');
16
19
  return {
17
20
  hooks: {
18
- postprocess: html => `${html}<nav class="tags">${links}</nav>\n`,
21
+ postprocess: html => `${html}<nav class="tags">${items}</nav>\n`,
19
22
  },
20
23
  } satisfies MarkedExtension;
21
24
  },
@@ -1,12 +1,4 @@
1
- import type { FeatureData } from './features/build';
2
- import type { Frontmatter } from './frontmatter';
3
-
4
- /** One `pages/**\/*.md`, as `vitePlugin.ts` turns it into a module at build time. */
5
- export interface Page {
6
- frontmatter: Frontmatter;
7
- html: string;
8
- features: FeatureData;
9
- }
1
+ import type { Page } from './compile';
10
2
 
11
3
  // Not eager: each page is its own file, downloaded only when that page is opened.
12
4
  // A file whose name starts with `_` is not a page; it can still be used by a feature, e.g. as a sidebar.
@@ -1,11 +1,10 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import type { Plugin } from 'vite';
4
- import { buildFeatures, FEATURE_FIELDS } from './features/build';
4
+ import { compilePage } from './compile';
5
+ import { FEATURE_FIELDS } from './features/build';
5
6
  import type { PageTags } from './features/tags/pages';
6
7
  import { parseFrontmatter } from './frontmatter';
7
- import type { Page } from './pages';
8
- import { renderMarkdown } from './render';
9
8
  import { APP_DIR } from '../appDir';
10
9
  import { parseSite } from '../site/vitePlugin';
11
10
 
@@ -28,10 +27,8 @@ export function markdownPages(): Plugin {
28
27
  const [filePath, query] = id.split('?');
29
28
  const file = path.relative(root, filePath);
30
29
  if (!file.startsWith(`pages${path.sep}`) || !file.endsWith('.md')) return;
31
- const { frontmatter, raw, body } = parseFrontmatter(file, markdown);
32
-
33
30
  if (new URLSearchParams(query).has('tags')) {
34
- const { title, description } = frontmatter;
31
+ const { frontmatter: { title, description }, raw } = parseFrontmatter(file, markdown);
35
32
  const page: PageTags = { title, description, tags: FEATURE_FIELDS.tags.parse(raw.tags ?? '') };
36
33
  return { code: `export default ${JSON.stringify(page)};`, map: null };
37
34
  }
@@ -53,8 +50,7 @@ export function markdownPages(): Plugin {
53
50
  return parseFrontmatter(shown, fs.readFileSync(target, 'utf8')).body;
54
51
  };
55
52
 
56
- const { data, extensions } = buildFeatures(file, raw, site, readFile);
57
- const page: Page = { frontmatter, html: renderMarkdown(body, extensions), features: data };
53
+ const page = compilePage(file, markdown, site, readFile, true);
58
54
  return { code: `export default ${JSON.stringify(page)};`, map: null };
59
55
  },
60
56
  };
@@ -1,3 +1,4 @@
1
+ import type { CustomModule } from '~/lib/customModules';
1
2
  import { Node2NodeFunc, SideEffectFunc } from './Node2NodeFunc';
2
3
  import * as basicNodes from './basic';
3
4
  import * as autodiffNodes from './autodiff';
@@ -10,33 +11,41 @@ const isNode2NodeFunc = (fn: unknown): fn is Node2NodeFunc =>
10
11
  const isSideEffectFunc = (fn: unknown): fn is SideEffectFunc =>
11
12
  typeof fn === 'object' && fn !== null && typeof (fn as SideEffectFunc).run === 'function';
12
13
 
13
- // The site's own functions: each `functions/**/*.ts` exports `Node2NodeFunc`s, like `basic.ts`.
14
- const customModules = import.meta.glob<Record<string, unknown>>('/functions/**/*.ts', { eager: true });
15
-
16
- // File path -> its exports. Built-ins first.
17
- const modules: [string, Record<string, unknown>][] = [
18
- ['src/lib/nodes/functions/basic.ts', basicNodes],
19
- ['src/lib/nodes/functions/autodiff.ts', autodiffNodes],
20
- ['src/lib/nodes/functions/array.ts', arrayNodes],
21
- ['src/lib/nodes/functions/sideEffects.ts', sideEffects],
22
- ...Object.entries(customModules).map(([path, mod]): [string, Record<string, unknown>] => [path.slice(1), mod]),
14
+ const BUILTINS: CustomModule[] = [
15
+ { file: 'src/lib/nodes/functions/basic.ts', exports: basicNodes },
16
+ { file: 'src/lib/nodes/functions/autodiff.ts', exports: autodiffNodes },
17
+ { file: 'src/lib/nodes/functions/array.ts', exports: arrayNodes },
18
+ { file: 'src/lib/nodes/functions/sideEffects.ts', exports: sideEffects },
23
19
  ];
24
20
 
25
- export const functionRegistry = new Map<string, Node2NodeFunc>();
26
- export const sideEffectRegistry = new Map<string, SideEffectFunc>();
27
- const sourceFile = new Map<string, string>();
28
- for (const [file, mod] of modules) {
29
- for (const fn of Object.values(mod).filter(fn => isNode2NodeFunc(fn) || isSideEffectFunc(fn))) {
30
- const other = sourceFile.get(fn.keyword);
31
- if (other !== undefined) {
32
- throw new Error(`Duplicate function keyword '${fn.keyword}': ${file} and ${other}`);
21
+ let functionRegistry = new Map<string, Node2NodeFunc>();
22
+ let sideEffectRegistry = new Map<string, SideEffectFunc>();
23
+
24
+ /**
25
+ * Replaces the custom functions with the `Node2NodeFunc`s and `SideEffectFunc`s that `modules` export, e.g. a site's `functions/**\/*.ts`.
26
+ * Throws on a keyword used twice, leaving the functions as they were.
27
+ */
28
+ export function setCustomFunctions(modules: CustomModule[]) {
29
+ const functions = new Map<string, Node2NodeFunc>();
30
+ const effects = new Map<string, SideEffectFunc>();
31
+ const sourceFile = new Map<string, string>();
32
+ for (const { file, exports } of [...BUILTINS, ...modules]) {
33
+ for (const fn of Object.values(exports).filter(fn => isNode2NodeFunc(fn) || isSideEffectFunc(fn))) {
34
+ const other = sourceFile.get(fn.keyword);
35
+ if (other !== undefined) {
36
+ throw new Error(`Duplicate function keyword '${fn.keyword}': ${file} and ${other}`);
37
+ }
38
+ if (isSideEffectFunc(fn)) effects.set(fn.keyword, fn);
39
+ else functions.set(fn.keyword, fn);
40
+ sourceFile.set(fn.keyword, file);
33
41
  }
34
- if (isSideEffectFunc(fn)) sideEffectRegistry.set(fn.keyword, fn);
35
- else functionRegistry.set(fn.keyword, fn);
36
- sourceFile.set(fn.keyword, file);
37
42
  }
43
+ functionRegistry = functions;
44
+ sideEffectRegistry = effects;
38
45
  }
39
46
 
47
+ setCustomFunctions([]);
48
+
40
49
  export function getFunction(keyword: string): Node2NodeFunc | undefined {
41
50
  return functionRegistry.get(keyword);
42
51
  }
@@ -1,5 +1,5 @@
1
1
  import { createMemo, createRoot, createSignal, onCleanup } from 'solid-js';
2
- import { SideEffectFunc } from './Node2NodeFunc';
2
+ import { ParameterDefinition, SideEffectFunc } from './Node2NodeFunc';
3
3
  import { AnyNode, PointNode, ScalarNode, TextNode, checkColor, getNodeTypes } from '../state/Node';
4
4
  import { NodeStore } from '../state/NodeStore';
5
5
  import { Dtype, add, num, ref, scalar } from '../jax';
@@ -35,8 +35,14 @@ export function replaceNode<N extends AnyNode>(store: NodeStore, node: N, change
35
35
  });
36
36
  }
37
37
 
38
- // Any node, but not a whole Array: an Array is looped over, so each item is hidden or shown.
39
- const anyItem = getNodeTypes().filter(type => type !== 'Array');
38
+ // A parameter that takes any node, but not a whole Array: an Array is looped over, so each item is hidden or shown.
39
+ // A getter, so it includes custom node types set after this file loads.
40
+ const anyItem = (name: string): ParameterDefinition => ({
41
+ name,
42
+ get type() {
43
+ return getNodeTypes().filter(type => type !== 'Array');
44
+ },
45
+ });
40
46
 
41
47
  /**
42
48
  * Hide a node: it is not drawn on the canvas.
@@ -44,7 +50,7 @@ const anyItem = getNodeTypes().filter(type => type !== 'Array');
44
50
  */
45
51
  export const Hide: SideEffectFunc = {
46
52
  keyword: 'hide',
47
- parameters: [{ name: 'node', type: anyItem }],
53
+ parameters: [anyItem('node')],
48
54
  run(inputs, store) {
49
55
  store.updateNode(inputs[0].id, { hidden: true });
50
56
  },
@@ -56,7 +62,7 @@ export const Hide: SideEffectFunc = {
56
62
  */
57
63
  export const Show: SideEffectFunc = {
58
64
  keyword: 'show',
59
- parameters: [{ name: 'node', type: anyItem }],
65
+ parameters: [anyItem('node')],
60
66
  run(inputs, store) {
61
67
  store.updateNode(inputs[0].id, { hidden: false });
62
68
  },
@@ -68,7 +74,7 @@ export const Show: SideEffectFunc = {
68
74
  */
69
75
  export const Remove: SideEffectFunc = {
70
76
  keyword: 'remove',
71
- parameters: [{ name: 'node', type: anyItem }],
77
+ parameters: [anyItem('node')],
72
78
  run(inputs, store) {
73
79
  store.removeNode(inputs[0].id);
74
80
  },
@@ -81,7 +87,7 @@ export const Remove: SideEffectFunc = {
81
87
  */
82
88
  export const SetFill: SideEffectFunc = {
83
89
  keyword: 'set_fill',
84
- parameters: [{ name: 'node', type: anyItem }, { name: 'color', type: 'Text' }],
90
+ parameters: [anyItem('node'), { name: 'color', type: 'Text' }],
85
91
  run(inputs, store) {
86
92
  const fill = (inputs[1] as TextNode).value;
87
93
  checkColor('set_fill', 'fill', fill);
@@ -96,7 +102,7 @@ export const SetFill: SideEffectFunc = {
96
102
  */
97
103
  export const SetStroke: SideEffectFunc = {
98
104
  keyword: 'set_stroke',
99
- parameters: [{ name: 'node', type: anyItem }, { name: 'color', type: 'Text' }],
105
+ parameters: [anyItem('node'), { name: 'color', type: 'Text' }],
100
106
  run(inputs, store) {
101
107
  const stroke = (inputs[1] as TextNode).value;
102
108
  checkColor('set_stroke', 'stroke', stroke);
@@ -3,8 +3,8 @@ import { runnerImport, type Plugin } from 'vite';
3
3
  import { APP_DIR } from '../../appDir';
4
4
 
5
5
  /**
6
- * Loads `registry.ts` and `state/Node.ts` once at build start, so a bad `functions/**\/*.ts`
7
- * (e.g. a keyword used twice) or `nodes/**\/*.{ts,tsx}` (e.g. a type used twice) fails the build.
6
+ * Loads `siteModules.ts` once at build start, so a bad `functions/**\/*.ts` (e.g. a keyword used twice),
7
+ * `nodes/**\/*.{ts,tsx}` (e.g. a type used twice) or `ui/**\/*.{ts,tsx}` (e.g. a name used twice) fails the build.
8
8
  * Without this, the error only stops pages from prerendering.
9
9
  */
10
10
  export function checkFunctions(): Plugin {
@@ -22,16 +22,14 @@ export function checkFunctions(): Plugin {
22
22
  if (checked) return;
23
23
  checked = true;
24
24
  try {
25
- const options = {
26
- configFile: false as const,
25
+ await runnerImport(path.join(APP_DIR, 'lib/siteModules.ts'), {
26
+ configFile: false,
27
27
  root,
28
- logLevel: 'silent' as const,
28
+ logLevel: 'silent',
29
29
  resolve: { alias: { '~': APP_DIR } },
30
- // Render functions use JSX. They never run here, so any JSX runtime that loads will do.
31
- oxc: { jsx: { runtime: 'automatic' as const, importSource: 'solid-js/h' } },
32
- };
33
- await runnerImport(path.join(APP_DIR, 'lib/nodes/functions/registry.ts'), options);
34
- await runnerImport(path.join(APP_DIR, 'lib/nodes/state/Node.ts'), options);
30
+ // Render functions and UIs use JSX. They never run here, so any JSX runtime that loads will do.
31
+ oxc: { jsx: { runtime: 'automatic', importSource: 'solid-js/h' } },
32
+ });
35
33
  } catch (e) {
36
34
  this.error((e as Error).message);
37
35
  }
@@ -1,4 +1,5 @@
1
1
  import type { Accessor, JSXElement } from 'solid-js';
2
+ import type { CustomModule } from '~/lib/customModules';
2
3
  import type { Dtype } from '../jax';
3
4
  import {
4
5
  renderPoint,
@@ -176,32 +177,39 @@ export type NodeType = keyof NodeTypeMap;
176
177
  const isNodeDefinition = (def: unknown): def is NodeDefinition =>
177
178
  typeof def === 'object' && def !== null && typeof (def as NodeDefinition).type === 'string';
178
179
 
179
- // The site's own node types: each `nodes/**/*.{ts,tsx}` exports `NodeDefinition`s, like `nodes/square.tsx`.
180
- const customModules = import.meta.glob<Record<string, unknown>>('/nodes/**/*.{ts,tsx}', { eager: true });
181
-
182
- // File path -> its definitions. Built-ins first.
183
- const definitionsByFile: [string, unknown[]][] = [
184
- ['src/lib/nodes/state/Node.ts', [
180
+ const BUILTINS: CustomModule[] = [{
181
+ file: 'src/lib/nodes/state/Node.ts',
182
+ exports: {
185
183
  TextDefinition, PointDefinition, ScalarDefinition, TriangleDefinition, LineDefinition, CircleDefinition,
186
184
  EllipseDefinition, BezierQuadraticDefinition, BezierCubicDefinition, ArcDefinition, PointGradDefinition,
187
185
  ArrayDefinition, DummyDefinition,
188
- ]],
189
- ...Object.entries(customModules).map(([path, mod]): [string, unknown[]] => [path.slice(1), Object.values(mod)]),
190
- ];
191
-
192
- const nodeDefinitions = new Map<string, NodeDefinition>();
193
- const sourceFile = new Map<string, string>();
194
- for (const [file, defs] of definitionsByFile) {
195
- for (const def of defs.filter(isNodeDefinition)) {
196
- const other = sourceFile.get(def.type);
197
- if (other !== undefined) {
198
- throw new Error(`Duplicate node type '${def.type}': ${file} and ${other}`);
186
+ },
187
+ }];
188
+
189
+ let nodeDefinitions = new Map<string, NodeDefinition>();
190
+
191
+ /**
192
+ * Replaces the custom node types with the `NodeDefinition`s that `modules` export, e.g. a site's `nodes/**\/*.{ts,tsx}`.
193
+ * Throws on a type used twice, leaving the types as they were.
194
+ */
195
+ export function setCustomNodes(modules: CustomModule[]) {
196
+ const definitions = new Map<string, NodeDefinition>();
197
+ const sourceFile = new Map<string, string>();
198
+ for (const { file, exports } of [...BUILTINS, ...modules]) {
199
+ for (const def of Object.values(exports).filter(isNodeDefinition)) {
200
+ const other = sourceFile.get(def.type);
201
+ if (other !== undefined) {
202
+ throw new Error(`Duplicate node type '${def.type}': ${file} and ${other}`);
203
+ }
204
+ definitions.set(def.type, def);
205
+ sourceFile.set(def.type, file);
199
206
  }
200
- nodeDefinitions.set(def.type, def);
201
- sourceFile.set(def.type, file);
202
207
  }
208
+ nodeDefinitions = definitions;
203
209
  }
204
210
 
211
+ setCustomNodes([]);
212
+
205
213
  export function getNodeDefinition(type: string): NodeDefinition | undefined {
206
214
  return nodeDefinitions.get(type);
207
215
  }
@@ -0,0 +1,13 @@
1
+ // The site's own functions, node types and UIs: its `functions/`, `nodes/` and `ui/` folders.
2
+ // The page route imports this file. `checkFunctions` loads it at build start, so a name used twice fails the build.
3
+ import { setCustomModules, type CustomModule } from './customModules';
4
+
5
+ /** `/functions/a.ts` -> `functions/a.ts`, with its exports. */
6
+ const files = (modules: Record<string, Record<string, unknown>>): CustomModule[] =>
7
+ Object.entries(modules).map(([path, exports]) => ({ file: path.slice(1), exports }));
8
+
9
+ setCustomModules({
10
+ functions: files(import.meta.glob('/functions/**/*.ts', { eager: true })),
11
+ nodes: files(import.meta.glob('/nodes/**/*.{ts,tsx}', { eager: true })),
12
+ ui: files(import.meta.glob('/ui/**/*.{ts,tsx}', { eager: true })),
13
+ });
@@ -61,6 +61,8 @@ export function dragUi(e: PointerEvent, box: HTMLElement, text: HTMLElement) {
61
61
  let frame = 0;
62
62
  const line = document.createElement('div');
63
63
  line.className = 'python-editor-ui-drop-line';
64
+ // Holds the box and the line while dragging. Inside the page, so styles limited to it apply; `fixed` keeps them unclipped.
65
+ const floating = text.parentElement!;
64
66
 
65
67
  const overLayer = () => layer !== null && inside(layer.getBoundingClientRect(), pointer);
66
68
 
@@ -72,7 +74,7 @@ export function dragUi(e: PointerEvent, box: HTMLElement, text: HTMLElement) {
72
74
  line.style.left = `${textRect.left}px`;
73
75
  line.style.width = `${textRect.width}px`;
74
76
  line.style.top = `${nearestGap(text, box, pointer.y).y}px`;
75
- if (!line.isConnected) document.body.append(line);
77
+ if (!line.isConnected) floating.append(line);
76
78
  };
77
79
 
78
80
  const autoScroll = () => {
@@ -93,7 +95,7 @@ export function dragUi(e: PointerEvent, box: HTMLElement, text: HTMLElement) {
93
95
  document.body.style.userSelect = 'none';
94
96
  box.classList.add('python-editor-ui-dragging');
95
97
  box.style.position = 'fixed';
96
- document.body.append(box);
98
+ floating.append(box);
97
99
  frame = requestAnimationFrame(autoScroll);
98
100
  };
99
101
 
@@ -1,11 +1,21 @@
1
1
  import type { Component } from 'solid-js';
2
+ import type { CustomModule } from '~/lib/customModules';
3
+ import * as checkboxes from './components/checkboxes';
4
+ import * as latex from './components/latex';
5
+ import * as radio from './components/radio';
6
+ import * as slider from './components/slider';
7
+ import * as textbox from './components/textbox';
2
8
 
3
9
  /** A UI: a Solid component. `args` holds the call's positional arguments; its keyword arguments are the other props. */
4
10
  export type UiComponent = Component<any>;
5
11
 
6
- // Built-ins, then the site's own: each PascalCase export of a `ui/**/*.{ts,tsx}` file is a UI.
7
- const builtinModules = import.meta.glob<Record<string, unknown>>('./components/*.tsx', { eager: true });
8
- const customModules = import.meta.glob<Record<string, unknown>>('/ui/**/*.{ts,tsx}', { eager: true });
12
+ const BUILTINS: CustomModule[] = [
13
+ { file: 'src/lib/ui/components/checkboxes.tsx', exports: checkboxes },
14
+ { file: 'src/lib/ui/components/latex.tsx', exports: latex },
15
+ { file: 'src/lib/ui/components/radio.tsx', exports: radio },
16
+ { file: 'src/lib/ui/components/slider.tsx', exports: slider },
17
+ { file: 'src/lib/ui/components/textbox.tsx', exports: textbox },
18
+ ];
9
19
 
10
20
  const isComponent = (name: string, value: unknown): value is UiComponent =>
11
21
  /^[A-Z][A-Za-z0-9]*$/.test(name) && typeof value === 'function';
@@ -13,21 +23,32 @@ const isComponent = (name: string, value: unknown): value is UiComponent =>
13
23
  /** Its name in Python: `Slider` gives `pie.ui.slider`, `MatrixProduct` gives `pie.ui.matrix_product`. */
14
24
  const pythonName = (exportName: string) => exportName.replace(/(?<!^)([A-Z])/g, '_$1').toLowerCase();
15
25
 
16
- const components = new Map<string, UiComponent>();
17
- const sourceFile = new Map<string, string>();
18
- for (const [file, mod] of [...Object.entries(builtinModules), ...Object.entries(customModules)]) {
19
- for (const [exportName, value] of Object.entries(mod)) {
20
- if (!isComponent(exportName, value)) continue;
21
- const name = pythonName(exportName);
22
- const other = sourceFile.get(name);
23
- if (other !== undefined) {
24
- throw new Error(`Duplicate UI 'pie.ui.${name}': ${file} and ${other}`);
26
+ let components = new Map<string, UiComponent>();
27
+
28
+ /**
29
+ * Replaces the custom UIs with the PascalCase exports of `modules`, e.g. a site's `ui/**\/*.{ts,tsx}`.
30
+ * Throws on a name used twice, leaving the UIs as they were.
31
+ */
32
+ export function setCustomUis(modules: CustomModule[]) {
33
+ const uis = new Map<string, UiComponent>();
34
+ const sourceFile = new Map<string, string>();
35
+ for (const { file, exports } of [...BUILTINS, ...modules]) {
36
+ for (const [exportName, value] of Object.entries(exports)) {
37
+ if (!isComponent(exportName, value)) continue;
38
+ const name = pythonName(exportName);
39
+ const other = sourceFile.get(name);
40
+ if (other !== undefined) {
41
+ throw new Error(`Duplicate UI 'pie.ui.${name}': ${file} and ${other}`);
42
+ }
43
+ uis.set(name, value);
44
+ sourceFile.set(name, file);
25
45
  }
26
- components.set(name, value);
27
- sourceFile.set(name, file);
28
46
  }
47
+ components = uis;
29
48
  }
30
49
 
50
+ setCustomUis([]);
51
+
31
52
  export function getUi(name: string): UiComponent | undefined {
32
53
  return components.get(name);
33
54
  }