@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,131 @@
|
|
|
1
|
+
# Footnotes, numbering, cross-references, and data
|
|
2
|
+
|
|
3
|
+
Everything here shares one mechanism: the framework **scans the rendered pages**
|
|
4
|
+
and fills in what only the finished layout knows. That is why none of these
|
|
5
|
+
numbers are written by hand, and why they are all correct after a page is
|
|
6
|
+
inserted in the middle of the document.
|
|
7
|
+
|
|
8
|
+
The trade is the same one `<TableOfContents>` makes: values are empty on the
|
|
9
|
+
first render pass and filled on the next. Both exporters scan their own copy
|
|
10
|
+
before serializing, so a PDF never contains a blank number.
|
|
11
|
+
|
|
12
|
+
## Footnotes
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { Footnote } from '@open-document/core';
|
|
16
|
+
|
|
17
|
+
<p style={p}>
|
|
18
|
+
Spend grew 8% quarter over quarter
|
|
19
|
+
<Footnote>Billing export, 2026-10-02. Excludes the edge tier.</Footnote>, driven
|
|
20
|
+
almost entirely by one service.
|
|
21
|
+
</p>
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
- The marker is numbered by position in the document — across fixed pages and
|
|
25
|
+
flow sections alike. Insert a note anywhere and everything after it renumbers.
|
|
26
|
+
- Inside a `flow()` section the note prints at the **foot of whatever page its
|
|
27
|
+
marker landed on**, and the space it takes is subtracted from that page's
|
|
28
|
+
budget before the packer decides where to break. You do not reserve anything.
|
|
29
|
+
- Give a note an `id` when something needs to point at it: `<Footnote id="src-1">`.
|
|
30
|
+
|
|
31
|
+
**On a fixed page, add `<Footnotes />` where the notes should print.** A fixed
|
|
32
|
+
page is your layout, so the framework does not inject anything into it:
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
const Cover: DocPage = () => (
|
|
36
|
+
<div style={page}>
|
|
37
|
+
<p>Q3 2026<Footnote>Covers the platform tier only.</Footnote></p>
|
|
38
|
+
<Footnotes />
|
|
39
|
+
</div>
|
|
40
|
+
);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**Put `<Footnote>` in the JSX you hand to `flow()`.** The notes are lifted out of
|
|
44
|
+
the blocks before measurement, by walking the element tree you wrote. One hidden
|
|
45
|
+
inside a helper component's own body is invisible to that walk and will render
|
|
46
|
+
inline instead of at the foot of the page.
|
|
47
|
+
|
|
48
|
+
## Figures and tables
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
import { Figure } from '@open-document/core';
|
|
52
|
+
|
|
53
|
+
<Figure id="topology" caption="Service topology, Q3 2026">
|
|
54
|
+
<img src={diagram} alt="Service topology" style={{ width: '100%', display: 'block' }} />
|
|
55
|
+
</Figure>
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
- `kind="table"` numbers it with the tables instead, and captions above rather
|
|
59
|
+
than below.
|
|
60
|
+
- Content and caption are **one block**, so a flow section never separates them.
|
|
61
|
+
- `caption` may carry markup; pass `captionText` as well when it does, so the
|
|
62
|
+
list of figures has plain text to print.
|
|
63
|
+
|
|
64
|
+
`<ListOfFigures />` and `<ListOfTables />` build the lists, exactly like
|
|
65
|
+
`<TableOfContents />` builds the contents.
|
|
66
|
+
|
|
67
|
+
## Cross-references
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
import { Ref } from '@open-document/core';
|
|
71
|
+
|
|
72
|
+
<p style={p}>The shape in <Ref to="topology" /> is what the table hides.</p>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Renders `Figure 3`, and appends the page — `Figure 3 (p. 12)` — only when the
|
|
76
|
+
target sits on another sheet. Force it either way with `showPage`.
|
|
77
|
+
|
|
78
|
+
**Never write "see Figure 3 on page 12" by hand.** Both numbers move.
|
|
79
|
+
|
|
80
|
+
A reference to an id nothing declares renders `[?the-id]` on the page and is
|
|
81
|
+
reported by `open-doc check` as an error. That is deliberate: a silent blank is
|
|
82
|
+
worse than a visible hole.
|
|
83
|
+
|
|
84
|
+
## What things are called
|
|
85
|
+
|
|
86
|
+
Numbering is structural; the words around it are the document's own business.
|
|
87
|
+
|
|
88
|
+
```tsx
|
|
89
|
+
export const meta: DocMeta = {
|
|
90
|
+
title: '平台可靠度回顧',
|
|
91
|
+
labels: { figure: '圖', table: '表', onPage: '(第 {page} 頁)' },
|
|
92
|
+
};
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`footnotes` adds a heading above a page's notes; leave it empty for none.
|
|
96
|
+
|
|
97
|
+
## Data
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
import { DataTable } from '@open-document/core';
|
|
101
|
+
import services from './data/services.csv';
|
|
102
|
+
|
|
103
|
+
<DataTable
|
|
104
|
+
id="service-table"
|
|
105
|
+
caption="Platform tier, Q3 2026"
|
|
106
|
+
rows={services}
|
|
107
|
+
columns={[
|
|
108
|
+
{ key: 'service', label: 'Service' },
|
|
109
|
+
{ key: 'requests', label: 'Requests', format: 'integer' },
|
|
110
|
+
{ key: 'error_rate', label: 'Errors', format: 'percent' },
|
|
111
|
+
]}
|
|
112
|
+
/>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
- `.csv` and `.tsv` under the document resolve to an array of objects **at build
|
|
116
|
+
time**. Quoted fields, embedded commas and newlines, and CRLF all work; a
|
|
117
|
+
numeric cell becomes a number, an empty cell becomes `null`.
|
|
118
|
+
- Data is never fetched at render time, and must not be: the packer measures the
|
|
119
|
+
real DOM to decide where pages break, so anything that arrives a tick later
|
|
120
|
+
arrives after the layout is decided.
|
|
121
|
+
- Columns default to the keys of the first row. A column whose values are all
|
|
122
|
+
numbers aligns right and gets `tabular-nums` without being told.
|
|
123
|
+
- `format`: `'integer'`, `'number'`, `'percent'`, `'text'`, or your own
|
|
124
|
+
`(value, row) => ReactNode`.
|
|
125
|
+
- `caption` numbers the table and prints the caption above it; without one you
|
|
126
|
+
get a plain table.
|
|
127
|
+
- `limit` prints the first N rows and says how many were left out — never
|
|
128
|
+
truncate a table silently.
|
|
129
|
+
|
|
130
|
+
**Retyping numbers into JSX is the thing this replaces.** If the user has the
|
|
131
|
+
data in a file, import the file.
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from 'react';
|
|
2
|
+
import { Figure } from './numbering';
|
|
3
|
+
|
|
4
|
+
export type DataAlign = 'left' | 'center' | 'right';
|
|
5
|
+
|
|
6
|
+
export type DataFormat =
|
|
7
|
+
| 'text'
|
|
8
|
+
| 'number'
|
|
9
|
+
| 'integer'
|
|
10
|
+
| 'percent'
|
|
11
|
+
| ((value: unknown, row: Record<string, unknown>) => ReactNode);
|
|
12
|
+
|
|
13
|
+
export type DataColumn = {
|
|
14
|
+
key: string;
|
|
15
|
+
label?: ReactNode;
|
|
16
|
+
align?: DataAlign;
|
|
17
|
+
format?: DataFormat;
|
|
18
|
+
/** Column width. A number is px; `tableLayout: fixed` means these are honoured. */
|
|
19
|
+
width?: number | string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type DataTableProps = {
|
|
23
|
+
/** Rows as parsed from a `.csv`/`.tsv` import, or any array of objects. */
|
|
24
|
+
rows: Array<Record<string, unknown>>;
|
|
25
|
+
/** Columns to print, in order. Derived from the first row when omitted. */
|
|
26
|
+
columns?: Array<DataColumn | string>;
|
|
27
|
+
/** Numbers the table and prints the caption above it. */
|
|
28
|
+
caption?: ReactNode;
|
|
29
|
+
/** Stable id for `<Ref to>`. Only meaningful alongside `caption`. */
|
|
30
|
+
id?: string;
|
|
31
|
+
/** Print at most this many rows, with a note about what was left out. */
|
|
32
|
+
limit?: number;
|
|
33
|
+
/** Tighter rows, for a dense appendix table. */
|
|
34
|
+
compact?: boolean;
|
|
35
|
+
/** What an empty cell prints as. Defaults to an em dash. */
|
|
36
|
+
emptyValue?: ReactNode;
|
|
37
|
+
style?: CSSProperties;
|
|
38
|
+
className?: string;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
function normalizeColumn(column: DataColumn | string): DataColumn {
|
|
42
|
+
return typeof column === 'string' ? { key: column } : column;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isNumber(value: unknown): value is number {
|
|
46
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A column of numbers aligns right; anything mixed stays left. */
|
|
50
|
+
function inferAlign(rows: Array<Record<string, unknown>>, key: string): DataAlign {
|
|
51
|
+
let seen = 0;
|
|
52
|
+
for (const row of rows) {
|
|
53
|
+
const value = row[key];
|
|
54
|
+
if (value === null || value === undefined || value === '') continue;
|
|
55
|
+
if (!isNumber(value)) return 'left';
|
|
56
|
+
seen++;
|
|
57
|
+
}
|
|
58
|
+
return seen > 0 ? 'right' : 'left';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function formatValue(
|
|
62
|
+
value: unknown,
|
|
63
|
+
format: DataFormat | undefined,
|
|
64
|
+
row: Record<string, unknown>,
|
|
65
|
+
emptyValue: ReactNode,
|
|
66
|
+
): ReactNode {
|
|
67
|
+
if (typeof format === 'function') return format(value, row);
|
|
68
|
+
if (value === null || value === undefined || value === '') return emptyValue;
|
|
69
|
+
|
|
70
|
+
if (isNumber(value)) {
|
|
71
|
+
if (format === 'integer') return Math.round(value).toLocaleString();
|
|
72
|
+
if (format === 'percent')
|
|
73
|
+
return `${(value * 100).toLocaleString(undefined, { maximumFractionDigits: 1 })}%`;
|
|
74
|
+
if (format === 'text') return String(value);
|
|
75
|
+
// Grouping separators are what make a column of figures readable; the
|
|
76
|
+
// fraction digits follow the data rather than padding every integer.
|
|
77
|
+
return value.toLocaleString(undefined, { maximumFractionDigits: 2 });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return String(value);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* A print-shaped table from data, so the numbers in a report come from a file
|
|
85
|
+
* instead of being retyped into JSX. Alignment and grouping follow the column's
|
|
86
|
+
* contents, which is the part people get wrong by hand.
|
|
87
|
+
*/
|
|
88
|
+
export function DataTable({
|
|
89
|
+
rows,
|
|
90
|
+
columns,
|
|
91
|
+
caption,
|
|
92
|
+
id,
|
|
93
|
+
limit,
|
|
94
|
+
compact = false,
|
|
95
|
+
emptyValue = '—',
|
|
96
|
+
style,
|
|
97
|
+
className,
|
|
98
|
+
}: DataTableProps) {
|
|
99
|
+
const resolved: DataColumn[] = (columns ?? Object.keys(rows[0] ?? {})).map(normalizeColumn);
|
|
100
|
+
|
|
101
|
+
const shown = limit !== undefined ? rows.slice(0, limit) : rows;
|
|
102
|
+
const hidden = rows.length - shown.length;
|
|
103
|
+
|
|
104
|
+
const cellPadding = compact ? '4px 8px' : '7px 8px';
|
|
105
|
+
|
|
106
|
+
const table = (
|
|
107
|
+
<table
|
|
108
|
+
className={className}
|
|
109
|
+
style={{
|
|
110
|
+
width: '100%',
|
|
111
|
+
borderCollapse: 'collapse',
|
|
112
|
+
tableLayout: 'fixed',
|
|
113
|
+
margin: 0,
|
|
114
|
+
...style,
|
|
115
|
+
}}
|
|
116
|
+
>
|
|
117
|
+
<thead>
|
|
118
|
+
<tr>
|
|
119
|
+
{resolved.map((column) => {
|
|
120
|
+
const align = column.align ?? inferAlign(rows, column.key);
|
|
121
|
+
return (
|
|
122
|
+
<th
|
|
123
|
+
key={column.key}
|
|
124
|
+
style={{
|
|
125
|
+
textAlign: align,
|
|
126
|
+
width: column.width,
|
|
127
|
+
fontFamily: 'var(--od-font-heading)',
|
|
128
|
+
fontSize: 11,
|
|
129
|
+
fontWeight: 600,
|
|
130
|
+
letterSpacing: '0.04em',
|
|
131
|
+
textTransform: 'uppercase',
|
|
132
|
+
color: 'var(--od-muted)',
|
|
133
|
+
borderBottom: '1px solid var(--od-rule)',
|
|
134
|
+
padding: '0 8px 6px',
|
|
135
|
+
}}
|
|
136
|
+
>
|
|
137
|
+
{column.label ?? column.key}
|
|
138
|
+
</th>
|
|
139
|
+
);
|
|
140
|
+
})}
|
|
141
|
+
</tr>
|
|
142
|
+
</thead>
|
|
143
|
+
<tbody>
|
|
144
|
+
{shown.map((row, rowIndex) => (
|
|
145
|
+
// Rows come from a file and carry no id of their own; their position
|
|
146
|
+
// is what identifies them.
|
|
147
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: row order is the row's identity here
|
|
148
|
+
<tr key={rowIndex}>
|
|
149
|
+
{resolved.map((column) => {
|
|
150
|
+
const align = column.align ?? inferAlign(rows, column.key);
|
|
151
|
+
return (
|
|
152
|
+
<td
|
|
153
|
+
key={column.key}
|
|
154
|
+
style={{
|
|
155
|
+
textAlign: align,
|
|
156
|
+
fontSize: compact ? 11 : 12,
|
|
157
|
+
padding: cellPadding,
|
|
158
|
+
borderBottom: '1px solid var(--od-rule)',
|
|
159
|
+
fontVariantNumeric: align === 'right' ? 'tabular-nums' : undefined,
|
|
160
|
+
overflowWrap: 'anywhere',
|
|
161
|
+
}}
|
|
162
|
+
>
|
|
163
|
+
{formatValue(row[column.key], column.format, row, emptyValue)}
|
|
164
|
+
</td>
|
|
165
|
+
);
|
|
166
|
+
})}
|
|
167
|
+
</tr>
|
|
168
|
+
))}
|
|
169
|
+
</tbody>
|
|
170
|
+
{hidden > 0 && (
|
|
171
|
+
<tfoot>
|
|
172
|
+
<tr>
|
|
173
|
+
<td
|
|
174
|
+
colSpan={resolved.length}
|
|
175
|
+
style={{
|
|
176
|
+
fontSize: 'var(--od-size-caption)',
|
|
177
|
+
color: 'var(--od-muted)',
|
|
178
|
+
padding: '6px 8px 0',
|
|
179
|
+
}}
|
|
180
|
+
>
|
|
181
|
+
{hidden} more row{hidden === 1 ? '' : 's'} not shown
|
|
182
|
+
</td>
|
|
183
|
+
</tr>
|
|
184
|
+
</tfoot>
|
|
185
|
+
)}
|
|
186
|
+
</table>
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
if (!caption) return table;
|
|
190
|
+
|
|
191
|
+
return (
|
|
192
|
+
<Figure kind="table" caption={caption} {...(id !== undefined ? { id } : {})}>
|
|
193
|
+
{table}
|
|
194
|
+
</Figure>
|
|
195
|
+
);
|
|
196
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from 'react';
|
|
2
|
+
import type { LabelKind } from '../lib/labels';
|
|
3
|
+
import { Figure } from './numbering';
|
|
4
|
+
|
|
5
|
+
export type DiagramSource = {
|
|
6
|
+
svg: string;
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type DiagramProps = {
|
|
12
|
+
/** The compiled module from `import chart from './architecture.mmd'`. */
|
|
13
|
+
chart: DiagramSource;
|
|
14
|
+
/** Caption text. Given one, the drawing is numbered like any other figure. */
|
|
15
|
+
caption?: ReactNode;
|
|
16
|
+
captionText?: string;
|
|
17
|
+
kind?: LabelKind;
|
|
18
|
+
id?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Drawn width in CSS px. Defaults to the diagram's natural size, capped to
|
|
21
|
+
* the column — a diagram wider than the text block is a layout fault, and
|
|
22
|
+
* `open-doc check` would report it as one.
|
|
23
|
+
*/
|
|
24
|
+
width?: number;
|
|
25
|
+
align?: 'left' | 'center';
|
|
26
|
+
style?: CSSProperties;
|
|
27
|
+
className?: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A diagram compiled from Mermaid-flavoured text at build time. The SVG is
|
|
32
|
+
* inlined rather than referenced through `<img>` so it inherits the document's
|
|
33
|
+
* theme variables — an `<img src="data:…">` would be painted in a document of
|
|
34
|
+
* its own, with none of this one's colours or faces.
|
|
35
|
+
*/
|
|
36
|
+
export function Diagram({
|
|
37
|
+
chart,
|
|
38
|
+
caption,
|
|
39
|
+
captionText,
|
|
40
|
+
kind = 'figure',
|
|
41
|
+
id,
|
|
42
|
+
width,
|
|
43
|
+
align = 'center',
|
|
44
|
+
style,
|
|
45
|
+
className,
|
|
46
|
+
}: DiagramProps) {
|
|
47
|
+
const drawing = (
|
|
48
|
+
<div
|
|
49
|
+
className={caption ? undefined : className}
|
|
50
|
+
style={{
|
|
51
|
+
width: width ?? chart.width,
|
|
52
|
+
maxWidth: '100%',
|
|
53
|
+
marginLeft: align === 'center' ? 'auto' : undefined,
|
|
54
|
+
marginRight: align === 'center' ? 'auto' : undefined,
|
|
55
|
+
...(caption ? undefined : style),
|
|
56
|
+
}}
|
|
57
|
+
// biome-ignore lint/security/noDangerouslySetInnerHtml: the SVG is produced by this package's own renderer at build time, never from document input at runtime
|
|
58
|
+
dangerouslySetInnerHTML={{ __html: chart.svg }}
|
|
59
|
+
/>
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
if (!caption) return drawing;
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<Figure
|
|
66
|
+
{...(id ? { id } : {})}
|
|
67
|
+
caption={caption}
|
|
68
|
+
{...(captionText ? { captionText } : {})}
|
|
69
|
+
kind={kind}
|
|
70
|
+
className={className}
|
|
71
|
+
{...(style ? { style } : {})}
|
|
72
|
+
>
|
|
73
|
+
{drawing}
|
|
74
|
+
</Figure>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { CSSProperties, ReactNode } from 'react';
|
|
2
2
|
import type { DesignSystem } from '../lib/design';
|
|
3
3
|
import type { FlowSection } from '../lib/flow';
|
|
4
|
+
import type { ExtractedNote } from '../lib/footnotes';
|
|
5
|
+
import { Footnotes } from './footnote';
|
|
4
6
|
|
|
5
7
|
export const FLOW_BLOCK_ATTR = 'data-od-flow-block';
|
|
6
8
|
|
|
@@ -8,6 +10,10 @@ export const FLOW_BLOCK_ATTR = 'data-od-flow-block';
|
|
|
8
10
|
* The page shell a flow section renders into. The framework owns the margin and
|
|
9
11
|
* base typography here — that is the trade for not hand-splitting pages — while
|
|
10
12
|
* the blocks keep their own styles.
|
|
13
|
+
*
|
|
14
|
+
* It is a column so the footnote area can sit at the foot of the sheet rather
|
|
15
|
+
* than immediately under the last paragraph. The blocks stay inside one block
|
|
16
|
+
* container, so their margins collapse exactly as they did when measured.
|
|
11
17
|
*/
|
|
12
18
|
export function flowShellStyle(design: DesignSystem | undefined, padding?: number): CSSProperties {
|
|
13
19
|
return {
|
|
@@ -22,6 +28,8 @@ export function flowShellStyle(design: DesignSystem | undefined, padding?: numbe
|
|
|
22
28
|
lineHeight: 'var(--od-leading)',
|
|
23
29
|
position: 'relative',
|
|
24
30
|
overflow: 'hidden',
|
|
31
|
+
display: 'flex',
|
|
32
|
+
flexDirection: 'column',
|
|
25
33
|
};
|
|
26
34
|
}
|
|
27
35
|
|
|
@@ -33,17 +41,26 @@ export function FlowPage({
|
|
|
33
41
|
section,
|
|
34
42
|
design,
|
|
35
43
|
blockIndices,
|
|
44
|
+
blocks,
|
|
45
|
+
notes,
|
|
36
46
|
}: {
|
|
37
47
|
section: FlowSection;
|
|
38
48
|
design: DesignSystem | undefined;
|
|
39
49
|
blockIndices: number[];
|
|
50
|
+
/** Blocks with footnotes already lifted out; falls back to the authored ones. */
|
|
51
|
+
blocks?: ReactNode[];
|
|
52
|
+
notes?: ExtractedNote[];
|
|
40
53
|
}) {
|
|
41
54
|
const Footer = section.footer;
|
|
55
|
+
const source = blocks ?? section.blocks;
|
|
42
56
|
return (
|
|
43
57
|
<div style={flowShellStyle(design, section.padding)}>
|
|
44
|
-
{
|
|
45
|
-
|
|
46
|
-
|
|
58
|
+
<div style={{ flex: 1, minHeight: 0 }}>
|
|
59
|
+
{blockIndices.map((index) => (
|
|
60
|
+
<FlowBlock key={index}>{source[index]}</FlowBlock>
|
|
61
|
+
))}
|
|
62
|
+
</div>
|
|
63
|
+
{notes && notes.length > 0 && <Footnotes notes={notes} />}
|
|
47
64
|
{Footer && <Footer />}
|
|
48
65
|
</div>
|
|
49
66
|
);
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Context,
|
|
3
|
+
type CSSProperties,
|
|
4
|
+
createContext,
|
|
5
|
+
type ReactNode,
|
|
6
|
+
useContext,
|
|
7
|
+
useEffect,
|
|
8
|
+
useId,
|
|
9
|
+
useMemo,
|
|
10
|
+
useRef,
|
|
11
|
+
useState,
|
|
12
|
+
} from 'react';
|
|
13
|
+
import { LABEL_ATTR, LABEL_ID_ATTR, useDocLabel, useLabelVocabulary } from '../lib/labels';
|
|
14
|
+
|
|
15
|
+
/** Stands in for a number the scan has not produced yet, or never will. */
|
|
16
|
+
const UNNUMBERED = '\u2022';
|
|
17
|
+
|
|
18
|
+
export type CollectedNote = { id: string; content: ReactNode };
|
|
19
|
+
|
|
20
|
+
type Collector = {
|
|
21
|
+
/** Registration order, which is document order. */
|
|
22
|
+
ids: string[];
|
|
23
|
+
/**
|
|
24
|
+
* Note bodies live outside React state on purpose. A `ReactNode` is a new
|
|
25
|
+
* object on every render, so storing one in state would make every commit
|
|
26
|
+
* look like a change — register, re-render, register — and never settle.
|
|
27
|
+
* Ids are stable, so only they drive a re-render; the bodies are read fresh
|
|
28
|
+
* from here in whatever render follows.
|
|
29
|
+
*/
|
|
30
|
+
contents: Map<string, ReactNode>;
|
|
31
|
+
register: (id: string, content: ReactNode) => void;
|
|
32
|
+
unregister: (id: string) => void;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// Same globalThis treatment as the page context: a document imports the
|
|
36
|
+
// published bundle while the viewer runs the source copy, and a context created
|
|
37
|
+
// twice registers into one instance and reads from the other.
|
|
38
|
+
const GLOBAL_KEY = '__open_doc_footnote_context__';
|
|
39
|
+
type GlobalWithCtx = typeof globalThis & { [GLOBAL_KEY]?: Context<Collector | null> };
|
|
40
|
+
const g = globalThis as GlobalWithCtx;
|
|
41
|
+
if (!g[GLOBAL_KEY]) {
|
|
42
|
+
g[GLOBAL_KEY] = createContext<Collector | null>(null);
|
|
43
|
+
}
|
|
44
|
+
const FootnoteContext = g[GLOBAL_KEY];
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Collects the footnotes a fixed page renders, so `<Footnotes />` can print
|
|
48
|
+
* them at its foot. Flow sections never use this: their notes are lifted out of
|
|
49
|
+
* the blocks before measurement, because the space they take decides where the
|
|
50
|
+
* page breaks land.
|
|
51
|
+
*/
|
|
52
|
+
export function FootnoteCollector({ children }: { children?: ReactNode }) {
|
|
53
|
+
const contents = useRef<Map<string, ReactNode>>(new Map()).current;
|
|
54
|
+
const [ids, setIds] = useState<string[]>([]);
|
|
55
|
+
|
|
56
|
+
const value = useMemo<Collector>(
|
|
57
|
+
() => ({
|
|
58
|
+
ids,
|
|
59
|
+
contents,
|
|
60
|
+
register: (id, content) => {
|
|
61
|
+
contents.set(id, content);
|
|
62
|
+
setIds((current) => (current.includes(id) ? current : [...current, id]));
|
|
63
|
+
},
|
|
64
|
+
unregister: (id) => {
|
|
65
|
+
contents.delete(id);
|
|
66
|
+
setIds((current) => (current.includes(id) ? current.filter((x) => x !== id) : current));
|
|
67
|
+
},
|
|
68
|
+
}),
|
|
69
|
+
[ids, contents],
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return <FootnoteContext.Provider value={value}>{children}</FootnoteContext.Provider>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const FOOTNOTE_MARKER_FLAG = '__odFootnote';
|
|
76
|
+
|
|
77
|
+
export type FootnoteProps = {
|
|
78
|
+
/** Stable id, so `<Ref to>` can point at the note. Generated when omitted. */
|
|
79
|
+
id?: string;
|
|
80
|
+
children?: ReactNode;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export function markerStyle(): CSSProperties {
|
|
84
|
+
return {
|
|
85
|
+
fontSize: '0.7em',
|
|
86
|
+
lineHeight: 0,
|
|
87
|
+
verticalAlign: 'super',
|
|
88
|
+
fontVariantNumeric: 'tabular-nums',
|
|
89
|
+
color: 'var(--od-accent)',
|
|
90
|
+
padding: '0 1px',
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The superscript in the text. Its position in the document decides the number.
|
|
96
|
+
*
|
|
97
|
+
* The placeholder is the one the printed note uses, and deliberately not an
|
|
98
|
+
* empty string: the two describe the same unresolved state, a note whose number
|
|
99
|
+
* never arrives has to be visible in both places rather than silently blank,
|
|
100
|
+
* and the packer measures this glyph — an empty marker is narrower than the
|
|
101
|
+
* digit that replaces it.
|
|
102
|
+
*/
|
|
103
|
+
export function FootnoteMarker({ id }: { id: string }) {
|
|
104
|
+
const entry = useDocLabel(id);
|
|
105
|
+
return (
|
|
106
|
+
<sup {...{ [LABEL_ATTR]: 'footnote', [LABEL_ID_ATTR]: id }} style={markerStyle()}>
|
|
107
|
+
{entry?.number ?? UNNUMBERED}
|
|
108
|
+
</sup>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* A note anchored to this point in the text. Inside a `flow()` section the
|
|
114
|
+
* framework lifts it to the foot of whatever page the marker lands on; on a
|
|
115
|
+
* fixed page, put a `<Footnotes />` where you want them printed.
|
|
116
|
+
*/
|
|
117
|
+
export function Footnote({ id, children }: FootnoteProps) {
|
|
118
|
+
const generated = useId();
|
|
119
|
+
const noteId = id ?? generated;
|
|
120
|
+
const collector = useContext(FootnoteContext);
|
|
121
|
+
const latest = useRef<Collector | null>(null);
|
|
122
|
+
|
|
123
|
+
// Runs after every commit so the body stays current; only a new id changes
|
|
124
|
+
// state, so this settles instead of looping.
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
latest.current = collector;
|
|
127
|
+
collector?.register(noteId, children);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
useEffect(() => () => latest.current?.unregister(noteId), [noteId]);
|
|
131
|
+
|
|
132
|
+
return <FootnoteMarker id={noteId} />;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
(Footnote as unknown as Record<string, boolean>)[FOOTNOTE_MARKER_FLAG] = true;
|
|
136
|
+
|
|
137
|
+
export type FootnotesProps = {
|
|
138
|
+
/** Notes to print. Omitted on a fixed page, where they are collected from it. */
|
|
139
|
+
notes?: CollectedNote[];
|
|
140
|
+
style?: CSSProperties;
|
|
141
|
+
className?: string;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/** Outside the area's box, so measurement has to add it back by hand. */
|
|
145
|
+
export const FOOTNOTE_AREA_MARGIN_TOP = 12;
|
|
146
|
+
|
|
147
|
+
export const FOOTNOTE_ROW_ATTR = 'data-od-footnote-row';
|
|
148
|
+
|
|
149
|
+
export function footnoteAreaStyle(): CSSProperties {
|
|
150
|
+
return {
|
|
151
|
+
borderTop: '1px solid var(--od-rule)',
|
|
152
|
+
paddingTop: 6,
|
|
153
|
+
marginTop: FOOTNOTE_AREA_MARGIN_TOP,
|
|
154
|
+
fontSize: 'var(--od-size-caption)',
|
|
155
|
+
lineHeight: 1.45,
|
|
156
|
+
color: 'var(--od-muted)',
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** One note's row. Shared with the measurement pass so the reserved space is real. */
|
|
161
|
+
export function FootnoteRow({ id, content }: CollectedNote) {
|
|
162
|
+
const entry = useDocLabel(id);
|
|
163
|
+
return (
|
|
164
|
+
<div {...{ [FOOTNOTE_ROW_ATTR]: '' }} style={{ display: 'flex', gap: 5, marginBottom: 3 }}>
|
|
165
|
+
<span
|
|
166
|
+
style={{
|
|
167
|
+
flex: 'none',
|
|
168
|
+
fontVariantNumeric: 'tabular-nums',
|
|
169
|
+
color: 'var(--od-accent)',
|
|
170
|
+
}}
|
|
171
|
+
>
|
|
172
|
+
{entry?.number ?? UNNUMBERED}
|
|
173
|
+
</span>
|
|
174
|
+
<span style={{ minWidth: 0 }}>{content}</span>
|
|
175
|
+
</div>
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function Footnotes({ notes, style, className }: FootnotesProps) {
|
|
180
|
+
const collector = useContext(FootnoteContext);
|
|
181
|
+
const vocabulary = useLabelVocabulary();
|
|
182
|
+
const list =
|
|
183
|
+
notes ?? collector?.ids.map((id) => ({ id, content: collector.contents.get(id) })) ?? [];
|
|
184
|
+
if (list.length === 0) return null;
|
|
185
|
+
|
|
186
|
+
return (
|
|
187
|
+
<div data-od-footnotes="" className={className} style={{ ...footnoteAreaStyle(), ...style }}>
|
|
188
|
+
{vocabulary.footnotes ? (
|
|
189
|
+
<div style={{ fontWeight: 600, marginBottom: 3 }}>{vocabulary.footnotes}</div>
|
|
190
|
+
) : null}
|
|
191
|
+
{list.map((note) => (
|
|
192
|
+
<FootnoteRow key={note.id} id={note.id} content={note.content} />
|
|
193
|
+
))}
|
|
194
|
+
</div>
|
|
195
|
+
);
|
|
196
|
+
}
|
|
@@ -180,6 +180,22 @@ export function Inspector({ docId, containerRef, onExit }: Props) {
|
|
|
180
180
|
};
|
|
181
181
|
}, [container, insidePanel, onExit, selected]);
|
|
182
182
|
|
|
183
|
+
// Report the pick to the dev server so `current.json` can answer "this
|
|
184
|
+
// element" for an agent. Clearing the selection clears it there too.
|
|
185
|
+
useEffect(() => {
|
|
186
|
+
if (!import.meta.hot) return;
|
|
187
|
+
import.meta.hot.send('open-doc:current', {
|
|
188
|
+
selection: selected
|
|
189
|
+
? {
|
|
190
|
+
line: selected.line,
|
|
191
|
+
column: selected.column,
|
|
192
|
+
tagName: selected.anchor.tagName.toLowerCase(),
|
|
193
|
+
text: normalize(selected.anchor.textContent ?? '').slice(0, 120),
|
|
194
|
+
}
|
|
195
|
+
: null,
|
|
196
|
+
});
|
|
197
|
+
}, [selected]);
|
|
198
|
+
|
|
183
199
|
// Read the element's text from source, not from the DOM: only source can say
|
|
184
200
|
// whether it is a single editable text child or markup we must not touch.
|
|
185
201
|
useEffect(() => {
|