merong-docs 0.1.11 → 1.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/OVERVIEW.md +19 -19
- package/OVERVIEW.zh-CN.md +19 -22
- package/README.md +77 -77
- package/README.zh-CN.md +81 -81
- package/client/android-file-share.ts +25 -25
- package/client/controls.tsx +163 -163
- package/client/document-panel.tsx +166 -166
- package/client/external-file.tsx +78 -78
- package/client/file-reader.tsx +43 -43
- package/client/markdown-panel.tsx +737 -737
- package/client/markdown-scroll.ts +122 -122
- package/client/messages.ts +224 -224
- package/client/native-contacts.ts +145 -145
- package/client/page-render-queue.ts +12 -12
- package/client/page-selection.tsx +227 -227
- package/client/page-tools.tsx +72 -72
- package/client/pdf-geometry.ts +96 -96
- package/client/pdf-panel.tsx +477 -477
- package/client/reader-find.tsx +190 -190
- package/client/reader-notice.tsx +46 -46
- package/client/settings.tsx +143 -143
- package/client/syntax.tsx +55 -55
- package/client/viewport-geometry.ts +61 -61
- package/client/web.ts +462 -462
- package/client/worksheet-panel.tsx +631 -631
- package/client/zoom-controls.tsx +140 -140
- package/index.client.tsx +36 -36
- package/index.server.ts +73 -73
- package/node_modules/markdown-it/bin/markdown-it.mjs +0 -0
- package/node_modules/yaml/bin.mjs +0 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/scripts/prepare-office.mjs +20 -20
- package/server/engine.ts +89 -89
- package/server/external-file.ts +42 -42
- package/server/generated/document-worker.ts +1 -1
- package/server/generated/rust-reader_bg.wasm +0 -0
- package/server/markdown-index.ts +305 -305
- package/server/markdown-links.ts +51 -51
- package/server/markdown-page.ts +120 -120
- package/server/office-fonts.cjs +824 -824
- package/server/office-location.ts +41 -41
- package/server/office-preview.ts +186 -186
- package/server/office-worker.cjs +49 -49
- package/server/office-xml.ts +79 -79
- package/server/pdf-engine.ts +168 -168
- package/server/pdf-text-geometry.ts +60 -60
- package/server/rust-reader.ts +40 -40
- package/server/structured-document.ts +69 -69
- package/server/text-search.ts +15 -15
- package/server/worker-entry.ts +42 -42
- package/server/worker-protocol.ts +38 -38
- package/server/worksheet.ts +67 -67
- package/server/workspace-file.ts +63 -63
- package/shared/contracts.ts +187 -187
- package/shared/file-limits.ts +5 -5
- package/shared/markdown-constants.ts +4 -4
- package/shared/markdown-math.ts +198 -198
- package/shared/markdown-references.ts +83 -83
- package/shared/markdown-source-location.ts +8 -8
- package/shared/reader-settings.ts +15 -15
- package/shared/reader-shortcuts.ts +132 -132
- package/vendor/office2pdf/office2pdf_bg.wasm +2 -2
|
@@ -1,227 +1,227 @@
|
|
|
1
|
-
import { memo, useEffect, useRef, useState } from "react";
|
|
2
|
-
import { View } from "react-native";
|
|
3
|
-
import type { PageGlyph, PageTextMatch } from "../shared/contracts";
|
|
4
|
-
import type { Contact, Point, Size } from "./viewport-geometry";
|
|
5
|
-
|
|
6
|
-
export interface TextPage extends Size {
|
|
7
|
-
number: number;
|
|
8
|
-
left: number;
|
|
9
|
-
top: number;
|
|
10
|
-
text: string;
|
|
11
|
-
glyphs: PageGlyph[];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
interface Selection {
|
|
15
|
-
page: number;
|
|
16
|
-
start: number;
|
|
17
|
-
end: number;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
interface Drag {
|
|
21
|
-
contact: Contact;
|
|
22
|
-
page?: number;
|
|
23
|
-
anchor?: number;
|
|
24
|
-
tap?: boolean;
|
|
25
|
-
waiting?: ReturnType<typeof setTimeout>;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function nearestGlyph(page: TextPage, point: Point, tolerance = Infinity) {
|
|
29
|
-
const x = point.x - page.left;
|
|
30
|
-
const y = point.y - page.top;
|
|
31
|
-
let nearest = -1;
|
|
32
|
-
let best = Infinity;
|
|
33
|
-
for (const [index, [, , left, top, right, bottom]] of page.glyphs.entries()) {
|
|
34
|
-
const horizontal = Math.max(left * page.width - x, x - right * page.width, 0);
|
|
35
|
-
const vertical = Math.max(top * page.height - y, y - bottom * page.height, 0);
|
|
36
|
-
if (horizontal > tolerance || vertical > tolerance) continue;
|
|
37
|
-
const distance = horizontal + vertical * 3;
|
|
38
|
-
if (distance >= best) continue;
|
|
39
|
-
nearest = index;
|
|
40
|
-
best = distance;
|
|
41
|
-
if (distance === 0) break;
|
|
42
|
-
}
|
|
43
|
-
return nearest;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const wordPart = /^[\p{L}\p{M}\p{N}_]+$/u;
|
|
47
|
-
const ideograph = /\p{Script=Han}/u;
|
|
48
|
-
|
|
49
|
-
function wordBounds(page: TextPage, index: number) {
|
|
50
|
-
const at = (offset: number) => {
|
|
51
|
-
const [first, last] = page.glyphs[offset];
|
|
52
|
-
return page.text.slice(first, last);
|
|
53
|
-
};
|
|
54
|
-
const includes = (value: string) => wordPart.test(value) && !ideograph.test(value);
|
|
55
|
-
let start = index;
|
|
56
|
-
let end = index;
|
|
57
|
-
if (includes(at(index))) {
|
|
58
|
-
while (start > 0 && page.glyphs[start - 1][1] === page.glyphs[start][0] && includes(at(start - 1))) start--;
|
|
59
|
-
while (end < page.glyphs.length - 1 && page.glyphs[end][1] === page.glyphs[end + 1][0] && includes(at(end + 1))) end++;
|
|
60
|
-
}
|
|
61
|
-
return { page: page.number, start, end };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function handlePoint(glyph: PageGlyph, size: Size, end: boolean) {
|
|
65
|
-
return { x: glyph[end ? 4 : 2] * size.width, y: glyph[5] * size.height };
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function usePageSelection(findPage: (point: Point, number?: number) => TextPage | undefined, version: string) {
|
|
69
|
-
const [selection, setSelection] = useState<Selection>();
|
|
70
|
-
const selected = useRef<Selection>(undefined);
|
|
71
|
-
const drag = useRef<Drag>(undefined);
|
|
72
|
-
|
|
73
|
-
useEffect(() => {
|
|
74
|
-
selected.current = undefined;
|
|
75
|
-
setSelection(undefined);
|
|
76
|
-
return () => clearTimeout(drag.current?.waiting);
|
|
77
|
-
}, [version]);
|
|
78
|
-
|
|
79
|
-
function show(next?: Selection) {
|
|
80
|
-
const previous = selected.current;
|
|
81
|
-
if (previous?.page === next?.page && previous?.start === next?.start && previous?.end === next?.end) return;
|
|
82
|
-
selected.current = next;
|
|
83
|
-
setSelection(next);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function track(contacts: Contact[], pointer: string) {
|
|
87
|
-
const previous = drag.current;
|
|
88
|
-
if (contacts.length !== 1) {
|
|
89
|
-
clearTimeout(previous?.waiting);
|
|
90
|
-
drag.current = undefined;
|
|
91
|
-
if (!contacts.length && previous?.tap && previous.anchor === undefined) show();
|
|
92
|
-
return !contacts.length && previous?.anchor !== undefined;
|
|
93
|
-
}
|
|
94
|
-
const [point] = contacts;
|
|
95
|
-
const touch = pointer === "touch";
|
|
96
|
-
if (previous?.contact.id !== point.id) {
|
|
97
|
-
clearTimeout(previous?.waiting);
|
|
98
|
-
const gesture: Drag = { contact: point, tap: touch };
|
|
99
|
-
drag.current = gesture;
|
|
100
|
-
const current = selected.current;
|
|
101
|
-
if (touch && current) {
|
|
102
|
-
const page = findPage(point, current.page);
|
|
103
|
-
if (page) {
|
|
104
|
-
const distance = (index: number, end: boolean) => {
|
|
105
|
-
const handle = handlePoint(page.glyphs[index], page, end);
|
|
106
|
-
return Math.hypot(point.x - page.left - handle.x, point.y - page.top - handle.y - 5);
|
|
107
|
-
};
|
|
108
|
-
const start = distance(current.start, false);
|
|
109
|
-
const end = distance(current.end, true);
|
|
110
|
-
if (Math.min(start, end) <= 19) {
|
|
111
|
-
gesture.page = current.page;
|
|
112
|
-
gesture.anchor = start <= end ? current.end : current.start;
|
|
113
|
-
return true;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
const page = findPage(point);
|
|
118
|
-
const index = page ? nearestGlyph(page, point, touch ? 7 : 2) : -1;
|
|
119
|
-
if (!page || index < 0) {
|
|
120
|
-
if (!touch) show();
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
gesture.page = page.number;
|
|
124
|
-
if (touch) {
|
|
125
|
-
gesture.waiting = setTimeout(() => {
|
|
126
|
-
gesture.waiting = undefined;
|
|
127
|
-
const word = wordBounds(page, index);
|
|
128
|
-
gesture.anchor = word.start;
|
|
129
|
-
show(word);
|
|
130
|
-
}, 450);
|
|
131
|
-
return false;
|
|
132
|
-
}
|
|
133
|
-
gesture.anchor = index;
|
|
134
|
-
show({ page: page.number, start: index, end: index });
|
|
135
|
-
return true;
|
|
136
|
-
}
|
|
137
|
-
const distance = Math.hypot(point.x - previous.contact.x, point.y - previous.contact.y);
|
|
138
|
-
if (previous.anchor === undefined) {
|
|
139
|
-
if (distance > 7) {
|
|
140
|
-
previous.tap = false;
|
|
141
|
-
clearTimeout(previous.waiting);
|
|
142
|
-
previous.waiting = undefined;
|
|
143
|
-
}
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
if (distance < 1) return true;
|
|
147
|
-
const page = findPage(point, previous.page);
|
|
148
|
-
const index = page ? nearestGlyph(page, point) : -1;
|
|
149
|
-
if (page && index >= 0) {
|
|
150
|
-
show({ page: page.number, start: Math.min(previous.anchor, index), end: Math.max(previous.anchor, index) });
|
|
151
|
-
}
|
|
152
|
-
return true;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function text() {
|
|
156
|
-
const range = selected.current;
|
|
157
|
-
if (!range) return "";
|
|
158
|
-
const page = findPage({ x: 0, y: 0 }, range.page);
|
|
159
|
-
if (!page) return "";
|
|
160
|
-
return page.text.slice(page.glyphs[range.start][0], page.glyphs[range.end][1]);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
return { selection, track, text, overText: (point: Point) => {
|
|
164
|
-
const page = findPage(point);
|
|
165
|
-
return !!page && nearestGlyph(page, point, 2) >= 0;
|
|
166
|
-
} };
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function glyphBands<Mark>(glyphs: PageGlyph[], classify: (glyph: PageGlyph) => Mark | undefined, start = 0, end = glyphs.length - 1) {
|
|
170
|
-
const bands: { left: number; top: number; right: number; bottom: number; mark: Mark }[] = [];
|
|
171
|
-
for (let index = start; index <= end; index++) {
|
|
172
|
-
const glyph = glyphs[index];
|
|
173
|
-
const mark = classify(glyph);
|
|
174
|
-
if (mark === undefined) continue;
|
|
175
|
-
const [, , left, top, right, bottom] = glyph;
|
|
176
|
-
const last = bands.at(-1);
|
|
177
|
-
if (last && last.mark === mark && top < last.bottom && bottom > last.top && Math.abs(top - last.top) < 0.012 &&
|
|
178
|
-
left >= last.left && left <= last.right + 0.005) {
|
|
179
|
-
last.right = Math.max(last.right, right);
|
|
180
|
-
last.top = Math.min(last.top, top);
|
|
181
|
-
last.bottom = Math.max(last.bottom, bottom);
|
|
182
|
-
} else bands.push({ left, top, right, bottom, mark });
|
|
183
|
-
}
|
|
184
|
-
return bands;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
export const PageFindOverlay = memo(function PageFindOverlay({ width, height, glyphs, matches, active }: Size & {
|
|
188
|
-
glyphs: PageGlyph[]; matches: PageTextMatch[]; active?: PageTextMatch;
|
|
189
|
-
}) {
|
|
190
|
-
let index = 0;
|
|
191
|
-
const bands = glyphBands(glyphs, ([start, end]) => {
|
|
192
|
-
while (matches[index] && matches[index][2] <= start) index++;
|
|
193
|
-
const match = matches[index];
|
|
194
|
-
return match && match[1] < end ? match === active ? "active" : "match" : undefined;
|
|
195
|
-
});
|
|
196
|
-
return (
|
|
197
|
-
<View pointerEvents="none" importantForAccessibility="no" testID="merong-page-find-highlights"
|
|
198
|
-
style={{ position: "absolute", left: 0, top: 0, width, height }}>
|
|
199
|
-
{bands.map(({ left, top, right, bottom, mark }, index) => (
|
|
200
|
-
<View key={index} testID={mark === "active" ? "merong-page-find-active" : undefined}
|
|
201
|
-
style={{ position: "absolute", left: left * width, top: top * height, width: (right - left) * width,
|
|
202
|
-
height: (bottom - top) * height, backgroundColor: mark === "active" ? "#f59e0b" : "#facc15", opacity: 0.5 }} />
|
|
203
|
-
))}
|
|
204
|
-
</View>
|
|
205
|
-
);
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
export const PageSelectionOverlay = memo(function PageSelectionOverlay({ width, height, glyphs, range, handles, accent, scale }: Size & {
|
|
209
|
-
glyphs: PageGlyph[]; range: Selection; handles: boolean; accent: string; scale: number;
|
|
210
|
-
}) {
|
|
211
|
-
const bands = glyphBands(glyphs, () => true, range.start, range.end);
|
|
212
|
-
const radius = 5 / scale;
|
|
213
|
-
return (
|
|
214
|
-
<View pointerEvents="none" importantForAccessibility="no" testID="merong-page-selection"
|
|
215
|
-
style={{ position: "absolute", left: 0, top: 0, width, height }}>
|
|
216
|
-
{bands.map(({ left, top, right, bottom }, index) => (
|
|
217
|
-
<View key={index} style={{ position: "absolute", left: left * width, top: top * height,
|
|
218
|
-
width: (right - left) * width, height: (bottom - top) * height, backgroundColor: accent, opacity: 0.35 }} />
|
|
219
|
-
))}
|
|
220
|
-
{handles && [false, true].map((end) => {
|
|
221
|
-
const point = handlePoint(glyphs[end ? range.end : range.start], { width, height }, end);
|
|
222
|
-
return <View key={String(end)} style={{ position: "absolute", left: point.x - radius, top: point.y,
|
|
223
|
-
width: radius * 2, height: radius * 2, borderRadius: radius, backgroundColor: accent }} />;
|
|
224
|
-
})}
|
|
225
|
-
</View>
|
|
226
|
-
);
|
|
227
|
-
});
|
|
1
|
+
import { memo, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { View } from "react-native";
|
|
3
|
+
import type { PageGlyph, PageTextMatch } from "../shared/contracts";
|
|
4
|
+
import type { Contact, Point, Size } from "./viewport-geometry";
|
|
5
|
+
|
|
6
|
+
export interface TextPage extends Size {
|
|
7
|
+
number: number;
|
|
8
|
+
left: number;
|
|
9
|
+
top: number;
|
|
10
|
+
text: string;
|
|
11
|
+
glyphs: PageGlyph[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface Selection {
|
|
15
|
+
page: number;
|
|
16
|
+
start: number;
|
|
17
|
+
end: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface Drag {
|
|
21
|
+
contact: Contact;
|
|
22
|
+
page?: number;
|
|
23
|
+
anchor?: number;
|
|
24
|
+
tap?: boolean;
|
|
25
|
+
waiting?: ReturnType<typeof setTimeout>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function nearestGlyph(page: TextPage, point: Point, tolerance = Infinity) {
|
|
29
|
+
const x = point.x - page.left;
|
|
30
|
+
const y = point.y - page.top;
|
|
31
|
+
let nearest = -1;
|
|
32
|
+
let best = Infinity;
|
|
33
|
+
for (const [index, [, , left, top, right, bottom]] of page.glyphs.entries()) {
|
|
34
|
+
const horizontal = Math.max(left * page.width - x, x - right * page.width, 0);
|
|
35
|
+
const vertical = Math.max(top * page.height - y, y - bottom * page.height, 0);
|
|
36
|
+
if (horizontal > tolerance || vertical > tolerance) continue;
|
|
37
|
+
const distance = horizontal + vertical * 3;
|
|
38
|
+
if (distance >= best) continue;
|
|
39
|
+
nearest = index;
|
|
40
|
+
best = distance;
|
|
41
|
+
if (distance === 0) break;
|
|
42
|
+
}
|
|
43
|
+
return nearest;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const wordPart = /^[\p{L}\p{M}\p{N}_]+$/u;
|
|
47
|
+
const ideograph = /\p{Script=Han}/u;
|
|
48
|
+
|
|
49
|
+
function wordBounds(page: TextPage, index: number) {
|
|
50
|
+
const at = (offset: number) => {
|
|
51
|
+
const [first, last] = page.glyphs[offset];
|
|
52
|
+
return page.text.slice(first, last);
|
|
53
|
+
};
|
|
54
|
+
const includes = (value: string) => wordPart.test(value) && !ideograph.test(value);
|
|
55
|
+
let start = index;
|
|
56
|
+
let end = index;
|
|
57
|
+
if (includes(at(index))) {
|
|
58
|
+
while (start > 0 && page.glyphs[start - 1][1] === page.glyphs[start][0] && includes(at(start - 1))) start--;
|
|
59
|
+
while (end < page.glyphs.length - 1 && page.glyphs[end][1] === page.glyphs[end + 1][0] && includes(at(end + 1))) end++;
|
|
60
|
+
}
|
|
61
|
+
return { page: page.number, start, end };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function handlePoint(glyph: PageGlyph, size: Size, end: boolean) {
|
|
65
|
+
return { x: glyph[end ? 4 : 2] * size.width, y: glyph[5] * size.height };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function usePageSelection(findPage: (point: Point, number?: number) => TextPage | undefined, version: string) {
|
|
69
|
+
const [selection, setSelection] = useState<Selection>();
|
|
70
|
+
const selected = useRef<Selection>(undefined);
|
|
71
|
+
const drag = useRef<Drag>(undefined);
|
|
72
|
+
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
selected.current = undefined;
|
|
75
|
+
setSelection(undefined);
|
|
76
|
+
return () => clearTimeout(drag.current?.waiting);
|
|
77
|
+
}, [version]);
|
|
78
|
+
|
|
79
|
+
function show(next?: Selection) {
|
|
80
|
+
const previous = selected.current;
|
|
81
|
+
if (previous?.page === next?.page && previous?.start === next?.start && previous?.end === next?.end) return;
|
|
82
|
+
selected.current = next;
|
|
83
|
+
setSelection(next);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function track(contacts: Contact[], pointer: string) {
|
|
87
|
+
const previous = drag.current;
|
|
88
|
+
if (contacts.length !== 1) {
|
|
89
|
+
clearTimeout(previous?.waiting);
|
|
90
|
+
drag.current = undefined;
|
|
91
|
+
if (!contacts.length && previous?.tap && previous.anchor === undefined) show();
|
|
92
|
+
return !contacts.length && previous?.anchor !== undefined;
|
|
93
|
+
}
|
|
94
|
+
const [point] = contacts;
|
|
95
|
+
const touch = pointer === "touch";
|
|
96
|
+
if (previous?.contact.id !== point.id) {
|
|
97
|
+
clearTimeout(previous?.waiting);
|
|
98
|
+
const gesture: Drag = { contact: point, tap: touch };
|
|
99
|
+
drag.current = gesture;
|
|
100
|
+
const current = selected.current;
|
|
101
|
+
if (touch && current) {
|
|
102
|
+
const page = findPage(point, current.page);
|
|
103
|
+
if (page) {
|
|
104
|
+
const distance = (index: number, end: boolean) => {
|
|
105
|
+
const handle = handlePoint(page.glyphs[index], page, end);
|
|
106
|
+
return Math.hypot(point.x - page.left - handle.x, point.y - page.top - handle.y - 5);
|
|
107
|
+
};
|
|
108
|
+
const start = distance(current.start, false);
|
|
109
|
+
const end = distance(current.end, true);
|
|
110
|
+
if (Math.min(start, end) <= 19) {
|
|
111
|
+
gesture.page = current.page;
|
|
112
|
+
gesture.anchor = start <= end ? current.end : current.start;
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const page = findPage(point);
|
|
118
|
+
const index = page ? nearestGlyph(page, point, touch ? 7 : 2) : -1;
|
|
119
|
+
if (!page || index < 0) {
|
|
120
|
+
if (!touch) show();
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
gesture.page = page.number;
|
|
124
|
+
if (touch) {
|
|
125
|
+
gesture.waiting = setTimeout(() => {
|
|
126
|
+
gesture.waiting = undefined;
|
|
127
|
+
const word = wordBounds(page, index);
|
|
128
|
+
gesture.anchor = word.start;
|
|
129
|
+
show(word);
|
|
130
|
+
}, 450);
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
gesture.anchor = index;
|
|
134
|
+
show({ page: page.number, start: index, end: index });
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
const distance = Math.hypot(point.x - previous.contact.x, point.y - previous.contact.y);
|
|
138
|
+
if (previous.anchor === undefined) {
|
|
139
|
+
if (distance > 7) {
|
|
140
|
+
previous.tap = false;
|
|
141
|
+
clearTimeout(previous.waiting);
|
|
142
|
+
previous.waiting = undefined;
|
|
143
|
+
}
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
if (distance < 1) return true;
|
|
147
|
+
const page = findPage(point, previous.page);
|
|
148
|
+
const index = page ? nearestGlyph(page, point) : -1;
|
|
149
|
+
if (page && index >= 0) {
|
|
150
|
+
show({ page: page.number, start: Math.min(previous.anchor, index), end: Math.max(previous.anchor, index) });
|
|
151
|
+
}
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function text() {
|
|
156
|
+
const range = selected.current;
|
|
157
|
+
if (!range) return "";
|
|
158
|
+
const page = findPage({ x: 0, y: 0 }, range.page);
|
|
159
|
+
if (!page) return "";
|
|
160
|
+
return page.text.slice(page.glyphs[range.start][0], page.glyphs[range.end][1]);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return { selection, track, text, overText: (point: Point) => {
|
|
164
|
+
const page = findPage(point);
|
|
165
|
+
return !!page && nearestGlyph(page, point, 2) >= 0;
|
|
166
|
+
} };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function glyphBands<Mark>(glyphs: PageGlyph[], classify: (glyph: PageGlyph) => Mark | undefined, start = 0, end = glyphs.length - 1) {
|
|
170
|
+
const bands: { left: number; top: number; right: number; bottom: number; mark: Mark }[] = [];
|
|
171
|
+
for (let index = start; index <= end; index++) {
|
|
172
|
+
const glyph = glyphs[index];
|
|
173
|
+
const mark = classify(glyph);
|
|
174
|
+
if (mark === undefined) continue;
|
|
175
|
+
const [, , left, top, right, bottom] = glyph;
|
|
176
|
+
const last = bands.at(-1);
|
|
177
|
+
if (last && last.mark === mark && top < last.bottom && bottom > last.top && Math.abs(top - last.top) < 0.012 &&
|
|
178
|
+
left >= last.left && left <= last.right + 0.005) {
|
|
179
|
+
last.right = Math.max(last.right, right);
|
|
180
|
+
last.top = Math.min(last.top, top);
|
|
181
|
+
last.bottom = Math.max(last.bottom, bottom);
|
|
182
|
+
} else bands.push({ left, top, right, bottom, mark });
|
|
183
|
+
}
|
|
184
|
+
return bands;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export const PageFindOverlay = memo(function PageFindOverlay({ width, height, glyphs, matches, active }: Size & {
|
|
188
|
+
glyphs: PageGlyph[]; matches: PageTextMatch[]; active?: PageTextMatch;
|
|
189
|
+
}) {
|
|
190
|
+
let index = 0;
|
|
191
|
+
const bands = glyphBands(glyphs, ([start, end]) => {
|
|
192
|
+
while (matches[index] && matches[index][2] <= start) index++;
|
|
193
|
+
const match = matches[index];
|
|
194
|
+
return match && match[1] < end ? match === active ? "active" : "match" : undefined;
|
|
195
|
+
});
|
|
196
|
+
return (
|
|
197
|
+
<View pointerEvents="none" importantForAccessibility="no" testID="merong-page-find-highlights"
|
|
198
|
+
style={{ position: "absolute", left: 0, top: 0, width, height }}>
|
|
199
|
+
{bands.map(({ left, top, right, bottom, mark }, index) => (
|
|
200
|
+
<View key={index} testID={mark === "active" ? "merong-page-find-active" : undefined}
|
|
201
|
+
style={{ position: "absolute", left: left * width, top: top * height, width: (right - left) * width,
|
|
202
|
+
height: (bottom - top) * height, backgroundColor: mark === "active" ? "#f59e0b" : "#facc15", opacity: 0.5 }} />
|
|
203
|
+
))}
|
|
204
|
+
</View>
|
|
205
|
+
);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
export const PageSelectionOverlay = memo(function PageSelectionOverlay({ width, height, glyphs, range, handles, accent, scale }: Size & {
|
|
209
|
+
glyphs: PageGlyph[]; range: Selection; handles: boolean; accent: string; scale: number;
|
|
210
|
+
}) {
|
|
211
|
+
const bands = glyphBands(glyphs, () => true, range.start, range.end);
|
|
212
|
+
const radius = 5 / scale;
|
|
213
|
+
return (
|
|
214
|
+
<View pointerEvents="none" importantForAccessibility="no" testID="merong-page-selection"
|
|
215
|
+
style={{ position: "absolute", left: 0, top: 0, width, height }}>
|
|
216
|
+
{bands.map(({ left, top, right, bottom }, index) => (
|
|
217
|
+
<View key={index} style={{ position: "absolute", left: left * width, top: top * height,
|
|
218
|
+
width: (right - left) * width, height: (bottom - top) * height, backgroundColor: accent, opacity: 0.35 }} />
|
|
219
|
+
))}
|
|
220
|
+
{handles && [false, true].map((end) => {
|
|
221
|
+
const point = handlePoint(glyphs[end ? range.end : range.start], { width, height }, end);
|
|
222
|
+
return <View key={String(end)} style={{ position: "absolute", left: point.x - radius, top: point.y,
|
|
223
|
+
width: radius * 2, height: radius * 2, borderRadius: radius, backgroundColor: accent }} />;
|
|
224
|
+
})}
|
|
225
|
+
</View>
|
|
226
|
+
);
|
|
227
|
+
});
|
package/client/page-tools.tsx
CHANGED
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
import { useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
-
import { ActivityIndicator, Image, Platform, Pressable, Text, View, type FlatList as NativeFlatList } from "react-native";
|
|
3
|
-
import { useQuery } from "@tanstack/react-query";
|
|
4
|
-
import { useTranslation } from "@getpaseo/plugin/client";
|
|
5
|
-
import { FlatList } from "@getpaseo/plugin/client/ui";
|
|
6
|
-
import type { Theme } from "./controls";
|
|
7
|
-
import { messages } from "./messages";
|
|
8
|
-
import type { createPageRenderQueue } from "./page-render-queue";
|
|
9
|
-
|
|
10
|
-
type SlidePreview = {
|
|
11
|
-
fileKey: readonly unknown[];
|
|
12
|
-
enqueue: ReturnType<typeof createPageRenderQueue>;
|
|
13
|
-
render: (page: number, pixels: number) => Promise<{ uri: string } | null>;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
function SlideImage({ number, theme, fileKey, enqueue, render }: SlidePreview & { number: number; theme: Theme }) {
|
|
17
|
-
const result = useQuery({
|
|
18
|
-
queryKey: [...fileKey, "thumbnail", number], queryFn: ({ signal }) => enqueue(signal, () => render(number, 256)),
|
|
19
|
-
staleTime: Infinity, gcTime: 30_000, retry: (count, error) => count === 0 && error.message.includes("Reader is busy"),
|
|
20
|
-
});
|
|
21
|
-
return result.data ? <Image accessible={false} source={{ uri: result.data.uri }} resizeMode="contain" fadeDuration={0}
|
|
22
|
-
style={{ width: "100%", flex: 1 }} /> : <View style={{ flex: 1, justifyContent: "center" }}>
|
|
23
|
-
{result.isPending && <ActivityIndicator color={theme.colors.foregroundMuted} />}
|
|
24
|
-
</View>;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function SlideSidebar({ count, page, goTo, theme, compact, preview }: {
|
|
28
|
-
count: number; page: number; goTo: (page: number) => void; theme: Theme; compact: boolean; preview?: SlidePreview;
|
|
29
|
-
}) {
|
|
30
|
-
const { t } = useTranslation(messages);
|
|
31
|
-
const rowHeight = preview ? compact ? 66 : 92 : 44;
|
|
32
|
-
const gap = 6;
|
|
33
|
-
const slides = useMemo(() => Array.from({ length: count }, (_, index) => index + 1), [count]);
|
|
34
|
-
const list = useRef<NativeFlatList<number>>(null);
|
|
35
|
-
const offset = useRef(0);
|
|
36
|
-
const [height, setHeight] = useState(0);
|
|
37
|
-
const [contentHeight, setContentHeight] = useState(0);
|
|
38
|
-
useEffect(() => {
|
|
39
|
-
if (!height || !contentHeight || page > count) return;
|
|
40
|
-
const top = gap + rowHeight * (page - 1);
|
|
41
|
-
const bottom = top + rowHeight - gap;
|
|
42
|
-
if (top < offset.current || bottom > offset.current + height) {
|
|
43
|
-
list.current?.scrollToIndex({ index: page - 1, viewPosition: 0.5, animated: false });
|
|
44
|
-
}
|
|
45
|
-
}, [contentHeight, count, gap, height, page, rowHeight]);
|
|
46
|
-
return (
|
|
47
|
-
<View testID="merong-slide-sidebar" style={{
|
|
48
|
-
width: compact ? 72 : 124, maxWidth: "28%", flexShrink: 0,
|
|
49
|
-
borderRightWidth: 1, borderColor: theme.colors.border, backgroundColor: theme.colors.surface0,
|
|
50
|
-
}}>
|
|
51
|
-
<FlatList ref={list} accessibilityLabel={t("thumbnails")} keyboardShouldPersistTaps="handled" nestedScrollEnabled
|
|
52
|
-
style={{ flex: 1, minHeight: 0 }} contentContainerStyle={{ padding: gap }}
|
|
53
|
-
data={slides} keyExtractor={String}
|
|
54
|
-
initialNumToRender={5} windowSize={3} maxToRenderPerBatch={5}
|
|
55
|
-
onLayout={({ nativeEvent: { layout } }) => setHeight(layout.height)}
|
|
56
|
-
onContentSizeChange={(_, height) => setContentHeight(height)}
|
|
57
|
-
onScroll={({ nativeEvent: { contentOffset } }) => { offset.current = contentOffset.y; }} scrollEventThrottle={16}
|
|
58
|
-
getItemLayout={(_, index) => ({ index, length: rowHeight, offset: gap + rowHeight * index })}
|
|
59
|
-
renderItem={({ item }) => (
|
|
60
|
-
<Pressable accessibilityRole="button" accessibilityLabel={`${t("slide")} ${item}`}
|
|
61
|
-
accessibilityState={{ selected: page === item }} onPress={() => goTo(item)}
|
|
62
|
-
{...(Platform.OS === "web" ? { "aria-pressed": page === item } : {})}
|
|
63
|
-
style={{ height: rowHeight - gap, marginBottom: gap, padding: 4, borderWidth: 1, borderRadius: 6,
|
|
64
|
-
alignItems: "center", justifyContent: "center", backgroundColor: page === item ? theme.colors.surface2 : theme.colors.surface0,
|
|
65
|
-
borderColor: page === item ? theme.colors.accent : theme.colors.border }}>
|
|
66
|
-
{preview && <SlideImage {...preview} number={item} theme={theme} />}
|
|
67
|
-
<Text numberOfLines={1} style={{ color: theme.colors.foreground, fontSize: 12 }}>{item}</Text>
|
|
68
|
-
</Pressable>
|
|
69
|
-
)} />
|
|
70
|
-
</View>
|
|
71
|
-
);
|
|
72
|
-
}
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { ActivityIndicator, Image, Platform, Pressable, Text, View, type FlatList as NativeFlatList } from "react-native";
|
|
3
|
+
import { useQuery } from "@tanstack/react-query";
|
|
4
|
+
import { useTranslation } from "@getpaseo/plugin/client";
|
|
5
|
+
import { FlatList } from "@getpaseo/plugin/client/ui";
|
|
6
|
+
import type { Theme } from "./controls";
|
|
7
|
+
import { messages } from "./messages";
|
|
8
|
+
import type { createPageRenderQueue } from "./page-render-queue";
|
|
9
|
+
|
|
10
|
+
type SlidePreview = {
|
|
11
|
+
fileKey: readonly unknown[];
|
|
12
|
+
enqueue: ReturnType<typeof createPageRenderQueue>;
|
|
13
|
+
render: (page: number, pixels: number) => Promise<{ uri: string } | null>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function SlideImage({ number, theme, fileKey, enqueue, render }: SlidePreview & { number: number; theme: Theme }) {
|
|
17
|
+
const result = useQuery({
|
|
18
|
+
queryKey: [...fileKey, "thumbnail", number], queryFn: ({ signal }) => enqueue(signal, () => render(number, 256)),
|
|
19
|
+
staleTime: Infinity, gcTime: 30_000, retry: (count, error) => count === 0 && error.message.includes("Reader is busy"),
|
|
20
|
+
});
|
|
21
|
+
return result.data ? <Image accessible={false} source={{ uri: result.data.uri }} resizeMode="contain" fadeDuration={0}
|
|
22
|
+
style={{ width: "100%", flex: 1 }} /> : <View style={{ flex: 1, justifyContent: "center" }}>
|
|
23
|
+
{result.isPending && <ActivityIndicator color={theme.colors.foregroundMuted} />}
|
|
24
|
+
</View>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function SlideSidebar({ count, page, goTo, theme, compact, preview }: {
|
|
28
|
+
count: number; page: number; goTo: (page: number) => void; theme: Theme; compact: boolean; preview?: SlidePreview;
|
|
29
|
+
}) {
|
|
30
|
+
const { t } = useTranslation(messages);
|
|
31
|
+
const rowHeight = preview ? compact ? 66 : 92 : 44;
|
|
32
|
+
const gap = 6;
|
|
33
|
+
const slides = useMemo(() => Array.from({ length: count }, (_, index) => index + 1), [count]);
|
|
34
|
+
const list = useRef<NativeFlatList<number>>(null);
|
|
35
|
+
const offset = useRef(0);
|
|
36
|
+
const [height, setHeight] = useState(0);
|
|
37
|
+
const [contentHeight, setContentHeight] = useState(0);
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
if (!height || !contentHeight || page > count) return;
|
|
40
|
+
const top = gap + rowHeight * (page - 1);
|
|
41
|
+
const bottom = top + rowHeight - gap;
|
|
42
|
+
if (top < offset.current || bottom > offset.current + height) {
|
|
43
|
+
list.current?.scrollToIndex({ index: page - 1, viewPosition: 0.5, animated: false });
|
|
44
|
+
}
|
|
45
|
+
}, [contentHeight, count, gap, height, page, rowHeight]);
|
|
46
|
+
return (
|
|
47
|
+
<View testID="merong-slide-sidebar" style={{
|
|
48
|
+
width: compact ? 72 : 124, maxWidth: "28%", flexShrink: 0,
|
|
49
|
+
borderRightWidth: 1, borderColor: theme.colors.border, backgroundColor: theme.colors.surface0,
|
|
50
|
+
}}>
|
|
51
|
+
<FlatList ref={list} accessibilityLabel={t("thumbnails")} keyboardShouldPersistTaps="handled" nestedScrollEnabled
|
|
52
|
+
style={{ flex: 1, minHeight: 0 }} contentContainerStyle={{ padding: gap }}
|
|
53
|
+
data={slides} keyExtractor={String}
|
|
54
|
+
initialNumToRender={5} windowSize={3} maxToRenderPerBatch={5}
|
|
55
|
+
onLayout={({ nativeEvent: { layout } }) => setHeight(layout.height)}
|
|
56
|
+
onContentSizeChange={(_, height) => setContentHeight(height)}
|
|
57
|
+
onScroll={({ nativeEvent: { contentOffset } }) => { offset.current = contentOffset.y; }} scrollEventThrottle={16}
|
|
58
|
+
getItemLayout={(_, index) => ({ index, length: rowHeight, offset: gap + rowHeight * index })}
|
|
59
|
+
renderItem={({ item }) => (
|
|
60
|
+
<Pressable accessibilityRole="button" accessibilityLabel={`${t("slide")} ${item}`}
|
|
61
|
+
accessibilityState={{ selected: page === item }} onPress={() => goTo(item)}
|
|
62
|
+
{...(Platform.OS === "web" ? { "aria-pressed": page === item } : {})}
|
|
63
|
+
style={{ height: rowHeight - gap, marginBottom: gap, padding: 4, borderWidth: 1, borderRadius: 6,
|
|
64
|
+
alignItems: "center", justifyContent: "center", backgroundColor: page === item ? theme.colors.surface2 : theme.colors.surface0,
|
|
65
|
+
borderColor: page === item ? theme.colors.accent : theme.colors.border }}>
|
|
66
|
+
{preview && <SlideImage {...preview} number={item} theme={theme} />}
|
|
67
|
+
<Text numberOfLines={1} style={{ color: theme.colors.foreground, fontSize: 12 }}>{item}</Text>
|
|
68
|
+
</Pressable>
|
|
69
|
+
)} />
|
|
70
|
+
</View>
|
|
71
|
+
);
|
|
72
|
+
}
|