Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Reduce mind map whitespace in PDF exports https://github.com/siyuan-note/siyuan/issues/18831

Vanessaf2b04af9556a
diff --git a/app/src/assets/scss/protyle/_list-mindmap.scss b/app/src/assets/scss/protyle/_list-mindmap.scss
index 1f96a0fff3..5a5cb020ed 100644
--- a/app/src/assets/scss/protyle/_list-mindmap.scss
+++ b/app/src/assets/scss/protyle/_list-mindmap.scss
@@ -564,7 +564,8 @@
@media print {
.list-mindmap {
break-inside: avoid;
- height: 420px;
+ height: var(--list-mindmap-print-height, 420px);
+ min-height: 0;
&__toolbar,
&__inspector,
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index b663378d2d..4f252b1c6b 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -807,6 +807,27 @@ const browserCases = async (sourceCode: string, css: string) => {
model.metadata.relations = savedRelations;
view.update(model);
+ // 打印收紧画布高度,结束打印后恢复屏幕上的缩放和平移。
+ await settle();
+ const screenTransform = [view.scale, view.offsetX, view.offsetY];
+ const screenHeight = host.offsetHeight;
+ await require("electron").ipcRenderer.invoke("list-mindmap-print-media", "print");
+ window.dispatchEvent(new Event("beforeprint"));
+ await settle();
+ check.ok(host.offsetHeight < screenHeight, "PDF does not retain the fixed editor canvas height");
+ const printBounds = viewport.getBoundingClientRect();
+ for (const node of view.positions.values()) {
+ check.ok(node.x * view.scale + view.offsetX >= 0);
+ check.ok((node.x + node.width) * view.scale + view.offsetX <= printBounds.width + 1);
+ check.ok(node.y * view.scale + view.offsetY >= 0);
+ check.ok((node.y + node.height) * view.scale + view.offsetY <= printBounds.height + 1);
+ }
+ await require("electron").ipcRenderer.invoke("list-mindmap-print-media", "screen");
+ window.dispatchEvent(new Event("afterprint"));
+ await settle();
+ check.equal(host.offsetHeight, screenHeight);
+ check.deepEqual([view.scale, view.offsetX, view.offsetY], screenTransform);
+
// 全屏使用编辑器同款窗口内布局,不调用浏览器全屏,并在退出和销毁时恢复原位置。
Object.defineProperty(host, "requestFullscreen", {configurable: true, value: () => check.fail("Native fullscreen must not be requested")});
hostParent.style.transform = "translateX(5px)";
@@ -1087,6 +1108,10 @@ app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});
app.commandLine.appendSwitch("disable-gpu");
app.whenReady().then(async () => {
const win = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true}});
+ win.webContents.debugger.attach("1.3");
+ ipcMain.handle("list-mindmap-print-media", async (_event, media) => {
+ await win.webContents.debugger.sendCommand("Emulation.setEmulatedMedia", {media});
+ });
ipcMain.handle("list-mindmap-native-input", async (_event, events) => {
for (const event of events) {
win.webContents.sendInputEvent(event);
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index 577fe0e342..d42bc89b79 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -176,13 +176,14 @@ export class ListMindmapView {
event.stopPropagation();
}));
this.listen(window, "beforeprint", () => {
- this.printTransform = {scale: this.scale, offsetX: this.offsetX, offsetY: this.offsetY};
- this.fit();
+ this.printTransform ||= {scale: this.scale, offsetX: this.offsetX, offsetY: this.offsetY};
+ this.fitPrint();
});
this.listen(window, "afterprint", () => {
if (this.printTransform) {
Object.assign(this, this.printTransform);
this.printTransform = undefined;
+ this.options.host.style.removeProperty("--list-mindmap-print-height");
this.draw();
}
});
@@ -546,7 +547,9 @@ export class ListMindmapView {
this.model.nodes.get(this.foldAnchor.id)?.collapsed) === this.foldAnchor.collapsed) {
this.foldAnchor = undefined;
}
- if (this.initialFit) {
+ if (this.printTransform) {
+ this.fitPrint();
+ } else if (this.initialFit) {
this.initialFit = false;
this.fit();
} else {
@@ -1304,6 +1307,41 @@ export class ListMindmapView {
this.draw();
}
+ private fitPrint() {
+ if (!this.positions.size || !this.viewport.clientWidth) {
+ return;
+ }
+ // 打印按实际可见内容裁定高度,不计入编辑画布为操作按钮预留的空间。
+ let left = Infinity;
+ let top = Infinity;
+ let right = -Infinity;
+ let bottom = -Infinity;
+ const include = (x: number, y: number, width = 0, height = 0) => {
+ left = Math.min(left, x);
+ top = Math.min(top, y);
+ right = Math.max(right, x + width);
+ bottom = Math.max(bottom, y + height);
+ };
+ this.positions.forEach(node => include(node.x, node.y - 1, node.width, node.height + 1));
+ this.relationRoutes.forEach(points => points.forEach(point => include(point.x, point.y)));
+ this.relationElements.forEach(element => {
+ if (!element.hidden && element.style.visibility !== "hidden") {
+ const x = parseFloat(element.style.left);
+ const y = parseFloat(element.style.top);
+ if (Number.isFinite(x) && Number.isFinite(y)) {
+ include(x - element.offsetWidth / 2, y - element.offsetHeight / 2,
+ element.offsetWidth, element.offsetHeight);
+ }
+ }
+ });
+ const width = this.viewport.clientWidth;
+ this.scale = Math.min(1, Math.max(1, width - 24) / Math.max(1, right - left));
+ this.options.host.style.setProperty("--list-mindmap-print-height", `${Math.ceil((bottom - top) * this.scale + 26)}px`);
+ this.offsetX = (width - (right - left) * this.scale) / 2 - left * this.scale;
+ this.offsetY = 12 - top * this.scale;
+ this.draw();
+ }
+
public fit() {
const width = this.viewport.clientWidth;
const height = this.viewport.clientHeight;