@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.
- package/dist/{build-DfUfpeTR.js → build-CbgIk6bA.js} +1 -1
- package/dist/{check-CHxiOkQG.js → check-BTroj-5Z.js} +3 -3
- package/dist/cli/bin.js +47 -10
- package/dist/config-BWtOZ1jc.d.ts +45 -0
- package/dist/{config-9IdbhVuT.js → config-DKKNIbvO.js} +68 -320
- package/dist/{context-BLI1-64_.js → context-DXNP8v9I.js} +2 -1
- package/dist/deploy-gcp-BUiSuqz7.js +758 -0
- package/dist/{dev-yNYKv5mm.js → dev-YXumw5iX.js} +1 -1
- package/dist/{diagram-BK4jTZr4.js → diagram-CKf3ptOE.js} +293 -13
- package/dist/{diff-Bs205mOx.js → diff-CTFfU4Tz.js} +2 -2
- package/dist/{export-tkquS5O5.js → export-D2QLtF84.js} +3 -3
- package/dist/{sdk-CHEGeINh.d.ts → flow-CS5apebG.d.ts} +72 -3
- package/dist/{import-D1hIKIr3.js → import-CcmrFetU.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 +2 -2
- package/dist/ops/index.js +2 -2
- package/dist/{ops-Dgqujq5a.js → ops-DmEecq1k.js} +2 -1
- package/dist/{preview-BImdTH6K.js → preview-_mHpBhMk.js} +1 -1
- package/dist/{templates-DT5zzqHq.js → templates-MMblHwmT.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
|
@@ -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 {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createElement } from 'react';
|
|
2
2
|
import { createRoot, type Root } from 'react-dom/client';
|
|
3
|
+
import { SheetWatermark } from '../components/page-frame';
|
|
3
4
|
import { designToCssVars } from './design';
|
|
4
5
|
import { PAGE_ATTR, PAGE_INDEX_ATTR } from './outline';
|
|
5
6
|
import { DocPageProvider } from './page-context';
|
|
@@ -124,7 +125,14 @@ export async function mountPrintCopy(
|
|
|
124
125
|
}
|
|
125
126
|
root.appendChild(host);
|
|
126
127
|
const r = createRoot(host);
|
|
127
|
-
r.render(
|
|
128
|
+
r.render(
|
|
129
|
+
createElement(
|
|
130
|
+
DocPageProvider,
|
|
131
|
+
{ index: i, total },
|
|
132
|
+
page.content,
|
|
133
|
+
createElement(SheetWatermark, { text: doc.meta?.watermark }),
|
|
134
|
+
),
|
|
135
|
+
);
|
|
128
136
|
reactRoots.push(r);
|
|
129
137
|
onProgress?.({
|
|
130
138
|
phase: 'rendering',
|
|
@@ -17,6 +17,8 @@ export type MeasurableSection = {
|
|
|
17
17
|
/** Index-aligned with `blocks`. */
|
|
18
18
|
notesByBlock: ExtractedNote[][];
|
|
19
19
|
padding?: number;
|
|
20
|
+
columns?: 1 | 2 | 3;
|
|
21
|
+
columnGap?: number;
|
|
20
22
|
};
|
|
21
23
|
|
|
22
24
|
export function metricsFor(el: HTMLElement, height: number, footnoteHeight: number): BlockMetrics {
|
|
@@ -29,6 +31,27 @@ export function metricsFor(el: HTMLElement, height: number, footnoteHeight: numb
|
|
|
29
31
|
const declaredKeepPrev = inner?.getAttribute('data-od-keep-with-previous') ?? null;
|
|
30
32
|
const declaredBreak = inner?.getAttribute('data-od-break-before') ?? null;
|
|
31
33
|
|
|
34
|
+
let splittable: BlockMetrics['splittable'];
|
|
35
|
+
if (typeof el.querySelector === 'function') {
|
|
36
|
+
const splitTable = el.querySelector<HTMLTableElement>('table[data-od-splittable]');
|
|
37
|
+
if (splitTable) {
|
|
38
|
+
const tbodyRows = Array.from(splitTable.querySelectorAll<HTMLElement>('tbody > tr'));
|
|
39
|
+
if (tbodyRows.length > 1) {
|
|
40
|
+
const blockRect = el.getBoundingClientRect();
|
|
41
|
+
const firstRowRect = tbodyRows[0].getBoundingClientRect();
|
|
42
|
+
const lastRowRect = tbodyRows[tbodyRows.length - 1].getBoundingClientRect();
|
|
43
|
+
const headerHeight = Math.max(0, firstRowRect.top - blockRect.top);
|
|
44
|
+
const footerHeight = Math.max(0, height - (lastRowRect.bottom - blockRect.top));
|
|
45
|
+
const rowHeights = tbodyRows.map((row, idx) => {
|
|
46
|
+
const r = row.getBoundingClientRect();
|
|
47
|
+
const next = tbodyRows[idx + 1]?.getBoundingClientRect();
|
|
48
|
+
return Math.max(0, (next ? next.top : lastRowRect.bottom) - r.top);
|
|
49
|
+
});
|
|
50
|
+
splittable = { headerHeight, footerHeight, rowHeights };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
32
55
|
return {
|
|
33
56
|
height,
|
|
34
57
|
footnoteHeight,
|
|
@@ -37,6 +60,7 @@ export function metricsFor(el: HTMLElement, height: number, footnoteHeight: numb
|
|
|
37
60
|
keepWithNext: declaredKeepNext !== null ? declaredKeepNext !== 'false' : HEADING_TAGS.has(tag),
|
|
38
61
|
keepWithPrevious: declaredKeepPrev !== null && declaredKeepPrev !== 'false',
|
|
39
62
|
breakBefore: declaredBreak !== null && declaredBreak !== 'false',
|
|
63
|
+
...(splittable ? { splittable } : {}),
|
|
40
64
|
};
|
|
41
65
|
}
|
|
42
66
|
|
|
@@ -122,9 +146,12 @@ export async function measureFlowSections(
|
|
|
122
146
|
// Out of the effect that called us, so `flushSync` below may commit.
|
|
123
147
|
await Promise.resolve();
|
|
124
148
|
for (const section of sections) {
|
|
125
|
-
const
|
|
149
|
+
const fullWidth = opts.geometry.width - paddingOf(section) * 2;
|
|
150
|
+
const cols = section.columns && section.columns > 1 ? section.columns : 1;
|
|
151
|
+
const gap = section.columnGap ?? 24;
|
|
152
|
+
const blockWidth = cols > 1 ? (fullWidth - gap * (cols - 1)) / cols : fullWidth;
|
|
126
153
|
|
|
127
|
-
const blocks = makeContainer(
|
|
154
|
+
const blocks = makeContainer(blockWidth);
|
|
128
155
|
blockContainers.push(blocks);
|
|
129
156
|
const blockRoot = createRoot(blocks);
|
|
130
157
|
// Committed now, not on React's schedule: on a slow machine a render
|
|
@@ -144,7 +171,7 @@ export async function measureFlowSections(
|
|
|
144
171
|
noteContainers.push(null);
|
|
145
172
|
continue;
|
|
146
173
|
}
|
|
147
|
-
const notes = makeContainer(
|
|
174
|
+
const notes = makeContainer(fullWidth);
|
|
148
175
|
noteContainers.push(notes);
|
|
149
176
|
const noteRoot = createRoot(notes);
|
|
150
177
|
flushSync(() => noteRoot.render(createElement(Footnotes, { notes: allNotes })));
|
|
@@ -168,6 +195,7 @@ export async function measureFlowSections(
|
|
|
168
195
|
|
|
169
196
|
return sections.map((section, sectionIndex) => {
|
|
170
197
|
const padding = paddingOf(section);
|
|
198
|
+
const cols = section.columns && section.columns > 1 ? section.columns : 1;
|
|
171
199
|
const blocks = blockContainers[sectionIndex];
|
|
172
200
|
const blockHeights = stackedHeights(blocks, `:scope > [${FLOW_BLOCK_ATTR}]`);
|
|
173
201
|
|
|
@@ -190,7 +218,8 @@ export async function measureFlowSections(
|
|
|
190
218
|
let notesHeight = 0;
|
|
191
219
|
for (let i = 0; i < count; i++) notesHeight += rowHeights[noteCursor + i] ?? 0;
|
|
192
220
|
noteCursor += count;
|
|
193
|
-
|
|
221
|
+
const rawHeight = blockHeights[index] ?? 0;
|
|
222
|
+
return metricsFor(node, cols > 1 ? rawHeight / cols : rawHeight, notesHeight);
|
|
194
223
|
});
|
|
195
224
|
|
|
196
225
|
return {
|
package/src/app/lib/flow.test.ts
CHANGED
|
@@ -59,7 +59,26 @@ describe('paginateBlocks', () => {
|
|
|
59
59
|
});
|
|
60
60
|
|
|
61
61
|
it('returns no pages for no blocks', () => {
|
|
62
|
-
expect(paginateBlocks([], 1000)).toEqual({ pages: [], overflowing: [] });
|
|
62
|
+
expect(paginateBlocks([], 1000)).toEqual({ pages: [], slices: [], overflowing: [] });
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('splits a tall splittable table across pages with repeated header', () => {
|
|
66
|
+
const tableBlock = b(680, {
|
|
67
|
+
splittable: {
|
|
68
|
+
headerHeight: 40,
|
|
69
|
+
footerHeight: 40,
|
|
70
|
+
rowHeights: [100, 100, 100, 100, 100, 100],
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
const result = paginateBlocks([b(500), tableBlock], 1000);
|
|
74
|
+
// Page 1 has 500px left: header(40) + 4 rows(400) = 440 <= 500.
|
|
75
|
+
// Page 2 gets the remaining 2 rows + header(40) + footer(40).
|
|
76
|
+
expect(result.pages).toEqual([[0, 1], [1]]);
|
|
77
|
+
expect(result.slices).toEqual([
|
|
78
|
+
[{ index: 0 }, { index: 1, rowStart: 0, rowEnd: 4 }],
|
|
79
|
+
[{ index: 1, rowStart: 4, rowEnd: 6 }],
|
|
80
|
+
]);
|
|
81
|
+
expect(result.overflowing).toEqual([]);
|
|
63
82
|
});
|
|
64
83
|
});
|
|
65
84
|
|