@altnovatrail/canvas 0.2.3 → 0.3.1

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
@@ -17,11 +17,11 @@
17
17
 
18
18
  The NovaTrail canvases. A canvas is the workplace of a task, where the learner writes, draws and types; this UI package holds the canvas kinds — for now two, the handwriting sheet and the workbook — with their layers and tools, captures the learner's strokes with time and pressure, draws the picture of the work that goes to be checked and shows the tutor's marks tied to the learner's strokes, and the app builds it in at an exact version. A separate build of it is the samples page, where adult volunteers make samples for eval on the same canvas that learners use: a sample is saved as a file on the volunteer's device and reaches a module's eval set by pull request, and nothing goes to the platform's server.
19
19
 
20
- A component of [NovaTrail](https://github.com/altnovatrail/spec/tree/v0.9.9), a universal open platform for adaptive learning, interactive lessons and AI tutoring. It belongs to stage 2, First slice with learners, with tutor, api, app, skill-map and infra; with tutor it is the stage's first code: first the handwriting sheet, while the samples page waits until a measurement needs volunteers' or children's samples.
20
+ A component of [NovaTrail](https://github.com/altnovatrail/spec/tree/v0.12.0), a universal open platform for adaptive learning, interactive lessons and AI tutoring. It belongs to stage 2, First slice with learners, with tutor, api, app, skill-map and infra; with tutor it is the stage's first code: first the handwriting sheet, while the samples page waits until a measurement needs volunteers' or children's samples.
21
21
 
22
22
  ## Status
23
23
 
24
- Code started. The detailing is done — epic E1, archived in [docs/plan/E1.md](docs/plan/E1.md). Epic E2, replanned for the MVP on 2026-10-22, built the handwriting sheet as the element `<novatrail-canvas>`, for tutor's check board to take in place of its own sheet; the board has decided to keep its own. Its last task was the author's try of the sheet by hand on the demo page, which found nothing to fix. E2 is done and archived in [docs/plan/E2.md](docs/plan/E2.md). The text canvas, epic E3, was dropped on 2026-10-25: canvas builds no text canvas. Epic E4, taken the same day, built the sheet for the product: the eraser and the history of writing, the layers and the grid, the groups of strokes and the crop for checking, the tutor's marks tied to the strokes, the package's full interface and the input on tablets. It was done on 2026-10-26 and is archived in [docs/plan/E4.md](docs/plan/E4.md). Epic E6, taken the same day, detailed and built the workbook, the first slice's second canvas kind, for school maths in grade 3, and was done on 2026-10-27; it is archived in [docs/plan/E6.md](docs/plan/E6.md). Epic E7, taken the same day, took what spec v0.2.42 brought: the task's new open fields, the exit condition that every answer slot holds something — `filled` on a sheet too — and the lesson's worked example, shown on a sheet the learner does not write on. On the way canvas moved to spec v0.4.1, whose npm package gives the types of the module format. E7 was done on 2026-11-01 and is archived in [docs/plan/E7.md](docs/plan/E7.md). Epic E8, taken the same day, is the first release of the package, which spec's cross-repository plan asks for before the app's code; the tests' coverage and the release's build come first. Then canvas moved to spec v0.6.2, of spec's 0.6 line, and its CI now checks the signature of each package of `@altnovatrail` in its lock file, as spec's rules ask; then the element took the module format 0.2 and the types of its contract with the app from spec's npm package, and the package came out in its first release, v0.1.0; from the next one on, the release puts it in npm itself. E8 was done on 2026-11-02 and is archived in [docs/plan/E8.md](docs/plan/E8.md). The same day canvas moved to spec v0.6.5, which made the `media` property part of the element's contract with the app and gave the buttons of every NovaTrail interface package one set of icons, and epic E5, the samples page, was taken: first trying a build opened from a file on the devices and building the page. On 2026-11-08 canvas moved to spec v0.9.8, whose eval protocol now holds the sample file, its markup and the rules the sheet shares with tutor's eval runner — the groups of strokes, the crop, the snapping of a model's box and a workbook's boxes: the tests hold the sheet's code to spec's test vectors of those rules, and the types of the tutor's marks are spec's too. The same day it moved on to v0.9.9, which answers canvas's last two open questions and changes nothing in its code. The synthetic samples that tutor's CI reads come first now, then the page. So far there is the package, `@altnovatrail/canvas`, with its tools and the sheet's input: the element takes a pen, a finger or a mouse, one pointer at a time, and draws the strokes, and its methods `undo()` and `clear()` undo the last action and clear the sheet. Its eraser, E4's first task, takes strokes off whole along the path of a press: the element's `tool` switches to it, and a pen's eraser erases whatever the tool, if the task allows the eraser. The sheet keeps the history of writing — every stroke, erasing, clearing and undo, at the sheet's time — for the app to keep. The sheet is a stack of layers, in the order of spec's canvas layers: its task, the element's `task` property, rules it in squares, lines or dots, and the task's statement stands above the sheet, its pictures from the consumer's `media`. The task and the module's manifest come in the types of spec's npm package, `@altnovatrail/spec`; the element reads the module format 0.3, which spec v0.9.0 brought and in which the modules' first release came out — a manifest of another version, 0.2 too, is a TypeError — and the task is checked as its schema checks it, a field it does not know a TypeError; what the statement gives and asks, the fields `givens` and `unknown`, the element takes and leaves for the app to show; a workbook's task the element shows too: its lines laid out on a page the size of the sheet — numbers, text fitted to the layout's own widths, and boxes, up to ten rows — which a screen reader reads as a list. On a workbook each stroke falls in one box or in none, by a rule in integers on the middle of its points' box; what falls in no box is draft, which stays on the page and goes to no check, and the property `filled` tells which boxes hold writing, and on a sheet whether its one answer anywhere on it, or the whole sheet, does. The module's manifest, the `manifest` property, gives the height of a digit on the crop, 24 px unless it gives another, as without a manifest, and the language of the statement. The sheet is at most 1000 CSS pixels wide, and the consumer themes it by custom properties. The DOM-free part parts the strokes into groups by place and time — two strokes no more than 2 seconds apart whose boxes come within a cell of each other are neighbours, and a chain of neighbours is a group — labels the groups A, B, C… by place and gives each one's rectangle on the sheet, for the crop, the tutor's marks and the samples to come; the rule's test vectors are a proposal for the eval protocol. It also draws the crop for checking, with its own raster and PNG encoder: the ink of the visible strokes, scaled so that the writing comes out at the digit height the module's manifest asks for, within the size every Claude model takes as it is, with the groups' frames in its fractions. Its test vectors go to the eval protocol too. The element takes the tutor's marks of a submission back by its property `marks`, with the context it is to give with the crop: a mark holds on to the learner's strokes — a group's, or those a model's box on the crop takes, two thirds of their points within a quarter of a digit of it — and a frame stands round those of them the sheet shows, over the work, read by its name and fading while the learner writes into it. The history goes out by the property `history` and the event `historychange`, which tells of every action with the events it adds, and comes back by the same property: the same history changes nothing, and another, one the sheet could have written, replaces the sheet, undo and all. The pictures' files come from the consumer's `media`, a function that gives a file's Blob by its reference, the contract's own. `exportWork()` gives the work for checking: the PNG of the crop and the context of its submission, which the app keeps and gives back with the tutor's marks, its digest telling the sheet's own context from another sheet's. On a workbook it gives the crop of each filled box, with its item and slot, and the context of the table of boxes; a tutor's mark there stands on an answer cell by its place and frames the box with its ink. Above the statement and the sheet, outside their scrolling, stands the element's own row of buttons — the pen and the eraser the task allows, undo and clear — a toolbar worked by pointer and by keyboard, labelled in English or Russian by the element's own `lang`, each label after an icon of Material Symbols, Google's set, which the build embeds, so the element asks no network for one and the package's NOTICE names it; a `toolchange` event tells of the learner's choice of tool. On a tablet a pen's touch puts the whole page in pen mode, where a finger writes nothing until the row's Draw with finger lets it write again; a touch wider or taller than 100 CSS pixels is a palm and writes nothing, and two fingers scroll the sheet. The row's Up and Down scroll it the same way with a single pointer, on any device, by four fifths of what is in sight. A lesson's worked example comes by the property `workedExample`, in place of a task: its statement stands above the sheet and its grid on it, but the learner does not write there — a finger or a pen scrolls the sheet as the browser scrolls the statement, and the row, named Worked example, has Next, Demo, Up and Down. Next opens the parts of the author's demo one at a time, into a numbered list above the sheet that a polite region tells a screen reader of, and Demo hides the whole demo and shows it again; the property `demoState` and the event `demochange` tell the app how far the learner has gone, and the app may give it back to a new element after the example — in React's JSX `workedExample` comes before `demoState`, and `ondemochange` listens. The pictures of the demo come from the consumer's `media` with the statement's, one file for each reference, all of them as the example comes, so that a part opened shows its picture at once; a picture on an area of the sheet stands on the sheet itself, stretched over its area, and the sheet scrolls to it as its part opens. The element now waits for the app and the samples page; a test plays the app's part, and `just demo` serves a page to try the element on by hand. A release comes out by itself after CI passes on `main`, when a path it is built from has changed: the package's archive, with its NOTICE, LICENSE and their sums, built, signed and checked by .github's shared release, and the same archive goes to npm: the first version by hand, the later ones by the release's own npm step, through trusted publishing, with no long-lived token. A pull request from a branch of this repository tries the release and publishes nothing.
24
+ Code started. The detailing is done — epic E1, archived in [docs/plan/E1.md](docs/plan/E1.md). Epic E2, replanned for the MVP on 2026-10-22, built the handwriting sheet as the element `<novatrail-canvas>`, for tutor's check board to take in place of its own sheet; the board has decided to keep its own. Its last task was the author's try of the sheet by hand on the demo page, which found nothing to fix. E2 is done and archived in [docs/plan/E2.md](docs/plan/E2.md). The text canvas, epic E3, was dropped on 2026-10-25: canvas builds no text canvas. Epic E4, taken the same day, built the sheet for the product: the eraser and the history of writing, the layers and the grid, the groups of strokes and the crop for checking, the tutor's marks tied to the strokes, the package's full interface and the input on tablets. It was done on 2026-10-26 and is archived in [docs/plan/E4.md](docs/plan/E4.md). Epic E6, taken the same day, detailed and built the workbook, the first slice's second canvas kind, for school maths in grade 3, and was done on 2026-10-27; it is archived in [docs/plan/E6.md](docs/plan/E6.md). Epic E7, taken the same day, took what spec v0.2.42 brought: the task's new open fields, the exit condition that every answer slot holds something — `filled` on a sheet too — and the lesson's worked example, shown on a sheet the learner does not write on. On the way canvas moved to spec v0.4.1, whose npm package gives the types of the module format. E7 was done on 2026-11-01 and is archived in [docs/plan/E7.md](docs/plan/E7.md). Epic E8, taken the same day, is the first release of the package, which spec's cross-repository plan asks for before the app's code; the tests' coverage and the release's build come first. Then canvas moved to spec v0.6.2, of spec's 0.6 line, and its CI now checks the signature of each package of `@altnovatrail` in its lock file, as spec's rules ask; then the element took the module format 0.2 and the types of its contract with the app from spec's npm package, and the package came out in its first release, v0.1.0; from the next one on, the release puts it in npm itself. E8 was done on 2026-11-02 and is archived in [docs/plan/E8.md](docs/plan/E8.md). The same day canvas moved to spec v0.6.5, which made the `media` property part of the element's contract with the app and gave the buttons of every NovaTrail interface package one set of icons, and epic E5, the samples page, was taken: first trying a build opened from a file on the devices and building the page. On 2026-11-08 canvas moved to spec v0.9.8, whose eval protocol now holds the sample file, its markup and the rules the sheet shares with tutor's eval runner — the groups of strokes, the crop, the snapping of a model's box and a workbook's boxes: the tests hold the sheet's code to spec's test vectors of those rules, and the types of the tutor's marks are spec's too. The same day it moved on to v0.9.9, which answers canvas's last two open questions and changes nothing in its code. On 2026-11-09 it moved to spec v0.12.0, whose module format 0.4 lays a workbook's page out by numbers and test vectors of spec's own, which were canvas's, and lets a lesson's worked example stand on a workbook, its demo filling the boxes. The synthetic samples that tutor's CI reads come first now, then the page. So far there is the package, `@altnovatrail/canvas`, with its tools and the sheet's input: the element takes a pen, a finger or a mouse, one pointer at a time, and draws the strokes, and its methods `undo()` and `clear()` undo the last action and clear the sheet. Its eraser, E4's first task, takes strokes off whole along the path of a press: the element's `tool` switches to it, and a pen's eraser erases whatever the tool, if the task allows the eraser. The sheet keeps the history of writing — every stroke, erasing, clearing and undo, at the sheet's time — for the app to keep. The sheet is a stack of layers, in the order of spec's canvas layers: its task, the element's `task` property, rules it in squares, lines or dots, and the task's statement stands above the sheet, its pictures from the consumer's `media`. The task and the module's manifest come in the types of spec's npm package, `@altnovatrail/spec`; the element reads the module format 0.4, which spec v0.10.0 brought — a manifest of another version, 0.3 too, is a TypeError — and the task is checked as its schema checks it, a field it does not know a TypeError; what the statement gives and asks, the fields `givens` and `unknown`, the element takes and leaves for the app to show; a workbook's task the element shows too: its lines laid out on a page the size of the sheet — numbers, text fitted to the layout's own widths, and boxes, up to ten rows — which a screen reader reads as a list. On a workbook each stroke falls in one box or in none, by a rule in integers on the middle of its points' box; what falls in no box is draft, which stays on the page and goes to no check, and the property `filled` tells which boxes hold writing, and on a sheet whether its one answer anywhere on it, or the whole sheet, does. The module's manifest, the `manifest` property, gives the height of a digit on the crop, 24 px unless it gives another, as without a manifest, and the language of the statement. The sheet is at most 1000 CSS pixels wide, and the consumer themes it by custom properties. The DOM-free part parts the strokes into groups by place and time — two strokes no more than 2 seconds apart whose boxes come within a cell of each other are neighbours, and a chain of neighbours is a group — labels the groups A, B, C… by place and gives each one's rectangle on the sheet, for the crop, the tutor's marks and the samples to come; the rule's test vectors are a proposal for the eval protocol. It also draws the crop for checking, with its own raster and PNG encoder: the ink of the visible strokes, scaled so that the writing comes out at the digit height the module's manifest asks for, within the size every Claude model takes as it is, with the groups' frames in its fractions. Its test vectors go to the eval protocol too. The element takes the tutor's marks of a submission back by its property `marks`, with the context it is to give with the crop: a mark holds on to the learner's strokes — a group's, or those a model's box on the crop takes, two thirds of their points within a quarter of a digit of it — and a frame stands round those of them the sheet shows, over the work, read by its name and fading while the learner writes into it. The history goes out by the property `history` and the event `historychange`, which tells of every action with the events it adds, and comes back by the same property: the same history changes nothing, and another, one the sheet could have written, replaces the sheet, undo and all. The pictures' files come from the consumer's `media`, a function that gives a file's Blob by its reference, the contract's own. `exportWork()` gives the work for checking: the PNG of the crop and the context of its submission, which the app keeps and gives back with the tutor's marks, its digest telling the sheet's own context from another sheet's. On a workbook it gives the crop of each filled box, with its item and slot, and the context of the table of boxes; a tutor's mark there stands on an answer cell by its place and frames the box with its ink. Above the statement and the sheet, outside their scrolling, stands the element's own row of buttons — the pen and the eraser the task allows, undo and clear — a toolbar worked by pointer and by keyboard, labelled in English or Russian by the element's own `lang`, each label after an icon of Material Symbols, Google's set, which the build embeds, so the element asks no network for one and the package's NOTICE names it; a `toolchange` event tells of the learner's choice of tool. On a tablet a pen's touch puts the whole page in pen mode, where a finger writes nothing until the row's Draw with finger lets it write again; a touch wider or taller than 100 CSS pixels is a palm and writes nothing, and two fingers scroll the sheet. The row's Up and Down scroll it the same way with a single pointer, on any device, by four fifths of what is in sight. A lesson's worked example comes by the property `workedExample`, in place of a task: its statement stands above the sheet and its grid on it, but the learner does not write there — a finger or a pen scrolls the sheet as the browser scrolls the statement, and the row, named Worked example, has Next, Demo, Up and Down. Next opens the parts of the author's demo one at a time, into a numbered list above the sheet that a polite region tells a screen reader of, and Demo hides the whole demo and shows it again; the property `demoState` and the event `demochange` tell the app how far the learner has gone, and the app may give it back to a new element after the example — in React's JSX `workedExample` comes before `demoState`, and `ondemochange` listens. The pictures of the demo come from the consumer's `media` with the statement's, one file for each reference, all of them as the example comes, so that a part opened shows its picture at once; a picture on an area of the sheet stands on the sheet itself, stretched over its area, and the sheet scrolls to it as its part opens. The element now waits for the app and the samples page; a test plays the app's part, and `just demo` serves a page to try the element on by hand. A release comes out by itself after CI passes on `main`, when a path it is built from has changed: the package's archive, with its NOTICE, LICENSE and their sums, built, signed and checked by .github's shared release, and the same archive goes to npm: the first version by hand, the later ones by the release's own npm step, through trusted publishing, with no long-lived token. A pull request from a branch of this repository tries the release and publishes nothing.
25
25
 
26
26
  ## Install
27
27
 
@@ -56,7 +56,7 @@ The documents are in Russian.
56
56
  - [docs/decisions.md](docs/decisions.md) — the decision log: what was decided, why, and what was rejected.
57
57
  - [RUN.md](RUN.md) — the work plan, in epics.
58
58
 
59
- The working rules, for people and coding agents alike, are in [CLAUDE.md](CLAUDE.md) and [AGENTS.md](AGENTS.md). The platform's shared vocabulary, formats, contracts and decisions are in [spec](https://github.com/altnovatrail/spec/tree/v0.9.9), checked out here as a git submodule at `v0.9.9`: clone this repository with `git clone --recurse-submodules`.
59
+ The working rules, for people and coding agents alike, are in [CLAUDE.md](CLAUDE.md) and [AGENTS.md](AGENTS.md). The platform's shared vocabulary, formats, contracts and decisions are in [spec](https://github.com/altnovatrail/spec/tree/v0.12.0), checked out here as a git submodule at `v0.9.9`: clone this repository with `git clone --recurse-submodules`.
60
60
 
61
61
  ## License
62
62
 
@@ -1,16 +1,40 @@
1
1
  import type { DemoState } from "@altnovatrail/spec/canvas";
2
2
  import type * as open from "@altnovatrail/spec/formatv0/open";
3
3
  import type { GridKind } from "./grid.js";
4
- import { type PicturePart, SHEET_KIND, type SheetTemplate, type StatementPart, type TextPart, type Zone } from "./task.js";
4
+ import { type PicturePart, SHEET_KIND, type StatementPart, type Template, type TextPart, WORKBOOK_KIND, type Zone } from "./task.js";
5
5
  /** A lesson's worked example as a consumer gives it: spec's type (R73 spec). */
6
6
  export type WorkedExample = open.WorkedExample;
7
- /** The sheet of a worked example, as the element checked it: a handwriting sheet and its grid. */
8
- export interface ExampleCanvas {
7
+ /**
8
+ * What a worked example is shown on, as the element checked it: a handwriting sheet and its grid,
9
+ * or a workbook, whose lines the example has (format 0.4).
10
+ */
11
+ export type ExampleCanvas = {
9
12
  readonly kind: typeof SHEET_KIND;
10
13
  /** Squares unless given. */
11
14
  readonly grid?: GridKind;
12
15
  /** The grid's step in sheet units, a whole number from 20 to 500: 50 unless given. */
13
16
  readonly grid_step?: number;
17
+ } | {
18
+ readonly kind: typeof WORKBOOK_KIND;
19
+ };
20
+ /** A box of a worked example's line, with what the demo writes in it. */
21
+ export interface ValueBox {
22
+ readonly binding: "box";
23
+ readonly value: string;
24
+ }
25
+ /** A line of a worked example on a workbook: its pieces of text and its boxes, a box at least. */
26
+ export interface ExampleLine {
27
+ readonly line: readonly (TextPart | ValueBox)[];
28
+ }
29
+ /**
30
+ * A part of the demo on a workbook that fills a box with its value: its line by its place among the
31
+ * example's lines and its box by its place in the line, each from 1.
32
+ */
33
+ export interface FillPart {
34
+ readonly fill: {
35
+ readonly line: number;
36
+ readonly box: number;
37
+ };
14
38
  }
15
39
  /** A picture of the demo on the sheet: shown on the layer of the author's demo, in its area. */
16
40
  export interface AreaPicture {
@@ -21,13 +45,18 @@ export interface AreaPicture {
21
45
  /** The area of the sheet it is shown in, at its size: on the sheet, by the rule lesson.areas. */
22
46
  readonly area: Zone;
23
47
  }
24
- /** A part of the author's demo: a paragraph, a picture beside the sheet, or one on the sheet. */
25
- export type ExamplePart = TextPart | PicturePart | AreaPicture;
48
+ /**
49
+ * A part of the author's demo: a paragraph, a picture beside the sheet, one on a handwriting sheet,
50
+ * or a box of a workbook filled.
51
+ */
52
+ export type ExamplePart = TextPart | PicturePart | AreaPicture | FillPart;
26
53
  /** A worked example as the element checked it: its statement, its sheet and its demo's parts. */
27
54
  export interface CheckedExample {
28
55
  /** What the example asks, in the module's explanation language, as a task's statement. */
29
56
  readonly statement: readonly StatementPart[];
30
57
  readonly canvas: ExampleCanvas;
58
+ /** On a workbook, the example's lines, laid out as a task's are; on a sheet, none. */
59
+ readonly lines?: readonly ExampleLine[];
31
60
  /** The parts of how the example is solved, in order: the learner opens them one at a time. */
32
61
  readonly demo: readonly ExamplePart[];
33
62
  }
@@ -47,10 +76,20 @@ export declare const DEMO_START: DemoState;
47
76
  */
48
77
  export declare function exampleOf(value: unknown): CheckedExample;
49
78
  /**
50
- * What the sheet makes of a worked example, as of a task (R42): the grid on the sheet, the
51
- * statement above it, and the pen alone, for the learner does not write on it.
79
+ * What the canvas makes of a worked example, as of a task (R42): the grid on the sheet, or the
80
+ * example's lines laid out on the workbook's page as a task's are, its boxes empty till the demo
81
+ * fills them; the statement above it, and the pen alone, for the learner does not write on it.
82
+ */
83
+ export declare function exampleTemplate(example: CheckedExample): Template;
84
+ /** Whether a part of the demo fills a box of a workbook. */
85
+ export declare function isFill(part: ExamplePart): part is FillPart;
86
+ /**
87
+ * The boxes the open parts of a demo have filled, by line and box from 1, with their values — none
88
+ * while the demo is hidden, as a picture on the sheet goes with it.
52
89
  */
53
- export declare function exampleTemplate(example: CheckedExample): SheetTemplate;
90
+ export declare function filledBy(example: CheckedExample, { opened, hidden }: DemoState): ReadonlyMap<string, string>;
91
+ /** What a fill of the demo writes: the value of its box, which the check found among the lines. */
92
+ export declare function fillValue(example: CheckedExample, { fill }: FillPart): string;
54
93
  /**
55
94
  * The state of a demo of `parts` parts, checked (R42): an object of `opened`, a whole number from 0
56
95
  * to `parts`, and `hidden`, true or false, both of them and nothing else. What it refuses is a
@@ -1,14 +1,18 @@
1
1
  // A lesson's worked example (R58 spec; R42): the open view of a segment `worked_example`, of spec's
2
- // type WorkedExample (R73 spec; R41) — its statement, the handwriting sheet the author's demo is
3
- // shown on, and the demo's parts in order: paragraphs, pictures beside the sheet and pictures on
4
- // areas of it. The element checks it as the schema does, every object closed (R32), and by the
5
- // format's rule lesson.areas too: an area lies on the sheet. What the check gives is the element's
6
- // own shape of the same example, the kinds of its parts apart, which the rest of the element reads,
7
- // and the sheet makes a template of it as of a task: its grid, its statement and the pen alone. How
8
- // far the learner has gone through the demo is a state of its own, which app keeps and gives back.
9
- import { closed, listOf, sameItems, shown, whole } from "./fields.js";
2
+ // type WorkedExample (R73 spec; R41) — its statement, the handwriting sheet or the workbook the
3
+ // author's demo is shown on, and the demo's parts in order: paragraphs, pictures beside the sheet,
4
+ // pictures on areas of a sheet, and on a workbook the boxes it fills (format 0.4; T85, R60). The
5
+ // element checks it as the schema does, every object closed (R32), and by the format's rules
6
+ // lesson.areas, lesson.example-fills and lesson.workbook-page too: an area lies on the sheet, the
7
+ // demo fills each box of the lines once, and the lines fit the page, each value its box. What the
8
+ // check gives is the element's own shape of the same example, the kinds of its parts apart, which
9
+ // the rest of the element reads, and the canvas makes a template of it as of a task: its grid or
10
+ // its lines, its statement and the pen alone. How far the learner has gone through the demo is a
11
+ // state of its own, which app keeps and gives back.
12
+ import { closed, listOf, record, sameItems, shown, text, whole } from "./fields.js";
13
+ import { LAYOUT, pageOf, pieceWidth } from "./page.js";
10
14
  import { SHEET } from "./sheet.js";
11
- import { altOf, gridFrom, gridOf, partOf, pictureOf, SHEET_KIND, samePart, textOrPictureOf, zoneOf, } from "./task.js";
15
+ import { altOf, gridFrom, gridOf, partOf, pictureOf, SHEET_KIND, samePart, textOrPictureOf, WORKBOOK_KIND, zoneOf, } from "./task.js";
12
16
  /** Where every demo starts: no part open, and nothing hidden. One for every element. */
13
17
  export const DEMO_START = Object.freeze({ opened: 0, hidden: false });
14
18
  /** The path the element names a worked example's fields by: its property's name. */
@@ -24,18 +28,71 @@ const PEN_ALONE = Object.freeze(["pen"]);
24
28
  * `null` is a value, and a wrong one.
25
29
  */
26
30
  export function exampleOf(value) {
27
- const example = closed(value, ROOT, ["statement", "canvas", "demo"], "a worked example");
31
+ const fields = ["statement", "canvas", "lines", "demo"];
32
+ const example = closed(value, ROOT, fields, "a worked example");
28
33
  const statement = listOf(example.statement, `${ROOT}.statement`, 1, "parts", partOf);
29
34
  const canvas = exampleCanvasOf(example.canvas, `${ROOT}.canvas`);
30
- const demo = listOf(example.demo, `${ROOT}.demo`, 1, "parts of the demo", demoPartOf);
31
- return { statement, canvas, demo };
35
+ if (canvas.kind === SHEET_KIND) {
36
+ if (example.lines !== undefined) {
37
+ throw new TypeError(`${ROOT}.lines: no lines on a handwriting sheet, only on a workbook`);
38
+ }
39
+ const demo = listOf(example.demo, `${ROOT}.demo`, 1, "parts of the demo", sheetDemoPartOf);
40
+ return { statement, canvas, demo };
41
+ }
42
+ if (example.lines === undefined) {
43
+ throw new TypeError(`${ROOT}: a worked example on a workbook has its lines, ${ROOT}.lines`);
44
+ }
45
+ const lines = listOf(example.lines, `${ROOT}.lines`, 1, "lines", exampleLineOf);
46
+ // The lines fit the page as a task's do, a TypeError at the first that does not
47
+ // (lesson.workbook-page).
48
+ try {
49
+ pageOf(itemsOf(lines));
50
+ }
51
+ catch (error) {
52
+ if (!(error instanceof TypeError))
53
+ throw error;
54
+ throw new TypeError(error.message.replace(/^task\.items/, `${ROOT}.lines`));
55
+ }
56
+ const parts = listOf(example.demo, `${ROOT}.demo`, 1, "parts of the demo", workbookDemoPartOf);
57
+ return { statement, canvas, lines, demo: fillsOf(parts, lines) };
32
58
  }
33
59
  /**
34
- * What the sheet makes of a worked example, as of a task (R42): the grid on the sheet, the
35
- * statement above it, and the pen alone, for the learner does not write on it.
60
+ * What the canvas makes of a worked example, as of a task (R42): the grid on the sheet, or the
61
+ * example's lines laid out on the workbook's page as a task's are, its boxes empty till the demo
62
+ * fills them; the statement above it, and the pen alone, for the learner does not write on it.
36
63
  */
37
64
  export function exampleTemplate(example) {
38
- return { grid: gridFrom(example.canvas), statement: example.statement, tools: PEN_ALONE };
65
+ const { canvas, lines, statement } = example;
66
+ if (canvas.kind === WORKBOOK_KIND) {
67
+ return { page: pageOf(itemsOf(lines ?? [])), statement, tools: PEN_ALONE };
68
+ }
69
+ return { grid: gridFrom(canvas), statement, tools: PEN_ALONE };
70
+ }
71
+ /** Whether a part of the demo fills a box of a workbook. */
72
+ export function isFill(part) {
73
+ return "fill" in part;
74
+ }
75
+ /**
76
+ * The boxes the open parts of a demo have filled, by line and box from 1, with their values — none
77
+ * while the demo is hidden, as a picture on the sheet goes with it.
78
+ */
79
+ export function filledBy(example, { opened, hidden }) {
80
+ const filled = new Map();
81
+ if (hidden)
82
+ return filled;
83
+ for (const part of example.demo.slice(0, opened)) {
84
+ if (isFill(part))
85
+ filled.set(`${part.fill.line}.${part.fill.box}`, fillValue(example, part));
86
+ }
87
+ return filled;
88
+ }
89
+ /** What a fill of the demo writes: the value of its box, which the check found among the lines. */
90
+ export function fillValue(example, { fill }) {
91
+ return boxesOf(example.lines?.[fill.line - 1])[fill.box - 1]?.value ?? "";
92
+ }
93
+ /** The boxes of a line of a worked example, in order: its pieces with a value. */
94
+ function boxesOf(line) {
95
+ return (line?.line ?? []).filter((piece) => "value" in piece);
39
96
  }
40
97
  /**
41
98
  * The state of a demo of `parts` parts, checked (R42): an object of `opened`, a whole number from 0
@@ -59,23 +116,131 @@ export function onSheet(part) {
59
116
  * step as drawn, the defaults filled in — and the same parts of the demo in the same order.
60
117
  */
61
118
  export function sameExample(a, b) {
119
+ if (a.canvas.kind !== b.canvas.kind)
120
+ return false;
121
+ if (a.canvas.kind === WORKBOOK_KIND) {
122
+ return (sameItems(a.statement, b.statement, samePart) &&
123
+ sameItems(a.lines ?? [], b.lines ?? [], sameLine) &&
124
+ sameItems(a.demo, b.demo, sameDemoPart));
125
+ }
62
126
  const [one, other] = [gridFrom(a.canvas), gridFrom(b.canvas)];
63
127
  return (sameItems(a.statement, b.statement, samePart) &&
64
128
  one.kind === other.kind &&
65
129
  one.step === other.step &&
66
130
  sameItems(a.demo, b.demo, sameDemoPart));
67
131
  }
132
+ /** Whether two lines of a worked example hold the same pieces and boxes, the same values in them. */
133
+ function sameLine(a, b) {
134
+ return sameItems(a.line, b.line, (one, other) => "text" in one
135
+ ? "text" in other && one.text === other.text
136
+ : "value" in other && one.value === other.value);
137
+ }
68
138
  /**
69
- * The sheet of a worked example: a handwriting sheet and its grid, and no tools, for the learner
70
- * does not write on it (README of the format, §21). A workbook is in no version of the format yet.
139
+ * What a worked example is shown on: a handwriting sheet and its grid, or a workbook, and no tools,
140
+ * for the learner does not write on it (README of the format, §21).
71
141
  */
72
142
  function exampleCanvasOf(value, path) {
73
143
  const canvas = closed(value, path, ["kind", "grid", "grid_step"], "a worked example's canvas");
144
+ if (canvas.kind === WORKBOOK_KIND) {
145
+ closed(canvas, path, ["kind"], "a worked example's workbook");
146
+ return { kind: WORKBOOK_KIND };
147
+ }
74
148
  if (canvas.kind !== SHEET_KIND) {
75
- throw new TypeError(`${path}.kind: "${SHEET_KIND}", for a worked example is shown on a handwriting sheet, not ${shown(canvas.kind)}`);
149
+ throw new TypeError(`${path}.kind: "${SHEET_KIND}" or "${WORKBOOK_KIND}", what a worked example is shown on, not ${shown(canvas.kind)}`);
76
150
  }
77
151
  return { kind: SHEET_KIND, ...gridOf(canvas, path) };
78
152
  }
153
+ /**
154
+ * A line of a worked example on a workbook: pieces of text and boxes, a box at least, each box with
155
+ * its value, no wider than the box as a text of the line (lesson.workbook-page).
156
+ */
157
+ function exampleLineOf(value, path) {
158
+ const item = closed(value, path, ["line"], "a line of a worked example");
159
+ const line = listOf(item.line, `${path}.line`, 1, "pieces of text and boxes", examplePieceOf);
160
+ if (!line.some((part) => "value" in part)) {
161
+ throw new TypeError(`${path}.line: at least one box, { binding: "box", value }, not ${shown(item.line)}`);
162
+ }
163
+ return { line };
164
+ }
165
+ /** A piece of a worked example's line: a piece of text, or a box with what the demo writes. */
166
+ function examplePieceOf(value, path) {
167
+ const part = closed(value, path, ["text", "binding", "value"], "a piece of a line");
168
+ if (part.binding === undefined) {
169
+ closed(part, path, ["text"], "a piece of a line's text");
170
+ return { text: text(part.text, `${path}.text`, "a piece of the line's text") };
171
+ }
172
+ closed(part, path, ["binding", "value"], "a box of a line");
173
+ if (part.binding !== "box") {
174
+ throw new TypeError(`${path}.binding: "box", a box of a workbook, not ${shown(part.binding)}`);
175
+ }
176
+ const written = text(part.value, `${path}.value`, "what the demo writes in the box");
177
+ const width = pieceWidth(written);
178
+ if (width > LAYOUT.boxWidth) {
179
+ throw new TypeError(`${path}.value: no wider than its box, ${LAYOUT.boxWidth} units, not ${width}: ${shown(written)}`);
180
+ }
181
+ return { binding: "box", value: written };
182
+ }
183
+ /** A worked example's lines as a task's items, for the page to lay out: the boxes without values. */
184
+ function itemsOf(lines) {
185
+ return lines.map(({ line }) => ({
186
+ line: line.map((part) => ("text" in part ? part : { binding: "box" })),
187
+ }));
188
+ }
189
+ /**
190
+ * The demo on a workbook, checked by its fills: each fill names a box of the lines, and the demo
191
+ * fills each box once (lesson.example-fills). A TypeError names the fill, or the box never filled.
192
+ */
193
+ function fillsOf(parts, lines) {
194
+ const done = new Set();
195
+ for (const [n, part] of parts.entries()) {
196
+ if (!isFill(part))
197
+ continue;
198
+ const { line, box } = part.fill;
199
+ const path = `${ROOT}.demo[${n}].fill`;
200
+ if (boxesOf(lines[line - 1])[box - 1] === undefined) {
201
+ throw new TypeError(`${path}: a box of the example's lines, not box ${box} of line ${line}`);
202
+ }
203
+ const key = `${line}.${box}`;
204
+ if (done.has(key)) {
205
+ throw new TypeError(`${path}: box ${box} of line ${line}, which the demo filled already`);
206
+ }
207
+ done.add(key);
208
+ }
209
+ for (const [l, line] of lines.entries()) {
210
+ for (let b = 1; b <= boxesOf(line).length; b++) {
211
+ if (!done.has(`${l + 1}.${b}`)) {
212
+ throw new TypeError(`${ROOT}.demo: a part that fills box ${b} of line ${l + 1}, which no part fills`);
213
+ }
214
+ }
215
+ }
216
+ return parts;
217
+ }
218
+ /** A part of the demo on a handwriting sheet: no box to fill. */
219
+ function sheetDemoPartOf(value, path) {
220
+ const part = record(value, path);
221
+ if (part.fill !== undefined) {
222
+ throw new TypeError(`${path}.fill: no box to fill on a handwriting sheet, only on a workbook`);
223
+ }
224
+ return demoPartOf(part, path);
225
+ }
226
+ /** A part of the demo on a workbook: a paragraph, a picture beside the sheet, or a box filled. */
227
+ function workbookDemoPartOf(value, path) {
228
+ const part = record(value, path);
229
+ if (part.fill === undefined) {
230
+ if (part.area !== undefined) {
231
+ throw new TypeError(`${path}.area: no picture stands on a workbook's page, only beside it`);
232
+ }
233
+ return demoPartOf(part, path);
234
+ }
235
+ closed(part, path, ["fill"], "a box the demo fills");
236
+ const fill = closed(part.fill, `${path}.fill`, ["line", "box"], "a box of the lines");
237
+ return {
238
+ fill: {
239
+ line: whole(fill.line, `${path}.fill.line`, 1, 99),
240
+ box: whole(fill.box, `${path}.fill.box`, 1, 99),
241
+ },
242
+ };
243
+ }
79
244
  /**
80
245
  * A part of the demo: a paragraph, a picture beside the sheet with its size, or a picture on the
81
246
  * sheet with its area, which has no size of its own.
@@ -103,8 +268,11 @@ function areaOf(value, path) {
103
268
  }
104
269
  return area;
105
270
  }
106
- /** Whether two parts of the demo show the same: a paragraph, or a picture beside or on the sheet. */
271
+ /** Whether two parts of the demo show the same: a paragraph, a picture beside or on the sheet, a fill. */
107
272
  function sameDemoPart(a, b) {
273
+ if (isFill(a) || isFill(b)) {
274
+ return isFill(a) && isFill(b) && a.fill.line === b.fill.line && a.fill.box === b.fill.box;
275
+ }
108
276
  if (!onSheet(a) || !onSheet(b))
109
277
  return !onSheet(a) && !onSheet(b) && samePart(a, b);
110
278
  return (a.picture === b.picture &&
@@ -4,7 +4,7 @@
4
4
  * spec; README of the format, §2). The format's rules of a module's versions are module-kit's and
5
5
  * api's: the element compares none (R32).
6
6
  */
7
- export declare const FORMAT = "0.3";
7
+ export declare const FORMAT = "0.4";
8
8
  /** The open view of a module's manifest, whole: spec's type (R73 spec). */
9
9
  export type { Manifest } from "@altnovatrail/spec/formatv0/open";
10
10
  /**
@@ -14,7 +14,7 @@ import { closed, record, shown, whole } from "./fields.js";
14
14
  * spec; README of the format, §2). The format's rules of a module's versions are module-kit's and
15
15
  * api's: the element compares none (R32).
16
16
  */
17
- export const FORMAT = "0.3";
17
+ export const FORMAT = "0.4";
18
18
  /** The settings without a manifest: digits of 24 px, and the element's own language. */
19
19
  export const NO_MANIFEST = Object.freeze({ digit: CROP.digit, lang: undefined });
20
20
  /**
@@ -53,6 +53,12 @@ export interface Page {
53
53
  }
54
54
  /** Whether a part of a line is a box. */
55
55
  export declare function isBox(part: PageText | PageBox): part is PageBox;
56
+ /**
57
+ * The width of a text as one piece on one line, in whole units: its words and the spaces between
58
+ * them, as the layout measures a fragment of a piece (R35). A worked example's value is held to its
59
+ * box's width by it (README of the format, §21: lesson.workbook-page; R60), and drawn at it.
60
+ */
61
+ export declare function pieceWidth(text: string, layout?: Layout): number;
56
62
  /**
57
63
  * The page of a workbook's items (R35): a function of the items alone, in whole units, frozen. A
58
64
  * row that does not fit is a TypeError at the first item of it, `task.items[i]`.
package/lib/core/page.js CHANGED
@@ -3,9 +3,11 @@
3
3
  // the sheet's, 1000 × 1400 units (R06), and the boxes stay where the layout puts them on any
4
4
  // device: the widths of the text come from the canvas's own table (widths.ts), and SVG fits the
5
5
  // text to them. Short lines stand two to a row, in the task's order; a long line takes the page's
6
- // width and wraps. A task whose lines do not fit is a TypeError: nothing shrinks. The core's entry
7
- // point does not give the layout, and vectors/pages.json pins its numbers: once released, they
8
- // change only by a decision, for histories know neither the task nor the layout (R34).
6
+ // width and wraps. A task whose lines do not fit is a TypeError: nothing shrinks. The numbers and
7
+ // the rule are the format's since 0.4, its page.yaml and its test vectors, which the npm package
8
+ // @altnovatrail/spec carries and the tests hold the layout to (README of the format, §14; R60):
9
+ // they change only with the format, for histories know neither the task nor the layout (R34). The
10
+ // core's entry point does not give the layout.
9
11
  import { SHEET } from "./sheet.js";
10
12
  import { clustersOf, clusterWidth, SPACE, wordsOf, wordWidth } from "./widths.js";
11
13
  /** The layout the workbook has (R35): the sweep of T47 chose it around the box the author chose. */
@@ -45,6 +47,18 @@ function measuresOf(layout) {
45
47
  floor: SHEET.height - layout.margin,
46
48
  };
47
49
  }
50
+ /**
51
+ * The width of a text as one piece on one line, in whole units: its words and the spaces between
52
+ * them, as the layout measures a fragment of a piece (R35). A worked example's value is held to its
53
+ * box's width by it (README of the format, §21: lesson.workbook-page; R60), and drawn at it.
54
+ */
55
+ export function pieceWidth(text, layout = LAYOUT) {
56
+ const words = wordsOf(text);
57
+ if (words.length === 0)
58
+ return 0;
59
+ const hundredths = words.reduce((sum, word) => sum + wordWidth(word), 0);
60
+ return textWidth(layout, hundredths + SPACE * (words.length - 1));
61
+ }
48
62
  /** The width of a text of `hundredths` of the size, in whole units, rounded up. */
49
63
  function textWidth(layout, hundredths) {
50
64
  const product = layout.size * hundredths;
package/lib/core/task.js CHANGED
@@ -1,4 +1,4 @@
1
- // The open view of a task of the module format 0.3 (R51 spec). What a consumer gives is of spec's
1
+ // The open view of a task of the module format 0.4 (R51 spec). What a consumer gives is of spec's
2
2
  // type, from its npm package at the version of the spec/ submodule (R73 spec; R41), with one field
3
3
  // of the element's own, `canvas.tools`, until the format has one (R28). The element checks a task as
4
4
  // the schema does, every object closed: a field the open view has not — an old name, a closed field
@@ -46,7 +46,7 @@ export declare class NovatrailCanvas extends HTMLElement implements CanvasElemen
46
46
  get tool(): Tool;
47
47
  set tool(tool: Tool);
48
48
  /**
49
- * The open view of the task, of the module format 0.3 (R32): its ID, its statement, which the
49
+ * The open view of the task, of the module format 0.4 (R32): its ID, its statement, which the
50
50
  * element shows above the page (R23), what the statement gives and asks, which app shows (R57
51
51
  * spec), its canvas — the kind, the sheet's grid and the tools the task allows (R28) — and its
52
52
  * items. A workbook's items are its lines, which the template shows, laid out on the page (R35,
@@ -63,8 +63,9 @@ export declare class NovatrailCanvas extends HTMLElement implements CanvasElemen
63
63
  set task(task: Task | undefined);
64
64
  /**
65
65
  * A lesson's worked example (R42; R58 spec): the open view of a segment `worked_example` of the
66
- * module format 0.3 — its statement, which the element shows above the sheet as a task's (R23),
67
- * the grid of its sheet, and the parts of the author's demo. The type is spec's (R73 spec; R41).
66
+ * module format 0.4 — its statement, which the element shows above the sheet as a task's (R23),
67
+ * the grid of its sheet or the lines of its workbook, and the parts of the author's demo, which
68
+ * on a workbook fill the lines' boxes with their values (T85). The type is spec's (R73 spec; R41).
68
69
  * The element shows a task or a worked example, not both: an example while a task is given, while
69
70
  * the sheet has a history of writing, or while a stroke or an erasing is under way, is a
70
71
  * TypeError, and so is one the schema refuses, at its field. `undefined` takes the example off,
@@ -99,7 +100,7 @@ export declare class NovatrailCanvas extends HTMLElement implements CanvasElemen
99
100
  set demoState(state: DemoState | undefined);
100
101
  /**
101
102
  * The open view of the module's manifest (R32), of which the element reads the version of the
102
- * module format — "0.3", or the manifest is a TypeError (R44 spec) — the height of a digit on the
103
+ * module format — "0.4", or the manifest is a TypeError (R44 spec) — the height of a digit on the
103
104
  * crop for checking, `canvas.digit_height`, 24 px unless given, and the explanation language, the
104
105
  * statement's and a workbook's lines'. Without a manifest the digits are 24 px, and the statement
105
106
  * and the lines are in the element's own `lang`. The rest of it the element passes over, but for
@@ -12,7 +12,7 @@
12
12
  // clearing — labelled in the element's `lang` (T41; R28). On a tablet a pen's touch puts the page
13
13
  // in pen mode, where a finger writes nothing and a button lets it write again, a palm writes
14
14
  // nothing, and two fingers scroll (T35; R29). The task comes in the names of the module format
15
- // 0.3, checked as its schema checks it, and the module's manifest gives the height of a digit and
15
+ // 0.4, checked as its schema checks it, and the module's manifest gives the height of a digit and
16
16
  // the statement's language (T44; R32). Up and Down in the row scroll what two fingers would, with
17
17
  // a single pointer (T45; R33). The workbook's lines, laid out on its page, stand on the template in
18
18
  // place of the grid — numbers, text and boxes, which a screen reader reads in the task's order (T51;
@@ -28,13 +28,14 @@
28
28
  // the demo come from the consumer's media, one file for each reference, all of them as the task or
29
29
  // the example comes: a part opened shows its picture at once (T64; R42). A picture on an area of
30
30
  // the sheet stands on the layer of the author's demo, over its area, and the sheet scrolls to the
31
- // area as the part opens (T61; R42). The types of its contract with app are spec's, of
32
- // @altnovatrail/spec/canvas: the element implements its CanvasElement, and the package puts the
31
+ // area as the part opens (T61; R42). A worked example on a workbook lays its lines out as a task's,
32
+ // and a part of its demo that fills a box writes the box's value in it (T85; R61). The types of
33
+ // its contract with app are spec's, of @altnovatrail/spec/canvas: the element implements its CanvasElement, and the package puts the
33
34
  // element and its events in the DOM's maps (T69; R07).
34
35
  import { cellOf, cellsOf, filledOf, sheetFilledOf } from "../core/boxes.js";
35
36
  import { Clock } from "../core/clock.js";
36
37
  import { ERASER_BUTTON, Eraser } from "../core/eraser.js";
37
- import { DEMO_START, demoStateOf, exampleOf, exampleTemplate, sameExample, } from "../core/example.js";
38
+ import { DEMO_START, demoStateOf, exampleOf, exampleTemplate, filledBy, fillValue, sameExample, } from "../core/example.js";
38
39
  import { sameItems } from "../core/fields.js";
39
40
  import { boxOf } from "../core/geometry.js";
40
41
  import { gridPath } from "../core/grid.js";
@@ -43,7 +44,7 @@ import { begins, sameHistory } from "../core/history.js";
43
44
  import { NO_MANIFEST, settingsOf } from "../core/manifest.js";
44
45
  import { marksOf, placesOf, shownOf } from "../core/marks.js";
45
46
  import { INK_REACH, outline } from "../core/outline.js";
46
- import { isBox, LAYOUT, } from "../core/page.js";
47
+ import { isBox, LAYOUT, pieceWidth, } from "../core/page.js";
47
48
  import { Recorder } from "../core/recorder.js";
48
49
  import { fitScale, K_MAX, scaleFor, toSheet } from "../core/screen.js";
49
50
  import { SHEET } from "../core/sheet.js";
@@ -383,6 +384,12 @@ export class NovatrailCanvas extends HTMLElement {
383
384
  #laid;
384
385
  #cells = [];
385
386
  #rects = [];
387
+ /**
388
+ * The values a worked example's demo has written in its boxes, by line and box from 1, and their
389
+ * texts on the page, one for each frame that has had one (T85; R60).
390
+ */
391
+ #fills = new Map();
392
+ #values = [];
386
393
  #taken = new WeakMap();
387
394
  /** Which answer slots are filled, the same array until one changes, or null (R37, R42). */
388
395
  #filled = null;
@@ -450,6 +457,8 @@ export class NovatrailCanvas extends HTMLElement {
450
457
  this.#stage.className = "stage";
451
458
  this.#grid.setAttribute("class", "grid");
452
459
  this.#print.setAttribute("class", "print");
460
+ // The constructor reads no attribute: the labels of a demo's boxes are English till `lang` comes.
461
+ this.#nameBoxes(null);
453
462
  this.#lines.setAttribute("role", "list");
454
463
  this.#print.append(this.#lines);
455
464
  this.#template.append(this.#grid, this.#print);
@@ -530,7 +539,7 @@ export class NovatrailCanvas extends HTMLElement {
530
539
  this.#refresh();
531
540
  }
532
541
  /**
533
- * The open view of the task, of the module format 0.3 (R32): its ID, its statement, which the
542
+ * The open view of the task, of the module format 0.4 (R32): its ID, its statement, which the
534
543
  * element shows above the page (R23), what the statement gives and asks, which app shows (R57
535
544
  * spec), its canvas — the kind, the sheet's grid and the tools the task allows (R28) — and its
536
545
  * items. A workbook's items are its lines, which the template shows, laid out on the page (R35,
@@ -560,8 +569,9 @@ export class NovatrailCanvas extends HTMLElement {
560
569
  }
561
570
  /**
562
571
  * A lesson's worked example (R42; R58 spec): the open view of a segment `worked_example` of the
563
- * module format 0.3 — its statement, which the element shows above the sheet as a task's (R23),
564
- * the grid of its sheet, and the parts of the author's demo. The type is spec's (R73 spec; R41).
572
+ * module format 0.4 — its statement, which the element shows above the sheet as a task's (R23),
573
+ * the grid of its sheet or the lines of its workbook, and the parts of the author's demo, which
574
+ * on a workbook fill the lines' boxes with their values (T85). The type is spec's (R73 spec; R41).
565
575
  * The element shows a task or a worked example, not both: an example while a task is given, while
566
576
  * the sheet has a history of writing, or while a stroke or an erasing is under way, is a
567
577
  * TypeError, and so is one the schema refuses, at its field. `undefined` takes the example off,
@@ -596,6 +606,7 @@ export class NovatrailCanvas extends HTMLElement {
596
606
  this.#sheet.classList.remove("example");
597
607
  this.#demoState = undefined;
598
608
  this.#parts.clear();
609
+ this.#showFills(new Map());
599
610
  this.#present(NO_TASK);
600
611
  }
601
612
  else if (shown === undefined || !sameExample(checked, shown)) {
@@ -606,6 +617,8 @@ export class NovatrailCanvas extends HTMLElement {
606
617
  this.#sheet.classList.add("example");
607
618
  this.#demoState = DEMO_START;
608
619
  this.#parts.clear();
620
+ // The last example's values leave its boxes: another may keep the same page.
621
+ this.#showFills(new Map());
609
622
  this.#present(exampleTemplate(checked));
610
623
  }
611
624
  }
@@ -640,7 +653,7 @@ export class NovatrailCanvas extends HTMLElement {
640
653
  }
641
654
  /**
642
655
  * The open view of the module's manifest (R32), of which the element reads the version of the
643
- * module format — "0.3", or the manifest is a TypeError (R44 spec) — the height of a digit on the
656
+ * module format — "0.4", or the manifest is a TypeError (R44 spec) — the height of a digit on the
644
657
  * crop for checking, `canvas.digit_height`, 24 px unless given, and the explanation language, the
645
658
  * statement's and a workbook's lines'. Without a manifest the digits are 24 px, and the statement
646
659
  * and the lines are in the element's own `lang`. The rest of it the element passes over, but for
@@ -827,8 +840,21 @@ export class NovatrailCanvas extends HTMLElement {
827
840
  */
828
841
  attributeChangedCallback() {
829
842
  this.#tools.label(this.getAttribute("lang"));
843
+ this.#nameBoxes();
830
844
  this.#markBoxes();
831
845
  }
846
+ /**
847
+ * The demo's list says a box it fills in the labels' language, and its value: «Окошко 1.2: 5»;
848
+ * its open items too, anew. Without `lang` the labels are English, as the element's own.
849
+ */
850
+ #nameBoxes(tag = this.getAttribute("lang")) {
851
+ const { lang, labels } = labelsFor(tag);
852
+ this.#parts.fillWords = (part) => {
853
+ const name = `${labels.box} ${part.fill.line}.${part.fill.box}`;
854
+ const value = this.#example === undefined ? "" : fillValue(this.#example, part);
855
+ return { number: value === "" ? name : `${name}: ${value}`, lang };
856
+ };
857
+ }
832
858
  /**
833
859
  * Takes back the last action on the sheet — the last stroke written, the last erasing, or the
834
860
  * clearing — and says whether there was one (R19, R22), with a `historychange` if there was
@@ -991,6 +1017,7 @@ export class NovatrailCanvas extends HTMLElement {
991
1017
  this.#cells = workbook ? cellsOf(template.page) : [];
992
1018
  // The frames in the order of the lines, the task's: item by item, slot by slot.
993
1019
  this.#rects = [...this.#lines.querySelectorAll("rect")];
1020
+ this.#values = [];
994
1021
  this.#taken = new WeakMap();
995
1022
  // New frames: marked by the filling if it changed, or else here.
996
1023
  if (!this.#fill())
@@ -1042,8 +1069,13 @@ export class NovatrailCanvas extends HTMLElement {
1042
1069
  const several = (this.#laid?.items[cell.item - 1]?.boxes.length ?? 0) > 1;
1043
1070
  const filled = this.#filled?.[cell.item - 1]?.[cell.slot - 1] === true;
1044
1071
  const name = several ? `${labels.box} ${cell.slot}` : labels.box;
1072
+ // A worked example's box says what its demo wrote in it: the text itself says nothing.
1073
+ const value = this.#fills.get(`${cell.item}.${cell.slot}`);
1045
1074
  rect.classList.toggle("filled", filled);
1046
- write(rect, "aria-label", filled ? `${name}, ${labels.filled}` : name);
1075
+ let label = filled ? `${name}, ${labels.filled}` : name;
1076
+ if (value !== undefined)
1077
+ label = `${name}: ${value}`;
1078
+ write(rect, "aria-label", label);
1047
1079
  write(rect, "lang", lang);
1048
1080
  }
1049
1081
  }
@@ -1538,9 +1570,52 @@ export class NovatrailCanvas extends HTMLElement {
1538
1570
  #showDemo(example, state) {
1539
1571
  this.#demoState = state;
1540
1572
  this.#parts.show(example.demo, state);
1573
+ this.#showFills(filledBy(example, state));
1541
1574
  this.#layout();
1542
1575
  this.#refresh();
1543
1576
  }
1577
+ /**
1578
+ * The values a worked example's demo has written in the boxes of its workbook (T85; R60): each in
1579
+ * the middle of its box, on the line's baseline, at the width the layout gives its text, as the
1580
+ * lines' texts are; none while the demo is hidden. Only what changed is written.
1581
+ */
1582
+ #showFills(fills) {
1583
+ this.#fills = fills;
1584
+ const page = this.#laid;
1585
+ if (page === undefined)
1586
+ return;
1587
+ let changed = false;
1588
+ for (const [c, cell] of this.#cells.entries()) {
1589
+ const value = fills.get(`${cell.item}.${cell.slot}`);
1590
+ let text = this.#values[c];
1591
+ if (value === (text?.textContent ?? undefined))
1592
+ continue;
1593
+ changed = true;
1594
+ if (value === undefined) {
1595
+ text?.remove();
1596
+ this.#values[c] = undefined;
1597
+ continue;
1598
+ }
1599
+ const box = page.items[cell.item - 1]?.boxes[cell.slot - 1];
1600
+ const rect = this.#rects[c];
1601
+ if (box === undefined || rect === undefined)
1602
+ continue;
1603
+ if (text === undefined) {
1604
+ text = document.createElementNS(SVG, "text");
1605
+ text.setAttribute("aria-hidden", "true");
1606
+ text.setAttribute("lengthAdjust", "spacingAndGlyphs");
1607
+ rect.after(text);
1608
+ this.#values[c] = text;
1609
+ }
1610
+ const width = pieceWidth(value);
1611
+ text.setAttribute("x", String(box.left + Math.floor((box.width - width) / 2)));
1612
+ text.setAttribute("y", String(box.top + Math.floor(box.height / 2) + Math.floor(LAYOUT.size / 3)));
1613
+ text.setAttribute("textLength", String(width));
1614
+ text.textContent = value;
1615
+ }
1616
+ if (changed)
1617
+ this.#markBoxes();
1618
+ }
1544
1619
  /** Whoever listens hears of the demo's new state, which the learner chose (R42). */
1545
1620
  #demoChanged() {
1546
1621
  const detail = this.#demoState;
@@ -1,4 +1,4 @@
1
- import { type DemoState, type ExamplePart } from "../core/example.js";
1
+ import { type DemoState, type ExamplePart, type FillPart } from "../core/example.js";
2
2
  import type { Pictures } from "./pictures.js";
3
3
  /** The number of a part the region tells of, as the labels say it, and the labels' language. */
4
4
  export interface Told {
@@ -28,6 +28,8 @@ export declare class Parts {
28
28
  * hidden, so it paints nothing and takes no pointer, but it has a box to scroll into sight.
29
29
  */
30
30
  readonly spot: HTMLDivElement;
31
+ /** What a part that fills a box says from now on, the items already open too. */
32
+ set fillWords(words: (part: FillPart) => Told);
31
33
  /** The list and the region, by their IDs, whose pictures the element's pictures give files. */
32
34
  constructor(ids: {
33
35
  readonly status: string;
@@ -6,11 +6,13 @@
6
6
  // picture is, which shows the picture once the element's pictures have its file and what it shows
7
7
  // in words till then. A picture on an area of the sheet is read by its words here, while the
8
8
  // picture itself stands on the layer of the author's demo, stretched over its area, the later part
9
- // over the earlier, and nothing stands there till its file is decoded (T61). This module is no
10
- // entry point of the package.
11
- import { onSheet } from "../core/example.js";
9
+ // over the earlier, and nothing stands there till its file is decoded (T61). A box of a workbook
10
+ // the demo fills is read as the box and its value, which the element writes in the box itself
11
+ // (T85). This module is no entry point of the package.
12
+ import { isFill, onSheet, } from "../core/example.js";
12
13
  import { SHEET } from "../core/sheet.js";
13
14
  import { placeOver, SVG, write } from "./attributes.js";
15
+ import { labelsFor } from "./labels.js";
14
16
  import { framed } from "./statement.js";
15
17
  /** The list of a demo's open parts and the region that tells of them, as the element shows them. */
16
18
  export class Parts {
@@ -37,6 +39,23 @@ export class Parts {
37
39
  #off = new WeakMap();
38
40
  /** The area of the sheet of an item whose picture stands there. */
39
41
  #areas = new WeakMap();
42
+ /** The open items of parts that fill a box, which a new language words anew. */
43
+ #fills = new Map();
44
+ /**
45
+ * What a part that fills a box says, and in what language: the box, in the labels' language, and
46
+ * its value. The element sets it with each example and as its language changes; `words` says the
47
+ * open items again in the new one.
48
+ */
49
+ #fillWords = ({ fill }) => {
50
+ const { lang, labels } = labelsFor(null);
51
+ return { number: `${labels.box} ${fill.line}.${fill.box}`, lang };
52
+ };
53
+ /** What a part that fills a box says from now on, the items already open too. */
54
+ set fillWords(words) {
55
+ this.#fillWords = words;
56
+ for (const [item, part] of this.#fills)
57
+ this.#sayFill(item, part);
58
+ }
40
59
  /** The list and the region, by their IDs, whose pictures the element's pictures give files. */
41
60
  constructor(ids, pictures) {
42
61
  this.#pictures = pictures;
@@ -116,12 +135,20 @@ export class Parts {
116
135
  const number = document.createElement("span");
117
136
  number.lang = told.lang;
118
137
  number.textContent = told.number;
138
+ const said = document.createElement("span");
139
+ if (isFill(part)) {
140
+ // A box and its value, in the labels' language, as its item says them.
141
+ const { number: words, lang } = this.#fillWords(part);
142
+ said.lang = lang;
143
+ said.textContent = words;
144
+ this.status.replaceChildren(number, " ", said);
145
+ return;
146
+ }
119
147
  const words = "text" in part ? part.text : part.alt;
120
148
  if (words === "") {
121
149
  this.status.replaceChildren(number);
122
150
  return;
123
151
  }
124
- const said = document.createElement("span");
125
152
  // The parts' language, which the list has, or none: then the element's own, as the list's.
126
153
  const lang = this.list.getAttribute("lang");
127
154
  if (lang !== null)
@@ -133,10 +160,18 @@ export class Parts {
133
160
  quiet() {
134
161
  this.status.replaceChildren();
135
162
  }
136
- /** An item leaving the list takes its picture off, once. */
163
+ /** An item of a part that fills a box says the box and its value, in the labels' language. */
164
+ #sayFill(item, part) {
165
+ const { number, lang } = this.#fillWords(part);
166
+ item.lang = lang;
167
+ item.textContent = number;
168
+ }
169
+ /** An item leaving the list takes its picture off, once, and is no fill's any more. */
137
170
  #takeOff(item) {
138
171
  this.#off.get(item)?.();
139
172
  this.#off.delete(item);
173
+ if (item instanceof HTMLLIElement)
174
+ this.#fills.delete(item);
140
175
  }
141
176
  /**
142
177
  * The item of an open part: a paragraph's text, a picture beside the sheet in its frame, or the
@@ -146,7 +181,11 @@ export class Parts {
146
181
  */
147
182
  #itemOf(part) {
148
183
  const item = document.createElement("li");
149
- if ("text" in part) {
184
+ if (isFill(part)) {
185
+ this.#fills.set(item, part);
186
+ this.#sayFill(item, part);
187
+ }
188
+ else if ("text" in part) {
150
189
  // Text, never markup: a module is data (R02 spec).
151
190
  item.textContent = part.text;
152
191
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@altnovatrail/canvas",
3
- "version": "0.2.3",
3
+ "version": "0.3.1",
4
4
  "description": "NovaTrail's canvases: the workplace of a task, where the learner writes, draws and types.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -26,12 +26,12 @@
26
26
  "./lib/element/index.js"
27
27
  ],
28
28
  "dependencies": {
29
- "@altnovatrail/spec": "0.9.9",
29
+ "@altnovatrail/spec": "0.12.0",
30
30
  "perfect-freehand": "1.2.3",
31
31
  "rbush": "4.0.1"
32
32
  },
33
33
  "devDependencies": {
34
- "@altnovatrail/contracts": "0.9.9",
34
+ "@altnovatrail/contracts": "0.12.0",
35
35
  "@biomejs/biome": "2.6.5",
36
36
  "@bufbuild/protobuf": "2.16.0",
37
37
  "@iconify-json/material-symbols": "1.2.97",