@open-document/core 0.2.0 → 0.4.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-U81g9Hnk.js → build-BzhYQKev.js} +1 -1
- package/dist/check-CCAaXofH.js +42 -0
- package/dist/cli/bin.js +45 -12
- package/dist/config-CNr8hd5X.js +2461 -0
- package/dist/{config-CSC5lHsR.d.ts → config-P61niZMf.d.ts} +6 -0
- package/dist/context-CJpdjYfU.js +15 -0
- package/dist/{dev-DwwFT6uX.js → dev-BhXgunme.js} +1 -1
- package/dist/diagram-xlVDekYk.js +763 -0
- package/dist/export-B69kDkyp.js +36 -0
- package/dist/import-DBhCpUsl.js +26 -0
- package/dist/index.d.ts +236 -91
- package/dist/index.js +516 -88
- package/dist/ops/index.d.ts +96 -1
- package/dist/ops/index.js +4 -199
- package/dist/ops-BCBmRMCS.js +1275 -0
- package/dist/{preview-C_jbySle.js → preview-Da4Aq9yF.js} +1 -1
- package/dist/sdk-C9nl30dW.d.ts +129 -0
- package/dist/sdk-DhcqZdAw.js +50 -0
- package/dist/vite/index.d.ts +7 -1
- package/dist/vite/index.js +1 -2
- package/env.d.ts +33 -1
- package/package.json +10 -1
- package/skills/create-doc/SKILL.md +7 -3
- package/skills/create-theme/SKILL.md +1 -1
- package/skills/doc-authoring/SKILL.md +86 -6
- package/skills/doc-authoring/references/long-form.md +131 -0
- package/skills/doc-authoring/references/pagination.md +2 -1
- package/src/app/components/data-table.tsx +196 -0
- package/src/app/components/diagram.tsx +76 -0
- package/src/app/components/flow-page.tsx +20 -3
- package/src/app/components/footnote.tsx +196 -0
- package/src/app/components/numbering.tsx +208 -0
- package/src/app/components/themes/theme-preview.tsx +2 -2
- package/src/app/lib/agent-bridge.ts +128 -0
- package/src/app/lib/diagnostics.ts +282 -0
- package/src/app/lib/export-html.ts +35 -11
- package/src/app/lib/export-pdf.ts +54 -19
- package/src/app/lib/flow-measure.ts +100 -31
- package/src/app/lib/flow.test.ts +46 -0
- package/src/app/lib/flow.ts +28 -3
- package/src/app/lib/footnotes.test.tsx +102 -0
- package/src/app/lib/footnotes.ts +94 -0
- package/src/app/lib/labels.ts +175 -0
- package/src/app/lib/page-context.tsx +6 -1
- package/src/app/lib/scan.ts +26 -0
- package/src/app/lib/sdk.test.ts +15 -9
- package/src/app/lib/sdk.ts +37 -11
- package/src/app/lib/use-doc-pages.ts +50 -15
- package/src/app/routes/doc.tsx +158 -115
- package/dist/config-8zkUElGq.js +0 -1169
- package/dist/design-IyRSwCtK.js +0 -52
- package/dist/edit-ops-D2W2plpc.js +0 -1190
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Children, cloneElement, createElement, isValidElement, type ReactNode } from 'react';
|
|
2
|
+
import { FOOTNOTE_MARKER_FLAG, FootnoteMarker } from '../components/footnote';
|
|
3
|
+
|
|
4
|
+
export type ExtractedNote = { id: string; content: ReactNode };
|
|
5
|
+
|
|
6
|
+
export type ExtractedBlock = {
|
|
7
|
+
/** The block with each `<Footnote>` swapped for its marker. */
|
|
8
|
+
node: ReactNode;
|
|
9
|
+
notes: ExtractedNote[];
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/** Identified by a flag on the function, not by identity — core exists twice at runtime. */
|
|
13
|
+
function isFootnoteElement(node: ReactNode): boolean {
|
|
14
|
+
if (!isValidElement(node)) return false;
|
|
15
|
+
const type = node.type as unknown;
|
|
16
|
+
if (typeof type !== 'function') return false;
|
|
17
|
+
return (type as unknown as Record<string, unknown>)[FOOTNOTE_MARKER_FLAG] === true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Lifts the notes out of a block and leaves the markers behind.
|
|
22
|
+
*
|
|
23
|
+
* A footnote occupies space at the foot of its page, which is space the packer
|
|
24
|
+
* cannot give to body content — so the notes have to be known *before*
|
|
25
|
+
* pagination, not discovered while rendering it. Walking the block's element
|
|
26
|
+
* tree is what makes that possible, and the trade is that a `<Footnote>` has to
|
|
27
|
+
* appear in the JSX you hand to `flow()`: one hidden inside a component's own
|
|
28
|
+
* body is invisible here and stays inline.
|
|
29
|
+
*/
|
|
30
|
+
export function extractBlockFootnotes(node: ReactNode, idPrefix: string): ExtractedBlock {
|
|
31
|
+
const notes: ExtractedNote[] = [];
|
|
32
|
+
|
|
33
|
+
const walkChildren = (children: ReactNode): ReactNode => {
|
|
34
|
+
if (Array.isArray(children)) {
|
|
35
|
+
let changed = false;
|
|
36
|
+
const mapped = Children.map(children, (child) => {
|
|
37
|
+
const next = walk(child);
|
|
38
|
+
if (next !== child) changed = true;
|
|
39
|
+
return next;
|
|
40
|
+
});
|
|
41
|
+
return changed ? mapped : children;
|
|
42
|
+
}
|
|
43
|
+
return walk(children);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const walk = (current: ReactNode): ReactNode => {
|
|
47
|
+
if (!isValidElement(current)) return current;
|
|
48
|
+
|
|
49
|
+
if (isFootnoteElement(current)) {
|
|
50
|
+
const props = current.props as { id?: string; children?: ReactNode };
|
|
51
|
+
const id = props.id ?? `${idPrefix}-${notes.length + 1}`;
|
|
52
|
+
notes.push({ id, content: props.children });
|
|
53
|
+
return createElement(FootnoteMarker, { key: current.key ?? id, id });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const props = current.props as { children?: ReactNode };
|
|
57
|
+
// A render-prop child is a function, not a tree — there is nothing to walk.
|
|
58
|
+
if (props.children === undefined || typeof props.children === 'function') return current;
|
|
59
|
+
|
|
60
|
+
const next = walkChildren(props.children);
|
|
61
|
+
return next === props.children ? current : cloneElement(current, undefined, next);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const walked = walk(node);
|
|
65
|
+
return { node: walked, notes };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type PreparedSection = {
|
|
69
|
+
blocks: ReactNode[];
|
|
70
|
+
/** Notes per block, index-aligned with `blocks`. */
|
|
71
|
+
notesByBlock: ExtractedNote[][];
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export function extractSectionFootnotes(
|
|
75
|
+
blocks: ReactNode[],
|
|
76
|
+
sectionIndex: number,
|
|
77
|
+
): PreparedSection {
|
|
78
|
+
const out: PreparedSection = { blocks: [], notesByBlock: [] };
|
|
79
|
+
blocks.forEach((block, blockIndex) => {
|
|
80
|
+
const extracted = extractBlockFootnotes(block, `fn-${sectionIndex}-${blockIndex}`);
|
|
81
|
+
out.blocks.push(extracted.node);
|
|
82
|
+
out.notesByBlock.push(extracted.notes);
|
|
83
|
+
});
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function notesForPage(
|
|
88
|
+
notesByBlock: ExtractedNote[][],
|
|
89
|
+
blockIndices: number[],
|
|
90
|
+
): ExtractedNote[] {
|
|
91
|
+
const out: ExtractedNote[] = [];
|
|
92
|
+
for (const index of blockIndices) out.push(...(notesByBlock[index] ?? []));
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { useSyncExternalStore } from 'react';
|
|
2
|
+
import { PAGE_ATTR, PAGE_INDEX_ATTR } from './outline';
|
|
3
|
+
|
|
4
|
+
export type LabelKind = 'figure' | 'table' | 'footnote';
|
|
5
|
+
|
|
6
|
+
export type LabelEntry = {
|
|
7
|
+
/** Author-supplied id for `<Ref to>`, or a generated one. */
|
|
8
|
+
id: string;
|
|
9
|
+
kind: LabelKind;
|
|
10
|
+
/** 1-based, counted per kind across the whole document. */
|
|
11
|
+
number: number;
|
|
12
|
+
/** Caption text, for a list of figures. */
|
|
13
|
+
text: string;
|
|
14
|
+
/** 1-based page the item sits on. */
|
|
15
|
+
page: number;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const LABEL_ATTR = 'data-od-label';
|
|
19
|
+
export const LABEL_ID_ATTR = 'data-od-label-id';
|
|
20
|
+
export const LABEL_TEXT_ATTR = 'data-od-label-text';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The words wrapped around a number. Numbering is structural; what it is
|
|
24
|
+
* *called* is a document's own business — `圖 3` and `Figure 3` are the same
|
|
25
|
+
* entry. Set it once in `meta.labels`.
|
|
26
|
+
*/
|
|
27
|
+
export type LabelVocabulary = {
|
|
28
|
+
figure: string;
|
|
29
|
+
table: string;
|
|
30
|
+
/** Heading above a page's footnotes. Empty string renders no heading. */
|
|
31
|
+
footnotes: string;
|
|
32
|
+
/** Page suffix for a cross-reference. `{page}` is replaced. */
|
|
33
|
+
onPage: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const defaultVocabulary: LabelVocabulary = {
|
|
37
|
+
figure: 'Figure',
|
|
38
|
+
table: 'Table',
|
|
39
|
+
footnotes: '',
|
|
40
|
+
onPage: ' (p. {page})',
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type LabelSnapshot = {
|
|
44
|
+
entries: LabelEntry[];
|
|
45
|
+
vocabulary: LabelVocabulary;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const EMPTY: LabelSnapshot = { entries: [], vocabulary: defaultVocabulary };
|
|
49
|
+
|
|
50
|
+
const KINDS: LabelKind[] = ['figure', 'table', 'footnote'];
|
|
51
|
+
|
|
52
|
+
function isKind(value: string | null): value is LabelKind {
|
|
53
|
+
return value !== null && (KINDS as string[]).includes(value);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Walks rendered page frames and numbers everything labelled, in document
|
|
58
|
+
* order. Numbering is a scan for the same reason the outline is: only the
|
|
59
|
+
* rendered pages know what ended up where, and a flow section's page breaks are
|
|
60
|
+
* decided by measurement, not by the author.
|
|
61
|
+
*/
|
|
62
|
+
export function collectLabels(root: ParentNode): LabelEntry[] {
|
|
63
|
+
const entries: LabelEntry[] = [];
|
|
64
|
+
const counters: Record<LabelKind, number> = { figure: 0, table: 0, footnote: 0 };
|
|
65
|
+
const frames = Array.from(root.querySelectorAll<HTMLElement>(`[${PAGE_ATTR}]`));
|
|
66
|
+
|
|
67
|
+
frames.forEach((frame, fallbackIndex) => {
|
|
68
|
+
const declared = Number(frame.getAttribute(PAGE_INDEX_ATTR));
|
|
69
|
+
const page = (Number.isFinite(declared) ? declared : fallbackIndex) + 1;
|
|
70
|
+
|
|
71
|
+
for (const el of Array.from(frame.querySelectorAll<HTMLElement>(`[${LABEL_ATTR}]`))) {
|
|
72
|
+
const kind = el.getAttribute(LABEL_ATTR);
|
|
73
|
+
if (!isKind(kind)) continue;
|
|
74
|
+
const id = el.getAttribute(LABEL_ID_ATTR);
|
|
75
|
+
if (!id) continue;
|
|
76
|
+
// A footnote's marker and the note at the foot of the page carry the same
|
|
77
|
+
// id; the marker is what fixes its position in the sequence.
|
|
78
|
+
if (entries.some((entry) => entry.id === id)) continue;
|
|
79
|
+
counters[kind] += 1;
|
|
80
|
+
entries.push({
|
|
81
|
+
id,
|
|
82
|
+
kind,
|
|
83
|
+
number: counters[kind],
|
|
84
|
+
text: (el.getAttribute(LABEL_TEXT_ATTR) ?? el.textContent ?? '')
|
|
85
|
+
.replace(/\s+/g, ' ')
|
|
86
|
+
.trim(),
|
|
87
|
+
page,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return entries;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Shared through globalThis for the same reason as the outline store: the
|
|
96
|
+
// viewer writes from the source copy of this module while a document's
|
|
97
|
+
// `<Figure>` reads from the published bundle.
|
|
98
|
+
const GLOBAL_KEY = '__open_doc_labels_store__';
|
|
99
|
+
type LabelStore = { snapshot: LabelSnapshot; listeners: Set<() => void> };
|
|
100
|
+
type GlobalWithStore = typeof globalThis & { [GLOBAL_KEY]?: LabelStore };
|
|
101
|
+
const g = globalThis as GlobalWithStore;
|
|
102
|
+
if (!g[GLOBAL_KEY]) {
|
|
103
|
+
g[GLOBAL_KEY] = { snapshot: EMPTY, listeners: new Set() };
|
|
104
|
+
}
|
|
105
|
+
const store = g[GLOBAL_KEY];
|
|
106
|
+
|
|
107
|
+
function sameEntries(a: LabelEntry[], b: LabelEntry[]): boolean {
|
|
108
|
+
if (a.length !== b.length) return false;
|
|
109
|
+
return a.every((entry, i) => {
|
|
110
|
+
const other = b[i];
|
|
111
|
+
return (
|
|
112
|
+
other !== undefined &&
|
|
113
|
+
entry.id === other.id &&
|
|
114
|
+
entry.kind === other.kind &&
|
|
115
|
+
entry.number === other.number &&
|
|
116
|
+
entry.text === other.text &&
|
|
117
|
+
entry.page === other.page
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function sameVocabulary(a: LabelVocabulary, b: LabelVocabulary): boolean {
|
|
123
|
+
return (
|
|
124
|
+
a.figure === b.figure &&
|
|
125
|
+
a.table === b.table &&
|
|
126
|
+
a.footnotes === b.footnotes &&
|
|
127
|
+
a.onPage === b.onPage
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function setLabels(entries: LabelEntry[], vocabulary?: Partial<LabelVocabulary>): void {
|
|
132
|
+
const next: LabelSnapshot = {
|
|
133
|
+
entries,
|
|
134
|
+
vocabulary: { ...defaultVocabulary, ...vocabulary },
|
|
135
|
+
};
|
|
136
|
+
if (
|
|
137
|
+
sameEntries(store.snapshot.entries, next.entries) &&
|
|
138
|
+
sameVocabulary(store.snapshot.vocabulary, next.vocabulary)
|
|
139
|
+
) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
store.snapshot = next;
|
|
143
|
+
for (const listener of store.listeners) listener();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function getLabels(): LabelSnapshot {
|
|
147
|
+
return store.snapshot;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function subscribe(listener: () => void): () => void {
|
|
151
|
+
store.listeners.add(listener);
|
|
152
|
+
return () => store.listeners.delete(listener);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function useLabelSnapshot(): LabelSnapshot {
|
|
156
|
+
return useSyncExternalStore(subscribe, getLabels, getLabels);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The document's numbered items of one kind, in order. Empty on the first render pass. */
|
|
160
|
+
export function useDocLabels(kind: LabelKind): LabelEntry[] {
|
|
161
|
+
return useLabelSnapshot().entries.filter((entry) => entry.kind === kind);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** One numbered item by id, once the scan has run. */
|
|
165
|
+
export function useDocLabel(id: string): LabelEntry | null {
|
|
166
|
+
return useLabelSnapshot().entries.find((entry) => entry.id === id) ?? null;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function useLabelVocabulary(): LabelVocabulary {
|
|
170
|
+
return useLabelSnapshot().vocabulary;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function formatOnPage(vocabulary: LabelVocabulary, page: number): string {
|
|
174
|
+
return vocabulary.onPage.replace('{page}', String(page));
|
|
175
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type Context, createContext, type ReactNode, useContext, useMemo } from 'react';
|
|
2
|
+
import { FootnoteCollector } from '../components/footnote';
|
|
2
3
|
|
|
3
4
|
type PageContextValue = { index: number; total: number };
|
|
4
5
|
|
|
@@ -22,7 +23,11 @@ export function DocPageProvider({
|
|
|
22
23
|
children,
|
|
23
24
|
}: PageContextValue & { children?: ReactNode }) {
|
|
24
25
|
const value = useMemo(() => ({ index, total }), [index, total]);
|
|
25
|
-
return
|
|
26
|
+
return (
|
|
27
|
+
<PageContext.Provider value={value}>
|
|
28
|
+
<FootnoteCollector>{children}</FootnoteCollector>
|
|
29
|
+
</PageContext.Provider>
|
|
30
|
+
);
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
/** 1-based page number of the page currently rendering. `0` outside a page. */
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { collectLabels, getLabels, type LabelSnapshot, setLabels } from './labels';
|
|
2
|
+
import { collectOutline, getOutline, type OutlineEntry, setOutline } from './outline';
|
|
3
|
+
import type { DocMeta } from './sdk';
|
|
4
|
+
|
|
5
|
+
export type ScanSnapshot = { outline: OutlineEntry[]; labels: LabelSnapshot };
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Reads everything the rendered pages know that the source does not: which
|
|
9
|
+
* headings exist and where they landed, and what number each figure, table, and
|
|
10
|
+
* footnote ended up with. One call, because the two scans must always describe
|
|
11
|
+
* the same copy of the document — the viewer's pages, or an exporter's private
|
|
12
|
+
* one.
|
|
13
|
+
*/
|
|
14
|
+
export function scanDocument(root: ParentNode, meta?: DocMeta): void {
|
|
15
|
+
setOutline(collectOutline(root));
|
|
16
|
+
setLabels(collectLabels(root), meta?.labels);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function captureScan(): ScanSnapshot {
|
|
20
|
+
return { outline: getOutline(), labels: getLabels() };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function restoreScan(snapshot: ScanSnapshot): void {
|
|
24
|
+
setOutline(snapshot.outline);
|
|
25
|
+
setLabels(snapshot.labels.entries, snapshot.labels.vocabulary);
|
|
26
|
+
}
|
package/src/app/lib/sdk.test.ts
CHANGED
|
@@ -1,24 +1,30 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { PAGE_SIZES, resolvePageGeometry } from './sdk.ts';
|
|
2
|
+
import { PAGE_SIZE_NAMES, PAGE_SIZES, resolvePageGeometry } from './sdk.ts';
|
|
3
3
|
|
|
4
4
|
describe('resolvePageGeometry', () => {
|
|
5
5
|
it('defaults to portrait A4', () => {
|
|
6
6
|
expect(resolvePageGeometry()).toEqual({
|
|
7
7
|
width: PAGE_SIZES.A4.width,
|
|
8
8
|
height: PAGE_SIZES.A4.height,
|
|
9
|
-
css:
|
|
9
|
+
css: '210mm 297mm',
|
|
10
10
|
});
|
|
11
11
|
});
|
|
12
12
|
|
|
13
|
-
it('
|
|
14
|
-
|
|
15
|
-
expect(
|
|
16
|
-
expect(geo.height).toBe(PAGE_SIZES.Letter.width);
|
|
17
|
-
expect(geo.css).toBe(`${PAGE_SIZES.Letter.css} landscape`);
|
|
13
|
+
it('offers A4, B4 and A3 and nothing else', () => {
|
|
14
|
+
expect([...PAGE_SIZE_NAMES]).toEqual(['A4', 'B4', 'A3']);
|
|
15
|
+
expect(Object.keys(PAGE_SIZES)).toEqual([...PAGE_SIZE_NAMES]);
|
|
18
16
|
});
|
|
19
17
|
|
|
20
|
-
it('
|
|
21
|
-
const geo = resolvePageGeometry({ pageSize: 'B4'
|
|
18
|
+
it('swaps both the pixel axes and the @page descriptor for landscape', () => {
|
|
19
|
+
const geo = resolvePageGeometry({ pageSize: 'B4', orientation: 'landscape' });
|
|
20
|
+
expect(geo.width).toBe(PAGE_SIZES.B4.height);
|
|
21
|
+
expect(geo.height).toBe(PAGE_SIZES.B4.width);
|
|
22
|
+
// Chromium drops `<mm> <mm> landscape` entirely — the axes must be swapped.
|
|
23
|
+
expect(geo.css).toBe('364mm 257mm');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('falls back to A4 for a page size that is no longer offered', () => {
|
|
27
|
+
const geo = resolvePageGeometry({ pageSize: 'Letter' as never });
|
|
22
28
|
expect(geo.width).toBe(PAGE_SIZES.A4.width);
|
|
23
29
|
});
|
|
24
30
|
});
|
package/src/app/lib/sdk.ts
CHANGED
|
@@ -1,24 +1,44 @@
|
|
|
1
1
|
import type { ComponentType, ReactNode } from 'react';
|
|
2
2
|
import type { DesignSystem } from './design.ts';
|
|
3
|
+
import type { LabelVocabulary } from './labels.ts';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
/** The only sheets a document may be laid out on. */
|
|
6
|
+
export const PAGE_SIZE_NAMES = ['A4', 'B4', 'A3'] as const;
|
|
5
7
|
|
|
6
|
-
export type
|
|
8
|
+
export type PageSizeName = (typeof PAGE_SIZE_NAMES)[number];
|
|
9
|
+
|
|
10
|
+
export const ORIENTATIONS = ['portrait', 'landscape'] as const;
|
|
11
|
+
|
|
12
|
+
export type Orientation = (typeof ORIENTATIONS)[number];
|
|
7
13
|
|
|
8
14
|
/**
|
|
9
|
-
*
|
|
10
|
-
* with the physical
|
|
11
|
-
* laid out at 794×1123 on screen maps to a real A4 sheet with no rescaling.
|
|
15
|
+
* Portrait dimensions in CSS pixels at 96dpi — the unit authors write in —
|
|
16
|
+
* paired with the physical millimetres used when printing. Keeping both means a
|
|
17
|
+
* page laid out at 794×1123 on screen maps to a real A4 sheet with no rescaling.
|
|
12
18
|
*/
|
|
13
|
-
export const PAGE_SIZES: Record<
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
export const PAGE_SIZES: Record<
|
|
20
|
+
PageSizeName,
|
|
21
|
+
{ width: number; height: number; mm: readonly [number, number] }
|
|
22
|
+
> = {
|
|
23
|
+
A4: { width: 794, height: 1123, mm: [210, 297] },
|
|
24
|
+
// JIS B4, not the ISO B4 (250×353mm) that the CSS `size: B4` keyword means —
|
|
25
|
+
// which is why the descriptor is written in millimetres rather than by name.
|
|
26
|
+
B4: { width: 971, height: 1376, mm: [257, 364] },
|
|
27
|
+
A3: { width: 1123, height: 1587, mm: [297, 420] },
|
|
18
28
|
};
|
|
19
29
|
|
|
20
30
|
export const DEFAULT_PAGE_SIZE: PageSizeName = 'A4';
|
|
21
31
|
|
|
32
|
+
export const DEFAULT_ORIENTATION: Orientation = 'portrait';
|
|
33
|
+
|
|
34
|
+
export function isPageSizeName(value: unknown): value is PageSizeName {
|
|
35
|
+
return typeof value === 'string' && value in PAGE_SIZES;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function isOrientation(value: unknown): value is Orientation {
|
|
39
|
+
return value === 'portrait' || value === 'landscape';
|
|
40
|
+
}
|
|
41
|
+
|
|
22
42
|
export type PageGeometry = {
|
|
23
43
|
width: number;
|
|
24
44
|
height: number;
|
|
@@ -54,6 +74,8 @@ export type DocMeta = {
|
|
|
54
74
|
theme?: string;
|
|
55
75
|
/** ISO 8601 timestamp. Set once at scaffold time; used to sort the doc list. */
|
|
56
76
|
createdAt?: string;
|
|
77
|
+
/** What numbered items are called — `圖`/`表` instead of `Figure`/`Table`. */
|
|
78
|
+
labels?: Partial<LabelVocabulary>;
|
|
57
79
|
};
|
|
58
80
|
|
|
59
81
|
export type FolderIcon = { type: 'emoji'; value: string } | { type: 'color'; value: string };
|
|
@@ -78,9 +100,13 @@ export type DocModule = {
|
|
|
78
100
|
export function resolvePageGeometry(meta?: DocMeta): PageGeometry {
|
|
79
101
|
const size = PAGE_SIZES[meta?.pageSize ?? DEFAULT_PAGE_SIZE] ?? PAGE_SIZES[DEFAULT_PAGE_SIZE];
|
|
80
102
|
const landscape = meta?.orientation === 'landscape';
|
|
103
|
+
const [across, down] = landscape ? [size.mm[1], size.mm[0]] : size.mm;
|
|
81
104
|
return {
|
|
82
105
|
width: landscape ? size.height : size.width,
|
|
83
106
|
height: landscape ? size.width : size.height,
|
|
84
|
-
|
|
107
|
+
// Not `<mm> <mm> landscape`: the `landscape` keyword is only valid next to a
|
|
108
|
+
// page-size *name*, and Chromium drops the whole descriptor if it sees both,
|
|
109
|
+
// which silently prints a landscape sheet at the dialog's default size.
|
|
110
|
+
css: `${across}mm ${down}mm`,
|
|
85
111
|
};
|
|
86
112
|
}
|
|
@@ -2,7 +2,8 @@ import { createElement, type ReactNode, useEffect, useMemo, useState } from 'rea
|
|
|
2
2
|
import { FlowPage } from '../components/flow-page';
|
|
3
3
|
import type { DesignSystem } from './design';
|
|
4
4
|
import { type DocEntry, type FlowSection, isFlowSection, paginateBlocks } from './flow';
|
|
5
|
-
import { measureFlowSections } from './flow-measure';
|
|
5
|
+
import { type MeasurableSection, measureFlowSections } from './flow-measure';
|
|
6
|
+
import { extractSectionFootnotes, notesForPage, type PreparedSection } from './footnotes';
|
|
6
7
|
import type { DocModule, PageGeometry } from './sdk';
|
|
7
8
|
|
|
8
9
|
export type ExpandedPage = {
|
|
@@ -18,6 +19,11 @@ type Plan = {
|
|
|
18
19
|
|
|
19
20
|
const EMPTY_PLAN: Plan = { bySection: [], overflowing: [] };
|
|
20
21
|
|
|
22
|
+
/** A plan is only current for the sections it was measured from. */
|
|
23
|
+
type Measured = { plan: Plan; sections: FlowSection[] | null };
|
|
24
|
+
|
|
25
|
+
const NOT_MEASURED: Measured = { plan: EMPTY_PLAN, sections: null };
|
|
26
|
+
|
|
21
27
|
function entriesOf(doc: DocModule | null): DocEntry[] {
|
|
22
28
|
return (doc?.default ?? []) as DocEntry[];
|
|
23
29
|
}
|
|
@@ -35,39 +41,60 @@ export function useDocPages(
|
|
|
35
41
|
const sections = useMemo(() => entries.filter(isFlowSection), [entries]);
|
|
36
42
|
const design = doc?.design as DesignSystem | undefined;
|
|
37
43
|
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
// Footnotes come out of the blocks before anything is measured: what they
|
|
45
|
+
// cost at the foot of a page is part of that page's budget.
|
|
46
|
+
const prepared = useMemo<PreparedSection[]>(
|
|
47
|
+
() => sections.map((section, index) => extractSectionFootnotes(section.blocks, index)),
|
|
48
|
+
[sections],
|
|
49
|
+
);
|
|
50
|
+
const measurable = useMemo<MeasurableSection[]>(
|
|
51
|
+
() =>
|
|
52
|
+
sections.map((section, index) => ({
|
|
53
|
+
blocks: prepared[index].blocks,
|
|
54
|
+
notesByBlock: prepared[index].notesByBlock,
|
|
55
|
+
...(section.padding !== undefined ? { padding: section.padding } : {}),
|
|
56
|
+
})),
|
|
57
|
+
[sections, prepared],
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const [state, setState] = useState<Measured>(NOT_MEASURED);
|
|
61
|
+
// Derived, not stored: a `measuring` flag set from an effect stays false for
|
|
62
|
+
// one commit after the document loads, and anything reading the page list in
|
|
63
|
+
// that window — the outline scan, the headless bridge — sees a whole flow
|
|
64
|
+
// section as one unpaginated page.
|
|
65
|
+
const measuring = sections.length > 0 && state.sections !== sections;
|
|
40
66
|
|
|
41
67
|
useEffect(() => {
|
|
42
68
|
if (sections.length === 0) {
|
|
43
|
-
|
|
44
|
-
setMeasuring(false);
|
|
69
|
+
setState({ plan: EMPTY_PLAN, sections });
|
|
45
70
|
return;
|
|
46
71
|
}
|
|
47
72
|
let cancelled = false;
|
|
48
|
-
|
|
49
|
-
measureFlowSections(sections, { geometry, design })
|
|
73
|
+
measureFlowSections(measurable, { geometry, design })
|
|
50
74
|
.then((measurements) => {
|
|
51
75
|
if (cancelled) return;
|
|
52
76
|
const bySection: number[][][] = [];
|
|
53
77
|
const overflowing: Plan['overflowing'] = [];
|
|
54
78
|
measurements.forEach((measurement, sectionIndex) => {
|
|
55
|
-
const result = paginateBlocks(measurement.metrics, measurement.available
|
|
79
|
+
const result = paginateBlocks(measurement.metrics, measurement.available, {
|
|
80
|
+
footnoteOverhead: measurement.footnoteOverhead,
|
|
81
|
+
});
|
|
56
82
|
bySection.push(result.pages);
|
|
57
83
|
for (const block of result.overflowing) {
|
|
58
84
|
overflowing.push({ section: sectionIndex, block });
|
|
59
85
|
}
|
|
60
86
|
});
|
|
61
|
-
|
|
62
|
-
setMeasuring(false);
|
|
87
|
+
setState({ plan: { bySection, overflowing }, sections });
|
|
63
88
|
})
|
|
64
89
|
.catch(() => {
|
|
65
|
-
if (!cancelled)
|
|
90
|
+
if (!cancelled) setState({ plan: EMPTY_PLAN, sections });
|
|
66
91
|
});
|
|
67
92
|
return () => {
|
|
68
93
|
cancelled = true;
|
|
69
94
|
};
|
|
70
|
-
}, [sections, geometry, design]);
|
|
95
|
+
}, [sections, measurable, geometry, design]);
|
|
96
|
+
|
|
97
|
+
const plan = state.plan;
|
|
71
98
|
|
|
72
99
|
const pages = useMemo(() => {
|
|
73
100
|
const out: ExpandedPage[] = [];
|
|
@@ -81,19 +108,27 @@ export function useDocPages(
|
|
|
81
108
|
}
|
|
82
109
|
sectionIndex++;
|
|
83
110
|
const section = entry as FlowSection;
|
|
111
|
+
const ready = prepared[sectionIndex];
|
|
112
|
+
const blocks = ready?.blocks ?? section.blocks;
|
|
84
113
|
// Before measurement lands, render the section as a single page so the
|
|
85
114
|
// viewer shows something rather than flashing empty.
|
|
86
|
-
const chunks = plan.bySection[sectionIndex] ?? [
|
|
115
|
+
const chunks = plan.bySection[sectionIndex] ?? [blocks.map((_, i) => i)];
|
|
87
116
|
chunks.forEach((blockIndices, pageIndex) => {
|
|
88
117
|
out.push({
|
|
89
118
|
key: `f${entryIndex}-${pageIndex}`,
|
|
90
|
-
content: createElement(FlowPage, {
|
|
119
|
+
content: createElement(FlowPage, {
|
|
120
|
+
section,
|
|
121
|
+
design,
|
|
122
|
+
blockIndices,
|
|
123
|
+
blocks,
|
|
124
|
+
notes: ready ? notesForPage(ready.notesByBlock, blockIndices) : [],
|
|
125
|
+
}),
|
|
91
126
|
});
|
|
92
127
|
});
|
|
93
128
|
});
|
|
94
129
|
|
|
95
130
|
return out;
|
|
96
|
-
}, [entries, plan, design]);
|
|
131
|
+
}, [entries, plan, prepared, design]);
|
|
97
132
|
|
|
98
133
|
return { pages, measuring, overflowing: plan.overflowing };
|
|
99
134
|
}
|