feat: 实现 DOCX 媒体预处理与跨端 PNG 捕获
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { WebContents } from "electron";
|
||||
import {
|
||||
createPagedDocumentPayload,
|
||||
defaultExportConfig,
|
||||
type DocxMediaCapturePlan
|
||||
} from "@md-to-pdf/core";
|
||||
import { captureDocxMediaWithElectronWebContents } from "../src/electron-docx-media-capture.js";
|
||||
|
||||
const plan: DocxMediaCapturePlan = {
|
||||
targets: [
|
||||
{
|
||||
id: "docx-media-1",
|
||||
kind: "image",
|
||||
ordinal: 1,
|
||||
kindOrdinal: 1,
|
||||
altText: "图片",
|
||||
displayWidthPx: 200,
|
||||
displayHeightPx: 100,
|
||||
captureX: 12,
|
||||
captureY: 24,
|
||||
captureWidthPx: 200,
|
||||
captureHeightPx: 100,
|
||||
rasterScale: 3.125
|
||||
}
|
||||
],
|
||||
echartsErrors: [],
|
||||
mermaidErrors: []
|
||||
};
|
||||
|
||||
const request = {
|
||||
payload: createPagedDocumentPayload({
|
||||
document: {
|
||||
rendererVersion: 1,
|
||||
articleHtml: '<article id="write"></article>',
|
||||
bodyHtml: "",
|
||||
metadata: {
|
||||
title: "",
|
||||
author: "",
|
||||
subject: "",
|
||||
keywords: [],
|
||||
language: "zh-CN"
|
||||
},
|
||||
features: [],
|
||||
warnings: []
|
||||
},
|
||||
fileName: "test.md",
|
||||
themeCss: "",
|
||||
exportConfig: defaultExportConfig
|
||||
}),
|
||||
dimensions: {
|
||||
contentWidthPx: 600,
|
||||
contentHeightPx: 900
|
||||
}
|
||||
};
|
||||
|
||||
describe("Electron DOCX 媒体捕获", () => {
|
||||
it("复用隐藏 Chromium 并在捕获后释放调试会话", async () => {
|
||||
let attached = false;
|
||||
const attach = vi.fn(() => {
|
||||
attached = true;
|
||||
});
|
||||
const detach = vi.fn(() => {
|
||||
attached = false;
|
||||
});
|
||||
const sendCommand = vi.fn(async () => ({
|
||||
data: Buffer.from("png").toString("base64")
|
||||
}));
|
||||
const webContents = {
|
||||
executeJavaScript: vi.fn(async () => plan),
|
||||
debugger: {
|
||||
isAttached: vi.fn(() => attached),
|
||||
attach,
|
||||
detach,
|
||||
sendCommand
|
||||
}
|
||||
} as unknown as WebContents;
|
||||
|
||||
const result = await captureDocxMediaWithElectronWebContents(
|
||||
webContents,
|
||||
request
|
||||
);
|
||||
|
||||
expect(result.captures[0]).toEqual({
|
||||
id: "docx-media-1",
|
||||
png: Buffer.from("png")
|
||||
});
|
||||
expect(attach).toHaveBeenCalledWith("1.3");
|
||||
expect(sendCommand).toHaveBeenCalledWith(
|
||||
"Page.captureScreenshot",
|
||||
expect.objectContaining({
|
||||
format: "png",
|
||||
clip: expect.objectContaining({ scale: 3.125 })
|
||||
})
|
||||
);
|
||||
expect(detach).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user