@botiverse/porthole 0.14.9 → 0.14.11

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.
@@ -51,13 +51,21 @@ export const MARKER_HIT = String.raw`(count, boxAt, x, y, touch) => {
51
51
  * was, its text anywhere, its words with any spacing) and drawn over it, in a layer of the
52
52
  * shell's own (a closed shadow root), again whenever the content changes; what can't be found
53
53
  * is reported `orphaned`;
54
- * - markers (P104): a mark with a count gets a small bubble with the number beside its anchor's
55
- * last line (P105: in the line's gutter); a click on it is reported (`activated`), and the
56
- * content's own handlers never see it; a draft (P107) is drawn apart, a dashed line and a hollow
57
- * bubble with no number, and has a marker with or without a count;
54
+ * - markers (P104): a mark with a count gets a small button with the number, a bubble: a text
55
+ * anchor's at its paragraph's end (P133), a draft's (P107) beside its anchor's last line (P105:
56
+ * in the line's gutter), an element's on its corner; a click on it, or Enter or Space with the
57
+ * focus on it (P133), is reported (`activated`), and the content's own handlers never see it; a
58
+ * draft is drawn apart, a dashed line and a hollow bubble with no number, and has a marker with
59
+ * or without a count;
58
60
  * - following: while a mark has a marker, once a scroll, a resize or a change of the content
59
61
  * settles, the marks that moved since the host was last told are reported, at most once a
60
- * second, so the host's own UI beside a mark can follow.
62
+ * second, so the host's own UI beside a mark can follow;
63
+ * - places in the document (P132): each result's box also in the document's coordinates
64
+ * (`documentRect`), with how far the document was scrolled as it was read (`scroll`), and the
65
+ * followed mark's (`tracked`) too, so a host places marks scrolled out of the frame, and keeps
66
+ * them placed as the document scrolls, from one report and the followed mark's scroll;
67
+ * - the last line (P133): where a selection or a mark's words end, in the document's coordinates
68
+ * too (`lastLineDocumentRect`), so a host places its UI after them.
61
69
  *
62
70
  * It shares the realm with the content, which can interfere with all of it: the host takes its
63
71
  * reports as UI hints only.
@@ -306,6 +314,32 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
306
314
  return null;
307
315
  };
308
316
  const rectOf = (found) => (found.range ? rangeRect.call(found.range) : elementRect.call(found.element));
317
+ // How far the document is scrolled (P132): the top left of its root's box from the
318
+ // viewport's, negated, read with the DOM's own getBoundingClientRect (taken before content
319
+ // runs), never window.scrollY, which the content can replace. 0 - n: no -0 for a top at 0.
320
+ const scrollOf = () => {
321
+ const root = document.documentElement;
322
+ if (!root) {
323
+ return { x: 0, y: 0 };
324
+ }
325
+ const rect = elementRect.call(root);
326
+ return { x: 0 - rect.left, y: 0 - rect.top };
327
+ };
328
+ // A box in the viewport, in the document (P132): where it is as the document scrolls.
329
+ const inDocument = (rect, scroll) => ({ x: rect.x + scroll.x, y: rect.y + scroll.y, width: rect.width, height: rect.height });
330
+ // Where what was found ends (P133), in the document as a documentRect is: its last line with
331
+ // words on it, or an element's whole box (it has no lines of its own). Null with nothing drawn.
332
+ const lastLineOf = (found, scroll) => {
333
+ const rect = found.range ? lastBox(rangeRects.call(found.range)) : elementRect.call(found.element);
334
+ return rect === null ? null : inDocument(box(rect), scroll);
335
+ };
336
+ // What a selection's report adds to its box (P133): its last line, with the scroll it was
337
+ // read at; neither where nothing of it is drawn.
338
+ const placeOf = (found) => {
339
+ const scroll = scrollOf();
340
+ const lastLineDocumentRect = lastLineOf(found, scroll);
341
+ return lastLineDocumentRect === null ? {} : { lastLineDocumentRect, scroll };
342
+ };
309
343
 
310
344
  // The shell's own layer, over the content: marks, the outline of what would be picked.
311
345
  let layer = null;
@@ -320,18 +354,22 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
320
354
  // marker hollow, a light bubble with a dashed edge and the glyph's outline, no number.
321
355
  // While the last pointer was a finger or a pen (P119), each marker takes the pointer in a
322
356
  // box of at least 44 by 44 px centred on its own (an invisible ::before; 100% is its padding
323
- // box, its border 1 px), so a tap a few px off still lands on the shell's layer.
324
- const STYLE = ".mark{position:absolute;border-radius:2px;background:rgba(250,204,21,.18);box-shadow:inset 0 -2px 0 rgba(234,179,8,.95)}.mark.element{background:rgba(250,204,21,.07);box-shadow:none;outline:2px solid rgba(234,179,8,.9);outline-offset:1px}.mark.active{background:rgba(250,204,21,.26);box-shadow:inset 0 -3px 0 rgb(234,179,8),0 0 0 2px rgba(234,179,8,.45)}.mark.element.active{background:rgba(250,204,21,.12);box-shadow:none;outline-width:3px}.mark.element.quiet{background:rgba(250,204,21,.05);outline:1px solid rgba(234,179,8,.6);outline-offset:2px}.mark.element.quiet.active{background:rgba(250,204,21,.1);outline:2px solid rgba(234,179,8,.9)}.mark.draft{box-sizing:border-box;background:rgba(250,204,21,.08);box-shadow:none;border-bottom:2px dashed rgba(202,138,4,.95)}.mark.draft.active{background:rgba(250,204,21,.16);box-shadow:0 0 0 2px rgba(234,179,8,.45);border-bottom-width:3px}.mark.element.draft{background:rgba(250,204,21,.04);border-bottom:0;outline-style:dashed}.mark.element.draft.active{background:rgba(250,204,21,.08);box-shadow:none;outline-style:dashed}.marker{position:absolute;box-sizing:border-box;height:18px;min-width:18px;display:flex;align-items:center;justify-content:center;gap:3px;margin:0;padding:0 5px;border:1px solid rgba(161,98,7,.6);border-radius:9px 9px 9px 2px;background:#fde68a;color:#422006;box-shadow:0 1px 2px rgba(0,0,0,.3);font:600 11px/16px system-ui,-apple-system,Segoe UI,Roboto,sans-serif;font-style:normal;letter-spacing:0;word-spacing:0;text-transform:none;text-indent:0;text-shadow:none;direction:ltr;writing-mode:horizontal-tb;white-space:nowrap;user-select:none;-webkit-user-select:none;cursor:pointer;pointer-events:auto}.marker:hover{background:#fcd34d}.marker.active{background:#facc15;box-shadow:0 0 0 2px rgba(234,179,8,.55),0 1px 2px rgba(0,0,0,.3)}.marker.compact{padding:0 4px}.marker.draft{border:1px dashed rgb(202,138,4);background:#fffdf5;color:#a16207;box-shadow:0 1px 2px rgba(0,0,0,.2)}.marker.draft:hover{background:#fef9e7}.marker.draft.active{background:#fef3c7;box-shadow:0 0 0 2px rgba(234,179,8,.55),0 1px 2px rgba(0,0,0,.2)}.marker.inert{pointer-events:none}.touch>.marker::before{content:'';position:absolute;left:50%;top:50%;width:max(100% + 2px,44px);height:max(100% + 2px,44px);transform:translate(-50%,-50%)}.marker svg{display:block;width:11px;height:11px;flex:none}.mark.flash{animation:flash 1.8s ease-out}@keyframes flash{from{box-shadow:inset 0 -3px 0 rgb(234,179,8),0 0 0 6px rgba(250,204,21,.6)}}.hover{position:absolute;outline:2px solid #2563eb;outline-offset:1px;background:rgba(37,99,235,.12)}.tag{position:absolute;max-width:70vw;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font:12px/18px ui-monospace,SFMono-Regular,Menlo,monospace;padding:0 6px;border-radius:4px;background:#1e293b;color:#fff}";
357
+ // box, its border 1 px), so a tap a few px off still lands on the shell's layer. A marker is
358
+ // a real button (P133): what a browser gives one is taken off, a ring says it has the focus.
359
+ const STYLE = ".mark{position:absolute;border-radius:2px;background:rgba(250,204,21,.18);box-shadow:inset 0 -2px 0 rgba(234,179,8,.95)}.mark.element{background:rgba(250,204,21,.07);box-shadow:none;outline:2px solid rgba(234,179,8,.9);outline-offset:1px}.mark.active{background:rgba(250,204,21,.26);box-shadow:inset 0 -3px 0 rgb(234,179,8),0 0 0 2px rgba(234,179,8,.45)}.mark.element.active{background:rgba(250,204,21,.12);box-shadow:none;outline-width:3px}.mark.element.quiet{background:rgba(250,204,21,.05);outline:1px solid rgba(234,179,8,.6);outline-offset:2px}.mark.element.quiet.active{background:rgba(250,204,21,.1);outline:2px solid rgba(234,179,8,.9)}.mark.draft{box-sizing:border-box;background:rgba(250,204,21,.08);box-shadow:none;border-bottom:2px dashed rgba(202,138,4,.95)}.mark.draft.active{background:rgba(250,204,21,.16);box-shadow:0 0 0 2px rgba(234,179,8,.45);border-bottom-width:3px}.mark.element.draft{background:rgba(250,204,21,.04);border-bottom:0;outline-style:dashed}.mark.element.draft.active{background:rgba(250,204,21,.08);box-shadow:none;outline-style:dashed}.marker{position:absolute;box-sizing:border-box;height:18px;min-width:18px;display:flex;align-items:center;justify-content:center;gap:3px;margin:0;padding:0 5px;border:1px solid rgba(161,98,7,.6);border-radius:9px 9px 9px 2px;background:#fde68a;color:#422006;box-shadow:0 1px 2px rgba(0,0,0,.3);font:600 11px/16px system-ui,-apple-system,Segoe UI,Roboto,sans-serif;font-style:normal;letter-spacing:0;word-spacing:0;text-transform:none;text-indent:0;text-shadow:none;direction:ltr;writing-mode:horizontal-tb;white-space:nowrap;user-select:none;-webkit-user-select:none;cursor:pointer;pointer-events:auto;appearance:none;-webkit-appearance:none}.marker:focus-visible{outline:2px solid #2563eb;outline-offset:1px}.marker:hover{background:#fcd34d}.marker.active{background:#facc15;box-shadow:0 0 0 2px rgba(234,179,8,.55),0 1px 2px rgba(0,0,0,.3)}.marker.compact{padding:0 4px}.marker.draft{border:1px dashed rgb(202,138,4);background:#fffdf5;color:#a16207;box-shadow:0 1px 2px rgba(0,0,0,.2)}.marker.draft:hover{background:#fef9e7}.marker.draft.active{background:#fef3c7;box-shadow:0 0 0 2px rgba(234,179,8,.55),0 1px 2px rgba(0,0,0,.2)}.marker.inert,[data-placing]>.marker{pointer-events:none}.touch>.marker::before{content:'';position:absolute;left:50%;top:50%;width:max(100% + 2px,44px);height:max(100% + 2px,44px);transform:translate(-50%,-50%)}.marker svg{display:block;width:11px;height:11px;flex:none}.mark.flash{animation:flash 1.8s ease-out}@keyframes flash{from{box-shadow:inset 0 -3px 0 rgb(234,179,8),0 0 0 6px rgba(250,204,21,.6)}}.hover{position:absolute;outline:2px solid #2563eb;outline-offset:1px;background:rgba(37,99,235,.12)}.tag{position:absolute;max-width:70vw;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font:12px/18px ui-monospace,SFMono-Regular,Menlo,monospace;padding:0 6px;border-radius:4px;background:#1e293b;color:#fff}";
325
360
  // Inline and important: the content's own style sheets can't hide, shrink or move it by
326
361
  // accident. Set again at each draw, since its scripts can rewrite it (narrowing only: the
327
- // content can still cover the layer, or move it with its ancestors, P104).
328
- const LAYER_STYLE = "position:fixed!important;inset:0!important;width:auto!important;height:auto!important;min-width:0!important;min-height:0!important;max-width:none!important;max-height:none!important;display:block!important;visibility:visible!important;opacity:1!important;pointer-events:none!important;z-index:2147483647!important;overflow:hidden!important;margin:0!important;padding:0!important;border:0!important;background:none!important;transform:none!important;scale:none!important;translate:none!important;rotate:none!important;zoom:1!important;clip-path:none!important;mask:none!important;filter:none!important;content-visibility:visible!important;contain:none!important";
362
+ // content can still cover the layer, or move it with its ancestors, P104). Clipped, where an
363
+ // engine has it, not hidden: hidden is a box a focused marker out of view would scroll (P133).
364
+ const LAYER_STYLE = "position:fixed!important;inset:0!important;width:auto!important;height:auto!important;min-width:0!important;min-height:0!important;max-width:none!important;max-height:none!important;display:block!important;visibility:visible!important;opacity:1!important;pointer-events:none!important;z-index:2147483647!important;overflow:hidden!important;overflow:clip!important;margin:0!important;padding:0!important;border:0!important;background:none!important;transform:none!important;scale:none!important;translate:none!important;rotate:none!important;zoom:1!important;clip-path:none!important;mask:none!important;filter:none!important;content-visibility:visible!important;contain:none!important";
329
365
  const ensureLayer = () => {
330
366
  if (layer === null) {
331
367
  layer = createElement.call(document, "div");
332
- layer.setAttribute("aria-hidden", "true");
333
368
  layer.setAttribute("data-porthole-annotations", "");
334
369
  shadow = attachShadow.call(layer, { mode: "closed" });
370
+ // Within the shadow root: a focus moving from one marker to the next never leaves it.
371
+ callOf(listenOn, shadow, ["focusin", onMarkerFocus]);
372
+ callOf(listenOn, shadow, ["focusout", onMarkerFocus]);
335
373
  }
336
374
  if (layer.style.cssText !== LAYER_STYLE) {
337
375
  layer.style.cssText = LAYER_STYLE;
@@ -363,6 +401,13 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
363
401
  const markerWidth = (label) => Math.max(MARKER_HEIGHT, 23 + (label === "" ? 0 : 3 + label.length * 7));
364
402
  // Compact (P105): the number alone, or the glyph alone for none, with less padding.
365
403
  const compactWidth = (label) => (label === "" ? 21 : Math.max(MARKER_HEIGHT, 10 + label.length * 7));
404
+ // What a marker is called (P133), to whoever can't see it: what it is, and the words it is
405
+ // on. In English, whatever the page's language: the shell has no words of the host's.
406
+ const nameOf = (mark) => {
407
+ const words = typeof mark.anchor.label === "string" ? squash(mark.anchor.label).slice(0, LIMIT.label) : "";
408
+ const what = mark.draft ? "Draft comment" : mark.count === 1 ? "1 comment" : mark.count > 1 ? mark.count + " comments" : "Comment";
409
+ return words === "" ? what : what + " on " + words;
410
+ };
366
411
  // A rect's last line with words on it: its last box with a width, else any with a size.
367
412
  const lastBox = (rects) => {
368
413
  let any = null;
@@ -386,7 +431,9 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
386
431
  // in a row of them), just past the line's last word. Markers on one line of one block go
387
432
  // together, in the order their words end. Where the bubble with its glyph fits in none of
388
433
  // these, the number alone does (compact); where that fits in none either (a full line on a
389
- // narrow screen), it goes as far right as it can, over the line's end.
434
+ // narrow screen), it goes as far right as it can, over the line's end. A posted comment's
435
+ // marker (P133) is placed the same way on its paragraph's last line instead, where nothing
436
+ // follows its words, and first tries just past that line's last word.
390
437
  const computedOf = window.getComputedStyle;
391
438
  const elementsAt = Document.prototype.elementsFromPoint;
392
439
  const containsOf = Node.prototype.contains;
@@ -446,6 +493,52 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
446
493
  }
447
494
  return end;
448
495
  };
496
+ // The end of the paragraph an anchor ends in (P133): a range over the block's last words
497
+ // that are drawn, from the anchor's end on, as far as a block inside it (a nested list, say:
498
+ // its words are their own paragraph), 256 text nodes at most. Null if none follow the anchor.
499
+ const inBlock = (node, block) => {
500
+ for (let at = node.parentElement; at !== null && at !== block; at = at.parentElement) {
501
+ if (!INLINE.test(computedOf.call(window, at).display)) {
502
+ return false;
503
+ }
504
+ }
505
+ return true;
506
+ };
507
+ const paragraphEnd = (range, block) => {
508
+ const start = range.endContainer;
509
+ if (start.nodeType !== 3) {
510
+ return null;
511
+ }
512
+ const walker = createTreeWalker.call(document, block, NodeFilter.SHOW_TEXT);
513
+ walker.currentNode = start;
514
+ let end = null;
515
+ let node = start;
516
+ for (let pieces = 0; node !== null && pieces < 256; pieces += 1) {
517
+ if (node !== start && !inBlock(node, block)) {
518
+ break;
519
+ }
520
+ const words = node.nodeValue;
521
+ const first = node === start ? range.endOffset : 0;
522
+ let to = words.length;
523
+ while (to > first && /\s/.test(words.charAt(to - 1))) {
524
+ to -= 1;
525
+ }
526
+ if (to > first) {
527
+ const piece = createRange.call(document);
528
+ try {
529
+ piece.setStart(node, Math.max(first, to - 2));
530
+ piece.setEnd(node, to);
531
+ } catch (_) {
532
+ return end;
533
+ }
534
+ if (lastBox(rangeRects.call(piece)) !== null) {
535
+ end = piece;
536
+ }
537
+ }
538
+ node = walker.nextNode();
539
+ }
540
+ return end;
541
+ };
449
542
  // Nothing of the content's at a point but a box that holds the block (its background); a
450
543
  // point out of view hides nothing.
451
544
  const clearAt = (x, y, block) => {
@@ -498,7 +591,11 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
498
591
  return null;
499
592
  };
500
593
  const gutter = right + gap;
501
- let left = fits(gutter, false);
594
+ // A paragraph's end (P133): just past its last word, where the line leaves the room.
595
+ let left = line.ending ? fits(line.words + gap, false) : null;
596
+ if (left === null) {
597
+ left = fits(gutter, false);
598
+ }
502
599
  if (left === null && gutter > most) {
503
600
  left = fits(most, true);
504
601
  }
@@ -510,7 +607,8 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
510
607
  const placeLines = (wanted, placed, room) => {
511
608
  // One line of one block: markers whose last lines share a middle.
512
609
  const lines = [];
513
- wanted.sort((a, b) => a.at.top - b.at.top || a.at.right - b.at.right);
610
+ // In the order their words end, those at one paragraph's end too.
611
+ wanted.sort((a, b) => a.at.top - b.at.top || a.at.right - b.at.right || a.words.top - b.words.top || a.words.right - b.words.right);
514
612
  for (const each of wanted) {
515
613
  const middle = each.at.top + each.at.height / 2;
516
614
  let line = null;
@@ -520,10 +618,11 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
520
618
  }
521
619
  }
522
620
  if (line === null) {
523
- line = { block: each.block, middle, height: each.at.height, bottom: each.at.bottom, range: each.range, at: each.at, items: [] };
621
+ line = { block: each.block, middle, height: each.at.height, bottom: each.at.bottom, range: each.range, at: each.at, ending: false, items: [] };
524
622
  lines.push(line);
525
623
  }
526
624
  line.items.push(each);
625
+ line.ending = line.ending || each.ending;
527
626
  if (each.at.right > line.at.right) {
528
627
  line.at = each.at;
529
628
  line.range = each.range;
@@ -602,20 +701,24 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
602
701
  const kept = markerNodes.get(mark.id);
603
702
  let marker = kept ? kept.element : null;
604
703
  if (marker === null) {
605
- marker = createElement.call(document, "div");
606
- // A listener of its own makes it a thing to tap: WebKit makes a tap a click only on such
607
- // an element (P105, measured: the window's listener isn't enough). It never runs (the
608
- // window's first listener stops every click on the layer); the click is told there.
609
- callOf(listenOn, marker, ["click", tapTarget]);
610
- }
704
+ // A real button (P133): the keyboard reaches it, Enter and Space click it, assistive
705
+ // technology names it, and WebKit makes a tap a click on it (P105).
706
+ marker = createElement.call(document, "button");
707
+ marker.setAttribute("type", "button");
708
+ }
709
+ // Stepped aside while elements are picked, from the keyboard too. Set, not left to the
710
+ // button: Safari's Tab skips a button without one, unless the person asked otherwise.
711
+ marker.tabIndex = mode.elements ? -1 : 0;
611
712
  marker.className = "marker" + (mark.draft ? " draft" : "") + (each.compact ? " compact" : "") + (mark.id === active ? " active" : "") + (mode.elements ? " inert" : "");
612
713
  marker.style.cssText = "left:" + each.left + "px;top:" + each.top + "px";
613
714
  markers.push({ id: mark.id, element: marker });
614
- const form = (mark.draft ? "draft:" : "") + (each.compact ? "compact:" : "full:") + label;
715
+ const name = nameOf(mark);
716
+ const form = (mark.draft ? "draft:" : "") + (each.compact ? "compact:" : "full:") + label + ":" + name;
615
717
  if (kept && kept.form === form) {
616
718
  return marker;
617
719
  }
618
720
  markerNodes.set(mark.id, { element: marker, form });
721
+ marker.setAttribute("aria-label", name);
619
722
  marker.replaceChildren();
620
723
  if (!each.compact || label === "") {
621
724
  const glyph = createElementNS.call(document, SVG, "svg");
@@ -642,10 +745,9 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
642
745
  }
643
746
  return marker;
644
747
  };
645
- const tapTarget = () => {};
646
748
  let active = null;
647
- // The mark whose marker the pointer is over: its words are lit as the active mark's are, so
648
- // a marker in the gutter shows which words it is for (P105).
749
+ // The mark whose marker the pointer is over, or the focus is on (P133): its words are lit as
750
+ // the active mark's are, so a marker in the gutter shows which words it is for (P105).
649
751
  let hot = null;
650
752
  let flashing = null;
651
753
  let flashUntil = 0;
@@ -661,6 +763,10 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
661
763
  style.nonce = nonce;
662
764
  }
663
765
  boxes = { style, marks: createElement.call(document, "div"), markers: createElement.call(document, "div"), hover: createElement.call(document, "div") };
766
+ // Only the markers are for assistive technology (P133): the rest draws what the page
767
+ // already says.
768
+ boxes.marks.setAttribute("aria-hidden", "true");
769
+ boxes.hover.setAttribute("aria-hidden", "true");
664
770
  root.replaceChildren(boxes.style, boxes.marks, boxes.markers, boxes.hover);
665
771
  }
666
772
  if (boxes.style.textContent !== STYLE) {
@@ -682,6 +788,7 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
682
788
  // What is measured first, then drawn: no node of the layer changes while the page is read.
683
789
  const corners = [];
684
790
  const wanted = [];
791
+ const focused = focusedMarker() !== null;
685
792
  markers = [];
686
793
  for (const mark of marks) {
687
794
  if (mark.found === null) {
@@ -702,14 +809,28 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
702
809
  if (at !== null) {
703
810
  // A draft's marker has no number (P107).
704
811
  const label = mark.draft ? "" : labelOf(mark.count);
705
- const each = { mark, order: corners.length + wanted.length, label, width: markerWidth(label), small: compactWidth(label), compact: false, at, left: 0, top: 0 };
812
+ const each = { mark, label, width: markerWidth(label), small: compactWidth(label), compact: false, at, words: at, left: 0, top: 0, ending: false };
706
813
  if (mark.found.range) {
707
814
  // Kept while it still holds the words (P105): a change elsewhere doesn't move them.
708
815
  if (mark.block === undefined || !mark.block.isConnected || !containsOf.call(mark.block, mark.found.range.endContainer)) {
709
816
  mark.block = blockOf(mark.found.range);
817
+ mark.end = undefined;
710
818
  }
711
819
  each.range = mark.found.range;
712
820
  each.block = mark.block;
821
+ // A posted comment's, at its paragraph's end (P133); read once for what was found,
822
+ // since a block's words can be long. A draft's stays by the words being written on.
823
+ if (!mark.draft) {
824
+ each.ending = true;
825
+ if (mark.end === undefined) {
826
+ mark.end = paragraphEnd(mark.found.range, mark.block);
827
+ }
828
+ const last = mark.end === null ? null : lastBox(rangeRects.call(mark.end));
829
+ if (last !== null) {
830
+ each.at = last;
831
+ each.range = mark.end;
832
+ }
833
+ }
713
834
  wanted.push(each);
714
835
  } else {
715
836
  corners.push(each);
@@ -725,18 +846,25 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
725
846
  }
726
847
  // The layer's own boxes out of the way while places are hit-tested (P105): each test
727
848
  // otherwise walks every mark and marker the last draw left (0.13 ms each, 300 of them).
849
+ // A marker with the focus would lose it out of the display (P133): while the person is on
850
+ // one, the markers only stop taking the pointer, which the tests skip but still walk.
728
851
  if (wanted.length > 0) {
729
852
  into.marks.style.display = "none";
730
- into.markers.style.display = "none";
853
+ if (focused) {
854
+ into.markers.setAttribute("data-placing", "");
855
+ } else {
856
+ into.markers.style.display = "none";
857
+ }
731
858
  try {
732
859
  placeLines(wanted, placed, room);
733
860
  } finally {
734
861
  into.marks.style.display = "";
735
862
  into.markers.style.display = "";
863
+ into.markers.removeAttribute("data-placing");
736
864
  }
737
865
  }
738
866
  const badges = [];
739
- for (const each of corners.concat(wanted).sort((a, b) => a.order - b.order)) {
867
+ for (const each of corners.concat(wanted).sort((a, b) => a.mark.reading - b.mark.reading)) {
740
868
  badges.push(markerOf(each));
741
869
  }
742
870
  for (const id of Array.from(markerNodes.keys())) {
@@ -762,11 +890,14 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
762
890
  into.markers.removeChild(child);
763
891
  }
764
892
  }
765
- for (const badge of badges) {
766
- if (badge.parentNode !== into.markers) {
767
- into.markers.appendChild(badge);
893
+ // In reading order, the Tab key's (P133). A marker that stays in its place isn't touched
894
+ // (a press spans a redraw, above), and none moves while one has the focus: it would lose it.
895
+ badges.forEach((badge, at) => {
896
+ const there = into.markers.childNodes[at] || null;
897
+ if (badge.parentNode !== into.markers || (!focused && there !== badge)) {
898
+ into.markers.insertBefore(badge, focused ? null : there);
768
899
  }
769
- }
900
+ });
770
901
  into.hover.replaceChildren(...outlines);
771
902
  };
772
903
  let drawing = false;
@@ -799,7 +930,11 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
799
930
  if (apart(trackedBox, rect) && now - trackedAt >= 16) {
800
931
  trackedBox = rect;
801
932
  trackedAt = now;
802
- tell(M.tracked, { id: tracking, rect });
933
+ // Its box moves as the document scrolls, inside the frame or not: each one tells the
934
+ // host the scroll, for every mark it places by its place in the document (P132).
935
+ const scroll = scrollOf();
936
+ const lastLineDocumentRect = lastLineOf(mark.found, scroll);
937
+ tell(M.tracked, Object.assign({ id: tracking, rect, documentRect: inDocument(rect, scroll), scroll }, lastLineDocumentRect === null ? {} : { lastLineDocumentRect }));
803
938
  }
804
939
  }
805
940
  trackFrame = nextFrame(trackTick);
@@ -843,12 +978,16 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
843
978
  placed.forEach((each, at) => orders.set(each.mark, at));
844
979
  return orders;
845
980
  };
846
- const resultOf = (mark, orders) => {
981
+ // A mark's result: its box in the viewport, and in the document (P132), its last line too
982
+ // (P133), by scroll, the report's one reading of it.
983
+ const resultOf = (mark, orders, scroll) => {
847
984
  if (mark.found === null) {
848
985
  return { id: mark.id, state: "orphaned" };
849
986
  }
850
987
  const order = (orders || ordersOf()).get(mark);
851
- return Object.assign({ id: mark.id, state: "attached", rect: box(rectOf(mark.found)) }, order === undefined ? {} : { order });
988
+ const rect = box(rectOf(mark.found));
989
+ const lastLineDocumentRect = lastLineOf(mark.found, scroll);
990
+ return Object.assign({ id: mark.id, state: "attached", rect, documentRect: inDocument(rect, scroll) }, lastLineDocumentRect === null ? {} : { lastLineDocumentRect }, order === undefined ? {} : { order });
852
991
  };
853
992
  // Two boxes more than 0.5 px apart on any side (or one of them unknown).
854
993
  const apart = (a, b) => !a || !b || Math.abs(a.x - b.x) > 0.5 || Math.abs(a.y - b.y) > 0.5 || Math.abs(a.width - b.width) > 0.5 || Math.abs(a.height - b.height) > 0.5;
@@ -860,17 +999,18 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
860
999
  }
861
1000
  return result.state === "attached" && (apart(told.rect, result.rect) || told.order !== result.order);
862
1001
  };
863
- const report = (pairs) => {
1002
+ // With the scroll the results' document boxes were read at (P132).
1003
+ const report = (pairs, scroll) => {
864
1004
  if (pairs.length > 0) {
865
1005
  for (const pair of pairs) {
866
1006
  pair.mark.told = pair.result;
867
1007
  }
868
- tell(M.anchored, { results: pairs.map((pair) => pair.result) });
1008
+ tell(M.anchored, { results: pairs.map((pair) => pair.result), scroll });
869
1009
  }
870
1010
  };
871
- const resultsOf = (only) => {
1011
+ const resultsOf = (only, scroll) => {
872
1012
  const orders = ordersOf();
873
- return marks.map((mark) => ({ mark, result: resultOf(mark, orders) })).filter((pair) => !only || moved(pair.mark, pair.result));
1013
+ return marks.map((mark) => ({ mark, result: resultOf(mark, orders, scroll) })).filter((pair) => !only || moved(pair.mark, pair.result));
874
1014
  };
875
1015
  // Finds every mark again; tells the host every result (after show, or when a state changed),
876
1016
  // only the marks that moved (once the content is quiet), or nothing.
@@ -889,10 +1029,18 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
889
1029
  changed = true;
890
1030
  }
891
1031
  mark.found = found;
1032
+ mark.end = undefined;
892
1033
  mark.state = state;
893
1034
  }
1035
+ // Where each starts, once for each finding: the markers' reading order (P133).
1036
+ const orders = ordersOf();
1037
+ for (const mark of marks) {
1038
+ const reading = orders.get(mark);
1039
+ mark.reading = reading === undefined ? marks.length : reading;
1040
+ }
894
1041
  if (all || changed) {
895
- report(resultsOf(false));
1042
+ const scroll = scrollOf();
1043
+ report(resultsOf(false, scroll), scroll);
896
1044
  } else if (quiet) {
897
1045
  follow();
898
1046
  }
@@ -977,9 +1125,10 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
977
1125
  const sendFollow = () => {
978
1126
  let sent = 0;
979
1127
  if (marks.length > 0 && !settling() && counted()) {
980
- const pairs = resultsOf(true);
1128
+ const scroll = scrollOf();
1129
+ const pairs = resultsOf(true, scroll);
981
1130
  if (pairs.length > 0) {
982
- report(pairs);
1131
+ report(pairs, scroll);
983
1132
  sent += 1;
984
1133
  }
985
1134
  }
@@ -1043,14 +1192,16 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
1043
1192
  setTimeout(redraw, 1850);
1044
1193
  }
1045
1194
  draw();
1046
- report([{ mark, result: resultOf(mark) }]);
1195
+ const scroll = scrollOf();
1196
+ report([{ mark, result: resultOf(mark, null, scroll) }], scroll);
1047
1197
  };
1048
1198
 
1049
1199
  // A marker's click (P104). The layer takes no pointer but on its markers, so a press whose
1050
1200
  // target is the layer is on a marker: the content's handlers never see it (this listener
1051
- // runs before any of theirs), nor does what lies under it, and a click is reported. No tab
1052
- // stop, by choice: it would join the page's focus order, which the content controls, and the
1053
- // layer is hidden from assistive technology; the host's own list of marks is the keyboard way.
1201
+ // runs before any of theirs), nor does what lies under it, and a click is reported. A press
1202
+ // of the mouse or a finger moves no focus; the keyboard reaches a marker as any button
1203
+ // (P133, which reverses P104's no tab stop): it joins the page's focus order, which the
1204
+ // content controls, so the host's own list of marks stays the keyboard way it can rely on.
1054
1205
  //
1055
1206
  // A press is read and stopped through the platform's own functions and getters, kept now
1056
1207
  // (P105), as the navigation guard reads its events (P099): content that later patches
@@ -1063,6 +1214,11 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
1063
1214
  const targetOf = getterOf(Event.prototype, "target");
1064
1215
  const typeOf = getterOf(Event.prototype, "type");
1065
1216
  const buttonOf = getterOf(MouseEvent.prototype, "button");
1217
+ const detailOf = getterOf(UIEvent.prototype, "detail");
1218
+ const keyOf = getterOf(KeyboardEvent.prototype, "key");
1219
+ const repeatOf = getterOf(KeyboardEvent.prototype, "repeat");
1220
+ const activeOf = getterOf(ShadowRoot.prototype, "activeElement");
1221
+ const scrollWindow = window.scrollBy;
1066
1222
  const clientXOf = getterOf(MouseEvent.prototype, "clientX");
1067
1223
  const clientYOf = getterOf(MouseEvent.prototype, "clientY");
1068
1224
  const leftOf = getterOf(DOMRectReadOnly.prototype, "left");
@@ -1079,6 +1235,16 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
1079
1235
  const at = markerHit(markers.length, (i) => markerBox(markers[i].element), x, y, touch);
1080
1236
  return at < 0 ? null : markers[at];
1081
1237
  };
1238
+ const markerOfNode = (element) => {
1239
+ for (let i = 0; i < markers.length; i += 1) {
1240
+ if (markers[i].element === element) {
1241
+ return markers[i];
1242
+ }
1243
+ }
1244
+ return null;
1245
+ };
1246
+ // The marker with the focus (P133), or null.
1247
+ const focusedMarker = () => (shadow === null || activeOf === null ? null : markerOfNode(callOf(activeOf, shadow, [])));
1082
1248
  const isShown = (id) => {
1083
1249
  for (let i = 0; i < marks.length; i += 1) {
1084
1250
  if (marks[i].id === id) {
@@ -1108,12 +1274,57 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
1108
1274
  }
1109
1275
  // isTrusted is the event's own, unforgeable.
1110
1276
  if (type === "click" && event.isTrusted && callOf(buttonOf, event, []) === 0) {
1277
+ // From the keyboard or assistive technology (P133) a click has no count and no place:
1278
+ // it is the focused marker's. Not while elements are picked: the markers step aside.
1279
+ const keyed = callOf(detailOf, event, []) === 0 ? focusedMarker() : null;
1280
+ if (keyed !== null) {
1281
+ if (!mode.elements && isShown(keyed.id)) {
1282
+ tell(M.activated, { id: keyed.id, rect: markerBox(keyed.element) });
1283
+ }
1284
+ return;
1285
+ }
1111
1286
  const hit = markerAt(callOf(clientXOf, event, []), callOf(clientYOf, event, []));
1112
1287
  if (hit !== null && isShown(hit.id)) {
1113
1288
  tell(M.activated, { id: hit.id, rect: markerBox(hit.element) });
1114
1289
  }
1115
1290
  }
1116
1291
  };
1292
+ // The keys that click a focused marker (P133), each stopped as a press is, so the content
1293
+ // neither hears them nor cancels the click they make; it keeps every other key, Tab and
1294
+ // Escape too, as it would on a button of its own, and the host's find shortcut stays. A key
1295
+ // held down clicks once: each repeat would spend the bridge's rate on one press.
1296
+ const onMarkerKey = (event) => {
1297
+ if (layer === null || callOf(targetOf, event, []) !== layer) {
1298
+ return;
1299
+ }
1300
+ const key = callOf(keyOf, event, []);
1301
+ if (key === "Enter" || key === " ") {
1302
+ callOf(stopPress, event, []);
1303
+ if (callOf(repeatOf, event, [])) {
1304
+ callOf(cancelPress, event, []);
1305
+ }
1306
+ }
1307
+ };
1308
+ for (const type of ["keydown", "keypress", "keyup"]) {
1309
+ window.addEventListener(type, onMarkerKey, true);
1310
+ }
1311
+ // The focus on a marker (P133) lights its words, as the pointer does, and brings it into view:
1312
+ // a marker out of view is out of the layer's box, which never scrolls, so the document must.
1313
+ const onMarkerFocus = (event) => {
1314
+ const on = callOf(typeOf, event, []) === "focusin" ? markerOfNode(callOf(targetOf, event, [])) : null;
1315
+ const id = on === null ? null : on.id;
1316
+ if (id !== hot) {
1317
+ hot = id;
1318
+ redraw();
1319
+ }
1320
+ if (on !== null) {
1321
+ const rect = markerBox(on.element);
1322
+ const seen = document.documentElement.clientHeight || window.innerHeight;
1323
+ if (rect.y < 0 || rect.y + rect.height > seen) {
1324
+ callOf(scrollWindow, window, [0, rect.y - (seen - rect.height) / 2]);
1325
+ }
1326
+ }
1327
+ };
1117
1328
  // The pointer moving over a marker is stopped too. Narrowing only: the content still sees
1118
1329
  // the pointer leave its own element for the layer (relatedTarget), the layer's :hover and
1119
1330
  // :active, and the frame's user activation, so it can tell a marker was pressed, and
@@ -1233,7 +1444,7 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
1233
1444
  return;
1234
1445
  }
1235
1446
  swallowUntil = performance.now() + 2000;
1236
- tell(M.selected, { anchor, rect: box(elementRect.call(element)) });
1447
+ tell(M.selected, Object.assign({ anchor, rect: box(elementRect.call(element)) }, placeOf({ element })));
1237
1448
  }, HOLD_MS),
1238
1449
  };
1239
1450
  }, true);
@@ -1281,7 +1492,7 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
1281
1492
  hover = null;
1282
1493
  setCursor(false);
1283
1494
  draw();
1284
- tell(M.selected, { anchor, rect: box(elementRect.call(element)) });
1495
+ tell(M.selected, Object.assign({ anchor, rect: box(elementRect.call(element)) }, placeOf({ element })));
1285
1496
  }, true);
1286
1497
  window.addEventListener("keydown", (event) => {
1287
1498
  if (event.key !== "Escape") {
@@ -1340,7 +1551,7 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
1340
1551
  }
1341
1552
  reported = key;
1342
1553
  reportedBox = rect;
1343
- tell(M.selected, { anchor, rect });
1554
+ tell(M.selected, Object.assign({ anchor, rect }, placeOf({ range })));
1344
1555
  return true;
1345
1556
  };
1346
1557
  document.addEventListener("selectionchange", () => {
package/src/shell/find.ts CHANGED
@@ -140,7 +140,7 @@ export const FIND_LATEST = String.raw`(later, time, quiet, run, superseded) => {
140
140
  * scripts can, P104). The annotation runtime carries the same text (a test holds them equal).
141
141
  */
142
142
  export const LAYER_STYLE =
143
- "position:fixed!important;inset:0!important;width:auto!important;height:auto!important;min-width:0!important;min-height:0!important;max-width:none!important;max-height:none!important;display:block!important;visibility:visible!important;opacity:1!important;pointer-events:none!important;z-index:2147483647!important;overflow:hidden!important;margin:0!important;padding:0!important;border:0!important;background:none!important;transform:none!important;scale:none!important;translate:none!important;rotate:none!important;zoom:1!important;clip-path:none!important;mask:none!important;filter:none!important;content-visibility:visible!important;contain:none!important";
143
+ "position:fixed!important;inset:0!important;width:auto!important;height:auto!important;min-width:0!important;min-height:0!important;max-width:none!important;max-height:none!important;display:block!important;visibility:visible!important;opacity:1!important;pointer-events:none!important;z-index:2147483647!important;overflow:hidden!important;overflow:clip!important;margin:0!important;padding:0!important;border:0!important;background:none!important;transform:none!important;scale:none!important;translate:none!important;rotate:none!important;zoom:1!important;clip-path:none!important;mask:none!important;filter:none!important;content-visibility:visible!important;contain:none!important";
144
144
 
145
145
  /**
146
146
  * The shell's find in page (P131), spliced into the shell's runtime (`__FIND__`) when the config