@botiverse/porthole 0.14.13 → 0.14.15

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.
@@ -54,6 +54,22 @@ export const MARKER_NAME = String.raw `(mark) => {
54
54
  const what = mark.draft ? "Draft comment" : mark.count === 1 ? "1 comment" : mark.count > 1 ? mark.count + " comments" : "Comment";
55
55
  return words === "" ? what : what + ' on "' + words + '"';
56
56
  }`;
57
+ /**
58
+ * Whether a host's `focusMark` is too old to land unasked (P137): `at` is the host's `Date.now()` as
59
+ * it sent the word, `now` the shell's, kept before content runs. Both read the one wall clock of
60
+ * this machine, the only clock two realms share (`performance.now()` counts from each one's own
61
+ * start). No number is an older host's word, which can't be known fresh. A word from ahead of the
62
+ * clock has the same bound behind it: two processes can read the clock a little apart, and past
63
+ * that the clock was set back, and the age says nothing. Comparisons only, nothing a page can
64
+ * patch, and a word that makes no number (`NaN`, `Infinity`) passes neither test.
65
+ */
66
+ export const WORD_LATE = String.raw `(at, now) => {
67
+ if (typeof at !== "number") {
68
+ return true;
69
+ }
70
+ const age = now - at;
71
+ return !(age > -100 && age < 100);
72
+ }`;
57
73
  /**
58
74
  * The shell's annotation runtime (P053), spliced into the shell's runtime (`__ANNOTATE__`) when
59
75
  * the config declares `annotate`, so it runs before any content does, with the DOM's own
@@ -61,7 +77,7 @@ export const MARKER_NAME = String.raw `(mark) => {
61
77
  * in one).
62
78
  *
63
79
  * The host drives it (`porthole/annotate.start|stop|show|focus|clear|track|clearSelection|focusMark`)
64
- * and it reports (`porthole/annotate.selected|unselected|cancelled|anchored|activated|tracked`):
80
+ * and it reports (`porthole/annotate.selected|unselected|cancelled|anchored|activated|tracked|selectionTracked`):
65
81
  * - picking an element: the element under the pointer (or finger) is outlined with its name; a
66
82
  * click or tap picks it, and the content's own handlers never see that click (unless the content
67
83
  * patched the DOM's built-ins, P105); Escape cancels (while selecting text too, P112);
@@ -77,8 +93,8 @@ export const MARKER_NAME = String.raw `(mark) => {
77
93
  * focus on it (P133), is reported (`activated`), and the content's own handlers never see it; a
78
94
  * draft is drawn apart, a dashed line and a hollow bubble with no number, and has a marker with
79
95
  * or without a count; the host's `focusMark` (P134) puts the focus on a marker that is there, and
80
- * tells nothing back; one that had to wait for the marks lands only if the focus is still in this
81
- * document (P135);
96
+ * tells nothing back; one that had to wait for the marks (P135), or that this document took late
97
+ * (P137), lands only if the focus is in this document by then;
82
98
  * - following: while a mark has a marker, once a scroll, a resize or a change of the content
83
99
  * settles, the marks that moved since the host was last told are reported, at most once a
84
100
  * second, so the host's own UI beside a mark can follow;
@@ -87,7 +103,11 @@ export const MARKER_NAME = String.raw `(mark) => {
87
103
  * followed mark's (`tracked`) too, so a host places marks scrolled out of the frame, and keeps
88
104
  * them placed as the document scrolls, from one report and the followed mark's scroll;
89
105
  * - the last line (P133): where a selection or a mark's words end, in the document's coordinates
90
- * too (`lastLineDocumentRect`), so a host places its UI after them.
106
+ * too (`lastLineDocumentRect`), so a host places its UI after them;
107
+ * - the selection followed (P138): what the last `selected` reported (the element picked, or the
108
+ * words selected) is kept, and while the host follows it (`track` with `selection`) it is drawn
109
+ * in the layer and its boxes are reported as a followed mark's are (`selectionTracked`), with
110
+ * whether it is in view, or that it is gone; it takes no pointer, no focus and no key.
91
111
  *
92
112
  * It shares the realm with the content, which can interfere with all of it: the host takes its
93
113
  * reports as UI hints only.
@@ -108,6 +128,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
108
128
  activated: "porthole/annotate.activated",
109
129
  track: "porthole/annotate.track",
110
130
  tracked: "porthole/annotate.tracked",
131
+ selectionTracked: "porthole/annotate.selectionTracked",
111
132
  clearSelection: "porthole/annotate.clearSelection",
112
133
  focusMark: "porthole/annotate.focusMark",
113
134
  };
@@ -122,9 +143,11 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
122
143
  const removeAllRanges = Selection.prototype.removeAllRanges;
123
144
  const focusElement = HTMLElement.prototype.focus;
124
145
  const hasFocus = Document.prototype.hasFocus;
146
+ const wallClock = Date.now;
125
147
  const elementRect = Element.prototype.getBoundingClientRect;
126
148
  const rangeRect = Range.prototype.getBoundingClientRect;
127
149
  const rangeRects = Range.prototype.getClientRects;
150
+ const cloneRange = Range.prototype.cloneRange;
128
151
  const setStart = Range.prototype.setStart;
129
152
  const setStartBefore = Range.prototype.setStartBefore;
130
153
  const compareBoundaryPoints = Range.prototype.compareBoundaryPoints;
@@ -381,7 +404,10 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
381
404
  // box of at least 44 by 44 px centred on its own (an invisible ::before; 100% is its padding
382
405
  // box, its border 1 px), so a tap a few px off still lands on the shell's layer. A marker is
383
406
  // a real button (P133): what a browser gives one is taken off, a ring says it has the focus.
384
- 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}";
407
+ // The selection the host follows (P138) is drawn as what is picked is: an element in the
408
+ // picking outline, a little lighter and without its name; words with a light fill and a line
409
+ // under them, in the same blue, apart from the marks' yellow (no comment is there yet).
410
+ 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)}.held{position:absolute;outline:2px solid #2563eb;outline-offset:1px;background:rgba(37,99,235,.06)}.held.text{outline:0;border-radius:2px;background:rgba(37,99,235,.12);box-shadow:inset 0 -2px 0 #2563eb}.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}";
385
411
  // Inline and important: the content's own style sheets can't hide, shrink or move it by
386
412
  // accident. Set again at each draw, since its scripts can rewrite it (narrowing only: the
387
413
  // content can still cover the layer, or move it with its ancestors, P104). Clipped, where an
@@ -407,6 +433,31 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
407
433
 
408
434
  let mode = { elements: false, text: false, hold: false };
409
435
  let hover = null;
436
+ // The selection last reported as selected (P138): its anchor, and what it is in the page (the
437
+ // element picked, or a copy of the selection's range, which the page's changes keep up to
438
+ // date and which stays when the page's own selection goes). Followed while the host asks
439
+ // (track with selection), and drawn then. Each new selected replaces it.
440
+ let held = null;
441
+ let followingSelection = false;
442
+ // What the host was last told of it: "attached", "orphaned", or null (nothing yet).
443
+ let heldTold = null;
444
+ const heldGone = (found) => (found.range ? found.range.collapsed || !found.range.startContainer.isConnected : !found.element.isConnected);
445
+ const heldShown = () => followingSelection && held !== null && heldTold !== "orphaned" && !heldGone(held.found);
446
+ // The element just picked (P138, Tenny's review): it keeps its outline after selected, so
447
+ // there is no gap before the host follows it, and the follow's highlight takes over where it
448
+ // does. Without a follow (an older host) it ends as a selection does: the next pick or
449
+ // selection, the host's clearSelection, a mode started or stopped, clear, Escape, or the
450
+ // element leaving the page. Not unselected: a selection made after the pick has already
451
+ // ended it as it was reported, so the one unselected can clear is one from before, which
452
+ // the pick's own click takes away when text is selected too.
453
+ let picked = null;
454
+ const endPick = () => {
455
+ if (picked !== null) {
456
+ picked = null;
457
+ redraw();
458
+ }
459
+ };
460
+ const pickShown = () => picked !== null && !followingSelection && picked.isConnected;
410
461
  let marks = [];
411
462
  // The markers as last drawn, for a click to find: { id, element }. Each mark keeps its
412
463
  // marker's node across draws, so a press that spans a redraw ends on the node it began on.
@@ -797,7 +848,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
797
848
  return boxes;
798
849
  };
799
850
  const draw = () => {
800
- if (marks.length === 0 && hover === null && layer === null) {
851
+ if (marks.length === 0 && hover === null && layer === null && !heldShown() && !pickShown()) {
801
852
  return;
802
853
  }
803
854
  const root = ensureLayer();
@@ -892,6 +943,24 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
892
943
  }
893
944
  }
894
945
  const outlines = [];
946
+ // The followed selection (P138), under the picking outline: no pointer, no focus, no name;
947
+ // before the host follows it, the element just picked, drawn the same way.
948
+ if (picked !== null && !picked.isConnected) {
949
+ picked = null;
950
+ }
951
+ const lit = heldShown() ? held.found : pickShown() ? { element: picked } : null;
952
+ if (lit !== null) {
953
+ const rects = lit.range ? Array.from(rangeRects.call(lit.range)).slice(0, LIMIT.boxes) : [elementRect.call(lit.element)];
954
+ for (const rect of rects) {
955
+ if (rect.width === 0 && rect.height === 0) {
956
+ continue;
957
+ }
958
+ const each = createElement.call(document, "div");
959
+ each.className = lit.range ? "held text" : "held";
960
+ each.style.cssText = "left:" + rect.left + "px;top:" + rect.top + "px;width:" + rect.width + "px;height:" + rect.height + "px";
961
+ outlines.push(each);
962
+ }
963
+ }
895
964
  if (hover !== null && hover.isConnected) {
896
965
  const rect = elementRect.call(hover);
897
966
  const outline = createElement.call(document, "div");
@@ -937,9 +1006,17 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
937
1006
  let trackedBox = null;
938
1007
  let trackedAt = 0;
939
1008
  let trackFrame = 0;
1009
+ // The followed selection's viewport as last told (P138): a frame made smaller can leave it
1010
+ // out of view without moving it.
1011
+ let trackedView = null;
940
1012
  const trackTick = () => {
941
1013
  trackFrame = 0;
942
- if (tracking === null) {
1014
+ if (tracking === null && !followingSelection) {
1015
+ return;
1016
+ }
1017
+ if (followingSelection) {
1018
+ followHeld();
1019
+ trackFrame = nextFrame(trackTick);
943
1020
  return;
944
1021
  }
945
1022
  const mark = marks.find((each) => each.id === tracking);
@@ -958,12 +1035,79 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
958
1035
  }
959
1036
  trackFrame = nextFrame(trackTick);
960
1037
  };
961
- const track = (id) => {
1038
+ // The followed selection (P138), as a followed mark is, and on its clock: where it is at each
1039
+ // animation frame where it moved, or the viewport changed size, with whether any of it is in
1040
+ // the viewport; orphaned once while it isn't in the page (a page that took the element out,
1041
+ // a re-render, the words gone), found again by its anchor at most twice a second meanwhile.
1042
+ // Geometry only: its anchor and words went with selected. Reads boxes, nothing else: no
1043
+ // focus, no selection, no listener.
1044
+ const followHeld = () => {
1045
+ const now = performance.now();
1046
+ if (held !== null && heldGone(held.found) && now - held.tried >= 500) {
1047
+ held.tried = now;
1048
+ let found = null;
1049
+ try {
1050
+ found = find(held.anchor, textIndex());
1051
+ } catch (_) {
1052
+ found = null;
1053
+ }
1054
+ if (found !== null && !heldGone(found)) {
1055
+ held.found = found;
1056
+ }
1057
+ }
1058
+ if (held === null || heldGone(held.found)) {
1059
+ if (heldTold !== "orphaned") {
1060
+ heldTold = "orphaned";
1061
+ trackedBox = null;
1062
+ tell(M.selectionTracked, { state: "orphaned" });
1063
+ redraw();
1064
+ }
1065
+ return;
1066
+ }
1067
+ const rect = box(rectOf(held.found));
1068
+ const root = document.documentElement;
1069
+ const view = { width: root ? root.clientWidth : 0, height: root ? root.clientHeight : 0 };
1070
+ const resized = trackedView === null || trackedView.width !== view.width || trackedView.height !== view.height;
1071
+ if ((heldTold !== "attached" || resized || apart(trackedBox, rect)) && now - trackedAt >= 16) {
1072
+ heldTold = "attached";
1073
+ trackedBox = rect;
1074
+ trackedView = view;
1075
+ trackedAt = now;
1076
+ const scroll = scrollOf();
1077
+ const lastLineDocumentRect = lastLineOf(held.found, scroll);
1078
+ const inView = rect.x < view.width && rect.x + rect.width > 0 && rect.y < view.height && rect.y + rect.height > 0;
1079
+ tell(M.selectionTracked, Object.assign({ state: "attached", rect, documentRect: inDocument(rect, scroll), scroll, inView }, lastLineDocumentRect === null ? {} : { lastLineDocumentRect }));
1080
+ // The highlight goes with it (a layout change moves it without a scroll).
1081
+ redraw();
1082
+ }
1083
+ };
1084
+ // A mark, the selection (P138), or nothing: one at a time, each replacing the last.
1085
+ const track = (id, selection) => {
1086
+ const was = heldShown() || pickShown();
962
1087
  tracking = typeof id === "string" ? id : null;
1088
+ followingSelection = tracking === null && selection === true;
1089
+ // The follow's highlight takes the pick's outline over, in this draw (Tenny's review).
1090
+ if (followingSelection) {
1091
+ picked = null;
1092
+ }
963
1093
  trackedBox = null;
964
- if (tracking !== null && trackFrame === 0) {
1094
+ trackedView = null;
1095
+ heldTold = null;
1096
+ if ((tracking !== null || followingSelection) && trackFrame === 0) {
965
1097
  trackFrame = nextFrame(trackTick);
966
1098
  }
1099
+ if (heldShown() || pickShown() || was) {
1100
+ draw();
1101
+ }
1102
+ };
1103
+ // A new selected (P138): what is followed, if the selection is, from the next frame on.
1104
+ const holdSelected = (anchor, found) => {
1105
+ held = { anchor, found, tried: 0 };
1106
+ if (followingSelection) {
1107
+ trackedBox = null;
1108
+ heldTold = null;
1109
+ redraw();
1110
+ }
967
1111
  };
968
1112
 
969
1113
  // Where each found mark starts in the document, in order (P112): its place in a list the host
@@ -1074,6 +1218,10 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1074
1218
  let waitingForContent = false;
1075
1219
  let pendingFocus = null;
1076
1220
  let pendingMarker = null;
1221
+ // Asked through the kept function: a page that says it has the focus gets no landing the
1222
+ // platform would refuse (P135).
1223
+ const focusIsHere = () => callOf(hasFocus, document, []) === true;
1224
+ const wordLate = ${WORD_LATE};
1077
1225
  const settling = () => anchorTimer !== 0 || waitingForContent || marks.some((mark) => mark.state === null);
1078
1226
  const scheduleAnchor = (all) => {
1079
1227
  if (all) {
@@ -1117,10 +1265,9 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1117
1265
  pendingMarker = null;
1118
1266
  // Parked, the host's word is old: the person may have moved on, and a marker's focus
1119
1267
  // takes the keyboard with no gesture. So it lands only if the focus is still in this
1120
- // document, asked through the kept function: a page that says it has the focus gets
1121
- // no landing the platform would refuse. Otherwise it is dropped (P135).
1122
- if (callOf(hasFocus, document, []) === true) {
1123
- focusMark(id);
1268
+ // document. Otherwise it is dropped (P135).
1269
+ if (focusIsHere()) {
1270
+ focusMark(id, false);
1124
1271
  }
1125
1272
  }
1126
1273
  }, wait);
@@ -1163,9 +1310,11 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1163
1310
  sent += 1;
1164
1311
  }
1165
1312
  }
1313
+ // Not while the host follows the selection (P138, Tenny's review): its reports carry where
1314
+ // it is; a scroll after the following ends is re-sent as before.
1166
1315
  if (selectionScrolled) {
1167
1316
  selectionScrolled = false;
1168
- if (reportSelection(true)) {
1317
+ if (!followingSelection && reportSelection(true)) {
1169
1318
  sent += 1;
1170
1319
  }
1171
1320
  }
@@ -1231,10 +1380,11 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1231
1380
  // markers yet, so it waits for them, as focus does; then a marker that isn't there, or has
1232
1381
  // stepped aside for picking, is nothing to do. The kept function, so a page that patched
1233
1382
  // focus isn't handed the marker from inside the closed root. The shell's focus listener
1234
- // lights it and brings it into view; nothing is told. A call that isn't parked reads no
1235
- // focus: it is the host's word just now, and after its card closed the focus is on the
1236
- // host's body, so this document has none yet. Only the landing of a parked one asks (P135).
1237
- const focusMark = (id) => {
1383
+ // lights it and brings it into view; nothing is told. A fresh word reads no focus: after the
1384
+ // host's card closed the focus is on the host's body, so this document has none yet. One
1385
+ // parked is asked as it lands (P135), and one this document took late (P137: late, by when
1386
+ // the host sent it: a busy page doesn't park it) is asked here: its moment has passed.
1387
+ const focusMark = (id, late) => {
1238
1388
  if (settling()) {
1239
1389
  pendingMarker = id;
1240
1390
  return;
@@ -1242,6 +1392,9 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1242
1392
  if (mode.elements || !isShown(id)) {
1243
1393
  return;
1244
1394
  }
1395
+ if (late && !focusIsHere()) {
1396
+ return;
1397
+ }
1245
1398
  for (let i = 0; i < markers.length; i += 1) {
1246
1399
  if (markers[i].id === id) {
1247
1400
  callOf(focusElement, markers[i].element, [{ preventScroll: true }]);
@@ -1431,6 +1584,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1431
1584
  const stop = () => {
1432
1585
  mode = { elements: false, text: false };
1433
1586
  hover = null;
1587
+ picked = null;
1434
1588
  reported = null;
1435
1589
  reportedBox = null;
1436
1590
  setCursor(false);
@@ -1498,6 +1652,9 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1498
1652
  return;
1499
1653
  }
1500
1654
  swallowUntil = performance.now() + 2000;
1655
+ holdSelected(anchor, { element });
1656
+ picked = followingSelection ? null : element;
1657
+ redraw();
1501
1658
  tell(M.selected, Object.assign({ anchor, rect: box(elementRect.call(element)) }, placeOf({ element })));
1502
1659
  }, HOLD_MS),
1503
1660
  };
@@ -1545,6 +1702,9 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1545
1702
  mode = { elements: false, text: mode.text };
1546
1703
  hover = null;
1547
1704
  setCursor(false);
1705
+ holdSelected(anchor, { element });
1706
+ // Its outline stays (P138), now without its name, until the host follows it or it ends.
1707
+ picked = followingSelection ? null : element;
1548
1708
  draw();
1549
1709
  tell(M.selected, Object.assign({ anchor, rect: box(elementRect.call(element)) }, placeOf({ element })));
1550
1710
  }, true);
@@ -1552,6 +1712,8 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1552
1712
  if (event.key !== "Escape") {
1553
1713
  return;
1554
1714
  }
1715
+ // The person wants out: a pick's outline goes (P138). The key is untouched by it.
1716
+ endPick();
1555
1717
  if (mode.elements) {
1556
1718
  event.preventDefault();
1557
1719
  event.stopImmediatePropagation();
@@ -1605,6 +1767,8 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1605
1767
  }
1606
1768
  reported = key;
1607
1769
  reportedBox = rect;
1770
+ holdSelected(anchor, { range: cloneRange.call(range) });
1771
+ endPick();
1608
1772
  tell(M.selected, Object.assign({ anchor, rect }, placeOf({ range })));
1609
1773
  return true;
1610
1774
  };
@@ -1615,6 +1779,10 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1615
1779
  }
1616
1780
  });
1617
1781
  window.addEventListener("scroll", () => {
1782
+ // Not while the host follows the selection (P138, Tenny's review).
1783
+ if (followingSelection) {
1784
+ return;
1785
+ }
1618
1786
  if (reported !== null) {
1619
1787
  selectionScrolled = true;
1620
1788
  settle();
@@ -1634,6 +1802,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1634
1802
  case M.start:
1635
1803
  mode = { elements: params.elements === true, text: params.text === true, hold: params.elements === "hold" };
1636
1804
  hover = null;
1805
+ picked = null;
1637
1806
  reported = null;
1638
1807
  reportedBox = null;
1639
1808
  setCursor(mode.elements);
@@ -1665,18 +1834,19 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1665
1834
  focus(String(params.id));
1666
1835
  break;
1667
1836
  case M.track:
1668
- track(params.id);
1837
+ track(params.id, params.selection === true);
1669
1838
  break;
1670
1839
  case M.clearSelection: {
1671
1840
  // The host used the person's selection (P115): it goes, and with it what the shell
1672
1841
  // held of it (a report waiting to settle, the one last told, a scroll's follow-up), so
1673
1842
  // nothing is told back: the host knows, and the selectionchange this makes finds
1674
- // nothing to report. Marks, a pick and the mode stay as they are. With nothing
1675
- // selected, nothing changes.
1843
+ // nothing to report. Marks, a pick and the mode stay as they are, but a pick's outline
1844
+ // goes (P138). With nothing selected, nothing changes.
1676
1845
  cancelLater(selectionTimer);
1677
1846
  selectionTimer = 0;
1678
1847
  reported = null;
1679
1848
  reportedBox = null;
1849
+ endPick();
1680
1850
  selectionScrolled = false;
1681
1851
  const selection = selectionOf.call(document);
1682
1852
  if (selection !== null) {
@@ -1685,10 +1855,11 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1685
1855
  break;
1686
1856
  }
1687
1857
  case M.focusMark:
1688
- focusMark(String(params.id));
1858
+ focusMark(String(params.id), wordLate(params.at, callOf(wallClock, undefined, [])));
1689
1859
  break;
1690
1860
  case M.clear:
1691
1861
  track(null);
1862
+ picked = null;
1692
1863
  pendingFocus = null;
1693
1864
  pendingMarker = null;
1694
1865
  marks = [];
@@ -23,7 +23,12 @@
23
23
  * - renews the page's sealed files' address before it ends, and gives the page the new one
24
24
  * (`porthole/files-renewed`): the page's own loads aren't its origin's (an opaque origin's never
25
25
  * are), so only the shell, on that origin, may renew it (P083);
26
- * - passes its focus to the page (the host's own move into the frame, or a Tab).
26
+ * - passes its focus to the page: at the focus event where that takes (Firefox, from the host's
27
+ * `<body>`), and on the host's word that it moved it there (`porthole/shell/focus`, P136), where
28
+ * it doesn't: a frame's move inside the focus event that made it the focused frame sets the
29
+ * shell's `activeElement` and goes no further (Chromium, WebKit), and from a focused element of
30
+ * the host's the window hears no focus event at all (Firefox). A later task's `frame.focus()`
31
+ * goes through on all of them, the frame blurred first as the failed one left it active.
27
32
  *
28
33
  * Plain ES2020, no imports and no build step, like the runtime. `__PLAN__` is its plan.
29
34
  */
@@ -25,7 +25,12 @@ import { hostOriginProblem, onNestedLabel } from "../isolation/served.js";
25
25
  * - renews the page's sealed files' address before it ends, and gives the page the new one
26
26
  * (`porthole/files-renewed`): the page's own loads aren't its origin's (an opaque origin's never
27
27
  * are), so only the shell, on that origin, may renew it (P083);
28
- * - passes its focus to the page (the host's own move into the frame, or a Tab).
28
+ * - passes its focus to the page: at the focus event where that takes (Firefox, from the host's
29
+ * `<body>`), and on the host's word that it moved it there (`porthole/shell/focus`, P136), where
30
+ * it doesn't: a frame's move inside the focus event that made it the focused frame sets the
31
+ * shell's `activeElement` and goes no further (Chromium, WebKit), and from a focused element of
32
+ * the host's the window hears no focus event at all (Firefox). A later task's `frame.focus()`
33
+ * goes through on all of them, the frame blurred first as the failed one left it active.
29
34
  *
30
35
  * Plain ES2020, no imports and no build step, like the runtime. `__PLAN__` is its plan.
31
36
  */
@@ -151,12 +156,39 @@ export const FRAME_SHELL_RUNTIME = String.raw `(() => {
151
156
  };
152
157
  requestAnimationFrame(tick);
153
158
  setInterval(look, 50);
154
- addEventListener("blur", look);
155
- addEventListener("focus", (event) => {
156
- // The host's move into the frame (or a Tab) lands here: on to the page.
157
- if (event.target === window && document.activeElement !== frame) {
159
+
160
+ // Whether the focus is on the shell's own document, not through it on the page's (P136). Firefox
161
+ // tells a move from a focused element of the host's on the document alone. The host's word needs
162
+ // this: it never moves a focus that isn't here, and a page that has it hears no blur.
163
+ let holds = false;
164
+ // Until when a word that came ahead of the focus it speaks of holds (it crosses processes too).
165
+ let wordUntil = 0;
166
+ const handOn = () => {
167
+ if (holds && document.hasFocus()) {
168
+ frame.blur();
158
169
  frame.focus();
159
170
  }
171
+ };
172
+ const came = () => {
173
+ holds = true;
174
+ if (performance.now() < wordUntil) {
175
+ wordUntil = 0;
176
+ setTimeout(handOn, 0);
177
+ }
178
+ };
179
+ document.addEventListener("focus", came);
180
+ addEventListener("blur", () => {
181
+ holds = false;
182
+ look();
183
+ });
184
+ addEventListener("focus", (event) => {
185
+ if (event.target === window) {
186
+ came();
187
+ // Where a move can go on from inside this event (P136): Firefox, from the host's <body>.
188
+ if (document.activeElement !== frame) {
189
+ frame.focus();
190
+ }
191
+ }
160
192
  look();
161
193
  });
162
194
  document.addEventListener("visibilitychange", look);
@@ -183,6 +215,14 @@ export const FRAME_SHELL_RUNTIME = String.raw `(() => {
183
215
  if (data.method === PREFIX + "look" && typeof n === "number" && n >= 0 && Math.floor(n) === n) {
184
216
  look(n);
185
217
  }
218
+ // The host's own move into the frame, done (P136): on to the page.
219
+ if (data.method === PREFIX + "focus") {
220
+ if (holds) {
221
+ handOn();
222
+ } else {
223
+ wordUntil = performance.now() + 250;
224
+ }
225
+ }
186
226
  return;
187
227
  }
188
228
  const child = frame.contentWindow;