@open-document/core 0.7.0 → 0.9.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.
@@ -1,13 +1,16 @@
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,
7
8
  FileCode2,
8
9
  FileImage,
9
10
  FileText,
11
+ FileType2,
10
12
  Image,
13
+ LayoutGrid,
11
14
  Loader2,
12
15
  Maximize,
13
16
  Minimize,
@@ -18,6 +21,7 @@ import {
18
21
  Pencil,
19
22
  Percent,
20
23
  Plus,
24
+ Rows3,
21
25
  } from 'lucide-react';
22
26
  import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
23
27
  import { Link, useParams } from 'react-router-dom';
@@ -32,6 +36,7 @@ import { EditSaveCard } from '../components/panel/edit-save-card';
32
36
  import { ThemeToggle } from '../components/theme-toggle';
33
37
  import { Menu, MenuItem } from '../components/ui/menu';
34
38
  import { useAgentBridge } from '../lib/agent-bridge';
39
+ import { exportDocAsDocx } from '../lib/export-docx';
35
40
  import { exportDocAsHtml } from '../lib/export-html';
36
41
  import { exportDocAsImages } from '../lib/export-image';
37
42
  import { exportDocAsPdf } from '../lib/export-pdf';
@@ -43,14 +48,22 @@ import { resolvePageGeometry } from '../lib/sdk';
43
48
  import { useDocModule } from '../lib/use-doc-module';
44
49
  import { useDocPages } from '../lib/use-doc-pages';
45
50
  import { cn } from '../lib/utils';
51
+ import {
52
+ fitWidthScale as fitScale,
53
+ pageAtMarker,
54
+ readViewMode,
55
+ type ViewMode,
56
+ writeViewMode,
57
+ } from '../lib/view-mode';
46
58
 
47
- type DownloadFormat = 'pdf' | 'html' | 'png' | 'svg';
59
+ type DownloadFormat = 'pdf' | 'html' | 'png' | 'svg' | 'docx';
48
60
 
49
61
  const DOWNLOAD_LABEL: Record<DownloadFormat, string> = {
50
62
  pdf: 'PDF',
51
63
  html: 'HTML',
52
64
  png: 'PNG',
53
65
  svg: 'SVG',
66
+ docx: 'Word',
54
67
  };
55
68
 
56
69
  const DOWNLOAD_FORMATS = [
@@ -58,6 +71,12 @@ const DOWNLOAD_FORMATS = [
58
71
  { format: 'html' as const, label: 'HTML', hint: 'Self-contained, printable', icon: FileCode2 },
59
72
  { format: 'png' as const, label: 'PNG', hint: 'Pixels, 2x — for slides and chat', icon: Image },
60
73
  { format: 'svg' as const, label: 'SVG', hint: 'Vector, keeps text as text', icon: FileImage },
74
+ {
75
+ format: 'docx' as const,
76
+ label: 'Word (DOCX)',
77
+ hint: 'Editable — Word lays out the pages',
78
+ icon: FileType2,
79
+ },
61
80
  ];
62
81
 
63
82
  const GUTTER = 48;
@@ -140,6 +159,11 @@ export function Doc() {
140
159
  const [textPending, setTextPending] = useState(0);
141
160
  const [cardShown, setCardShown] = useState(false);
142
161
  const [isFullscreen, setIsFullscreen] = useState(false);
162
+ const [viewMode, setViewModeState] = useState<ViewMode>(() => readViewMode(docId));
163
+ // The page the reader was on when the layout changed, to put back in view.
164
+ const keepPageRef = useRef<number | null>(null);
165
+ // The page last jumped to, reported while it shares the row in view.
166
+ const jumpedRef = useRef<number | null>(null);
143
167
 
144
168
  const geometry = useMemo(() => resolvePageGeometry(doc?.meta), [doc?.meta]);
145
169
  const { pages, measuring, overflowing } = useDocPages(doc, geometry);
@@ -148,10 +172,13 @@ export function Doc() {
148
172
  useAgentBridge({ docId: docId ?? '', doc, pages, geometry, measuring, oversized: overflowing });
149
173
 
150
174
  const clamp = (value: number) => Math.max(MIN_SCALE, Math.min(MAX_SCALE, value));
151
- const fitWidthScale = available.width ? clamp(available.width / geometry.width) : 1;
152
- // Fit page is bounded by both axes so the whole sheet lands inside the pane.
175
+ // Every fit is of the unit the mode lays side by side — a sheet, a spread,
176
+ // a row of the grid — so zoom and view mode compose instead of fighting.
177
+ const widthFit = fitScale(viewMode, available.width, geometry.width, PAGE_GAP);
178
+ const fitWidthScale = available.width ? clamp(widthFit) : 1;
179
+ // Fit page is bounded by both axes so the whole unit lands inside the pane.
153
180
  const fitPageScale = available.height
154
- ? clamp(Math.min(available.width / geometry.width, available.height / geometry.height))
181
+ ? clamp(Math.min(widthFit, available.height / geometry.height))
155
182
  : 1;
156
183
  // Auto keeps a page at its true size unless the window is too narrow to hold
157
184
  // it; the explicit fit modes may go past 100%.
@@ -211,11 +238,13 @@ export function Doc() {
211
238
  frame = 0;
212
239
  const marker = root.scrollTop + root.clientHeight / 3;
213
240
  const frames = Array.from(container.children) as HTMLElement[];
214
- let page = 1;
215
- frames.forEach((el, index) => {
216
- if (el.offsetTop <= marker) page = index + 1;
217
- });
218
- setCurrentPage(page);
241
+ setCurrentPage(
242
+ pageAtMarker(
243
+ frames.map((el) => el.offsetTop),
244
+ marker,
245
+ jumpedRef.current,
246
+ ),
247
+ );
219
248
  };
220
249
  const onScroll = () => {
221
250
  if (frame) return;
@@ -228,9 +257,35 @@ export function Doc() {
228
257
  root.removeEventListener('scroll', onScroll);
229
258
  if (frame) cancelAnimationFrame(frame);
230
259
  };
231
- }, [pages.length, scale]);
260
+ }, [pages.length, scale, viewMode]);
261
+
262
+ useEffect(() => setViewModeState(readViewMode(docId)), [docId]);
263
+
264
+ const setViewMode = (mode: ViewMode) => {
265
+ if (mode === viewMode) return;
266
+ keepPageRef.current = currentPage;
267
+ setViewModeState(mode);
268
+ writeViewMode(docId, mode);
269
+ };
270
+
271
+ // Changing the layout moves every sheet. Without this the reader lands on
272
+ // whatever page now sits where their scroll position happens to be.
273
+ // biome-ignore lint/correctness/useExhaustiveDependencies: runs once per layout change, after the sheets have moved.
274
+ useLayoutEffect(() => {
275
+ const page = keepPageRef.current;
276
+ keepPageRef.current = null;
277
+ const root = scrollRef.current;
278
+ const frame = pagesRef.current?.children[page === null ? -1 : page - 1] as
279
+ | HTMLElement
280
+ | undefined;
281
+ if (!root || !frame) return;
282
+ // Keep the gutter above the sheet, as a fresh document opens with, rather
283
+ // than butting its top edge against the toolbar.
284
+ root.scrollTop = Math.max(0, frame.offsetTop - GUTTER);
285
+ }, [viewMode]);
232
286
 
233
287
  const scrollToPage = useCallback((page: number) => {
288
+ jumpedRef.current = page;
234
289
  const frame = pagesRef.current?.children[page - 1];
235
290
  frame?.scrollIntoView({ behavior: 'smooth', block: 'start' });
236
291
  }, []);
@@ -279,6 +334,8 @@ export function Doc() {
279
334
  );
280
335
  } else if (format === 'html') {
281
336
  await exportDocAsHtml(doc, docId, chosen);
337
+ } else if (format === 'docx') {
338
+ await exportDocAsDocx(doc, docId, chosen);
282
339
  } else {
283
340
  await exportDocAsImages(doc, docId, chosen, format, (progress) =>
284
341
  setDownload({ format, percent: progress.percent }),
@@ -435,6 +492,31 @@ export function Doc() {
435
492
  </IconButton>
436
493
  </div>
437
494
 
495
+ <fieldset className="flex items-center gap-0.5 rounded-md border border-border px-1 py-0.5">
496
+ <legend className="sr-only">Page layout</legend>
497
+ <IconButton
498
+ label="Continuous"
499
+ active={viewMode === 'continuous'}
500
+ onClick={() => setViewMode('continuous')}
501
+ >
502
+ <Rows3 className="size-3.5" />
503
+ </IconButton>
504
+ <IconButton
505
+ label="Two-up"
506
+ active={viewMode === 'spread'}
507
+ onClick={() => setViewMode('spread')}
508
+ >
509
+ <BookOpen className="size-3.5" />
510
+ </IconButton>
511
+ <IconButton
512
+ label="Grid"
513
+ active={viewMode === 'grid'}
514
+ onClick={() => setViewMode('grid')}
515
+ >
516
+ <LayoutGrid className="size-3.5" />
517
+ </IconButton>
518
+ </fieldset>
519
+
438
520
  <IconButton
439
521
  label={isFullscreen ? 'Exit fullscreen (F)' : 'Fullscreen (F)'}
440
522
  onClick={toggleFullscreen}
@@ -563,8 +645,25 @@ export function Doc() {
563
645
  >
564
646
  <div
565
647
  ref={pagesRef}
566
- className="flex flex-col items-center"
567
- style={{ gap: PAGE_GAP, padding: `${GUTTER}px ${GUTTER}px ${GUTTER * 1.5}px` }}
648
+ data-od-view={viewMode}
649
+ className={cn(
650
+ viewMode === 'continuous' && 'flex flex-col items-center',
651
+ // Facing pages as a bound document is read: page 1 alone on
652
+ // the right, then 2–3, 4–5.
653
+ viewMode === 'spread' && 'grid justify-center [&>:first-child]:col-start-2',
654
+ // A contact sheet: columns that line up, the last row starting
655
+ // under the first sheet rather than centred in the gap.
656
+ viewMode === 'grid' && 'grid content-start justify-center',
657
+ )}
658
+ style={{
659
+ gap: PAGE_GAP,
660
+ padding: `${GUTTER}px ${GUTTER}px ${GUTTER * 1.5}px`,
661
+ ...(viewMode === 'spread'
662
+ ? { gridTemplateColumns: `repeat(2, ${geometry.width * scale}px)` }
663
+ : viewMode === 'grid'
664
+ ? { gridTemplateColumns: `repeat(auto-fill, ${geometry.width * scale}px)` }
665
+ : {}),
666
+ }}
568
667
  >
569
668
  {pages.map((page, index) => (
570
669
  <PageFrame