@open-document/core 0.1.0 → 0.3.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/dist/{build-BWZJ1gvL.js → build-C3pMGxJN.js} +2 -6
- package/dist/check-Dtwb1wvU.js +42 -0
- package/dist/cli/bin.d.ts +1 -1
- package/dist/cli/bin.js +124 -13
- package/dist/{config-EBFfKtWH.d.ts → config-CSC5lHsR.d.ts} +3 -2
- package/dist/config-Dg1AMP0r.js +2461 -0
- package/dist/context-FoX0afRf.js +26 -0
- package/dist/{dev-CZtw1oqs.js → dev-Cdd7NZRQ.js} +2 -6
- package/dist/diagram-xlVDekYk.js +763 -0
- package/dist/export-ClJ4T1uU.js +36 -0
- package/dist/import-D7Psi8WK.js +25 -0
- package/dist/index.d.ts +238 -117
- package/dist/index.js +516 -57
- package/dist/ops/index.d.ts +98 -8
- package/dist/ops/index.js +3 -202
- package/dist/ops-Ci5Xw6QG.js +1270 -0
- package/dist/{preview-DUfQl7Ij.js → preview-BhbCiAKC.js} +2 -6
- package/dist/sdk-CWMsH9Ms.d.ts +123 -0
- package/dist/vite/index.d.ts +9 -4
- package/dist/vite/index.js +2 -5
- package/env.d.ts +33 -1
- package/package.json +17 -6
- package/skills/create-doc/SKILL.md +6 -2
- package/skills/current-doc/SKILL.md +120 -0
- package/skills/doc-authoring/SKILL.md +79 -0
- package/skills/doc-authoring/references/long-form.md +131 -0
- 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/inspector/inspector.tsx +16 -0
- package/src/app/components/numbering.tsx +208 -0
- 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.ts +3 -0
- package/src/app/lib/use-doc-pages.ts +50 -15
- package/src/app/routes/doc.tsx +25 -5
- package/dist/config-DBp4mOOf.js +0 -1121
- package/dist/design-BJLvOcCO.js +0 -53
- package/dist/edit-ops-DSdUroVO.js +0 -1225
- package/dist/sync-6GTPAt2t.js +0 -86
- package/dist/sync-C6CsPmyK.js +0 -3
|
@@ -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.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
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
|
export type PageSizeName = 'A4' | 'Letter' | 'A5' | 'Legal';
|
|
5
6
|
|
|
@@ -54,6 +55,8 @@ export type DocMeta = {
|
|
|
54
55
|
theme?: string;
|
|
55
56
|
/** ISO 8601 timestamp. Set once at scaffold time; used to sort the doc list. */
|
|
56
57
|
createdAt?: string;
|
|
58
|
+
/** What numbered items are called — `圖`/`表` instead of `Figure`/`Table`. */
|
|
59
|
+
labels?: Partial<LabelVocabulary>;
|
|
57
60
|
};
|
|
58
61
|
|
|
59
62
|
export type FolderIcon = { type: 'emoji'; value: string } | { type: 'color'; value: string };
|
|
@@ -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
|
}
|
package/src/app/routes/doc.tsx
CHANGED
|
@@ -22,10 +22,12 @@ import { DocSidebar } from '../components/doc-sidebar';
|
|
|
22
22
|
import { Inspector } from '../components/inspector/inspector';
|
|
23
23
|
import { PageFrame } from '../components/page-frame';
|
|
24
24
|
import { Menu, MenuItem } from '../components/ui/menu';
|
|
25
|
+
import { useAgentBridge } from '../lib/agent-bridge';
|
|
25
26
|
import { exportDocAsHtml } from '../lib/export-html';
|
|
26
27
|
import { exportDocAsPdf } from '../lib/export-pdf';
|
|
27
|
-
import {
|
|
28
|
+
import { type OutlineEntry, useDocOutline } from '../lib/outline';
|
|
28
29
|
import { nextFrame, waitForFonts } from '../lib/print-ready';
|
|
30
|
+
import { scanDocument } from '../lib/scan';
|
|
29
31
|
import { resolvePageGeometry } from '../lib/sdk';
|
|
30
32
|
import { useDocModule } from '../lib/use-doc-module';
|
|
31
33
|
import { useDocPages } from '../lib/use-doc-pages';
|
|
@@ -71,9 +73,11 @@ export function Doc() {
|
|
|
71
73
|
const [isFullscreen, setIsFullscreen] = useState(false);
|
|
72
74
|
|
|
73
75
|
const geometry = useMemo(() => resolvePageGeometry(doc?.meta), [doc?.meta]);
|
|
74
|
-
const { pages, measuring } = useDocPages(doc, geometry);
|
|
76
|
+
const { pages, measuring, overflowing } = useDocPages(doc, geometry);
|
|
75
77
|
const outline = useDocOutline();
|
|
76
78
|
|
|
79
|
+
useAgentBridge({ docId: docId ?? '', doc, pages, geometry, measuring, oversized: overflowing });
|
|
80
|
+
|
|
77
81
|
const clamp = (value: number) => Math.max(MIN_SCALE, Math.min(MAX_SCALE, value));
|
|
78
82
|
const fitWidthScale = available.width ? clamp(available.width / geometry.width) : 1;
|
|
79
83
|
// Fit page is bounded by both axes so the whole sheet lands inside the pane.
|
|
@@ -108,7 +112,6 @@ export function Doc() {
|
|
|
108
112
|
// Headings only exist once the pages are in the DOM at their final metrics,
|
|
109
113
|
// so the scan waits for fonts — a late-loading face reflows headings and
|
|
110
114
|
// would otherwise strand the outline on stale text.
|
|
111
|
-
// biome-ignore lint/correctness/useExhaustiveDependencies: `doc` re-scans on hot reload, when the module identity changes but the status stays 'ready'.
|
|
112
115
|
useEffect(() => {
|
|
113
116
|
if (state.status !== 'ready' || measuring) return;
|
|
114
117
|
let cancelled = false;
|
|
@@ -118,7 +121,7 @@ export function Doc() {
|
|
|
118
121
|
await nextFrame();
|
|
119
122
|
const root = pagesRef.current;
|
|
120
123
|
if (cancelled || !root) return;
|
|
121
|
-
|
|
124
|
+
scanDocument(root, doc?.meta);
|
|
122
125
|
})();
|
|
123
126
|
return () => {
|
|
124
127
|
cancelled = true;
|
|
@@ -230,6 +233,19 @@ export function Doc() {
|
|
|
230
233
|
return () => document.removeEventListener('fullscreenchange', onChange);
|
|
231
234
|
}, []);
|
|
232
235
|
|
|
236
|
+
// Tell the dev server where the reader is, so an agent can resolve "this
|
|
237
|
+
// page" from node_modules/.open-doc/current.json. See vite/current-plugin.ts.
|
|
238
|
+
useEffect(() => {
|
|
239
|
+
if (!import.meta.hot) return;
|
|
240
|
+
if (!docId || !doc || pages.length === 0) return;
|
|
241
|
+
import.meta.hot.send('open-doc:current', {
|
|
242
|
+
docId,
|
|
243
|
+
pageIndex: currentPage - 1,
|
|
244
|
+
totalPages: pages.length,
|
|
245
|
+
docTitle: doc.meta?.title ?? docId,
|
|
246
|
+
});
|
|
247
|
+
}, [docId, doc, currentPage, pages.length]);
|
|
248
|
+
|
|
233
249
|
useEffect(() => {
|
|
234
250
|
const onKeyDown = (e: KeyboardEvent) => {
|
|
235
251
|
if (e.metaKey || e.ctrlKey || e.altKey) return;
|
|
@@ -401,7 +417,11 @@ export function Doc() {
|
|
|
401
417
|
onSelectPage={scrollToPage}
|
|
402
418
|
onSelectEntry={scrollToEntry}
|
|
403
419
|
/>
|
|
404
|
-
<div
|
|
420
|
+
<div
|
|
421
|
+
ref={scrollRef}
|
|
422
|
+
data-od-viewer
|
|
423
|
+
className="relative min-w-0 flex-1 overflow-auto bg-canvas"
|
|
424
|
+
>
|
|
405
425
|
<div
|
|
406
426
|
ref={pagesRef}
|
|
407
427
|
className="flex flex-col items-center"
|