@open-document/core 0.1.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 (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +50 -0
  3. package/README.zh-TW.md +50 -0
  4. package/bin.js +2 -0
  5. package/dist/build-BWZJ1gvL.js +18 -0
  6. package/dist/cli/bin.d.ts +1 -0
  7. package/dist/cli/bin.js +86 -0
  8. package/dist/config-DBp4mOOf.js +1121 -0
  9. package/dist/config-EBFfKtWH.d.ts +17 -0
  10. package/dist/design-BJLvOcCO.js +53 -0
  11. package/dist/dev-CZtw1oqs.js +24 -0
  12. package/dist/edit-ops-DSdUroVO.js +1225 -0
  13. package/dist/index.d.ts +206 -0
  14. package/dist/index.js +240 -0
  15. package/dist/ops/index.d.ts +160 -0
  16. package/dist/ops/index.js +202 -0
  17. package/dist/preview-DUfQl7Ij.js +23 -0
  18. package/dist/sync-6GTPAt2t.js +86 -0
  19. package/dist/sync-C6CsPmyK.js +3 -0
  20. package/dist/vite/index.d.ts +22 -0
  21. package/dist/vite/index.js +5 -0
  22. package/env.d.ts +51 -0
  23. package/package.json +91 -0
  24. package/skills/apply-comments/SKILL.md +74 -0
  25. package/skills/create-doc/SKILL.md +102 -0
  26. package/skills/create-theme/SKILL.md +184 -0
  27. package/skills/doc-authoring/SKILL.md +354 -0
  28. package/skills/doc-authoring/references/assets.md +47 -0
  29. package/skills/doc-authoring/references/design-system.md +81 -0
  30. package/skills/doc-authoring/references/pagination.md +117 -0
  31. package/skills/doc-authoring/references/tables-and-charts.md +161 -0
  32. package/src/app/app.tsx +42 -0
  33. package/src/app/components/design-panel/design-panel.tsx +318 -0
  34. package/src/app/components/design-panel/design-provider.tsx +121 -0
  35. package/src/app/components/design-panel/use-design.ts +85 -0
  36. package/src/app/components/doc-assets.tsx +129 -0
  37. package/src/app/components/doc-sidebar.tsx +162 -0
  38. package/src/app/components/flow-page.tsx +50 -0
  39. package/src/app/components/image-placeholder.tsx +50 -0
  40. package/src/app/components/inspector/inspector.tsx +426 -0
  41. package/src/app/components/page-frame.tsx +70 -0
  42. package/src/app/components/sidebar/folder-item.tsx +212 -0
  43. package/src/app/components/sidebar/icon-picker.tsx +99 -0
  44. package/src/app/components/sidebar/sidebar.tsx +252 -0
  45. package/src/app/components/table-of-contents.tsx +81 -0
  46. package/src/app/components/theme-toggle.tsx +50 -0
  47. package/src/app/components/themes/markdown.tsx +249 -0
  48. package/src/app/components/themes/theme-preview.tsx +74 -0
  49. package/src/app/components/ui/menu.tsx +139 -0
  50. package/src/app/index.html +12 -0
  51. package/src/app/lib/assets.ts +151 -0
  52. package/src/app/lib/design-presets.ts +109 -0
  53. package/src/app/lib/design.ts +88 -0
  54. package/src/app/lib/doc-preview.tsx +29 -0
  55. package/src/app/lib/docs.ts +26 -0
  56. package/src/app/lib/export-html.ts +309 -0
  57. package/src/app/lib/export-pdf.ts +166 -0
  58. package/src/app/lib/flow-measure.ts +109 -0
  59. package/src/app/lib/flow.test.ts +64 -0
  60. package/src/app/lib/flow.ts +111 -0
  61. package/src/app/lib/folders.ts +192 -0
  62. package/src/app/lib/inspector/fiber.ts +99 -0
  63. package/src/app/lib/outline.ts +109 -0
  64. package/src/app/lib/page-context.tsx +38 -0
  65. package/src/app/lib/print-ready.ts +69 -0
  66. package/src/app/lib/sdk.test.ts +24 -0
  67. package/src/app/lib/sdk.ts +86 -0
  68. package/src/app/lib/themes.ts +31 -0
  69. package/src/app/lib/use-doc-module.ts +53 -0
  70. package/src/app/lib/use-doc-pages.ts +99 -0
  71. package/src/app/lib/utils.ts +6 -0
  72. package/src/app/main.tsx +14 -0
  73. package/src/app/routes/assets.tsx +257 -0
  74. package/src/app/routes/doc.tsx +480 -0
  75. package/src/app/routes/home-shell.tsx +203 -0
  76. package/src/app/routes/home.tsx +269 -0
  77. package/src/app/routes/themes.tsx +121 -0
  78. package/src/app/styles.css +72 -0
  79. package/src/app/virtual.d.ts +30 -0
@@ -0,0 +1,117 @@
1
+ # Pagination — flow sections and the vertical budget
2
+
3
+ A document's default export is a list of **entries**. There are two kinds, and they mix freely:
4
+
5
+ | Entry | What it is | Who decides the page break |
6
+ | --- | --- | --- |
7
+ | `DocPage` (a component) | Exactly one sheet | You |
8
+ | `flow(<>…</>)` | Continuous content | The framework, by measuring |
9
+
10
+ **Default to `flow()` for body content.** Use fixed pages for the cover, a contents page, a section divider — anything whose layout is the point. Hand-splitting prose into fixed pages is how you end up with eleven pages that are each 60% full.
11
+
12
+ ## Flow sections
13
+
14
+ ```tsx
15
+ import { flow, type DocEntry } from '@open-document/core';
16
+
17
+ const Body = flow(
18
+ <>
19
+ <h1 style={h1}>1. 概觀</h1>
20
+ <p style={p}>…</p>
21
+ <Table>…</Table>
22
+ <p style={caption} data-od-keep-with-previous>表 1 — …</p>
23
+ <h2 style={h2}>1.1 細節</h2>
24
+ <p style={p}>…</p>
25
+ </>,
26
+ { footer: Footer },
27
+ );
28
+
29
+ export default [Cover, Contents, Body] satisfies DocEntry[];
30
+ ```
31
+
32
+ - **Each direct child of the fragment is one block.** Blocks are atomic: a block never splits across a page, so a table either fits whole or moves to the next page.
33
+ - The framework owns the page shell for flow pages — margin, background, and base typography come from the `design` const. Your blocks keep their own styles. `flow(node, { padding })` overrides the margin for that section.
34
+ - `footer` is a component rendered on **every** page the section expands into. `useDocPageNumber()` works inside it, so a running footer needs no extra wiring.
35
+ - Blocks are measured in the real DOM at the real page width after fonts settle, then packed greedily. Change a paragraph and the pagination re-runs on hot reload.
36
+
37
+ ### Keep rules
38
+
39
+ | Rule | How | Default |
40
+ | --- | --- | --- |
41
+ | Heading never last on a page | `data-od-keep-with-next` | **on** for `h1`–`h4` |
42
+ | Caption stays with its figure | `data-od-keep-with-previous` on the caption | off |
43
+ | Force a new page here | `data-od-break-before` | off |
44
+
45
+ Set `data-od-keep-with-next="false"` on a heading if you deliberately want it to end a page.
46
+
47
+ ### What flow does not do
48
+
49
+ - **It never splits a block.** A 40-row table taller than one page stays whole and overflows — split it yourself into two tables with a repeated header.
50
+ - **It does not balance pages.** Content fills top-down; the last page can be short.
51
+ - **It does not reorder anything.** Blocks keep the order you wrote them in.
52
+
53
+ ## Fixed pages and the vertical budget
54
+
55
+ Everything below applies to `DocPage` components — the pages you lay out yourself.
56
+
57
+ ```
58
+ usable_height = page_height − 2 × margin
59
+ ```
60
+
61
+ | Page | Margin | Usable height |
62
+ | --- | --- | --- |
63
+ | A4 portrait (1123px) | 76 | **971px** |
64
+ | A4 portrait | 96 | 931px |
65
+ | Letter portrait (1056px) | 76 | 904px |
66
+ | A4 landscape (794px) | 76 | 642px |
67
+
68
+ A running footer sits inside the margin band, so it costs nothing from the budget — but leave **24px of clearance** above it, i.e. treat the budget as ~947px on A4 when a footer is present.
69
+
70
+ ### Estimating how many lines a paragraph takes
71
+
72
+ ```
73
+ chars_per_line ≈ text_block_width / (font_size × 0.5)
74
+ lines = ceil(characters / chars_per_line)
75
+ height = lines × font_size × line_height
76
+ ```
77
+
78
+ The `0.5` is the average glyph-width ratio for a humanist sans at body sizes. Use `0.52` for serif faces, `0.6` for monospace, and **`1.0` for CJK** (one character per em).
79
+
80
+ On A4 with 76px margins (text block 642px) at 14px body:
81
+
82
+ - Latin: `642 / (14 × 0.5)` ≈ **92 characters per line**
83
+ - CJK: `642 / 14` ≈ **45 characters per line**
84
+
85
+ A 400-character English paragraph → `ceil(400/92)` = 5 lines → `5 × 14 × 1.55` = **109px**.
86
+
87
+ ### Worked example — A4 content page, 76px margins, footer present (budget 947px)
88
+
89
+ | Element | Height |
90
+ | --- | --- |
91
+ | H1 28px × 1.2 | 34px |
92
+ | Gap | 20px |
93
+ | Intro paragraph, 380 chars → 5 lines × 14 × 1.55 | 109px |
94
+ | Gap | 14px |
95
+ | H2 20px × 1.25 | 25px |
96
+ | Gap | 12px |
97
+ | Body paragraph, 620 chars → 7 lines | 152px |
98
+ | Gap | 14px |
99
+ | Table: header 28px + 6 rows × 26px | 184px |
100
+ | Gap | 14px |
101
+ | Caption 10px × 1.4 | 14px |
102
+ | Gap | 24px |
103
+ | Body paragraph, 520 chars → 6 lines | 130px |
104
+ | **Total** | **746px ✅ fits in 947** |
105
+
106
+ ### Where to split a fixed page
107
+
108
+ 1. **At a section boundary.** A new H1 starts a new page.
109
+ 2. **Before a subsection heading.** Never leave an H2 as the last element on a page.
110
+ 3. **Between a table/figure and its surrounding prose.** Keep a caption with its table.
111
+ 4. **Mid-prose, at a paragraph boundary.** Never split a paragraph across two page components.
112
+
113
+ Rules: give every page the same top edge; if a page is more than ~85% full, move its last block; if you find yourself shrinking type or margins to fit, add a page instead. **Or convert the section to `flow()` and stop doing this arithmetic.**
114
+
115
+ ## Landscape pages
116
+
117
+ `meta.orientation` applies to the **whole document** — there is no per-page orientation. If one wide table needs landscape, either rotate it inside a portrait page (`transform: rotate(-90deg)` on a sized wrapper) or make the whole document landscape. Mixing is not supported.
@@ -0,0 +1,161 @@
1
+ # Tables, stats, and charts
2
+
3
+ Reports are mostly evidence. This file covers the three shapes evidence takes on a page.
4
+
5
+ ## Tables
6
+
7
+ ```tsx
8
+ const Th = ({ children, align = 'left' }: { children: ReactNode; align?: 'left' | 'right' }) => (
9
+ <th
10
+ style={{
11
+ textAlign: align,
12
+ fontFamily: 'var(--od-font-heading)',
13
+ fontSize: 11,
14
+ fontWeight: 600,
15
+ letterSpacing: '0.04em',
16
+ textTransform: 'uppercase',
17
+ color: 'var(--od-muted)',
18
+ borderBottom: '1px solid var(--od-rule)',
19
+ padding: '0 8px 6px',
20
+ }}
21
+ >
22
+ {children}
23
+ </th>
24
+ );
25
+
26
+ const Td = ({ children, align = 'left' }: { children: ReactNode; align?: 'left' | 'right' }) => (
27
+ <td
28
+ style={{
29
+ textAlign: align,
30
+ fontSize: 12,
31
+ padding: '7px 8px',
32
+ borderBottom: '1px solid var(--od-rule)',
33
+ fontVariantNumeric: align === 'right' ? 'tabular-nums' : undefined,
34
+ }}
35
+ >
36
+ {children}
37
+ </td>
38
+ );
39
+
40
+ <table style={{ width: '100%', borderCollapse: 'collapse', tableLayout: 'fixed' }}>
41
+ <thead>
42
+ <tr>
43
+ <Th>Service</Th>
44
+ <Th align="right">Requests</Th>
45
+ <Th align="right">p99</Th>
46
+ </tr>
47
+ </thead>
48
+ <tbody>
49
+ <tr>
50
+ <Td>checkout-api</Td>
51
+ <Td align="right">18,402,111</Td>
52
+ <Td align="right">412 ms</Td>
53
+ </tr>
54
+ </tbody>
55
+ </table>
56
+ ```
57
+
58
+ Rules:
59
+
60
+ - `borderCollapse: 'collapse'` + `tableLayout: 'fixed'`, always. Auto layout produces columns that shift when the copy changes.
61
+ - Numbers right-aligned with `fontVariantNumeric: 'tabular-nums'`. Text left-aligned. Never center either.
62
+ - Horizontal rules only. Vertical borders and zebra striping both add noise that print exaggerates.
63
+ - Cell font 11–12px, row padding 6–8px → a row costs ~26–30px. Budget accordingly (`references/pagination.md`).
64
+ - Portrait A4 tops out around **7 columns**; past that, either drop columns or transpose the table.
65
+ - One unit per column, declared in the header (`p99 (ms)`), not repeated in every cell.
66
+ - A caption goes **below** the table at `--od-size-caption`, muted, and must stay on the same page as the table.
67
+
68
+ ### Long tables
69
+
70
+ Split by row groups across pages. Repeat the full header row and mark the continuation in the caption ("Table 3 (continued)"). Never let a header land on one page and its rows on the next.
71
+
72
+ ## Stat rows
73
+
74
+ A row of 3–4 headline numbers under a section title. Define one component, instantiate it per stat — do **not** `map` over a data array (an explicit instance per stat keeps each number editable on its own):
75
+
76
+ ```tsx
77
+ const Stat = ({ value, label, note }: { value: string; label: string; note?: string }) => (
78
+ <div style={{ flex: 1 }}>
79
+ <div style={{ fontFamily: 'var(--od-font-heading)', fontSize: 30, fontWeight: 650, lineHeight: 1.1 }}>
80
+ {value}
81
+ </div>
82
+ <div style={{ fontSize: 12, marginTop: 4 }}>{label}</div>
83
+ {note && <div style={{ fontSize: 10, color: 'var(--od-muted)', marginTop: 2 }}>{note}</div>}
84
+ </div>
85
+ );
86
+
87
+ <div style={{ display: 'flex', gap: 24, borderTop: '1px solid var(--od-rule)', paddingTop: 14 }}>
88
+ <Stat value="99.94%" label="Availability" note="target 99.9%" />
89
+ <Stat value="412 ms" label="p99 latency" note="−18% QoQ" />
90
+ <Stat value="$41.2k" label="Monthly spend" note="+6% QoQ" />
91
+ </div>
92
+ ```
93
+
94
+ Every stat needs a comparison (target, prior period) — a number with nothing to compare against tells the reader nothing.
95
+
96
+ ## Charts
97
+
98
+ No chart library is available, and none is needed. Write **inline SVG** sized in absolute px:
99
+
100
+ ```tsx
101
+ const Bars = () => {
102
+ const data = [
103
+ { label: 'Jan', value: 38 },
104
+ { label: 'Feb', value: 52 },
105
+ { label: 'Mar', value: 47 },
106
+ ];
107
+ const w = 642;
108
+ const h = 180;
109
+ const max = 60;
110
+ const gap = 12;
111
+ const barW = (w - gap * (data.length - 1)) / data.length;
112
+
113
+ return (
114
+ <svg width={w} height={h} role="img" aria-label="Monthly incidents">
115
+ <title>Monthly incidents</title>
116
+ {data.map((d, i) => {
117
+ const barH = (d.value / max) * (h - 24);
118
+ return (
119
+ <g key={d.label}>
120
+ <rect x={i * (barW + gap)} y={h - 24 - barH} width={barW} height={barH} fill="var(--od-accent)" rx={2} />
121
+ <text x={i * (barW + gap) + barW / 2} y={h - 8} textAnchor="middle" fontSize={10} fill="var(--od-muted)">
122
+ {d.label}
123
+ </text>
124
+ </g>
125
+ );
126
+ })}
127
+ </svg>
128
+ );
129
+ };
130
+ ```
131
+
132
+ Rules:
133
+
134
+ - Size the SVG to the text block width (A4 @ 76px margins → 642) so it aligns with the prose.
135
+ - Vector only. SVG stays sharp in the PDF; a rasterized chart does not.
136
+ - Label directly on the chart — a separate legend forces the reader to look twice. If you must have a legend, put it on one line above the plot.
137
+ - Start bar axes at zero. Truncated axes in a report are a credibility problem, not a style choice.
138
+ - Two decimals maximum, units in the axis label or the caption.
139
+ - `data.map` inside one SVG is fine — the "explicit instance" rule is about repeated *page elements*, not path geometry.
140
+ - If the chart needs data the user has to supply, don't fabricate plausible numbers. Use `<ImagePlaceholder hint="Bar chart: monthly incidents, Jan–Sep, from the ops dashboard" height={180} />` and flag it at hand-off.
141
+
142
+ ## Callouts
143
+
144
+ One box style, used sparingly (at most one or two per page):
145
+
146
+ ```tsx
147
+ <div
148
+ style={{
149
+ borderLeft: '3px solid var(--od-accent)',
150
+ background: '#f8fafc',
151
+ padding: '12px 14px',
152
+ borderRadius: 'var(--od-radius)',
153
+ fontSize: 12,
154
+ }}
155
+ >
156
+ <strong style={{ display: 'block', marginBottom: 4 }}>Recommendation</strong>
157
+ Move the checkout queue to its own cluster before the November peak.
158
+ </div>
159
+ ```
160
+
161
+ Reserve callouts for recommendations, risks, and definitions. A page where everything is boxed emphasizes nothing.
@@ -0,0 +1,42 @@
1
+ import config from 'virtual:open-doc/config';
2
+ import { BrowserRouter, Link, Route, Routes } from 'react-router-dom';
3
+ import { AssetsPage } from './routes/assets';
4
+ import { Doc } from './routes/doc';
5
+ import { Home } from './routes/home';
6
+ import { HomeShell } from './routes/home-shell';
7
+ import { ThemeDetailPage, ThemesGalleryPage } from './routes/themes';
8
+
9
+ export function App() {
10
+ return (
11
+ <BrowserRouter basename={import.meta.env.BASE_URL}>
12
+ <Routes>
13
+ {config.build.showDocBrowser ? (
14
+ <Route element={<HomeShell />}>
15
+ <Route path="/" element={<Home />} />
16
+ <Route path="/themes" element={<ThemesGalleryPage />} />
17
+ <Route path="/themes/:themeId" element={<ThemeDetailPage />} />
18
+ <Route path="/assets" element={<AssetsPage />} />
19
+ </Route>
20
+ ) : (
21
+ <Route path="/" element={<NotFound />} />
22
+ )}
23
+ <Route path="/d/:docId" element={<Doc />} />
24
+ <Route path="*" element={<NotFound />} />
25
+ </Routes>
26
+ </BrowserRouter>
27
+ );
28
+ }
29
+
30
+ function NotFound() {
31
+ return (
32
+ <div className="grid h-screen place-items-center bg-background px-6 text-center text-foreground">
33
+ <div>
34
+ <p className="font-mono text-muted-foreground text-xs uppercase tracking-widest">404</p>
35
+ <h1 className="mt-2 font-medium text-xl tracking-tight">Nothing here</h1>
36
+ <Link to="/" className="mt-4 inline-block text-muted-foreground text-xs underline">
37
+ Back to documents
38
+ </Link>
39
+ </div>
40
+ </div>
41
+ );
42
+ }
@@ -0,0 +1,318 @@
1
+ import { Loader2, RotateCcw, Shuffle, X } from 'lucide-react';
2
+ import type { DesignSystem } from '../../lib/design';
3
+ import { useDesignPanelState } from './design-provider';
4
+
5
+ const FONT_OPTIONS: Array<{ label: string; value: string }> = [
6
+ {
7
+ label: 'System sans',
8
+ value: '-apple-system, BlinkMacSystemFont, "Inter", system-ui, sans-serif',
9
+ },
10
+ { label: 'Helvetica', value: '"Helvetica Neue", Helvetica, Arial, sans-serif' },
11
+ { label: 'Georgia serif', value: 'Georgia, "Times New Roman", serif' },
12
+ { label: 'Times serif', value: '"Times New Roman", Times, serif' },
13
+ { label: 'Mono', value: 'ui-monospace, "SF Mono", "JetBrains Mono", Menlo, monospace' },
14
+ ];
15
+
16
+ const PALETTE_KEYS: Array<{ key: keyof DesignSystem['palette']; label: string }> = [
17
+ { key: 'bg', label: 'Sheet' },
18
+ { key: 'text', label: 'Text' },
19
+ { key: 'muted', label: 'Muted' },
20
+ { key: 'accent', label: 'Accent' },
21
+ { key: 'rule', label: 'Rule' },
22
+ ];
23
+
24
+ const TYPE_KEYS: Array<{
25
+ key: keyof DesignSystem['typeScale'];
26
+ label: string;
27
+ min: number;
28
+ max: number;
29
+ }> = [
30
+ { key: 'title', label: 'Title', min: 24, max: 72 },
31
+ { key: 'h1', label: 'H1', min: 16, max: 48 },
32
+ { key: 'h2', label: 'H2', min: 14, max: 36 },
33
+ { key: 'h3', label: 'H3', min: 12, max: 28 },
34
+ { key: 'body', label: 'Body', min: 9, max: 20 },
35
+ { key: 'caption', label: 'Caption', min: 7, max: 16 },
36
+ ];
37
+
38
+ 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();
53
+
54
+ 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}>
60
+ <Shuffle className="size-3.5" />
61
+ </IconButton>
62
+ <IconButton label="Reset to defaults" onClick={resetToDefaults}>
63
+ <RotateCcw className="size-3.5" />
64
+ </IconButton>
65
+ <IconButton label="Close design panel" onClick={onClose}>
66
+ <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"
314
+ >
315
+ {children}
316
+ </button>
317
+ );
318
+ }