@embedpdf/plugin-selection 3.0.0-next.1 → 3.0.0-next.11
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/host.cjs +3 -0
- package/dist/contract/host.d.cts +2 -0
- package/dist/contract/host.d.ts +2 -0
- package/dist/contract/host.js +2 -0
- package/dist/contract.cjs +7 -0
- package/dist/contract.cjs.map +1 -0
- package/dist/contract.d.cts +7 -0
- package/dist/contract.d.ts +7 -0
- package/dist/contract.js +7 -0
- package/dist/contract.js.map +1 -0
- package/dist/index.cjs +119 -547
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +71 -149
- package/dist/index.d.ts +71 -149
- package/dist/index.js +116 -540
- package/dist/index.js.map +1 -1
- package/dist/internal.cjs +13 -0
- package/dist/internal.d.cts +63 -0
- package/dist/internal.d.ts +63 -0
- package/dist/internal.js +4 -0
- package/dist/reducer-BAOQ2CJ8.cjs +732 -0
- package/dist/reducer-BAOQ2CJ8.cjs.map +1 -0
- package/dist/reducer-DbKtk9Xl.js +685 -0
- package/dist/reducer-DbKtk9Xl.js.map +1 -0
- package/dist/types-BVUcOsCh.js +14 -0
- package/dist/types-BVUcOsCh.js.map +1 -0
- package/dist/types-CUrUVl6K.d.cts +292 -0
- package/dist/types-CUrUVl6K.d.ts +292 -0
- package/dist/types-D9kEBMFj.cjs +18 -0
- package/dist/types-D9kEBMFj.cjs.map +1 -0
- package/package.json +17 -5
package/dist/index.cjs
CHANGED
|
@@ -1,575 +1,147 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_reducer = require("./reducer-BAOQ2CJ8.cjs");
|
|
3
|
+
const require_types = require("./types-D9kEBMFj.cjs");
|
|
4
|
+
const require_contract = require("./contract.cjs");
|
|
2
5
|
let _embedpdf_core = require("@embedpdf/core");
|
|
3
|
-
let
|
|
6
|
+
let _embedpdf_plugin_interaction_contract = require("@embedpdf/plugin-interaction/contract");
|
|
4
7
|
let _embedpdf_core_geometry = require("@embedpdf/core-geometry");
|
|
5
|
-
//#region src/
|
|
8
|
+
//#region src/selection.plugin.ts
|
|
6
9
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
+
* Text selection — document-scoped, requires the interaction hub. In `init` it
|
|
11
|
+
* registers ITS pointer handler with the hub; the hub owns the pointer stream
|
|
12
|
+
* and arbitration. Works with `<Stage>` or a standalone `<PageView>` — selection
|
|
13
|
+
* only needs the page coordinate context + the engine's text geometry.
|
|
10
14
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* - `rectsForRange` — Chromium `pdfium_range.cc` `MergeAdjacentRects` (one rect per line).
|
|
15
|
-
* - `expandToWord/Line` — Chromium `OnMultipleClick` (double = word, triple = line).
|
|
16
|
-
* The PDF↔content y-flip is the geometry package's `pageGeometry` (the one place
|
|
17
|
-
* that math lives), never hand-rolled here.
|
|
18
|
-
*/
|
|
19
|
-
const FLAG_EMPTY = 2;
|
|
20
|
-
const isBoundary = (flags) => (flags & 3) !== 0;
|
|
21
|
-
const isEmpty = (flags) => (flags & FLAG_EMPTY) !== 0;
|
|
22
|
-
const toContent = (m, b) => (0, _embedpdf_core_geometry.applyRect)(m, {
|
|
23
|
-
x: b.left,
|
|
24
|
-
y: b.bottom,
|
|
25
|
-
width: b.right - b.left,
|
|
26
|
-
height: b.top - b.bottom
|
|
27
|
-
});
|
|
28
|
-
/**
|
|
29
|
-
* Flatten a page's text geometry into content-space glyphs + runs. Rotation does
|
|
30
|
-
* NOT enter here — the overlay rides the page's CSS rotation, so glyphs stay in
|
|
31
|
-
* un-rotated content space (the crop-relative y-flip is the only conversion).
|
|
15
|
+
* Registry/content invalidation is wired inside the capability itself
|
|
16
|
+
* (revision watch + document event subscription), so headless/programmatic
|
|
17
|
+
* use gets the same reconciliation as the full viewer.
|
|
32
18
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
});
|
|
48
|
-
runs.push({
|
|
49
|
-
start,
|
|
50
|
-
count: run.glyphs.length,
|
|
51
|
-
rect: toContent(pdfToContent, run.rect),
|
|
52
|
-
fontSize: run.fontSize
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
return {
|
|
56
|
-
glyphs,
|
|
57
|
-
runs
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
function avgGlyphHeight(text) {
|
|
61
|
-
let total = 0;
|
|
62
|
-
let count = 0;
|
|
63
|
-
for (const g of text.glyphs) {
|
|
64
|
-
if (isEmpty(g.flags)) continue;
|
|
65
|
-
total += g.loose.height;
|
|
66
|
-
count++;
|
|
67
|
-
}
|
|
68
|
-
return count === 0 ? 0 : total / count;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* The glyph index at a content-space point, or null when nothing is near (so the
|
|
72
|
-
* caller can show the pointer cursor off-text). PDFium `GetIndexAtPos`: exact
|
|
73
|
-
* tight-box containment first, then a tolerance pass (closest by Manhattan
|
|
74
|
-
* distance within `toleranceFactor × average glyph height`).
|
|
75
|
-
*/
|
|
76
|
-
function glyphAt(text, p, toleranceFactor = 1.5) {
|
|
77
|
-
for (const run of text.runs) {
|
|
78
|
-
const r = run.rect;
|
|
79
|
-
if (p.x < r.x || p.x > r.x + r.width || p.y < r.y || p.y > r.y + r.height) continue;
|
|
80
|
-
for (let i = 0; i < run.count; i++) {
|
|
81
|
-
const b = text.glyphs[run.start + i].tight ?? text.glyphs[run.start + i].loose;
|
|
82
|
-
if (p.x >= b.x && p.x <= b.x + b.width && p.y >= b.y && p.y <= b.y + b.height) return run.start + i;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
if (toleranceFactor <= 0) return null;
|
|
86
|
-
const half = avgGlyphHeight(text) * toleranceFactor / 2;
|
|
87
|
-
let best = -1;
|
|
88
|
-
let bestDist = Infinity;
|
|
89
|
-
for (const run of text.runs) {
|
|
90
|
-
const r = run.rect;
|
|
91
|
-
if (p.y < r.y - half || p.y > r.y + r.height + half || p.x < r.x - half || p.x > r.x + r.width + half) continue;
|
|
92
|
-
for (let i = 0; i < run.count; i++) {
|
|
93
|
-
const g = text.glyphs[run.start + i];
|
|
94
|
-
if (isEmpty(g.flags)) continue;
|
|
95
|
-
const b = g.tight ?? g.loose;
|
|
96
|
-
if (p.x < b.x - half || p.x > b.x + b.width + half || p.y < b.y - half || p.y > b.y + b.height + half) continue;
|
|
97
|
-
const dx = Math.min(Math.abs(p.x - b.x), Math.abs(p.x - (b.x + b.width)));
|
|
98
|
-
const dy = Math.min(Math.abs(p.y - b.y), Math.abs(p.y - (b.y + b.height)));
|
|
99
|
-
if (dx + dy < bestDist) {
|
|
100
|
-
bestDist = dx + dy;
|
|
101
|
-
best = run.start + i;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return best >= 0 ? best : null;
|
|
106
|
-
}
|
|
107
|
-
/** Double-click: the word around `i` (walk to space/empty glyphs both ways). */
|
|
108
|
-
function expandToWord(text, i) {
|
|
109
|
-
const n = text.glyphs.length;
|
|
110
|
-
if (i < 0 || i >= n) return [i, i];
|
|
111
|
-
let from = i;
|
|
112
|
-
while (from > 0 && !isBoundary(text.glyphs[from - 1].flags)) from--;
|
|
113
|
-
let to = i;
|
|
114
|
-
while (to < n - 1 && !isBoundary(text.glyphs[to + 1].flags)) to++;
|
|
115
|
-
return [from, to];
|
|
116
|
-
}
|
|
117
|
-
/** Triple-click: the full visual line — runs whose vertical extent overlaps the anchor run's. */
|
|
118
|
-
function expandToLine(text, i) {
|
|
119
|
-
const ri = text.runs.findIndex((r) => i >= r.start && i < r.start + r.count);
|
|
120
|
-
if (ri < 0) return [i, i];
|
|
121
|
-
const anchor = text.runs[ri];
|
|
122
|
-
const top = anchor.rect.y;
|
|
123
|
-
const bottom = anchor.rect.y + anchor.rect.height;
|
|
124
|
-
let from = anchor.start;
|
|
125
|
-
let to = anchor.start + anchor.count - 1;
|
|
126
|
-
for (let r = ri - 1; r >= 0; r--) {
|
|
127
|
-
const run = text.runs[r];
|
|
128
|
-
if (isZero(run.rect)) continue;
|
|
129
|
-
if (!overlapV(run.rect.y, run.rect.y + run.rect.height, top, bottom)) break;
|
|
130
|
-
from = run.start;
|
|
131
|
-
}
|
|
132
|
-
for (let r = ri + 1; r < text.runs.length; r++) {
|
|
133
|
-
const run = text.runs[r];
|
|
134
|
-
if (isZero(run.rect)) continue;
|
|
135
|
-
if (!overlapV(run.rect.y, run.rect.y + run.rect.height, top, bottom)) break;
|
|
136
|
-
to = run.start + run.count - 1;
|
|
137
|
-
}
|
|
138
|
-
return [from, to];
|
|
139
|
-
}
|
|
140
|
-
const CHAR_DISTANCE_FACTOR = 2.5;
|
|
141
|
-
const FONT_SIZE_RATIO_THRESHOLD = 1.5;
|
|
142
|
-
const VERTICAL_OVERLAP_THRESHOLD = .8;
|
|
143
|
-
const LINE_OVERLAP_THRESHOLD = .5;
|
|
144
|
-
function rectsForRange(text, from, to) {
|
|
145
|
-
const lo = Math.max(0, Math.min(from, to));
|
|
146
|
-
const hi = Math.min(text.glyphs.length - 1, Math.max(from, to));
|
|
147
|
-
const subRuns = [];
|
|
148
|
-
for (const run of text.runs) {
|
|
149
|
-
const runEnd = run.start + run.count - 1;
|
|
150
|
-
if (runEnd < lo || run.start > hi) continue;
|
|
151
|
-
const s = Math.max(lo, run.start);
|
|
152
|
-
const e = Math.min(hi, runEnd);
|
|
153
|
-
let minX = Infinity;
|
|
154
|
-
let maxX = -Infinity;
|
|
155
|
-
let minY = Infinity;
|
|
156
|
-
let maxY = -Infinity;
|
|
157
|
-
let charCount = 0;
|
|
158
|
-
let widthSum = 0;
|
|
159
|
-
let prevRight = -Infinity;
|
|
160
|
-
const flush = () => {
|
|
161
|
-
if (minX !== Infinity && charCount > 0) subRuns.push({
|
|
162
|
-
rect: {
|
|
163
|
-
x: minX,
|
|
164
|
-
y: minY,
|
|
165
|
-
width: maxX - minX,
|
|
166
|
-
height: maxY - minY
|
|
167
|
-
},
|
|
168
|
-
charCount,
|
|
169
|
-
fontSize: run.fontSize
|
|
170
|
-
});
|
|
171
|
-
minX = Infinity;
|
|
172
|
-
maxX = -Infinity;
|
|
173
|
-
minY = Infinity;
|
|
174
|
-
maxY = -Infinity;
|
|
175
|
-
charCount = 0;
|
|
176
|
-
widthSum = 0;
|
|
177
|
-
prevRight = -Infinity;
|
|
178
|
-
};
|
|
179
|
-
for (let gi = s; gi <= e; gi++) {
|
|
180
|
-
const g = text.glyphs[gi];
|
|
181
|
-
if (isEmpty(g.flags)) continue;
|
|
182
|
-
const b = g.loose;
|
|
183
|
-
if (charCount > 0 && prevRight > -Infinity) {
|
|
184
|
-
const avg = widthSum / charCount;
|
|
185
|
-
if (avg > 0 && Math.abs(b.x - prevRight) > CHAR_DISTANCE_FACTOR * avg) flush();
|
|
186
|
-
}
|
|
187
|
-
minX = Math.min(minX, b.x);
|
|
188
|
-
maxX = Math.max(maxX, b.x + b.width);
|
|
189
|
-
minY = Math.min(minY, b.y);
|
|
190
|
-
maxY = Math.max(maxY, b.y + b.height);
|
|
191
|
-
charCount++;
|
|
192
|
-
widthSum += b.width;
|
|
193
|
-
prevRight = b.x + b.width;
|
|
194
|
-
}
|
|
195
|
-
flush();
|
|
196
|
-
}
|
|
197
|
-
return mergeAdjacentRects(subRuns);
|
|
198
|
-
}
|
|
199
|
-
function mergeAdjacentRects(runs) {
|
|
200
|
-
const out = [];
|
|
201
|
-
let prev = null;
|
|
202
|
-
let cur = null;
|
|
203
|
-
for (const run of runs) {
|
|
204
|
-
if (prev && cur && shouldMerge(prev, run)) cur = (0, _embedpdf_core_geometry.boundsOfRects)([cur, run.rect]);
|
|
205
|
-
else {
|
|
206
|
-
if (cur) out.push(cur);
|
|
207
|
-
cur = run.rect;
|
|
208
|
-
}
|
|
209
|
-
prev = run;
|
|
210
|
-
}
|
|
211
|
-
if (cur && cur.width > 0 && cur.height > 0) out.push(cur);
|
|
212
|
-
return out;
|
|
213
|
-
}
|
|
214
|
-
function shouldMerge(a, b) {
|
|
215
|
-
if (a.fontSize != null && b.fontSize != null && a.fontSize > 0 && b.fontSize > 0) {
|
|
216
|
-
if (Math.max(a.fontSize, b.fontSize) / Math.min(a.fontSize, b.fontSize) > FONT_SIZE_RATIO_THRESHOLD) return false;
|
|
19
|
+
const selectionPlugin = () => (0, _embedpdf_core.definePlugin)({
|
|
20
|
+
id: "selection",
|
|
21
|
+
token: require_types.SelectionToken,
|
|
22
|
+
scope: "document",
|
|
23
|
+
requires: [_embedpdf_plugin_interaction_contract.InteractionToken],
|
|
24
|
+
optional: [_embedpdf_plugin_interaction_contract.FeedbackToken],
|
|
25
|
+
initialState: require_reducer.initialSelectionState,
|
|
26
|
+
reduce: require_reducer.selectionReducer,
|
|
27
|
+
capability: require_reducer.createSelectionCapability,
|
|
28
|
+
init: (ctx) => {
|
|
29
|
+
const interaction = ctx.get(_embedpdf_plugin_interaction_contract.InteractionToken);
|
|
30
|
+
const selection = ctx.get(require_types.SelectionToken);
|
|
31
|
+
const feedback = ctx.tryGet(_embedpdf_plugin_interaction_contract.FeedbackToken);
|
|
32
|
+
interaction.registerHandler(require_reducer.createTextSelectHandler(selection, interaction, feedback ?? void 0));
|
|
217
33
|
}
|
|
218
|
-
|
|
219
|
-
const aw = a.rect.width / a.charCount;
|
|
220
|
-
const bw = b.rect.width / b.charCount;
|
|
221
|
-
const aL = a.rect.x - aw;
|
|
222
|
-
const aR = a.rect.x + a.rect.width + aw;
|
|
223
|
-
const bL = b.rect.x - bw;
|
|
224
|
-
return aL < b.rect.x + b.rect.width + bw && aR > bL;
|
|
225
|
-
}
|
|
226
|
-
function verticalOverlap(a, b) {
|
|
227
|
-
if (a.height <= 0 || b.height <= 0) return 0;
|
|
228
|
-
const u = Math.max(a.y + a.height, b.y + b.height) - Math.min(a.y, b.y);
|
|
229
|
-
if (u === a.height || u === b.height) return 1;
|
|
230
|
-
return Math.max(0, Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y)) / u;
|
|
231
|
-
}
|
|
232
|
-
const isZero = (r) => r.width === 0 && r.height === 0;
|
|
233
|
-
function overlapV(top1, bottom1, top2, bottom2) {
|
|
234
|
-
const u = Math.max(bottom1, bottom2) - Math.min(top1, top2);
|
|
235
|
-
if (u === 0) return false;
|
|
236
|
-
return Math.max(0, Math.min(bottom1, bottom2) - Math.max(top1, top2)) / u >= LINE_OVERLAP_THRESHOLD;
|
|
237
|
-
}
|
|
34
|
+
});
|
|
238
35
|
//#endregion
|
|
239
|
-
//#region src/
|
|
240
|
-
const EMPTY = [];
|
|
36
|
+
//#region src/handles.ts
|
|
241
37
|
/**
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
38
|
+
* Selection handles — the pure policy half (the `wheel.ts` convention).
|
|
39
|
+
*
|
|
40
|
+
* A handle IS the boundary glyph's leading (or trailing) edge: a segment
|
|
41
|
+
* between two corners of the glyph's ORIENTED cell, with the grab head just
|
|
42
|
+
* beyond the ascent at the start / past the baseline at the end. Bar length,
|
|
43
|
+
* angle, and head all derive from that one segment, so rotated text and
|
|
44
|
+
* rotated pages are carried by construction — the AABB never enters.
|
|
245
45
|
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
46
|
+
* Everything here is DOM-free and framework-free: geometry is pure given a
|
|
47
|
+
* projector, and the drag session speaks the selection's own gesture verbs
|
|
48
|
+
* (`beginAt`/`extendTo`/`end` — the same ones the pointer handler uses; a
|
|
49
|
+
* handle drag IS a selection gesture, re-anchored). The framework adapters
|
|
50
|
+
* keep only subscriptions and markup; the DOM listener mechanics live in
|
|
51
|
+
* `@embedpdf/web`'s `attachSelectionHandle`.
|
|
52
|
+
*
|
|
53
|
+
* The view dependency is STRUCTURAL (satisfied by five lines over
|
|
54
|
+
* `StageCapability`) so this plugin stays stage-free — selection also runs
|
|
55
|
+
* in stage-less hosts (`PageView`) — and the math tests run against a fake.
|
|
250
56
|
*/
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
if (!selection) return {
|
|
287
|
-
pages,
|
|
288
|
-
start: null,
|
|
289
|
-
end: null,
|
|
290
|
-
direction: "forward"
|
|
291
|
-
};
|
|
292
|
-
const { start, end, direction } = orderedEnds(selection);
|
|
293
|
-
return {
|
|
294
|
-
pages,
|
|
295
|
-
start: endpointFor(start, "start"),
|
|
296
|
-
end: endpointFor(end, "end"),
|
|
297
|
-
direction
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
function ensurePage(pon) {
|
|
301
|
-
if (cache.has(pon) || pending.has(pon)) return;
|
|
302
|
-
const doc = ctx.doc;
|
|
303
|
-
const layout = ctx.document()?.pages.find((p) => p.pageObjectNumber === pon);
|
|
304
|
-
if (!doc || !layout) return;
|
|
305
|
-
pending.add(pon);
|
|
306
|
-
doc.page(pon).geometry.read().then((snapshot) => {
|
|
307
|
-
pending.delete(pon);
|
|
308
|
-
cache.set(pon, buildPageText(snapshot, layout.boxes.crop, layout.rotation, layout.userUnit));
|
|
309
|
-
ctx.dispatch({
|
|
310
|
-
type: "PAGE_LOADED",
|
|
311
|
-
pon
|
|
312
|
-
});
|
|
313
|
-
if (ctx.getState().selection) recompute();
|
|
314
|
-
}, () => {
|
|
315
|
-
pending.delete(pon);
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
function recompute(sel = ctx.getState().selection) {
|
|
319
|
-
if (!sel) return;
|
|
320
|
-
const { start, end } = orderedEnds(sel);
|
|
321
|
-
const si = pageIndexOf(start.pon);
|
|
322
|
-
const ei = pageIndexOf(end.pon);
|
|
323
|
-
if (si < 0 || ei < 0) return;
|
|
324
|
-
const rects = {};
|
|
325
|
-
for (let i = si; i <= ei; i++) {
|
|
326
|
-
const pon = ponAtIndex(i);
|
|
327
|
-
if (pon == null) continue;
|
|
328
|
-
const text = cache.get(pon);
|
|
329
|
-
if (!text) {
|
|
330
|
-
ensurePage(pon);
|
|
331
|
-
continue;
|
|
332
|
-
}
|
|
333
|
-
rects[pon] = rectsForRange(text, i === si ? start.glyph : 0, i === ei ? end.glyph : text.glyphs.length - 1);
|
|
334
|
-
}
|
|
335
|
-
ctx.dispatch({
|
|
336
|
-
type: "SET",
|
|
337
|
-
selection: sel,
|
|
338
|
-
rects
|
|
339
|
-
});
|
|
340
|
-
fireChange();
|
|
341
|
-
}
|
|
342
|
-
function selectSpan(pon, point, expand) {
|
|
343
|
-
const text = cache.get(pon);
|
|
344
|
-
if (!text) return;
|
|
345
|
-
const i = glyphAt(text, point);
|
|
346
|
-
if (i == null) return;
|
|
347
|
-
const [from, to] = expand === "word" ? expandToWord(text, i) : expandToLine(text, i);
|
|
348
|
-
recompute({
|
|
349
|
-
anchor: {
|
|
350
|
-
pon,
|
|
351
|
-
glyph: from
|
|
352
|
-
},
|
|
353
|
-
focus: {
|
|
354
|
-
pon,
|
|
355
|
-
glyph: to
|
|
356
|
-
}
|
|
357
|
-
});
|
|
358
|
-
}
|
|
57
|
+
const HANDLE_HEAD = 12;
|
|
58
|
+
const HANDLE_BAR = 2;
|
|
59
|
+
const HANDLE_PAD = 14;
|
|
60
|
+
/** Tilts within ~0.05° of upright render untransformed (float-noise guard —
|
|
61
|
+
* and the dominant case stays pixel-identical to an axis-aligned box). */
|
|
62
|
+
const HANDLE_ROT_EPSILON = .05;
|
|
63
|
+
const midpoint = (a, b) => ({
|
|
64
|
+
x: (a.x + b.x) / 2,
|
|
65
|
+
y: (a.y + b.y) / 2
|
|
66
|
+
});
|
|
67
|
+
/**
|
|
68
|
+
* The handle's geometry for one endpoint, in overlay space. Null when the
|
|
69
|
+
* endpoint's page isn't laid out (or the cell is degenerate) — render nothing.
|
|
70
|
+
*/
|
|
71
|
+
function selectionHandleGeom(view, ep, role) {
|
|
72
|
+
const leading = role === "start" ? ep.advance > 0 : ep.advance < 0;
|
|
73
|
+
const [upperPage, lowerPage] = (0, _embedpdf_core_geometry.textQuadEdge)(ep.glyphQuad, leading ? "start" : "end");
|
|
74
|
+
const upper = view.toOverlay(ep.pon, upperPage);
|
|
75
|
+
const lower = view.toOverlay(ep.pon, lowerPage);
|
|
76
|
+
if (!upper || !lower) return null;
|
|
77
|
+
const dx = lower.x - upper.x;
|
|
78
|
+
const dy = lower.y - upper.y;
|
|
79
|
+
const length = Math.hypot(dx, dy);
|
|
80
|
+
if (length === 0) return null;
|
|
81
|
+
const rotation = Math.atan2(dy, dx) * 180 / Math.PI - 90;
|
|
82
|
+
const upright = Math.abs(rotation) < HANDLE_ROT_EPSILON;
|
|
83
|
+
const ux = dx / length;
|
|
84
|
+
const uy = dy / length;
|
|
85
|
+
const head = role === "start" ? {
|
|
86
|
+
x: upper.x - ux * 12 / 2,
|
|
87
|
+
y: upper.y - uy * 12 / 2
|
|
88
|
+
} : {
|
|
89
|
+
x: lower.x + ux * 12 / 2,
|
|
90
|
+
y: lower.y + uy * 12 / 2
|
|
91
|
+
};
|
|
359
92
|
return {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
const text = cache.get(pon);
|
|
364
|
-
return text ? glyphAt(text, point) != null : false;
|
|
365
|
-
},
|
|
366
|
-
beginAt: (pon, point) => {
|
|
367
|
-
const text = cache.get(pon);
|
|
368
|
-
if (!text) return false;
|
|
369
|
-
const i = glyphAt(text, point);
|
|
370
|
-
if (i == null) return false;
|
|
371
|
-
recompute({
|
|
372
|
-
anchor: {
|
|
373
|
-
pon,
|
|
374
|
-
glyph: i
|
|
375
|
-
},
|
|
376
|
-
focus: {
|
|
377
|
-
pon,
|
|
378
|
-
glyph: i
|
|
379
|
-
}
|
|
380
|
-
});
|
|
381
|
-
return true;
|
|
382
|
-
},
|
|
383
|
-
selectWord: (pon, point) => selectSpan(pon, point, "word"),
|
|
384
|
-
selectLine: (pon, point) => selectSpan(pon, point, "line"),
|
|
385
|
-
extendTo: (pon, point) => {
|
|
386
|
-
const cur = ctx.getState().selection;
|
|
387
|
-
if (!cur) return;
|
|
388
|
-
const text = cache.get(pon);
|
|
389
|
-
if (!text) {
|
|
390
|
-
ensurePage(pon);
|
|
391
|
-
return;
|
|
392
|
-
}
|
|
393
|
-
const i = glyphAt(text, point);
|
|
394
|
-
if (i == null) return;
|
|
395
|
-
recompute({
|
|
396
|
-
anchor: cur.anchor,
|
|
397
|
-
focus: {
|
|
398
|
-
pon,
|
|
399
|
-
glyph: i
|
|
400
|
-
}
|
|
401
|
-
});
|
|
402
|
-
},
|
|
403
|
-
end: () => commitCbs.forEach((cb) => cb()),
|
|
404
|
-
clear: () => {
|
|
405
|
-
ctx.dispatch({ type: "CLEAR" });
|
|
406
|
-
fireChange();
|
|
407
|
-
},
|
|
408
|
-
snapshot,
|
|
409
|
-
rectsForPage: (pon) => ctx.getState().rects[pon] ?? EMPTY,
|
|
410
|
-
hasSelection: () => ctx.getState().selection != null,
|
|
411
|
-
selectedPages: () => {
|
|
412
|
-
const { rects } = ctx.getState();
|
|
413
|
-
return Object.keys(rects).map(Number).filter((pon) => (rects[pon]?.length ?? 0) > 0);
|
|
93
|
+
bar: {
|
|
94
|
+
from: upper,
|
|
95
|
+
to: lower
|
|
414
96
|
},
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
onCommit: (cb) => {
|
|
420
|
-
commitCbs.add(cb);
|
|
421
|
-
return () => commitCbs.delete(cb);
|
|
422
|
-
},
|
|
423
|
-
setHighlightVisible: (visible) => ctx.dispatch({
|
|
424
|
-
type: "SET_HIGHLIGHT_HIDDEN",
|
|
425
|
-
hidden: !visible
|
|
426
|
-
}),
|
|
427
|
-
highlightVisible: () => !ctx.getState().highlightHidden
|
|
97
|
+
length,
|
|
98
|
+
rotation,
|
|
99
|
+
upright,
|
|
100
|
+
head
|
|
428
101
|
};
|
|
429
102
|
}
|
|
430
|
-
//#endregion
|
|
431
|
-
//#region src/handler.ts
|
|
432
|
-
const CURSOR_TOKEN = "selection-text";
|
|
433
|
-
const DRAG_THRESHOLD_PX = 4;
|
|
434
103
|
/**
|
|
435
|
-
*
|
|
436
|
-
*
|
|
437
|
-
*
|
|
438
|
-
*
|
|
439
|
-
*
|
|
440
|
-
*
|
|
441
|
-
*
|
|
442
|
-
*
|
|
443
|
-
* - triple-click → select the visual line
|
|
444
|
-
* - down off-text → deselect, and DON'T capture (so it can't block)
|
|
445
|
-
* - hover → I-beam only when over text, else the pointer cursor
|
|
104
|
+
* One armed handle drag. Dragging a handle extends from the OPPOSITE
|
|
105
|
+
* endpoint: the first movement re-roots the selection gesture at that
|
|
106
|
+
* endpoint's cell centre, and every position then extends toward the pointer
|
|
107
|
+
* — snapping to glyphs and crossing pages exactly like a pointer drag,
|
|
108
|
+
* firing the same change/commit signals. Over a gap the point projects onto
|
|
109
|
+
* the last page hit (unclamped), so the selection keeps tracking instead of
|
|
110
|
+
* freezing at a page edge. `end()` commits only if the drag actually
|
|
111
|
+
* re-rooted; an untouched press settles nothing.
|
|
446
112
|
*/
|
|
447
|
-
function
|
|
448
|
-
|
|
449
|
-
|
|
113
|
+
function createSelectionHandleDrag(selection, view, opposite, draggedPon) {
|
|
114
|
+
const q = opposite.glyphQuad;
|
|
115
|
+
const anchorPoint = midpoint(midpoint(q.upperStart, q.lowerEnd), midpoint(q.upperEnd, q.lowerStart));
|
|
116
|
+
let begun = false;
|
|
117
|
+
let lastPon = draggedPon;
|
|
450
118
|
return {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
anchor = null;
|
|
456
|
-
dragging = false;
|
|
457
|
-
if (!s.page) return false;
|
|
458
|
-
const { pon, point } = s.page;
|
|
459
|
-
const clicks = s.clickCount ?? 1;
|
|
460
|
-
if (clicks >= 3) {
|
|
461
|
-
selection.selectLine(pon, point);
|
|
462
|
-
return true;
|
|
119
|
+
move: (overlay) => {
|
|
120
|
+
if (!begun) {
|
|
121
|
+
if (!selection.beginAt(opposite.pon, anchorPoint)) return;
|
|
122
|
+
begun = true;
|
|
463
123
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
124
|
+
const hit = view.pageAt(overlay);
|
|
125
|
+
if (hit) {
|
|
126
|
+
lastPon = hit.pon;
|
|
127
|
+
selection.extendTo(hit.pon, hit.point);
|
|
128
|
+
} else {
|
|
129
|
+
const p = view.pointOnPage(lastPon, overlay);
|
|
130
|
+
if (p) selection.extendTo(lastPon, p);
|
|
467
131
|
}
|
|
468
|
-
selection.clear();
|
|
469
|
-
if (!selection.isOverText(pon, point)) return false;
|
|
470
|
-
anchor = {
|
|
471
|
-
pon,
|
|
472
|
-
point,
|
|
473
|
-
vx: s.viewport.x,
|
|
474
|
-
vy: s.viewport.y
|
|
475
|
-
};
|
|
476
|
-
return true;
|
|
477
132
|
},
|
|
478
|
-
|
|
479
|
-
if (
|
|
480
|
-
if (!dragging) {
|
|
481
|
-
if (Math.hypot(s.viewport.x - anchor.vx, s.viewport.y - anchor.vy) < DRAG_THRESHOLD_PX) return;
|
|
482
|
-
dragging = true;
|
|
483
|
-
selection.beginAt(anchor.pon, anchor.point);
|
|
484
|
-
}
|
|
485
|
-
selection.extendTo(s.page.pon, s.page.point);
|
|
486
|
-
},
|
|
487
|
-
onUp: () => {
|
|
488
|
-
anchor = null;
|
|
489
|
-
dragging = false;
|
|
490
|
-
selection.end();
|
|
491
|
-
},
|
|
492
|
-
onHover: (s) => {
|
|
493
|
-
if (!s.page) {
|
|
494
|
-
interaction.setCursor(CURSOR_TOKEN, null);
|
|
495
|
-
return;
|
|
496
|
-
}
|
|
497
|
-
selection.ensurePage(s.page.pon);
|
|
498
|
-
const overText = selection.isOverText(s.page.pon, s.page.point);
|
|
499
|
-
interaction.setCursor(CURSOR_TOKEN, overText ? "text" : null, 10);
|
|
133
|
+
end: () => {
|
|
134
|
+
if (begun) selection.end();
|
|
500
135
|
}
|
|
501
136
|
};
|
|
502
137
|
}
|
|
503
138
|
//#endregion
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
};
|
|
511
|
-
const selectionReducer = (state, action) => {
|
|
512
|
-
switch (action.type) {
|
|
513
|
-
case "PAGE_LOADED": return state.loaded[action.pon] ? state : {
|
|
514
|
-
...state,
|
|
515
|
-
loaded: {
|
|
516
|
-
...state.loaded,
|
|
517
|
-
[action.pon]: true
|
|
518
|
-
}
|
|
519
|
-
};
|
|
520
|
-
case "SET": return {
|
|
521
|
-
...state,
|
|
522
|
-
selection: action.selection,
|
|
523
|
-
rects: action.rects
|
|
524
|
-
};
|
|
525
|
-
case "CLEAR": return state.selection === null && Object.keys(state.rects).length === 0 ? state : {
|
|
526
|
-
...state,
|
|
527
|
-
selection: null,
|
|
528
|
-
rects: {}
|
|
529
|
-
};
|
|
530
|
-
case "SET_HIGHLIGHT_HIDDEN": return state.highlightHidden === action.hidden ? state : {
|
|
531
|
-
...state,
|
|
532
|
-
highlightHidden: action.hidden
|
|
533
|
-
};
|
|
534
|
-
default: return state;
|
|
535
|
-
}
|
|
536
|
-
};
|
|
537
|
-
//#endregion
|
|
538
|
-
//#region src/types.ts
|
|
539
|
-
const SelectionToken = (0, _embedpdf_core.createCapabilityToken)("selection");
|
|
540
|
-
//#endregion
|
|
541
|
-
//#region src/selection.plugin.ts
|
|
542
|
-
/**
|
|
543
|
-
* Text selection — document-scoped, requires the interaction hub. In `init` it
|
|
544
|
-
* registers ITS pointer handler with the hub; the hub owns the pointer stream
|
|
545
|
-
* and arbitration. Works with `<Stage>` or a standalone `<PageView>` — selection
|
|
546
|
-
* only needs the page coordinate context + the engine's text geometry.
|
|
547
|
-
*/
|
|
548
|
-
const selectionPlugin = () => (0, _embedpdf_core.definePlugin)({
|
|
549
|
-
id: "selection",
|
|
550
|
-
token: SelectionToken,
|
|
551
|
-
scope: "document",
|
|
552
|
-
requires: [_embedpdf_plugin_interaction.InteractionToken],
|
|
553
|
-
initialState: initialSelectionState,
|
|
554
|
-
reduce: selectionReducer,
|
|
555
|
-
capability: createSelectionCapability,
|
|
556
|
-
init: (ctx) => {
|
|
557
|
-
const interaction = ctx.get(_embedpdf_plugin_interaction.InteractionToken);
|
|
558
|
-
const selection = ctx.get(SelectionToken);
|
|
559
|
-
interaction.registerHandler(createTextSelectHandler(selection, interaction));
|
|
560
|
-
}
|
|
561
|
-
});
|
|
562
|
-
//#endregion
|
|
563
|
-
exports.SelectionToken = SelectionToken;
|
|
564
|
-
exports.buildPageText = buildPageText;
|
|
565
|
-
exports.createSelectionCapability = createSelectionCapability;
|
|
566
|
-
exports.createTextSelectHandler = createTextSelectHandler;
|
|
567
|
-
exports.expandToLine = expandToLine;
|
|
568
|
-
exports.expandToWord = expandToWord;
|
|
569
|
-
exports.glyphAt = glyphAt;
|
|
570
|
-
exports.initialSelectionState = initialSelectionState;
|
|
571
|
-
exports.rectsForRange = rectsForRange;
|
|
139
|
+
exports.HANDLE_BAR = HANDLE_BAR;
|
|
140
|
+
exports.HANDLE_HEAD = HANDLE_HEAD;
|
|
141
|
+
exports.HANDLE_PAD = HANDLE_PAD;
|
|
142
|
+
exports.SelectionToken = require_contract.SelectionToken;
|
|
143
|
+
exports.createSelectionHandleDrag = createSelectionHandleDrag;
|
|
144
|
+
exports.selectionHandleGeom = selectionHandleGeom;
|
|
572
145
|
exports.selectionPlugin = selectionPlugin;
|
|
573
|
-
exports.selectionReducer = selectionReducer;
|
|
574
146
|
|
|
575
147
|
//# sourceMappingURL=index.cjs.map
|