@open-document/core 0.5.0 → 0.7.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.
Files changed (35) hide show
  1. package/dist/{build-BzhYQKev.js → build-CFL7nhz0.js} +1 -1
  2. package/dist/{check-CCAaXofH.js → check-pEQh4jJE.js} +3 -3
  3. package/dist/cli/bin.js +6 -6
  4. package/dist/{config-CNr8hd5X.js → config-4nz5NnxW.js} +899 -159
  5. package/dist/{context-CJpdjYfU.js → context-BH8C-dSW.js} +1 -1
  6. package/dist/{dev-BhXgunme.js → dev-BCSUczXW.js} +1 -1
  7. package/dist/{export-B69kDkyp.js → export-BQ-GvMsb.js} +3 -3
  8. package/dist/{import-DBhCpUsl.js → import-Daat3L2J.js} +2 -2
  9. package/dist/index.d.ts +21 -3
  10. package/dist/index.js +41 -9
  11. package/dist/ops/index.d.ts +47 -3
  12. package/dist/ops/index.js +3 -3
  13. package/dist/{ops-BCBmRMCS.js → ops-DQfckiyp.js} +2 -45
  14. package/dist/{preview-Da4Aq9yF.js → preview-CwCrw-i3.js} +1 -1
  15. package/dist/vite/index.js +1 -1
  16. package/package.json +1 -1
  17. package/src/app/components/data-table.tsx +17 -2
  18. package/src/app/components/design-panel/design-panel.tsx +149 -274
  19. package/src/app/components/design-panel/design-provider.tsx +35 -15
  20. package/src/app/components/footnote.tsx +27 -10
  21. package/src/app/components/history-provider.tsx +125 -0
  22. package/src/app/components/inspector/inspector.tsx +1158 -146
  23. package/src/app/components/numbering.tsx +13 -1
  24. package/src/app/components/panel/edit-save-card.tsx +112 -0
  25. package/src/app/components/panel/fields.tsx +247 -0
  26. package/src/app/components/panel/panel-shell.tsx +71 -0
  27. package/src/app/components/panel/save-card.tsx +146 -0
  28. package/src/app/components/table-of-contents.tsx +4 -0
  29. package/src/app/lib/footnotes.ts +6 -3
  30. package/src/app/lib/href.ts +14 -0
  31. package/src/app/lib/inspector/inline-edit.test.ts +244 -0
  32. package/src/app/lib/inspector/inline-edit.ts +770 -0
  33. package/src/app/lib/source-loc.ts +15 -0
  34. package/src/app/routes/doc.tsx +109 -39
  35. package/src/app/styles.css +41 -0
@@ -1,12 +1,15 @@
1
- import { Loader2, RotateCcw, Shuffle, X } from 'lucide-react';
1
+ import { Palette, RotateCcw, Shuffle, X } from 'lucide-react';
2
2
  import type { DesignSystem } from '../../lib/design';
3
+ import { ColorField, FontField, type FontPreset, Section, SliderField } from '../panel/fields';
4
+ import { PanelBanner, PanelIconButton, PanelShell } from '../panel/panel-shell';
3
5
  import { useDesignPanelState } from './design-provider';
4
6
 
5
- const FONT_OPTIONS: Array<{ label: string; value: string }> = [
7
+ const FONT_PRESETS: FontPreset[] = [
6
8
  {
7
9
  label: 'System sans',
8
10
  value: '-apple-system, BlinkMacSystemFont, "Inter", system-ui, sans-serif',
9
11
  },
12
+ { label: 'Inter', value: '"Inter", system-ui, sans-serif' },
10
13
  { label: 'Helvetica', value: '"Helvetica Neue", Helvetica, Arial, sans-serif' },
11
14
  { label: 'Georgia serif', value: 'Georgia, "Times New Roman", serif' },
12
15
  { label: 'Times serif', value: '"Times New Roman", Times, serif' },
@@ -21,6 +24,12 @@ const PALETTE_KEYS: Array<{ key: keyof DesignSystem['palette']; label: string }>
21
24
  { key: 'rule', label: 'Rule' },
22
25
  ];
23
26
 
27
+ const FONT_KEYS: Array<{ key: keyof DesignSystem['fonts']; label: string }> = [
28
+ { key: 'heading', label: 'Heading' },
29
+ { key: 'body', label: 'Body' },
30
+ { key: 'mono', label: 'Mono' },
31
+ ];
32
+
24
33
  const TYPE_KEYS: Array<{
25
34
  key: keyof DesignSystem['typeScale'];
26
35
  label: string;
@@ -35,284 +44,150 @@ const TYPE_KEYS: Array<{
35
44
  { key: 'caption', label: 'Caption', min: 7, max: 16 },
36
45
  ];
37
46
 
47
+ /**
48
+ * The document's design tokens, edited as a draft. Every change previews on
49
+ * the pages at once and is one step in the view's undo history; saving and
50
+ * discarding happen on the save card, with the page edits, so there is one
51
+ * place that says what is unsaved.
52
+ */
38
53
  export function DesignPanel({ onClose }: { onClose: () => void }) {
39
- const {
40
- loaded,
41
- exists,
42
- warning,
43
- draft,
44
- dirty,
45
- committing,
46
- error,
47
- update,
48
- commit,
49
- discard,
50
- resetToDefaults,
51
- shuffle,
52
- } = useDesignPanelState();
54
+ const { loaded, exists, warning, draft, dirty, update, resetToDefaults, shuffle } =
55
+ useDesignPanelState();
56
+
57
+ // The dock stays closed until there is a draft to show, rather than opening
58
+ // on a spinner the reader has to wait out.
59
+ if (!loaded || !draft) return null;
53
60
 
54
61
  return (
55
- <aside className="flex w-72 flex-none flex-col border-border border-l bg-background">
56
- <header className="flex h-10 flex-none items-center justify-between border-border border-b px-3">
57
- <span className="font-medium text-xs uppercase tracking-wider">Design</span>
58
- <div className="flex items-center gap-0.5">
59
- <IconButton label="Shuffle preset" onClick={shuffle}>
62
+ <PanelShell
63
+ label="Design"
64
+ header={
65
+ <>
66
+ <Palette aria-hidden className="size-3.5 flex-none text-muted-foreground" />
67
+ <span className="font-medium text-xs">Design</span>
68
+ {!exists && (
69
+ <span
70
+ title="This document has no design const yet — saving writes one"
71
+ className="rounded border border-border bg-muted px-1.5 py-px font-mono text-[9.5px] text-muted-foreground uppercase tracking-wider"
72
+ >
73
+ Draft
74
+ </span>
75
+ )}
76
+ <span
77
+ aria-hidden
78
+ title={dirty ? 'Unsaved changes' : undefined}
79
+ className={`size-1.5 rounded-full bg-foreground transition-opacity duration-150 ${dirty ? 'opacity-100' : 'opacity-0'}`}
80
+ />
81
+ </>
82
+ }
83
+ actions={
84
+ <>
85
+ <PanelIconButton label="Shuffle preset" onClick={shuffle}>
60
86
  <Shuffle className="size-3.5" />
61
- </IconButton>
62
- <IconButton label="Reset to defaults" onClick={resetToDefaults}>
87
+ </PanelIconButton>
88
+ <PanelIconButton label="Reset to defaults" onClick={resetToDefaults}>
63
89
  <RotateCcw className="size-3.5" />
64
- </IconButton>
65
- <IconButton label="Close design panel" onClick={onClose}>
90
+ </PanelIconButton>
91
+ <PanelIconButton label="Close design panel (D)" onClick={onClose}>
66
92
  <X className="size-3.5" />
67
- </IconButton>
68
- </div>
69
- </header>
70
-
71
- <div className="min-h-0 flex-1 overflow-y-auto px-3 py-3">
72
- {!loaded || !draft ? (
73
- <div className="grid place-items-center py-10">
74
- <Loader2 className="size-4 animate-spin text-muted-foreground" />
75
- </div>
76
- ) : (
77
- <>
78
- {warning && (
79
- <p className="mb-3 rounded-md border border-border bg-muted px-2 py-1.5 text-[11px] text-muted-foreground">
80
- {warning}
81
- </p>
82
- )}
83
- {!exists && (
84
- <p className="mb-3 rounded-md border border-border bg-muted px-2 py-1.5 text-[11px] text-muted-foreground">
85
- This document has no <code className="font-mono">design</code> const yet. Saving
86
- writes one into <code className="font-mono">index.tsx</code>.
87
- </p>
88
- )}
89
-
90
- <Section title="Palette">
91
- {PALETTE_KEYS.map(({ key, label }) => (
92
- <Row key={key} label={label}>
93
- <input
94
- type="color"
95
- value={draft.palette[key]}
96
- onChange={(e) => {
97
- const value = e.target.value;
98
- update((d) => {
99
- d.palette[key] = value;
100
- });
101
- }}
102
- className="size-6 cursor-pointer rounded border border-border bg-transparent"
103
- />
104
- <input
105
- type="text"
106
- value={draft.palette[key]}
107
- onChange={(e) => {
108
- const value = e.target.value;
109
- update((d) => {
110
- d.palette[key] = value;
111
- });
112
- }}
113
- className="w-20 rounded border border-border bg-transparent px-1.5 py-0.5 font-mono text-[11px]"
114
- />
115
- </Row>
116
- ))}
117
- </Section>
118
-
119
- <Section title="Fonts">
120
- {(['heading', 'body', 'mono'] as const).map((slot) => (
121
- <Row key={slot} label={slot[0].toUpperCase() + slot.slice(1)}>
122
- <select
123
- value={
124
- FONT_OPTIONS.some((o) => o.value === draft.fonts[slot])
125
- ? draft.fonts[slot]
126
- : 'custom'
127
- }
128
- onChange={(e) => {
129
- const value = e.target.value;
130
- if (value === 'custom') return;
131
- update((d) => {
132
- d.fonts[slot] = value;
133
- });
134
- }}
135
- className="w-40 rounded border border-border bg-transparent px-1.5 py-0.5 text-[11px]"
136
- >
137
- {FONT_OPTIONS.map((option) => (
138
- <option key={option.label} value={option.value}>
139
- {option.label}
140
- </option>
141
- ))}
142
- <option value="custom">Custom (from source)</option>
143
- </select>
144
- </Row>
145
- ))}
146
- </Section>
147
-
148
- <Section title="Type scale">
149
- {TYPE_KEYS.map(({ key, label, min, max }) => (
150
- <SliderRow
151
- key={key}
152
- label={label}
153
- value={draft.typeScale[key]}
154
- min={min}
155
- max={max}
156
- step={1}
157
- suffix="px"
158
- onChange={(value) =>
159
- update((d) => {
160
- d.typeScale[key] = value;
161
- })
162
- }
163
- />
164
- ))}
165
- </Section>
166
-
167
- <Section title="Page">
168
- <SliderRow
169
- label="Margin"
170
- value={draft.margin}
171
- min={32}
172
- max={140}
173
- step={2}
174
- suffix="px"
175
- onChange={(value) =>
176
- update((d) => {
177
- d.margin = value;
178
- })
179
- }
180
- />
181
- <SliderRow
182
- label="Leading"
183
- value={draft.leading}
184
- min={1.2}
185
- max={2}
186
- step={0.05}
187
- onChange={(value) =>
188
- update((d) => {
189
- d.leading = Number(value.toFixed(2));
190
- })
191
- }
192
- />
193
- <SliderRow
194
- label="Radius"
195
- value={draft.radius}
196
- min={0}
197
- max={24}
198
- step={1}
199
- suffix="px"
200
- onChange={(value) =>
201
- update((d) => {
202
- d.radius = value;
203
- })
204
- }
205
- />
206
- </Section>
207
-
208
- {draft.typeScale.body < 12 && (
209
- <p className="mt-3 rounded-md border border-border bg-muted px-2 py-1.5 text-[11px] text-muted-foreground">
210
- Body under 12px prints below 9pt — hard to read on paper.
211
- </p>
212
- )}
213
- </>
214
- )}
215
- </div>
216
-
217
- <footer className="flex-none border-border border-t p-3">
218
- {error && <p className="mb-2 text-[11px] text-muted-foreground">{error}</p>}
219
- <div className="flex items-center gap-2">
220
- <button
221
- type="button"
222
- onClick={commit}
223
- disabled={!dirty || committing}
224
- className="flex flex-1 items-center justify-center gap-1.5 rounded-md bg-primary px-3 py-1.5 text-primary-foreground text-xs transition-opacity hover:opacity-90 disabled:opacity-50"
225
- >
226
- {committing && <Loader2 className="size-3.5 animate-spin" />}
227
- Save to source
228
- </button>
229
- <button
230
- type="button"
231
- onClick={discard}
232
- disabled={!dirty || committing}
233
- className="rounded-md border border-border px-3 py-1.5 text-xs transition-colors hover:bg-accent disabled:opacity-50"
234
- >
235
- Discard
236
- </button>
237
- </div>
238
- </footer>
239
- </aside>
240
- );
241
- }
242
-
243
- function Section({ title, children }: { title: string; children: React.ReactNode }) {
244
- return (
245
- <section className="mb-4">
246
- <h3 className="mb-1.5 text-[10px] text-muted-foreground uppercase tracking-wider">{title}</h3>
247
- <div className="space-y-1.5">{children}</div>
248
- </section>
249
- );
250
- }
251
-
252
- function Row({ label, children }: { label: string; children: React.ReactNode }) {
253
- return (
254
- <div className="flex items-center justify-between gap-2">
255
- <span className="text-xs">{label}</span>
256
- <div className="flex items-center gap-1.5">{children}</div>
257
- </div>
258
- );
259
- }
260
-
261
- function SliderRow({
262
- label,
263
- value,
264
- min,
265
- max,
266
- step,
267
- suffix = '',
268
- onChange,
269
- }: {
270
- label: string;
271
- value: number;
272
- min: number;
273
- max: number;
274
- step: number;
275
- suffix?: string;
276
- onChange: (value: number) => void;
277
- }) {
278
- return (
279
- <div className="flex items-center gap-2">
280
- <span className="w-16 flex-none text-xs">{label}</span>
281
- <input
282
- type="range"
283
- min={min}
284
- max={max}
285
- step={step}
286
- value={value}
287
- onChange={(e) => onChange(Number(e.target.value))}
288
- className="h-1 flex-1 cursor-pointer accent-foreground"
289
- />
290
- <span className="w-12 flex-none text-right font-mono text-[11px] tabular-nums">
291
- {value}
292
- {suffix}
293
- </span>
294
- </div>
295
- );
296
- }
297
-
298
- function IconButton({
299
- label,
300
- onClick,
301
- children,
302
- }: {
303
- label: string;
304
- onClick: () => void;
305
- children: React.ReactNode;
306
- }) {
307
- return (
308
- <button
309
- type="button"
310
- aria-label={label}
311
- title={label}
312
- onClick={onClick}
313
- className="flex size-6 items-center justify-center rounded text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
93
+ </PanelIconButton>
94
+ </>
95
+ }
96
+ banner={warning ? <PanelBanner>{warning}</PanelBanner> : null}
314
97
  >
315
- {children}
316
- </button>
98
+ <Section title="Palette">
99
+ {PALETTE_KEYS.map(({ key, label }) => (
100
+ <ColorField
101
+ key={key}
102
+ label={label}
103
+ value={draft.palette[key]}
104
+ onChange={(value) =>
105
+ update((d) => {
106
+ d.palette[key] = value;
107
+ }, `palette.${key}`)
108
+ }
109
+ />
110
+ ))}
111
+ </Section>
112
+
113
+ <Section title="Fonts">
114
+ {FONT_KEYS.map(({ key, label }) => (
115
+ <FontField
116
+ key={key}
117
+ label={label}
118
+ value={draft.fonts[key]}
119
+ presets={FONT_PRESETS}
120
+ onChange={(value) =>
121
+ update((d) => {
122
+ d.fonts[key] = value;
123
+ }, `fonts.${key}`)
124
+ }
125
+ />
126
+ ))}
127
+ </Section>
128
+
129
+ <Section title="Type scale">
130
+ {TYPE_KEYS.map(({ key, label, min, max }) => (
131
+ <SliderField
132
+ key={key}
133
+ label={label}
134
+ value={draft.typeScale[key]}
135
+ min={min}
136
+ max={max}
137
+ suffix="px"
138
+ onChange={(value) =>
139
+ update((d) => {
140
+ d.typeScale[key] = value;
141
+ }, `typeScale.${key}`)
142
+ }
143
+ />
144
+ ))}
145
+ {draft.typeScale.body < 12 && (
146
+ <p className="text-[11px] text-muted-foreground leading-relaxed">
147
+ Body under 12px prints below 9pt — hard to read on paper.
148
+ </p>
149
+ )}
150
+ </Section>
151
+
152
+ <Section title="Page">
153
+ <SliderField
154
+ label="Margin"
155
+ value={draft.margin}
156
+ min={32}
157
+ max={140}
158
+ step={2}
159
+ suffix="px"
160
+ onChange={(value) =>
161
+ update((d) => {
162
+ d.margin = value;
163
+ }, 'margin')
164
+ }
165
+ />
166
+ <SliderField
167
+ label="Leading"
168
+ value={draft.leading}
169
+ min={1.2}
170
+ max={2}
171
+ step={0.05}
172
+ onChange={(value) =>
173
+ update((d) => {
174
+ d.leading = Number(value.toFixed(2));
175
+ }, 'leading')
176
+ }
177
+ />
178
+ <SliderField
179
+ label="Radius"
180
+ value={draft.radius}
181
+ min={0}
182
+ max={24}
183
+ suffix="px"
184
+ onChange={(value) =>
185
+ update((d) => {
186
+ d.radius = value;
187
+ }, 'radius')
188
+ }
189
+ />
190
+ </Section>
191
+ </PanelShell>
317
192
  );
318
193
  }
@@ -10,6 +10,7 @@ import {
10
10
  } from 'react';
11
11
  import { type DesignSystem, defaultDesign, designToCssVars } from '../../lib/design';
12
12
  import { shuffleDesign } from '../../lib/design-presets';
13
+ import { useHistory } from '../history-provider';
13
14
  import { useDesign as useDesignFetch } from './use-design';
14
15
 
15
16
  type DesignCtx = {
@@ -22,8 +23,9 @@ type DesignCtx = {
22
23
  dirty: boolean;
23
24
  committing: boolean;
24
25
  error: string | null;
25
- update: (mut: (next: DesignSystem) => void) => void;
26
- commit: () => Promise<void>;
26
+ /** `coalesceKey` folds a run of changes to one field — a slider drag — into one undo step. */
27
+ update: (mut: (next: DesignSystem) => void, coalesceKey?: string) => void;
28
+ commit: () => Promise<{ ok: boolean; error?: string }>;
27
29
  discard: () => void;
28
30
  resetToDefaults: () => void;
29
31
  shuffle: () => void;
@@ -46,6 +48,7 @@ export function DesignProvider({ docId, children }: { docId: string; children: R
46
48
  const [draft, setDraft] = useState<DesignSystem | null>(null);
47
49
  const [committing, setCommitting] = useState(false);
48
50
  const [error, setError] = useState<string | null>(null);
51
+ const history = useHistory();
49
52
  const draftRef = useRef<DesignSystem | null>(null);
50
53
  draftRef.current = draft;
51
54
 
@@ -58,29 +61,46 @@ export function DesignProvider({ docId, children }: { docId: string; children: R
58
61
  return JSON.stringify(draft) !== JSON.stringify(design);
59
62
  }, [draft, design]);
60
63
 
61
- const update = useCallback((mut: (next: DesignSystem) => void) => {
62
- const prev = draftRef.current;
63
- if (!prev) return;
64
- const next = clone(prev);
65
- mut(next);
66
- setDraft(next);
67
- }, []);
64
+ /** Every change to the draft goes through here, so every change can be undone. */
65
+ const change = useCallback(
66
+ (next: DesignSystem, coalesceKey?: string) => {
67
+ const prev = draftRef.current;
68
+ if (!prev) return;
69
+ setDraft(next);
70
+ history.record({ coalesceKey, undo: () => setDraft(prev), redo: () => setDraft(next) });
71
+ },
72
+ [history],
73
+ );
74
+
75
+ const update = useCallback(
76
+ (mut: (next: DesignSystem) => void, coalesceKey?: string) => {
77
+ const prev = draftRef.current;
78
+ if (!prev) return;
79
+ const next = clone(prev);
80
+ mut(next);
81
+ change(next, coalesceKey);
82
+ },
83
+ [change],
84
+ );
68
85
 
69
86
  const commit = useCallback(async () => {
70
- if (!draft) return;
87
+ const current = draftRef.current;
88
+ if (!current) return { ok: true };
71
89
  setCommitting(true);
72
- const result = await save(draft);
90
+ const result = await save(current);
73
91
  setCommitting(false);
74
- setError(result.ok ? null : (result.error ?? 'Failed to save'));
75
- }, [draft, save]);
92
+ const error = result.ok ? null : (result.error ?? 'Failed to save');
93
+ setError(error);
94
+ return error ? { ok: false, error } : { ok: true };
95
+ }, [save]);
76
96
 
77
97
  const discard = useCallback(() => {
78
98
  if (design) setDraft(clone(design));
79
99
  setError(null);
80
100
  }, [design]);
81
101
 
82
- const resetToDefaults = useCallback(() => setDraft(clone(defaultDesign)), []);
83
- const shuffle = useCallback(() => setDraft(clone(shuffleDesign(draftRef.current))), []);
102
+ const resetToDefaults = useCallback(() => change(clone(defaultDesign)), [change]);
103
+ const shuffle = useCallback(() => change(clone(shuffleDesign(draftRef.current))), [change]);
84
104
 
85
105
  // PageFrame writes its design vars inline on each page root, so the draft
86
106
  // overlay has to outrank inline styles — hence `!important`.
@@ -11,11 +11,13 @@ import {
11
11
  useState,
12
12
  } from 'react';
13
13
  import { LABEL_ATTR, LABEL_ID_ATTR, useDocLabel, useLabelVocabulary } from '../lib/labels';
14
+ import { LOC_PROP, sourceAttrs } from '../lib/source-loc';
14
15
 
15
16
  /** Stands in for a number the scan has not produced yet, or never will. */
16
17
  const UNNUMBERED = '\u2022';
17
18
 
18
- export type CollectedNote = { id: string; content: ReactNode };
19
+ /** `loc` is the `<Footnote>` call site, so the printed note can be edited where it prints. */
20
+ export type CollectedNote = { id: string; content: ReactNode; loc?: string };
19
21
 
20
22
  type Collector = {
21
23
  /** Registration order, which is document order. */
@@ -28,7 +30,8 @@ type Collector = {
28
30
  * from here in whatever render follows.
29
31
  */
30
32
  contents: Map<string, ReactNode>;
31
- register: (id: string, content: ReactNode) => void;
33
+ locs: Map<string, string>;
34
+ register: (id: string, content: ReactNode, loc?: string) => void;
32
35
  unregister: (id: string) => void;
33
36
  };
34
37
 
@@ -51,22 +54,26 @@ const FootnoteContext = g[GLOBAL_KEY];
51
54
  */
52
55
  export function FootnoteCollector({ children }: { children?: ReactNode }) {
53
56
  const contents = useRef<Map<string, ReactNode>>(new Map()).current;
57
+ const locs = useRef<Map<string, string>>(new Map()).current;
54
58
  const [ids, setIds] = useState<string[]>([]);
55
59
 
56
60
  const value = useMemo<Collector>(
57
61
  () => ({
58
62
  ids,
59
63
  contents,
60
- register: (id, content) => {
64
+ locs,
65
+ register: (id, content, loc) => {
61
66
  contents.set(id, content);
67
+ if (loc) locs.set(id, loc);
62
68
  setIds((current) => (current.includes(id) ? current : [...current, id]));
63
69
  },
64
70
  unregister: (id) => {
65
71
  contents.delete(id);
72
+ locs.delete(id);
66
73
  setIds((current) => (current.includes(id) ? current.filter((x) => x !== id) : current));
67
74
  },
68
75
  }),
69
- [ids, contents],
76
+ [ids, contents, locs],
70
77
  );
71
78
 
72
79
  return <FootnoteContext.Provider value={value}>{children}</FootnoteContext.Provider>;
@@ -78,6 +85,8 @@ export type FootnoteProps = {
78
85
  /** Stable id, so `<Ref to>` can point at the note. Generated when omitted. */
79
86
  id?: string;
80
87
  children?: ReactNode;
88
+ /** Stamped in dev by the loc-tags plugin; see `lib/source-loc.ts`. */
89
+ [LOC_PROP]?: string;
81
90
  };
82
91
 
83
92
  export function markerStyle(): CSSProperties {
@@ -114,7 +123,7 @@ export function FootnoteMarker({ id }: { id: string }) {
114
123
  * framework lifts it to the foot of whatever page the marker lands on; on a
115
124
  * fixed page, put a `<Footnotes />` where you want them printed.
116
125
  */
117
- export function Footnote({ id, children }: FootnoteProps) {
126
+ export function Footnote({ id, children, [LOC_PROP]: loc }: FootnoteProps) {
118
127
  const generated = useId();
119
128
  const noteId = id ?? generated;
120
129
  const collector = useContext(FootnoteContext);
@@ -124,7 +133,7 @@ export function Footnote({ id, children }: FootnoteProps) {
124
133
  // state, so this settles instead of looping.
125
134
  useEffect(() => {
126
135
  latest.current = collector;
127
- collector?.register(noteId, children);
136
+ collector?.register(noteId, children, loc);
128
137
  });
129
138
 
130
139
  useEffect(() => () => latest.current?.unregister(noteId), [noteId]);
@@ -158,7 +167,7 @@ export function footnoteAreaStyle(): CSSProperties {
158
167
  }
159
168
 
160
169
  /** One note's row. Shared with the measurement pass so the reserved space is real. */
161
- export function FootnoteRow({ id, content }: CollectedNote) {
170
+ export function FootnoteRow({ id, content, loc }: CollectedNote) {
162
171
  const entry = useDocLabel(id);
163
172
  return (
164
173
  <div {...{ [FOOTNOTE_ROW_ATTR]: '' }} style={{ display: 'flex', gap: 5, marginBottom: 3 }}>
@@ -171,7 +180,9 @@ export function FootnoteRow({ id, content }: CollectedNote) {
171
180
  >
172
181
  {entry?.number ?? UNNUMBERED}
173
182
  </span>
174
- <span style={{ minWidth: 0 }}>{content}</span>
183
+ <span {...sourceAttrs(loc)} style={{ minWidth: 0 }}>
184
+ {content}
185
+ </span>
175
186
  </div>
176
187
  );
177
188
  }
@@ -180,7 +191,13 @@ export function Footnotes({ notes, style, className }: FootnotesProps) {
180
191
  const collector = useContext(FootnoteContext);
181
192
  const vocabulary = useLabelVocabulary();
182
193
  const list =
183
- notes ?? collector?.ids.map((id) => ({ id, content: collector.contents.get(id) })) ?? [];
194
+ notes ??
195
+ collector?.ids.map((id) => ({
196
+ id,
197
+ content: collector.contents.get(id),
198
+ loc: collector.locs.get(id),
199
+ })) ??
200
+ [];
184
201
  if (list.length === 0) return null;
185
202
 
186
203
  return (
@@ -189,7 +206,7 @@ export function Footnotes({ notes, style, className }: FootnotesProps) {
189
206
  <div style={{ fontWeight: 600, marginBottom: 3 }}>{vocabulary.footnotes}</div>
190
207
  ) : null}
191
208
  {list.map((note) => (
192
- <FootnoteRow key={note.id} id={note.id} content={note.content} />
209
+ <FootnoteRow key={note.id} id={note.id} content={note.content} loc={note.loc} />
193
210
  ))}
194
211
  </div>
195
212
  );