@altnovatrail/canvas 0.3.11 → 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.
@@ -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,13 +99,23 @@ 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 layers of the sheet stand in its visible part, the stage, and each shows what lies there,
99
- // shifted in the frame the ink is drawn in (R18, R23; architecture 3.2 in docs/architecture.md).
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
103
- // (R28). The element is as tall as the row, the statement and the sheet unless it is given a
104
- // height: then the statement and the sheet scroll in it, under the row. A worked example's open
112
+ // (R28). In a low window, where the row would cover more than a quarter of what it sticks in, it
113
+ // stands in its place instead, `loose`, and goes with the scrolling (1.4.10, 2.4.11). A button in
114
+ // focus shows a ring in the ink's colour, two pixels out, that the row's side margins keep on its
115
+ // paper; an unavailable one is paler by its colour, not its opacity, which would pale the ring too
116
+ // (2.4.7, 1.4.11). Nothing in the element moves but the scrolling, which jumps (2.3.3). The
117
+ // element is as tall as the row, the statement and the sheet unless it is given a height: then
118
+ // the statement and the sheet scroll in it, under the row. A worked example's open
105
119
  // parts stand between the statement and the sheet, and the region that tells of them is out of
106
120
  // sight, but not out of the tree a screen reader reads, where a hidden region would tell nothing
107
121
  // (R42). A picture's frame there stands in its item's line, from its top, so that the item's
@@ -130,12 +144,14 @@ const STYLE = `
130
144
  .tools {
131
145
  position: sticky; top: var(--novatrail-canvas-top, 0px); z-index: 1; flex: none;
132
146
  box-sizing: border-box; width: min(100%, ${K_MAX * SHEET.width}px); margin: 0 auto;
133
- padding: 8px 0; display: flex; flex-wrap: wrap; gap: 8px;
147
+ padding: 8px 4px; display: flex; flex-wrap: wrap; gap: 8px;
134
148
  background: var(--novatrail-canvas-paper, ${PAPER}); color: var(--novatrail-canvas-ink, ${INK});
135
149
  }
136
- .tools .demo, .tools .choice, .tools .updown {
150
+ .tools.loose { position: static; }
151
+ .tools .demo, .tools .choice, .tools .updown, .tools .zoom, .tools .across {
137
152
  display: flex; gap: 8px; margin-inline-end: 16px;
138
153
  }
154
+ .tools:dir(rtl) .across { flex-direction: row-reverse; }
139
155
  .tools button {
140
156
  box-sizing: border-box; min-width: 44px; min-height: 44px; padding: 0 8px; margin: 0;
141
157
  font: inherit; color: inherit; background: transparent; cursor: pointer;
@@ -145,17 +161,44 @@ const STYLE = `
145
161
  background: var(--novatrail-canvas-ink, ${INK}); color: var(--novatrail-canvas-paper, ${PAPER});
146
162
  border-color: var(--novatrail-canvas-ink, ${INK});
147
163
  }
148
- .tools [aria-disabled="true"] { opacity: 0.4; cursor: default; }
164
+ .tools button:focus-visible {
165
+ outline: 2px solid var(--novatrail-canvas-ink, ${INK}); outline-offset: 2px;
166
+ }
167
+ .tools [aria-disabled="true"] {
168
+ color: color-mix(
169
+ in srgb, var(--novatrail-canvas-ink, ${INK}) 40%, var(--novatrail-canvas-paper, ${PAPER})
170
+ );
171
+ cursor: default;
172
+ }
173
+ @supports not (color: color-mix(in srgb, red 40%, blue)) {
174
+ .tools [aria-disabled="true"] { opacity: 0.4; }
175
+ }
149
176
  .tools [hidden] { display: none; }
150
177
  .tools button svg {
151
178
  display: inline-block; width: 1.125em; height: 1.125em; margin-inline-end: 4px;
152
179
  vertical-align: -0.25em; fill: currentColor;
153
180
  }
154
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
+ }
155
185
  .viewport {
156
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);
157
201
  }
158
- .column { position: relative; width: min(100%, ${K_MAX * SHEET.width}px); margin: 0 auto; }
159
202
  .statement { padding: 0 0 12px; overflow-wrap: break-word; }
160
203
  .statement:empty { display: none; }
161
204
  .statement p { margin: 0 0 8px; }
@@ -179,7 +222,7 @@ const STYLE = `
179
222
  background: var(--novatrail-canvas-paper, ${PAPER});
180
223
  }
181
224
  .stage {
182
- position: sticky; top: 0; max-height: 100%; overflow: clip;
225
+ position: sticky; top: 0; left: 0; max-height: 100%; overflow: clip;
183
226
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
184
227
  }
185
228
  .spot { position: absolute; visibility: hidden; }
@@ -224,7 +267,8 @@ canvas {
224
267
  .tools [aria-checked="true"] {
225
268
  background: Highlight; color: HighlightText; border-color: Highlight;
226
269
  }
227
- .tools [aria-disabled="true"] { color: GrayText; border-color: GrayText; opacity: 1; }
270
+ .tools [aria-disabled="true"] { color: GrayText; border-color: GrayText; }
271
+ .tools button:focus-visible, .viewport:focus-visible + .ring { outline-color: Highlight; }
228
272
  }
229
273
  `;
230
274
  /** The sheet of STYLE that the elements of a document adopt, made by the first of them there. */
@@ -281,7 +325,14 @@ function htmlLayer(name) {
281
325
  * commands undo and clear, which the contract does not name.
282
326
  */
283
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");
284
334
  #viewport = document.createElement("div");
335
+ #ring = document.createElement("div");
285
336
  #column = document.createElement("div");
286
337
  #sheet = document.createElement("div");
287
338
  /** The sheet's visible part, where its layers stand, bottom to top (concepts 8 spec; R23). */
@@ -314,7 +365,9 @@ export class NovatrailCanvas extends HTMLElement {
314
365
  choose: (tool) => this.#choose(tool),
315
366
  undo: () => this.undo(),
316
367
  clear: () => this.clear(),
317
- upOrDown: (onward) => this.#upOrDown(onward),
368
+ upOrDown: (onward) => this.#step(onward, "y"),
369
+ zoom: (larger) => this.#zoomBy(larger),
370
+ leftOrRight: (onward) => this.#step(onward, "x"),
318
371
  // The finger writes again on every sheet of the page, with no event: the next pen's touch
319
372
  // puts the page back in pen mode (R29).
320
373
  fingers: () => {
@@ -350,25 +403,49 @@ export class NovatrailCanvas extends HTMLElement {
350
403
  #hands = new Hands({
351
404
  writer: () => this.#writer(),
352
405
  drop: (pointerId) => this.#drop(pointerId),
353
- scroll: (dy, fresh) => this.#scroll(dy, fresh),
406
+ scroll: (dx, dy, fresh) => this.#scroll(dx, dy, fresh),
407
+ across: () => this.#zoom > 1,
354
408
  }, PEN_MODE);
355
409
  /** Whether the row shows the finger's button: it comes and goes between strokes alone. */
356
410
  #finger = false;
357
- /** What two fingers scroll in the gesture under way, chosen at its first step. */
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
+ */
358
415
  #latch;
416
+ #latchAcross;
359
417
  /**
360
418
  * A scroll of the page, or of an ancestor of the element in the page's own tree, heard on its way
361
419
  * down: Up or Down may have come to an end, or left one (R33). Other scrollers are none of theirs.
362
420
  */
363
421
  #heard = (event) => {
364
422
  const target = event.target;
365
- if (target === document || (target instanceof Element && holds(target, this)))
423
+ if (target === document || (target instanceof Element && holds(target, this))) {
424
+ // A new --novatrail-canvas-top changes no size: the next scroll is the first to hear of it,
425
+ // by one read of the row's style, and only a new offset asks anew whether the row sticks.
426
+ if (getComputedStyle(this.#tools.element).top !== this.#topSeen)
427
+ this.#stick();
366
428
  this.#edges();
429
+ }
367
430
  };
368
- /** The page grown or shrunk, or the window: what scrolls the element may have room now, or none. */
369
- #page = new ResizeObserver(() => this.#edges());
370
- #resized = () => this.#edges();
431
+ /**
432
+ * The page grown or shrunk, or the window, the scroller the row sticks in or the row itself: what
433
+ * scrolls the element may have room now, or none, and the row may cover too much of it to stick.
434
+ */
435
+ #resized = () => {
436
+ this.#stick();
437
+ this.#region();
438
+ this.#edges();
439
+ };
440
+ #page = new ResizeObserver(this.#resized);
441
+ /** The scroll container the row sticks in, which `#page` watches, or null for the page's viewport. */
442
+ #port = null;
443
+ /** The row's `top` as the last check of whether it sticks read it: the consumer's offset. */
444
+ #topSeen = "";
371
445
  #unwatchPen;
446
+ #unwatchZoom;
447
+ /** The page's zoom as the sheet is laid out by it (T94). */
448
+ #zoom = 0;
372
449
  #tool = "pen";
373
450
  #task;
374
451
  /** A lesson's worked example as the consumer gave it, and as the element checked it (R42). */
@@ -439,8 +516,11 @@ export class NovatrailCanvas extends HTMLElement {
439
516
  */
440
517
  #k = 0;
441
518
  #sheetAt = 0;
442
- /** The top of the visible part, in sheet units, kept from the last scroll. */
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. */
443
522
  #vy = 0;
523
+ #vx = 0;
444
524
  /** The view box the layers were last given, so that a frame without a scroll writes none. */
445
525
  #viewBox = "";
446
526
  #frame = 0;
@@ -451,7 +531,9 @@ export class NovatrailCanvas extends HTMLElement {
451
531
  super();
452
532
  const root = this.attachShadow({ mode: "open" });
453
533
  adoptStyle(root);
534
+ this.#wrapper.className = "frame";
454
535
  this.#viewport.className = "viewport";
536
+ this.#ring.className = "ring";
455
537
  this.#column.className = "column";
456
538
  this.#sheet.className = "sheet";
457
539
  this.#stage.className = "stage";
@@ -467,7 +549,9 @@ export class NovatrailCanvas extends HTMLElement {
467
549
  this.#sheet.append(this.#stage, this.#parts.spot);
468
550
  this.#column.append(this.#statement.element, this.#parts.list, this.#parts.status, this.#sheet);
469
551
  this.#viewport.append(this.#column);
470
- root.append(this.#tools.element, this.#viewport);
552
+ this.#wrapper.append(this.#viewport, this.#ring);
553
+ root.append(this.#tools.element, this.#wrapper);
554
+ this.#zoomTo(PAGE_ZOOM.value);
471
555
  this.#lay(NO_TASK);
472
556
  this.#refresh();
473
557
  this.#canvas.addEventListener("pointerdown", (event) => this.#down(event));
@@ -494,6 +578,8 @@ export class NovatrailCanvas extends HTMLElement {
494
578
  this.#canvas.addEventListener("transitionend", () => this.#request());
495
579
  this.#canvas.addEventListener("transitioncancel", () => this.#request());
496
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());
497
583
  attachProbe(this, () => ({
498
584
  strokes: this.#work.strokes,
499
585
  live: this.#recorder.live,
@@ -825,10 +911,16 @@ export class NovatrailCanvas extends HTMLElement {
825
911
  unwatch();
826
912
  unenlist();
827
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());
828
917
  // A scroll does not bubble, but the document hears every one of its tree on the way down. A
829
918
  // page that grows or shrinks scrolls nothing, and neither does a window of another size.
830
919
  document.addEventListener("scroll", this.#heard, { capture: true, passive: true });
831
920
  this.#page.observe(document.documentElement);
921
+ // The row wraps anew with the element's width, its language, the canvas kind and the finger's
922
+ // button, and grows or shrinks.
923
+ this.#page.observe(this.#tools.element);
832
924
  window.addEventListener("resize", this.#resized);
833
925
  this.#refresh();
834
926
  }
@@ -840,6 +932,7 @@ export class NovatrailCanvas extends HTMLElement {
840
932
  */
841
933
  attributeChangedCallback() {
842
934
  this.#tools.label(this.getAttribute("lang"));
935
+ this.#nameRegion();
843
936
  this.#nameBoxes();
844
937
  this.#markBoxes();
845
938
  }
@@ -916,10 +1009,14 @@ export class NovatrailCanvas extends HTMLElement {
916
1009
  this.#eraser.end();
917
1010
  this.#hands.reset();
918
1011
  this.#latch = undefined;
1012
+ this.#latchAcross = undefined;
919
1013
  this.#unwatchPen?.();
920
1014
  this.#unwatchPen = undefined;
1015
+ this.#unwatchZoom?.();
1016
+ this.#unwatchZoom = undefined;
921
1017
  document.removeEventListener("scroll", this.#heard, { capture: true });
922
1018
  this.#page.disconnect();
1019
+ this.#port = null;
923
1020
  window.removeEventListener("resize", this.#resized);
924
1021
  // A stroke that ended here may be the one the other rows of the page waited for.
925
1022
  PEN_MODE.settle();
@@ -1234,8 +1331,10 @@ export class NovatrailCanvas extends HTMLElement {
1234
1331
  */
1235
1332
  #lifted(pointerId) {
1236
1333
  this.#hands.lift(pointerId);
1237
- if (!this.#hands.scrolling)
1334
+ if (!this.#hands.scrolling) {
1238
1335
  this.#latch = undefined;
1336
+ this.#latchAcross = undefined;
1337
+ }
1239
1338
  this.#refresh();
1240
1339
  PEN_MODE.settle();
1241
1340
  }
@@ -1271,18 +1370,20 @@ export class NovatrailCanvas extends HTMLElement {
1271
1370
  }
1272
1371
  /**
1273
1372
  * Where the sheet is on the screen now. The rectangles of the canvas and of the sheet give it:
1274
- * the canvas is as wide as the sheet, and its top is the top of the visible part. `k` is the one
1275
- * 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
1276
1375
  * rectangle gives the part of the canvas it shows: with the statement in sight, the canvas
1277
1376
  * goes on below the container's bottom. The row of buttons above the container hides what it
1278
- * covers, once it sticks over the sheet as the page scrolls the element by (R28).
1377
+ * covers, once it sticks over the sheet as the page scrolls the element by (R28); in its place,
1378
+ * in a low window, it stands above the container and hides nothing of it.
1279
1379
  */
1280
1380
  #view() {
1281
1381
  const canvas = this.#canvas.getBoundingClientRect();
1282
- if (canvas.width <= 0)
1283
- return undefined;
1284
- const k = scaleFor(canvas.width);
1285
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;
1286
1387
  const vy = (canvas.top - sheet.top) / k;
1287
1388
  const port = clientBoxOf(this.#viewport);
1288
1389
  // The row ends where the container begins, unless it sticks over it.
@@ -1293,161 +1394,279 @@ export class NovatrailCanvas extends HTMLElement {
1293
1394
  right: Math.min(canvas.right, port.right),
1294
1395
  bottom: Math.min(canvas.bottom, port.bottom),
1295
1396
  };
1296
- return { k, vy, left: canvas.left, top: canvas.top, height: canvas.height, shown };
1397
+ const { left, top, width, height } = canvas;
1398
+ return { k, vx, vy, left, top, width, height, shown };
1297
1399
  }
1298
- /** The sheet's top in the container's content, in CSS pixels as laid out: under the statement. */
1299
- #sheetTop() {
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) {
1300
1406
  const port = clientBoxOf(this.#viewport);
1301
1407
  const sheet = this.#sheet.getBoundingClientRect();
1302
- return (sheet.top - port.top) / port.down + this.#viewport.scrollTop;
1408
+ return axis === "y"
1409
+ ? (sheet.top - port.top) / port.down + this.#viewport.scrollTop
1410
+ : (sheet.left - port.left) / port.across + this.#viewport.scrollLeft;
1303
1411
  }
1304
1412
  /**
1305
- * The top of the visible part from the scroll and the last layout, without reading the layout
1306
- * again: the stage sticks to the container's top once the sheet's top has gone past it. Up or
1307
- * Down may have come to an end of the container, or left one.
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.
1308
1417
  */
1309
1418
  #scrolled() {
1310
- if (this.#k > 0)
1419
+ if (this.#k > 0) {
1311
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
+ }
1312
1423
  this.#request();
1313
1424
  this.#edges();
1314
1425
  }
1315
1426
  /**
1316
- * Up or Down was pressed (R33): what two fingers would scroll that way goes by four fifths of what
1317
- * it shows in sight. It is chosen anew at every press, so that a container at its end hands the
1318
- * scroll on at the next. While a pointer writes or erases on a sheet of the page, nothing moves
1319
- * 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.
1320
1431
  */
1321
- #upOrDown(onward) {
1322
- const latch = PEN_MODE.writing ? undefined : this.#scrollerFor(onward);
1432
+ #step(onward, axis) {
1433
+ const latch = PEN_MODE.writing ? undefined : this.#scrollerFor(onward, axis);
1323
1434
  if (latch !== undefined) {
1324
- const step = Math.max(1, Math.round((UP_DOWN * this.#seen(latch.target)) / latch.scale));
1325
- latch.target.scrollBy({ top: onward ? step : -step, behavior: "instant" });
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" });
1326
1439
  }
1327
1440
  this.#edges();
1328
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
+ }
1329
1488
  /**
1330
1489
  * How much of what a scroller shows is in sight, down the screen: within the ancestors that clip
1331
- * it and the visual viewport, and below the row of buttons while the row sticks over its top. A
1332
- * scroller out of sight counts whole.
1333
- */
1334
- #seen(target) {
1335
- const sight = window.visualViewport;
1336
- let top = sight?.offsetTop ?? 0;
1337
- let bottom = top + (sight?.height ?? window.innerHeight);
1338
- let whole = bottom - top;
1339
- if (target instanceof Element) {
1340
- const own = clientBoxOf(target);
1341
- whole = own.bottom - own.top;
1342
- // The scroller, and every ancestor that clips it, up to the page, whose part is the sight.
1343
- for (let node = target; node !== null; node = parentOf(node)) {
1344
- if (node === document.documentElement)
1345
- break;
1346
- if (node !== target && getComputedStyle(node).overflowY === "visible")
1347
- continue;
1348
- const clip = node === target ? own : clientBoxOf(node);
1349
- top = Math.max(top, clip.top);
1350
- bottom = Math.min(bottom, clip.bottom);
1490
+ * it and the visual viewport, and below the row of buttons while the row sticks over its top, or
1491
+ * below the consumer's band, --novatrail-canvas-top, while the row stands in its place in a low
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;
1500
+ // The row hides the top while it sticks there, as far down as the consumer's offset puts it. A
1501
+ // row that stands in its place, in a low window, leaves the consumer's band over it alone.
1502
+ const row = this.#tools.element;
1503
+ if (this.#tools.sticks) {
1504
+ const box = row.getBoundingClientRect();
1505
+ if (box.top <= top + offsetOf(row, 0) * scaleOf(row, box) + 0.5) {
1506
+ top = Math.max(top, box.bottom);
1351
1507
  }
1352
1508
  }
1353
- // The row hides the top while it sticks there, as far down as the consumer's offset puts it.
1354
- const row = this.#tools.element;
1355
- const box = row.getBoundingClientRect();
1356
- if (box.top <= top + (Number.parseFloat(getComputedStyle(row).top) || 0) + 0.5) {
1357
- top = Math.max(top, box.bottom);
1509
+ else {
1510
+ top = Math.max(top, this.#band(target));
1358
1511
  }
1359
1512
  return bottom > top ? bottom - top : whole;
1360
1513
  }
1361
1514
  /**
1362
- * Up and Down as the scrolling now is (R33): each stands unavailable while what two fingers would
1363
- * scroll its way can go no further. The element hears its container, the page and the ancestors
1364
- * in the page's own tree scroll, and the page and the window change size, but not the scrolling
1365
- * in another shadow tree: a press asks anew.
1515
+ * Whether the row sticks (R28): while it is no taller than a quarter of what it sticks in, below
1516
+ * the consumer's offset. In a lower window, or once it wraps over more of it, it stands in its
1517
+ * place, above the statement, and goes with the scrolling, as WCAG's technique C34 un-fixes a
1518
+ * sticky header (1.4.10, 2.4.11). Its place and its size are the same either way, so the choice
1519
+ * changes nothing it is made from. The scroller it sticks in is watched for its size. Only an
1520
+ * element on the page asks: what calls this goes when the element leaves it.
1521
+ */
1522
+ #stick() {
1523
+ const room = roomOf(this.#tools.element);
1524
+ if (room.port !== this.#port) {
1525
+ if (this.#port !== null)
1526
+ this.#page.unobserve(this.#port);
1527
+ if (room.port !== null)
1528
+ this.#page.observe(room.port);
1529
+ this.#port = room.port;
1530
+ }
1531
+ const row = this.#tools.element;
1532
+ const tall = row.getBoundingClientRect().height;
1533
+ if (tall > 0 && room.height > 0)
1534
+ this.#tools.stick(4 * tall <= room.height - room.offset);
1535
+ this.#topSeen = getComputedStyle(row).top;
1536
+ }
1537
+ /**
1538
+ * Where the consumer's band over the top of what the row sticks in ends, down the screen: its
1539
+ * offset, --novatrail-canvas-top, below the top of the scroller or of the window — for a target
1540
+ * that is or lies within what the row sticks in, and above everything for any other.
1541
+ */
1542
+ #band(target) {
1543
+ const room = roomOf(this.#tools.element);
1544
+ // The band stands over the top of what the row sticks in, and so over a scroller within it,
1545
+ // but not over the page's when the row sticks in a scroller of its own.
1546
+ const within = room.port === null
1547
+ ? target !== undefined
1548
+ : target instanceof Element && (target === room.port || holds(room.port, target));
1549
+ return within ? room.top + room.offset : Number.NEGATIVE_INFINITY;
1550
+ }
1551
+ /**
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.
1366
1557
  */
1367
1558
  #edges() {
1368
1559
  if (!this.isConnected)
1369
1560
  return;
1370
1561
  const above = this.#scrollerFor(false) !== undefined;
1371
- this.#tools.edges(above, this.#scrollerFor(true) !== undefined);
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);
1372
1567
  }
1373
1568
  /**
1374
- * Two fingers moved down the screen by `dy` CSS pixels (R29), and what they scroll goes with
1375
- * them. A gesture's first step chooses it, for the whole gesture: the sheet's own container if it
1376
- * can go that way, or else the nearest ancestor of the element that scrolls and can, or else the
1377
- * page. A container at its end hands the scroll on at the next gesture, not within this one. The
1378
- * steps of an event add up, and its end scrolls them at once.
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.
1379
1576
  */
1380
- #scroll(dy, fresh) {
1381
- if (fresh)
1382
- this.#latch = this.#scrollerFor(dy < 0);
1577
+ #scroll(dx, dy, fresh) {
1578
+ if (fresh) {
1579
+ this.#latch = undefined;
1580
+ this.#latchAcross = undefined;
1581
+ }
1383
1582
  // The content goes with the fingers: down the screen, the scroll goes back.
1384
- if (this.#latch !== undefined)
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)
1385
1589
  this.#latch.rest -= dy / this.#latch.scale;
1590
+ if (this.#latchAcross)
1591
+ this.#latchAcross.rest -= dx / this.#latchAcross.scale;
1386
1592
  }
1387
- /** The scroll the fingers' moves of an event owe, in whole pixels: the rest waits for the next. */
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
+ */
1388
1597
  #flush() {
1389
- const latch = this.#latch;
1390
- if (latch === undefined)
1391
- return;
1392
- const step = Math.round(latch.rest);
1393
- if (step === 0)
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" });
1394
1602
  return;
1395
- latch.rest -= step;
1396
- latch.target.scrollBy({ top: step, behavior: "instant" });
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" });
1397
1608
  }
1398
- /** What scrolls on down the content, for `onward`, or back up it. */
1399
- #scrollerFor(onward) {
1400
- if (scrollsOn(this.#viewport, onward)) {
1401
- return { target: this.#viewport, scale: scaleOf(this.#viewport), rest: 0 };
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 };
1402
1616
  }
1403
- // The viewport takes the root's overflow, or the body's when the root's is visible (CSS
1404
- // Overflow 3): then the body scrolls nothing of its own, and the page scrolls only as it allows.
1405
- const root = getComputedStyle(document.documentElement).overflowY;
1406
- const body = document.body === null ? "visible" : getComputedStyle(document.body).overflowY;
1407
- const fromBody = root === "visible" && body !== "visible";
1617
+ const { overflow, fromBody } = pageOverflow(axis);
1408
1618
  for (let node = parentOf(this); node !== null; node = parentOf(node)) {
1409
- if (node === document.documentElement || (fromBody && node === document.body))
1619
+ if (isPage(node, fromBody))
1410
1620
  break;
1411
1621
  const style = getComputedStyle(node);
1412
- if (!SCROLLING.test(style.overflowY))
1622
+ if (!SCROLLING.test(axis === "y" ? style.overflowY : style.overflowX))
1413
1623
  continue;
1414
- if (scrollsOn(node, onward))
1415
- 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
+ }
1416
1627
  // A container that keeps its scrolling to itself hands none on.
1417
- if (style.overscrollBehaviorY === "contain" || style.overscrollBehaviorY === "none") {
1628
+ const keeps = axis === "y" ? style.overscrollBehaviorY : style.overscrollBehaviorX;
1629
+ if (keeps === "contain" || keeps === "none")
1418
1630
  return undefined;
1419
- }
1420
1631
  }
1421
1632
  // A page that hides its overflow, as one under a modal does, a finger would not scroll: nor do two.
1422
- const overflow = fromBody ? body : root;
1423
1633
  if (overflow === "hidden" || overflow === "clip")
1424
1634
  return undefined;
1425
1635
  const page = document.scrollingElement;
1426
- if (page === null || !scrollsOn(page, onward))
1636
+ if (page === null || !scrollsOn(page, onward, axis))
1427
1637
  return undefined;
1428
1638
  return { target: window, scale: 1, rest: 0 };
1429
1639
  }
1430
1640
  /**
1431
1641
  * The stage takes the visible part of the sheet, and the canvas a pixel of its buffer for each
1432
1642
  * device pixel: its size on the screen times the density (R18). The layers follow in the same
1433
- * frame.
1643
+ * frame. However large the zoom, the canvas is no larger than the container (T94).
1434
1644
  */
1435
1645
  #layout() {
1646
+ // The content of a new size, or the container, may overflow it now, or no more.
1647
+ this.#region();
1436
1648
  const area = this.#area;
1437
1649
  if (area === undefined || area.width === 0)
1438
1650
  return;
1439
- // The column is as wide as the container up to the cap, and the stage as tall as the visible
1440
- // part of the sheet.
1441
- const k = fitScale(area.width);
1442
- const sheetAt = this.#sheetTop();
1443
- // A new width scales the sheet, and a statement of a new height moves it: the top of the
1444
- // visible part stays where it was on the sheet, unless the sheet's top is in sight. A new
1445
- // height of the container alone moves neither, and a scroll under way goes on.
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.
1446
1660
  if (this.#vy > 0 && (k !== this.#k || Math.abs(sheetAt - this.#sheetAt) > 0.5)) {
1447
1661
  this.#viewport.scrollTop = sheetAt + this.#vy * k;
1448
1662
  }
1663
+ if (this.#vx > 0 && k !== this.#k)
1664
+ this.#viewport.scrollLeft = sheetAtX + this.#vx * k;
1449
1665
  this.#k = k;
1450
1666
  this.#sheetAt = sheetAt;
1667
+ this.#sheetAtX = sheetAtX;
1668
+ this.#keepDirection();
1669
+ this.#stage.style.width = `${Math.min(area.width, SHEET.width * k)}px`;
1451
1670
  this.#stage.style.height = `${Math.min(area.height, SHEET.height * k)}px`;
1452
1671
  const onScreen = this.#canvas.getBoundingClientRect();
1453
1672
  const width = Math.round(onScreen.width * devicePixelRatio);
@@ -1631,7 +1850,9 @@ export class NovatrailCanvas extends HTMLElement {
1631
1850
  /**
1632
1851
  * An item of the list, or an area of the sheet, comes into sight at once, as near as it is (R42),
1633
1852
  * and then out from under the row of buttons, should the row stick over where it came to: what
1634
- * scrolls back up goes back as far as the row covers it. A margin of scrolling would hold for
1853
+ * scrolls back up goes back as far as the row covers it. A row in its place, in a low window,
1854
+ * covers nothing, and the consumer's band, --novatrail-canvas-top, is what it comes out from
1855
+ * under then. A margin of scrolling would hold for
1635
1856
  * every scroller of the chain alike — the sheet's container, which the row stands above, and the
1636
1857
  * page, which it sticks over. An area of the sheet taller than what is in sight comes by its top
1637
1858
  * from below, but by its bottom from above, and then the row's cover brings its top back into
@@ -1639,16 +1860,25 @@ export class NovatrailCanvas extends HTMLElement {
1639
1860
  */
1640
1861
  #bringIntoSight(item) {
1641
1862
  item.scrollIntoView({ block: "nearest", behavior: "instant" });
1642
- // A scroller that comes to its top hands the rest on to the next, as Up does (R33): each of
1643
- // the chain goes once.
1644
- const gone = new Set();
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();
1645
1868
  for (;;) {
1646
- const row = this.#tools.element.getBoundingClientRect();
1647
- const cover = row.bottom - item.getBoundingClientRect().top;
1648
- const latch = cover > 0.5 ? this.#scrollerFor(false) : undefined;
1649
- if (latch === undefined || gone.has(latch.target))
1869
+ const latch = this.#scrollerFor(false);
1870
+ // A row in its place, in a low window, covers nothing of it: the consumer's band may, over
1871
+ // what the row sticks in.
1872
+ const over = this.#tools.sticks
1873
+ ? this.#tools.element.getBoundingClientRect().bottom
1874
+ : this.#band(latch?.target);
1875
+ const cover = over - item.getBoundingClientRect().top;
1876
+ if (cover <= EDGE || latch === undefined)
1650
1877
  return;
1651
- gone.add(latch.target);
1878
+ const last = covered.get(latch.target);
1879
+ if (last !== undefined && cover >= last - EDGE)
1880
+ return;
1881
+ covered.set(latch.target, cover);
1652
1882
  latch.target.scrollBy({ top: -cover / latch.scale, behavior: "instant" });
1653
1883
  }
1654
1884
  }
@@ -1670,10 +1900,43 @@ export class NovatrailCanvas extends HTMLElement {
1670
1900
  tool: this.#tool,
1671
1901
  undoable: this.#work.undoable,
1672
1902
  clearable: this.#work.strokes.length > 0,
1903
+ larger: PAGE_ZOOM.larger,
1904
+ smaller: PAGE_ZOOM.smaller,
1673
1905
  finger: this.#finger,
1674
1906
  opens: this.#nextPart() !== undefined,
1675
1907
  expanded: demo?.hidden !== true,
1676
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);
1677
1940
  }
1678
1941
  /** What the row of buttons is of, and is named by: a worked example, or the canvas kind (R42). */
1679
1942
  #rowKind() {
@@ -1695,10 +1958,11 @@ export class NovatrailCanvas extends HTMLElement {
1695
1958
  * showing a part of the sheet the others have left.
1696
1959
  */
1697
1960
  #place(screen) {
1961
+ const width = screen.width / screen.k;
1698
1962
  const height = screen.height / screen.k;
1699
1963
  if (!(height > 0))
1700
1964
  return;
1701
- const viewBox = `0 ${screen.vy} ${SHEET.width} ${height}`;
1965
+ const viewBox = `${screen.vx} ${screen.vy} ${width} ${height}`;
1702
1966
  if (viewBox === this.#viewBox)
1703
1967
  return;
1704
1968
  this.#viewBox = viewBox;
@@ -1812,14 +2076,15 @@ export class NovatrailCanvas extends HTMLElement {
1812
2076
  const { width, height } = this.#canvas;
1813
2077
  if (context === null || width === 0 || height === 0)
1814
2078
  return;
1815
- // Device pixels to a unit, across and down. The canvas is as wide as the sheet, and the ratio
1816
- // of its rectangles holds under any scale the page puts on the element.
1817
- const across = width / SHEET.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;
1818
2082
  const down = (height / screen.height) * screen.k;
1819
2083
  context.setTransform(1, 0, 0, 1, 0, 0);
1820
2084
  context.clearRect(0, 0, width, height);
1821
- context.setTransform(across, 0, 0, down, 0, -screen.vy * down);
2085
+ context.setTransform(across, 0, 0, down, -screen.vx * across, -screen.vy * down);
1822
2086
  context.fillStyle = colour;
2087
+ const right = screen.vx + screen.width / screen.k;
1823
2088
  const bottom = screen.vy + screen.height / screen.k;
1824
2089
  const erasing = this.#eraser.pending;
1825
2090
  for (const stroke of this.#work.strokes) {
@@ -1829,6 +2094,8 @@ export class NovatrailCanvas extends HTMLElement {
1829
2094
  const box = this.#pointBoxOf(stroke);
1830
2095
  if (box.maxY + 2 * INK_REACH < screen.vy || box.minY - 2 * INK_REACH > bottom)
1831
2096
  continue;
2097
+ if (box.maxX + 2 * INK_REACH < screen.vx || box.minX - 2 * INK_REACH > right)
2098
+ continue;
1832
2099
  const ink = this.#finished(stroke);
1833
2100
  if (ink.bottom >= screen.vy && ink.top <= bottom)
1834
2101
  context.fill(ink.path);
@@ -1924,12 +2191,142 @@ function contactOf(event) {
1924
2191
  time: event.timeStamp,
1925
2192
  };
1926
2193
  }
1927
- /** The values of `overflow-y` that make an element scroll its content. */
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. */
1928
2205
  const SCROLLING = /^(auto|scroll|overlay)$/;
1929
- /** Whether an element can scroll on down its content, for `onward`, or back up it. */
1930
- function scrollsOn(element, onward) {
1931
- const end = element.scrollHeight - element.clientHeight;
1932
- return onward ? element.scrollTop < end - 0.5 : element.scrollTop > 0.5;
2206
+ /**
2207
+ * The values of `overflow-y` that make an element a scroll container, which a sticky box sticks in:
2208
+ * a hidden overflow too, which a script may scroll though no hand does (CSS Overflow 3).
2209
+ */
2210
+ const CONTAINING = /^(auto|scroll|overlay|hidden)$/;
2211
+ /**
2212
+ * The overflow of the page's viewport: the root's, or the body's when the root's is visible (CSS
2213
+ * Overflow 3), and then the body scrolls nothing of its own.
2214
+ */
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));
2219
+ const fromBody = root === "visible" && body !== "visible";
2220
+ return { overflow: fromBody ? body : root, fromBody };
2221
+ }
2222
+ /** Whether an ancestor on the way up is where the page's viewport takes over. */
2223
+ function isPage(node, fromBody) {
2224
+ return node === document.documentElement || (fromBody && node === document.body);
2225
+ }
2226
+ /**
2227
+ * The scroll container a sticky box sticks in: its nearest ancestor in the flat tree that is one,
2228
+ * or null for the page's viewport.
2229
+ */
2230
+ function stickyPortOf(element) {
2231
+ const { fromBody } = pageOverflow();
2232
+ let node = parentOf(element);
2233
+ while (node !== null && !isPage(node, fromBody)) {
2234
+ if (CONTAINING.test(getComputedStyle(node).overflowY))
2235
+ return node;
2236
+ node = parentOf(node);
2237
+ }
2238
+ return null;
2239
+ }
2240
+ /**
2241
+ * What a sticky box sticks in, on the screen: the scroll container, or null for the page's
2242
+ * viewport, its top and its height — the viewport's without its scroll bar, which does not change
2243
+ * as a phone's toolbar of the browser comes and goes — and the box's offset below that top.
2244
+ */
2245
+ function roomOf(element) {
2246
+ const port = stickyPortOf(element);
2247
+ const laid = port === null ? (document.scrollingElement?.clientHeight ?? 0) : port.clientHeight;
2248
+ const own = port === null ? undefined : clientBoxOf(port);
2249
+ return {
2250
+ port,
2251
+ top: own?.top ?? 0,
2252
+ height: own === undefined ? laid : own.bottom - own.top,
2253
+ offset: offsetOf(element, laid) * scaleOf(element),
2254
+ };
2255
+ }
2256
+ /**
2257
+ * How far below the top of what it sticks in a sticky box stands, by its `top`, in CSS pixels as
2258
+ * laid out: a percentage of the scroller's `height`, and nothing for `auto`. A sticky box's `top`
2259
+ * reads as the pixels it is used at, a static one's as it was given.
2260
+ */
2261
+ function offsetOf(element, height) {
2262
+ const top = getComputedStyle(element).top;
2263
+ const value = Number.parseFloat(top);
2264
+ if (!Number.isFinite(value))
2265
+ return 0;
2266
+ return top.endsWith("%") ? (value / 100) * height : value;
2267
+ }
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 };
1933
2330
  }
1934
2331
  /** Whether an element holds another in the flat tree, across the borders of shadow trees. */
1935
2332
  function holds(ancestor, element) {
@@ -1955,22 +2352,24 @@ function parentOf(element) {
1955
2352
  */
1956
2353
  function clientBoxOf(element) {
1957
2354
  const box = element.getBoundingClientRect();
1958
- const wide = element instanceof HTMLElement ? element.offsetWidth : 0;
1959
- const across = wide > 0 ? box.width / wide : 1;
2355
+ const across = scaleOf(element, box, "x");
1960
2356
  const down = scaleOf(element, box);
1961
2357
  const left = box.left + element.clientLeft * across;
1962
2358
  const top = box.top + element.clientTop * down;
1963
2359
  const right = left + element.clientWidth * across;
1964
2360
  const bottom = top + element.clientHeight * down;
1965
- return { left, top, right, bottom, down };
2361
+ return { left, top, right, bottom, across, down };
1966
2362
  }
1967
2363
  /**
1968
- * How much the page scales an element down the screen: 1 unless an ancestor transforms it. `box` is
1969
- * 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.
1970
2366
  */
1971
- function scaleOf(element, box) {
1972
- const tall = element instanceof HTMLElement ? element.offsetHeight : 0;
1973
- return tall > 0 ? (box ?? element.getBoundingClientRect()).height / tall : 1;
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;
1974
2373
  }
1975
2374
  /**
1976
2375
  * A path around an outline: a closed curve through the midpoints of its edges, each point of the