@shadow-garden/bapbong-selection 0.1.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/LICENSE +21 -0
- package/README.md +22 -0
- package/dist/index.cjs +269 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +238 -0
- package/dist/lib/selection.d.ts +28 -0
- package/dist/lib/selection.d.ts.map +1 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Le Phuoc Minh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# @shadow-garden/bapbong-selection
|
|
2
|
+
|
|
3
|
+
Pure caret/selection math over a `ResolvedLayout`. No DOM, no ProseMirror
|
|
4
|
+
dependency — positions come from the layout (`LayoutSegment.pos`,
|
|
5
|
+
`LayoutLine.from/to`) and text widths from an injected `MeasureText`.
|
|
6
|
+
|
|
7
|
+
- **Scope:** `scope:selection`
|
|
8
|
+
- **Depends on:** `@shadow-garden/bapbong-contracts`
|
|
9
|
+
|
|
10
|
+
## Public API
|
|
11
|
+
|
|
12
|
+
- `hitTest(layout, point, measure)` — page-local `(x, y)` → ProseMirror position
|
|
13
|
+
- `caretRect(layout, pos, measure)` — PM position → caret rectangle (with page index)
|
|
14
|
+
- `selectionRects(layout, from, to, measure)` — selection range → highlight rects
|
|
15
|
+
- `verticalCaret(layout, pos, dir, goalX, measure)` — Up/Down caret motion
|
|
16
|
+
|
|
17
|
+
## Build / test
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
pnpm nx build @shadow-garden/bapbong-selection
|
|
21
|
+
pnpm nx test @shadow-garden/bapbong-selection
|
|
22
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// packages/selection/src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
caretRect: () => caretRect,
|
|
24
|
+
hitTest: () => hitTest,
|
|
25
|
+
imageAtPoint: () => imageAtPoint,
|
|
26
|
+
selectionRects: () => selectionRects,
|
|
27
|
+
verticalCaret: () => verticalCaret
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(index_exports);
|
|
30
|
+
|
|
31
|
+
// packages/selection/src/lib/selection.ts
|
|
32
|
+
var EMPTY_LINE_RECT_WIDTH = 6;
|
|
33
|
+
function allLines(page) {
|
|
34
|
+
const lines = [...page.lines];
|
|
35
|
+
const visit = (t) => {
|
|
36
|
+
for (const cell of t.cells) {
|
|
37
|
+
lines.push(...cell.lines);
|
|
38
|
+
cell.tables?.forEach(visit);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
page.tables?.forEach(visit);
|
|
42
|
+
return lines.filter((l) => l.from != null).sort((a, b) => a.y - b.y || a.x - b.x);
|
|
43
|
+
}
|
|
44
|
+
function lineItems(line, measure) {
|
|
45
|
+
const items = [];
|
|
46
|
+
for (const seg of line.segments) {
|
|
47
|
+
if (seg.pos == null) continue;
|
|
48
|
+
if (seg.field) {
|
|
49
|
+
items.push({ x: seg.x, width: seg.width ?? measure(seg.text, seg.font), pos: seg.pos, size: 1 });
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
items.push({
|
|
53
|
+
x: seg.x,
|
|
54
|
+
width: measure(seg.text, seg.font),
|
|
55
|
+
pos: seg.pos,
|
|
56
|
+
size: seg.text.length,
|
|
57
|
+
text: seg.text,
|
|
58
|
+
font: seg.font
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
for (const img of line.images ?? []) {
|
|
62
|
+
if (img.pos == null) continue;
|
|
63
|
+
items.push({ x: img.x, width: img.width, pos: img.pos, size: 1 });
|
|
64
|
+
}
|
|
65
|
+
return items.sort((a, b) => a.x - b.x);
|
|
66
|
+
}
|
|
67
|
+
function caretXOnLine(line, pos, measure) {
|
|
68
|
+
const items = lineItems(line, measure);
|
|
69
|
+
if (items.length === 0) return line.x;
|
|
70
|
+
const first = items[0];
|
|
71
|
+
if (pos <= first.pos) return first.x;
|
|
72
|
+
for (const it of items) {
|
|
73
|
+
if (pos > it.pos + it.size) continue;
|
|
74
|
+
if (pos <= it.pos) return it.x;
|
|
75
|
+
if (it.text != null && it.font) {
|
|
76
|
+
return it.x + measure(it.text.slice(0, pos - it.pos), it.font);
|
|
77
|
+
}
|
|
78
|
+
return pos === it.pos ? it.x : it.x + it.width;
|
|
79
|
+
}
|
|
80
|
+
const last = items[items.length - 1];
|
|
81
|
+
return last.x + last.width;
|
|
82
|
+
}
|
|
83
|
+
function posAtLineX(line, x, measure) {
|
|
84
|
+
const items = lineItems(line, measure);
|
|
85
|
+
if (items.length === 0) return line.from ?? 0;
|
|
86
|
+
if (x <= items[0].x) return items[0].pos;
|
|
87
|
+
for (let i = 0; i < items.length; i++) {
|
|
88
|
+
const it = items[i];
|
|
89
|
+
const end = it.x + it.width;
|
|
90
|
+
if (x <= end) {
|
|
91
|
+
if (it.text != null && it.font) {
|
|
92
|
+
let prevW = 0;
|
|
93
|
+
for (let c = 1; c <= it.size; c++) {
|
|
94
|
+
const w = measure(it.text.slice(0, c), it.font);
|
|
95
|
+
if (x < it.x + (prevW + w) / 2) return it.pos + c - 1;
|
|
96
|
+
prevW = w;
|
|
97
|
+
}
|
|
98
|
+
return it.pos + it.size;
|
|
99
|
+
}
|
|
100
|
+
return x < it.x + it.width / 2 ? it.pos : it.pos + it.size;
|
|
101
|
+
}
|
|
102
|
+
const next = items[i + 1];
|
|
103
|
+
if (next && x < next.x) {
|
|
104
|
+
return x < (end + next.x) / 2 ? it.pos + it.size : next.pos;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const last = items[items.length - 1];
|
|
108
|
+
return last.pos + last.size;
|
|
109
|
+
}
|
|
110
|
+
function lineForPos(lines, pos) {
|
|
111
|
+
for (const line of lines) {
|
|
112
|
+
const from = line.from;
|
|
113
|
+
const to = line.to;
|
|
114
|
+
if (pos >= from && pos < to || from === to && pos === from) return line;
|
|
115
|
+
}
|
|
116
|
+
let best = null;
|
|
117
|
+
for (const line of lines) {
|
|
118
|
+
const to = line.to;
|
|
119
|
+
if (to <= pos && (best == null || to > best.to)) best = line;
|
|
120
|
+
}
|
|
121
|
+
return best;
|
|
122
|
+
}
|
|
123
|
+
function hitTest(layout, point, measure) {
|
|
124
|
+
const page = layout.pages[point.pageIndex];
|
|
125
|
+
if (!page) return null;
|
|
126
|
+
const lines = allLines(page);
|
|
127
|
+
if (lines.length === 0) return null;
|
|
128
|
+
const hits = lines.filter((l) => point.y >= l.y && point.y < l.y + l.height);
|
|
129
|
+
let target;
|
|
130
|
+
if (hits.length > 0) {
|
|
131
|
+
const xDist = (l) => point.x < l.x ? l.x - point.x : point.x > l.x + l.width ? point.x - (l.x + l.width) : 0;
|
|
132
|
+
target = hits.reduce((a, b) => xDist(b) < xDist(a) ? b : a);
|
|
133
|
+
} else {
|
|
134
|
+
const yDist = (l) => point.y < l.y ? l.y - point.y : point.y - (l.y + l.height);
|
|
135
|
+
target = lines.reduce((a, b) => yDist(b) < yDist(a) ? b : a);
|
|
136
|
+
}
|
|
137
|
+
return posAtLineX(target, point.x, measure);
|
|
138
|
+
}
|
|
139
|
+
function caretRect(layout, pos, measure) {
|
|
140
|
+
for (const page of layout.pages) {
|
|
141
|
+
const lines = allLines(page);
|
|
142
|
+
if (lines.length === 0) continue;
|
|
143
|
+
const line = lineForPos(lines, pos);
|
|
144
|
+
if (!line) continue;
|
|
145
|
+
const maxTo = Math.max(...lines.map((l) => l.to));
|
|
146
|
+
if (pos > maxTo && page.index < layout.pages.length - 1) continue;
|
|
147
|
+
const clamped = Math.min(pos, line.to);
|
|
148
|
+
return {
|
|
149
|
+
pageIndex: page.index,
|
|
150
|
+
x: caretXOnLine(line, clamped, measure),
|
|
151
|
+
y: line.y,
|
|
152
|
+
height: line.height
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
function selectionRects(layout, from, to, measure) {
|
|
158
|
+
if (to <= from) return [];
|
|
159
|
+
const rects = [];
|
|
160
|
+
for (const page of layout.pages) {
|
|
161
|
+
for (const line of allLines(page)) {
|
|
162
|
+
const lineFrom = line.from;
|
|
163
|
+
const lineTo = line.to;
|
|
164
|
+
if (lineFrom === lineTo) {
|
|
165
|
+
if (from <= lineFrom && to > lineTo) {
|
|
166
|
+
rects.push({ pageIndex: page.index, x: line.x, y: line.y, width: EMPTY_LINE_RECT_WIDTH, height: line.height });
|
|
167
|
+
}
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const s = Math.max(from, lineFrom);
|
|
171
|
+
const e = Math.min(to, lineTo);
|
|
172
|
+
if (e <= s) continue;
|
|
173
|
+
const x1 = caretXOnLine(line, s, measure);
|
|
174
|
+
const x2 = caretXOnLine(line, e, measure);
|
|
175
|
+
rects.push({ pageIndex: page.index, x: x1, y: line.y, width: Math.max(x2 - x1, 2), height: line.height });
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return rects;
|
|
179
|
+
}
|
|
180
|
+
function verticalCaret(layout, pos, dir, goalX, measure) {
|
|
181
|
+
let pageIdx = -1;
|
|
182
|
+
let current = null;
|
|
183
|
+
for (const page of layout.pages) {
|
|
184
|
+
const lines = allLines(page);
|
|
185
|
+
if (lines.length === 0) continue;
|
|
186
|
+
const line = lineForPos(lines, pos);
|
|
187
|
+
if (line && pos <= Math.max(...lines.map((l) => l.to))) {
|
|
188
|
+
pageIdx = page.index;
|
|
189
|
+
current = line;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (!current || pageIdx < 0) return null;
|
|
194
|
+
const pick = (cands2, extremeTop) => {
|
|
195
|
+
if (cands2.length === 0) return null;
|
|
196
|
+
const edgeY = extremeTop ? Math.min(...cands2.map((l) => l.y)) : Math.max(...cands2.map((l) => l.y));
|
|
197
|
+
const band = cands2.filter((l) => Math.abs(l.y - edgeY) < 0.5);
|
|
198
|
+
const xDist = (l) => goalX < l.x ? l.x - goalX : goalX > l.x + l.width ? goalX - (l.x + l.width) : 0;
|
|
199
|
+
return band.reduce((a, b) => xDist(b) < xDist(a) ? b : a);
|
|
200
|
+
};
|
|
201
|
+
const sameLines = allLines(layout.pages[pageIdx]);
|
|
202
|
+
const cands = dir === 1 ? sameLines.filter((l) => l.y > current.y + 0.5) : sameLines.filter((l) => l.y < current.y - 0.5);
|
|
203
|
+
let target = pick(cands, dir === 1);
|
|
204
|
+
if (!target) {
|
|
205
|
+
const nextPage = layout.pages[pageIdx + dir];
|
|
206
|
+
if (!nextPage) return null;
|
|
207
|
+
target = pick(allLines(nextPage), dir === 1);
|
|
208
|
+
if (!target) return null;
|
|
209
|
+
}
|
|
210
|
+
return posAtLineX(target, goalX, measure);
|
|
211
|
+
}
|
|
212
|
+
function imageAtPoint(layout, point) {
|
|
213
|
+
const page = layout.pages[point.pageIndex];
|
|
214
|
+
if (!page) return null;
|
|
215
|
+
const inside = (x, y, w, h, rotation) => {
|
|
216
|
+
let px = point.x;
|
|
217
|
+
let py = point.y;
|
|
218
|
+
if (rotation) {
|
|
219
|
+
const rad = -rotation * Math.PI / 180;
|
|
220
|
+
const cx = x + w / 2;
|
|
221
|
+
const cy = y + h / 2;
|
|
222
|
+
const dx = px - cx;
|
|
223
|
+
const dy = py - cy;
|
|
224
|
+
px = cx + dx * Math.cos(rad) - dy * Math.sin(rad);
|
|
225
|
+
py = cy + dx * Math.sin(rad) + dy * Math.cos(rad);
|
|
226
|
+
}
|
|
227
|
+
return px >= x && px <= x + w && py >= y && py <= y + h;
|
|
228
|
+
};
|
|
229
|
+
const floats = [...page.floats ?? []];
|
|
230
|
+
const visitCells = (t) => {
|
|
231
|
+
for (const cell of t.cells) {
|
|
232
|
+
floats.push(...cell.floats ?? []);
|
|
233
|
+
cell.tables?.forEach(visitCells);
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
page.tables?.forEach(visitCells);
|
|
237
|
+
for (let i = floats.length - 1; i >= 0; i--) {
|
|
238
|
+
const f = floats[i];
|
|
239
|
+
if (f.pos == null || !inside(f.x, f.y, f.width, f.height, f.rotation)) continue;
|
|
240
|
+
return {
|
|
241
|
+
pos: f.pos,
|
|
242
|
+
pageIndex: point.pageIndex,
|
|
243
|
+
rect: { x: f.x, y: f.y, width: f.width, height: f.height },
|
|
244
|
+
kind: "float"
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
for (const line of allLines(page)) {
|
|
248
|
+
for (const img of line.images ?? []) {
|
|
249
|
+
if (img.pos == null) continue;
|
|
250
|
+
const top = line.y + line.baseline - img.height;
|
|
251
|
+
if (!inside(img.x, top, img.width, img.height, img.rotation)) continue;
|
|
252
|
+
return {
|
|
253
|
+
pos: img.pos,
|
|
254
|
+
pageIndex: point.pageIndex,
|
|
255
|
+
rect: { x: img.x, y: top, width: img.width, height: img.height },
|
|
256
|
+
kind: "inline"
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
263
|
+
0 && (module.exports = {
|
|
264
|
+
caretRect,
|
|
265
|
+
hitTest,
|
|
266
|
+
imageAtPoint,
|
|
267
|
+
selectionRects,
|
|
268
|
+
verticalCaret
|
|
269
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
// packages/selection/src/lib/selection.ts
|
|
2
|
+
var EMPTY_LINE_RECT_WIDTH = 6;
|
|
3
|
+
function allLines(page) {
|
|
4
|
+
const lines = [...page.lines];
|
|
5
|
+
const visit = (t) => {
|
|
6
|
+
for (const cell of t.cells) {
|
|
7
|
+
lines.push(...cell.lines);
|
|
8
|
+
cell.tables?.forEach(visit);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
page.tables?.forEach(visit);
|
|
12
|
+
return lines.filter((l) => l.from != null).sort((a, b) => a.y - b.y || a.x - b.x);
|
|
13
|
+
}
|
|
14
|
+
function lineItems(line, measure) {
|
|
15
|
+
const items = [];
|
|
16
|
+
for (const seg of line.segments) {
|
|
17
|
+
if (seg.pos == null) continue;
|
|
18
|
+
if (seg.field) {
|
|
19
|
+
items.push({ x: seg.x, width: seg.width ?? measure(seg.text, seg.font), pos: seg.pos, size: 1 });
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
items.push({
|
|
23
|
+
x: seg.x,
|
|
24
|
+
width: measure(seg.text, seg.font),
|
|
25
|
+
pos: seg.pos,
|
|
26
|
+
size: seg.text.length,
|
|
27
|
+
text: seg.text,
|
|
28
|
+
font: seg.font
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
for (const img of line.images ?? []) {
|
|
32
|
+
if (img.pos == null) continue;
|
|
33
|
+
items.push({ x: img.x, width: img.width, pos: img.pos, size: 1 });
|
|
34
|
+
}
|
|
35
|
+
return items.sort((a, b) => a.x - b.x);
|
|
36
|
+
}
|
|
37
|
+
function caretXOnLine(line, pos, measure) {
|
|
38
|
+
const items = lineItems(line, measure);
|
|
39
|
+
if (items.length === 0) return line.x;
|
|
40
|
+
const first = items[0];
|
|
41
|
+
if (pos <= first.pos) return first.x;
|
|
42
|
+
for (const it of items) {
|
|
43
|
+
if (pos > it.pos + it.size) continue;
|
|
44
|
+
if (pos <= it.pos) return it.x;
|
|
45
|
+
if (it.text != null && it.font) {
|
|
46
|
+
return it.x + measure(it.text.slice(0, pos - it.pos), it.font);
|
|
47
|
+
}
|
|
48
|
+
return pos === it.pos ? it.x : it.x + it.width;
|
|
49
|
+
}
|
|
50
|
+
const last = items[items.length - 1];
|
|
51
|
+
return last.x + last.width;
|
|
52
|
+
}
|
|
53
|
+
function posAtLineX(line, x, measure) {
|
|
54
|
+
const items = lineItems(line, measure);
|
|
55
|
+
if (items.length === 0) return line.from ?? 0;
|
|
56
|
+
if (x <= items[0].x) return items[0].pos;
|
|
57
|
+
for (let i = 0; i < items.length; i++) {
|
|
58
|
+
const it = items[i];
|
|
59
|
+
const end = it.x + it.width;
|
|
60
|
+
if (x <= end) {
|
|
61
|
+
if (it.text != null && it.font) {
|
|
62
|
+
let prevW = 0;
|
|
63
|
+
for (let c = 1; c <= it.size; c++) {
|
|
64
|
+
const w = measure(it.text.slice(0, c), it.font);
|
|
65
|
+
if (x < it.x + (prevW + w) / 2) return it.pos + c - 1;
|
|
66
|
+
prevW = w;
|
|
67
|
+
}
|
|
68
|
+
return it.pos + it.size;
|
|
69
|
+
}
|
|
70
|
+
return x < it.x + it.width / 2 ? it.pos : it.pos + it.size;
|
|
71
|
+
}
|
|
72
|
+
const next = items[i + 1];
|
|
73
|
+
if (next && x < next.x) {
|
|
74
|
+
return x < (end + next.x) / 2 ? it.pos + it.size : next.pos;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const last = items[items.length - 1];
|
|
78
|
+
return last.pos + last.size;
|
|
79
|
+
}
|
|
80
|
+
function lineForPos(lines, pos) {
|
|
81
|
+
for (const line of lines) {
|
|
82
|
+
const from = line.from;
|
|
83
|
+
const to = line.to;
|
|
84
|
+
if (pos >= from && pos < to || from === to && pos === from) return line;
|
|
85
|
+
}
|
|
86
|
+
let best = null;
|
|
87
|
+
for (const line of lines) {
|
|
88
|
+
const to = line.to;
|
|
89
|
+
if (to <= pos && (best == null || to > best.to)) best = line;
|
|
90
|
+
}
|
|
91
|
+
return best;
|
|
92
|
+
}
|
|
93
|
+
function hitTest(layout, point, measure) {
|
|
94
|
+
const page = layout.pages[point.pageIndex];
|
|
95
|
+
if (!page) return null;
|
|
96
|
+
const lines = allLines(page);
|
|
97
|
+
if (lines.length === 0) return null;
|
|
98
|
+
const hits = lines.filter((l) => point.y >= l.y && point.y < l.y + l.height);
|
|
99
|
+
let target;
|
|
100
|
+
if (hits.length > 0) {
|
|
101
|
+
const xDist = (l) => point.x < l.x ? l.x - point.x : point.x > l.x + l.width ? point.x - (l.x + l.width) : 0;
|
|
102
|
+
target = hits.reduce((a, b) => xDist(b) < xDist(a) ? b : a);
|
|
103
|
+
} else {
|
|
104
|
+
const yDist = (l) => point.y < l.y ? l.y - point.y : point.y - (l.y + l.height);
|
|
105
|
+
target = lines.reduce((a, b) => yDist(b) < yDist(a) ? b : a);
|
|
106
|
+
}
|
|
107
|
+
return posAtLineX(target, point.x, measure);
|
|
108
|
+
}
|
|
109
|
+
function caretRect(layout, pos, measure) {
|
|
110
|
+
for (const page of layout.pages) {
|
|
111
|
+
const lines = allLines(page);
|
|
112
|
+
if (lines.length === 0) continue;
|
|
113
|
+
const line = lineForPos(lines, pos);
|
|
114
|
+
if (!line) continue;
|
|
115
|
+
const maxTo = Math.max(...lines.map((l) => l.to));
|
|
116
|
+
if (pos > maxTo && page.index < layout.pages.length - 1) continue;
|
|
117
|
+
const clamped = Math.min(pos, line.to);
|
|
118
|
+
return {
|
|
119
|
+
pageIndex: page.index,
|
|
120
|
+
x: caretXOnLine(line, clamped, measure),
|
|
121
|
+
y: line.y,
|
|
122
|
+
height: line.height
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
function selectionRects(layout, from, to, measure) {
|
|
128
|
+
if (to <= from) return [];
|
|
129
|
+
const rects = [];
|
|
130
|
+
for (const page of layout.pages) {
|
|
131
|
+
for (const line of allLines(page)) {
|
|
132
|
+
const lineFrom = line.from;
|
|
133
|
+
const lineTo = line.to;
|
|
134
|
+
if (lineFrom === lineTo) {
|
|
135
|
+
if (from <= lineFrom && to > lineTo) {
|
|
136
|
+
rects.push({ pageIndex: page.index, x: line.x, y: line.y, width: EMPTY_LINE_RECT_WIDTH, height: line.height });
|
|
137
|
+
}
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
const s = Math.max(from, lineFrom);
|
|
141
|
+
const e = Math.min(to, lineTo);
|
|
142
|
+
if (e <= s) continue;
|
|
143
|
+
const x1 = caretXOnLine(line, s, measure);
|
|
144
|
+
const x2 = caretXOnLine(line, e, measure);
|
|
145
|
+
rects.push({ pageIndex: page.index, x: x1, y: line.y, width: Math.max(x2 - x1, 2), height: line.height });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return rects;
|
|
149
|
+
}
|
|
150
|
+
function verticalCaret(layout, pos, dir, goalX, measure) {
|
|
151
|
+
let pageIdx = -1;
|
|
152
|
+
let current = null;
|
|
153
|
+
for (const page of layout.pages) {
|
|
154
|
+
const lines = allLines(page);
|
|
155
|
+
if (lines.length === 0) continue;
|
|
156
|
+
const line = lineForPos(lines, pos);
|
|
157
|
+
if (line && pos <= Math.max(...lines.map((l) => l.to))) {
|
|
158
|
+
pageIdx = page.index;
|
|
159
|
+
current = line;
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (!current || pageIdx < 0) return null;
|
|
164
|
+
const pick = (cands2, extremeTop) => {
|
|
165
|
+
if (cands2.length === 0) return null;
|
|
166
|
+
const edgeY = extremeTop ? Math.min(...cands2.map((l) => l.y)) : Math.max(...cands2.map((l) => l.y));
|
|
167
|
+
const band = cands2.filter((l) => Math.abs(l.y - edgeY) < 0.5);
|
|
168
|
+
const xDist = (l) => goalX < l.x ? l.x - goalX : goalX > l.x + l.width ? goalX - (l.x + l.width) : 0;
|
|
169
|
+
return band.reduce((a, b) => xDist(b) < xDist(a) ? b : a);
|
|
170
|
+
};
|
|
171
|
+
const sameLines = allLines(layout.pages[pageIdx]);
|
|
172
|
+
const cands = dir === 1 ? sameLines.filter((l) => l.y > current.y + 0.5) : sameLines.filter((l) => l.y < current.y - 0.5);
|
|
173
|
+
let target = pick(cands, dir === 1);
|
|
174
|
+
if (!target) {
|
|
175
|
+
const nextPage = layout.pages[pageIdx + dir];
|
|
176
|
+
if (!nextPage) return null;
|
|
177
|
+
target = pick(allLines(nextPage), dir === 1);
|
|
178
|
+
if (!target) return null;
|
|
179
|
+
}
|
|
180
|
+
return posAtLineX(target, goalX, measure);
|
|
181
|
+
}
|
|
182
|
+
function imageAtPoint(layout, point) {
|
|
183
|
+
const page = layout.pages[point.pageIndex];
|
|
184
|
+
if (!page) return null;
|
|
185
|
+
const inside = (x, y, w, h, rotation) => {
|
|
186
|
+
let px = point.x;
|
|
187
|
+
let py = point.y;
|
|
188
|
+
if (rotation) {
|
|
189
|
+
const rad = -rotation * Math.PI / 180;
|
|
190
|
+
const cx = x + w / 2;
|
|
191
|
+
const cy = y + h / 2;
|
|
192
|
+
const dx = px - cx;
|
|
193
|
+
const dy = py - cy;
|
|
194
|
+
px = cx + dx * Math.cos(rad) - dy * Math.sin(rad);
|
|
195
|
+
py = cy + dx * Math.sin(rad) + dy * Math.cos(rad);
|
|
196
|
+
}
|
|
197
|
+
return px >= x && px <= x + w && py >= y && py <= y + h;
|
|
198
|
+
};
|
|
199
|
+
const floats = [...page.floats ?? []];
|
|
200
|
+
const visitCells = (t) => {
|
|
201
|
+
for (const cell of t.cells) {
|
|
202
|
+
floats.push(...cell.floats ?? []);
|
|
203
|
+
cell.tables?.forEach(visitCells);
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
page.tables?.forEach(visitCells);
|
|
207
|
+
for (let i = floats.length - 1; i >= 0; i--) {
|
|
208
|
+
const f = floats[i];
|
|
209
|
+
if (f.pos == null || !inside(f.x, f.y, f.width, f.height, f.rotation)) continue;
|
|
210
|
+
return {
|
|
211
|
+
pos: f.pos,
|
|
212
|
+
pageIndex: point.pageIndex,
|
|
213
|
+
rect: { x: f.x, y: f.y, width: f.width, height: f.height },
|
|
214
|
+
kind: "float"
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
for (const line of allLines(page)) {
|
|
218
|
+
for (const img of line.images ?? []) {
|
|
219
|
+
if (img.pos == null) continue;
|
|
220
|
+
const top = line.y + line.baseline - img.height;
|
|
221
|
+
if (!inside(img.x, top, img.width, img.height, img.rotation)) continue;
|
|
222
|
+
return {
|
|
223
|
+
pos: img.pos,
|
|
224
|
+
pageIndex: point.pageIndex,
|
|
225
|
+
rect: { x: img.x, y: top, width: img.width, height: img.height },
|
|
226
|
+
kind: "inline"
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
export {
|
|
233
|
+
caretRect,
|
|
234
|
+
hitTest,
|
|
235
|
+
imageAtPoint,
|
|
236
|
+
selectionRects,
|
|
237
|
+
verticalCaret
|
|
238
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { CaretRect, MeasureText, PagePoint, ResolvedLayout, SelectionRect } from '@shadow-garden/bapbong-contracts';
|
|
2
|
+
/** Page-local point → PM position (nearest caret slot), or null on an empty page. */
|
|
3
|
+
export declare function hitTest(layout: ResolvedLayout, point: PagePoint, measure: MeasureText): number | null;
|
|
4
|
+
/** PM position → caret rectangle in page-local coordinates, or null. */
|
|
5
|
+
export declare function caretRect(layout: ResolvedLayout, pos: number, measure: MeasureText): CaretRect | null;
|
|
6
|
+
/** Selection [from, to) → highlight rectangles (one per line touched). */
|
|
7
|
+
export declare function selectionRects(layout: ResolvedLayout, from: number, to: number, measure: MeasureText): SelectionRect[];
|
|
8
|
+
/** Caret position one visual line above/below `pos`, keeping `goalX`. Returns
|
|
9
|
+
* null at the document's vertical edges. */
|
|
10
|
+
export declare function verticalCaret(layout: ResolvedLayout, pos: number, dir: -1 | 1, goalX: number, measure: MeasureText): number | null;
|
|
11
|
+
/** A document image under a page point: the carrying node's PM position plus
|
|
12
|
+
* its page-local box — what the resize plugin anchors its frame to. */
|
|
13
|
+
export interface ImageHit {
|
|
14
|
+
pos: number;
|
|
15
|
+
pageIndex: number;
|
|
16
|
+
rect: {
|
|
17
|
+
x: number;
|
|
18
|
+
y: number;
|
|
19
|
+
width: number;
|
|
20
|
+
height: number;
|
|
21
|
+
};
|
|
22
|
+
kind: 'inline' | 'float';
|
|
23
|
+
}
|
|
24
|
+
/** The topmost document image at a page-local point, or null. Floats win over
|
|
25
|
+
* inline images (they paint over the text); among floats, the later one wins
|
|
26
|
+
* (painted last = on top). Chrome/textbox art carries no pos and is skipped. */
|
|
27
|
+
export declare function imageAtPoint(layout: ResolvedLayout, point: PagePoint): ImageHit | null;
|
|
28
|
+
//# sourceMappingURL=selection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selection.d.ts","sourceRoot":"","sources":["../../src/lib/selection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EAGT,WAAW,EACX,SAAS,EACT,cAAc,EAGd,aAAa,EACd,MAAM,kCAAkC,CAAC;AA0H1C,qFAAqF;AACrF,wBAAgB,OAAO,CAAC,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,GAAG,MAAM,GAAG,IAAI,CAoBrG;AAED,wEAAwE;AACxE,wBAAgB,SAAS,CAAC,MAAM,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,GAAG,SAAS,GAAG,IAAI,CAkBrG;AAED,0EAA0E;AAC1E,wBAAgB,cAAc,CAC5B,MAAM,EAAE,cAAc,EACtB,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,WAAW,GACnB,aAAa,EAAE,CAuBjB;AAED;6CAC6C;AAC7C,wBAAgB,aAAa,CAC3B,MAAM,EAAE,cAAc,EACtB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,CAAC,CAAC,GAAG,CAAC,EACX,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,WAAW,GACnB,MAAM,GAAG,IAAI,CA2Cf;AAED;wEACwE;AACxE,MAAM,WAAW,QAAQ;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC;IAC9D,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC;CAC1B;AAED;;iFAEiF;AACjF,wBAAgB,YAAY,CAAC,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,SAAS,GAAG,QAAQ,GAAG,IAAI,CAsDtF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shadow-garden/bapbong-selection",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "bapbong — caret/selection math over ResolvedLayout (hit-test, caret rect, selection rects)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/shadowgarden-app/bapbong.git",
|
|
9
|
+
"directory": "packages/selection"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.cjs",
|
|
13
|
+
"module": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
"./package.json": "./package.json",
|
|
17
|
+
".": {
|
|
18
|
+
"@shadow-garden/source": "./src/index.ts",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"require": "./dist/index.cjs",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"!**/*.tsbuildinfo"
|
|
28
|
+
],
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"nx": {
|
|
33
|
+
"tags": [
|
|
34
|
+
"scope:selection"
|
|
35
|
+
],
|
|
36
|
+
"targets": {
|
|
37
|
+
"build": {
|
|
38
|
+
"executor": "@nx/esbuild:esbuild",
|
|
39
|
+
"outputs": [
|
|
40
|
+
"{options.outputPath}"
|
|
41
|
+
],
|
|
42
|
+
"options": {
|
|
43
|
+
"outputPath": "packages/selection/dist",
|
|
44
|
+
"main": "packages/selection/src/index.ts",
|
|
45
|
+
"tsConfig": "packages/selection/tsconfig.lib.json",
|
|
46
|
+
"format": [
|
|
47
|
+
"esm",
|
|
48
|
+
"cjs"
|
|
49
|
+
],
|
|
50
|
+
"declarationRootDir": "packages/selection/src",
|
|
51
|
+
"external": [
|
|
52
|
+
"@shadow-garden/bapbong-contracts"
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@shadow-garden/bapbong-contracts": "^0.1.0"
|
|
60
|
+
}
|
|
61
|
+
}
|