Skip to content

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

:bug: Preserve diagram backgrounds in image previews https://github.com/siyuan-note/siyuan/issues/19995

Daniel6e9c12001184
diff --git a/app/src/protyle/preview/diagram.test.ts b/app/src/protyle/preview/diagram.test.ts
index b249e17227..d4a1f79e20 100644
--- a/app/src/protyle/preview/diagram.test.ts
+++ b/app/src/protyle/preview/diagram.test.ts
@@ -50,12 +50,46 @@ for (const scenario of [
assert.deepEqual([Number(attributes.get("width")), Number(attributes.get("height"))], scenario.expected);
assert.equal(clone.style.width, `${scenario.expected[0]}px`);
assert.equal(clone.style.height, `${scenario.expected[1]}px`);
+ assert.equal(clone.style.backgroundColor, "#fff");
assert.equal(previewBlob.type, "image/svg+xml");
cleanup();
assert.deepEqual(revoked, ["blob:diagram"]);
});
}
+for (const subtype of ["mermaid", "echarts"]) {
+ for (const background of ["rgb(30, 30, 30)", "rgb(255, 255, 255)"]) {
+ test(`${subtype} preview uses the editor background ${background}`, async () => {
+ const editor = {parentElement: null};
+ const diagram = {parentElement: editor, getAttribute: () => subtype, querySelector: () => render};
+ const clone = {style: {}, querySelectorAll: () => [], setAttribute: () => {}};
+ const render = {
+ viewBox: {baseVal: {width: 300, height: 150}}, cloneNode: () => clone,
+ querySelectorAll: () => [], getBoundingClientRect: () => ({width: 300, height: 150}),
+ };
+ let rasterBackground: string;
+ const exports = {} as {previewDiagram: (element: unknown) => Promise<void>};
+ runInNewContext(compiled, {
+ exports, Blob,
+ require: () => ({Constants: {PROTYLE_CDN: ""}, addScript: async () => {}, previewImages: () => {}}),
+ window: {
+ getComputedStyle: (element: unknown) => ({length: 0,
+ backgroundColor: element === editor ? background : "rgba(0, 0, 0, 0)"}),
+ htmlToImage: {toBlob: async (_element: unknown, options: {backgroundColor: string}) => {
+ rasterBackground = options.backgroundColor;
+ return new Blob(["png"]);
+ }},
+ },
+ XMLSerializer: class { public serializeToString() { return "<svg/>"; } },
+ URL: {createObjectURL: () => "blob:diagram"},
+ });
+ await exports.previewDiagram(diagram);
+ assert.equal(subtype === "echarts" ? rasterBackground : (clone.style as {backgroundColor: string}).backgroundColor,
+ background);
+ });
+ }
+}
+
for (const carrier of ["object", "img", "empty"]) {
test(`PlantUML preview uses the ${carrier} resource without rasterizing or fetching`, async () => {
const url = "https://example.com/plantuml/svg/diagram";
diff --git a/app/src/protyle/preview/diagram.ts b/app/src/protyle/preview/diagram.ts
index 7798ec44be..6ae6c36113 100644
--- a/app/src/protyle/preview/diagram.ts
+++ b/app/src/protyle/preview/diagram.ts
@@ -5,6 +5,17 @@ import {getPlantumlImageURL} from "../render/plantumlImage";
const DIAGRAM_SUBTYPES = ["mermaid", "graphviz", "flowchart", "echarts", "plantuml"];
+// 从图表所在容器向外查找背景,使独立预览与编辑器中的图表保持一致。
+const getDiagramBackground = (element: HTMLElement) => {
+ for (let parent = element; parent; parent = parent.parentElement) {
+ const color = window.getComputedStyle(parent).backgroundColor;
+ if (color && color !== "transparent" && !/^rgba\([^)]*,\s*0\)$/.test(color)) {
+ return color;
+ }
+ }
+ return "#fff";
+};
+
export const getDiagramBlock = (element: HTMLElement) => {
if (!element) {
return false;
@@ -16,7 +27,7 @@ export const getDiagramBlock = (element: HTMLElement) => {
};
// 将继承的样式写入副本,使 SVG 在独立图片中保留编辑器内的显示效果。
-const createDiagramSVG = (element: SVGSVGElement) => {
+const createDiagramSVG = (element: SVGSVGElement, backgroundColor: string) => {
const clone = element.cloneNode(true) as SVGSVGElement;
const elements = [element, ...Array.from(element.querySelectorAll("*"))];
const clones = [clone, ...Array.from(clone.querySelectorAll("*"))];
@@ -40,7 +51,7 @@ const createDiagramSVG = (element: SVGSVGElement) => {
clone.style.width = `${width}px`;
clone.style.height = `${height}px`;
clone.style.maxWidth = "none";
- clone.style.backgroundColor = "#fff";
+ clone.style.backgroundColor = backgroundColor;
return new Blob([new XMLSerializer().serializeToString(clone)], {type: "image/svg+xml"});
};
@@ -64,9 +75,10 @@ export const previewDiagram = (diagramElement: HTMLElement) => {
let blob: Blob;
try {
+ const backgroundColor = getDiagramBackground(diagramElement);
blob = type === "echarts" ?
- await window.htmlToImage.toBlob(renderElement, {backgroundColor: "#fff"}) :
- createDiagramSVG(renderElement as SVGSVGElement);
+ await window.htmlToImage.toBlob(renderElement, {backgroundColor}) :
+ createDiagramSVG(renderElement as SVGSVGElement, backgroundColor);
} catch (e) {
return;
}