siyuan/app/src/protyle/render/mindmapRender.ts

146 lines
8.3 KiB
TypeScript
Raw Normal View History

2025-11-24 22:56:40 +08:00
import { addScript } from "../util/addScript";
import { Constants } from "../../constants";
import { hasClosestByClassName } from "../util/hasClosest";
import { genIconHTML } from "./util";
export const mindmapRender = (element: Element, cdn = Constants.PROTYLE_CDN) => {
let mindmapElements: Element[] = [];
if (element.getAttribute("data-subtype") === "mindmap") {
// 编辑器内代码块编辑渲染
mindmapElements = [element];
} else {
mindmapElements = Array.from(element.querySelectorAll('[data-subtype="mindmap"]'));
}
if (mindmapElements.length === 0) {
return;
}
2025-11-24 22:56:36 +08:00
// load d3 first, then markmap-lib, then markmap-view (in order)
addScript(`${cdn}/js/d3/d3.min.js?v6.7.0`, "protyleD3Script")
.then(() => addScript(`${cdn}/js/markmap/markmap-lib.min.js?v0.14.4`, "protyleMarkmapLibScript"))
.then(() => addScript(`${cdn}/js/markmap/markmap-view.min.js?v0.14.4`, "protyleMarkmapScript"))
2025-11-24 22:56:36 +08:00
.then(() => {
2025-11-24 22:56:40 +08:00
const wysiswgElement = hasClosestByClassName(element, "protyle-wysiwyg", true);
let width: number = undefined;
if (wysiswgElement && wysiswgElement.clientWidth > 0 && mindmapElements[0].firstElementChild.clientWidth === 0 && wysiswgElement.firstElementChild) {
width = wysiswgElement.firstElementChild.clientWidth;
}
2025-11-24 22:56:40 +08:00
mindmapElements.forEach((e: HTMLDivElement) => {
if (e.getAttribute("data-render") === "true") {
return;
}
if (!e.firstElementChild.classList.contains("protyle-icons")) {
e.insertAdjacentHTML("afterbegin", genIconHTML(wysiswgElement));
}
const renderElement = e.firstElementChild.nextElementSibling as HTMLElement;
if (!e.getAttribute("data-content")) {
renderElement.innerHTML = `<span style="position: absolute;left:0;top:0;width: 1px;">${Constants.ZWSP}</span>`;
return;
}
2025-11-24 22:56:40 +08:00
try {
// create or reuse container for markmap
if (!renderElement.lastElementChild || renderElement.childElementCount === 1) {
renderElement.innerHTML = `<span style="position: absolute;left:0;top:0;width: 1px;">${Constants.ZWSP}</span><div style="height:${e.style.height || "420px"}" contenteditable="false"></div>`;
} else {
renderElement.lastElementChild.classList.remove("ft__error");
}
2025-11-24 22:56:36 +08:00
2025-11-24 22:56:40 +08:00
// Convert stored content to markdown using Lute (prefer existing instance), then transform/render with markmap
const raw = Lute.UnEscapeHTMLStr(e.getAttribute("data-content"));
let md: string = raw;
// prefer protyle's lute instance if available
if ((window as any).protyle && (window as any).protyle.lute && typeof (window as any).protyle.lute.BlockDOM2Md === "function") {
md = (window as any).protyle.lute.BlockDOM2Md(raw);
} else if (typeof Lute === "function" && typeof Lute.New === "function") {
try {
const luteInst = Lute.New();
if (luteInst && typeof luteInst.BlockDOM2Md === "function") {
md = luteInst.BlockDOM2Md(raw);
} else if (luteInst && typeof luteInst.BlockDOM2HTML === "function") {
md = luteInst.BlockDOM2HTML(raw);
}
} catch (e) {
// fallback to raw
md = raw;
2025-11-24 22:56:36 +08:00
}
}
2025-11-24 22:56:40 +08:00
// Try to obtain markmap entry from loaded bundles (single unified reference)
const mm: any = (window as any).markmap || (window as any).Markmap || null;
2025-11-24 22:56:36 +08:00
// Prefer the Transformer API when available (example usage: transform -> getUsedAssets -> load assets -> create)
2025-11-24 22:56:40 +08:00
let data: any = null;
let rootData: any = null;
try {
if (mm && typeof mm.Transformer === "function") {
const transformer = new mm.Transformer();
// transform markdown -> { root, features }
const tx = transformer.transform(md) || {};
rootData = tx.root || null;
2025-11-24 22:56:36 +08:00
// select asset getter: prefer getUsedAssets (only assets used), fallback to getAssets
2025-11-24 22:56:40 +08:00
const assetsGetter = typeof transformer.getUsedAssets === "function" ? "getUsedAssets" : (typeof transformer.getAssets === "function" ? "getAssets" : null);
if (assetsGetter) {
const assets = (transformer as any)[assetsGetter](tx.features || {});
2025-11-24 22:56:40 +08:00
const styles = assets && assets.styles;
const scripts = assets && assets.scripts;
// Use markmap provided loaders when available
const loadCSS = typeof mm.loadCSS === "function" ? mm.loadCSS : null;
const loadJS = typeof mm.loadJS === "function" ? mm.loadJS : null;
if (styles && loadCSS) {
try { loadCSS(styles); } catch (err) { /* ignore */ }
2025-11-24 22:56:40 +08:00
}
if (scripts && loadJS) {
try { loadJS(scripts, { getMarkmap: () => (window as any).markmap || mm }); } catch (err) { /* ignore */ }
2025-11-24 22:56:40 +08:00
}
2025-11-24 22:56:36 +08:00
}
} else {
// fallback: try older transform functions
const transformFn = mm && (mm.transform || (window as any).markmap && (window as any).markmap.transform);
if (typeof transformFn === "function") {
data = transformFn(md);
}
2025-11-24 22:56:36 +08:00
}
2025-11-24 22:56:40 +08:00
} catch (e) {
// leave data/rootData null and let downstream handle it
2025-11-24 22:56:40 +08:00
data = null;
rootData = null;
2025-11-24 22:56:36 +08:00
}
2025-11-24 22:56:40 +08:00
// container for svg
const container = renderElement.lastElementChild as HTMLElement;
// clear existing content and append an svg for markmap
container.innerHTML = "";
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
// use width if calculated earlier
if (typeof width === "number" && width > 0) {
svg.setAttribute("width", String(width));
2025-11-24 22:56:36 +08:00
} else {
2025-11-24 22:56:40 +08:00
svg.setAttribute("width", "100%");
}
svg.setAttribute("height", "100%");
container.appendChild(svg);
// prefer Markmap.create if available
const MarkmapCtor = (mm && (mm.Markmap || mm.default || mm)) || (window as any).Markmap;
const options = {
duration: 0, // 🔥 禁用动画设为0
};
2025-11-24 22:56:40 +08:00
if (MarkmapCtor && typeof MarkmapCtor.create === "function") {
if (rootData) {
// When Transformer was used we have a `root` structure
MarkmapCtor.create(svg, options, rootData);
2025-11-24 22:56:36 +08:00
}
2025-11-24 22:56:40 +08:00
} else {
throw new Error("Markmap not available");
2025-11-24 22:56:36 +08:00
}
2025-11-24 22:56:40 +08:00
} catch (error) {
2025-11-24 22:56:36 +08:00
2025-11-24 22:56:40 +08:00
renderElement.innerHTML = `<span style="position: absolute;left:0;top:0;width: 1px;">${Constants.ZWSP}</span><div class="ft__error" style="height:${e.style.height || "420px"}" contenteditable="false">Mindmap render error: <br>${error}</div>`;
}
e.setAttribute("data-render", "true");
});
});
};