@open-document/core 0.3.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/README.md +1 -0
- package/README.zh-TW.md +1 -0
- package/dist/{build-C3pMGxJN.js → build-BzhYQKev.js} +1 -1
- package/dist/{check-Dtwb1wvU.js → check-CCAaXofH.js} +3 -3
- package/dist/cli/bin.js +33 -21
- package/dist/{config-CSC5lHsR.d.ts → config-P61niZMf.d.ts} +6 -0
- package/dist/{context-FoX0afRf.js → context-CJpdjYfU.js} +3 -14
- package/dist/{dev-Cdd7NZRQ.js → dev-BhXgunme.js} +1 -1
- package/dist/{export-ClJ4T1uU.js → export-B69kDkyp.js} +3 -3
- package/dist/{import-D7Psi8WK.js → import-DBhCpUsl.js} +3 -2
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -40
- package/dist/ops/index.d.ts +2 -2
- package/dist/ops/index.js +4 -3
- package/dist/{ops-Ci5Xw6QG.js → ops-BCBmRMCS.js} +7 -2
- package/dist/{preview-BhbCiAKC.js → preview-Da4Aq9yF.js} +1 -1
- package/dist/{sdk-CWMsH9Ms.d.ts → sdk-C9nl30dW.d.ts} +13 -7
- package/dist/sdk-DhcqZdAw.js +50 -0
- package/dist/vite/index.d.ts +1 -1
- package/dist/vite/index.js +1 -1
- package/package.json +1 -1
- package/skills/create-doc/SKILL.md +1 -1
- package/skills/create-theme/SKILL.md +1 -1
- package/skills/doc-authoring/SKILL.md +7 -6
- package/skills/doc-authoring/references/pagination.md +2 -1
- package/src/app/components/doc-search.tsx +248 -0
- package/src/app/components/themes/theme-preview.tsx +2 -2
- 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/lib/sdk.test.ts +15 -9
- package/src/app/lib/sdk.ts +34 -11
- package/src/app/routes/doc.tsx +352 -114
- package/src/app/styles.css +25 -0
- package/dist/{config-Dg1AMP0r.js → config-CNr8hd5X.js} +1 -1
package/src/app/routes/doc.tsx
CHANGED
|
@@ -1,31 +1,39 @@
|
|
|
1
|
+
import appConfig from 'virtual:open-doc/config';
|
|
1
2
|
import {
|
|
2
3
|
ArrowLeft,
|
|
3
4
|
Check,
|
|
4
5
|
Download,
|
|
5
6
|
FileCode2,
|
|
7
|
+
FileImage,
|
|
6
8
|
FileText,
|
|
9
|
+
Image,
|
|
7
10
|
Loader2,
|
|
8
11
|
Maximize,
|
|
9
12
|
Minimize,
|
|
10
13
|
Minus,
|
|
11
14
|
MousePointerClick,
|
|
12
15
|
MoveHorizontal,
|
|
16
|
+
MoveVertical,
|
|
13
17
|
Palette,
|
|
18
|
+
Percent,
|
|
14
19
|
Plus,
|
|
15
|
-
Scan,
|
|
16
20
|
} from 'lucide-react';
|
|
17
21
|
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
18
22
|
import { Link, useParams } from 'react-router-dom';
|
|
19
23
|
import { DesignPanel } from '../components/design-panel/design-panel';
|
|
20
24
|
import { DesignProvider } from '../components/design-panel/design-provider';
|
|
25
|
+
import { DocSearch } from '../components/doc-search';
|
|
21
26
|
import { DocSidebar } from '../components/doc-sidebar';
|
|
22
27
|
import { Inspector } from '../components/inspector/inspector';
|
|
23
28
|
import { PageFrame } from '../components/page-frame';
|
|
29
|
+
import { ThemeToggle } from '../components/theme-toggle';
|
|
24
30
|
import { Menu, MenuItem } from '../components/ui/menu';
|
|
25
31
|
import { useAgentBridge } from '../lib/agent-bridge';
|
|
26
32
|
import { exportDocAsHtml } from '../lib/export-html';
|
|
33
|
+
import { exportDocAsImages } from '../lib/export-image';
|
|
27
34
|
import { exportDocAsPdf } from '../lib/export-pdf';
|
|
28
35
|
import { type OutlineEntry, useDocOutline } from '../lib/outline';
|
|
36
|
+
import { describeSelection, type PageSelection, resolveSelection } from '../lib/page-range';
|
|
29
37
|
import { nextFrame, waitForFonts } from '../lib/print-ready';
|
|
30
38
|
import { scanDocument } from '../lib/scan';
|
|
31
39
|
import { resolvePageGeometry } from '../lib/sdk';
|
|
@@ -33,16 +41,20 @@ import { useDocModule } from '../lib/use-doc-module';
|
|
|
33
41
|
import { useDocPages } from '../lib/use-doc-pages';
|
|
34
42
|
import { cn } from '../lib/utils';
|
|
35
43
|
|
|
36
|
-
type DownloadFormat = 'pdf' | 'html';
|
|
44
|
+
type DownloadFormat = 'pdf' | 'html' | 'png' | 'svg';
|
|
37
45
|
|
|
38
46
|
const DOWNLOAD_LABEL: Record<DownloadFormat, string> = {
|
|
39
47
|
pdf: 'PDF',
|
|
40
48
|
html: 'HTML',
|
|
49
|
+
png: 'PNG',
|
|
50
|
+
svg: 'SVG',
|
|
41
51
|
};
|
|
42
52
|
|
|
43
53
|
const DOWNLOAD_FORMATS = [
|
|
44
54
|
{ format: 'pdf' as const, label: 'PDF', hint: 'True page size, print-ready', icon: FileText },
|
|
45
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 },
|
|
46
58
|
];
|
|
47
59
|
|
|
48
60
|
const GUTTER = 48;
|
|
@@ -52,6 +64,34 @@ const MIN_SCALE = 0.25;
|
|
|
52
64
|
const MAX_SCALE = 2;
|
|
53
65
|
const PAGE_GAP = 24;
|
|
54
66
|
|
|
67
|
+
const BACK_CLASS =
|
|
68
|
+
'flex size-8 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-foreground';
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* `config.home` leaves the router on purpose. A `<Link>` resolves inside the
|
|
72
|
+
* app's basename, which is exactly wrong when the viewer is mounted under a
|
|
73
|
+
* larger site and "back" means that site.
|
|
74
|
+
*
|
|
75
|
+
* With no `home` and no document browser there is nowhere to go: `/` renders
|
|
76
|
+
* "not found". A control that leads nowhere is worse than no control, and a
|
|
77
|
+
* host that mounts the viewer this way is providing its own way back.
|
|
78
|
+
*/
|
|
79
|
+
const HeaderBackLink = () => {
|
|
80
|
+
if (appConfig.home !== undefined) {
|
|
81
|
+
return (
|
|
82
|
+
<a href={appConfig.home} className={BACK_CLASS} aria-label="Back to workspace">
|
|
83
|
+
<ArrowLeft className="size-4" />
|
|
84
|
+
</a>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
if (!appConfig.build.showDocBrowser) return null;
|
|
88
|
+
return (
|
|
89
|
+
<Link to="/" className={BACK_CLASS} aria-label="Back to documents">
|
|
90
|
+
<ArrowLeft className="size-4" />
|
|
91
|
+
</Link>
|
|
92
|
+
);
|
|
93
|
+
};
|
|
94
|
+
|
|
55
95
|
export function Doc() {
|
|
56
96
|
const { docId } = useParams<{ docId: string }>();
|
|
57
97
|
const state = useDocModule(docId);
|
|
@@ -68,6 +108,8 @@ export function Doc() {
|
|
|
68
108
|
null,
|
|
69
109
|
);
|
|
70
110
|
const [downloaded, setDownloaded] = useState<DownloadFormat | null>(null);
|
|
111
|
+
const [selection, setSelection] = useState<PageSelection>({ kind: 'all' });
|
|
112
|
+
const [customRange, setCustomRange] = useState('');
|
|
71
113
|
const [designOpen, setDesignOpen] = useState(false);
|
|
72
114
|
const [inspecting, setInspecting] = useState(false);
|
|
73
115
|
const [isFullscreen, setIsFullscreen] = useState(false);
|
|
@@ -187,14 +229,33 @@ export function Doc() {
|
|
|
187
229
|
|
|
188
230
|
const runDownload = async (format: DownloadFormat) => {
|
|
189
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
|
+
|
|
190
247
|
setDownload({ format, percent: 0 });
|
|
191
248
|
try {
|
|
192
249
|
if (format === 'pdf') {
|
|
193
|
-
await exportDocAsPdf(doc, docId,
|
|
250
|
+
await exportDocAsPdf(doc, docId, chosen, (progress) =>
|
|
194
251
|
setDownload({ format, percent: progress.percent }),
|
|
195
252
|
);
|
|
253
|
+
} else if (format === 'html') {
|
|
254
|
+
await exportDocAsHtml(doc, docId, chosen);
|
|
196
255
|
} else {
|
|
197
|
-
await
|
|
256
|
+
await exportDocAsImages(doc, docId, chosen, format, (progress) =>
|
|
257
|
+
setDownload({ format, percent: progress.percent }),
|
|
258
|
+
);
|
|
198
259
|
}
|
|
199
260
|
setDownloaded(format);
|
|
200
261
|
setTimeout(() => setDownloaded(null), 2000);
|
|
@@ -203,6 +264,13 @@ export function Doc() {
|
|
|
203
264
|
}
|
|
204
265
|
};
|
|
205
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
|
+
|
|
206
274
|
const zoom = (delta: number) => {
|
|
207
275
|
setManualScale((prev) =>
|
|
208
276
|
Math.min(MAX_SCALE, Math.max(MIN_SCALE, Number(((prev ?? scale) + delta).toFixed(2)))),
|
|
@@ -280,129 +348,156 @@ export function Doc() {
|
|
|
280
348
|
|
|
281
349
|
const view = (
|
|
282
350
|
<div ref={rootRef} className="flex h-screen flex-col bg-background text-foreground">
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
<h1 className="truncate font-medium text-sm">{doc.meta?.title ?? docId}</h1>
|
|
293
|
-
{doc.meta?.subtitle && (
|
|
294
|
-
<p className="truncate text-muted-foreground text-xs">{doc.meta.subtitle}</p>
|
|
295
|
-
)}
|
|
351
|
+
{/* Equal `1fr` rails put the title at the true centre of the bar rather
|
|
352
|
+
than the centre of what is left over, which is where a flex row would
|
|
353
|
+
drop it — the control cluster is many times wider than the back link.
|
|
354
|
+
The control rail keeps its automatic minimum — no `min-w-0` — so when
|
|
355
|
+
it outgrows its share the title truncates and slides instead of being
|
|
356
|
+
overlapped by it. */}
|
|
357
|
+
<header className="grid h-12 flex-none grid-cols-[1fr_minmax(0,auto)_1fr] items-center gap-3 border-b border-border px-3">
|
|
358
|
+
<div className="flex min-w-0 items-center">
|
|
359
|
+
<HeaderBackLink />
|
|
296
360
|
</div>
|
|
297
361
|
|
|
298
|
-
<
|
|
299
|
-
|
|
300
|
-
|
|
362
|
+
<h1 className="truncate text-center font-medium text-sm">{doc.meta?.title ?? docId}</h1>
|
|
363
|
+
|
|
364
|
+
<div className="flex items-center justify-end gap-3">
|
|
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} />
|
|
368
|
+
</span>
|
|
369
|
+
|
|
370
|
+
<div className="flex items-center gap-0.5 rounded-md border border-border px-1 py-0.5">
|
|
371
|
+
<IconButton label="Zoom out" onClick={() => zoom(-0.1)}>
|
|
372
|
+
<Minus className="size-3.5" />
|
|
373
|
+
</IconButton>
|
|
374
|
+
<button
|
|
375
|
+
type="button"
|
|
376
|
+
onClick={actualSize}
|
|
377
|
+
title="Actual size (100%)"
|
|
378
|
+
className="w-11 rounded text-center font-mono text-[11px] tabular-nums transition-colors hover:bg-accent"
|
|
379
|
+
>
|
|
380
|
+
{Math.round(scale * 100)}%
|
|
381
|
+
</button>
|
|
382
|
+
<IconButton label="Zoom in" onClick={() => zoom(0.1)}>
|
|
383
|
+
<Plus className="size-3.5" />
|
|
384
|
+
</IconButton>
|
|
385
|
+
<IconButton
|
|
386
|
+
label="Fit width"
|
|
387
|
+
onClick={() => fitTo('fit-width')}
|
|
388
|
+
active={manualScale === null && zoomMode === 'fit-width'}
|
|
389
|
+
>
|
|
390
|
+
<MoveHorizontal className="size-3.5" />
|
|
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. */}
|
|
396
|
+
<IconButton
|
|
397
|
+
label="Fit page"
|
|
398
|
+
onClick={() => fitTo('fit-page')}
|
|
399
|
+
active={manualScale === null && zoomMode === 'fit-page'}
|
|
400
|
+
>
|
|
401
|
+
<MoveVertical className="size-3.5" />
|
|
402
|
+
</IconButton>
|
|
403
|
+
<IconButton label="Actual size (100%)" onClick={actualSize}>
|
|
404
|
+
<Percent className="size-3.5" />
|
|
405
|
+
</IconButton>
|
|
406
|
+
</div>
|
|
301
407
|
|
|
302
|
-
<div className="flex items-center gap-0.5 rounded-md border border-border px-1 py-0.5">
|
|
303
|
-
<IconButton label="Zoom out" onClick={() => zoom(-0.1)}>
|
|
304
|
-
<Minus className="size-3.5" />
|
|
305
|
-
</IconButton>
|
|
306
|
-
<button
|
|
307
|
-
type="button"
|
|
308
|
-
onClick={actualSize}
|
|
309
|
-
title="Actual size (100%)"
|
|
310
|
-
className="w-11 rounded text-center font-mono text-[11px] tabular-nums transition-colors hover:bg-accent"
|
|
311
|
-
>
|
|
312
|
-
{Math.round(scale * 100)}%
|
|
313
|
-
</button>
|
|
314
|
-
<IconButton label="Zoom in" onClick={() => zoom(0.1)}>
|
|
315
|
-
<Plus className="size-3.5" />
|
|
316
|
-
</IconButton>
|
|
317
|
-
<IconButton
|
|
318
|
-
label="Fit width"
|
|
319
|
-
onClick={() => fitTo('fit-width')}
|
|
320
|
-
active={manualScale === null && zoomMode === 'fit-width'}
|
|
321
|
-
>
|
|
322
|
-
<MoveHorizontal className="size-3.5" />
|
|
323
|
-
</IconButton>
|
|
324
408
|
<IconButton
|
|
325
|
-
label=
|
|
326
|
-
onClick={
|
|
327
|
-
active={manualScale === null && zoomMode === 'fit-page'}
|
|
409
|
+
label={isFullscreen ? 'Exit fullscreen (F)' : 'Fullscreen (F)'}
|
|
410
|
+
onClick={toggleFullscreen}
|
|
328
411
|
>
|
|
329
|
-
<
|
|
412
|
+
{isFullscreen ? <Minimize className="size-4" /> : <Maximize className="size-4" />}
|
|
330
413
|
</IconButton>
|
|
331
|
-
</div>
|
|
332
414
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
{isFullscreen ? <Minimize className="size-4" /> : <Maximize className="size-4" />}
|
|
338
|
-
</IconButton>
|
|
415
|
+
{/* The document browser normally carries this. A viewer mounted with
|
|
416
|
+
`showDocBrowser: false` never shows that shell, and without it a
|
|
417
|
+
reader has no way to change the theme at all. */}
|
|
418
|
+
{!appConfig.build.showDocBrowser && <ThemeToggle />}
|
|
339
419
|
|
|
340
|
-
|
|
341
|
-
<button
|
|
342
|
-
type="button"
|
|
343
|
-
onClick={() => setInspecting((on) => !on)}
|
|
344
|
-
title="Inspect and edit on the page"
|
|
345
|
-
className={cn(
|
|
346
|
-
'flex items-center gap-1.5 rounded-md border border-border px-2.5 py-1.5 text-xs transition-colors hover:bg-accent',
|
|
347
|
-
inspecting && 'border-transparent bg-[#3b82f6] text-white hover:bg-[#3b82f6]',
|
|
348
|
-
)}
|
|
349
|
-
>
|
|
350
|
-
<MousePointerClick className="size-3.5" />
|
|
351
|
-
Inspect
|
|
352
|
-
</button>
|
|
353
|
-
)}
|
|
354
|
-
{import.meta.env.DEV && (
|
|
355
|
-
<button
|
|
356
|
-
type="button"
|
|
357
|
-
onClick={() => setDesignOpen((open) => !open)}
|
|
358
|
-
className={cn(
|
|
359
|
-
'flex items-center gap-1.5 rounded-md border border-border px-2.5 py-1.5 text-xs transition-colors hover:bg-accent',
|
|
360
|
-
designOpen && 'bg-accent',
|
|
361
|
-
)}
|
|
362
|
-
>
|
|
363
|
-
<Palette className="size-3.5" />
|
|
364
|
-
Design
|
|
365
|
-
</button>
|
|
366
|
-
)}
|
|
367
|
-
<Menu
|
|
368
|
-
trigger={(props) => (
|
|
420
|
+
{import.meta.env.DEV && (
|
|
369
421
|
<button
|
|
370
422
|
type="button"
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
{
|
|
423
|
+
onClick={() => setInspecting((on) => !on)}
|
|
424
|
+
title="Inspect and edit on the page"
|
|
425
|
+
className={cn(
|
|
426
|
+
'flex items-center gap-1.5 rounded-md border border-border px-2.5 py-1.5 text-xs transition-colors hover:bg-accent',
|
|
427
|
+
inspecting && 'border-transparent bg-[#3b82f6] text-white hover:bg-[#3b82f6]',
|
|
428
|
+
)}
|
|
374
429
|
>
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
430
|
+
<MousePointerClick className="size-3.5" />
|
|
431
|
+
Inspect
|
|
432
|
+
</button>
|
|
433
|
+
)}
|
|
434
|
+
{import.meta.env.DEV && (
|
|
435
|
+
<button
|
|
436
|
+
type="button"
|
|
437
|
+
onClick={() => setDesignOpen((open) => !open)}
|
|
438
|
+
className={cn(
|
|
439
|
+
'flex items-center gap-1.5 rounded-md border border-border px-2.5 py-1.5 text-xs transition-colors hover:bg-accent',
|
|
440
|
+
designOpen && 'bg-accent',
|
|
381
441
|
)}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
442
|
+
>
|
|
443
|
+
<Palette className="size-3.5" />
|
|
444
|
+
Design
|
|
385
445
|
</button>
|
|
386
446
|
)}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
void runDownload(format);
|
|
395
|
-
}}
|
|
447
|
+
<Menu
|
|
448
|
+
trigger={(props) => (
|
|
449
|
+
<button
|
|
450
|
+
type="button"
|
|
451
|
+
disabled={download !== null}
|
|
452
|
+
className="flex items-center gap-1.5 rounded-md bg-primary px-2.5 py-1.5 text-primary-foreground text-xs transition-opacity hover:opacity-90 disabled:opacity-70 aria-expanded:opacity-90"
|
|
453
|
+
{...props}
|
|
396
454
|
>
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
<
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
455
|
+
{download ? (
|
|
456
|
+
<Loader2 className="size-3.5 animate-spin" />
|
|
457
|
+
) : downloaded ? (
|
|
458
|
+
<Check className="size-3.5" />
|
|
459
|
+
) : (
|
|
460
|
+
<Download className="size-3.5" />
|
|
461
|
+
)}
|
|
462
|
+
{download
|
|
463
|
+
? `${DOWNLOAD_LABEL[download.format]} ${Math.round(download.percent)}%`
|
|
464
|
+
: 'Download'}
|
|
465
|
+
</button>
|
|
466
|
+
)}
|
|
467
|
+
>
|
|
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
|
+
)}
|
|
499
|
+
</Menu>
|
|
500
|
+
</div>
|
|
406
501
|
</header>
|
|
407
502
|
|
|
408
503
|
<div className="flex min-h-0 flex-1">
|
|
@@ -455,6 +550,144 @@ export function Doc() {
|
|
|
455
550
|
return <DesignProvider docId={docId}>{view}</DesignProvider>;
|
|
456
551
|
}
|
|
457
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
|
+
|
|
458
691
|
function IconButton({
|
|
459
692
|
label,
|
|
460
693
|
onClick,
|
|
@@ -492,9 +725,14 @@ function Centered({ children }: { children: React.ReactNode }) {
|
|
|
492
725
|
}
|
|
493
726
|
|
|
494
727
|
function BackLink() {
|
|
495
|
-
|
|
496
|
-
|
|
728
|
+
const className = 'mt-4 inline-block text-muted-foreground text-xs underline';
|
|
729
|
+
return appConfig.home === undefined ? (
|
|
730
|
+
<Link to="/" className={className}>
|
|
497
731
|
Back to documents
|
|
498
732
|
</Link>
|
|
733
|
+
) : (
|
|
734
|
+
<a href={appConfig.home} className={className}>
|
|
735
|
+
Back to workspace
|
|
736
|
+
</a>
|
|
499
737
|
);
|
|
500
738
|
}
|
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
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { a as defaultDesign, n as DiagramSyntaxError, r as parseDelimited, t as compileDiagram } from "./diagram-xlVDekYk.js";
|
|
2
2
|
import chalk from "chalk";
|
|
3
|
-
import fs from "node:fs/promises";
|
|
4
3
|
import path from "node:path";
|
|
5
4
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
5
|
+
import fs from "node:fs/promises";
|
|
6
6
|
import { existsSync, readFileSync } from "node:fs";
|
|
7
7
|
import fg from "fast-glob";
|
|
8
8
|
import { loadConfigFromFile, normalizePath } from "vite";
|