@altnovatrail/canvas 0.3.42 → 0.3.43
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 +17 -2
- package/lib/core/crop.js +45 -17
- package/lib/core/cut.d.ts +46 -0
- package/lib/core/cut.js +297 -0
- package/lib/core/eraser.d.ts +27 -9
- package/lib/core/eraser.js +32 -10
- package/lib/core/history.d.ts +21 -15
- package/lib/core/history.js +234 -28
- package/lib/core/index.d.ts +2 -1
- package/lib/core/index.js +3 -2
- package/lib/core/outline.d.ts +13 -3
- package/lib/core/outline.js +110 -11
- package/lib/core/png.d.ts +4 -3
- package/lib/core/png.js +15 -10
- package/lib/core/raster.d.ts +20 -9
- package/lib/core/raster.js +121 -35
- package/lib/core/recorder.d.ts +13 -2
- package/lib/core/recorder.js +20 -4
- package/lib/core/sheet.d.ts +3 -2
- package/lib/core/sheet.js +6 -4
- package/lib/core/style.d.ts +54 -0
- package/lib/core/style.js +146 -0
- package/lib/core/submission.js +4 -3
- package/lib/core/tracing.d.ts +6 -4
- package/lib/core/tracing.js +15 -7
- package/lib/core/work.d.ts +40 -11
- package/lib/core/work.js +106 -15
- package/lib/element/index.d.ts +37 -15
- package/lib/element/index.js +412 -70
- package/lib/element/labels.d.ts +42 -0
- package/lib/element/labels.js +73 -3
- package/lib/element/pens.d.ts +55 -0
- package/lib/element/pens.js +676 -0
- package/lib/element/tools.d.ts +9 -0
- package/lib/element/tools.js +77 -9
- package/package.json +3 -3
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
// The popovers of the pen and of the eraser, as the mockup of the pen's settings draws them, in
|
|
2
|
+
// the glass of the bar: a second press of the chosen pen opens the pen's — the kind of pen, its
|
|
3
|
+
// width, its opacity and its colour, each kind with its own —, and a second press of the chosen
|
|
4
|
+
// eraser the eraser's — whether it takes a stroke whole or only what it passes over, and its size.
|
|
5
|
+
// Each is a dialog that is no modal one, named in the element's language: its choices are toggle
|
|
6
|
+
// buttons in named groups, the opacity a slider, and the other colours behind a disclosure button.
|
|
7
|
+
// It stands to the right of the bar, level with its button and kept within the element, its arrow
|
|
8
|
+
// at the button, or docked along the element's bottom when the element is narrow. It pops in
|
|
9
|
+
// only to whom motion is no harm. Escape closes it and gives the focus back to its button; the
|
|
10
|
+
// focus leaving it to anywhere but its button closes it too, and so does whatever the bar or the
|
|
11
|
+
// element closes it for. Every target is 44 × 44 at least (WCAG 2.5.5). The popover shows no text
|
|
12
|
+
// on a fill seen through: its fill is the paper's, so that the contrast of its words holds over any
|
|
13
|
+
// ink (WCAG 1.4.3). This module is no entry point of the package.
|
|
14
|
+
import { ERASER_MODES, ERASER_SIZES, MARKER_WIDTHS, OTHER_COLOURS, PALETTE, PEN_KINDS, PEN_SETTINGS, PEN_WIDTHS, penSettingsOf, sameStyle, } from "../core/style.js";
|
|
15
|
+
import { SVG, write } from "./attributes.js";
|
|
16
|
+
import { labelsFor, say } from "./labels.js";
|
|
17
|
+
/** The IDs in the shadow root of the hint the chosen pen and eraser are described by, and of the
|
|
18
|
+
* other colours, which the colour wheel shows and hides. */
|
|
19
|
+
export const HINT_ID = "pens-hint";
|
|
20
|
+
const MORE_ID = "pens-more";
|
|
21
|
+
const MODE_ID = "pens-mode";
|
|
22
|
+
const SIZE_ID = "pens-size";
|
|
23
|
+
/** How narrow an element is, in CSS pixels, below which a popover is docked along its bottom. */
|
|
24
|
+
export const NARROW_PX = 600;
|
|
25
|
+
/** How far a popover keeps from the bar and from the element's edges, in CSS pixels. */
|
|
26
|
+
const GAP_PX = 14;
|
|
27
|
+
const EDGE_PX = 8;
|
|
28
|
+
/** The mockup's sample stroke of a width, on 30 × 26. */
|
|
29
|
+
const SAMPLE = "M4 20c3-8 8-12 11-12 4 0 4 6 1 9-2.6 2.6-5.2 2-4.8-0.5 0.6-3.6 6.4-6.8 9.8-5.5 2 0.8 2.5 3.8 3 9";
|
|
30
|
+
/** The labels of the kinds, of the palette's colours and of the others, in their orders. */
|
|
31
|
+
const KIND_LABELS = {
|
|
32
|
+
pen: "kindPen",
|
|
33
|
+
liner: "kindLiner",
|
|
34
|
+
marker: "kindMarker",
|
|
35
|
+
};
|
|
36
|
+
const PALETTE_LABELS = ["black", "blue", "green", "yellow", "red"];
|
|
37
|
+
const OTHER_LABELS = [
|
|
38
|
+
"grey",
|
|
39
|
+
"purple",
|
|
40
|
+
"orange",
|
|
41
|
+
"teal",
|
|
42
|
+
"pink",
|
|
43
|
+
"brown",
|
|
44
|
+
"skyBlue",
|
|
45
|
+
"lightGreen",
|
|
46
|
+
"amber",
|
|
47
|
+
"violet",
|
|
48
|
+
"scarlet",
|
|
49
|
+
"white",
|
|
50
|
+
];
|
|
51
|
+
const MODE_LABELS = {
|
|
52
|
+
stroke: "wholeStroke",
|
|
53
|
+
part: "partly",
|
|
54
|
+
};
|
|
55
|
+
/** The widths a kind offers: the marker's four times the pen's. */
|
|
56
|
+
export function widthsOf(kind) {
|
|
57
|
+
return kind === "marker" ? MARKER_WIDTHS : PEN_WIDTHS;
|
|
58
|
+
}
|
|
59
|
+
/** Whether two settings say the same, field by field. */
|
|
60
|
+
export function samePens(a, b) {
|
|
61
|
+
return (a === b ||
|
|
62
|
+
(a.pen === b.pen &&
|
|
63
|
+
a.eraser === b.eraser &&
|
|
64
|
+
a.eraserSize === b.eraserSize &&
|
|
65
|
+
PEN_KINDS.every((kind) => sameStyle(a.styles[kind], b.styles[kind]))));
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The colour a stroke of a style is drawn in on the screen: its own, but for the palette's black,
|
|
69
|
+
* which is the theme's ink, so that a dark theme keeps it in sight, and none at all in forced
|
|
70
|
+
* colours, where every stroke is the ink. `ink` is the ink's colour as the theme gives it.
|
|
71
|
+
*/
|
|
72
|
+
export function screenColourOf(style, ink, forced) {
|
|
73
|
+
if (style === undefined || forced || style.color === PALETTE[0])
|
|
74
|
+
return ink;
|
|
75
|
+
return style.color;
|
|
76
|
+
}
|
|
77
|
+
/** The popovers' style, of the element's one sheet, in its colours unless the theme gives its own. */
|
|
78
|
+
export function pensStyle(ink, paper, accent) {
|
|
79
|
+
const INK = `var(--novatrail-canvas-ink, ${ink})`;
|
|
80
|
+
const PAPER = `var(--novatrail-canvas-paper, ${paper})`;
|
|
81
|
+
const ACCENT = `var(--novatrail-canvas-accent, ${accent})`;
|
|
82
|
+
const SECONDARY = `color-mix(in srgb, ${INK} 62%, ${PAPER})`;
|
|
83
|
+
const HAIR = `color-mix(in srgb, ${INK} 14%, transparent)`;
|
|
84
|
+
return `
|
|
85
|
+
.tools .settings { position: relative; }
|
|
86
|
+
.tools .settings[aria-checked="true"]::after {
|
|
87
|
+
content: ""; position: absolute; right: 7px; bottom: 7px; width: 0; height: 0;
|
|
88
|
+
border-left: 4px solid transparent; border-top: 4px solid transparent;
|
|
89
|
+
border-right: 4px solid currentColor; border-bottom: 4px solid currentColor;
|
|
90
|
+
border-radius: 0 0 2px 0; opacity: 0.55;
|
|
91
|
+
}
|
|
92
|
+
.tools .settings.dotted::before {
|
|
93
|
+
content: ""; position: absolute; right: 7px; top: 7px; width: 8px; height: 8px;
|
|
94
|
+
box-sizing: border-box; border-radius: 50%; background: var(--pen-dot, ${INK});
|
|
95
|
+
box-shadow: 0 0 0 1px color-mix(in srgb, ${INK} 25%, transparent); forced-color-adjust: none;
|
|
96
|
+
}
|
|
97
|
+
.pop {
|
|
98
|
+
position: absolute; left: var(--pop-x, 76px); top: var(--pop-y, 0px); z-index: 3;
|
|
99
|
+
box-sizing: border-box; width: 320px; border-radius: 22px; font-size: 15px; line-height: 1.35; color: ${INK}; background: ${PAPER};
|
|
100
|
+
box-shadow: 0 0 0 0.5px rgba(0, 0, 0, 0.1), 0 18px 50px rgba(0, 0, 0, 0.16),
|
|
101
|
+
0 2px 6px rgba(0, 0, 0, 0.06);
|
|
102
|
+
transform-origin: left center;
|
|
103
|
+
}
|
|
104
|
+
.pop.docked { width: var(--pop-width, auto); transform-origin: bottom center; }
|
|
105
|
+
.pop[hidden] { display: none; }
|
|
106
|
+
.pop > .arrow {
|
|
107
|
+
position: absolute; left: -7px; top: var(--arrow, 50%); width: 14px; height: 14px;
|
|
108
|
+
margin-top: -7px; background: ${PAPER}; transform: rotate(45deg); border-radius: 2px;
|
|
109
|
+
box-shadow: -0.5px 0.5px 0 rgba(0, 0, 0, 0.08);
|
|
110
|
+
}
|
|
111
|
+
.pop.docked > .arrow { display: none; }
|
|
112
|
+
.pop .body {
|
|
113
|
+
box-sizing: border-box; max-height: var(--pop-max, none); overflow-y: auto; padding: 14px;
|
|
114
|
+
overscroll-behavior: contain; scrollbar-width: none; border-radius: inherit;
|
|
115
|
+
}
|
|
116
|
+
.pop section + section { margin-top: 14px; padding-top: 14px; border-top: 1px solid ${HAIR}; }
|
|
117
|
+
.pop .heading { margin: 0 0 10px; font-size: 13px; font-weight: 600; color: ${SECONDARY}; }
|
|
118
|
+
.pop button {
|
|
119
|
+
box-sizing: border-box; min-width: 44px; min-height: 44px; margin: 0; padding: 0;
|
|
120
|
+
font: inherit; color: inherit; background: transparent; border: none; cursor: pointer;
|
|
121
|
+
}
|
|
122
|
+
.pop button:focus-visible, .pop input:focus-visible {
|
|
123
|
+
outline: 2px solid ${ACCENT}; outline-offset: 2px;
|
|
124
|
+
}
|
|
125
|
+
.pop .kinds { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
|
|
126
|
+
.pop .kind {
|
|
127
|
+
border-radius: 14px; padding: 6px 4px 8px; display: flex; flex-direction: column;
|
|
128
|
+
align-items: center; gap: 4px; font-size: 12px; color: ${SECONDARY};
|
|
129
|
+
}
|
|
130
|
+
.pop .kind svg { display: block; width: 30px; height: 64px; forced-color-adjust: none; }
|
|
131
|
+
.pop .kind[aria-pressed="true"] {
|
|
132
|
+
background: color-mix(in srgb, ${ACCENT} 12%, transparent); color: ${INK}; font-weight: 600;
|
|
133
|
+
}
|
|
134
|
+
.pop .widths { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; }
|
|
135
|
+
.pop .width { height: 48px; border-radius: 12px; display: grid; place-items: center; }
|
|
136
|
+
.pop .width svg { display: block; width: 38px; height: 30px; overflow: visible; }
|
|
137
|
+
.pop .width[aria-pressed="true"] { background: ${INK}; color: ${PAPER}; }
|
|
138
|
+
.pop .width[aria-pressed="true"] :is(path, circle) { stroke: currentColor; stroke-opacity: 1; }
|
|
139
|
+
.pop .opacity { display: flex; align-items: center; gap: 12px; margin-top: 12px; }
|
|
140
|
+
.pop .opacity output {
|
|
141
|
+
flex: none; min-width: 44px; text-align: end; font-size: 13px; color: ${SECONDARY};
|
|
142
|
+
font-variant-numeric: tabular-nums;
|
|
143
|
+
}
|
|
144
|
+
.pop input[type="range"] {
|
|
145
|
+
user-select: none; -webkit-user-select: none;
|
|
146
|
+
-webkit-appearance: none; appearance: none; flex: 1 1 auto; min-width: 0; height: 44px;
|
|
147
|
+
margin: 0; padding: 8px 0; box-sizing: border-box; border-radius: 22px; background-clip: content-box;
|
|
148
|
+
background-color: transparent;
|
|
149
|
+
background-image: linear-gradient(90deg, color-mix(in srgb, ${INK} 4%, transparent),
|
|
150
|
+
var(--swatch, ${INK}));
|
|
151
|
+
touch-action: none; cursor: pointer;
|
|
152
|
+
}
|
|
153
|
+
.pop input[type="range"]::-webkit-slider-thumb {
|
|
154
|
+
-webkit-appearance: none; appearance: none; width: 26px; height: 26px; border-radius: 50%;
|
|
155
|
+
background: #fff; border: 3px solid #fff;
|
|
156
|
+
box-shadow: 0 0 0 0.5px rgba(0, 0, 0, 0.12), 0 2px 6px rgba(0, 0, 0, 0.2);
|
|
157
|
+
}
|
|
158
|
+
.pop input[type="range"]::-moz-range-thumb {
|
|
159
|
+
width: 22px; height: 22px; border-radius: 50%; background: #fff; border: 0;
|
|
160
|
+
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
|
|
161
|
+
}
|
|
162
|
+
.pop .swatches, .pop .more {
|
|
163
|
+
display: grid; grid-template-columns: repeat(6, 44px); justify-content: space-between;
|
|
164
|
+
row-gap: 4px;
|
|
165
|
+
}
|
|
166
|
+
.pop .more { margin-top: 8px; }
|
|
167
|
+
.pop .more[hidden] { display: none; }
|
|
168
|
+
.pop .swatch { position: relative; border-radius: 50%; forced-color-adjust: none; }
|
|
169
|
+
.pop .swatch::before {
|
|
170
|
+
content: ""; position: absolute; inset: 5px; border-radius: 50%; background: var(--c, ${INK});
|
|
171
|
+
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.2);
|
|
172
|
+
}
|
|
173
|
+
.pop .swatch[aria-pressed="true"]::before {
|
|
174
|
+
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.2), 0 0 0 2px ${PAPER}, 0 0 0 4px ${ACCENT};
|
|
175
|
+
}
|
|
176
|
+
.pop .swatch.wheel::before {
|
|
177
|
+
background: conic-gradient(#ff3b30, #ff9500, #ffcc00, #34c759, #00c7be, #0a84ff, #5e5ce6,
|
|
178
|
+
#bf5af2, #ff2d55, #ff3b30);
|
|
179
|
+
}
|
|
180
|
+
.pop .swatch.wheel[aria-pressed="true"]::after {
|
|
181
|
+
content: ""; position: absolute; inset: 14px; border-radius: 50%; background: var(--c);
|
|
182
|
+
box-shadow: 0 0 0 2px ${PAPER};
|
|
183
|
+
}
|
|
184
|
+
.pop .seg {
|
|
185
|
+
display: flex; padding: 2px; border-radius: 12px;
|
|
186
|
+
background: color-mix(in srgb, rgb(118, 118, 128) 12%, transparent);
|
|
187
|
+
}
|
|
188
|
+
.pop .seg button { flex: 1 1 0; border-radius: 10px; padding: 0 6px; font-size: 13px; }
|
|
189
|
+
.pop .seg button[aria-pressed="true"] {
|
|
190
|
+
background: ${PAPER}; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); font-weight: 600;
|
|
191
|
+
}
|
|
192
|
+
.pop .seg button:focus-visible { outline-offset: -2px; }
|
|
193
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
194
|
+
.pop:not([hidden]) { animation: novatrail-pop 160ms cubic-bezier(0.2, 0.9, 0.3, 1.2); }
|
|
195
|
+
}
|
|
196
|
+
@keyframes novatrail-pop { from { opacity: 0; transform: scale(0.94); } }
|
|
197
|
+
@media (forced-colors: active) {
|
|
198
|
+
.pop { background: Canvas; color: CanvasText; border: 1px solid CanvasText; }
|
|
199
|
+
.pop > .arrow { background: Canvas; border-left: 1px solid CanvasText; border-bottom: 1px solid CanvasText; }
|
|
200
|
+
.pop section + section { border-top-color: CanvasText; }
|
|
201
|
+
.pop .heading, .pop .opacity output { color: CanvasText; }
|
|
202
|
+
.pop button { background: ButtonFace; color: ButtonText; border: 1px solid ButtonBorder; }
|
|
203
|
+
.pop .swatch { background: Canvas; border-color: transparent; }
|
|
204
|
+
.pop .swatch::before { box-shadow: inset 0 0 0 1px CanvasText; }
|
|
205
|
+
.pop .swatch[aria-pressed="true"]::before {
|
|
206
|
+
box-shadow: inset 0 0 0 1px CanvasText, 0 0 0 2px Canvas, 0 0 0 4px Highlight;
|
|
207
|
+
}
|
|
208
|
+
.pop .swatch.wheel[aria-pressed="true"]::after { box-shadow: 0 0 0 2px Canvas; }
|
|
209
|
+
.pop .kind[aria-pressed="true"], .pop .width[aria-pressed="true"],
|
|
210
|
+
.pop .seg button[aria-pressed="true"] {
|
|
211
|
+
background: Highlight; color: HighlightText; border-color: Highlight;
|
|
212
|
+
}
|
|
213
|
+
.pop .width :is(path, circle) { stroke: currentColor; stroke-opacity: 1; }
|
|
214
|
+
.pop .seg { background: Canvas; }
|
|
215
|
+
.pop button:focus-visible, .pop input:focus-visible { outline-color: Highlight; }
|
|
216
|
+
.tools .settings[aria-checked="true"]::after { border-right-color: HighlightText; border-bottom-color: HighlightText; }
|
|
217
|
+
.tools .settings.dotted::before { box-shadow: 0 0 0 1px ButtonText; }
|
|
218
|
+
}
|
|
219
|
+
`;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* The popovers of the pen and of the eraser, as the bar shows them: at most one open at a time,
|
|
223
|
+
* by its tool's button, and every choice in it a change of the settings the element decides on.
|
|
224
|
+
*/
|
|
225
|
+
export class Pens {
|
|
226
|
+
pen = dialog();
|
|
227
|
+
eraser = dialog();
|
|
228
|
+
/** What the chosen pen and eraser say of a second press, which a screen reader reads. */
|
|
229
|
+
hint = document.createElement("span");
|
|
230
|
+
#choose;
|
|
231
|
+
/** The bar asks to hear when a popover opens or closes: its tooltips give way to it. */
|
|
232
|
+
#toggled;
|
|
233
|
+
#kinds = PEN_KINDS.map((kind) => kindButton(kind));
|
|
234
|
+
#kindGroup = group("kinds");
|
|
235
|
+
#widths = PEN_WIDTHS.map(() => toggle("width", sampleOf()));
|
|
236
|
+
#widthGroup = group("widths");
|
|
237
|
+
#opacity = document.createElement("input");
|
|
238
|
+
#opacityShown = document.createElement("output");
|
|
239
|
+
#swatchGroup = group("swatches");
|
|
240
|
+
#palette = PALETTE.map((colour) => swatch(colour));
|
|
241
|
+
#wheel = toggle("swatch wheel");
|
|
242
|
+
#more = group("more");
|
|
243
|
+
#others = OTHER_COLOURS.map((colour) => swatch(colour));
|
|
244
|
+
#modeHeading = heading(MODE_ID);
|
|
245
|
+
#modes = ERASER_MODES.map(() => toggle(""));
|
|
246
|
+
#sizeHeading = heading(SIZE_ID);
|
|
247
|
+
#sizes = ERASER_SIZES.map((size) => toggle("width", circleOf(size)));
|
|
248
|
+
#settings = PEN_SETTINGS;
|
|
249
|
+
#open;
|
|
250
|
+
#opener;
|
|
251
|
+
#lang;
|
|
252
|
+
/**
|
|
253
|
+
* Escape, heard on the document while a popover is open, wherever the focus is: on its button
|
|
254
|
+
* after a press of the pointer too. The focus goes back to the button from within the popover.
|
|
255
|
+
*/
|
|
256
|
+
#escape = (event) => {
|
|
257
|
+
if (event.key !== "Escape" || this.#open === undefined)
|
|
258
|
+
return;
|
|
259
|
+
const pop = this.#open === "pen" ? this.pen : this.eraser;
|
|
260
|
+
const root = pop.getRootNode();
|
|
261
|
+
const focused = root instanceof ShadowRoot ? root.activeElement : null;
|
|
262
|
+
event.preventDefault();
|
|
263
|
+
this.close(focused !== null && pop.contains(focused));
|
|
264
|
+
};
|
|
265
|
+
constructor(choose, toggled) {
|
|
266
|
+
this.#choose = choose;
|
|
267
|
+
this.#toggled = toggled;
|
|
268
|
+
this.hint.id = HINT_ID;
|
|
269
|
+
this.hint.hidden = true;
|
|
270
|
+
this.#kindGroup.append(...this.#kinds);
|
|
271
|
+
this.#widthGroup.append(...this.#widths);
|
|
272
|
+
this.#opacity.type = "range";
|
|
273
|
+
this.#opacity.min = "10";
|
|
274
|
+
this.#opacity.max = "100";
|
|
275
|
+
this.#opacity.step = "1";
|
|
276
|
+
// The slider says its value already: the output is for the eye, and no live region.
|
|
277
|
+
this.#opacityShown.setAttribute("aria-hidden", "true");
|
|
278
|
+
const opacity = document.createElement("div");
|
|
279
|
+
opacity.className = "opacity";
|
|
280
|
+
opacity.append(this.#opacity, this.#opacityShown);
|
|
281
|
+
this.#more.id = MORE_ID;
|
|
282
|
+
this.#more.hidden = true;
|
|
283
|
+
this.#more.append(...this.#others);
|
|
284
|
+
this.#wheel.setAttribute("aria-controls", MORE_ID);
|
|
285
|
+
this.#wheel.setAttribute("aria-expanded", "false");
|
|
286
|
+
this.#swatchGroup.append(...this.#palette, this.#wheel);
|
|
287
|
+
this.pen.append(body(section(this.#kindGroup), section(this.#widthGroup, opacity), section(this.#swatchGroup, this.#more)));
|
|
288
|
+
const modes = document.createElement("div");
|
|
289
|
+
modes.className = "seg";
|
|
290
|
+
modes.setAttribute("role", "group");
|
|
291
|
+
modes.setAttribute("aria-labelledby", MODE_ID);
|
|
292
|
+
modes.append(...this.#modes);
|
|
293
|
+
const sizes = group("widths");
|
|
294
|
+
sizes.removeAttribute("aria-label");
|
|
295
|
+
sizes.setAttribute("aria-labelledby", SIZE_ID);
|
|
296
|
+
sizes.append(...this.#sizes);
|
|
297
|
+
this.eraser.append(body(section(this.#modeHeading, modes), section(this.#sizeHeading, sizes)));
|
|
298
|
+
for (const [k, button] of this.#kinds.entries()) {
|
|
299
|
+
button.addEventListener("click", () => this.#change({ pen: PEN_KINDS[k] }));
|
|
300
|
+
}
|
|
301
|
+
for (const [k, button] of this.#widths.entries()) {
|
|
302
|
+
button.addEventListener("click", () => {
|
|
303
|
+
const width = widthsOf(this.#settings.pen)[k];
|
|
304
|
+
if (width !== undefined)
|
|
305
|
+
this.#restyle({ width });
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
this.#opacity.addEventListener("input", () => {
|
|
309
|
+
this.#restyle({ opacity: Number(this.#opacity.value) });
|
|
310
|
+
});
|
|
311
|
+
for (const button of [...this.#palette, ...this.#others]) {
|
|
312
|
+
button.addEventListener("click", () => {
|
|
313
|
+
const color = button.dataset.colour;
|
|
314
|
+
if (color !== undefined)
|
|
315
|
+
this.#restyle({ color });
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
this.#wheel.addEventListener("click", () => {
|
|
319
|
+
this.#more.hidden = !this.#more.hidden;
|
|
320
|
+
write(this.#wheel, "aria-expanded", String(!this.#more.hidden));
|
|
321
|
+
this.place();
|
|
322
|
+
});
|
|
323
|
+
for (const [k, button] of this.#modes.entries()) {
|
|
324
|
+
button.addEventListener("click", () => this.#change({ eraser: ERASER_MODES[k] }));
|
|
325
|
+
}
|
|
326
|
+
for (const [k, button] of this.#sizes.entries()) {
|
|
327
|
+
button.addEventListener("click", () => this.#change({ eraserSize: ERASER_SIZES[k] }));
|
|
328
|
+
}
|
|
329
|
+
for (const [which, pop] of [
|
|
330
|
+
["pen", this.pen],
|
|
331
|
+
["eraser", this.eraser],
|
|
332
|
+
]) {
|
|
333
|
+
// The focus gone to another of the element's controls, or out of it, takes the popover
|
|
334
|
+
// with it, but not to its own button, whose press closes it, nor to nowhere: Safari takes the
|
|
335
|
+
// focus off a button it presses, and the window may lose it for a while. A popover already
|
|
336
|
+
// closed can still lose the focus it kept while hidden, to the other one as it opens.
|
|
337
|
+
pop.addEventListener("focusout", (event) => {
|
|
338
|
+
const to = event.relatedTarget;
|
|
339
|
+
if (this.#open !== which)
|
|
340
|
+
return;
|
|
341
|
+
if (!(to instanceof Node) || pop.contains(to) || to === this.#opener)
|
|
342
|
+
return;
|
|
343
|
+
this.#hide();
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
this.label(null);
|
|
347
|
+
this.show(PEN_SETTINGS);
|
|
348
|
+
}
|
|
349
|
+
/** The tool whose popover is open, if one is. */
|
|
350
|
+
get opened() {
|
|
351
|
+
return this.#open;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* A tool's popover opens by its button, and the other's closes. From the keyboard the focus goes
|
|
355
|
+
* to its first choice: the kind of pen in use, or the eraser's mode.
|
|
356
|
+
*/
|
|
357
|
+
open(which, opener, focus) {
|
|
358
|
+
const pop = which === "pen" ? this.pen : this.eraser;
|
|
359
|
+
(which === "pen" ? this.eraser : this.pen).hidden = true;
|
|
360
|
+
this.#open = which;
|
|
361
|
+
this.#opener = opener;
|
|
362
|
+
pop.hidden = false;
|
|
363
|
+
document.addEventListener("keydown", this.#escape, true);
|
|
364
|
+
this.place();
|
|
365
|
+
this.#toggled();
|
|
366
|
+
if (!focus)
|
|
367
|
+
return;
|
|
368
|
+
const choices = which === "pen" ? this.#kinds : this.#modes;
|
|
369
|
+
const first = choices.find((button) => button.getAttribute("aria-pressed") === "true");
|
|
370
|
+
(first ?? choices[0])?.focus();
|
|
371
|
+
}
|
|
372
|
+
/** The popover open closes, the focus back on its button if asked, or if the popover held it. */
|
|
373
|
+
close(refocus = false) {
|
|
374
|
+
const opener = this.#opener;
|
|
375
|
+
if (this.#open === undefined)
|
|
376
|
+
return;
|
|
377
|
+
const root = this.pen.getRootNode();
|
|
378
|
+
const focused = root instanceof ShadowRoot ? root.activeElement : null;
|
|
379
|
+
const held = focused !== null && (this.pen.contains(focused) || this.eraser.contains(focused));
|
|
380
|
+
this.#hide();
|
|
381
|
+
// The focus a popover held would be lost with it: it goes back to the popover's button.
|
|
382
|
+
if (refocus || held)
|
|
383
|
+
opener?.focus();
|
|
384
|
+
}
|
|
385
|
+
#hide() {
|
|
386
|
+
this.#open = undefined;
|
|
387
|
+
this.#opener = undefined;
|
|
388
|
+
document.removeEventListener("keydown", this.#escape, true);
|
|
389
|
+
this.pen.hidden = true;
|
|
390
|
+
this.eraser.hidden = true;
|
|
391
|
+
this.#more.hidden = true;
|
|
392
|
+
write(this.#wheel, "aria-expanded", "false");
|
|
393
|
+
this.#toggled();
|
|
394
|
+
}
|
|
395
|
+
/** The popovers as the settings are: only what has changed is written. */
|
|
396
|
+
show(settings) {
|
|
397
|
+
this.#settings = settings;
|
|
398
|
+
const style = settings.styles[settings.pen];
|
|
399
|
+
for (const [k, button] of this.#kinds.entries()) {
|
|
400
|
+
const kind = PEN_KINDS[k];
|
|
401
|
+
write(button, "aria-pressed", String(kind === settings.pen));
|
|
402
|
+
for (const tint of button.querySelectorAll(".tint")) {
|
|
403
|
+
write(tint, "fill", settings.styles[kind].color);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const widths = widthsOf(settings.pen);
|
|
407
|
+
const marker = settings.pen === "marker";
|
|
408
|
+
for (const [k, button] of this.#widths.entries()) {
|
|
409
|
+
const width = widths[k] ?? 0;
|
|
410
|
+
write(button, "aria-pressed", String(width === style.width));
|
|
411
|
+
const path = button.querySelector("path");
|
|
412
|
+
if (path === null)
|
|
413
|
+
continue;
|
|
414
|
+
write(path, "stroke-width", String(Math.min(9, Math.max(1, width * 0.55))));
|
|
415
|
+
write(path, "stroke", marker ? style.color : "currentColor");
|
|
416
|
+
write(path, "stroke-opacity", marker ? "0.7" : "1");
|
|
417
|
+
}
|
|
418
|
+
const opacity = String(style.opacity);
|
|
419
|
+
if (this.#opacity.value !== opacity)
|
|
420
|
+
this.#opacity.value = opacity;
|
|
421
|
+
this.#opacity.style.setProperty("--swatch", style.color);
|
|
422
|
+
const inPalette = PALETTE.includes(style.color);
|
|
423
|
+
for (const button of [...this.#palette, ...this.#others]) {
|
|
424
|
+
write(button, "aria-pressed", String(button.dataset.colour === style.color));
|
|
425
|
+
}
|
|
426
|
+
write(this.#wheel, "aria-pressed", String(!inPalette));
|
|
427
|
+
this.#wheel.style.setProperty("--c", style.color);
|
|
428
|
+
for (const [k, button] of this.#modes.entries()) {
|
|
429
|
+
write(button, "aria-pressed", String(ERASER_MODES[k] === settings.eraser));
|
|
430
|
+
}
|
|
431
|
+
for (const [k, button] of this.#sizes.entries()) {
|
|
432
|
+
write(button, "aria-pressed", String(ERASER_SIZES[k] === settings.eraserSize));
|
|
433
|
+
}
|
|
434
|
+
this.#nameOpacity();
|
|
435
|
+
}
|
|
436
|
+
/** The popovers' words in the language of a tag, the element's `lang`, and their `lang` theirs. */
|
|
437
|
+
label(tag) {
|
|
438
|
+
const { lang, labels } = labelsFor(tag);
|
|
439
|
+
if (lang === this.#lang)
|
|
440
|
+
return;
|
|
441
|
+
this.#lang = lang;
|
|
442
|
+
for (const node of [this.pen, this.eraser, this.hint])
|
|
443
|
+
node.lang = lang;
|
|
444
|
+
this.hint.textContent = labels.settingsHint;
|
|
445
|
+
this.pen.setAttribute("aria-label", labels.penSettings);
|
|
446
|
+
this.eraser.setAttribute("aria-label", labels.eraserSettings);
|
|
447
|
+
for (const [k, button] of this.#kinds.entries()) {
|
|
448
|
+
const name = button.lastElementChild;
|
|
449
|
+
if (name !== null)
|
|
450
|
+
name.textContent = labels[KIND_LABELS[PEN_KINDS[k]]];
|
|
451
|
+
}
|
|
452
|
+
this.#kindGroup.setAttribute("aria-label", labels.penKind);
|
|
453
|
+
this.#widthGroup.setAttribute("aria-label", labels.widths);
|
|
454
|
+
const total = PEN_WIDTHS.length;
|
|
455
|
+
for (const [k, button] of this.#widths.entries()) {
|
|
456
|
+
button.setAttribute("aria-label", say(labels.widthAt, { n: k + 1, total }));
|
|
457
|
+
}
|
|
458
|
+
this.#opacity.setAttribute("aria-label", labels.opacity);
|
|
459
|
+
this.#swatchGroup.setAttribute("aria-label", labels.colour);
|
|
460
|
+
for (const [k, button] of this.#palette.entries()) {
|
|
461
|
+
button.setAttribute("aria-label", labels[PALETTE_LABELS[k]]);
|
|
462
|
+
}
|
|
463
|
+
this.#wheel.setAttribute("aria-label", labels.otherColours);
|
|
464
|
+
this.#more.setAttribute("aria-label", labels.otherColours);
|
|
465
|
+
for (const [k, button] of this.#others.entries()) {
|
|
466
|
+
button.setAttribute("aria-label", labels[OTHER_LABELS[k]]);
|
|
467
|
+
}
|
|
468
|
+
this.#modeHeading.textContent = labels.whatToErase;
|
|
469
|
+
for (const [k, button] of this.#modes.entries()) {
|
|
470
|
+
button.textContent = labels[MODE_LABELS[ERASER_MODES[k]]];
|
|
471
|
+
}
|
|
472
|
+
this.#sizeHeading.textContent = labels.size;
|
|
473
|
+
for (const [k, button] of this.#sizes.entries()) {
|
|
474
|
+
button.setAttribute("aria-label", say(labels.sizeAt, { n: k + 1, total: ERASER_SIZES.length }));
|
|
475
|
+
}
|
|
476
|
+
this.#nameOpacity(true);
|
|
477
|
+
this.place();
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* The popover open stands to the right of the bar, level with the middle of its button as far as
|
|
481
|
+
* the element lets it, its arrow at the button; in an element narrower than `NARROW_PX`, along
|
|
482
|
+
* the element's bottom, `EDGE_PX` off its sides and above the consumer's bar at its bottom. It is
|
|
483
|
+
* kept within the part of the element in sight, and scrolls in itself when taller than that.
|
|
484
|
+
* Read from the layout, which a page's transform may scale: the places are the rail's own.
|
|
485
|
+
*/
|
|
486
|
+
place() {
|
|
487
|
+
const opener = this.#opener;
|
|
488
|
+
const pop = this.#open === "pen" ? this.pen : this.eraser;
|
|
489
|
+
const rail = pop.parentElement;
|
|
490
|
+
if (this.#open === undefined || opener === undefined || rail === null)
|
|
491
|
+
return;
|
|
492
|
+
const root = rail.getRootNode();
|
|
493
|
+
const host = root instanceof ShadowRoot ? root.host : rail;
|
|
494
|
+
const outer = rail.getBoundingClientRect();
|
|
495
|
+
const scale = rail.offsetHeight > 0 ? outer.height / rail.offsetHeight : 1;
|
|
496
|
+
const box = host.getBoundingClientRect();
|
|
497
|
+
const below = Number.parseFloat(getComputedStyle(host).getPropertyValue("--novatrail-canvas-pad-bottom"));
|
|
498
|
+
const top = Math.max(box.top, 0) + EDGE_PX * scale;
|
|
499
|
+
const bottom = Math.min(box.bottom, innerHeight) - ((Number.isFinite(below) ? below : 0) + EDGE_PX) * scale;
|
|
500
|
+
const narrow = box.width / scale < NARROW_PX;
|
|
501
|
+
pop.classList.toggle("docked", narrow);
|
|
502
|
+
const set = (name, client) => pop.style.setProperty(name, `${client / scale}px`);
|
|
503
|
+
set("--pop-max", Math.max(0, bottom - top));
|
|
504
|
+
if (narrow)
|
|
505
|
+
set("--pop-width", box.width - 2 * EDGE_PX * scale);
|
|
506
|
+
const height = pop.offsetHeight * scale;
|
|
507
|
+
if (narrow) {
|
|
508
|
+
set("--pop-x", box.left + EDGE_PX * scale - outer.left);
|
|
509
|
+
set("--pop-y", Math.max(top, bottom - height) - outer.top);
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
const bar = (opener.closest(".tools") ?? opener).getBoundingClientRect();
|
|
513
|
+
const button = opener.getBoundingClientRect();
|
|
514
|
+
const middle = button.top + button.height / 2;
|
|
515
|
+
const y = Math.max(top, Math.min(middle - height / 2, bottom - height));
|
|
516
|
+
set("--pop-x", bar.right + GAP_PX * scale - outer.left);
|
|
517
|
+
set("--pop-y", y - outer.top);
|
|
518
|
+
// The arrow stays on the popover's side, clear of its round corners.
|
|
519
|
+
const corner = 22 * scale;
|
|
520
|
+
set("--arrow", Math.max(corner, Math.min(middle - y, height - corner)));
|
|
521
|
+
}
|
|
522
|
+
/** The opacity's value, said and shown in the labels' language. */
|
|
523
|
+
#nameOpacity(anew = false) {
|
|
524
|
+
const { labels } = labelsFor(this.#lang ?? null);
|
|
525
|
+
const style = this.#settings.styles[this.#settings.pen];
|
|
526
|
+
const said = say(labels.percent, { percent: style.opacity });
|
|
527
|
+
if (!anew && this.#opacity.getAttribute("aria-valuetext") === said)
|
|
528
|
+
return;
|
|
529
|
+
this.#opacity.setAttribute("aria-valuetext", said);
|
|
530
|
+
this.#opacityShown.textContent = said;
|
|
531
|
+
}
|
|
532
|
+
/** A choice of the style of the kind in use. */
|
|
533
|
+
#restyle(patch) {
|
|
534
|
+
const { styles, pen } = this.#settings;
|
|
535
|
+
this.#change({ styles: { ...styles, [pen]: { ...styles[pen], ...patch } } });
|
|
536
|
+
}
|
|
537
|
+
/** A choice: the settings with it, which the element takes and tells of, if they changed. */
|
|
538
|
+
#change(patch) {
|
|
539
|
+
const next = penSettingsOf({ ...this.#settings, ...patch });
|
|
540
|
+
if (samePens(next, this.#settings))
|
|
541
|
+
return;
|
|
542
|
+
this.#choose(next);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* A popover: a dialog that is no modal one, hidden till its button opens it, with its arrow. The
|
|
547
|
+
* arrow is an element of its own, not the popover's pseudo-element: axe cannot tell the background
|
|
548
|
+
* of text under an ancestor's pseudo-element, and leaves its contrast undecided.
|
|
549
|
+
*/
|
|
550
|
+
function dialog() {
|
|
551
|
+
const pop = document.createElement("div");
|
|
552
|
+
pop.className = "pop";
|
|
553
|
+
pop.setAttribute("role", "dialog");
|
|
554
|
+
pop.hidden = true;
|
|
555
|
+
const arrow = document.createElement("span");
|
|
556
|
+
arrow.className = "arrow";
|
|
557
|
+
arrow.setAttribute("aria-hidden", "true");
|
|
558
|
+
pop.append(arrow);
|
|
559
|
+
return pop;
|
|
560
|
+
}
|
|
561
|
+
/** What a popover holds, which scrolls within it when the element is shorter: its arrow stays. */
|
|
562
|
+
function body(...children) {
|
|
563
|
+
const inside = document.createElement("div");
|
|
564
|
+
inside.className = "body";
|
|
565
|
+
inside.append(...children);
|
|
566
|
+
return inside;
|
|
567
|
+
}
|
|
568
|
+
function section(...children) {
|
|
569
|
+
const part = document.createElement("section");
|
|
570
|
+
part.append(...children);
|
|
571
|
+
return part;
|
|
572
|
+
}
|
|
573
|
+
/** A named group of choices. */
|
|
574
|
+
function group(className) {
|
|
575
|
+
const choices = document.createElement("div");
|
|
576
|
+
choices.className = className;
|
|
577
|
+
choices.setAttribute("role", "group");
|
|
578
|
+
return choices;
|
|
579
|
+
}
|
|
580
|
+
/** A heading of a section, which names its group. */
|
|
581
|
+
function heading(id) {
|
|
582
|
+
const text = document.createElement("div");
|
|
583
|
+
text.className = "heading";
|
|
584
|
+
text.id = id;
|
|
585
|
+
return text;
|
|
586
|
+
}
|
|
587
|
+
/** A toggle button, its picture hidden from a screen reader. */
|
|
588
|
+
function toggle(className, picture) {
|
|
589
|
+
const button = document.createElement("button");
|
|
590
|
+
button.type = "button";
|
|
591
|
+
if (className !== "")
|
|
592
|
+
button.className = className;
|
|
593
|
+
button.setAttribute("aria-pressed", "false");
|
|
594
|
+
if (picture !== undefined)
|
|
595
|
+
button.append(picture);
|
|
596
|
+
return button;
|
|
597
|
+
}
|
|
598
|
+
/** A colour's swatch, its colour its data. */
|
|
599
|
+
function swatch(colour) {
|
|
600
|
+
const button = toggle("swatch");
|
|
601
|
+
button.dataset.colour = colour;
|
|
602
|
+
button.style.setProperty("--c", colour);
|
|
603
|
+
return button;
|
|
604
|
+
}
|
|
605
|
+
/** An SVG drawing that says nothing, of these shapes. */
|
|
606
|
+
function drawing(viewBox, shapes) {
|
|
607
|
+
const svg = document.createElementNS(SVG, "svg");
|
|
608
|
+
svg.setAttribute("viewBox", viewBox);
|
|
609
|
+
svg.setAttribute("aria-hidden", "true");
|
|
610
|
+
for (const [tag, attributes] of shapes) {
|
|
611
|
+
const shape = document.createElementNS(SVG, tag);
|
|
612
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
613
|
+
shape.setAttribute(name, String(value));
|
|
614
|
+
}
|
|
615
|
+
svg.append(shape);
|
|
616
|
+
}
|
|
617
|
+
return svg;
|
|
618
|
+
}
|
|
619
|
+
/** The body of a pen, a liner or a marker, as the mockup draws it. */
|
|
620
|
+
const BODY = { fill: "#f2f2f4", stroke: "#d1d1d6" };
|
|
621
|
+
/** A kind's button: the mockup's picture of the pen, its tip in the kind's colour, and its name. */
|
|
622
|
+
function kindButton(kind) {
|
|
623
|
+
const tint = { class: "tint", fill: "#000" };
|
|
624
|
+
const shapes = kind === "marker"
|
|
625
|
+
? [
|
|
626
|
+
["rect", { x: 8, y: 22, width: 14, height: 40, rx: 3, ...BODY }],
|
|
627
|
+
["path", { d: "M10 22l2-12h6l2 12z", fill: "#e5e5ea", stroke: BODY.stroke }],
|
|
628
|
+
["path", { d: "M12 10l1-6h4l1 6z", ...tint }],
|
|
629
|
+
]
|
|
630
|
+
: kind === "liner"
|
|
631
|
+
? [
|
|
632
|
+
["rect", { x: 9, y: 22, width: 12, height: 40, rx: 5, ...BODY }],
|
|
633
|
+
["path", { d: "M9 26l6-22 6 22z", ...BODY }],
|
|
634
|
+
["rect", { x: 13.6, y: 4, width: 2.8, height: 7, rx: 1, ...tint }],
|
|
635
|
+
]
|
|
636
|
+
: [
|
|
637
|
+
["rect", { x: 9, y: 22, width: 12, height: 40, rx: 5, ...BODY }],
|
|
638
|
+
["path", { d: "M9 26l6-22 6 22z", ...BODY }],
|
|
639
|
+
["path", { d: "M12.6 12.8L15 4l2.4 8.8z", ...tint }],
|
|
640
|
+
["rect", { x: 9, y: 30, width: 12, height: 4, ...tint }],
|
|
641
|
+
];
|
|
642
|
+
const button = toggle("kind", drawing("0 0 30 64", shapes));
|
|
643
|
+
button.append(document.createElement("span"));
|
|
644
|
+
return button;
|
|
645
|
+
}
|
|
646
|
+
/** A width's sample stroke, which `show` gives its width and colour. */
|
|
647
|
+
function sampleOf() {
|
|
648
|
+
return drawing("0 0 30 26", [
|
|
649
|
+
[
|
|
650
|
+
"path",
|
|
651
|
+
{
|
|
652
|
+
d: SAMPLE,
|
|
653
|
+
fill: "none",
|
|
654
|
+
stroke: "currentColor",
|
|
655
|
+
"stroke-linecap": "round",
|
|
656
|
+
"stroke-linejoin": "round",
|
|
657
|
+
},
|
|
658
|
+
],
|
|
659
|
+
]);
|
|
660
|
+
}
|
|
661
|
+
/** An eraser's size, a circle as large as the mockup draws it. */
|
|
662
|
+
function circleOf(size) {
|
|
663
|
+
return drawing("0 0 30 26", [
|
|
664
|
+
[
|
|
665
|
+
"circle",
|
|
666
|
+
{
|
|
667
|
+
cx: 15,
|
|
668
|
+
cy: 13,
|
|
669
|
+
r: Math.max(2, size / 5.5),
|
|
670
|
+
fill: "none",
|
|
671
|
+
stroke: "currentColor",
|
|
672
|
+
"stroke-width": 1.6,
|
|
673
|
+
},
|
|
674
|
+
],
|
|
675
|
+
]);
|
|
676
|
+
}
|