Skip to content

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

:sparkles: Support Mermaid layouts through chart block custom attributes https://github.com/siyuan-note/siyuan/issues/16129

Danielc36a50cf103c
diff --git a/app/src/protyle/render/mermaidLayout.test.ts b/app/src/protyle/render/mermaidLayout.test.ts
new file mode 100644
index 0000000000..dc4bad66ca
--- /dev/null
+++ b/app/src/protyle/render/mermaidLayout.test.ts
@@ -0,0 +1,36 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {applyMermaidLayout, getMermaidLayout} from "./mermaidLayout";
+
+describe("mermaidLayout", () => {
+ it("accepts supported layout names", () => {
+ assert.equal(getMermaidLayout("dagre"), "dagre");
+ assert.equal(getMermaidLayout(" cose-bilkent "), "cose-bilkent");
+ assert.equal(getMermaidLayout("TIDY-TREE"), "tidy-tree");
+ });
+
+ it("ignores unsupported layout names", () => {
+ assert.equal(getMermaidLayout("elk"), undefined);
+ assert.equal(getMermaidLayout(""), undefined);
+ assert.equal(getMermaidLayout(), undefined);
+ });
+
+ it("keeps content unchanged without a layout", () => {
+ const content = "mindmap\n Root";
+ assert.equal(applyMermaidLayout(content), content);
+ });
+
+ it("appends a layout directive so the custom attribute takes precedence", () => {
+ const content = `---
+config:
+ layout: dagre
+---
+mindmap
+ Root
+%%{init: {"layout":"cose-bilkent"}}%%`;
+ const result = applyMermaidLayout(content, "tidy-tree");
+
+ assert.equal(result.startsWith(content), true);
+ assert.equal(result.lastIndexOf('{"layout":"tidy-tree"}') > result.lastIndexOf('{"layout":"cose-bilkent"}'), true);
+ });
+});
diff --git a/app/src/protyle/render/mermaidLayout.ts b/app/src/protyle/render/mermaidLayout.ts
new file mode 100644
index 0000000000..524bf536be
--- /dev/null
+++ b/app/src/protyle/render/mermaidLayout.ts
@@ -0,0 +1,22 @@
+export const MERMAID_LAYOUT_ATTR = "custom-mermaid-layout";
+
+export type TMermaidLayout = "dagre" | "cose-bilkent" | "tidy-tree";
+
+const MERMAID_LAYOUTS = new Set<TMermaidLayout>([
+ "dagre",
+ "cose-bilkent",
+ "tidy-tree",
+]);
+
+export const getMermaidLayout = (value?: string | null): TMermaidLayout | undefined => {
+ const layout = value?.trim().toLowerCase() as TMermaidLayout;
+ return MERMAID_LAYOUTS.has(layout) ? layout : undefined;
+};
+
+export const applyMermaidLayout = (content: string, layout?: TMermaidLayout) => {
+ if (!layout) {
+ return content;
+ }
+ const separator = content.endsWith("\n") ? "" : "\n";
+ return `${content}${separator}%%{init: ${JSON.stringify({layout})}}%%`;
+};
diff --git a/app/src/protyle/render/mermaidRender.ts b/app/src/protyle/render/mermaidRender.ts
index 5e24c37280..ffbde2b87d 100644
--- a/app/src/protyle/render/mermaidRender.ts
+++ b/app/src/protyle/render/mermaidRender.ts
@@ -2,13 +2,31 @@ import {addScript} from "../util/addScript";
import {Constants} from "../../constants";
import {hasClosestByAttribute, hasClosestByClassName} from "../util/hasClosest";
import {genIconHTML} from "./util";
+import {applyMermaidLayout, getMermaidLayout, MERMAID_LAYOUT_ATTR} from "./mermaidLayout";
+
+let mermaidTidyTreePromise: Promise<void>;
+
+const registerMermaidLayouts = (mermaidElements: Element[], cdn: string) => {
+ if (!mermaidElements.some((item) => getMermaidLayout(item.getAttribute(MERMAID_LAYOUT_ATTR)) === "tidy-tree")) {
+ return Promise.resolve();
+ }
+ if (!mermaidTidyTreePromise) {
+ mermaidTidyTreePromise = addScript(
+ `${cdn}/js/mermaid/mermaid-layout-tidy-tree.min.js?v=0.2.1`,
+ "protyleMermaidTidyTreeScript"
+ ).then(() => {
+ window.mermaid.registerLayoutLoaders(window.mermaidTidyTree);
+ });
+ }
+ return mermaidTidyTreePromise;
+};
export const mermaidRender = (element: Element, cdn = Constants.PROTYLE_CDN) => {
- let mermaidElements: Element[] | NodeListOf<Element> = [];
+ let mermaidElements: Element[] = [];
if (element.getAttribute("data-subtype") === "mermaid" && element.getAttribute("data-render") !== "true") {
mermaidElements = [element];
} else {
- mermaidElements = element.querySelectorAll('[data-subtype="mermaid"]:not([data-render="true"])');
+ mermaidElements = Array.from(element.querySelectorAll('[data-subtype="mermaid"]:not([data-render="true"])'));
}
if (mermaidElements.length === 0) {
return;
@@ -16,6 +34,7 @@ export const mermaidRender = (element: Element, cdn = Constants.PROTYLE_CDN) =>
addScript(`${cdn}/js/mermaid/mermaid.min.js?v=11.13.0`, "protyleMermaidScript").then(() => {
addScript(`${cdn}/js/mermaid/mermaid-zenuml.min.js?v=0.2.2`, "protyleMermaidZenumlScript").then(async () => {
await window.mermaid.registerExternalDiagrams([window.zenuml]);
+ await registerMermaidLayouts(mermaidElements, cdn);
window.mermaid.registerIconPacks([
{
name: "logos",
@@ -97,7 +116,11 @@ const initMermaid = (mermaidElements: Element[]) => {
const id = "mermaid" + Lute.NewNodeID();
try {
renderElement.innerHTML = `<span style="position: absolute;left:0;top:0;width: 1px;">${Constants.ZWSP}</span><div contenteditable="false"><span id="${id}"></span></div>`;
- const mermaidData = await window.mermaid.render(id, Lute.UnEscapeHTMLStr(item.getAttribute("data-content")));
+ const content = applyMermaidLayout(
+ Lute.UnEscapeHTMLStr(item.getAttribute("data-content")),
+ getMermaidLayout(item.getAttribute(MERMAID_LAYOUT_ATTR))
+ );
+ const mermaidData = await window.mermaid.render(id, content);
let svg = mermaidData.svg.replace(/(href|src|xlink:href)\s*=\s*["']\\\\/gi, (match, p1) => `${p1}="about:blank"`);
svg = window.DOMPurify.sanitize(svg, {
USE_PROFILES: {svg: true, svgFilters: true, mathMl: true},
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 3c3ea5fb46..2eef5eb751 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -38,6 +38,7 @@ import {
cleanHeadingNumberOperations,
queueHeadingNumberRefresh
} from "../util/headingNumber";
+import {MERMAID_LAYOUT_ATTR} from "../render/mermaidLayout";
const removeTopElement = (updateElement: Element, protyle: IProtyle) => {
// 移动到其他文档中,该块需移除
@@ -760,6 +761,7 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUnd
}
if (operation.action === "updateAttrs") { // 调用接口才推送
const data = operation.data as any;
+ const mermaidLayoutChanged = data.new[MERMAID_LAYOUT_ATTR] !== data.old[MERMAID_LAYOUT_ATTR];
const attrsResult: Record<string, string> = {};
let bookmarkHTML = "";
let nameHTML = "";
@@ -884,6 +886,10 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUnd
nodeAttrHTML += refElement.outerHTML;
}
attrElement.innerHTML = nodeAttrHTML + Constants.ZWSP;
+ if (mermaidLayoutChanged && item.getAttribute("data-subtype") === "mermaid") {
+ item.removeAttribute("data-render");
+ processRender(item);
+ }
});
return;
}
diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts
index ee3ba65469..24dae7c49e 100644
--- a/app/src/types/index.d.ts
+++ b/app/src/types/index.d.ts
@@ -249,10 +249,12 @@ interface Window {
}): string;
};
zenuml: object,
+ mermaidTidyTree: object[],
mermaid: {
initialize(options: any): void,
- render(id: string, text: string): { svg: string },
- registerExternalDiagrams(ex: object[]): void,
+ render(id: string, text: string): Promise<{ svg: string }>,
+ registerExternalDiagrams(ex: object[]): Promise<void>,
+ registerLayoutLoaders(layouts: object[]): void,
registerIconPacks(options: {
name: string,
loader(): Promise<Response>
diff --git a/app/stage/protyle/js/mermaid/mermaid-layout-tidy-tree.min.js b/app/stage/protyle/js/mermaid/mermaid-layout-tidy-tree.min.js
new file mode 100644
index 0000000000..4fd53ce654
--- /dev/null
+++ b/app/stage/protyle/js/mermaid/mermaid-layout-tidy-tree.min.js
@@ -0,0 +1,3 @@
+/*! @mermaid-js/layout-tidy-tree v0.2.1 and non-layered-tidy-tree-layout v2.0.2 | MIT */
+var mermaidTidyTree=(()=>{var F=Object.defineProperty,Pe=Object.defineProperties,Ae=Object.getOwnPropertyDescriptor,Ye=Object.getOwnPropertyDescriptors,_e=Object.getOwnPropertyNames,le=Object.getOwnPropertySymbols;var ce=Object.prototype.hasOwnProperty,$e=Object.prototype.propertyIsEnumerable;var ge=(e,t,i)=>t in e?F(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i,A=(e,t)=>{for(var i in t||(t={}))ce.call(t,i)&&ge(e,i,t[i]);if(le)for(var i of le(t))$e.call(t,i)&&ge(e,i,t[i]);return e},Y=(e,t)=>Pe(e,Ye(t));var ue=(e,t,i)=>()=>{if(i)throw i[0];try{return e&&(t=e(e=0)),t}catch(r){throw i=[r],r}};var ye=(e,t)=>{for(var i in t)F(e,i,{get:t[i],enumerable:!0})},Ce=(e,t,i,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of _e(t))!ce.call(e,o)&&o!==i&&F(e,o,{get:()=>t[o],enumerable:!(r=Ae(t,o))||r.enumerable});return e};var Re=e=>Ce(F({},"__esModule",{value:!0}),e);var C=(e,t,i)=>new Promise((r,o)=>{var d=s=>{try{n(i.next(s))}catch(x){o(x)}},h=s=>{try{n(i.throw(s))}catch(x){o(x)}},n=s=>s.done?r(s.value):Promise.resolve(s.value).then(d,h);n((i=i.apply(e,t)).next())});function Q(e){e.cs===0?(e.el=e,e.er=e,e.msel=e.mser=0):(e.el=e.c[0].el,e.msel=e.c[0].msel,e.er=e.c[e.cs-1].er,e.mser=e.c[e.cs-1].mser)}function R(e){return e.y+e.h}function U(e,t,i){for(;i!==null&&e>=i.lowY;)i=i.next;return new qe(e,t,i)}function me(e,t,i,r){if(i!==t-1){let o=t-i;e.c[i+1].shift+=r/o,e.c[t].shift-=r/o,e.c[t].change-=r-r/o}}function xe(e,t,i,r){e.c[t].mod+=r,e.c[t].msel+=r,e.c[t].mser+=r,me(e,t,i,r)}function fe(e){return e.cs===0?e.tl:e.c[0]}function we(e){return e.cs===0?e.tr:e.c[e.cs-1]}function pe(e,t,i,r){let o=e.c[0].el;o.tl=i;let d=r-i.mod-e.c[0].msel;o.mod+=d,o.prelim-=d,e.c[0].el=e.c[t].el,e.c[0].msel=e.c[t].msel}function be(e,t,i,r){let o=e.c[t].er;o.tr=i;let d=r-i.mod-e.c[t].mser;o.mod+=d,o.prelim-=d,e.c[t].er=e.c[t-1].er,e.c[t].mser=e.c[t-1].mser}function Ne(e,t,i){let r=e.c[t-1],o=r.mod,d=e.c[t],h=d.mod;for(;r!==null&&d!==null;){R(r)>i.lowY&&(i=i.next);let n=o+r.prelim+r.w-(h+d.prelim);n>0&&(h+=n,xe(e,t,i.index,n));let s=R(r),x=R(d);s<=x&&(r=we(r),r!==null&&(o+=r.mod)),s>=x&&(d=fe(d),d!==null&&(h+=d.mod))}r===null&&d!==null?pe(e,t,d,h):r!==null&&d===null&&be(e,t,r,o)}function ve(e){e.prelim=(e.c[0].prelim+e.c[0].mod+e.c[e.cs-1].mod+e.c[e.cs-1].prelim+e.c[e.cs-1].w)/2-e.w/2}function G(e){if(e.cs===0){Q(e);return}G(e.c[0]);let t=U(R(e.c[0].el),0,null);for(let i=1;i<e.cs;i++){G(e.c[i]);let r=R(e.c[i].er);Ne(e,i,t),t=U(r,i,t)}ve(e),Q(e)}function Me(e){let t=0,i=0;for(let r=0;r<e.cs;r++)t+=e.c[r].shift,i+=t+e.c[r].change,e.c[r].mod+=i}function Z(e,t){t+=e.mod,e.x=e.prelim+t,Me(e);for(let i=0;i<e.cs;i++)Z(e.c[i],t)}function Te(e){G(e),Z(e,0)}function J(e){let t=50;return new Promise((i,r)=>{try{if(!e.nodes||!Array.isArray(e.nodes)||e.nodes.length===0)throw new Error("No nodes found in layout data");(!e.edges||!Array.isArray(e.edges))&&(e.edges=[]);let{leftTree:o,rightTree:d,rootNode:h}=Ee(e),n=20,s=40;t=30;let x=new ke(n,s),p=new De(x),m=null,c=null;o&&(m=p.layout(o).result),d&&(c=p.layout(d).result);let N=Be(h,m,c),b=Le(e.edges,N,t);i({nodes:N,edges:b})}catch(o){r(o)}})}function Ee(e){var b,u,T;let{nodes:t,edges:i}=e,r=new Map;t.forEach(l=>r.set(l.id,l));let o=new Map,d=new Map;i.forEach(l=>{let f=l.start,y=l.end;f&&y&&(o.has(f)||o.set(f,[]),o.get(f).push(y),d.set(y,f))});let h=t.find(l=>!d.has(l.id));if(!h&&t.length===0)throw new Error("No nodes available to create tree");let n=h!=null?h:t[0],s={id:n.id,width:(b=n.width)!=null?b:100,height:(u=n.height)!=null?u:50,_originalNode:n},x=(T=o.get(n.id))!=null?T:[],p=[],m=[];x.forEach((l,f)=>{f%2===0?p.push(l):m.push(l)});let c=p.length>0?V(p,o,r):null,N=m.length>0?V(m,o,r):null;return{leftTree:c,rightTree:N,rootNode:s}}function V(e,t,i){return{id:`virtual-root-${Math.random()}`,width:1,height:1,children:e.map(r=>i.get(r)).filter(r=>r!==void 0).map(r=>ee(r,t,i))}}function ee(e,t,i){var o,d,h;let r=((o=t.get(e.id))!=null?o:[]).map(n=>i.get(n)).filter(n=>n!==void 0).map(n=>ee(n,t,i));return{id:e.id,width:(d=e.height)!=null?d:50,height:(h=e.width)!=null?h:100,children:r.length>0?r:void 0,_originalNode:e}}function Be(e,t,i){var N,b,u,T;let r=[],o=e.width/2+30,d=[],h=[];t!=null&&t.children&&te(t.children,d,0-o,0),i!=null&&i.children&&ie(i.children,h,0+o,0);let n=0,s=0;if(d.length>0){let l=[...new Set(d.map(y=>y.x))].sort((y,L)=>L-y)[0],f=d.filter(y=>y.x===l);if(f.length>0){let y=Math.min(...f.map(M=>{var v;return M.y-((v=M.height)!=null?v:50)/2})),L=Math.max(...f.map(M=>{var v;return M.y+((v=M.height)!=null?v:50)/2}));n=(y+L)/2}}if(h.length>0){let l=[...new Set(h.map(y=>y.x))].sort((y,L)=>y-L)[0],f=h.filter(y=>y.x===l);if(f.length>0){let y=Math.min(...f.map(M=>{var v;return M.y-((v=M.height)!=null?v:50)/2})),L=Math.max(...f.map(M=>{var v;return M.y+((v=M.height)!=null?v:50)/2}));s=(y+L)/2}}let x=-n,p=-s;r.push({id:String(e.id),x:0,y:20,section:"root",width:(b=(N=e._originalNode)==null?void 0:N.width)!=null?b:e.width,height:(T=(u=e._originalNode)==null?void 0:u.height)!=null?T:e.height,originalNode:e._originalNode});let m=d.map(l=>{var f,y;return{id:l.id,x:l.x-((f=l.width)!=null?f:0)/2,y:l.y+x+((y=l.height)!=null?y:0)/2,section:"left",width:l.width,height:l.height,originalNode:l.originalNode}}),c=h.map(l=>{var f,y;return{id:l.id,x:l.x+((f=l.width)!=null?f:0)/2,y:l.y+p+((y=l.height)!=null?y:0)/2,section:"right",width:l.width,height:l.height,originalNode:l.originalNode}});return r.push(...m),r.push(...c),r}function te(e,t,i,r){e.forEach(o=>{var p,m,c,N,b,u;let d=(p=o.y)!=null?p:0,h=(m=o.x)!=null?m:0,n=(N=(c=o._originalNode)==null?void 0:c.width)!=null?N:100,s=(u=(b=o._originalNode)==null?void 0:b.height)!=null?u:50,x=r+h;t.push({id:String(o.id),x:i-d,y:x,width:n,height:s,originalNode:o._originalNode}),o.children&&te(o.children,t,i,r)})}function ie(e,t,i,r){e.forEach(o=>{var p,m,c,N,b,u;let d=(p=o.y)!=null?p:0,h=(m=o.x)!=null?m:0,n=(N=(c=o._originalNode)==null?void 0:c.width)!=null?N:100,s=(u=(b=o._originalNode)==null?void 0:b.height)!=null?u:50,x=r+h;t.push({id:String(o.id),x:i+d,y:x,width:n,height:s,originalNode:o._originalNode}),o.children&&ie(o.children,t,i,r)})}function O(e,t,i){let r=Math.min(e.width,e.height)/2,o=i.x-t.x,d=i.y-t.y,h=Math.sqrt(o*o+d*d);if(h===0)return t;let n=o/h,s=d/h;return{x:e.x-n*r,y:e.y-s*r}}function j(e,t,i){let r=e.x,o=e.y;if(!e.width||!e.height)return{x:t.x,y:t.y};let d=Math.abs(r-i.x),h=(e==null?void 0:e.width)/2,n=i.x<t.x?h-d:h+d,s=e.height/2,x=Math.abs(t.y-i.y),p=Math.abs(t.x-i.x);if(Math.abs(o-t.y)*h>Math.abs(r-t.x)*s){let m=i.y<t.y?t.y-s-o:o-s-t.y;n=p*m/x;let c={x:i.x<t.x?i.x+n:i.x-p+n,y:i.y<t.y?i.y+x-m:i.y-x+m};return n===0&&(c.x=t.x,c.y=t.y),p===0&&(c.x=t.x),x===0&&(c.y=t.y),c}else{i.x<t.x?n=t.x-h-r:n=r-h-t.x;let m=x*n/p,c=i.x<t.x?i.x+p-n:i.x-p+n,N=i.y<t.y?i.y+m:i.y-m;return n===0&&(c=t.x,N=t.y),p===0&&(c=t.x),x===0&&(N=t.y),{x:c,y:N}}}function Le(e,t,i){let r=new Map;return t.forEach(o=>{r.set(o.id,o)}),e.map(o=>{var f,y,L,M,v,a,w,E,B,I,P,D,H,z,_,S,$,de,he,ne,se,ae;let d=r.get((f=o.start)!=null?f:""),h=r.get((y=o.end)!=null?y:"");if(!d||!h)return{id:o.id,source:(L=o.start)!=null?L:"",target:(M=o.end)!=null?M:"",startX:0,startY:0,midX:0,midY:0,endX:0,endY:0,points:[{x:0,y:0}],sourceSection:void 0,targetSection:void 0,sourceWidth:void 0,sourceHeight:void 0,targetWidth:void 0,targetHeight:void 0};let n={x:d.x,y:d.y},s={x:h.x,y:h.y},x=["circle","cloud","bang"].includes((a=(v=d.originalNode)==null?void 0:v.shape)!=null?a:""),p=["circle","cloud","bang"].includes((E=(w=h.originalNode)==null?void 0:w.shape)!=null?E:""),m=x?O({x:d.x,y:d.y,width:(B=d.width)!=null?B:100,height:(I=d.height)!=null?I:100},s,n):j(d,n,s),c=p?O({x:h.x,y:h.y,width:(P=h.width)!=null?P:100,height:(D=h.height)!=null?D:100},n,s):j(h,s,n),N=(m.x+c.x)/2,b=(m.y+c.y)/2,u=[m];d.section==="left"?u.push({x:d.x-((H=d.width)!=null?H:0)/2-i,y:d.y}):d.section==="right"&&u.push({x:d.x+((z=d.width)!=null?z:0)/2+i,y:d.y}),h.section==="left"?u.push({x:h.x+((_=h.width)!=null?_:0)/2+i,y:h.y}):h.section==="right"&&u.push({x:h.x-((S=h.width)!=null?S:0)/2-i,y:h.y}),u.push(c);let T=u.length>1?u[1]:s;m=x?O({x:d.x,y:d.y,width:($=d.width)!=null?$:100,height:(de=d.height)!=null?de:100},T,n):j(d,T,n),u[0]=m;let l=u.length>1?u[u.length-2]:n;return c=p?O({x:h.x,y:h.y,width:(he=h.width)!=null?he:100,height:(ne=h.height)!=null?ne:100},l,s):j(h,l,s),u[u.length-1]=c,{id:o.id,source:(se=o.start)!=null?se:"",target:(ae=o.end)!=null?ae:"",startX:m.x,startY:m.y,midX:N,midY:b,endX:c.x,endY:c.y,points:u,sourceSection:d==null?void 0:d.section,targetSection:h==null?void 0:h.section,sourceWidth:d==null?void 0:d.width,sourceHeight:d==null?void 0:d.height,targetWidth:h==null?void 0:h.width,targetHeight:h==null?void 0:h.height}})}function re(e){if(!e)throw new Error("Layout data is required");if(!e.config)throw new Error("Configuration is required in layout data");if(!Array.isArray(e.nodes))throw new Error("Nodes array is required in layout data");if(!Array.isArray(e.edges))throw new Error("Edges array is required in layout data");return!0}var We,g,W,Xe,X,qe,q,ke,k,De,K,oe=ue(()=>{We=Object.defineProperty,g=(e,t)=>We(e,"name",{value:t,configurable:!0}),Xe=(W=class{constructor(t,i,r,o){this.w=t,this.h=i,this.y=r,this.c=o,this.cs=o.length,this.x=0,this.prelim=0,this.mod=0,this.shift=0,this.change=0,this.tl=null,this.tr=null,this.el=null,this.er=null,this.msel=0,this.mser=0}},g(W,"Tree"),W);g(Q,"setExtremes");g(R,"bottom");qe=(X=class{constructor(t,i,r){this.lowY=t,this.index=i,this.next=r}},g(X,"IYL"),X);g(U,"updateIYL");g(me,"distributeExtra");g(xe,"moveSubtree");g(fe,"nextLeftContour");g(we,"nextRightContour");g(pe,"setLeftThread");g(be,"setRightThread");g(Ne,"seperate");g(ve,"positionRoot");g(G,"firstWalk");g(Me,"addChildSpacing");g(Z,"secondWalk");g(Te,"layout");ke=(q=class{constructor(t,i){this.gap=t,this.bottomPadding=i}addBoundingBox(t,i){return{width:t+this.gap,height:i+this.bottomPadding}}removeBoundingBox(t,i){return{x:t+this.gap/2,y:i}}},g(q,"BoundingBox"),q),De=(k=class{constructor(t){this.bb=t}layout(t){let i=this.convert(t);Te(i);let{boundingBox:r,result:o}=this.assignLayout(i,t);return{result:o,boundingBox:r}}convert(t,i=0){if(t===null)return null;let{width:r,height:o}=this.bb.addBoundingBox(t.width,t.height),d=[];if(t.children&&t.children.length)for(let h=0;h<t.children.length;h++)d[h]=this.convert(t.children[h],i+o);return new Xe(r,o,i,d)}assignCoordinates(t,i){let{x:r,y:o}=this.bb.removeBoundingBox(t.x,t.y);i.x=r,i.y=o;for(let d=0;d<t.c.length;d++)this.assignCoordinates(t.c[d],i.children[d])}getSize(t,i=null){let{x:r,y:o,width:d,height:h}=t;if(i===null&&(i={left:r,right:r+d,top:o,bottom:o+h}),i.left=Math.min(i.left,r),i.right=Math.max(i.right,r+d),i.top=Math.min(i.top,o),i.bottom=Math.max(i.bottom,o+h),t.children)for(let n of t.children)this.getSize(n,i);return i}assignLayout(t,i,r=null){let{x:o,y:d}=this.bb.removeBoundingBox(t.x,t.y);i.x=o,i.y=d;let{width:h,height:n}=i;r===null&&(r={left:o,right:o+h,top:d,bottom:d+n}),r.left=Math.min(r.left,o),r.right=Math.max(r.right,o+h),r.top=Math.min(r.top,d),r.bottom=Math.max(r.bottom,d+n);for(let s=0;s<t.c.length;s++)this.assignLayout(t.c[s],i.children[s],r);return{result:i,boundingBox:r}}},g(k,"Layout"),k);g(J,"executeTidyTreeLayout");g(Ee,"convertToDualTreeFormat");g(V,"buildSubTree");g(ee,"convertNodeToTidyTreeTransposed");g(Be,"combineAndPositionTrees");g(te,"positionLeftTreeBidirectional");g(ie,"positionRightTreeBidirectional");g(O,"computeCircleEdgeIntersection");g(j,"intersection");g(Le,"calculateEdgePositions");g(re,"validateLayoutData");K=g((p,m,c,N)=>C(null,[p,m,c,N],function*(e,t,{insertCluster:i,insertEdge:r,insertEdgeLabel:o,insertMarkers:d,insertNode:h,log:n,positionEdgeLabel:s},{algorithm:x}){let b={},u={},T=t.select("g");d(T,e.markers,e.type,e.diagramId);let l=T.insert("g").attr("class","subgraphs"),f=T.insert("g").attr("class","edgePaths"),y=T.insert("g").attr("class","edgeLabels"),L=T.insert("g").attr("class","nodes");n.debug("Inserting nodes into DOM for dimension calculation"),yield Promise.all(e.nodes.map(a=>C(null,null,function*(){if(a.isGroup){let w=Y(A({},a),{id:a.id,width:a.width,height:a.height});u[a.id]=w,b[a.id]=w,yield i(l,a)}else{let w=Y(A({},a),{id:a.id,width:a.width,height:a.height});b[a.id]=w;let E=yield h(L,a,{config:e.config,dir:e.direction||"TB"}),B=E.node().getBBox();w.width=B.width,w.height=B.height,w.domId=E,n.debug(`Node ${a.id} dimensions: ${B.width}x${B.height}`)}}))),n.debug("Running bidirectional tidy-tree layout algorithm");let M=Y(A({},e),{nodes:e.nodes.map(a=>{var E,B,I,P;let w=b[a.id];return Y(A({},a),{width:(B=(E=w.width)!=null?E:a.width)!=null?B:100,height:(P=(I=w.height)!=null?I:a.height)!=null?P:50})})}),v=yield J(M);n.debug("Positioning nodes based on bidirectional layout results"),v.nodes.forEach(a=>{let w=b[a.id];w!=null&&w.domId&&(w.domId.attr("transform",`translate(${a.x}, ${a.y})`),w.x=a.x,w.y=a.y,n.debug(`Positioned node ${w.id} at (${a.x}, ${a.y})`))}),n.debug("Inserting and positioning edges"),yield Promise.all(e.edges.map(a=>C(null,null,function*(){var B,I,P,D,H,z;yield o(y,a);let w=b[(B=a.start)!=null?B:""],E=b[(I=a.end)!=null?I:""];if(w&&E){let _=v.edges.find(S=>S.id===a.id);if(_){n.debug("APA01 positionedEdge",_);let S=Y(A({},a),{points:_.points}),$=r(f,S,u,e.type,w,E,e.diagramId);s(S,$)}else{let S=Y(A({},a),{points:[{x:(P=w.x)!=null?P:0,y:(D=w.y)!=null?D:0},{x:(H=E.x)!=null?H:0,y:(z=E.y)!=null?z:0}]}),$=r(f,S,u,e.type,w,E,e.diagramId);s(S,$)}}}))),n.debug("Bidirectional tidy-tree rendering completed")}),"render")});var Se={};ye(Se,{render:()=>K});var Ie=ue(()=>{oe()});var je={};ye(je,{default:()=>Oe,executeTidyTreeLayout:()=>J,render:()=>K,validateLayoutData:()=>re});oe();var He=g(()=>C(null,null,function*(){return yield Promise.resolve().then(()=>(Ie(),Se))}),"loader"),ze=[{name:"tidy-tree",loader:He,algorithm:"tidy-tree"}],Oe=ze;return Re(je);})();
+mermaidTidyTree=mermaidTidyTree.default;