@open-document/core 0.12.0 → 0.13.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 (48) hide show
  1. package/dist/{build-DfUfpeTR.js → build-CbgIk6bA.js} +1 -1
  2. package/dist/{check-CHxiOkQG.js → check-BTroj-5Z.js} +3 -3
  3. package/dist/cli/bin.js +47 -10
  4. package/dist/config-BWtOZ1jc.d.ts +45 -0
  5. package/dist/{config-9IdbhVuT.js → config-DKKNIbvO.js} +68 -320
  6. package/dist/{context-BLI1-64_.js → context-DXNP8v9I.js} +2 -1
  7. package/dist/deploy-gcp-BUiSuqz7.js +758 -0
  8. package/dist/{dev-yNYKv5mm.js → dev-YXumw5iX.js} +1 -1
  9. package/dist/{diagram-BK4jTZr4.js → diagram-CKf3ptOE.js} +293 -13
  10. package/dist/{diff-Bs205mOx.js → diff-CTFfU4Tz.js} +2 -2
  11. package/dist/{export-tkquS5O5.js → export-D2QLtF84.js} +3 -3
  12. package/dist/{sdk-CHEGeINh.d.ts → flow-CS5apebG.d.ts} +72 -3
  13. package/dist/{import-D1hIKIr3.js → import-CcmrFetU.js} +2 -2
  14. package/dist/index.d.ts +155 -59
  15. package/dist/index.js +896 -90
  16. package/dist/open-doc-plugin-DnX5usgI.js +320 -0
  17. package/dist/ops/index.d.ts +2 -2
  18. package/dist/ops/index.js +2 -2
  19. package/dist/{ops-Dgqujq5a.js → ops-DmEecq1k.js} +2 -1
  20. package/dist/{preview-BImdTH6K.js → preview-_mHpBhMk.js} +1 -1
  21. package/dist/{templates-DT5zzqHq.js → templates-MMblHwmT.js} +3 -3
  22. package/dist/vite/index.d.ts +1 -1
  23. package/dist/vite/index.js +2 -1
  24. package/package.json +1 -1
  25. package/skills/doc-authoring/SKILL.md +15 -16
  26. package/skills/doc-authoring/references/long-form.md +43 -2
  27. package/skills/doc-authoring/references/pagination.md +14 -2
  28. package/skills/doc-authoring/references/tables-and-charts.md +21 -17
  29. package/src/app/components/callout.tsx +101 -0
  30. package/src/app/components/chart.tsx +191 -33
  31. package/src/app/components/code-block.tsx +249 -0
  32. package/src/app/components/data-table.tsx +146 -18
  33. package/src/app/components/flow-page.tsx +57 -8
  34. package/src/app/components/numbering.tsx +226 -12
  35. package/src/app/components/page-frame.tsx +42 -0
  36. package/src/app/lib/chart.test.ts +15 -0
  37. package/src/app/lib/chart.ts +134 -9
  38. package/src/app/lib/diagnostics.ts +45 -1
  39. package/src/app/lib/export-dom.ts +15 -2
  40. package/src/app/lib/export-pdf.ts +9 -1
  41. package/src/app/lib/flow-measure.ts +33 -4
  42. package/src/app/lib/flow.test.ts +20 -1
  43. package/src/app/lib/flow.ts +120 -20
  44. package/src/app/lib/labels.ts +29 -10
  45. package/src/app/lib/sdk.ts +6 -0
  46. package/src/app/lib/use-doc-pages.ts +23 -8
  47. package/src/app/routes/doc.tsx +1 -0
  48. package/dist/config-P61niZMf.d.ts +0 -24
@@ -27,7 +27,13 @@ function toBlocks(node: ReactNode): ReactNode[] {
27
27
  */
28
28
  export function flow(
29
29
  children: ReactNode,
30
- opts: { header?: ComponentType; footer?: ComponentType; padding?: number } = {},
30
+ opts: {
31
+ header?: ComponentType;
32
+ footer?: ComponentType;
33
+ padding?: number;
34
+ columns?: 1 | 2 | 3;
35
+ columnGap?: number;
36
+ } = {},
31
37
  ): FlowSection {
32
38
  return { __odFlow: true, blocks: toBlocks(children), ...opts };
33
39
  }
@@ -36,6 +42,12 @@ export function isFlowSection(entry: DocEntry): entry is FlowSection {
36
42
  return typeof entry === 'object' && entry !== null && (entry as FlowSection).__odFlow === true;
37
43
  }
38
44
 
45
+ export type BlockSlice = {
46
+ index: number;
47
+ rowStart?: number;
48
+ rowEnd?: number;
49
+ };
50
+
39
51
  export type BlockMetrics = {
40
52
  height: number;
41
53
  /** Height this block's footnotes add to the foot of whatever page it lands on. */
@@ -46,11 +58,19 @@ export type BlockMetrics = {
46
58
  keepWithPrevious?: boolean;
47
59
  /** Always start a new page before this block. */
48
60
  breakBefore?: boolean;
61
+ /** When present, this block is a table that can split between rows across page breaks. */
62
+ splittable?: {
63
+ headerHeight: number;
64
+ footerHeight: number;
65
+ rowHeights: number[];
66
+ };
49
67
  };
50
68
 
51
69
  export type PaginationResult = {
52
70
  /** Block indices per page, in order. */
53
71
  pages: number[][];
72
+ /** Detailed block slices per page, carrying row ranges when a table splits across pages. */
73
+ slices: BlockSlice[][];
54
74
  /** Blocks taller than one page — they stay whole and overflow. */
55
75
  overflowing: number[];
56
76
  };
@@ -63,7 +83,9 @@ export type PaginateOptions = {
63
83
  /**
64
84
  * Greedy top-to-bottom packing: fill a page until the next block would cross
65
85
  * the bottom edge, then push that block — plus anything glued to it — onto the
66
- * next page. Blocks are atomic; nothing is split mid-block.
86
+ * next page. Blocks are atomic unless marked `splittable`, in which case rows
87
+ * pack into the remaining space and continue on the next sheet with the header
88
+ * repeated.
67
89
  *
68
90
  * Footnotes are packed from the same budget: a block that carries notes brings
69
91
  * their height with it, because those notes print at the foot of whichever page
@@ -75,22 +97,42 @@ export function paginateBlocks(
75
97
  opts: PaginateOptions = {},
76
98
  ): PaginationResult {
77
99
  const overhead = opts.footnoteOverhead ?? 0;
78
- const pages: number[][] = [];
100
+ const slices: BlockSlice[][] = [];
79
101
  const overflowing: number[] = [];
80
- let current: number[] = [];
102
+ let current: BlockSlice[] = [];
81
103
  let used = 0;
82
104
  let notes = 0;
83
105
 
84
- const heightOf = (indices: number[]) =>
85
- indices.reduce((sum, index) => sum + (blocks[index]?.height ?? 0), 0);
86
- const notesOf = (indices: number[]) =>
87
- indices.reduce((sum, index) => sum + (blocks[index]?.footnoteHeight ?? 0), 0);
106
+ const sliceHeight = (slice: BlockSlice): number => {
107
+ const block = blocks[slice.index];
108
+ if (!block) return 0;
109
+ if (slice.rowStart !== undefined && slice.rowEnd !== undefined && block.splittable) {
110
+ let rows = 0;
111
+ for (let r = slice.rowStart; r < slice.rowEnd; r++) {
112
+ rows += block.splittable.rowHeights[r] ?? 0;
113
+ }
114
+ const isLast = slice.rowEnd >= block.splittable.rowHeights.length;
115
+ return block.splittable.headerHeight + rows + (isLast ? block.splittable.footerHeight : 0);
116
+ }
117
+ return block.height;
118
+ };
119
+
120
+ const heightOf = (items: BlockSlice[]) => items.reduce((sum, item) => sum + sliceHeight(item), 0);
121
+ const notesOf = (items: BlockSlice[]) =>
122
+ items.reduce(
123
+ (sum, item) =>
124
+ sum +
125
+ (item.rowStart === undefined || item.rowStart === 0
126
+ ? (blocks[item.index]?.footnoteHeight ?? 0)
127
+ : 0),
128
+ 0,
129
+ );
88
130
  /** A page with no notes pays nothing; the first note also pays for the rule. */
89
131
  const reserve = (total: number) => (total > 0 ? total + overhead : 0);
90
132
 
91
133
  const closePage = () => {
92
134
  if (current.length === 0) return;
93
- pages.push(current);
135
+ slices.push(current);
94
136
  current = [];
95
137
  used = 0;
96
138
  notes = 0;
@@ -98,35 +140,93 @@ export function paginateBlocks(
98
140
 
99
141
  blocks.forEach((block, index) => {
100
142
  const own = block.footnoteHeight ?? 0;
101
- if (block.height + reserve(own) > available) overflowing.push(index);
102
143
 
103
144
  if (block.breakBefore) closePage();
104
145
 
146
+ const split = block.splittable;
147
+ const canSplit = split && split.rowHeights.length > 1;
148
+
149
+ if (!canSplit && block.height + reserve(own) > available) {
150
+ overflowing.push(index);
151
+ }
152
+
105
153
  if (current.length > 0 && used + block.height + reserve(notes + own) > available) {
106
154
  // Everything glued to this block travels with it: trailing headings
107
155
  // (keep-with-next) and, when this block is a caption, its figure.
108
156
  let splitAt = current.length;
109
- while (splitAt > 0 && blocks[current[splitAt - 1]]?.keepWithNext) splitAt--;
157
+ while (splitAt > 0 && blocks[current[splitAt - 1].index]?.keepWithNext) splitAt--;
110
158
  if (block.keepWithPrevious && splitAt === current.length) splitAt--;
111
159
 
112
- if (splitAt > 0 && splitAt < current.length) {
113
- const moved = current.slice(splitAt);
114
- pages.push(current.slice(0, splitAt));
115
- current = moved;
116
- used = heightOf(moved);
117
- notes = notesOf(moved);
160
+ if (canSplit) {
161
+ const firstRowCost = split.headerHeight + (split.rowHeights[0] ?? 0);
162
+ if (used + firstRowCost + reserve(notes + own) > available) {
163
+ if (splitAt > 0 && splitAt < current.length) {
164
+ const moved = current.slice(splitAt);
165
+ slices.push(current.slice(0, splitAt));
166
+ current = moved;
167
+ used = heightOf(moved);
168
+ notes = notesOf(moved);
169
+ } else {
170
+ closePage();
171
+ }
172
+ }
118
173
  } else {
119
- closePage();
174
+ if (splitAt > 0 && splitAt < current.length) {
175
+ const moved = current.slice(splitAt);
176
+ slices.push(current.slice(0, splitAt));
177
+ current = moved;
178
+ used = heightOf(moved);
179
+ notes = notesOf(moved);
180
+ } else {
181
+ closePage();
182
+ }
183
+ }
184
+ }
185
+
186
+ if (canSplit && used + block.height + reserve(notes + own) > available) {
187
+ let rowCursor = 0;
188
+ const totalRows = split.rowHeights.length;
189
+ let firstSlice = true;
190
+ while (rowCursor < totalRows) {
191
+ const sliceNotes = firstSlice ? own : 0;
192
+ const budget = available - used - reserve(notes + sliceNotes) - split.headerHeight;
193
+ let rowEnd = rowCursor;
194
+ let rowSum = 0;
195
+ while (rowEnd < totalRows) {
196
+ const rHeight = split.rowHeights[rowEnd] ?? 0;
197
+ const tail = rowEnd + 1 === totalRows ? split.footerHeight : 0;
198
+ if (rowSum + rHeight + tail <= budget || (rowEnd === rowCursor && current.length === 0)) {
199
+ rowSum += rHeight;
200
+ rowEnd++;
201
+ } else {
202
+ break;
203
+ }
204
+ }
205
+ if (rowEnd === rowCursor) {
206
+ closePage();
207
+ continue;
208
+ }
209
+ const slice: BlockSlice = { index, rowStart: rowCursor, rowEnd };
210
+ current.push(slice);
211
+ used += sliceHeight(slice);
212
+ notes += sliceNotes;
213
+ firstSlice = false;
214
+ rowCursor = rowEnd;
215
+ if (rowCursor < totalRows) {
216
+ closePage();
217
+ }
120
218
  }
219
+ return;
121
220
  }
122
221
 
123
- current.push(index);
222
+ current.push({ index });
124
223
  used += block.height;
125
224
  notes += own;
126
225
  });
127
226
 
128
227
  closePage();
129
- return { pages, overflowing };
228
+ const pages = slices.map((page) => page.map((s) => s.index));
229
+ return { pages, slices, overflowing };
130
230
  }
131
231
 
132
232
  /** Blocks of the first flow section, for thumbnails that skip measurement. */
@@ -1,7 +1,7 @@
1
1
  import { useSyncExternalStore } from 'react';
2
2
  import { PAGE_ATTR, PAGE_INDEX_ATTR } from './outline';
3
3
 
4
- export type LabelKind = 'figure' | 'table' | 'footnote';
4
+ export type LabelKind = 'figure' | 'table' | 'footnote' | 'citation';
5
5
 
6
6
  export type LabelEntry = {
7
7
  /** Author-supplied id for `<Ref to>`, or a generated one. */
@@ -29,6 +29,8 @@ export type LabelVocabulary = {
29
29
  table: string;
30
30
  /** Heading above a page's footnotes. Empty string renders no heading. */
31
31
  footnotes: string;
32
+ /** Heading above a bibliography section when not overridden. */
33
+ bibliography: string;
32
34
  /** Page suffix for a cross-reference. `{page}` is replaced. */
33
35
  onPage: string;
34
36
  };
@@ -37,6 +39,7 @@ export const defaultVocabulary: LabelVocabulary = {
37
39
  figure: 'Figure',
38
40
  table: 'Table',
39
41
  footnotes: '',
42
+ bibliography: 'References',
40
43
  onPage: ' (p. {page})',
41
44
  };
42
45
 
@@ -47,7 +50,7 @@ export type LabelSnapshot = {
47
50
 
48
51
  const EMPTY: LabelSnapshot = { entries: [], vocabulary: defaultVocabulary };
49
52
 
50
- const KINDS: LabelKind[] = ['figure', 'table', 'footnote'];
53
+ const KINDS: LabelKind[] = ['figure', 'table', 'footnote', 'citation'];
51
54
 
52
55
  function isKind(value: string | null): value is LabelKind {
53
56
  return value !== null && (KINDS as string[]).includes(value);
@@ -61,7 +64,12 @@ function isKind(value: string | null): value is LabelKind {
61
64
  */
62
65
  export function collectLabels(root: ParentNode): LabelEntry[] {
63
66
  const entries: LabelEntry[] = [];
64
- const counters: Record<LabelKind, number> = { figure: 0, table: 0, footnote: 0 };
67
+ const counters: Record<LabelKind, number> = {
68
+ figure: 0,
69
+ table: 0,
70
+ footnote: 0,
71
+ citation: 0,
72
+ };
65
73
  const frames = Array.from(root.querySelectorAll<HTMLElement>(`[${PAGE_ATTR}]`));
66
74
 
67
75
  frames.forEach((frame, fallbackIndex) => {
@@ -73,17 +81,23 @@ export function collectLabels(root: ParentNode): LabelEntry[] {
73
81
  if (!isKind(kind)) continue;
74
82
  const id = el.getAttribute(LABEL_ID_ATTR);
75
83
  if (!id) continue;
84
+ const text = (el.getAttribute(LABEL_TEXT_ATTR) ?? el.textContent ?? '')
85
+ .replace(/\s+/g, ' ')
86
+ .trim();
76
87
  // A footnote's marker and the note at the foot of the page carry the same
77
- // id; the marker is what fixes its position in the sequence.
78
- if (entries.some((entry) => entry.id === id)) continue;
88
+ // id; the marker is what fixes its position in the sequence. Same for repeated `<Cite id>`
89
+ // calls — the first appearance fixes its citation number, and later ones reuse it.
90
+ const existing = entries.find((entry) => entry.kind === kind && entry.id === id);
91
+ if (existing) {
92
+ if (!existing.text && text) existing.text = text;
93
+ continue;
94
+ }
79
95
  counters[kind] += 1;
80
96
  entries.push({
81
97
  id,
82
98
  kind,
83
99
  number: counters[kind],
84
- text: (el.getAttribute(LABEL_TEXT_ATTR) ?? el.textContent ?? '')
85
- .replace(/\s+/g, ' ')
86
- .trim(),
100
+ text,
87
101
  page,
88
102
  });
89
103
  }
@@ -124,6 +138,7 @@ function sameVocabulary(a: LabelVocabulary, b: LabelVocabulary): boolean {
124
138
  a.figure === b.figure &&
125
139
  a.table === b.table &&
126
140
  a.footnotes === b.footnotes &&
141
+ a.bibliography === b.bibliography &&
127
142
  a.onPage === b.onPage
128
143
  );
129
144
  }
@@ -162,8 +177,12 @@ export function useDocLabels(kind: LabelKind): LabelEntry[] {
162
177
  }
163
178
 
164
179
  /** One numbered item by id, once the scan has run. */
165
- export function useDocLabel(id: string): LabelEntry | null {
166
- return useLabelSnapshot().entries.find((entry) => entry.id === id) ?? null;
180
+ export function useDocLabel(id: string, kind?: LabelKind): LabelEntry | null {
181
+ return (
182
+ useLabelSnapshot().entries.find(
183
+ (entry) => entry.id === id && (kind === undefined || entry.kind === kind),
184
+ ) ?? null
185
+ );
167
186
  }
168
187
 
169
188
  export function useLabelVocabulary(): LabelVocabulary {
@@ -62,6 +62,10 @@ export type FlowSection = {
62
62
  footer?: ComponentType;
63
63
  /** Page padding override in px; defaults to the design's `margin`. */
64
64
  padding?: number;
65
+ /** Number of columns for body content inside each page of this flow section. */
66
+ columns?: 1 | 2 | 3;
67
+ /** Gap in px between columns when `columns > 1`; defaults to 24. */
68
+ columnGap?: number;
65
69
  };
66
70
 
67
71
  /** What a document's default export may contain: fixed pages, flow sections, or both. */
@@ -101,6 +105,8 @@ export type DocMeta = {
101
105
  recordName?: string;
102
106
  /** With `records`: the column that names a row in the viewer's picker. */
103
107
  recordLabel?: string;
108
+ /** Diagonal watermark text stamped across every sheet (e.g. 'CONFIDENTIAL', 'DRAFT'). */
109
+ watermark?: string;
104
110
  };
105
111
 
106
112
  /**
@@ -1,7 +1,13 @@
1
1
  import { createElement, type ReactNode, useEffect, useMemo, useState } from 'react';
2
2
  import { FlowPage } from '../components/flow-page';
3
3
  import type { DesignSystem } from './design';
4
- import { type DocEntry, type FlowSection, isFlowSection, paginateBlocks } from './flow';
4
+ import {
5
+ type BlockSlice,
6
+ type DocEntry,
7
+ type FlowSection,
8
+ isFlowSection,
9
+ paginateBlocks,
10
+ } from './flow';
5
11
  import { type MeasurableSection, measureFlowSections } from './flow-measure';
6
12
  import { extractSectionFootnotes, notesForPage, type PreparedSection } from './footnotes';
7
13
  import { useRecordIndex } from './records';
@@ -13,8 +19,8 @@ export type ExpandedPage = {
13
19
  };
14
20
 
15
21
  type Plan = {
16
- /** Section index → block indices per page. */
17
- bySection: number[][][];
22
+ /** Section index → block slices per page. */
23
+ bySection: BlockSlice[][][];
18
24
  overflowing: Array<{ section: number; block: number }>;
19
25
  };
20
26
 
@@ -59,6 +65,8 @@ export function useDocPages(
59
65
  blocks: prepared[index].blocks,
60
66
  notesByBlock: prepared[index].notesByBlock,
61
67
  ...(section.padding !== undefined ? { padding: section.padding } : {}),
68
+ ...(section.columns !== undefined ? { columns: section.columns } : {}),
69
+ ...(section.columnGap !== undefined ? { columnGap: section.columnGap } : {}),
62
70
  })),
63
71
  [sections, prepared],
64
72
  );
@@ -79,13 +87,13 @@ export function useDocPages(
79
87
  measureFlowSections(measurable, { geometry, design, lang })
80
88
  .then((measurements) => {
81
89
  if (cancelled) return;
82
- const bySection: number[][][] = [];
90
+ const bySection: BlockSlice[][][] = [];
83
91
  const overflowing: Plan['overflowing'] = [];
84
92
  measurements.forEach((measurement, sectionIndex) => {
85
93
  const result = paginateBlocks(measurement.metrics, measurement.available, {
86
94
  footnoteOverhead: measurement.footnoteOverhead,
87
95
  });
88
- bySection.push(result.pages);
96
+ bySection.push(result.slices);
89
97
  for (const block of result.overflowing) {
90
98
  overflowing.push({ section: sectionIndex, block });
91
99
  }
@@ -118,16 +126,23 @@ export function useDocPages(
118
126
  const blocks = ready?.blocks ?? section.blocks;
119
127
  // Before measurement lands, render the section as a single page so the
120
128
  // viewer shows something rather than flashing empty.
121
- const chunks = plan.bySection[sectionIndex] ?? [blocks.map((_, i) => i)];
122
- chunks.forEach((blockIndices, pageIndex) => {
129
+ const chunks: BlockSlice[][] = plan.bySection[sectionIndex] ?? [
130
+ blocks.map((_, index) => ({ index })),
131
+ ];
132
+ chunks.forEach((blockSlices, pageIndex) => {
133
+ const blockIndices = blockSlices.map((s) => s.index);
134
+ const noteIndices = blockSlices
135
+ .filter((s) => s.rowStart === undefined || s.rowStart === 0)
136
+ .map((s) => s.index);
123
137
  out.push({
124
138
  key: `f${entryIndex}-${pageIndex}`,
125
139
  content: createElement(FlowPage, {
126
140
  section,
127
141
  design,
128
142
  blockIndices,
143
+ blockSlices,
129
144
  blocks,
130
- notes: ready ? notesForPage(ready.notesByBlock, blockIndices) : [],
145
+ notes: ready ? notesForPage(ready.notesByBlock, noteIndices) : [],
131
146
  }),
132
147
  });
133
148
  });
@@ -1041,6 +1041,7 @@ export function Doc() {
1041
1041
  scale={scale}
1042
1042
  design={doc.design}
1043
1043
  lang={pageLang(doc.meta)}
1044
+ watermark={doc.meta?.watermark}
1044
1045
  >
1045
1046
  {page.content}
1046
1047
  </PageFrame>
@@ -1,24 +0,0 @@
1
- //#region src/config.d.ts
2
- type OpenDocBuildConfig = {
3
- /** Ship the document browser (`/`) in the static build. Defaults to true. */
4
- showDocBrowser?: boolean;
5
- /** Offer the "Export HTML" button in the static build. Defaults to true. */
6
- allowHtmlExport?: boolean;
7
- };
8
- type OpenDocConfig = {
9
- base?: string;
10
- /**
11
- * Where the viewer's back affordance goes. Unset it points at this app's own
12
- * document browser; set it to escape the app entirely — a viewer mounted
13
- * under a larger site needs to return to that site, not to its own index.
14
- */
15
- home?: string;
16
- docsDir?: string;
17
- themesDir?: string;
18
- assetsDir?: string;
19
- port?: number;
20
- allowedHosts?: string[] | true;
21
- build?: OpenDocBuildConfig;
22
- };
23
- //#endregion
24
- export { OpenDocConfig as n, OpenDocBuildConfig as t };