@open-document/core 0.7.0 → 0.8.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/package.json
CHANGED
|
@@ -1043,8 +1043,11 @@ export function Inspector({
|
|
|
1043
1043
|
setStatus(null);
|
|
1044
1044
|
setNote('');
|
|
1045
1045
|
};
|
|
1046
|
+
// On the window, not the pane: the first click opens the element panel,
|
|
1047
|
+
// and in two-up or grid a sheet near the right edge can have the panel
|
|
1048
|
+
// open right under the pointer — the second click then lands on it, and a
|
|
1049
|
+
// listener on the pane would never hear the double-click at all.
|
|
1046
1050
|
const onDblClick = (e: MouseEvent) => {
|
|
1047
|
-
if (insideChrome(e.target)) return;
|
|
1048
1051
|
const first = firstClickRef.current;
|
|
1049
1052
|
firstClickRef.current = null;
|
|
1050
1053
|
if (first && Date.now() - first.time < DOUBLE_CLICK_MS) {
|
|
@@ -1052,6 +1055,7 @@ export function Inspector({
|
|
|
1052
1055
|
setWantEdit({ anchor: first.anchor, at: first.at, selectWord: true });
|
|
1053
1056
|
return;
|
|
1054
1057
|
}
|
|
1058
|
+
if (!container.contains(e.target as Node) || insideChrome(e.target)) return;
|
|
1055
1059
|
if (entryAt(e.target)) return;
|
|
1056
1060
|
const target = targetFrom(e.target as Element);
|
|
1057
1061
|
if (!target?.anchor.contains(e.target as Node)) return;
|
|
@@ -1099,13 +1103,13 @@ export function Inspector({
|
|
|
1099
1103
|
|
|
1100
1104
|
container.addEventListener('pointermove', onMove, true);
|
|
1101
1105
|
container.addEventListener('click', onClick, true);
|
|
1102
|
-
|
|
1106
|
+
window.addEventListener('dblclick', onDblClick, true);
|
|
1103
1107
|
window.addEventListener('pointerdown', onPointerDown, true);
|
|
1104
1108
|
window.addEventListener('keydown', onKey, true);
|
|
1105
1109
|
return () => {
|
|
1106
1110
|
container.removeEventListener('pointermove', onMove, true);
|
|
1107
1111
|
container.removeEventListener('click', onClick, true);
|
|
1108
|
-
|
|
1112
|
+
window.removeEventListener('dblclick', onDblClick, true);
|
|
1109
1113
|
window.removeEventListener('pointerdown', onPointerDown, true);
|
|
1110
1114
|
window.removeEventListener('keydown', onKey, true);
|
|
1111
1115
|
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { fitWidthScale, pageAtMarker, sheetsAcross } from './view-mode.ts';
|
|
3
|
+
|
|
4
|
+
describe('fitWidthScale', () => {
|
|
5
|
+
it('fits one sheet, a spread, or a row of three into the width', () => {
|
|
6
|
+
expect(sheetsAcross('continuous')).toBe(1);
|
|
7
|
+
for (const mode of ['continuous', 'spread', 'grid'] as const) {
|
|
8
|
+
const scale = fitWidthScale(mode, 960, 794, 24);
|
|
9
|
+
const across = sheetsAcross(mode);
|
|
10
|
+
// The row the viewer lays out — scaled sheets plus fixed gaps — fills it exactly.
|
|
11
|
+
expect(across * 794 * scale + (across - 1) * 24).toBeCloseTo(960);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('pageAtMarker', () => {
|
|
17
|
+
it('follows a single column', () => {
|
|
18
|
+
expect(pageAtMarker([0, 1100, 2200], 1500)).toBe(2);
|
|
19
|
+
expect(pageAtMarker([0, 1100, 2200], 10)).toBe(1);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('reports the left page of the spread in view', () => {
|
|
23
|
+
// Page 1 alone on the right, then 2–3, 4–5.
|
|
24
|
+
const tops = [0, 1100, 1100, 2200, 2200];
|
|
25
|
+
expect(pageAtMarker(tops, 1500)).toBe(2);
|
|
26
|
+
expect(pageAtMarker(tops, 2300)).toBe(4);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('reports the first sheet of the grid row in view', () => {
|
|
30
|
+
const tops = [0, 0, 0, 300, 300, 300, 600];
|
|
31
|
+
expect(pageAtMarker(tops, 450)).toBe(4);
|
|
32
|
+
expect(pageAtMarker(tops, 650)).toBe(7);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('keeps a page just jumped to while its row is in view', () => {
|
|
36
|
+
const tops = [0, 1100, 1100, 2200, 2200];
|
|
37
|
+
expect(pageAtMarker(tops, 1500, 3)).toBe(3);
|
|
38
|
+
// Scrolled on to the next spread: the jump no longer describes the view.
|
|
39
|
+
expect(pageAtMarker(tops, 2300, 3)).toBe(4);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('falls back to the first page before anything has passed the marker', () => {
|
|
43
|
+
expect(pageAtMarker([200, 1300], 100)).toBe(1);
|
|
44
|
+
expect(pageAtMarker([], 100)).toBe(1);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the viewer lays the sheets out. Every mode keeps all pages in the one
|
|
3
|
+
* scroll container, so the page counter, page jump, the outline, find and the
|
|
4
|
+
* inspector overlay work the same in each — there is no single-sheet mode,
|
|
5
|
+
* which would need its own navigation path through all of them.
|
|
6
|
+
*/
|
|
7
|
+
export const VIEW_MODES = ['continuous', 'spread', 'grid'] as const;
|
|
8
|
+
export type ViewMode = (typeof VIEW_MODES)[number];
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* How many sheets side by side the zoom fits to. Fit width in two-up fits the
|
|
12
|
+
* spread, not one of its pages; in grid it fits a row of three, which is what
|
|
13
|
+
* keeps a chapter readable at a glance rather than a strip of postage stamps.
|
|
14
|
+
*/
|
|
15
|
+
export function sheetsAcross(mode: ViewMode): number {
|
|
16
|
+
return mode === 'continuous' ? 1 : mode === 'spread' ? 2 : 3;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The scale at which the mode's row of sheets exactly fills `width`. The gaps
|
|
21
|
+
* between sheets are fixed pixels, not scaled with the pages, so they come off
|
|
22
|
+
* the width before the sheets share what is left — counting them as page width
|
|
23
|
+
* would leave the row a little too wide, and a grid of three would wrap to two.
|
|
24
|
+
*/
|
|
25
|
+
export function fitWidthScale(
|
|
26
|
+
mode: ViewMode,
|
|
27
|
+
width: number,
|
|
28
|
+
pageWidth: number,
|
|
29
|
+
gap: number,
|
|
30
|
+
): number {
|
|
31
|
+
const across = sheetsAcross(mode);
|
|
32
|
+
return Math.max(0, width - (across - 1) * gap) / (across * pageWidth);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The page the reader is on: the first sheet of the lowest row whose top has
|
|
37
|
+
* passed the marker. In two-up that is the left page of the spread in view —
|
|
38
|
+
* the one a reader starts on — not the right one that shares its row.
|
|
39
|
+
*
|
|
40
|
+
* `prefer` is a page the reader just asked for. Jumping to page 3 in two-up
|
|
41
|
+
* brings the 2–3 spread into view, and reporting 2 would read as the jump
|
|
42
|
+
* having missed; while that page is in the row in view, it is the answer.
|
|
43
|
+
*/
|
|
44
|
+
export function pageAtMarker(tops: number[], marker: number, prefer?: number | null): number {
|
|
45
|
+
let rowTop = Number.NEGATIVE_INFINITY;
|
|
46
|
+
tops.forEach((top) => {
|
|
47
|
+
if (top <= marker && top > rowTop) rowTop = top;
|
|
48
|
+
});
|
|
49
|
+
const index = tops.indexOf(rowTop);
|
|
50
|
+
if (index < 0) return 1;
|
|
51
|
+
if (prefer && tops[prefer - 1] === rowTop) return prefer;
|
|
52
|
+
return index + 1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const key = (docId: string) => `open-doc:view:${docId}`;
|
|
56
|
+
|
|
57
|
+
/** The mode last used for this document, so coming back resumes the same reading. */
|
|
58
|
+
export function readViewMode(docId: string | undefined): ViewMode {
|
|
59
|
+
if (!docId) return 'continuous';
|
|
60
|
+
try {
|
|
61
|
+
const stored = localStorage.getItem(key(docId));
|
|
62
|
+
return (VIEW_MODES as readonly string[]).includes(stored ?? '')
|
|
63
|
+
? (stored as ViewMode)
|
|
64
|
+
: 'continuous';
|
|
65
|
+
} catch {
|
|
66
|
+
return 'continuous';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function writeViewMode(docId: string | undefined, mode: ViewMode): void {
|
|
71
|
+
if (!docId) return;
|
|
72
|
+
try {
|
|
73
|
+
if (mode === 'continuous') localStorage.removeItem(key(docId));
|
|
74
|
+
else localStorage.setItem(key(docId), mode);
|
|
75
|
+
} catch {}
|
|
76
|
+
}
|
package/src/app/routes/doc.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import appConfig from 'virtual:open-doc/config';
|
|
2
2
|
import {
|
|
3
3
|
ArrowLeft,
|
|
4
|
+
BookOpen,
|
|
4
5
|
Check,
|
|
5
6
|
Download,
|
|
6
7
|
Eye,
|
|
@@ -8,6 +9,7 @@ import {
|
|
|
8
9
|
FileImage,
|
|
9
10
|
FileText,
|
|
10
11
|
Image,
|
|
12
|
+
LayoutGrid,
|
|
11
13
|
Loader2,
|
|
12
14
|
Maximize,
|
|
13
15
|
Minimize,
|
|
@@ -18,6 +20,7 @@ import {
|
|
|
18
20
|
Pencil,
|
|
19
21
|
Percent,
|
|
20
22
|
Plus,
|
|
23
|
+
Rows3,
|
|
21
24
|
} from 'lucide-react';
|
|
22
25
|
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
23
26
|
import { Link, useParams } from 'react-router-dom';
|
|
@@ -43,6 +46,13 @@ import { resolvePageGeometry } from '../lib/sdk';
|
|
|
43
46
|
import { useDocModule } from '../lib/use-doc-module';
|
|
44
47
|
import { useDocPages } from '../lib/use-doc-pages';
|
|
45
48
|
import { cn } from '../lib/utils';
|
|
49
|
+
import {
|
|
50
|
+
fitWidthScale as fitScale,
|
|
51
|
+
pageAtMarker,
|
|
52
|
+
readViewMode,
|
|
53
|
+
type ViewMode,
|
|
54
|
+
writeViewMode,
|
|
55
|
+
} from '../lib/view-mode';
|
|
46
56
|
|
|
47
57
|
type DownloadFormat = 'pdf' | 'html' | 'png' | 'svg';
|
|
48
58
|
|
|
@@ -140,6 +150,11 @@ export function Doc() {
|
|
|
140
150
|
const [textPending, setTextPending] = useState(0);
|
|
141
151
|
const [cardShown, setCardShown] = useState(false);
|
|
142
152
|
const [isFullscreen, setIsFullscreen] = useState(false);
|
|
153
|
+
const [viewMode, setViewModeState] = useState<ViewMode>(() => readViewMode(docId));
|
|
154
|
+
// The page the reader was on when the layout changed, to put back in view.
|
|
155
|
+
const keepPageRef = useRef<number | null>(null);
|
|
156
|
+
// The page last jumped to, reported while it shares the row in view.
|
|
157
|
+
const jumpedRef = useRef<number | null>(null);
|
|
143
158
|
|
|
144
159
|
const geometry = useMemo(() => resolvePageGeometry(doc?.meta), [doc?.meta]);
|
|
145
160
|
const { pages, measuring, overflowing } = useDocPages(doc, geometry);
|
|
@@ -148,10 +163,13 @@ export function Doc() {
|
|
|
148
163
|
useAgentBridge({ docId: docId ?? '', doc, pages, geometry, measuring, oversized: overflowing });
|
|
149
164
|
|
|
150
165
|
const clamp = (value: number) => Math.max(MIN_SCALE, Math.min(MAX_SCALE, value));
|
|
151
|
-
|
|
152
|
-
//
|
|
166
|
+
// Every fit is of the unit the mode lays side by side — a sheet, a spread,
|
|
167
|
+
// a row of the grid — so zoom and view mode compose instead of fighting.
|
|
168
|
+
const widthFit = fitScale(viewMode, available.width, geometry.width, PAGE_GAP);
|
|
169
|
+
const fitWidthScale = available.width ? clamp(widthFit) : 1;
|
|
170
|
+
// Fit page is bounded by both axes so the whole unit lands inside the pane.
|
|
153
171
|
const fitPageScale = available.height
|
|
154
|
-
? clamp(Math.min(
|
|
172
|
+
? clamp(Math.min(widthFit, available.height / geometry.height))
|
|
155
173
|
: 1;
|
|
156
174
|
// Auto keeps a page at its true size unless the window is too narrow to hold
|
|
157
175
|
// it; the explicit fit modes may go past 100%.
|
|
@@ -211,11 +229,13 @@ export function Doc() {
|
|
|
211
229
|
frame = 0;
|
|
212
230
|
const marker = root.scrollTop + root.clientHeight / 3;
|
|
213
231
|
const frames = Array.from(container.children) as HTMLElement[];
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
232
|
+
setCurrentPage(
|
|
233
|
+
pageAtMarker(
|
|
234
|
+
frames.map((el) => el.offsetTop),
|
|
235
|
+
marker,
|
|
236
|
+
jumpedRef.current,
|
|
237
|
+
),
|
|
238
|
+
);
|
|
219
239
|
};
|
|
220
240
|
const onScroll = () => {
|
|
221
241
|
if (frame) return;
|
|
@@ -228,9 +248,35 @@ export function Doc() {
|
|
|
228
248
|
root.removeEventListener('scroll', onScroll);
|
|
229
249
|
if (frame) cancelAnimationFrame(frame);
|
|
230
250
|
};
|
|
231
|
-
}, [pages.length, scale]);
|
|
251
|
+
}, [pages.length, scale, viewMode]);
|
|
252
|
+
|
|
253
|
+
useEffect(() => setViewModeState(readViewMode(docId)), [docId]);
|
|
254
|
+
|
|
255
|
+
const setViewMode = (mode: ViewMode) => {
|
|
256
|
+
if (mode === viewMode) return;
|
|
257
|
+
keepPageRef.current = currentPage;
|
|
258
|
+
setViewModeState(mode);
|
|
259
|
+
writeViewMode(docId, mode);
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
// Changing the layout moves every sheet. Without this the reader lands on
|
|
263
|
+
// whatever page now sits where their scroll position happens to be.
|
|
264
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: runs once per layout change, after the sheets have moved.
|
|
265
|
+
useLayoutEffect(() => {
|
|
266
|
+
const page = keepPageRef.current;
|
|
267
|
+
keepPageRef.current = null;
|
|
268
|
+
const root = scrollRef.current;
|
|
269
|
+
const frame = pagesRef.current?.children[page === null ? -1 : page - 1] as
|
|
270
|
+
| HTMLElement
|
|
271
|
+
| undefined;
|
|
272
|
+
if (!root || !frame) return;
|
|
273
|
+
// Keep the gutter above the sheet, as a fresh document opens with, rather
|
|
274
|
+
// than butting its top edge against the toolbar.
|
|
275
|
+
root.scrollTop = Math.max(0, frame.offsetTop - GUTTER);
|
|
276
|
+
}, [viewMode]);
|
|
232
277
|
|
|
233
278
|
const scrollToPage = useCallback((page: number) => {
|
|
279
|
+
jumpedRef.current = page;
|
|
234
280
|
const frame = pagesRef.current?.children[page - 1];
|
|
235
281
|
frame?.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
|
236
282
|
}, []);
|
|
@@ -435,6 +481,31 @@ export function Doc() {
|
|
|
435
481
|
</IconButton>
|
|
436
482
|
</div>
|
|
437
483
|
|
|
484
|
+
<fieldset className="flex items-center gap-0.5 rounded-md border border-border px-1 py-0.5">
|
|
485
|
+
<legend className="sr-only">Page layout</legend>
|
|
486
|
+
<IconButton
|
|
487
|
+
label="Continuous"
|
|
488
|
+
active={viewMode === 'continuous'}
|
|
489
|
+
onClick={() => setViewMode('continuous')}
|
|
490
|
+
>
|
|
491
|
+
<Rows3 className="size-3.5" />
|
|
492
|
+
</IconButton>
|
|
493
|
+
<IconButton
|
|
494
|
+
label="Two-up"
|
|
495
|
+
active={viewMode === 'spread'}
|
|
496
|
+
onClick={() => setViewMode('spread')}
|
|
497
|
+
>
|
|
498
|
+
<BookOpen className="size-3.5" />
|
|
499
|
+
</IconButton>
|
|
500
|
+
<IconButton
|
|
501
|
+
label="Grid"
|
|
502
|
+
active={viewMode === 'grid'}
|
|
503
|
+
onClick={() => setViewMode('grid')}
|
|
504
|
+
>
|
|
505
|
+
<LayoutGrid className="size-3.5" />
|
|
506
|
+
</IconButton>
|
|
507
|
+
</fieldset>
|
|
508
|
+
|
|
438
509
|
<IconButton
|
|
439
510
|
label={isFullscreen ? 'Exit fullscreen (F)' : 'Fullscreen (F)'}
|
|
440
511
|
onClick={toggleFullscreen}
|
|
@@ -563,8 +634,25 @@ export function Doc() {
|
|
|
563
634
|
>
|
|
564
635
|
<div
|
|
565
636
|
ref={pagesRef}
|
|
566
|
-
|
|
567
|
-
|
|
637
|
+
data-od-view={viewMode}
|
|
638
|
+
className={cn(
|
|
639
|
+
viewMode === 'continuous' && 'flex flex-col items-center',
|
|
640
|
+
// Facing pages as a bound document is read: page 1 alone on
|
|
641
|
+
// the right, then 2–3, 4–5.
|
|
642
|
+
viewMode === 'spread' && 'grid justify-center [&>:first-child]:col-start-2',
|
|
643
|
+
// A contact sheet: columns that line up, the last row starting
|
|
644
|
+
// under the first sheet rather than centred in the gap.
|
|
645
|
+
viewMode === 'grid' && 'grid content-start justify-center',
|
|
646
|
+
)}
|
|
647
|
+
style={{
|
|
648
|
+
gap: PAGE_GAP,
|
|
649
|
+
padding: `${GUTTER}px ${GUTTER}px ${GUTTER * 1.5}px`,
|
|
650
|
+
...(viewMode === 'spread'
|
|
651
|
+
? { gridTemplateColumns: `repeat(2, ${geometry.width * scale}px)` }
|
|
652
|
+
: viewMode === 'grid'
|
|
653
|
+
? { gridTemplateColumns: `repeat(auto-fill, ${geometry.width * scale}px)` }
|
|
654
|
+
: {}),
|
|
655
|
+
}}
|
|
568
656
|
>
|
|
569
657
|
{pages.map((page, index) => (
|
|
570
658
|
<PageFrame
|