@open-document/core 0.11.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-BHFGY8eF.js → build-CbgIk6bA.js} +1 -1
- package/dist/{check-UEBmDfT_.js → check-BTroj-5Z.js} +3 -3
- package/dist/cli/bin.js +58 -34
- package/dist/config-BWtOZ1jc.d.ts +45 -0
- package/dist/{config-B_pdNbYg.js → config-DKKNIbvO.js} +1458 -983
- package/dist/{context-BM9_-u9P.js → context-DXNP8v9I.js} +3 -2
- package/dist/deploy-gcp-BUiSuqz7.js +758 -0
- package/dist/{dev-DX3QMIRi.js → dev-YXumw5iX.js} +1 -1
- package/dist/{diagram-BK4jTZr4.js → diagram-CKf3ptOE.js} +293 -13
- package/dist/{diff-DayvbXfX.js → diff-CTFfU4Tz.js} +2 -2
- package/dist/{export-BPsXj1i6.js → export-D2QLtF84.js} +11 -4
- package/dist/flow-CS5apebG.d.ts +252 -0
- package/dist/{import-71wlAsrd.js → import-CcmrFetU.js} +3 -2
- package/dist/index.d.ts +155 -59
- package/dist/index.js +945 -104
- package/dist/open-doc-plugin-DnX5usgI.js +320 -0
- package/dist/ops/index.d.ts +112 -3
- package/dist/ops/index.js +3 -3
- package/dist/{ops-BUo67DaP.js → ops-DmEecq1k.js} +86 -164
- package/dist/package-version-C89gtYSk.js +36 -0
- package/dist/{preview-D3m1-gUL.js → preview-_mHpBhMk.js} +1 -1
- package/dist/templates-MMblHwmT.js +34 -0
- package/dist/vite/index.d.ts +1 -1
- package/dist/vite/index.js +2 -1
- package/package.json +2 -1
- package/skills/apply-comments/SKILL.md +3 -1
- package/skills/create-doc/SKILL.md +2 -0
- package/skills/doc-authoring/SKILL.md +17 -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/browser/browser-ui.tsx +21 -0
- package/src/app/components/browser/new-document-dialog.tsx +208 -0
- package/src/app/components/callout.tsx +101 -0
- package/src/app/components/changes/changes-overlay.tsx +68 -0
- package/src/app/components/changes/changes-panel.tsx +341 -0
- package/src/app/components/changes/use-changes.ts +85 -0
- package/src/app/components/chart.tsx +191 -33
- package/src/app/components/code-block.tsx +249 -0
- package/src/app/components/comments/comments-overlay.tsx +50 -0
- package/src/app/components/comments/comments-panel.tsx +272 -0
- package/src/app/components/comments/use-comments.ts +61 -0
- package/src/app/components/data-table.tsx +146 -18
- package/src/app/components/doc-sidebar.tsx +32 -2
- package/src/app/components/flow-page.tsx +57 -8
- package/src/app/components/inspector/field-source.tsx +41 -0
- package/src/app/components/inspector/inspector.tsx +64 -65
- package/src/app/components/numbering.tsx +226 -12
- package/src/app/components/overlay-frame.tsx +79 -0
- package/src/app/components/page-frame.tsx +46 -0
- package/src/app/components/page-placement.ts +68 -0
- package/src/app/components/record-picker.tsx +79 -0
- package/src/app/components/themes/theme-preview.tsx +5 -4
- package/src/app/lib/agent-bridge.ts +29 -1
- 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/docx/ooxml.test.ts +24 -1
- package/src/app/lib/docx/ooxml.ts +27 -1
- package/src/app/lib/export-docx.ts +3 -1
- package/src/app/lib/export-dom.ts +17 -3
- package/src/app/lib/export-html.ts +4 -2
- package/src/app/lib/export-image.ts +2 -1
- package/src/app/lib/export-pdf.ts +9 -1
- package/src/app/lib/flow-measure.test.ts +30 -0
- package/src/app/lib/flow-measure.ts +68 -14
- package/src/app/lib/flow.test.ts +20 -1
- package/src/app/lib/flow.ts +120 -20
- package/src/app/lib/i18n-ja/browser.ts +10 -0
- package/src/app/lib/i18n-ja/doc.ts +4 -0
- package/src/app/lib/i18n-ja/panels.ts +40 -0
- package/src/app/lib/i18n-ko/browser.ts +10 -0
- package/src/app/lib/i18n-ko/doc.ts +4 -0
- package/src/app/lib/i18n-ko/panels.ts +39 -0
- package/src/app/lib/i18n-zh-cn/browser.ts +10 -0
- package/src/app/lib/i18n-zh-cn/doc.ts +4 -0
- package/src/app/lib/i18n-zh-cn/panels.ts +38 -0
- package/src/app/lib/i18n-zh-tw/browser.ts +10 -0
- package/src/app/lib/i18n-zh-tw/doc.ts +4 -0
- package/src/app/lib/i18n-zh-tw/panels.ts +38 -0
- package/src/app/lib/labels.ts +29 -10
- package/src/app/lib/records.test.ts +38 -0
- package/src/app/lib/records.ts +127 -0
- package/src/app/lib/sdk.ts +35 -0
- package/src/app/lib/use-doc-module.ts +7 -0
- package/src/app/lib/use-doc-pages.ts +38 -17
- package/src/app/routes/doc.tsx +275 -13
- package/src/app/routes/home.tsx +30 -5
- package/src/app/styles.css +23 -0
- package/templates/blank/index.tsx +63 -0
- package/templates/blank/template.json +6 -0
- package/templates/certificate/data/recipients.csv +4 -0
- package/templates/certificate/index.tsx +128 -0
- package/templates/certificate/template.json +6 -0
- package/templates/letter/index.tsx +106 -0
- package/templates/letter/template.json +5 -0
- package/templates/meeting-notes/index.tsx +194 -0
- package/templates/meeting-notes/template.json +6 -0
- package/templates/proposal/index.tsx +264 -0
- package/templates/proposal/template.json +6 -0
- package/templates/report/data/metrics.csv +5 -0
- package/templates/report/index.tsx +224 -0
- package/templates/report/template.json +6 -0
- package/dist/config-P61niZMf.d.ts +0 -24
- package/dist/sdk-CpDEGRIo.d.ts +0 -138
|
@@ -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
|
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
buildDocxParts,
|
|
4
|
+
type DocxModel,
|
|
5
|
+
escapeXml,
|
|
6
|
+
fontsFor,
|
|
7
|
+
hexColor,
|
|
8
|
+
wordLang,
|
|
9
|
+
} from './ooxml.ts';
|
|
3
10
|
|
|
4
11
|
describe('fontsFor', () => {
|
|
5
12
|
it('skips system aliases for the first real family', () => {
|
|
@@ -313,3 +320,19 @@ describe('buildDocxParts', () => {
|
|
|
313
320
|
expect(numbering).toContain('<w:num w:numId="2"><w:abstractNumId w:val="1"/><w:lvlOverride');
|
|
314
321
|
});
|
|
315
322
|
});
|
|
323
|
+
|
|
324
|
+
describe('wordLang', () => {
|
|
325
|
+
it('files Chinese, Japanese and Korean as the East Asian language', () => {
|
|
326
|
+
expect(wordLang('zh-Hant-TW')).toBe('<w:lang w:val="en-US" w:eastAsia="zh-TW"/>');
|
|
327
|
+
expect(wordLang('zh-Hans')).toBe('<w:lang w:val="en-US" w:eastAsia="zh-CN"/>');
|
|
328
|
+
expect(wordLang('zh-HK')).toBe('<w:lang w:val="en-US" w:eastAsia="zh-HK"/>');
|
|
329
|
+
expect(wordLang('ja')).toBe('<w:lang w:val="en-US" w:eastAsia="ja-JP"/>');
|
|
330
|
+
expect(wordLang('ko-KR')).toBe('<w:lang w:val="en-US" w:eastAsia="ko-KR"/>');
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it('files anything else as the Latin language', () => {
|
|
334
|
+
expect(wordLang()).toBe('<w:lang w:val="en-US"/>');
|
|
335
|
+
expect(wordLang('en-GB')).toBe('<w:lang w:val="en-GB"/>');
|
|
336
|
+
expect(wordLang('de"x')).toBe('<w:lang w:val="dex"/>');
|
|
337
|
+
});
|
|
338
|
+
});
|
|
@@ -169,8 +169,34 @@ export type DocxModel = {
|
|
|
169
169
|
images: ImagePart[];
|
|
170
170
|
/** One entry per list; ordered lists restart at 1 each. */
|
|
171
171
|
lists: Array<'bullet' | 'decimal'>;
|
|
172
|
+
/** The document's language as a BCP 47 tag — see `pageLang`. */
|
|
173
|
+
lang?: string;
|
|
172
174
|
};
|
|
173
175
|
|
|
176
|
+
/**
|
|
177
|
+
* The document's language the way Word files it. Word keeps Latin and East
|
|
178
|
+
* Asian text apart: a Chinese document still has Latin runs, and its
|
|
179
|
+
* proofing, line-breaking (kinsoku) and fallback fonts follow the East Asian
|
|
180
|
+
* language, so that one goes in `eastAsia`.
|
|
181
|
+
*/
|
|
182
|
+
export function wordLang(lang = 'en'): string {
|
|
183
|
+
const tag = lang.toLowerCase();
|
|
184
|
+
const eastAsia = tag.startsWith('zh')
|
|
185
|
+
? /hant|-tw|-hk|-mo/.test(tag)
|
|
186
|
+
? tag.includes('-hk')
|
|
187
|
+
? 'zh-HK'
|
|
188
|
+
: 'zh-TW'
|
|
189
|
+
: 'zh-CN'
|
|
190
|
+
: tag.startsWith('ja')
|
|
191
|
+
? 'ja-JP'
|
|
192
|
+
: tag.startsWith('ko')
|
|
193
|
+
? 'ko-KR'
|
|
194
|
+
: null;
|
|
195
|
+
if (eastAsia) return `<w:lang w:val="en-US" w:eastAsia="${eastAsia}"/>`;
|
|
196
|
+
const latin = tag === 'en' ? 'en-US' : lang.replace(/[^A-Za-z0-9-]/g, '');
|
|
197
|
+
return `<w:lang w:val="${latin}"/>`;
|
|
198
|
+
}
|
|
199
|
+
|
|
174
200
|
export const PX_TO_TWIPS = 15;
|
|
175
201
|
export const PX_TO_EMU = 9525;
|
|
176
202
|
export const PX_TO_HALF_POINTS = 1.5;
|
|
@@ -546,7 +572,7 @@ function stylesXml(model: DocxModel): string {
|
|
|
546
572
|
`<w:style w:type="paragraph" w:styleId="TOC${level}"><w:name w:val="toc ${level}"/><w:basedOn w:val="Normal"/><w:next w:val="Normal"/><w:uiPriority w:val="39"/><w:pPr><w:tabs><w:tab w:val="right" w:leader="dot" w:pos="${textWidth}"/></w:tabs><w:spacing w:after="60"/><w:ind w:left="${(level - 1) * 240}"/></w:pPr></w:style>`;
|
|
547
573
|
return (
|
|
548
574
|
`${XML_HEAD}<w:styles ${NS}>` +
|
|
549
|
-
`<w:docDefaults><w:rPrDefault><w:rPr>${fontsXml(fonts.body)}<w:color w:val="${colors.text}"/><w:sz w:val="${sizes.body}"/><w:szCs w:val="${sizes.body}"
|
|
575
|
+
`<w:docDefaults><w:rPrDefault><w:rPr>${fontsXml(fonts.body)}<w:color w:val="${colors.text}"/><w:sz w:val="${sizes.body}"/><w:szCs w:val="${sizes.body}"/>${wordLang(model.lang)}</w:rPr></w:rPrDefault>` +
|
|
550
576
|
`<w:pPrDefault><w:pPr><w:spacing w:after="160" w:line="${Math.round(model.line)}" w:lineRule="atLeast"/></w:pPr></w:pPrDefault></w:docDefaults>` +
|
|
551
577
|
'<w:style w:type="paragraph" w:default="1" w:styleId="Normal"><w:name w:val="Normal"/><w:qFormat/></w:style>' +
|
|
552
578
|
'<w:style w:type="character" w:default="1" w:styleId="DefaultParagraphFont"><w:name w:val="Default Paragraph Font"/><w:uiPriority w:val="1"/><w:semiHidden/></w:style>' +
|
|
@@ -27,7 +27,7 @@ import { downloadBlob, withRenderedPages } from './export-dom';
|
|
|
27
27
|
import { type DocEntry, type FlowSection, isFlowSection } from './flow';
|
|
28
28
|
import { DocPageProvider } from './page-context';
|
|
29
29
|
import { nextFrame, waitForFonts } from './print-ready';
|
|
30
|
-
import { type DocModule, resolvePageGeometry } from './sdk';
|
|
30
|
+
import { type DocModule, pageLang, resolvePageGeometry } from './sdk';
|
|
31
31
|
import type { ExpandedPage } from './use-doc-pages';
|
|
32
32
|
|
|
33
33
|
export type DocxBundle = { filename: string; mimeType: string; bytes: Uint8Array };
|
|
@@ -108,6 +108,7 @@ async function runningLineOf(
|
|
|
108
108
|
host.setAttribute('aria-hidden', 'true');
|
|
109
109
|
// The sheet's base styles (styles.css), so the line reads as it prints.
|
|
110
110
|
host.setAttribute('data-od-measure', '');
|
|
111
|
+
host.setAttribute('lang', pageLang(doc.meta));
|
|
111
112
|
Object.assign(host.style, { position: 'fixed', left: '-99999px', top: '0', width: `${width}px` });
|
|
112
113
|
for (const [name, value] of Object.entries(designToCssVars(design))) {
|
|
113
114
|
host.style.setProperty(name, value);
|
|
@@ -178,6 +179,7 @@ export async function buildDocDocx(
|
|
|
178
179
|
const size = (px: number) => Math.round(px * PX_TO_HALF_POINTS);
|
|
179
180
|
const text = hexColor(design.palette.text) ?? '000000';
|
|
180
181
|
const model: DocxModel = {
|
|
182
|
+
lang: pageLang(doc.meta),
|
|
181
183
|
title: doc.meta?.title ?? docId,
|
|
182
184
|
...(doc.meta?.author ? { author: doc.meta.author } : {}),
|
|
183
185
|
page: {
|
|
@@ -6,12 +6,13 @@
|
|
|
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';
|
|
12
13
|
import { nextFrame, waitForFonts, waitForImages } from './print-ready';
|
|
13
14
|
import { captureScan, restoreScan, scanDocument } from './scan';
|
|
14
|
-
import type
|
|
15
|
+
import { type DocModule, type PageGeometry, pageLang } from './sdk';
|
|
15
16
|
import { scopeSvgIds } from './svg-ids';
|
|
16
17
|
import type { ExpandedPage } from './use-doc-pages';
|
|
17
18
|
|
|
@@ -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');
|
|
@@ -52,6 +54,7 @@ export async function withRenderedPages<T>(
|
|
|
52
54
|
const host = document.createElement('div');
|
|
53
55
|
host.setAttribute(PAGE_ATTR, '');
|
|
54
56
|
host.setAttribute(PAGE_INDEX_ATTR, String(i));
|
|
57
|
+
host.setAttribute('lang', pageLang(doc.meta));
|
|
55
58
|
host.style.width = `${geometry.width}px`;
|
|
56
59
|
host.style.height = `${geometry.height}px`;
|
|
57
60
|
if (designVars) {
|
|
@@ -60,7 +63,16 @@ export async function withRenderedPages<T>(
|
|
|
60
63
|
container.appendChild(host);
|
|
61
64
|
hosts.push(host);
|
|
62
65
|
const root = createRoot(host);
|
|
63
|
-
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
|
+
);
|
|
64
76
|
roots.push(root);
|
|
65
77
|
}
|
|
66
78
|
|
|
@@ -87,7 +99,9 @@ export async function renderPagesToHtml(
|
|
|
87
99
|
geometry: PageGeometry,
|
|
88
100
|
doc: DocModule,
|
|
89
101
|
): Promise<string[]> {
|
|
90
|
-
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
|
+
});
|
|
91
105
|
}
|
|
92
106
|
|
|
93
107
|
export function collectCss(): string {
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
uniqueAssetName,
|
|
13
13
|
} from './export-dom';
|
|
14
14
|
import { PAGE_ATTR, PAGE_INDEX_ATTR } from './outline';
|
|
15
|
-
import { type DocModule, type PageGeometry, resolvePageGeometry } from './sdk';
|
|
15
|
+
import { type DocModule, type PageGeometry, pageLang, resolvePageGeometry } from './sdk';
|
|
16
16
|
import type { ExpandedPage } from './use-doc-pages';
|
|
17
17
|
|
|
18
18
|
type AssetEntry = { name: string; bytes: Uint8Array };
|
|
@@ -61,6 +61,7 @@ export async function buildDocHtmlBundle(
|
|
|
61
61
|
|
|
62
62
|
const html = buildHtml({
|
|
63
63
|
title,
|
|
64
|
+
lang: pageLang(doc.meta),
|
|
64
65
|
geometry,
|
|
65
66
|
design: doc.design,
|
|
66
67
|
pagesHtml: pagesHtml.map((page) => rewriteUrls(page, assets, 'html')),
|
|
@@ -112,6 +113,7 @@ function rewriteUrls(
|
|
|
112
113
|
|
|
113
114
|
function buildHtml(opts: {
|
|
114
115
|
title: string;
|
|
116
|
+
lang: string;
|
|
115
117
|
geometry: PageGeometry;
|
|
116
118
|
design: DocModule['design'];
|
|
117
119
|
pagesHtml: string[];
|
|
@@ -133,7 +135,7 @@ function buildHtml(opts: {
|
|
|
133
135
|
.join('\n');
|
|
134
136
|
|
|
135
137
|
return `<!doctype html>
|
|
136
|
-
<html lang="
|
|
138
|
+
<html lang="${escapeAttr(opts.lang)}">
|
|
137
139
|
<head>
|
|
138
140
|
<meta charset="utf-8">
|
|
139
141
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
toAbsolute,
|
|
21
21
|
} from './export-dom';
|
|
22
22
|
import { PAGE_ATTR } from './outline';
|
|
23
|
-
import { type DocModule, type PageGeometry, resolvePageGeometry } from './sdk';
|
|
23
|
+
import { type DocModule, type PageGeometry, pageLang, resolvePageGeometry } from './sdk';
|
|
24
24
|
import type { ExpandedPage } from './use-doc-pages';
|
|
25
25
|
|
|
26
26
|
export type ImageFormat = 'png' | 'svg';
|
|
@@ -186,6 +186,7 @@ function buildSvg(pageHtml: string, css: string, geometry: Box, doc: DocModule):
|
|
|
186
186
|
const content = document.createElementNS(XHTML_NS, 'div');
|
|
187
187
|
content.setAttribute('style', 'width:100%;height:100%');
|
|
188
188
|
content.setAttribute(PAGE_ATTR, '');
|
|
189
|
+
content.setAttribute('lang', pageLang(doc.meta));
|
|
189
190
|
content.innerHTML = pageHtml;
|
|
190
191
|
wrapper.appendChild(content);
|
|
191
192
|
|
|
@@ -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',
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { metricsFor } from './flow-measure';
|
|
3
|
+
|
|
4
|
+
/** Just what `metricsFor` reads of a block's wrapper. */
|
|
5
|
+
function block(inner: { tagName: string; attrs?: Record<string, string> } | null): HTMLElement {
|
|
6
|
+
return {
|
|
7
|
+
firstElementChild: inner && {
|
|
8
|
+
tagName: inner.tagName,
|
|
9
|
+
getAttribute: (name: string) => inner.attrs?.[name] ?? null,
|
|
10
|
+
},
|
|
11
|
+
} as unknown as HTMLElement;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
describe('metricsFor', () => {
|
|
15
|
+
it('treats a block that renders nothing as plain, not as a page break', () => {
|
|
16
|
+
expect(metricsFor(block(null), 0, 0)).toMatchObject({
|
|
17
|
+
breakBefore: false,
|
|
18
|
+
keepWithNext: false,
|
|
19
|
+
keepWithPrevious: false,
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('glues a heading to what follows, and reads declared breaks', () => {
|
|
24
|
+
expect(metricsFor(block({ tagName: 'H2' }), 20, 0).keepWithNext).toBe(true);
|
|
25
|
+
expect(
|
|
26
|
+
metricsFor(block({ tagName: 'DIV', attrs: { 'data-od-break-before': '' } }), 20, 0)
|
|
27
|
+
.breakBefore,
|
|
28
|
+
).toBe(true);
|
|
29
|
+
});
|
|
30
|
+
});
|