@tinyvolt/pie 0.8.2 → 0.9.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.
@@ -52,7 +52,7 @@ canvas: left
52
52
 
53
53
  - `title`: the tab title. Empty means the tab shows the site title alone.
54
54
  - `description`: shown in search results and link previews. Empty means `site.yml`'s `description`.
55
- - `canvas`: `left`, `right`, `top`, `bottom` or `none` (default). Adds a canvas on that side of the page.
55
+ - `canvas`: `left`, `right`, `top`, `bottom` or `none` (default). Every page has a canvas, on that side of the page. `none`: it starts collapsed on the right, and the button on the divider opens it. Every `pie.*` call works either way. Python can show, hide or move it: see `pie.show_canvas` under "Side effects".
56
56
  - `sidebar`: a `.md` file shown in a collapsible sidebar on the left, e.g. `sidebar: _topics.md`. The path is relative to the page's file. `none`: no sidebar, even when `site.yml` sets one. Left out: `site.yml`'s `sidebar`.
57
57
  - It starts collapsed. A button opens it, and the rest of the page shrinks to make room. A new page starts with it collapsed.
58
58
  - On screens narrower than `--sidebar-drawer-below` (768px by default), it opens over the page instead. Tapping outside it or on one of its links closes it.
@@ -141,7 +141,7 @@ Arguments can be nodes or plain numbers. Passing an array where one item is expe
141
141
 
142
142
  ### Side effects
143
143
 
144
- These change existing nodes instead of making a new one. They run once, when called.
144
+ These change existing nodes, or the canvas, instead of making a new one. They run once, when called.
145
145
 
146
146
  | Call | Does |
147
147
  | --- | --- |
@@ -155,8 +155,11 @@ These change existing nodes instead of making a new one. They run once, when cal
155
155
  | `pie.hide(node)` | stops drawing `node` |
156
156
  | `pie.show(node)` | draws a hidden `node` again |
157
157
  | `pie.remove(node)` | removes `node`; nodes built from it are removed too |
158
+ | `pie.show_canvas(side)` | shows the canvas. `side` is optional: `left`, `right`, `top` or `bottom` also moves it there. Any other value raises an error |
159
+ | `pie.hide_canvas()` | collapses the canvas; its nodes stay |
158
160
 
159
161
  - `p`, `s` or `node` can be an array: every item changes.
162
+ - `pie.clear()` doesn't change whether the canvas is shown, or its side.
160
163
  - A moved node stops following the nodes it was built from. Nodes built from it follow the move. A moved point keeps its colors.
161
164
  - The four `translate_*` calls take `animate=True`: the value moves to its new value over half a second instead of jumping. The call returns when the move ends, so a loop of animated calls plays one after another. An array's items move together. Other calls raise an error if passed `animate`.
162
165
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinyvolt/pie",
3
- "version": "0.8.2",
3
+ "version": "0.9.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
6
  "homepage": "https://pie.tinyvolt.com/",
package/pages/about.md CHANGED
@@ -5,3 +5,7 @@ tags: #math
5
5
  ---
6
6
 
7
7
  # About
8
+
9
+ ```python-editor
10
+ # write your code here
11
+ ```
@@ -3,7 +3,8 @@ import { Chevron, type Direction } from './icons';
3
3
 
4
4
  /** The edge the first pane sits against. */
5
5
  export type SplitSide = 'left' | 'right' | 'top' | 'bottom';
6
- type Collapsed = 'none' | 'first' | 'second';
6
+ /** The collapsed pane, if any. */
7
+ export type Collapsed = 'none' | 'first' | 'second';
7
8
 
8
9
  // Limits on the first pane's share, in %, while dragging.
9
10
  const MIN_PERCENT = 20;
@@ -113,6 +114,8 @@ export interface SplitPaneProps {
113
114
  /** Names used on the collapse buttons, e.g. "Hide canvas". */
114
115
  firstLabel: string;
115
116
  secondLabel: string;
117
+ collapsed: Collapsed;
118
+ setCollapsed: (c: Collapsed) => void;
116
119
  class?: string;
117
120
  }
118
121
 
@@ -123,7 +126,6 @@ export interface SplitPaneProps {
123
126
  export function SplitPane(props: SplitPaneProps) {
124
127
  let ref: HTMLDivElement | undefined;
125
128
  const [firstPercent, setFirstPercent] = createSignal(50);
126
- const [collapsed, setCollapsed] = createSignal<Collapsed>('none');
127
129
 
128
130
  const startResize = (e: PointerEvent) => {
129
131
  e.preventDefault();
@@ -131,23 +133,23 @@ export function SplitPane(props: SplitPaneProps) {
131
133
  if (!ref) return;
132
134
  const pct = firstPercentAt(props.side, ref.getBoundingClientRect(), ev.clientX, ev.clientY);
133
135
  if (pct < SNAP_PERCENT) {
134
- setCollapsed('first');
136
+ props.setCollapsed('first');
135
137
  } else if (pct > 100 - SNAP_PERCENT) {
136
- setCollapsed('second');
138
+ props.setCollapsed('second');
137
139
  } else {
138
- setCollapsed('none');
140
+ props.setCollapsed('none');
139
141
  setFirstPercent(Math.min(MAX_PERCENT, Math.max(MIN_PERCENT, pct)));
140
142
  }
141
143
  });
142
144
  };
143
145
 
144
146
  const firstStyle = (): JSX.CSSProperties => {
145
- const c = collapsed();
147
+ const c = props.collapsed;
146
148
  if (c === 'first') return HIDDEN_PANE;
147
149
  if (c === 'second') return { flex: '1 1 0%' };
148
150
  return { flex: `0 0 ${firstPercent()}%` };
149
151
  };
150
- const secondStyle = (): JSX.CSSProperties => (collapsed() === 'second' ? HIDDEN_PANE : { flex: '1 1 0%' });
152
+ const secondStyle = (): JSX.CSSProperties => (props.collapsed === 'second' ? HIDDEN_PANE : { flex: '1 1 0%' });
151
153
 
152
154
  return (
153
155
  <div class={`split-pane split-pane--${props.side} ${props.class ?? ''}`} ref={ref}>
@@ -156,8 +158,8 @@ export function SplitPane(props: SplitPaneProps) {
156
158
  </div>
157
159
  <SplitDivider
158
160
  side={props.side}
159
- collapsed={collapsed()}
160
- setCollapsed={setCollapsed}
161
+ collapsed={props.collapsed}
162
+ setCollapsed={props.setCollapsed}
161
163
  firstLabel={props.firstLabel}
162
164
  secondLabel={props.secondLabel}
163
165
  onPointerDown={startResize}
@@ -5,14 +5,19 @@ import { UiLayer } from '~/lib/ui/UiLayer';
5
5
  import type { CanvasSide } from '../frontmatter';
6
6
  import { MarkdownBody } from './MarkdownBody';
7
7
 
8
- /** A canvas on `side` of the page's text. `python-editor` blocks in the text draw on it. */
9
- export function CanvasPage(props: { side: Exclude<CanvasSide, 'none'>; html: string }) {
10
- const store = new NodeStore();
8
+ /**
9
+ * A canvas on `side` of the page's text. `python-editor` blocks in the text draw on it.
10
+ * `none`: the canvas starts collapsed, on the right.
11
+ */
12
+ export function CanvasPage(props: { side: CanvasSide; html: string }) {
13
+ const store = props.side === 'none' ? new NodeStore('right', 'first') : new NodeStore(props.side, 'none');
11
14
 
12
15
  return (
13
16
  <SplitPane
14
17
  class="canvas-page"
15
- side={props.side}
18
+ side={store.canvasSide()}
19
+ collapsed={store.collapsedPane()}
20
+ setCollapsed={store.setCollapsedPane}
16
21
  first={
17
22
  <div class="canvas-page-canvas">
18
23
  <NodeCanvas store={store} />
@@ -4,6 +4,7 @@ import { AnyNode, PointNode, ScalarNode, TextNode, checkColor, getNodeTypes } fr
4
4
  import { NodeStore } from '../state/NodeStore';
5
5
  import { Dtype, add, num, ref, scalar } from '../jax';
6
6
  import type { Track } from '../animation';
7
+ import type { SplitSide } from '~/components/SplitPane';
7
8
 
8
9
  /**
9
10
  * Saves `node` with `changes`, new values for some of its numbers, in place of `node`.
@@ -114,6 +115,39 @@ export const Clear: SideEffectFunc = {
114
115
  },
115
116
  };
116
117
 
118
+ const CANVAS_SIDES: SplitSide[] = ['left', 'right', 'top', 'bottom'];
119
+
120
+ /**
121
+ * Show the canvas, and move it to `side` when given.
122
+ * @param side - 'left', 'right', 'top' or 'bottom' (default: where it is now)
123
+ */
124
+ export const ShowCanvas: SideEffectFunc = {
125
+ keyword: 'show_canvas',
126
+ parameters: [{ name: 'side', type: 'Text', defaultValue: null }],
127
+ broadcastable: false,
128
+ run(inputs, store) {
129
+ const side = (inputs[0] as TextNode | undefined)?.value;
130
+ if (side !== undefined) {
131
+ if (!CANVAS_SIDES.includes(side as SplitSide)) {
132
+ throw new Error(`show_canvas: side must be one of ${CANVAS_SIDES.join(', ')}, got '${side}'`);
133
+ }
134
+ store.setCanvasSide(side as SplitSide);
135
+ }
136
+ if (store.collapsedPane() === 'first') store.setCollapsedPane('none');
137
+ },
138
+ };
139
+
140
+ /**
141
+ * Hide the canvas. Its nodes stay.
142
+ */
143
+ export const HideCanvas: SideEffectFunc = {
144
+ keyword: 'hide_canvas',
145
+ parameters: [],
146
+ run(_inputs, store) {
147
+ store.setCollapsedPane('first');
148
+ },
149
+ };
150
+
117
151
  /**
118
152
  * Move a point to (x, y). With `animate=True` it moves there over half a second.
119
153
  * @param point - Point to move
@@ -3,6 +3,7 @@ import { createStore, reconcile } from 'solid-js/store';
3
3
  import { AnyNode, ScalarNode, TextNode } from './Node';
4
4
  import { NameGenerator } from './NameGenerator';
5
5
  import { num, scalar } from '../jax';
6
+ import type { Collapsed, SplitSide } from '~/components/SplitPane';
6
7
 
7
8
  /** Below this many pixels per unit, the grid hides its lines and keeps only the axes. */
8
9
  export const MIN_GRID_UNIT = 20;
@@ -20,6 +21,13 @@ export class NodeStore {
20
21
  public canvasBounds: Accessor<{ width: number; height: number }>;
21
22
  public setCanvasBounds: (bounds: { width: number; height: number }) => void;
22
23
 
24
+ /** The side of the page the canvas sits on. */
25
+ public canvasSide: Accessor<SplitSide>;
26
+ public setCanvasSide: (side: SplitSide) => void;
27
+ /** The page's collapsed pane: `first` is the canvas, `second` the text. */
28
+ public collapsedPane: Accessor<Collapsed>;
29
+ public setCollapsedPane: (c: Collapsed) => void;
30
+
23
31
  /** Pixels per grid unit, read from the `unit` node. */
24
32
  public unit!: Accessor<number>;
25
33
  /** Opacity multiplier for the grid lines and axes, read from the `gridOpacity` node. */
@@ -31,7 +39,7 @@ export class NodeStore {
31
39
 
32
40
  public nameGenerator: NameGenerator;
33
41
 
34
- constructor() {
42
+ constructor(canvasSide: SplitSide, collapsedPane: Collapsed) {
35
43
  const [nodesState, setNodesState] = createStore<Record<string, NodeSignal>>({});
36
44
  this.nodes = nodesState;
37
45
  this.setNodes = (id, value) => { setNodesState(id, value as any); };
@@ -41,6 +49,9 @@ export class NodeStore {
41
49
  this.canvasBounds = canvasBounds;
42
50
  this.setCanvasBounds = setCanvasBounds;
43
51
 
52
+ [this.canvasSide, this.setCanvasSide] = createSignal(canvasSide);
53
+ [this.collapsedPane, this.setCollapsedPane] = createSignal(collapsedPane);
54
+
44
55
  createRoot(() => {
45
56
  this.unit = createMemo(() => {
46
57
  const node = this.nodes['unit']?.signal() as ScalarNode | undefined;
@@ -5,7 +5,6 @@ import 'katex/dist/katex.min.css';
5
5
  import { PageMeta } from '~/components/PageMeta';
6
6
  import { PageTitle } from '~/components/PageTitle';
7
7
  import { CanvasPage } from '~/lib/markdown/components/CanvasPage';
8
- import { MarkdownBody } from '~/lib/markdown/components/MarkdownBody';
9
8
  import { wrapPage } from '~/lib/markdown/features/views';
10
9
  import { loadPage } from '~/lib/markdown/pages';
11
10
 
@@ -26,8 +25,7 @@ export default function Page() {
26
25
  </Match>
27
26
  <Match when={page()} keyed>
28
27
  {(page) => {
29
- const canvas = page.frontmatter.canvas;
30
- const body = canvas === 'none' ? <MarkdownBody html={page.html} /> : <CanvasPage side={canvas} html={page.html} />;
28
+ const body = <CanvasPage side={page.frontmatter.canvas} html={page.html} />;
31
29
  return (
32
30
  <>
33
31
  <PageMeta title={page.frontmatter.title} description={page.frontmatter.description} />