@embedpdf/plugin-search 3.0.0-next.9 → 3.0.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.
- package/dist/contract-BMKyZdwD.cjs +42 -0
- package/dist/contract-BMKyZdwD.cjs.map +1 -0
- package/dist/contract-Cl2UZGHY.d.ts +216 -0
- package/dist/contract-DYeW6lpW.d.cts +216 -0
- package/dist/contract-MvB7O9LG.js +31 -0
- package/dist/contract-MvB7O9LG.js.map +1 -0
- package/dist/contract.cjs +17 -0
- package/dist/contract.d.cts +2 -0
- package/dist/contract.d.ts +2 -0
- package/dist/contract.js +2 -0
- package/dist/index.cjs +339 -292
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +43 -164
- package/dist/index.d.ts +43 -164
- package/dist/index.js +337 -293
- package/dist/index.js.map +1 -1
- package/package.json +9 -5
package/dist/index.js
CHANGED
|
@@ -1,358 +1,402 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
//#region src/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { i as SearchToken, n as validateSearchQuery, r as validateSearchRegex, t as SEARCH_DEFAULTS } from "./contract-MvB7O9LG.js";
|
|
2
|
+
import { PluginError, definePlugin, defineState, isPluginError, pageRefsEqual, toPluginError, toPluginErrorInfo } from "@embedpdf/core";
|
|
3
|
+
import { StageToken } from "@embedpdf/plugin-stage/contract";
|
|
4
|
+
import { boundsOfRects } from "@embedpdf/core-geometry";
|
|
5
|
+
//#region src/model.ts
|
|
6
|
+
/**
|
|
7
|
+
* The search session: the query, the hits found so far (also grouped by
|
|
8
|
+
* page), the active hit, and progress. Every function below is a pure
|
|
9
|
+
* transition; the controller applies them with `ctx.state.update`.
|
|
10
|
+
*/
|
|
11
|
+
/** The progress of no search, one object for every session that hasn't searched a page yet. */
|
|
12
|
+
const NO_PROGRESS = Object.freeze({
|
|
13
|
+
pagesSearched: 0,
|
|
14
|
+
pageCount: 0
|
|
15
|
+
});
|
|
16
|
+
const initialSearchModel = () => ({
|
|
17
|
+
query: null,
|
|
18
|
+
status: "idle",
|
|
19
|
+
operationId: null,
|
|
20
|
+
hits: [],
|
|
21
|
+
hitsByPage: {},
|
|
22
|
+
activeHitIndex: -1,
|
|
23
|
+
progress: NO_PROGRESS,
|
|
24
|
+
error: null
|
|
25
|
+
});
|
|
26
|
+
/** A new scan starts: everything from the previous session is dropped. */
|
|
27
|
+
const startSession = (_state, query, operationId) => ({
|
|
28
|
+
...initialSearchModel(),
|
|
29
|
+
query,
|
|
30
|
+
status: "searching",
|
|
31
|
+
operationId
|
|
32
|
+
});
|
|
33
|
+
const sameProgress = (left, right) => left.pagesSearched === right.pagesSearched && left.pageCount === right.pageCount;
|
|
34
|
+
/**
|
|
35
|
+
* A slice of hits arrived. The first hit becomes active so "1 of N" reads
|
|
36
|
+
* right; moving the camera is navigation's job, not the stream's. Progress
|
|
37
|
+
* with the same counts keeps its object: readers, and `onProgressChanged`,
|
|
38
|
+
* see a change only when the counts change.
|
|
39
|
+
*/
|
|
40
|
+
function appendHits(state, hits, slice) {
|
|
41
|
+
const progress = sameProgress(state.progress, slice) ? state.progress : slice;
|
|
42
|
+
if (hits.length === 0) return progress === state.progress ? state : {
|
|
43
|
+
...state,
|
|
44
|
+
progress
|
|
45
|
+
};
|
|
46
|
+
const hitsByPage = { ...state.hitsByPage };
|
|
47
|
+
for (const hit of hits) {
|
|
48
|
+
const pageObjectNumber = hit.page.objectNumber;
|
|
49
|
+
hitsByPage[pageObjectNumber] = [...hitsByPage[pageObjectNumber] ?? [], hit];
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
...state,
|
|
53
|
+
hits: [...state.hits, ...hits],
|
|
54
|
+
hitsByPage,
|
|
55
|
+
activeHitIndex: state.activeHitIndex === -1 ? 0 : state.activeHitIndex,
|
|
56
|
+
progress
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
const completeSession = (state) => ({
|
|
60
|
+
...state,
|
|
61
|
+
status: "complete"
|
|
62
|
+
});
|
|
63
|
+
const cancelSession = (state) => ({
|
|
64
|
+
...state,
|
|
65
|
+
status: "cancelled"
|
|
66
|
+
});
|
|
67
|
+
const failSession = (state, error) => ({
|
|
68
|
+
...state,
|
|
69
|
+
status: "error",
|
|
70
|
+
error
|
|
71
|
+
});
|
|
72
|
+
const setActiveHit = (state, index) => state.activeHitIndex === index ? state : {
|
|
73
|
+
...state,
|
|
74
|
+
activeHitIndex: index
|
|
11
75
|
};
|
|
12
|
-
|
|
13
|
-
|
|
76
|
+
const clearSession = (state) => state.status === "idle" ? state : initialSearchModel();
|
|
77
|
+
/**
|
|
78
|
+
* Where `hit` is in `hits`: the same object, or else the hit that covers the
|
|
79
|
+
* same characters of the same page, so a hit kept from before the search ran
|
|
80
|
+
* again still finds its place. -1 when it isn't there.
|
|
81
|
+
*/
|
|
82
|
+
function indexOfHit(hits, hit) {
|
|
83
|
+
const same = hits.indexOf(hit);
|
|
84
|
+
if (same >= 0) return same;
|
|
85
|
+
return hits.findIndex((candidate) => candidate.start === hit.start && candidate.count === hit.count && pageRefsEqual(candidate.page, hit.page));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The pages that have at least one hit, in document order: `pages` is the
|
|
89
|
+
* document's page list, whose order moving a page changes (object numbers
|
|
90
|
+
* don't follow it).
|
|
91
|
+
*/
|
|
92
|
+
const pagesWithHits = (state, pages) => pages.filter((page) => (state.hitsByPage[page.ref.objectNumber]?.length ?? 0) > 0).map((page) => page.ref);
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/controller.ts
|
|
95
|
+
const EMPTY = Object.freeze([]);
|
|
96
|
+
const EMPTY_PAGES = Object.freeze([]);
|
|
97
|
+
/** Reruns after document mutations are coalesced: a burst of events triggers one rescan. */
|
|
98
|
+
const RERUN_DELAY_MS = 250;
|
|
14
99
|
/**
|
|
15
|
-
* The search
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
100
|
+
* The search controller. `search` runs on a `latest` lane (a newer search
|
|
101
|
+
* supersedes the older one, which can no longer publish); `findAll` runs on
|
|
102
|
+
* its own signal and never touches the session. Both share `collect`, the
|
|
103
|
+
* one cursor loop over the engine's budgeted slices.
|
|
19
104
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
105
|
+
* The scan's own events (started, completed, cancelled, failed) fire where
|
|
106
|
+
* the scan reaches those points. The progress, active-hit and cleared events
|
|
107
|
+
* are derived from state changes, so they always agree with the getters.
|
|
23
108
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* at arrive in the first slice) and `next()`/`prev()`/`goTo()` reveal the
|
|
27
|
-
* hit's page. Without one, navigation still works; the host scrolls.
|
|
109
|
+
* The settings are read where they're used, never copied, so
|
|
110
|
+
* `updateSettings({ reveal })` applies to the next move at once.
|
|
28
111
|
*/
|
|
29
|
-
function
|
|
30
|
-
|
|
31
|
-
|
|
112
|
+
function createSearchController(ctx) {
|
|
113
|
+
const settings = ctx.settings();
|
|
114
|
+
const session = ctx.latest("session");
|
|
115
|
+
const started = ctx.events.source();
|
|
116
|
+
const progressChanged = ctx.events.source();
|
|
117
|
+
const completed = ctx.events.source();
|
|
118
|
+
const cancelled = ctx.events.source();
|
|
119
|
+
const failed = ctx.events.source();
|
|
120
|
+
const activeHitChanged = ctx.events.source();
|
|
121
|
+
const cleared = ctx.events.source();
|
|
122
|
+
const state = () => ctx.state.get();
|
|
123
|
+
ctx.state.onChange(({ previous, next }) => {
|
|
124
|
+
if (previous.progress !== next.progress) progressChanged.emit({
|
|
125
|
+
...next.progress,
|
|
126
|
+
hitCount: next.hits.length
|
|
127
|
+
});
|
|
128
|
+
if (previous.activeHitIndex !== next.activeHitIndex) activeHitChanged.emit({
|
|
129
|
+
index: next.activeHitIndex,
|
|
130
|
+
hit: next.hits[next.activeHitIndex] ?? null
|
|
131
|
+
});
|
|
132
|
+
if (previous.status !== "idle" && next.status === "idle") cleared.emit({});
|
|
133
|
+
});
|
|
32
134
|
function hitsFromSlice(slice) {
|
|
33
|
-
const converters = /* @__PURE__ */ new Map();
|
|
34
|
-
const converterFor = (pon) => {
|
|
35
|
-
let conv = converters.get(pon);
|
|
36
|
-
if (conv !== void 0) return conv;
|
|
37
|
-
const layout = ctx.document()?.pages.find((p) => p.pageObjectNumber === pon);
|
|
38
|
-
if (!layout) conv = null;
|
|
39
|
-
else {
|
|
40
|
-
const { pdfToContent } = pageGeometry({
|
|
41
|
-
crop: layout.boxes.crop,
|
|
42
|
-
rotation: layout.rotation,
|
|
43
|
-
userUnit: layout.userUnit
|
|
44
|
-
}, 1);
|
|
45
|
-
const toPoint = (pt) => applyPoint(pdfToContent, pt);
|
|
46
|
-
conv = {
|
|
47
|
-
pageIndex: layout.index,
|
|
48
|
-
toQuad: (q) => ({
|
|
49
|
-
upperStart: toPoint(q.p1),
|
|
50
|
-
upperEnd: toPoint(q.p2),
|
|
51
|
-
lowerStart: toPoint(q.p3),
|
|
52
|
-
lowerEnd: toPoint(q.p4)
|
|
53
|
-
})
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
converters.set(pon, conv);
|
|
57
|
-
return conv;
|
|
58
|
-
};
|
|
59
135
|
const hits = [];
|
|
60
136
|
for (const match of slice.matches) {
|
|
61
|
-
const
|
|
62
|
-
if (!
|
|
63
|
-
const segments = match.segments
|
|
64
|
-
const quad = conv.toQuad(segment.quad);
|
|
65
|
-
return {
|
|
66
|
-
quad,
|
|
67
|
-
rect: textQuadBounds(quad),
|
|
68
|
-
advance: segment.advance
|
|
69
|
-
};
|
|
70
|
-
});
|
|
137
|
+
const page = ctx.getPage(match.page);
|
|
138
|
+
if (!page) continue;
|
|
139
|
+
const segments = match.segments;
|
|
71
140
|
hits.push({
|
|
72
|
-
|
|
73
|
-
pageIndex:
|
|
74
|
-
|
|
75
|
-
|
|
141
|
+
page: match.page,
|
|
142
|
+
pageIndex: page.index,
|
|
143
|
+
start: match.start,
|
|
144
|
+
count: match.count,
|
|
76
145
|
segments,
|
|
77
|
-
...segments.length ? { bounds: boundsOfRects(segments.map((
|
|
146
|
+
...segments.length ? { bounds: boundsOfRects(segments.map((segment) => segment.rect)) ?? void 0 } : {},
|
|
78
147
|
...match.snippet ? { snippet: match.snippet } : {}
|
|
79
148
|
});
|
|
80
149
|
}
|
|
81
150
|
return hits;
|
|
82
151
|
}
|
|
83
152
|
/**
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* when cancelled; throws on error.
|
|
153
|
+
* One cursor loop over budgeted slices: snippets when the session may copy
|
|
154
|
+
* text (unless pinned), falling back to none if the engine still denies them,
|
|
155
|
+
* restart-once on a stale cursor, projection per slice. Returns true on
|
|
156
|
+
* exhaustion, false when the signal aborted; throws on a real error (already
|
|
157
|
+
* a PluginError: ctx.doc is guarded).
|
|
90
158
|
*/
|
|
91
|
-
async function collect(query,
|
|
92
|
-
const
|
|
93
|
-
if (!doc) return false;
|
|
159
|
+
async function collect(query, options, sink) {
|
|
160
|
+
const { signal } = options;
|
|
94
161
|
sink.onStart();
|
|
95
|
-
let
|
|
96
|
-
const
|
|
162
|
+
let snippets = options.snippets ?? ctx.allows("doc.text.copy");
|
|
163
|
+
const snippetsPinned = options.snippets !== void 0;
|
|
97
164
|
let cursor;
|
|
98
165
|
let restarted = false;
|
|
99
166
|
for (;;) {
|
|
167
|
+
if (signal?.aborted) return false;
|
|
100
168
|
let slice;
|
|
169
|
+
const request = {
|
|
170
|
+
...query,
|
|
171
|
+
snippets,
|
|
172
|
+
...cursor !== void 0 ? { cursor } : options.from !== void 0 ? { from: options.from } : {}
|
|
173
|
+
};
|
|
101
174
|
try {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const pending = doc.search.query(request);
|
|
108
|
-
sink.setInflight(pending);
|
|
109
|
-
slice = await pending;
|
|
110
|
-
} catch (err) {
|
|
111
|
-
if (sink.isCancelled()) return false;
|
|
112
|
-
if (!modePinned && mode === "full" && cursor === void 0 && isPermissionDenied(err)) {
|
|
113
|
-
mode = "rects";
|
|
175
|
+
slice = await ctx.cancellable(signal, ctx.doc.with({ priority: "low" }).search.query(request));
|
|
176
|
+
} catch (error) {
|
|
177
|
+
if (signal?.aborted) return false;
|
|
178
|
+
if (!snippetsPinned && snippets && cursor === void 0 && isPluginError(error, "permission-denied")) {
|
|
179
|
+
snippets = false;
|
|
114
180
|
continue;
|
|
115
181
|
}
|
|
116
|
-
if (cursor !== void 0 && !restarted &&
|
|
182
|
+
if (cursor !== void 0 && !restarted && isPluginError(error, "invalid-input")) {
|
|
117
183
|
restarted = true;
|
|
118
184
|
cursor = void 0;
|
|
119
185
|
sink.onStart();
|
|
120
186
|
continue;
|
|
121
187
|
}
|
|
122
|
-
throw
|
|
123
|
-
}
|
|
124
|
-
if (sink.isCancelled()) return false;
|
|
125
|
-
sink.onSlice(hitsFromSlice(slice), slice.scannedPages, slice.totalPages);
|
|
126
|
-
if (slice.nextCursor === null) {
|
|
127
|
-
sink.setInflight(null);
|
|
128
|
-
return true;
|
|
188
|
+
throw error;
|
|
129
189
|
}
|
|
190
|
+
if (signal?.aborted) return false;
|
|
191
|
+
sink.onSlice(hitsFromSlice(slice), slice.pagesSearched, slice.pageCount);
|
|
192
|
+
if (slice.nextCursor === null) return true;
|
|
130
193
|
cursor = slice.nextCursor;
|
|
131
194
|
}
|
|
132
195
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const stage = ctx.tryGet(StageToken);
|
|
144
|
-
if (stage) startPage = ctx.document()?.pages[stage.currentPage()]?.pageObjectNumber;
|
|
196
|
+
const viewportFirstPage = () => {
|
|
197
|
+
return ctx.tryGet(StageToken)?.getCurrentPage() ?? void 0;
|
|
198
|
+
};
|
|
199
|
+
async function search(query, options = {}) {
|
|
200
|
+
if (query.text.length === 0) {
|
|
201
|
+
clear();
|
|
202
|
+
return {
|
|
203
|
+
status: "complete",
|
|
204
|
+
hitCount: 0
|
|
205
|
+
};
|
|
145
206
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
207
|
+
ctx.assertAllowed("doc.text.search", "search");
|
|
208
|
+
const from = options.from === void 0 ? viewportFirstPage() : ctx.pageOf(options.from).ref;
|
|
209
|
+
let operationId = "";
|
|
210
|
+
return session.run(async (run) => {
|
|
211
|
+
operationId = run.id;
|
|
212
|
+
if (!await collect(query, {
|
|
213
|
+
from,
|
|
214
|
+
signal: run.signal
|
|
215
|
+
}, {
|
|
216
|
+
onStart: () => run.commit(() => {
|
|
217
|
+
ctx.state.update(startSession, query, run.id);
|
|
218
|
+
started.emit({ query });
|
|
151
219
|
}),
|
|
152
|
-
onSlice: (hits,
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
if (gen === generation) inflight = pending;
|
|
161
|
-
}
|
|
162
|
-
}) && gen === generation) ctx.dispatch({ type: "COMPLETE" });
|
|
163
|
-
} catch (err) {
|
|
164
|
-
if (gen !== generation) return;
|
|
165
|
-
ctx.dispatch({
|
|
166
|
-
type: "ERROR",
|
|
167
|
-
message: err instanceof Error ? err.message : String(err)
|
|
220
|
+
onSlice: (hits, pagesSearched, pageCount) => run.commit(() => ctx.state.update(appendHits, hits, {
|
|
221
|
+
pagesSearched,
|
|
222
|
+
pageCount
|
|
223
|
+
}))
|
|
224
|
+
})) throw new PluginError("operation-cancelled", "search", "search aborted");
|
|
225
|
+
run.commit(() => {
|
|
226
|
+
ctx.state.update(completeSession);
|
|
227
|
+
completed.emit({ hitCount: state().hits.length });
|
|
168
228
|
});
|
|
169
|
-
|
|
229
|
+
return {
|
|
230
|
+
status: "complete",
|
|
231
|
+
hitCount: state().hits.length
|
|
232
|
+
};
|
|
233
|
+
}, options).catch((error) => {
|
|
234
|
+
if (isPluginError(error, "instance-closed")) return {
|
|
235
|
+
status: "cancelled",
|
|
236
|
+
hitCount: 0
|
|
237
|
+
};
|
|
238
|
+
if (isPluginError(error, "operation-cancelled")) {
|
|
239
|
+
const why = error.details?.reason ?? "cancelled";
|
|
240
|
+
const reason = why === "superseded" ? "superseded" : why === "cleared" ? "cleared" : "cancelled";
|
|
241
|
+
if (reason === "cancelled" && state().operationId === operationId) ctx.state.update(cancelSession);
|
|
242
|
+
cancelled.emit({ reason });
|
|
243
|
+
return {
|
|
244
|
+
status: reason === "superseded" ? "superseded" : "cancelled",
|
|
245
|
+
hitCount: state().hits.length
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
const info = toPluginErrorInfo(toPluginError("search", error));
|
|
249
|
+
if (state().operationId === operationId) ctx.state.update(failSession, info);
|
|
250
|
+
failed.emit({ error: info });
|
|
251
|
+
throw error;
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
function clear() {
|
|
255
|
+
session.cancel("cleared");
|
|
256
|
+
ctx.state.update(clearSession);
|
|
170
257
|
}
|
|
171
|
-
function
|
|
172
|
-
const { hits } =
|
|
258
|
+
function goToHit(target, options) {
|
|
259
|
+
const { hits } = state();
|
|
173
260
|
if (hits.length === 0) return null;
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
261
|
+
const index = typeof target === "number" ? (target % hits.length + hits.length) % hits.length : indexOfHit(hits, target);
|
|
262
|
+
if (index < 0) return null;
|
|
263
|
+
ctx.state.update(setActiveHit, index);
|
|
264
|
+
reveal(hits[index], options);
|
|
265
|
+
return hits[index];
|
|
266
|
+
}
|
|
267
|
+
function reveal(hit, options) {
|
|
180
268
|
const arrival = {
|
|
181
|
-
...
|
|
182
|
-
...
|
|
183
|
-
...reveal
|
|
269
|
+
...settings.get().reveal,
|
|
270
|
+
...options
|
|
184
271
|
};
|
|
185
|
-
ctx.tryGet(StageToken)?.reveal(hit.
|
|
272
|
+
ctx.tryGet(StageToken)?.reveal(hit.page, {
|
|
186
273
|
rect: hit.bounds,
|
|
187
|
-
|
|
274
|
+
align: arrival.align,
|
|
188
275
|
behavior: arrival.behavior
|
|
189
276
|
});
|
|
190
|
-
return hit;
|
|
191
277
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
278
|
+
/**
|
|
279
|
+
* One page's hits, by the page's ref or index. A page that isn't in the
|
|
280
|
+
* document has none: a layer may read while its page is being deleted.
|
|
281
|
+
*/
|
|
282
|
+
const hitsOn = (page) => {
|
|
283
|
+
const found = ctx.getPage(page);
|
|
284
|
+
return found ? state().hitsByPage[found.ref.objectNumber] ?? EMPTY : EMPTY;
|
|
197
285
|
};
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
286
|
+
const api = {
|
|
287
|
+
...settings.api,
|
|
288
|
+
canSearch: (options) => ctx.allows("doc.text.search") && (!options?.snippets || ctx.allows("doc.text.copy")),
|
|
289
|
+
search,
|
|
290
|
+
refresh: async (options) => {
|
|
291
|
+
const { query, status } = state();
|
|
292
|
+
if (!query || status === "idle") return;
|
|
293
|
+
await search(query, options);
|
|
203
294
|
},
|
|
295
|
+
cancel: () => session.cancel("cancelled"),
|
|
204
296
|
clear,
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
if (
|
|
211
|
-
const all = [];
|
|
212
|
-
const signal = opts.signal;
|
|
213
|
-
let pending = null;
|
|
214
|
-
const onAbort = () => pending?.abort(signal?.reason);
|
|
215
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
216
|
-
try {
|
|
217
|
-
await collect(query, { mode: opts.mode }, {
|
|
218
|
-
onStart: () => {
|
|
219
|
-
all.length = 0;
|
|
220
|
-
},
|
|
221
|
-
onSlice: (hits) => all.push(...hits),
|
|
222
|
-
isCancelled: () => signal?.aborted ?? false,
|
|
223
|
-
setInflight: (p) => {
|
|
224
|
-
pending = p;
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
if (signal?.aborted) throw signal.reason ?? /* @__PURE__ */ new Error("findAll aborted");
|
|
228
|
-
return all;
|
|
229
|
-
} finally {
|
|
230
|
-
signal?.removeEventListener("abort", onAbort);
|
|
231
|
-
}
|
|
232
|
-
},
|
|
233
|
-
status: () => ctx.getState().status,
|
|
234
|
-
hits: () => ctx.getState().hits,
|
|
235
|
-
hitCount: () => ctx.getState().hits.length,
|
|
236
|
-
activeIndex: () => ctx.getState().activeIndex,
|
|
237
|
-
activeHit: () => {
|
|
238
|
-
const { hits, activeIndex } = ctx.getState();
|
|
239
|
-
return activeIndex >= 0 && activeIndex < hits.length ? hits[activeIndex] : null;
|
|
297
|
+
nextHit: (options) => goToHit(state().activeHitIndex + 1, options),
|
|
298
|
+
previousHit: (options) => goToHit(state().activeHitIndex - 1, options),
|
|
299
|
+
goToHit,
|
|
300
|
+
revealActiveHit: (options) => {
|
|
301
|
+
const hit = api.getActiveHit();
|
|
302
|
+
if (hit) reveal(hit, options);
|
|
240
303
|
},
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
304
|
+
getQuery: () => state().query,
|
|
305
|
+
getStatus: () => state().status,
|
|
306
|
+
listHits: (filter) => filter?.page === void 0 ? state().hits : hitsOn(filter.page),
|
|
307
|
+
getHitCount: (page) => (page === void 0 ? state().hits : hitsOn(page)).length,
|
|
308
|
+
listPagesWithHits: () => state().hits.length ? pagesWithHits(state(), ctx.document()?.pages ?? []) : EMPTY_PAGES,
|
|
309
|
+
getActiveHitIndex: () => state().activeHitIndex,
|
|
310
|
+
getActiveHit: () => state().hits[state().activeHitIndex] ?? null,
|
|
311
|
+
getProgress: () => state().progress,
|
|
312
|
+
getError: () => state().error,
|
|
313
|
+
findAll: async (query, options = {}) => {
|
|
314
|
+
ctx.assertAllowed("doc.text.search", "findAll");
|
|
315
|
+
if (options.snippets) ctx.assertAllowed("doc.text.copy", "findAll");
|
|
316
|
+
const all = [];
|
|
317
|
+
if (!await collect(query, {
|
|
318
|
+
...options.snippets !== void 0 ? { snippets: options.snippets } : {},
|
|
319
|
+
...options.signal ? { signal: options.signal } : {}
|
|
320
|
+
}, {
|
|
321
|
+
onStart: () => {
|
|
322
|
+
all.length = 0;
|
|
323
|
+
},
|
|
324
|
+
onSlice: (hits) => all.push(...hits)
|
|
325
|
+
})) throw new PluginError("operation-cancelled", "search", "findAll aborted");
|
|
326
|
+
return all;
|
|
245
327
|
},
|
|
246
|
-
|
|
247
|
-
|
|
328
|
+
onStarted: started.on,
|
|
329
|
+
onProgressChanged: progressChanged.on,
|
|
330
|
+
onCompleted: completed.on,
|
|
331
|
+
onCancelled: cancelled.on,
|
|
332
|
+
onFailed: failed.on,
|
|
333
|
+
onActiveHitChanged: activeHitChanged.on,
|
|
334
|
+
onCleared: cleared.on
|
|
248
335
|
};
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
if (ctx.getState().status === "idle") return;
|
|
262
|
-
if (timer !== null) clearTimeout(timer);
|
|
263
|
-
timer = setTimeout(() => {
|
|
264
|
-
timer = null;
|
|
265
|
-
ctx.get(SearchToken).rerun();
|
|
266
|
-
}, RERUN_DELAY_MS);
|
|
267
|
-
});
|
|
268
|
-
ctx.cleanup(() => {
|
|
269
|
-
if (timer !== null) clearTimeout(timer);
|
|
270
|
-
unsubscribe();
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
//#endregion
|
|
274
|
-
//#region src/reducer.ts
|
|
275
|
-
const initialSearchState = () => ({
|
|
276
|
-
query: null,
|
|
277
|
-
status: "idle",
|
|
278
|
-
hits: [],
|
|
279
|
-
hitsByPage: {},
|
|
280
|
-
activeIndex: -1,
|
|
281
|
-
progress: {
|
|
282
|
-
scanned: 0,
|
|
283
|
-
total: 0
|
|
284
|
-
},
|
|
285
|
-
error: null
|
|
286
|
-
});
|
|
287
|
-
const searchReducer = (state, action) => {
|
|
288
|
-
switch (action.type) {
|
|
289
|
-
case "START": return {
|
|
290
|
-
...initialSearchState(),
|
|
291
|
-
query: action.query,
|
|
292
|
-
status: "searching"
|
|
293
|
-
};
|
|
294
|
-
case "APPEND": {
|
|
295
|
-
if (action.hits.length === 0) return {
|
|
296
|
-
...state,
|
|
297
|
-
progress: {
|
|
298
|
-
scanned: action.scanned,
|
|
299
|
-
total: action.total
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
const hits = state.hits.concat(action.hits);
|
|
303
|
-
const hitsByPage = { ...state.hitsByPage };
|
|
304
|
-
for (let i = state.hits.length; i < hits.length; i++) {
|
|
305
|
-
const pon = hits[i].pon;
|
|
306
|
-
hitsByPage[pon] = (hitsByPage[pon] ?? []).concat(i);
|
|
307
|
-
}
|
|
308
|
-
return {
|
|
309
|
-
...state,
|
|
310
|
-
hits,
|
|
311
|
-
hitsByPage,
|
|
312
|
-
activeIndex: state.activeIndex === -1 ? 0 : state.activeIndex,
|
|
313
|
-
progress: {
|
|
314
|
-
scanned: action.scanned,
|
|
315
|
-
total: action.total
|
|
316
|
-
}
|
|
317
|
-
};
|
|
336
|
+
return {
|
|
337
|
+
api,
|
|
338
|
+
connect() {
|
|
339
|
+
let cancelRerun = null;
|
|
340
|
+
ctx.listen(ctx.doc.events, () => {
|
|
341
|
+
if (state().status === "idle") return;
|
|
342
|
+
cancelRerun?.();
|
|
343
|
+
cancelRerun = ctx.clock.after(RERUN_DELAY_MS, () => {
|
|
344
|
+
cancelRerun = null;
|
|
345
|
+
api.refresh().catch(() => {});
|
|
346
|
+
});
|
|
347
|
+
});
|
|
318
348
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
status: "complete"
|
|
322
|
-
};
|
|
323
|
-
case "ERROR": return {
|
|
324
|
-
...state,
|
|
325
|
-
status: "error",
|
|
326
|
-
error: action.message
|
|
327
|
-
};
|
|
328
|
-
case "SET_ACTIVE": return {
|
|
329
|
-
...state,
|
|
330
|
-
activeIndex: action.index
|
|
331
|
-
};
|
|
332
|
-
case "CLEAR": return initialSearchState();
|
|
333
|
-
default: return state;
|
|
334
|
-
}
|
|
335
|
-
};
|
|
349
|
+
};
|
|
350
|
+
}
|
|
336
351
|
//#endregion
|
|
337
352
|
//#region src/search.plugin.ts
|
|
338
353
|
/**
|
|
339
|
-
* Document text search over
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
* the hit's page; without it the host owns scrolling.
|
|
354
|
+
* Document text search over the engine's budgeted, resumable slices.
|
|
355
|
+
* Document-scoped; no pointer handling. The stage is optional: with one,
|
|
356
|
+
* scans start at the current page and navigation reveals hits. `config` is
|
|
357
|
+
* the settings the app registers, over {@link SEARCH_DEFAULTS}.
|
|
344
358
|
*/
|
|
345
359
|
const searchPlugin = (config) => definePlugin({
|
|
346
360
|
id: "search",
|
|
347
361
|
token: SearchToken,
|
|
348
362
|
scope: "document",
|
|
349
363
|
optional: [StageToken],
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
364
|
+
state: initialSearchModel,
|
|
365
|
+
settings: {
|
|
366
|
+
defaults: SEARCH_DEFAULTS,
|
|
367
|
+
registered: config
|
|
368
|
+
},
|
|
369
|
+
create: createSearchController
|
|
370
|
+
});
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/state.ts
|
|
373
|
+
/**
|
|
374
|
+
* The search page's State table as code: what `useSearchState()` returns, and
|
|
375
|
+
* the same fields in every other framework. The matches themselves are not
|
|
376
|
+
* here: `useSearchHits()` reads them, so a component that only shows the
|
|
377
|
+
* count doesn't re-render for every match that arrives.
|
|
378
|
+
*/
|
|
379
|
+
const searchState = defineState(SearchToken, {
|
|
380
|
+
read: (search) => ({
|
|
381
|
+
query: search.getQuery(),
|
|
382
|
+
status: search.getStatus(),
|
|
383
|
+
hitCount: search.getHitCount(),
|
|
384
|
+
activeHitIndex: search.getActiveHitIndex(),
|
|
385
|
+
activeHit: search.getActiveHit(),
|
|
386
|
+
progress: search.getProgress(),
|
|
387
|
+
error: search.getError()
|
|
388
|
+
}),
|
|
389
|
+
empty: {
|
|
390
|
+
query: null,
|
|
391
|
+
status: "idle",
|
|
392
|
+
hitCount: 0,
|
|
393
|
+
activeHitIndex: -1,
|
|
394
|
+
activeHit: null,
|
|
395
|
+
progress: NO_PROGRESS,
|
|
396
|
+
error: null
|
|
397
|
+
}
|
|
354
398
|
});
|
|
355
399
|
//#endregion
|
|
356
|
-
export { SearchToken, searchPlugin, validateSearchQuery, validateSearchRegex };
|
|
400
|
+
export { SEARCH_DEFAULTS, SearchToken, searchPlugin, searchState, validateSearchQuery, validateSearchRegex };
|
|
357
401
|
|
|
358
402
|
//# sourceMappingURL=index.js.map
|