@altnovatrail/canvas 0.3.12 → 0.3.13
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/README.md +1 -1
- package/lib/core/hands.d.ts +17 -4
- package/lib/core/hands.js +42 -12
- package/lib/core/screen.d.ts +10 -7
- package/lib/core/screen.js +10 -9
- package/lib/core/zoom.d.ts +23 -0
- package/lib/core/zoom.js +43 -0
- package/lib/element/icons.d.ts +36 -0
- package/lib/element/icons.js +44 -0
- package/lib/element/index.js +378 -123
- package/lib/element/labels.d.ts +15 -4
- package/lib/element/labels.js +17 -1
- package/lib/element/session.d.ts +3 -0
- package/lib/element/session.js +8 -4
- package/lib/element/tools.d.ts +16 -8
- package/lib/element/tools.js +58 -23
- package/package.json +1 -1
package/lib/element/index.js
CHANGED
|
@@ -31,7 +31,11 @@
|
|
|
31
31
|
// area as the part opens (T61; R42). A worked example on a workbook lays its lines out as a task's,
|
|
32
32
|
// and a part of its demo that fills a box writes the box's value in it (T85; R61). The types of
|
|
33
33
|
// its contract with app are spec's, of @altnovatrail/spec/canvas: the element implements its CanvasElement, and the package puts the
|
|
34
|
-
// element and its events in the DOM's maps (T69; R07).
|
|
34
|
+
// element and its events in the DOM's maps (T69; R07). The keyboard goes into the sheet and out:
|
|
35
|
+
// a container that scrolls is a region named by the canvas kind, with a stop of Tab of its own and
|
|
36
|
+
// a ring when the keyboard brings the focus there (T93; R33). The row's Zoom in and Zoom out step
|
|
37
|
+
// the page's zoom of the sheet from its fit to twice it, and a zoomed sheet scrolls across as well
|
|
38
|
+
// as down, with Left and Right in the row, two fingers and the keys (T94).
|
|
35
39
|
import { cellOf, cellsOf, filledOf, sheetFilledOf } from "../core/boxes.js";
|
|
36
40
|
import { Clock } from "../core/clock.js";
|
|
37
41
|
import { ERASER_BUTTON, Eraser } from "../core/eraser.js";
|
|
@@ -56,7 +60,7 @@ import { labelsFor, say } from "./labels.js";
|
|
|
56
60
|
import { Parts } from "./parts.js";
|
|
57
61
|
import { Pictures } from "./pictures.js";
|
|
58
62
|
import { attachProbe } from "./probe.js";
|
|
59
|
-
import { PEN_MODE } from "./session.js";
|
|
63
|
+
import { PAGE_ZOOM, PEN_MODE } from "./session.js";
|
|
60
64
|
import { Statement } from "./statement.js";
|
|
61
65
|
import { Tools } from "./tools.js";
|
|
62
66
|
/** The name the element is registered under. */
|
|
@@ -95,8 +99,13 @@ const UP_DOWN = 4 / 5;
|
|
|
95
99
|
// The statement and the sheet stand in a column of their own, as wide as the element up to
|
|
96
100
|
// 1000 CSS pixels and in its middle beyond, and scroll in a container of their own, by the
|
|
97
101
|
// browser's own scrolling: the wheel, the scroll bar and the keyboard work as anywhere on a page.
|
|
98
|
-
// The
|
|
99
|
-
//
|
|
102
|
+
// The page's zoom widens the column with the sheet, to twice its fit, and the container scrolls
|
|
103
|
+
// it across too, left to right whatever the page's direction, while the statement and a demo's
|
|
104
|
+
// parts stay as wide as the fit and stick to its left, in the element's own direction: they wrap
|
|
105
|
+
// as before, and scroll down alone (T94). Left and Right stand in that order in a row that runs
|
|
106
|
+
// right to left too: they point to the sides of the screen. The layers of the sheet stand in its visible part, the stage, which sticks to the
|
|
107
|
+
// container's top and left, and each shows what lies there, shifted in the frame the ink is drawn
|
|
108
|
+
// in (R18, R23; architecture 3.2 in docs/architecture.md).
|
|
100
109
|
// Above the column, as wide, stands the row of the element's own buttons, outside the container's
|
|
101
110
|
// scrolling: it sticks to the top of whatever scrolls the element, as far down as the consumer's
|
|
102
111
|
// --novatrail-canvas-top says, while the element is in sight, over the sheet as it scrolls by
|
|
@@ -139,9 +148,10 @@ const STYLE = `
|
|
|
139
148
|
background: var(--novatrail-canvas-paper, ${PAPER}); color: var(--novatrail-canvas-ink, ${INK});
|
|
140
149
|
}
|
|
141
150
|
.tools.loose { position: static; }
|
|
142
|
-
.tools .demo, .tools .choice, .tools .updown {
|
|
151
|
+
.tools .demo, .tools .choice, .tools .updown, .tools .zoom, .tools .across {
|
|
143
152
|
display: flex; gap: 8px; margin-inline-end: 16px;
|
|
144
153
|
}
|
|
154
|
+
.tools:dir(rtl) .across { flex-direction: row-reverse; }
|
|
145
155
|
.tools button {
|
|
146
156
|
box-sizing: border-box; min-width: 44px; min-height: 44px; padding: 0 8px; margin: 0;
|
|
147
157
|
font: inherit; color: inherit; background: transparent; cursor: pointer;
|
|
@@ -169,10 +179,26 @@ const STYLE = `
|
|
|
169
179
|
vertical-align: -0.25em; fill: currentColor;
|
|
170
180
|
}
|
|
171
181
|
.tools:dir(rtl) svg.mirrored { transform: scaleX(-1); }
|
|
182
|
+
.frame {
|
|
183
|
+
position: relative; flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column;
|
|
184
|
+
}
|
|
172
185
|
.viewport {
|
|
173
186
|
flex: 1 1 auto; min-height: 0; overflow-x: hidden; overflow-y: auto; overflow-anchor: none;
|
|
187
|
+
outline: none; -webkit-tap-highlight-color: transparent;
|
|
188
|
+
}
|
|
189
|
+
.viewport.zoomed { overflow-x: auto; direction: ltr; }
|
|
190
|
+
.ring { position: absolute; inset: 0; z-index: 0; pointer-events: none; display: none; }
|
|
191
|
+
.viewport:focus-visible + .ring {
|
|
192
|
+
display: block; outline: 2px solid var(--novatrail-canvas-ink, ${INK}); outline-offset: -2px;
|
|
193
|
+
}
|
|
194
|
+
.column {
|
|
195
|
+
position: relative; width: calc(min(100%, ${K_MAX * SHEET.width}px) * var(--zoom));
|
|
196
|
+
margin: 0 auto;
|
|
197
|
+
}
|
|
198
|
+
.zoomed .statement, .zoomed .parts {
|
|
199
|
+
position: sticky; left: 0; box-sizing: border-box; width: calc(100% / var(--zoom));
|
|
200
|
+
direction: var(--direction, ltr);
|
|
174
201
|
}
|
|
175
|
-
.column { position: relative; width: min(100%, ${K_MAX * SHEET.width}px); margin: 0 auto; }
|
|
176
202
|
.statement { padding: 0 0 12px; overflow-wrap: break-word; }
|
|
177
203
|
.statement:empty { display: none; }
|
|
178
204
|
.statement p { margin: 0 0 8px; }
|
|
@@ -196,7 +222,7 @@ const STYLE = `
|
|
|
196
222
|
background: var(--novatrail-canvas-paper, ${PAPER});
|
|
197
223
|
}
|
|
198
224
|
.stage {
|
|
199
|
-
position: sticky; top: 0; max-height: 100%; overflow: clip;
|
|
225
|
+
position: sticky; top: 0; left: 0; max-height: 100%; overflow: clip;
|
|
200
226
|
user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
|
|
201
227
|
}
|
|
202
228
|
.spot { position: absolute; visibility: hidden; }
|
|
@@ -242,7 +268,7 @@ canvas {
|
|
|
242
268
|
background: Highlight; color: HighlightText; border-color: Highlight;
|
|
243
269
|
}
|
|
244
270
|
.tools [aria-disabled="true"] { color: GrayText; border-color: GrayText; }
|
|
245
|
-
.tools button:focus-visible { outline-color: Highlight; }
|
|
271
|
+
.tools button:focus-visible, .viewport:focus-visible + .ring { outline-color: Highlight; }
|
|
246
272
|
}
|
|
247
273
|
`;
|
|
248
274
|
/** The sheet of STYLE that the elements of a document adopt, made by the first of them there. */
|
|
@@ -299,7 +325,14 @@ function htmlLayer(name) {
|
|
|
299
325
|
* commands undo and clear, which the contract does not name.
|
|
300
326
|
*/
|
|
301
327
|
export class NovatrailCanvas extends HTMLElement {
|
|
328
|
+
/**
|
|
329
|
+
* The wrapper, which does not scroll, of the container that scrolls and of the ring of its focus
|
|
330
|
+
* over it: an outline of the container itself would stand under the sheet's paper and stage,
|
|
331
|
+
* which are positioned in it (T93).
|
|
332
|
+
*/
|
|
333
|
+
#wrapper = document.createElement("div");
|
|
302
334
|
#viewport = document.createElement("div");
|
|
335
|
+
#ring = document.createElement("div");
|
|
303
336
|
#column = document.createElement("div");
|
|
304
337
|
#sheet = document.createElement("div");
|
|
305
338
|
/** The sheet's visible part, where its layers stand, bottom to top (concepts 8 spec; R23). */
|
|
@@ -332,7 +365,9 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
332
365
|
choose: (tool) => this.#choose(tool),
|
|
333
366
|
undo: () => this.undo(),
|
|
334
367
|
clear: () => this.clear(),
|
|
335
|
-
upOrDown: (onward) => this.#
|
|
368
|
+
upOrDown: (onward) => this.#step(onward, "y"),
|
|
369
|
+
zoom: (larger) => this.#zoomBy(larger),
|
|
370
|
+
leftOrRight: (onward) => this.#step(onward, "x"),
|
|
336
371
|
// The finger writes again on every sheet of the page, with no event: the next pen's touch
|
|
337
372
|
// puts the page back in pen mode (R29).
|
|
338
373
|
fingers: () => {
|
|
@@ -368,12 +403,17 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
368
403
|
#hands = new Hands({
|
|
369
404
|
writer: () => this.#writer(),
|
|
370
405
|
drop: (pointerId) => this.#drop(pointerId),
|
|
371
|
-
scroll: (dy, fresh) => this.#scroll(dy, fresh),
|
|
406
|
+
scroll: (dx, dy, fresh) => this.#scroll(dx, dy, fresh),
|
|
407
|
+
across: () => this.#zoom > 1,
|
|
372
408
|
}, PEN_MODE);
|
|
373
409
|
/** Whether the row shows the finger's button: it comes and goes between strokes alone. */
|
|
374
410
|
#finger = false;
|
|
375
|
-
/**
|
|
411
|
+
/**
|
|
412
|
+
* What two fingers scroll in the gesture under way, down and across, each chosen at the first
|
|
413
|
+
* step that goes along its axis (T94): undefined till then, and null when nothing scrolls so.
|
|
414
|
+
*/
|
|
376
415
|
#latch;
|
|
416
|
+
#latchAcross;
|
|
377
417
|
/**
|
|
378
418
|
* A scroll of the page, or of an ancestor of the element in the page's own tree, heard on its way
|
|
379
419
|
* down: Up or Down may have come to an end, or left one (R33). Other scrollers are none of theirs.
|
|
@@ -394,6 +434,7 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
394
434
|
*/
|
|
395
435
|
#resized = () => {
|
|
396
436
|
this.#stick();
|
|
437
|
+
this.#region();
|
|
397
438
|
this.#edges();
|
|
398
439
|
};
|
|
399
440
|
#page = new ResizeObserver(this.#resized);
|
|
@@ -402,6 +443,9 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
402
443
|
/** The row's `top` as the last check of whether it sticks read it: the consumer's offset. */
|
|
403
444
|
#topSeen = "";
|
|
404
445
|
#unwatchPen;
|
|
446
|
+
#unwatchZoom;
|
|
447
|
+
/** The page's zoom as the sheet is laid out by it (T94). */
|
|
448
|
+
#zoom = 0;
|
|
405
449
|
#tool = "pen";
|
|
406
450
|
#task;
|
|
407
451
|
/** A lesson's worked example as the consumer gave it, and as the element checked it (R42). */
|
|
@@ -472,8 +516,11 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
472
516
|
*/
|
|
473
517
|
#k = 0;
|
|
474
518
|
#sheetAt = 0;
|
|
475
|
-
/** The
|
|
519
|
+
/** The sheet's left in the container's content, as laid out then, in CSS pixels (T94). */
|
|
520
|
+
#sheetAtX = 0;
|
|
521
|
+
/** The top and the left of the visible part, in sheet units, kept from the last scroll. */
|
|
476
522
|
#vy = 0;
|
|
523
|
+
#vx = 0;
|
|
477
524
|
/** The view box the layers were last given, so that a frame without a scroll writes none. */
|
|
478
525
|
#viewBox = "";
|
|
479
526
|
#frame = 0;
|
|
@@ -484,7 +531,9 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
484
531
|
super();
|
|
485
532
|
const root = this.attachShadow({ mode: "open" });
|
|
486
533
|
adoptStyle(root);
|
|
534
|
+
this.#wrapper.className = "frame";
|
|
487
535
|
this.#viewport.className = "viewport";
|
|
536
|
+
this.#ring.className = "ring";
|
|
488
537
|
this.#column.className = "column";
|
|
489
538
|
this.#sheet.className = "sheet";
|
|
490
539
|
this.#stage.className = "stage";
|
|
@@ -500,7 +549,9 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
500
549
|
this.#sheet.append(this.#stage, this.#parts.spot);
|
|
501
550
|
this.#column.append(this.#statement.element, this.#parts.list, this.#parts.status, this.#sheet);
|
|
502
551
|
this.#viewport.append(this.#column);
|
|
503
|
-
|
|
552
|
+
this.#wrapper.append(this.#viewport, this.#ring);
|
|
553
|
+
root.append(this.#tools.element, this.#wrapper);
|
|
554
|
+
this.#zoomTo(PAGE_ZOOM.value);
|
|
504
555
|
this.#lay(NO_TASK);
|
|
505
556
|
this.#refresh();
|
|
506
557
|
this.#canvas.addEventListener("pointerdown", (event) => this.#down(event));
|
|
@@ -527,6 +578,8 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
527
578
|
this.#canvas.addEventListener("transitionend", () => this.#request());
|
|
528
579
|
this.#canvas.addEventListener("transitioncancel", () => this.#request());
|
|
529
580
|
this.#viewport.addEventListener("scroll", () => this.#scrolled(), { passive: true });
|
|
581
|
+
// A container that has nothing to scroll any more keeps its stop of Tab while it has the focus.
|
|
582
|
+
this.#viewport.addEventListener("blur", () => this.#region());
|
|
530
583
|
attachProbe(this, () => ({
|
|
531
584
|
strokes: this.#work.strokes,
|
|
532
585
|
live: this.#recorder.live,
|
|
@@ -858,6 +911,9 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
858
911
|
unwatch();
|
|
859
912
|
unenlist();
|
|
860
913
|
};
|
|
914
|
+
// The page's zoom, on every sheet of it (T94): it may have changed while the element was away.
|
|
915
|
+
this.#zoomTo(PAGE_ZOOM.value);
|
|
916
|
+
this.#unwatchZoom = PAGE_ZOOM.watch(() => this.#zoomed());
|
|
861
917
|
// A scroll does not bubble, but the document hears every one of its tree on the way down. A
|
|
862
918
|
// page that grows or shrinks scrolls nothing, and neither does a window of another size.
|
|
863
919
|
document.addEventListener("scroll", this.#heard, { capture: true, passive: true });
|
|
@@ -876,6 +932,7 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
876
932
|
*/
|
|
877
933
|
attributeChangedCallback() {
|
|
878
934
|
this.#tools.label(this.getAttribute("lang"));
|
|
935
|
+
this.#nameRegion();
|
|
879
936
|
this.#nameBoxes();
|
|
880
937
|
this.#markBoxes();
|
|
881
938
|
}
|
|
@@ -952,8 +1009,11 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
952
1009
|
this.#eraser.end();
|
|
953
1010
|
this.#hands.reset();
|
|
954
1011
|
this.#latch = undefined;
|
|
1012
|
+
this.#latchAcross = undefined;
|
|
955
1013
|
this.#unwatchPen?.();
|
|
956
1014
|
this.#unwatchPen = undefined;
|
|
1015
|
+
this.#unwatchZoom?.();
|
|
1016
|
+
this.#unwatchZoom = undefined;
|
|
957
1017
|
document.removeEventListener("scroll", this.#heard, { capture: true });
|
|
958
1018
|
this.#page.disconnect();
|
|
959
1019
|
this.#port = null;
|
|
@@ -1271,8 +1331,10 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1271
1331
|
*/
|
|
1272
1332
|
#lifted(pointerId) {
|
|
1273
1333
|
this.#hands.lift(pointerId);
|
|
1274
|
-
if (!this.#hands.scrolling)
|
|
1334
|
+
if (!this.#hands.scrolling) {
|
|
1275
1335
|
this.#latch = undefined;
|
|
1336
|
+
this.#latchAcross = undefined;
|
|
1337
|
+
}
|
|
1276
1338
|
this.#refresh();
|
|
1277
1339
|
PEN_MODE.settle();
|
|
1278
1340
|
}
|
|
@@ -1308,8 +1370,8 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1308
1370
|
}
|
|
1309
1371
|
/**
|
|
1310
1372
|
* Where the sheet is on the screen now. The rectangles of the canvas and of the sheet give it:
|
|
1311
|
-
* the canvas is
|
|
1312
|
-
* on the screen, after whatever transform the page puts on the element. The container's
|
|
1373
|
+
* the canvas's top left corner is that of the visible part, and the sheet's width gives `k`, the
|
|
1374
|
+
* one on the screen, after whatever transform the page puts on the element. The container's
|
|
1313
1375
|
* rectangle gives the part of the canvas it shows: with the statement in sight, the canvas
|
|
1314
1376
|
* goes on below the container's bottom. The row of buttons above the container hides what it
|
|
1315
1377
|
* covers, once it sticks over the sheet as the page scrolls the element by (R28); in its place,
|
|
@@ -1317,10 +1379,11 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1317
1379
|
*/
|
|
1318
1380
|
#view() {
|
|
1319
1381
|
const canvas = this.#canvas.getBoundingClientRect();
|
|
1320
|
-
if (canvas.width <= 0)
|
|
1321
|
-
return undefined;
|
|
1322
|
-
const k = scaleFor(canvas.width);
|
|
1323
1382
|
const sheet = this.#sheet.getBoundingClientRect();
|
|
1383
|
+
if (canvas.width <= 0 || sheet.width <= 0)
|
|
1384
|
+
return undefined;
|
|
1385
|
+
const k = scaleFor(sheet.width);
|
|
1386
|
+
const vx = (canvas.left - sheet.left) / k;
|
|
1324
1387
|
const vy = (canvas.top - sheet.top) / k;
|
|
1325
1388
|
const port = clientBoxOf(this.#viewport);
|
|
1326
1389
|
// The row ends where the container begins, unless it sticks over it.
|
|
@@ -1331,64 +1394,109 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1331
1394
|
right: Math.min(canvas.right, port.right),
|
|
1332
1395
|
bottom: Math.min(canvas.bottom, port.bottom),
|
|
1333
1396
|
};
|
|
1334
|
-
|
|
1397
|
+
const { left, top, width, height } = canvas;
|
|
1398
|
+
return { k, vx, vy, left, top, width, height, shown };
|
|
1335
1399
|
}
|
|
1336
|
-
/**
|
|
1337
|
-
|
|
1400
|
+
/**
|
|
1401
|
+
* The sheet's top in the container's content, in CSS pixels as laid out: under the statement —
|
|
1402
|
+
* or, along `x`, its left: its margin in the middle of a wide container, and none once it is
|
|
1403
|
+
* wider (T94).
|
|
1404
|
+
*/
|
|
1405
|
+
#sheetEdge(axis) {
|
|
1338
1406
|
const port = clientBoxOf(this.#viewport);
|
|
1339
1407
|
const sheet = this.#sheet.getBoundingClientRect();
|
|
1340
|
-
return
|
|
1408
|
+
return axis === "y"
|
|
1409
|
+
? (sheet.top - port.top) / port.down + this.#viewport.scrollTop
|
|
1410
|
+
: (sheet.left - port.left) / port.across + this.#viewport.scrollLeft;
|
|
1341
1411
|
}
|
|
1342
1412
|
/**
|
|
1343
|
-
* The top of the visible part from the scroll and the last layout, without reading
|
|
1344
|
-
* again: the stage sticks to the container's top once the sheet's top has gone past
|
|
1345
|
-
* Down may have come to an end of the container, or
|
|
1413
|
+
* The top and the left of the visible part from the scroll and the last layout, without reading
|
|
1414
|
+
* the layout again: the stage sticks to the container's top once the sheet's top has gone past
|
|
1415
|
+
* it, and to its left so. Up or Down, Left or Right may have come to an end of the container, or
|
|
1416
|
+
* left one.
|
|
1346
1417
|
*/
|
|
1347
1418
|
#scrolled() {
|
|
1348
|
-
if (this.#k > 0)
|
|
1419
|
+
if (this.#k > 0) {
|
|
1349
1420
|
this.#vy = Math.max(0, this.#viewport.scrollTop - this.#sheetAt) / this.#k;
|
|
1421
|
+
this.#vx = Math.max(0, this.#viewport.scrollLeft - this.#sheetAtX) / this.#k;
|
|
1422
|
+
}
|
|
1350
1423
|
this.#request();
|
|
1351
1424
|
this.#edges();
|
|
1352
1425
|
}
|
|
1353
1426
|
/**
|
|
1354
|
-
* Up or Down was pressed (R33)
|
|
1355
|
-
* it shows in sight. It is chosen anew at every press, so
|
|
1356
|
-
* scroll on at the next. While a pointer writes or erases
|
|
1357
|
-
* under it.
|
|
1427
|
+
* Up or Down was pressed (R33), or along `x` Left or Right (T94): what two fingers would scroll
|
|
1428
|
+
* that way goes by four fifths of what it shows in sight. It is chosen anew at every press, so
|
|
1429
|
+
* that a container at its end hands the scroll on at the next. While a pointer writes or erases
|
|
1430
|
+
* on a sheet of the page, nothing moves under it.
|
|
1358
1431
|
*/
|
|
1359
|
-
#
|
|
1360
|
-
const latch = PEN_MODE.writing ? undefined : this.#scrollerFor(onward);
|
|
1432
|
+
#step(onward, axis) {
|
|
1433
|
+
const latch = PEN_MODE.writing ? undefined : this.#scrollerFor(onward, axis);
|
|
1361
1434
|
if (latch !== undefined) {
|
|
1362
|
-
const
|
|
1363
|
-
|
|
1435
|
+
const seen = this.#seen(latch.target, axis);
|
|
1436
|
+
const step = Math.max(1, Math.round((UP_DOWN * seen) / latch.scale));
|
|
1437
|
+
const by = onward ? step : -step;
|
|
1438
|
+
latch.target.scrollBy({ [axis === "y" ? "top" : "left"]: by, behavior: "instant" });
|
|
1364
1439
|
}
|
|
1365
1440
|
this.#edges();
|
|
1366
1441
|
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Zoom in or Zoom out was pressed (T94): the page's zoom goes a step larger or smaller, and every
|
|
1444
|
+
* sheet of the page is laid out by it — but not while a pointer writes or erases on one of them,
|
|
1445
|
+
* for nothing moves under it.
|
|
1446
|
+
*/
|
|
1447
|
+
#zoomBy(larger) {
|
|
1448
|
+
if (!PEN_MODE.writing)
|
|
1449
|
+
PAGE_ZOOM.step(larger);
|
|
1450
|
+
}
|
|
1451
|
+
/**
|
|
1452
|
+
* The page's zoom has changed (T94): the sheet is laid out by it at once, its top left corner in
|
|
1453
|
+
* sight staying where it was on the sheet, and the row shows what the zoom can do now.
|
|
1454
|
+
*/
|
|
1455
|
+
#zoomed() {
|
|
1456
|
+
if (!this.#zoomTo(PAGE_ZOOM.value))
|
|
1457
|
+
return;
|
|
1458
|
+
this.#layout();
|
|
1459
|
+
this.#refresh();
|
|
1460
|
+
}
|
|
1461
|
+
/**
|
|
1462
|
+
* The column and the container as wide as a zoom asks, and whether that is another than the
|
|
1463
|
+
* last: the column widens with the sheet, and the container scrolls it across while the zoom is
|
|
1464
|
+
* beyond the fit (T94).
|
|
1465
|
+
*/
|
|
1466
|
+
#zoomTo(zoom) {
|
|
1467
|
+
if (zoom === this.#zoom)
|
|
1468
|
+
return false;
|
|
1469
|
+
this.#zoom = zoom;
|
|
1470
|
+
this.#column.style.setProperty("--zoom", String(zoom));
|
|
1471
|
+
this.#viewport.classList.toggle("zoomed", zoom > 1);
|
|
1472
|
+
this.#keepDirection();
|
|
1473
|
+
return true;
|
|
1474
|
+
}
|
|
1475
|
+
/**
|
|
1476
|
+
* The statement and a demo's parts keep the element's own direction while the zoomed container
|
|
1477
|
+
* runs left to right: the direction the element computes, by an attribute or by a style, as the
|
|
1478
|
+
* last layout read it (T94).
|
|
1479
|
+
*/
|
|
1480
|
+
#keepDirection() {
|
|
1481
|
+
if (this.#zoom <= 1 || !this.isConnected)
|
|
1482
|
+
return;
|
|
1483
|
+
const direction = getComputedStyle(this).direction;
|
|
1484
|
+
if (this.#column.style.getPropertyValue("--direction") !== direction) {
|
|
1485
|
+
this.#column.style.setProperty("--direction", direction);
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1367
1488
|
/**
|
|
1368
1489
|
* How much of what a scroller shows is in sight, down the screen: within the ancestors that clip
|
|
1369
1490
|
* it and the visual viewport, and below the row of buttons while the row sticks over its top, or
|
|
1370
1491
|
* below the consumer's band, --novatrail-canvas-top, while the row stands in its place in a low
|
|
1371
|
-
* window. A scroller out of sight counts whole.
|
|
1372
|
-
*/
|
|
1373
|
-
#seen(target) {
|
|
1374
|
-
const
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
const own = clientBoxOf(target);
|
|
1380
|
-
whole = own.bottom - own.top;
|
|
1381
|
-
// The scroller, and every ancestor that clips it, up to the page, whose part is the sight.
|
|
1382
|
-
for (let node = target; node !== null; node = parentOf(node)) {
|
|
1383
|
-
if (node === document.documentElement)
|
|
1384
|
-
break;
|
|
1385
|
-
if (node !== target && getComputedStyle(node).overflowY === "visible")
|
|
1386
|
-
continue;
|
|
1387
|
-
const clip = node === target ? own : clientBoxOf(node);
|
|
1388
|
-
top = Math.max(top, clip.top);
|
|
1389
|
-
bottom = Math.min(bottom, clip.bottom);
|
|
1390
|
-
}
|
|
1391
|
-
}
|
|
1492
|
+
* window. A scroller out of sight counts whole. Across the screen, the row covers nothing (T94).
|
|
1493
|
+
*/
|
|
1494
|
+
#seen(target, axis = "y") {
|
|
1495
|
+
const { start, end, whole } = sightOf(target, axis);
|
|
1496
|
+
if (axis === "x")
|
|
1497
|
+
return end > start ? end - start : whole;
|
|
1498
|
+
const bottom = end;
|
|
1499
|
+
let top = start;
|
|
1392
1500
|
// The row hides the top while it sticks there, as far down as the consumer's offset puts it. A
|
|
1393
1501
|
// row that stands in its place, in a low window, leaves the consumer's band over it alone.
|
|
1394
1502
|
const row = this.#tools.element;
|
|
@@ -1441,90 +1549,124 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1441
1549
|
return within ? room.top + room.offset : Number.NEGATIVE_INFINITY;
|
|
1442
1550
|
}
|
|
1443
1551
|
/**
|
|
1444
|
-
* Up and Down as the scrolling now is (R33)
|
|
1445
|
-
* scroll its way can go no further. The
|
|
1446
|
-
*
|
|
1447
|
-
* in another shadow tree: a press asks
|
|
1552
|
+
* Up and Down as the scrolling now is (R33), and Left and Right while the sheet is zoomed (T94):
|
|
1553
|
+
* each stands unavailable while what two fingers would scroll its way can go no further. The
|
|
1554
|
+
* element hears its container, the page and the ancestors in the page's own tree scroll, and the
|
|
1555
|
+
* page and the window change size, but not the scrolling in another shadow tree: a press asks
|
|
1556
|
+
* anew.
|
|
1448
1557
|
*/
|
|
1449
1558
|
#edges() {
|
|
1450
1559
|
if (!this.isConnected)
|
|
1451
1560
|
return;
|
|
1452
1561
|
const above = this.#scrollerFor(false) !== undefined;
|
|
1453
|
-
|
|
1562
|
+
const below = this.#scrollerFor(true) !== undefined;
|
|
1563
|
+
const zoomed = this.#zoom > 1;
|
|
1564
|
+
const before = zoomed && this.#scrollerFor(false, "x") !== undefined;
|
|
1565
|
+
const after = zoomed && this.#scrollerFor(true, "x") !== undefined;
|
|
1566
|
+
this.#tools.edges(above, below, before, after);
|
|
1454
1567
|
}
|
|
1455
1568
|
/**
|
|
1456
|
-
* Two fingers moved
|
|
1457
|
-
* them.
|
|
1458
|
-
*
|
|
1459
|
-
*
|
|
1460
|
-
*
|
|
1569
|
+
* Two fingers moved right on the screen by `dx` CSS pixels and down it by `dy` (R29), and what
|
|
1570
|
+
* they scroll goes with them. The gesture's first step down chooses what it scrolls down, for the
|
|
1571
|
+
* whole gesture, and its first step across, while the sheet is zoomed, what it scrolls across
|
|
1572
|
+
* (T94): the sheet's own container if it can go that way, or else the nearest ancestor of the
|
|
1573
|
+
* element that scrolls and can, or else the page. A container at its end hands the scroll on at
|
|
1574
|
+
* the next gesture, not within this one. The steps of an event add up, and its end scrolls them
|
|
1575
|
+
* at once.
|
|
1461
1576
|
*/
|
|
1462
|
-
#scroll(dy, fresh) {
|
|
1463
|
-
if (fresh)
|
|
1464
|
-
this.#latch =
|
|
1577
|
+
#scroll(dx, dy, fresh) {
|
|
1578
|
+
if (fresh) {
|
|
1579
|
+
this.#latch = undefined;
|
|
1580
|
+
this.#latchAcross = undefined;
|
|
1581
|
+
}
|
|
1465
1582
|
// The content goes with the fingers: down the screen, the scroll goes back.
|
|
1466
|
-
if (this.#latch
|
|
1583
|
+
if (dy !== 0 && this.#latch === undefined)
|
|
1584
|
+
this.#latch = this.#scrollerFor(dy < 0) ?? null;
|
|
1585
|
+
if (dx !== 0 && this.#latchAcross === undefined) {
|
|
1586
|
+
this.#latchAcross = this.#zoom > 1 ? (this.#scrollerFor(dx < 0, "x") ?? null) : null;
|
|
1587
|
+
}
|
|
1588
|
+
if (this.#latch)
|
|
1467
1589
|
this.#latch.rest -= dy / this.#latch.scale;
|
|
1590
|
+
if (this.#latchAcross)
|
|
1591
|
+
this.#latchAcross.rest -= dx / this.#latchAcross.scale;
|
|
1468
1592
|
}
|
|
1469
|
-
/**
|
|
1593
|
+
/**
|
|
1594
|
+
* The scroll the fingers' moves of an event owe, in whole pixels: the rest waits for the next.
|
|
1595
|
+
* One scroller that goes both ways goes both at once.
|
|
1596
|
+
*/
|
|
1470
1597
|
#flush() {
|
|
1471
|
-
const
|
|
1472
|
-
|
|
1598
|
+
const down = owed(this.#latch);
|
|
1599
|
+
const across = owed(this.#latchAcross);
|
|
1600
|
+
if (down !== undefined && across !== undefined && down.target === across.target) {
|
|
1601
|
+
down.target.scrollBy({ left: across.step, top: down.step, behavior: "instant" });
|
|
1473
1602
|
return;
|
|
1474
|
-
|
|
1475
|
-
if (
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1603
|
+
}
|
|
1604
|
+
if (down !== undefined)
|
|
1605
|
+
down.target.scrollBy({ top: down.step, behavior: "instant" });
|
|
1606
|
+
if (across !== undefined)
|
|
1607
|
+
across.target.scrollBy({ left: across.step, behavior: "instant" });
|
|
1479
1608
|
}
|
|
1480
|
-
/**
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1609
|
+
/**
|
|
1610
|
+
* What scrolls on down the content, for `onward`, or back up it — or, along `x`, on right across
|
|
1611
|
+
* it or back left (T94).
|
|
1612
|
+
*/
|
|
1613
|
+
#scrollerFor(onward, axis = "y") {
|
|
1614
|
+
if (scrollsOn(this.#viewport, onward, axis)) {
|
|
1615
|
+
return { target: this.#viewport, scale: scaleOf(this.#viewport, undefined, axis), rest: 0 };
|
|
1484
1616
|
}
|
|
1485
|
-
const { overflow, fromBody } = pageOverflow();
|
|
1617
|
+
const { overflow, fromBody } = pageOverflow(axis);
|
|
1486
1618
|
for (let node = parentOf(this); node !== null; node = parentOf(node)) {
|
|
1487
1619
|
if (isPage(node, fromBody))
|
|
1488
1620
|
break;
|
|
1489
1621
|
const style = getComputedStyle(node);
|
|
1490
|
-
if (!SCROLLING.test(style.overflowY))
|
|
1622
|
+
if (!SCROLLING.test(axis === "y" ? style.overflowY : style.overflowX))
|
|
1491
1623
|
continue;
|
|
1492
|
-
if (scrollsOn(node, onward))
|
|
1493
|
-
return { target: node, scale: scaleOf(node), rest: 0 };
|
|
1624
|
+
if (scrollsOn(node, onward, axis)) {
|
|
1625
|
+
return { target: node, scale: scaleOf(node, undefined, axis), rest: 0 };
|
|
1626
|
+
}
|
|
1494
1627
|
// A container that keeps its scrolling to itself hands none on.
|
|
1495
|
-
|
|
1628
|
+
const keeps = axis === "y" ? style.overscrollBehaviorY : style.overscrollBehaviorX;
|
|
1629
|
+
if (keeps === "contain" || keeps === "none")
|
|
1496
1630
|
return undefined;
|
|
1497
|
-
}
|
|
1498
1631
|
}
|
|
1499
1632
|
// A page that hides its overflow, as one under a modal does, a finger would not scroll: nor do two.
|
|
1500
1633
|
if (overflow === "hidden" || overflow === "clip")
|
|
1501
1634
|
return undefined;
|
|
1502
1635
|
const page = document.scrollingElement;
|
|
1503
|
-
if (page === null || !scrollsOn(page, onward))
|
|
1636
|
+
if (page === null || !scrollsOn(page, onward, axis))
|
|
1504
1637
|
return undefined;
|
|
1505
1638
|
return { target: window, scale: 1, rest: 0 };
|
|
1506
1639
|
}
|
|
1507
1640
|
/**
|
|
1508
1641
|
* The stage takes the visible part of the sheet, and the canvas a pixel of its buffer for each
|
|
1509
1642
|
* device pixel: its size on the screen times the density (R18). The layers follow in the same
|
|
1510
|
-
* frame.
|
|
1643
|
+
* frame. However large the zoom, the canvas is no larger than the container (T94).
|
|
1511
1644
|
*/
|
|
1512
1645
|
#layout() {
|
|
1646
|
+
// The content of a new size, or the container, may overflow it now, or no more.
|
|
1647
|
+
this.#region();
|
|
1513
1648
|
const area = this.#area;
|
|
1514
1649
|
if (area === undefined || area.width === 0)
|
|
1515
1650
|
return;
|
|
1516
|
-
// The column is as wide as the container up to the cap,
|
|
1517
|
-
// part of the sheet.
|
|
1518
|
-
const k = fitScale(area.width);
|
|
1519
|
-
const sheetAt = this.#
|
|
1520
|
-
|
|
1521
|
-
//
|
|
1522
|
-
//
|
|
1651
|
+
// The column is as wide as the container up to the cap, times the page's zoom, and the stage
|
|
1652
|
+
// as tall and as wide as the visible part of the sheet.
|
|
1653
|
+
const k = fitScale(area.width, this.#zoom);
|
|
1654
|
+
const sheetAt = this.#sheetEdge("y");
|
|
1655
|
+
const sheetAtX = this.#sheetEdge("x");
|
|
1656
|
+
// A new width or zoom scales the sheet, and a statement of a new height moves it: the top left
|
|
1657
|
+
// corner of the visible part stays where it was on the sheet, unless the sheet's top, or its
|
|
1658
|
+
// left, is in sight. A new height of the container alone moves neither, and a scroll under way
|
|
1659
|
+
// goes on.
|
|
1523
1660
|
if (this.#vy > 0 && (k !== this.#k || Math.abs(sheetAt - this.#sheetAt) > 0.5)) {
|
|
1524
1661
|
this.#viewport.scrollTop = sheetAt + this.#vy * k;
|
|
1525
1662
|
}
|
|
1663
|
+
if (this.#vx > 0 && k !== this.#k)
|
|
1664
|
+
this.#viewport.scrollLeft = sheetAtX + this.#vx * k;
|
|
1526
1665
|
this.#k = k;
|
|
1527
1666
|
this.#sheetAt = sheetAt;
|
|
1667
|
+
this.#sheetAtX = sheetAtX;
|
|
1668
|
+
this.#keepDirection();
|
|
1669
|
+
this.#stage.style.width = `${Math.min(area.width, SHEET.width * k)}px`;
|
|
1528
1670
|
this.#stage.style.height = `${Math.min(area.height, SHEET.height * k)}px`;
|
|
1529
1671
|
const onScreen = this.#canvas.getBoundingClientRect();
|
|
1530
1672
|
const width = Math.round(onScreen.width * devicePixelRatio);
|
|
@@ -1718,9 +1860,11 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1718
1860
|
*/
|
|
1719
1861
|
#bringIntoSight(item) {
|
|
1720
1862
|
item.scrollIntoView({ block: "nearest", behavior: "instant" });
|
|
1721
|
-
// A scroller that comes to its top hands the rest on to the next, as Up does (R33)
|
|
1722
|
-
// the
|
|
1723
|
-
|
|
1863
|
+
// A scroller that comes to its top hands the rest on to the next, as Up does (R33). A scroller
|
|
1864
|
+
// goes again only while each of its scrolls leaves less covered than the last: a row that the
|
|
1865
|
+
// element's bottom pushed up from where it sticks comes back down as the page scrolls, over
|
|
1866
|
+
// what the scroll brought out, and the next brings it out again (T94).
|
|
1867
|
+
const covered = new Map();
|
|
1724
1868
|
for (;;) {
|
|
1725
1869
|
const latch = this.#scrollerFor(false);
|
|
1726
1870
|
// A row in its place, in a low window, covers nothing of it: the consumer's band may, over
|
|
@@ -1729,9 +1873,12 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1729
1873
|
? this.#tools.element.getBoundingClientRect().bottom
|
|
1730
1874
|
: this.#band(latch?.target);
|
|
1731
1875
|
const cover = over - item.getBoundingClientRect().top;
|
|
1732
|
-
if (cover <=
|
|
1876
|
+
if (cover <= EDGE || latch === undefined)
|
|
1877
|
+
return;
|
|
1878
|
+
const last = covered.get(latch.target);
|
|
1879
|
+
if (last !== undefined && cover >= last - EDGE)
|
|
1733
1880
|
return;
|
|
1734
|
-
|
|
1881
|
+
covered.set(latch.target, cover);
|
|
1735
1882
|
latch.target.scrollBy({ top: -cover / latch.scale, behavior: "instant" });
|
|
1736
1883
|
}
|
|
1737
1884
|
}
|
|
@@ -1753,10 +1900,43 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1753
1900
|
tool: this.#tool,
|
|
1754
1901
|
undoable: this.#work.undoable,
|
|
1755
1902
|
clearable: this.#work.strokes.length > 0,
|
|
1903
|
+
larger: PAGE_ZOOM.larger,
|
|
1904
|
+
smaller: PAGE_ZOOM.smaller,
|
|
1756
1905
|
finger: this.#finger,
|
|
1757
1906
|
opens: this.#nextPart() !== undefined,
|
|
1758
1907
|
expanded: demo?.hidden !== true,
|
|
1759
1908
|
});
|
|
1909
|
+
this.#nameRegion();
|
|
1910
|
+
}
|
|
1911
|
+
/**
|
|
1912
|
+
* The container as a region of its own, a stop of Tab and a name, while its content overflows
|
|
1913
|
+
* it, and the keyboard scrolls it: in Safari a container that nothing in it can take the focus in
|
|
1914
|
+
* scrolls by no key without (R90 spec; WebKit's bug 277290). Without a height of its own the
|
|
1915
|
+
* element scrolls with the page and has no stop of Tab, and neither has one whose task fits. A
|
|
1916
|
+
* container that holds the focus keeps all three till the focus leaves it, not the window (T93).
|
|
1917
|
+
* Across, it scrolls while the sheet is zoomed, and an element without a height has a region
|
|
1918
|
+
* then too, if its sheet is wider than it (T94); unzoomed, it hides what overflows across.
|
|
1919
|
+
*/
|
|
1920
|
+
#region() {
|
|
1921
|
+
if (!this.isConnected)
|
|
1922
|
+
return;
|
|
1923
|
+
const viewport = this.#viewport;
|
|
1924
|
+
const across = this.#zoom > 1 && overflowOf(viewport, "x") > EDGE;
|
|
1925
|
+
const focused = this.shadowRoot?.activeElement === viewport;
|
|
1926
|
+
const scrolls = overflowOf(viewport) > EDGE || across || focused;
|
|
1927
|
+
write(viewport, "tabindex", scrolls ? "0" : null);
|
|
1928
|
+
write(viewport, "role", scrolls ? "region" : null);
|
|
1929
|
+
this.#nameRegion();
|
|
1930
|
+
}
|
|
1931
|
+
/**
|
|
1932
|
+
* The region's name: the canvas kind's, or a worked example's, in the labels' language, which
|
|
1933
|
+
* the row's buttons are named in. The container takes no `lang` of its own: the statement in it
|
|
1934
|
+
* has the task's (T93).
|
|
1935
|
+
*/
|
|
1936
|
+
#nameRegion() {
|
|
1937
|
+
const { labels } = labelsFor(this.getAttribute("lang"));
|
|
1938
|
+
const named = this.#viewport.hasAttribute("role");
|
|
1939
|
+
write(this.#viewport, "aria-label", named ? labels[this.#rowKind()] : null);
|
|
1760
1940
|
}
|
|
1761
1941
|
/** What the row of buttons is of, and is named by: a worked example, or the canvas kind (R42). */
|
|
1762
1942
|
#rowKind() {
|
|
@@ -1778,10 +1958,11 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1778
1958
|
* showing a part of the sheet the others have left.
|
|
1779
1959
|
*/
|
|
1780
1960
|
#place(screen) {
|
|
1961
|
+
const width = screen.width / screen.k;
|
|
1781
1962
|
const height = screen.height / screen.k;
|
|
1782
1963
|
if (!(height > 0))
|
|
1783
1964
|
return;
|
|
1784
|
-
const viewBox =
|
|
1965
|
+
const viewBox = `${screen.vx} ${screen.vy} ${width} ${height}`;
|
|
1785
1966
|
if (viewBox === this.#viewBox)
|
|
1786
1967
|
return;
|
|
1787
1968
|
this.#viewBox = viewBox;
|
|
@@ -1895,14 +2076,15 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1895
2076
|
const { width, height } = this.#canvas;
|
|
1896
2077
|
if (context === null || width === 0 || height === 0)
|
|
1897
2078
|
return;
|
|
1898
|
-
// Device pixels to a unit, across and down
|
|
1899
|
-
//
|
|
1900
|
-
const across = width /
|
|
2079
|
+
// Device pixels to a unit, across and down: the ratio of the canvas's buffer to its rectangle,
|
|
2080
|
+
// which holds under any scale the page puts on the element, times `k`.
|
|
2081
|
+
const across = (width / screen.width) * screen.k;
|
|
1901
2082
|
const down = (height / screen.height) * screen.k;
|
|
1902
2083
|
context.setTransform(1, 0, 0, 1, 0, 0);
|
|
1903
2084
|
context.clearRect(0, 0, width, height);
|
|
1904
|
-
context.setTransform(across, 0, 0, down,
|
|
2085
|
+
context.setTransform(across, 0, 0, down, -screen.vx * across, -screen.vy * down);
|
|
1905
2086
|
context.fillStyle = colour;
|
|
2087
|
+
const right = screen.vx + screen.width / screen.k;
|
|
1906
2088
|
const bottom = screen.vy + screen.height / screen.k;
|
|
1907
2089
|
const erasing = this.#eraser.pending;
|
|
1908
2090
|
for (const stroke of this.#work.strokes) {
|
|
@@ -1912,6 +2094,8 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1912
2094
|
const box = this.#pointBoxOf(stroke);
|
|
1913
2095
|
if (box.maxY + 2 * INK_REACH < screen.vy || box.minY - 2 * INK_REACH > bottom)
|
|
1914
2096
|
continue;
|
|
2097
|
+
if (box.maxX + 2 * INK_REACH < screen.vx || box.minX - 2 * INK_REACH > right)
|
|
2098
|
+
continue;
|
|
1915
2099
|
const ink = this.#finished(stroke);
|
|
1916
2100
|
if (ink.bottom >= screen.vy && ink.top <= bottom)
|
|
1917
2101
|
context.fill(ink.path);
|
|
@@ -2007,7 +2191,17 @@ function contactOf(event) {
|
|
|
2007
2191
|
time: event.timeStamp,
|
|
2008
2192
|
};
|
|
2009
2193
|
}
|
|
2010
|
-
/** The
|
|
2194
|
+
/** The scroll an event's fingers owe a scroller, in whole pixels, if any: the rest waits (R29). */
|
|
2195
|
+
function owed(latch) {
|
|
2196
|
+
if (!latch)
|
|
2197
|
+
return undefined;
|
|
2198
|
+
const step = Math.round(latch.rest);
|
|
2199
|
+
if (step === 0)
|
|
2200
|
+
return undefined;
|
|
2201
|
+
latch.rest -= step;
|
|
2202
|
+
return { target: latch.target, step };
|
|
2203
|
+
}
|
|
2204
|
+
/** The values of `overflow-y`, or of `overflow-x`, that make an element scroll its content. */
|
|
2011
2205
|
const SCROLLING = /^(auto|scroll|overlay)$/;
|
|
2012
2206
|
/**
|
|
2013
2207
|
* The values of `overflow-y` that make an element a scroll container, which a sticky box sticks in:
|
|
@@ -2018,9 +2212,10 @@ const CONTAINING = /^(auto|scroll|overlay|hidden)$/;
|
|
|
2018
2212
|
* The overflow of the page's viewport: the root's, or the body's when the root's is visible (CSS
|
|
2019
2213
|
* Overflow 3), and then the body scrolls nothing of its own.
|
|
2020
2214
|
*/
|
|
2021
|
-
function pageOverflow() {
|
|
2022
|
-
const
|
|
2023
|
-
const
|
|
2215
|
+
function pageOverflow(axis = "y") {
|
|
2216
|
+
const along = (style) => (axis === "y" ? style.overflowY : style.overflowX);
|
|
2217
|
+
const root = along(getComputedStyle(document.documentElement));
|
|
2218
|
+
const body = document.body === null ? "visible" : along(getComputedStyle(document.body));
|
|
2024
2219
|
const fromBody = root === "visible" && body !== "visible";
|
|
2025
2220
|
return { overflow: fromBody ? body : root, fromBody };
|
|
2026
2221
|
}
|
|
@@ -2070,10 +2265,68 @@ function offsetOf(element, height) {
|
|
|
2070
2265
|
return 0;
|
|
2071
2266
|
return top.endsWith("%") ? (value / 100) * height : value;
|
|
2072
2267
|
}
|
|
2073
|
-
/**
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2268
|
+
/** How far a scroll short of its end, or past it, still counts as at it: half a CSS pixel. */
|
|
2269
|
+
const EDGE = 0.5;
|
|
2270
|
+
/**
|
|
2271
|
+
* How far an element's content overflows it down, or across along `x`, in CSS pixels: where its
|
|
2272
|
+
* scroll ends.
|
|
2273
|
+
*/
|
|
2274
|
+
function overflowOf(element, axis = "y") {
|
|
2275
|
+
return axis === "y"
|
|
2276
|
+
? element.scrollHeight - element.clientHeight
|
|
2277
|
+
: element.scrollWidth - element.clientWidth;
|
|
2278
|
+
}
|
|
2279
|
+
/**
|
|
2280
|
+
* How far an element is scrolled from the top of its content, or from its left along `x`: in a
|
|
2281
|
+
* scroller that runs right to left, `scrollLeft` goes from 0 at the right down to minus its
|
|
2282
|
+
* overflow at the left (CSSOM View).
|
|
2283
|
+
*/
|
|
2284
|
+
function scrolledOf(element, axis) {
|
|
2285
|
+
if (axis === "y")
|
|
2286
|
+
return element.scrollTop;
|
|
2287
|
+
// Only one that overflows across reads its direction: it is asked at every scroll.
|
|
2288
|
+
const over = overflowOf(element, "x");
|
|
2289
|
+
if (over <= EDGE)
|
|
2290
|
+
return 0;
|
|
2291
|
+
return getComputedStyle(element).direction === "rtl"
|
|
2292
|
+
? over + element.scrollLeft
|
|
2293
|
+
: element.scrollLeft;
|
|
2294
|
+
}
|
|
2295
|
+
/**
|
|
2296
|
+
* Whether an element can scroll on down its content, for `onward`, or back up it — or, along `x`,
|
|
2297
|
+
* on right across it or back left.
|
|
2298
|
+
*/
|
|
2299
|
+
function scrollsOn(element, onward, axis = "y") {
|
|
2300
|
+
const at = scrolledOf(element, axis);
|
|
2301
|
+
return onward ? at < overflowOf(element, axis) - EDGE : at > EDGE;
|
|
2302
|
+
}
|
|
2303
|
+
/**
|
|
2304
|
+
* Where what a scroller shows is in sight down the screen, or across it along `x` (T94): within
|
|
2305
|
+
* the ancestors that clip it and the visual viewport, from `start` to `end` — and how much it
|
|
2306
|
+
* shows whole, for a scroller out of sight.
|
|
2307
|
+
*/
|
|
2308
|
+
function sightOf(target, axis) {
|
|
2309
|
+
const down = axis === "y";
|
|
2310
|
+
const sight = window.visualViewport;
|
|
2311
|
+
let start = (down ? sight?.offsetTop : sight?.offsetLeft) ?? 0;
|
|
2312
|
+
let end = start + ((down ? sight?.height : sight?.width) ?? (down ? innerHeight : innerWidth));
|
|
2313
|
+
let whole = end - start;
|
|
2314
|
+
if (target instanceof Element) {
|
|
2315
|
+
const own = clientBoxOf(target);
|
|
2316
|
+
whole = down ? own.bottom - own.top : own.right - own.left;
|
|
2317
|
+
// The scroller, and every ancestor that clips it, up to the page, whose part is the sight.
|
|
2318
|
+
for (let node = target; node !== null; node = parentOf(node)) {
|
|
2319
|
+
if (node === document.documentElement)
|
|
2320
|
+
break;
|
|
2321
|
+
const style = node === target ? undefined : getComputedStyle(node);
|
|
2322
|
+
if (style !== undefined && (down ? style.overflowY : style.overflowX) === "visible")
|
|
2323
|
+
continue;
|
|
2324
|
+
const clip = node === target ? own : clientBoxOf(node);
|
|
2325
|
+
start = Math.max(start, down ? clip.top : clip.left);
|
|
2326
|
+
end = Math.min(end, down ? clip.bottom : clip.right);
|
|
2327
|
+
}
|
|
2328
|
+
}
|
|
2329
|
+
return { start, end, whole };
|
|
2077
2330
|
}
|
|
2078
2331
|
/** Whether an element holds another in the flat tree, across the borders of shadow trees. */
|
|
2079
2332
|
function holds(ancestor, element) {
|
|
@@ -2099,22 +2352,24 @@ function parentOf(element) {
|
|
|
2099
2352
|
*/
|
|
2100
2353
|
function clientBoxOf(element) {
|
|
2101
2354
|
const box = element.getBoundingClientRect();
|
|
2102
|
-
const
|
|
2103
|
-
const across = wide > 0 ? box.width / wide : 1;
|
|
2355
|
+
const across = scaleOf(element, box, "x");
|
|
2104
2356
|
const down = scaleOf(element, box);
|
|
2105
2357
|
const left = box.left + element.clientLeft * across;
|
|
2106
2358
|
const top = box.top + element.clientTop * down;
|
|
2107
2359
|
const right = left + element.clientWidth * across;
|
|
2108
2360
|
const bottom = top + element.clientHeight * down;
|
|
2109
|
-
return { left, top, right, bottom, down };
|
|
2361
|
+
return { left, top, right, bottom, across, down };
|
|
2110
2362
|
}
|
|
2111
2363
|
/**
|
|
2112
|
-
* How much the page scales an element down the screen
|
|
2113
|
-
* its rectangle on the screen, when the caller has it already.
|
|
2364
|
+
* How much the page scales an element down the screen, or across it along `x` (T94): 1 unless an
|
|
2365
|
+
* ancestor transforms it. `box` is its rectangle on the screen, when the caller has it already.
|
|
2114
2366
|
*/
|
|
2115
|
-
function scaleOf(element, box) {
|
|
2116
|
-
const
|
|
2117
|
-
|
|
2367
|
+
function scaleOf(element, box, axis = "y") {
|
|
2368
|
+
const laid = element instanceof HTMLElement ? element[axis === "y" ? "offsetHeight" : "offsetWidth"] : 0;
|
|
2369
|
+
if (laid <= 0)
|
|
2370
|
+
return 1;
|
|
2371
|
+
const shown = box ?? element.getBoundingClientRect();
|
|
2372
|
+
return (axis === "y" ? shown.height : shown.width) / laid;
|
|
2118
2373
|
}
|
|
2119
2374
|
/**
|
|
2120
2375
|
* A path around an outline: a closed curve through the midpoints of its edges, each point of the
|