@html-graph/html-graph 2.2.2 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/html-graph.d.ts +61 -4
- package/dist/html-graph.js +1126 -1057
- package/dist/html-graph.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/html-graph.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(E,S){typeof exports=="object"&&typeof module<"u"?S(exports):typeof define=="function"&&define.amd?define(["exports"],S):(E=typeof globalThis<"u"?globalThis:E||self,S(E.HtmlGraph={}))})(this,function(E){"use strict";var Xt=Object.defineProperty;var Bt=(E,S,b)=>S in E?Xt(E,S,{enumerable:!0,configurable:!0,writable:!0,value:b}):E[S]=b;var i=(E,S,b)=>Bt(E,typeof S!="symbol"?S+"":S,b);class S{constructor(t){this.graphStore=t}getNode(t){const o=this.graphStore.getNode(t);return o===void 0?null:{element:o.element,x:o.x,y:o.y,centerFn:o.centerFn,priority:o.priority}}getAllNodeIds(){return this.graphStore.getAllNodeIds()}getPort(t){const o=this.graphStore.getPort(t);return o===void 0?null:{element:o.element,direction:o.direction,nodeId:o.nodeId}}getAllPortIds(){return this.graphStore.getAllPortIds()}getNodePortIds(t){return this.graphStore.getNodePortIds(t)??null}getPortNodeId(t){var o;return((o=this.graphStore.getPort(t))==null?void 0:o.nodeId)??null}getAllEdgeIds(){return this.graphStore.getAllEdgeIds()}getEdge(t){const o=this.graphStore.getEdge(t);return o===void 0?null:{from:o.from,to:o.to,priority:o.priority}}getPortIncomingEdgeIds(t){return this.graphStore.getPort(t)===void 0?null:this.graphStore.getPortIncomingEdgeIds(t)}getPortOutcomingEdgeIds(t){return this.graphStore.getPort(t)===void 0?null:this.graphStore.getPortOutcomingEdgeIds(t)}getPortCycleEdgeIds(t){return this.graphStore.getPort(t)===void 0?null:this.graphStore.getPortCycleEdgeIds(t)}getPortAdjacentEdgeIds(t){return this.graphStore.getPort(t)===void 0?null:this.graphStore.getPortAdjacentEdgeIds(t)}getNodeIncomingEdgeIds(t){return this.graphStore.getNode(t)===void 0?null:this.graphStore.getNodeIncomingEdgeIds(t)}getNodeOutcomingEdgeIds(t){return this.graphStore.getNode(t)===void 0?null:this.graphStore.getNodeOutcomingEdgeIds(t)}getNodeCycleEdgeIds(t){return this.graphStore.getNode(t)===void 0?null:this.graphStore.getNodeCycleEdgeIds(t)}getNodeAdjacentEdgeIds(t){return this.graphStore.getNode(t)===void 0?null:this.graphStore.getNodeAdjacentEdgeIds(t)}}class b{constructor(t){this.transformer=t}getViewportMatrix(){return{...this.transformer.getViewportMatrix()}}getContentMatrix(){return{...this.transformer.getContentMatrix()}}}class ht{constructor(t,o,r){i(this,"viewport");i(this,"graph");this.graphStore=t,this.viewportStore=o,this.htmlView=r,this.graph=new S(this.graphStore),this.viewport=new b(this.viewportStore)}attach(t){this.htmlView.attach(t)}detach(){this.htmlView.detach()}addNode(t){this.graphStore.addNode(t),this.htmlView.attachNode(t.id)}updateNode(t,o){const r=this.graphStore.getNode(t);r.x=(o==null?void 0:o.x)??r.x,r.y=(o==null?void 0:o.y)??r.y,r.centerFn=o.centerFn??r.centerFn,this.htmlView.updateNodeCoordinates(t),this.graphStore.getNodeAdjacentEdgeIds(t).forEach(h=>{this.htmlView.renderEdge(h)}),o.priority!==void 0&&(r.priority=o.priority,this.htmlView.updateNodePriority(t))}removeNode(t){this.graphStore.getNodePortIds(t).forEach(o=>{this.unmarkPort(o)}),this.graphStore.removeNode(t),this.htmlView.detachNode(t)}markPort(t){this.graphStore.addPort(t)}updatePort(t,o){const r=this.graphStore.getPort(t);r.direction=o.direction??r.direction,this.graphStore.getPortAdjacentEdgeIds(t).forEach(h=>{this.htmlView.renderEdge(h)})}unmarkPort(t){this.graphStore.getPortAdjacentEdgeIds(t).forEach(o=>{this.removeEdge(o)}),this.graphStore.removePort(t)}addEdge(t){this.graphStore.addEdge(t),this.htmlView.attachEdge(t.id)}updateEdge(t,o){const r=this.graphStore.getEdge(t);o.shape!==void 0&&(r.shape=o.shape,this.htmlView.updateEdgeShape(t)),o.from!==void 0&&this.graphStore.updateEdgeFrom(t,o.from),o.to!==void 0&&this.graphStore.updateEdgeTo(t,o.to),this.htmlView.renderEdge(t),o.priority!==void 0&&(r.priority=o.priority,this.htmlView.updateEdgePriority(t))}removeEdge(t){this.graphStore.removeEdge(t),this.htmlView.detachEdge(t)}patchViewportMatrix(t){this.viewportStore.patchViewportMatrix(t)}patchContentMatrix(t){this.viewportStore.patchContentMatrix(t)}clear(){this.htmlView.clear(),this.graphStore.clear()}destroy(){this.clear(),this.htmlView.destroy()}}const W=(e,t,o)=>{const{x:r,y:n,width:h,height:s}=e.getBoundingClientRect();return t>=r&&t<=r+h&&o>=n&&o<=n+s},I=(e,t,o)=>t>=0&&t<=e.innerWidth&&o>=0&&o<=e.innerHeight,F=(e,t)=>{t!==null?e.style.cursor=t:e.style.removeProperty("cursor")},ct=e=>{var g,y,x,v,p,T;const t=((g=e==null?void 0:e.events)==null?void 0:g.onNodeDrag)??(()=>{}),o=((y=e==null?void 0:e.events)==null?void 0:y.onBeforeNodeDrag)??(()=>!0),r=((x=e==null?void 0:e.events)==null?void 0:x.onNodeDragFinished)??(()=>{}),n=(e==null?void 0:e.moveOnTop)===!1,h=(v=e==null?void 0:e.mouse)==null?void 0:v.dragCursor,s=h!==void 0?h:"grab",c=(p=e==null?void 0:e.mouse)==null?void 0:p.mouseDownEventVerifier,d=c!==void 0?c:M=>M.button===0,a=(T=e==null?void 0:e.mouse)==null?void 0:T.mouseUpEventVerifier;return{freezePriority:n,dragCursor:s,mouseDownEventVerifier:d,mouseUpEventVerifier:a!==void 0?a:M=>M.button===0,onNodeDrag:t,onBeforeNodeDrag:o,onNodeDragFinished:r}};class dt{constructor(t,o){i(this,"graph");i(this,"viewport");i(this,"maxNodePriority",0);i(this,"nodes",new Map);i(this,"grabbedNodeId",null);i(this,"element",null);i(this,"onWindowMouseMove",t=>{if(this.element!==null&&(!W(this.element,t.clientX,t.clientY)||!I(this.window,t.clientX,t.clientY))){this.cancelMouseDrag();return}this.grabbedNodeId!==null&&this.dragNode(this.grabbedNodeId,t.movementX,t.movementY)});i(this,"onWindowMouseUp",t=>{this.options.mouseUpEventVerifier(t)&&this.cancelMouseDrag()});i(this,"onWindowTouchMove",t=>{if(t.touches.length!==1)return;const o=t.touches[0];if(this.element!==null&&(!W(this.element,o.clientX,o.clientY)||!I(this.window,o.clientX,o.clientY))){this.cancelTouchDrag();return}if(this.grabbedNodeId!==null&&this.previousTouchCoords!==null){const r=o.clientX-this.previousTouchCoords.x,n=o.clientY-this.previousTouchCoords.y;this.dragNode(this.grabbedNodeId,r,n),this.previousTouchCoords={x:t.touches[0].clientX,y:t.touches[0].clientY}}});i(this,"onWindowTouchFinish",()=>{this.previousTouchCoords=null,this.cancelTouchDrag()});i(this,"previousTouchCoords",null);i(this,"window",window);i(this,"options");this.canvas=t,this.viewport=this.canvas.viewport,this.graph=this.canvas.graph,this.options=ct(o??{})}attach(t){this.detach(),this.element=t,this.canvas.attach(this.element)}detach(){this.canvas.detach(),this.element!==null&&(this.element=null)}addNode(t){this.canvas.addNode(t),this.updateMaxNodePriority(t.id);const o=n=>{if(this.element===null||!this.options.mouseDownEventVerifier(n))return;const h=this.graph.getNode(t.id);this.options.onBeforeNodeDrag({nodeId:t.id,element:t.element,x:h.x,y:h.y})&&(n.stopImmediatePropagation(),this.grabbedNodeId=t.id,F(this.element,this.options.dragCursor),this.moveNodeOnTop(t.id),this.window.addEventListener("mouseup",this.onWindowMouseUp),this.window.addEventListener("mousemove",this.onWindowMouseMove))},r=n=>{if(n.touches.length!==1)return;n.stopImmediatePropagation(),this.previousTouchCoords={x:n.touches[0].clientX,y:n.touches[0].clientY};const h=this.graph.getNode(t.id);this.options.onBeforeNodeDrag({nodeId:t.id,element:t.element,x:h.x,y:h.y})&&(this.grabbedNodeId=t.id,this.moveNodeOnTop(t.id),this.window.addEventListener("touchmove",this.onWindowTouchMove),this.window.addEventListener("touchend",this.onWindowTouchFinish),this.window.addEventListener("touchcancel",this.onWindowTouchFinish))};this.nodes.set(t.id,{element:t.element,onMouseDown:o,onTouchStart:r}),t.element.addEventListener("mousedown",o),t.element.addEventListener("touchstart",r)}updateNode(t,o){this.canvas.updateNode(t,o),this.updateMaxNodePriority(t)}removeNode(t){const o=this.nodes.get(t);o!==void 0&&(o.element.removeEventListener("mousedown",o.onMouseDown),o.element.removeEventListener("touchstart",o.onTouchStart)),this.nodes.delete(t),this.canvas.removeNode(t)}markPort(t){this.canvas.markPort(t)}updatePort(t,o){this.canvas.updatePort(t,o)}unmarkPort(t){this.canvas.unmarkPort(t)}addEdge(t){this.canvas.addEdge(t)}updateEdge(t,o){this.canvas.updateEdge(t,o)}removeEdge(t){this.canvas.removeEdge(t)}patchViewportMatrix(t){this.canvas.patchViewportMatrix(t)}patchContentMatrix(t){this.canvas.patchContentMatrix(t)}clear(){this.canvas.clear(),this.nodes.forEach(t=>{t.element.removeEventListener("mousedown",t.onMouseDown),t.element.removeEventListener("touchstart",t.onTouchStart)}),this.nodes.clear(),this.maxNodePriority=0}destroy(){this.detach(),this.clear(),this.removeMouseDragListeners(),this.removeTouchDragListeners(),this.canvas.destroy()}dragNode(t,o,r){const n=this.graph.getNode(t);if(n===null)return;const h=this.canvas.viewport.getContentMatrix(),s=h.scale*n.x+h.x,c=h.scale*n.y+h.y,d=s+o,a=c+r,l=this.canvas.viewport.getViewportMatrix(),g=l.scale*d+l.x,y=l.scale*a+l.y;this.canvas.updateNode(t,{x:g,y}),this.options.onNodeDrag({nodeId:t,element:n.element,x:g,y})}updateMaxNodePriority(t){const o=this.graph.getNode(t).priority;this.maxNodePriority=Math.max(this.maxNodePriority,o)}moveNodeOnTop(t){if(this.options.freezePriority)return;this.maxNodePriority+=2,this.updateNode(t,{priority:this.maxNodePriority});const o=this.maxNodePriority-1;this.graph.getNodeAdjacentEdgeIds(t).forEach(n=>{this.updateEdge(n,{priority:o})})}cancelMouseDrag(){const t=this.graph.getNode(this.grabbedNodeId);t!==null&&this.options.onNodeDragFinished({nodeId:this.grabbedNodeId,element:t.element,x:t.x,y:t.y}),this.grabbedNodeId=null,this.element!==null&&F(this.element,null),this.removeMouseDragListeners()}removeMouseDragListeners(){this.window.removeEventListener("mouseup",this.onWindowMouseUp),this.window.removeEventListener("mousemove",this.onWindowMouseMove)}cancelTouchDrag(){this.previousTouchCoords=null;const t=this.graph.getNode(this.grabbedNodeId);t!==null&&this.options.onNodeDragFinished({nodeId:this.grabbedNodeId,element:t.element,x:t.x,y:t.y}),this.grabbedNodeId=null,this.removeTouchDragListeners()}removeTouchDragListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}}const at=e=>{const t=e.minX!==null?e.minX:-1/0,o=e.maxX!==null?e.maxX:1/0,r=e.minY!==null?e.minY:-1/0,n=e.maxY!==null?e.maxY:1/0;return h=>{let s=h.nextTransform.x,c=h.nextTransform.y;s<t&&s<h.prevTransform.x&&(s=Math.min(h.prevTransform.x,t));const d=h.canvasWidth*h.prevTransform.scale,a=o-d;s>a&&s>h.prevTransform.x&&(s=Math.max(h.prevTransform.x,a)),c<r&&c<h.prevTransform.y&&(c=Math.min(h.prevTransform.y,r));const l=h.canvasHeight*h.prevTransform.scale,g=n-l;return c>g&&c>h.prevTransform.y&&(c=Math.max(h.prevTransform.y,g)),{scale:h.nextTransform.scale,x:s,y:c}}},lt=e=>{const t=e.maxContentScale,o=e.minContentScale,r=t!==null?1/t:0,n=o!==null?1/o:1/0;return h=>{const s=h.prevTransform,c=h.nextTransform;let d=c.scale,a=c.x,l=c.y;if(c.scale>n&&c.scale>s.scale){d=Math.max(s.scale,n),a=s.x,l=s.y;const g=(d-s.scale)/(c.scale-s.scale);a=s.x+(c.x-s.x)*g,l=s.y+(c.y-s.y)*g}if(c.scale<r&&c.scale<s.scale){d=Math.min(s.scale,r),a=s.x,l=s.y;const g=(d-s.scale)/(c.scale-s.scale);a=s.x+(c.x-s.x)*g,l=s.y+(c.y-s.y)*g}return{scale:d,x:a,y:l}}},gt=e=>t=>e.reduce((o,r)=>r({prevTransform:t.prevTransform,nextTransform:o,canvasWidth:t.canvasWidth,canvasHeight:t.canvasHeight}),t.nextTransform),_=e=>{if(typeof e=="function")return e;switch(e.type){case"scale-limit":return lt({minContentScale:e.minContentScale??0,maxContentScale:e.maxContentScale??1/0});case"shift-limit":return at({minX:e.minX??-1/0,maxX:e.maxX??1/0,minY:e.minY??-1/0,maxY:e.maxY??1/0})}},ut=e=>{var v,p,T,M,K,m,C,$,rt,it,st,nt;const t=(v=e==null?void 0:e.scale)==null?void 0:v.mouseWheelSensitivity,o=t!==void 0?t:1.2,r=e==null?void 0:e.transformPreprocessor;let n;r!==void 0?Array.isArray(r)?n=gt(r.map(D=>_(D))):n=_(r):n=D=>D.nextTransform;const h=((p=e==null?void 0:e.shift)==null?void 0:p.cursor)!==void 0?e.shift.cursor:"grab",s=((T=e==null?void 0:e.events)==null?void 0:T.onBeforeTransformChange)??(()=>{}),c=((M=e==null?void 0:e.events)==null?void 0:M.onTransformChange)??(()=>{}),d=(K=e==null?void 0:e.shift)==null?void 0:K.mouseDownEventVerifier,a=d!==void 0?d:D=>D.button===0,l=(m=e==null?void 0:e.shift)==null?void 0:m.mouseUpEventVerifier,g=l!==void 0?l:D=>D.button===0,y=(C=e==null?void 0:e.scale)==null?void 0:C.mouseWheelEventVerifier,x=y!==void 0?y:()=>!0;return{wheelSensitivity:o,onTransformStarted:(($=e==null?void 0:e.events)==null?void 0:$.onTransformStarted)??(()=>{}),onTransformFinished:((rt=e==null?void 0:e.events)==null?void 0:rt.onTransformFinished)??(()=>{}),onBeforeTransformChange:s,onTransformChange:c,transformPreprocessor:n,shiftCursor:h,mouseDownEventVerifier:a,mouseUpEventVerifier:g,mouseWheelEventVerifier:x,scaleWheelFinishTimeout:((it=e==null?void 0:e.scale)==null?void 0:it.wheelFinishTimeout)??500,onResizeTransformStarted:((st=e==null?void 0:e.events)==null?void 0:st.onResizeTransformStarted)??(()=>{}),onResizeTransformFinished:((nt=e==null?void 0:e.events)==null?void 0:nt.onResizeTransformFinished)??(()=>{})}},R=e=>{const t=[],o=e.touches.length;for(let c=0;c<o;c++)t.push([e.touches[c].clientX,e.touches[c].clientY]);const r=t.reduce((c,d)=>[c[0]+d[0],c[1]+d[1]],[0,0]),n=[r[0]/o,r[1]/o],s=t.map(c=>[c[0]-n[0],c[1]-n[1]]).reduce((c,d)=>c+Math.sqrt(d[0]*d[0]+d[1]*d[1]),0);return{x:n[0],y:n[1],scale:s/o,touchesCnt:o,touches:t}},wt=(e,t,o)=>({scale:e.scale,x:e.x+e.scale*t,y:e.y+e.scale*o}),yt=(e,t,o,r)=>({scale:e.scale*t,x:e.scale*(1-t)*o+e.x,y:e.scale*(1-t)*r+e.y});class J{constructor(t,o){i(this,"graph");i(this,"viewport");i(this,"element",null);i(this,"prevTouches",null);i(this,"window",window);i(this,"wheelFinishTimer",null);i(this,"onMouseDown",t=>{this.element===null||!this.options.mouseDownEventVerifier(t)||(F(this.element,this.options.shiftCursor),this.window.addEventListener("mousemove",this.onWindowMouseMove),this.window.addEventListener("mouseup",this.onWindowMouseUp),this.options.onTransformStarted())});i(this,"onWindowMouseMove",t=>{if(this.element===null||!W(this.element,t.clientX,t.clientY)||!I(this.window,t.clientX,t.clientY)){this.stopMouseDrag();return}const o=-t.movementX,r=-t.movementY;this.moveViewport(this.element,o,r)});i(this,"onWindowMouseUp",t=>{this.element===null||!this.options.mouseUpEventVerifier(t)||this.stopMouseDrag()});i(this,"onWheelScroll",t=>{if(!this.options.mouseWheelEventVerifier(t))return;t.preventDefault();const{left:o,top:r}=this.element.getBoundingClientRect(),n=t.clientX-o,h=t.clientY-r,c=1/(t.deltaY<0?this.options.wheelSensitivity:1/this.options.wheelSensitivity);this.wheelFinishTimer===null&&this.options.onTransformStarted(),this.scaleViewport(this.element,c,n,h),this.wheelFinishTimer!==null&&clearTimeout(this.wheelFinishTimer),this.wheelFinishTimer=setTimeout(()=>{this.options.onTransformFinished(),this.wheelFinishTimer=null},this.options.scaleWheelFinishTimeout)});i(this,"onTouchStart",t=>{if(this.prevTouches!==null){this.prevTouches=R(t);return}this.prevTouches=R(t),this.window.addEventListener("touchmove",this.onWindowTouchMove),this.window.addEventListener("touchend",this.onWindowTouchFinish),this.window.addEventListener("touchcancel",this.onWindowTouchFinish),this.options.onTransformStarted()});i(this,"onWindowTouchMove",t=>{const o=this.element;if(o===null)return;const r=R(t);if(!r.touches.every(h=>W(o,h[0],h[1])&&I(this.window,h[0],h[1]))){this.stopTouchDrag();return}if((r.touchesCnt===1||r.touchesCnt===2)&&this.moveViewport(o,-(r.x-this.prevTouches.x),-(r.y-this.prevTouches.y)),r.touchesCnt===2){const{left:h,top:s}=o.getBoundingClientRect(),c=this.prevTouches.x-h,d=this.prevTouches.y-s,l=1/(r.scale/this.prevTouches.scale);this.scaleViewport(o,l,c,d)}this.prevTouches=r});i(this,"onWindowTouchFinish",t=>{t.touches.length>0?this.prevTouches=R(t):this.stopTouchDrag()});i(this,"observer",new ResizeObserver(()=>{const t=this.canvas.viewport.getViewportMatrix(),{width:o,height:r}=this.element.getBoundingClientRect(),n=this.options.transformPreprocessor({prevTransform:t,nextTransform:t,canvasWidth:o,canvasHeight:r});this.options.onResizeTransformStarted(),this.canvas.patchViewportMatrix(n),this.options.onResizeTransformFinished()}));i(this,"options");this.canvas=t,this.options=ut(o),this.viewport=this.canvas.viewport,this.graph=this.canvas.graph}attach(t){this.detach(),this.element=t,this.observer.observe(this.element),this.element.addEventListener("mousedown",this.onMouseDown),this.element.addEventListener("wheel",this.onWheelScroll),this.element.addEventListener("touchstart",this.onTouchStart),this.canvas.attach(this.element)}detach(){this.canvas.detach(),this.element!==null&&(this.observer.unobserve(this.element),this.element.removeEventListener("mousedown",this.onMouseDown),this.element.removeEventListener("wheel",this.onWheelScroll),this.element.removeEventListener("touchstart",this.onTouchStart),this.element=null)}addNode(t){this.canvas.addNode(t)}updateNode(t,o){this.canvas.updateNode(t,o)}removeNode(t){this.canvas.removeNode(t)}markPort(t){this.canvas.markPort(t)}updatePort(t,o){this.canvas.updatePort(t,o)}unmarkPort(t){this.canvas.unmarkPort(t)}addEdge(t){this.canvas.addEdge(t)}updateEdge(t,o){this.canvas.updateEdge(t,o)}removeEdge(t){this.canvas.removeEdge(t)}patchViewportMatrix(t){this.canvas.patchViewportMatrix(t)}patchContentMatrix(t){this.canvas.patchContentMatrix(t)}clear(){this.canvas.clear()}destroy(){this.detach(),this.removeMouseDragListeners(),this.removeTouchDragListeners(),this.canvas.destroy()}moveViewport(t,o,r){const n=this.viewport.getViewportMatrix(),h=wt(n,o,r),{width:s,height:c}=t.getBoundingClientRect(),d=this.options.transformPreprocessor({prevTransform:n,nextTransform:h,canvasWidth:s,canvasHeight:c});this.performTransform(d)}scaleViewport(t,o,r,n){const h=this.canvas.viewport.getViewportMatrix(),s=yt(h,o,r,n),{width:c,height:d}=t.getBoundingClientRect(),a=this.options.transformPreprocessor({prevTransform:h,nextTransform:s,canvasWidth:c,canvasHeight:d});this.performTransform(a)}stopMouseDrag(){this.element!==null&&F(this.element,null),this.removeMouseDragListeners(),this.options.onTransformFinished()}removeMouseDragListeners(){this.window.removeEventListener("mousemove",this.onWindowMouseMove),this.window.removeEventListener("mouseup",this.onWindowMouseUp)}stopTouchDrag(){this.prevTouches=null,this.removeTouchDragListeners(),this.options.onTransformFinished()}removeTouchDragListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}performTransform(t){this.options.onBeforeTransformChange(),this.canvas.patchViewportMatrix(t),this.options.onTransformChange()}}class vt{constructor(){i(this,"keyMap",new Map);i(this,"valueMap",new Map)}set(t,o){this.keyMap.set(t,o),this.valueMap.set(o,t)}hasKey(t){return this.keyMap.has(t)}hasValue(t){return this.valueMap.has(t)}getByKey(t){return this.keyMap.get(t)}getByValue(t){return this.valueMap.get(t)}deleteByKey(t){const o=this.keyMap.get(t);o!==void 0&&this.valueMap.delete(o),this.keyMap.delete(t)}deleteByValue(t){const o=this.valueMap.get(t);o!==void 0&&this.keyMap.delete(o),this.valueMap.delete(t)}forEach(t){this.keyMap.forEach((o,r)=>{t(o,r)})}clear(){this.keyMap.clear(),this.valueMap.clear()}}class ft{constructor(t){i(this,"viewport");i(this,"graph");i(this,"nodes",new vt);i(this,"nodesResizeObserver");i(this,"window",window);this.canvas=t,this.nodesResizeObserver=new this.window.ResizeObserver(o=>{o.forEach(r=>{const n=r.target;this.handleNodeResize(n)})}),this.viewport=this.canvas.viewport,this.graph=this.canvas.graph}attach(t){this.canvas.attach(t)}detach(){this.canvas.detach()}addNode(t){this.canvas.addNode(t),this.nodes.set(t.id,t.element),this.nodesResizeObserver.observe(t.element)}updateNode(t,o){this.canvas.updateNode(t,o)}removeNode(t){this.canvas.removeNode(t);const o=this.nodes.getByKey(t);this.nodes.deleteByKey(t),this.nodesResizeObserver.unobserve(o)}markPort(t){this.canvas.markPort(t)}updatePort(t,o){this.canvas.updatePort(t,o)}unmarkPort(t){this.canvas.unmarkPort(t)}addEdge(t){this.canvas.addEdge(t)}updateEdge(t,o){this.canvas.updateEdge(t,o)}removeEdge(t){this.canvas.removeEdge(t)}patchViewportMatrix(t){this.canvas.patchViewportMatrix(t)}patchContentMatrix(t){this.canvas.patchContentMatrix(t)}clear(){this.canvas.clear(),this.nodesResizeObserver.disconnect(),this.nodes.clear()}destroy(){this.clear(),this.canvas.destroy()}handleNodeResize(t){const o=this.nodes.getByValue(t);this.canvas.updateNode(o,{}),this.graph.getNodeAdjacentEdgeIds(o).forEach(n=>{this.canvas.updateEdge(n,{})})}}class xt{constructor(t,o,r,n){i(this,"graph");i(this,"viewport");i(this,"canvas");i(this,"element",null);i(this,"canvasResizeObserver");i(this,"window",window);i(this,"nodeHorizontal");i(this,"nodeVertical");i(this,"viewportWidth",0);i(this,"viewportHeight",0);i(this,"viewportMatrix");i(this,"loadedArea",{xFrom:1/0,xTo:1/0,yFrom:1/0,yTo:1/0});i(this,"updateLoadedArea",t=>{this.loadedArea={xFrom:t.x,xTo:t.x+t.width,yFrom:t.y,yTo:t.y+t.height}});var d,a;this.trigger=o,this.virtualScrollOptions=n,this.nodeHorizontal=this.virtualScrollOptions.nodeContainingRadius.horizontal,this.nodeVertical=this.virtualScrollOptions.nodeContainingRadius.vertical,this.canvasResizeObserver=new this.window.ResizeObserver(l=>{const g=l[0];this.viewportWidth=g.contentRect.width,this.viewportHeight=g.contentRect.height,this.scheduleLoadAreaAroundViewport()});const h=((d=r==null?void 0:r.events)==null?void 0:d.onTransformFinished)??(()=>{}),s=((a=r==null?void 0:r.events)==null?void 0:a.onTransformChange)??(()=>{}),c={...r,events:{...r==null?void 0:r.events,onTransformChange:()=>{const l=this.viewportMatrix;this.viewportMatrix=this.canvas.viewport.getViewportMatrix(),l.scale!==this.viewportMatrix.scale&&this.scheduleEnsureViewportAreaLoaded(),s()},onTransformFinished:()=>{this.scheduleLoadAreaAroundViewport(),h()}}};this.canvas=new J(t,c),this.viewportMatrix=this.canvas.viewport.getViewportMatrix(),this.viewport=this.canvas.viewport,this.graph=this.canvas.graph,this.trigger.subscribe(this.updateLoadedArea)}attach(t){this.detach(),this.element=t,this.canvasResizeObserver.observe(this.element),this.canvas.attach(t)}detach(){this.element!==null&&(this.canvasResizeObserver.unobserve(this.element),this.element=null,this.viewportWidth=0,this.viewportHeight=0),this.canvas.detach()}addNode(t){this.canvas.addNode(t)}updateNode(t,o){this.canvas.updateNode(t,o)}removeNode(t){this.canvas.removeNode(t)}markPort(t){this.canvas.markPort(t)}updatePort(t,o){this.canvas.updatePort(t,o)}unmarkPort(t){this.canvas.unmarkPort(t)}addEdge(t){this.canvas.addEdge(t)}updateEdge(t,o){this.canvas.updateEdge(t,o)}removeEdge(t){this.canvas.removeEdge(t)}patchViewportMatrix(t){this.canvas.patchViewportMatrix(t),this.viewportMatrix=this.canvas.viewport.getViewportMatrix(),this.loadAreaAroundViewport()}patchContentMatrix(t){this.canvas.patchContentMatrix(t),this.viewportMatrix=this.canvas.viewport.getViewportMatrix(),this.loadAreaAroundViewport()}clear(){this.canvas.clear()}destroy(){this.trigger.unsubscribe(this.updateLoadedArea),this.canvas.destroy()}scheduleLoadAreaAroundViewport(){setTimeout(()=>{this.loadAreaAroundViewport()})}scheduleEnsureViewportAreaLoaded(){const t=this.viewportWidth*this.viewportMatrix.scale,o=this.viewportHeight*this.viewportMatrix.scale,r=this.viewportMatrix.x-this.nodeHorizontal,n=this.viewportMatrix.y-this.nodeVertical,h=this.viewportMatrix.x+t+this.nodeHorizontal,s=this.viewportMatrix.y+o+this.nodeVertical;this.loadedArea.xFrom<r&&this.loadedArea.xTo>h&&this.loadedArea.yFrom<n&&this.loadedArea.yTo>s||this.scheduleLoadAreaAroundViewport()}loadAreaAroundViewport(){const t=this.viewportWidth*this.viewportMatrix.scale,o=this.viewportHeight*this.viewportMatrix.scale,r=this.viewportMatrix.x-t-this.nodeHorizontal,n=this.viewportMatrix.y-o-this.nodeVertical,h=3*t+2*this.nodeHorizontal,s=3*o+2*this.nodeVertical;this.trigger.emit({x:r,y:n,width:h,height:s})}}const Et=()=>{const e=document.createElement("div");return e.style.width="100%",e.style.height="100%",e.style.position="relative",e.style.overflow="hidden",e},St=()=>{const e=document.createElement("div");return e.style.position="absolute",e.style.top="0",e.style.left="0",e.style.width="0",e.style.height="0",e},At=()=>{const e=document.createElement("div");return e.style.position="absolute",e.style.top="0",e.style.left="0",e.style.visibility="hidden",e};class Tt{constructor(t,o){i(this,"canvasWrapper",null);i(this,"host",Et());i(this,"container",St());i(this,"nodeIdToWrapperElementMap",new Map);i(this,"edgeIdToElementMap",new Map);i(this,"applyTransform",()=>{const t=this.viewportStore.getContentMatrix();this.container.style.transform=`matrix(${t.scale}, 0, 0, ${t.scale}, ${t.x}, ${t.y})`});this.graphStore=t,this.viewportStore=o,this.host.appendChild(this.container),this.viewportStore.onAfterUpdate.subscribe(this.applyTransform)}attach(t){this.detach(),this.canvasWrapper=t,this.canvasWrapper.appendChild(this.host),this.graphStore.getAllEdgeIds().forEach(o=>{this.renderEdge(o)})}detach(){this.canvasWrapper!==null&&(this.canvasWrapper.removeChild(this.host),this.canvasWrapper=null)}attachNode(t){const o=this.graphStore.getNode(t),r=At();r.appendChild(o.element),this.container.appendChild(r),this.nodeIdToWrapperElementMap.set(t,r),this.updateNodeCoordinates(t),this.updateNodePriority(t),r.style.visibility="visible"}detachNode(t){const o=this.nodeIdToWrapperElementMap.get(t);o.removeChild(o.firstChild),this.container.removeChild(o),this.nodeIdToWrapperElementMap.delete(t)}attachEdge(t){const o=this.graphStore.getEdge(t).shape.svg;this.edgeIdToElementMap.set(t,o),this.container.appendChild(o),this.renderEdge(t),this.updateEdgePriority(t)}detachEdge(t){const o=this.edgeIdToElementMap.get(t);this.container.removeChild(o),this.edgeIdToElementMap.delete(t)}clear(){this.edgeIdToElementMap.forEach((t,o)=>{this.detachEdge(o)}),this.nodeIdToWrapperElementMap.forEach((t,o)=>{this.detachNode(o)})}destroy(){this.viewportStore.onAfterUpdate.unsubscribe(this.applyTransform),this.clear(),this.detach(),this.host.removeChild(this.container)}updateNodeCoordinates(t){const o=this.nodeIdToWrapperElementMap.get(t),r=this.graphStore.getNode(t),{width:n,height:h}=r.element.getBoundingClientRect(),s=this.viewportStore.getViewportMatrix().scale,c=r.centerFn(n,h),d=r.x-s*c.x,a=r.y-s*c.y;o.style.transform=`translate(${d}px, ${a}px)`}updateNodePriority(t){const o=this.graphStore.getNode(t),r=this.nodeIdToWrapperElementMap.get(t);r.style.zIndex=`${o.priority}`}updateEdgeShape(t){const o=this.edgeIdToElementMap.get(t);this.container.removeChild(o);const r=this.graphStore.getEdge(t);this.edgeIdToElementMap.set(t,r.shape.svg),this.container.appendChild(r.shape.svg)}renderEdge(t){const o=this.graphStore.getEdge(t),r=this.graphStore.getPort(o.from),n=this.graphStore.getPort(o.to),h=r.element.getBoundingClientRect(),s=n.element.getBoundingClientRect(),c=this.host.getBoundingClientRect(),d=this.viewportStore.getViewportMatrix(),a={x:d.scale*(h.left-c.left)+d.x,y:d.scale*(h.top-c.top)+d.y},l={x:d.scale*(s.left-c.left)+d.x,y:d.scale*(s.top-c.top)+d.y},g={x:a.x,y:a.y,width:h.width*d.scale,height:h.height*d.scale,direction:r.direction,portId:o.from,nodeId:r.nodeId},y={x:l.x,y:l.y,width:s.width*d.scale,height:s.height*d.scale,direction:n.direction,portId:o.to,nodeId:n.nodeId};o.shape.render({from:g,to:y})}updateEdgePriority(t){const o=this.graphStore.getEdge(t);o.shape.svg.style.zIndex=`${o.priority}`}}class pt{constructor(t){i(this,"xFrom",1/0);i(this,"yFrom",1/0);i(this,"xTo",1/0);i(this,"yTo",1/0);this.graphStore=t}setRenderingBox(t){this.xFrom=t.x,this.xTo=t.x+t.width,this.yFrom=t.y,this.yTo=t.y+t.height}hasNode(t){const o=this.graphStore.getNode(t);return o.x>=this.xFrom&&o.x<=this.xTo&&o.y>=this.yFrom&&o.y<=this.yTo}hasEdge(t){const o=this.graphStore.getEdge(t),r=this.graphStore.getPort(o.from).nodeId,n=this.graphStore.getPort(o.to).nodeId,h=this.graphStore.getNode(r),s=this.graphStore.getNode(n),c=Math.min(h.x,s.x),d=Math.max(h.x,s.x),a=Math.min(h.y,s.y),l=Math.max(h.y,s.y);return c<=this.xTo&&d>=this.xFrom&&a<=this.yTo&&l>=this.yFrom}}class Mt{constructor(t,o,r){i(this,"attachedNodes",new Set);i(this,"attachedEdges",new Set);i(this,"renderingBox");i(this,"updateViewport",t=>{this.renderingBox.setRenderingBox(t);const o=new Set,r=new Set,n=new Set,h=new Set;this.graphStore.getAllNodeIds().forEach(s=>{const c=this.renderingBox.hasNode(s),d=this.attachedNodes.has(s);c&&!d?o.add(s):!c&&d&&r.add(s)}),this.graphStore.getAllEdgeIds().forEach(s=>{const c=this.renderingBox.hasEdge(s),d=this.attachedEdges.has(s),a=this.graphStore.getEdge(s),l=this.graphStore.getPort(a.from).nodeId,g=this.graphStore.getPort(a.to).nodeId;c&&(this.renderingBox.hasNode(l)||(o.add(l),r.delete(l)),this.renderingBox.hasNode(g)||(o.add(g),r.delete(g))),c&&!d?n.add(s):!c&&d&&h.add(s)}),h.forEach(s=>{this.handleDetachEdge(s)}),r.forEach(s=>{this.handleDetachNode(s)}),o.forEach(s=>{this.attachedNodes.has(s)||this.handleAttachNode(s)}),n.forEach(s=>{this.handleAttachEdge(s)})});this.htmlView=t,this.graphStore=o,this.trigger=r,this.renderingBox=new pt(this.graphStore),this.trigger.subscribe(this.updateViewport)}attach(t){this.htmlView.attach(t)}detach(){this.htmlView.detach()}attachNode(t){this.renderingBox.hasNode(t)&&this.handleAttachNode(t)}detachNode(t){this.attachedNodes.has(t)&&this.handleDetachNode(t)}attachEdge(t){this.renderingBox.hasEdge(t)&&this.attachEdgeEntities(t)}detachEdge(t){this.attachedEdges.has(t)&&this.handleDetachEdge(t)}updateNodeCoordinates(t){this.attachedNodes.has(t)?this.htmlView.updateNodeCoordinates(t):this.renderingBox.hasNode(t)&&(this.handleAttachNode(t),this.graphStore.getNodeAdjacentEdgeIds(t).forEach(o=>{this.attachEdgeEntities(o)}))}updateNodePriority(t){this.attachedNodes.has(t)&&this.htmlView.updateNodePriority(t)}updateEdgeShape(t){this.attachedEdges.has(t)&&this.htmlView.updateEdgeShape(t)}renderEdge(t){this.attachedEdges.has(t)&&this.htmlView.renderEdge(t)}updateEdgePriority(t){this.attachedEdges.has(t)&&this.htmlView.updateEdgePriority(t)}clear(){this.htmlView.clear(),this.attachedNodes.clear(),this.attachedEdges.clear()}destroy(){this.clear(),this.htmlView.destroy(),this.trigger.unsubscribe(this.updateViewport)}attachEdgeEntities(t){const o=this.graphStore.getEdge(t),r=this.graphStore.getPort(o.from).nodeId,n=this.graphStore.getPort(o.to).nodeId;this.attachedNodes.has(r)||this.handleAttachNode(r),this.attachedNodes.has(n)||this.handleAttachNode(n),this.handleAttachEdge(t)}handleAttachNode(t){this.attachedNodes.add(t),this.htmlView.attachNode(t)}handleDetachNode(t){this.htmlView.detachNode(t),this.attachedNodes.delete(t)}handleAttachEdge(t){this.attachedEdges.add(t),this.htmlView.attachEdge(t)}handleDetachEdge(t){this.htmlView.detachEdge(t),this.attachedEdges.delete(t)}}class B{constructor(){i(this,"callbacks",new Set)}subscribe(t){this.callbacks.add(t)}unsubscribe(t){this.callbacks.delete(t)}emit(t){this.callbacks.forEach(o=>{o(t)})}}const Nt=()=>{const e=new B;return[e,e]};class H{constructor(t){i(this,"counter",0);this.checkExists=t}create(t){if(t!==void 0)return t;for(;this.checkExists(this.counter);)this.counter++;return this.counter}reset(){this.counter=0}}class A extends Error{constructor(){super(...arguments);i(this,"name","HtmlGraphError")}}const Vt=(e,t)=>({x:e/2,y:t/2}),U=e=>()=>e,Q=U(0),Lt=()=>{let e=0;return()=>e++},Dt=(e,t)=>{let o=Q,r=Q;const n=Lt();return e==="incremental"&&(o=n),t==="incremental"&&(r=n),typeof e=="number"&&(o=U(e)),typeof t=="number"&&(r=U(t)),typeof e=="function"&&(o=e),typeof t=="function"&&(r=t),{nodesPriorityFn:o,edgesPriorityFn:r}},f=(e,t,o)=>({x:t.x*e.x-t.y*e.y+((1-t.x)*o.x+t.y*o.y),y:t.y*e.x+t.x*e.y+((1-t.x)*o.y-t.y*o.x)}),N=(e,t,o)=>({x:t*Math.cos(e),y:o*Math.sin(e)}),u={x:0,y:0},V=(e,t,o,r)=>{const h=[u,{x:o,y:r},{x:o,y:-r}].map(a=>f(a,e,u)).map(a=>({x:a.x+t.x,y:a.y+t.y})),s=`M ${h[0].x} ${h[0].y}`,c=`L ${h[1].x} ${h[1].y}`,d=`L ${h[2].x} ${h[2].y}`;return`${s} ${c} ${d}`},P=(e,t)=>{const o=[];if(e.length>0&&o.push(`M ${e[0].x} ${e[0].y}`),e.length===2&&o.push(`L ${e[1].x} ${e[1].y}`),e.length>2){const r=e.length-1;let n=0,h=0,s=0;e.forEach((c,d)=>{let a=0,l=0,g=0;const y=d>0,x=d<r,v=y&&x;if(y&&(a=-n,l=-h,g=s),x){const $=e[d+1];n=$.x-c.x,h=$.y-c.y,s=Math.sqrt(n*n+h*h)}const T=s!==0?Math.min((v?t:0)/s,d<r-1?.5:1):0,M=v?{x:c.x+n*T,y:c.y+h*T}:c,m=g!==0?Math.min((v?t:0)/g,d>1?.5:1):0,C=v?{x:c.x+a*m,y:c.y+l*m}:c;d>0&&o.push(`L ${C.x} ${C.y}`),v&&o.push(`C ${c.x} ${c.y} ${c.x} ${c.y} ${M.x} ${M.y}`)})}return o.join(" ")},k=()=>{const e=document.createElementNS("http://www.w3.org/2000/svg","svg");return e.style.pointerEvents="none",e.style.position="absolute",e.style.top="0",e.style.left="0",e.style.overflow="visible",e},z=()=>{const e=document.createElementNS("http://www.w3.org/2000/svg","g");return e.style.transformOrigin="50% 50%",e},Y=(e,t)=>{const o=document.createElementNS("http://www.w3.org/2000/svg","path");return o.setAttribute("stroke",e),o.setAttribute("stroke-width",`${t}`),o.setAttribute("fill","none"),o},L=e=>{const t=document.createElementNS("http://www.w3.org/2000/svg","path");return t.setAttribute("fill",e),t},X=(e,t)=>{const o={x:e.x+e.width/2,y:e.y+e.height/2},r={x:t.x+t.width/2,y:t.y+t.height/2},n=Math.min(o.x,r.x),h=Math.min(o.y,r.y),s=Math.abs(r.x-o.x),c=Math.abs(r.y-o.y),d=o.x<=r.x?1:-1,a=o.y<=r.y?1:-1;return{x:n,y:h,width:s,height:c,flipX:d,flipY:a}},bt=e=>{const t=f({x:e.arrowLength,y:u.y},e.fromVect,u),o=f({x:e.to.x-e.arrowLength,y:e.to.y},e.toVect,e.to),r={x:t.x+e.fromVect.x*e.curvature,y:t.y+e.fromVect.y*e.curvature},n={x:o.x-e.toVect.x*e.curvature,y:o.y-e.toVect.y*e.curvature},h=`M ${t.x} ${t.y} C ${r.x} ${r.y}, ${n.x} ${n.y}, ${o.x} ${o.y}`,s=e.hasSourceArrow?"":`M ${u.x} ${u.y} L ${t.x} ${t.y} `,c=e.hasTargetArrow?"":` M ${o.x} ${o.y} L ${e.to.x} ${e.to.y}`;return`${s}${h}${c}`},Pt=e=>{const t=e.hasSourceArrow?f({x:e.arrowLength,y:u.y},e.fromVect,u):u,o=e.hasTargetArrow?f({x:e.to.x-e.arrowLength,y:e.to.y},e.toVect,e.to):e.to,r=e.arrowLength,n=Math.cos(e.detourDirection)*e.detourDistance,h=Math.sin(e.detourDirection)*e.detourDistance,s=n*e.flipX,c=h*e.flipY,d=f({x:r,y:u.y},e.fromVect,u),a={x:d.x+s,y:d.y+c},l=f({x:e.to.x-r,y:e.to.y},e.toVect,e.to),g={x:l.x+s,y:l.y+c},y={x:(a.x+g.x)/2,y:(a.y+g.y)/2},x={x:d.x+e.curvature*e.fromVect.x,y:d.y+e.curvature*e.fromVect.y},v={x:l.x-e.curvature*e.toVect.x,y:l.y-e.curvature*e.toVect.y},p={x:d.x+s,y:d.y+c},T={x:l.x+s,y:l.y+c};return[`M ${t.x} ${t.y}`,`L ${d.x} ${d.y}`,`C ${x.x} ${x.y} ${p.x} ${p.y} ${y.x} ${y.y}`,`C ${T.x} ${T.y} ${v.x} ${v.y} ${l.x} ${l.y}`,`L ${o.x} ${o.y}`].join(" ")},mt=e=>{const t=e.hasSourceArrow?f({x:e.arrowLength,y:u.y},e.fromVect,u):u,o=e.hasTargetArrow?f({x:e.to.x-e.arrowLength,y:e.to.y},e.toVect,e.to):e.to,r=e.arrowLength+e.arrowOffset,n=r-e.roundness,h=f({x:n,y:u.y},e.fromVect,u),s=f({x:e.to.x-n,y:e.to.y},e.toVect,e.to),c=Math.max((h.x+s.x)/2,r),d=e.to.y/2,a={x:e.flipX>0?c:-r,y:h.y},l={x:a.x,y:d},g={x:e.flipX>0?e.to.x-c:e.to.x+r,y:s.y},y={x:g.x,y:d};return P([t,h,a,l,y,g,s,o],e.roundness)},j=e=>{const t=e.hasSourceArrow?f({x:e.arrowLength,y:u.y},e.fromVect,u):u,o=e.hasTargetArrow?f({x:e.to.x-e.arrowLength,y:e.to.y},e.toVect,e.to):e.to,r=e.arrowLength+e.arrowOffset,n=f({x:r,y:u.y},e.fromVect,u),h=Math.cos(e.detourDirection)*e.detourDistance,s=Math.sin(e.detourDirection)*e.detourDistance,c=h*e.flipX,d=s*e.flipY,a={x:n.x+c,y:n.y+d},l=f({x:e.to.x-r,y:e.to.y},e.toVect,e.to),g={x:l.x+c,y:l.y+d};return P([t,n,a,g,l,o],e.roundness)},Ct=e=>{const t=e.hasSourceArrow?f({x:e.arrowLength,y:u.y},e.fromVect,u):u,o=e.hasTargetArrow?f({x:e.to.x-e.arrowLength,y:e.to.y},e.toVect,e.to):e.to,r=e.arrowLength+e.arrowOffset,n=f({x:r,y:u.y},e.fromVect,u),h=f({x:e.to.x-r,y:e.to.y},e.toVect,e.to);return P([t,n,h,o],e.roundness)},$t=e=>{const t=e.hasSourceArrow?f({x:e.arrowLength,y:u.y},e.fromVect,u):u,o=e.hasTargetArrow?f({x:e.to.x-e.arrowLength,y:e.to.y},e.toVect,e.to):e.to,r=e.arrowLength+e.arrowOffset,n=r-e.roundness,h=f({x:n,y:u.y},e.fromVect,u),s=f({x:e.to.x-n,y:e.to.y},e.toVect,e.to),c=Math.max((h.y+s.y)/2,r),d=e.to.x/2,a={x:h.x,y:e.flipY>0?c:-r},l={x:d,y:a.y},g={x:s.x,y:e.flipY>0?e.to.y-c:e.to.y+r},y={x:d,y:g.y};return P([t,h,a,l,y,g,s,o],e.roundness)},G=e=>{const t=e.arrowOffset,o=e.side,r=e.arrowLength+t,n=r+2*o,s=[{x:e.arrowLength,y:u.y},{x:r,y:u.y},{x:r,y:e.side},{x:n,y:e.side},{x:n,y:-e.side},{x:r,y:-e.side},{x:r,y:u.y},{x:e.arrowLength,y:u.y}].map(d=>f(d,e.fromVect,u)),c=`M ${u.x} ${u.y} L ${s[0].x} ${s[0].y} `;return`${e.hasSourceArrow||e.hasTargetArrow?"":c}${P(s,e.roundness)}`},Wt=e=>{const t=e.smallRadius,o=e.radius,r=Math.sqrt(t*t+o*o),n=t+o,h=e.arrowLength+r*(1-o/n),s=t*o/n,d=[{x:e.arrowLength,y:u.y},{x:h,y:s},{x:h,y:-s}].map(g=>f(g,e.fromVect,u)),a=[`M ${d[0].x} ${d[0].y}`,`A ${t} ${t} 0 0 1 ${d[1].x} ${d[1].y}`,`A ${o} ${o} 0 1 0 ${d[2].x} ${d[2].y}`,`A ${t} ${t} 0 0 1 ${d[0].x} ${d[0].y}`].join(" "),l=`M 0 0 L ${d[0].x} ${d[0].y} `;return`${e.hasSourceArrow||e.hasTargetArrow?"":l}${a}`},w=Object.freeze({color:"#777777",width:1,arrowLength:15,arrowWidth:4,arrowOffset:15,hasSourceArrow:!1,hasTargetArrow:!1,cycleRadius:30,cycleSquareSide:30,roundness:10,detourDistance:100,detourDirection:-Math.PI/2,detourDirectionVertical:0,smallCycleRadius:15,curvature:90});class Z{constructor(t){i(this,"svg",k());i(this,"group",z());i(this,"line");i(this,"sourceArrow",null);i(this,"targetArrow",null);i(this,"arrowLength");i(this,"arrowWidth");i(this,"curvature");i(this,"portCycleRadius");i(this,"portCycleSmallRadius");i(this,"detourDirection");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");this.arrowLength=(t==null?void 0:t.arrowLength)??w.arrowLength,this.arrowWidth=(t==null?void 0:t.arrowWidth)??w.arrowWidth,this.curvature=(t==null?void 0:t.curvature)??w.curvature,this.portCycleRadius=(t==null?void 0:t.cycleRadius)??w.cycleRadius,this.portCycleSmallRadius=(t==null?void 0:t.smallCycleRadius)??w.smallCycleRadius,this.detourDirection=(t==null?void 0:t.detourDirection)??w.detourDirection,this.detourDistance=(t==null?void 0:t.detourDistance)??w.detourDistance,this.hasSourceArrow=(t==null?void 0:t.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(t==null?void 0:t.hasTargetArrow)??w.hasTargetArrow;const o=(t==null?void 0:t.color)??w.color,r=(t==null?void 0:t.width)??w.width;this.svg.appendChild(this.group),this.line=Y(o,r),this.group.appendChild(this.line),this.hasSourceArrow&&(this.sourceArrow=L(o),this.group.appendChild(this.sourceArrow)),this.hasTargetArrow&&(this.targetArrow=L(o),this.group.appendChild(this.targetArrow))}render(t){const{x:o,y:r,width:n,height:h,flipX:s,flipY:c}=X(t.from,t.to);this.svg.style.transform=`translate(${o}px, ${r}px)`,this.svg.style.width=`${n}px`,this.svg.style.height=`${h}px`,this.group.style.transform=`scale(${s}, ${c})`;const d=N(t.from.direction,s,c),a=N(t.to.direction,s,c),l={x:n,y:h};let g,y=a,x=-this.arrowLength;if(t.from.portId===t.to.portId?(g=Wt({fromVect:d,radius:this.portCycleRadius,smallRadius:this.portCycleSmallRadius,arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),y=d,x=this.arrowLength):t.from.nodeId===t.to.nodeId?g=Pt({to:l,fromVect:d,toVect:a,flipX:s,flipY:c,arrowLength:this.arrowLength,detourDirection:this.detourDirection,detourDistance:this.detourDistance,curvature:this.curvature,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}):g=bt({to:l,fromVect:d,toVect:a,arrowLength:this.arrowLength,curvature:this.curvature,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),this.line.setAttribute("d",g),this.sourceArrow){const v=V(d,u,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",v)}if(this.targetArrow){const v=V(y,l,x,this.arrowWidth);this.targetArrow.setAttribute("d",v)}}}class O{constructor(t){i(this,"svg",k());i(this,"group",z());i(this,"line");i(this,"sourceArrow",null);i(this,"targetArrow",null);i(this,"arrowLength");i(this,"arrowWidth");i(this,"arrowOffset");i(this,"roundness");i(this,"cycleSquareSide");i(this,"detourDirection");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");this.arrowLength=(t==null?void 0:t.arrowLength)??w.arrowLength,this.arrowWidth=(t==null?void 0:t.arrowWidth)??w.arrowWidth,this.arrowOffset=(t==null?void 0:t.arrowOffset)??w.arrowOffset,this.cycleSquareSide=(t==null?void 0:t.cycleSquareSide)??w.cycleSquareSide;const o=(t==null?void 0:t.roundness)??w.roundness;this.roundness=Math.min(o,this.arrowOffset,this.cycleSquareSide/2),this.detourDirection=(t==null?void 0:t.detourDirection)??w.detourDirection,this.detourDistance=(t==null?void 0:t.detourDistance)??w.detourDistance,this.hasSourceArrow=(t==null?void 0:t.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(t==null?void 0:t.hasTargetArrow)??w.hasTargetArrow;const r=(t==null?void 0:t.color)??w.color,n=(t==null?void 0:t.width)??w.width;this.svg.appendChild(this.group),this.line=Y(r,n),this.group.appendChild(this.line),this.hasSourceArrow&&(this.sourceArrow=L(r),this.group.appendChild(this.sourceArrow)),this.hasTargetArrow&&(this.targetArrow=L(r),this.group.appendChild(this.targetArrow))}render(t){const{x:o,y:r,width:n,height:h,flipX:s,flipY:c}=X(t.from,t.to);this.svg.style.transform=`translate(${o}px, ${r}px)`,this.svg.style.width=`${n}px`,this.svg.style.height=`${h}px`,this.group.style.transform=`scale(${s}, ${c})`;const d=N(t.from.direction,s,c),a=N(t.to.direction,s,c),l={x:n,y:h};let g,y=a,x=-this.arrowLength;if(t.from.portId===t.to.portId?(g=G({fromVect:d,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),y=d,x=this.arrowLength):t.from.nodeId===t.to.nodeId?g=j({to:l,fromVect:d,toVect:a,flipX:s,flipY:c,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDirection:this.detourDirection,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}):g=mt({to:l,fromVect:d,toVect:a,flipX:s,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),this.line.setAttribute("d",g),this.sourceArrow){const v=V(d,u,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",v)}if(this.targetArrow){const v=V(y,l,x,this.arrowWidth);this.targetArrow.setAttribute("d",v)}}}class q{constructor(t){i(this,"svg",k());i(this,"group",z());i(this,"line");i(this,"sourceArrow",null);i(this,"targetArrow",null);i(this,"arrowLength");i(this,"arrowWidth");i(this,"arrowOffset");i(this,"roundness");i(this,"cycleSquareSide");i(this,"detourDirection");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");this.arrowLength=(t==null?void 0:t.arrowLength)??w.arrowLength,this.arrowWidth=(t==null?void 0:t.arrowWidth)??w.arrowWidth,this.arrowOffset=(t==null?void 0:t.arrowOffset)??w.arrowOffset,this.cycleSquareSide=(t==null?void 0:t.cycleSquareSide)??w.cycleSquareSide;const o=(t==null?void 0:t.roundness)??w.roundness;this.roundness=Math.min(o,this.arrowOffset,this.cycleSquareSide/2),this.detourDirection=(t==null?void 0:t.detourDirection)??w.detourDirection,this.detourDistance=(t==null?void 0:t.detourDistance)??w.detourDistance,this.hasSourceArrow=(t==null?void 0:t.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(t==null?void 0:t.hasTargetArrow)??w.hasTargetArrow;const r=(t==null?void 0:t.color)??w.color,n=(t==null?void 0:t.width)??w.width;this.svg.appendChild(this.group),this.line=Y(r,n),this.group.appendChild(this.line),this.hasSourceArrow&&(this.sourceArrow=L(r),this.group.appendChild(this.sourceArrow)),this.hasTargetArrow&&(this.targetArrow=L(r),this.group.appendChild(this.targetArrow))}render(t){const{x:o,y:r,width:n,height:h,flipX:s,flipY:c}=X(t.from,t.to);this.svg.style.transform=`translate(${o}px, ${r}px)`,this.svg.style.width=`${n}px`,this.svg.style.height=`${h}px`,this.group.style.transform=`scale(${s}, ${c})`;const d=N(t.from.direction,s,c),a=N(t.to.direction,s,c),l={x:n,y:h};let g,y=a,x=-this.arrowLength;if(t.from.portId===t.to.portId?(g=G({fromVect:d,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),y=d,x=this.arrowLength):t.from.nodeId===t.to.nodeId?g=j({to:l,fromVect:d,toVect:a,flipX:s,flipY:c,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDirection:this.detourDirection,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}):g=Ct({to:l,fromVect:d,toVect:a,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),this.line.setAttribute("d",g),this.sourceArrow){const v=V(d,u,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",v)}if(this.targetArrow){const v=V(y,l,x,this.arrowWidth);this.targetArrow.setAttribute("d",v)}}}class tt{constructor(t){i(this,"svg",k());i(this,"group",z());i(this,"line");i(this,"sourceArrow",null);i(this,"targetArrow",null);i(this,"arrowLength");i(this,"arrowWidth");i(this,"arrowOffset");i(this,"roundness");i(this,"cycleSquareSide");i(this,"detourDirection");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");this.arrowLength=(t==null?void 0:t.arrowLength)??w.arrowLength,this.arrowWidth=(t==null?void 0:t.arrowWidth)??w.arrowWidth,this.arrowOffset=(t==null?void 0:t.arrowOffset)??w.arrowOffset,this.cycleSquareSide=(t==null?void 0:t.cycleSquareSide)??w.cycleSquareSide;const o=(t==null?void 0:t.roundness)??w.roundness;this.roundness=Math.min(o,this.arrowOffset,this.cycleSquareSide/2),this.detourDirection=(t==null?void 0:t.detourDirection)??w.detourDirectionVertical,this.detourDistance=(t==null?void 0:t.detourDistance)??w.detourDistance,this.hasSourceArrow=(t==null?void 0:t.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(t==null?void 0:t.hasTargetArrow)??w.hasTargetArrow;const r=(t==null?void 0:t.color)??w.color,n=(t==null?void 0:t.width)??w.width;this.svg.appendChild(this.group),this.line=Y(r,n),this.group.appendChild(this.line),this.hasSourceArrow&&(this.sourceArrow=L(r),this.group.appendChild(this.sourceArrow)),this.hasTargetArrow&&(this.targetArrow=L(r),this.group.appendChild(this.targetArrow))}render(t){const{x:o,y:r,width:n,height:h,flipX:s,flipY:c}=X(t.from,t.to);this.svg.style.transform=`translate(${o}px, ${r}px)`,this.svg.style.width=`${n}px`,this.svg.style.height=`${h}px`,this.group.style.transform=`scale(${s}, ${c})`;const d=N(t.from.direction,s,c),a=N(t.to.direction,s,c),l={x:n,y:h};let g,y=a,x=-this.arrowLength;if(t.from.portId===t.to.portId?(g=G({fromVect:d,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),y=d,x=this.arrowLength):t.from.nodeId===t.to.nodeId?g=j({to:l,fromVect:d,toVect:a,flipX:s,flipY:c,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDirection:this.detourDirection,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}):g=$t({to:l,fromVect:d,toVect:a,flipY:c,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),this.line.setAttribute("d",g),this.sourceArrow){const v=V(d,u,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",v)}if(this.targetArrow){const v=V(y,l,x,this.arrowWidth);this.targetArrow.setAttribute("d",v)}}}const It=e=>{if(typeof e=="function")return e;switch(e==null?void 0:e.type){case"straight":return()=>new q({color:e.color,width:e.width,arrowLength:e.arrowLength,arrowWidth:e.arrowWidth,arrowOffset:e.arrowOffset,hasSourceArrow:e.hasSourceArrow,hasTargetArrow:e.hasTargetArrow,cycleSquareSide:e.cycleSquareSide,roundness:e.roundness,detourDistance:e.detourDistance,detourDirection:e.detourDirection});case"horizontal":return()=>new O({color:e.color,width:e.width,arrowLength:e.arrowLength,arrowWidth:e.arrowWidth,arrowOffset:e.arrowOffset,hasSourceArrow:e.hasSourceArrow,hasTargetArrow:e.hasTargetArrow,cycleSquareSide:e.cycleSquareSide,roundness:e.roundness,detourDistance:e.detourDistance,detourDirection:e.detourDirection});case"vertical":return()=>new tt({color:e.color,width:e.width,arrowLength:e.arrowLength,arrowWidth:e.arrowWidth,arrowOffset:e.arrowOffset,hasSourceArrow:e.hasSourceArrow,hasTargetArrow:e.hasTargetArrow,cycleSquareSide:e.cycleSquareSide,roundness:e.roundness,detourDistance:e.detourDistance,detourDirection:e.detourDirection});default:return()=>new Z({color:e.color,width:e.width,arrowLength:e.arrowLength,arrowWidth:e.arrowWidth,hasSourceArrow:e.hasSourceArrow,hasTargetArrow:e.hasTargetArrow,cycleRadius:e.cycleRadius,smallCycleRadius:e.smallCycleRadius,curvature:e.curvature,detourDistance:e.detourDistance,detourDirection:e.detourDirection})}},Ft=e=>{var o,r,n,h,s;const t=Dt((o=e==null?void 0:e.nodes)==null?void 0:o.priority,(r=e==null?void 0:e.edges)==null?void 0:r.priority);return{nodes:{centerFn:((n=e==null?void 0:e.nodes)==null?void 0:n.centerFn)??Vt,priorityFn:t.nodesPriorityFn},ports:{direction:((h=e==null?void 0:e.ports)==null?void 0:h.direction)??0},edges:{shapeFactory:It(((s=e==null?void 0:e.edges)==null?void 0:s.shape)??{}),priorityFn:t.edgesPriorityFn}}};class Rt{constructor(t,o){i(this,"nodeIdGenerator",new H(t=>this.graph.getNode(t)!==null));i(this,"portIdGenerator",new H(t=>this.graph.getPort(t)!==null));i(this,"edgeIdGenerator",new H(t=>this.graph.getEdge(t)!==null));i(this,"defaults");i(this,"graph");i(this,"viewport");this.controller=t,this.defaults=Ft(o),this.graph=t.graph,this.viewport=t.viewport}attach(t){return this.controller.attach(t),this}detach(){return this.controller.detach(),this}addNode(t){const o=this.nodeIdGenerator.create(t.id);if(this.graph.getNode(o)!==null)throw new A("failed to add node with existing id");if(this.controller.addNode({id:o,element:t.element,x:t.x,y:t.y,centerFn:t.centerFn??this.defaults.nodes.centerFn,priority:t.priority??this.defaults.nodes.priorityFn()}),t.ports!==void 0)for(const r of t.ports)this.markPort({id:r.id,element:r.element,nodeId:o,direction:r.direction});return this}updateNode(t,o){if(this.graph.getNode(t)===null)throw new A("failed to update nonexisting node");return this.controller.updateNode(t,o??{}),this}removeNode(t){if(this.graph.getNode(t)===null)throw new A("failed to remove nonexisting node");return this.controller.removeNode(t),this}markPort(t){const o=this.portIdGenerator.create(t.id);if(this.graph.getPort(o)!==null)throw new A("failed to add port with existing id");if(this.graph.getNode(t.nodeId)===null)throw new A("failed to set port on nonexisting node");return this.controller.markPort({id:o,element:t.element,nodeId:t.nodeId,direction:t.direction??this.defaults.ports.direction}),this}updatePort(t,o){if(this.graph.getPort(t)===null)throw new A("failed to unset nonexisting port");return this.controller.updatePort(t,o??{}),this}unmarkPort(t){if(this.graph.getPort(t)===null)throw new A("failed to unset nonexisting port");return this.controller.unmarkPort(t),this}addEdge(t){const o=this.edgeIdGenerator.create(t.id);if(this.graph.getEdge(o)!==null)throw new A("failed to add edge with existing id");if(this.graph.getPort(t.from)===null)throw new A("failed to add edge from nonexisting port");if(this.graph.getPort(t.to)===null)throw new A("failed to add edge to nonexisting port");return this.controller.addEdge({id:o,from:t.from,to:t.to,shape:t.shape??this.defaults.edges.shapeFactory(),priority:t.priority??this.defaults.edges.priorityFn()}),this}updateEdge(t,o){if(this.graph.getEdge(t)===null)throw new A("failed to update nonexisting edge");return this.controller.updateEdge(t,o??{}),this}removeEdge(t){if(this.graph.getEdge(t)===null)throw new A("failed to remove nonexisting edge");return this.controller.removeEdge(t),this}patchViewportMatrix(t){return this.controller.patchViewportMatrix(t),this}patchContentMatrix(t){return this.controller.patchContentMatrix(t),this}clear(){return this.controller.clear(),this.nodeIdGenerator.reset(),this.portIdGenerator.reset(),this.edgeIdGenerator.reset(),this}destroy(){this.controller.destroy()}}class kt{constructor(){i(this,"nodes",new Map);i(this,"ports",new Map);i(this,"edges",new Map);i(this,"incommingEdges",new Map);i(this,"outcommingEdges",new Map);i(this,"cycleEdges",new Map)}addNode(t){const o=new Map,r={element:t.element,x:t.x,y:t.y,centerFn:t.centerFn,priority:t.priority,ports:o};this.nodes.set(t.id,r)}getAllNodeIds(){return Array.from(this.nodes.keys())}getNode(t){return this.nodes.get(t)}removeNode(t){this.nodes.delete(t)}addPort(t){this.ports.set(t.id,{element:t.element,direction:t.direction,nodeId:t.nodeId}),this.cycleEdges.set(t.id,new Set),this.incommingEdges.set(t.id,new Set),this.outcommingEdges.set(t.id,new Set),this.nodes.get(t.nodeId).ports.set(t.id,t.element)}getPort(t){return this.ports.get(t)}getAllPortIds(){return Array.from(this.ports.keys())}getNodePortIds(t){const o=this.nodes.get(t);if(o!==void 0)return Array.from(o.ports.keys())}removePort(t){const o=this.ports.get(t).nodeId;this.nodes.get(o).ports.delete(t),this.ports.delete(t)}addEdge(t){this.edges.set(t.id,{from:t.from,to:t.to,shape:t.shape,priority:t.priority}),t.from!==t.to?(this.outcommingEdges.get(t.from).add(t.id),this.incommingEdges.get(t.to).add(t.id)):this.cycleEdges.get(t.from).add(t.id)}updateEdgeFrom(t,o){const r=this.edges.get(t);this.removeEdge(t),this.addEdge({id:t,from:o,to:r.to,shape:r.shape,priority:r.priority})}updateEdgeTo(t,o){const r=this.edges.get(t);this.removeEdge(t),this.addEdge({id:t,from:r.from,to:o,shape:r.shape,priority:r.priority})}getAllEdgeIds(){return Array.from(this.edges.keys())}getEdge(t){return this.edges.get(t)}removeEdge(t){const o=this.edges.get(t),r=o.from,n=o.to;this.cycleEdges.get(r).delete(t),this.cycleEdges.get(n).delete(t),this.incommingEdges.get(r).delete(t),this.incommingEdges.get(n).delete(t),this.outcommingEdges.get(r).delete(t),this.outcommingEdges.get(n).delete(t),this.edges.delete(t)}clear(){this.incommingEdges.clear(),this.outcommingEdges.clear(),this.cycleEdges.clear(),this.edges.clear(),this.ports.clear(),this.nodes.clear()}getPortIncomingEdgeIds(t){return Array.from(this.incommingEdges.get(t))}getPortOutcomingEdgeIds(t){return Array.from(this.outcommingEdges.get(t))}getPortCycleEdgeIds(t){return Array.from(this.cycleEdges.get(t))}getPortAdjacentEdgeIds(t){return[...this.getPortIncomingEdgeIds(t),...this.getPortOutcomingEdgeIds(t),...this.getPortCycleEdgeIds(t)]}getNodeIncomingEdgeIds(t){const o=Array.from(this.nodes.get(t).ports.keys());let r=[];return o.forEach(n=>{r=[...r,...this.getPortIncomingEdgeIds(n)]}),r}getNodeOutcomingEdgeIds(t){const o=Array.from(this.nodes.get(t).ports.keys());let r=[];return o.forEach(n=>{r=[...r,...this.getPortOutcomingEdgeIds(n)]}),r}getNodeCycleEdgeIds(t){const o=Array.from(this.nodes.get(t).ports.keys());let r=[];return o.forEach(n=>{r=[...r,...this.getPortCycleEdgeIds(n)]}),r}getNodeAdjacentEdgeIds(t){return[...this.getNodeIncomingEdgeIds(t),...this.getNodeOutcomingEdgeIds(t),...this.getNodeCycleEdgeIds(t)]}}const et=e=>({scale:1/e.scale,x:-e.x/e.scale,y:-e.y/e.scale}),ot={scale:1,x:0,y:0};class zt{constructor(){i(this,"viewportMatrix",ot);i(this,"contentMatrix",ot);i(this,"emitter");i(this,"onAfterUpdate");[this.emitter,this.onAfterUpdate]=Nt()}getViewportMatrix(){return this.viewportMatrix}getContentMatrix(){return this.contentMatrix}patchViewportMatrix(t){this.viewportMatrix={scale:t.scale??this.viewportMatrix.scale,x:t.x??this.viewportMatrix.x,y:t.y??this.viewportMatrix.y},this.contentMatrix=et(this.viewportMatrix),this.emitter.emit()}patchContentMatrix(t){this.contentMatrix={scale:t.scale??this.contentMatrix.scale,x:t.x??this.contentMatrix.x,y:t.y??this.contentMatrix.y},this.viewportMatrix=et(this.contentMatrix),this.emitter.emit()}}class Yt{constructor(){i(this,"canvasDefaults",{});i(this,"dragOptions");i(this,"transformOptions");i(this,"virtualScrollOptions");i(this,"hasDraggableNode",!1);i(this,"hasTransformableViewport",!1);i(this,"hasResizeReactiveNodes",!1);i(this,"boxRenderingTrigger")}setDefaults(t){return this.canvasDefaults=t,this}setOptions(t){return this.setDefaults(t),this}enableUserDraggableNodes(t){return this.hasDraggableNode=!0,this.dragOptions=t,this}enableUserTransformableViewport(t){return this.hasTransformableViewport=!0,this.transformOptions=t,this}enableResizeReactiveNodes(){return this.hasResizeReactiveNodes=!0,this}enableBoxAreaRendering(t){return this.boxRenderingTrigger=t,this}enableVirtualScroll(t){return this.virtualScrollOptions=t,this}build(){let t=this.boxRenderingTrigger;this.virtualScrollOptions!==void 0&&t===void 0&&(t=new B);const o=new kt,r=new zt;let n=new Tt(o,r);t!==void 0&&(n=new Mt(n,o,t));let h=new ht(o,r,n);this.hasResizeReactiveNodes&&(h=new ft(h)),this.hasDraggableNode&&(h=new dt(h,this.dragOptions)),this.virtualScrollOptions!==void 0?h=new xt(h,t,this.transformOptions,this.virtualScrollOptions):this.hasTransformableViewport&&(h=new J(h,this.transformOptions));const s=new Rt(h,this.canvasDefaults);return this.reset(),s}reset(){this.canvasDefaults={},this.dragOptions=void 0,this.transformOptions=void 0,this.virtualScrollOptions=void 0,this.hasDraggableNode=!1,this.hasTransformableViewport=!1,this.hasResizeReactiveNodes=!1,this.boxRenderingTrigger=void 0}}E.BezierEdgeShape=Z,E.CanvasBuilder=Yt,E.EventSubject=B,E.HorizontalEdgeShape=O,E.HtmlGraphError=A,E.StraightEdgeShape=q,E.VerticalEdgeShape=tt,Object.defineProperty(E,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(E,S){typeof exports=="object"&&typeof module<"u"?S(exports):typeof define=="function"&&define.amd?define(["exports"],S):(E=typeof globalThis<"u"?globalThis:E||self,S(E.HtmlGraph={}))})(this,function(E){"use strict";var ze=Object.defineProperty;var Ye=(E,S,C)=>S in E?ze(E,S,{enumerable:!0,configurable:!0,writable:!0,value:C}):E[S]=C;var i=(E,S,C)=>Ye(E,typeof S!="symbol"?S+"":S,C);class S{constructor(e){i(this,"onAfterNodeAdded");i(this,"onAfterNodeCoordinatesUpdated");i(this,"onAfterNodePriorityUpdated");i(this,"onBeforeNodeRemoved");i(this,"onAfterPortAdded");i(this,"onAfterPortDirectionUpdated");i(this,"onBeforePortRemoved");i(this,"onAfterEdgeAdded");i(this,"onAfterEdgeShapeUpdated");i(this,"onAfterEdgeAdjacentPortsUpdated");i(this,"onAfterEdgePriorityUpdated");i(this,"onBeforeEdgeRemoved");i(this,"onBeforeClear");this.graphStore=e,this.onAfterNodeAdded=this.graphStore.onAfterNodeAdded,this.onAfterNodeCoordinatesUpdated=this.graphStore.onAfterNodeCoordinatesUpdated,this.onAfterNodePriorityUpdated=this.graphStore.onAfterNodePriorityUpdated,this.onBeforeNodeRemoved=this.graphStore.onBeforeNodeRemoved,this.onAfterPortAdded=this.graphStore.onAfterPortAdded,this.onAfterPortDirectionUpdated=this.graphStore.onAfterPortDirectionUpdated,this.onBeforePortRemoved=this.graphStore.onBeforePortRemoved,this.onAfterEdgeAdded=this.graphStore.onAfterEdgeAdded,this.onAfterEdgeShapeUpdated=this.graphStore.onAfterEdgeShapeUpdated,this.onAfterEdgeAdjacentPortsUpdated=this.graphStore.onAfterEdgeAdjacentPortsUpdated,this.onAfterEdgePriorityUpdated=this.graphStore.onAfterEdgePriorityUpdated,this.onBeforeEdgeRemoved=this.graphStore.onBeforeEdgeRemoved,this.onBeforeClear=this.graphStore.onBeforeClear}getNode(e){const o=this.graphStore.getNode(e);return o===void 0?null:{element:o.element,x:o.x,y:o.y,centerFn:o.centerFn,priority:o.priority}}getAllNodeIds(){return this.graphStore.getAllNodeIds()}getPort(e){const o=this.graphStore.getPort(e);return o===void 0?null:{element:o.element,direction:o.direction,nodeId:o.nodeId}}getAllPortIds(){return this.graphStore.getAllPortIds()}getNodePortIds(e){return this.graphStore.getNodePortIds(e)??null}getPortNodeId(e){var o;return((o=this.graphStore.getPort(e))==null?void 0:o.nodeId)??null}getAllEdgeIds(){return this.graphStore.getAllEdgeIds()}getEdge(e){const o=this.graphStore.getEdge(e);return o===void 0?null:{from:o.from,to:o.to,priority:o.priority}}getPortIncomingEdgeIds(e){return this.graphStore.getPort(e)===void 0?null:this.graphStore.getPortIncomingEdgeIds(e)}getPortOutcomingEdgeIds(e){return this.graphStore.getPort(e)===void 0?null:this.graphStore.getPortOutcomingEdgeIds(e)}getPortCycleEdgeIds(e){return this.graphStore.getPort(e)===void 0?null:this.graphStore.getPortCycleEdgeIds(e)}getPortAdjacentEdgeIds(e){return this.graphStore.getPort(e)===void 0?null:this.graphStore.getPortAdjacentEdgeIds(e)}getNodeIncomingEdgeIds(e){return this.graphStore.getNode(e)===void 0?null:this.graphStore.getNodeIncomingEdgeIds(e)}getNodeOutcomingEdgeIds(e){return this.graphStore.getNode(e)===void 0?null:this.graphStore.getNodeOutcomingEdgeIds(e)}getNodeCycleEdgeIds(e){return this.graphStore.getNode(e)===void 0?null:this.graphStore.getNodeCycleEdgeIds(e)}getNodeAdjacentEdgeIds(e){return this.graphStore.getNode(e)===void 0?null:this.graphStore.getNodeAdjacentEdgeIds(e)}}class C{constructor(e){this.transformer=e}getViewportMatrix(){return{...this.transformer.getViewportMatrix()}}getContentMatrix(){return{...this.transformer.getContentMatrix()}}}class de{constructor(e,o,r){i(this,"viewport");i(this,"graph");this.graphStore=e,this.viewportStore=o,this.htmlView=r,this.graph=new S(this.graphStore),this.viewport=new C(this.viewportStore)}attach(e){this.htmlView.attach(e)}detach(){this.htmlView.detach()}addNode(e){this.graphStore.addNode(e),this.htmlView.attachNode(e.id)}updateNode(e,o){this.graphStore.updateNodeCoordinates(e,{x:o.x,y:o.y,centerFn:o.centerFn}),this.htmlView.updateNodeCoordinates(e),o.priority!==void 0&&(this.graphStore.updateNodePriority(e,o.priority),this.htmlView.updateNodePriority(e)),this.graphStore.getNodeAdjacentEdgeIds(e).forEach(n=>{this.htmlView.renderEdge(n)})}removeNode(e){this.graphStore.getNodePortIds(e).forEach(o=>{this.unmarkPort(o)}),this.graphStore.removeNode(e),this.htmlView.detachNode(e)}markPort(e){this.graphStore.addPort(e)}updatePort(e,o){o.direction!==void 0&&this.graphStore.updatePortDirection(e,o.direction),this.graphStore.getPortAdjacentEdgeIds(e).forEach(n=>{this.htmlView.renderEdge(n)})}unmarkPort(e){this.graphStore.getPortAdjacentEdgeIds(e).forEach(o=>{this.removeEdge(o)}),this.graphStore.removePort(e)}addEdge(e){this.graphStore.addEdge(e),this.htmlView.attachEdge(e.id)}updateEdge(e,o){(o.from!==void 0||o.to!==void 0)&&this.graphStore.updateEdgeAdjacentPorts(e,{from:o.from,to:o.to}),o.shape!==void 0&&(this.graphStore.updateEdgeShape(e,o.shape),this.htmlView.updateEdgeShape(e)),this.htmlView.renderEdge(e),o.priority!==void 0&&(this.graphStore.updateEdgePriority(e,o.priority),this.htmlView.updateEdgePriority(e))}removeEdge(e){this.graphStore.removeEdge(e),this.htmlView.detachEdge(e)}patchViewportMatrix(e){this.viewportStore.patchViewportMatrix(e)}patchContentMatrix(e){this.viewportStore.patchContentMatrix(e)}clear(){this.htmlView.clear(),this.graphStore.clear()}destroy(){this.clear(),this.htmlView.destroy()}}const R=(t,e,o)=>{const{x:r,y:n,width:h,height:s}=t.getBoundingClientRect();return e>=r&&e<=r+h&&o>=n&&o<=n+s},I=(t,e,o)=>e>=0&&e<=t.innerWidth&&o>=0&&o<=t.innerHeight,F=(t,e)=>{e!==null?t.style.cursor=e:t.style.removeProperty("cursor")},ce=t=>{var g,f,x,y,N,T;const e=((g=t==null?void 0:t.events)==null?void 0:g.onNodeDrag)??(()=>{}),o=((f=t==null?void 0:t.events)==null?void 0:f.onBeforeNodeDrag)??(()=>!0),r=((x=t==null?void 0:t.events)==null?void 0:x.onNodeDragFinished)??(()=>{}),n=(t==null?void 0:t.moveOnTop)===!1,h=(y=t==null?void 0:t.mouse)==null?void 0:y.dragCursor,s=h!==void 0?h:"grab",d=(N=t==null?void 0:t.mouse)==null?void 0:N.mouseDownEventVerifier,c=d!==void 0?d:m=>m.button===0,a=(T=t==null?void 0:t.mouse)==null?void 0:T.mouseUpEventVerifier;return{freezePriority:n,dragCursor:s,mouseDownEventVerifier:c,mouseUpEventVerifier:a!==void 0?a:m=>m.button===0,onNodeDrag:e,onBeforeNodeDrag:o,onNodeDragFinished:r}};class ae{constructor(e,o){i(this,"graph");i(this,"viewport");i(this,"maxNodePriority",0);i(this,"nodes",new Map);i(this,"grabbedNodeId",null);i(this,"element",null);i(this,"onWindowMouseMove",e=>{if(this.element!==null&&(!R(this.element,e.clientX,e.clientY)||!I(this.window,e.clientX,e.clientY))){this.cancelMouseDrag();return}this.grabbedNodeId!==null&&this.dragNode(this.grabbedNodeId,e.movementX,e.movementY)});i(this,"onWindowMouseUp",e=>{this.options.mouseUpEventVerifier(e)&&this.cancelMouseDrag()});i(this,"onWindowTouchMove",e=>{if(e.touches.length!==1)return;const o=e.touches[0];if(this.element!==null&&(!R(this.element,o.clientX,o.clientY)||!I(this.window,o.clientX,o.clientY))){this.cancelTouchDrag();return}if(this.grabbedNodeId!==null&&this.previousTouchCoords!==null){const r=o.clientX-this.previousTouchCoords.x,n=o.clientY-this.previousTouchCoords.y;this.dragNode(this.grabbedNodeId,r,n),this.previousTouchCoords={x:e.touches[0].clientX,y:e.touches[0].clientY}}});i(this,"onWindowTouchFinish",()=>{this.previousTouchCoords=null,this.cancelTouchDrag()});i(this,"previousTouchCoords",null);i(this,"window",window);i(this,"options");this.canvas=e,this.viewport=this.canvas.viewport,this.graph=this.canvas.graph,this.options=ce(o??{})}attach(e){this.detach(),this.element=e,this.canvas.attach(this.element)}detach(){this.canvas.detach(),this.element!==null&&(this.element=null)}addNode(e){this.canvas.addNode(e),this.updateMaxNodePriority(e.id);const o=n=>{if(this.element===null||!this.options.mouseDownEventVerifier(n))return;const h=this.graph.getNode(e.id);this.options.onBeforeNodeDrag({nodeId:e.id,element:e.element,x:h.x,y:h.y})&&(n.stopImmediatePropagation(),this.grabbedNodeId=e.id,F(this.element,this.options.dragCursor),this.moveNodeOnTop(e.id),this.window.addEventListener("mouseup",this.onWindowMouseUp),this.window.addEventListener("mousemove",this.onWindowMouseMove))},r=n=>{if(n.touches.length!==1)return;n.stopImmediatePropagation(),this.previousTouchCoords={x:n.touches[0].clientX,y:n.touches[0].clientY};const h=this.graph.getNode(e.id);this.options.onBeforeNodeDrag({nodeId:e.id,element:e.element,x:h.x,y:h.y})&&(this.grabbedNodeId=e.id,this.moveNodeOnTop(e.id),this.window.addEventListener("touchmove",this.onWindowTouchMove),this.window.addEventListener("touchend",this.onWindowTouchFinish),this.window.addEventListener("touchcancel",this.onWindowTouchFinish))};this.nodes.set(e.id,{element:e.element,onMouseDown:o,onTouchStart:r}),e.element.addEventListener("mousedown",o),e.element.addEventListener("touchstart",r)}updateNode(e,o){this.canvas.updateNode(e,o),this.updateMaxNodePriority(e)}removeNode(e){const o=this.nodes.get(e);o!==void 0&&(o.element.removeEventListener("mousedown",o.onMouseDown),o.element.removeEventListener("touchstart",o.onTouchStart)),this.nodes.delete(e),this.canvas.removeNode(e)}markPort(e){this.canvas.markPort(e)}updatePort(e,o){this.canvas.updatePort(e,o)}unmarkPort(e){this.canvas.unmarkPort(e)}addEdge(e){this.canvas.addEdge(e)}updateEdge(e,o){this.canvas.updateEdge(e,o)}removeEdge(e){this.canvas.removeEdge(e)}patchViewportMatrix(e){this.canvas.patchViewportMatrix(e)}patchContentMatrix(e){this.canvas.patchContentMatrix(e)}clear(){this.canvas.clear(),this.nodes.forEach(e=>{e.element.removeEventListener("mousedown",e.onMouseDown),e.element.removeEventListener("touchstart",e.onTouchStart)}),this.nodes.clear(),this.maxNodePriority=0}destroy(){this.detach(),this.clear(),this.removeMouseDragListeners(),this.removeTouchDragListeners(),this.canvas.destroy()}dragNode(e,o,r){const n=this.graph.getNode(e);if(n===null)return;const h=this.canvas.viewport.getContentMatrix(),s=h.scale*n.x+h.x,d=h.scale*n.y+h.y,c=s+o,a=d+r,l=this.canvas.viewport.getViewportMatrix(),g=l.scale*c+l.x,f=l.scale*a+l.y;this.canvas.updateNode(e,{x:g,y:f}),this.options.onNodeDrag({nodeId:e,element:n.element,x:g,y:f})}updateMaxNodePriority(e){const o=this.graph.getNode(e).priority;this.maxNodePriority=Math.max(this.maxNodePriority,o)}moveNodeOnTop(e){if(this.options.freezePriority)return;this.maxNodePriority+=2,this.updateNode(e,{priority:this.maxNodePriority});const o=this.maxNodePriority-1;this.graph.getNodeAdjacentEdgeIds(e).forEach(n=>{this.updateEdge(n,{priority:o})})}cancelMouseDrag(){const e=this.graph.getNode(this.grabbedNodeId);e!==null&&this.options.onNodeDragFinished({nodeId:this.grabbedNodeId,element:e.element,x:e.x,y:e.y}),this.grabbedNodeId=null,this.element!==null&&F(this.element,null),this.removeMouseDragListeners()}removeMouseDragListeners(){this.window.removeEventListener("mouseup",this.onWindowMouseUp),this.window.removeEventListener("mousemove",this.onWindowMouseMove)}cancelTouchDrag(){this.previousTouchCoords=null;const e=this.graph.getNode(this.grabbedNodeId);e!==null&&this.options.onNodeDragFinished({nodeId:this.grabbedNodeId,element:e.element,x:e.x,y:e.y}),this.grabbedNodeId=null,this.removeTouchDragListeners()}removeTouchDragListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}}const le=t=>{const e=t.minX!==null?t.minX:-1/0,o=t.maxX!==null?t.maxX:1/0,r=t.minY!==null?t.minY:-1/0,n=t.maxY!==null?t.maxY:1/0;return h=>{let s=h.nextTransform.x,d=h.nextTransform.y;s<e&&s<h.prevTransform.x&&(s=Math.min(h.prevTransform.x,e));const c=h.canvasWidth*h.prevTransform.scale,a=o-c;s>a&&s>h.prevTransform.x&&(s=Math.max(h.prevTransform.x,a)),d<r&&d<h.prevTransform.y&&(d=Math.min(h.prevTransform.y,r));const l=h.canvasHeight*h.prevTransform.scale,g=n-l;return d>g&&d>h.prevTransform.y&&(d=Math.max(h.prevTransform.y,g)),{scale:h.nextTransform.scale,x:s,y:d}}},ge=t=>{const e=t.maxContentScale,o=t.minContentScale,r=e!==null?1/e:0,n=o!==null?1/o:1/0;return h=>{const s=h.prevTransform,d=h.nextTransform;let c=d.scale,a=d.x,l=d.y;if(d.scale>n&&d.scale>s.scale){c=Math.max(s.scale,n),a=s.x,l=s.y;const g=(c-s.scale)/(d.scale-s.scale);a=s.x+(d.x-s.x)*g,l=s.y+(d.y-s.y)*g}if(d.scale<r&&d.scale<s.scale){c=Math.min(s.scale,r),a=s.x,l=s.y;const g=(c-s.scale)/(d.scale-s.scale);a=s.x+(d.x-s.x)*g,l=s.y+(d.y-s.y)*g}return{scale:c,x:a,y:l}}},ue=t=>e=>t.reduce((o,r)=>r({prevTransform:e.prevTransform,nextTransform:o,canvasWidth:e.canvasWidth,canvasHeight:e.canvasHeight}),e.nextTransform),J=t=>{if(typeof t=="function")return t;switch(t.type){case"scale-limit":return ge({minContentScale:t.minContentScale??0,maxContentScale:t.maxContentScale??1/0});case"shift-limit":return le({minX:t.minX??-1/0,maxX:t.maxX??1/0,minY:t.minY??-1/0,maxY:t.maxY??1/0})}},we=t=>{var y,N,T,m,_,b,$,W,ie,se,ne,he;const e=(y=t==null?void 0:t.scale)==null?void 0:y.mouseWheelSensitivity,o=e!==void 0?e:1.2,r=t==null?void 0:t.transformPreprocessor;let n;r!==void 0?Array.isArray(r)?n=ue(r.map(D=>J(D))):n=J(r):n=D=>D.nextTransform;const h=((N=t==null?void 0:t.shift)==null?void 0:N.cursor)!==void 0?t.shift.cursor:"grab",s=((T=t==null?void 0:t.events)==null?void 0:T.onBeforeTransformChange)??(()=>{}),d=((m=t==null?void 0:t.events)==null?void 0:m.onTransformChange)??(()=>{}),c=(_=t==null?void 0:t.shift)==null?void 0:_.mouseDownEventVerifier,a=c!==void 0?c:D=>D.button===0,l=(b=t==null?void 0:t.shift)==null?void 0:b.mouseUpEventVerifier,g=l!==void 0?l:D=>D.button===0,f=($=t==null?void 0:t.scale)==null?void 0:$.mouseWheelEventVerifier,x=f!==void 0?f:()=>!0;return{wheelSensitivity:o,onTransformStarted:((W=t==null?void 0:t.events)==null?void 0:W.onTransformStarted)??(()=>{}),onTransformFinished:((ie=t==null?void 0:t.events)==null?void 0:ie.onTransformFinished)??(()=>{}),onBeforeTransformChange:s,onTransformChange:d,transformPreprocessor:n,shiftCursor:h,mouseDownEventVerifier:a,mouseUpEventVerifier:g,mouseWheelEventVerifier:x,scaleWheelFinishTimeout:((se=t==null?void 0:t.scale)==null?void 0:se.wheelFinishTimeout)??500,onResizeTransformStarted:((ne=t==null?void 0:t.events)==null?void 0:ne.onResizeTransformStarted)??(()=>{}),onResizeTransformFinished:((he=t==null?void 0:t.events)==null?void 0:he.onResizeTransformFinished)??(()=>{})}},U=t=>{const e=[],o=t.touches.length;for(let d=0;d<o;d++)e.push([t.touches[d].clientX,t.touches[d].clientY]);const r=e.reduce((d,c)=>[d[0]+c[0],d[1]+c[1]],[0,0]),n=[r[0]/o,r[1]/o],s=e.map(d=>[d[0]-n[0],d[1]-n[1]]).reduce((d,c)=>d+Math.sqrt(c[0]*c[0]+c[1]*c[1]),0);return{x:n[0],y:n[1],scale:s/o,touchesCnt:o,touches:e}},fe=(t,e,o)=>({scale:t.scale,x:t.x+t.scale*e,y:t.y+t.scale*o}),ye=(t,e,o,r)=>({scale:t.scale*e,x:t.scale*(1-e)*o+t.x,y:t.scale*(1-e)*r+t.y});class Q{constructor(e,o){i(this,"graph");i(this,"viewport");i(this,"element",null);i(this,"prevTouches",null);i(this,"window",window);i(this,"wheelFinishTimer",null);i(this,"onMouseDown",e=>{this.element===null||!this.options.mouseDownEventVerifier(e)||(F(this.element,this.options.shiftCursor),this.window.addEventListener("mousemove",this.onWindowMouseMove),this.window.addEventListener("mouseup",this.onWindowMouseUp),this.options.onTransformStarted())});i(this,"onWindowMouseMove",e=>{if(this.element===null||!R(this.element,e.clientX,e.clientY)||!I(this.window,e.clientX,e.clientY)){this.stopMouseDrag();return}const o=-e.movementX,r=-e.movementY;this.moveViewport(this.element,o,r)});i(this,"onWindowMouseUp",e=>{this.element===null||!this.options.mouseUpEventVerifier(e)||this.stopMouseDrag()});i(this,"onWheelScroll",e=>{if(!this.options.mouseWheelEventVerifier(e))return;e.preventDefault();const{left:o,top:r}=this.element.getBoundingClientRect(),n=e.clientX-o,h=e.clientY-r,d=1/(e.deltaY<0?this.options.wheelSensitivity:1/this.options.wheelSensitivity);this.wheelFinishTimer===null&&this.options.onTransformStarted(),this.scaleViewport(this.element,d,n,h),this.wheelFinishTimer!==null&&clearTimeout(this.wheelFinishTimer),this.wheelFinishTimer=setTimeout(()=>{this.options.onTransformFinished(),this.wheelFinishTimer=null},this.options.scaleWheelFinishTimeout)});i(this,"onTouchStart",e=>{if(this.prevTouches!==null){this.prevTouches=U(e);return}this.prevTouches=U(e),this.window.addEventListener("touchmove",this.onWindowTouchMove),this.window.addEventListener("touchend",this.onWindowTouchFinish),this.window.addEventListener("touchcancel",this.onWindowTouchFinish),this.options.onTransformStarted()});i(this,"onWindowTouchMove",e=>{const o=this.element;if(o===null)return;const r=U(e);if(!r.touches.every(h=>R(o,h[0],h[1])&&I(this.window,h[0],h[1]))){this.stopTouchDrag();return}if((r.touchesCnt===1||r.touchesCnt===2)&&this.moveViewport(o,-(r.x-this.prevTouches.x),-(r.y-this.prevTouches.y)),r.touchesCnt===2){const{left:h,top:s}=o.getBoundingClientRect(),d=this.prevTouches.x-h,c=this.prevTouches.y-s,l=1/(r.scale/this.prevTouches.scale);this.scaleViewport(o,l,d,c)}this.prevTouches=r});i(this,"onWindowTouchFinish",e=>{e.touches.length>0?this.prevTouches=U(e):this.stopTouchDrag()});i(this,"observer",new ResizeObserver(()=>{const e=this.canvas.viewport.getViewportMatrix(),{width:o,height:r}=this.element.getBoundingClientRect(),n=this.options.transformPreprocessor({prevTransform:e,nextTransform:e,canvasWidth:o,canvasHeight:r});this.options.onResizeTransformStarted(),this.canvas.patchViewportMatrix(n),this.options.onResizeTransformFinished()}));i(this,"options");this.canvas=e,this.options=we(o),this.viewport=this.canvas.viewport,this.graph=this.canvas.graph}attach(e){this.detach(),this.element=e,this.observer.observe(this.element),this.element.addEventListener("mousedown",this.onMouseDown),this.element.addEventListener("wheel",this.onWheelScroll),this.element.addEventListener("touchstart",this.onTouchStart),this.canvas.attach(this.element)}detach(){this.canvas.detach(),this.element!==null&&(this.observer.unobserve(this.element),this.element.removeEventListener("mousedown",this.onMouseDown),this.element.removeEventListener("wheel",this.onWheelScroll),this.element.removeEventListener("touchstart",this.onTouchStart),this.element=null)}addNode(e){this.canvas.addNode(e)}updateNode(e,o){this.canvas.updateNode(e,o)}removeNode(e){this.canvas.removeNode(e)}markPort(e){this.canvas.markPort(e)}updatePort(e,o){this.canvas.updatePort(e,o)}unmarkPort(e){this.canvas.unmarkPort(e)}addEdge(e){this.canvas.addEdge(e)}updateEdge(e,o){this.canvas.updateEdge(e,o)}removeEdge(e){this.canvas.removeEdge(e)}patchViewportMatrix(e){this.canvas.patchViewportMatrix(e)}patchContentMatrix(e){this.canvas.patchContentMatrix(e)}clear(){this.canvas.clear()}destroy(){this.detach(),this.removeMouseDragListeners(),this.removeTouchDragListeners(),this.canvas.destroy()}moveViewport(e,o,r){const n=this.viewport.getViewportMatrix(),h=fe(n,o,r),{width:s,height:d}=e.getBoundingClientRect(),c=this.options.transformPreprocessor({prevTransform:n,nextTransform:h,canvasWidth:s,canvasHeight:d});this.performTransform(c)}scaleViewport(e,o,r,n){const h=this.canvas.viewport.getViewportMatrix(),s=ye(h,o,r,n),{width:d,height:c}=e.getBoundingClientRect(),a=this.options.transformPreprocessor({prevTransform:h,nextTransform:s,canvasWidth:d,canvasHeight:c});this.performTransform(a)}stopMouseDrag(){this.element!==null&&F(this.element,null),this.removeMouseDragListeners(),this.options.onTransformFinished()}removeMouseDragListeners(){this.window.removeEventListener("mousemove",this.onWindowMouseMove),this.window.removeEventListener("mouseup",this.onWindowMouseUp)}stopTouchDrag(){this.prevTouches=null,this.removeTouchDragListeners(),this.options.onTransformFinished()}removeTouchDragListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}performTransform(e){this.options.onBeforeTransformChange(),this.canvas.patchViewportMatrix(e),this.options.onTransformChange()}}class ve{constructor(){i(this,"keyMap",new Map);i(this,"valueMap",new Map)}set(e,o){this.keyMap.set(e,o),this.valueMap.set(o,e)}hasKey(e){return this.keyMap.has(e)}hasValue(e){return this.valueMap.has(e)}getByKey(e){return this.keyMap.get(e)}getByValue(e){return this.valueMap.get(e)}deleteByKey(e){const o=this.keyMap.get(e);o!==void 0&&this.valueMap.delete(o),this.keyMap.delete(e)}deleteByValue(e){const o=this.valueMap.get(e);o!==void 0&&this.keyMap.delete(o),this.valueMap.delete(e)}forEach(e){this.keyMap.forEach((o,r)=>{e(o,r)})}clear(){this.keyMap.clear(),this.valueMap.clear()}}class xe{constructor(e){i(this,"viewport");i(this,"graph");i(this,"nodes",new ve);i(this,"nodesResizeObserver");i(this,"window",window);this.canvas=e,this.nodesResizeObserver=new this.window.ResizeObserver(o=>{o.forEach(r=>{const n=r.target;this.handleNodeResize(n)})}),this.viewport=this.canvas.viewport,this.graph=this.canvas.graph}attach(e){this.canvas.attach(e)}detach(){this.canvas.detach()}addNode(e){this.canvas.addNode(e),this.nodes.set(e.id,e.element),this.nodesResizeObserver.observe(e.element)}updateNode(e,o){this.canvas.updateNode(e,o)}removeNode(e){this.canvas.removeNode(e);const o=this.nodes.getByKey(e);this.nodes.deleteByKey(e),this.nodesResizeObserver.unobserve(o)}markPort(e){this.canvas.markPort(e)}updatePort(e,o){this.canvas.updatePort(e,o)}unmarkPort(e){this.canvas.unmarkPort(e)}addEdge(e){this.canvas.addEdge(e)}updateEdge(e,o){this.canvas.updateEdge(e,o)}removeEdge(e){this.canvas.removeEdge(e)}patchViewportMatrix(e){this.canvas.patchViewportMatrix(e)}patchContentMatrix(e){this.canvas.patchContentMatrix(e)}clear(){this.canvas.clear(),this.nodesResizeObserver.disconnect(),this.nodes.clear()}destroy(){this.clear(),this.canvas.destroy()}handleNodeResize(e){const o=this.nodes.getByValue(e);this.canvas.updateNode(o,{}),this.graph.getNodeAdjacentEdgeIds(o).forEach(n=>{this.canvas.updateEdge(n,{})})}}class Ee{constructor(e,o,r,n){i(this,"graph");i(this,"viewport");i(this,"canvas");i(this,"element",null);i(this,"canvasResizeObserver");i(this,"window",window);i(this,"nodeHorizontal");i(this,"nodeVertical");i(this,"viewportWidth",0);i(this,"viewportHeight",0);i(this,"viewportMatrix");i(this,"loadedArea",{xFrom:1/0,xTo:1/0,yFrom:1/0,yTo:1/0});i(this,"updateLoadedArea",e=>{this.loadedArea={xFrom:e.x,xTo:e.x+e.width,yFrom:e.y,yTo:e.y+e.height}});var c,a;this.trigger=o,this.virtualScrollOptions=n,this.nodeHorizontal=this.virtualScrollOptions.nodeContainingRadius.horizontal,this.nodeVertical=this.virtualScrollOptions.nodeContainingRadius.vertical,this.canvasResizeObserver=new this.window.ResizeObserver(l=>{const g=l[0];this.viewportWidth=g.contentRect.width,this.viewportHeight=g.contentRect.height,this.scheduleLoadAreaAroundViewport()});const h=((c=r==null?void 0:r.events)==null?void 0:c.onTransformFinished)??(()=>{}),s=((a=r==null?void 0:r.events)==null?void 0:a.onTransformChange)??(()=>{}),d={...r,events:{...r==null?void 0:r.events,onTransformChange:()=>{const l=this.viewportMatrix;this.viewportMatrix=this.canvas.viewport.getViewportMatrix(),l.scale!==this.viewportMatrix.scale&&this.scheduleEnsureViewportAreaLoaded(),s()},onTransformFinished:()=>{this.scheduleLoadAreaAroundViewport(),h()}}};this.canvas=new Q(e,d),this.viewportMatrix=this.canvas.viewport.getViewportMatrix(),this.viewport=this.canvas.viewport,this.graph=this.canvas.graph,this.trigger.subscribe(this.updateLoadedArea)}attach(e){this.detach(),this.element=e,this.canvasResizeObserver.observe(this.element),this.canvas.attach(e)}detach(){this.element!==null&&(this.canvasResizeObserver.unobserve(this.element),this.element=null,this.viewportWidth=0,this.viewportHeight=0),this.canvas.detach()}addNode(e){this.canvas.addNode(e)}updateNode(e,o){this.canvas.updateNode(e,o)}removeNode(e){this.canvas.removeNode(e)}markPort(e){this.canvas.markPort(e)}updatePort(e,o){this.canvas.updatePort(e,o)}unmarkPort(e){this.canvas.unmarkPort(e)}addEdge(e){this.canvas.addEdge(e)}updateEdge(e,o){this.canvas.updateEdge(e,o)}removeEdge(e){this.canvas.removeEdge(e)}patchViewportMatrix(e){this.canvas.patchViewportMatrix(e),this.viewportMatrix=this.canvas.viewport.getViewportMatrix(),this.loadAreaAroundViewport()}patchContentMatrix(e){this.canvas.patchContentMatrix(e),this.viewportMatrix=this.canvas.viewport.getViewportMatrix(),this.loadAreaAroundViewport()}clear(){this.canvas.clear()}destroy(){this.trigger.unsubscribe(this.updateLoadedArea),this.canvas.destroy()}scheduleLoadAreaAroundViewport(){setTimeout(()=>{this.loadAreaAroundViewport()})}scheduleEnsureViewportAreaLoaded(){const e=this.viewportWidth*this.viewportMatrix.scale,o=this.viewportHeight*this.viewportMatrix.scale,r=this.viewportMatrix.x-this.nodeHorizontal,n=this.viewportMatrix.y-this.nodeVertical,h=this.viewportMatrix.x+e+this.nodeHorizontal,s=this.viewportMatrix.y+o+this.nodeVertical;this.loadedArea.xFrom<r&&this.loadedArea.xTo>h&&this.loadedArea.yFrom<n&&this.loadedArea.yTo>s||this.scheduleLoadAreaAroundViewport()}loadAreaAroundViewport(){const e=this.viewportWidth*this.viewportMatrix.scale,o=this.viewportHeight*this.viewportMatrix.scale,r=this.viewportMatrix.x-e-this.nodeHorizontal,n=this.viewportMatrix.y-o-this.nodeVertical,h=3*e+2*this.nodeHorizontal,s=3*o+2*this.nodeVertical;this.trigger.emit({x:r,y:n,width:h,height:s})}}const Ae=()=>{const t=document.createElement("div");return t.style.width="100%",t.style.height="100%",t.style.position="relative",t.style.overflow="hidden",t},Se=()=>{const t=document.createElement("div");return t.style.position="absolute",t.style.top="0",t.style.left="0",t.style.width="0",t.style.height="0",t},pe=()=>{const t=document.createElement("div");return t.style.position="absolute",t.style.top="0",t.style.left="0",t.style.visibility="hidden",t};class Te{constructor(e,o){i(this,"canvasWrapper",null);i(this,"host",Ae());i(this,"container",Se());i(this,"nodeIdToWrapperElementMap",new Map);i(this,"edgeIdToElementMap",new Map);i(this,"applyTransform",()=>{const e=this.viewportStore.getContentMatrix();this.container.style.transform=`matrix(${e.scale}, 0, 0, ${e.scale}, ${e.x}, ${e.y})`});this.graphStore=e,this.viewportStore=o,this.host.appendChild(this.container),this.viewportStore.onAfterUpdate.subscribe(this.applyTransform)}attach(e){this.detach(),this.canvasWrapper=e,this.canvasWrapper.appendChild(this.host),this.graphStore.getAllEdgeIds().forEach(o=>{this.renderEdge(o)})}detach(){this.canvasWrapper!==null&&(this.canvasWrapper.removeChild(this.host),this.canvasWrapper=null)}attachNode(e){const o=this.graphStore.getNode(e),r=pe();r.appendChild(o.element),this.container.appendChild(r),this.nodeIdToWrapperElementMap.set(e,r),this.updateNodeCoordinates(e),this.updateNodePriority(e),r.style.visibility="visible"}detachNode(e){const o=this.nodeIdToWrapperElementMap.get(e);o.removeChild(o.firstChild),this.container.removeChild(o),this.nodeIdToWrapperElementMap.delete(e)}attachEdge(e){const o=this.graphStore.getEdge(e).shape.svg;this.edgeIdToElementMap.set(e,o),this.container.appendChild(o),this.renderEdge(e),this.updateEdgePriority(e)}detachEdge(e){const o=this.edgeIdToElementMap.get(e);this.container.removeChild(o),this.edgeIdToElementMap.delete(e)}clear(){this.edgeIdToElementMap.forEach((e,o)=>{this.detachEdge(o)}),this.nodeIdToWrapperElementMap.forEach((e,o)=>{this.detachNode(o)})}destroy(){this.viewportStore.onAfterUpdate.unsubscribe(this.applyTransform),this.clear(),this.detach(),this.host.removeChild(this.container)}updateNodeCoordinates(e){const o=this.nodeIdToWrapperElementMap.get(e),r=this.graphStore.getNode(e),{width:n,height:h}=r.element.getBoundingClientRect(),s=this.viewportStore.getViewportMatrix().scale,d=r.centerFn(n,h),c=r.x-s*d.x,a=r.y-s*d.y;o.style.transform=`translate(${c}px, ${a}px)`}updateNodePriority(e){const o=this.graphStore.getNode(e),r=this.nodeIdToWrapperElementMap.get(e);r.style.zIndex=`${o.priority}`}updateEdgeShape(e){const o=this.edgeIdToElementMap.get(e);this.container.removeChild(o);const r=this.graphStore.getEdge(e);this.edgeIdToElementMap.set(e,r.shape.svg),this.container.appendChild(r.shape.svg)}renderEdge(e){const o=this.graphStore.getEdge(e),r=this.graphStore.getPort(o.from),n=this.graphStore.getPort(o.to),h=r.element.getBoundingClientRect(),s=n.element.getBoundingClientRect(),d=this.host.getBoundingClientRect(),c=this.viewportStore.getViewportMatrix(),a={x:c.scale*(h.left-d.left)+c.x,y:c.scale*(h.top-d.top)+c.y},l={x:c.scale*(s.left-d.left)+c.x,y:c.scale*(s.top-d.top)+c.y},g={x:a.x,y:a.y,width:h.width*c.scale,height:h.height*c.scale,direction:r.direction,portId:o.from,nodeId:r.nodeId},f={x:l.x,y:l.y,width:s.width*c.scale,height:s.height*c.scale,direction:n.direction,portId:o.to,nodeId:n.nodeId};o.shape.render({from:g,to:f})}updateEdgePriority(e){const o=this.graphStore.getEdge(e);o.shape.svg.style.zIndex=`${o.priority}`}}class Ne{constructor(e){i(this,"xFrom",1/0);i(this,"yFrom",1/0);i(this,"xTo",1/0);i(this,"yTo",1/0);this.graphStore=e}setRenderingBox(e){this.xFrom=e.x,this.xTo=e.x+e.width,this.yFrom=e.y,this.yTo=e.y+e.height}hasNode(e){const o=this.graphStore.getNode(e);return o.x>=this.xFrom&&o.x<=this.xTo&&o.y>=this.yFrom&&o.y<=this.yTo}hasEdge(e){const o=this.graphStore.getEdge(e),r=this.graphStore.getPort(o.from).nodeId,n=this.graphStore.getPort(o.to).nodeId,h=this.graphStore.getNode(r),s=this.graphStore.getNode(n),d=Math.min(h.x,s.x),c=Math.max(h.x,s.x),a=Math.min(h.y,s.y),l=Math.max(h.y,s.y);return d<=this.xTo&&c>=this.xFrom&&a<=this.yTo&&l>=this.yFrom}}class me{constructor(e,o,r){i(this,"attachedNodes",new Set);i(this,"attachedEdges",new Set);i(this,"renderingBox");i(this,"updateViewport",e=>{this.renderingBox.setRenderingBox(e);const o=new Set,r=new Set,n=new Set,h=new Set;this.graphStore.getAllNodeIds().forEach(s=>{const d=this.renderingBox.hasNode(s),c=this.attachedNodes.has(s);d&&!c?o.add(s):!d&&c&&r.add(s)}),this.graphStore.getAllEdgeIds().forEach(s=>{const d=this.renderingBox.hasEdge(s),c=this.attachedEdges.has(s),a=this.graphStore.getEdge(s),l=this.graphStore.getPort(a.from).nodeId,g=this.graphStore.getPort(a.to).nodeId;d&&(this.renderingBox.hasNode(l)||(o.add(l),r.delete(l)),this.renderingBox.hasNode(g)||(o.add(g),r.delete(g))),d&&!c?n.add(s):!d&&c&&h.add(s)}),h.forEach(s=>{this.handleDetachEdge(s)}),r.forEach(s=>{this.handleDetachNode(s)}),o.forEach(s=>{this.attachedNodes.has(s)||this.handleAttachNode(s)}),n.forEach(s=>{this.handleAttachEdge(s)})});this.htmlView=e,this.graphStore=o,this.trigger=r,this.renderingBox=new Ne(this.graphStore),this.trigger.subscribe(this.updateViewport)}attach(e){this.htmlView.attach(e)}detach(){this.htmlView.detach()}attachNode(e){this.renderingBox.hasNode(e)&&this.handleAttachNode(e)}detachNode(e){this.attachedNodes.has(e)&&this.handleDetachNode(e)}attachEdge(e){this.renderingBox.hasEdge(e)&&this.attachEdgeEntities(e)}detachEdge(e){this.attachedEdges.has(e)&&this.handleDetachEdge(e)}updateNodeCoordinates(e){this.attachedNodes.has(e)?this.htmlView.updateNodeCoordinates(e):this.renderingBox.hasNode(e)&&(this.handleAttachNode(e),this.graphStore.getNodeAdjacentEdgeIds(e).forEach(o=>{this.attachEdgeEntities(o)}))}updateNodePriority(e){this.attachedNodes.has(e)&&this.htmlView.updateNodePriority(e)}updateEdgeShape(e){this.attachedEdges.has(e)&&this.htmlView.updateEdgeShape(e)}renderEdge(e){this.attachedEdges.has(e)&&this.htmlView.renderEdge(e)}updateEdgePriority(e){this.attachedEdges.has(e)&&this.htmlView.updateEdgePriority(e)}clear(){this.htmlView.clear(),this.attachedNodes.clear(),this.attachedEdges.clear()}destroy(){this.clear(),this.htmlView.destroy(),this.trigger.unsubscribe(this.updateViewport)}attachEdgeEntities(e){const o=this.graphStore.getEdge(e),r=this.graphStore.getPort(o.from).nodeId,n=this.graphStore.getPort(o.to).nodeId;this.attachedNodes.has(r)||this.handleAttachNode(r),this.attachedNodes.has(n)||this.handleAttachNode(n),this.handleAttachEdge(e)}handleAttachNode(e){this.attachedNodes.add(e),this.htmlView.attachNode(e)}handleDetachNode(e){this.htmlView.detachNode(e),this.attachedNodes.delete(e)}handleAttachEdge(e){this.attachedEdges.add(e),this.htmlView.attachEdge(e)}handleDetachEdge(e){this.htmlView.detachEdge(e),this.attachedEdges.delete(e)}}class X{constructor(){i(this,"callbacks",new Set)}subscribe(e){this.callbacks.add(e)}unsubscribe(e){this.callbacks.delete(e)}emit(e){this.callbacks.forEach(o=>{o(e)})}}const A=()=>{const t=new X;return[t,t]};class j{constructor(e){i(this,"counter",0);this.checkExists=e}create(e){if(e!==void 0)return e;for(;this.checkExists(this.counter);)this.counter++;return this.counter}reset(){this.counter=0}}class p extends Error{constructor(){super(...arguments);i(this,"name","HtmlGraphError")}}const Me=(t,e)=>({x:t/2,y:e/2}),H=t=>()=>t,Z=H(0),Pe=()=>{let t=0;return()=>t++},Ve=(t,e)=>{let o=Z,r=Z;const n=Pe();return t==="incremental"&&(o=n),e==="incremental"&&(r=n),typeof t=="number"&&(o=H(t)),typeof e=="number"&&(r=H(e)),typeof t=="function"&&(o=t),typeof e=="function"&&(r=e),{nodesPriorityFn:o,edgesPriorityFn:r}},v=(t,e,o)=>({x:e.x*t.x-e.y*t.y+((1-e.x)*o.x+e.y*o.y),y:e.y*t.x+e.x*t.y+((1-e.x)*o.y-e.y*o.x)}),M=(t,e,o)=>({x:e*Math.cos(t),y:o*Math.sin(t)}),u={x:0,y:0},P=(t,e,o,r)=>{const h=[u,{x:o,y:r},{x:o,y:-r}].map(a=>v(a,t,u)).map(a=>({x:a.x+e.x,y:a.y+e.y})),s=`M ${h[0].x} ${h[0].y}`,d=`L ${h[1].x} ${h[1].y}`,c=`L ${h[2].x} ${h[2].y}`;return`${s} ${d} ${c}`},L=(t,e)=>{const o=[];if(t.length>0&&o.push(`M ${t[0].x} ${t[0].y}`),t.length===2&&o.push(`L ${t[1].x} ${t[1].y}`),t.length>2){const r=t.length-1;let n=0,h=0,s=0;t.forEach((d,c)=>{let a=0,l=0,g=0;const f=c>0,x=c<r,y=f&&x;if(f&&(a=-n,l=-h,g=s),x){const W=t[c+1];n=W.x-d.x,h=W.y-d.y,s=Math.sqrt(n*n+h*h)}const T=s!==0?Math.min((y?e:0)/s,c<r-1?.5:1):0,m=y?{x:d.x+n*T,y:d.y+h*T}:d,b=g!==0?Math.min((y?e:0)/g,c>1?.5:1):0,$=y?{x:d.x+a*b,y:d.y+l*b}:d;c>0&&o.push(`L ${$.x} ${$.y}`),y&&o.push(`C ${d.x} ${d.y} ${d.x} ${d.y} ${m.x} ${m.y}`)})}return o.join(" ")},B=()=>{const t=document.createElementNS("http://www.w3.org/2000/svg","svg");return t.style.pointerEvents="none",t.style.position="absolute",t.style.top="0",t.style.left="0",t.style.overflow="visible",t},k=()=>{const t=document.createElementNS("http://www.w3.org/2000/svg","g");return t.style.transformOrigin="50% 50%",t},z=(t,e)=>{const o=document.createElementNS("http://www.w3.org/2000/svg","path");return o.setAttribute("stroke",t),o.setAttribute("stroke-width",`${e}`),o.setAttribute("fill","none"),o},V=t=>{const e=document.createElementNS("http://www.w3.org/2000/svg","path");return e.setAttribute("fill",t),e},Y=(t,e)=>{const o={x:t.x+t.width/2,y:t.y+t.height/2},r={x:e.x+e.width/2,y:e.y+e.height/2},n=Math.min(o.x,r.x),h=Math.min(o.y,r.y),s=Math.abs(r.x-o.x),d=Math.abs(r.y-o.y),c=o.x<=r.x?1:-1,a=o.y<=r.y?1:-1;return{x:n,y:h,width:s,height:d,flipX:c,flipY:a}},De=t=>{const e=v({x:t.arrowLength,y:u.y},t.fromVect,u),o=v({x:t.to.x-t.arrowLength,y:t.to.y},t.toVect,t.to),r={x:e.x+t.fromVect.x*t.curvature,y:e.y+t.fromVect.y*t.curvature},n={x:o.x-t.toVect.x*t.curvature,y:o.y-t.toVect.y*t.curvature},h=`M ${e.x} ${e.y} C ${r.x} ${r.y}, ${n.x} ${n.y}, ${o.x} ${o.y}`,s=t.hasSourceArrow?"":`M ${u.x} ${u.y} L ${e.x} ${e.y} `,d=t.hasTargetArrow?"":` M ${o.x} ${o.y} L ${t.to.x} ${t.to.y}`;return`${s}${h}${d}`},Ce=t=>{const e=t.hasSourceArrow?v({x:t.arrowLength,y:u.y},t.fromVect,u):u,o=t.hasTargetArrow?v({x:t.to.x-t.arrowLength,y:t.to.y},t.toVect,t.to):t.to,r=t.arrowLength,n=Math.cos(t.detourDirection)*t.detourDistance,h=Math.sin(t.detourDirection)*t.detourDistance,s=n*t.flipX,d=h*t.flipY,c=v({x:r,y:u.y},t.fromVect,u),a={x:c.x+s,y:c.y+d},l=v({x:t.to.x-r,y:t.to.y},t.toVect,t.to),g={x:l.x+s,y:l.y+d},f={x:(a.x+g.x)/2,y:(a.y+g.y)/2},x={x:c.x+t.curvature*t.fromVect.x,y:c.y+t.curvature*t.fromVect.y},y={x:l.x-t.curvature*t.toVect.x,y:l.y-t.curvature*t.toVect.y},N={x:c.x+s,y:c.y+d},T={x:l.x+s,y:l.y+d};return[`M ${e.x} ${e.y}`,`L ${c.x} ${c.y}`,`C ${x.x} ${x.y} ${N.x} ${N.y} ${f.x} ${f.y}`,`C ${T.x} ${T.y} ${y.x} ${y.y} ${l.x} ${l.y}`,`L ${o.x} ${o.y}`].join(" ")},Le=t=>{const e=t.hasSourceArrow?v({x:t.arrowLength,y:u.y},t.fromVect,u):u,o=t.hasTargetArrow?v({x:t.to.x-t.arrowLength,y:t.to.y},t.toVect,t.to):t.to,r=t.arrowLength+t.arrowOffset,n=r-t.roundness,h=v({x:n,y:u.y},t.fromVect,u),s=v({x:t.to.x-n,y:t.to.y},t.toVect,t.to),d=Math.max((h.x+s.x)/2,r),c=t.to.y/2,a={x:t.flipX>0?d:-r,y:h.y},l={x:a.x,y:c},g={x:t.flipX>0?t.to.x-d:t.to.x+r,y:s.y},f={x:g.x,y:c};return L([e,h,a,l,f,g,s,o],t.roundness)},G=t=>{const e=t.hasSourceArrow?v({x:t.arrowLength,y:u.y},t.fromVect,u):u,o=t.hasTargetArrow?v({x:t.to.x-t.arrowLength,y:t.to.y},t.toVect,t.to):t.to,r=t.arrowLength+t.arrowOffset,n=v({x:r,y:u.y},t.fromVect,u),h=Math.cos(t.detourDirection)*t.detourDistance,s=Math.sin(t.detourDirection)*t.detourDistance,d=h*t.flipX,c=s*t.flipY,a={x:n.x+d,y:n.y+c},l=v({x:t.to.x-r,y:t.to.y},t.toVect,t.to),g={x:l.x+d,y:l.y+c};return L([e,n,a,g,l,o],t.roundness)},be=t=>{const e=t.hasSourceArrow?v({x:t.arrowLength,y:u.y},t.fromVect,u):u,o=t.hasTargetArrow?v({x:t.to.x-t.arrowLength,y:t.to.y},t.toVect,t.to):t.to,r=t.arrowLength+t.arrowOffset,n=v({x:r,y:u.y},t.fromVect,u),h=v({x:t.to.x-r,y:t.to.y},t.toVect,t.to);return L([e,n,h,o],t.roundness)},$e=t=>{const e=t.hasSourceArrow?v({x:t.arrowLength,y:u.y},t.fromVect,u):u,o=t.hasTargetArrow?v({x:t.to.x-t.arrowLength,y:t.to.y},t.toVect,t.to):t.to,r=t.arrowLength+t.arrowOffset,n=r-t.roundness,h=v({x:n,y:u.y},t.fromVect,u),s=v({x:t.to.x-n,y:t.to.y},t.toVect,t.to),d=Math.max((h.y+s.y)/2,r),c=t.to.x/2,a={x:h.x,y:t.flipY>0?d:-r},l={x:c,y:a.y},g={x:s.x,y:t.flipY>0?t.to.y-d:t.to.y+r},f={x:c,y:g.y};return L([e,h,a,l,f,g,s,o],t.roundness)},K=t=>{const e=t.arrowOffset,o=t.side,r=t.arrowLength+e,n=r+2*o,s=[{x:t.arrowLength,y:u.y},{x:r,y:u.y},{x:r,y:t.side},{x:n,y:t.side},{x:n,y:-t.side},{x:r,y:-t.side},{x:r,y:u.y},{x:t.arrowLength,y:u.y}].map(c=>v(c,t.fromVect,u)),d=`M ${u.x} ${u.y} L ${s[0].x} ${s[0].y} `;return`${t.hasSourceArrow||t.hasTargetArrow?"":d}${L(s,t.roundness)}`},We=t=>{const e=t.smallRadius,o=t.radius,r=Math.sqrt(e*e+o*o),n=e+o,h=t.arrowLength+r*(1-o/n),s=e*o/n,c=[{x:t.arrowLength,y:u.y},{x:h,y:s},{x:h,y:-s}].map(g=>v(g,t.fromVect,u)),a=[`M ${c[0].x} ${c[0].y}`,`A ${e} ${e} 0 0 1 ${c[1].x} ${c[1].y}`,`A ${o} ${o} 0 1 0 ${c[2].x} ${c[2].y}`,`A ${e} ${e} 0 0 1 ${c[0].x} ${c[0].y}`].join(" "),l=`M 0 0 L ${c[0].x} ${c[0].y} `;return`${t.hasSourceArrow||t.hasTargetArrow?"":l}${a}`},w=Object.freeze({color:"#777777",width:1,arrowLength:15,arrowWidth:4,arrowOffset:15,hasSourceArrow:!1,hasTargetArrow:!1,cycleRadius:30,cycleSquareSide:30,roundness:10,detourDistance:100,detourDirection:-Math.PI/2,detourDirectionVertical:0,smallCycleRadius:15,curvature:90});class O{constructor(e){i(this,"svg",B());i(this,"group",k());i(this,"line");i(this,"sourceArrow",null);i(this,"targetArrow",null);i(this,"arrowLength");i(this,"arrowWidth");i(this,"curvature");i(this,"portCycleRadius");i(this,"portCycleSmallRadius");i(this,"detourDirection");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");this.arrowLength=(e==null?void 0:e.arrowLength)??w.arrowLength,this.arrowWidth=(e==null?void 0:e.arrowWidth)??w.arrowWidth,this.curvature=(e==null?void 0:e.curvature)??w.curvature,this.portCycleRadius=(e==null?void 0:e.cycleRadius)??w.cycleRadius,this.portCycleSmallRadius=(e==null?void 0:e.smallCycleRadius)??w.smallCycleRadius,this.detourDirection=(e==null?void 0:e.detourDirection)??w.detourDirection,this.detourDistance=(e==null?void 0:e.detourDistance)??w.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??w.hasTargetArrow;const o=(e==null?void 0:e.color)??w.color,r=(e==null?void 0:e.width)??w.width;this.svg.appendChild(this.group),this.line=z(o,r),this.group.appendChild(this.line),this.hasSourceArrow&&(this.sourceArrow=V(o),this.group.appendChild(this.sourceArrow)),this.hasTargetArrow&&(this.targetArrow=V(o),this.group.appendChild(this.targetArrow))}render(e){const{x:o,y:r,width:n,height:h,flipX:s,flipY:d}=Y(e.from,e.to);this.svg.style.transform=`translate(${o}px, ${r}px)`,this.svg.style.width=`${n}px`,this.svg.style.height=`${h}px`,this.group.style.transform=`scale(${s}, ${d})`;const c=M(e.from.direction,s,d),a=M(e.to.direction,s,d),l={x:n,y:h};let g,f=a,x=-this.arrowLength;if(e.from.portId===e.to.portId?(g=We({fromVect:c,radius:this.portCycleRadius,smallRadius:this.portCycleSmallRadius,arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),f=c,x=this.arrowLength):e.from.nodeId===e.to.nodeId?g=Ce({to:l,fromVect:c,toVect:a,flipX:s,flipY:d,arrowLength:this.arrowLength,detourDirection:this.detourDirection,detourDistance:this.detourDistance,curvature:this.curvature,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}):g=De({to:l,fromVect:c,toVect:a,arrowLength:this.arrowLength,curvature:this.curvature,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),this.line.setAttribute("d",g),this.sourceArrow){const y=P(c,u,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",y)}if(this.targetArrow){const y=P(f,l,x,this.arrowWidth);this.targetArrow.setAttribute("d",y)}}}class q{constructor(e){i(this,"svg",B());i(this,"group",k());i(this,"line");i(this,"sourceArrow",null);i(this,"targetArrow",null);i(this,"arrowLength");i(this,"arrowWidth");i(this,"arrowOffset");i(this,"roundness");i(this,"cycleSquareSide");i(this,"detourDirection");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");this.arrowLength=(e==null?void 0:e.arrowLength)??w.arrowLength,this.arrowWidth=(e==null?void 0:e.arrowWidth)??w.arrowWidth,this.arrowOffset=(e==null?void 0:e.arrowOffset)??w.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??w.cycleSquareSide;const o=(e==null?void 0:e.roundness)??w.roundness;this.roundness=Math.min(o,this.arrowOffset,this.cycleSquareSide/2),this.detourDirection=(e==null?void 0:e.detourDirection)??w.detourDirection,this.detourDistance=(e==null?void 0:e.detourDistance)??w.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??w.hasTargetArrow;const r=(e==null?void 0:e.color)??w.color,n=(e==null?void 0:e.width)??w.width;this.svg.appendChild(this.group),this.line=z(r,n),this.group.appendChild(this.line),this.hasSourceArrow&&(this.sourceArrow=V(r),this.group.appendChild(this.sourceArrow)),this.hasTargetArrow&&(this.targetArrow=V(r),this.group.appendChild(this.targetArrow))}render(e){const{x:o,y:r,width:n,height:h,flipX:s,flipY:d}=Y(e.from,e.to);this.svg.style.transform=`translate(${o}px, ${r}px)`,this.svg.style.width=`${n}px`,this.svg.style.height=`${h}px`,this.group.style.transform=`scale(${s}, ${d})`;const c=M(e.from.direction,s,d),a=M(e.to.direction,s,d),l={x:n,y:h};let g,f=a,x=-this.arrowLength;if(e.from.portId===e.to.portId?(g=K({fromVect:c,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),f=c,x=this.arrowLength):e.from.nodeId===e.to.nodeId?g=G({to:l,fromVect:c,toVect:a,flipX:s,flipY:d,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDirection:this.detourDirection,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}):g=Le({to:l,fromVect:c,toVect:a,flipX:s,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),this.line.setAttribute("d",g),this.sourceArrow){const y=P(c,u,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",y)}if(this.targetArrow){const y=P(f,l,x,this.arrowWidth);this.targetArrow.setAttribute("d",y)}}}class ee{constructor(e){i(this,"svg",B());i(this,"group",k());i(this,"line");i(this,"sourceArrow",null);i(this,"targetArrow",null);i(this,"arrowLength");i(this,"arrowWidth");i(this,"arrowOffset");i(this,"roundness");i(this,"cycleSquareSide");i(this,"detourDirection");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");this.arrowLength=(e==null?void 0:e.arrowLength)??w.arrowLength,this.arrowWidth=(e==null?void 0:e.arrowWidth)??w.arrowWidth,this.arrowOffset=(e==null?void 0:e.arrowOffset)??w.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??w.cycleSquareSide;const o=(e==null?void 0:e.roundness)??w.roundness;this.roundness=Math.min(o,this.arrowOffset,this.cycleSquareSide/2),this.detourDirection=(e==null?void 0:e.detourDirection)??w.detourDirection,this.detourDistance=(e==null?void 0:e.detourDistance)??w.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??w.hasTargetArrow;const r=(e==null?void 0:e.color)??w.color,n=(e==null?void 0:e.width)??w.width;this.svg.appendChild(this.group),this.line=z(r,n),this.group.appendChild(this.line),this.hasSourceArrow&&(this.sourceArrow=V(r),this.group.appendChild(this.sourceArrow)),this.hasTargetArrow&&(this.targetArrow=V(r),this.group.appendChild(this.targetArrow))}render(e){const{x:o,y:r,width:n,height:h,flipX:s,flipY:d}=Y(e.from,e.to);this.svg.style.transform=`translate(${o}px, ${r}px)`,this.svg.style.width=`${n}px`,this.svg.style.height=`${h}px`,this.group.style.transform=`scale(${s}, ${d})`;const c=M(e.from.direction,s,d),a=M(e.to.direction,s,d),l={x:n,y:h};let g,f=a,x=-this.arrowLength;if(e.from.portId===e.to.portId?(g=K({fromVect:c,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),f=c,x=this.arrowLength):e.from.nodeId===e.to.nodeId?g=G({to:l,fromVect:c,toVect:a,flipX:s,flipY:d,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDirection:this.detourDirection,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}):g=be({to:l,fromVect:c,toVect:a,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),this.line.setAttribute("d",g),this.sourceArrow){const y=P(c,u,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",y)}if(this.targetArrow){const y=P(f,l,x,this.arrowWidth);this.targetArrow.setAttribute("d",y)}}}class te{constructor(e){i(this,"svg",B());i(this,"group",k());i(this,"line");i(this,"sourceArrow",null);i(this,"targetArrow",null);i(this,"arrowLength");i(this,"arrowWidth");i(this,"arrowOffset");i(this,"roundness");i(this,"cycleSquareSide");i(this,"detourDirection");i(this,"detourDistance");i(this,"hasSourceArrow");i(this,"hasTargetArrow");this.arrowLength=(e==null?void 0:e.arrowLength)??w.arrowLength,this.arrowWidth=(e==null?void 0:e.arrowWidth)??w.arrowWidth,this.arrowOffset=(e==null?void 0:e.arrowOffset)??w.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??w.cycleSquareSide;const o=(e==null?void 0:e.roundness)??w.roundness;this.roundness=Math.min(o,this.arrowOffset,this.cycleSquareSide/2),this.detourDirection=(e==null?void 0:e.detourDirection)??w.detourDirectionVertical,this.detourDistance=(e==null?void 0:e.detourDistance)??w.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??w.hasTargetArrow;const r=(e==null?void 0:e.color)??w.color,n=(e==null?void 0:e.width)??w.width;this.svg.appendChild(this.group),this.line=z(r,n),this.group.appendChild(this.line),this.hasSourceArrow&&(this.sourceArrow=V(r),this.group.appendChild(this.sourceArrow)),this.hasTargetArrow&&(this.targetArrow=V(r),this.group.appendChild(this.targetArrow))}render(e){const{x:o,y:r,width:n,height:h,flipX:s,flipY:d}=Y(e.from,e.to);this.svg.style.transform=`translate(${o}px, ${r}px)`,this.svg.style.width=`${n}px`,this.svg.style.height=`${h}px`,this.group.style.transform=`scale(${s}, ${d})`;const c=M(e.from.direction,s,d),a=M(e.to.direction,s,d),l={x:n,y:h};let g,f=a,x=-this.arrowLength;if(e.from.portId===e.to.portId?(g=K({fromVect:c,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),f=c,x=this.arrowLength):e.from.nodeId===e.to.nodeId?g=G({to:l,fromVect:c,toVect:a,flipX:s,flipY:d,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDirection:this.detourDirection,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}):g=$e({to:l,fromVect:c,toVect:a,flipY:d,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}),this.line.setAttribute("d",g),this.sourceArrow){const y=P(c,u,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",y)}if(this.targetArrow){const y=P(f,l,x,this.arrowWidth);this.targetArrow.setAttribute("d",y)}}}const Re=t=>{if(typeof t=="function")return t;switch(t==null?void 0:t.type){case"straight":return()=>new ee({color:t.color,width:t.width,arrowLength:t.arrowLength,arrowWidth:t.arrowWidth,arrowOffset:t.arrowOffset,hasSourceArrow:t.hasSourceArrow,hasTargetArrow:t.hasTargetArrow,cycleSquareSide:t.cycleSquareSide,roundness:t.roundness,detourDistance:t.detourDistance,detourDirection:t.detourDirection});case"horizontal":return()=>new q({color:t.color,width:t.width,arrowLength:t.arrowLength,arrowWidth:t.arrowWidth,arrowOffset:t.arrowOffset,hasSourceArrow:t.hasSourceArrow,hasTargetArrow:t.hasTargetArrow,cycleSquareSide:t.cycleSquareSide,roundness:t.roundness,detourDistance:t.detourDistance,detourDirection:t.detourDirection});case"vertical":return()=>new te({color:t.color,width:t.width,arrowLength:t.arrowLength,arrowWidth:t.arrowWidth,arrowOffset:t.arrowOffset,hasSourceArrow:t.hasSourceArrow,hasTargetArrow:t.hasTargetArrow,cycleSquareSide:t.cycleSquareSide,roundness:t.roundness,detourDistance:t.detourDistance,detourDirection:t.detourDirection});default:return()=>new O({color:t.color,width:t.width,arrowLength:t.arrowLength,arrowWidth:t.arrowWidth,hasSourceArrow:t.hasSourceArrow,hasTargetArrow:t.hasTargetArrow,cycleRadius:t.cycleRadius,smallCycleRadius:t.smallCycleRadius,curvature:t.curvature,detourDistance:t.detourDistance,detourDirection:t.detourDirection})}},Ie=t=>{var o,r,n,h,s;const e=Ve((o=t==null?void 0:t.nodes)==null?void 0:o.priority,(r=t==null?void 0:t.edges)==null?void 0:r.priority);return{nodes:{centerFn:((n=t==null?void 0:t.nodes)==null?void 0:n.centerFn)??Me,priorityFn:e.nodesPriorityFn},ports:{direction:((h=t==null?void 0:t.ports)==null?void 0:h.direction)??0},edges:{shapeFactory:Re(((s=t==null?void 0:t.edges)==null?void 0:s.shape)??{}),priorityFn:e.edgesPriorityFn}}};class Fe{constructor(e,o){i(this,"nodeIdGenerator",new j(e=>this.graph.getNode(e)!==null));i(this,"portIdGenerator",new j(e=>this.graph.getPort(e)!==null));i(this,"edgeIdGenerator",new j(e=>this.graph.getEdge(e)!==null));i(this,"defaults");i(this,"graph");i(this,"viewport");this.controller=e,this.defaults=Ie(o),this.graph=e.graph,this.viewport=e.viewport}attach(e){return this.controller.attach(e),this}detach(){return this.controller.detach(),this}addNode(e){const o=this.nodeIdGenerator.create(e.id);if(this.graph.getNode(o)!==null)throw new p("failed to add node with existing id");if(this.controller.addNode({id:o,element:e.element,x:e.x,y:e.y,centerFn:e.centerFn??this.defaults.nodes.centerFn,priority:e.priority??this.defaults.nodes.priorityFn()}),e.ports!==void 0)for(const r of e.ports)this.markPort({id:r.id,element:r.element,nodeId:o,direction:r.direction});return this}updateNode(e,o){if(this.graph.getNode(e)===null)throw new p("failed to update nonexisting node");return this.controller.updateNode(e,o??{}),this}removeNode(e){if(this.graph.getNode(e)===null)throw new p("failed to remove nonexisting node");return this.controller.removeNode(e),this}markPort(e){const o=this.portIdGenerator.create(e.id);if(this.graph.getPort(o)!==null)throw new p("failed to add port with existing id");if(this.graph.getNode(e.nodeId)===null)throw new p("failed to set port on nonexisting node");return this.controller.markPort({id:o,element:e.element,nodeId:e.nodeId,direction:e.direction??this.defaults.ports.direction}),this}updatePort(e,o){if(this.graph.getPort(e)===null)throw new p("failed to unset nonexisting port");return this.controller.updatePort(e,o??{}),this}unmarkPort(e){if(this.graph.getPort(e)===null)throw new p("failed to unset nonexisting port");return this.controller.unmarkPort(e),this}addEdge(e){const o=this.edgeIdGenerator.create(e.id);if(this.graph.getEdge(o)!==null)throw new p("failed to add edge with existing id");if(this.graph.getPort(e.from)===null)throw new p("failed to add edge from nonexisting port");if(this.graph.getPort(e.to)===null)throw new p("failed to add edge to nonexisting port");return this.controller.addEdge({id:o,from:e.from,to:e.to,shape:e.shape??this.defaults.edges.shapeFactory(),priority:e.priority??this.defaults.edges.priorityFn()}),this}updateEdge(e,o){if(this.graph.getEdge(e)===null)throw new p("failed to update nonexisting edge");return this.controller.updateEdge(e,o??{}),this}removeEdge(e){if(this.graph.getEdge(e)===null)throw new p("failed to remove nonexisting edge");return this.controller.removeEdge(e),this}patchViewportMatrix(e){return this.controller.patchViewportMatrix(e),this}patchContentMatrix(e){return this.controller.patchContentMatrix(e),this}clear(){return this.controller.clear(),this.nodeIdGenerator.reset(),this.portIdGenerator.reset(),this.edgeIdGenerator.reset(),this}destroy(){this.controller.destroy()}}class Ue{constructor(){i(this,"nodes",new Map);i(this,"ports",new Map);i(this,"edges",new Map);i(this,"incommingEdges",new Map);i(this,"outcommingEdges",new Map);i(this,"cycleEdges",new Map);i(this,"onAfterNodeAddedEmitter");i(this,"onAfterNodeAdded");i(this,"onAfterNodeCoordinatesUpdatedEmitter");i(this,"onAfterNodeCoordinatesUpdated");i(this,"onAfterNodePriorityUpdatedEmitter");i(this,"onAfterNodePriorityUpdated");i(this,"onBeforeNodeRemovedEmitter");i(this,"onBeforeNodeRemoved");i(this,"onAfterPortAddedEmitter");i(this,"onAfterPortAdded");i(this,"onAfterPortDirectionUpdatedEmitter");i(this,"onAfterPortDirectionUpdated");i(this,"onBeforePortRemovedEmitter");i(this,"onBeforePortRemoved");i(this,"onAfterEdgeAddedEmitter");i(this,"onAfterEdgeAdded");i(this,"onAfterEdgeShapeUpdatedEmitter");i(this,"onAfterEdgeShapeUpdated");i(this,"onAfterEdgeAdjacentPortsUpdatedEmitter");i(this,"onAfterEdgeAdjacentPortsUpdated");i(this,"onAfterEdgePriorityUpdatedEmitter");i(this,"onAfterEdgePriorityUpdated");i(this,"onBeforeEdgeRemovedEmitter");i(this,"onBeforeEdgeRemoved");i(this,"onBeforeClearEmitter");i(this,"onBeforeClear");[this.onAfterNodeAddedEmitter,this.onAfterNodeAdded]=A(),[this.onAfterNodeCoordinatesUpdatedEmitter,this.onAfterNodeCoordinatesUpdated]=A(),[this.onAfterNodePriorityUpdatedEmitter,this.onAfterNodePriorityUpdated]=A(),[this.onBeforeNodeRemovedEmitter,this.onBeforeNodeRemoved]=A(),[this.onAfterPortAddedEmitter,this.onAfterPortAdded]=A(),[this.onAfterPortDirectionUpdatedEmitter,this.onAfterPortDirectionUpdated]=A(),[this.onBeforePortRemovedEmitter,this.onBeforePortRemoved]=A(),[this.onAfterEdgeAddedEmitter,this.onAfterEdgeAdded]=A(),[this.onAfterEdgeShapeUpdatedEmitter,this.onAfterEdgeShapeUpdated]=A(),[this.onAfterEdgeAdjacentPortsUpdatedEmitter,this.onAfterEdgeAdjacentPortsUpdated]=A(),[this.onAfterEdgePriorityUpdatedEmitter,this.onAfterEdgePriorityUpdated]=A(),[this.onBeforeEdgeRemovedEmitter,this.onBeforeEdgeRemoved]=A(),[this.onBeforeClearEmitter,this.onBeforeClear]=A()}addNode(e){const o=new Map,r={element:e.element,x:e.x,y:e.y,centerFn:e.centerFn,priority:e.priority,ports:o};this.nodes.set(e.id,r),this.onAfterNodeAddedEmitter.emit(e.id)}getAllNodeIds(){return Array.from(this.nodes.keys())}getNode(e){return this.nodes.get(e)}updateNodeCoordinates(e,o){const r=this.nodes.get(e);r.x=(o==null?void 0:o.x)??r.x,r.y=(o==null?void 0:o.y)??r.y,r.centerFn=o.centerFn??r.centerFn,this.onAfterNodeCoordinatesUpdatedEmitter.emit(e)}updateNodePriority(e,o){const r=this.nodes.get(e);r.priority=o,this.onAfterNodePriorityUpdatedEmitter.emit(e)}removeNode(e){this.onBeforeNodeRemovedEmitter.emit(e),this.nodes.delete(e)}addPort(e){this.ports.set(e.id,{element:e.element,direction:e.direction,nodeId:e.nodeId}),this.cycleEdges.set(e.id,new Set),this.incommingEdges.set(e.id,new Set),this.outcommingEdges.set(e.id,new Set),this.nodes.get(e.nodeId).ports.set(e.id,e.element),this.onAfterPortAddedEmitter.emit(e.id)}getPort(e){return this.ports.get(e)}updatePortDirection(e,o){const r=this.ports.get(e);r.direction=o,this.onAfterPortDirectionUpdatedEmitter.emit(e)}getAllPortIds(){return Array.from(this.ports.keys())}getNodePortIds(e){const o=this.nodes.get(e);if(o!==void 0)return Array.from(o.ports.keys())}removePort(e){const o=this.ports.get(e).nodeId;this.onBeforePortRemovedEmitter.emit(e),this.nodes.get(o).ports.delete(e),this.ports.delete(e)}addEdge(e){this.addEdgeInternal(e),this.onAfterEdgeAddedEmitter.emit(e.id)}updateEdgeShape(e,o){const r=this.edges.get(e);r.shape=o,this.onAfterEdgeShapeUpdatedEmitter.emit(e)}updateEdgeAdjacentPorts(e,o){const r=this.edges.get(e);this.removeEdgeInternal(e),this.addEdgeInternal({id:e,from:o.from??r.from,to:o.to??r.to,shape:r.shape,priority:r.priority}),this.onAfterEdgeAdjacentPortsUpdatedEmitter.emit(e)}updateEdgePriority(e,o){const r=this.edges.get(e);r.priority=o,this.onAfterEdgePriorityUpdatedEmitter.emit(e)}getAllEdgeIds(){return Array.from(this.edges.keys())}getEdge(e){return this.edges.get(e)}removeEdge(e){this.onBeforeEdgeRemovedEmitter.emit(e),this.removeEdgeInternal(e)}clear(){this.onBeforeClearEmitter.emit(),this.incommingEdges.clear(),this.outcommingEdges.clear(),this.cycleEdges.clear(),this.edges.clear(),this.ports.clear(),this.nodes.clear()}getPortIncomingEdgeIds(e){return Array.from(this.incommingEdges.get(e))}getPortOutcomingEdgeIds(e){return Array.from(this.outcommingEdges.get(e))}getPortCycleEdgeIds(e){return Array.from(this.cycleEdges.get(e))}getPortAdjacentEdgeIds(e){return[...this.getPortIncomingEdgeIds(e),...this.getPortOutcomingEdgeIds(e),...this.getPortCycleEdgeIds(e)]}getNodeIncomingEdgeIds(e){const o=Array.from(this.nodes.get(e).ports.keys());let r=[];return o.forEach(n=>{r=[...r,...this.getPortIncomingEdgeIds(n)]}),r}getNodeOutcomingEdgeIds(e){const o=Array.from(this.nodes.get(e).ports.keys());let r=[];return o.forEach(n=>{r=[...r,...this.getPortOutcomingEdgeIds(n)]}),r}getNodeCycleEdgeIds(e){const o=Array.from(this.nodes.get(e).ports.keys());let r=[];return o.forEach(n=>{r=[...r,...this.getPortCycleEdgeIds(n)]}),r}getNodeAdjacentEdgeIds(e){return[...this.getNodeIncomingEdgeIds(e),...this.getNodeOutcomingEdgeIds(e),...this.getNodeCycleEdgeIds(e)]}addEdgeInternal(e){this.edges.set(e.id,{from:e.from,to:e.to,shape:e.shape,priority:e.priority}),e.from!==e.to?(this.outcommingEdges.get(e.from).add(e.id),this.incommingEdges.get(e.to).add(e.id)):this.cycleEdges.get(e.from).add(e.id)}removeEdgeInternal(e){const o=this.edges.get(e),r=o.from,n=o.to;this.cycleEdges.get(r).delete(e),this.cycleEdges.get(n).delete(e),this.incommingEdges.get(r).delete(e),this.incommingEdges.get(n).delete(e),this.outcommingEdges.get(r).delete(e),this.outcommingEdges.get(n).delete(e),this.edges.delete(e)}}const oe=t=>({scale:1/t.scale,x:-t.x/t.scale,y:-t.y/t.scale}),re={scale:1,x:0,y:0};class Be{constructor(){i(this,"viewportMatrix",re);i(this,"contentMatrix",re);i(this,"emitter");i(this,"onAfterUpdate");[this.emitter,this.onAfterUpdate]=A()}getViewportMatrix(){return this.viewportMatrix}getContentMatrix(){return this.contentMatrix}patchViewportMatrix(e){this.viewportMatrix={scale:e.scale??this.viewportMatrix.scale,x:e.x??this.viewportMatrix.x,y:e.y??this.viewportMatrix.y},this.contentMatrix=oe(this.viewportMatrix),this.emitter.emit()}patchContentMatrix(e){this.contentMatrix={scale:e.scale??this.contentMatrix.scale,x:e.x??this.contentMatrix.x,y:e.y??this.contentMatrix.y},this.viewportMatrix=oe(this.contentMatrix),this.emitter.emit()}}class ke{constructor(){i(this,"canvasDefaults",{});i(this,"dragOptions");i(this,"transformOptions");i(this,"virtualScrollOptions");i(this,"hasDraggableNode",!1);i(this,"hasTransformableViewport",!1);i(this,"hasResizeReactiveNodes",!1);i(this,"boxRenderingTrigger")}setDefaults(e){return this.canvasDefaults=e,this}setOptions(e){return this.setDefaults(e),this}enableUserDraggableNodes(e){return this.hasDraggableNode=!0,this.dragOptions=e,this}enableUserTransformableViewport(e){return this.hasTransformableViewport=!0,this.transformOptions=e,this}enableResizeReactiveNodes(){return this.hasResizeReactiveNodes=!0,this}enableBoxAreaRendering(e){return this.boxRenderingTrigger=e,this}enableVirtualScroll(e){return this.virtualScrollOptions=e,this}build(){let e=this.boxRenderingTrigger;this.virtualScrollOptions!==void 0&&e===void 0&&(e=new X);const o=new Ue,r=new Be;let n=new Te(o,r);e!==void 0&&(n=new me(n,o,e));let h=new de(o,r,n);this.hasResizeReactiveNodes&&(h=new xe(h)),this.hasDraggableNode&&(h=new ae(h,this.dragOptions)),this.virtualScrollOptions!==void 0?h=new Ee(h,e,this.transformOptions,this.virtualScrollOptions):this.hasTransformableViewport&&(h=new Q(h,this.transformOptions));const s=new Fe(h,this.canvasDefaults);return this.reset(),s}reset(){this.canvasDefaults={},this.dragOptions=void 0,this.transformOptions=void 0,this.virtualScrollOptions=void 0,this.hasDraggableNode=!1,this.hasTransformableViewport=!1,this.hasResizeReactiveNodes=!1,this.boxRenderingTrigger=void 0}}E.BezierEdgeShape=O,E.CanvasBuilder=ke,E.EventSubject=X,E.HorizontalEdgeShape=q,E.HtmlGraphError=p,E.StraightEdgeShape=ee,E.VerticalEdgeShape=te,Object.defineProperty(E,Symbol.toStringTag,{value:"Module"})});
|