@open-document/core 0.12.0 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{build-DfUfpeTR.js → build-CvsOak5B.js} +1 -1
- package/dist/{check-CHxiOkQG.js → check-CbUBwYmf.js} +3 -3
- package/dist/cli/bin.js +47 -10
- package/dist/config-BWtOZ1jc.d.ts +45 -0
- package/dist/{config-9IdbhVuT.js → config-CltJJVsw.js} +398 -493
- package/dist/{context-BLI1-64_.js → context-CWgfsHoE.js} +2 -1
- package/dist/deploy-gcp-BUiSuqz7.js +758 -0
- package/dist/{dev-yNYKv5mm.js → dev-B8H2qT6G.js} +1 -1
- package/dist/{diagram-BK4jTZr4.js → diagram-CKf3ptOE.js} +293 -13
- package/dist/{diff-Bs205mOx.js → diff-BvIjjgnc.js} +2 -2
- package/dist/{export-tkquS5O5.js → export-DX30_s2q.js} +3 -3
- package/dist/{sdk-CHEGeINh.d.ts → flow-CS5apebG.d.ts} +72 -3
- package/dist/{import-D1hIKIr3.js → import-DgsdJT5e.js} +2 -2
- package/dist/index.d.ts +155 -59
- package/dist/index.js +896 -90
- package/dist/open-doc-plugin-DnX5usgI.js +320 -0
- package/dist/ops/index.d.ts +98 -13
- package/dist/ops/index.js +3 -3
- package/dist/{ops-Dgqujq5a.js → ops-CSR5RlrE.js} +2 -1
- package/dist/{preview-BImdTH6K.js → preview-DYMUNjXL.js} +1 -1
- package/dist/{templates-DT5zzqHq.js → templates-B5Tz0AKb.js} +3 -3
- package/dist/vite/index.d.ts +1 -1
- package/dist/vite/index.js +2 -1
- package/package.json +1 -1
- package/skills/doc-authoring/SKILL.md +15 -16
- package/skills/doc-authoring/references/long-form.md +43 -2
- package/skills/doc-authoring/references/pagination.md +14 -2
- package/skills/doc-authoring/references/tables-and-charts.md +21 -17
- package/src/app/components/callout.tsx +101 -0
- package/src/app/components/chart.tsx +191 -33
- package/src/app/components/code-block.tsx +249 -0
- package/src/app/components/data-table.tsx +146 -18
- package/src/app/components/flow-page.tsx +57 -8
- package/src/app/components/numbering.tsx +226 -12
- package/src/app/components/page-frame.tsx +42 -0
- package/src/app/lib/chart.test.ts +15 -0
- package/src/app/lib/chart.ts +134 -9
- package/src/app/lib/diagnostics.ts +45 -1
- package/src/app/lib/export-dom.ts +15 -2
- package/src/app/lib/export-pdf.ts +9 -1
- package/src/app/lib/flow-measure.ts +33 -4
- package/src/app/lib/flow.test.ts +20 -1
- package/src/app/lib/flow.ts +120 -20
- package/src/app/lib/labels.ts +29 -10
- package/src/app/lib/sdk.ts +6 -0
- package/src/app/lib/use-doc-pages.ts +23 -8
- package/src/app/routes/doc.tsx +1 -0
- package/dist/config-P61niZMf.d.ts +0 -24
|
@@ -27,6 +27,8 @@ export type FigureProps = {
|
|
|
27
27
|
captionPosition?: 'above' | 'below';
|
|
28
28
|
/** Plain-text caption for the list of figures, when `caption` carries markup. */
|
|
29
29
|
captionText?: string;
|
|
30
|
+
/** When false, prints the caption without registering a new label number (e.g. continued table slices). */
|
|
31
|
+
numbered?: boolean;
|
|
30
32
|
style?: CSSProperties;
|
|
31
33
|
captionStyle?: CSSProperties;
|
|
32
34
|
className?: string;
|
|
@@ -49,6 +51,7 @@ export function Figure({
|
|
|
49
51
|
kind = 'figure',
|
|
50
52
|
captionPosition,
|
|
51
53
|
captionText,
|
|
54
|
+
numbered = true,
|
|
52
55
|
style,
|
|
53
56
|
captionStyle,
|
|
54
57
|
className,
|
|
@@ -75,28 +78,35 @@ export function Figure({
|
|
|
75
78
|
...captionStyle,
|
|
76
79
|
}}
|
|
77
80
|
>
|
|
78
|
-
|
|
79
|
-
{
|
|
80
|
-
|
|
81
|
+
{numbered && (
|
|
82
|
+
<span style={{ fontWeight: 600, color: 'var(--od-text)' }}>
|
|
83
|
+
{nameFor(kind, vocabulary)} {entry?.number ?? ''}
|
|
84
|
+
</span>
|
|
85
|
+
)}
|
|
81
86
|
{typeof caption === 'string' || typeof caption === 'number' ? (
|
|
82
87
|
<span>
|
|
83
|
-
{' — '}
|
|
88
|
+
{numbered ? ' — ' : ''}
|
|
84
89
|
<span {...sourceAttrs(loc, 'caption')}>{caption}</span>
|
|
85
90
|
</span>
|
|
86
91
|
) : caption ? (
|
|
87
|
-
<span>
|
|
92
|
+
<span>
|
|
93
|
+
{numbered ? ' — ' : ''}
|
|
94
|
+
{caption}
|
|
95
|
+
</span>
|
|
88
96
|
) : null}
|
|
89
97
|
</figcaption>
|
|
90
98
|
) : null;
|
|
91
99
|
|
|
92
100
|
return (
|
|
93
101
|
<figure
|
|
94
|
-
{...
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
102
|
+
{...(numbered
|
|
103
|
+
? {
|
|
104
|
+
[LABEL_ATTR]: kind,
|
|
105
|
+
[LABEL_ID_ATTR]: labelId,
|
|
106
|
+
[LABEL_TEXT_ATTR]: text,
|
|
107
|
+
...sourceAttrs(loc),
|
|
108
|
+
}
|
|
109
|
+
: sourceAttrs(loc))}
|
|
100
110
|
className={className}
|
|
101
111
|
style={{ margin: '0 0 16px', ...style }}
|
|
102
112
|
>
|
|
@@ -143,7 +153,10 @@ export function Ref({ to, showPage = 'auto', style, className }: RefProps) {
|
|
|
143
153
|
);
|
|
144
154
|
}
|
|
145
155
|
|
|
146
|
-
const name =
|
|
156
|
+
const name =
|
|
157
|
+
entry.kind === 'footnote' || entry.kind === 'citation'
|
|
158
|
+
? ''
|
|
159
|
+
: `${nameFor(entry.kind, vocabulary)} `;
|
|
147
160
|
const withPage = showPage === 'auto' ? entry.page !== here : showPage;
|
|
148
161
|
|
|
149
162
|
return (
|
|
@@ -219,3 +232,204 @@ export function ListOfFigures(props: Omit<ListOfProps, 'kind'>) {
|
|
|
219
232
|
export function ListOfTables(props: Omit<ListOfProps, 'kind'>) {
|
|
220
233
|
return <ListOf {...props} kind="table" />;
|
|
221
234
|
}
|
|
235
|
+
|
|
236
|
+
export type CiteProps = {
|
|
237
|
+
/** Stable citation key (e.g. 'rfc9110' or 'q3-audit'). */
|
|
238
|
+
id: string;
|
|
239
|
+
/** Citation details printed in `<Bibliography />`. Needs only to be supplied on one `<Cite>` for that id, or in `<Bibliography items={...} />`. */
|
|
240
|
+
source?: string;
|
|
241
|
+
style?: CSSProperties;
|
|
242
|
+
className?: string;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* An inline bibliographic citation (`[1]`), numbered in order of first
|
|
247
|
+
* appearance across the rendered pages. Multiple `<Cite id="x" />` calls share
|
|
248
|
+
* the same number.
|
|
249
|
+
*/
|
|
250
|
+
export function Cite({ id, source, style, className }: CiteProps) {
|
|
251
|
+
const entry = useDocLabel(id, 'citation');
|
|
252
|
+
return (
|
|
253
|
+
<span
|
|
254
|
+
{...{
|
|
255
|
+
[LABEL_ATTR]: 'citation',
|
|
256
|
+
[LABEL_ID_ATTR]: id,
|
|
257
|
+
...(source ? { [LABEL_TEXT_ATTR]: source } : {}),
|
|
258
|
+
}}
|
|
259
|
+
data-od-cite={id}
|
|
260
|
+
className={className}
|
|
261
|
+
style={{
|
|
262
|
+
fontSize: '0.82em',
|
|
263
|
+
verticalAlign: 'super',
|
|
264
|
+
lineHeight: 0,
|
|
265
|
+
fontWeight: 600,
|
|
266
|
+
color: 'var(--od-accent)',
|
|
267
|
+
fontVariantNumeric: 'tabular-nums',
|
|
268
|
+
...style,
|
|
269
|
+
}}
|
|
270
|
+
>
|
|
271
|
+
[{entry?.number ?? '·'}]
|
|
272
|
+
</span>
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export type BibliographyItem = {
|
|
277
|
+
id?: string;
|
|
278
|
+
title?: ReactNode;
|
|
279
|
+
source?: ReactNode;
|
|
280
|
+
author?: string;
|
|
281
|
+
year?: string | number;
|
|
282
|
+
url?: string;
|
|
283
|
+
note?: string;
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
export type BibliographyProps = {
|
|
287
|
+
/** Optional catalog of reference details keyed by citation `id` (or an array with `id`). Merged with inline `<Cite source="..." />` texts. */
|
|
288
|
+
items?: Record<string, string | BibliographyItem> | BibliographyItem[];
|
|
289
|
+
/** Optional heading text above the reference list. Pass `""` to omit. */
|
|
290
|
+
title?: ReactNode;
|
|
291
|
+
/** Show page numbers where each citation first appeared. */
|
|
292
|
+
showPageNumbers?: boolean;
|
|
293
|
+
style?: CSSProperties;
|
|
294
|
+
className?: string;
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
function renderBibItem(spec: string | BibliographyItem | undefined, fallback: string): ReactNode {
|
|
298
|
+
if (!spec) return fallback;
|
|
299
|
+
if (typeof spec === 'string') return spec;
|
|
300
|
+
if (spec.source && !spec.title) return spec.source;
|
|
301
|
+
return (
|
|
302
|
+
<span>
|
|
303
|
+
{spec.author ? `${spec.author}. ` : ''}
|
|
304
|
+
<strong style={{ fontWeight: 600, color: 'var(--od-text)' }}>{spec.title}</strong>
|
|
305
|
+
{spec.year ? ` (${spec.year}).` : '.'}
|
|
306
|
+
{spec.note ? ` ${spec.note}` : ''}
|
|
307
|
+
{spec.url ? (
|
|
308
|
+
<>
|
|
309
|
+
{' '}
|
|
310
|
+
<span
|
|
311
|
+
style={{
|
|
312
|
+
fontFamily: 'var(--od-font-mono)',
|
|
313
|
+
fontSize: '0.92em',
|
|
314
|
+
color: 'var(--od-muted)',
|
|
315
|
+
}}
|
|
316
|
+
>
|
|
317
|
+
{spec.url}
|
|
318
|
+
</span>
|
|
319
|
+
</>
|
|
320
|
+
) : null}
|
|
321
|
+
</span>
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Renders the numbered list of all `<Cite>` references in order of appearance,
|
|
327
|
+
* plus any additional uncited entries passed in `items`.
|
|
328
|
+
*/
|
|
329
|
+
export function Bibliography({
|
|
330
|
+
items,
|
|
331
|
+
title,
|
|
332
|
+
showPageNumbers = false,
|
|
333
|
+
style,
|
|
334
|
+
className,
|
|
335
|
+
}: BibliographyProps) {
|
|
336
|
+
const cited = useDocLabels('citation');
|
|
337
|
+
const vocabulary = useLabelVocabulary();
|
|
338
|
+
|
|
339
|
+
const map = new Map<string, string | BibliographyItem>();
|
|
340
|
+
if (Array.isArray(items)) {
|
|
341
|
+
items.forEach((item, idx) => {
|
|
342
|
+
map.set(item.id ?? `bib-${idx + 1}`, item);
|
|
343
|
+
});
|
|
344
|
+
} else if (items) {
|
|
345
|
+
for (const [k, v] of Object.entries(items)) map.set(k, v);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const rows: Array<{ id: string; number: number; content: ReactNode; page?: number }> = cited.map(
|
|
349
|
+
(entry) => ({
|
|
350
|
+
id: entry.id,
|
|
351
|
+
number: entry.number,
|
|
352
|
+
content: renderBibItem(map.get(entry.id), entry.text || entry.id),
|
|
353
|
+
page: entry.page,
|
|
354
|
+
}),
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
for (const [id, spec] of map.entries()) {
|
|
358
|
+
if (!rows.some((r) => r.id === id)) {
|
|
359
|
+
rows.push({
|
|
360
|
+
id,
|
|
361
|
+
number: rows.length + 1,
|
|
362
|
+
content: renderBibItem(spec, id),
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const heading = title !== undefined ? title : vocabulary.bibliography;
|
|
368
|
+
|
|
369
|
+
return (
|
|
370
|
+
<div
|
|
371
|
+
data-od-bibliography=""
|
|
372
|
+
className={className}
|
|
373
|
+
style={{
|
|
374
|
+
fontFamily: 'var(--od-font-body)',
|
|
375
|
+
fontSize: 'var(--od-size-caption, 11px)',
|
|
376
|
+
lineHeight: 1.55,
|
|
377
|
+
color: 'var(--od-text)',
|
|
378
|
+
margin: '8px 0 14px',
|
|
379
|
+
...style,
|
|
380
|
+
}}
|
|
381
|
+
>
|
|
382
|
+
{heading ? (
|
|
383
|
+
<div
|
|
384
|
+
style={{
|
|
385
|
+
fontFamily: 'var(--od-font-heading)',
|
|
386
|
+
fontSize: 'var(--od-size-h3, 14px)',
|
|
387
|
+
fontWeight: 600,
|
|
388
|
+
marginBottom: 8,
|
|
389
|
+
color: 'var(--od-text)',
|
|
390
|
+
}}
|
|
391
|
+
>
|
|
392
|
+
{heading}
|
|
393
|
+
</div>
|
|
394
|
+
) : null}
|
|
395
|
+
<ol style={{ listStyle: 'none', padding: 0, margin: 0 }}>
|
|
396
|
+
{rows.map((row) => (
|
|
397
|
+
<li
|
|
398
|
+
key={row.id}
|
|
399
|
+
data-od-bib-entry={row.id}
|
|
400
|
+
style={{
|
|
401
|
+
display: 'flex',
|
|
402
|
+
alignItems: 'baseline',
|
|
403
|
+
gap: 8,
|
|
404
|
+
marginBottom: 5,
|
|
405
|
+
}}
|
|
406
|
+
>
|
|
407
|
+
<span
|
|
408
|
+
style={{
|
|
409
|
+
flex: 'none',
|
|
410
|
+
minWidth: 24,
|
|
411
|
+
fontWeight: 600,
|
|
412
|
+
color: 'var(--od-accent)',
|
|
413
|
+
fontVariantNumeric: 'tabular-nums',
|
|
414
|
+
}}
|
|
415
|
+
>
|
|
416
|
+
[{row.number}]
|
|
417
|
+
</span>
|
|
418
|
+
<span style={{ flex: 1, color: 'var(--od-text)' }}>{row.content}</span>
|
|
419
|
+
{showPageNumbers && row.page !== undefined && (
|
|
420
|
+
<span
|
|
421
|
+
style={{
|
|
422
|
+
flex: 'none',
|
|
423
|
+
color: 'var(--od-muted)',
|
|
424
|
+
fontVariantNumeric: 'tabular-nums',
|
|
425
|
+
}}
|
|
426
|
+
>
|
|
427
|
+
p. {row.page}
|
|
428
|
+
</span>
|
|
429
|
+
)}
|
|
430
|
+
</li>
|
|
431
|
+
))}
|
|
432
|
+
</ol>
|
|
433
|
+
</div>
|
|
434
|
+
);
|
|
435
|
+
}
|
|
@@ -13,11 +13,51 @@ type Props = {
|
|
|
13
13
|
design?: DesignSystem;
|
|
14
14
|
/** The document's language — see `pageLang`. */
|
|
15
15
|
lang?: string;
|
|
16
|
+
/** Optional diagonal watermark stamped across the sheet. */
|
|
17
|
+
watermark?: string;
|
|
16
18
|
flat?: boolean;
|
|
17
19
|
className?: string;
|
|
18
20
|
children: ReactNode;
|
|
19
21
|
};
|
|
20
22
|
|
|
23
|
+
export function SheetWatermark({ text }: { text?: string }) {
|
|
24
|
+
const clean = text?.trim();
|
|
25
|
+
if (!clean) return null;
|
|
26
|
+
return (
|
|
27
|
+
<div
|
|
28
|
+
aria-hidden="true"
|
|
29
|
+
data-od-watermark=""
|
|
30
|
+
style={{
|
|
31
|
+
position: 'absolute',
|
|
32
|
+
inset: 0,
|
|
33
|
+
display: 'flex',
|
|
34
|
+
alignItems: 'center',
|
|
35
|
+
justifyContent: 'center',
|
|
36
|
+
pointerEvents: 'none',
|
|
37
|
+
userSelect: 'none',
|
|
38
|
+
zIndex: 40,
|
|
39
|
+
overflow: 'hidden',
|
|
40
|
+
}}
|
|
41
|
+
>
|
|
42
|
+
<span
|
|
43
|
+
style={{
|
|
44
|
+
transform: 'rotate(-34deg)',
|
|
45
|
+
fontFamily: 'var(--od-font-heading, sans-serif)',
|
|
46
|
+
fontSize: 68,
|
|
47
|
+
fontWeight: 800,
|
|
48
|
+
letterSpacing: '0.18em',
|
|
49
|
+
textTransform: 'uppercase',
|
|
50
|
+
color: 'var(--od-text, #16181d)',
|
|
51
|
+
opacity: 0.055,
|
|
52
|
+
whiteSpace: 'nowrap',
|
|
53
|
+
}}
|
|
54
|
+
>
|
|
55
|
+
{clean}
|
|
56
|
+
</span>
|
|
57
|
+
</div>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
21
61
|
/**
|
|
22
62
|
* One sheet of paper. The inner node always lays out at the page's true pixel
|
|
23
63
|
* size — authors write absolute px against A4 — and only the wrapper shrinks,
|
|
@@ -30,6 +70,7 @@ export function PageFrame({
|
|
|
30
70
|
scale,
|
|
31
71
|
design,
|
|
32
72
|
lang = 'en',
|
|
73
|
+
watermark,
|
|
33
74
|
flat = false,
|
|
34
75
|
className,
|
|
35
76
|
children,
|
|
@@ -67,6 +108,7 @@ export function PageFrame({
|
|
|
67
108
|
>
|
|
68
109
|
<DocPageProvider index={index} total={total}>
|
|
69
110
|
{children}
|
|
111
|
+
<SheetWatermark text={watermark} />
|
|
70
112
|
</DocPageProvider>
|
|
71
113
|
</div>
|
|
72
114
|
</div>
|
|
@@ -105,4 +105,19 @@ describe('layoutChart', () => {
|
|
|
105
105
|
const chart = layoutChart({ ...base, type: 'pie', data: [DATA[0] ?? {}], y: ['cost'] });
|
|
106
106
|
expect(chart.slices[0]?.path).toMatch(/ a.* a/);
|
|
107
107
|
});
|
|
108
|
+
|
|
109
|
+
it('fills an area path closed down to the baseline for area charts', () => {
|
|
110
|
+
const chart = layoutChart({ ...base, type: 'area', y: ['cost'] });
|
|
111
|
+
expect(chart.lines).toHaveLength(1);
|
|
112
|
+
expect(chart.lines[0]?.areaPath).toMatch(/^M.* L.* Z$/);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('lays out horizontal bars from left to right', () => {
|
|
116
|
+
const chart = layoutChart({ ...base, type: 'horizontal-bar', y: ['cost'] });
|
|
117
|
+
expect(chart.bars).toHaveLength(3);
|
|
118
|
+
for (const bar of chart.bars) {
|
|
119
|
+
expect(bar.x).toBeCloseTo(chart.plot.x, 1);
|
|
120
|
+
expect(bar.width).toBeGreaterThan(0);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
108
123
|
});
|
package/src/app/lib/chart.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
import { measureText } from '../../diagram/layout';
|
|
12
12
|
|
|
13
|
-
export type ChartType = 'bar' | 'line' | 'pie';
|
|
13
|
+
export type ChartType = 'bar' | 'line' | 'pie' | 'area' | 'horizontal-bar';
|
|
14
14
|
|
|
15
15
|
export type ChartFormat = 'number' | 'integer' | 'percent' | ((value: number) => string);
|
|
16
16
|
|
|
@@ -31,17 +31,22 @@ export type ChartSpec = {
|
|
|
31
31
|
|
|
32
32
|
export type Rect = { x: number; y: number; width: number; height: number };
|
|
33
33
|
export type Tick = { value: number; at: number; label: string };
|
|
34
|
-
export type CategoryLabel = { text: string; x: number };
|
|
34
|
+
export type CategoryLabel = { text: string; x: number; y?: number };
|
|
35
35
|
export type LegendItem = { name: string; series: number; x: number; y: number };
|
|
36
|
-
export type ValueLabel = { text: string; x: number; y: number; anchor: 'middle' | 'start' };
|
|
36
|
+
export type ValueLabel = { text: string; x: number; y: number; anchor: 'middle' | 'start' | 'end' };
|
|
37
37
|
export type Bar = Rect & { series: number; value: number };
|
|
38
|
-
export type Line = {
|
|
38
|
+
export type Line = {
|
|
39
|
+
series: number;
|
|
40
|
+
points: Array<{ x: number; y: number; value: number }>;
|
|
41
|
+
areaPath?: string;
|
|
42
|
+
};
|
|
39
43
|
export type Slice = { series: number; path: string; share: number; name: string };
|
|
40
44
|
|
|
41
45
|
export type ChartLayout = {
|
|
42
46
|
width: number;
|
|
43
47
|
height: number;
|
|
44
48
|
plot: Rect;
|
|
49
|
+
horizontal?: boolean;
|
|
45
50
|
/** Where zero sits on the value axis, when the axis has one. */
|
|
46
51
|
baseline: number | null;
|
|
47
52
|
ticks: Tick[];
|
|
@@ -190,15 +195,126 @@ function layoutPie(spec: ChartSpec): ChartLayout {
|
|
|
190
195
|
};
|
|
191
196
|
}
|
|
192
197
|
|
|
198
|
+
function layoutHorizontalBar(spec: ChartSpec): ChartLayout {
|
|
199
|
+
const names = spec.y.map((key) => seriesName(spec, key));
|
|
200
|
+
const categories = spec.data.map((row) => String(row[spec.x] ?? ''));
|
|
201
|
+
const matrix = spec.data.map((row) => spec.y.map((key) => toNumber(row[key])));
|
|
202
|
+
|
|
203
|
+
let lo = 0;
|
|
204
|
+
let hi = 0;
|
|
205
|
+
for (const values of matrix) {
|
|
206
|
+
if (spec.stacked) {
|
|
207
|
+
const up = values.reduce<number>((sum, v) => sum + Math.max(0, v ?? 0), 0);
|
|
208
|
+
const down = values.reduce<number>((sum, v) => sum + Math.min(0, v ?? 0), 0);
|
|
209
|
+
lo = Math.min(lo, down);
|
|
210
|
+
hi = Math.max(hi, up);
|
|
211
|
+
} else {
|
|
212
|
+
for (const v of values) {
|
|
213
|
+
if (v === null) continue;
|
|
214
|
+
lo = Math.min(lo, v);
|
|
215
|
+
hi = Math.max(hi, v);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (lo === 0 && hi === 0) hi = 1;
|
|
220
|
+
|
|
221
|
+
const tickValues = niceTicks(lo, hi);
|
|
222
|
+
const min = tickValues[0] ?? 0;
|
|
223
|
+
const max = tickValues[tickValues.length - 1] ?? 1;
|
|
224
|
+
const tickLabels = tickValues.map((value) => formatChartValue(value, spec.format));
|
|
225
|
+
|
|
226
|
+
const legendHeight = spec.y.length > 1 ? CHART_FONT_SIZE * 2.2 : 0;
|
|
227
|
+
const top = 6 + legendHeight;
|
|
228
|
+
const left =
|
|
229
|
+
Math.ceil(Math.max(24, ...categories.map((c) => measureText(c, CHART_FONT_SIZE))) * 1.12) + 12;
|
|
230
|
+
const bottom = CHART_FONT_SIZE * 2;
|
|
231
|
+
const right = spec.values ? 44 : 14;
|
|
232
|
+
const plot: Rect = {
|
|
233
|
+
x: left,
|
|
234
|
+
y: top,
|
|
235
|
+
width: Math.max(10, spec.width - left - right),
|
|
236
|
+
height: Math.max(10, spec.height - top - bottom),
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
const xOf = (value: number) => round(plot.x + ((value - min) / (max - min)) * plot.width);
|
|
240
|
+
|
|
241
|
+
const ticks = tickValues.map((value, i) => ({
|
|
242
|
+
value,
|
|
243
|
+
at: xOf(value),
|
|
244
|
+
label: tickLabels[i] ?? '',
|
|
245
|
+
}));
|
|
246
|
+
|
|
247
|
+
const band = plot.height / Math.max(1, categories.length);
|
|
248
|
+
const categoryLabels: CategoryLabel[] = categories.map((text, i) => ({
|
|
249
|
+
text,
|
|
250
|
+
x: plot.x - 8,
|
|
251
|
+
y: round(plot.y + band * (i + 0.5) + CHART_FONT_SIZE * 0.35),
|
|
252
|
+
}));
|
|
253
|
+
|
|
254
|
+
const bars: Bar[] = [];
|
|
255
|
+
const values: ValueLabel[] = [];
|
|
256
|
+
const group = band * 0.68;
|
|
257
|
+
const each = spec.stacked ? group : group / Math.max(1, spec.y.length);
|
|
258
|
+
|
|
259
|
+
matrix.forEach((row, i) => {
|
|
260
|
+
const startY = plot.y + band * i + (band - group) / 2;
|
|
261
|
+
let up = 0;
|
|
262
|
+
let down = 0;
|
|
263
|
+
row.forEach((value, series) => {
|
|
264
|
+
if (value === null) return;
|
|
265
|
+
let from: number;
|
|
266
|
+
let to: number;
|
|
267
|
+
if (spec.stacked) {
|
|
268
|
+
from = value >= 0 ? up : down;
|
|
269
|
+
to = from + value;
|
|
270
|
+
if (value >= 0) up = to;
|
|
271
|
+
else down = to;
|
|
272
|
+
} else {
|
|
273
|
+
from = 0;
|
|
274
|
+
to = value;
|
|
275
|
+
}
|
|
276
|
+
const x1 = xOf(Math.min(from, to));
|
|
277
|
+
const x2 = xOf(Math.max(from, to));
|
|
278
|
+
const y = round(spec.stacked ? startY : startY + each * series);
|
|
279
|
+
const height = round(Math.max(1, each - (spec.stacked ? 0 : 2)));
|
|
280
|
+
bars.push({ x: x1, y, width: round(Math.max(0, x2 - x1)), height, series, value });
|
|
281
|
+
if (spec.values && !spec.stacked) {
|
|
282
|
+
values.push({
|
|
283
|
+
text: formatChartValue(value, spec.format),
|
|
284
|
+
x: round(value >= 0 ? x2 + 5 : x1 - 5),
|
|
285
|
+
y: round(y + height / 2 + CHART_FONT_SIZE * 0.35),
|
|
286
|
+
anchor: value >= 0 ? 'start' : 'end',
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
return {
|
|
293
|
+
width: spec.width,
|
|
294
|
+
height: spec.height,
|
|
295
|
+
plot,
|
|
296
|
+
horizontal: true,
|
|
297
|
+
baseline: min <= 0 && max >= 0 ? xOf(0) : null,
|
|
298
|
+
ticks,
|
|
299
|
+
categories: categoryLabels,
|
|
300
|
+
bars,
|
|
301
|
+
lines: [],
|
|
302
|
+
slices: [],
|
|
303
|
+
legend: spec.y.length > 1 ? legendRow(names, plot.x, 6 + CHART_FONT_SIZE * 0.8) : [],
|
|
304
|
+
values,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
|
|
193
308
|
export function layoutChart(spec: ChartSpec): ChartLayout {
|
|
194
309
|
if (spec.type === 'pie') return layoutPie(spec);
|
|
310
|
+
if (spec.type === 'horizontal-bar') return layoutHorizontalBar(spec);
|
|
195
311
|
|
|
196
312
|
const names = spec.y.map((key) => seriesName(spec, key));
|
|
197
313
|
const categories = spec.data.map((row) => String(row[spec.x] ?? ''));
|
|
198
314
|
const matrix = spec.data.map((row) => spec.y.map((key) => toNumber(row[key])));
|
|
199
315
|
|
|
200
|
-
// The value range, with stacked bars summed per category. Bars
|
|
201
|
-
// on zero; a line keeps zero in view unless its values sit far above it.
|
|
316
|
+
// The value range, with stacked bars summed per category. Bars and area charts
|
|
317
|
+
// stand on zero; a line keeps zero in view unless its values sit far above it.
|
|
202
318
|
let lo = Number.POSITIVE_INFINITY;
|
|
203
319
|
let hi = Number.NEGATIVE_INFINITY;
|
|
204
320
|
for (const values of matrix) {
|
|
@@ -219,8 +335,9 @@ export function layoutChart(spec: ChartSpec): ChartLayout {
|
|
|
219
335
|
lo = 0;
|
|
220
336
|
hi = 1;
|
|
221
337
|
}
|
|
222
|
-
if (spec.type === 'bar' || (lo >= 0 && lo <= hi * 0.5))
|
|
223
|
-
|
|
338
|
+
if (spec.type === 'bar' || spec.type === 'area' || (lo >= 0 && lo <= hi * 0.5))
|
|
339
|
+
lo = Math.min(0, lo);
|
|
340
|
+
if (spec.type === 'bar' || spec.type === 'area') hi = Math.max(0, hi);
|
|
224
341
|
|
|
225
342
|
const tickValues = niceTicks(lo, hi);
|
|
226
343
|
const min = tickValues[0] ?? 0;
|
|
@@ -261,6 +378,7 @@ export function layoutChart(spec: ChartSpec): ChartLayout {
|
|
|
261
378
|
const bars: Bar[] = [];
|
|
262
379
|
const lines: Line[] = [];
|
|
263
380
|
const values: ValueLabel[] = [];
|
|
381
|
+
const baseLineY = yOf(Math.max(min, Math.min(max, 0)));
|
|
264
382
|
|
|
265
383
|
if (spec.type === 'bar') {
|
|
266
384
|
const group = band * 0.68;
|
|
@@ -313,7 +431,14 @@ export function layoutChart(spec: ChartSpec): ChartLayout {
|
|
|
313
431
|
? []
|
|
314
432
|
: [{ x: round(plot.x + band * (i + 0.5)), y: yOf(value), value }];
|
|
315
433
|
});
|
|
316
|
-
|
|
434
|
+
let areaPath: string | undefined;
|
|
435
|
+
if (spec.type === 'area' && points.length >= 2) {
|
|
436
|
+
const first = points[0];
|
|
437
|
+
const last = points[points.length - 1];
|
|
438
|
+
const strokePts = points.map((p, idx) => `${idx === 0 ? 'M' : 'L'}${p.x},${p.y}`).join(' ');
|
|
439
|
+
areaPath = `${strokePts} L${last.x},${baseLineY} L${first.x},${baseLineY} Z`;
|
|
440
|
+
}
|
|
441
|
+
lines.push({ series, points, ...(areaPath ? { areaPath } : {}) });
|
|
317
442
|
if (spec.values) {
|
|
318
443
|
for (const point of points) {
|
|
319
444
|
values.push({
|
|
@@ -3,6 +3,8 @@ import type { PageGeometry } from './sdk';
|
|
|
3
3
|
|
|
4
4
|
export type LayoutRule =
|
|
5
5
|
| 'unresolved-ref'
|
|
6
|
+
| 'duplicate-id'
|
|
7
|
+
| 'heading-skip'
|
|
6
8
|
| 'page-overflow'
|
|
7
9
|
| 'off-page'
|
|
8
10
|
| 'blank-page'
|
|
@@ -102,6 +104,8 @@ export function diagnosePages(
|
|
|
102
104
|
|
|
103
105
|
const findings: LayoutFinding[] = [];
|
|
104
106
|
const frames = Array.from(root.querySelectorAll<HTMLElement>(`[${PAGE_ATTR}]`));
|
|
107
|
+
const seenLabelIds = new Map<string, number>();
|
|
108
|
+
let lastHeadingLevel = 0;
|
|
105
109
|
|
|
106
110
|
frames.forEach((frame, index) => {
|
|
107
111
|
const page = pageNumberOf(frame, index);
|
|
@@ -115,6 +119,7 @@ export function diagnosePages(
|
|
|
115
119
|
let hasMedia = false;
|
|
116
120
|
|
|
117
121
|
for (const el of descendants) {
|
|
122
|
+
if (el.closest('[data-od-watermark]')) continue;
|
|
118
123
|
const rect = el.getBoundingClientRect();
|
|
119
124
|
if (!isPainted(el, rect)) continue;
|
|
120
125
|
if (MEDIA_TAGS.has(el.tagName)) hasMedia = true;
|
|
@@ -129,7 +134,9 @@ export function diagnosePages(
|
|
|
129
134
|
if (el instanceof HTMLImageElement && el.complete && el.naturalWidth === 0) broken.push(el);
|
|
130
135
|
}
|
|
131
136
|
|
|
132
|
-
const
|
|
137
|
+
const watermarkText = frame.querySelector('[data-od-watermark]')?.textContent?.trim() ?? '';
|
|
138
|
+
const rawText = (frame.textContent ?? '').trim();
|
|
139
|
+
const text = watermarkText && rawText === watermarkText ? '' : rawText;
|
|
133
140
|
// A page shell always paints — background, borders. What makes a sheet
|
|
134
141
|
// blank is that it carries neither words nor a picture.
|
|
135
142
|
if (text === '' && !hasMedia) {
|
|
@@ -205,6 +212,43 @@ export function diagnosePages(
|
|
|
205
212
|
});
|
|
206
213
|
}
|
|
207
214
|
|
|
215
|
+
for (const el of Array.from(frame.querySelectorAll<HTMLElement>('[data-od-label]'))) {
|
|
216
|
+
const kind = el.getAttribute('data-od-label');
|
|
217
|
+
if (kind !== 'figure' && kind !== 'table') continue;
|
|
218
|
+
const id = el.getAttribute('data-od-label-id');
|
|
219
|
+
if (!id) continue;
|
|
220
|
+
const prevPage = seenLabelIds.get(id);
|
|
221
|
+
if (prevPage !== undefined) {
|
|
222
|
+
findings.push({
|
|
223
|
+
page,
|
|
224
|
+
rule: 'duplicate-id',
|
|
225
|
+
severity: 'error',
|
|
226
|
+
message: `Duplicate ${kind} id "${id}" (already declared on p. ${prevPage}).`,
|
|
227
|
+
element: snippet(el),
|
|
228
|
+
loc: locOf(el),
|
|
229
|
+
});
|
|
230
|
+
} else {
|
|
231
|
+
seenLabelIds.set(id, page);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
for (const heading of Array.from(frame.querySelectorAll<HTMLElement>('h1, h2, h3, h4'))) {
|
|
236
|
+
if (heading.getAttribute('data-od-outline') === 'skip') continue;
|
|
237
|
+
const level = Number(heading.tagName.slice(1));
|
|
238
|
+
if (!Number.isFinite(level)) continue;
|
|
239
|
+
if (lastHeadingLevel > 0 && level > lastHeadingLevel + 1) {
|
|
240
|
+
findings.push({
|
|
241
|
+
page,
|
|
242
|
+
rule: 'heading-skip',
|
|
243
|
+
severity: 'warn',
|
|
244
|
+
message: `Heading jumps from <h${lastHeadingLevel}> to <h${level}> without an <h${lastHeadingLevel + 1}> in between.`,
|
|
245
|
+
element: snippet(heading),
|
|
246
|
+
loc: locOf(heading),
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
lastHeadingLevel = level;
|
|
250
|
+
}
|
|
251
|
+
|
|
208
252
|
findings.push(...danglingHeadings(frame, frameRect, geometry, page).slice(0, perPageLimit));
|
|
209
253
|
});
|
|
210
254
|
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import { createElement } from 'react';
|
|
8
8
|
import { createRoot, type Root } from 'react-dom/client';
|
|
9
|
+
import { SheetWatermark } from '../components/page-frame';
|
|
9
10
|
import { designToCssVars } from './design';
|
|
10
11
|
import { PAGE_ATTR, PAGE_INDEX_ATTR } from './outline';
|
|
11
12
|
import { DocPageProvider } from './page-context';
|
|
@@ -29,6 +30,7 @@ export async function withRenderedPages<T>(
|
|
|
29
30
|
geometry: PageGeometry,
|
|
30
31
|
doc: DocModule,
|
|
31
32
|
visit: (hosts: HTMLElement[], container: HTMLElement) => Promise<T> | T,
|
|
33
|
+
opts: { includeWatermark?: boolean } = {},
|
|
32
34
|
): Promise<T> {
|
|
33
35
|
const container = document.createElement('div');
|
|
34
36
|
container.setAttribute('aria-hidden', 'true');
|
|
@@ -61,7 +63,16 @@ export async function withRenderedPages<T>(
|
|
|
61
63
|
container.appendChild(host);
|
|
62
64
|
hosts.push(host);
|
|
63
65
|
const root = createRoot(host);
|
|
64
|
-
root.render(
|
|
66
|
+
root.render(
|
|
67
|
+
createElement(
|
|
68
|
+
DocPageProvider,
|
|
69
|
+
{ index: i, total: pages.length },
|
|
70
|
+
page.content,
|
|
71
|
+
opts.includeWatermark
|
|
72
|
+
? createElement(SheetWatermark, { text: doc.meta?.watermark })
|
|
73
|
+
: null,
|
|
74
|
+
),
|
|
75
|
+
);
|
|
65
76
|
roots.push(root);
|
|
66
77
|
}
|
|
67
78
|
|
|
@@ -88,7 +99,9 @@ export async function renderPagesToHtml(
|
|
|
88
99
|
geometry: PageGeometry,
|
|
89
100
|
doc: DocModule,
|
|
90
101
|
): Promise<string[]> {
|
|
91
|
-
return withRenderedPages(pages, geometry, doc, (hosts) => hosts.map((host) => host.innerHTML)
|
|
102
|
+
return withRenderedPages(pages, geometry, doc, (hosts) => hosts.map((host) => host.innerHTML), {
|
|
103
|
+
includeWatermark: true,
|
|
104
|
+
});
|
|
92
105
|
}
|
|
93
106
|
|
|
94
107
|
export function collectCss(): string {
|