@open-document/core 0.2.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-U81g9Hnk.js → build-C3pMGxJN.js} +1 -1
- package/dist/check-Dtwb1wvU.js +42 -0
- package/dist/cli/bin.js +24 -3
- package/dist/config-Dg1AMP0r.js +2461 -0
- package/dist/context-FoX0afRf.js +26 -0
- package/dist/{dev-DwwFT6uX.js → dev-Cdd7NZRQ.js} +1 -1
- 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 +235 -90
- package/dist/index.js +515 -49
- package/dist/ops/index.d.ts +96 -1
- package/dist/ops/index.js +3 -199
- package/dist/ops-Ci5Xw6QG.js +1270 -0
- package/dist/{preview-C_jbySle.js → preview-BhbCiAKC.js} +1 -1
- package/dist/sdk-CWMsH9Ms.d.ts +123 -0
- package/dist/vite/index.d.ts +6 -0
- package/dist/vite/index.js +1 -2
- package/env.d.ts +33 -1
- package/package.json +10 -1
- package/skills/create-doc/SKILL.md +6 -2
- 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/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 +7 -4
- 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,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
|
+
}
|