@open-document/core 0.4.0 → 0.5.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 +1 -1
- package/src/app/components/doc-search.tsx +248 -0
- package/src/app/components/ui/menu.tsx +4 -0
- package/src/app/lib/export-dom.ts +183 -0
- package/src/app/lib/export-html.ts +12 -183
- package/src/app/lib/export-image.ts +226 -0
- package/src/app/lib/page-range.test.ts +95 -0
- package/src/app/lib/page-range.ts +90 -0
- package/src/app/routes/doc.tsx +222 -24
- package/src/app/styles.css +25 -0
package/src/app/routes/doc.tsx
CHANGED
|
@@ -4,21 +4,25 @@ import {
|
|
|
4
4
|
Check,
|
|
5
5
|
Download,
|
|
6
6
|
FileCode2,
|
|
7
|
+
FileImage,
|
|
7
8
|
FileText,
|
|
9
|
+
Image,
|
|
8
10
|
Loader2,
|
|
9
11
|
Maximize,
|
|
10
12
|
Minimize,
|
|
11
13
|
Minus,
|
|
12
14
|
MousePointerClick,
|
|
13
15
|
MoveHorizontal,
|
|
16
|
+
MoveVertical,
|
|
14
17
|
Palette,
|
|
18
|
+
Percent,
|
|
15
19
|
Plus,
|
|
16
|
-
Scan,
|
|
17
20
|
} from 'lucide-react';
|
|
18
21
|
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
19
22
|
import { Link, useParams } from 'react-router-dom';
|
|
20
23
|
import { DesignPanel } from '../components/design-panel/design-panel';
|
|
21
24
|
import { DesignProvider } from '../components/design-panel/design-provider';
|
|
25
|
+
import { DocSearch } from '../components/doc-search';
|
|
22
26
|
import { DocSidebar } from '../components/doc-sidebar';
|
|
23
27
|
import { Inspector } from '../components/inspector/inspector';
|
|
24
28
|
import { PageFrame } from '../components/page-frame';
|
|
@@ -26,8 +30,10 @@ import { ThemeToggle } from '../components/theme-toggle';
|
|
|
26
30
|
import { Menu, MenuItem } from '../components/ui/menu';
|
|
27
31
|
import { useAgentBridge } from '../lib/agent-bridge';
|
|
28
32
|
import { exportDocAsHtml } from '../lib/export-html';
|
|
33
|
+
import { exportDocAsImages } from '../lib/export-image';
|
|
29
34
|
import { exportDocAsPdf } from '../lib/export-pdf';
|
|
30
35
|
import { type OutlineEntry, useDocOutline } from '../lib/outline';
|
|
36
|
+
import { describeSelection, type PageSelection, resolveSelection } from '../lib/page-range';
|
|
31
37
|
import { nextFrame, waitForFonts } from '../lib/print-ready';
|
|
32
38
|
import { scanDocument } from '../lib/scan';
|
|
33
39
|
import { resolvePageGeometry } from '../lib/sdk';
|
|
@@ -35,16 +41,20 @@ import { useDocModule } from '../lib/use-doc-module';
|
|
|
35
41
|
import { useDocPages } from '../lib/use-doc-pages';
|
|
36
42
|
import { cn } from '../lib/utils';
|
|
37
43
|
|
|
38
|
-
type DownloadFormat = 'pdf' | 'html';
|
|
44
|
+
type DownloadFormat = 'pdf' | 'html' | 'png' | 'svg';
|
|
39
45
|
|
|
40
46
|
const DOWNLOAD_LABEL: Record<DownloadFormat, string> = {
|
|
41
47
|
pdf: 'PDF',
|
|
42
48
|
html: 'HTML',
|
|
49
|
+
png: 'PNG',
|
|
50
|
+
svg: 'SVG',
|
|
43
51
|
};
|
|
44
52
|
|
|
45
53
|
const DOWNLOAD_FORMATS = [
|
|
46
54
|
{ format: 'pdf' as const, label: 'PDF', hint: 'True page size, print-ready', icon: FileText },
|
|
47
55
|
{ format: 'html' as const, label: 'HTML', hint: 'Self-contained, printable', icon: FileCode2 },
|
|
56
|
+
{ format: 'png' as const, label: 'PNG', hint: 'Pixels, 2x — for slides and chat', icon: Image },
|
|
57
|
+
{ format: 'svg' as const, label: 'SVG', hint: 'Vector, keeps text as text', icon: FileImage },
|
|
48
58
|
];
|
|
49
59
|
|
|
50
60
|
const GUTTER = 48;
|
|
@@ -98,6 +108,8 @@ export function Doc() {
|
|
|
98
108
|
null,
|
|
99
109
|
);
|
|
100
110
|
const [downloaded, setDownloaded] = useState<DownloadFormat | null>(null);
|
|
111
|
+
const [selection, setSelection] = useState<PageSelection>({ kind: 'all' });
|
|
112
|
+
const [customRange, setCustomRange] = useState('');
|
|
101
113
|
const [designOpen, setDesignOpen] = useState(false);
|
|
102
114
|
const [inspecting, setInspecting] = useState(false);
|
|
103
115
|
const [isFullscreen, setIsFullscreen] = useState(false);
|
|
@@ -217,14 +229,33 @@ export function Doc() {
|
|
|
217
229
|
|
|
218
230
|
const runDownload = async (format: DownloadFormat) => {
|
|
219
231
|
if (!doc || !docId || download) return;
|
|
232
|
+
|
|
233
|
+
/*
|
|
234
|
+
* The pages are chosen here, once, and every exporter is handed the subset
|
|
235
|
+
* rather than the whole document plus a range to obey. An exporter that has
|
|
236
|
+
* to remember to filter is an exporter that will one day forget.
|
|
237
|
+
*/
|
|
238
|
+
const wanted = resolveSelection(
|
|
239
|
+
selection.kind === 'custom' ? { kind: 'custom', text: customRange } : selection,
|
|
240
|
+
pages.length,
|
|
241
|
+
currentPage,
|
|
242
|
+
);
|
|
243
|
+
if (!wanted) return;
|
|
244
|
+
const chosen = wanted.map((index) => pages[index]).filter((page) => page !== undefined);
|
|
245
|
+
if (chosen.length === 0) return;
|
|
246
|
+
|
|
220
247
|
setDownload({ format, percent: 0 });
|
|
221
248
|
try {
|
|
222
249
|
if (format === 'pdf') {
|
|
223
|
-
await exportDocAsPdf(doc, docId,
|
|
250
|
+
await exportDocAsPdf(doc, docId, chosen, (progress) =>
|
|
224
251
|
setDownload({ format, percent: progress.percent }),
|
|
225
252
|
);
|
|
253
|
+
} else if (format === 'html') {
|
|
254
|
+
await exportDocAsHtml(doc, docId, chosen);
|
|
226
255
|
} else {
|
|
227
|
-
await
|
|
256
|
+
await exportDocAsImages(doc, docId, chosen, format, (progress) =>
|
|
257
|
+
setDownload({ format, percent: progress.percent }),
|
|
258
|
+
);
|
|
228
259
|
}
|
|
229
260
|
setDownloaded(format);
|
|
230
261
|
setTimeout(() => setDownloaded(null), 2000);
|
|
@@ -233,6 +264,13 @@ export function Doc() {
|
|
|
233
264
|
}
|
|
234
265
|
};
|
|
235
266
|
|
|
267
|
+
/* What the menu is about to do, so nobody has to count commas themselves. */
|
|
268
|
+
const chosenPages = describeSelection(
|
|
269
|
+
selection.kind === 'custom' ? { kind: 'custom', text: customRange } : selection,
|
|
270
|
+
pages.length,
|
|
271
|
+
currentPage,
|
|
272
|
+
);
|
|
273
|
+
|
|
236
274
|
const zoom = (delta: number) => {
|
|
237
275
|
setManualScale((prev) =>
|
|
238
276
|
Math.min(MAX_SCALE, Math.max(MIN_SCALE, Number(((prev ?? scale) + delta).toFixed(2)))),
|
|
@@ -324,8 +362,9 @@ export function Doc() {
|
|
|
324
362
|
<h1 className="truncate text-center font-medium text-sm">{doc.meta?.title ?? docId}</h1>
|
|
325
363
|
|
|
326
364
|
<div className="flex items-center justify-end gap-3">
|
|
327
|
-
<span className="hidden
|
|
328
|
-
{currentPage}
|
|
365
|
+
<span className="hidden items-center gap-1.5 sm:flex">
|
|
366
|
+
<PageJump page={currentPage} total={pages.length} onJump={scrollToPage} />
|
|
367
|
+
<DocSearch scrollRef={scrollRef} pagesRef={pagesRef} onFoundPage={setCurrentPage} />
|
|
329
368
|
</span>
|
|
330
369
|
|
|
331
370
|
<div className="flex items-center gap-0.5 rounded-md border border-border px-1 py-0.5">
|
|
@@ -350,12 +389,19 @@ export function Doc() {
|
|
|
350
389
|
>
|
|
351
390
|
<MoveHorizontal className="size-3.5" />
|
|
352
391
|
</IconButton>
|
|
392
|
+
{/* Fit-width moves the page sideways to the edges, fit-page moves it
|
|
393
|
+
up and down to them. Both used to be a square-ish glyph, and the
|
|
394
|
+
fit-page one was the same square as fullscreen — three controls,
|
|
395
|
+
two shapes, no way to tell which did what without clicking. */}
|
|
353
396
|
<IconButton
|
|
354
397
|
label="Fit page"
|
|
355
398
|
onClick={() => fitTo('fit-page')}
|
|
356
399
|
active={manualScale === null && zoomMode === 'fit-page'}
|
|
357
400
|
>
|
|
358
|
-
<
|
|
401
|
+
<MoveVertical className="size-3.5" />
|
|
402
|
+
</IconButton>
|
|
403
|
+
<IconButton label="Actual size (100%)" onClick={actualSize}>
|
|
404
|
+
<Percent className="size-3.5" />
|
|
359
405
|
</IconButton>
|
|
360
406
|
</div>
|
|
361
407
|
|
|
@@ -419,23 +465,37 @@ export function Doc() {
|
|
|
419
465
|
</button>
|
|
420
466
|
)}
|
|
421
467
|
>
|
|
422
|
-
{(close) =>
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
468
|
+
{(close) => (
|
|
469
|
+
<>
|
|
470
|
+
{/* Which pages, before which format. A reader who picks PDF and
|
|
471
|
+
then discovers they exported forty pages has already waited
|
|
472
|
+
for all forty. */}
|
|
473
|
+
<PageChoice
|
|
474
|
+
selection={selection}
|
|
475
|
+
custom={customRange}
|
|
476
|
+
currentPage={currentPage}
|
|
477
|
+
total={pages.length}
|
|
478
|
+
onSelection={setSelection}
|
|
479
|
+
onCustom={setCustomRange}
|
|
480
|
+
/>
|
|
481
|
+
{DOWNLOAD_FORMATS.map(({ format, label, hint, icon: Icon }) => (
|
|
482
|
+
<MenuItem
|
|
483
|
+
key={format}
|
|
484
|
+
disabled={!chosenPages.valid}
|
|
485
|
+
onClick={() => {
|
|
486
|
+
close();
|
|
487
|
+
void runDownload(format);
|
|
488
|
+
}}
|
|
489
|
+
>
|
|
490
|
+
<Icon className="size-3.5 flex-none" />
|
|
491
|
+
<span className="flex-1">
|
|
492
|
+
{label}
|
|
493
|
+
<span className="block text-[10px] text-muted-foreground">{hint}</span>
|
|
494
|
+
</span>
|
|
495
|
+
</MenuItem>
|
|
496
|
+
))}
|
|
497
|
+
</>
|
|
498
|
+
)}
|
|
439
499
|
</Menu>
|
|
440
500
|
</div>
|
|
441
501
|
</header>
|
|
@@ -490,6 +550,144 @@ export function Doc() {
|
|
|
490
550
|
return <DesignProvider docId={docId}>{view}</DesignProvider>;
|
|
491
551
|
}
|
|
492
552
|
|
|
553
|
+
/**
|
|
554
|
+
* 全部/此頁/自訂 —— 和列印對話框問的是同一件事,因為那是使用者已經會的問法。
|
|
555
|
+
*
|
|
556
|
+
* 自訂欄位只在被選中時出現。三個選項配一個永遠佔著位置的空欄位,會讓人以為那是
|
|
557
|
+
* 必填的。
|
|
558
|
+
*/
|
|
559
|
+
function PageChoice({
|
|
560
|
+
selection,
|
|
561
|
+
custom,
|
|
562
|
+
currentPage,
|
|
563
|
+
total,
|
|
564
|
+
onSelection,
|
|
565
|
+
onCustom,
|
|
566
|
+
}: {
|
|
567
|
+
selection: PageSelection;
|
|
568
|
+
custom: string;
|
|
569
|
+
currentPage: number;
|
|
570
|
+
total: number;
|
|
571
|
+
onSelection: (selection: PageSelection) => void;
|
|
572
|
+
onCustom: (text: string) => void;
|
|
573
|
+
}) {
|
|
574
|
+
const options = [
|
|
575
|
+
{ kind: 'all' as const, label: 'All', hint: `${total}` },
|
|
576
|
+
{ kind: 'current' as const, label: 'This page', hint: `${currentPage}` },
|
|
577
|
+
{ kind: 'custom' as const, label: 'Custom', hint: '' },
|
|
578
|
+
];
|
|
579
|
+
const chosen = describeSelection(
|
|
580
|
+
selection.kind === 'custom' ? { kind: 'custom', text: custom } : selection,
|
|
581
|
+
total,
|
|
582
|
+
currentPage,
|
|
583
|
+
);
|
|
584
|
+
|
|
585
|
+
return (
|
|
586
|
+
<div className="border-border border-b px-1 pt-1 pb-2">
|
|
587
|
+
<p className="px-1 pb-1 text-[10px] text-muted-foreground uppercase tracking-wide">Pages</p>
|
|
588
|
+
<div className="flex gap-0.5">
|
|
589
|
+
{options.map((option) => (
|
|
590
|
+
<button
|
|
591
|
+
key={option.kind}
|
|
592
|
+
type="button"
|
|
593
|
+
onClick={(event) => {
|
|
594
|
+
event.stopPropagation();
|
|
595
|
+
onSelection(
|
|
596
|
+
option.kind === 'custom' ? { kind: 'custom', text: custom } : { kind: option.kind },
|
|
597
|
+
);
|
|
598
|
+
}}
|
|
599
|
+
className={cn(
|
|
600
|
+
'flex-1 rounded px-2 py-1 text-[11px] transition-colors hover:bg-accent',
|
|
601
|
+
selection.kind === option.kind && 'bg-accent text-foreground',
|
|
602
|
+
)}
|
|
603
|
+
>
|
|
604
|
+
{option.label}
|
|
605
|
+
{option.hint && (
|
|
606
|
+
<span className="ml-1 font-mono text-[10px] text-muted-foreground">
|
|
607
|
+
{option.hint}
|
|
608
|
+
</span>
|
|
609
|
+
)}
|
|
610
|
+
</button>
|
|
611
|
+
))}
|
|
612
|
+
</div>
|
|
613
|
+
{selection.kind === 'custom' && (
|
|
614
|
+
<input
|
|
615
|
+
value={custom}
|
|
616
|
+
onChange={(event) => onCustom(event.target.value)}
|
|
617
|
+
onClick={(event) => event.stopPropagation()}
|
|
618
|
+
placeholder="e.g. 1-3, 5"
|
|
619
|
+
aria-label="Pages to download"
|
|
620
|
+
aria-invalid={!chosen.valid}
|
|
621
|
+
className={cn(
|
|
622
|
+
'mt-1.5 w-full rounded border border-border bg-transparent px-2 py-1 text-[11px] outline-none placeholder:text-muted-foreground focus:border-foreground/40',
|
|
623
|
+
!chosen.valid && custom !== '' && 'border-foreground/40',
|
|
624
|
+
)}
|
|
625
|
+
/>
|
|
626
|
+
)}
|
|
627
|
+
<p className="px-1 pt-1.5 text-[10px] text-muted-foreground">
|
|
628
|
+
{chosen.valid
|
|
629
|
+
? `${chosen.count} page${chosen.count === 1 ? '' : 's'} will be downloaded`
|
|
630
|
+
: 'Type page numbers, like 1-3, 5'}
|
|
631
|
+
</p>
|
|
632
|
+
</div>
|
|
633
|
+
);
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* 頁碼既是顯示也是輸入。
|
|
638
|
+
*
|
|
639
|
+
* 只在編輯時才變成受控欄位:一直受控的話,讀者捲動時每一次頁碼更新都會把游標推
|
|
640
|
+
* 回去,打到一半的數字就消失了。
|
|
641
|
+
*/
|
|
642
|
+
function PageJump({
|
|
643
|
+
page,
|
|
644
|
+
total,
|
|
645
|
+
onJump,
|
|
646
|
+
}: {
|
|
647
|
+
page: number;
|
|
648
|
+
total: number;
|
|
649
|
+
onJump: (page: number) => void;
|
|
650
|
+
}) {
|
|
651
|
+
const [draft, setDraft] = useState<string | null>(null);
|
|
652
|
+
|
|
653
|
+
const commit = (raw: string) => {
|
|
654
|
+
const wanted = Number(raw.replace(/[0-9]/g, (d) => String(d.charCodeAt(0) - 0xff10)));
|
|
655
|
+
setDraft(null);
|
|
656
|
+
if (!Number.isFinite(wanted) || wanted < 1) return;
|
|
657
|
+
onJump(Math.min(total, Math.round(wanted)));
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
return (
|
|
661
|
+
<span className="flex items-center whitespace-nowrap font-mono text-muted-foreground text-xs tabular-nums">
|
|
662
|
+
<input
|
|
663
|
+
value={draft ?? String(page)}
|
|
664
|
+
onChange={(event) => setDraft(event.target.value)}
|
|
665
|
+
onFocus={(event) => {
|
|
666
|
+
setDraft(String(page));
|
|
667
|
+
event.target.select();
|
|
668
|
+
}}
|
|
669
|
+
onBlur={(event) => commit(event.target.value)}
|
|
670
|
+
onKeyDown={(event) => {
|
|
671
|
+
if (event.key === 'Enter') {
|
|
672
|
+
event.preventDefault();
|
|
673
|
+
event.currentTarget.blur();
|
|
674
|
+
}
|
|
675
|
+
if (event.key === 'Escape') {
|
|
676
|
+
setDraft(null);
|
|
677
|
+
event.currentTarget.blur();
|
|
678
|
+
}
|
|
679
|
+
}}
|
|
680
|
+
aria-label={`Page number, ${total} pages`}
|
|
681
|
+
title="Go to page"
|
|
682
|
+
inputMode="numeric"
|
|
683
|
+
className="w-7 rounded bg-transparent text-right outline-none transition-colors hover:bg-accent focus:bg-accent focus:text-foreground"
|
|
684
|
+
/>
|
|
685
|
+
<span className="px-1">/</span>
|
|
686
|
+
<span>{total}</span>
|
|
687
|
+
</span>
|
|
688
|
+
);
|
|
689
|
+
}
|
|
690
|
+
|
|
493
691
|
function IconButton({
|
|
494
692
|
label,
|
|
495
693
|
onClick,
|
package/src/app/styles.css
CHANGED
|
@@ -70,3 +70,28 @@
|
|
|
70
70
|
-webkit-font-smoothing: antialiased;
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
+
|
|
74
|
+
/* Search hits.
|
|
75
|
+
*
|
|
76
|
+
* Drawn with the CSS Custom Highlight API, so nothing in the page is wrapped or
|
|
77
|
+
* rewritten to show a match. Wrapping matches in <mark> would mean editing a
|
|
78
|
+
* tree React owns, and the next render would throw the marks away — or worse,
|
|
79
|
+
* keep them and lose the reader's place. Highlights are ranges held beside the
|
|
80
|
+
* DOM instead, which is what they are for.
|
|
81
|
+
*
|
|
82
|
+
* A browser without the API shows no highlight; the page-to-page jumping still
|
|
83
|
+
* works, so search degrades to "take me there" rather than breaking.
|
|
84
|
+
*/
|
|
85
|
+
/* Literal colours, deliberately. These paint the sheet, and the sheet is paper —
|
|
86
|
+
it does not follow the viewer's theme. A chrome token here resolves to
|
|
87
|
+
near-white in dark mode and disappears on a white page, which is the leak the
|
|
88
|
+
guidelines warn about, arriving as an invisible highlight rather than an
|
|
89
|
+
obviously wrong one. Amber is what every reader already reads as "found". */
|
|
90
|
+
::highlight(od-search) {
|
|
91
|
+
background-color: rgb(255 214 0 / 0.42);
|
|
92
|
+
color: inherit;
|
|
93
|
+
}
|
|
94
|
+
::highlight(od-search-active) {
|
|
95
|
+
background-color: rgb(255 138 0 / 0.62);
|
|
96
|
+
color: inherit;
|
|
97
|
+
}
|