@altnovatrail/canvas 0.3.37 → 0.3.39

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.
@@ -1,16 +1,37 @@
1
+ import type { TracingReading } from "@altnovatrail/spec/canvas";
1
2
  import { type Gray, type Size } from "../core/tracing.js";
2
- /** What the grip asks of the element: the tracing paper moved to a place, and the move ended. */
3
+ import { type Labels } from "./labels.js";
4
+ /**
5
+ * What the grip, the corners and the bar ask of the element: the tracing paper moved to a place,
6
+ * or to a place at a scale, or fit to the window, the change ended, and the statement read.
7
+ */
3
8
  export interface TracingMoves {
4
9
  /** The place the learner moves it to: the element keeps it to the sheet, and gives where it lies. */
5
10
  readonly move: (at: readonly [number, number]) => readonly [number, number];
6
- /** The move is over: a drag let go, or a key pressed. */
11
+ /** The place and the scale the learner gives it: the element keeps it to the sheet. */
12
+ readonly resize: (at: readonly [number, number], scale: number) => Placed;
13
+ /** As large as the window's width takes it, right of the bar of tools. */
14
+ readonly fit: () => void;
15
+ /** The change is over: a drag let go, a key pressed, a button pressed, the wheel at rest. */
7
16
  readonly moved: () => void;
17
+ /** The learner pressed "Recognise" while the reading is offered. */
18
+ readonly read: () => void;
8
19
  }
9
- /** Where the sheet is on the stage: `k` CSS pixels a unit, and the visible part's top left corner. */
20
+ /**
21
+ * Where the sheet is on the stage: `k` CSS pixels a unit, the visible part's top left corner, and
22
+ * the stage's size, CSS pixels.
23
+ */
10
24
  export interface StageView {
11
25
  readonly k: number;
12
26
  readonly vx: number;
13
27
  readonly vy: number;
28
+ readonly width: number;
29
+ readonly height: number;
30
+ }
31
+ /** Where the paper lies and how large, as the element kept it to the sheet. */
32
+ interface Placed {
33
+ readonly at: readonly [number, number];
34
+ readonly scale: number;
14
35
  }
15
36
  /** The crop's picture as the element read it: its size on the sheet and its grey pixels. */
16
37
  interface Read {
@@ -26,7 +47,29 @@ export declare class TracingPaper {
26
47
  readonly grip: HTMLButtonElement;
27
48
  /** The grip's tooltip, which says what its name says already: a screen reader reads that alone. */
28
49
  readonly tip: HTMLDivElement;
50
+ /**
51
+ * Under the ink, over the paper: the statements read, in place of the crop, and the light that
52
+ * sweeps the crop while they are read. It says nothing: the bar's button and the sign do.
53
+ */
54
+ readonly cover: HTMLDivElement;
55
+ /** Over the ink: the frame of the paper chosen, and its corners, which scale it. */
56
+ readonly frame: HTMLDivElement;
57
+ /** Over the ink: the sign above the paper's corner of what it shows. */
58
+ readonly sign: HTMLDivElement;
59
+ /** Over the ink: the bar of scale under the paper chosen. */
60
+ readonly bar: HTMLDivElement;
29
61
  constructor(moves: TracingMoves);
62
+ /** The paper's scale, as the element last placed it. */
63
+ get scale(): number;
64
+ /**
65
+ * The reading of the statement the consumer gives: the bar offers "Recognise" while it is any,
66
+ * and the statements take the crop's place once they come for the learner who asked.
67
+ */
68
+ set reading(reading: TracingReading | undefined);
69
+ /** The press on the sheet lets the paper go: its frame, corners and bar go out of sight. */
70
+ letGo(): void;
71
+ /** Ctrl or ⌘ with the wheel over the paper: its scale by a notch, over once the wheel rests. */
72
+ wheel(up: boolean): void;
30
73
  /** The tracing paper's size on the sheet: the largest till the crop's picture is read. */
31
74
  get size(): Size;
32
75
  /**
@@ -41,8 +84,8 @@ export declare class TracingPaper {
41
84
  * the paper's size is known.
42
85
  */
43
86
  show(blob: Blob | undefined, sized: () => void): void;
44
- /** The label of the grip, in the element's language, and its tooltip's. */
45
- label(name: string, lang: string): void;
87
+ /** The labels of the grip, its tooltip, the sign and the bar, in the element's language. */
88
+ label(labels: Labels, lang: string): void;
46
89
  /**
47
90
  * The layer and the grip as the tracing paper now is: at `at`, shown or hidden, or not there at
48
91
  * all, and the grip where the sheet's view puts the paper's top edge on the stage.
@@ -50,6 +93,7 @@ export declare class TracingPaper {
50
93
  place(state: {
51
94
  readonly at: readonly [number, number];
52
95
  readonly hidden: boolean;
96
+ readonly scale?: number;
53
97
  } | undefined, view: StageView): void;
54
98
  connect(): void;
55
99
  /** The element left the page: the crop's address goes, and a drag or a tooltip with it. */
@@ -6,12 +6,32 @@
6
6
  // keys, a step at a time, Shift a leap (WCAG 2.1.1); a drag is the pointer's one way, as the sheet
7
7
  // moves under the hand (R90 spec). The grip's tooltip says its name on the hover of a pointer that
8
8
  // hovers and on the keyboard's focus, and Escape dismisses it, as the bar's do (WCAG 1.4.13). The
9
- // crop's picture is read once, to its grey pixels, for the crop of the work. This module is no
10
- // entry point of the package.
11
- import { grayOf, LARGEST, sizeOf, TRACING } from "../core/tracing.js";
9
+ // crop's picture is read once, to its grey pixels, for the crop of the work.
10
+ //
11
+ // Its scale (R89), as the mockup's `Zadacha-masshtab` has it: the grip's press, or its focus,
12
+ // chooses the paper — a blue frame round it with a handle at each corner, which a drag scales from
13
+ // the corner across, and under it the bar of scale: smaller, the percentage, which sets it back to
14
+ // its own size, larger, and fit to the window —; a press on the sheet lets it go. The bar's buttons
15
+ // are the keyboard's way, and a drag's alternative (WCAG 2.5.7). Above its corner a sign says what
16
+ // it shows: the photo, the reading on its way, or the statements the AI read, which the bar's last
17
+ // button offers, waits for and gives back the photo from. This module is no entry point of the
18
+ // package.
19
+ import { grayOf, LARGEST, SCALE, scaleOf, sizeOf, TRACING, } from "../core/tracing.js";
12
20
  import { SVG, write } from "./attributes.js";
21
+ import { ICONS, ROW } from "./icons.js";
22
+ import { say } from "./labels.js";
23
+ import { lineOf } from "./notation.js";
24
+ import { draw, plainButton } from "./tools.js";
13
25
  /** The grip's size, CSS pixels: a target of 68 × 44 (WCAG 2.5.5), the bar above the paper's edge. */
14
26
  const GRIP = Object.freeze({ width: 68, height: 44, over: 29 });
27
+ /**
28
+ * The bar of scale and the sign, CSS pixels, as the mockup sets them: the bar `gap` under the
29
+ * paper, `height` high, and kept `margin` within the stage; the sign `over` above the paper's top.
30
+ */
31
+ const BAR = Object.freeze({ gap: 18, height: 52, margin: 8 });
32
+ const SIGN = Object.freeze({ over: 34 });
33
+ /** How long the wheel rests before a scale by it is over, ms. */
34
+ const WHEEL_REST = 300;
15
35
  /** The tracing paper's layer, its grip and the grip's tooltip, and the crop's picture. */
16
36
  export class TracingPaper {
17
37
  /** The layer, in sheet units by its view box, which says nothing to a screen reader. */
@@ -22,7 +42,48 @@ export class TracingPaper {
22
42
  grip = document.createElement("button");
23
43
  /** The grip's tooltip, which says what its name says already: a screen reader reads that alone. */
24
44
  tip = document.createElement("div");
45
+ /**
46
+ * Under the ink, over the paper: the statements read, in place of the crop, and the light that
47
+ * sweeps the crop while they are read. It says nothing: the bar's button and the sign do.
48
+ */
49
+ cover = document.createElement("div");
50
+ #text = document.createElement("div");
51
+ #scan = document.createElement("div");
52
+ /** Over the ink: the frame of the paper chosen, and its corners, which scale it. */
53
+ frame = document.createElement("div");
54
+ /** Over the ink: the sign above the paper's corner of what it shows. */
55
+ sign = document.createElement("div");
56
+ #signWords = document.createElement("span");
57
+ /** Over the ink: the bar of scale under the paper chosen. */
58
+ bar = document.createElement("div");
59
+ #smaller = plainButton("smaller");
60
+ #percent = document.createElement("button");
61
+ #larger = plainButton("larger");
62
+ #fit = plainButton("fit");
63
+ #mode = plainButton("recognise");
25
64
  #moves;
65
+ #labels;
66
+ #lang = "en";
67
+ #scale = 1;
68
+ #chosen = false;
69
+ #reading;
70
+ #shows = "photo";
71
+ /** The learner asked for the statements: they take the crop's place once they come. */
72
+ #asked = false;
73
+ /** The statements drawn, and the size they were fitted to: drawn again only when either changes. */
74
+ #drawn = "";
75
+ /** The letters' size the paper's height gives the statements, before they are fitted. */
76
+ #full = 0;
77
+ /**
78
+ * The lines' sizes, which change when they are first laid out — the paper hidden when they were
79
+ * drawn, or the font not yet loaded — and when they are fitted: each time they are fitted again.
80
+ */
81
+ #lines = new ResizeObserver(() => this.#fitText());
82
+ /** The paper on the stage, CSS pixels, as last placed. */
83
+ #box = { width: 0, height: 0 };
84
+ /** A corner's drag under way: its pointer, where it began, the paper then and the corner. */
85
+ #corner;
86
+ #wheel;
26
87
  #blob;
27
88
  #url;
28
89
  #connected = false;
@@ -81,6 +142,9 @@ export class TracingPaper {
81
142
  });
82
143
  this.grip.addEventListener("focus", () => {
83
144
  this.#focused = this.grip.matches(":focus-visible");
145
+ // The keyboard chooses the paper by its grip, and Tab goes on to the bar of scale.
146
+ if (this.#focused)
147
+ this.#choose(true);
84
148
  this.#showTip();
85
149
  });
86
150
  this.grip.addEventListener("blur", () => {
@@ -88,6 +152,99 @@ export class TracingPaper {
88
152
  this.#dismissed = false;
89
153
  this.#showTip();
90
154
  });
155
+ this.#build();
156
+ }
157
+ /** The cover, the frame and its corners, the sign and the bar, out of sight till the paper is. */
158
+ #build() {
159
+ this.cover.className = "tracing-cover";
160
+ this.cover.setAttribute("aria-hidden", "true");
161
+ this.cover.hidden = true;
162
+ this.#text.className = "statements";
163
+ this.#text.hidden = true;
164
+ this.#scan.className = "scan";
165
+ this.#scan.hidden = true;
166
+ this.#scan.append(document.createElement("i"));
167
+ this.cover.append(this.#text, this.#scan);
168
+ this.frame.className = "tracing-frame";
169
+ this.frame.setAttribute("aria-hidden", "true");
170
+ this.frame.hidden = true;
171
+ for (const corner of ["tl", "tr", "bl", "br"]) {
172
+ const handle = document.createElement("div");
173
+ handle.className = `corner ${corner}`;
174
+ handle.addEventListener("pointerdown", (event) => this.#cornerDown(event, corner));
175
+ handle.addEventListener("pointermove", (event) => this.#cornerMove(event));
176
+ handle.addEventListener("pointerup", (event) => this.#cornerUp(event));
177
+ handle.addEventListener("pointercancel", (event) => this.#cornerUp(event));
178
+ handle.addEventListener("lostpointercapture", (event) => this.#cornerUp(event));
179
+ this.frame.append(handle);
180
+ }
181
+ this.sign.className = "tracing-sign";
182
+ this.sign.setAttribute("aria-hidden", "true");
183
+ this.sign.hidden = true;
184
+ this.sign.append(document.createElement("span"), this.#signWords);
185
+ this.bar.className = "tracing-scale";
186
+ this.bar.setAttribute("role", "toolbar");
187
+ this.bar.hidden = true;
188
+ this.#smaller.classList.add("icon");
189
+ this.#larger.classList.add("icon");
190
+ this.#percent.type = "button";
191
+ this.#percent.className = "percent";
192
+ this.#mode.classList.add("mode");
193
+ const line = document.createElement("span");
194
+ line.className = "line";
195
+ line.setAttribute("aria-hidden", "true");
196
+ this.bar.append(this.#smaller, this.#percent, this.#larger, line, this.#fit, this.#mode);
197
+ this.#smaller.addEventListener("click", () => this.#zoomBy(1 / SCALE.button));
198
+ this.#larger.addEventListener("click", () => this.#zoomBy(SCALE.button));
199
+ this.#percent.addEventListener("click", () => this.#zoomTo(1));
200
+ this.#fit.addEventListener("click", () => {
201
+ this.#moves.fit();
202
+ this.#moves.moved();
203
+ });
204
+ this.#mode.addEventListener("click", () => this.#pressMode());
205
+ // Escape lets the paper go, and the focus goes back to its grip.
206
+ this.bar.addEventListener("keydown", (event) => {
207
+ if (event.key !== "Escape")
208
+ return;
209
+ event.preventDefault();
210
+ this.#choose(false);
211
+ this.grip.focus();
212
+ });
213
+ }
214
+ /** The paper's scale, as the element last placed it. */
215
+ get scale() {
216
+ return this.#scale;
217
+ }
218
+ /**
219
+ * The reading of the statement the consumer gives: the bar offers "Recognise" while it is any,
220
+ * and the statements take the crop's place once they come for the learner who asked.
221
+ */
222
+ set reading(reading) {
223
+ this.#reading = reading;
224
+ if (reading === undefined || reading.state !== "done") {
225
+ this.#shows = "photo";
226
+ if (reading === undefined)
227
+ this.#asked = false;
228
+ }
229
+ else if (this.#asked) {
230
+ this.#asked = false;
231
+ this.#shows = "text";
232
+ }
233
+ this.#drawn = "";
234
+ this.#showReading();
235
+ }
236
+ /** The press on the sheet lets the paper go: its frame, corners and bar go out of sight. */
237
+ letGo() {
238
+ this.#choose(false);
239
+ }
240
+ /** Ctrl or ⌘ with the wheel over the paper: its scale by a notch, over once the wheel rests. */
241
+ wheel(up) {
242
+ this.#zoomBy(up ? SCALE.wheel : 1 / SCALE.wheel, false);
243
+ clearTimeout(this.#wheel);
244
+ this.#wheel = setTimeout(() => {
245
+ this.#wheel = undefined;
246
+ this.#moves.moved();
247
+ }, WHEEL_REST);
91
248
  }
92
249
  /** The tracing paper's size on the sheet: the largest till the crop's picture is read. */
93
250
  get size() {
@@ -130,12 +287,28 @@ export class TracingPaper {
130
287
  return read;
131
288
  });
132
289
  }
133
- /** The label of the grip, in the element's language, and its tooltip's. */
134
- label(name, lang) {
135
- write(this.grip, "aria-label", name);
290
+ /** The labels of the grip, its tooltip, the sign and the bar, in the element's language. */
291
+ label(labels, lang) {
292
+ this.#labels = labels;
293
+ this.#lang = lang;
294
+ write(this.grip, "aria-label", labels.moveTask);
136
295
  write(this.grip, "lang", lang);
137
- this.tip.textContent = name;
296
+ this.tip.textContent = labels.moveTask;
138
297
  write(this.tip, "lang", lang);
298
+ for (const part of [this.bar, this.sign])
299
+ write(part, "lang", lang);
300
+ write(this.bar, "aria-label", labels.scaleButtons);
301
+ for (const [button, words] of [
302
+ [this.#smaller, labels.smaller],
303
+ [this.#larger, labels.larger],
304
+ [this.#fit, labels.fit],
305
+ ]) {
306
+ const label = button.querySelector(".label");
307
+ if (label !== null && label.textContent !== words)
308
+ label.textContent = words;
309
+ }
310
+ this.#showScale();
311
+ this.#showReading();
139
312
  }
140
313
  /**
141
314
  * The layer and the grip as the tracing paper now is: at `at`, shown or hidden, or not there at
@@ -144,27 +317,62 @@ export class TracingPaper {
144
317
  place(state, view) {
145
318
  this.#at = state?.at ?? [0, 0];
146
319
  this.#k = view.k;
320
+ const scale = state?.scale ?? 1;
321
+ if (scale !== this.#scale) {
322
+ this.#scale = scale;
323
+ this.#showScale();
324
+ }
147
325
  const shown = state !== undefined && !state.hidden && this.#blob !== undefined && this.#ready;
148
326
  write(this.layer, "display", shown ? null : "none");
149
327
  if (this.grip.hidden === shown)
150
328
  this.grip.hidden = !shown;
329
+ for (const part of [this.cover, this.sign])
330
+ if (part.hidden === shown)
331
+ part.hidden = !shown;
332
+ const chosen = shown && this.#chosen;
333
+ for (const part of [this.frame, this.bar])
334
+ if (part.hidden === chosen)
335
+ part.hidden = !chosen;
151
336
  if (!shown) {
152
337
  this.#showTip();
153
338
  return;
154
339
  }
155
340
  const [x, y] = this.#at;
156
- const { width, height } = this.#size;
341
+ const width = this.#size.width * scale;
342
+ const height = this.#size.height * scale;
157
343
  for (const shape of [this.#image, this.#edge]) {
158
344
  write(shape, "x", String(x));
159
345
  write(shape, "y", String(y));
160
346
  write(shape, "width", String(width));
161
347
  write(shape, "height", String(height));
162
348
  }
163
- const left = (x + width / 2 - view.vx) * view.k - GRIP.width / 2;
164
- const top = (y - view.vy) * view.k - GRIP.over;
349
+ const { k } = view;
350
+ const box = {
351
+ left: (x - view.vx) * k,
352
+ top: (y - view.vy) * k,
353
+ width: width * k,
354
+ height: height * k,
355
+ };
356
+ const left = box.left + box.width / 2 - GRIP.width / 2;
357
+ const top = box.top - GRIP.over;
165
358
  this.grip.style.transform = `translate(${left}px, ${top}px)`;
166
359
  this.tip.style.setProperty("--tip-x", `${left + GRIP.width + 8}px`);
167
360
  this.tip.style.setProperty("--tip-y", `${top + GRIP.height / 2}px`);
361
+ for (const part of [this.cover, this.frame]) {
362
+ part.style.transform = `translate(${box.left}px, ${box.top}px)`;
363
+ part.style.width = `${box.width}px`;
364
+ part.style.height = `${box.height}px`;
365
+ }
366
+ this.sign.style.transform = `translate(${box.left}px, ${box.top - SIGN.over}px)`;
367
+ // Under the paper, but kept within the stage: a paper as tall as the window keeps its bar.
368
+ const under = Math.min(box.top + box.height + BAR.gap, view.height - BAR.height - BAR.margin);
369
+ this.bar.style.setProperty("--bar-x", `${box.left + box.width / 2}px`);
370
+ this.bar.style.setProperty("--bar-y", `${Math.max(BAR.margin, under)}px`);
371
+ // The paper's corners round with its scale, as the mockup's: 6 px at its own size, 12 at most.
372
+ this.frame.style.setProperty("--round", `${Math.round(6 * Math.min(scale, 2))}px`);
373
+ this.#box = { width: box.width, height: box.height };
374
+ if (this.#shows === "text")
375
+ this.#drawText();
168
376
  this.#showTip();
169
377
  }
170
378
  connect() {
@@ -175,11 +383,19 @@ export class TracingPaper {
175
383
  /** The element left the page: the crop's address goes, and a drag or a tooltip with it. */
176
384
  disconnect() {
177
385
  this.#connected = false;
386
+ this.#lines.disconnect();
387
+ this.#drawn = "";
178
388
  this.#release();
179
389
  this.#drag = undefined;
180
390
  this.grip.classList.remove("dragging");
181
391
  this.#hovered = false;
182
392
  this.#focused = false;
393
+ this.#corner = undefined;
394
+ if (this.#wheel !== undefined) {
395
+ clearTimeout(this.#wheel);
396
+ this.#wheel = undefined;
397
+ this.#moves.moved();
398
+ }
183
399
  this.#showTip();
184
400
  }
185
401
  #address() {
@@ -198,6 +414,7 @@ export class TracingPaper {
198
414
  if (event.button !== 0 || this.#drag !== undefined)
199
415
  return;
200
416
  event.preventDefault();
417
+ this.#choose(true);
201
418
  try {
202
419
  this.grip.setPointerCapture(event.pointerId);
203
420
  }
@@ -256,6 +473,207 @@ export class TracingPaper {
256
473
  this.#at = this.#moves.move(to);
257
474
  this.#moves.moved();
258
475
  }
476
+ /** The paper chosen, or let go: its frame, corners and bar come or go with the next place. */
477
+ #choose(chosen) {
478
+ if (this.#chosen === chosen)
479
+ return;
480
+ this.#chosen = chosen;
481
+ const shown = chosen && !this.grip.hidden;
482
+ for (const part of [this.frame, this.bar])
483
+ if (part.hidden === shown)
484
+ part.hidden = !shown;
485
+ }
486
+ /** A scale about the paper's centre, kept to the rule; over at once unless the wheel goes on. */
487
+ #zoomTo(scale, over = true) {
488
+ const to = scaleOf(scale);
489
+ const from = this.#scale;
490
+ if (to !== from) {
491
+ const { width, height } = this.#size;
492
+ const [x, y] = this.#at;
493
+ // Where it lies now, not at the next frame's place(): presses within one frame add up.
494
+ this.#resized(this.#moves.resize([x + (width * (from - to)) / 2, y + (height * (from - to)) / 2], to));
495
+ }
496
+ if (over)
497
+ this.#moves.moved();
498
+ }
499
+ #zoomBy(factor, over = true) {
500
+ this.#zoomTo(this.#scale * factor, over);
501
+ }
502
+ /** A corner's press: the paper scales from the corner across as the pointer goes. */
503
+ #cornerDown(event, corner) {
504
+ if (event.button !== 0 || this.#corner !== undefined)
505
+ return;
506
+ event.preventDefault();
507
+ event.stopPropagation();
508
+ const handle = event.currentTarget;
509
+ try {
510
+ if (handle instanceof Element)
511
+ handle.setPointerCapture(event.pointerId);
512
+ }
513
+ catch {
514
+ // An event a script made, which no pointer backs: the drag goes on by its moves all the same.
515
+ }
516
+ this.#corner = {
517
+ pointerId: event.pointerId,
518
+ x: event.clientX,
519
+ y: event.clientY,
520
+ at: this.#at,
521
+ scale: this.#scale,
522
+ k: this.#k,
523
+ corner,
524
+ };
525
+ }
526
+ /**
527
+ * The scale a corner's drag gives, as the mockup's: the larger of the pointer's two moves outward,
528
+ * each over the paper's own size on the screen, added to the scale it began at; the corner across
529
+ * stays where it was.
530
+ */
531
+ #cornerMove(event) {
532
+ const drag = this.#corner;
533
+ if (drag === undefined || event.pointerId !== drag.pointerId)
534
+ return;
535
+ const across = drag.corner.endsWith("r") ? 1 : -1;
536
+ const down = drag.corner.startsWith("b") ? 1 : -1;
537
+ const { width, height } = this.#size;
538
+ const grown = Math.max((across * (event.clientX - drag.x)) / (width * drag.k), (down * (event.clientY - drag.y)) / (height * drag.k));
539
+ const scale = scaleOf(drag.scale + grown);
540
+ const [x, y] = drag.at;
541
+ this.#resized(this.#moves.resize([
542
+ across < 0 ? x + width * (drag.scale - scale) : x,
543
+ down < 0 ? y + height * (drag.scale - scale) : y,
544
+ ], scale));
545
+ }
546
+ #resized(placed) {
547
+ this.#at = placed.at;
548
+ if (placed.scale === this.#scale)
549
+ return;
550
+ this.#scale = placed.scale;
551
+ this.#showScale();
552
+ }
553
+ #cornerUp(event) {
554
+ const drag = this.#corner;
555
+ if (drag === undefined || event.pointerId !== drag.pointerId)
556
+ return;
557
+ if (event.type === "pointerup")
558
+ this.#cornerMove(event);
559
+ this.#corner = undefined;
560
+ this.#moves.moved();
561
+ }
562
+ /** The percentage, in the element's language, and the name of the button that sets it back. */
563
+ #showScale() {
564
+ const percent = new Intl.NumberFormat(this.#lang, {
565
+ style: "percent",
566
+ maximumFractionDigits: 0,
567
+ }).format(this.#scale);
568
+ if (this.#percent.textContent !== percent)
569
+ this.#percent.textContent = percent;
570
+ if (this.#labels !== undefined) {
571
+ write(this.#percent, "aria-label", say(this.#labels.scaleReset, { percent }));
572
+ }
573
+ }
574
+ /**
575
+ * "Recognise" offers the reading, waits for it, and gives the photo back once the statements show;
576
+ * statements that came show again with no new reading.
577
+ */
578
+ #pressMode() {
579
+ const reading = this.#reading;
580
+ if (reading === undefined || reading.state === "busy")
581
+ return;
582
+ if (reading.state === "done") {
583
+ this.#shows = this.#shows === "text" ? "photo" : "text";
584
+ this.#drawn = "";
585
+ this.#showReading();
586
+ return;
587
+ }
588
+ this.#asked = true;
589
+ this.#moves.read();
590
+ }
591
+ /** The bar's last button, the sign and the cover, by the reading and what the paper shows. */
592
+ #showReading() {
593
+ const labels = this.#labels;
594
+ const reading = this.#reading;
595
+ this.#mode.hidden = reading === undefined;
596
+ const busy = reading?.state === "busy";
597
+ const text = this.#shows === "text";
598
+ this.#mode.classList.toggle("busy", busy);
599
+ this.#mode.classList.toggle("back", text);
600
+ if (busy)
601
+ this.#mode.setAttribute("aria-disabled", "true");
602
+ else
603
+ this.#mode.removeAttribute("aria-disabled");
604
+ draw(this.#mode, text ? "original" : "recognise", false);
605
+ write(this.#image, "display", text ? "none" : null);
606
+ this.#text.hidden = !text;
607
+ this.#scan.hidden = !busy;
608
+ if (text)
609
+ this.#drawText();
610
+ const shows = busy ? "busy" : text ? "text" : "photo";
611
+ if (this.sign.dataset.shows !== shows) {
612
+ this.sign.dataset.shows = shows;
613
+ this.sign.firstElementChild?.replaceWith(signOf(shows));
614
+ }
615
+ if (labels === undefined)
616
+ return;
617
+ const words = busy ? labels.recognising : text ? labels.original : labels.recognise;
618
+ const label = this.#mode.querySelector(".label");
619
+ if (label !== null && label.textContent !== words)
620
+ label.textContent = words;
621
+ const sign = busy ? labels.recognising : text ? labels.readingShown : labels.photoShown;
622
+ if (this.#signWords.textContent !== sign)
623
+ this.#signWords.textContent = sign;
624
+ }
625
+ /**
626
+ * The statements in place of the crop, a line each, "□" a dashed box and a fraction built up
627
+ * (./notation.ts): their letters as large as the paper's height shares out among them, and
628
+ * smaller where the longest line, or the lines with their fractions, would not fit.
629
+ */
630
+ #drawText() {
631
+ const { width, height } = this.#box;
632
+ const reading = this.#reading;
633
+ const statements = reading?.state === "done" ? reading.statements : [];
634
+ const key = JSON.stringify([statements, Math.round(width), Math.round(height)]);
635
+ if (key === this.#drawn)
636
+ return;
637
+ this.#drawn = key;
638
+ this.#text.replaceChildren(...statements.map((statement) => {
639
+ const line = document.createElement("div");
640
+ line.className = "line";
641
+ line.append(...lineOf(statement));
642
+ return line;
643
+ }));
644
+ const count = Math.max(1, statements.length);
645
+ this.#full = Math.min(height / (count * 1.5), height * 0.55);
646
+ this.#text.style.fontSize = `${this.#full}px`;
647
+ this.#lines.disconnect();
648
+ for (const line of this.#text.children)
649
+ this.#lines.observe(line);
650
+ this.#fitText();
651
+ }
652
+ /**
653
+ * The statements' letters at the paper's full size, or smaller by as much as the widest line, or
654
+ * the lines together, exceed the paper. Measured at the size they have, so that once fitted they
655
+ * fit and their size stays: the observer of the lines, which calls this again, then stops.
656
+ */
657
+ #fitText() {
658
+ const { width, height } = this.#box;
659
+ // The lines as laid out, not the box's scroll: centred, they spill over both its edges.
660
+ const lines = [...this.#text.children];
661
+ const wide = Math.max(0, ...lines.map((line) => line.offsetWidth));
662
+ const first = lines[0];
663
+ const last = lines.at(-1);
664
+ const tall = first === undefined || last === undefined
665
+ ? 0
666
+ : last.offsetTop + last.offsetHeight - first.offsetTop;
667
+ // Not laid out: hidden, or out of the page. The observer calls again once they are.
668
+ if (wide === 0 || tall === 0)
669
+ return;
670
+ const size = Number.parseFloat(this.#text.style.fontSize) || this.#full;
671
+ const k = Math.min((width * 0.92) / wide, (height * 0.92) / tall);
672
+ const fitted = Math.min(this.#full, size * k);
673
+ // Within half a percent it stays: a size that moves by a rounding feeds the observer again.
674
+ if (Math.abs(fitted - size) > size * 0.005)
675
+ this.#text.style.fontSize = `${fitted}px`;
676
+ }
259
677
  /** The tooltip on hover or the keyboard's focus, unless Escape or a drag dismissed it. */
260
678
  #showTip() {
261
679
  const wanted = !this.grip.hidden && (this.#hovered || this.#focused) && !this.#dismissed;
@@ -301,3 +719,21 @@ async function readOf(blob) {
301
719
  URL.revokeObjectURL(url);
302
720
  }
303
721
  }
722
+ /** The sign's mark of what the paper shows: the photo's, the reading's wait, or the AI's reading. */
723
+ function signOf(shows) {
724
+ if (shows === "busy") {
725
+ const spinner = document.createElement("span");
726
+ spinner.className = "spinner";
727
+ return spinner;
728
+ }
729
+ const name = ROW[shows === "text" ? "recognise" : "original"].outline;
730
+ const { viewBox, paths } = ICONS[name];
731
+ const icon = document.createElementNS(SVG, "svg");
732
+ icon.setAttribute("viewBox", viewBox);
733
+ for (const d of paths) {
734
+ const path = document.createElementNS(SVG, "path");
735
+ path.setAttribute("d", d);
736
+ icon.append(path);
737
+ }
738
+ return icon;
739
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@altnovatrail/canvas",
3
- "version": "0.3.37",
3
+ "version": "0.3.39",
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.19.4",
29
+ "@altnovatrail/spec": "0.19.9",
30
30
  "perfect-freehand": "1.2.3",
31
31
  "rbush": "4.0.1"
32
32
  },
33
33
  "devDependencies": {
34
- "@altnovatrail/contracts": "0.19.4",
34
+ "@altnovatrail/contracts": "0.19.9",
35
35
  "@biomejs/biome": "2.6.6",
36
36
  "@bufbuild/protobuf": "2.17.0",
37
37
  "@iconify-json/material-symbols": "1.2.99",