@openbooknetwork/hesap 0.3.1 → 0.3.3

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.
@@ -3,7 +3,7 @@
3
3
  // round the point, gaps that open into lines of their own, and label cards for blank marks. The looks stones and
4
4
  // stones-on-line draw the frog's stepping stones of the opening pages. A challenge page asks the reader to put the
5
5
  // point on a number.
6
- import { opening, jump, place, pick, open, lay, show as reveal, next, toggleMiddle, estimate as setEstimate, moveStart, writtenOf, isFree, numbered, standing, posOf, slide, labelKeep, settle as settleMarks, gapsKey, sides, SETTLE, arcsOf, jumpsOf, counts, readoutsOf, measuresOf, readingOf, tensOf, gapsOf, numberAt, markOf, labelled, findsOf, shuffled, isStones, gapOn, labelRows, secondTo, drawLine } from './model.js';
6
+ import { opening, jump, place, pick, open, lay, show as reveal, next, toggleMiddle, estimate as setEstimate, moveStart, resize, writtenOf, backOf, isFree, numbered, standing, posOf, slide, labelKeep, settle as settleMarks, gapsKey, sides, SETTLE, arcsOf, jumpsOf, counts, readoutsOf, measuresOf, readingOf, tensOf, gapsOf, numberAt, markOf, labelled, findsOf, shuffled, isStones, gapOn, labelRows, secondTo, drawLine } from './model.js';
7
7
  import { GOALS } from './goals.js';
8
8
  import { pointShape, flag, arc, frog, stone, pond, bracket as makeBracket, blankBox, card, house, plaque, van, book, bookmark, BOX, CARD } from './figures.js';
9
9
  import { lens as makeLens, LENS_H } from './lens.js';
@@ -25,6 +25,7 @@ const STILL = 4; // A press that moves less than this many pixels leaves the poi
25
25
  const POLE = 16, FLAG = 24, STACK = 30; // A flag's pole, its box, and how far a second flag over the same place rises.
26
26
  const LONG = 11, SHORT = 6; // Half the height of a long mark and of a short mark.
27
27
  const BOOK_TOP = 34; // The bookmark stands this far above the edge of the book.
28
+ const ARC_Y = -6; // The arcs over the line start this far above it.
28
29
  const WHO = ['point', 'second'];
29
30
  const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));
30
31
 
@@ -65,8 +66,10 @@ export default function line({ host, stage, panel, T, lang, moving, motion }) {
65
66
  // A blank box at an end of the line keeps its whole width on the stage.
66
67
  const gap = gapOn(W - (spec.blank.length ? BOX.w : 0) - (street() ? 8 : 0), n, stones), x0 = (W - n * gap) / 2;
67
68
  const big = gap >= 34, r = big ? 11 : 9, arcH = clamp(gap * 0.42, Math.min(10, gap * 1.5), 26);
69
+ // A jump over several gaps rises higher than a jump over one, so its arc does not lie flat along the line.
70
+ const rise = len => len <= 1 ? arcH : Math.min(arcH * (1 + 0.35 * (len - 1)), arcH * 2.5, 48);
68
71
  const sw = Math.min(gap * 0.86, 58), fs = Math.min(Math.max(gap * 0.9, 26), 46);
69
- let top, bottom, g = { gap, x0, r, arcH, sw, fs, n, x: k => x0 + k * gap, narrow: gap < (spec.look === 'street' ? NARROW_STREET : NARROW) && onLine && !isFree(spec), font: big ? 16 : 14, rows: 1 };
72
+ let top, bottom, g = { gap, x0, r, arcH, rise, sw, fs, n, x: k => x0 + k * gap, narrow: gap < (spec.look === 'street' ? NARROW_STREET : NARROW) && onLine && !isFree(spec), font: big ? 16 : 14, rows: 1 };
70
73
  // A house of the street is about as wide as its gap, and a flag stands over its door number.
71
74
  g.houseH = clamp(gap * 1.1, 12, 36); g.flagFrom = street() ? -(g.houseH + 30) : spec.look === 'book' ? -BOOK_TOP : -r;
72
75
  if (!onLine) {
@@ -79,12 +82,18 @@ export default function line({ host, stage, panel, T, lang, moving, motion }) {
79
82
  const spacing = gap * Math.min(n, ...ks.slice(1).map((k, i) => k - ks[i]));
80
83
  Object.assign(g, labelRows(wide(ks.map(k => int(num(k)))), spacing, { base: g.font, least: FONT, apart: APART }));
81
84
  g.row = new Map(ks.map((k, i) => [k, g.rows === 2 ? i % 2 : 0]));
82
- g.labelY = 26 + g.font / 2; g.arcY = -6;
85
+ g.labelY = 26 + g.font / 2;
86
+ // Every jump of an undo page starts or ends at the start, so its arcs over the line stand on the top of the
87
+ // start's ring, which reaches r + 7 above the line, where a short arc would hide inside the ring.
88
+ g.arcY = spec.undo ? -(r + 7 + 2) : ARC_Y;
83
89
  if (street()) g.labelY = -(g.houseH + 16);
84
90
  let y = street() ? 34 : Math.max(g.labelY + g.font / 2 + (g.rows === 2 ? g.font + 3 : 0), spec.blank.length ? g.labelY + BOX.h / 2 : 0) + 6;
85
- if (spec.jump) { g.downY = y + 6; y = g.downY + g.arcH + 6; }
91
+ const most = rise(spec.sizeCtl?.hi ?? spec.size ?? 1);
92
+ if (spec.jump) { g.downY = y + 6; y = g.downY + most + 6; }
86
93
  if (spec.reading || spec.moved) { g.readingY = y + 22; y += 46; }
87
94
  if (spec.written) { g.writtenY = y + 32; y += 64; }
95
+ // The line of the jump back stands under the line of the first jump.
96
+ if (spec.undo) { g.backY = y + 12; y += 44; }
88
97
  if (spec.open) { g.openTop = y; g.openY = y + 56; y = g.openY + 40; }
89
98
  if (spec.cards) {
90
99
  const per = Math.max(1, Math.floor((W - 20) / (CARD.w + 8))), all = s.cards.length, row0 = y + 30 + CARD.h / 2;
@@ -96,7 +105,7 @@ export default function line({ host, stage, panel, T, lang, moving, motion }) {
96
105
  // the flag of the number to find over it.
97
106
  const flags = spec.flag || spec.challenge || spec.find != null || spec.estimate != null ? -g.flagFrom + POLE + FLAG + (spec.second != null || spec.find != null || spec.challenge ? STACK : 0) : r;
98
107
  g.lensY = street() ? g.labelY - 14 : -r - 6;
99
- top = -Math.max(flags, spec.jump ? 6 + arcH + 4 : 0, g.narrow ? -g.lensY + LENS_H : 0, spec.bracket ? 30 : 0) - 8;
108
+ top = -Math.max(flags, spec.jump ? -(g.arcY ?? ARC_Y) + most + 4 : 0, g.narrow ? -g.lensY + LENS_H : 0, spec.bracket ? 30 : 0) - 8;
100
109
  if (stones) {
101
110
  // The stones stand over the line, each over the mark of its number, with the frog on the stone over the point.
102
111
  g.water = [0, -(spec.jump ? arcH + 18 : 20)];
@@ -179,7 +188,11 @@ export default function line({ host, stage, panel, T, lang, moving, motion }) {
179
188
  nodes.push(...['find', 'second', 'point'].filter(w => parts.flags[w]).map(w => parts.flags[w].el));
180
189
  if (spec.start != null) nodes.push(ring());
181
190
  nodes.push(layer);
182
- if (spec.written) { parts.written = null; parts.writtenAt = el('g', { class: 'line-written' }); nodes.push(parts.writtenAt); }
191
+ if (spec.written) { parts.written = null; parts.writtenOrder = null; parts.writtenAt = el('g', { class: 'line-written' }); nodes.push(parts.writtenAt); }
192
+ if (spec.undo) {
193
+ parts.back = writtenLine({ refs: { b: 'size', c: 'start' }, cls: { c: 'line-c-point' } });
194
+ parts.back.at(area.w / 2, y0 + g.backY); nodes.push(parts.back.el);
195
+ } else parts.back = null;
183
196
  if (parts.keys) nodes.push(...parts.keys);
184
197
  if (spec.cards) nodes.push(cards());
185
198
  if (g.narrow) { parts.lens = makeLens(); nodes.push(parts.lens.el); }
@@ -400,7 +413,8 @@ export default function line({ host, stage, panel, T, lang, moving, motion }) {
400
413
  settle();
401
414
  const next = jump(spec, s, dir);
402
415
  if (!next) return;
403
- const from = geo ? xm(s.at) : null, adds = jumpsOf(next) > jumpsOf(s);
416
+ // Each press of an undo page draws a new arc, the jump back or a new first jump.
417
+ const from = geo ? xm(s.at) : null, adds = spec.undo || jumpsOf(next) > jumpsOf(s);
404
418
  s = next; look(false);
405
419
  if (geo) go({ who: 'point', from, to: xm(s.at), hop: isStones(spec), adds }, 320);
406
420
  else refresh();
@@ -435,7 +449,7 @@ export default function line({ host, stage, panel, T, lang, moving, motion }) {
435
449
  const g = geo, y0 = g.y0, st = now();
436
450
  // The arcs run over the gaps the point has jumped. The newest one draws itself while the point is on its way.
437
451
  const arcs = arcsOf(st).flatMap((a, i, all) => {
438
- const down = a.dir < 0, y = y0 + (down ? g.downY : g.arcY), part = arc(xm(a.from, st), xm(a.to, st), y, g.arcH, down);
452
+ const down = a.dir < 0, y = y0 + (down ? g.downY : g.arcY), part = arc(xm(a.from, st), xm(a.to, st), y, g.rise(Math.abs(a.to - a.from)), down);
439
453
  if (glide?.adds && i === all.length - 1) { part.path.setAttribute('stroke-dasharray', '1 1'); part.path.setAttribute('stroke-dashoffset', 1 - glide.o.t); part.head.setAttribute('opacity', glide.o.t > 0.9 ? 1 : 0); }
440
454
  return g.narrow ? [part.path] : [part.path, part.head];
441
455
  });
@@ -496,14 +510,16 @@ export default function line({ host, stage, panel, T, lang, moving, motion }) {
496
510
  // its goal holds. Its numbers take the colour of the point or the second point they write.
497
511
  if (parts.ring) { parts.ring.setAttribute('transform', `translate(${xm(st.base, st)} ${y0})`); parts.ring.setAttribute('aria-valuenow', num(st.base, st)); }
498
512
  if (parts.writtenAt) {
499
- const w = writtenOf(spec, st), order = `${w.ids.a}|${w.ids.b}`;
513
+ // An undo page has no line before its first jump, and its line keeps the ids of its first jump.
514
+ const w = writtenOf(spec, st), ids = w?.ids ?? { a: 'start', b: 'size' }, order = `${ids.a}|${ids.b}`;
500
515
  if (parts.writtenOrder !== order) {
501
- const cls = Object.fromEntries(Object.entries(w.ids).filter(([, id]) => id === 'start' || id === 'second').map(([role, id]) => [role, id === 'start' ? 'line-c-point' : 'line-c-second']));
502
- parts.written = writtenLine({ refs: w.ids, cls }); parts.writtenOrder = order;
516
+ const cls = Object.fromEntries(Object.entries(ids).filter(([, id]) => id === 'start' || id === 'second').map(([role, id]) => [role, id === 'start' ? 'line-c-point' : 'line-c-second']));
517
+ parts.written = writtenLine({ refs: ids, cls }); parts.writtenOrder = order;
503
518
  parts.writtenAt.replaceChildren(parts.written.el); parts.written.at(area.w / 2, y0 + g.writtenY);
504
519
  }
505
520
  parts.written.set(spec.challenge && !solved ? null : w, int);
506
521
  }
522
+ parts.back?.set(backOf(spec, st), int);
507
523
  if (parts.reading) fit(parts.reading, spec.moved ? T(gapsKey(st)) : words(st), area.w - 24);
508
524
  // The marks, their labels and their touch bands stand where the marks stand.
509
525
  if (spec.moved) {
@@ -538,7 +554,10 @@ export default function line({ host, stage, panel, T, lang, moving, motion }) {
538
554
  // refresh writes every value that follows the figure, in the readouts, the challenge's line and the text. Every
539
555
  // goal speaks with the line and the notes of place.
540
556
  function refresh() {
541
- const st = now(), c = counts(spec, st), ids = measuresOf(spec), w = writtenOf(spec, st), value = id => id === 'gaps' ? T(gapsKey(st)) : id === 'written' ? writeWords(w, int) : id === 'reading' ? (w ? writeReading(w, int, T) : words(st)) : c[id] === null ? DASH : int(c[id]);
557
+ const st = now(), c = counts(spec, st), ids = measuresOf(spec), w = writtenOf(spec, st), back = backOf(spec, st);
558
+ // A line not yet made, such as the first jump of an undo page before a press, reads a dash.
559
+ const line = (l, wording) => !l ? DASH : wording ? writeReading(l, int, T) : writeWords(l, int);
560
+ const value = id => id === 'gaps' ? T(gapsKey(st)) : id === 'written' ? line(w) : id === 'reading' ? (spec.written ? line(w, true) : words(st)) : id === 'written-back' ? line(back) : id === 'reading-back' ? line(back, true) : c[id] === null ? DASH : int(c[id]);
542
561
  const shows = id => id === 'placed' ? T('placed-of', { n: int(c.placed), all: int(spec.blank.length) }) : value(id);
543
562
  for (const k of chips) if (k.set(shows(k.id)) && moving() && !held) motion.flash(k.el);
544
563
  if (goal) {
@@ -565,6 +584,8 @@ export default function line({ host, stage, panel, T, lang, moving, motion }) {
565
584
  const ctls = [];
566
585
  // A pick of the step keeps the gaps, the point's mark and its jumps, and writes every label again.
567
586
  if (spec.stepCtl) ctls.push(segment({ id: 'step', label: T('step'), items: spec.stepCtl.items.map(v => [v, int(v)]), value: s.step, pick: v => { s = pick(s, Number(v)); build(); } }));
587
+ // The size of a jump steps, which clears the jumps and puts the point on the start.
588
+ if (spec.sizeCtl) ctls.push(stepper({ id: 'size', label: T('size'), min: spec.sizeCtl.lo, max: spec.sizeCtl.hi, value: s.size, write: int, change: v => { settle(); s = resize(s, v); paint(); } }));
568
589
  if (jumps) ctls.push(h('div', { class: 'line-jumps' }, [jumps.left, jumps.right]));
569
590
  // Middle draws the middle's mark and takes it away. Show reveals where the number to find is, and Next asks for
570
591
  // the next number of the list once the reader has seen this one.
@@ -612,6 +633,7 @@ export default function line({ host, stage, panel, T, lang, moving, motion }) {
612
633
  if (!spec) return;
613
634
  for (const part of svg.querySelectorAll(`[data-ref="${id}"]`)) part.classList.toggle('line-hot', on);
614
635
  parts?.written?.light(id, on, ['written', 'reading']);
636
+ parts?.back?.light(id, on, ['written-back', 'reading-back']);
615
637
  for (const part of panel.querySelectorAll('[data-ref]')) part.classList.toggle('hot', on && part.dataset.ref === id);
616
638
  }
617
639
 
@@ -46,7 +46,8 @@ export function opening(spec) {
46
46
  // A page with start counts its jumps from the start, and its point takes the start when the page leaves it out.
47
47
  const at = spec.point === false ? null : markOf(spec, spec.point ?? spec.start ?? spec.from);
48
48
  return { at, base: spec.start != null ? markOf(spec, spec.start) : at, second: spec.second == null ? null : markOf(spec, spec.second), step: spec.step, checked: false, open: null,
49
- cards: cardsOf(spec), placed: [], middle: spec.middle === true, find: 0, shown: false, estimate: spec.estimate ?? null, offs: offsetsOf(spec) };
49
+ cards: cardsOf(spec), placed: [], middle: spec.middle === true, find: 0, shown: false, estimate: spec.estimate ?? null, offs: offsetsOf(spec),
50
+ size: spec.size ?? 1, undo: !!spec.undo, first: 0, back: false };
50
51
  }
51
52
 
52
53
  // A page with moved opens with some marks off their places. offs holds each such mark's offset, in gaps, and a mark
@@ -86,18 +87,37 @@ export function sides(s, k) {
86
87
  return Math.abs(left - right) <= 2 * SETTLE + 1e-9 ? 'mark-even' : left > right ? 'mark-left' : 'mark-right';
87
88
  }
88
89
 
89
- // The jumps run from base to the point, one arc over each gap, all in one direction: a press against the last jump
90
- // takes it back.
91
- export const jumpsOf = s => s.at === null ? 0 : Math.abs(s.at - s.base);
92
- export const arcsOf = s => Array.from({ length: jumpsOf(s) }, (_, i) => {
93
- const dir = Math.sign(s.at - s.base), from = s.base + i * dir;
94
- return { from, to: from + dir, dir };
95
- });
96
- // jump moves the point one gap, dir 1 to the right and -1 to the left, and gives null at an end of the line.
90
+ // The jumps run from base to the point, each one arc over size gaps, all in one direction: a press against the last
91
+ // jump takes it back. On an undo page first is the direction of the first jump, or 0 before it, and back says the
92
+ // jump back is made: two arcs at most, the second from the end of the first back to base.
93
+ export const jumpsOf = s => s.undo ? arcsOf(s).length : s.at === null ? 0 : Math.abs(s.at - s.base) / (s.size ?? 1);
94
+ export function arcsOf(s) {
95
+ const size = s.size ?? 1;
96
+ if (s.undo) {
97
+ if (!s.first) return [];
98
+ const turn = s.base + s.first * size, out = [{ from: s.base, to: turn, dir: s.first }];
99
+ return s.back ? [...out, { from: turn, to: s.base, dir: -s.first }] : out;
100
+ }
101
+ return Array.from({ length: jumpsOf(s) }, (_, i) => {
102
+ const dir = Math.sign(s.at - s.base), from = s.base + i * dir * size;
103
+ return { from, to: from + dir * size, dir };
104
+ });
105
+ }
106
+ // jump moves the point one jump, dir 1 to the right and -1 to the left, and gives null where the jump would leave the
107
+ // line. On an undo page the button of the first jump's direction rests until the jump back is made, and a press after
108
+ // the jump back makes a new first jump from base.
97
109
  export function jump(spec, s, dir) {
98
- const at = s.at + dir;
99
- return at < 0 || at > gapsOf(spec) ? null : { ...s, at, checked: false, shown: false };
110
+ const size = s.size ?? 1, fits = k => k >= 0 && k <= gapsOf(spec), moved = { checked: false, shown: false };
111
+ if (s.undo) {
112
+ if (s.first && !s.back) return dir === s.first ? null : { ...s, ...moved, at: s.base, back: true };
113
+ const at = s.base + dir * size;
114
+ return fits(at) ? { ...s, ...moved, at, first: dir, back: false } : null;
115
+ }
116
+ const at = s.at + dir * size;
117
+ return fits(at) ? { ...s, ...moved, at } : null;
100
118
  }
119
+ // resize sets the size of a jump, which clears the jumps and puts the point on base.
120
+ export const resize = (s, size) => ({ ...s, size, at: s.base, first: 0, back: false, checked: false });
101
121
  // place puts a point on mark k after a drag. A drag of the point starts the jumps again from its new mark.
102
122
  export const place = (s, who, k) => who === 'second' ? { ...s, second: k } : { ...s, at: k, base: k, checked: false, shown: false };
103
123
  // show reveals where the number to find is, and next moves to the next number of the list, back to the first after
@@ -120,7 +140,9 @@ export const estimate = (s, n) => ({ ...s, estimate: n, shown: false });
120
140
  // moveStart moves the start to mark k and carries the point and its arcs along, so the jumps keep their number and
121
141
  // their direction. The start stops where the point would leave the line.
122
142
  // On a page that counts on to the second point, the start also stops one mark left of the second point.
143
+ // On an undo page the start clears both jumps and takes the point along.
123
144
  export function moveStart(spec, s, k) {
145
+ if (s.undo) { const base = Math.max(0, Math.min(gapsOf(spec), k)); return { ...s, base, at: base, first: 0, back: false, checked: false }; }
124
146
  const n = gapsOf(spec), d = s.at - s.base, lo = Math.max(0, -d), hi = Math.min(n, n - d, keepsRight(spec) ? s.second - 1 : n), base = Math.max(lo, Math.min(hi, k));
125
147
  return { ...s, base, at: base + d, checked: false };
126
148
  }
@@ -141,7 +163,16 @@ export function writtenOf(spec, s) {
141
163
  // The second point stands right of the start, so the line reads the second point minus the start.
142
164
  return { a: numberAt(spec, s.second, s.step), sign: MINUS, b: start, open: s.at !== s.second, ids: { a: 'second', b: 'start', c: 'jumps' } };
143
165
  }
144
- return { a: start, sign: at >= start ? PLUS : MINUS, b: Math.abs(at - start), ids: { a: 'start', b: 'jumps', c: 'point' } };
166
+ // An undo page writes its first jump once it is made, and the number between the signs is the size of a jump.
167
+ if (spec.undo) return s.first ? { a: start, sign: s.first > 0 ? PLUS : MINUS, b: s.size, ids: { a: 'start', b: 'size' } } : null;
168
+ // Jumps of a size write the gaps they cover, which no link reads.
169
+ return { a: start, sign: at >= start ? PLUS : MINUS, b: Math.abs(at - start), ids: { a: 'start', b: s.size > 1 ? null : 'jumps', c: 'point' } };
170
+ }
171
+ // backOf gives the line of the jump back on an undo page once it is made, from the end of the first jump back to the
172
+ // start, or null.
173
+ export function backOf(spec, s) {
174
+ if (!spec.undo || !s.back) return null;
175
+ return { a: numberAt(spec, s.base + s.first * s.size, s.step), sign: s.first > 0 ? MINUS : PLUS, b: s.size, ids: { b: 'size', c: 'start' } };
145
176
  }
146
177
  // pick changes the step. Every label changes, and the point and its jumps keep their marks.
147
178
  export const pick = (s, step) => ({ ...s, step });
@@ -171,18 +202,21 @@ export function measuresOf(spec) {
171
202
  // A page that asks the reader to place a number reads out no number at the point.
172
203
  // An unmarked line, and an estimate page, read out no number at the point either.
173
204
  const has = spec.point !== false && spec.find == null && !isFree(spec) && spec.estimate == null;
174
- return [...(has ? ['point'] : []), ...(spec.start != null ? ['start'] : []), ...(spec.written ? ['written', 'reading'] : []), ...(spec.moved ? ['gaps'] : []), ...(spec.find != null ? ['find'] : []), ...(spec.estimate != null ? ['estimate'] : []), ...(spec.middle ? ['middle'] : []), ...(spec.second != null ? ['second'] : []), ...(spec.reading ? ['reading'] : []), ...(spec.jump ? ['jumps'] : []),
205
+ // An undo page offers the lines of both jumps and no count of jumps, since the loop ends where it began.
206
+ return [...(has ? ['point'] : []), ...(spec.start != null ? ['start'] : []), ...(spec.sized ? ['size'] : []), ...(spec.written ? ['written', 'reading'] : []), ...(spec.undo ? BACK : []), ...(spec.moved ? ['gaps'] : []), ...(spec.find != null ? ['find'] : []), ...(spec.estimate != null ? ['estimate'] : []), ...(spec.middle ? ['middle'] : []), ...(spec.second != null ? ['second'] : []), ...(spec.reading ? ['reading'] : []), ...(spec.jump && !spec.undo ? ['jumps'] : []),
175
207
  ...(spec.bracket ? ['ten-before', 'ten-after'] : []), ...(spec.cards ? ['placed'] : []), 'step', 'from', 'to'];
176
208
  }
177
- // The written line and its words take a readout, and the words of two points and of the gaps take none.
178
- export const readoutsOf = spec => measuresOf(spec).filter(id => (id !== 'reading' || spec.written) && id !== 'gaps');
209
+ export const BACK = ['written-back', 'reading-back'];
210
+ // The written line and its words take a readout. The words of two points and of the gaps take none, nor the line of
211
+ // the jump back, which the figure writes under the first.
212
+ export const readoutsOf = spec => measuresOf(spec).filter(id => (id !== 'reading' || spec.written) && id !== 'gaps' && !BACK.includes(id));
179
213
 
180
214
  // counts gives every value a page can read out, as the page stands. The right end follows the step, and the ten
181
215
  // before and the ten after read null while the point is on a ten.
182
216
  export function counts(spec, s) {
183
217
  const at = s.at === null ? null : numberAt(spec, s.at, s.step), tens = spec.bracket && at !== null ? tensOf(spec, s) : null;
184
218
  return { point: at, second: s.second == null ? null : numberAt(spec, s.second, s.step), jumps: jumpsOf(s), step: s.step, from: spec.from, to: numberAt(spec, gapsOf(spec), s.step),
185
- 'ten-before': tens?.before ?? null, 'ten-after': tens?.after ?? null, placed: s.placed.length,
219
+ 'ten-before': tens?.before ?? null, 'ten-after': tens?.after ?? null, placed: s.placed.length, size: s.size,
186
220
  start: s.base === null ? null : numberAt(spec, s.base, s.step), find: findsOf(spec)[s.find] ?? null, estimate: s.estimate, middle: spec.middle ? middleOf(spec, s.step) : null };
187
221
  }
188
222
 
package/src/i18n/en.yaml CHANGED
@@ -20,6 +20,7 @@ hesap_written-plus: "{a} plus {b} is equal to {c}"
20
20
  hesap_written-minus: "{a} minus {b} is equal to {c}"
21
21
  hesap_written-plus-open: "{a} plus {b}"
22
22
  hesap_written-minus-open: "{a} minus {b}"
23
+ hesap_facts: Facts of the family
23
24
  counters_lead: "Tap or drag the counters and watch the numbers change."
24
25
  counters_whole: Whole
25
26
  counters_first: First part
@@ -218,6 +219,70 @@ counters_hint-steps-to-ten-minus: "First work out how many to take away to bring
218
219
  counters_add-take-to-ten: Down to ten
219
220
  counters_goal-steps-to-ten-minus: "Split the {second} in {calc} into the part that takes {first} down to ten and the rest, then press Check."
220
221
  counters_add-shelf-left: Left
222
+ counters_add-family-first: First part
223
+ counters_add-family-second: Second part
224
+ counters_add-family-whole: Whole
225
+ counters_add-domino-first: First half
226
+ counters_add-domino-second: Second half
227
+ counters_add-domino-whole: Whole
228
+ counters_add-domino-cover: "Cover the half that shows {x}"
229
+ counters_add-domino-lift: Lift the card
230
+ counters_add-whole-whole: Whole
231
+ counters_add-whole-first: Known part
232
+ counters_add-whole-missing: Missing part
233
+ counters_add-whole-second: Counters put in
234
+ counters_add-claim: Sum
235
+ counters_add-claim-right: "The check gives back {first}, so the sum is right."
236
+ counters_add-claim-wrong: "{back} is not {first}, so the sum is wrong."
237
+ counters_add-squad-whole: Squad
238
+ counters_add-squad-first: On the pitch
239
+ counters_add-squad-missing: Bench seats
240
+ counters_add-squad-second: On the bench
241
+ counters_add-undo-back: Taken back
242
+ counters_add-undo-left: Left
243
+ counters_add-whole-put: Put a counter in the next place
244
+ counters_add-put: Put in a counter
245
+ counters_add-squad-put: Seat the next player on the bench
246
+ counters_add-squad-player: "Player {x} on the pitch"
247
+ counters_add-squad-bench: "Player {x} on the bench"
248
+ counters_add-whole-out: "Counter {x}. Take out the last counter."
249
+ counters_add-out: Take out the last counter
250
+ counters_goal-missing-part: "Put in the part that {calc} asks for, then press Check."
251
+ counters_goal-missing-part-done: "{line}, since {part} and {missing} make {whole}."
252
+ counters_note-missing-part-more: "{part} and {put} make {made}, more than {whole}."
253
+ counters_note-missing-part-less: "{part} and {put} make {made}, less than {whole}."
254
+ counters_hint-missing-part: "{calc} asks: {part} and what make {whole}? Count on from {part} to {whole} and put in a counter for each number you say."
255
+ counters_bar-first: First part
256
+ counters_bar-second: Second part
257
+ counters_bar-whole: Whole
258
+ counters_bar-chocolate-first: First piece
259
+ counters_bar-chocolate-second: Second piece
260
+ counters_bar-chocolate-whole: Squares
261
+ counters_bar-split: Split
262
+ counters_bar-end: End
263
+ counters_bar-cover-whole: Cover the whole
264
+ counters_bar-cover-first: Cover the first part
265
+ counters_bar-cover-second: Cover the second part
266
+ counters_bar-lift: Lift the card
267
+ counters_goal-bar-for-fact: "Set the bar model of the family of {fact}, then press Check."
268
+ counters_goal-bar-for-fact-done: "{whole} is the whole, and {first} and {second} are its parts."
269
+ counters_note-bar-for-fact-longer: "The bar is longer than the whole of {fact}."
270
+ counters_note-bar-for-fact-shorter: "The bar is shorter than the whole of {fact}."
271
+ counters_bar-use: Use them as
272
+ counters_bar-use-parts: the two parts
273
+ counters_bar-use-whole: the whole and a part
274
+ counters_bar-third: Third number
275
+ counters_note-bar-for-fact-right: "The whole is right, and the split is too far to the left for the parts of {fact}. Drag it to the right."
276
+ counters_note-bar-for-fact-left: "The whole is right, and the split is too far to the right for the parts of {fact}. Drag it to the left."
277
+ counters_hint-bar-for-fact-plus: "In {fact}, the whole comes after the = sign. Drag the end to the whole, then split it into the two parts."
278
+ counters_hint-bar-for-fact-minus: "In {fact}, the whole comes first. Drag the end to the whole, then split it into the other two numbers."
279
+ counters_goal-bar-for-two: "Find a third number that makes a family with {greater} and {less}, then press Check."
280
+ counters_goal-bar-for-two-done: "The family of {greater}, {less} and {mine} holds both numbers. So does the family of {greater}, {less} and {other}."
281
+ counters_note-bar-for-two-parts-over: "{greater} and {less} make less than {n}."
282
+ counters_note-bar-for-two-parts-under: "{greater} and {less} make more than {n}."
283
+ counters_note-bar-for-two-whole-over: "{less} and {n} make more than {greater}."
284
+ counters_note-bar-for-two-whole-under: "{less} and {n} make less than {greater}."
285
+ counters_hint-bar-for-two: "A family holds a whole and two parts. Use {greater} and {less} as the two parts and add them, or use {greater} as the whole and {less} as a part and find the missing part."
221
286
  line_lead: "Move the point along the line and read the number at its mark."
222
287
  line_jump-left: Jump left
223
288
  line_jump-right: Jump right
@@ -279,3 +344,5 @@ line_hint-jump-minus-one: "Start at {start}. Press Jump left once and say the nu
279
344
  line_note-jump-way-plus: "{calc} is an addition, so the jumps go to the right from {start}."
280
345
  line_note-jump-way-minus: "{calc} is a subtraction, so the jumps go to the left from {start}."
281
346
  line_note-jump-few-none: "The point is still on {start}. Make the jumps for {calc} from there."
347
+ line_size: Size of a jump
348
+ line_size-short: Size
package/src/i18n/tr.yaml CHANGED
@@ -20,6 +20,7 @@ hesap_written-plus: "{a} artı {b} eşittir {c}"
20
20
  hesap_written-minus: "{a} eksi {b} eşittir {c}"
21
21
  hesap_written-plus-open: "{a} artı {b}"
22
22
  hesap_written-minus-open: "{a} eksi {b}"
23
+ # hesap_facts waits for the word of "fact family (calculations)" in the book's i18n/terms/tr.yaml, and reads English until then.
23
24
  counters_lead: "Sayaçlara dokunun, onları sürükleyin ve sayıların onları nasıl izlediğine bakın."
24
25
  counters_whole: Bütün
25
26
  counters_first: Birinci parça
@@ -218,6 +219,70 @@ counters_hint-steps-to-ten-minus: "Ona kadar, {first} sayısının dolu onluğun
218
219
  counters_add-take-to-ten: Ona kadar
219
220
  counters_goal-steps-to-ten-minus: "{calc} için Ona kadar ve Diğer parça düğmelerini ayarlayın, sonra Kontrol et düğmesine basın."
220
221
  counters_add-shelf-left: Kalan
222
+ counters_add-family-first: Birinci parça
223
+ counters_add-family-second: İkinci parça
224
+ counters_add-family-whole: Bütün
225
+ counters_add-domino-first: Birinci yarı
226
+ counters_add-domino-second: İkinci yarı
227
+ counters_add-domino-whole: Bütün
228
+ counters_add-domino-cover: "{x} gösteren yarıyı kapat"
229
+ counters_add-domino-lift: Kartı kaldır
230
+ counters_add-whole-whole: Bütün
231
+ counters_add-whole-first: Bilinen parça
232
+ counters_add-whole-missing: Bilinmeyen parça
233
+ counters_add-whole-second: Konan sayaçlar
234
+ counters_add-claim: Toplam
235
+ counters_add-claim-right: "Çıkarma işlemi {first} sayısını geri veriyor, yani toplam doğru."
236
+ counters_add-claim-wrong: "{back}, {first} sayısına eşit değil, yani toplam yanlış."
237
+ counters_add-squad-whole: Kadro
238
+ counters_add-squad-first: Sahada
239
+ counters_add-squad-missing: Kulübedeki yerler
240
+ counters_add-squad-second: Kulübede
241
+ counters_add-undo-back: Geri alınan
242
+ counters_add-undo-left: Kalan
243
+ counters_add-whole-put: Sonraki yere bir sayaç koy
244
+ counters_add-put: Bir sayaç koy
245
+ counters_add-squad-put: Sıradaki oyuncuyu kulübeye oturt
246
+ counters_add-squad-player: "Sahadaki {x} numaralı oyuncu"
247
+ counters_add-squad-bench: "Kulübedeki {x} numaralı oyuncu"
248
+ counters_add-whole-out: "{x} numaralı sayaç. Son sayacı geri al."
249
+ counters_add-out: Son sayacı geri al
250
+ counters_goal-missing-part: "{calc} işleminin sorduğu parçayı koyun, sonra Kontrol et düğmesine basın."
251
+ counters_goal-missing-part-done: "{line}, çünkü {part} ile {missing}, {whole} eder."
252
+ counters_note-missing-part-more: "{part} ile {put}, {made} eder. Bu, {whole} sayısından büyük."
253
+ counters_note-missing-part-less: "{part} ile {put}, {made} eder. Bu, {whole} sayısından küçük."
254
+ counters_hint-missing-part: "{calc} şunu sorar: {part} ile kaç, {whole} eder? {part} sayısından {whole} sayısına kadar ileri sayın ve söylediğiniz her sayı için bir sayaç koyun."
255
+ counters_bar-first: Birinci parça
256
+ counters_bar-second: İkinci parça
257
+ counters_bar-whole: Bütün
258
+ counters_bar-chocolate-first: Birinci parça
259
+ counters_bar-chocolate-second: İkinci parça
260
+ counters_bar-chocolate-whole: Kareler
261
+ counters_bar-cover-whole: Bütünü kapat
262
+ counters_bar-cover-first: Birinci parçayı kapat
263
+ counters_bar-cover-second: İkinci parçayı kapat
264
+ counters_bar-lift: Kartı kaldır
265
+ counters_goal-bar-for-fact-done: "Bütün {whole}, parçaları {first} ve {second}."
266
+ # waits: counters_bar-split, for "split (bar model)" in the book's i18n/terms/tr.yaml
267
+ # waits: counters_bar-end, for "end (bar model)"
268
+ # waits: counters_goal-bar-for-fact, for "bar model (figure)" and "fact family (calculations)"
269
+ # waits: counters_note-bar-for-fact-longer, for "bar model (figure)"
270
+ # waits: counters_note-bar-for-fact-shorter, for "bar model (figure)"
271
+ counters_bar-use: Sayılar
272
+ counters_bar-use-parts: iki parça
273
+ counters_bar-use-whole: bütün ve bir parça
274
+ counters_bar-third: Üçüncü sayı
275
+ # waits: counters_note-bar-for-fact-right, for "split (bar model)"
276
+ # waits: counters_note-bar-for-fact-left, for "split (bar model)"
277
+ # waits: counters_hint-bar-for-fact-plus, for "end (bar model)" and "split (bar model)"
278
+ # waits: counters_hint-bar-for-fact-minus, for "end (bar model)" and "split (bar model)"
279
+ counters_note-bar-for-two-parts-over: "{greater} ile {less}, {n} sayısından küçük bir sayı eder."
280
+ counters_note-bar-for-two-parts-under: "{greater} ile {less}, {n} sayısından büyük bir sayı eder."
281
+ counters_note-bar-for-two-whole-over: "{less} ile {n}, {greater} sayısından büyük bir sayı eder."
282
+ counters_note-bar-for-two-whole-under: "{less} ile {n}, {greater} sayısından küçük bir sayı eder."
283
+ # waits: counters_goal-bar-for-two, for "fact family (calculations)"
284
+ # waits: counters_goal-bar-for-two-done, for "fact family (calculations)"
285
+ # waits: counters_hint-bar-for-two, for "fact family (calculations)"
221
286
  line_lead: "Noktayı doğru boyunca hareket ettirin ve çizgisindeki sayıyı okuyun."
222
287
  line_jump-left: Sola zıpla
223
288
  line_jump-right: Sağa zıpla
@@ -279,3 +344,6 @@ line_hint-jump-minus-one: "Başlangıçtaki sayıyı söyleyin, sonra her zıpla
279
344
  line_note-jump-way-plus: Nokta yanlış yöne zıpladı.
280
345
  line_note-jump-way-minus: Nokta yanlış yöne zıpladı.
281
346
  line_note-jump-few-none: Zıplamalar az.
347
+ # The Turkish words wait for the term "jump (number line)", which the book's i18n/terms/tr.yaml has not sourced yet.
348
+ line_size: Size of a jump
349
+ line_size-short: Size
package/src/kit/goal.js CHANGED
@@ -10,6 +10,10 @@ export function goalRow({ T, hint, another, motion, actions = [], quiet = false
10
10
  const text = h('p', { class: 'hesap-goal-text' }), state = h('span', { class: 'hesap-goal-state', role: 'status' });
11
11
  const hintText = h('p', { class: 'hesap-hint', hidden: true });
12
12
  let done = false, hinted = false;
13
+ // hold keeps the goal line and the status at the tallest they have been on this page, so the panel keeps its
14
+ // height as the goal line, a note and the done line take turns, and the figure above does not rebuild.
15
+ const tallest = new Map();
16
+ const hold = node => { node.style.minHeight = ''; const h = Math.max(node.offsetHeight, tallest.get(node) ?? 0); tallest.set(node, h); if (h) node.style.minHeight = `${h}px`; };
13
17
  // A hint is words, or an element the family built, such as a formula. kept says the hint opens again after a
14
18
  // change of language, so the family shows it without marking it as new.
15
19
  const toggle = kept => {
@@ -23,13 +27,14 @@ export function goalRow({ T, hint, another, motion, actions = [], quiet = false
23
27
  return {
24
28
  el, text, hintText,
25
29
  // say writes the goal. A family that sets formulas in the goal writes into text itself.
26
- say(words) { if (words instanceof Node) text.replaceChildren(words); else if (text.textContent !== (words ?? '')) text.textContent = words ?? ''; },
30
+ say(words) { if (words instanceof Node) text.replaceChildren(words); else if (text.textContent !== (words ?? '')) text.textContent = words ?? ''; hold(text); },
27
31
  // show says whether the goal is reached. note is a line of progress for a goal not reached yet. It returns true
28
32
  // the first time the goal is reached, when the family cheers.
29
33
  show(reached, note) {
30
34
  const words = reached ? T('solved') : note ?? (quiet ? '' : T('not-yet'));
31
35
  if (state.dataset.words !== words) { state.dataset.words = words; state.replaceChildren(...(reached ? [icon('tick'), h('b', {}, words)] : [words])); }
32
36
  el.classList.toggle('solved', reached);
37
+ hold(state);
33
38
  const first = reached && !done; done = reached;
34
39
  if (first) motion?.pop(state);
35
40
  return first;
@@ -43,6 +48,8 @@ export function goalRow({ T, hint, another, motion, actions = [], quiet = false
43
48
  done = old.reached();
44
49
  if (old.hinted() && hintBtn && !hinted) toggle(true);
45
50
  },
51
+ // closeHint closes an open hint, for a family whose figure needs the room once the goal is reached.
52
+ closeHint() { if (hinted) toggle(false); },
46
53
  // reset closes the hint and forgets the last result, for another target.
47
54
  reset() { done = false; hinted = false; hintBtn?.setAttribute('aria-expanded', 'false'); hintText.hidden = true; },
48
55
  };
@@ -20,3 +20,4 @@ hesap_written-plus: "{a} plus {b} is equal to {c}"
20
20
  hesap_written-minus: "{a} minus {b} is equal to {c}"
21
21
  hesap_written-plus-open: "{a} plus {b}"
22
22
  hesap_written-minus-open: "{a} minus {b}"
23
+ hesap_facts: Facts of the family
@@ -20,3 +20,4 @@ hesap_written-plus: "{a} artı {b} eşittir {c}"
20
20
  hesap_written-minus: "{a} eksi {b} eşittir {c}"
21
21
  hesap_written-plus-open: "{a} artı {b}"
22
22
  hesap_written-minus-open: "{a} eksi {b}"
23
+ # hesap_facts waits for the word of "fact family (calculations)" in the book's i18n/terms/tr.yaml, and reads English until then.
package/src/kit/kit.css CHANGED
@@ -59,7 +59,7 @@ button.hesap-chip{cursor:pointer;min-height:36px;align-items:center}
59
59
  /* A challenge's line holds the goal, whether it is reached, another target and a hint. The goal takes a line of its
60
60
  own, so a longer status keeps the panel's height. */
61
61
  .hesap-goal{display:flex;flex-wrap:wrap;align-items:center;gap:.286rem .571rem}
62
- .hesap-goal-text{margin:0;flex-basis:100%;font-weight:600}
62
+ .hesap-goal-text{margin:0;flex-basis:100%;font-weight:600;line-height:1.4}
63
63
  .hesap-goal-state{display:inline-flex;align-items:center;gap:.286rem;color:var(--ink-2);flex:1;min-width:0}
64
64
  .hesap-goal.solved .hesap-goal-state{color:var(--accent)}
65
65
  .hesap-goal-state svg{width:20px;height:20px}
@@ -79,6 +79,10 @@ aside.story .place[data-formula] .katex-display>.katex{text-align:start}
79
79
  .hesap-btn,.hesap-seg button,.hesap-step button{min-height:42px}
80
80
  .hesap-chip{padding:.214rem .643rem}
81
81
  .hesap-chip small{font-size:.786rem}
82
+ /* On a phone the goal line and the status keep room for two lines each, and the status takes a line of its own,
83
+ so a note or the done line leaves the panel's height as it was. */
84
+ .hesap-goal-text{min-height:2.8em}
85
+ .hesap-goal-state{flex-basis:100%;min-height:2.8em;line-height:1.4}
82
86
  .hesap-long{display:none}
83
87
  .hesap-short{display:inline}
84
88
  /* A compact button shows its glyph alone. */
@@ -96,3 +100,7 @@ aside.story .place[data-formula] .katex-display>.katex{text-align:start}
96
100
  .hesap .hesap-written [data-ref]{stroke:none}
97
101
  .hesap .hesap-written .hesap-written-num{fill:var(--c,var(--ink))}
98
102
  .hesap .hesap-written .hesap-written-hot,.hesap .hesap-written.hesap-written-hot{text-decoration:underline}
103
+
104
+ /* A number sentence or a calculation written in the text, marked up as code: it stays on one line and stands out from
105
+ the words around it, in the text's own font, in the text and on the folded card. */
106
+ :is(aside.story,.peek) code{font:inherit;font-weight:700;font-variant-numeric:tabular-nums;white-space:nowrap;padding:.071rem .357rem;border-radius:4px;background:color-mix(in srgb,var(--accent) 14%,transparent)}
@@ -5,8 +5,8 @@ import { svg as el } from './dom.js';
5
5
 
6
6
  export const PLUS = '+', MINUS = '−';
7
7
  // A line is { a, sign, b }, the number before the sign, the sign and the number after it. A line with open: true
8
- // leaves out its answer.
9
- export const answerOf = line => line.sign === PLUS ? line.a + line.b : line.a - line.b;
8
+ // leaves out its answer, and a line with c writes c as its answer, such as a sum a reader claims.
9
+ export const answerOf = line => line.c ?? (line.sign === PLUS ? line.a + line.b : line.a - line.b);
10
10
 
11
11
  // parts gives the pieces of a line in order. A number carries its role: a and b for the numbers either side of the
12
12
  // sign, and c for the answer.
@@ -23,6 +23,39 @@ export const readingKey = line => `written-${line.sign === PLUS ? 'plus' : 'minu
23
23
  // hesap_, as a family's T does in the browser.
24
24
  export const reading = (line, int, T) => T(readingKey(line), { a: int(line.a), b: int(line.b), c: int(answerOf(line)) });
25
25
 
26
+ // The facts of a family: a whole and its two parts give the two parts added in either order and either part
27
+ // subtracted from the whole. FACTS are their link ids in the order a figure writes them, the additions first.
28
+ export const FACTS = ['written', 'written-swap', 'written-minus-second', 'written-minus-first'];
29
+ // The reading of a fact takes the id of the fact with reading for written, such as reading-swap.
30
+ export const readingOf = id => id.replace(/^written/, 'reading');
31
+ export const writtenOf = id => id.replace(/^reading/, 'written');
32
+ // FACT_IDS are the eight links a page that writes the facts offers.
33
+ export const FACT_IDS = [...FACTS, ...FACTS.map(readingOf)];
34
+ // The link id of each number of a fact, by its role in the line.
35
+ const REFS = { written: { a: 'first', b: 'second', c: 'whole' }, 'written-swap': { a: 'second', b: 'first', c: 'whole' },
36
+ 'written-minus-second': { a: 'whole', b: 'second', c: 'first' }, 'written-minus-first': { a: 'whole', b: 'first', c: 'second' } };
37
+ // factsOf gives the four facts of a first part and a second part as lines, each carrying the link ids of its numbers.
38
+ export function factsOf(first, second) {
39
+ const whole = first + second;
40
+ return { written: { a: first, sign: PLUS, b: second, refs: REFS.written }, 'written-swap': { a: second, sign: PLUS, b: first, refs: REFS['written-swap'] },
41
+ 'written-minus-second': { a: whole, sign: MINUS, b: second, refs: REFS['written-minus-second'] }, 'written-minus-first': { a: whole, sign: MINUS, b: first, refs: REFS['written-minus-first'] } };
42
+ }
43
+ // factLine gives the line of a fact id or of its reading.
44
+ export const factLine = (id, first, second) => factsOf(first, second)[writtenOf(id)];
45
+ // sameFact gives the fact that shows for an id. Two equal parts give two facts, so the swap is the first addition and
46
+ // the whole minus the first part is the whole minus the second.
47
+ export const sameFact = (id, first, second) => first !== second ? writtenOf(id) : writtenOf(id) === 'written-swap' ? 'written' : writtenOf(id) === 'written-minus-first' ? 'written-minus-second' : writtenOf(id);
48
+ // shownFacts gives the facts a figure writes out of the ones a page lists, each once.
49
+ export const shownFacts = (first, second, listed = FACTS) => FACTS.filter(id => listed.includes(id) && sameFact(id, first, second) === id);
50
+ // takenBy gives the part a subtraction takes away from the whole, and null for an addition.
51
+ export const takenBy = id => ({ 'written-minus-second': 'second', 'written-minus-first': 'first' })[writtenOf(id)] ?? null;
52
+ // finding gives the calculation that finds a covered number: the first part plus the second part for the whole, and
53
+ // the whole minus the other part for a part. open leaves out its answer while the number is covered.
54
+ export function finding(first, second, covered, open) {
55
+ const id = covered === 'whole' ? 'written' : covered === 'first' ? 'written-minus-second' : 'written-minus-first';
56
+ return { ...factsOf(first, second)[id], open };
57
+ }
58
+
26
59
  // The size of a written line in a figure's units, the size of the numbers of the line of two numbers in counters/match.
27
60
  export const SIZE = 34;
28
61
  // The size of a step line.