@altnovatrail/canvas 0.3.35 → 0.3.37
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/crop.d.ts +4 -2
- package/lib/core/crop.js +4 -3
- package/lib/core/history.d.ts +6 -4
- package/lib/core/history.js +13 -6
- package/lib/core/index.d.ts +2 -1
- package/lib/core/index.js +4 -4
- package/lib/core/marks.d.ts +31 -10
- package/lib/core/marks.js +131 -14
- package/lib/core/submission.d.ts +23 -1
- package/lib/core/submission.js +49 -1
- package/lib/core/tracing.d.ts +110 -0
- package/lib/core/tracing.js +219 -0
- package/lib/core/work.d.ts +16 -6
- package/lib/core/work.js +36 -11
- package/lib/element/icons.d.ts +13 -0
- package/lib/element/icons.js +13 -0
- package/lib/element/index.d.ts +32 -4
- package/lib/element/index.js +589 -65
- package/lib/element/labels.d.ts +7 -0
- package/lib/element/labels.js +10 -0
- package/lib/element/tools.d.ts +11 -3
- package/lib/element/tools.js +48 -6
- package/lib/element/tracing.d.ts +58 -0
- package/lib/element/tracing.js +303 -0
- package/package.json +3 -3
package/lib/element/labels.d.ts
CHANGED
|
@@ -7,11 +7,14 @@
|
|
|
7
7
|
export interface Labels {
|
|
8
8
|
readonly sheet: string;
|
|
9
9
|
readonly workbook: string;
|
|
10
|
+
/** A task from the learner's paper, its crop as tracing paper over a dotted sheet (R87). */
|
|
11
|
+
readonly paper: string;
|
|
10
12
|
/** A lesson's worked example, whose row has no tools of writing (R42). */
|
|
11
13
|
readonly example: string;
|
|
12
14
|
/** The row of buttons of each: not the name of the container, which a screen reader shows too. */
|
|
13
15
|
readonly sheetButtons: string;
|
|
14
16
|
readonly workbookButtons: string;
|
|
17
|
+
readonly paperButtons: string;
|
|
15
18
|
readonly exampleButtons: string;
|
|
16
19
|
/** The buttons of a worked example's demo: the next part, and the whole demo, shown or hidden. */
|
|
17
20
|
readonly next: string;
|
|
@@ -26,6 +29,10 @@ export interface Labels {
|
|
|
26
29
|
readonly zoomOut: string;
|
|
27
30
|
/** The button that lets a finger write again, in pen mode (R29). */
|
|
28
31
|
readonly finger: string;
|
|
32
|
+
/** The tracing paper's grip, and the button atop the tools that hides it or shows it (R87). */
|
|
33
|
+
readonly moveTask: string;
|
|
34
|
+
readonly hideTask: string;
|
|
35
|
+
readonly showTask: string;
|
|
29
36
|
/** A box of a workbook's line, alone in it (R36). */
|
|
30
37
|
readonly box: string;
|
|
31
38
|
/** A box among several of its line, by its slot: {slot} (R36). */
|
package/lib/element/labels.js
CHANGED
|
@@ -15,9 +15,11 @@
|
|
|
15
15
|
const ENGLISH = Object.freeze({
|
|
16
16
|
sheet: "Sheet",
|
|
17
17
|
workbook: "Workbook",
|
|
18
|
+
paper: "Task from paper",
|
|
18
19
|
example: "Worked example",
|
|
19
20
|
sheetButtons: "Sheet buttons",
|
|
20
21
|
workbookButtons: "Workbook buttons",
|
|
22
|
+
paperButtons: "Task from paper buttons",
|
|
21
23
|
exampleButtons: "Worked example buttons",
|
|
22
24
|
next: "Next",
|
|
23
25
|
demo: "Demo",
|
|
@@ -29,6 +31,9 @@ const ENGLISH = Object.freeze({
|
|
|
29
31
|
zoomIn: "Zoom in",
|
|
30
32
|
zoomOut: "Zoom out",
|
|
31
33
|
finger: "Draw with finger",
|
|
34
|
+
moveTask: "Move the task",
|
|
35
|
+
hideTask: "Hide the task",
|
|
36
|
+
showTask: "Show the task",
|
|
32
37
|
box: "Box",
|
|
33
38
|
boxAt: "Box {slot}",
|
|
34
39
|
boxIn: "Box {line}.{slot}",
|
|
@@ -47,9 +52,11 @@ const LABELS = new Map([
|
|
|
47
52
|
Object.freeze({
|
|
48
53
|
sheet: "Лист",
|
|
49
54
|
workbook: "Тетрадь",
|
|
55
|
+
paper: "Задача с бумаги",
|
|
50
56
|
example: "Пример",
|
|
51
57
|
sheetButtons: "Кнопки листа",
|
|
52
58
|
workbookButtons: "Кнопки тетради",
|
|
59
|
+
paperButtons: "Кнопки задачи с бумаги",
|
|
53
60
|
exampleButtons: "Кнопки примера",
|
|
54
61
|
next: "Дальше",
|
|
55
62
|
demo: "Показ",
|
|
@@ -61,6 +68,9 @@ const LABELS = new Map([
|
|
|
61
68
|
zoomIn: "Крупнее",
|
|
62
69
|
zoomOut: "Мельче",
|
|
63
70
|
finger: "Рисовать пальцем",
|
|
71
|
+
moveTask: "Подвинуть задачу",
|
|
72
|
+
hideTask: "Спрятать задачу",
|
|
73
|
+
showTask: "Показать задачу",
|
|
64
74
|
box: "Окошко",
|
|
65
75
|
boxAt: "Окошко {slot}",
|
|
66
76
|
boxIn: "Окошко {line}.{slot}",
|
package/lib/element/tools.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export interface ToolActions {
|
|
|
12
12
|
readonly fingers: () => void;
|
|
13
13
|
readonly next: () => void;
|
|
14
14
|
readonly toggle: () => void;
|
|
15
|
+
/** A task from paper's tracing paper hidden, or shown again (R87). */
|
|
16
|
+
readonly trace: () => void;
|
|
15
17
|
}
|
|
16
18
|
/** What Next and Demo control, by their IDs in the element's shadow root (R42). */
|
|
17
19
|
export interface DemoControls {
|
|
@@ -20,8 +22,11 @@ export interface DemoControls {
|
|
|
20
22
|
/** The list of the open parts, which Demo hides and shows. */
|
|
21
23
|
readonly parts: string;
|
|
22
24
|
}
|
|
23
|
-
/**
|
|
24
|
-
|
|
25
|
+
/**
|
|
26
|
+
* What the bar is of, and is named by: a sheet, a workbook, a task from paper or a worked example
|
|
27
|
+
* (R36, R42, R87).
|
|
28
|
+
*/
|
|
29
|
+
export type RowKind = "sheet" | "workbook" | "paper" | "example";
|
|
25
30
|
/**
|
|
26
31
|
* What the bar shows: what it is of, the tools the task allows, the one in use, whether undo and
|
|
27
32
|
* clearing have anything to do, whether the zoom has a larger step and a smaller one, whether the
|
|
@@ -39,6 +44,8 @@ export interface ToolState {
|
|
|
39
44
|
readonly finger: boolean;
|
|
40
45
|
readonly opens: boolean;
|
|
41
46
|
readonly expanded: boolean;
|
|
47
|
+
/** On a task from paper, whether its tracing paper is shown (R87). */
|
|
48
|
+
readonly traced: boolean;
|
|
42
49
|
}
|
|
43
50
|
/**
|
|
44
51
|
* How long a tooltip stays after the pointer leaves its button, in milliseconds: long enough for
|
|
@@ -67,7 +74,8 @@ export declare class Tools {
|
|
|
67
74
|
* has Next, unavailable while the demo is hidden or open whole, and Demo, expanded while the demo
|
|
68
75
|
* is shown (R42). Zoom in and Zoom out stand unavailable at their ends (R73). The tool in use
|
|
69
76
|
* shows its icon filled, and Demo's icon points down while the demo is shown, and along the row
|
|
70
|
-
* while it is hidden (R46).
|
|
77
|
+
* while it is hidden (R46). A task from paper's button atop the tools says what it does to the
|
|
78
|
+
* tracing paper, and fills its icon while the paper is shown (R87).
|
|
71
79
|
*/
|
|
72
80
|
show(state: ToolState): void;
|
|
73
81
|
/**
|
package/lib/element/tools.js
CHANGED
|
@@ -19,8 +19,12 @@
|
|
|
19
19
|
// that hides the whole demo and shows it again, are actions, not tools (R42; R90 spec): each shows
|
|
20
20
|
// its label beside its icon, a stop of Tab of its own, in a row of glass at the bottom of the
|
|
21
21
|
// element, and after a line the slot `actions`, where the consumer puts its own buttons — app's
|
|
22
|
-
// Continue —, as the mockup has one row (R84; R36 spec). A
|
|
23
|
-
//
|
|
22
|
+
// Continue —, as the mockup has one row (R84; R36 spec). A task from paper's bar has one button
|
|
23
|
+
// more, atop the tools, after a line: "Hide the task" while its tracing paper is shown, its icon
|
|
24
|
+
// filled, and "Show the task" while it is hidden — the name says the state, so the button is no
|
|
25
|
+
// toggle button as well, which would say it twice (R87; R90 spec). A button that goes gives its
|
|
26
|
+
// stop of Tab and the focus to the first button shown. This module is no entry point of the
|
|
27
|
+
// package.
|
|
24
28
|
import { SVG, write } from "./attributes.js";
|
|
25
29
|
import { ICONS, ROW } from "./icons.js";
|
|
26
30
|
import { labelsFor } from "./labels.js";
|
|
@@ -47,6 +51,9 @@ export class Tools {
|
|
|
47
51
|
tip = document.createElement("div");
|
|
48
52
|
#next = plainButton("next");
|
|
49
53
|
#demo = plainButton("demoShown");
|
|
54
|
+
/** A task from paper's button atop the tools, and the line under it (R87). */
|
|
55
|
+
#task = plainButton("task");
|
|
56
|
+
#afterTask = separator();
|
|
50
57
|
#group = document.createElement("div");
|
|
51
58
|
#pen = radioButton("pen");
|
|
52
59
|
#eraser = radioButton("eraser");
|
|
@@ -66,6 +73,7 @@ export class Tools {
|
|
|
66
73
|
#labelled = [
|
|
67
74
|
[this.#next, "next"],
|
|
68
75
|
[this.#demo, "demo"],
|
|
76
|
+
[this.#task, "hideTask"],
|
|
69
77
|
[this.#pen, "pen"],
|
|
70
78
|
[this.#eraser, "eraser"],
|
|
71
79
|
[this.#undo, "undo"],
|
|
@@ -85,6 +93,8 @@ export class Tools {
|
|
|
85
93
|
#lang;
|
|
86
94
|
/** What the bar is of, named so. */
|
|
87
95
|
#kind = "sheet";
|
|
96
|
+
/** Whether a task from paper's tracing paper is shown: the button atop the tools says which. */
|
|
97
|
+
#traced = true;
|
|
88
98
|
/** Whether a button of the bar has had the focus: Tab comes to the first button until one has. */
|
|
89
99
|
#chosen = false;
|
|
90
100
|
/** The button the pointer is over, the one the keyboard's focus is on, and the tooltip's. */
|
|
@@ -141,12 +151,20 @@ export class Tools {
|
|
|
141
151
|
this.#zoomPair.className = "zoom";
|
|
142
152
|
this.#zoomPair.append(this.#zoomIn, this.#zoomOut);
|
|
143
153
|
// The finger's button comes last: coming and going, it moves no other button of the bar.
|
|
144
|
-
this.element.append(this.#group, this.#undo, this.#clear, this.#beforeZoom, this.#zoomPair, this.#beforeFinger, this.#finger);
|
|
154
|
+
this.element.append(this.#task, this.#afterTask, this.#group, this.#undo, this.#clear, this.#beforeZoom, this.#zoomPair, this.#beforeFinger, this.#finger);
|
|
145
155
|
this.rail.append(this.element, this.row, this.tip);
|
|
146
156
|
for (const button of this.#buttons)
|
|
147
157
|
button.classList.add("icon");
|
|
148
158
|
// A worked example's row and the finger's button come with what they are for.
|
|
149
|
-
for (const hidden of [
|
|
159
|
+
for (const hidden of [
|
|
160
|
+
this.row,
|
|
161
|
+
this.#next,
|
|
162
|
+
this.#demo,
|
|
163
|
+
this.#task,
|
|
164
|
+
this.#afterTask,
|
|
165
|
+
this.#beforeFinger,
|
|
166
|
+
this.#finger,
|
|
167
|
+
]) {
|
|
150
168
|
hidden.hidden = true;
|
|
151
169
|
}
|
|
152
170
|
// Tab comes to the first button shown, until another has had the focus.
|
|
@@ -168,6 +186,7 @@ export class Tools {
|
|
|
168
186
|
doing(this.#zoomOut, () => actions.zoom(false));
|
|
169
187
|
this.#demo.addEventListener("click", () => actions.toggle());
|
|
170
188
|
this.#finger.addEventListener("click", () => actions.fingers());
|
|
189
|
+
this.#task.addEventListener("click", () => actions.trace());
|
|
171
190
|
this.element.addEventListener("keydown", (event) => this.#key(event));
|
|
172
191
|
// The button that had the focus last is the one Tab comes back to.
|
|
173
192
|
this.element.addEventListener("focusin", (event) => {
|
|
@@ -209,10 +228,11 @@ export class Tools {
|
|
|
209
228
|
* has Next, unavailable while the demo is hidden or open whole, and Demo, expanded while the demo
|
|
210
229
|
* is shown (R42). Zoom in and Zoom out stand unavailable at their ends (R73). The tool in use
|
|
211
230
|
* shows its icon filled, and Demo's icon points down while the demo is shown, and along the row
|
|
212
|
-
* while it is hidden (R46).
|
|
231
|
+
* while it is hidden (R46). A task from paper's button atop the tools says what it does to the
|
|
232
|
+
* tracing paper, and fills its icon while the paper is shown (R87).
|
|
213
233
|
*/
|
|
214
234
|
show(state) {
|
|
215
|
-
const { kind, tools, tool, undoable, clearable, larger, smaller, finger, opens, expanded } = state;
|
|
235
|
+
const { kind, tools, tool, undoable, clearable, larger, smaller, finger, opens, expanded, traced, } = state;
|
|
216
236
|
if (kind !== this.#kind) {
|
|
217
237
|
this.#kind = kind;
|
|
218
238
|
this.#name();
|
|
@@ -223,6 +243,8 @@ export class Tools {
|
|
|
223
243
|
[this.row, !writes],
|
|
224
244
|
[this.#next, !writes],
|
|
225
245
|
[this.#demo, !writes],
|
|
246
|
+
[this.#task, kind === "paper"],
|
|
247
|
+
[this.#afterTask, kind === "paper"],
|
|
226
248
|
[this.#group, writes],
|
|
227
249
|
[this.#pen, writes],
|
|
228
250
|
[this.#eraser, writes && tools.includes("eraser")],
|
|
@@ -243,6 +265,11 @@ export class Tools {
|
|
|
243
265
|
draw(this.#pen, "pen", tool === "pen");
|
|
244
266
|
draw(this.#eraser, "eraser", tool === "eraser");
|
|
245
267
|
draw(this.#demo, expanded ? "demoShown" : "demoHidden", false);
|
|
268
|
+
draw(this.#task, "task", traced);
|
|
269
|
+
if (traced !== this.#traced) {
|
|
270
|
+
this.#traced = traced;
|
|
271
|
+
this.#nameTask();
|
|
272
|
+
}
|
|
246
273
|
}
|
|
247
274
|
/**
|
|
248
275
|
* The labels in the language of a tag — the element's `lang` — and the bar's own `lang` theirs,
|
|
@@ -263,6 +290,7 @@ export class Tools {
|
|
|
263
290
|
if (label !== null)
|
|
264
291
|
label.textContent = labels[key];
|
|
265
292
|
}
|
|
293
|
+
this.#nameTask();
|
|
266
294
|
this.#tipFor = undefined;
|
|
267
295
|
this.#showTip();
|
|
268
296
|
}
|
|
@@ -274,6 +302,20 @@ export class Tools {
|
|
|
274
302
|
this.#onTip = false;
|
|
275
303
|
this.#showTip();
|
|
276
304
|
}
|
|
305
|
+
/**
|
|
306
|
+
* The name of a task from paper's button atop the tools, by the tracing paper's state, and its
|
|
307
|
+
* tooltip's, should it show (R87).
|
|
308
|
+
*/
|
|
309
|
+
#nameTask() {
|
|
310
|
+
const { labels } = labelsFor(this.#lang ?? null);
|
|
311
|
+
const label = this.#task.lastElementChild;
|
|
312
|
+
const name = this.#traced ? labels.hideTask : labels.showTask;
|
|
313
|
+
if (label === null || label.textContent === name)
|
|
314
|
+
return;
|
|
315
|
+
label.textContent = name;
|
|
316
|
+
if (this.#tipFor === this.#task)
|
|
317
|
+
this.tip.textContent = name;
|
|
318
|
+
}
|
|
277
319
|
/** The bar's name: the canvas kind's buttons, in the labels' language. */
|
|
278
320
|
#name() {
|
|
279
321
|
const { labels } = labelsFor(this.#lang ?? null);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
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
|
+
export interface TracingMoves {
|
|
4
|
+
/** The place the learner moves it to: the element keeps it to the sheet, and gives where it lies. */
|
|
5
|
+
readonly move: (at: readonly [number, number]) => readonly [number, number];
|
|
6
|
+
/** The move is over: a drag let go, or a key pressed. */
|
|
7
|
+
readonly moved: () => void;
|
|
8
|
+
}
|
|
9
|
+
/** Where the sheet is on the stage: `k` CSS pixels a unit, and the visible part's top left corner. */
|
|
10
|
+
export interface StageView {
|
|
11
|
+
readonly k: number;
|
|
12
|
+
readonly vx: number;
|
|
13
|
+
readonly vy: number;
|
|
14
|
+
}
|
|
15
|
+
/** The crop's picture as the element read it: its size on the sheet and its grey pixels. */
|
|
16
|
+
interface Read {
|
|
17
|
+
readonly size: Size;
|
|
18
|
+
readonly gray: Gray | null;
|
|
19
|
+
}
|
|
20
|
+
/** The tracing paper's layer, its grip and the grip's tooltip, and the crop's picture. */
|
|
21
|
+
export declare class TracingPaper {
|
|
22
|
+
#private;
|
|
23
|
+
/** The layer, in sheet units by its view box, which says nothing to a screen reader. */
|
|
24
|
+
readonly layer: SVGSVGElement;
|
|
25
|
+
/** The grip, a button of the stage's own, which takes the pointer as the canvas does. */
|
|
26
|
+
readonly grip: HTMLButtonElement;
|
|
27
|
+
/** The grip's tooltip, which says what its name says already: a screen reader reads that alone. */
|
|
28
|
+
readonly tip: HTMLDivElement;
|
|
29
|
+
constructor(moves: TracingMoves);
|
|
30
|
+
/** The tracing paper's size on the sheet: the largest till the crop's picture is read. */
|
|
31
|
+
get size(): Size;
|
|
32
|
+
/**
|
|
33
|
+
* Whether the paper's size is known: before it, a stroke would be kept to the largest paper and
|
|
34
|
+
* the crop drawn stretched, so the paper waits for its picture.
|
|
35
|
+
*/
|
|
36
|
+
get ready(): boolean;
|
|
37
|
+
/** The crop's picture as read: its size and its grey pixels, or none if it could not be read. */
|
|
38
|
+
get read(): Promise<Read>;
|
|
39
|
+
/**
|
|
40
|
+
* A crop to lay as the tracing paper, or none. Its picture is read at once; `sized` hears when
|
|
41
|
+
* the paper's size is known.
|
|
42
|
+
*/
|
|
43
|
+
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;
|
|
46
|
+
/**
|
|
47
|
+
* The layer and the grip as the tracing paper now is: at `at`, shown or hidden, or not there at
|
|
48
|
+
* all, and the grip where the sheet's view puts the paper's top edge on the stage.
|
|
49
|
+
*/
|
|
50
|
+
place(state: {
|
|
51
|
+
readonly at: readonly [number, number];
|
|
52
|
+
readonly hidden: boolean;
|
|
53
|
+
} | undefined, view: StageView): void;
|
|
54
|
+
connect(): void;
|
|
55
|
+
/** The element left the page: the crop's address goes, and a drag or a tooltip with it. */
|
|
56
|
+
disconnect(): void;
|
|
57
|
+
}
|
|
58
|
+
export {};
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
// The tracing paper of a task from paper (R87): the task's crop over the dotted sheet, on a layer
|
|
2
|
+
// of its own between the author's demo and the learner's ink, in sheet units, so that it moves with
|
|
3
|
+
// the sheet as it scrolls and zooms. The crop is multiplied over the sheet: the white of its paper
|
|
4
|
+
// lets the dots and the strokes under it show, and its print stays dark. Its grip "Move the task",
|
|
5
|
+
// a bar above its top edge, moves it by the pointer — a drag, kept to the sheet — and by the arrow
|
|
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
|
+
// moves under the hand (R90 spec). The grip's tooltip says its name on the hover of a pointer that
|
|
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";
|
|
12
|
+
import { SVG, write } from "./attributes.js";
|
|
13
|
+
/** The grip's size, CSS pixels: a target of 68 × 44 (WCAG 2.5.5), the bar above the paper's edge. */
|
|
14
|
+
const GRIP = Object.freeze({ width: 68, height: 44, over: 29 });
|
|
15
|
+
/** The tracing paper's layer, its grip and the grip's tooltip, and the crop's picture. */
|
|
16
|
+
export class TracingPaper {
|
|
17
|
+
/** The layer, in sheet units by its view box, which says nothing to a screen reader. */
|
|
18
|
+
layer = document.createElementNS(SVG, "svg");
|
|
19
|
+
#image = document.createElementNS(SVG, "image");
|
|
20
|
+
#edge = document.createElementNS(SVG, "rect");
|
|
21
|
+
/** The grip, a button of the stage's own, which takes the pointer as the canvas does. */
|
|
22
|
+
grip = document.createElement("button");
|
|
23
|
+
/** The grip's tooltip, which says what its name says already: a screen reader reads that alone. */
|
|
24
|
+
tip = document.createElement("div");
|
|
25
|
+
#moves;
|
|
26
|
+
#blob;
|
|
27
|
+
#url;
|
|
28
|
+
#connected = false;
|
|
29
|
+
#read = Promise.resolve({ size: LARGEST, gray: null });
|
|
30
|
+
#size = LARGEST;
|
|
31
|
+
/** The picture is read, or could not be: till then the paper is neither shown nor written on. */
|
|
32
|
+
#ready = false;
|
|
33
|
+
/** A drag under way: its pointer, where it began on the screen and the paper then. */
|
|
34
|
+
#drag;
|
|
35
|
+
#at = [0, 0];
|
|
36
|
+
#k = 1;
|
|
37
|
+
#hovered = false;
|
|
38
|
+
#focused = false;
|
|
39
|
+
#dismissed = false;
|
|
40
|
+
#escape = (event) => {
|
|
41
|
+
if (event.key !== "Escape" || this.tip.hidden)
|
|
42
|
+
return;
|
|
43
|
+
this.#dismissed = true;
|
|
44
|
+
this.#showTip();
|
|
45
|
+
};
|
|
46
|
+
constructor(moves) {
|
|
47
|
+
this.#moves = moves;
|
|
48
|
+
this.layer.setAttribute("class", "tracing");
|
|
49
|
+
this.layer.setAttribute("preserveAspectRatio", "none");
|
|
50
|
+
this.layer.setAttribute("aria-hidden", "true");
|
|
51
|
+
this.#image.setAttribute("preserveAspectRatio", "none");
|
|
52
|
+
this.#edge.setAttribute("class", "edge");
|
|
53
|
+
this.layer.append(this.#image, this.#edge);
|
|
54
|
+
this.layer.setAttribute("display", "none");
|
|
55
|
+
this.grip.type = "button";
|
|
56
|
+
this.grip.className = "grip";
|
|
57
|
+
const bar = document.createElement("span");
|
|
58
|
+
bar.className = "bar";
|
|
59
|
+
bar.setAttribute("aria-hidden", "true");
|
|
60
|
+
this.grip.append(bar);
|
|
61
|
+
this.grip.hidden = true;
|
|
62
|
+
this.tip.className = "tip grip-tip";
|
|
63
|
+
this.tip.setAttribute("aria-hidden", "true");
|
|
64
|
+
this.tip.hidden = true;
|
|
65
|
+
this.grip.addEventListener("pointerdown", (event) => this.#down(event));
|
|
66
|
+
this.grip.addEventListener("pointermove", (event) => this.#move(event));
|
|
67
|
+
this.grip.addEventListener("pointerup", (event) => this.#up(event));
|
|
68
|
+
this.grip.addEventListener("pointercancel", (event) => this.#up(event));
|
|
69
|
+
this.grip.addEventListener("lostpointercapture", (event) => this.#up(event));
|
|
70
|
+
this.grip.addEventListener("keydown", (event) => this.#key(event));
|
|
71
|
+
this.grip.addEventListener("pointerenter", (event) => {
|
|
72
|
+
if (event.pointerType === "touch")
|
|
73
|
+
return;
|
|
74
|
+
this.#hovered = true;
|
|
75
|
+
this.#showTip();
|
|
76
|
+
});
|
|
77
|
+
this.grip.addEventListener("pointerleave", () => {
|
|
78
|
+
this.#hovered = false;
|
|
79
|
+
this.#dismissed = false;
|
|
80
|
+
this.#showTip();
|
|
81
|
+
});
|
|
82
|
+
this.grip.addEventListener("focus", () => {
|
|
83
|
+
this.#focused = this.grip.matches(":focus-visible");
|
|
84
|
+
this.#showTip();
|
|
85
|
+
});
|
|
86
|
+
this.grip.addEventListener("blur", () => {
|
|
87
|
+
this.#focused = false;
|
|
88
|
+
this.#dismissed = false;
|
|
89
|
+
this.#showTip();
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/** The tracing paper's size on the sheet: the largest till the crop's picture is read. */
|
|
93
|
+
get size() {
|
|
94
|
+
return this.#size;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Whether the paper's size is known: before it, a stroke would be kept to the largest paper and
|
|
98
|
+
* the crop drawn stretched, so the paper waits for its picture.
|
|
99
|
+
*/
|
|
100
|
+
get ready() {
|
|
101
|
+
return this.#ready;
|
|
102
|
+
}
|
|
103
|
+
/** The crop's picture as read: its size and its grey pixels, or none if it could not be read. */
|
|
104
|
+
get read() {
|
|
105
|
+
return this.#read;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* A crop to lay as the tracing paper, or none. Its picture is read at once; `sized` hears when
|
|
109
|
+
* the paper's size is known.
|
|
110
|
+
*/
|
|
111
|
+
show(blob, sized) {
|
|
112
|
+
if (blob === this.#blob)
|
|
113
|
+
return;
|
|
114
|
+
this.#blob = blob;
|
|
115
|
+
this.#size = LARGEST;
|
|
116
|
+
this.#ready = false;
|
|
117
|
+
this.#release();
|
|
118
|
+
if (blob === undefined) {
|
|
119
|
+
this.#read = Promise.resolve({ size: LARGEST, gray: null });
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (this.#connected)
|
|
123
|
+
this.#address();
|
|
124
|
+
this.#read = readOf(blob).then((read) => {
|
|
125
|
+
if (this.#blob === blob) {
|
|
126
|
+
this.#size = read.size;
|
|
127
|
+
this.#ready = true;
|
|
128
|
+
sized();
|
|
129
|
+
}
|
|
130
|
+
return read;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
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);
|
|
136
|
+
write(this.grip, "lang", lang);
|
|
137
|
+
this.tip.textContent = name;
|
|
138
|
+
write(this.tip, "lang", lang);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The layer and the grip as the tracing paper now is: at `at`, shown or hidden, or not there at
|
|
142
|
+
* all, and the grip where the sheet's view puts the paper's top edge on the stage.
|
|
143
|
+
*/
|
|
144
|
+
place(state, view) {
|
|
145
|
+
this.#at = state?.at ?? [0, 0];
|
|
146
|
+
this.#k = view.k;
|
|
147
|
+
const shown = state !== undefined && !state.hidden && this.#blob !== undefined && this.#ready;
|
|
148
|
+
write(this.layer, "display", shown ? null : "none");
|
|
149
|
+
if (this.grip.hidden === shown)
|
|
150
|
+
this.grip.hidden = !shown;
|
|
151
|
+
if (!shown) {
|
|
152
|
+
this.#showTip();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const [x, y] = this.#at;
|
|
156
|
+
const { width, height } = this.#size;
|
|
157
|
+
for (const shape of [this.#image, this.#edge]) {
|
|
158
|
+
write(shape, "x", String(x));
|
|
159
|
+
write(shape, "y", String(y));
|
|
160
|
+
write(shape, "width", String(width));
|
|
161
|
+
write(shape, "height", String(height));
|
|
162
|
+
}
|
|
163
|
+
const left = (x + width / 2 - view.vx) * view.k - GRIP.width / 2;
|
|
164
|
+
const top = (y - view.vy) * view.k - GRIP.over;
|
|
165
|
+
this.grip.style.transform = `translate(${left}px, ${top}px)`;
|
|
166
|
+
this.tip.style.setProperty("--tip-x", `${left + GRIP.width + 8}px`);
|
|
167
|
+
this.tip.style.setProperty("--tip-y", `${top + GRIP.height / 2}px`);
|
|
168
|
+
this.#showTip();
|
|
169
|
+
}
|
|
170
|
+
connect() {
|
|
171
|
+
this.#connected = true;
|
|
172
|
+
if (this.#blob !== undefined && this.#url === undefined)
|
|
173
|
+
this.#address();
|
|
174
|
+
}
|
|
175
|
+
/** The element left the page: the crop's address goes, and a drag or a tooltip with it. */
|
|
176
|
+
disconnect() {
|
|
177
|
+
this.#connected = false;
|
|
178
|
+
this.#release();
|
|
179
|
+
this.#drag = undefined;
|
|
180
|
+
this.grip.classList.remove("dragging");
|
|
181
|
+
this.#hovered = false;
|
|
182
|
+
this.#focused = false;
|
|
183
|
+
this.#showTip();
|
|
184
|
+
}
|
|
185
|
+
#address() {
|
|
186
|
+
if (this.#blob === undefined)
|
|
187
|
+
return;
|
|
188
|
+
this.#url = URL.createObjectURL(this.#blob);
|
|
189
|
+
this.#image.setAttribute("href", this.#url);
|
|
190
|
+
}
|
|
191
|
+
#release() {
|
|
192
|
+
if (this.#url !== undefined)
|
|
193
|
+
URL.revokeObjectURL(this.#url);
|
|
194
|
+
this.#url = undefined;
|
|
195
|
+
this.#image.removeAttribute("href");
|
|
196
|
+
}
|
|
197
|
+
#down(event) {
|
|
198
|
+
if (event.button !== 0 || this.#drag !== undefined)
|
|
199
|
+
return;
|
|
200
|
+
event.preventDefault();
|
|
201
|
+
try {
|
|
202
|
+
this.grip.setPointerCapture(event.pointerId);
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
// An event a script made, which no pointer backs: the drag goes on by its moves all the same.
|
|
206
|
+
}
|
|
207
|
+
this.#drag = {
|
|
208
|
+
pointerId: event.pointerId,
|
|
209
|
+
x: event.clientX,
|
|
210
|
+
y: event.clientY,
|
|
211
|
+
at: this.#at,
|
|
212
|
+
k: this.#k,
|
|
213
|
+
};
|
|
214
|
+
this.grip.classList.add("dragging");
|
|
215
|
+
this.#dismissed = true;
|
|
216
|
+
this.#showTip();
|
|
217
|
+
}
|
|
218
|
+
#move(event) {
|
|
219
|
+
const drag = this.#drag;
|
|
220
|
+
if (drag === undefined || event.pointerId !== drag.pointerId)
|
|
221
|
+
return;
|
|
222
|
+
this.#at = this.#moves.move([
|
|
223
|
+
drag.at[0] + (event.clientX - drag.x) / drag.k,
|
|
224
|
+
drag.at[1] + (event.clientY - drag.y) / drag.k,
|
|
225
|
+
]);
|
|
226
|
+
}
|
|
227
|
+
#up(event) {
|
|
228
|
+
const drag = this.#drag;
|
|
229
|
+
if (drag === undefined || event.pointerId !== drag.pointerId)
|
|
230
|
+
return;
|
|
231
|
+
if (event.type === "pointerup")
|
|
232
|
+
this.#move(event);
|
|
233
|
+
this.#drag = undefined;
|
|
234
|
+
this.grip.classList.remove("dragging");
|
|
235
|
+
this.#moves.moved();
|
|
236
|
+
}
|
|
237
|
+
/** The arrow keys move the paper a step, and with Shift a leap; no other key is the grip's. */
|
|
238
|
+
#key(event) {
|
|
239
|
+
if (event.altKey || event.ctrlKey || event.metaKey)
|
|
240
|
+
return;
|
|
241
|
+
const by = event.shiftKey ? TRACING.leap : TRACING.step;
|
|
242
|
+
const [x, y] = this.#at;
|
|
243
|
+
let to;
|
|
244
|
+
if (event.key === "ArrowLeft")
|
|
245
|
+
to = [x - by, y];
|
|
246
|
+
else if (event.key === "ArrowRight")
|
|
247
|
+
to = [x + by, y];
|
|
248
|
+
else if (event.key === "ArrowUp")
|
|
249
|
+
to = [x, y - by];
|
|
250
|
+
else if (event.key === "ArrowDown")
|
|
251
|
+
to = [x, y + by];
|
|
252
|
+
else
|
|
253
|
+
return;
|
|
254
|
+
event.preventDefault();
|
|
255
|
+
// Where it lies now, not at the next frame's place(): keys pressed within one frame add up.
|
|
256
|
+
this.#at = this.#moves.move(to);
|
|
257
|
+
this.#moves.moved();
|
|
258
|
+
}
|
|
259
|
+
/** The tooltip on hover or the keyboard's focus, unless Escape or a drag dismissed it. */
|
|
260
|
+
#showTip() {
|
|
261
|
+
const wanted = !this.grip.hidden && (this.#hovered || this.#focused) && !this.#dismissed;
|
|
262
|
+
if (this.tip.hidden === !wanted)
|
|
263
|
+
return;
|
|
264
|
+
this.tip.hidden = !wanted;
|
|
265
|
+
if (wanted)
|
|
266
|
+
document.addEventListener("keydown", this.#escape, true);
|
|
267
|
+
else
|
|
268
|
+
document.removeEventListener("keydown", this.#escape, true);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* The crop's picture, read: decoded by the browser, its size on the sheet by its proportions, and
|
|
273
|
+
* its pixels drawn once at its own size and read back as grey. A picture the browser cannot decode
|
|
274
|
+
* has the largest size and no pixels, and says so in the console.
|
|
275
|
+
*/
|
|
276
|
+
async function readOf(blob) {
|
|
277
|
+
const url = URL.createObjectURL(blob);
|
|
278
|
+
try {
|
|
279
|
+
const image = new Image();
|
|
280
|
+
image.src = url;
|
|
281
|
+
await image.decode();
|
|
282
|
+
const { naturalWidth: width, naturalHeight: height } = image;
|
|
283
|
+
const canvas = document.createElement("canvas");
|
|
284
|
+
canvas.width = width;
|
|
285
|
+
canvas.height = height;
|
|
286
|
+
const context = canvas.getContext("2d", { willReadFrequently: true });
|
|
287
|
+
if (context === null)
|
|
288
|
+
return { size: sizeOf(width, height), gray: null };
|
|
289
|
+
context.drawImage(image, 0, 0);
|
|
290
|
+
const { data } = context.getImageData(0, 0, width, height);
|
|
291
|
+
// The canvas's memory goes at once, not at a collection: WebKit caps what all canvases hold.
|
|
292
|
+
canvas.width = 0;
|
|
293
|
+
canvas.height = 0;
|
|
294
|
+
return { size: sizeOf(width, height), gray: grayOf(data, width, height) };
|
|
295
|
+
}
|
|
296
|
+
catch (error) {
|
|
297
|
+
console.warn("<novatrail-canvas> could not read the task's crop:", error);
|
|
298
|
+
return { size: LARGEST, gray: null };
|
|
299
|
+
}
|
|
300
|
+
finally {
|
|
301
|
+
URL.revokeObjectURL(url);
|
|
302
|
+
}
|
|
303
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@altnovatrail/canvas",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.37",
|
|
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.
|
|
29
|
+
"@altnovatrail/spec": "0.19.4",
|
|
30
30
|
"perfect-freehand": "1.2.3",
|
|
31
31
|
"rbush": "4.0.1"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@altnovatrail/contracts": "0.
|
|
34
|
+
"@altnovatrail/contracts": "0.19.4",
|
|
35
35
|
"@biomejs/biome": "2.6.6",
|
|
36
36
|
"@bufbuild/protobuf": "2.17.0",
|
|
37
37
|
"@iconify-json/material-symbols": "1.2.99",
|