@pencil-lang/figure 0.0.5 → 0.0.6

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 CHANGED
@@ -8,6 +8,7 @@ compiles is only read by the WASM built from the same source.
8
8
  | `@pencil-lang/figure` | Defines `<pencil-figure>`, and exports `PencilFigure`. |
9
9
  | `@pencil-lang/figure/host` | The WASM as a page calls it: `ready()`, `Player`, `gather`, `uses`, `inspect`, `from_latex` and the rest. |
10
10
  | `@pencil-lang/figure/pack` | `pack` and `unpack`: a figure's sources as a string for a link's `#pen=`, which the playground opens. |
11
+ | `@pencil-lang/figure/controls` | What writes the default controls (`markup`, `declarative`, `CONTROLS_CSS`), and `readoutHtml`, which sets a readout in a page's own box. |
11
12
  | `@pencil-lang/figure/astro` | The Astro integration (the default export), `build()` and `alternates()`. |
12
13
  | `@pencil-lang/figure/Figure.astro` | The Astro component. |
13
14
 
@@ -53,6 +54,101 @@ a handle's reach drags the handle and doesn't scroll the page. One anywhere else
53
54
  page, as it would over an image, so a figure never traps a reader's scroll. A drag never selects text, in the figure or
54
55
  on the page around it, and a long press shows no callout.
55
56
 
57
+ ## Controls
58
+
59
+ ```html
60
+ <pencil-figure controls src="tube.pen" sheets="theme.style tube.style"></pencil-figure>
61
+ ```
62
+
63
+ `controls` asks for the default controls, as `<video controls>` does: a play button and time when anything follows time,
64
+ beat stepping with each beat's caption, a control for each param a handle doesn't set, and the readouts, each label the
65
+ document writes without `at`. Each param's control says its caption, `param q "arms" = 8 in …`, or its name, and is the
66
+ kind the figure and its sheets give it: a slider with its value after it for a range, radios for a choice of four options
67
+ or fewer, a `<select>` for more, and a checkbox for a choice of off and on (`docs/language.md`, Controls and readouts).
68
+
69
+ - **They're keyboard controls,** native ones: Tab reaches each, the arrows move a slider and pick within a group of radios,
70
+ Space and Enter press the play button, and each has a focus ring.
71
+ - **A screen reader gets each control's caption as its name** and its value as its text (`aria-valuetext` on a slider), the
72
+ radios as a group named for the caption, and each readout's words, with its mathematics in words ("z squared = 1").
73
+ - **They're `<pencil-controls slot="controls">`,** a child of the figure whose shadow root holds them. The element writes
74
+ one when the figure loads, if the page has none. The Astro component writes it at build, with `controls`, as a
75
+ declarative shadow root, disabled until the figure loads and with every size reserved, so nothing moves. One whose
76
+ declarative shadow root wasn't read, as in HTML a script put in the page, gets the controls written when the figure
77
+ loads. Without `controls` the defaults are hidden, and whatever the page put in `<pencil-controls>` stays.
78
+ - **Nothing in them moves** when the figure loads, as a value changes or as time plays. An output is as wide as the widest
79
+ value its param's range shows, every beat's caption lies in one place, the play button is as wide as the wider of its
80
+ words, and a readout's box is as big as the most it says over the moments a reader can reach, from the one the figure
81
+ starts at, which the core tries (`readout_room`). Each sits in a contained box. Every slider is in one grid, so each
82
+ starts and ends where the others do, and a long caption wraps rather than squeezing its slider.
83
+ - **On a phone,** or a screen with little height, one row stays, on one line, in a bar of fixed height, and the rest open
84
+ from a `⋯` button as a menu over the page, from the bottom. The figure keeps the height. Which row is `BAR` in
85
+ [`src/controls.js`](src/controls.js): `'first'`, the first row, or `'readouts'`. A row that doesn't fit the bar's line,
86
+ as radios with long words, goes behind the menu too, and the button says "Controls", so nothing in the bar is cut off.
87
+ A row that comes to fit again, as when a phone turns, comes back to the bar. The beats' caption is a line of its own
88
+ under the bar, wherever the stepper is, as tall as the longest caption at that width, so stepping moves nothing.
89
+ Escape closes the menu and gives the focus back to `⋯`, and controls written again, as for another look, keep an
90
+ open menu open and the focus where it was.
91
+ - **A new beat is said,** in a polite live region: which beat it is and its caption, `2 / 3: Then as many…`, whether
92
+ a reader stepped to it or time brought it.
93
+ - **Anything a page puts in `<pencil-controls>`,** as a choice of looks, is shown after the defaults, and on a phone in
94
+ their menu. With the Astro component it goes in the `controls` slot: `<Figure …><label slot="controls">…</label></Figure>`.
95
+
96
+ **Styling.** The defaults are styled through `::part`, as `pencil-controls::part(slider)`, and these custom properties,
97
+ set on the figure or anything around it:
98
+
99
+ | Custom property | What it is |
100
+ |---|---|
101
+ | `--pencil-controls-font` | Their text, as the `font` shorthand. |
102
+ | `--pencil-ink` | Values, a param's name and readouts. |
103
+ | `--pencil-muted` | What a control says it is, and captions. |
104
+ | `--pencil-accent` | Sliders, radios and checkboxes. |
105
+ | `--pencil-rule`, `--pencil-surface` | A button's border and background. |
106
+ | `--pencil-surface-menu` | The menu's background on a phone. |
107
+ | `--pencil-math-font` | A param's name, as the `font` shorthand. |
108
+ | `--pencil-focus` | The focus ring, as on the figure's handles. |
109
+ | `--pencil-readout-size` | A readout's text size. |
110
+
111
+ | Part | What it is |
112
+ |---|---|
113
+ | `controls`, `bar`, `rest` | All of them, the first row, and the rest, which on a phone is the menu. |
114
+ | `row` | Each row, with `time`, `beats`, `param`, `choice` or `readouts` as well. |
115
+ | `label` | What a control says it is: a `<label>`, or a group's `<legend>`. |
116
+ | `slider`, `select`, `radio`, `checkbox`, `option` | Each control, and a radio's label. |
117
+ | `value` | A slider's value, the time, or the beat (with `time` or `beat` as well). |
118
+ | `button` | Each button, with `play`, `step` or `more` as well. |
119
+ | `captions`, `caption` | The beats' captions, and each. |
120
+ | `readout` | Each readout's box. |
121
+
122
+ The canvas and the poster are the figure's `stage` part, so a page's background or focus ring for the figure goes there,
123
+ `pencil-figure::part(stage)`, and not on the controls.
124
+
125
+ **A page's own controls.** A page that wants its own puts them in the `controls` slot, marked with what each is for, and
126
+ the element sets each one's range, step and value from the figure, follows them every frame, and holds a param when a
127
+ reader sets one, with or without the `controls` attribute:
128
+
129
+ ```html
130
+ <pencil-figure src="tube.pen" sheets="theme.style tube.style">
131
+ <div slot="controls">
132
+ <label>Arms <input type="range" data-param="p"></label> <output data-param="p"></output>
133
+ <span data-readout="ratio" style="display: inline-block; width: 8em; height: 1.4em; contain: strict"></span>
134
+ </div>
135
+ </pencil-figure>
136
+ ```
137
+
138
+ | Marked | What the element does |
139
+ |---|---|
140
+ | `input[type=range][data-param="q"]` | Sets its `min`, `max`, `step` and value from `q`, and its `aria-valuetext`. Moving it pauses and holds `q`. |
141
+ | `select[data-param="along"]` | Fills in the choice's options if it has none, and holds which one is picked. A choice is named by its value, `along`, or its param, `along.index`. |
142
+ | `input[type=radio][data-param="along"][value="1"]` | Checks the one whose value is the option's place, from 0. |
143
+ | `input[type=checkbox][data-param="glow"]` | Checked for the second option, on. |
144
+ | `output[data-param="q"]` | Says `q`'s value, as a control does. |
145
+ | `[data-readout="ratio"]` | Says what the readout `ratio` says, laid out as the figure lays it out, with its words for a screen reader (`readoutHtml`). A page gives it its size. |
146
+ | `button[data-play]`, `input[type=range][data-time]`, `output[data-time]` | Play and pause, and the time. |
147
+ | `button[data-step="1"]`, `button[data-step="-1"]`, `output[data-beat]`, `[data-captions]` | Beats: next, previous, which one, and its caption, the child of `[data-captions]` in its place shown with `data-on`. |
148
+
149
+ The figure's `readouts` is what its readouts say this frame, each `{ id, text, words, math, width, height, laid }`, in
150
+ ems, for a page that sets them itself.
151
+
56
152
  Labels are set in the package's own fonts, in [`fonts/`](fonts/README.md): Libertinus, under the SIL Open Font License.
57
153
  They're the files the `pencil` command draws a poster with, so the figure that takes over from a poster sets the same
58
154
  glyphs in the same places. The element fetches the two text faces when it loads, and the math font the first time a
@@ -118,13 +214,17 @@ import Figure from '@pencil-lang/figure/Figure.astro';
118
214
  is built again, with a `pencil` from the same source as the package. A build names the folders nothing asked for,
119
215
  and leaves them: one that skips drafts asks for fewer. A figure is built elsewhere and put in the folder only once
120
216
  it has built, so a build that fails, or has no `pencil`, never removes what's kept.
217
+ - **`controls` writes the default controls,** at the poster's moment and disabled, with every size reserved, so nothing
218
+ moves when the figure loads (see Controls). What a page puts in the component's `controls` slot comes after them.
121
219
  - **`alt` is required.** It's the poster's text and the figure's name.
122
220
  - Anything else, such as `id`, `class`, `params`, `paused` or `data-*`, goes on `<pencil-figure>`.
123
221
 
124
222
  `build({ src, sheets, time, params, size, card })` is what the component calls. A page can call it too, for a
125
- figure's `info` (its size, params with their ranges, beats and captions, and the colors its sheets name) and its
126
- `moment` (the time, and every param's value and range, where the poster shows it), to write its controls in their
127
- first state before any script runs, or with `card: true` for a 1200×630 image for sharing.
223
+ figure's `info` (its size, params with their ranges, captions and controls, beats and captions, readouts with the room
224
+ each needs, and the colors its sheets name) and its `moment` (the time, every param's value and range, and what each
225
+ readout says, where the poster shows it), to write controls of its own in their first state before any script runs, or
226
+ with `card: true` for a 1200×630 image for sharing. `declarative({ info, moment, paused, disabled })` from
227
+ `@pencil-lang/figure/controls` is the defaults' shadow root, as the component writes it.
128
228
 
129
229
  `alternates({ sheets, dark, narrow, narrowMedia })` gives the sets of sheets the component builds a figure under: its
130
230
  own as `sheets`, and as `others` each `{ media, sheets }` it writes a `<source>` for. A page that swaps one figure
@@ -157,7 +257,8 @@ pnpm test
157
257
 
158
258
  Playwright's, in [`tests/`](tests), against the corpus in this repo, headless, on its own Chromium and WebKit: each at
159
259
  a desktop size and as a phone (a Pixel 7 and an iPhone 15, emulated). They cover drawing, every key on a handle and on
160
- beats, a mouse drag and a touch drag, a swipe that scrolls past a figure, a drag that selects no text, a range that follows another param, reduced motion, sheets picked by theme, the
260
+ beats, the default controls and a page's own, readouts, and that no control moves as the figure loads, plays or is
261
+ dragged, a mouse drag and a touch drag, a swipe that scrolls past a figure, a drag that selects no text, a range that follows another param, reduced motion, sheets picked by theme, the
161
262
  poster's swap, and a compiled figure. `PENCIL_GL=1 pnpm test` runs them all on WebGL2 in place of WebGPU, and `PENCIL_MIN=1 pnpm test` on the minified bundle. They serve the repo on
162
263
  port 8743, and reuse a server already there, so a second checkout runs them on its own with `PENCIL_PORT=8751 pnpm test`.
163
264
  [`playwright.config.js`](playwright.config.js) says which GPU path each browser has headless. An emulated phone is a
@@ -21,9 +21,15 @@
21
21
  // WASM, so a line of script in the page starts on them as it's read, behind the poster.
22
22
  // A page that pins a theme over the reader's system says so with `color-scheme` in its CSS, and
23
23
  // a figure with `dark` sheets follows it (`src/scheme.js`), from the first paint.
24
+ // `controls` writes the default controls into the page, from what the build says of the figure, as
25
+ // it will stand when it starts and disabled, with every size reserved, so they have their place
26
+ // before any script runs and nothing moves when the figure loads. They're `<pencil-controls>`, with
27
+ // a declarative shadow root, and the element turns them on. Anything the page puts in the
28
+ // component's `controls` slot is shown after them, and on a phone in their menu.
24
29
  // Anything else (`id`, `class`, `params`, `paused`, `gl`, `data-*`) goes on `<pencil-figure>`.
25
30
  import { alternates, build } from './build.js';
26
31
  import { settle } from '../src/scheme.js';
32
+ import { declarative, hasControls, shape as controlsShape } from '../src/controls.js';
27
33
  import wasm from '../pkg/pencil_web_bg.wasm?url';
28
34
 
29
35
  interface Props {
@@ -37,10 +43,13 @@ interface Props {
37
43
  poster?: { time?: number; params?: Record<string, number> };
38
44
  /** The figure is in view when the page opens. */
39
45
  priority?: boolean;
46
+ /** The default controls, written at build: play and time, beats, a control for each param a
47
+ * handle doesn't set, and the readouts. */
48
+ controls?: boolean;
40
49
  [attribute: string]: unknown;
41
50
  }
42
51
 
43
- const { src, sheets, dark, narrow, narrowMedia, alt, poster = {}, priority = false, ...attributes } = Astro.props;
52
+ const { src, sheets, dark, narrow, narrowMedia, alt, poster = {}, priority = false, controls = false, ...attributes } = Astro.props;
44
53
  // As in `<picture>`, the first choice that matches wins, so the narrowest case comes first.
45
54
  const { sheets: always, others: choices } = alternates({ sheets, dark, narrow, narrowMedia });
46
55
 
@@ -79,12 +88,22 @@ const start = (themed || early)
79
88
  ? `((f)=>{${themed ? `(${String(settle).replace(/\n\s+/g, '\n')})(f);` : ''}${early ? fetchAhead : ''}})(document.currentScript.parentElement)`
80
89
  : undefined;
81
90
  const mark = reshaped.length ? main.hash : undefined;
91
+ // The controls as the figure starts: at the poster's moment, paused if the page starts it paused,
92
+ // and disabled until it loads. A figure that doesn't compile, in `astro dev`, gets its controls
93
+ // from the element once it does.
94
+ // What the page put in the `controls` slot, which can be nothing: a slot written with a condition is
95
+ // there whether or not the condition holds.
96
+ const own = Astro.slots.has('controls') ? (await Astro.slots.render('controls')).trim() : '';
97
+ const extra = own !== '';
98
+ const written = controls && main.info && (hasControls(main.info) || extra)
99
+ ? declarative({ info: main.info, moment: main.moment, paused: Boolean(attributes.paused), disabled: true, extra })
100
+ : null;
82
101
  const rules = reshaped
83
102
  .map((each) => `@media ${each.media}{[data-pencil-poster="${mark}"] img{--pencil-shape:${each.shape}}}`)
84
103
  .join('');
85
104
  ---
86
105
  {mark && <style is:inline set:html={rules}></style>}
87
- <pencil-figure {...attributes} {...from(main)} aria-label={alt} data-pencil-poster={mark}>
106
+ <pencil-figure {...attributes} {...from(main)} aria-label={alt} data-pencil-poster={mark} controls={controls || undefined}>
88
107
  {others.map((built, i) => <source media={choices[i].media} {...from(built)} />)}
89
108
  {main.poster && (
90
109
  <picture style={shape(main) && `--pencil-shape: ${shape(main)}`}>
@@ -109,6 +128,12 @@ const rules = reshaped
109
128
  </picture>
110
129
  )}
111
130
  {start && <script is:inline set:html={start}></script>}
131
+ {(written || extra) && (
132
+ <pencil-controls slot="controls" data-shape={written ? controlsShape(main.info) : undefined}>
133
+ {written && <Fragment set:html={written} />}
134
+ {extra && <Fragment set:html={own} />}
135
+ </pencil-controls>
136
+ )}
112
137
  </pencil-figure>
113
138
 
114
139
  <script>
package/astro/build.d.ts CHANGED
@@ -37,6 +37,20 @@ export declare function alternates(options: {
37
37
  narrowMedia?: string;
38
38
  }): { sheets: string[]; others: Choice[] };
39
39
 
40
+ /** What a readout says, as the element's `readouts` give it and `readoutHtml` sets it. */
41
+ export interface Readout {
42
+ id: string;
43
+ text: string;
44
+ words: string;
45
+ math: boolean;
46
+ width: number;
47
+ height: number;
48
+ laid: {
49
+ runs: { text: string; x: number; top: number; size: number; math: boolean; stretch: number }[];
50
+ rules: { x: number; y: number; width: number; height: number }[];
51
+ } | null;
52
+ }
53
+
40
54
  /** What a figure says of itself, as the element's `info` does. */
41
55
  export interface FigureInfo {
42
56
  /** Its design size, `[width, height]`. */
@@ -57,7 +71,15 @@ export interface FigureInfo {
57
71
  /** The other params its binding reads: a reader setting one of them hands it back to its binding. */
58
72
  reads: string[];
59
73
  choice: { of: string; options: string[] } | null;
74
+ /** What its control says, as the document writes it: `param q "arms" = …`. */
75
+ caption: string | null;
76
+ /** Its control, as the sheets say: its name, which `style q.control` reaches, its kind, and
77
+ * which parts of `caption`, `name` and `value` it says. */
78
+ control: { name: string; kind: 'slider' | 'radios' | 'select' | 'checkbox'; text: ('caption' | 'name' | 'expr' | 'value')[] };
60
79
  }[];
80
+ /** Each label with no place, said beside the figure, in the order it's written, with the room it
81
+ * needs, `[width, height]` in ems, so nothing beside it moves as it changes. */
82
+ readouts: { id: string; room?: [number, number] }[];
61
83
  /** Each mark a reader can move, in every panel: its panel, its mark, and the params it sets, one or
62
84
  * two (a point dragged in two dimensions). */
63
85
  handles: { panel: number; mark: string; params: string[] }[];
@@ -77,7 +99,13 @@ export interface Built {
77
99
  card: { src: string; width: number; height: number } | null;
78
100
  info: FigureInfo | null;
79
101
  /** The figure as its poster shows it: the time, and every param's value and range by name. */
80
- moment: { time: number; values: Record<string, number>; ranges: Record<string, [number, number]> } | null;
102
+ moment: {
103
+ time: number;
104
+ values: Record<string, number>;
105
+ ranges: Record<string, [number, number]>;
106
+ /** What each readout says there, laid out in ems, as the element's `readouts` are. */
107
+ readouts: Readout[];
108
+ } | null;
81
109
  /** A link that opens the figure's sources in a playground, when the integration has one. */
82
110
  playground: string | null;
83
111
  hash: string | null;
package/astro/build.js CHANGED
@@ -193,8 +193,9 @@ export async function replace(dir, built) {
193
193
  *
194
194
  * Gives `{ compiled, poster: { src, srcset, webp, width, height }, card, info, moment, hash }`,
195
195
  * with URLs from the site's root. `moment` is the figure as its poster shows it, which is how a
196
- * page that holds the same params starts it: `{ time, values, ranges }`, the last two for every
197
- * param by name, so a page can write its controls as they'll be before the figure runs.
196
+ * page that holds the same params starts it: `{ time, values, ranges, readouts }`, the middle two for
197
+ * every param by name, and what each readout says, so a page can write its controls as they'll be
198
+ * before the figure runs.
198
199
  * `playground` is a link that opens the figure's sources in a playground, when the integration
199
200
  * was told where one is: the page ships none of them, so the link carries them all. `webp` is a `srcset` of the same poster as WebP, where that's the smaller,
200
201
  * and in `astro dev` also `src` and `sheets`, the sources' URLs, which the
@@ -295,7 +296,12 @@ async function make(cfg, key, { src, sheets, time, params, size, card }) {
295
296
  },
296
297
  card: manifest.card ? { src: url(manifest.card.src), width: manifest.card.width, height: manifest.card.height } : null,
297
298
  info: manifest.info,
298
- moment: { time: manifest.poster.time, values: manifest.poster.values, ranges: manifest.poster.ranges },
299
+ moment: {
300
+ time: manifest.poster.time,
301
+ values: manifest.poster.values,
302
+ ranges: manifest.poster.ranges,
303
+ readouts: manifest.poster.readouts ?? [],
304
+ },
299
305
  playground: await playgroundLink(cfg, stem, src, sheets, manifest.sources),
300
306
  hash: manifest.hash,
301
307
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pencil-lang/figure",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "Pencil figures on a page: the <pencil-figure> element, the WASM host it runs on, and an Astro component that builds a figure and its poster.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -22,6 +22,7 @@
22
22
  ".": "./src/element.js",
23
23
  "./host": "./src/host.js",
24
24
  "./pack": "./src/pack.js",
25
+ "./controls": "./src/controls.js",
25
26
  "./astro": {
26
27
  "types": "./astro/index.d.ts",
27
28
  "default": "./astro/index.js"