@open-document/core 0.10.0 → 0.11.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.
@@ -1,4 +1,4 @@
1
- import { t as createViteConfig } from "./config-CvzpyAUA.js";
1
+ import { t as createViteConfig } from "./config-B_pdNbYg.js";
2
2
  import { createServer, mergeConfig } from "vite";
3
3
  //#region src/cli/dev.ts
4
4
  async function dev(opts = {}) {
@@ -1,5 +1,5 @@
1
- import { g as diffDocument } from "./ops-DKidjLXP.js";
2
- import { t as cliContext } from "./context-B4ip7SoI.js";
1
+ import { g as diffDocument } from "./ops-BUo67DaP.js";
2
+ import { t as cliContext } from "./context-BM9_-u9P.js";
3
3
  import chalk from "chalk";
4
4
  //#region src/cli/diff.ts
5
5
  const MARK = {
@@ -1,7 +1,7 @@
1
1
  import { r as EXPORT_FORMATS } from "./sdk-CqNZzbkz.js";
2
- import { f as listDocIds } from "./config-CvzpyAUA.js";
3
- import { d as closeRenderSession, f as exportDocument } from "./ops-DKidjLXP.js";
4
- import { t as cliContext } from "./context-B4ip7SoI.js";
2
+ import { f as listDocIds } from "./config-B_pdNbYg.js";
3
+ import { d as closeRenderSession, f as exportDocument } from "./ops-BUo67DaP.js";
4
+ import { t as cliContext } from "./context-BM9_-u9P.js";
5
5
  import chalk from "chalk";
6
6
  //#region src/cli/export.ts
7
7
  const FORMATS = EXPORT_FORMATS;
@@ -1,5 +1,5 @@
1
- import { m as importMarkdown } from "./ops-DKidjLXP.js";
2
- import { t as cliContext } from "./context-B4ip7SoI.js";
1
+ import { m as importMarkdown } from "./ops-BUo67DaP.js";
2
+ import { t as cliContext } from "./context-BM9_-u9P.js";
3
3
  import chalk from "chalk";
4
4
  import path from "node:path";
5
5
  //#region src/cli/import.ts
package/dist/ops/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import { i as ORIENTATIONS, o as PAGE_SIZE_NAMES, r as EXPORT_FORMATS } from "../sdk-CqNZzbkz.js";
2
- import { O as makeContext, _ as writeDocument, a as writeText, c as createDocument, d as duplicateDocument, f as listDocIds, g as resolveEntry, h as renameDocument, i as readText, l as deleteDocument, m as readDocument, o as writeTexts, p as listDocuments, r as addComment, s as OpsError, u as docDir } from "../config-CvzpyAUA.js";
3
- import { a as listAssets, c as readTheme, d as closeRenderSession, f as exportDocument, g as diffDocument, h as slugify, i as findAssetUsages, l as writeAsset, m as importMarkdown, n as deleteAsset, o as listFolders, p as renderDocPage, r as fileDocument, s as listThemes, t as createFolder, u as checkLayout } from "../ops-DKidjLXP.js";
2
+ import { O as makeContext, _ as writeDocument, a as writeText, c as createDocument, d as duplicateDocument, f as listDocIds, g as resolveEntry, h as renameDocument, i as readText, l as deleteDocument, m as readDocument, o as writeTexts, p as listDocuments, r as addComment, s as OpsError, u as docDir } from "../config-B_pdNbYg.js";
3
+ import { a as listAssets, c as readTheme, d as closeRenderSession, f as exportDocument, g as diffDocument, h as slugify, i as findAssetUsages, l as writeAsset, m as importMarkdown, n as deleteAsset, o as listFolders, p as renderDocPage, r as fileDocument, s as listThemes, t as createFolder, u as checkLayout } from "../ops-BUo67DaP.js";
4
4
  export { EXPORT_FORMATS, ORIENTATIONS, OpsError, PAGE_SIZE_NAMES, addComment, checkLayout, closeRenderSession, createDocument, createFolder, deleteAsset, deleteDocument, diffDocument, docDir, duplicateDocument, exportDocument, fileDocument, findAssetUsages, importMarkdown, listAssets, listDocIds, listDocuments, listFolders, listThemes, makeContext, readDocument, readText, readTheme, renameDocument, renderDocPage, resolveEntry, slugify, writeAsset, writeDocument, writeText, writeTexts };
@@ -1,5 +1,5 @@
1
1
  import { c as isPageSizeName, i as ORIENTATIONS, o as PAGE_SIZE_NAMES, s as isOrientation } from "./sdk-CqNZzbkz.js";
2
- import { C as assetImportPath, D as validateAssetName, E as resolveScopedAssetsDir, S as writeManifest, T as resolveScopedAssetFile, b as validateIcon, c as createDocument, f as listDocIds, g as resolveEntry, k as DOC_ID_RE, n as parseFrontmatter$1, s as OpsError, t as createViteConfig, v as newFolderId, w as countAssetUsages, x as validateName, y as readManifest } from "./config-CvzpyAUA.js";
2
+ import { C as assetImportPath, D as validateAssetName, E as resolveScopedAssetsDir, S as writeManifest, T as resolveScopedAssetFile, b as validateIcon, c as createDocument, f as listDocIds, g as resolveEntry, k as DOC_ID_RE, n as parseFrontmatter$1, s as OpsError, t as createViteConfig, v as newFolderId, w as countAssetUsages, x as validateName, y as readManifest } from "./config-B_pdNbYg.js";
3
3
  import path from "node:path";
4
4
  import fs from "node:fs/promises";
5
5
  import { existsSync } from "node:fs";
@@ -1,4 +1,4 @@
1
- import { t as createViteConfig } from "./config-CvzpyAUA.js";
1
+ import { t as createViteConfig } from "./config-B_pdNbYg.js";
2
2
  import { mergeConfig, preview as preview$1 } from "vite";
3
3
  //#region src/cli/preview.ts
4
4
  async function preview(opts = {}) {
@@ -1,2 +1,2 @@
1
- import { j as openDocPlugin, t as createViteConfig } from "../config-CvzpyAUA.js";
1
+ import { j as openDocPlugin, t as createViteConfig } from "../config-B_pdNbYg.js";
2
2
  export { createViteConfig, openDocPlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-document/core",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Runtime and CLI for open-doc — write documents in docs/, we handle the pages, print layout, and export.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -0,0 +1,434 @@
1
+ import {
2
+ AlignCenter,
3
+ AlignJustify,
4
+ AlignLeft,
5
+ AlignRight,
6
+ Bold,
7
+ CaseSensitive,
8
+ ChevronDown,
9
+ Italic,
10
+ Link2,
11
+ Lock,
12
+ type LucideIcon,
13
+ MoveHorizontal,
14
+ MoveVertical,
15
+ Type,
16
+ } from 'lucide-react';
17
+ import type { ReactNode } from 'react';
18
+ import { type Translate, useT } from '../../lib/i18n';
19
+ import {
20
+ isValidStyleValue,
21
+ PROP_TOKENS,
22
+ rgbToHex,
23
+ type StyleChanges,
24
+ type StyleInfo,
25
+ type StyleOrigin,
26
+ type StyleProp,
27
+ type StyleValue,
28
+ TOKENS,
29
+ tokenOf,
30
+ tokenValue,
31
+ } from '../../lib/inspector/format';
32
+ import { ColorField, NumberField, Section } from '../panel/fields';
33
+
34
+ /** What a key reads as right now: an unsaved change first, then the source, else inherited. */
35
+ type Current = { origin: StyleOrigin | null; pending: boolean };
36
+
37
+ function currentOf(prop: StyleProp, info: StyleInfo | null, pending: StyleChanges): Current {
38
+ if (prop in pending) {
39
+ const value = pending[prop];
40
+ if (value === null || value === undefined) return { origin: null, pending: true };
41
+ const token = typeof value === 'string' ? tokenOf(value) : null;
42
+ return {
43
+ origin: token ? { kind: 'token', token } : { kind: 'literal', value },
44
+ pending: true,
45
+ };
46
+ }
47
+ return { origin: info?.props?.[prop] ?? null, pending: false };
48
+ }
49
+
50
+ /** Where a value comes from, for a tooltip: a shared style object, or code the panel only shows. */
51
+ function originNote(t: Translate, current: Current): string | undefined {
52
+ const origin = current.origin;
53
+ if (current.pending || !origin) return undefined;
54
+ const from = origin.from ? t('from {name}', { name: origin.from }) : null;
55
+ const code = origin.kind === 'code' ? t('set in code: {code}', { code: origin.code }) : null;
56
+ return [code, from].filter(Boolean).join(' · ') || undefined;
57
+ }
58
+
59
+ // One frame for a composite control, so a number and its token chip read as
60
+ // one field, as they are one value.
61
+ const BOX_CLASS =
62
+ 'flex h-8 min-w-0 items-center rounded border border-border transition-colors focus-within:border-foreground/40';
63
+
64
+ const SELECT_CLASS =
65
+ 'h-8 min-w-0 flex-1 cursor-pointer appearance-none bg-transparent pr-6 text-xs outline-none';
66
+
67
+ const SEGMENT_CLASS =
68
+ 'flex h-8 min-w-8 flex-1 items-center justify-center rounded-[3px] text-foreground/70 transition-colors hover:bg-accent hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-foreground/60 aria-pressed:bg-accent aria-pressed:text-foreground';
69
+
70
+ // Named with the number, as one string: "Light" alone is already the theme's.
71
+ const WEIGHTS: Array<[number, string]> = [
72
+ [300, 'Light 300'],
73
+ [400, 'Regular 400'],
74
+ [500, 'Medium 500'],
75
+ [600, 'Semibold 600'],
76
+ [700, 'Bold 700'],
77
+ [800, 'Extrabold 800'],
78
+ ];
79
+
80
+ const ALIGNS: Array<[string, string, LucideIcon]> = [
81
+ ['left', 'Align left', AlignLeft],
82
+ ['center', 'Align center', AlignCenter],
83
+ ['right', 'Align right', AlignRight],
84
+ ['justify', 'Justify', AlignJustify],
85
+ ];
86
+
87
+ type Choice = 'inherit' | `token:${string}` | 'custom';
88
+
89
+ function choiceOf(prop: StyleProp, current: Current): Choice {
90
+ const origin = current.origin;
91
+ if (origin === null || (origin.from && !current.pending)) return 'inherit';
92
+ const group = PROP_TOKENS[prop];
93
+ if (origin.kind === 'token' && group && TOKENS[group].some((t) => t.token === origin.token)) {
94
+ return `token:${origin.token}`;
95
+ }
96
+ return 'custom';
97
+ }
98
+
99
+ /**
100
+ * The value's source as a choice — inherited, one of the document's tokens,
101
+ * or this element's own. The chip names it, so a field linked to the design
102
+ * panel says so without being opened; the native select under it does the
103
+ * choosing, and the keyboard and screen reader with it.
104
+ */
105
+ function TokenChip({
106
+ label,
107
+ prop,
108
+ current,
109
+ compact = false,
110
+ onPick,
111
+ }: {
112
+ label: string;
113
+ prop: StyleProp;
114
+ current: Current;
115
+ /**
116
+ * Only an icon, for a field too narrow for a token name; `linked` keeps the
117
+ * name while a token is chosen, since that is the one worth reading.
118
+ */
119
+ compact?: boolean | 'linked';
120
+ onPick: (value: 'inherit' | string) => void;
121
+ }) {
122
+ const t = useT();
123
+ const group = PROP_TOKENS[prop];
124
+ const tokens = group ? TOKENS[group] : [];
125
+ const choice = choiceOf(prop, current);
126
+ const origin = current.origin;
127
+ const linked = choice.startsWith('token:');
128
+ const inheritLabel =
129
+ origin?.from && !current.pending ? t('From {name}', { name: origin.from }) : t('Inherited');
130
+ const shown =
131
+ choice === 'inherit'
132
+ ? origin?.from && !current.pending
133
+ ? origin.from
134
+ : t('Inherited')
135
+ : choice === 'custom'
136
+ ? origin?.kind === 'code'
137
+ ? t('Code')
138
+ : t('Custom')
139
+ : t(tokens.find((entry) => `token:${entry.token}` === choice)?.label ?? 'Custom');
140
+ return (
141
+ <span
142
+ title={originNote(t, current) ?? (linked ? `--od-${choice.slice(6)}` : undefined)}
143
+ className={`relative mr-1 flex h-6 flex-none items-center gap-0.5 rounded px-1.5 text-[11px] transition-colors hover:bg-accent has-[:focus-visible]:outline-2 has-[:focus-visible]:outline-foreground/60 ${
144
+ linked ? 'bg-accent text-accent-foreground' : 'text-muted-foreground'
145
+ }`}
146
+ >
147
+ {compact === true || (compact === 'linked' && !linked) ? (
148
+ <Link2 aria-hidden className="size-3.5" />
149
+ ) : (
150
+ <span className="max-w-20 truncate">{shown}</span>
151
+ )}
152
+ <ChevronDown aria-hidden className="size-3 opacity-60" />
153
+ <select
154
+ aria-label={t('{label} token', { label })}
155
+ value={choice}
156
+ onChange={(e) => {
157
+ const next = e.target.value;
158
+ if (next === 'inherit') onPick('inherit');
159
+ else if (next.startsWith('token:')) onPick(tokenValue(next.slice(6)));
160
+ }}
161
+ className="absolute inset-0 cursor-pointer opacity-0"
162
+ >
163
+ <option value="inherit">{inheritLabel}</option>
164
+ {tokens.map((entry) => (
165
+ <option key={entry.token} value={`token:${entry.token}`}>
166
+ {t(entry.label)}
167
+ </option>
168
+ ))}
169
+ {choice === 'custom' && <option value="custom">{t('Custom')}</option>}
170
+ </select>
171
+ </span>
172
+ );
173
+ }
174
+
175
+ function Segments({ label, children }: { label: string; children: ReactNode }) {
176
+ return (
177
+ <fieldset
178
+ aria-label={label}
179
+ className="m-0 flex h-9 min-w-0 items-center gap-0.5 rounded border border-border p-0.5"
180
+ >
181
+ {children}
182
+ </fieldset>
183
+ );
184
+ }
185
+
186
+ export type FormatSectionProps = {
187
+ anchor: HTMLElement;
188
+ /** `null` while the source is still being read. */
189
+ info: StyleInfo | null;
190
+ pending: StyleChanges;
191
+ onChange: (prop: StyleProp, value: StyleValue | undefined, shown: StyleOrigin | null) => void;
192
+ };
193
+
194
+ /**
195
+ * Typography and colour for the selected element, written into its `style` in
196
+ * source. Values are read from the rendered element, so what the fields show
197
+ * is what is printed; whether a value is a design token, a shared style
198
+ * object's, or code comes from the source.
199
+ */
200
+ export function FormatSection({ anchor, info, pending, onChange }: FormatSectionProps) {
201
+ const t = useT();
202
+ const editable = info?.editable === true;
203
+ const cs = getComputedStyle(anchor);
204
+ const current = (prop: StyleProp) => currentOf(prop, info, pending);
205
+ const set = (prop: StyleProp, value: StyleValue | undefined) => {
206
+ if (value !== undefined && !isValidStyleValue(prop, value)) return;
207
+ onChange(prop, value, info?.props?.[prop] ?? null);
208
+ };
209
+ // Off in source when the element writes the key itself; otherwise only the
210
+ // unsaved change is taken back, since an inherited key is not this element's.
211
+ const clear = (prop: StyleProp) => {
212
+ const origin = info?.props?.[prop];
213
+ set(prop, origin && !origin.from ? null : undefined);
214
+ };
215
+ const pick = (prop: StyleProp) => (value: 'inherit' | string) =>
216
+ value === 'inherit' ? clear(prop) : set(prop, value);
217
+
218
+ const fontSize = Number.parseFloat(cs.fontSize) || 0;
219
+ const weight = Number.parseInt(cs.fontWeight, 10) || 400;
220
+ const italic = cs.fontStyle === 'italic';
221
+ const align = cs.textAlign === 'start' ? 'left' : cs.textAlign === 'end' ? 'right' : cs.textAlign;
222
+ const lineHeight =
223
+ cs.lineHeight === 'normal' || fontSize === 0
224
+ ? 1.2
225
+ : Number((Number.parseFloat(cs.lineHeight) / fontSize).toFixed(2));
226
+ const tracking = cs.letterSpacing === 'normal' ? 0 : Number.parseFloat(cs.letterSpacing) || 0;
227
+ const color = rgbToHex(cs.color) ?? '';
228
+ const background = rgbToHex(cs.backgroundColor) ?? '';
229
+
230
+ // A refused element keeps its fields, read-only: they still say what the
231
+ // element is set to, and the lock says why they do not move.
232
+ const locked = info !== null && !editable ? t(info.reason) : null;
233
+ const lock = locked ? (
234
+ <span role="img" aria-label={locked} title={locked} className="flex text-muted-foreground">
235
+ <Lock className="size-3.5" />
236
+ </span>
237
+ ) : undefined;
238
+
239
+ const font = current('fontFamily');
240
+ const fontChoice = choiceOf('fontFamily', font);
241
+
242
+ return (
243
+ <fieldset
244
+ disabled={!editable}
245
+ className={`m-0 min-w-0 border-0 p-0 transition-opacity ${locked ? 'opacity-50' : ''}`}
246
+ >
247
+ <Section title={t('Typography')} action={lock}>
248
+ <label className={`${BOX_CLASS} relative`} title={originNote(t, font)}>
249
+ <Type aria-hidden className="ml-2 size-3.5 flex-none text-muted-foreground" />
250
+ <select
251
+ aria-label={t('Font')}
252
+ value={fontChoice}
253
+ onChange={(e) => pick('fontFamily')(e.target.value)}
254
+ className={`${SELECT_CLASS} pl-2`}
255
+ >
256
+ <option value="inherit">
257
+ {font.origin?.from && !font.pending
258
+ ? t('From {name}', { name: font.origin.from })
259
+ : t('Inherited font')}
260
+ </option>
261
+ {TOKENS.font.map((entry) => (
262
+ <option key={entry.token} value={`token:${entry.token}`}>
263
+ {t('{name} font', { name: t(entry.label) })}
264
+ </option>
265
+ ))}
266
+ {fontChoice === 'custom' && <option value="custom">{t('Custom font')}</option>}
267
+ </select>
268
+ <ChevronDown
269
+ aria-hidden
270
+ className="pointer-events-none absolute right-2 size-3.5 text-muted-foreground"
271
+ />
272
+ </label>
273
+
274
+ <div className={BOX_CLASS} title={originNote(t, current('fontSize'))}>
275
+ <NumberField
276
+ bare
277
+ label={t('Size in pixels')}
278
+ prefix={<CaseSensitive className="size-4" />}
279
+ value={Number(fontSize.toFixed(1))}
280
+ min={4}
281
+ max={400}
282
+ step={0.5}
283
+ suffix="px"
284
+ onChange={(value) => set('fontSize', `${value}px`)}
285
+ />
286
+ <TokenChip
287
+ label={t('Size')}
288
+ prop="fontSize"
289
+ current={current('fontSize')}
290
+ onPick={pick('fontSize')}
291
+ />
292
+ </div>
293
+
294
+ <div className="flex min-w-0 gap-2">
295
+ <label
296
+ className={`${BOX_CLASS} relative flex-1`}
297
+ title={originNote(t, current('fontWeight'))}
298
+ >
299
+ <select
300
+ aria-label={t('Weight')}
301
+ value={weight}
302
+ onChange={(e) => set('fontWeight', Number(e.target.value))}
303
+ className={`${SELECT_CLASS} pl-2`}
304
+ >
305
+ {WEIGHTS.some(([value]) => value === weight) ? null : (
306
+ <option value={weight}>{weight}</option>
307
+ )}
308
+ {WEIGHTS.map(([value, label]) => (
309
+ <option key={value} value={value}>
310
+ {t(label)}
311
+ </option>
312
+ ))}
313
+ </select>
314
+ <ChevronDown
315
+ aria-hidden
316
+ className="pointer-events-none absolute right-2 size-3.5 text-muted-foreground"
317
+ />
318
+ </label>
319
+ <Segments label={t('Emphasis')}>
320
+ <button
321
+ type="button"
322
+ aria-label={t('Bold')}
323
+ title={t('Bold')}
324
+ aria-pressed={weight >= 600}
325
+ onClick={() => set('fontWeight', weight >= 600 ? 400 : 700)}
326
+ className={SEGMENT_CLASS}
327
+ >
328
+ <Bold className="size-3.5" />
329
+ </button>
330
+ <button
331
+ type="button"
332
+ aria-label={t('Italic')}
333
+ title={t('Italic')}
334
+ aria-pressed={italic}
335
+ onClick={() => set('fontStyle', italic ? 'normal' : 'italic')}
336
+ className={SEGMENT_CLASS}
337
+ >
338
+ <Italic className="size-3.5" />
339
+ </button>
340
+ </Segments>
341
+ </div>
342
+
343
+ <Segments label={t('Alignment')}>
344
+ {ALIGNS.map(([value, label, Icon]) => (
345
+ <button
346
+ key={value}
347
+ type="button"
348
+ aria-label={t(label)}
349
+ title={t(label)}
350
+ aria-pressed={align === value}
351
+ onClick={() => set('textAlign', value)}
352
+ className={SEGMENT_CLASS}
353
+ >
354
+ <Icon className="size-3.5" />
355
+ </button>
356
+ ))}
357
+ </Segments>
358
+
359
+ <div className="grid grid-cols-2 gap-2">
360
+ <div
361
+ className={BOX_CLASS}
362
+ title={originNote(t, current('lineHeight')) ?? t('Line height')}
363
+ >
364
+ <NumberField
365
+ bare
366
+ label={t('Line height')}
367
+ prefix={<MoveVertical className="size-3.5" />}
368
+ value={lineHeight}
369
+ min={0.5}
370
+ max={4}
371
+ step={0.05}
372
+ onChange={(value) => set('lineHeight', value)}
373
+ />
374
+ <TokenChip
375
+ compact
376
+ label={t('Line height')}
377
+ prop="lineHeight"
378
+ current={current('lineHeight')}
379
+ onPick={pick('lineHeight')}
380
+ />
381
+ </div>
382
+ <div
383
+ className={BOX_CLASS}
384
+ title={originNote(t, current('letterSpacing')) ?? t('Letter spacing')}
385
+ >
386
+ <NumberField
387
+ bare
388
+ label={t('Letter spacing in pixels')}
389
+ prefix={<MoveHorizontal className="size-3.5" />}
390
+ value={Number(tracking.toFixed(2))}
391
+ min={-20}
392
+ max={50}
393
+ step={0.1}
394
+ suffix="px"
395
+ onChange={(value) => set('letterSpacing', `${value}px`)}
396
+ />
397
+ </div>
398
+ </div>
399
+ </Section>
400
+
401
+ <Section title={t('Color')} action={lock}>
402
+ <ColorField
403
+ label={t('Text')}
404
+ value={color}
405
+ onChange={(value) => set('color', value)}
406
+ trailing={
407
+ <TokenChip
408
+ compact="linked"
409
+ label={t('Text color')}
410
+ prop="color"
411
+ current={current('color')}
412
+ onPick={pick('color')}
413
+ />
414
+ }
415
+ />
416
+ <ColorField
417
+ label={t('Fill')}
418
+ value={background}
419
+ placeholder={t('None')}
420
+ onChange={(value) => set('background', value)}
421
+ trailing={
422
+ <TokenChip
423
+ compact="linked"
424
+ label={t('Fill')}
425
+ prop="background"
426
+ current={current('background')}
427
+ onPick={pick('background')}
428
+ />
429
+ }
430
+ />
431
+ </Section>
432
+ </fieldset>
433
+ );
434
+ }