@open-document/core 0.6.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 (34) hide show
  1. package/dist/{build-BvIHtePO.js → build-CFL7nhz0.js} +1 -1
  2. package/dist/{check-BqEdVH7G.js → check-pEQh4jJE.js} +3 -3
  3. package/dist/cli/bin.js +6 -6
  4. package/dist/{config-B7vFf9qZ.js → config-4nz5NnxW.js} +593 -137
  5. package/dist/{context-BaUE_aqU.js → context-BH8C-dSW.js} +1 -1
  6. package/dist/{dev-DXG-Dg-y.js → dev-BCSUczXW.js} +1 -1
  7. package/dist/{export-CT1Axnjv.js → export-BQ-GvMsb.js} +3 -3
  8. package/dist/{import-C8ZC4R6T.js → import-Daat3L2J.js} +2 -2
  9. package/dist/index.d.ts +21 -3
  10. package/dist/index.js +40 -9
  11. package/dist/ops/index.d.ts +47 -3
  12. package/dist/ops/index.js +3 -3
  13. package/dist/{ops-ByclxTfk.js → ops-DQfckiyp.js} +2 -45
  14. package/dist/{preview-CdmWbaHy.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 +1145 -208
  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/lib/footnotes.ts +6 -3
  29. package/src/app/lib/href.ts +14 -0
  30. package/src/app/lib/inspector/inline-edit.test.ts +244 -0
  31. package/src/app/lib/inspector/inline-edit.ts +770 -0
  32. package/src/app/lib/source-loc.ts +15 -0
  33. package/src/app/routes/doc.tsx +109 -39
  34. package/src/app/styles.css +41 -0
@@ -10,6 +10,7 @@ import {
10
10
  useLabelVocabulary,
11
11
  } from '../lib/labels';
12
12
  import { useDocPageNumber } from '../lib/page-context';
13
+ import { LOC_PROP, sourceAttrs } from '../lib/source-loc';
13
14
 
14
15
  function nameFor(kind: LabelKind, vocabulary: { figure: string; table: string }): string {
15
16
  return kind === 'table' ? vocabulary.table : vocabulary.figure;
@@ -30,6 +31,8 @@ export type FigureProps = {
30
31
  captionStyle?: CSSProperties;
31
32
  className?: string;
32
33
  children?: ReactNode;
34
+ /** Stamped in dev by the loc-tags plugin; see `lib/source-loc.ts`. */
35
+ [LOC_PROP]?: string;
33
36
  };
34
37
 
35
38
  /**
@@ -50,6 +53,7 @@ export function Figure({
50
53
  captionStyle,
51
54
  className,
52
55
  children,
56
+ [LOC_PROP]: loc,
53
57
  }: FigureProps) {
54
58
  const generated = useId();
55
59
  const labelId = id ?? generated;
@@ -74,7 +78,14 @@ export function Figure({
74
78
  <span style={{ fontWeight: 600, color: 'var(--od-text)' }}>
75
79
  {nameFor(kind, vocabulary)} {entry?.number ?? ''}
76
80
  </span>
77
- {caption ? <span> — {caption}</span> : null}
81
+ {typeof caption === 'string' || typeof caption === 'number' ? (
82
+ <span>
83
+ {' — '}
84
+ <span {...sourceAttrs(loc, 'caption')}>{caption}</span>
85
+ </span>
86
+ ) : caption ? (
87
+ <span> — {caption}</span>
88
+ ) : null}
78
89
  </figcaption>
79
90
  ) : null;
80
91
 
@@ -84,6 +95,7 @@ export function Figure({
84
95
  [LABEL_ATTR]: kind,
85
96
  [LABEL_ID_ATTR]: labelId,
86
97
  [LABEL_TEXT_ATTR]: text,
98
+ ...sourceAttrs(loc),
87
99
  }}
88
100
  className={className}
89
101
  style={{ margin: '0 0 16px', ...style }}
@@ -0,0 +1,112 @@
1
+ import { type MutableRefObject, useCallback, useEffect, useState } from 'react';
2
+ import { useDesignPanelState } from '../design-panel/design-provider';
3
+ import { useHistory } from '../history-provider';
4
+ import type { InspectorControls } from '../inspector/inspector';
5
+ import { SaveCard } from './save-card';
6
+
7
+ function isTypingTarget(target: EventTarget | null): boolean {
8
+ return (
9
+ target instanceof Element &&
10
+ target.closest('input, textarea, select, [contenteditable="true"], [contenteditable=""]') !==
11
+ null
12
+ );
13
+ }
14
+
15
+ /**
16
+ * Joins the two things that can be unsaved in the document view — text edited
17
+ * on the page and the design draft — into one card, one ⌘S, and one history.
18
+ *
19
+ * Both write the same `index.tsx`, each as a read-modify-write on the server,
20
+ * so they are saved one after the other and never at once: two writes in
21
+ * flight would each start from the file before the other, and the second
22
+ * would put back what the first changed.
23
+ */
24
+ export function EditSaveCard({
25
+ textCount,
26
+ controlsRef,
27
+ onShownChange,
28
+ }: {
29
+ textCount: number;
30
+ controlsRef: MutableRefObject<InspectorControls | null>;
31
+ onShownChange: (shown: boolean) => void;
32
+ }) {
33
+ const design = useDesignPanelState();
34
+ const history = useHistory();
35
+ const [committing, setCommitting] = useState(false);
36
+ const [error, setError] = useState<string | null>(null);
37
+ const count = textCount + (design.dirty ? 1 : 0);
38
+
39
+ const save = useCallback(async (): Promise<boolean> => {
40
+ if (committing) return false;
41
+ setCommitting(true);
42
+ setError(null);
43
+ try {
44
+ const text =
45
+ textCount > 0 && controlsRef.current ? await controlsRef.current.save() : { ok: true };
46
+ const tokens = design.dirty ? await design.commit() : { ok: true };
47
+ const failure = text.ok ? tokens : text;
48
+ if (!failure.ok) {
49
+ setError(failure.error ?? 'Not everything was saved');
50
+ return false;
51
+ }
52
+ history.clear();
53
+ return true;
54
+ } finally {
55
+ setCommitting(false);
56
+ }
57
+ }, [committing, textCount, controlsRef, design, history]);
58
+
59
+ const discard = useCallback(() => {
60
+ controlsRef.current?.discard();
61
+ design.discard();
62
+ history.clear();
63
+ setError(null);
64
+ }, [controlsRef, design, history]);
65
+
66
+ useEffect(() => {
67
+ const onKey = (e: KeyboardEvent) => {
68
+ if (!(e.metaKey || e.ctrlKey) || e.altKey || e.isComposing) return;
69
+ const key = e.key.toLowerCase();
70
+ if (key === 's') {
71
+ if (count === 0) return;
72
+ e.preventDefault();
73
+ void save();
74
+ return;
75
+ }
76
+ // Inside a field — the text being edited on the page, a hex, a number —
77
+ // undo belongs to the field.
78
+ if (isTypingTarget(e.target)) return;
79
+ if (key === 'z' || (key === 'y' && e.ctrlKey)) {
80
+ e.preventDefault();
81
+ if (e.shiftKey || key === 'y') history.redo();
82
+ else history.undo();
83
+ }
84
+ };
85
+ window.addEventListener('keydown', onKey, true);
86
+ return () => window.removeEventListener('keydown', onKey, true);
87
+ }, [count, save, history]);
88
+
89
+ useEffect(() => {
90
+ if (!design.dirty) return;
91
+ const onBeforeUnload = (e: BeforeUnloadEvent) => e.preventDefault();
92
+ window.addEventListener('beforeunload', onBeforeUnload);
93
+ return () => window.removeEventListener('beforeunload', onBeforeUnload);
94
+ }, [design.dirty]);
95
+
96
+ const shown = count > 0 || committing || history.canUndo || history.canRedo || error !== null;
97
+ useEffect(() => onShownChange(shown), [shown, onShownChange]);
98
+
99
+ return (
100
+ <SaveCard
101
+ count={count}
102
+ committing={committing || design.committing}
103
+ error={error}
104
+ canUndo={history.canUndo}
105
+ canRedo={history.canRedo}
106
+ onSave={save}
107
+ onDiscard={discard}
108
+ onUndo={history.undo}
109
+ onRedo={history.redo}
110
+ />
111
+ );
112
+ }
@@ -0,0 +1,247 @@
1
+ import { type ReactNode, useEffect, useId, useState } from 'react';
2
+
3
+ export function Section({ title, children }: { title: string; children: ReactNode }) {
4
+ return (
5
+ <section className="border-border border-b px-3 py-3 last:border-b-0">
6
+ <h3 className="mb-2 text-[10px] text-muted-foreground uppercase tracking-wider">{title}</h3>
7
+ <div className="flex flex-col gap-2">{children}</div>
8
+ </section>
9
+ );
10
+ }
11
+
12
+ export function Field({
13
+ label,
14
+ htmlFor,
15
+ children,
16
+ }: {
17
+ label: string;
18
+ htmlFor?: string;
19
+ children: ReactNode;
20
+ }) {
21
+ return (
22
+ <div className="grid grid-cols-[64px_1fr] items-center gap-2">
23
+ <label htmlFor={htmlFor} className="truncate text-[11px] text-muted-foreground">
24
+ {label}
25
+ </label>
26
+ <div className="flex min-w-0 items-center gap-1.5">{children}</div>
27
+ </div>
28
+ );
29
+ }
30
+
31
+ const INPUT_CLASS =
32
+ 'h-8 rounded border border-border bg-transparent px-2 text-xs outline-none focus:border-foreground/40 aria-invalid:border-foreground';
33
+
34
+ const HEX6 = /^#[0-9a-fA-F]{6}$/;
35
+
36
+ /**
37
+ * A swatch and its hex. Only a complete `#rrggbb` reaches the draft: anything
38
+ * else would be written into the preview CSS, and saved, as whatever was typed
39
+ * so far. A half-typed value goes back to the last good one when the field is
40
+ * left.
41
+ */
42
+ export function ColorField({
43
+ label,
44
+ value,
45
+ onChange,
46
+ }: {
47
+ label: string;
48
+ value: string;
49
+ onChange: (value: string) => void;
50
+ }) {
51
+ const id = useId();
52
+ const [draft, setDraft] = useState(value);
53
+ useEffect(() => setDraft(value), [value]);
54
+ const invalid = !HEX6.test(draft);
55
+
56
+ return (
57
+ <Field label={label} htmlFor={id}>
58
+ <span className="relative inline-flex size-8 flex-none items-center justify-center rounded border border-border has-[:focus-visible]:outline-2 has-[:focus-visible]:outline-foreground/60">
59
+ <span className="size-5 rounded-sm border border-border" style={{ background: value }} />
60
+ <input
61
+ type="color"
62
+ aria-label={`${label} swatch`}
63
+ value={HEX6.test(value) ? value : '#000000'}
64
+ onChange={(e) => onChange(e.target.value)}
65
+ className="absolute inset-0 cursor-pointer opacity-0"
66
+ />
67
+ </span>
68
+ <input
69
+ id={id}
70
+ type="text"
71
+ spellCheck={false}
72
+ value={draft}
73
+ aria-invalid={invalid}
74
+ onChange={(e) => {
75
+ const next = e.target.value.trim();
76
+ setDraft(next);
77
+ if (HEX6.test(next)) onChange(next.toLowerCase());
78
+ }}
79
+ onBlur={() => {
80
+ if (!HEX6.test(draft)) setDraft(value);
81
+ }}
82
+ className={`${INPUT_CLASS} min-w-0 flex-1 font-mono uppercase tabular-nums`}
83
+ />
84
+ </Field>
85
+ );
86
+ }
87
+
88
+ function decimalsOf(step: number): number {
89
+ const text = String(step);
90
+ return text.includes('.') ? (text.split('.')[1] as string).length : 0;
91
+ }
92
+
93
+ /**
94
+ * A number typed or stepped with the arrow keys, kept inside its range and to
95
+ * its step's precision. It commits on Enter or when the field is left, not on
96
+ * every keystroke — typing `1` on the way to `14` would otherwise apply a
97
+ * 1px heading for a moment.
98
+ */
99
+ export function NumberField({
100
+ label,
101
+ value,
102
+ min,
103
+ max,
104
+ step = 1,
105
+ suffix,
106
+ onChange,
107
+ }: {
108
+ label: string;
109
+ value: number;
110
+ min: number;
111
+ max: number;
112
+ step?: number;
113
+ suffix?: string;
114
+ onChange: (value: number) => void;
115
+ }) {
116
+ const [draft, setDraft] = useState(String(value));
117
+ useEffect(() => setDraft(String(value)), [value]);
118
+ const places = decimalsOf(step);
119
+ const settle = (raw: number) => {
120
+ const clamped = Math.min(max, Math.max(min, raw));
121
+ return Number(clamped.toFixed(places));
122
+ };
123
+ const commit = () => {
124
+ const parsed = Number.parseFloat(draft);
125
+ if (!Number.isFinite(parsed)) {
126
+ setDraft(String(value));
127
+ return;
128
+ }
129
+ const next = settle(parsed);
130
+ setDraft(String(next));
131
+ if (next !== value) onChange(next);
132
+ };
133
+
134
+ return (
135
+ <span className="relative flex-none">
136
+ <input
137
+ type="text"
138
+ inputMode="decimal"
139
+ aria-label={label}
140
+ value={draft}
141
+ onChange={(e) => setDraft(e.target.value)}
142
+ onBlur={commit}
143
+ onKeyDown={(e) => {
144
+ if (e.key === 'Enter') {
145
+ e.preventDefault();
146
+ commit();
147
+ } else if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
148
+ e.preventDefault();
149
+ const next = settle(
150
+ value + (e.key === 'ArrowUp' ? step : -step) * (e.shiftKey ? 10 : 1),
151
+ );
152
+ if (next !== value) onChange(next);
153
+ }
154
+ }}
155
+ className={`${INPUT_CLASS} w-16 pr-6 text-right font-mono tabular-nums`}
156
+ />
157
+ {suffix && (
158
+ <span
159
+ aria-hidden
160
+ className="pointer-events-none absolute top-1/2 right-2 -translate-y-1/2 text-[10px] text-muted-foreground"
161
+ >
162
+ {suffix}
163
+ </span>
164
+ )}
165
+ </span>
166
+ );
167
+ }
168
+
169
+ export function SliderField({
170
+ label,
171
+ value,
172
+ min,
173
+ max,
174
+ step = 1,
175
+ suffix,
176
+ onChange,
177
+ }: {
178
+ label: string;
179
+ value: number;
180
+ min: number;
181
+ max: number;
182
+ step?: number;
183
+ suffix?: string;
184
+ onChange: (value: number) => void;
185
+ }) {
186
+ const id = useId();
187
+ return (
188
+ <Field label={label} htmlFor={id}>
189
+ <input
190
+ id={id}
191
+ type="range"
192
+ min={min}
193
+ max={max}
194
+ step={step}
195
+ value={value}
196
+ onChange={(e) => onChange(Number(e.target.value))}
197
+ className="h-8 min-w-0 flex-1 cursor-pointer accent-foreground"
198
+ />
199
+ <NumberField
200
+ label={label}
201
+ value={value}
202
+ min={min}
203
+ max={max}
204
+ step={step}
205
+ suffix={suffix}
206
+ onChange={onChange}
207
+ />
208
+ </Field>
209
+ );
210
+ }
211
+
212
+ export type FontPreset = { label: string; value: string };
213
+
214
+ /** A preset, or "Custom" — shown only while the source holds a stack no preset matches. */
215
+ export function FontField({
216
+ label,
217
+ value,
218
+ presets,
219
+ onChange,
220
+ }: {
221
+ label: string;
222
+ value: string;
223
+ presets: FontPreset[];
224
+ onChange: (value: string) => void;
225
+ }) {
226
+ const id = useId();
227
+ const matched = presets.find((preset) => preset.value === value);
228
+ return (
229
+ <Field label={label} htmlFor={id}>
230
+ <select
231
+ id={id}
232
+ value={matched ? matched.value : '__custom__'}
233
+ onChange={(e) => {
234
+ if (e.target.value !== '__custom__') onChange(e.target.value);
235
+ }}
236
+ className={`${INPUT_CLASS} min-w-0 flex-1`}
237
+ >
238
+ {presets.map((preset) => (
239
+ <option key={preset.label} value={preset.value}>
240
+ {preset.label}
241
+ </option>
242
+ ))}
243
+ {!matched && <option value="__custom__">Custom (from source)</option>}
244
+ </select>
245
+ </Field>
246
+ );
247
+ }
@@ -0,0 +1,71 @@
1
+ import type { ReactNode } from 'react';
2
+
3
+ /**
4
+ * The right-hand dock's chrome, shared by the design panel and the element
5
+ * panel so switching between them changes the content, not the frame. The dock
6
+ * does not animate its width — that would reflow the pages on every frame —
7
+ * so only the content fades in, from the edge it docks against.
8
+ */
9
+ export function PanelShell({
10
+ label,
11
+ header,
12
+ actions,
13
+ banner,
14
+ children,
15
+ panelRef,
16
+ }: {
17
+ /** The accessible name of the region. */
18
+ label: string;
19
+ header: ReactNode;
20
+ actions?: ReactNode;
21
+ banner?: ReactNode;
22
+ children: ReactNode;
23
+ panelRef?: React.Ref<HTMLElement>;
24
+ }) {
25
+ return (
26
+ <aside
27
+ ref={panelRef}
28
+ aria-label={label}
29
+ className="od-panel-in flex w-72 flex-none flex-col border-border border-l bg-background"
30
+ >
31
+ <header className="flex h-10 flex-none items-center justify-between gap-2 border-border border-b px-3">
32
+ <div className="flex min-w-0 items-center gap-2">{header}</div>
33
+ {actions && <div className="flex flex-none items-center gap-0.5">{actions}</div>}
34
+ </header>
35
+ {banner}
36
+ <div className="min-h-0 flex-1 overflow-y-auto">{children}</div>
37
+ </aside>
38
+ );
39
+ }
40
+
41
+ export function PanelIconButton({
42
+ label,
43
+ onClick,
44
+ children,
45
+ }: {
46
+ label: string;
47
+ onClick: () => void;
48
+ children: ReactNode;
49
+ }) {
50
+ return (
51
+ <button
52
+ type="button"
53
+ aria-label={label}
54
+ title={label}
55
+ onClick={onClick}
56
+ className="flex size-8 items-center justify-center rounded text-foreground/70 transition-colors hover:bg-accent hover:text-foreground focus-visible:outline-2 focus-visible:outline-foreground/60 focus-visible:outline-offset-1"
57
+ >
58
+ {children}
59
+ </button>
60
+ );
61
+ }
62
+
63
+ /** A quiet line under the header: something about the panel as a whole. */
64
+ export function PanelBanner({ children }: { children: ReactNode }) {
65
+ return (
66
+ <div className="flex gap-2 border-border border-b bg-muted px-3 py-2 text-[11px] text-muted-foreground leading-relaxed">
67
+ <span aria-hidden className="mt-1.5 size-1.5 flex-none rounded-full bg-foreground/40" />
68
+ <span>{children}</span>
69
+ </div>
70
+ );
71
+ }
@@ -0,0 +1,146 @@
1
+ import { Check, Loader2, Redo2, Save, Undo2 } from 'lucide-react';
2
+ import { useEffect, useState } from 'react';
3
+
4
+ const IS_APPLE = typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform);
5
+ const UNDO_KEYS = IS_APPLE ? '⌘Z' : 'Ctrl+Z';
6
+ const REDO_KEYS = IS_APPLE ? '⇧⌘Z' : 'Ctrl+Y';
7
+ const SAVE_KEYS = IS_APPLE ? '⌘S' : 'Ctrl+S';
8
+
9
+ /**
10
+ * The one place that says what is unsaved in the document view — text edited
11
+ * on the page and design tokens alike — with undo and redo beside it. Both
12
+ * previews make the page *look* saved; without this, closing the tab would
13
+ * quietly lose them.
14
+ */
15
+ export function SaveCard({
16
+ count,
17
+ committing,
18
+ error,
19
+ canUndo,
20
+ canRedo,
21
+ onSave,
22
+ onDiscard,
23
+ onUndo,
24
+ onRedo,
25
+ }: {
26
+ count: number;
27
+ committing: boolean;
28
+ error: string | null;
29
+ canUndo: boolean;
30
+ canRedo: boolean;
31
+ /** Resolves true when everything was written. */
32
+ onSave: () => Promise<boolean>;
33
+ onDiscard: () => void;
34
+ onUndo: () => void;
35
+ onRedo: () => void;
36
+ }) {
37
+ const [justSaved, setJustSaved] = useState(false);
38
+ useEffect(() => {
39
+ if (!justSaved) return;
40
+ const timer = setTimeout(() => setJustSaved(false), 1200);
41
+ return () => clearTimeout(timer);
42
+ }, [justSaved]);
43
+
44
+ const dirty = count > 0;
45
+ if (!(dirty || committing || justSaved || canUndo || canRedo || error)) return null;
46
+
47
+ const save = async () => {
48
+ if (await onSave()) setJustSaved(true);
49
+ };
50
+
51
+ return (
52
+ <div
53
+ role="toolbar"
54
+ aria-label="Unsaved changes"
55
+ className="od-card-in pointer-events-auto absolute bottom-4 left-1/2 z-40 flex h-10 -translate-x-1/2 items-center gap-1 rounded-lg border border-border bg-background py-1 pr-1 pl-1 text-xs shadow-md"
56
+ >
57
+ <HistoryButton
58
+ label={`Undo (${UNDO_KEYS})`}
59
+ disabled={committing || !canUndo}
60
+ onClick={onUndo}
61
+ >
62
+ <Undo2 className="size-3.5" />
63
+ </HistoryButton>
64
+ <HistoryButton
65
+ label={`Redo (${REDO_KEYS})`}
66
+ disabled={committing || !canRedo}
67
+ onClick={onRedo}
68
+ >
69
+ <Redo2 className="size-3.5" />
70
+ </HistoryButton>
71
+ {(justSaved || dirty || committing || error) && (
72
+ <span aria-hidden className="mx-1 h-4 w-px bg-border" />
73
+ )}
74
+ <span role="status" className="flex items-center gap-1.5 whitespace-nowrap px-1.5">
75
+ {justSaved ? (
76
+ <>
77
+ <Check className="size-3.5 flex-none" strokeWidth={2.5} />
78
+ Saved
79
+ </>
80
+ ) : error ? (
81
+ <span className="max-w-72 truncate text-muted-foreground" title={error}>
82
+ {error}
83
+ </span>
84
+ ) : dirty || committing ? (
85
+ <>
86
+ <span aria-hidden className="size-1.5 flex-none rounded-full bg-foreground" />
87
+ <span className="tabular-nums">
88
+ {count} unsaved change{count === 1 ? '' : 's'}
89
+ </span>
90
+ </>
91
+ ) : null}
92
+ </span>
93
+ {!justSaved && dirty && (
94
+ <button
95
+ type="button"
96
+ onClick={onDiscard}
97
+ disabled={committing}
98
+ className="h-8 rounded-md px-3 text-foreground/80 transition-colors hover:bg-accent hover:text-foreground focus-visible:outline-2 focus-visible:outline-foreground/60 focus-visible:outline-offset-1 disabled:opacity-50"
99
+ >
100
+ Discard
101
+ </button>
102
+ )}
103
+ {(dirty || committing) && (
104
+ <button
105
+ type="button"
106
+ onClick={() => void save()}
107
+ disabled={committing}
108
+ title={`Save (${SAVE_KEYS})`}
109
+ className="flex h-8 items-center gap-1.5 rounded-md bg-primary px-3 text-primary-foreground transition-opacity hover:opacity-90 focus-visible:outline-2 focus-visible:outline-foreground/60 focus-visible:outline-offset-1 disabled:opacity-70"
110
+ >
111
+ {committing ? (
112
+ <Loader2 className="size-3.5 animate-spin motion-reduce:animate-none" />
113
+ ) : (
114
+ <Save className="size-3.5" />
115
+ )}
116
+ {committing ? 'Saving' : 'Save'}
117
+ </button>
118
+ )}
119
+ </div>
120
+ );
121
+ }
122
+
123
+ function HistoryButton({
124
+ label,
125
+ disabled,
126
+ onClick,
127
+ children,
128
+ }: {
129
+ label: string;
130
+ disabled: boolean;
131
+ onClick: () => void;
132
+ children: React.ReactNode;
133
+ }) {
134
+ return (
135
+ <button
136
+ type="button"
137
+ aria-label={label.replace(/ \(.*\)$/, '')}
138
+ title={label}
139
+ disabled={disabled}
140
+ onClick={onClick}
141
+ className="flex size-8 items-center justify-center rounded-md text-foreground/75 transition-colors hover:bg-accent hover:text-foreground focus-visible:outline-2 focus-visible:outline-foreground/60 focus-visible:outline-offset-1 disabled:text-foreground/30 disabled:hover:bg-transparent"
142
+ >
143
+ {children}
144
+ </button>
145
+ );
146
+ }
@@ -1,7 +1,9 @@
1
1
  import { Children, cloneElement, createElement, isValidElement, type ReactNode } from 'react';
2
2
  import { FOOTNOTE_MARKER_FLAG, FootnoteMarker } from '../components/footnote';
3
+ import { LOC_PROP } from './source-loc';
3
4
 
4
- export type ExtractedNote = { id: string; content: ReactNode };
5
+ /** `loc` is the `<Footnote>` call site, carried so the printed note can be edited. */
6
+ export type ExtractedNote = { id: string; content: ReactNode; loc?: string };
5
7
 
6
8
  export type ExtractedBlock = {
7
9
  /** The block with each `<Footnote>` swapped for its marker. */
@@ -47,9 +49,10 @@ export function extractBlockFootnotes(node: ReactNode, idPrefix: string): Extrac
47
49
  if (!isValidElement(current)) return current;
48
50
 
49
51
  if (isFootnoteElement(current)) {
50
- const props = current.props as { id?: string; children?: ReactNode };
52
+ const props = current.props as { id?: string; children?: ReactNode; [LOC_PROP]?: string };
51
53
  const id = props.id ?? `${idPrefix}-${notes.length + 1}`;
52
- notes.push({ id, content: props.children });
54
+ const loc = props[LOC_PROP];
55
+ notes.push({ id, content: props.children, ...(loc ? { loc } : {}) });
53
56
  return createElement(FootnoteMarker, { key: current.key ?? id, id });
54
57
  }
55
58
 
@@ -0,0 +1,14 @@
1
+ const LINK_SCHEME = /^\s*([a-z][a-z0-9+.-]*):/i;
2
+
3
+ /**
4
+ * A link survives into the exported HTML, where a `javascript:` URL would run.
5
+ * Web, mail and phone addresses and in-document or relative paths only. The
6
+ * page editor checks before it offers the link; the server checks again before
7
+ * it writes one.
8
+ */
9
+ export function isSafeHref(href: string): boolean {
10
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are what is being refused
11
+ if (/[\u0000-\u001f]/.test(href) || href.trim() === '') return false;
12
+ const scheme = href.match(LINK_SCHEME)?.[1]?.toLowerCase();
13
+ return scheme === undefined || ['http', 'https', 'mailto', 'tel'].includes(scheme);
14
+ }