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