Skip to content

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

:art: https://github.com/siyuan-note/siyuan/issues/12691

Vanessa0dd669080ecc
diff --git a/app/src/protyle/export/util.ts b/app/src/protyle/export/util.ts
index 7560d7547d..5f8b79f20b 100644
--- a/app/src/protyle/export/util.ts
+++ b/app/src/protyle/export/util.ts
@@ -90,7 +90,7 @@ export const exportImage = (id: string) => {
item.textContent = (index + 1).toString();
});
setTimeout(() => {
- addScript("/stage/protyle/js/html-to-image.min.js?v=1.11.13", "protyleHtml2image").then(async () => {
+ addScript("${Constants.PROTYLE_CDN}/js/html-to-image.min.js?v=1.11.13", "protyleHtml2image").then(async () => {
let blob = await window.htmlToImage.toBlob(exportDialog.element.querySelector(".b3-dialog__content"));
if (isIPhone() || isSafari()) {
await window.htmlToImage.toBlob(contentElement);
@@ -138,7 +138,7 @@ export const exportImage = (id: string) => {
if (window.siyuan.config.export.imageWatermarkStr.startsWith("http")) {
watermarkPreviewElement.setAttribute("style", `background-image: url(${window.siyuan.config.export.imageWatermarkStr});background-repeat: repeat;position: absolute;top: 0;left: 0;width: 100%;height: 100%;border-radius: var(--b3-border-radius-b);`);
} else {
- addScript("/stage/protyle/js/html-to-image.min.js?v=1.11.13", "protyleHtml2image").then(() => {
+ addScript("${Constants.PROTYLE_CDN}/js/html-to-image.min.js?v=1.11.13", "protyleHtml2image").then(() => {
const width = Math.max(exportDialog.element.querySelector(".export-img").clientWidth / 3, 150);
watermarkPreviewElement.setAttribute("style", `width: ${width}px;height: ${width}px;display: flex;justify-content: center;align-items: center;color: var(--b3-border-color);font-size: 14px;`);
watermarkPreviewElement.innerHTML = `<div style="transform: rotate(-45deg)">${window.siyuan.config.export.imageWatermarkStr}</div>`;
diff --git a/app/src/protyle/preview/diagram.ts b/app/src/protyle/preview/diagram.ts
new file mode 100644
index 0000000000..236c9c7495
--- /dev/null
+++ b/app/src/protyle/preview/diagram.ts
@@ -0,0 +1,40 @@
+import {Constants} from "../../constants";
+import {addScript} from "../util/addScript";
+import {previewImages} from "./image";
+
+const DIAGRAM_SUBTYPES = ["mermaid", "graphviz", "flowchart", "plantuml", "echarts"];
+
+export const getDiagramBlock = (element: HTMLElement) => {
+ if (!element) {
+ return false;
+ }
+ if (DIAGRAM_SUBTYPES.includes(element.getAttribute("data-subtype"))) {
+ return element;
+ }
+ return false;
+};
+
+/**
+ * Open a rendered diagram (mermaid / graphviz / flowchart / plantuml / mindmap) in a
+ * full-screen viewer that supports zoom, pan and rotation. The diagram's SVG is rasterized
+ * to a PNG via html-to-image (the same library used by the "export as image" action) and
+ * handed to the existing image viewer so diagrams get the same controls as image previews.
+ *
+ * https://github.com/siyuan-note/siyuan/issues/12691
+ */
+export const previewDiagram = (diagramElement: HTMLElement) => {
+ addScript(`${Constants.PROTYLE_CDN}/js/html-to-image.min.js?v=1.11.13`, "protyleHtml2image").then(async () => {
+ const renderElement = diagramElement.querySelector('[contenteditable="false"]') || diagramElement;
+ let blob: Blob;
+ try {
+ blob = await window.htmlToImage.toBlob(renderElement);
+ } catch (e) {
+ return;
+ }
+ const url = URL.createObjectURL(blob);
+ previewImages([url], url);
+ // viewerjs copies the <img src> into its own container, so the object URL can be
+ // released once the viewer has finished reading it.
+ setTimeout(() => URL.revokeObjectURL(url), 10000);
+ });
+};
diff --git a/app/src/protyle/preview/index.ts b/app/src/protyle/preview/index.ts
index 00a4732339..acef69d0c5 100644
--- a/app/src/protyle/preview/index.ts
+++ b/app/src/protyle/preview/index.ts
@@ -3,6 +3,7 @@ import {focusByRange} from "../util/selection";
import {showMessage} from "../../dialog/message";
import {isLocalPath, pathPosix} from "../../util/pathName";
import {previewDocImage} from "./image";
+import {getDiagramBlock, previewDiagram} from "./diagram";
import {needSubscribe} from "../../util/needSubscribe";
import {Constants} from "../../constants";
import {getSearch, isMobile} from "../../util/functions";
@@ -19,7 +20,7 @@ import {highlightRender} from "../render/highlightRender";
import {speechRender} from "../render/speechRender";
import {avRender} from "../render/av/render";
import {getPadding} from "../ui/initUI";
-import {hasClosestByAttribute} from "../util/hasClosest";
+import {hasTopClosestByAttribute} from "../util/hasClosest";
import {addScriptSync} from "../util/addScript";
export class Preview {
@@ -146,7 +147,7 @@ export class Preview {
}
target = target.parentElement;
}
- const nodeElement = hasClosestByAttribute(event.target as HTMLElement, "id", undefined);
+ const nodeElement = hasTopClosestByAttribute(event.target as HTMLElement, "id", undefined);
if (nodeElement) {
// 用于点击后大纲定位
this.element.querySelectorAll(".protyle-wysiwyg--select").forEach(item => {
@@ -164,6 +165,13 @@ export class Preview {
/// #else
window.siyuan.mobile.docks.outline?.setCurrentByPreview(nodeElement);
/// #endif
+ const diagramElement = getDiagramBlock(nodeElement);
+ if (diagramElement) {
+ previewDiagram(diagramElement);
+ event.stopPropagation();
+ event.preventDefault();
+ return;
+ }
}
});
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts
index 2581a70755..02d60cc24e 100644
--- a/app/src/protyle/toolbar/index.ts
+++ b/app/src/protyle/toolbar/index.ts
@@ -1061,7 +1061,7 @@ export class Toolbar {
return;
}
setTimeout(() => {
- addScript("/stage/protyle/js/html-to-image.min.js?v=1.11.13", "protyleHtml2image").then(() => {
+ addScript("${Constants.PROTYLE_CDN}/js/html-to-image.min.js?v=1.11.13", "protyleHtml2image").then(() => {
(renderElement as HTMLHtmlElement).style.display = "inline-block";
window.htmlToImage.toBlob(renderElement).then(blob => {
(renderElement as HTMLHtmlElement).style.display = "";
diff --git a/app/src/protyle/util/hasClosest.ts b/app/src/protyle/util/hasClosest.ts
index d78b75672d..e628a3ade8 100644
--- a/app/src/protyle/util/hasClosest.ts
+++ b/app/src/protyle/util/hasClosest.ts
@@ -52,7 +52,9 @@ export const hasClosestByAttribute = (element: Node, attr: string, value: string
}
let e = element as HTMLElement;
let isClosest = false;
- while (e && !isClosest && (top ? e.tagName !== "BODY" : !e.classList.contains("protyle-wysiwyg"))) {
+ while (e && !isClosest && (top ? e.tagName !== "BODY" : (
+ !e.classList.contains("protyle-wysiwyg") &&!e.classList.contains("b3-typography")
+ ))) {
if (typeof value === "string" && e.getAttribute(attr)?.split(" ").includes(value)) {
isClosest = true;
} else if (typeof value !== "string" && e.hasAttribute(attr)) {
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 82fc268de3..7c9f3e6d15 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -21,6 +21,7 @@ import {
import {Constants} from "../../constants";
import {isMobile} from "../../util/functions";
import {previewDocImage} from "../preview/image";
+import {getDiagramBlock, previewDiagram} from "../preview/diagram";
import {
contentMenu,
enterBack,
@@ -2690,6 +2691,13 @@ export class WYSIWYG {
previewDocImage((event.target as HTMLElement).getAttribute("src"), protyle.block.rootID);
return;
}
+ // https://github.com/siyuan-note/siyuan/issues/12691
+ const diagramElement = getDiagramBlock(hasClosestBlock(event.target) as HTMLElement);
+ if (diagramElement) {
+ previewDiagram(diagramElement);
+ event.stopPropagation();
+ event.preventDefault();
+ }
});
let mobileBlur = false;
this.element.addEventListener("click", (event: MouseEvent & { target: HTMLElement }) => {