@openbooknetwork/hesap 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/package.json +1 -1
  2. package/src/client.css +168 -0
  3. package/src/families/counters/add-figures.js +85 -0
  4. package/src/families/counters/add-goals.js +82 -0
  5. package/src/families/counters/add-model.js +82 -0
  6. package/src/families/counters/add.js +442 -0
  7. package/src/families/counters/add.mjs +102 -0
  8. package/src/families/counters/bonds.js +51 -12
  9. package/src/families/counters/client.css +40 -0
  10. package/src/families/counters/client.js +2 -1
  11. package/src/families/counters/count.js +4 -5
  12. package/src/families/counters/counter.js +14 -0
  13. package/src/families/counters/figures.js +12 -4
  14. package/src/families/counters/goals.js +1 -9
  15. package/src/families/counters/i18n/en.yaml +89 -45
  16. package/src/families/counters/i18n/tr.yaml +56 -12
  17. package/src/families/counters/index.mjs +5 -3
  18. package/src/families/counters/match-model.js +10 -1
  19. package/src/families/counters/match.js +31 -11
  20. package/src/families/counters/match.mjs +7 -1
  21. package/src/families/line/client.css +118 -0
  22. package/src/families/line/client.js +12 -0
  23. package/src/families/line/figures.js +143 -0
  24. package/src/families/line/goals.js +168 -0
  25. package/src/families/line/i18n/en.yaml +61 -0
  26. package/src/families/line/i18n/tr.yaml +61 -0
  27. package/src/families/line/index.mjs +227 -0
  28. package/src/families/line/lens.js +49 -0
  29. package/src/families/line/line.js +624 -0
  30. package/src/families/line/model.js +219 -0
  31. package/src/families.mjs +2 -1
  32. package/src/i18n/en.yaml +158 -46
  33. package/src/i18n/tr.yaml +124 -12
  34. package/src/kit/i18n/en.yaml +8 -1
  35. package/src/kit/i18n/tr.yaml +7 -0
  36. package/src/kit/kit.css +9 -0
  37. package/src/kit/panel.js +2 -0
  38. package/src/kit/random.js +9 -0
  39. package/src/kit/written.js +70 -0
  40. package/src/node.mjs +3 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openbooknetwork/hesap",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "description": "Hesap: the maths engine of the Open Book Network, figures a reader can move with the numbers and formulas that follow them",
6
6
  "keywords": [
package/src/client.css CHANGED
@@ -88,6 +88,15 @@ aside.story .place[data-formula] .katex-display>.katex{text-align:start}
88
88
  .peek{bottom:calc(var(--hesap-panel,0px) + .571rem)}
89
89
  }
90
90
 
91
+ /* The written line of a calculation under a figure, such as 5 + 3 = 8. A number takes its group's colour through
92
+ the family's class, and the signs and the answer are ink. A link underlines the numbers it points to, and a link
93
+ to the whole line underlines the line. */
94
+ .hesap .hesap-written{font-size:34px;font-weight:700;white-space:pre;fill:var(--ink);stroke:none;text-anchor:middle;dominant-baseline:central;font-variant-numeric:tabular-nums;pointer-events:none}
95
+ .hesap .hesap-written.hesap-written-step{font-size:26px}
96
+ .hesap .hesap-written [data-ref]{stroke:none}
97
+ .hesap .hesap-written .hesap-written-num{fill:var(--c,var(--ink))}
98
+ .hesap .hesap-written .hesap-written-hot,.hesap .hesap-written.hesap-written-hot{text-decoration:underline}
99
+
91
100
  /* counters: the colours are theme tokens and the two counter colours of the family, so a theme switch needs no
92
101
  redraw. */
93
102
  .hesap-counters .counters-svg{position:absolute;left:0;top:0;width:100%;height:100%;touch-action:none;font-family:var(--body);overflow:visible}
@@ -158,6 +167,9 @@ aside.story .place[data-formula] .katex-display>.katex{text-align:start}
158
167
  .hesap-counters .counters-pin-stripe{fill:var(--z-counters-red,#d1453b)}
159
168
  .hesap-counters .counters-spot{fill:var(--rule)}
160
169
  .hesap-counters .counters-down .counters-pin-body{opacity:.45}
170
+ .hesap-counters .counters-ball{pointer-events:none}
171
+ .hesap-counters .counters-ball-shape{fill:var(--z-counters-blue,#2a6bd1);stroke:var(--ink);stroke-width:2}
172
+ .hesap-counters .counters-ball-hole{fill:var(--ink)}
161
173
  .hesap-counters .counters-pin:focus-visible .counters-hit{stroke:var(--accent);stroke-width:2.5}
162
174
 
163
175
  /* The car park: two rows of spaces, an aisle, and a sign that shows the free spaces. */
@@ -326,3 +338,159 @@ aside.story .place[data-formula] .katex-display>.katex{text-align:start}
326
338
  .hesap-counters .counters-line-card{fill:var(--panel);stroke:var(--c);stroke-width:2}
327
339
  .hesap-counters .counters-added .counters-ring{display:none}
328
340
  .hesap-counters .counters-plus{fill:none;stroke:var(--paper);stroke-width:4;stroke-linecap:round;pointer-events:none}
341
+
342
+ /* The add scene: the dice over the frames and the tray, and the counters a link lights. */
343
+ .hesap-counters .counters-die-box{fill:var(--paper);stroke:var(--ink);stroke-width:2.5}
344
+ .hesap-counters .counters-pip{fill:var(--ink);stroke:none}
345
+ .hesap-counters .counters-die.counters-hot{stroke:none}
346
+ .hesap-counters .counters-die.counters-hot .counters-die-box{stroke:var(--accent);stroke-width:4}
347
+ .hesap-counters .counters-lit .counters-body{stroke:var(--accent);stroke-width:4}
348
+ /* A counter taken away leaves a dashed ring in its cell. */
349
+ .hesap-counters .counters-gone{display:none}
350
+ .hesap-counters .counters-taken .counters-gone{display:inline}
351
+ .hesap-counters .counters-taken .counters-body,.hesap-counters .counters-taken .counters-ring{display:none}
352
+ .hesap-counters .counters-lit .counters-gone{stroke:var(--accent);stroke-width:3.5}
353
+ /* The jam shelf: jars on wooden boards, and two crates for the jars sold. A sold jar's lid takes its crate's colour. */
354
+ .hesap-counters .counters-jar-glass{fill:var(--paper);fill-opacity:.4;stroke:var(--ink-2);stroke-width:2}
355
+ .hesap-counters .counters-jam{fill:#e08a1e}
356
+ .hesap-counters .counters-jar-lid{fill:var(--c,var(--ink-2));stroke:var(--paper);stroke-width:1}
357
+ .hesap-counters .counters-lit .counters-jar-glass{stroke:var(--accent);stroke-width:3.5}
358
+ .hesap-counters .counters-shelf-board{fill:#b98a5a;stroke:#8a6238;stroke-width:2}
359
+ .hesap-counters rect.counters-crate.counters-press{fill:var(--panel);stroke:#8a6238;stroke-width:3}
360
+ .hesap-counters .counters-crate-name{font-size:16px;font-weight:700;fill:var(--ink-2)}
361
+ .hesap-counters .counters-crate-name.counters-hot{stroke:none;fill:var(--accent)}
362
+ /* The seed tray: cells of bare soil, grown seedlings and small sprouts. */
363
+ .hesap-counters .counters-seedtray{fill:#8a6238;stroke:#5c3f22;stroke-width:2.5}
364
+ .hesap-counters .counters-soil{fill:#6b4a2b;stroke:#5c3f22;stroke-width:1}
365
+ .hesap-counters .counters-plant-stem{fill:none;stroke:#3f9a4a;stroke-width:3;stroke-linecap:round}
366
+ /* The seed packet of the seed tray: a paper packet with a folded top, whose seeds turn into sprouts when sown. */
367
+ .hesap-counters .counters-packet-paper{fill:#efe2c4;stroke:#a88a55;stroke-width:2}
368
+ .hesap-counters .counters-packet-fold{stroke:#a88a55;stroke-width:2;stroke-dasharray:5 4}
369
+ .hesap-counters .counters-packet.counters-hot .counters-packet-paper{stroke:var(--accent);stroke-width:4}
370
+ .hesap-counters .counters-seed{display:none}
371
+ .hesap-counters .counters-in-packet .counters-seed{display:inline}
372
+ .hesap-counters .counters-in-packet .counters-body:not(.counters-seed),.hesap-counters .counters-in-packet .counters-plant-stem{display:none}
373
+ /* The link pairs lights each paired counter with a filled halo of the accent, which stays plain on a small phone
374
+ figure and differs from the ink ring of a counter without a partner. */
375
+ .hesap-counters .counters-paired-lit .counters-hit{fill:var(--accent);fill-opacity:.6;stroke:var(--accent);stroke-width:4}
376
+ .hesap-counters .counters-paired-lit .counters-body{stroke:var(--paper);stroke-width:2.5}
377
+ .hesap-counters .counters-wire.counters-hot{stroke:var(--accent);stroke-width:5}
378
+
379
+ /* line: the colours are theme tokens and the two point colours of the family, so a theme switch needs no redraw. */
380
+ .hesap-line .line-svg{position:absolute;left:0;top:0;width:100%;height:100%;touch-action:none;font-family:var(--body);overflow:hidden}
381
+ .hesap-line text{fill:var(--ink);text-anchor:middle;dominant-baseline:central;pointer-events:none;font-variant-numeric:tabular-nums}
382
+ .hesap-line .line-c-point{--c:var(--z-line-point,#d1453b)}
383
+ .hesap-line .line-c-second{--c:var(--z-line-second,#2a6bd1)}
384
+ /* The flag of the number to find is ink, apart from both points' colours. */
385
+ .hesap-line .line-c-find{--c:var(--ink)}
386
+ .hesap-line .line-c-find .line-flag-text{fill:var(--paper)}
387
+
388
+ /* The line, its marks and their labels. */
389
+ .hesap-line .line-axis{stroke:var(--ink);stroke-width:2.5;stroke-linecap:round}
390
+ .hesap-line .line-mark{stroke:var(--ink);stroke-width:2;stroke-linecap:round}
391
+ .hesap-line .line-short{stroke:var(--ink-2);stroke-width:1.5}
392
+ .hesap-line .line-label{font-weight:400;font-variant-numeric:proportional-nums}
393
+ .hesap-line .line-label.line-lit{font-weight:700;fill:var(--accent)}
394
+ .hesap-line .line-guide{stroke:var(--ink-2);stroke-width:1;stroke-dasharray:2 4;opacity:.6}
395
+
396
+ /* A point is a disc or a square with a touch target round it, and its flag carries its number. */
397
+ .hesap-line .line-hit{fill:transparent}
398
+ .hesap-line .line-point{cursor:grab;outline:none}
399
+ .hesap-line .line-point.drag{cursor:grabbing}
400
+ .hesap-line .line-dot{fill:var(--c);stroke:var(--paper);stroke-width:2}
401
+ .hesap-line .line-point:focus-visible .line-hit{stroke:var(--accent);stroke-width:2.5}
402
+ .hesap-line .line-pole{stroke:var(--c);stroke-width:2}
403
+ .hesap-line .line-flag-box{fill:var(--c);stroke:var(--paper);stroke-width:1.5}
404
+ .hesap-line .line-flag-text{font-size:14px;font-weight:700;fill:#fff}
405
+
406
+ /* The jumps are arcs over the gaps they cross, with a head that shows their way. */
407
+ .hesap-line .line-arc{fill:none;stroke:var(--accent);stroke-width:2.5;stroke-linecap:round}
408
+ .hesap-line .line-arc-head{fill:var(--accent)}
409
+
410
+ /* The lens shows the marks around the point at full size, above a line whose gaps are too narrow to read. */
411
+ .hesap-line .line-lens{pointer-events:none}
412
+ .hesap-line .line-lens-box{fill:var(--panel);stroke:var(--ink-2);stroke-width:1.5}
413
+ .hesap-line .line-lens-tip{fill:var(--ink-2)}
414
+
415
+ /* The bracket over the tens round the point. */
416
+ .hesap-line .line-bracket{fill:none;stroke:var(--accent);stroke-width:2.5;stroke-linejoin:round;stroke-linecap:round}
417
+
418
+ /* A gap opens under the line as a line of its own. */
419
+ .hesap-line .line-gap{fill:transparent;cursor:pointer;outline:none}
420
+ .hesap-line .line-gap:hover,.hesap-line .line-gap[aria-pressed="true"]{fill:var(--accent);fill-opacity:.08}
421
+ .hesap-line .line-gap:focus-visible{stroke:var(--accent);stroke-width:2}
422
+ .hesap-line .line-open-gap{stroke:var(--accent);stroke-width:5;stroke-linecap:round}
423
+ .hesap-line .line-funnel{stroke:var(--ink-2);stroke-width:1;stroke-dasharray:3 3}
424
+
425
+ /* Blank label boxes and the label cards that fill them. */
426
+ .hesap-line .line-blank{fill:var(--paper);stroke:var(--ink-2);stroke-width:1.5;stroke-dasharray:4 3}
427
+ .hesap-line .line-card{cursor:grab;outline:none}
428
+ .hesap-line .line-card.drag{cursor:grabbing}
429
+ .hesap-line .line-card-box{fill:var(--panel);stroke:var(--ink);stroke-width:1.5}
430
+ .hesap-line .line-card-num{font-size:16px;font-weight:700}
431
+
432
+ /* The middle's long mark. */
433
+ .hesap-line .line-middle{stroke:var(--accent);stroke-width:2.5}
434
+
435
+ /* The street: a road, a house at each mark, door numbers on plaques, and the van as the point. */
436
+ .hesap-line .line-road{fill:color-mix(in srgb,var(--ink) 22%,var(--paper))}
437
+ .hesap-line .line-house-body{fill:color-mix(in srgb,#d99a1f 30%,var(--paper));stroke:var(--ink-2);stroke-width:1}
438
+ .hesap-line .line-door{fill:var(--ink-2)}
439
+ .hesap-line .line-plaque rect{fill:var(--paper);stroke:var(--ink);stroke-width:1.2}
440
+ .hesap-line .line-plaque text{font-weight:700}
441
+ .hesap-line .line-wheel{fill:var(--ink)}
442
+ .hesap-line .line-van-tip{fill:var(--c)}
443
+ .hesap-line .line-van-num{font-size:10px;font-weight:700;fill:#fff}
444
+ .hesap-line .line-card:focus-visible .line-card-box{stroke:var(--accent);stroke-width:3}
445
+
446
+ /* The estimate mark is pale, apart from the point and the marks. */
447
+ .hesap-line .line-estimate line{stroke:var(--accent);stroke-width:3;stroke-linecap:round;opacity:.55;stroke-dasharray:4 3}
448
+ .hesap-line .line-estimate path{fill:var(--accent);opacity:.55}
449
+ .hesap-line .line-fixed .line-dot{fill:var(--c);stroke:var(--paper);stroke-width:2}
450
+
451
+ /* The book: the edge of its pages between its covers, and the bookmark. */
452
+ .hesap-line .line-book-pages{fill:color-mix(in srgb,#f3e6c4 80%,var(--paper));stroke:var(--ink-2);stroke-width:1}
453
+ .hesap-line .line-book-leaf{stroke:var(--ink-2);stroke-width:.6;opacity:.5}
454
+ .hesap-line .line-book-cover{fill:color-mix(in srgb,#2a6bd1 60%,var(--ink))}
455
+
456
+ /* The touch band of a mark that the reader drags into place. */
457
+ .hesap-line .line-band{fill:transparent}
458
+ .hesap-line .line-handle{cursor:ew-resize;outline:none}
459
+ .hesap-line .line-mark-key{outline:none}
460
+ .hesap-line .line-handle.line-focus .line-band{stroke:var(--accent);stroke-width:2;fill:var(--accent);fill-opacity:.06}
461
+
462
+ /* The start of the jumps: a ring round its mark, and the point that moves by the buttons alone inside it. */
463
+ .hesap-line .line-ring{fill:none;stroke:var(--c);stroke-width:3}
464
+ .hesap-line .line-start.line-moves{cursor:grab;outline:none}
465
+ .hesap-line .line-start:focus-visible .line-hit{stroke:var(--accent);stroke-width:2.5}
466
+ .hesap-line .line-start.line-hot .line-ring{stroke-width:5}
467
+ .hesap-line .line-fixed{pointer-events:none}
468
+ .hesap-line .line-chip-wide{flex-basis:100%}
469
+
470
+ /* The words under the line. */
471
+ .hesap-line .line-reading{font-size:20px;font-weight:700}
472
+
473
+ /* The pond of the opening pages: the water, the bank, the stones and the frog. */
474
+ .hesap-line .line-water{fill:color-mix(in srgb,#2a8bd1 20%,var(--paper));stroke:none}
475
+ .hesap-line .line-bank{fill:color-mix(in srgb,#4a9d4f 40%,var(--paper));stroke:color-mix(in srgb,#4a9d4f 70%,var(--paper));stroke-width:2}
476
+ .hesap-line .line-stone-body{fill:color-mix(in srgb,var(--ink) 16%,var(--paper));stroke:var(--ink-2);stroke-width:1.5}
477
+ .hesap-line .line-stone-num{font-weight:700}
478
+ .hesap-line .line-frog-body,.hesap-line .line-frog-leg{fill:#3f9a46;stroke:#24622a;stroke-width:1.5}
479
+ .hesap-line .line-frog-eye{fill:#fff;stroke:#24622a;stroke-width:1.5}
480
+ .hesap-line .line-frog-pupil{fill:#1d1d1d}
481
+ .hesap-line .line-frog-mouth{fill:none;stroke:#24622a;stroke-width:1.5;stroke-linecap:round}
482
+
483
+ /* A part the text points at stands out. */
484
+ .hesap-line .line-hot .line-dot,.hesap-line .line-hot .line-flag-box{stroke:var(--accent);stroke-width:3}
485
+ .hesap-line .line-arcs.line-hot .line-arc{stroke-width:4}
486
+ .hesap-line .line-reading.line-hot{fill:var(--accent)}
487
+ .hesap-line .line-hot .line-frog-body{stroke:var(--accent);stroke-width:3}
488
+
489
+ /* The jump buttons keep to one row and show their sign large beside their name. */
490
+ .hesap-line .line-jumps{display:inline-flex;flex-wrap:nowrap;gap:.429rem}
491
+ .hesap-line .line-jump b{font-size:1.286rem;line-height:1}
492
+
493
+ @media (max-width:900px){
494
+ /* The figure ends above the peek card, which covers the foot of the stage until the reader closes it. */
495
+ .hesap-line .line-svg{height:calc(100% - var(--hesap-peek,0px))}
496
+ }
@@ -0,0 +1,85 @@
1
+ // The parts of an add figure besides the ten frames: the tray where counters of the second group wait, a die, and the
2
+ // shelf and the crates of the jam shelf.
3
+ import { svg as el } from '../../kit/dom.js';
4
+ import { CELL } from './frame.js';
5
+
6
+ const EDGE = 8; // The border of a tray, a shelf or a crate round its objects.
7
+
8
+ // waitTray gives the size of a tray that holds n counters in rows of up to five, two rows while they fit, and draws
9
+ // it at a corner with the place of each counter. A packet is the paper seed packet of the seed tray, with a folded top.
10
+ export function waitTray(n, packet = false) {
11
+ const cols = Math.max(1, Math.min(5, Math.ceil(n / 2))), rows = Math.max(1, Math.ceil(n / cols));
12
+ const w = cols * CELL + 2 * EDGE, h = rows * CELL + 2 * EDGE;
13
+ return { w, h, draw(x, y) {
14
+ const box = packet ? el('g', { class: 'counters-packet', 'data-ref': 'waiting' }, [el('rect', { class: 'counters-packet-paper', x, y: y - 14, width: w, height: h + 14, rx: 4 }),
15
+ el('path', { class: 'counters-packet-fold', d: `M ${x} ${y - 4} H ${x + w}` })])
16
+ : el('rect', { class: 'counters-tray', 'data-ref': 'waiting', x, y, width: w, height: h, rx: 10 });
17
+ const pos = i => ({ x: x + EDGE + (i % cols + 0.5) * CELL, y: y + EDGE + (Math.floor(i / cols) + 0.5) * CELL });
18
+ return { box, pos, area: { x, y, w, h } };
19
+ } };
20
+ }
21
+
22
+ // The dots of each face of a die, in steps of a third of the face from its middle.
23
+ const PIPS = { 1: [[0, 0]], 2: [[-1, -1], [1, 1]], 3: [[-1, -1], [0, 0], [1, 1]], 4: [[-1, -1], [1, -1], [-1, 1], [1, 1]],
24
+ 5: [[-1, -1], [1, -1], [0, 0], [-1, 1], [1, 1]], 6: [[-1, -1], [1, -1], [-1, 0], [1, 0], [-1, 1], [1, 1]] };
25
+ export const DIE_SIZE = 44;
26
+
27
+ // die draws a die with its middle at 0, 0 of its own group. set turns it to a face and says whether the face changed.
28
+ export function die(ref) {
29
+ const s = DIE_SIZE, dots = el('g'), pop = el('g', { class: 'a' }, [el('rect', { class: 'counters-die-box', x: -s / 2, y: -s / 2, width: s, height: s, rx: 9 }), dots]);
30
+ const g = el('g', { class: 'counters-die', 'data-ref': ref, role: 'img' }, [pop]);
31
+ let face = null;
32
+ return {
33
+ g, pop,
34
+ set(n, label) {
35
+ g.setAttribute('aria-label', label);
36
+ if (face === n) return false;
37
+ face = n;
38
+ dots.replaceChildren(...PIPS[n].map(([i, j]) => el('circle', { class: 'counters-pip', cx: i * s * 0.27, cy: j * s * 0.27, r: s * 0.09 })));
39
+ return true;
40
+ },
41
+ };
42
+ }
43
+
44
+ // The shelf and its crates: a shop shelf holds a jar in each of its places, on one board or on two boards of five,
45
+ // and two crates under it hold the jars sold, each in rows of five.
46
+ const SLOT = 44, JAR_ROW = 48, BOARD = 10, LABEL = 22;
47
+ export function shelfWays(n) {
48
+ return [n, Math.ceil(n / 2)].map(cols => {
49
+ const rows = Math.ceil(n / cols), w = cols * SLOT + 2 * EDGE, h = rows * (JAR_ROW + BOARD) + (rows - 1) * EDGE;
50
+ return { w, h, draw(x, y) {
51
+ const boards = Array.from({ length: rows }, (_, r) => el('rect', { class: 'counters-shelf-board', 'data-ref': 'left', x, y: y + r * (JAR_ROW + BOARD + EDGE) + JAR_ROW, width: w, height: BOARD, rx: 3 }));
52
+ const pos = i => ({ x: x + EDGE + (i % cols + 0.5) * SLOT, y: y + Math.floor(i / cols) * (JAR_ROW + BOARD + EDGE) + JAR_ROW / 2 + 2 });
53
+ // A tap anywhere on the shelf sells a jar, so a phone needs no aim at a single jar.
54
+ const area = el('rect', { class: 'counters-press', x, y, width: w, height: h });
55
+ return { nodes: [area, ...boards], pos, area };
56
+ } };
57
+ });
58
+ }
59
+ // crate gives the ways to draw a crate named label that holds n jars, in rows of five or in rows of three for a
60
+ // narrow stage, with its name above it.
61
+ export function crate(n, label, ref) {
62
+ return [5, 3].map(cols => {
63
+ const rows = Math.ceil(n / cols), w = cols * SLOT + 2 * EDGE, h = LABEL + rows * JAR_ROW + 2 * EDGE;
64
+ return { w, h, draw(x, y) {
65
+ const name = el('text', { class: 'counters-crate-name', 'data-ref': ref, x: x + w / 2, y: y + LABEL / 2 - 2 });
66
+ name.textContent = label;
67
+ const box = el('rect', { class: 'counters-crate counters-press', 'data-ref': ref, x, y: y + LABEL, width: w, height: h - LABEL, rx: 6 });
68
+ const pos = i => ({ x: x + EDGE + (i % cols + 0.5) * SLOT, y: y + LABEL + EDGE + (Math.floor(i / cols) + 0.5) * JAR_ROW });
69
+ return { nodes: [box, name], pos, box };
70
+ } };
71
+ });
72
+ }
73
+ // A jar takes a touch in the whole of its place, a slot wide and a row of jars high.
74
+ export const JAR_HIT = { w: SLOT, h: JAR_ROW };
75
+
76
+ // seedTray draws a seed tray of two rows of ten cells, each cell of bare soil, and gives the middle of every cell,
77
+ // the first row first.
78
+ export function seedTray() {
79
+ const w = 10 * CELL + 2 * EDGE, h = 2 * CELL + 2 * EDGE;
80
+ return { w, h, draw(x, y) {
81
+ const cells = Array.from({ length: 20 }, (_, i) => ({ x: x + EDGE + (i % 10 + 0.5) * CELL, y: y + EDGE + (Math.floor(i / 10) + 0.5) * CELL }));
82
+ const soil = cells.map(c => el('rect', { class: 'counters-soil', x: c.x - CELL / 2 + 2, y: c.y - CELL / 2 + 2, width: CELL - 4, height: CELL - 4, rx: 4 }));
83
+ return { nodes: [el('rect', { class: 'counters-seedtray', x, y, width: w, height: h, rx: 8 }), ...soil], cells, box: { x, y, w, h } };
84
+ } };
85
+ }
@@ -0,0 +1,82 @@
1
+ // The goals a counters/add page can name. The page declares ranges, and the goal draws a target from them each time
2
+ // the page opens and each time the reader asks for another one. The build and the browser both use this file.
3
+ import { PLUS, MINUS, answerOf } from '../../kit/written.js';
4
+
5
+ export const SUM_STEP = [0, 20]; // The stepper Sum of a find-sum page, as far as the two frames hold.
6
+ export const PART_STEP = [0, 9]; // The steppers Makes ten and The rest of a steps-to-ten page.
7
+ // The widest ranges a page may draw from. A first number reaches past the first ten, and the second joins after it
8
+ // within the two frames.
9
+ const FIRSTS = [1, 19], PARTS = [1, 10], SUMS = [1, 20], SECONDS = [2, 9];
10
+ const span = (lo, hi) => Array.from({ length: Math.max(0, hi - lo + 1) }, (_, i) => lo + i);
11
+ const range = (r, [lo, hi]) => Array.isArray(r) && r.length === 2 && r.every(Number.isInteger) && r[0] <= r[1] && r[0] >= lo && r[1] <= hi;
12
+ const extra = (c, names) => { const f = Object.keys(c).find(k => k !== 'goal' && !names.includes(k)); return f ? `${f} belongs to another goal, this one draws from ${names.join(' and ')}` : null; };
13
+ const words = r => JSON.stringify(r ?? null);
14
+ const stepper = ([lo, hi]) => ({ kind: 'stepper', lo, hi });
15
+
16
+ // The additions a find-sum page can draw: every first and second number of their ranges whose sum lies in its range.
17
+ const sums = c => span(...c.first).flatMap(a => span(...c.second).map(b => ({ a, b }))).filter(t => t.a + t.b >= c.sum[0] && t.a + t.b <= c.sum[1]);
18
+ // The calculations a steps-to-ten page can draw, of one kind: an addition from 1 to 9 whose sum lies from 11 to 18, or
19
+ // a subtraction from 11 to 18 that leaves 1 to 9. Each crosses ten.
20
+ const crossing = (c, sign) => sign === PLUS
21
+ ? span(1, 9).flatMap(a => span(...c.second).map(b => ({ a, sign, b }))).filter(t => t.a + t.b >= 11 && t.a + t.b <= 18)
22
+ : span(11, 18).flatMap(a => span(...c.second).map(b => ({ a, sign, b }))).filter(t => t.a - t.b >= 1 && t.a - t.b <= 9);
23
+ // toTenOf gives the part of the second number that takes the first number to 10.
24
+ export const toTenOf = t => t.sign === PLUS ? 10 - t.a : t.a - 10;
25
+
26
+ export const ADD_GOALS = {
27
+ // The page writes a drawn addition open, such as 4 + 3, over empty frames. The reader sets its sum on the stepper
28
+ // Sum and presses Check, and the two groups join in the frames once the sum is right.
29
+ 'find-sum': {
30
+ scenes: ['add'],
31
+ problem(c) {
32
+ const stop = extra(c, ['first', 'second', 'sum']);
33
+ if (stop) return stop;
34
+ for (const [k, [lo, hi], eg] of [['first', FIRSTS, '[9, 15]'], ['second', PARTS, '[2, 4]']]) if (!range(c[k], [lo, hi])) return `${k} is ${words(c[k])}, write the range of the ${k} number as [low, high] from ${lo} to ${hi}, such as ${eg}`;
35
+ if (!range(c.sum, SUMS)) return `sum is ${words(c.sum)}, write the range of the sum as [low, high] from ${SUMS[0]} to ${SUMS[1]}, such as [11, 19], since the stepper Sum opens at 0`;
36
+ return sums(c).length ? null : 'first, second and sum leave no addition to draw, widen one of them';
37
+ },
38
+ draw: (c, spec, dice) => { const all = sums(c); return all.length ? dice.pick(all) : null; },
39
+ page: () => ({ first: 0, second: 0, sumCtl: stepper(SUM_STEP) }),
40
+ holds(state, spec, c, t) {
41
+ if (!state.checked) return { done: false };
42
+ const sum = t.a + t.b;
43
+ return state.sum === sum ? { done: true } : { done: false, note: state.sum > sum ? 'less' : 'greater' };
44
+ },
45
+ reach: (spec, c, t) => t.a + t.b > SUM_STEP[1] ? `the stepper Sum stops at ${SUM_STEP[1]}, below ${t.a + t.b}` : null,
46
+ // The line the page writes as it opens, without its answer.
47
+ line: (t, done) => ({ a: t.a, sign: PLUS, b: t.b, open: !done }),
48
+ words: (c, t, f) => ({ a: f.int(t.a), b: f.int(t.b), sum: f.int(t.a + t.b) }),
49
+ },
50
+ // The page writes a drawn addition or subtraction across ten open, such as 8 + 5 or 15 − 8, over its counters. The
51
+ // reader splits the second number on the steppers Makes ten and The rest and presses Check, and the counters move in
52
+ // the two steps once the split is right.
53
+ 'steps-to-ten': {
54
+ scenes: ['add'],
55
+ problem(c) {
56
+ const stop = extra(c, ['second']);
57
+ if (stop) return stop;
58
+ if (!range(c.second, SECONDS)) return `second is ${words(c.second)}, write the range of the second number as [low, high] from ${SECONDS[0]} to ${SECONDS[1]}, such as [3, 9]`;
59
+ for (const [sign, kind] of [[PLUS, 'addition'], [MINUS, 'subtraction']]) if (!crossing(c, sign).length) return `second is ${words(c.second)}, which leaves no ${kind} across ten to draw, widen it`;
60
+ return null;
61
+ },
62
+ // An addition or a subtraction each half the time, then one calculation of that kind.
63
+ draw: (c, spec, dice) => { const all = crossing(c, dice.pick([PLUS, MINUS])); return all.length ? dice.pick(all) : null; },
64
+ page: t => ({ first: t.a, second: 0, ...(t.sign === PLUS ? { waiting: t.b } : { take: true }), toTenCtl: stepper(PART_STEP), restCtl: stepper(PART_STEP) }),
65
+ // The reader's Makes ten is checked first, then the two parts against the second number.
66
+ holds(state, spec, c, t) {
67
+ if (!state.checked) return { done: false };
68
+ const toTen = toTenOf(t);
69
+ if (state.toTen !== toTen) return { done: false, note: state.toTen > toTen ? 'past' : 'short' };
70
+ const both = state.toTen + state.rest;
71
+ return both === t.b ? { done: true } : { done: false, note: both > t.b ? 'more' : 'less' };
72
+ },
73
+ reach: (spec, c, t) => toTenOf(t) > PART_STEP[1] || t.b - toTenOf(t) > PART_STEP[1] ? `the steppers stop at ${PART_STEP[1]}` : null,
74
+ line: (t, done) => ({ a: t.a, sign: t.sign, b: t.b, open: !done }),
75
+ // The two steps of the drawn calculation: to 10, then from 10.
76
+ steps: t => [{ a: t.a, sign: t.sign, b: toTenOf(t) }, { a: 10, sign: t.sign, b: t.b - toTenOf(t) }],
77
+ words: (c, t, f) => ({ first: f.int(t.a), second: f.int(t.b), sum: f.int(answerOf(t)) }),
78
+ },
79
+ };
80
+
81
+ // The reader's state as a challenge opens: every stepper at 0 and nothing checked.
82
+ export const addStart = () => ({ sum: 0, toTen: 0, rest: 0, checked: false });
@@ -0,0 +1,82 @@
1
+ // What a counters/add page holds and what it counts: two groups of counters in two ten frames, or counters taken
2
+ // away from one group, or the jars of a shop shelf, and the written lines of their calculation. The build and the
3
+ // browser both use this file, so a page's text and its live values agree.
4
+ import { PLUS, MINUS } from '../../kit/written.js';
5
+
6
+ // frames is two ten frames, shelf a shop shelf of jars of jam with two crates under it, and tray a seed tray of two
7
+ // rows of ten that fills as the frames do, the first row first.
8
+ export const ADD_LOOKS = ['frames', 'shelf', 'tray'];
9
+ // The two ten frames hold this many counters, and every counter of a page fits in them.
10
+ export const CELLS = 20;
11
+ // The shelf holds this many jars.
12
+ export const JARS = 10;
13
+ // A die shows a number from 1 to 6.
14
+ export const DIE = [1, 6];
15
+
16
+ const shelf = spec => spec.look === 'shelf';
17
+ // A seed tray with waiting has a packet of seeds beside it, and its written line writes the whole packet.
18
+ export const packet = spec => spec.look === 'tray' && spec.waiting != null;
19
+
20
+ // opening gives the counts as the page opens. first is the first group, or every counter of a take-away page, or
21
+ // the jars sold in the morning. second is the second group in the frames, the counters taken away, or the jars sold
22
+ // in the afternoon. waiting is the counters of the second group in the tray.
23
+ // ten says that the figure shows the full ten: on an addition the first frame is full, and on a take away every
24
+ // counter of the second frame is taken. The counters open in order, so they show it as the page opens.
25
+ export const opening = spec => ({ first: spec.first, second: spec.second, waiting: spec.waiting ?? 0, ten: true });
26
+
27
+ // stepsOf gives the two steps of making ten while the calculation crosses ten and the figure shows the ten: to-ten
28
+ // fills the ten or takes the first number down to it, and rest is the rest of the second number. It gives null
29
+ // otherwise, and on a page without written: ten.
30
+ export function stepsOf(spec, s) {
31
+ if (spec.written !== 'ten' || !s.ten || (packet(spec) && s.waiting > 0)) return null;
32
+ if (spec.take) {
33
+ const left = s.first - s.second;
34
+ if (s.first <= 10 || left >= 10) return null;
35
+ const toTen = s.first - 10, rest = 10 - left;
36
+ return { toTen, rest, lines: [{ a: s.first, sign: MINUS, b: toTen }, { a: 10, sign: MINUS, b: rest }] };
37
+ }
38
+ const sum = s.first + s.second;
39
+ if (s.first >= 10 || sum <= 10) return null;
40
+ const toTen = 10 - s.first, rest = sum - 10;
41
+ return { toTen, rest, lines: [{ a: s.first, sign: PLUS, b: toTen }, { a: 10, sign: PLUS, b: rest }] };
42
+ }
43
+
44
+ // counts gives every number a page can show. A take-away page leaves first minus second, and the shelf leaves the
45
+ // jars that are not sold.
46
+ export function counts(spec, s) {
47
+ const sum = s.first + s.second;
48
+ const steps = stepsOf(spec, s);
49
+ return { first: s.first, second: s.second, waiting: s.waiting, sum, left: spec.take ? s.first - s.second : shelf(spec) ? JARS - sum : null,
50
+ 'to-ten': steps?.toTen ?? null, rest: steps?.rest ?? null };
51
+ }
52
+
53
+ // lineOf gives the written line of the counters: first + second, or first − second on a take-away page. The line of
54
+ // a seed packet adds the seeds sown and in the packet, and takes its answer once the packet is empty.
55
+ export const lineOf = (spec, s) => packet(spec) ? { a: s.first, sign: PLUS, b: s.second + s.waiting, open: s.waiting > 0 }
56
+ : { a: s.first, sign: spec.take ? MINUS : PLUS, b: s.second };
57
+ // leftOf gives the shelf's line of the jars left: the jars of a full shelf minus the jars sold.
58
+ export const leftOf = (spec, s) => ({ a: JARS, sign: MINUS, b: s.first + s.second });
59
+ // The lines a link reads, by its id: the written line, its words, and on the shelf the line of the jars left.
60
+ // A step line gives null while the steps do not show.
61
+ const step = k => (spec, s) => stepsOf(spec, s)?.lines[k] ?? null;
62
+ export const LINES = { written: lineOf, reading: lineOf, 'written-left': leftOf, 'reading-left': leftOf,
63
+ 'written-to-ten': step(0), 'reading-to-ten': step(0), 'written-rest': step(1), 'reading-rest': step(1) };
64
+
65
+ // The measures a page can read out and link in its text. The written lines and their words come with written.
66
+ export function measuresOf(spec) {
67
+ if (spec.challenge) return [];
68
+ const lines = spec.written ? ['written', 'reading'] : [];
69
+ if (spec.written === 'ten') lines.push('to-ten', 'rest', 'written-to-ten', 'reading-to-ten', 'written-rest', 'reading-rest');
70
+ if (shelf(spec)) return ['first', 'second', 'sum', 'left', ...lines, ...(spec.written ? ['written-left', 'reading-left'] : [])];
71
+ if (spec.take) return ['first', 'second', 'left', ...lines];
72
+ return ['first', 'second', 'sum', ...(spec.waiting != null ? ['waiting'] : []), ...lines];
73
+ }
74
+
75
+ // The context of a page picks the names of its readouts and steppers, such as "Taken away" for second.
76
+ export const contextOf = spec => shelf(spec) ? 'shelf' : spec.take ? 'take' : 'add';
77
+ // labelKeys gives the strings that may name a readout or a stepper, the page's own first, so to-ten reads "Makes ten"
78
+ // on an addition and "Down to ten" on a take away. A dice page comes before the word sum, so its count has a plain name.
79
+ export const labelKeys = (spec, id) => [...(spec.dice ? [`add-dice-${id}`] : []), `add-${contextOf(spec)}-${id}`, `add-${id}`];
80
+ // goalKey gives a challenge's string for its kind of calculation: a subtraction of steps-to-ten has its own goal
81
+ // line and notes, which name Down to ten, and every other draw takes the plain key.
82
+ export const goalKey = (key, t, has) => t?.sign === '−' && has(`${key}-minus`) ? `${key}-minus` : key;