@altnovatrail/canvas 0.3.37 → 0.3.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/core/geometry.js +2 -2
- package/lib/core/grid.d.ts +11 -4
- package/lib/core/grid.js +33 -8
- package/lib/core/hands.d.ts +7 -7
- package/lib/core/hands.js +5 -5
- package/lib/core/history.js +3 -3
- package/lib/core/marks.d.ts +2 -0
- package/lib/core/marks.js +11 -6
- package/lib/core/screen.d.ts +10 -11
- package/lib/core/screen.js +13 -14
- package/lib/core/sheet.d.ts +46 -3
- package/lib/core/sheet.js +46 -3
- package/lib/core/submission.js +5 -1
- package/lib/core/task.d.ts +9 -4
- package/lib/core/task.js +7 -2
- package/lib/core/tracing.d.ts +49 -13
- package/lib/core/tracing.js +110 -31
- package/lib/element/icons.d.ts +81 -14
- package/lib/element/icons.js +82 -15
- package/lib/element/index.d.ts +32 -13
- package/lib/element/index.js +619 -246
- package/lib/element/labels.d.ts +29 -7
- package/lib/element/labels.js +32 -9
- package/lib/element/session.d.ts +0 -3
- package/lib/element/session.js +4 -8
- package/lib/element/tools.d.ts +30 -19
- package/lib/element/tools.js +80 -62
- package/lib/element/tracing.d.ts +49 -5
- package/lib/element/tracing.js +419 -10
- package/package.json +3 -3
- package/lib/core/zoom.d.ts +0 -23
- package/lib/core/zoom.js +0 -43
package/lib/element/tracing.js
CHANGED
|
@@ -6,12 +6,31 @@
|
|
|
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.
|
|
10
|
-
//
|
|
11
|
-
|
|
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 { draw, plainButton } from "./tools.js";
|
|
13
24
|
/** The grip's size, CSS pixels: a target of 68 × 44 (WCAG 2.5.5), the bar above the paper's edge. */
|
|
14
25
|
const GRIP = Object.freeze({ width: 68, height: 44, over: 29 });
|
|
26
|
+
/**
|
|
27
|
+
* The bar of scale and the sign, CSS pixels, as the mockup sets them: the bar `gap` under the
|
|
28
|
+
* paper, `height` high, and kept `margin` within the stage; the sign `over` above the paper's top.
|
|
29
|
+
*/
|
|
30
|
+
const BAR = Object.freeze({ gap: 18, height: 52, margin: 8 });
|
|
31
|
+
const SIGN = Object.freeze({ over: 34 });
|
|
32
|
+
/** How long the wheel rests before a scale by it is over, ms. */
|
|
33
|
+
const WHEEL_REST = 300;
|
|
15
34
|
/** The tracing paper's layer, its grip and the grip's tooltip, and the crop's picture. */
|
|
16
35
|
export class TracingPaper {
|
|
17
36
|
/** The layer, in sheet units by its view box, which says nothing to a screen reader. */
|
|
@@ -22,7 +41,41 @@ export class TracingPaper {
|
|
|
22
41
|
grip = document.createElement("button");
|
|
23
42
|
/** The grip's tooltip, which says what its name says already: a screen reader reads that alone. */
|
|
24
43
|
tip = document.createElement("div");
|
|
44
|
+
/**
|
|
45
|
+
* Under the ink, over the paper: the statements read, in place of the crop, and the light that
|
|
46
|
+
* sweeps the crop while they are read. It says nothing: the bar's button and the sign do.
|
|
47
|
+
*/
|
|
48
|
+
cover = document.createElement("div");
|
|
49
|
+
#text = document.createElement("div");
|
|
50
|
+
#scan = document.createElement("div");
|
|
51
|
+
/** Over the ink: the frame of the paper chosen, and its corners, which scale it. */
|
|
52
|
+
frame = document.createElement("div");
|
|
53
|
+
/** Over the ink: the sign above the paper's corner of what it shows. */
|
|
54
|
+
sign = document.createElement("div");
|
|
55
|
+
#signWords = document.createElement("span");
|
|
56
|
+
/** Over the ink: the bar of scale under the paper chosen. */
|
|
57
|
+
bar = document.createElement("div");
|
|
58
|
+
#smaller = plainButton("zoomOut");
|
|
59
|
+
#percent = document.createElement("button");
|
|
60
|
+
#larger = plainButton("zoomIn");
|
|
61
|
+
#fit = plainButton("fit");
|
|
62
|
+
#mode = plainButton("recognise");
|
|
25
63
|
#moves;
|
|
64
|
+
#labels;
|
|
65
|
+
#lang = "en";
|
|
66
|
+
#scale = 1;
|
|
67
|
+
#chosen = false;
|
|
68
|
+
#reading;
|
|
69
|
+
#shows = "photo";
|
|
70
|
+
/** The learner asked for the statements: they take the crop's place once they come. */
|
|
71
|
+
#asked = false;
|
|
72
|
+
/** The statements drawn, and the size they were fitted to: drawn again only when either changes. */
|
|
73
|
+
#drawn = "";
|
|
74
|
+
/** The paper on the stage, CSS pixels, as last placed. */
|
|
75
|
+
#box = { width: 0, height: 0 };
|
|
76
|
+
/** A corner's drag under way: its pointer, where it began, the paper then and the corner. */
|
|
77
|
+
#corner;
|
|
78
|
+
#wheel;
|
|
26
79
|
#blob;
|
|
27
80
|
#url;
|
|
28
81
|
#connected = false;
|
|
@@ -81,6 +134,9 @@ export class TracingPaper {
|
|
|
81
134
|
});
|
|
82
135
|
this.grip.addEventListener("focus", () => {
|
|
83
136
|
this.#focused = this.grip.matches(":focus-visible");
|
|
137
|
+
// The keyboard chooses the paper by its grip, and Tab goes on to the bar of scale.
|
|
138
|
+
if (this.#focused)
|
|
139
|
+
this.#choose(true);
|
|
84
140
|
this.#showTip();
|
|
85
141
|
});
|
|
86
142
|
this.grip.addEventListener("blur", () => {
|
|
@@ -88,6 +144,99 @@ export class TracingPaper {
|
|
|
88
144
|
this.#dismissed = false;
|
|
89
145
|
this.#showTip();
|
|
90
146
|
});
|
|
147
|
+
this.#build();
|
|
148
|
+
}
|
|
149
|
+
/** The cover, the frame and its corners, the sign and the bar, out of sight till the paper is. */
|
|
150
|
+
#build() {
|
|
151
|
+
this.cover.className = "tracing-cover";
|
|
152
|
+
this.cover.setAttribute("aria-hidden", "true");
|
|
153
|
+
this.cover.hidden = true;
|
|
154
|
+
this.#text.className = "statements";
|
|
155
|
+
this.#text.hidden = true;
|
|
156
|
+
this.#scan.className = "scan";
|
|
157
|
+
this.#scan.hidden = true;
|
|
158
|
+
this.#scan.append(document.createElement("i"));
|
|
159
|
+
this.cover.append(this.#text, this.#scan);
|
|
160
|
+
this.frame.className = "tracing-frame";
|
|
161
|
+
this.frame.setAttribute("aria-hidden", "true");
|
|
162
|
+
this.frame.hidden = true;
|
|
163
|
+
for (const corner of ["tl", "tr", "bl", "br"]) {
|
|
164
|
+
const handle = document.createElement("div");
|
|
165
|
+
handle.className = `corner ${corner}`;
|
|
166
|
+
handle.addEventListener("pointerdown", (event) => this.#cornerDown(event, corner));
|
|
167
|
+
handle.addEventListener("pointermove", (event) => this.#cornerMove(event));
|
|
168
|
+
handle.addEventListener("pointerup", (event) => this.#cornerUp(event));
|
|
169
|
+
handle.addEventListener("pointercancel", (event) => this.#cornerUp(event));
|
|
170
|
+
handle.addEventListener("lostpointercapture", (event) => this.#cornerUp(event));
|
|
171
|
+
this.frame.append(handle);
|
|
172
|
+
}
|
|
173
|
+
this.sign.className = "tracing-sign";
|
|
174
|
+
this.sign.setAttribute("aria-hidden", "true");
|
|
175
|
+
this.sign.hidden = true;
|
|
176
|
+
this.sign.append(document.createElement("span"), this.#signWords);
|
|
177
|
+
this.bar.className = "tracing-scale";
|
|
178
|
+
this.bar.setAttribute("role", "toolbar");
|
|
179
|
+
this.bar.hidden = true;
|
|
180
|
+
this.#smaller.classList.add("icon");
|
|
181
|
+
this.#larger.classList.add("icon");
|
|
182
|
+
this.#percent.type = "button";
|
|
183
|
+
this.#percent.className = "percent";
|
|
184
|
+
this.#mode.classList.add("mode");
|
|
185
|
+
const line = document.createElement("span");
|
|
186
|
+
line.className = "line";
|
|
187
|
+
line.setAttribute("aria-hidden", "true");
|
|
188
|
+
this.bar.append(this.#smaller, this.#percent, this.#larger, line, this.#fit, this.#mode);
|
|
189
|
+
this.#smaller.addEventListener("click", () => this.#zoomBy(1 / SCALE.button));
|
|
190
|
+
this.#larger.addEventListener("click", () => this.#zoomBy(SCALE.button));
|
|
191
|
+
this.#percent.addEventListener("click", () => this.#zoomTo(1));
|
|
192
|
+
this.#fit.addEventListener("click", () => {
|
|
193
|
+
this.#moves.fit();
|
|
194
|
+
this.#moves.moved();
|
|
195
|
+
});
|
|
196
|
+
this.#mode.addEventListener("click", () => this.#pressMode());
|
|
197
|
+
// Escape lets the paper go, and the focus goes back to its grip.
|
|
198
|
+
this.bar.addEventListener("keydown", (event) => {
|
|
199
|
+
if (event.key !== "Escape")
|
|
200
|
+
return;
|
|
201
|
+
event.preventDefault();
|
|
202
|
+
this.#choose(false);
|
|
203
|
+
this.grip.focus();
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/** The paper's scale, as the element last placed it. */
|
|
207
|
+
get scale() {
|
|
208
|
+
return this.#scale;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The reading of the statement the consumer gives: the bar offers "Recognise" while it is any,
|
|
212
|
+
* and the statements take the crop's place once they come for the learner who asked.
|
|
213
|
+
*/
|
|
214
|
+
set reading(reading) {
|
|
215
|
+
this.#reading = reading;
|
|
216
|
+
if (reading === undefined || reading.state !== "done") {
|
|
217
|
+
this.#shows = "photo";
|
|
218
|
+
if (reading === undefined)
|
|
219
|
+
this.#asked = false;
|
|
220
|
+
}
|
|
221
|
+
else if (this.#asked) {
|
|
222
|
+
this.#asked = false;
|
|
223
|
+
this.#shows = "text";
|
|
224
|
+
}
|
|
225
|
+
this.#drawn = "";
|
|
226
|
+
this.#showReading();
|
|
227
|
+
}
|
|
228
|
+
/** The press on the sheet lets the paper go: its frame, corners and bar go out of sight. */
|
|
229
|
+
letGo() {
|
|
230
|
+
this.#choose(false);
|
|
231
|
+
}
|
|
232
|
+
/** Ctrl or ⌘ with the wheel over the paper: its scale by a notch, over once the wheel rests. */
|
|
233
|
+
wheel(up) {
|
|
234
|
+
this.#zoomBy(up ? SCALE.wheel : 1 / SCALE.wheel, false);
|
|
235
|
+
clearTimeout(this.#wheel);
|
|
236
|
+
this.#wheel = setTimeout(() => {
|
|
237
|
+
this.#wheel = undefined;
|
|
238
|
+
this.#moves.moved();
|
|
239
|
+
}, WHEEL_REST);
|
|
91
240
|
}
|
|
92
241
|
/** The tracing paper's size on the sheet: the largest till the crop's picture is read. */
|
|
93
242
|
get size() {
|
|
@@ -130,12 +279,28 @@ export class TracingPaper {
|
|
|
130
279
|
return read;
|
|
131
280
|
});
|
|
132
281
|
}
|
|
133
|
-
/** The
|
|
134
|
-
label(
|
|
135
|
-
|
|
282
|
+
/** The labels of the grip, its tooltip, the sign and the bar, in the element's language. */
|
|
283
|
+
label(labels, lang) {
|
|
284
|
+
this.#labels = labels;
|
|
285
|
+
this.#lang = lang;
|
|
286
|
+
write(this.grip, "aria-label", labels.moveTask);
|
|
136
287
|
write(this.grip, "lang", lang);
|
|
137
|
-
this.tip.textContent =
|
|
288
|
+
this.tip.textContent = labels.moveTask;
|
|
138
289
|
write(this.tip, "lang", lang);
|
|
290
|
+
for (const part of [this.bar, this.sign])
|
|
291
|
+
write(part, "lang", lang);
|
|
292
|
+
write(this.bar, "aria-label", labels.scaleButtons);
|
|
293
|
+
for (const [button, words] of [
|
|
294
|
+
[this.#smaller, labels.zoomOut],
|
|
295
|
+
[this.#larger, labels.zoomIn],
|
|
296
|
+
[this.#fit, labels.fit],
|
|
297
|
+
]) {
|
|
298
|
+
const label = button.querySelector(".label");
|
|
299
|
+
if (label !== null && label.textContent !== words)
|
|
300
|
+
label.textContent = words;
|
|
301
|
+
}
|
|
302
|
+
this.#showScale();
|
|
303
|
+
this.#showReading();
|
|
139
304
|
}
|
|
140
305
|
/**
|
|
141
306
|
* The layer and the grip as the tracing paper now is: at `at`, shown or hidden, or not there at
|
|
@@ -144,27 +309,62 @@ export class TracingPaper {
|
|
|
144
309
|
place(state, view) {
|
|
145
310
|
this.#at = state?.at ?? [0, 0];
|
|
146
311
|
this.#k = view.k;
|
|
312
|
+
const scale = state?.scale ?? 1;
|
|
313
|
+
if (scale !== this.#scale) {
|
|
314
|
+
this.#scale = scale;
|
|
315
|
+
this.#showScale();
|
|
316
|
+
}
|
|
147
317
|
const shown = state !== undefined && !state.hidden && this.#blob !== undefined && this.#ready;
|
|
148
318
|
write(this.layer, "display", shown ? null : "none");
|
|
149
319
|
if (this.grip.hidden === shown)
|
|
150
320
|
this.grip.hidden = !shown;
|
|
321
|
+
for (const part of [this.cover, this.sign])
|
|
322
|
+
if (part.hidden === shown)
|
|
323
|
+
part.hidden = !shown;
|
|
324
|
+
const chosen = shown && this.#chosen;
|
|
325
|
+
for (const part of [this.frame, this.bar])
|
|
326
|
+
if (part.hidden === chosen)
|
|
327
|
+
part.hidden = !chosen;
|
|
151
328
|
if (!shown) {
|
|
152
329
|
this.#showTip();
|
|
153
330
|
return;
|
|
154
331
|
}
|
|
155
332
|
const [x, y] = this.#at;
|
|
156
|
-
const
|
|
333
|
+
const width = this.#size.width * scale;
|
|
334
|
+
const height = this.#size.height * scale;
|
|
157
335
|
for (const shape of [this.#image, this.#edge]) {
|
|
158
336
|
write(shape, "x", String(x));
|
|
159
337
|
write(shape, "y", String(y));
|
|
160
338
|
write(shape, "width", String(width));
|
|
161
339
|
write(shape, "height", String(height));
|
|
162
340
|
}
|
|
163
|
-
const
|
|
164
|
-
const
|
|
341
|
+
const { k } = view;
|
|
342
|
+
const box = {
|
|
343
|
+
left: (x - view.vx) * k,
|
|
344
|
+
top: (y - view.vy) * k,
|
|
345
|
+
width: width * k,
|
|
346
|
+
height: height * k,
|
|
347
|
+
};
|
|
348
|
+
const left = box.left + box.width / 2 - GRIP.width / 2;
|
|
349
|
+
const top = box.top - GRIP.over;
|
|
165
350
|
this.grip.style.transform = `translate(${left}px, ${top}px)`;
|
|
166
351
|
this.tip.style.setProperty("--tip-x", `${left + GRIP.width + 8}px`);
|
|
167
352
|
this.tip.style.setProperty("--tip-y", `${top + GRIP.height / 2}px`);
|
|
353
|
+
for (const part of [this.cover, this.frame]) {
|
|
354
|
+
part.style.transform = `translate(${box.left}px, ${box.top}px)`;
|
|
355
|
+
part.style.width = `${box.width}px`;
|
|
356
|
+
part.style.height = `${box.height}px`;
|
|
357
|
+
}
|
|
358
|
+
this.sign.style.transform = `translate(${box.left}px, ${box.top - SIGN.over}px)`;
|
|
359
|
+
// Under the paper, but kept within the stage: a paper as tall as the window keeps its bar.
|
|
360
|
+
const under = Math.min(box.top + box.height + BAR.gap, view.height - BAR.height - BAR.margin);
|
|
361
|
+
this.bar.style.setProperty("--bar-x", `${box.left + box.width / 2}px`);
|
|
362
|
+
this.bar.style.setProperty("--bar-y", `${Math.max(BAR.margin, under)}px`);
|
|
363
|
+
// The paper's corners round with its scale, as the mockup's: 6 px at its own size, 12 at most.
|
|
364
|
+
this.frame.style.setProperty("--round", `${Math.round(6 * Math.min(scale, 2))}px`);
|
|
365
|
+
this.#box = { width: box.width, height: box.height };
|
|
366
|
+
if (this.#shows === "text")
|
|
367
|
+
this.#drawText();
|
|
168
368
|
this.#showTip();
|
|
169
369
|
}
|
|
170
370
|
connect() {
|
|
@@ -180,6 +380,12 @@ export class TracingPaper {
|
|
|
180
380
|
this.grip.classList.remove("dragging");
|
|
181
381
|
this.#hovered = false;
|
|
182
382
|
this.#focused = false;
|
|
383
|
+
this.#corner = undefined;
|
|
384
|
+
if (this.#wheel !== undefined) {
|
|
385
|
+
clearTimeout(this.#wheel);
|
|
386
|
+
this.#wheel = undefined;
|
|
387
|
+
this.#moves.moved();
|
|
388
|
+
}
|
|
183
389
|
this.#showTip();
|
|
184
390
|
}
|
|
185
391
|
#address() {
|
|
@@ -198,6 +404,7 @@ export class TracingPaper {
|
|
|
198
404
|
if (event.button !== 0 || this.#drag !== undefined)
|
|
199
405
|
return;
|
|
200
406
|
event.preventDefault();
|
|
407
|
+
this.#choose(true);
|
|
201
408
|
try {
|
|
202
409
|
this.grip.setPointerCapture(event.pointerId);
|
|
203
410
|
}
|
|
@@ -256,6 +463,190 @@ export class TracingPaper {
|
|
|
256
463
|
this.#at = this.#moves.move(to);
|
|
257
464
|
this.#moves.moved();
|
|
258
465
|
}
|
|
466
|
+
/** The paper chosen, or let go: its frame, corners and bar come or go with the next place. */
|
|
467
|
+
#choose(chosen) {
|
|
468
|
+
if (this.#chosen === chosen)
|
|
469
|
+
return;
|
|
470
|
+
this.#chosen = chosen;
|
|
471
|
+
const shown = chosen && !this.grip.hidden;
|
|
472
|
+
for (const part of [this.frame, this.bar])
|
|
473
|
+
if (part.hidden === shown)
|
|
474
|
+
part.hidden = !shown;
|
|
475
|
+
}
|
|
476
|
+
/** A scale about the paper's centre, kept to the rule; over at once unless the wheel goes on. */
|
|
477
|
+
#zoomTo(scale, over = true) {
|
|
478
|
+
const to = scaleOf(scale);
|
|
479
|
+
const from = this.#scale;
|
|
480
|
+
if (to !== from) {
|
|
481
|
+
const { width, height } = this.#size;
|
|
482
|
+
const [x, y] = this.#at;
|
|
483
|
+
// Where it lies now, not at the next frame's place(): presses within one frame add up.
|
|
484
|
+
this.#resized(this.#moves.resize([x + (width * (from - to)) / 2, y + (height * (from - to)) / 2], to));
|
|
485
|
+
}
|
|
486
|
+
if (over)
|
|
487
|
+
this.#moves.moved();
|
|
488
|
+
}
|
|
489
|
+
#zoomBy(factor, over = true) {
|
|
490
|
+
this.#zoomTo(this.#scale * factor, over);
|
|
491
|
+
}
|
|
492
|
+
/** A corner's press: the paper scales from the corner across as the pointer goes. */
|
|
493
|
+
#cornerDown(event, corner) {
|
|
494
|
+
if (event.button !== 0 || this.#corner !== undefined)
|
|
495
|
+
return;
|
|
496
|
+
event.preventDefault();
|
|
497
|
+
event.stopPropagation();
|
|
498
|
+
const handle = event.currentTarget;
|
|
499
|
+
try {
|
|
500
|
+
if (handle instanceof Element)
|
|
501
|
+
handle.setPointerCapture(event.pointerId);
|
|
502
|
+
}
|
|
503
|
+
catch {
|
|
504
|
+
// An event a script made, which no pointer backs: the drag goes on by its moves all the same.
|
|
505
|
+
}
|
|
506
|
+
this.#corner = {
|
|
507
|
+
pointerId: event.pointerId,
|
|
508
|
+
x: event.clientX,
|
|
509
|
+
y: event.clientY,
|
|
510
|
+
at: this.#at,
|
|
511
|
+
scale: this.#scale,
|
|
512
|
+
k: this.#k,
|
|
513
|
+
corner,
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* The scale a corner's drag gives, as the mockup's: the larger of the pointer's two moves outward,
|
|
518
|
+
* each over the paper's own size on the screen, added to the scale it began at; the corner across
|
|
519
|
+
* stays where it was.
|
|
520
|
+
*/
|
|
521
|
+
#cornerMove(event) {
|
|
522
|
+
const drag = this.#corner;
|
|
523
|
+
if (drag === undefined || event.pointerId !== drag.pointerId)
|
|
524
|
+
return;
|
|
525
|
+
const across = drag.corner.endsWith("r") ? 1 : -1;
|
|
526
|
+
const down = drag.corner.startsWith("b") ? 1 : -1;
|
|
527
|
+
const { width, height } = this.#size;
|
|
528
|
+
const grown = Math.max((across * (event.clientX - drag.x)) / (width * drag.k), (down * (event.clientY - drag.y)) / (height * drag.k));
|
|
529
|
+
const scale = scaleOf(drag.scale + grown);
|
|
530
|
+
const [x, y] = drag.at;
|
|
531
|
+
this.#resized(this.#moves.resize([
|
|
532
|
+
across < 0 ? x + width * (drag.scale - scale) : x,
|
|
533
|
+
down < 0 ? y + height * (drag.scale - scale) : y,
|
|
534
|
+
], scale));
|
|
535
|
+
}
|
|
536
|
+
#resized(placed) {
|
|
537
|
+
this.#at = placed.at;
|
|
538
|
+
if (placed.scale === this.#scale)
|
|
539
|
+
return;
|
|
540
|
+
this.#scale = placed.scale;
|
|
541
|
+
this.#showScale();
|
|
542
|
+
}
|
|
543
|
+
#cornerUp(event) {
|
|
544
|
+
const drag = this.#corner;
|
|
545
|
+
if (drag === undefined || event.pointerId !== drag.pointerId)
|
|
546
|
+
return;
|
|
547
|
+
if (event.type === "pointerup")
|
|
548
|
+
this.#cornerMove(event);
|
|
549
|
+
this.#corner = undefined;
|
|
550
|
+
this.#moves.moved();
|
|
551
|
+
}
|
|
552
|
+
/** The percentage, in the element's language, and the name of the button that sets it back. */
|
|
553
|
+
#showScale() {
|
|
554
|
+
const percent = new Intl.NumberFormat(this.#lang, {
|
|
555
|
+
style: "percent",
|
|
556
|
+
maximumFractionDigits: 0,
|
|
557
|
+
}).format(this.#scale);
|
|
558
|
+
if (this.#percent.textContent !== percent)
|
|
559
|
+
this.#percent.textContent = percent;
|
|
560
|
+
if (this.#labels !== undefined) {
|
|
561
|
+
write(this.#percent, "aria-label", say(this.#labels.scaleReset, { percent }));
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* "Recognise" offers the reading, waits for it, and gives the photo back once the statements show;
|
|
566
|
+
* statements that came show again with no new reading.
|
|
567
|
+
*/
|
|
568
|
+
#pressMode() {
|
|
569
|
+
const reading = this.#reading;
|
|
570
|
+
if (reading === undefined || reading.state === "busy")
|
|
571
|
+
return;
|
|
572
|
+
if (reading.state === "done") {
|
|
573
|
+
this.#shows = this.#shows === "text" ? "photo" : "text";
|
|
574
|
+
this.#drawn = "";
|
|
575
|
+
this.#showReading();
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
this.#asked = true;
|
|
579
|
+
this.#moves.read();
|
|
580
|
+
}
|
|
581
|
+
/** The bar's last button, the sign and the cover, by the reading and what the paper shows. */
|
|
582
|
+
#showReading() {
|
|
583
|
+
const labels = this.#labels;
|
|
584
|
+
const reading = this.#reading;
|
|
585
|
+
this.#mode.hidden = reading === undefined;
|
|
586
|
+
const busy = reading?.state === "busy";
|
|
587
|
+
const text = this.#shows === "text";
|
|
588
|
+
this.#mode.classList.toggle("busy", busy);
|
|
589
|
+
this.#mode.classList.toggle("back", text);
|
|
590
|
+
if (busy)
|
|
591
|
+
this.#mode.setAttribute("aria-disabled", "true");
|
|
592
|
+
else
|
|
593
|
+
this.#mode.removeAttribute("aria-disabled");
|
|
594
|
+
draw(this.#mode, text ? "original" : "recognise", false);
|
|
595
|
+
write(this.#image, "display", text ? "none" : null);
|
|
596
|
+
this.#text.hidden = !text;
|
|
597
|
+
this.#scan.hidden = !busy;
|
|
598
|
+
if (text)
|
|
599
|
+
this.#drawText();
|
|
600
|
+
const shows = busy ? "busy" : text ? "text" : "photo";
|
|
601
|
+
if (this.sign.dataset.shows !== shows) {
|
|
602
|
+
this.sign.dataset.shows = shows;
|
|
603
|
+
this.sign.firstElementChild?.replaceWith(signOf(shows));
|
|
604
|
+
}
|
|
605
|
+
if (labels === undefined)
|
|
606
|
+
return;
|
|
607
|
+
const words = busy ? labels.recognising : text ? labels.original : labels.recognise;
|
|
608
|
+
const label = this.#mode.querySelector(".label");
|
|
609
|
+
if (label !== null && label.textContent !== words)
|
|
610
|
+
label.textContent = words;
|
|
611
|
+
const sign = busy ? labels.recognising : text ? labels.readingShown : labels.photoShown;
|
|
612
|
+
if (this.#signWords.textContent !== sign)
|
|
613
|
+
this.#signWords.textContent = sign;
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* The statements in place of the crop, a line each, "□" a dashed box: their letters as large as
|
|
617
|
+
* the paper's height shares out among them, and smaller where the longest line would not fit.
|
|
618
|
+
*/
|
|
619
|
+
#drawText() {
|
|
620
|
+
const { width, height } = this.#box;
|
|
621
|
+
const reading = this.#reading;
|
|
622
|
+
const statements = reading?.state === "done" ? reading.statements : [];
|
|
623
|
+
const key = JSON.stringify([statements, Math.round(width), Math.round(height)]);
|
|
624
|
+
if (key === this.#drawn)
|
|
625
|
+
return;
|
|
626
|
+
this.#drawn = key;
|
|
627
|
+
this.#text.replaceChildren(...statements.map((statement) => {
|
|
628
|
+
const line = document.createElement("div");
|
|
629
|
+
line.className = "line";
|
|
630
|
+
for (const [n, piece] of statement.split("□").entries()) {
|
|
631
|
+
if (n > 0) {
|
|
632
|
+
const box = document.createElement("span");
|
|
633
|
+
box.className = "slot";
|
|
634
|
+
line.append(box);
|
|
635
|
+
}
|
|
636
|
+
if (piece !== "")
|
|
637
|
+
line.append(piece);
|
|
638
|
+
}
|
|
639
|
+
return line;
|
|
640
|
+
}));
|
|
641
|
+
const lines = Math.max(1, statements.length);
|
|
642
|
+
let size = Math.min(height / (lines * 1.5), height * 0.55);
|
|
643
|
+
this.#text.style.fontSize = `${size}px`;
|
|
644
|
+
const wide = this.#text.scrollWidth;
|
|
645
|
+
if (wide > width && wide > 0) {
|
|
646
|
+
size *= (width * 0.92) / wide;
|
|
647
|
+
this.#text.style.fontSize = `${size}px`;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
259
650
|
/** The tooltip on hover or the keyboard's focus, unless Escape or a drag dismissed it. */
|
|
260
651
|
#showTip() {
|
|
261
652
|
const wanted = !this.grip.hidden && (this.#hovered || this.#focused) && !this.#dismissed;
|
|
@@ -301,3 +692,21 @@ async function readOf(blob) {
|
|
|
301
692
|
URL.revokeObjectURL(url);
|
|
302
693
|
}
|
|
303
694
|
}
|
|
695
|
+
/** The sign's mark of what the paper shows: the photo's, the reading's wait, or the AI's reading. */
|
|
696
|
+
function signOf(shows) {
|
|
697
|
+
if (shows === "busy") {
|
|
698
|
+
const spinner = document.createElement("span");
|
|
699
|
+
spinner.className = "spinner";
|
|
700
|
+
return spinner;
|
|
701
|
+
}
|
|
702
|
+
const name = ROW[shows === "text" ? "recognise" : "original"].outline;
|
|
703
|
+
const { viewBox, paths } = ICONS[name];
|
|
704
|
+
const icon = document.createElementNS(SVG, "svg");
|
|
705
|
+
icon.setAttribute("viewBox", viewBox);
|
|
706
|
+
for (const d of paths) {
|
|
707
|
+
const path = document.createElementNS(SVG, "path");
|
|
708
|
+
path.setAttribute("d", d);
|
|
709
|
+
icon.append(path);
|
|
710
|
+
}
|
|
711
|
+
return icon;
|
|
712
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@altnovatrail/canvas",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.38",
|
|
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.
|
|
29
|
+
"@altnovatrail/spec": "0.19.8",
|
|
30
30
|
"perfect-freehand": "1.2.3",
|
|
31
31
|
"rbush": "4.0.1"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@altnovatrail/contracts": "0.19.
|
|
34
|
+
"@altnovatrail/contracts": "0.19.8",
|
|
35
35
|
"@biomejs/biome": "2.6.6",
|
|
36
36
|
"@bufbuild/protobuf": "2.17.0",
|
|
37
37
|
"@iconify-json/material-symbols": "1.2.99",
|
package/lib/core/zoom.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The zooms of the sheet, from its fit to twice it, by quarters (R73): the learner steps through
|
|
3
|
-
* them with the row's Zoom in and Zoom out, so that the workbook's printed lines, and the ink, can
|
|
4
|
-
* be read at 200 % (WCAG 1.4.4, by the technique G178; R90 spec).
|
|
5
|
-
*/
|
|
6
|
-
export declare const ZOOMS: readonly number[];
|
|
7
|
-
/**
|
|
8
|
-
* The page's zoom (R73): one for every sheet of the page, from 100 % when the page is loaded, as
|
|
9
|
-
* the pen mode is (R29). The next task is a new element (R20), and the zoom stays: a learner who
|
|
10
|
-
* needs it larger asks once. It lives in memory alone: nothing is stored or sent.
|
|
11
|
-
*/
|
|
12
|
-
export declare class PageZoom {
|
|
13
|
-
#private;
|
|
14
|
-
/** The zoom now: 1 for the fit, 2 for twice it. */
|
|
15
|
-
get value(): number;
|
|
16
|
-
/** Whether there is a larger zoom to step to, for Zoom in, or a smaller one, for Zoom out. */
|
|
17
|
-
get larger(): boolean;
|
|
18
|
-
get smaller(): boolean;
|
|
19
|
-
/** A step larger, for `larger`, or smaller, if there is one: every watcher hears of it. */
|
|
20
|
-
step(larger: boolean): boolean;
|
|
21
|
-
/** `watcher` hears of every change from now on, until the function this gives is called. */
|
|
22
|
-
watch(watcher: () => void): () => void;
|
|
23
|
-
}
|
package/lib/core/zoom.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The zooms of the sheet, from its fit to twice it, by quarters (R73): the learner steps through
|
|
3
|
-
* them with the row's Zoom in and Zoom out, so that the workbook's printed lines, and the ink, can
|
|
4
|
-
* be read at 200 % (WCAG 1.4.4, by the technique G178; R90 spec).
|
|
5
|
-
*/
|
|
6
|
-
export const ZOOMS = [1, 1.25, 1.5, 1.75, 2];
|
|
7
|
-
/**
|
|
8
|
-
* The page's zoom (R73): one for every sheet of the page, from 100 % when the page is loaded, as
|
|
9
|
-
* the pen mode is (R29). The next task is a new element (R20), and the zoom stays: a learner who
|
|
10
|
-
* needs it larger asks once. It lives in memory alone: nothing is stored or sent.
|
|
11
|
-
*/
|
|
12
|
-
export class PageZoom {
|
|
13
|
-
#step = 0;
|
|
14
|
-
#watchers = new Set();
|
|
15
|
-
/** The zoom now: 1 for the fit, 2 for twice it. */
|
|
16
|
-
get value() {
|
|
17
|
-
return ZOOMS[this.#step] ?? 1;
|
|
18
|
-
}
|
|
19
|
-
/** Whether there is a larger zoom to step to, for Zoom in, or a smaller one, for Zoom out. */
|
|
20
|
-
get larger() {
|
|
21
|
-
return this.#step < ZOOMS.length - 1;
|
|
22
|
-
}
|
|
23
|
-
get smaller() {
|
|
24
|
-
return this.#step > 0;
|
|
25
|
-
}
|
|
26
|
-
/** A step larger, for `larger`, or smaller, if there is one: every watcher hears of it. */
|
|
27
|
-
step(larger) {
|
|
28
|
-
const step = this.#step + (larger ? 1 : -1);
|
|
29
|
-
if (step < 0 || step >= ZOOMS.length)
|
|
30
|
-
return false;
|
|
31
|
-
this.#step = step;
|
|
32
|
-
for (const watcher of [...this.#watchers])
|
|
33
|
-
watcher();
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
/** `watcher` hears of every change from now on, until the function this gives is called. */
|
|
37
|
-
watch(watcher) {
|
|
38
|
-
this.#watchers.add(watcher);
|
|
39
|
-
return () => {
|
|
40
|
-
this.#watchers.delete(watcher);
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
}
|