@botiverse/porthole 0.14.8 → 0.14.10
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.
- package/CHANGELOG.md +46 -0
- package/dist/annotate/types.d.ts +33 -2
- package/dist/annotate/validate.d.ts +6 -1
- package/dist/annotate/validate.js +31 -3
- package/dist/bridge/bridge.d.ts +6 -0
- package/dist/bridge/bridge.js +5 -0
- package/dist/check/page-source.js +1 -1
- package/dist/config/capabilities.d.ts +16 -2
- package/dist/config/capabilities.js +8 -0
- package/dist/conformance/harness-source.js +1 -1
- package/dist/find/types.d.ts +83 -0
- package/dist/find/types.js +29 -0
- package/dist/find/validate.d.ts +17 -0
- package/dist/find/validate.js +82 -0
- package/dist/guide/guide.js +2 -1
- package/dist/host/create-host.js +219 -3
- package/dist/host/pass-on.js +3 -2
- package/dist/host/types.d.ts +54 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +4 -1
- package/dist/react/frame.d.ts +45 -1
- package/dist/react/frame.js +107 -3
- package/dist/react/view.js +2 -0
- package/dist/runtime/contract.d.ts +1 -1
- package/dist/runtime/contract.js +1 -1
- package/dist/shell/annotate.d.ts +5 -1
- package/dist/shell/annotate.js +41 -107
- package/dist/shell/document.d.ts +3 -3
- package/dist/shell/document.js +5 -3
- package/dist/shell/find.d.ts +82 -0
- package/dist/shell/find.js +725 -0
- package/dist/shell/runtime.js +4 -1
- package/dist/shell/text.d.ts +17 -0
- package/dist/shell/text.js +140 -0
- package/docs/ADOPTING.md +112 -0
- package/docs/ARCHITECTURE.md +10 -3
- package/docs/DECISIONS.md +284 -1
- package/docs/DESIGN.md +48 -2
- package/docs/THREAT_MODEL.md +61 -5
- package/docs/insertion-entry-points.md +17 -0
- package/package.json +1 -1
- package/src/annotate/types.ts +43 -4
- package/src/annotate/validate.ts +34 -3
- package/src/bridge/bridge.ts +7 -0
- package/src/config/capabilities.ts +13 -1
- package/src/find/types.ts +86 -0
- package/src/find/validate.ts +101 -0
- package/src/guide/guide.ts +3 -1
- package/src/host/create-host.ts +235 -3
- package/src/host/pass-on.ts +3 -2
- package/src/host/types.ts +55 -1
- package/src/index.ts +14 -1
- package/src/react/frame.tsx +162 -2
- package/src/react/view.tsx +3 -0
- package/src/runtime/contract.ts +1 -1
- package/src/shell/annotate.ts +42 -107
- package/src/shell/document.ts +5 -3
- package/src/shell/find.ts +731 -0
- package/src/shell/runtime.ts +4 -1
- package/src/shell/text.ts +140 -0
package/src/react/frame.tsx
CHANGED
|
@@ -19,6 +19,8 @@ import {
|
|
|
19
19
|
type Ref,
|
|
20
20
|
} from "react";
|
|
21
21
|
import type { AnnotateMode, AnnotationMark, AnnotationReport } from "../annotate/types.ts";
|
|
22
|
+
import type { FindOptions, FindResult } from "../find/types.ts";
|
|
23
|
+
import { blankQuery, checkFindQuery } from "../find/validate.ts";
|
|
22
24
|
import type {
|
|
23
25
|
CallContext,
|
|
24
26
|
CapabilityHandler,
|
|
@@ -495,6 +497,68 @@ export interface PortholeFrameHandle {
|
|
|
495
497
|
* `"annotations"` (the core's call throws; this never does).
|
|
496
498
|
*/
|
|
497
499
|
clearSelection(): void;
|
|
500
|
+
/**
|
|
501
|
+
* Finds `query` in the frame and highlights it (`find.query`, P131): where the matches stand,
|
|
502
|
+
* or `"unsupported"` (a config without `find`, an older shell, no answer in time, a later
|
|
503
|
+
* query superseded it, or no display running now). The query is kept for the content the frame
|
|
504
|
+
* shows now only: a display made later for the same content (the frame neared the screen again)
|
|
505
|
+
* finds it again and tells `onFindUpdated`; other content (another `content` that doesn't
|
|
506
|
+
* continue it, `document`, `instance`, `config`, `declaration` or `viewer`, a new `generation`)
|
|
507
|
+
* forgets it, and nothing is found there until `find` is called again (clear your find bar
|
|
508
|
+
* then). A query too long, or options that aren't these: `onHostError`, kind `"find"`, and
|
|
509
|
+
* `"unsupported"` (never a rejection). An empty query clears.
|
|
510
|
+
*/
|
|
511
|
+
find(query: string, options?: FindOptions): Promise<FindResult | "unsupported">;
|
|
512
|
+
/** Removes the highlights and forgets the query (`find.clear`): the host's find bar closed. */
|
|
513
|
+
clearFind(): void;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* The query a frame's handle last found (P131), and what the frame showed when it was asked: a
|
|
518
|
+
* display made after it finds it again only where the frame still shows that (`sameFindScope`).
|
|
519
|
+
*/
|
|
520
|
+
type FindShown = {
|
|
521
|
+
readonly query: string;
|
|
522
|
+
readonly caseSensitive: boolean;
|
|
523
|
+
readonly scope: FindScope;
|
|
524
|
+
} | null;
|
|
525
|
+
|
|
526
|
+
/** What a frame shows, as its handle's query holds it (P131). */
|
|
527
|
+
export interface FindScope {
|
|
528
|
+
/** The config, instance (its version too), declaration, viewer and generation, by value. */
|
|
529
|
+
readonly shows: string;
|
|
530
|
+
readonly document: string | undefined;
|
|
531
|
+
readonly content: string | undefined;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/** What the frame shows with these props, for its handle's query (P131). */
|
|
535
|
+
export function findScopeOf(props: PortholeFrameProps): FindScope {
|
|
536
|
+
return {
|
|
537
|
+
shows: JSON.stringify([
|
|
538
|
+
props.config,
|
|
539
|
+
props.instance ?? null,
|
|
540
|
+
props.declaration ?? null,
|
|
541
|
+
props.viewer ?? null,
|
|
542
|
+
props.generation ?? 0,
|
|
543
|
+
]),
|
|
544
|
+
document: props.document,
|
|
545
|
+
content: props.content,
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Whether a frame showing `now` still shows what a query was asked of (`asked`, P131): the same
|
|
551
|
+
* config, instance, declaration, viewer, generation and document, and the content as it was or
|
|
552
|
+
* continued (more of a streamed page; asked before any content: whatever came). Otherwise the
|
|
553
|
+
* query was the last page's, and a new one is never searched with it.
|
|
554
|
+
*/
|
|
555
|
+
export function sameFindScope(asked: FindScope, now: FindScope): boolean {
|
|
556
|
+
return (
|
|
557
|
+
asked.shows === now.shows &&
|
|
558
|
+
asked.document === now.document &&
|
|
559
|
+
(asked.content === undefined ||
|
|
560
|
+
(now.content !== undefined && now.content.startsWith(asked.content)))
|
|
561
|
+
);
|
|
498
562
|
}
|
|
499
563
|
|
|
500
564
|
export interface PortholeFrameProps {
|
|
@@ -626,7 +690,8 @@ export interface PortholeFrameProps {
|
|
|
626
690
|
| null;
|
|
627
691
|
/**
|
|
628
692
|
* The mark to follow (`annotations.track`, P112, P118): while it moves, `onAnnotation` gets
|
|
629
|
-
* `{ type: "tracked", id, rect }` at most once per animation frame, for it alone
|
|
693
|
+
* `{ type: "tracked", id, rect }` at most once per animation frame, for it alone (from a newer
|
|
694
|
+
* shell with its `documentRect` and the document's `scroll`, P132). `null` (or
|
|
630
695
|
* leaving it out once set) stops. By value: given when it changes, and when the display is made.
|
|
631
696
|
* Only a shown mark (`annotations`) is followed: another id, or a config without `annotate`, is
|
|
632
697
|
* `onHostError` (kind `"annotations"`), as the core's call throws. `annotations` without it
|
|
@@ -641,6 +706,18 @@ export interface PortholeFrameProps {
|
|
|
641
706
|
* (`activated`). The content's word: UI hints, never permissions.
|
|
642
707
|
*/
|
|
643
708
|
readonly onAnnotation?: (report: AnnotationReport) => void;
|
|
709
|
+
/**
|
|
710
|
+
* The person pressed Cmd+F or Ctrl+F in the frame (`findRequested`, P131): open the host's find
|
|
711
|
+
* bar. Given, it makes the frame's shortcut the host's (`find.takeShortcut(true)`): the
|
|
712
|
+
* browser's own find doesn't open there. The content's word (it can say it, or keep the key):
|
|
713
|
+
* open the bar, nothing more. Where the config declares `find`.
|
|
714
|
+
*/
|
|
715
|
+
readonly onFindRequested?: () => void;
|
|
716
|
+
/**
|
|
717
|
+
* The matches of the query shown changed without the host asking (`findUpdated`, P131): the
|
|
718
|
+
* content changed, or a display made later found the handle's query again.
|
|
719
|
+
*/
|
|
720
|
+
readonly onFindUpdated?: (result: FindResult) => void;
|
|
644
721
|
/** Draws the placeholder instead of the default one. */
|
|
645
722
|
readonly renderPlaceholder?: (props: PlaceholderProps) => ReactNode;
|
|
646
723
|
readonly className?: string;
|
|
@@ -714,6 +791,18 @@ export function PortholeFrame(props: PortholeFrameProps): ReactElement {
|
|
|
714
791
|
useLayoutEffect(() => {
|
|
715
792
|
own.current = props;
|
|
716
793
|
});
|
|
794
|
+
// The query the handle last found (P131): a display made after it, for the same content, finds
|
|
795
|
+
// it again.
|
|
796
|
+
const findShown = useRef<FindShown>(null);
|
|
797
|
+
// The query kept for what a display shows (`scope`), else forgotten: null.
|
|
798
|
+
const findFor = useCallback((scope: FindScope): FindShown => {
|
|
799
|
+
const kept = findShown.current;
|
|
800
|
+
if (kept !== null && !sameFindScope(kept.scope, scope)) {
|
|
801
|
+
findShown.current = null;
|
|
802
|
+
return null;
|
|
803
|
+
}
|
|
804
|
+
return kept;
|
|
805
|
+
}, []);
|
|
717
806
|
useImperativeHandle(
|
|
718
807
|
handleRef,
|
|
719
808
|
(): PortholeFrameHandle => ({
|
|
@@ -723,6 +812,32 @@ export function PortholeFrame(props: PortholeFrameProps): ReactElement {
|
|
|
723
812
|
attempt(() => now.annotations.clearSelection(), own.current);
|
|
724
813
|
}
|
|
725
814
|
},
|
|
815
|
+
find(query, options) {
|
|
816
|
+
try {
|
|
817
|
+
checkFindQuery(query, options);
|
|
818
|
+
} catch (error) {
|
|
819
|
+
hostFailed(own.current, error, "find");
|
|
820
|
+
return Promise.resolve("unsupported");
|
|
821
|
+
}
|
|
822
|
+
findShown.current = blankQuery(query)
|
|
823
|
+
? null
|
|
824
|
+
: {
|
|
825
|
+
query,
|
|
826
|
+
caseSensitive: options?.caseSensitive === true,
|
|
827
|
+
scope: findScopeOf(own.current),
|
|
828
|
+
};
|
|
829
|
+
const now = display.current;
|
|
830
|
+
return now === null
|
|
831
|
+
? Promise.resolve("unsupported")
|
|
832
|
+
: now.find.query(query, options).catch((error: unknown) => {
|
|
833
|
+
hostFailed(own.current, error, "find");
|
|
834
|
+
return "unsupported" as const;
|
|
835
|
+
});
|
|
836
|
+
},
|
|
837
|
+
clearFind() {
|
|
838
|
+
findShown.current = null;
|
|
839
|
+
display.current?.find.clear();
|
|
840
|
+
},
|
|
726
841
|
}),
|
|
727
842
|
[],
|
|
728
843
|
);
|
|
@@ -795,6 +910,7 @@ export function PortholeFrame(props: PortholeFrameProps): ReactElement {
|
|
|
795
910
|
frame={props}
|
|
796
911
|
page={page}
|
|
797
912
|
running={running}
|
|
913
|
+
findFor={findFor}
|
|
798
914
|
initialHeightPx={height}
|
|
799
915
|
placeholder={placeholder}
|
|
800
916
|
run={run}
|
|
@@ -811,6 +927,11 @@ interface MountProps {
|
|
|
811
927
|
readonly page: PageState;
|
|
812
928
|
/** Tells the frame which display runs now (null: none), for its handle. */
|
|
813
929
|
readonly running: (display: DisplayHandle | null) => void;
|
|
930
|
+
/**
|
|
931
|
+
* The query the frame's handle last found, where it was asked of what `scope` shows (P131): a
|
|
932
|
+
* new display for its content finds it again. Otherwise it is forgotten, and null.
|
|
933
|
+
*/
|
|
934
|
+
readonly findFor: (scope: FindScope) => FindShown;
|
|
814
935
|
readonly initialHeightPx: number;
|
|
815
936
|
readonly placeholder: (props: PlaceholderProps) => ReactNode;
|
|
816
937
|
readonly run: () => void;
|
|
@@ -823,6 +944,7 @@ function Mount({
|
|
|
823
944
|
frame,
|
|
824
945
|
page,
|
|
825
946
|
running,
|
|
947
|
+
findFor,
|
|
826
948
|
initialHeightPx,
|
|
827
949
|
placeholder,
|
|
828
950
|
run,
|
|
@@ -919,6 +1041,12 @@ function Mount({
|
|
|
919
1041
|
case "annotation":
|
|
920
1042
|
frame.onAnnotation?.(detail as DisplayEvents["annotation"]);
|
|
921
1043
|
return;
|
|
1044
|
+
case "findRequested":
|
|
1045
|
+
frame.onFindRequested?.();
|
|
1046
|
+
return;
|
|
1047
|
+
case "findUpdated":
|
|
1048
|
+
frame.onFindUpdated?.(detail as DisplayEvents["findUpdated"]);
|
|
1049
|
+
return;
|
|
922
1050
|
}
|
|
923
1051
|
},
|
|
924
1052
|
);
|
|
@@ -1045,6 +1173,8 @@ function Mount({
|
|
|
1045
1173
|
"focusTaken",
|
|
1046
1174
|
"outcome",
|
|
1047
1175
|
"annotation",
|
|
1176
|
+
"findRequested",
|
|
1177
|
+
"findUpdated",
|
|
1048
1178
|
"contentError",
|
|
1049
1179
|
"hostError",
|
|
1050
1180
|
];
|
|
@@ -1064,6 +1194,22 @@ function Mount({
|
|
|
1064
1194
|
following.current = wanted;
|
|
1065
1195
|
}, first);
|
|
1066
1196
|
}
|
|
1197
|
+
// Find (P131): the shortcut is the host's where it listens for it; the handle's query, found
|
|
1198
|
+
// again in this display where it shows what the query was asked of (its numbers told as an
|
|
1199
|
+
// update: nobody awaits them), else forgotten: another page is never searched with it.
|
|
1200
|
+
if (first.onFindRequested !== undefined) {
|
|
1201
|
+
display.find.takeShortcut(true);
|
|
1202
|
+
}
|
|
1203
|
+
const shownQuery = findFor(findScopeOf(first));
|
|
1204
|
+
if (shownQuery !== null) {
|
|
1205
|
+
void display.find
|
|
1206
|
+
.query(shownQuery.query, { caseSensitive: shownQuery.caseSensitive })
|
|
1207
|
+
.then((result) => {
|
|
1208
|
+
if (result !== "unsupported" && findFor(findScopeOf(latest())) === shownQuery) {
|
|
1209
|
+
latest().onFindUpdated?.(result);
|
|
1210
|
+
}
|
|
1211
|
+
});
|
|
1212
|
+
}
|
|
1067
1213
|
if (first.content !== undefined) {
|
|
1068
1214
|
display.setContent(first.content, { partial: first.partial === true });
|
|
1069
1215
|
}
|
|
@@ -1095,13 +1241,17 @@ function Mount({
|
|
|
1095
1241
|
locate,
|
|
1096
1242
|
nonce,
|
|
1097
1243
|
running,
|
|
1244
|
+
findFor,
|
|
1098
1245
|
]);
|
|
1099
1246
|
|
|
1100
1247
|
useEffect(() => {
|
|
1101
1248
|
if (content !== undefined) {
|
|
1102
1249
|
handle.current?.setContent(content, { partial: partial === true });
|
|
1103
1250
|
}
|
|
1104
|
-
|
|
1251
|
+
// Other content: the handle's query was the last one's, forgotten (the display forgot it too,
|
|
1252
|
+
// P131).
|
|
1253
|
+
findFor(findScopeOf(latest()));
|
|
1254
|
+
}, [content, partial, findFor]);
|
|
1105
1255
|
// Annotations follow their props (a new display gets them all when it is made, above).
|
|
1106
1256
|
const annotates = config.capabilities.annotate !== undefined;
|
|
1107
1257
|
const { annotating } = frame;
|
|
@@ -1170,6 +1320,16 @@ function Mount({
|
|
|
1170
1320
|
attempt(() => display.annotations.focus(id), latest());
|
|
1171
1321
|
}
|
|
1172
1322
|
}, [annotates, focusToken]);
|
|
1323
|
+
// The frame's shortcut is the host's while it listens for it (P131).
|
|
1324
|
+
const takesFind = frame.onFindRequested !== undefined;
|
|
1325
|
+
const [mountTakesFind] = useState(takesFind);
|
|
1326
|
+
const findFlipped = useRef(false);
|
|
1327
|
+
useEffect(() => {
|
|
1328
|
+
if (findFlipped.current || takesFind !== mountTakesFind) {
|
|
1329
|
+
findFlipped.current = true;
|
|
1330
|
+
handle.current?.find.takeShortcut(takesFind);
|
|
1331
|
+
}
|
|
1332
|
+
}, [takesFind, mountTakesFind]);
|
|
1173
1333
|
// A flip or a restyle after the display was made: its first scheme and values went with it.
|
|
1174
1334
|
const [mountScheme] = useState(scheme);
|
|
1175
1335
|
const flipped = useRef(false);
|
package/src/react/view.tsx
CHANGED
|
@@ -253,6 +253,9 @@ function InstanceView(props: PortholeViewProps): ReactElement | null {
|
|
|
253
253
|
ref,
|
|
254
254
|
(): PortholeFrameHandle => ({
|
|
255
255
|
clearSelection: () => shown.current?.clearSelection(),
|
|
256
|
+
find: (query, options) =>
|
|
257
|
+
shown.current?.find(query, options) ?? Promise.resolve("unsupported"),
|
|
258
|
+
clearFind: () => shown.current?.clearFind(),
|
|
256
259
|
}),
|
|
257
260
|
[],
|
|
258
261
|
);
|
package/src/runtime/contract.ts
CHANGED
|
@@ -171,7 +171,7 @@ export const RUNTIME_HOST_FUNCTIONS: readonly CapabilityName[] = [
|
|
|
171
171
|
|
|
172
172
|
/**
|
|
173
173
|
* Where a host function sits in the runtime: its capability and the methods it serves. Null for
|
|
174
|
-
* the shell's own (`resize`, `reportError`, `annotate`), which content never calls by name.
|
|
174
|
+
* the shell's own (`resize`, `reportError`, `annotate`, `find`), which content never calls by name.
|
|
175
175
|
*/
|
|
176
176
|
export function familyOf(
|
|
177
177
|
serves: CapabilityName,
|
package/src/shell/annotate.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { TEXT_INDEX } from "./text.ts";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Which marker a press on the shell's layer is for (P119), spliced into the runtime: the index of
|
|
3
5
|
* `count` boxes (`boxAt(i)`, the markers' own boxes in the viewport, the last drawn on top), or -1.
|
|
@@ -55,7 +57,11 @@ export const MARKER_HIT = String.raw`(count, boxAt, x, y, touch) => {
|
|
|
55
57
|
* bubble with no number, and has a marker with or without a count;
|
|
56
58
|
* - following: while a mark has a marker, once a scroll, a resize or a change of the content
|
|
57
59
|
* settles, the marks that moved since the host was last told are reported, at most once a
|
|
58
|
-
* second, so the host's own UI beside a mark can follow
|
|
60
|
+
* second, so the host's own UI beside a mark can follow;
|
|
61
|
+
* - places in the document (P132): each result's box also in the document's coordinates
|
|
62
|
+
* (`documentRect`), with how far the document was scrolled as it was read (`scroll`), and the
|
|
63
|
+
* followed mark's (`tracked`) too, so a host places marks scrolled out of the frame, and keeps
|
|
64
|
+
* them placed as the document scrolls, from one report and the followed mark's scroll.
|
|
59
65
|
*
|
|
60
66
|
* It shares the realm with the content, which can interfere with all of it: the host takes its
|
|
61
67
|
* reports as UI hints only.
|
|
@@ -107,101 +113,7 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
|
|
|
107
113
|
const squash = (text) => String(text).replace(/\s+/g, " ").trim();
|
|
108
114
|
const box = (rect) => ({ x: rect.left, y: rect.top, width: rect.width, height: rect.height });
|
|
109
115
|
|
|
110
|
-
|
|
111
|
-
const SKIPPED = { script: 1, style: 1, noscript: 1, template: 1 };
|
|
112
|
-
const textIndex = () => {
|
|
113
|
-
const nodes = [];
|
|
114
|
-
const starts = [];
|
|
115
|
-
const at = new Map();
|
|
116
|
-
let text = "";
|
|
117
|
-
const body = document.body;
|
|
118
|
-
if (body) {
|
|
119
|
-
const walker = createTreeWalker.call(document, body, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, {
|
|
120
|
-
acceptNode: (node) => (node.nodeType === 1 ? (SKIPPED[node.localName] ? NodeFilter.FILTER_REJECT : NodeFilter.FILTER_SKIP) : NodeFilter.FILTER_ACCEPT),
|
|
121
|
-
});
|
|
122
|
-
for (let node = walker.nextNode(); node !== null; node = walker.nextNode()) {
|
|
123
|
-
at.set(node, nodes.length);
|
|
124
|
-
nodes.push(node);
|
|
125
|
-
starts.push(text.length);
|
|
126
|
-
text += node.nodeValue;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
return { nodes, starts, at, text };
|
|
130
|
-
};
|
|
131
|
-
// A boundary point as an offset into the text: in a text node, where it is; elsewhere, the
|
|
132
|
-
// first text at or after it.
|
|
133
|
-
const offsetOf = (index, container, offset) => {
|
|
134
|
-
if (container.nodeType === 3 && index.at.has(container)) {
|
|
135
|
-
return index.starts[index.at.get(container)] + Math.min(offset, container.nodeValue.length);
|
|
136
|
-
}
|
|
137
|
-
const point = createRange.call(document);
|
|
138
|
-
try {
|
|
139
|
-
point.setStart(container, offset);
|
|
140
|
-
} catch (_) {
|
|
141
|
-
return index.text.length;
|
|
142
|
-
}
|
|
143
|
-
point.collapse(true);
|
|
144
|
-
let low = 0;
|
|
145
|
-
let high = index.nodes.length;
|
|
146
|
-
while (low < high) {
|
|
147
|
-
const middle = (low + high) >> 1;
|
|
148
|
-
if (point.comparePoint(index.nodes[middle], 0) >= 0) {
|
|
149
|
-
high = middle;
|
|
150
|
-
} else {
|
|
151
|
-
low = middle + 1;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return low < index.nodes.length ? index.starts[low] : index.text.length;
|
|
155
|
-
};
|
|
156
|
-
const pointAt = (index, at, end) => {
|
|
157
|
-
let low = 0;
|
|
158
|
-
let high = index.nodes.length - 1;
|
|
159
|
-
let found = -1;
|
|
160
|
-
while (low <= high) {
|
|
161
|
-
const middle = (low + high) >> 1;
|
|
162
|
-
if (index.starts[middle] <= at) {
|
|
163
|
-
found = middle;
|
|
164
|
-
low = middle + 1;
|
|
165
|
-
} else {
|
|
166
|
-
high = middle - 1;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
if (found === -1) {
|
|
170
|
-
return null;
|
|
171
|
-
}
|
|
172
|
-
let node = index.nodes[found];
|
|
173
|
-
let offset = at - index.starts[found];
|
|
174
|
-
if (end) {
|
|
175
|
-
// An end at a node's very start belongs to the text before it.
|
|
176
|
-
while (offset === 0 && found > 0) {
|
|
177
|
-
found -= 1;
|
|
178
|
-
node = index.nodes[found];
|
|
179
|
-
offset = node.nodeValue.length;
|
|
180
|
-
}
|
|
181
|
-
} else {
|
|
182
|
-
while (offset >= node.nodeValue.length && found + 1 < index.nodes.length) {
|
|
183
|
-
found += 1;
|
|
184
|
-
node = index.nodes[found];
|
|
185
|
-
offset = 0;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return { node, offset: Math.min(offset, node.nodeValue.length) };
|
|
189
|
-
};
|
|
190
|
-
const rangeAt = (index, start, end) => {
|
|
191
|
-
const from = pointAt(index, start, false);
|
|
192
|
-
const to = pointAt(index, end, true);
|
|
193
|
-
if (from === null || to === null) {
|
|
194
|
-
return null;
|
|
195
|
-
}
|
|
196
|
-
const range = createRange.call(document);
|
|
197
|
-
try {
|
|
198
|
-
range.setStart(from.node, from.offset);
|
|
199
|
-
range.setEnd(to.node, to.offset);
|
|
200
|
-
} catch (_) {
|
|
201
|
-
return null;
|
|
202
|
-
}
|
|
203
|
-
return range;
|
|
204
|
-
};
|
|
116
|
+
${TEXT_INDEX}
|
|
205
117
|
const spanOf = (index, node) => {
|
|
206
118
|
const range = createRange.call(document);
|
|
207
119
|
range.selectNodeContents(node);
|
|
@@ -398,6 +310,19 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
|
|
|
398
310
|
return null;
|
|
399
311
|
};
|
|
400
312
|
const rectOf = (found) => (found.range ? rangeRect.call(found.range) : elementRect.call(found.element));
|
|
313
|
+
// How far the document is scrolled (P132): the top left of its root's box from the
|
|
314
|
+
// viewport's, negated, read with the DOM's own getBoundingClientRect (taken before content
|
|
315
|
+
// runs), never window.scrollY, which the content can replace. 0 - n: no -0 for a top at 0.
|
|
316
|
+
const scrollOf = () => {
|
|
317
|
+
const root = document.documentElement;
|
|
318
|
+
if (!root) {
|
|
319
|
+
return { x: 0, y: 0 };
|
|
320
|
+
}
|
|
321
|
+
const rect = elementRect.call(root);
|
|
322
|
+
return { x: 0 - rect.left, y: 0 - rect.top };
|
|
323
|
+
};
|
|
324
|
+
// A box in the viewport, in the document (P132): where it is as the document scrolls.
|
|
325
|
+
const inDocument = (rect, scroll) => ({ x: rect.x + scroll.x, y: rect.y + scroll.y, width: rect.width, height: rect.height });
|
|
401
326
|
|
|
402
327
|
// The shell's own layer, over the content: marks, the outline of what would be picked.
|
|
403
328
|
let layer = null;
|
|
@@ -891,7 +816,10 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
|
|
|
891
816
|
if (apart(trackedBox, rect) && now - trackedAt >= 16) {
|
|
892
817
|
trackedBox = rect;
|
|
893
818
|
trackedAt = now;
|
|
894
|
-
|
|
819
|
+
// Its box moves as the document scrolls, inside the frame or not: each one tells the
|
|
820
|
+
// host the scroll, for every mark it places by its place in the document (P132).
|
|
821
|
+
const scroll = scrollOf();
|
|
822
|
+
tell(M.tracked, { id: tracking, rect, documentRect: inDocument(rect, scroll), scroll });
|
|
895
823
|
}
|
|
896
824
|
}
|
|
897
825
|
trackFrame = nextFrame(trackTick);
|
|
@@ -935,12 +863,15 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
|
|
|
935
863
|
placed.forEach((each, at) => orders.set(each.mark, at));
|
|
936
864
|
return orders;
|
|
937
865
|
};
|
|
938
|
-
|
|
866
|
+
// A mark's result: its box in the viewport, and in the document (P132) by scroll, the
|
|
867
|
+
// report's one reading of it.
|
|
868
|
+
const resultOf = (mark, orders, scroll) => {
|
|
939
869
|
if (mark.found === null) {
|
|
940
870
|
return { id: mark.id, state: "orphaned" };
|
|
941
871
|
}
|
|
942
872
|
const order = (orders || ordersOf()).get(mark);
|
|
943
|
-
|
|
873
|
+
const rect = box(rectOf(mark.found));
|
|
874
|
+
return Object.assign({ id: mark.id, state: "attached", rect, documentRect: inDocument(rect, scroll) }, order === undefined ? {} : { order });
|
|
944
875
|
};
|
|
945
876
|
// Two boxes more than 0.5 px apart on any side (or one of them unknown).
|
|
946
877
|
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;
|
|
@@ -952,17 +883,18 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
|
|
|
952
883
|
}
|
|
953
884
|
return result.state === "attached" && (apart(told.rect, result.rect) || told.order !== result.order);
|
|
954
885
|
};
|
|
955
|
-
|
|
886
|
+
// With the scroll the results' document boxes were read at (P132).
|
|
887
|
+
const report = (pairs, scroll) => {
|
|
956
888
|
if (pairs.length > 0) {
|
|
957
889
|
for (const pair of pairs) {
|
|
958
890
|
pair.mark.told = pair.result;
|
|
959
891
|
}
|
|
960
|
-
tell(M.anchored, { results: pairs.map((pair) => pair.result) });
|
|
892
|
+
tell(M.anchored, { results: pairs.map((pair) => pair.result), scroll });
|
|
961
893
|
}
|
|
962
894
|
};
|
|
963
|
-
const resultsOf = (only) => {
|
|
895
|
+
const resultsOf = (only, scroll) => {
|
|
964
896
|
const orders = ordersOf();
|
|
965
|
-
return marks.map((mark) => ({ mark, result: resultOf(mark, orders) })).filter((pair) => !only || moved(pair.mark, pair.result));
|
|
897
|
+
return marks.map((mark) => ({ mark, result: resultOf(mark, orders, scroll) })).filter((pair) => !only || moved(pair.mark, pair.result));
|
|
966
898
|
};
|
|
967
899
|
// Finds every mark again; tells the host every result (after show, or when a state changed),
|
|
968
900
|
// only the marks that moved (once the content is quiet), or nothing.
|
|
@@ -984,7 +916,8 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
|
|
|
984
916
|
mark.state = state;
|
|
985
917
|
}
|
|
986
918
|
if (all || changed) {
|
|
987
|
-
|
|
919
|
+
const scroll = scrollOf();
|
|
920
|
+
report(resultsOf(false, scroll), scroll);
|
|
988
921
|
} else if (quiet) {
|
|
989
922
|
follow();
|
|
990
923
|
}
|
|
@@ -1069,9 +1002,10 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
|
|
|
1069
1002
|
const sendFollow = () => {
|
|
1070
1003
|
let sent = 0;
|
|
1071
1004
|
if (marks.length > 0 && !settling() && counted()) {
|
|
1072
|
-
const
|
|
1005
|
+
const scroll = scrollOf();
|
|
1006
|
+
const pairs = resultsOf(true, scroll);
|
|
1073
1007
|
if (pairs.length > 0) {
|
|
1074
|
-
report(pairs);
|
|
1008
|
+
report(pairs, scroll);
|
|
1075
1009
|
sent += 1;
|
|
1076
1010
|
}
|
|
1077
1011
|
}
|
|
@@ -1135,7 +1069,8 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
|
|
|
1135
1069
|
setTimeout(redraw, 1850);
|
|
1136
1070
|
}
|
|
1137
1071
|
draw();
|
|
1138
|
-
|
|
1072
|
+
const scroll = scrollOf();
|
|
1073
|
+
report([{ mark, result: resultOf(mark, null, scroll) }], scroll);
|
|
1139
1074
|
};
|
|
1140
1075
|
|
|
1141
1076
|
// A marker's click (P104). The layer takes no pointer but on its markers, so a press whose
|
package/src/shell/document.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { hostOriginProblem } from "../isolation/served.ts";
|
|
2
2
|
import { ANNOTATE_RUNTIME } from "./annotate.ts";
|
|
3
|
+
import { FIND_RUNTIME } from "./find.ts";
|
|
3
4
|
import { LEGACY_GLOBALS_RUNTIME } from "./legacy.ts";
|
|
4
5
|
import { SHELL_RUNTIME } from "./runtime.ts";
|
|
5
6
|
import { USE_RUNTIME } from "./use.ts";
|
|
@@ -133,14 +134,15 @@ body {
|
|
|
133
134
|
* A given `document` travels in the runtime's plan, as data.
|
|
134
135
|
*/
|
|
135
136
|
/**
|
|
136
|
-
* The shell's runtime for a plan, before its plan goes in: annotations
|
|
137
|
-
* globals (the legacy path, P085) for what the config declares, and the
|
|
138
|
-
* the config has one.
|
|
137
|
+
* The shell's runtime for a plan, before its plan goes in: annotations and find in page (P131)
|
|
138
|
+
* where declared, 0.6's globals (the legacy path, P085) for what the config declares, and the
|
|
139
|
+
* capability runtime where the config has one.
|
|
139
140
|
*/
|
|
140
141
|
export function composedRuntime(plan: ShellPlan): string {
|
|
141
142
|
return SHELL_RUNTIME.replace("__ANNOTATE__", () =>
|
|
142
143
|
plan.capabilities.annotate === undefined ? "" : ANNOTATE_RUNTIME,
|
|
143
144
|
)
|
|
145
|
+
.replace("__FIND__", () => (plan.capabilities.find === undefined ? "" : FIND_RUNTIME))
|
|
144
146
|
.replace("__LEGACY__", () => LEGACY_GLOBALS_RUNTIME)
|
|
145
147
|
.replace("__RUNTIME__", () => (plan.runtime === undefined ? "" : USE_RUNTIME));
|
|
146
148
|
}
|