@botiverse/porthole 0.14.1 → 0.14.3

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.
@@ -7,6 +7,7 @@ import {
7
7
  useEffect,
8
8
  useEffectEvent,
9
9
  useId,
10
+ useImperativeHandle,
10
11
  useLayoutEffect,
11
12
  useMemo,
12
13
  useRef,
@@ -15,6 +16,7 @@ import {
15
16
  type CSSProperties,
16
17
  type ReactElement,
17
18
  type ReactNode,
19
+ type Ref,
18
20
  } from "react";
19
21
  import type { AnnotateMode, AnnotationMark, AnnotationReport } from "../annotate/types.ts";
20
22
  import type {
@@ -480,6 +482,21 @@ export interface PlaceholderProps {
480
482
  readonly run: () => void;
481
483
  }
482
484
 
485
+ /**
486
+ * What a `ref` on `PortholeFrame` or `PortholeView` holds (P118): the display's few imperative
487
+ * calls, never the display itself. The same handle for the frame's whole life: its calls do
488
+ * nothing while no display runs (waiting to near the screen, stopped, a fallback drawn).
489
+ */
490
+ export interface PortholeFrameHandle {
491
+ /**
492
+ * Takes the page's text selection away, and its highlight (`annotations.clearSelection()`,
493
+ * P115), once the host has used it (a comment posted, a draft added). No `unselected` follows.
494
+ * Nothing runs: nothing happens. A config without `annotate`: `onHostError`, kind
495
+ * `"annotations"` (the core's call throws; this never does).
496
+ */
497
+ clearSelection(): void;
498
+ }
499
+
483
500
  export interface PortholeFrameProps {
484
501
  /** A preset's config. Keep it stable (module scope or memoized): a new one remounts the frame. */
485
502
  readonly config: PortholeConfig;
@@ -606,6 +623,17 @@ export interface PortholeFrameProps {
606
623
  | string
607
624
  | { readonly id: string; readonly key: string | number }
608
625
  | null;
626
+ /**
627
+ * The mark to follow (`annotations.track`, P112, P118): while it moves, `onAnnotation` gets
628
+ * `{ type: "tracked", id, rect }` at most once per animation frame, for it alone. `null` (or
629
+ * leaving it out once set) stops. By value: given when it changes, and when the display is made.
630
+ * Only a shown mark (`annotations`) is followed: another id, or a config without `annotate`, is
631
+ * `onHostError` (kind `"annotations"`), as the core's call throws. `annotations` without it
632
+ * stop the following, as the core's `show` does; it starts again once they have it back.
633
+ */
634
+ readonly trackedAnnotation?: string | null;
635
+ /** The display's imperative calls (`PortholeFrameHandle`, P118): React 19's `ref` prop. */
636
+ readonly ref?: Ref<PortholeFrameHandle>;
609
637
  /**
610
638
  * What the frame reported: a selection (an anchor and where it is), its clearing, a cancelled
611
639
  * pick, where marks are (again once a scroll settles: those that moved), a marker clicked
@@ -674,7 +702,29 @@ export function PortholeFrame(props: PortholeFrameProps): ReactElement {
674
702
  () => live.state(key),
675
703
  (): FrameState => "waiting",
676
704
  );
677
- const { eager } = props;
705
+ const { eager, ref: handleRef } = props;
706
+ // The running display (the Mount's), for the handle's calls; null while none runs.
707
+ const display = useRef<DisplayHandle | null>(null);
708
+ const running = useCallback((each: DisplayHandle | null) => {
709
+ display.current = each;
710
+ }, []);
711
+ // The props as of the last render, read when the handle is called (never during render).
712
+ const own = useRef(props);
713
+ useLayoutEffect(() => {
714
+ own.current = props;
715
+ });
716
+ useImperativeHandle(
717
+ handleRef,
718
+ (): PortholeFrameHandle => ({
719
+ clearSelection() {
720
+ const now = display.current;
721
+ if (now !== null) {
722
+ attempt(() => now.annotations.clearSelection(), own.current);
723
+ }
724
+ },
725
+ }),
726
+ [],
727
+ );
678
728
 
679
729
  useEffect(() => {
680
730
  const element = box.current;
@@ -743,6 +793,7 @@ export function PortholeFrame(props: PortholeFrameProps): ReactElement {
743
793
  key={String(props.generation ?? 0)}
744
794
  frame={props}
745
795
  page={page}
796
+ running={running}
746
797
  initialHeightPx={height}
747
798
  placeholder={placeholder}
748
799
  run={run}
@@ -757,6 +808,8 @@ export function PortholeFrame(props: PortholeFrameProps): ReactElement {
757
808
  interface MountProps {
758
809
  readonly frame: PortholeFrameProps;
759
810
  readonly page: PageState;
811
+ /** Tells the frame which display runs now (null: none), for its handle. */
812
+ readonly running: (display: DisplayHandle | null) => void;
760
813
  readonly initialHeightPx: number;
761
814
  readonly placeholder: (props: PlaceholderProps) => ReactNode;
762
815
  readonly run: () => void;
@@ -765,9 +818,19 @@ interface MountProps {
765
818
  type AnyHandler = (args: unknown, context: CallContext) => unknown;
766
819
 
767
820
  /** One running display: created when mounted, torn down when unmounted, fed as props change. */
768
- function Mount({ frame, page, initialHeightPx, placeholder, run }: MountProps): ReactElement {
821
+ function Mount({
822
+ frame,
823
+ page,
824
+ running,
825
+ initialHeightPx,
826
+ placeholder,
827
+ run,
828
+ }: MountProps): ReactElement {
769
829
  const holder = useRef<HTMLDivElement>(null);
770
830
  const handle = useRef<DisplayHandle | null>(null);
831
+ // The mark the display follows now, as the core keeps it (P118), and the prop as last seen.
832
+ const following = useRef<string | null>(null);
833
+ const asked = useRef<string | null>(null);
771
834
  const [ready, setReady] = useState(false);
772
835
  // The height this display started at: later reports don't make it start again.
773
836
  const [startHeight] = useState(initialHeightPx);
@@ -969,6 +1032,7 @@ function Mount({ frame, page, initialHeightPx, placeholder, run }: MountProps):
969
1032
  return;
970
1033
  }
971
1034
  handle.current = display;
1035
+ running(display);
972
1036
  const events: (keyof DisplayEvents)[] = [
973
1037
  "initialized",
974
1038
  "resize",
@@ -989,12 +1053,24 @@ function Mount({ frame, page, initialHeightPx, placeholder, run }: MountProps):
989
1053
  if (config.capabilities.annotate !== undefined) {
990
1054
  applyAnnotations(display, first);
991
1055
  }
1056
+ // The followed mark, once the marks are shown; without `annotate`, the core's error.
1057
+ following.current = null;
1058
+ const wanted = first.trackedAnnotation ?? null;
1059
+ asked.current = wanted;
1060
+ if (wanted !== null) {
1061
+ attempt(() => {
1062
+ display.annotations.track(wanted);
1063
+ following.current = wanted;
1064
+ }, first);
1065
+ }
992
1066
  if (first.content !== undefined) {
993
1067
  display.setContent(first.content, { partial: first.partial === true });
994
1068
  }
995
1069
  return () => {
996
1070
  display.destroy();
997
1071
  handle.current = null;
1072
+ running(null);
1073
+ following.current = null;
998
1074
  };
999
1075
  // A new display only for a new config, document or instance, or what the provider makes
1000
1076
  // displays with (its nonce, hostInfo by value, whether it locates or confirms; generation, by
@@ -1017,6 +1093,7 @@ function Mount({ frame, page, initialHeightPx, placeholder, run }: MountProps):
1017
1093
  hostInfo,
1018
1094
  locate,
1019
1095
  nonce,
1096
+ running,
1020
1097
  ]);
1021
1098
 
1022
1099
  useEffect(() => {
@@ -1057,9 +1134,34 @@ function Mount({ frame, page, initialHeightPx, placeholder, run }: MountProps):
1057
1134
  useEffect(() => {
1058
1135
  const display = handle.current;
1059
1136
  if (annotates && display !== null) {
1060
- attempt(() => display.annotations.show(annotations ?? []), latest());
1137
+ attempt(() => {
1138
+ display.annotations.show(annotations ?? []);
1139
+ // A show without the followed mark stops the following (the core's own).
1140
+ if (following.current !== null && !shown(annotations, following.current)) {
1141
+ following.current = null;
1142
+ }
1143
+ }, latest());
1061
1144
  }
1062
1145
  }, [annotates, annotations]);
1146
+ // The followed mark follows its prop (after the marks above, so the two can change together).
1147
+ const trackedAnnotation = frame.trackedAnnotation ?? null;
1148
+ useEffect(() => {
1149
+ const display = handle.current;
1150
+ const changed = trackedAnnotation !== asked.current;
1151
+ asked.current = trackedAnnotation;
1152
+ if (display === null || trackedAnnotation === following.current) {
1153
+ return;
1154
+ }
1155
+ // Its mark was taken off (the error, if any, was told when the prop said it): followed again
1156
+ // once the marks have it back.
1157
+ if (!changed && trackedAnnotation !== null && !shown(annotations, trackedAnnotation)) {
1158
+ return;
1159
+ }
1160
+ attempt(() => {
1161
+ display.annotations.track(trackedAnnotation);
1162
+ following.current = trackedAnnotation;
1163
+ }, latest());
1164
+ }, [trackedAnnotation, annotations]);
1063
1165
  useEffect(() => {
1064
1166
  const display = handle.current;
1065
1167
  const id = focusOf(latest().focusedAnnotation);
@@ -1161,6 +1263,11 @@ function useByValue<T>(value: T): T {
1161
1263
  return useMemo(() => (key === "" ? undefined : JSON.parse(key)) as T, [key]);
1162
1264
  }
1163
1265
 
1266
+ /** Whether `marks` has a mark `id`. */
1267
+ function shown(marks: readonly AnnotationMark[] | undefined, id: string): boolean {
1268
+ return (marks ?? []).some((mark) => mark.id === id);
1269
+ }
1270
+
1164
1271
  /** The id `focusedAnnotation` names, if any. */
1165
1272
  function focusOf(value: PortholeFrameProps["focusedAnnotation"]): string | null {
1166
1273
  return typeof value === "string"
@@ -13,6 +13,7 @@ export {
13
13
  PortholeFrame,
14
14
  PortholeProvider,
15
15
  type PlaceholderProps,
16
+ type PortholeFrameHandle,
16
17
  type PortholeFrameProps,
17
18
  type ConfirmActions,
18
19
  type PortholeProviderProps,
@@ -2,6 +2,8 @@ import {
2
2
  useCallback,
3
3
  useContext,
4
4
  useEffect,
5
+ useImperativeHandle,
6
+ useRef,
5
7
  useState,
6
8
  useSyncExternalStore,
7
9
  type ReactElement,
@@ -11,7 +13,12 @@ import type { ColorScheme } from "../config/schema.ts";
11
13
  import { frameFallback, type FrameFallback, type PortholeClient } from "../host/client.ts";
12
14
  import type { FrameOutcome } from "../host/types.ts";
13
15
  import type { InstanceRef } from "../isolation/types.ts";
14
- import { PortholeFrame, PortholeContext, type PortholeFrameProps } from "./frame.tsx";
16
+ import {
17
+ PortholeFrame,
18
+ PortholeContext,
19
+ type PortholeFrameHandle,
20
+ type PortholeFrameProps,
21
+ } from "./frame.tsx";
15
22
 
16
23
  /**
17
24
  * The host kit's React side (P072): a display by its kind, served or opaque as the server says
@@ -91,7 +98,17 @@ function DefaultFallback({ title, message, link }: PortholeFallbackProps): React
91
98
  export function PortholeView(props: PortholeViewProps): ReactElement | null {
92
99
  const client = useClient();
93
100
  const meta = useMeta(client);
94
- const { display, instance, content, fallback, pending, onOutcome, ...frame } = props;
101
+ const { display, instance, content, fallback, pending, onOutcome, ref, ...frame } = props;
102
+ // The view's handle (P118) is there while the view is, a fallback drawn or not: its calls reach
103
+ // the frame's display when one runs, and do nothing otherwise.
104
+ const shown = useRef<PortholeFrameHandle>(null);
105
+ useImperativeHandle(
106
+ ref,
107
+ (): PortholeFrameHandle => ({
108
+ clearSelection: () => shown.current?.clearSelection(),
109
+ }),
110
+ [],
111
+ );
95
112
  const [failed, setFailed] = useState<FrameFallback | null>(null);
96
113
  const [generation, setGeneration] = useState(0);
97
114
  const [link, setLink] = useState<string | null>(null);
@@ -145,6 +162,7 @@ export function PortholeView(props: PortholeViewProps): ReactElement | null {
145
162
  return (
146
163
  <PortholeFrame
147
164
  {...frame}
165
+ ref={shown}
148
166
  config={config}
149
167
  generation={(frame.generation ?? 0) + generation}
150
168
  {...(instance === undefined ? {} : { instance })}
@@ -1,3 +1,37 @@
1
+ /**
2
+ * Which marker a press on the shell's layer is for (P119), spliced into the runtime: the index of
3
+ * `count` boxes (`boxAt(i)`, the markers' own boxes in the viewport, the last drawn on top), or -1.
4
+ * A point on a marker's own box is that marker's (the topmost, as before). With `touch` (the last
5
+ * pointer a finger or a pen), a marker also takes the points of a 44 by 44 CSS px box centred on
6
+ * its own (its own size where it is larger): where those overlap, the marker whose own box's
7
+ * centre is nearest the point. No built-in is called (content may have patched them).
8
+ */
9
+ export const MARKER_HIT = String.raw`(count, boxAt, x, y, touch) => {
10
+ const TARGET = 44;
11
+ let best = -1;
12
+ let nearest = 0;
13
+ for (let i = count - 1; i >= 0; i -= 1) {
14
+ const rect = boxAt(i);
15
+ if (x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height) {
16
+ return i;
17
+ }
18
+ if (touch) {
19
+ const dx = x - (rect.x + rect.width / 2);
20
+ const dy = y - (rect.y + rect.height / 2);
21
+ const halfWidth = (rect.width > TARGET ? rect.width : TARGET) / 2;
22
+ const halfHeight = (rect.height > TARGET ? rect.height : TARGET) / 2;
23
+ if (dx >= -halfWidth && dx <= halfWidth && dy >= -halfHeight && dy <= halfHeight) {
24
+ const distance = dx * dx + dy * dy;
25
+ if (best === -1 || distance < nearest) {
26
+ best = i;
27
+ nearest = distance;
28
+ }
29
+ }
30
+ }
31
+ }
32
+ return best;
33
+ }`;
34
+
1
35
  /**
2
36
  * The shell's annotation runtime (P053), spliced into the shell's runtime (`__ANNOTATE__`) when
3
37
  * the config declares `annotate`, so it runs before any content does, with the DOM's own
@@ -376,7 +410,10 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
376
410
  // layer inherits from the page could reach it, so each of those is set here). A draft
377
411
  // (P107) is a comment not yet posted: its line is dashed (an element's outline too) and its
378
412
  // marker hollow, a light bubble with a dashed edge and the glyph's outline, no number.
379
- 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}";
413
+ // While the last pointer was a finger or a pen (P119), each marker takes the pointer in a
414
+ // box of at least 44 by 44 px centred on its own (an invisible ::before; 100% is its padding
415
+ // box, its border 1 px), so a tap a few px off still lands on the shell's layer.
416
+ 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}";
380
417
  // Inline and important: the content's own style sheets can't hide, shrink or move it by
381
418
  // accident. Set again at each draw, since its scripts can rewrite it (narrowing only: the
382
419
  // content can still cover the layer, or move it with its ancestors, P104).
@@ -404,6 +441,13 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
404
441
  // marker's node across draws, so a press that spans a redraw ends on the node it began on.
405
442
  let markers = [];
406
443
  const markerNodes = new Map();
444
+ // Whether the last pointer was a finger or a pen (P119): then each marker takes the pointer
445
+ // in a 44 px box around it (the markers' box has the class "touch"). Until a pointer is
446
+ // seen, whether the device has a coarse pointer at all (a phone, a tablet, a laptop with a
447
+ // touch screen): a mouse moves before it presses, and its first move turns it off.
448
+ const coarseQuery = typeof window.matchMedia === "function" ? window.matchMedia("(any-pointer: coarse)") : null;
449
+ let touch = coarseQuery !== null && coarseQuery.matches === true;
450
+ const markerHit = ${MARKER_HIT};
407
451
  const MARKER_HEIGHT = 18;
408
452
  const SVG = "http://www.w3.org/2000/svg";
409
453
  const labelOf = (count) => (count === 0 ? "" : count > 99 ? "99+" : String(count));
@@ -714,6 +758,9 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
714
758
  if (boxes.style.textContent !== STYLE) {
715
759
  boxes.style.textContent = STYLE;
716
760
  }
761
+ if (boxes.markers.className !== (touch ? "touch" : "")) {
762
+ boxes.markers.className = touch ? "touch" : "";
763
+ }
717
764
  return boxes;
718
765
  };
719
766
  const draw = () => {
@@ -1118,14 +1165,11 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
1118
1165
  const rect = callOf(elementRect, element, []);
1119
1166
  return { x: callOf(leftOf, rect, []), y: callOf(topOf, rect, []), width: callOf(widthOf, rect, []), height: callOf(heightOf, rect, []) };
1120
1167
  };
1168
+ // The marker a press at (x, y) is for: on a marker's own box, that one; after a finger or a
1169
+ // pen, also within the 44 px box around one, the nearest (P119).
1121
1170
  const markerAt = (x, y) => {
1122
- for (let i = markers.length - 1; i >= 0; i -= 1) {
1123
- const rect = markerBox(markers[i].element);
1124
- if (x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height) {
1125
- return markers[i];
1126
- }
1127
- }
1128
- return null;
1171
+ const at = markerHit(markers.length, (i) => markerBox(markers[i].element), x, y, touch);
1172
+ return at < 0 ? null : markers[at];
1129
1173
  };
1130
1174
  const isShown = (id) => {
1131
1175
  for (let i = 0; i < marks.length; i += 1) {
@@ -1170,6 +1214,28 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
1170
1214
  const CANCELLED = { mousedown: 1, mouseup: 1, click: 1, dblclick: 1, auxclick: 1, contextmenu: 1 };
1171
1215
  // The pointer on a marker (1), or leaving the layer for the content (0).
1172
1216
  const HOVERS = { pointerover: 1, pointermove: 1, pointerout: 0, pointerleave: 0 };
1217
+ // Which pointer the person uses (P119), from each real one that goes down or moves, before
1218
+ // the marker's listener (which stops a press on the layer): a finger or a pen gives every
1219
+ // marker its 44 px box, a mouse takes it away. A mouse moves before it presses, so its press
1220
+ // is hit-tested on the markers' own boxes. Read through the platform's getter, kept now; an
1221
+ // event the page makes up (isTrusted is false) changes nothing.
1222
+ const pointerTypeOf = getterOf(typeof PointerEvent === "function" ? PointerEvent.prototype : null, "pointerType");
1223
+ const onPointer = (event) => {
1224
+ if (!event.isTrusted || pointerTypeOf === null) {
1225
+ return;
1226
+ }
1227
+ const kind = callOf(pointerTypeOf, event, []);
1228
+ const next = kind === "touch" || kind === "pen";
1229
+ if (next !== touch) {
1230
+ touch = next;
1231
+ if (boxes !== null) {
1232
+ boxes.markers.className = touch ? "touch" : "";
1233
+ }
1234
+ }
1235
+ };
1236
+ for (const type of ["pointerdown", "pointermove"]) {
1237
+ window.addEventListener(type, onPointer, true);
1238
+ }
1173
1239
  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"]) {
1174
1240
  window.addEventListener(type, onMarker, true);
1175
1241
  }