@open-document/core 0.9.1 → 0.10.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-B0GJXy8I.js → build-DJMLK50m.js} +1 -1
- package/dist/{check-CuDIWsPT.js → check-CUKuufBm.js} +3 -3
- package/dist/cli/bin.js +10 -6
- package/dist/{config-CAZ7Z_va.js → config-CvzpyAUA.js} +558 -546
- package/dist/{context-PWAh9qbf.js → context-B4ip7SoI.js} +1 -1
- package/dist/{dev-CYqppXDI.js → dev-dnH7DTlD.js} +1 -1
- package/dist/{diagram-CCaUUnRe.js → diagram-BK4jTZr4.js} +146 -146
- package/dist/diff-QTo0A5Rx.js +44 -0
- package/dist/{export-1JtO2OMC.js → export-BrRFrexj.js} +3 -3
- package/dist/{import-CsrdBOZ8.js → import-C14Vn9ER.js} +2 -2
- package/dist/index.d.ts +53 -2
- package/dist/index.js +457 -18
- package/dist/ops/index.d.ts +72 -17
- package/dist/ops/index.js +3 -3
- package/dist/{ops-B2y9BMpk.js → ops-DKidjLXP.js} +642 -124
- package/dist/{preview-DW3PM-JV.js → preview-wKM4C6Uk.js} +1 -1
- package/dist/{sdk-BqU6mwD_.d.ts → sdk-CpDEGRIo.d.ts} +2 -0
- package/dist/vite/index.js +1 -1
- package/package.json +2 -2
- package/skills/doc-authoring/SKILL.md +9 -4
- package/skills/doc-authoring/references/pagination.md +1 -1
- package/skills/doc-authoring/references/tables-and-charts.md +18 -1
- package/src/app/components/browser/browser-ui.tsx +105 -0
- package/src/app/components/chart.tsx +239 -0
- package/src/app/components/command-palette.tsx +284 -0
- package/src/app/components/design-panel/design-panel.tsx +22 -20
- package/src/app/components/design-panel/design-provider.tsx +4 -2
- package/src/app/components/diagram.tsx +11 -1
- package/src/app/components/doc-assets.tsx +6 -5
- package/src/app/components/doc-search.tsx +12 -10
- package/src/app/components/doc-sidebar.tsx +24 -18
- package/src/app/components/flow-page.tsx +2 -0
- package/src/app/components/inspector/inspector.tsx +75 -48
- package/src/app/components/language-toggle.tsx +38 -0
- package/src/app/components/palette-items.ts +128 -0
- package/src/app/components/panel/edit-save-card.tsx +4 -2
- package/src/app/components/panel/fields.tsx +5 -2
- package/src/app/components/panel/save-card.tsx +18 -10
- package/src/app/components/sidebar/folder-item.tsx +10 -8
- package/src/app/components/sidebar/icon-picker.tsx +3 -1
- package/src/app/components/sidebar/sidebar.tsx +44 -13
- package/src/app/components/theme-toggle.tsx +5 -3
- package/src/app/components/ui/menu.tsx +5 -0
- package/src/app/lib/agent-bridge.ts +8 -2
- package/src/app/lib/chart.test.ts +108 -0
- package/src/app/lib/chart.ts +343 -0
- package/src/app/lib/doc-preview.tsx +0 -10
- package/src/app/lib/docs.ts +3 -0
- package/src/app/lib/docx/from-dom.ts +11 -11
- package/src/app/lib/docx/ooxml.test.ts +25 -9
- package/src/app/lib/docx/ooxml.ts +58 -37
- package/src/app/lib/export-docx.ts +13 -8
- package/src/app/lib/flow.ts +1 -1
- package/src/app/lib/i18n-ja/browser.ts +88 -0
- package/src/app/lib/i18n-ja/doc.ts +65 -0
- package/src/app/lib/i18n-ja/index.ts +5 -0
- package/src/app/lib/i18n-ja/panels.ts +104 -0
- package/src/app/lib/i18n-ko/browser.ts +87 -0
- package/src/app/lib/i18n-ko/doc.ts +65 -0
- package/src/app/lib/i18n-ko/index.ts +5 -0
- package/src/app/lib/i18n-ko/panels.ts +102 -0
- package/src/app/lib/i18n-zh-cn/browser.ts +84 -0
- package/src/app/lib/i18n-zh-cn/doc.ts +64 -0
- package/src/app/lib/i18n-zh-cn/index.ts +5 -0
- package/src/app/lib/i18n-zh-cn/panels.ts +98 -0
- package/src/app/lib/i18n-zh-tw/browser.ts +84 -0
- package/src/app/lib/i18n-zh-tw/doc.ts +64 -0
- package/src/app/lib/i18n-zh-tw/index.ts +5 -0
- package/src/app/lib/i18n-zh-tw/panels.ts +99 -0
- package/src/app/lib/i18n.test.ts +24 -0
- package/src/app/lib/i18n.tsx +109 -0
- package/src/app/lib/outline.ts +23 -0
- package/src/app/lib/page-range.test.ts +10 -1
- package/src/app/lib/page-range.ts +26 -0
- package/src/app/lib/sdk.ts +2 -0
- package/src/app/main.tsx +4 -1
- package/src/app/routes/assets.tsx +193 -132
- package/src/app/routes/doc.tsx +666 -310
- package/src/app/routes/home-shell.tsx +115 -88
- package/src/app/routes/home.tsx +200 -67
- package/src/app/routes/themes.tsx +42 -35
- package/src/app/styles.css +84 -5
- package/src/app/virtual.d.ts +1 -0
|
@@ -1,11 +1,651 @@
|
|
|
1
1
|
import { c as isPageSizeName, i as ORIENTATIONS, o as PAGE_SIZE_NAMES, s as isOrientation } from "./sdk-CqNZzbkz.js";
|
|
2
|
-
import { C as
|
|
2
|
+
import { C as assetImportPath, D as validateAssetName, E as resolveScopedAssetsDir, S as writeManifest, T as resolveScopedAssetFile, b as validateIcon, c as createDocument, f as listDocIds, g as resolveEntry, k as DOC_ID_RE, n as parseFrontmatter$1, s as OpsError, t as createViteConfig, v as newFolderId, w as countAssetUsages, x as validateName, y as readManifest } from "./config-CvzpyAUA.js";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import fs from "node:fs/promises";
|
|
5
5
|
import { existsSync } from "node:fs";
|
|
6
6
|
import fg from "fast-glob";
|
|
7
7
|
import { createServer, mergeConfig } from "vite";
|
|
8
|
+
import { execFile } from "node:child_process";
|
|
9
|
+
import { promisify } from "node:util";
|
|
8
10
|
import { Buffer } from "node:buffer";
|
|
11
|
+
//#region src/render/session.ts
|
|
12
|
+
var RenderUnavailableError = class extends Error {
|
|
13
|
+
constructor(message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "RenderUnavailableError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
const INSTALL_HINT = "Headless rendering needs Playwright. Install it in this workspace:\n pnpm add -D playwright && pnpm exec playwright install chromium";
|
|
19
|
+
async function loadPlaywright() {
|
|
20
|
+
try {
|
|
21
|
+
return await import("playwright");
|
|
22
|
+
} catch {
|
|
23
|
+
throw new RenderUnavailableError(INSTALL_HINT);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const DEFAULT_TIMEOUT = 6e4;
|
|
27
|
+
const PRINT_PAGE_SELECTOR = "#od-print-root .od-print-page";
|
|
28
|
+
async function bootServer(userCwd) {
|
|
29
|
+
const base = await createViteConfig({
|
|
30
|
+
userCwd,
|
|
31
|
+
headless: true
|
|
32
|
+
});
|
|
33
|
+
const config = mergeConfig(base, {
|
|
34
|
+
logLevel: "silent",
|
|
35
|
+
server: {
|
|
36
|
+
port: 0,
|
|
37
|
+
strictPort: false,
|
|
38
|
+
open: false
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
const server = await createServer(config);
|
|
42
|
+
await server.listen();
|
|
43
|
+
const origin = server.resolvedUrls?.local?.[0]?.replace(/\/$/, "");
|
|
44
|
+
if (!origin) {
|
|
45
|
+
await server.close();
|
|
46
|
+
throw new Error("Could not determine the dev server URL");
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
server,
|
|
50
|
+
origin
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
async function createRenderSession(opts) {
|
|
54
|
+
const { chromium } = await loadPlaywright();
|
|
55
|
+
const timeout = opts.timeoutMs ?? DEFAULT_TIMEOUT;
|
|
56
|
+
let server = null;
|
|
57
|
+
let origin = opts.origin?.replace(/\/$/, "") ?? "";
|
|
58
|
+
if (!origin) {
|
|
59
|
+
const booted = await bootServer(opts.userCwd);
|
|
60
|
+
server = booted.server;
|
|
61
|
+
origin = booted.origin;
|
|
62
|
+
}
|
|
63
|
+
let browser;
|
|
64
|
+
try {
|
|
65
|
+
browser = await chromium.launch({ headless: true });
|
|
66
|
+
} catch (err) {
|
|
67
|
+
await server?.close();
|
|
68
|
+
throw new RenderUnavailableError(`${INSTALL_HINT}\n\nChromium failed to launch: ${err.message}`);
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
origin,
|
|
72
|
+
async open(docId) {
|
|
73
|
+
const page = await browser.newPage({
|
|
74
|
+
viewport: {
|
|
75
|
+
width: 1280,
|
|
76
|
+
height: 1024
|
|
77
|
+
},
|
|
78
|
+
...opts.deviceScaleFactor !== void 0 ? { deviceScaleFactor: opts.deviceScaleFactor } : {}
|
|
79
|
+
});
|
|
80
|
+
const errors = [];
|
|
81
|
+
page.on("pageerror", (err) => errors.push(String(err?.message ?? err)));
|
|
82
|
+
await page.goto(`${origin}/d/${encodeURIComponent(docId)}`, {
|
|
83
|
+
waitUntil: "load",
|
|
84
|
+
timeout
|
|
85
|
+
});
|
|
86
|
+
try {
|
|
87
|
+
await page.waitForFunction("globalThis.__openDoc ? globalThis.__openDoc.status().ready : false", void 0, { timeout });
|
|
88
|
+
} catch {
|
|
89
|
+
await page.close();
|
|
90
|
+
const detail = errors.length ? `\n${errors.join("\n")}` : "";
|
|
91
|
+
throw new Error(`Document "${docId}" never finished rendering.${detail}`);
|
|
92
|
+
}
|
|
93
|
+
const status = await page.evaluate("globalThis.__openDoc.status()");
|
|
94
|
+
return {
|
|
95
|
+
status,
|
|
96
|
+
diagnose: () => page.evaluate("globalThis.__openDoc.diagnose()"),
|
|
97
|
+
async pdf() {
|
|
98
|
+
await page.evaluate("globalThis.__openDoc.preparePrint()");
|
|
99
|
+
try {
|
|
100
|
+
return await page.pdf({
|
|
101
|
+
printBackground: true,
|
|
102
|
+
preferCSSPageSize: true,
|
|
103
|
+
outline: true,
|
|
104
|
+
tagged: true
|
|
105
|
+
});
|
|
106
|
+
} finally {
|
|
107
|
+
await page.evaluate("globalThis.__openDoc.releasePrint()");
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
async screenshot(pageNumber) {
|
|
111
|
+
if (pageNumber < 1 || pageNumber > status.pageCount) throw new Error(`page ${pageNumber} is out of range — "${docId}" has ${status.pageCount}`);
|
|
112
|
+
await page.evaluate("globalThis.__openDoc.preparePrint()");
|
|
113
|
+
await page.emulateMedia({ media: "print" });
|
|
114
|
+
try {
|
|
115
|
+
const sheet = await page.$(`${PRINT_PAGE_SELECTOR}:nth-child(${pageNumber})`);
|
|
116
|
+
if (!sheet) throw new Error(`page ${pageNumber} did not render`);
|
|
117
|
+
return await sheet.screenshot({ type: "png" });
|
|
118
|
+
} finally {
|
|
119
|
+
await page.emulateMedia({ media: null });
|
|
120
|
+
await page.evaluate("globalThis.__openDoc.releasePrint()");
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
async sheets() {
|
|
124
|
+
await page.evaluate("globalThis.__openDoc.preparePrint()");
|
|
125
|
+
await page.emulateMedia({ media: "print" });
|
|
126
|
+
try {
|
|
127
|
+
const out = [];
|
|
128
|
+
for (let n = 1; n <= status.pageCount; n++) {
|
|
129
|
+
const selector = `${PRINT_PAGE_SELECTOR}:nth-child(${n})`;
|
|
130
|
+
const sheet = await page.$(selector);
|
|
131
|
+
if (!sheet) throw new Error(`page ${n} did not render`);
|
|
132
|
+
const png = await sheet.screenshot({ type: "png" });
|
|
133
|
+
const text = await page.evaluate(`document.querySelector(${JSON.stringify(selector)})?.innerText ?? ''`);
|
|
134
|
+
out.push({
|
|
135
|
+
png,
|
|
136
|
+
text
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
} finally {
|
|
141
|
+
await page.emulateMedia({ media: null });
|
|
142
|
+
await page.evaluate("globalThis.__openDoc.releasePrint()");
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
html: () => page.evaluate("globalThis.__openDoc.htmlBundle()"),
|
|
146
|
+
docx: () => page.evaluate("globalThis.__openDoc.docxBundle()"),
|
|
147
|
+
close: () => page.close()
|
|
148
|
+
};
|
|
149
|
+
},
|
|
150
|
+
async compareImages(before, after) {
|
|
151
|
+
const page = await browser.newPage();
|
|
152
|
+
try {
|
|
153
|
+
const toUrl = (png) => `data:image/png;base64,${Buffer.from(png).toString("base64")}`;
|
|
154
|
+
return await page.evaluate(compareInBrowser, {
|
|
155
|
+
a: toUrl(before),
|
|
156
|
+
b: toUrl(after),
|
|
157
|
+
cell: DIFF_CELL
|
|
158
|
+
});
|
|
159
|
+
} finally {
|
|
160
|
+
await page.close();
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
async close() {
|
|
164
|
+
await browser.close();
|
|
165
|
+
await server?.close();
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/** Changed pixels are gathered into cells this many px square before boxing. */
|
|
170
|
+
const DIFF_CELL = 12;
|
|
171
|
+
/**
|
|
172
|
+
* Runs in the page. The browser decodes both PNGs, so nothing on the Node side
|
|
173
|
+
* has to: draw each onto white, mark every cell where a pixel moved by more
|
|
174
|
+
* than antialiasing does, and box the clusters of marked cells. Clusters
|
|
175
|
+
* within two cells of each other join, so one edited paragraph is one box.
|
|
176
|
+
*/
|
|
177
|
+
async function compareInBrowser({ a, b, cell }) {
|
|
178
|
+
const load = async (src) => {
|
|
179
|
+
const image = new Image();
|
|
180
|
+
image.src = src;
|
|
181
|
+
await image.decode();
|
|
182
|
+
return image;
|
|
183
|
+
};
|
|
184
|
+
const [x, y] = await Promise.all([load(a), load(b)]);
|
|
185
|
+
const width = Math.max(x.naturalWidth, y.naturalWidth);
|
|
186
|
+
const height = Math.max(x.naturalHeight, y.naturalHeight);
|
|
187
|
+
const pixels = (image) => {
|
|
188
|
+
const canvas = document.createElement("canvas");
|
|
189
|
+
canvas.width = width;
|
|
190
|
+
canvas.height = height;
|
|
191
|
+
const g = canvas.getContext("2d");
|
|
192
|
+
if (!g) throw new Error("no 2D canvas");
|
|
193
|
+
g.fillStyle = "#ffffff";
|
|
194
|
+
g.fillRect(0, 0, width, height);
|
|
195
|
+
g.drawImage(image, 0, 0);
|
|
196
|
+
return g.getImageData(0, 0, width, height).data;
|
|
197
|
+
};
|
|
198
|
+
const p = pixels(x);
|
|
199
|
+
const q = pixels(y);
|
|
200
|
+
const cols = Math.ceil(width / cell);
|
|
201
|
+
const rows = Math.ceil(height / cell);
|
|
202
|
+
const hot = new Uint8Array(cols * rows);
|
|
203
|
+
let changed = 0;
|
|
204
|
+
for (let i = 0; i < p.length; i += 4) {
|
|
205
|
+
if (Math.abs((p[i] ?? 0) - (q[i] ?? 0)) + Math.abs((p[i + 1] ?? 0) - (q[i + 1] ?? 0)) + Math.abs((p[i + 2] ?? 0) - (q[i + 2] ?? 0)) <= 48) continue;
|
|
206
|
+
changed++;
|
|
207
|
+
const px = i / 4 % width;
|
|
208
|
+
const py = Math.floor(i / 4 / width);
|
|
209
|
+
hot[Math.floor(py / cell) * cols + Math.floor(px / cell)] = 1;
|
|
210
|
+
}
|
|
211
|
+
const seen = new Uint8Array(cols * rows);
|
|
212
|
+
const boxes = [];
|
|
213
|
+
for (let start = 0; start < hot.length; start++) {
|
|
214
|
+
if (!hot[start] || seen[start]) continue;
|
|
215
|
+
let minC = cols;
|
|
216
|
+
let maxC = 0;
|
|
217
|
+
let minR = rows;
|
|
218
|
+
let maxR = 0;
|
|
219
|
+
const stack = [start];
|
|
220
|
+
seen[start] = 1;
|
|
221
|
+
while (stack.length > 0) {
|
|
222
|
+
const at = stack.pop();
|
|
223
|
+
const c = at % cols;
|
|
224
|
+
const r = Math.floor(at / cols);
|
|
225
|
+
minC = Math.min(minC, c);
|
|
226
|
+
maxC = Math.max(maxC, c);
|
|
227
|
+
minR = Math.min(minR, r);
|
|
228
|
+
maxR = Math.max(maxR, r);
|
|
229
|
+
for (let dr = -2; dr <= 2; dr++) for (let dc = -2; dc <= 2; dc++) {
|
|
230
|
+
const nr = r + dr;
|
|
231
|
+
const nc = c + dc;
|
|
232
|
+
if (nr < 0 || nc < 0 || nr >= rows || nc >= cols) continue;
|
|
233
|
+
const next = nr * cols + nc;
|
|
234
|
+
if (hot[next] && !seen[next]) {
|
|
235
|
+
seen[next] = 1;
|
|
236
|
+
stack.push(next);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
boxes.push({
|
|
241
|
+
x: minC * cell,
|
|
242
|
+
y: minR * cell,
|
|
243
|
+
width: Math.min(width, (maxC + 1) * cell) - minC * cell,
|
|
244
|
+
height: Math.min(height, (maxR + 1) * cell) - minR * cell
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
return {
|
|
248
|
+
width,
|
|
249
|
+
height,
|
|
250
|
+
changed: changed / (width * height),
|
|
251
|
+
boxes
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
//#endregion
|
|
255
|
+
//#region src/ops/diff-pages.ts
|
|
256
|
+
/** Longest common subsequence over two lists, as pairs of matching indices. */
|
|
257
|
+
function lcs(a, b, same) {
|
|
258
|
+
const rows = a.length;
|
|
259
|
+
const cols = b.length;
|
|
260
|
+
const table = Array.from({ length: rows + 1 }, () => new Array(cols + 1).fill(0));
|
|
261
|
+
for (let i = rows - 1; i >= 0; i--) for (let j = cols - 1; j >= 0; j--) {
|
|
262
|
+
const row = table[i];
|
|
263
|
+
const below = table[i + 1];
|
|
264
|
+
row[j] = same(a[i], b[j]) ? (below[j + 1] ?? 0) + 1 : Math.max(below[j] ?? 0, row[j + 1] ?? 0);
|
|
265
|
+
}
|
|
266
|
+
const pairs = [];
|
|
267
|
+
let i = 0;
|
|
268
|
+
let j = 0;
|
|
269
|
+
while (i < rows && j < cols) if (same(a[i], b[j])) {
|
|
270
|
+
pairs.push([i, j]);
|
|
271
|
+
i++;
|
|
272
|
+
j++;
|
|
273
|
+
} else if ((table[i + 1]?.[j] ?? 0) >= (table[i]?.[j + 1] ?? 0)) i++;
|
|
274
|
+
else j++;
|
|
275
|
+
return pairs;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Pairs the sheets of two versions. Pages whose text is unchanged anchor the
|
|
279
|
+
* alignment, so a page inserted in the middle shows as one new page rather than
|
|
280
|
+
* as every page after it having changed. Between two anchors the remaining
|
|
281
|
+
* pages pair up in order; whichever side has more keeps the rest as added or
|
|
282
|
+
* removed.
|
|
283
|
+
*/
|
|
284
|
+
function alignPages(before, after) {
|
|
285
|
+
const anchors = lcs(before, after, (x, y) => x === y);
|
|
286
|
+
const pairs = [];
|
|
287
|
+
let i = 0;
|
|
288
|
+
let j = 0;
|
|
289
|
+
const fill = (untilI, untilJ) => {
|
|
290
|
+
while (i < untilI && j < untilJ) pairs.push({
|
|
291
|
+
before: i++,
|
|
292
|
+
after: j++
|
|
293
|
+
});
|
|
294
|
+
while (i < untilI) pairs.push({
|
|
295
|
+
before: i++,
|
|
296
|
+
after: null
|
|
297
|
+
});
|
|
298
|
+
while (j < untilJ) pairs.push({
|
|
299
|
+
before: null,
|
|
300
|
+
after: j++
|
|
301
|
+
});
|
|
302
|
+
};
|
|
303
|
+
for (const [ai, aj] of anchors) {
|
|
304
|
+
fill(ai, aj);
|
|
305
|
+
pairs.push({
|
|
306
|
+
before: i++,
|
|
307
|
+
after: j++
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
fill(before.length, after.length);
|
|
311
|
+
return pairs;
|
|
312
|
+
}
|
|
313
|
+
function textLines(text) {
|
|
314
|
+
return text.split("\n").map((line) => line.replace(/\s+/g, " ").trim()).filter((line) => line !== "");
|
|
315
|
+
}
|
|
316
|
+
/** A line-level diff, in reading order. */
|
|
317
|
+
function diffLines(before, after) {
|
|
318
|
+
const anchors = lcs(before, after, (x, y) => x === y);
|
|
319
|
+
const out = [];
|
|
320
|
+
let i = 0;
|
|
321
|
+
let j = 0;
|
|
322
|
+
for (const [ai, aj] of [...anchors, [before.length, after.length]]) {
|
|
323
|
+
while (i < ai) out.push({
|
|
324
|
+
op: "remove",
|
|
325
|
+
text: before[i++] ?? ""
|
|
326
|
+
});
|
|
327
|
+
while (j < aj) out.push({
|
|
328
|
+
op: "add",
|
|
329
|
+
text: after[j++] ?? ""
|
|
330
|
+
});
|
|
331
|
+
if (i < before.length && j < after.length && ai < before.length) {
|
|
332
|
+
out.push({
|
|
333
|
+
op: "same",
|
|
334
|
+
text: before[i] ?? ""
|
|
335
|
+
});
|
|
336
|
+
i++;
|
|
337
|
+
j++;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return out;
|
|
341
|
+
}
|
|
342
|
+
//#endregion
|
|
343
|
+
//#region src/ops/diff-report.ts
|
|
344
|
+
function escapeHtml(text) {
|
|
345
|
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
346
|
+
}
|
|
347
|
+
function dataUrl(png) {
|
|
348
|
+
return png ? `data:image/png;base64,${Buffer.from(png).toString("base64")}` : "";
|
|
349
|
+
}
|
|
350
|
+
/** A sheet with its changed regions outlined, as percentages so it scales with the image. */
|
|
351
|
+
function sheet(png, boxes, size) {
|
|
352
|
+
const marks = boxes.map((box) => {
|
|
353
|
+
const left = box.x / size.width * 100;
|
|
354
|
+
const top = box.y / size.height * 100;
|
|
355
|
+
const width = box.width / size.width * 100;
|
|
356
|
+
const height = box.height / size.height * 100;
|
|
357
|
+
return `<span class="mark" style="left:${left.toFixed(2)}%;top:${top.toFixed(2)}%;width:${width.toFixed(2)}%;height:${height.toFixed(2)}%"></span>`;
|
|
358
|
+
}).join("");
|
|
359
|
+
return `<div class="sheet"><img src="${dataUrl(png)}" alt="" />${marks}</div>`;
|
|
360
|
+
}
|
|
361
|
+
/** PNG width and height, read from the IHDR chunk. */
|
|
362
|
+
function sizeOf(png) {
|
|
363
|
+
if (!png || png.length < 24) return {
|
|
364
|
+
width: 1,
|
|
365
|
+
height: 1
|
|
366
|
+
};
|
|
367
|
+
const view = new DataView(png.buffer, png.byteOffset, png.byteLength);
|
|
368
|
+
return {
|
|
369
|
+
width: view.getUint32(16) || 1,
|
|
370
|
+
height: view.getUint32(20) || 1
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
const LABEL = {
|
|
374
|
+
same: "Unchanged",
|
|
375
|
+
changed: "Changed",
|
|
376
|
+
added: "Added",
|
|
377
|
+
removed: "Removed"
|
|
378
|
+
};
|
|
379
|
+
function pageName(change) {
|
|
380
|
+
if (change.after !== null && change.before !== null && change.after !== change.before) return `Page ${change.after} <span class="was">(was ${change.before})</span>`;
|
|
381
|
+
return `Page ${change.after ?? change.before}`;
|
|
382
|
+
}
|
|
383
|
+
function section(change, input) {
|
|
384
|
+
const before = change.before === null ? void 0 : input.before[change.before - 1];
|
|
385
|
+
const after = change.after === null ? void 0 : input.after[change.after - 1];
|
|
386
|
+
const head = `<h2>${pageName(change)} <span class="badge ${change.status}">${LABEL[change.status]}</span></h2>`;
|
|
387
|
+
if (change.status === "added") return `<section>${head}<div class="pair one"><figure class="added">${sheet(after, [], sizeOf(after))}<figcaption>New</figcaption></figure></div></section>`;
|
|
388
|
+
if (change.status === "removed") return `<section>${head}<div class="pair one"><figure class="removed">${sheet(before, [], sizeOf(before))}<figcaption>Removed</figcaption></figure></div></section>`;
|
|
389
|
+
const lines = change.lines.map((line) => `<li class="${line.op}"><span>${line.op === "add" ? "+" : "−"}</span>${escapeHtml(line.text)}</li>`).join("");
|
|
390
|
+
return `<section>${head}<div class="pair"><figure>${sheet(before, change.boxes, sizeOf(after))}<figcaption>Before · p. ${change.before}</figcaption></figure><figure>${sheet(after, change.boxes, sizeOf(after))}<figcaption>After · p. ${change.after}</figcaption></figure></div>${lines ? `<ul class="lines">${lines}</ul>` : "<p class=\"note\">Only the drawing changed — no text was added or removed.</p>"}</section>`;
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* The report `open-doc diff` writes: one self-contained HTML file, pictures
|
|
394
|
+
* inlined, so it can be sent to a reviewer who has neither the repository nor
|
|
395
|
+
* open-doc. Changed pages show before and after side by side with the changed
|
|
396
|
+
* regions outlined and the text that moved listed beneath; unchanged pages are
|
|
397
|
+
* a single line.
|
|
398
|
+
*/
|
|
399
|
+
function renderDiffReport(input) {
|
|
400
|
+
const count = (status) => input.pages.filter((page) => page.status === status).length;
|
|
401
|
+
const changed = count("changed");
|
|
402
|
+
const added = count("added");
|
|
403
|
+
const removed = count("removed");
|
|
404
|
+
const unchanged = input.pages.filter((page) => page.status === "same");
|
|
405
|
+
const summary = changed + added + removed === 0 ? "No changes." : [
|
|
406
|
+
changed && `${changed} changed`,
|
|
407
|
+
added && `${added} added`,
|
|
408
|
+
removed && `${removed} removed`,
|
|
409
|
+
unchanged.length && `${unchanged.length} unchanged`
|
|
410
|
+
].filter(Boolean).join(" · ");
|
|
411
|
+
const body = input.pages.filter((page) => page.status !== "same").map((page) => section(page, input)).join("");
|
|
412
|
+
const same = unchanged.length ? `<p class="note">Unchanged: ${unchanged.map((page) => `p. ${page.after}`).join(", ")}</p>` : "";
|
|
413
|
+
return `<!doctype html>
|
|
414
|
+
<html lang="en">
|
|
415
|
+
<head>
|
|
416
|
+
<meta charset="utf-8" />
|
|
417
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
418
|
+
<title>${escapeHtml(`${input.title} — changes since ${input.since}`)}</title>
|
|
419
|
+
<style>
|
|
420
|
+
:root { --text: #16181d; --muted: #6b7280; --rule: #e5e7eb; --bg: #f6f7f9; --add: #15803d; --remove: #b91c1c; --mark: #f59e0b; }
|
|
421
|
+
* { box-sizing: border-box; }
|
|
422
|
+
body { margin: 0; padding: 32px 16px 64px; background: var(--bg); color: var(--text);
|
|
423
|
+
font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Noto Sans TC", "Segoe UI", sans-serif; }
|
|
424
|
+
main { max-width: 1100px; margin: 0 auto; }
|
|
425
|
+
header h1 { margin: 0 0 4px; font-size: 22px; }
|
|
426
|
+
header p { margin: 0; color: var(--muted); }
|
|
427
|
+
code { font: 12px ui-monospace, Menlo, monospace; }
|
|
428
|
+
.summary { margin: 16px 0 8px; font-weight: 600; }
|
|
429
|
+
section { margin-top: 28px; padding-top: 20px; border-top: 1px solid var(--rule); }
|
|
430
|
+
h2 { margin: 0 0 12px; font-size: 16px; }
|
|
431
|
+
.was { color: var(--muted); font-weight: 400; }
|
|
432
|
+
.badge { margin-left: 6px; padding: 1px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; vertical-align: 2px; }
|
|
433
|
+
.badge.changed { background: #fef3c7; color: #92400e; }
|
|
434
|
+
.badge.added { background: #dcfce7; color: var(--add); }
|
|
435
|
+
.badge.removed { background: #fee2e2; color: var(--remove); }
|
|
436
|
+
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
|
|
437
|
+
.pair.one { grid-template-columns: minmax(0, 520px); }
|
|
438
|
+
@media (max-width: 720px) { .pair { grid-template-columns: 1fr; } }
|
|
439
|
+
figure { margin: 0; }
|
|
440
|
+
figcaption { margin-top: 6px; color: var(--muted); font-size: 12px; }
|
|
441
|
+
.sheet { position: relative; background: #fff; box-shadow: 0 1px 3px rgb(0 0 0 / 0.12); }
|
|
442
|
+
.sheet img { display: block; width: 100%; height: auto; }
|
|
443
|
+
.added .sheet { outline: 3px solid var(--add); }
|
|
444
|
+
.removed .sheet { outline: 3px solid var(--remove); opacity: 0.7; }
|
|
445
|
+
.mark { position: absolute; border: 2px solid var(--mark); background: rgb(245 158 11 / 0.12); border-radius: 2px; }
|
|
446
|
+
.lines { margin: 14px 0 0; padding: 0; list-style: none; font-size: 13px; }
|
|
447
|
+
.lines li { display: flex; gap: 8px; padding: 2px 8px; border-radius: 4px; }
|
|
448
|
+
.lines li span { flex: none; width: 1ch; font-weight: 700; }
|
|
449
|
+
.lines .add { background: #f0fdf4; color: var(--add); }
|
|
450
|
+
.lines .remove { background: #fef2f2; color: var(--remove); text-decoration: line-through; text-decoration-color: rgb(185 28 28 / 0.4); }
|
|
451
|
+
.note { color: var(--muted); }
|
|
452
|
+
</style>
|
|
453
|
+
</head>
|
|
454
|
+
<body>
|
|
455
|
+
<main>
|
|
456
|
+
<header>
|
|
457
|
+
<h1>${escapeHtml(input.title)}</h1>
|
|
458
|
+
<p>Changes since <code>${escapeHtml(input.since)}</code> (<code>${escapeHtml(input.commit.slice(0, 7))}</code>) · <code>${escapeHtml(input.docId)}</code></p>
|
|
459
|
+
</header>
|
|
460
|
+
<p class="summary">${summary}</p>
|
|
461
|
+
${same}
|
|
462
|
+
${body}
|
|
463
|
+
</main>
|
|
464
|
+
</body>
|
|
465
|
+
</html>
|
|
466
|
+
`;
|
|
467
|
+
}
|
|
468
|
+
//#endregion
|
|
469
|
+
//#region src/ops/diff.ts
|
|
470
|
+
const run = promisify(execFile);
|
|
471
|
+
const MAX_BUFFER = 67108864;
|
|
472
|
+
async function git(cwd, args) {
|
|
473
|
+
const { stdout } = await run("git", args, {
|
|
474
|
+
cwd,
|
|
475
|
+
maxBuffer: MAX_BUFFER
|
|
476
|
+
});
|
|
477
|
+
return stdout;
|
|
478
|
+
}
|
|
479
|
+
async function gitBytes(cwd, args) {
|
|
480
|
+
const { stdout } = await run("git", args, {
|
|
481
|
+
cwd,
|
|
482
|
+
maxBuffer: MAX_BUFFER,
|
|
483
|
+
encoding: "buffer"
|
|
484
|
+
});
|
|
485
|
+
return stdout;
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* The workspace as it was at `commit`, as far as one document needs it: its
|
|
489
|
+
* folder, the global assets, and the config. It lives inside the workspace so
|
|
490
|
+
* that `react` and `@open-document/core` resolve to the same installed copies
|
|
491
|
+
* the current version renders with.
|
|
492
|
+
*/
|
|
493
|
+
async function checkout(ctx, docId, commit) {
|
|
494
|
+
const prefix = (await git(ctx.userCwd, ["rev-parse", "--show-prefix"])).trim();
|
|
495
|
+
const dir = path.join(ctx.userCwd, ".open-doc-diff", `${commit.slice(0, 12)}-${docId}`);
|
|
496
|
+
await fs.rm(dir, {
|
|
497
|
+
recursive: true,
|
|
498
|
+
force: true
|
|
499
|
+
});
|
|
500
|
+
const docRel = path.relative(ctx.userCwd, path.join(ctx.docsRoot, docId));
|
|
501
|
+
const wanted = [
|
|
502
|
+
docRel,
|
|
503
|
+
path.relative(ctx.userCwd, ctx.globalAssetsRoot),
|
|
504
|
+
"open-doc.config.ts"
|
|
505
|
+
];
|
|
506
|
+
let exists = false;
|
|
507
|
+
for (const rel of wanted) {
|
|
508
|
+
const listed = await git(ctx.userCwd, [
|
|
509
|
+
"ls-tree",
|
|
510
|
+
"-r",
|
|
511
|
+
"--full-tree",
|
|
512
|
+
"--name-only",
|
|
513
|
+
commit,
|
|
514
|
+
"--",
|
|
515
|
+
`${prefix}${rel.split(path.sep).join("/")}`
|
|
516
|
+
]);
|
|
517
|
+
for (const file of listed.split("\n").filter(Boolean)) {
|
|
518
|
+
const local = file.slice(prefix.length);
|
|
519
|
+
const target = path.join(dir, local);
|
|
520
|
+
await fs.mkdir(path.dirname(target), { recursive: true });
|
|
521
|
+
await fs.writeFile(target, await gitBytes(ctx.userCwd, ["show", `${commit}:${file}`]));
|
|
522
|
+
if (rel === docRel) exists = true;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
const config = path.join(dir, "open-doc.config.ts");
|
|
526
|
+
try {
|
|
527
|
+
await fs.access(config);
|
|
528
|
+
} catch {
|
|
529
|
+
await fs.copyFile(path.join(ctx.userCwd, "open-doc.config.ts"), config).catch(() => {});
|
|
530
|
+
}
|
|
531
|
+
return {
|
|
532
|
+
dir,
|
|
533
|
+
exists
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
async function sheetsOf(session, docId) {
|
|
537
|
+
const renderer = await session.open(docId);
|
|
538
|
+
try {
|
|
539
|
+
return {
|
|
540
|
+
title: renderer.status.title || docId,
|
|
541
|
+
sheets: await renderer.sheets()
|
|
542
|
+
};
|
|
543
|
+
} finally {
|
|
544
|
+
await renderer.close();
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
async function openSession(userCwd, origin) {
|
|
548
|
+
try {
|
|
549
|
+
return await createRenderSession({
|
|
550
|
+
userCwd,
|
|
551
|
+
...origin ? { origin } : {},
|
|
552
|
+
deviceScaleFactor: 1
|
|
553
|
+
});
|
|
554
|
+
} catch (err) {
|
|
555
|
+
if (err instanceof RenderUnavailableError) throw new OpsError(503, err.message);
|
|
556
|
+
throw err;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* What changed in a document since a revision, page by page: the old and new
|
|
561
|
+
* sheets rendered through the same print pipeline as an export, paired by
|
|
562
|
+
* content, compared as pictures and as text, and written up as a report a
|
|
563
|
+
* reviewer can read without the source.
|
|
564
|
+
*/
|
|
565
|
+
async function diffDocument(ctx, docId, opts = {}) {
|
|
566
|
+
if (!resolveEntry(ctx, docId)) throw new OpsError(404, `document not found: ${docId}`);
|
|
567
|
+
const since = opts.since ?? "HEAD";
|
|
568
|
+
let commit;
|
|
569
|
+
try {
|
|
570
|
+
commit = (await git(ctx.userCwd, [
|
|
571
|
+
"rev-parse",
|
|
572
|
+
"--verify",
|
|
573
|
+
`${since}^{commit}`
|
|
574
|
+
])).trim();
|
|
575
|
+
} catch {
|
|
576
|
+
throw new OpsError(400, `not a revision in this repository: ${since}`);
|
|
577
|
+
}
|
|
578
|
+
const outDir = path.resolve(ctx.userCwd, opts.outDir ?? "out");
|
|
579
|
+
if (outDir !== ctx.userCwd && !outDir.startsWith(ctx.userCwd + path.sep)) throw new OpsError(400, `outDir must stay inside the workspace: ${opts.outDir}`);
|
|
580
|
+
const old = await checkout(ctx, docId, commit);
|
|
581
|
+
const current = await openSession(ctx.userCwd, ctx.serverOrigin);
|
|
582
|
+
try {
|
|
583
|
+
let before = [];
|
|
584
|
+
if (old.exists) {
|
|
585
|
+
const past = await openSession(old.dir);
|
|
586
|
+
try {
|
|
587
|
+
before = (await sheetsOf(past, docId)).sheets;
|
|
588
|
+
} finally {
|
|
589
|
+
await past.close();
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
const after = await sheetsOf(current, docId);
|
|
593
|
+
const pairs = alignPages(before.map((sheet) => sheet.text), after.sheets.map((sheet) => sheet.text));
|
|
594
|
+
const pages = [];
|
|
595
|
+
for (const pair of pairs) {
|
|
596
|
+
const b = pair.before === null ? null : before[pair.before];
|
|
597
|
+
const a = pair.after === null ? null : after.sheets[pair.after];
|
|
598
|
+
const page = {
|
|
599
|
+
before: pair.before === null ? null : pair.before + 1,
|
|
600
|
+
after: pair.after === null ? null : pair.after + 1
|
|
601
|
+
};
|
|
602
|
+
if (!b || !a) {
|
|
603
|
+
pages.push({
|
|
604
|
+
...page,
|
|
605
|
+
status: b ? "removed" : "added",
|
|
606
|
+
lines: [],
|
|
607
|
+
boxes: []
|
|
608
|
+
});
|
|
609
|
+
continue;
|
|
610
|
+
}
|
|
611
|
+
const picture = await current.compareImages(b.png, a.png);
|
|
612
|
+
const lines = diffLines(textLines(b.text), textLines(a.text)).filter((line) => line.op !== "same");
|
|
613
|
+
const same = picture.boxes.length === 0 && lines.length === 0;
|
|
614
|
+
pages.push({
|
|
615
|
+
...page,
|
|
616
|
+
status: same ? "same" : "changed",
|
|
617
|
+
lines,
|
|
618
|
+
boxes: picture.boxes
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
await fs.mkdir(outDir, { recursive: true });
|
|
622
|
+
const file = path.join(outDir, `${docId}-diff.html`);
|
|
623
|
+
await fs.writeFile(file, renderDiffReport({
|
|
624
|
+
docId,
|
|
625
|
+
title: after.title,
|
|
626
|
+
since,
|
|
627
|
+
commit,
|
|
628
|
+
pages,
|
|
629
|
+
before: before.map((sheet) => sheet.png),
|
|
630
|
+
after: after.sheets.map((sheet) => sheet.png)
|
|
631
|
+
}));
|
|
632
|
+
return {
|
|
633
|
+
docId,
|
|
634
|
+
since,
|
|
635
|
+
commit,
|
|
636
|
+
pages,
|
|
637
|
+
file: path.relative(ctx.userCwd, file)
|
|
638
|
+
};
|
|
639
|
+
} finally {
|
|
640
|
+
await current.close();
|
|
641
|
+
await fs.rm(old.dir, {
|
|
642
|
+
recursive: true,
|
|
643
|
+
force: true
|
|
644
|
+
});
|
|
645
|
+
await fs.rmdir(path.dirname(old.dir)).catch(() => {});
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
//#endregion
|
|
9
649
|
//#region src/import/markdown.ts
|
|
10
650
|
const FENCE_RE = /^(```|~~~)\s*([^\s`]*)\s*$/;
|
|
11
651
|
const ATX_RE = /^(#{1,6})\s+(.*?)\s*#*\s*$/;
|
|
@@ -822,128 +1462,6 @@ async function importMarkdown(ctx, opts) {
|
|
|
822
1462
|
};
|
|
823
1463
|
}
|
|
824
1464
|
//#endregion
|
|
825
|
-
//#region src/render/session.ts
|
|
826
|
-
var RenderUnavailableError = class extends Error {
|
|
827
|
-
constructor(message) {
|
|
828
|
-
super(message);
|
|
829
|
-
this.name = "RenderUnavailableError";
|
|
830
|
-
}
|
|
831
|
-
};
|
|
832
|
-
const INSTALL_HINT = "Headless rendering needs Playwright. Install it in this workspace:\n pnpm add -D playwright && pnpm exec playwright install chromium";
|
|
833
|
-
async function loadPlaywright() {
|
|
834
|
-
try {
|
|
835
|
-
return await import("playwright");
|
|
836
|
-
} catch {
|
|
837
|
-
throw new RenderUnavailableError(INSTALL_HINT);
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
const DEFAULT_TIMEOUT = 6e4;
|
|
841
|
-
const PRINT_PAGE_SELECTOR = "#od-print-root .od-print-page";
|
|
842
|
-
async function bootServer(userCwd) {
|
|
843
|
-
const base = await createViteConfig({
|
|
844
|
-
userCwd,
|
|
845
|
-
headless: true
|
|
846
|
-
});
|
|
847
|
-
const config = mergeConfig(base, {
|
|
848
|
-
logLevel: "silent",
|
|
849
|
-
server: {
|
|
850
|
-
port: 0,
|
|
851
|
-
strictPort: false,
|
|
852
|
-
open: false
|
|
853
|
-
}
|
|
854
|
-
});
|
|
855
|
-
const server = await createServer(config);
|
|
856
|
-
await server.listen();
|
|
857
|
-
const origin = server.resolvedUrls?.local?.[0]?.replace(/\/$/, "");
|
|
858
|
-
if (!origin) {
|
|
859
|
-
await server.close();
|
|
860
|
-
throw new Error("Could not determine the dev server URL");
|
|
861
|
-
}
|
|
862
|
-
return {
|
|
863
|
-
server,
|
|
864
|
-
origin
|
|
865
|
-
};
|
|
866
|
-
}
|
|
867
|
-
async function createRenderSession(opts) {
|
|
868
|
-
const { chromium } = await loadPlaywright();
|
|
869
|
-
const timeout = opts.timeoutMs ?? DEFAULT_TIMEOUT;
|
|
870
|
-
let server = null;
|
|
871
|
-
let origin = opts.origin?.replace(/\/$/, "") ?? "";
|
|
872
|
-
if (!origin) {
|
|
873
|
-
const booted = await bootServer(opts.userCwd);
|
|
874
|
-
server = booted.server;
|
|
875
|
-
origin = booted.origin;
|
|
876
|
-
}
|
|
877
|
-
let browser;
|
|
878
|
-
try {
|
|
879
|
-
browser = await chromium.launch({ headless: true });
|
|
880
|
-
} catch (err) {
|
|
881
|
-
await server?.close();
|
|
882
|
-
throw new RenderUnavailableError(`${INSTALL_HINT}\n\nChromium failed to launch: ${err.message}`);
|
|
883
|
-
}
|
|
884
|
-
return {
|
|
885
|
-
origin,
|
|
886
|
-
async open(docId) {
|
|
887
|
-
const page = await browser.newPage({
|
|
888
|
-
viewport: {
|
|
889
|
-
width: 1280,
|
|
890
|
-
height: 1024
|
|
891
|
-
},
|
|
892
|
-
...opts.deviceScaleFactor !== void 0 ? { deviceScaleFactor: opts.deviceScaleFactor } : {}
|
|
893
|
-
});
|
|
894
|
-
const errors = [];
|
|
895
|
-
page.on("pageerror", (err) => errors.push(String(err?.message ?? err)));
|
|
896
|
-
await page.goto(`${origin}/d/${encodeURIComponent(docId)}`, {
|
|
897
|
-
waitUntil: "load",
|
|
898
|
-
timeout
|
|
899
|
-
});
|
|
900
|
-
try {
|
|
901
|
-
await page.waitForFunction("globalThis.__openDoc ? globalThis.__openDoc.status().ready : false", void 0, { timeout });
|
|
902
|
-
} catch {
|
|
903
|
-
await page.close();
|
|
904
|
-
const detail = errors.length ? `\n${errors.join("\n")}` : "";
|
|
905
|
-
throw new Error(`Document "${docId}" never finished rendering.${detail}`);
|
|
906
|
-
}
|
|
907
|
-
const status = await page.evaluate("globalThis.__openDoc.status()");
|
|
908
|
-
return {
|
|
909
|
-
status,
|
|
910
|
-
diagnose: () => page.evaluate("globalThis.__openDoc.diagnose()"),
|
|
911
|
-
async pdf() {
|
|
912
|
-
await page.evaluate("globalThis.__openDoc.preparePrint()");
|
|
913
|
-
try {
|
|
914
|
-
return await page.pdf({
|
|
915
|
-
printBackground: true,
|
|
916
|
-
preferCSSPageSize: true
|
|
917
|
-
});
|
|
918
|
-
} finally {
|
|
919
|
-
await page.evaluate("globalThis.__openDoc.releasePrint()");
|
|
920
|
-
}
|
|
921
|
-
},
|
|
922
|
-
async screenshot(pageNumber) {
|
|
923
|
-
if (pageNumber < 1 || pageNumber > status.pageCount) throw new Error(`page ${pageNumber} is out of range — "${docId}" has ${status.pageCount}`);
|
|
924
|
-
await page.evaluate("globalThis.__openDoc.preparePrint()");
|
|
925
|
-
await page.emulateMedia({ media: "print" });
|
|
926
|
-
try {
|
|
927
|
-
const sheet = await page.$(`${PRINT_PAGE_SELECTOR}:nth-child(${pageNumber})`);
|
|
928
|
-
if (!sheet) throw new Error(`page ${pageNumber} did not render`);
|
|
929
|
-
return await sheet.screenshot({ type: "png" });
|
|
930
|
-
} finally {
|
|
931
|
-
await page.emulateMedia({ media: null });
|
|
932
|
-
await page.evaluate("globalThis.__openDoc.releasePrint()");
|
|
933
|
-
}
|
|
934
|
-
},
|
|
935
|
-
html: () => page.evaluate("globalThis.__openDoc.htmlBundle()"),
|
|
936
|
-
docx: () => page.evaluate("globalThis.__openDoc.docxBundle()"),
|
|
937
|
-
close: () => page.close()
|
|
938
|
-
};
|
|
939
|
-
},
|
|
940
|
-
async close() {
|
|
941
|
-
await browser.close();
|
|
942
|
-
await server?.close();
|
|
943
|
-
}
|
|
944
|
-
};
|
|
945
|
-
}
|
|
946
|
-
//#endregion
|
|
947
1465
|
//#region src/ops/layout.ts
|
|
948
1466
|
/**
|
|
949
1467
|
* Chromium and a Vite server are expensive to start, and an agent checking its
|
|
@@ -1230,4 +1748,4 @@ async function fileDocument(ctx, docId, folderId) {
|
|
|
1230
1748
|
await writeManifest(ctx.manifestPath, manifest);
|
|
1231
1749
|
}
|
|
1232
1750
|
//#endregion
|
|
1233
|
-
export { listAssets as a, readTheme as c, closeRenderSession as d, exportDocument as f, slugify as h, findAssetUsages as i, writeAsset as l, importMarkdown as m, deleteAsset as n, listFolders as o, renderDocPage as p, fileDocument as r, listThemes as s, createFolder as t, checkLayout as u };
|
|
1751
|
+
export { listAssets as a, readTheme as c, closeRenderSession as d, exportDocument as f, diffDocument as g, slugify as h, findAssetUsages as i, writeAsset as l, importMarkdown as m, deleteAsset as n, listFolders as o, renderDocPage as p, fileDocument as r, listThemes as s, createFolder as t, checkLayout as u };
|