@botiverse/porthole 0.9.0 → 0.12.0

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.
Files changed (65) hide show
  1. package/dist/annotate/types.d.ts +21 -7
  2. package/dist/annotate/validate.d.ts +18 -3
  3. package/dist/annotate/validate.js +29 -5
  4. package/dist/bridge/bridge.js +1 -51
  5. package/dist/bridge/unseen.d.ts +18 -0
  6. package/dist/bridge/unseen.js +71 -0
  7. package/dist/check/files.d.ts +2 -0
  8. package/dist/check/files.js +67 -50
  9. package/dist/check/lint.d.ts +8 -1
  10. package/dist/check/lint.js +18 -0
  11. package/dist/check/markup.d.ts +7 -0
  12. package/dist/check/markup.js +49 -0
  13. package/dist/check/page-source.js +1 -1
  14. package/dist/check/scroll.d.ts +100 -0
  15. package/dist/check/scroll.js +1429 -0
  16. package/dist/conformance/behavior.js +107 -68
  17. package/dist/conformance/clipboard.js +16 -14
  18. package/dist/conformance/harness-source.js +1 -1
  19. package/dist/conformance/limits.js +117 -64
  20. package/dist/conformance/probes.js +66 -49
  21. package/dist/conformance/runtime.js +6 -0
  22. package/dist/conformance/served.js +66 -63
  23. package/dist/guide/guide.js +1 -1
  24. package/dist/guide/runtime.js +2 -2
  25. package/dist/host/create-host.js +46 -4
  26. package/dist/index.d.ts +1 -1
  27. package/dist/platform/types.d.ts +4 -2
  28. package/dist/react/frame.d.ts +2 -1
  29. package/dist/runtime/requests.d.ts +5 -0
  30. package/dist/runtime/requests.js +16 -1
  31. package/dist/runtime/types.generated.js +1 -1
  32. package/dist/shell/annotate.d.ts +6 -3
  33. package/dist/shell/annotate.js +474 -104
  34. package/dist/shell/use.js +9 -1
  35. package/docs/ADOPTING.md +99 -1
  36. package/docs/ARCHITECTURE.md +2 -1
  37. package/docs/DECISIONS.md +764 -14
  38. package/docs/DESIGN.md +192 -42
  39. package/docs/THREAT_MODEL.md +109 -27
  40. package/package.json +1 -1
  41. package/src/annotate/types.ts +22 -7
  42. package/src/annotate/validate.ts +32 -6
  43. package/src/bridge/bridge.ts +1 -60
  44. package/src/bridge/unseen.ts +82 -0
  45. package/src/check/files.ts +68 -54
  46. package/src/check/lint.ts +24 -0
  47. package/src/check/markup.ts +53 -0
  48. package/src/check/scroll.ts +1757 -0
  49. package/src/conformance/behavior.ts +221 -69
  50. package/src/conformance/clipboard.ts +44 -14
  51. package/src/conformance/limits.ts +169 -76
  52. package/src/conformance/probes.ts +97 -49
  53. package/src/conformance/runtime.ts +9 -0
  54. package/src/conformance/served.ts +112 -63
  55. package/src/guide/guide.ts +1 -0
  56. package/src/guide/runtime.ts +2 -2
  57. package/src/host/create-host.ts +50 -4
  58. package/src/index.ts +1 -0
  59. package/src/platform/types.ts +4 -2
  60. package/src/react/frame.tsx +2 -1
  61. package/src/runtime/requests.ts +23 -1
  62. package/src/runtime/types.generated.ts +1 -1
  63. package/src/shell/annotate.ts +474 -104
  64. package/src/shell/use.ts +9 -1
  65. package/types/runtime/v1/comments.d.ts +14 -4
@@ -7,14 +7,17 @@
7
7
  * The host drives it (`porthole/annotate.start|stop|show|focus|clear`) and it reports
8
8
  * (`porthole/annotate.selected|unselected|cancelled|anchored|activated`):
9
9
  * - picking an element: the element under the pointer (or finger) is outlined with its name; a
10
- * click or tap picks it, and the content's own handlers never see that click; Escape cancels;
10
+ * click or tap picks it, and the content's own handlers never see that click (unless the content
11
+ * patched the DOM's built-ins, P105); Escape cancels;
11
12
  * - selecting text: each selection that settles is reported, with where it is, and its clearing;
12
13
  * - marks: each anchor is found in the content as it is now (its CSS selector, its text where it
13
14
  * was, its text anywhere, its words with any spacing) and drawn over it, in a layer of the
14
15
  * shell's own (a closed shadow root), again whenever the content changes; what can't be found
15
16
  * is reported `orphaned`;
16
- * - markers (P104): a mark with a count gets a small bubble with the number at its anchor's end;
17
- * a click on it is reported (`activated`), and the content's own handlers never see it;
17
+ * - markers (P104): a mark with a count gets a small bubble with the number beside its anchor's
18
+ * last line (P105: in the line's gutter); a click on it is reported (`activated`), and the
19
+ * content's own handlers never see it; a draft (P107) is drawn apart, a dashed line and a hollow
20
+ * bubble with no number, and has a marker with or without a count;
18
21
  * - following: while a mark has a marker, once a scroll, a resize or a change of the content
19
22
  * settles, the marks that moved since the host was last told are reported, at most once a
20
23
  * second, so the host's own UI beside a mark can follow.
@@ -50,9 +53,12 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
50
53
  const rangeRects = Range.prototype.getClientRects;
51
54
  const attachShadow = Element.prototype.attachShadow;
52
55
  const getAttribute = Element.prototype.getAttribute;
56
+ const listenOn = EventTarget.prototype.addEventListener;
53
57
  const post = window.parent.postMessage.bind(window.parent);
58
+ // Read once: content can replace window.parent (it is [Replaceable]), never what was read.
59
+ const framed = window.parent !== window;
54
60
  const tell = (method, params) => {
55
- if (window.parent !== window) {
61
+ if (framed) {
56
62
  post({ jsonrpc: "2.0", method, params }, "*");
57
63
  }
58
64
  };
@@ -359,8 +365,10 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
359
365
  // they are; the active one and a flash add a ring around it, not more fill over its words.
360
366
  // An element's mark with a marker is quieter (a thin line): the marker says what it is.
361
367
  // A marker is a small bubble with a number, drawn in its own colors and font (only what the
362
- // layer inherits from the page could reach it, so each of those is set here).
363
- 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)}.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.inert{pointer-events:none}.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}";
368
+ // layer inherits from the page could reach it, so each of those is set here). A draft
369
+ // (P107) is a comment not yet posted: its line is dashed (an element's outline too) and its
370
+ // marker hollow, a light bubble with a dashed edge and the glyph's outline, no number.
371
+ 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}.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}";
364
372
  // Inline and important: the content's own style sheets can't hide, shrink or move it by
365
373
  // accident. Set again at each draw, since its scripts can rewrite it (narrowing only: the
366
374
  // content can still cover the layer, or move it with its ancestors, P104).
@@ -390,55 +398,283 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
390
398
  const markerNodes = new Map();
391
399
  const MARKER_HEIGHT = 18;
392
400
  const SVG = "http://www.w3.org/2000/svg";
393
- const markerOf = (mark, rects, overlaps) => {
394
- let at = null;
395
- for (let i = rects.length - 1; i >= 0 && at === null; i -= 1) {
396
- if (rects[i].width !== 0 || rects[i].height !== 0) {
397
- at = rects[i];
401
+ const labelOf = (count) => (count === 0 ? "" : count > 99 ? "99+" : String(count));
402
+ // Its size, near enough to place it: the padding and the glyph, then the number.
403
+ const markerWidth = (label) => Math.max(MARKER_HEIGHT, 23 + (label === "" ? 0 : 3 + label.length * 7));
404
+ // Compact (P105): the number alone, or the glyph alone for none, with less padding.
405
+ const compactWidth = (label) => (label === "" ? 21 : Math.max(MARKER_HEIGHT, 10 + label.length * 7));
406
+ // A rect's last line with words on it: its last box with a width, else any with a size.
407
+ const lastBox = (rects) => {
408
+ let any = null;
409
+ for (let i = rects.length - 1; i >= 0; i -= 1) {
410
+ if (rects[i].width > 0) {
411
+ return rects[i];
398
412
  }
413
+ if (any === null && rects[i].height > 0) {
414
+ any = rects[i];
415
+ }
416
+ }
417
+ return any;
418
+ };
419
+ // Where a text anchor's marker goes (P105, #36). At its words' end, raised (0.9), it lay over
420
+ // the first letters of the next word, or between two lines. Now it sits beside the anchor's
421
+ // last line, centred on it, clear of that line's words, and over nothing of the content's but
422
+ // the background of a box holding the anchor, with nothing else between it and the words:
423
+ // in the line's gutter, just past the right edge of the anchor's block (its content box);
424
+ // where the frame is too narrow for that (text that runs to the frame's edges), at the
425
+ // frame's right edge; where something else is beside the block (a table's next cell, a card
426
+ // in a row of them), just past the line's last word. Markers on one line of one block go
427
+ // together, in the order their words end. Where the bubble with its glyph fits in none of
428
+ // these, the number alone does (compact); where that fits in none either (a full line on a
429
+ // narrow screen), it goes as far right as it can, over the line's end.
430
+ const computedOf = window.getComputedStyle;
431
+ const elementsAt = Document.prototype.elementsFromPoint;
432
+ const containsOf = Node.prototype.contains;
433
+ const INLINE = /^(inline|contents|none|ruby|table-(row|column|header|footer))/;
434
+ const blockOf = (range) => {
435
+ let node = range.endContainer;
436
+ if (node.nodeType !== 1) {
437
+ node = node.parentElement;
438
+ }
439
+ while (node !== null && node !== document.body && node !== document.documentElement && INLINE.test(computedOf.call(window, node).display)) {
440
+ node = node.parentElement;
441
+ }
442
+ return node === null || node === document.documentElement ? document.body : node;
443
+ };
444
+ // The right edge of its content box: past it is the block's padding, then what is beside it.
445
+ const contentRight = (block) => {
446
+ const style = computedOf.call(window, block);
447
+ return elementRect.call(block).right - (parseFloat(style.borderRightWidth) || 0) - (parseFloat(style.paddingRight) || 0);
448
+ };
449
+ // How far the words reach on the anchor's last line (at), in its block: at least its own.
450
+ // Read forward from the anchor's end, 64 characters at a time, only until the text leaves
451
+ // that line (P105: the whole rest of a long block, for each marker, cost 100 ms and more).
452
+ const lineEnd = (range, block, at) => {
453
+ let end = at.right;
454
+ let node = range.endContainer;
455
+ if (node.nodeType !== 3) {
456
+ return end;
457
+ }
458
+ const middle = at.top + at.height / 2;
459
+ const walker = createTreeWalker.call(document, block, NodeFilter.SHOW_TEXT);
460
+ walker.currentNode = node;
461
+ const piece = createRange.call(document);
462
+ let from = range.endOffset;
463
+ for (let pieces = 0; node !== null && pieces < 64; pieces += 1) {
464
+ const length = node.nodeValue.length;
465
+ if (from >= length) {
466
+ node = walker.nextNode();
467
+ from = 0;
468
+ continue;
469
+ }
470
+ const to = Math.min(length, from + 64);
471
+ try {
472
+ piece.setStart(node, from);
473
+ piece.setEnd(node, to);
474
+ } catch (_) {
475
+ return end;
476
+ }
477
+ for (const rect of Array.from(rangeRects.call(piece))) {
478
+ if (rect.top > middle) {
479
+ return end;
480
+ }
481
+ if (rect.width > 0 && rect.bottom >= middle && rect.right > end) {
482
+ end = rect.right;
483
+ }
484
+ }
485
+ from = to;
486
+ }
487
+ return end;
488
+ };
489
+ // Nothing of the content's at a point but a box that holds the block (its background); a
490
+ // point out of view hides nothing.
491
+ const clearAt = (x, y, block) => {
492
+ if (x < 0 || y < 0 || x >= document.documentElement.clientWidth || y >= document.documentElement.clientHeight) {
493
+ return true;
399
494
  }
400
- if (at === null) {
495
+ const hits = elementsAt.call(document, x, y);
496
+ const first = hits[0] === layer ? hits[1] : hits[0];
497
+ return first === undefined || containsOf.call(first, block);
498
+ };
499
+ // Its box: two opposite corners and its middle (the line's middle, up to it, is the path's).
500
+ const clearOver = (left, top, width, block) =>
501
+ clearAt(left + width / 2, top + MARKER_HEIGHT / 2, block) && clearAt(left + 1, top + 1, block) && clearAt(left + width - 1, top + MARKER_HEIGHT - 1, block);
502
+ const GAP = 6;
503
+ const NEAR = 3;
504
+ const BETWEEN = 3;
505
+ const EDGE = 2;
506
+ const STEP = 8;
507
+ const overlapping = (placed, left, top, width) => placed.filter((other) => Math.abs(other.top - top) < MARKER_HEIGHT && left < other.left + other.width + BETWEEN && other.left < left + width + BETWEEN)[0];
508
+ // The first place, of those above, where a line's markers fit, or null.
509
+ const placeLine = (line, compact, gap, placed, room) => {
510
+ const block = line.block;
511
+ let width = -BETWEEN;
512
+ for (const each of line.items) {
513
+ width += (compact ? each.small : each.width) + BETWEEN;
514
+ }
515
+ const most = room - EDGE - width;
516
+ const right = Math.max(contentRight(block), line.words);
517
+ // Along the line's middle, past the block, how far nothing stands in the way.
518
+ const reach = (to) => {
519
+ for (let x = right + 1; x < to; x += STEP) {
520
+ if (!clearAt(x, line.middle, block)) {
521
+ return x;
522
+ }
523
+ }
524
+ return to;
525
+ };
526
+ // A place, moved past the markers already there (rightwards; at the frame's edge, leftwards).
527
+ const fits = (left, leftwards) => {
528
+ for (let tries = 0; tries < 20; tries += 1) {
529
+ if (left < line.words + gap || left > most || reach(left + width) < left + width) {
530
+ return null;
531
+ }
532
+ const hit = overlapping(placed, left, line.top, width);
533
+ if (!hit) {
534
+ return clearOver(left, line.top, width, block) ? left : null;
535
+ }
536
+ left = leftwards ? hit.left - BETWEEN - width : hit.left + hit.width + BETWEEN;
537
+ }
401
538
  return null;
539
+ };
540
+ const gutter = right + gap;
541
+ let left = fits(gutter, false);
542
+ if (left === null && gutter > most) {
543
+ left = fits(most, true);
402
544
  }
403
- const label = mark.count === 0 ? "" : mark.count > 99 ? "99+" : String(mark.count);
404
- // Its size, near enough to place it: the padding and the glyph, then the number.
405
- const width = Math.max(MARKER_HEIGHT, 23 + (label === "" ? 0 : 3 + label.length * 7));
406
- const room = document.documentElement.clientWidth || window.innerWidth;
407
- // At the end of a text anchor's last line, raised; on an element's top right corner.
408
- let left = mark.found.range ? at.right + 2 : at.right - width / 2;
409
- let top = at.top - MARKER_HEIGHT / 2 - (mark.found.range ? 3 : 0);
410
- // Not cut off at the frame's top while its anchor is in view; with its anchor as it goes.
545
+ if (left === null) {
546
+ left = fits(line.words + gap, false);
547
+ }
548
+ return left === null ? null : { left, width };
549
+ };
550
+ const placeLines = (wanted, placed, room) => {
551
+ // One line of one block: markers whose last lines share a middle.
552
+ const lines = [];
553
+ wanted.sort((a, b) => a.at.top - b.at.top || a.at.right - b.at.right);
554
+ for (const each of wanted) {
555
+ const middle = each.at.top + each.at.height / 2;
556
+ let line = null;
557
+ for (const one of lines) {
558
+ if (line === null && one.block === each.block && Math.abs(one.middle - middle) < Math.min(one.height, each.at.height) / 2) {
559
+ line = one;
560
+ }
561
+ }
562
+ if (line === null) {
563
+ line = { block: each.block, middle, height: each.at.height, bottom: each.at.bottom, range: each.range, at: each.at, items: [] };
564
+ lines.push(line);
565
+ }
566
+ line.items.push(each);
567
+ if (each.at.right > line.at.right) {
568
+ line.at = each.at;
569
+ line.range = each.range;
570
+ }
571
+ }
572
+ const seen = document.documentElement.clientHeight || window.innerHeight;
573
+ for (const line of lines) {
574
+ line.top = line.middle - MARKER_HEIGHT / 2;
575
+ // Not cut off at the frame's top while its anchor is in view; with its anchor as it goes.
576
+ if (line.top < 2) {
577
+ line.top = Math.min(2, line.bottom - MARKER_HEIGHT);
578
+ }
579
+ let compact = false;
580
+ let found = null;
581
+ let left;
582
+ if (line.top + MARKER_HEIGHT < 0 || line.top > seen) {
583
+ // Out of view, nothing is measured: it is placed again as it comes into view.
584
+ found = { left: Math.max(EDGE, Math.min(contentRight(line.block) + GAP, room - EDGE - line.items.length * MARKER_HEIGHT)) };
585
+ } else {
586
+ line.words = lineEnd(line.range, line.block, line.at);
587
+ found = placeLine(line, false, GAP, placed, room);
588
+ if (found === null) {
589
+ compact = true;
590
+ found = placeLine(line, true, NEAR, placed, room);
591
+ }
592
+ }
593
+ if (found !== null) {
594
+ left = found.left;
595
+ } else {
596
+ // Over the line's end, as far right as nothing else stands in the way.
597
+ let width = -BETWEEN;
598
+ for (const each of line.items) {
599
+ width += each.small + BETWEEN;
600
+ }
601
+ let limit = room - EDGE;
602
+ const right = Math.max(contentRight(line.block), line.words);
603
+ for (let x = right + 1; x < limit; x += STEP) {
604
+ if (!clearAt(x, line.middle, line.block)) {
605
+ limit = x - NEAR;
606
+ }
607
+ }
608
+ left = Math.max(EDGE, Math.min(room - EDGE, limit) - width);
609
+ }
610
+ for (const each of line.items) {
611
+ each.compact = compact;
612
+ each.left = left;
613
+ each.top = line.top;
614
+ const width = compact ? each.small : each.width;
615
+ placed.push({ left, top: line.top, width });
616
+ left += width + BETWEEN;
617
+ }
618
+ }
619
+ };
620
+ // An element's marker: on its top right corner, inside the frame's width, beside another.
621
+ const placeCorner = (each, placed, room) => {
622
+ let left = each.at.right - each.width / 2;
623
+ let top = each.at.top - MARKER_HEIGHT / 2;
411
624
  if (top < 2) {
412
- top = Math.min(2, at.bottom - MARKER_HEIGHT);
625
+ top = Math.min(2, each.at.bottom - MARKER_HEIGHT);
413
626
  }
414
- // Two marks ending in one place: the later marker goes beside the earlier one.
415
627
  for (let tries = 0; tries < 20; tries += 1) {
416
- const hit = overlaps.filter((other) => Math.abs(other.top - top) < MARKER_HEIGHT && left < other.left + other.width + 2 && other.left < left + width + 2)[0];
628
+ const hit = overlapping(placed, left, top, each.width);
417
629
  if (!hit) {
418
630
  break;
419
631
  }
420
- left = hit.left + hit.width + 2;
632
+ left = hit.left + hit.width + BETWEEN;
421
633
  }
422
- left = Math.max(2, Math.min(left, room - width - 2));
423
- overlaps.push({ left, top, width });
634
+ each.left = Math.max(EDGE, Math.min(left, room - each.width - EDGE));
635
+ each.top = top;
636
+ placed.push({ left: each.left, top, width: each.width });
637
+ };
638
+ // The marker's node, kept across draws for its mark, where it was placed.
639
+ const markerOf = (each) => {
640
+ const mark = each.mark;
641
+ const label = each.label;
424
642
  const kept = markerNodes.get(mark.id);
425
- const marker = kept ? kept.element : createElement.call(document, "div");
426
- marker.className = "marker" + (mark.id === active ? " active" : "") + (mode.elements ? " inert" : "");
427
- marker.style.cssText = "left:" + left + "px;top:" + top + "px";
643
+ let marker = kept ? kept.element : null;
644
+ if (marker === null) {
645
+ marker = createElement.call(document, "div");
646
+ // A listener of its own makes it a thing to tap: WebKit makes a tap a click only on such
647
+ // an element (P105, measured: the window's listener isn't enough). It never runs (the
648
+ // window's first listener stops every click on the layer); the click is told there.
649
+ callOf(listenOn, marker, ["click", tapTarget]);
650
+ }
651
+ marker.className = "marker" + (mark.draft ? " draft" : "") + (each.compact ? " compact" : "") + (mark.id === active ? " active" : "") + (mode.elements ? " inert" : "");
652
+ marker.style.cssText = "left:" + each.left + "px;top:" + each.top + "px";
428
653
  markers.push({ id: mark.id, element: marker });
429
- if (kept && kept.label === label) {
654
+ const form = (mark.draft ? "draft:" : "") + (each.compact ? "compact:" : "full:") + label;
655
+ if (kept && kept.form === form) {
430
656
  return marker;
431
657
  }
432
- markerNodes.set(mark.id, { element: marker, label });
658
+ markerNodes.set(mark.id, { element: marker, form });
433
659
  marker.replaceChildren();
434
- const glyph = createElementNS.call(document, SVG, "svg");
435
- glyph.setAttribute("viewBox", "0 0 16 16");
436
- glyph.setAttribute("aria-hidden", "true");
437
- const path = createElementNS.call(document, SVG, "path");
438
- path.setAttribute("fill", "currentColor");
439
- path.setAttribute("d", "M3 2h10a2 2 0 0 1 2 2v6a2 2 0 0 1-2 2H8l-4 3v-3H3a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2z");
440
- glyph.appendChild(path);
441
- marker.appendChild(glyph);
660
+ if (!each.compact || label === "") {
661
+ const glyph = createElementNS.call(document, SVG, "svg");
662
+ glyph.setAttribute("viewBox", "0 0 16 16");
663
+ glyph.setAttribute("aria-hidden", "true");
664
+ const path = createElementNS.call(document, SVG, "path");
665
+ // A draft's glyph is its outline (P107), a posted one's filled.
666
+ if (mark.draft) {
667
+ path.setAttribute("fill", "none");
668
+ path.setAttribute("stroke", "currentColor");
669
+ path.setAttribute("stroke-width", "1.5");
670
+ path.setAttribute("stroke-linejoin", "round");
671
+ } else {
672
+ path.setAttribute("fill", "currentColor");
673
+ }
674
+ path.setAttribute("d", "M3 2h10a2 2 0 0 1 2 2v6a2 2 0 0 1-2 2H8l-4 3v-3H3a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2z");
675
+ glyph.appendChild(path);
676
+ marker.appendChild(glyph);
677
+ }
442
678
  if (label !== "") {
443
679
  const number = createElement.call(document, "span");
444
680
  number.textContent = label;
@@ -446,25 +682,43 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
446
682
  }
447
683
  return marker;
448
684
  };
685
+ const tapTarget = () => {};
449
686
  let active = null;
687
+ // The mark whose marker the pointer is over: its words are lit as the active mark's are, so
688
+ // a marker in the gutter shows which words it is for (P105).
689
+ let hot = null;
450
690
  let flashing = null;
451
691
  let flashUntil = 0;
692
+ // The layer's parts: its style, the marks, the markers, the outline while picking. A draw
693
+ // replaces the marks and the outline, and leaves each marker that stays where it is (P105): a
694
+ // marker taken out of the page and put back mid-press loses its click (WebKit, measured once
695
+ // the pointer's arrival on a marker lit its mark and redrew).
696
+ let boxes = null;
697
+ const boxesOf = (root) => {
698
+ if (boxes === null || boxes.style.parentNode !== root || boxes.marks.parentNode !== root || boxes.markers.parentNode !== root || boxes.hover.parentNode !== root) {
699
+ const style = createElement.call(document, "style");
700
+ if (nonce !== "") {
701
+ style.nonce = nonce;
702
+ }
703
+ boxes = { style, marks: createElement.call(document, "div"), markers: createElement.call(document, "div"), hover: createElement.call(document, "div") };
704
+ root.replaceChildren(boxes.style, boxes.marks, boxes.markers, boxes.hover);
705
+ }
706
+ if (boxes.style.textContent !== STYLE) {
707
+ boxes.style.textContent = STYLE;
708
+ }
709
+ return boxes;
710
+ };
452
711
  const draw = () => {
453
712
  if (marks.length === 0 && hover === null && layer === null) {
454
713
  return;
455
714
  }
456
715
  const root = ensureLayer();
716
+ const into = boxesOf(root);
457
717
  const parts = [];
458
- const style = createElement.call(document, "style");
459
- if (nonce !== "") {
460
- style.nonce = nonce;
461
- }
462
- style.textContent = STYLE;
463
- parts.push(style);
464
718
  const flashOn = Date.now() < flashUntil;
465
- // Markers go over every mark, in the order of the marks.
466
- const badges = [];
467
- const overlaps = [];
719
+ // What is measured first, then drawn: no node of the layer changes while the page is read.
720
+ const corners = [];
721
+ const wanted = [];
468
722
  markers = [];
469
723
  for (const mark of marks) {
470
724
  if (mark.found === null) {
@@ -476,25 +730,58 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
476
730
  continue;
477
731
  }
478
732
  const each = createElement.call(document, "div");
479
- each.className = "mark " + (mark.found.range ? "text" : "element") + (mark.count === null ? "" : " quiet") + (mark.id === active ? " active" : "") + (flashOn && mark.id === flashing ? " flash" : "");
733
+ each.className = "mark " + (mark.found.range ? "text" : "element") + (mark.marker ? " quiet" : "") + (mark.draft ? " draft" : "") + (mark.id === active || mark.id === hot ? " active" : "") + (flashOn && mark.id === flashing ? " flash" : "");
480
734
  each.style.cssText = "left:" + rect.left + "px;top:" + rect.top + "px;width:" + rect.width + "px;height:" + rect.height + "px";
481
735
  parts.push(each);
482
736
  }
483
- if (mark.count !== null) {
484
- const marker = markerOf(mark, rects, overlaps);
485
- if (marker !== null) {
486
- badges.push(marker);
737
+ if (mark.marker) {
738
+ const at = lastBox(rects);
739
+ if (at !== null) {
740
+ // A draft's marker has no number (P107).
741
+ const label = mark.draft ? "" : labelOf(mark.count);
742
+ const each = { mark, order: corners.length + wanted.length, label, width: markerWidth(label), small: compactWidth(label), compact: false, at, left: 0, top: 0 };
743
+ if (mark.found.range) {
744
+ // Kept while it still holds the words (P105): a change elsewhere doesn't move them.
745
+ if (mark.block === undefined || !mark.block.isConnected || !containsOf.call(mark.block, mark.found.range.endContainer)) {
746
+ mark.block = blockOf(mark.found.range);
747
+ }
748
+ each.range = mark.found.range;
749
+ each.block = mark.block;
750
+ wanted.push(each);
751
+ } else {
752
+ corners.push(each);
753
+ }
487
754
  }
488
755
  }
489
756
  }
490
- for (const marker of badges) {
491
- parts.push(marker);
757
+ // Markers go over every mark: elements' on their corners first, then the lines'.
758
+ const room = document.documentElement.clientWidth || window.innerWidth;
759
+ const placed = [];
760
+ for (const each of corners) {
761
+ placeCorner(each, placed, room);
762
+ }
763
+ // The layer's own boxes out of the way while places are hit-tested (P105): each test
764
+ // otherwise walks every mark and marker the last draw left (0.13 ms each, 300 of them).
765
+ if (wanted.length > 0) {
766
+ into.marks.style.display = "none";
767
+ into.markers.style.display = "none";
768
+ try {
769
+ placeLines(wanted, placed, room);
770
+ } finally {
771
+ into.marks.style.display = "";
772
+ into.markers.style.display = "";
773
+ }
774
+ }
775
+ const badges = [];
776
+ for (const each of corners.concat(wanted).sort((a, b) => a.order - b.order)) {
777
+ badges.push(markerOf(each));
492
778
  }
493
779
  for (const id of Array.from(markerNodes.keys())) {
494
780
  if (!markers.some((each) => each.id === id)) {
495
781
  markerNodes.delete(id);
496
782
  }
497
783
  }
784
+ const outlines = [];
498
785
  if (hover !== null && hover.isConnected) {
499
786
  const rect = elementRect.call(hover);
500
787
  const outline = createElement.call(document, "div");
@@ -504,9 +791,20 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
504
791
  tag.className = "tag";
505
792
  tag.textContent = describe(hover).slice(0, 80);
506
793
  tag.style.cssText = "left:" + Math.max(4, rect.left) + "px;top:" + (rect.top >= 24 ? rect.top - 22 : Math.max(4, rect.bottom + 4)) + "px";
507
- parts.push(outline, tag);
794
+ outlines.push(outline, tag);
795
+ }
796
+ into.marks.replaceChildren(...parts);
797
+ for (const child of Array.from(into.markers.childNodes)) {
798
+ if (badges.indexOf(child) === -1) {
799
+ into.markers.removeChild(child);
800
+ }
801
+ }
802
+ for (const badge of badges) {
803
+ if (badge.parentNode !== into.markers) {
804
+ into.markers.appendChild(badge);
805
+ }
508
806
  }
509
- root.replaceChildren(...parts);
807
+ into.hover.replaceChildren(...outlines);
510
808
  };
511
809
  let drawing = false;
512
810
  const redraw = () => {
@@ -520,18 +818,15 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
520
818
  };
521
819
 
522
820
  const resultOf = (mark) => (mark.found === null ? { id: mark.id, state: "orphaned" } : { id: mark.id, state: "attached", rect: box(rectOf(mark.found)) });
821
+ // Two boxes more than 0.5 px apart on any side (or one of them unknown).
822
+ 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;
523
823
  // What the host was last told of each mark (mark.told), so a settle tells only what moved.
524
824
  const moved = (mark, result) => {
525
825
  const told = mark.told;
526
826
  if (!told || told.state !== result.state) {
527
827
  return true;
528
828
  }
529
- if (result.state !== "attached") {
530
- return false;
531
- }
532
- const a = told.rect;
533
- const b = result.rect;
534
- return 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;
829
+ return result.state === "attached" && apart(told.rect, result.rect);
535
830
  };
536
831
  const report = (pairs) => {
537
832
  if (pairs.length > 0) {
@@ -630,27 +925,42 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
630
925
  }).observe(document, { childList: true, subtree: true, characterData: true });
631
926
  window.addEventListener("resize", redraw);
632
927
  window.addEventListener("scroll", redraw, true);
633
- // Once a scroll, a resize or a run of changes has settled, the marks that moved since the host
634
- // was last told, in one report (P104), while any mark has a marker (a host without markers
635
- // hears what it heard before), and at most one a second, the latest: the reports share the
636
- // bridge's 120 a minute with the person's selections and clicks. While marks are being
637
- // found again, that run tells it instead.
928
+ // Follow-ups. Once a scroll, a resize or a run of changes has settled: the marks that moved
929
+ // since the host was last told, in one report (P104), while any mark has a marker (a host
930
+ // without markers hears the reports it heard before); and once a scroll has settled, the
931
+ // person's selection, if its box moved since it was told (P105). Each only what moved more
932
+ // than 0.5 px, and all of them on one clock, at most one report a second (the latest): they
933
+ // share the bridge's 120 a minute with the person's own selections and clicks. While marks
934
+ // are being found again, that run tells them instead.
638
935
  const FOLLOW_EVERY = 1000;
639
936
  let settleTimer = 0;
640
937
  let followTimer = 0;
641
938
  let followed = 0;
939
+ // A scroll moved the page since the selection was last told: it is followed only then.
940
+ let selectionScrolled = false;
941
+ const counted = () => marks.some((mark) => mark.marker);
642
942
  const sendFollow = () => {
643
- if (marks.length === 0 || settling()) {
644
- return;
943
+ let sent = 0;
944
+ if (marks.length > 0 && !settling() && counted()) {
945
+ const pairs = resultsOf(true);
946
+ if (pairs.length > 0) {
947
+ report(pairs);
948
+ sent += 1;
949
+ }
645
950
  }
646
- const pairs = resultsOf(true);
647
- if (pairs.length > 0) {
648
- followed = Date.now();
649
- report(pairs);
951
+ if (selectionScrolled) {
952
+ selectionScrolled = false;
953
+ if (reportSelection(true)) {
954
+ sent += 1;
955
+ }
956
+ }
957
+ if (sent > 0) {
958
+ // Each report takes its own second of the clock: two at once leave the next one empty.
959
+ followed = Date.now() + (sent - 1) * FOLLOW_EVERY;
650
960
  }
651
961
  };
652
962
  const follow = () => {
653
- if (followTimer !== 0 || !marks.some((mark) => mark.count !== null)) {
963
+ if (followTimer !== 0 || !(selectionScrolled || counted())) {
654
964
  return;
655
965
  }
656
966
  const wait = followed + FOLLOW_EVERY - Date.now();
@@ -664,7 +974,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
664
974
  }, wait);
665
975
  };
666
976
  const settle = () => {
667
- if (marks.length === 0) {
977
+ if (marks.length === 0 && !selectionScrolled) {
668
978
  return;
669
979
  }
670
980
  clearTimeout(settleTimer);
@@ -706,36 +1016,80 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
706
1016
  // runs before any of theirs), nor does what lies under it, and a click is reported. No tab
707
1017
  // stop, by choice: it would join the page's focus order, which the content controls, and the
708
1018
  // layer is hidden from assistive technology; the host's own list of marks is the keyboard way.
1019
+ //
1020
+ // A press is read and stopped through the platform's own functions and getters, kept now
1021
+ // (P105), as the navigation guard reads its events (P099): content that later patches
1022
+ // Event's, MouseEvent's or DOMRect's prototypes (or Function's call) neither hears a press
1023
+ // through this listener nor makes it misread one. Narrowing only: content can still cover
1024
+ // the layer, reach its nodes through other built-ins, and tell that a press happened.
1025
+ const stopPress = Event.prototype.stopImmediatePropagation;
1026
+ // The platform's preventDefault, kept before a tab's shell locks its own over it (P099).
1027
+ const cancelPress = preventNavigation;
1028
+ const targetOf = getterOf(Event.prototype, "target");
1029
+ const typeOf = getterOf(Event.prototype, "type");
1030
+ const buttonOf = getterOf(MouseEvent.prototype, "button");
1031
+ const clientXOf = getterOf(MouseEvent.prototype, "clientX");
1032
+ const clientYOf = getterOf(MouseEvent.prototype, "clientY");
1033
+ const leftOf = getterOf(DOMRectReadOnly.prototype, "left");
1034
+ const topOf = getterOf(DOMRectReadOnly.prototype, "top");
1035
+ const widthOf = getterOf(DOMRectReadOnly.prototype, "width");
1036
+ const heightOf = getterOf(DOMRectReadOnly.prototype, "height");
1037
+ const markerBox = (element) => {
1038
+ const rect = callOf(elementRect, element, []);
1039
+ return { x: callOf(leftOf, rect, []), y: callOf(topOf, rect, []), width: callOf(widthOf, rect, []), height: callOf(heightOf, rect, []) };
1040
+ };
709
1041
  const markerAt = (x, y) => {
710
1042
  for (let i = markers.length - 1; i >= 0; i -= 1) {
711
- const rect = elementRect.call(markers[i].element);
712
- if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) {
1043
+ const rect = markerBox(markers[i].element);
1044
+ if (x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height) {
713
1045
  return markers[i];
714
1046
  }
715
1047
  }
716
1048
  return null;
717
1049
  };
1050
+ const isShown = (id) => {
1051
+ for (let i = 0; i < marks.length; i += 1) {
1052
+ if (marks[i].id === id) {
1053
+ return true;
1054
+ }
1055
+ }
1056
+ return false;
1057
+ };
718
1058
  const onMarker = (event) => {
719
- if (layer === null || event.target !== layer) {
1059
+ if (layer === null || callOf(targetOf, event, []) !== layer) {
720
1060
  return;
721
1061
  }
722
- event.stopImmediatePropagation();
723
- // No focus moves, no selection changes, no menu; a touch still makes its click.
724
- if (CANCELLED[event.type] === 1) {
725
- event.preventDefault();
1062
+ callOf(stopPress, event, []);
1063
+ const type = callOf(typeOf, event, []);
1064
+ // No focus moves, no selection changes, no menu; a touch still makes its click (on WebKit,
1065
+ // since the marker has a listener of its own, P105).
1066
+ if (CANCELLED[type] === 1) {
1067
+ callOf(cancelPress, event, []);
1068
+ }
1069
+ if (HOVERS[type] === 1 || HOVERS[type] === 0) {
1070
+ const hit = HOVERS[type] === 1 ? markerAt(callOf(clientXOf, event, []), callOf(clientYOf, event, [])) : null;
1071
+ const id = hit === null ? null : hit.id;
1072
+ if (id !== hot) {
1073
+ hot = id;
1074
+ redraw();
1075
+ }
726
1076
  }
727
- if (event.type === "click" && event.isTrusted && event.button === 0) {
728
- const hit = markerAt(event.clientX, event.clientY);
729
- if (hit !== null && marks.some((mark) => mark.id === hit.id)) {
730
- tell(M.activated, { id: hit.id, rect: box(elementRect.call(hit.element)) });
1077
+ // isTrusted is the event's own, unforgeable.
1078
+ if (type === "click" && event.isTrusted && callOf(buttonOf, event, []) === 0) {
1079
+ const hit = markerAt(callOf(clientXOf, event, []), callOf(clientYOf, event, []));
1080
+ if (hit !== null && isShown(hit.id)) {
1081
+ tell(M.activated, { id: hit.id, rect: markerBox(hit.element) });
731
1082
  }
732
1083
  }
733
1084
  };
734
1085
  // The pointer moving over a marker is stopped too. Narrowing only: the content still sees
735
1086
  // the pointer leave its own element for the layer (relatedTarget), the layer's :hover and
736
1087
  // :active, and the frame's user activation, so it can tell a marker was pressed, and
737
- // roughly where; it can't act on or cancel the click.
1088
+ // roughly where. Content that patches no built-in can't act on or cancel the click; one that
1089
+ // does can still cover the layer or reach its nodes (P105).
738
1090
  const CANCELLED = { mousedown: 1, mouseup: 1, click: 1, dblclick: 1, auxclick: 1, contextmenu: 1 };
1091
+ // The pointer on a marker (1), or leaving the layer for the content (0).
1092
+ const HOVERS = { pointerover: 1, pointermove: 1, pointerout: 0, pointerleave: 0 };
739
1093
  for (const type of ["pointerdown", "pointerup", "pointercancel", "mousedown", "mouseup", "click", "dblclick", "auxclick", "contextmenu", "touchstart", "touchend", "touchcancel", "pointerover", "pointerout", "pointerenter", "pointerleave", "pointermove", "mouseover", "mouseout", "mouseenter", "mouseleave", "mousemove"]) {
740
1094
  window.addEventListener(type, onMarker, true);
741
1095
  }
@@ -759,11 +1113,12 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
759
1113
  mode = { elements: false, text: false };
760
1114
  hover = null;
761
1115
  reported = null;
1116
+ reportedBox = null;
762
1117
  setCursor(false);
763
1118
  draw();
764
1119
  };
765
- // The content's own handlers never see the pointer while an element is being picked; a
766
- // finger outlines what it is on as it goes down.
1120
+ // The content's own handlers never see the pointer while an element is being picked (content
1121
+ // that patches Event.prototype can: P105); a finger outlines what it is on as it goes down.
767
1122
  let pressed = null;
768
1123
  const swallow = (event) => {
769
1124
  if (mode.elements) {
@@ -826,35 +1181,44 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
826
1181
  }
827
1182
  }, true);
828
1183
 
829
- // Selecting text: each selection that settles, and its clearing; again after a scroll.
1184
+ // Selecting text: each selection that settles, and its clearing; after a scroll, the same
1185
+ // selection again only if its box moved, on the follow-ups' clock (P105: re-sent after every
1186
+ // settle, an honest page's scrolling spent the budget its marker's click needed).
830
1187
  let reported = null;
1188
+ let reportedBox = null;
831
1189
  let selectionTimer = 0;
832
- const reportSelection = (again) => {
1190
+ // Whether it told the host anything.
1191
+ const reportSelection = (following) => {
833
1192
  if (!mode.text) {
834
- return;
1193
+ return false;
835
1194
  }
836
1195
  const selection = selectionOf.call(document);
837
1196
  if (selection === null || selection.isCollapsed || selection.rangeCount === 0) {
838
1197
  if (reported !== null) {
839
1198
  reported = null;
1199
+ reportedBox = null;
840
1200
  tell(M.unselected, {});
1201
+ return true;
841
1202
  }
842
- return;
1203
+ return false;
843
1204
  }
844
1205
  const range = selection.getRangeAt(0);
845
1206
  if (!document.body || !document.body.contains(range.commonAncestorContainer)) {
846
- return;
1207
+ return false;
847
1208
  }
848
1209
  const anchor = textAnchor(range, textIndex());
849
1210
  if (anchor === null) {
850
- return;
1211
+ return false;
851
1212
  }
852
1213
  const key = anchor.selectors[1].start + ":" + anchor.selectors[1].end;
853
- if (key === reported && !again) {
854
- return;
1214
+ const rect = box(rangeRect.call(range));
1215
+ if (key === reported && !(following && apart(reportedBox, rect))) {
1216
+ return false;
855
1217
  }
856
1218
  reported = key;
857
- tell(M.selected, { anchor, rect: box(rangeRect.call(range)) });
1219
+ reportedBox = rect;
1220
+ tell(M.selected, { anchor, rect });
1221
+ return true;
858
1222
  };
859
1223
  document.addEventListener("selectionchange", () => {
860
1224
  if (mode.text) {
@@ -862,11 +1226,10 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
862
1226
  selectionTimer = setTimeout(() => reportSelection(false), 250);
863
1227
  }
864
1228
  });
865
- let scrollTimer = 0;
866
1229
  window.addEventListener("scroll", () => {
867
1230
  if (reported !== null) {
868
- clearTimeout(scrollTimer);
869
- scrollTimer = setTimeout(() => reportSelection(true), 150);
1231
+ selectionScrolled = true;
1232
+ settle();
870
1233
  }
871
1234
  }, true);
872
1235
 
@@ -884,6 +1247,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
884
1247
  mode = { elements: params.elements === true, text: params.text === true };
885
1248
  hover = null;
886
1249
  reported = null;
1250
+ reportedBox = null;
887
1251
  setCursor(mode.elements);
888
1252
  draw();
889
1253
  reportSelection(false);
@@ -892,10 +1256,15 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
892
1256
  stop();
893
1257
  break;
894
1258
  case M.show:
1259
+ // A draft (P107) has a marker and never a number; a kind the shell doesn't know is no mark.
895
1260
  marks = (Array.isArray(params.marks) ? params.marks : [])
896
1261
  .slice(0, LIMIT.marks)
897
- .filter((mark) => mark && typeof mark.id === "string" && mark.anchor && Array.isArray(mark.anchor.selectors))
898
- .map((mark) => ({ id: mark.id, anchor: mark.anchor, count: typeof mark.count === "number" && mark.count >= 0 && mark.count <= LIMIT.count && Math.floor(mark.count) === mark.count ? mark.count : null, found: null, state: null, told: null }));
1262
+ .filter((mark) => mark && typeof mark.id === "string" && mark.anchor && Array.isArray(mark.anchor.selectors) && (mark.kind === undefined || mark.kind === "posted" || mark.kind === "draft"))
1263
+ .map((mark) => {
1264
+ const draft = mark.kind === "draft";
1265
+ const count = !draft && typeof mark.count === "number" && mark.count >= 0 && mark.count <= LIMIT.count && Math.floor(mark.count) === mark.count ? mark.count : null;
1266
+ return { id: mark.id, anchor: mark.anchor, count, draft, marker: draft || count !== null, found: null, state: null, told: null };
1267
+ });
899
1268
  if (typeof params.active === "string") {
900
1269
  active = params.active;
901
1270
  }
@@ -911,6 +1280,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
911
1280
  pendingFocus = null;
912
1281
  marks = [];
913
1282
  active = null;
1283
+ hot = null;
914
1284
  draw();
915
1285
  break;
916
1286
  default: