@html-graph/html-graph 0.0.51 → 0.0.52

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/main.umd.cjs CHANGED
@@ -1 +1 @@
1
- (function(f,N){typeof exports=="object"&&typeof module<"u"?N(exports):typeof define=="function"&&define.amd?define(["exports"],N):(f=typeof globalThis<"u"?globalThis:f||self,N(f.HTMLGraph={}))})(this,function(f){"use strict";var ct=Object.defineProperty;var lt=(f,N,P)=>N in f?ct(f,N,{enumerable:!0,configurable:!0,writable:!0,value:P}):f[N]=P;var n=(f,N,P)=>lt(f,typeof N!="symbol"?N+"":N,P);class N{constructor(t,e,s,o,i){n(this,"canvasWrapper",null);n(this,"host");n(this,"nodesContainer");n(this,"edgesContainer");n(this,"canvas");n(this,"canvasCtx");n(this,"hostResizeObserver");n(this,"nodesResizeObserver");n(this,"nodeElementToIdMap",new Map);n(this,"nodeWrapperElementToIdMap",new Map);n(this,"nodeIdToWrapperElementMap",new Map);n(this,"edgeIdToElementMap",new Map);n(this,"currentZIndex",0);n(this,"layers",{"edges-on-top":{create:()=>{this.host.appendChild(this.nodesContainer),this.host.appendChild(this.edgesContainer)},update:(t,e,s)=>{this.nodesContainer.style.transform=`matrix(${t}, 0, 0, ${t}, ${e}, ${s})`,this.edgesContainer.style.transform=`matrix(${t}, 0, 0, ${t}, ${e}, ${s})`},moveOnTop:t=>{this.currentZIndex+=1;const e=this.nodeIdToWrapperElementMap.get(t);e.style.zIndex=`${this.currentZIndex}`}},"edges-follow-node":{create:()=>{this.host.appendChild(this.nodesContainer),this.edgesContainer=this.nodesContainer},update:(t,e,s)=>{this.nodesContainer.style.transform=`matrix(${t}, 0, 0, ${t}, ${e}, ${s})`},moveOnTop:t=>{const e=this.nodeIdToWrapperElementMap.get(t);this.currentZIndex+=2,e.style.zIndex=`${this.currentZIndex}`,this.graphStore.getNodeAdjacentEdges(t).forEach(o=>{this.edgeIdToElementMap.get(o).style.zIndex=`${this.currentZIndex-1}`})}},"nodes-on-top":{create:()=>{this.host.appendChild(this.edgesContainer),this.host.appendChild(this.nodesContainer)},update:(t,e,s)=>{this.nodesContainer.style.transform=`matrix(${t}, 0, 0, ${t}, ${e}, ${s})`,this.edgesContainer.style.transform=`matrix(${t}, 0, 0, ${t}, ${e}, ${s})`},moveOnTop:t=>{this.currentZIndex+=1;const e=this.nodeIdToWrapperElementMap.get(t);e.style.zIndex=`${this.currentZIndex}`}}});this.graphStore=t,this.viewportTransformer=e,this.publicViewportTransformer=s,this.layersMode=o,this.backgroundDrawingFn=i,this.host=this.createHost(),this.canvas=this.createCanvas(),this.nodesContainer=this.createNodesContainer(),this.edgesContainer=this.createEdgesContainer();const h=this.canvas.getContext("2d");if(h===null)throw new Error("unable to get canvas context");this.canvasCtx=h,this.host.appendChild(this.canvas),this.layers[this.layersMode].create(),this.hostResizeObserver=this.createHostResizeObserver(),this.hostResizeObserver.observe(this.host),this.nodesResizeObserver=this.createNodesResizeObserver()}clear(){Array.from(this.edgeIdToElementMap.keys()).forEach(t=>{this.detachEdge(t)}),Array.from(this.nodeElementToIdMap.values()).forEach(t=>{this.detachNode(t)})}attach(t){this.detach(),this.canvasWrapper=t,this.canvasWrapper.appendChild(this.host)}detach(){this.canvasWrapper!==null&&(this.canvasWrapper.removeChild(this.host),this.canvasWrapper=null)}destroy(){this.hostResizeObserver.disconnect(),this.nodesResizeObserver.disconnect(),this.host.removeChild(this.canvas),this.host.removeChild(this.edgesContainer),this.host.removeChild(this.nodesContainer),this.canvasWrapper!==null&&(this.canvasWrapper.removeChild(this.host),this.canvasWrapper=null)}applyTransform(){this.backgroundDrawingFn(this.canvasCtx,this.publicViewportTransformer);const[t,e]=this.viewportTransformer.getViewCoords(0,0),s=this.viewportTransformer.getViewScale();this.layers[this.layersMode].update(s,t,e)}attachNode(t){const e=this.graphStore.getNode(t),s=document.createElement("div");s.appendChild(e.element),s.style.position="absolute",s.style.top="0",s.style.left="0",s.style.zIndex=`${this.currentZIndex}`,this.currentZIndex+=1,s.style.visibility="hidden",this.nodesContainer.appendChild(s),this.nodeElementToIdMap.set(e.element,t),this.nodeWrapperElementToIdMap.set(s,t),this.nodeIdToWrapperElementMap.set(t,s),this.updateNodeCoords(t,e.x,e.y),this.nodesResizeObserver.observe(s),s.style.visibility="visible"}detachNode(t){const e=this.graphStore.getNode(t);this.nodesResizeObserver.unobserve(e.element),this.nodesContainer.removeChild(e.element);const s=this.nodeIdToWrapperElementMap.get(t);s.removeChild(e.element),this.nodeElementToIdMap.delete(e.element),this.nodeWrapperElementToIdMap.delete(s),this.nodeIdToWrapperElementMap.delete(t)}attachEdge(t){const s=this.graphStore.getEdge(t).controller.svg;s.style.transformOrigin="50% 50%",s.style.position="absolute",s.style.top="0",s.style.left="0",s.style.zIndex=`${this.currentZIndex}`,this.currentZIndex+=1,this.edgeIdToElementMap.set(t,s),this.updateEdgeCoords(t),this.edgesContainer.appendChild(s)}detachEdge(t){const e=this.edgeIdToElementMap.get(t);this.edgeIdToElementMap.delete(t),this.edgesContainer.removeChild(e)}moveNodeOnTop(t){this.layers[this.layersMode].moveOnTop(t)}updateNodeCoordinates(t){const e=this.graphStore.getNode(t),s=this.graphStore.getNodeAdjacentEdges(t);this.updateNodeCoords(t,e.x,e.y),s.forEach(o=>{this.updateEdgeCoords(o)})}updatePortEdges(t){this.graphStore.getPortAdjacentEdges(t).forEach(s=>{this.updateEdgeCoords(s)})}getViewportDimensions(){const t=this.host.getBoundingClientRect();return[t.width,t.height]}createHost(){const t=document.createElement("div");return t.style.width="100%",t.style.height="100%",t.style.position="relative",t.style.overflow="hidden",t}createCanvas(){const t=document.createElement("canvas");return t.style.position="absolute",t.style.inset="0",t}createNodesContainer(){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}createEdgesContainer(){const t=document.createElement("div");return t.style.position="absolute",t.style.pointerEvents="none",t.style.top="0",t.style.left="0",t.style.width="0",t.style.height="0",t}createHostResizeObserver(){return new ResizeObserver(()=>{this.updateCanvasDimensions(),this.applyTransform()})}createNodesResizeObserver(){return new ResizeObserver(t=>{t.forEach(e=>{const s=e.target,o=this.nodeWrapperElementToIdMap.get(s),i=this.graphStore.getNode(o);this.updateNodeCoords(o,i.x,i.y),this.graphStore.getNodeAdjacentEdges(o).forEach(a=>{this.updateEdgeCoords(a)})})})}updateCanvasDimensions(){const{width:t,height:e}=this.host.getBoundingClientRect();this.canvas.width=t,this.canvas.height=e}updateNodeCoords(t,e,s){const o=this.nodeIdToWrapperElementMap.get(t),{width:i,height:h}=o.getBoundingClientRect(),a=this.viewportTransformer.getAbsScale(),c=this.graphStore.getNode(t),[l,d]=c.centerFn(i,h);o.style.transform=`matrix(1, 0, 0, 1, ${e-a*l}, ${s-a*d})`}updateEdgeCoords(t){const e=this.graphStore.getEdge(t),s=this.graphStore.getPort(e.from),o=this.graphStore.getPort(e.to),i=s.element.getBoundingClientRect(),h=o.element.getBoundingClientRect(),a=this.host.getBoundingClientRect(),[c,l]=this.viewportTransformer.getAbsCoords(i.left-a.left,i.top-a.top),[d,u]=this.viewportTransformer.getAbsCoords(h.left-a.left,h.top-a.top),g=this.viewportTransformer.getAbsScale(),[w,m]=s.centerFn(i.width*g,i.height*g),[C,p]=o.centerFn(h.width*g,h.height*g),E=w+c,v=m+l,y=C+d,A=p+u,b=Math.min(E,y),O=Math.min(v,A),V=Math.abs(y-E),D=Math.abs(A-v);e.controller.update(b,O,V,D,s,o)}}class P{constructor(){n(this,"state",{scale:1,x:0,y:0})}getViewCoords(t,e){return[(t-this.state.x)/this.state.scale,(e-this.state.y)/this.state.scale]}getViewScale(){return 1/this.state.scale}getAbsCoords(t,e){return[t*this.state.scale+this.state.x,e*this.state.scale+this.state.y]}getAbsScale(){return this.state.scale}patchState(t,e,s){this.state={scale:t??this.state.scale,x:e??this.state.x,y:s??this.state.y}}}class _{constructor(t){this.transformer=t}getViewCoords(t,e){return this.transformer.getViewCoords(t,e)}getViewScale(){return this.transformer.getViewScale()}getAbsCoords(t,e){return this.transformer.getAbsCoords(t,e)}getAbsScale(){return this.transformer.getAbsScale()}}class J{constructor(){n(this,"nodes",Object.create(null));n(this,"ports",Object.create(null));n(this,"nodePorts",Object.create(null));n(this,"portNodeId",Object.create(null));n(this,"edges",Object.create(null));n(this,"incommingEdges",Object.create(null));n(this,"outcommingEdges",Object.create(null));n(this,"cycleEdges",Object.create(null))}addNode(t,e,s,o,i){this.nodes[t]={element:e,x:s,y:o,centerFn:i},this.nodePorts[t]=Object.create(null)}hasNode(t){return this.nodes[t]!==void 0}getNode(t){return this.nodes[t]}updateNodeCoords(t,e,s){this.nodes[t].x=e,this.nodes[t].y=s}updateEdgeController(t,e){this.edges[t].controller=e}removeNode(t){this.getNodeAdjacentEdges(t).forEach(o=>{this.removeEdge(o)}),delete this.nodes[t];const s=this.nodePorts[t];Object.keys(s).forEach(o=>{delete this.portNodeId[o]}),delete this.nodePorts[t]}addPort(t,e,s,o,i){this.ports[t]={element:e,centerFn:o,direction:i},this.cycleEdges[t]={},this.incommingEdges[t]={},this.outcommingEdges[t]={},this.portNodeId[t]=s;const h=this.nodePorts[s];h!==void 0&&(h[t]=e)}getPort(t){return this.ports[t]}getPortNode(t){return this.portNodeId[t]}hasPort(t){return this.portNodeId[t]!==void 0}removePort(t){Object.keys(this.cycleEdges[t]).forEach(o=>{this.removeEdge(o)}),delete this.cycleEdges[t],Object.keys(this.incommingEdges[t]).forEach(o=>{this.removeEdge(o)}),delete this.incommingEdges[t],Object.keys(this.outcommingEdges[t]).forEach(o=>{this.removeEdge(o)}),delete this.outcommingEdges[t];const e=this.portNodeId[t];delete this.portNodeId[t];const s=this.nodePorts[e];delete s[t],delete this.ports[t]}addEdge(t,e,s,o){this.edges[t]={from:e,to:s,controller:o},e!==s?(this.outcommingEdges[e][t]=!0,this.incommingEdges[s][t]=!0):this.cycleEdges[e][t]=!0}getEdge(t){return this.edges[t]}hasEdge(t){return this.edges[t]!==void 0}removeEdge(t){const e=this.edges[t],s=e.from,o=e.to;delete this.cycleEdges[s][t],delete this.cycleEdges[o][t],delete this.incommingEdges[s][t],delete this.incommingEdges[o][t],delete this.outcommingEdges[s][t],delete this.outcommingEdges[o][t],delete this.edges[t]}getPortAdjacentEdges(t){return[...this.getPortIncomingEdges(t),...this.getPortOutcomingEdges(t),...this.getPortCycleEdges(t)]}getNodeAdjacentEdges(t){return[...this.getNodeIncomingEdges(t),...this.getNodeOutcomingEdges(t),...this.getNodeCycleEdges(t)]}clear(){this.nodes=Object.create(null),this.ports=Object.create(null),this.nodePorts=Object.create(null),this.portNodeId=Object.create(null),this.edges=Object.create(null),this.incommingEdges=Object.create(null),this.outcommingEdges=Object.create(null),this.cycleEdges=Object.create(null)}getPortIncomingEdges(t){return Object.keys(this.incommingEdges[t]??{})}getPortOutcomingEdges(t){return Object.keys(this.outcommingEdges[t]??{})}getPortCycleEdges(t){return Object.keys(this.cycleEdges[t]??{})}getNodeIncomingEdges(t){const e=Object.keys(this.nodePorts[t]);let s=[];return e.forEach(o=>{s=[...s,...this.getPortIncomingEdges(o)]}),s}getNodeOutcomingEdges(t){const e=Object.keys(this.nodePorts[t]);let s=[];return e.forEach(o=>{s=[...s,...this.getPortOutcomingEdges(o)]}),s}getNodeCycleEdges(t){const e=Object.keys(this.nodePorts[t]);let s=[];return e.forEach(o=>{s=[...s,...this.getPortCycleEdges(o)]}),s}}const M=r=>{const{top:t,left:e,width:s,height:o}=r.element.getBoundingClientRect(),i=r.centerFn(s,o);return[e+i[0],t+i[1]]},S=(r,t,e)=>[t[0]*r[0]-t[1]*r[1]+((1-t[0])*e[0]+t[1]*e[1]),t[1]*r[0]+t[0]*r[1]+((1-t[0])*e[1]-t[1]*e[0])],T=(r,t,e)=>[t*Math.cos(r),e*Math.sin(r)],$=(r,t,e,s,o)=>{const h=[[0,0],[s,o],[s,-o]].map(d=>S(d,r,[0,0])).map(d=>[d[0]+t,d[1]+e]),a=`M ${h[0][0]} ${h[0][1]}`,c=`L ${h[1][0]} ${h[1][1]}`,l=`L ${h[2][0]} ${h[2][1]}`;return`${a} ${c} ${l}`};class W{constructor(t,e,s,o,i,h,a){n(this,"svg");n(this,"group");n(this,"line");n(this,"sourceArrow",null);n(this,"targetArrow",null);this.color=t,this.width=e,this.curvature=s,this.arrowLength=o,this.arrowWidth=i,this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.style.pointerEvents="none",this.group=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svg.appendChild(this.group),this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("stroke",this.color),this.line.setAttribute("stroke-width",`${this.width}`),this.line.setAttribute("fill","none"),this.group.appendChild(this.line),this.group.style.transformOrigin="50% 50%",h&&(this.sourceArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.sourceArrow.setAttribute("fill",this.color),this.group.appendChild(this.sourceArrow)),a&&(this.targetArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.targetArrow.setAttribute("fill",this.color),this.group.appendChild(this.targetArrow)),this.svg.style.overflow="visible"}update(t,e,s,o,i,h){this.svg.style.width=`${s}px`,this.svg.style.height=`${o}px`;const a=M(i),c=M(h),l=a[0]<=c[0]?1:-1,d=a[1]<=c[1]?1:-1;this.svg.style.transform=`translate(${t}px, ${e}px)`,this.group.style.transform=`scale(${l}, ${d})`;const u=T(i.direction,l,d),g=T(h.direction,l,d),w=S([this.arrowLength,0],u,[0,0]),m=S([s-this.arrowLength,o],g,[s,o]),C=[w[0]+u[0]*this.curvature,w[1]+u[1]*this.curvature],p=[m[0]-g[0]*this.curvature,m[1]-g[1]*this.curvature],E=`M ${w[0]} ${w[1]} C ${C[0]} ${C[1]}, ${p[0]} ${p[1]}, ${m[0]} ${m[1]}`,v=this.sourceArrow?"":`M 0 0 L ${w[0]} ${w[1]} `,y=this.targetArrow?"":` M ${m[0]} ${m[1]} L ${s} ${o}`,A=`${v}${E}${y}`;if(this.line.setAttribute("d",A),this.sourceArrow){const b=$(u,0,0,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",b)}if(this.targetArrow){const b=$(g,s,o,-this.arrowLength,this.arrowWidth);this.targetArrow.setAttribute("d",b)}}}const F=(r,t)=>{const e=[];if(r.length>0&&e.push(`M ${r[0][0]} ${r[0][1]}`),r.length===2&&e.push(`L ${r[1][0]} ${r[1][1]}`),r.length>2){const s=r.length-1;let o=0,i=0,h=0;r.forEach((a,c)=>{let l=0,d=0,u=0;const g=c>0,w=c<s,m=g&&w;if(g&&(l=-o,d=-i,u=h),w){const b=r[c+1];o=b[0]-a[0],i=b[1]-a[1],h=Math.sqrt(o*o+i*i)}const p=Math.min((m?t:0)/h,c<s-1?.5:1),E=m?[a[0]+o*p,a[1]+i*p]:a,y=Math.min((m?t:0)/u,c>1?.5:1),A=m?[a[0]+l*y,a[1]+d*y]:a;c>0&&e.push(`L ${A[0]} ${A[1]}`),m&&e.push(`C ${a[0]} ${a[1]} ${a[0]} ${a[1]} ${E[0]} ${E[1]}`)})}return e.join(" ")};class j{constructor(t,e,s,o,i,h,a,c){n(this,"svg");n(this,"group");n(this,"line");n(this,"sourceArrow",null);n(this,"targetArrow",null);this.color=t,this.width=e,this.arrowLength=s,this.arrowWidth=o,this.arrowOffset=i,this.roundness=c,this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.style.pointerEvents="none",this.group=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svg.appendChild(this.group),this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("stroke",this.color),this.line.setAttribute("stroke-width",`${this.width}`),this.line.setAttribute("fill","none"),this.group.appendChild(this.line),this.group.style.transformOrigin="50% 50%",h&&(this.sourceArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.sourceArrow.setAttribute("fill",this.color),this.group.appendChild(this.sourceArrow)),a&&(this.targetArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.targetArrow.setAttribute("fill",this.color),this.group.appendChild(this.targetArrow)),this.svg.style.overflow="visible"}update(t,e,s,o,i,h){this.svg.style.width=`${s}px`,this.svg.style.height=`${o}px`;const a=M(i),c=M(h),l=a[0]<=c[0]?1:-1,d=a[1]<=c[1]?1:-1;this.svg.style.transform=`translate(${t}px, ${e}px)`,this.group.style.transform=`scale(${l}, ${d})`;const u=T(i.direction,l,d),g=T(h.direction,l,d),w=this.sourceArrow?S([this.arrowLength,0],u,[0,0]):[0,0],m=this.targetArrow?S([s-this.arrowLength,o],g,[s,o]):[s,o],C=this.arrowLength+this.arrowOffset,p=S([C,0],u,[0,0]),E=S([s-C,o],g,[s,o]),v=F([w,p,E,m],this.roundness);if(this.line.setAttribute("d",v),this.sourceArrow){const y=$(u,0,0,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",y)}if(this.targetArrow){const y=$(g,s,o,-this.arrowLength,this.arrowWidth);this.targetArrow.setAttribute("d",y)}}}class R{constructor(t,e,s,o,i,h,a){n(this,"svg");n(this,"group");n(this,"line");n(this,"arrow",null);this.color=t,this.width=e,this.arrowLength=s,this.arrowWidth=o,this.radius=h,this.smallRadius=a,this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.style.pointerEvents="none",this.group=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svg.appendChild(this.group),this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("stroke",this.color),this.line.setAttribute("stroke-width",`${this.width}`),this.line.setAttribute("fill","none"),this.group.appendChild(this.line),this.group.style.transformOrigin="50% 50%",i&&(this.arrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.arrow.setAttribute("fill",this.color),this.group.appendChild(this.arrow)),this.svg.style.overflow="visible",this.svg.style.width="0px",this.svg.style.height="0px"}update(t,e,s,o,i){this.svg.style.transform=`translate(${t}px, ${e}px)`;const h=T(i.direction,1,1),a=this.smallRadius,c=this.radius,l=Math.sqrt(a*a+c*c),d=a+c,u=this.arrowLength+l*(1-c/d),g=a*c/d,m=[[this.arrowLength,0],[u,g],[u,-g]].map(v=>S(v,h,[0,0])),C=[`M ${m[0][0]} ${m[0][1]}`,`A ${a} ${a} 0 0 1 ${m[1][0]} ${m[1][1]}`,`A ${c} ${c} 0 1 0 ${m[2][0]} ${m[2][1]}`,`A ${a} ${a} 0 0 1 ${m[0][0]} ${m[0][1]}`].join(" "),p=`M 0 0 L ${m[0][0]} ${m[0][1]} `,E=`${this.arrow!==null?"":p}${C}`;if(this.line.setAttribute("d",E),this.arrow){const v=$(h,0,0,this.arrowLength,this.arrowWidth);this.arrow.setAttribute("d",v)}}}class B{constructor(t,e,s,o,i,h,a,c){n(this,"svg");n(this,"group");n(this,"line");n(this,"arrow",null);n(this,"roundness");n(this,"linePoints");this.color=t,this.width=e,this.arrowLength=s,this.arrowWidth=o,this.side=h,this.minPortOffset=a,this.roundness=Math.min(c,this.minPortOffset,this.side/2),this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.style.pointerEvents="none",this.group=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svg.appendChild(this.group),this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("stroke",this.color),this.line.setAttribute("stroke-width",`${this.width}`),this.line.setAttribute("fill","none"),this.group.appendChild(this.line),this.group.style.transformOrigin="50% 50%",i&&(this.arrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.arrow.setAttribute("fill",this.color),this.group.appendChild(this.arrow)),this.svg.style.overflow="visible",this.svg.style.width="0px",this.svg.style.height="0px";const l=this.minPortOffset,d=this.side,u=this.arrowLength+l,g=this.roundness,w=u+2*d;console.log(g),this.linePoints=[[this.arrowLength,0],[u,0],[u,this.side],[w,this.side],[w,-this.side],[u,-this.side],[u,0],[this.arrowLength,0]]}update(t,e,s,o,i){this.svg.style.transform=`translate(${t}px, ${e}px)`;const h=T(i.direction,1,1),a=this.linePoints.map(d=>S(d,h,[0,0])),c=`M 0 0 L ${a[0][0]} ${a[0][1]} `,l=`${this.arrow?"":c}${F(a,this.roundness)}`;if(this.line.setAttribute("d",l),this.arrow){const d=$(h,0,0,this.arrowLength,this.arrowWidth);this.arrow.setAttribute("d",d)}}}class z{constructor(t,e,s,o,i,h,a,c,l,d){n(this,"svg");n(this,"group");n(this,"line");n(this,"sourceArrow",null);n(this,"targetArrow",null);n(this,"detourX");n(this,"detourY");this.color=t,this.width=e,this.arrowLength=s,this.arrowWidth=o,this.arrowOffset=i,this.roundness=c,this.detourX=Math.cos(d)*l,this.detourY=Math.sin(d)*l,this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.style.pointerEvents="none",this.group=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svg.appendChild(this.group),this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("stroke",this.color),this.line.setAttribute("stroke-width",`${this.width}`),this.line.setAttribute("fill","none"),this.group.appendChild(this.line),this.group.style.transformOrigin="50% 50%",h&&(this.sourceArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.sourceArrow.setAttribute("fill",this.color),this.group.appendChild(this.sourceArrow)),a&&(this.targetArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.targetArrow.setAttribute("fill",this.color),this.group.appendChild(this.targetArrow)),this.svg.style.overflow="visible"}update(t,e,s,o,i,h){this.svg.style.width=`${s}px`,this.svg.style.height=`${o}px`;const a=M(i),c=M(h),l=a[0]<=c[0]?1:-1,d=a[1]<=c[1]?1:-1;this.svg.style.transform=`translate(${t}px, ${e}px)`,this.group.style.transform=`scale(${l}, ${d})`;const u=T(i.direction,l,d),g=T(h.direction,l,d),w=this.sourceArrow?S([this.arrowLength,0],u,[0,0]):[0,0],m=this.targetArrow?S([s-this.arrowLength,o],g,[s,o]):[s,o],C=this.arrowLength+this.arrowOffset,p=S([C,0],u,[0,0]),E=[p[0]+this.detourX,p[1]+this.detourY],v=S([s-C,o],g,[s,o]),y=[v[0]+this.detourX,v[1]+this.detourY],A=F([w,p,E,y,v,m],this.roundness);if(this.line.setAttribute("d",A),this.sourceArrow){const b=$(u,0,0,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",b)}if(this.targetArrow){const b=$(g,s,o,-this.arrowLength,this.arrowWidth);this.targetArrow.setAttribute("d",b)}}}var x=(r=>(r.Regular="regular",r.PortCycle="port-cycle",r.NodeCycle="node-cycle",r))(x||{});class K{constructor(t,e,s,o,i,h,a,c,l){n(this,"svg");n(this,"group");n(this,"line");n(this,"sourceArrow",null);n(this,"targetArrow",null);n(this,"detourX");n(this,"detourY");this.color=t,this.width=e,this.curvature=s,this.arrowLength=o,this.arrowWidth=i,this.detourX=Math.cos(l)*c,this.detourY=Math.sin(l)*c,this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.style.pointerEvents="none",this.group=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svg.appendChild(this.group),this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("stroke",this.color),this.line.setAttribute("stroke-width",`${this.width}`),this.line.setAttribute("fill","none"),this.group.appendChild(this.line),this.group.style.transformOrigin="50% 50%",h&&(this.sourceArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.sourceArrow.setAttribute("fill",this.color),this.group.appendChild(this.sourceArrow)),a&&(this.targetArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.targetArrow.setAttribute("fill",this.color),this.group.appendChild(this.targetArrow)),this.svg.style.overflow="visible"}update(t,e,s,o,i,h){this.svg.style.width=`${s}px`,this.svg.style.height=`${o}px`;const a=M(i),c=M(h),l=a[0]<=c[0]?1:-1,d=a[1]<=c[1]?1:-1;this.svg.style.transform=`translate(${t}px, ${e}px)`,this.group.style.transform=`scale(${l}, ${d})`;const u=T(i.direction,l,d),g=T(h.direction,l,d),w=this.sourceArrow?S([this.arrowLength,0],u,[0,0]):[0,0],m=this.targetArrow?S([s-this.arrowLength,o],g,[s,o]):[s,o],C=this.arrowLength,p=S([C,0],u,[0,0]),E=[p[0]+this.detourX,p[1]+this.detourY],v=S([s-C,o],g,[s,o]),y=[v[0]+this.detourX,v[1]+this.detourY],A=[(E[0]+y[0])/2,(E[1]+y[1])/2],b=[p[0]-this.curvature*Math.cos(i.direction),p[1]-this.curvature*Math.sin(i.direction)],O=[v[0]+this.curvature*Math.cos(h.direction),v[1]+this.curvature*Math.sin(h.direction)],V=[p[0]+this.detourX,p[1]+this.detourY],D=[v[0]+this.detourX,v[1]+this.detourY],at=[`M ${w[0]} ${w[1]}`,`L ${p[0]} ${p[1]}`,`C ${b[0]} ${b[1]} ${V[0]} ${V[1]} ${A[0]} ${A[1]}`,`C ${D[0]} ${D[1]} ${O[0]} ${O[1]} ${v[0]} ${v[1]}`,`L ${m[0]} ${m[1]}`].join(" ");if(this.line.setAttribute("d",at),this.sourceArrow){const k=$(u,0,0,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",k)}if(this.targetArrow){const k=$(g,s,o,-this.arrowLength,this.arrowWidth);this.targetArrow.setAttribute("d",k)}}}const Y=r=>t=>t===x.PortCycle?new R(r.color,r.width,r.arrowLength,r.arrowWidth,r.hasSourceArrow||r.hasTargetArrow,r.cycleRadius,r.smallCycleRadius):t===x.NodeCycle?new K(r.color,r.width,r.curvature,r.arrowLength,r.arrowWidth,r.hasSourceArrow,r.hasTargetArrow,r.detourDistance,r.detourDirection):new W(r.color,r.width,r.curvature,r.arrowLength,r.arrowWidth,r.hasSourceArrow,r.hasTargetArrow),X=r=>t=>t===x.PortCycle?new B(r.color,r.width,r.arrowLength,r.arrowWidth,r.hasSourceArrow||r.hasTargetArrow,r.cycleSquareSide,r.arrowOffset,r.roundness):t===x.NodeCycle?new z(r.color,r.width,r.arrowLength,r.arrowWidth,r.arrowOffset,r.hasSourceArrow,r.hasTargetArrow,r.roundness,r.detourDistance,r.detourDirection):new j(r.color,r.width,r.arrowLength,r.arrowWidth,r.arrowOffset,r.hasSourceArrow,r.hasTargetArrow,r.roundness);class L{constructor(){n(this,"counter",0)}next(){const t=`${this.counter}`;return this.counter++,t}reset(){this.counter=0}}class Q{constructor(t,e,s,o,i,h){n(this,"nodeIdGenerator",new L);n(this,"portIdGenerator",new L);n(this,"edgeIdGenerator",new L);this.graphStore=t,this.htmlController=e,this.viewportTransformer=s,this.nodesCenterFn=o,this.portsCenterFn=i,this.portsDirection=h}moveNodeOnTop(t){if(!this.graphStore.hasNode(t))throw new Error("failed to move on top nonexisting node");this.htmlController.moveNodeOnTop(t)}addNode(t,e,s,o,i,h){if(t===void 0)do t=this.nodeIdGenerator.next();while(this.graphStore.hasNode(t));if(this.graphStore.hasNode(t))throw new Error("failed to add node with existing id");this.graphStore.addNode(t,e,s,o,h??this.nodesCenterFn),this.htmlController.attachNode(t),i!==void 0&&Object.entries(i).forEach(([a,c])=>{c instanceof HTMLElement?this.markPort(a,c,t,this.portsCenterFn,this.portsDirection):this.markPort(a,c.element,t,c.centerFn??this.portsCenterFn,c.direction??this.portsDirection)})}markPort(t,e,s,o,i){if(t===void 0)do t=this.portIdGenerator.next();while(this.graphStore.hasPort(t));if(!this.graphStore.hasNode(s))throw new Error("failed to set port on nonexisting node");if(this.graphStore.hasPort(t))throw new Error("failed to add port with existing id");this.graphStore.addPort(t,e,s,o??this.portsCenterFn,i??0)}updatePortEdges(t){if(!this.graphStore.hasPort(t))throw new Error("failed to unset nonexisting port");this.htmlController.updatePortEdges(t)}unmarkPort(t){if(!this.graphStore.hasPort(t))throw new Error("failed to unset nonexisting port");this.graphStore.getPortAdjacentEdges(t).forEach(e=>{this.removeEdge(e)}),this.graphStore.removePort(t)}addEdge(t,e,s,o){if(t===void 0)do t=this.edgeIdGenerator.next();while(this.graphStore.hasEdge(t));if(!this.graphStore.hasPort(e))throw new Error("failed to add edge from nonexisting port");if(!this.graphStore.hasPort(s))throw new Error("failed to add edge to nonexisting port");let i=x.Regular;const h=this.graphStore.getPortNode(e),a=this.graphStore.getPortNode(s);e===s?i=x.PortCycle:h===a&&(i=x.NodeCycle),this.graphStore.addEdge(t,e,s,o(i)),this.htmlController.attachEdge(t)}removeEdge(t){if(!this.graphStore.hasEdge(t))throw new Error("failed to remove nonexisting edge");this.htmlController.detachEdge(t),this.graphStore.removeEdge(t)}removeNode(t){if(!this.graphStore.hasNode(t))throw new Error("failed to remove nonexisting node");this.htmlController.detachNode(t),this.graphStore.removeNode(t)}patchViewportState(t,e,s){this.viewportTransformer.patchState(t,e,s),this.htmlController.applyTransform()}moveToNodes(t){if(t.length===0)return;const e=t.map(g=>this.graphStore.getNode(g)).filter(g=>g!==void 0);if(e.length<t.length)throw new Error("failed to move to nonexisting node");const[s,o]=e.reduce((g,w)=>[g[0]+w.x,g[1]+w.y],[0,0]),i=s/e.length,h=o/e.length,[a,c]=this.htmlController.getViewportDimensions(),l=this.viewportTransformer.getAbsScale(),d=i-l*a/2,u=h-l*c/2;this.patchViewportState(null,d,u)}updateNodeCoordinates(t,e,s){if(!this.graphStore.hasNode(t))throw new Error("failed to update coordinates of nonexisting node");this.graphStore.updateNodeCoords(t,e,s),this.htmlController.updateNodeCoordinates(t)}updateEdgeController(t,e){if(!this.graphStore.hasEdge(t))throw new Error("failed to update nonexisting edge");this.htmlController.detachEdge(t),this.graphStore.updateEdgeController(t,e),this.htmlController.attachEdge(t)}attach(t){this.htmlController.attach(t)}detach(){this.htmlController.detach()}clear(){this.htmlController.clear(),this.graphStore.clear(),this.nodeIdGenerator.reset(),this.portIdGenerator.reset(),this.edgeIdGenerator.reset()}destroy(){this.htmlController.destroy()}}class I{constructor(t,e,s,o,i){n(this,"publicViewportTransformer");n(this,"canvasController");const h=new P,a=new J;this.publicViewportTransformer=new _(h);const c=new N(a,h,this.publicViewportTransformer,t,e);this.canvasController=new Q(a,c,h,s,o,i)}}const G=(r,t)=>[r/2,t/2],tt=()=>()=>{},et=(r,t,e,s,o,i)=>{r.clearRect(0,0,r.canvas.width,r.canvas.height),r.fillStyle=i,r.fillRect(0,0,r.canvas.width,r.canvas.height);const h=t.getViewCoords(0,0),a=t.getViewScale(),c=s*a;let l=0,d=0,u=c/2;do u*=2,l=r.canvas.width/u,d=r.canvas.height/u;while(l*d>1e4);const g=h[0]-Math.floor(h[0]/u)*u,w=h[1]-Math.floor(h[1]/u)*u,m=o*a,C=2*Math.PI,p=g-u,E=w-u,v=r.canvas.width+g,y=r.canvas.height+w;r.fillStyle=e;for(let A=p;A<=v;A+=u)for(let b=E;b<=y;b+=u)r.beginPath(),r.arc(A,b,m,0,C),r.closePath(),r.fill()},st=(r,t,e,s)=>(o,i)=>{et(o,i,r,t,e,s)},rt=(r,t)=>{r.fillStyle=t,r.fillRect(0,0,r.canvas.width,r.canvas.height)},ot=r=>t=>{rt(t,r)},nt=r=>{switch(r==null?void 0:r.type){case"custom":return r.drawingFn;case"dots":return st(r.dotColor??"#d8d8d8",r.dotGap??25,r.dotRadius??1.5,r.color??"#ffffff");case"color":return ot(r.color??"#ffffff");default:return tt()}},U=r=>{switch(r==null?void 0:r.type){case"custom":return r.controllerFactory;case"straight":return X({color:r.color??"#5c5c5c",width:r.width??1,arrowLength:r.arrowLength??15,arrowWidth:r.arrowWidth??4,arrowOffset:r.arrowOffset??15,hasSourceArrow:r.hasSourceArrow??!1,hasTargetArrow:r.hasTargetArrow??!1,cycleSquareSide:r.cycleSquareSide??30,roundness:r.roundness??10,detourDistance:r.detourDistance??100,detourDirection:r.detourDirection??-Math.PI/2});default:return Y({color:r.color??"#5c5c5c",width:r.width??1,curvature:r.curvature??90,arrowLength:r.arrowLength??15,arrowWidth:r.arrowWidth??4,hasSourceArrow:r.hasSourceArrow??!1,hasTargetArrow:r.hasTargetArrow??!1,cycleRadius:r.cycleRadius??30,smallCycleRadius:r.smallCycleRadius??15,detourDistance:r.detourDistance??100,detourDirection:r.detourDirection??-Math.PI/2})}},it=r=>{var t,e,s,o;return{background:{drawingFn:nt(r.background??{type:"none"})},nodes:{centerFn:((t=r.nodes)==null?void 0:t.centerFn)??G},ports:{centerFn:((e=r.ports)==null?void 0:e.centerFn)??G,direction:((s=r.ports)==null?void 0:s.direction)??0},edges:{controllerFactory:U(r.edges??{})},layers:{mode:((o=r.layers)==null?void 0:o.mode)??"edges-follow-node"}}};class Z{constructor(t){n(this,"transformation");n(this,"di");n(this,"edgeControllerFactory");this.apiOptions=t;const e=it(this.apiOptions??{});this.di=new I(e.layers.mode,e.background.drawingFn,e.nodes.centerFn,e.ports.centerFn,e.ports.direction),this.transformation=this.di.publicViewportTransformer,this.edgeControllerFactory=e.edges.controllerFactory}addNode(t){return this.di.canvasController.addNode(t.id,t.element,t.x,t.y,t.ports,t.centerFn),this}moveNodeOnTop(t){return this.di.canvasController.moveNodeOnTop(t),this}removeNode(t){return this.di.canvasController.removeNode(t),this}markPort(t){return this.di.canvasController.markPort(t.id,t.element,t.nodeId,t.centerFn,t.direction),this}updatePortEdges(t){return this.di.canvasController.updatePortEdges(t),this}unmarkPort(t){return this.di.canvasController.unmarkPort(t),this}addEdge(t){const e=t.options!==void 0?U(t.options):this.edgeControllerFactory;return this.di.canvasController.addEdge(t.id,t.from,t.to,e),this}removeEdge(t){return this.di.canvasController.removeEdge(t),this}patchViewportState(t){return this.di.canvasController.patchViewportState(t.scale??null,t.x??null,t.y??null),this}moveToNodes(t){return this.di.canvasController.moveToNodes(t),this}updateNodeCoordinates(t,e,s){return this.di.canvasController.updateNodeCoordinates(t,e,s),this}updateEdge(t,e){return e.controller!==void 0&&this.di.canvasController.updateEdgeController(t,e.controller),this}clear(){return this.di.canvasController.clear(),this}attach(t){return this.di.canvasController.attach(t),this}detach(){return this.di.canvasController.detach(),this}destroy(){this.di.canvasController.destroy()}}class H{constructor(t,e){n(this,"transformation");n(this,"nodes",new Map);n(this,"grabbedNodeId",null);n(this,"onNodeDrag");n(this,"onBeforeNodeDrag");n(this,"nodeIdGenerator",new L);n(this,"element",null);n(this,"onCanvasMouseUp",()=>{this.setCursor(null),this.grabbedNodeId=null});n(this,"onCanvasMouseMove",t=>{this.grabbedNodeId!==null&&(t.stopPropagation(),this.dragNode(this.grabbedNodeId,t.movementX,t.movementY))});n(this,"onCanvasTouchStart",t=>{this.previousTouchCoords=[t.touches[0].clientX,t.touches[0].clientY]});n(this,"onCanvasTouchMove",t=>{if(this.grabbedNodeId===null||t.touches.length!==1||this.previousTouchCoords===null)return;t.stopImmediatePropagation();const[e,s]=[t.touches[0].clientX-this.previousTouchCoords[0],t.touches[0].clientY-this.previousTouchCoords[1]];this.dragNode(this.grabbedNodeId,e,s),this.previousTouchCoords=[t.touches[0].clientX,t.touches[0].clientY]});n(this,"onCanvasTouchEnd",t=>{t.touches.length>0?this.previousTouchCoords=[t.touches[0].clientX,t.touches[0].clientY]:(this.previousTouchCoords=null,this.grabbedNodeId=null)});n(this,"previousTouchCoords",null);var i,h;this.canvas=t,this.transformation=this.canvas.transformation;const s=()=>{};this.onNodeDrag=((i=e==null?void 0:e.events)==null?void 0:i.onNodeDrag)??s;const o=()=>!0;this.onBeforeNodeDrag=((h=e==null?void 0:e.events)==null?void 0:h.onBeforeNodeDrag)??o}addNode(t){let e=t.id;if(e===void 0)do e=this.nodeIdGenerator.next();while(this.nodes.has(e));this.canvas.addNode(t);const s=i=>{this.onBeforeNodeDrag({nodeId:e,element:t.element,x:t.x,y:t.y})&&(i.stopImmediatePropagation(),this.grabbedNodeId=e,this.setCursor("grab"),this.canvas.moveNodeOnTop(e))},o=i=>{this.onBeforeNodeDrag({nodeId:e,element:t.element,x:t.x,y:t.y})&&i.touches.length===1&&(this.grabbedNodeId=e,this.canvas.moveNodeOnTop(e))};return this.nodes.set(e,{element:t.element,onMouseDown:s,onTouchStart:o,x:t.x,y:t.y}),t.element.addEventListener("mousedown",s),t.element.addEventListener("touchstart",o),this}removeNode(t){const e=this.nodes.get(t);return e!==void 0&&(e.element.removeEventListener("mousedown",e.onMouseDown),e.element.removeEventListener("touchstart",e.onTouchStart)),this.canvas.removeNode(t),this.nodes.delete(t),this}markPort(t){return this.canvas.markPort(t),this}updatePortEdges(t){return this.canvas.updatePortEdges(t),this}unmarkPort(t){return this.canvas.unmarkPort(t),this}addEdge(t){return this.canvas.addEdge(t),this}removeEdge(t){return this.canvas.removeEdge(t),this}patchViewportState(t){return this.canvas.patchViewportState(t),this}moveToNodes(t){return this.canvas.moveToNodes(t),this}updateNodeCoordinates(t,e,s){return this.canvas.updateNodeCoordinates(t,e,s),this}updateEdge(t,e){return this.canvas.updateEdge(t,e),this}moveNodeOnTop(t){return this.canvas.moveNodeOnTop(t),this}clear(){return this.canvas.clear(),this.nodes.forEach(t=>{t.element.removeEventListener("mousedown",t.onMouseDown),t.element.removeEventListener("touchstart",t.onTouchStart)}),this.nodes.clear(),this}attach(t){return this.detach(),this.element=t,this.canvas.attach(this.element),this.element.addEventListener("mouseup",this.onCanvasMouseUp),this.element.addEventListener("mousemove",this.onCanvasMouseMove),this.element.addEventListener("touchstart",this.onCanvasTouchStart),this.element.addEventListener("touchmove",this.onCanvasTouchMove),this.element.addEventListener("touchend",this.onCanvasTouchEnd),this.element.addEventListener("touchcancel",this.onCanvasTouchEnd),this}detach(){return this.canvas.detach(),this.element!==null&&(this.element.removeEventListener("mouseup",this.onCanvasMouseUp),this.element.removeEventListener("mousemove",this.onCanvasMouseMove),this.element.removeEventListener("touchstart",this.onCanvasTouchStart),this.element.removeEventListener("touchmove",this.onCanvasTouchMove),this.element.removeEventListener("touchend",this.onCanvasTouchEnd),this.element.removeEventListener("touchcancel",this.onCanvasTouchEnd),this.element=null),this}destroy(){this.detach(),this.nodes.forEach(t=>{t.element.removeEventListener("mousedown",t.onMouseDown),t.element.removeEventListener("touchstart",t.onTouchStart)}),this.nodes.clear(),this.canvas.destroy()}setCursor(t){this.element!==null&&(t!==null?this.element.style.cursor=t:this.element.style.removeProperty("cursor"))}dragNode(t,e,s){const o=this.nodes.get(t);if(o===void 0)throw new Error("failed to drag nonexisting node");const[i,h]=this.transformation.getViewCoords(o.x,o.y),a=i+e,c=h+s,[l,d]=this.transformation.getAbsCoords(a,c);o.x=l,o.y=d,this.canvas.updateNodeCoordinates(t,l,d),this.onNodeDrag({nodeId:t,element:o.element,x:o.x,y:o.y})}}class q{constructor(t,e){n(this,"transformation");n(this,"element",null);n(this,"isMoving",!1);n(this,"prevTouches",null);n(this,"onTransform");n(this,"onBeforeTransform");n(this,"isScalable");n(this,"isShiftable");n(this,"minViewScale");n(this,"maxViewScale");n(this,"wheelSensitivity");n(this,"onMouseDown",()=>{this.setCursor("grab"),this.isMoving=!0});n(this,"onMouseMove",t=>{if(!this.isMoving||!this.isShiftable)return;const e=-t.movementX,s=-t.movementY;this.moveViewport(e,s)});n(this,"onMouseUp",()=>{this.setCursor(null),this.isMoving=!1});n(this,"onWheelScroll",t=>{if(this.element===null||this.isScalable===!1)return;t.preventDefault();const{left:e,top:s}=this.element.getBoundingClientRect(),o=t.clientX-e,i=t.clientY-s,a=1/(t.deltaY<0?this.wheelSensitivity:1/this.wheelSensitivity);this.scaleViewport(a,o,i)});n(this,"onTouchStart",t=>{this.prevTouches=this.getAverageTouch(t)});n(this,"onTouchMove",t=>{if(this.prevTouches===null||this.element===null||!this.isShiftable)return;const e=this.getAverageTouch(t);if((e.touchesCnt===1||e.touchesCnt===2)&&this.moveViewport(-(e.x-this.prevTouches.x),-(e.y-this.prevTouches.y)),e.touchesCnt===2&&this.isScalable){const{left:s,top:o}=this.element.getBoundingClientRect(),i=this.prevTouches.x-s,h=this.prevTouches.y-o,c=1/(e.scale/this.prevTouches.scale);this.scaleViewport(c,i,h)}this.prevTouches=e,t.preventDefault()});n(this,"onTouchEnd",t=>{t.touches.length>0?this.prevTouches=this.getAverageTouch(t):this.prevTouches=null});var c,l,d,u,g,w,m,C,p,E,v,y;this.canvas=t,this.options=e,this.transformation=this.canvas.transformation;const s=((l=(c=this.options)==null?void 0:c.scale)==null?void 0:l.min)??null,o=((u=(d=this.options)==null?void 0:d.scale)==null?void 0:u.max)??null;this.isScalable=((w=(g=this.options)==null?void 0:g.scale)==null?void 0:w.enabled)!==!1,this.minViewScale=o!==null?1/o:null,this.maxViewScale=s!==null?1/s:null,this.isShiftable=((C=(m=this.options)==null?void 0:m.shift)==null?void 0:C.enabled)!==!1;const i=(E=(p=this.options)==null?void 0:p.scale)==null?void 0:E.wheelSensitivity;this.wheelSensitivity=i!==void 0?i:1.2;const h=()=>{};this.onTransform=((v=e==null?void 0:e.events)==null?void 0:v.onTransform)??h;const a=()=>!0;this.onBeforeTransform=((y=e==null?void 0:e.events)==null?void 0:y.onBeforeTransform)??a}addNode(t){return this.canvas.addNode(t),this}removeNode(t){return this.canvas.removeNode(t),this}markPort(t){return this.canvas.markPort(t),this}updatePortEdges(t){return this.canvas.updatePortEdges(t),this}unmarkPort(t){return this.canvas.unmarkPort(t),this}addEdge(t){return this.canvas.addEdge(t),this}removeEdge(t){return this.canvas.removeEdge(t),this}patchViewportState(t){return this.canvas.patchViewportState(t),this}moveToNodes(t){return this.canvas.moveToNodes(t),this}updateNodeCoordinates(t,e,s){return this.canvas.updateNodeCoordinates(t,e,s),this}updateEdge(t,e){return this.canvas.updateEdge(t,e),this}moveNodeOnTop(t){return this.canvas.moveNodeOnTop(t),this}clear(){return this.canvas.clear(),this}attach(t){return this.detach(),this.element=t,this.canvas.attach(this.element),this.element.addEventListener("mousedown",this.onMouseDown),this.element.addEventListener("mousemove",this.onMouseMove),this.element.addEventListener("mouseup",this.onMouseUp),this.element.addEventListener("wheel",this.onWheelScroll),this.element.addEventListener("touchstart",this.onTouchStart),this.element.addEventListener("touchmove",this.onTouchMove),this.element.addEventListener("touchend",this.onTouchEnd),this.element.addEventListener("touchcancel",this.onTouchEnd),this}detach(){return this.canvas.detach(),this.element!==null&&(this.element.removeEventListener("mousedown",this.onMouseDown),this.element.removeEventListener("mousemove",this.onMouseMove),this.element.removeEventListener("mouseup",this.onMouseUp),this.element.removeEventListener("wheel",this.onWheelScroll),this.element.removeEventListener("touchstart",this.onTouchStart),this.element.removeEventListener("touchmove",this.onTouchMove),this.element.removeEventListener("touchend",this.onTouchEnd),this.element.removeEventListener("touchcancel",this.onTouchEnd),this.element=null),this}destroy(){this.detach(),this.canvas.destroy()}getAverageTouch(t){const e=[],s=t.touches.length;for(let c=0;c<s;c++)e.push([t.touches[c].clientX,t.touches[c].clientY]);const o=e.reduce((c,l)=>[c[0]+l[0],c[1]+l[1]],[0,0]),i=[o[0]/s,o[1]/s],a=e.map(c=>[c[0]-i[0],c[1]-i[1]]).reduce((c,l)=>c+Math.sqrt(l[0]*l[0]+l[1]*l[1]),0);return{x:i[0],y:i[1],scale:a/s,touchesCnt:s}}setCursor(t){this.element!==null&&(t!==null?this.element.style.cursor=t:this.element.style.removeProperty("cursor"))}moveViewport(t,e){const[s,o]=this.transformation.getAbsCoords(0,0),i=this.canvas.transformation.getAbsScale(),h={scale:i,x:s+i*t,y:o+i*e};this.onBeforeTransform({...h})&&(this.canvas.patchViewportState(h),this.onTransform(h))}scaleViewport(t,e,s){const[o,i]=this.canvas.transformation.getAbsCoords(0,0),h=this.canvas.transformation.getAbsScale(),a=h*t,c=h*(1-t)*e+o,l=h*(1-t)*s+i;if(this.maxViewScale!==null&&a>this.maxViewScale&&a>h||this.minViewScale!==null&&a<this.minViewScale&&a<h)return;const d={scale:a,x:c,y:l};this.onBeforeTransform({...d})&&(this.canvas.patchViewportState(d),this.onTransform(d))}}class ht{constructor(){n(this,"coreOptions");n(this,"dragOptions");n(this,"transformOptions");n(this,"isDraggable",!1);n(this,"isTransformable",!1)}setOptions(t){return this.coreOptions=t,this}setUserDraggableNodes(t){return this.isDraggable=!0,this.dragOptions=t,this}setUserTransformableCanvas(t){return this.isTransformable=!0,this.transformOptions=t,this}build(){let t=new Z(this.coreOptions);return this.isDraggable&&(t=new H(t,this.dragOptions)),this.isTransformable&&(t=new q(t,this.transformOptions)),t}}f.BezierEdgeController=W,f.CanvasCore=Z,f.CycleCircleEdgeController=R,f.CycleSquareEdgeController=B,f.DetourStraightEdgeController=z,f.EdgeType=x,f.HtmlGraphBuilder=ht,f.StraightEdgeController=j,f.UserDraggableNodesCanvas=H,f.UserTransformableCanvas=q,f.createBezierEdgeControllerFactory=Y,f.createStraightEdgeControllerFactory=X,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
1
+ (function(C,$){typeof exports=="object"&&typeof module<"u"?$(exports):typeof define=="function"&&define.amd?define(["exports"],$):(C=typeof globalThis<"u"?globalThis:C||self,$(C.HTMLGraph={}))})(this,function(C){"use strict";var yt=Object.defineProperty;var At=(C,$,O)=>$ in C?yt(C,$,{enumerable:!0,configurable:!0,writable:!0,value:O}):C[$]=O;var a=(C,$,O)=>At(C,typeof $!="symbol"?$+"":$,O);const $=()=>{const e=document.createElement("div");return e.style.width="100%",e.style.height="100%",e.style.position="relative",e.style.overflow="hidden",e},O=()=>{const e=document.createElement("canvas");return e.style.position="absolute",e.style.inset="0",e},F=()=>{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};class et{constructor(t){a(this,"nodesContainer",F());a(this,"edgesContainer",F());this.host=t,this.host.appendChild(this.edgesContainer),this.host.appendChild(this.nodesContainer)}appendNodeElement(t){this.nodesContainer.appendChild(t)}removeNodeElement(t){this.nodesContainer.removeChild(t)}appendEdgeElement(t){this.edgesContainer.appendChild(t)}removeEdgeElement(t){this.edgesContainer.removeChild(t)}update(t,r,s){this.nodesContainer.style.transform=`matrix(${t}, 0, 0, ${t}, ${r}, ${s})`,this.edgesContainer.style.transform=`matrix(${t}, 0, 0, ${t}, ${r}, ${s})`}destroy(){this.host.removeChild(this.nodesContainer),this.host.removeChild(this.edgesContainer)}}class rt{constructor(t){a(this,"nodesContainer",F());a(this,"edgesContainer",F());this.host=t,this.host.appendChild(this.nodesContainer),this.host.appendChild(this.edgesContainer)}appendNodeElement(t){this.nodesContainer.appendChild(t)}removeNodeElement(t){this.nodesContainer.removeChild(t)}appendEdgeElement(t){this.edgesContainer.appendChild(t)}removeEdgeElement(t){this.edgesContainer.removeChild(t)}update(t,r,s){this.nodesContainer.style.transform=`matrix(${t}, 0, 0, ${t}, ${r}, ${s})`,this.edgesContainer.style.transform=`matrix(${t}, 0, 0, ${t}, ${r}, ${s})`}destroy(){this.host.removeChild(this.nodesContainer),this.host.removeChild(this.edgesContainer)}}class st{constructor(t){a(this,"container",F());this.host=t,this.host.appendChild(this.container)}appendNodeElement(t){this.container.appendChild(t)}removeNodeElement(t){this.container.removeChild(t)}appendEdgeElement(t){this.container.appendChild(t)}removeEdgeElement(t){this.container.removeChild(t)}update(t,r,s){this.container.style.transform=`matrix(${t}, 0, 0, ${t}, ${r}, ${s})`}destroy(){this.host.removeChild(this.container)}}const ot={"edges-on-top":e=>new rt(e),"edges-follow-node":e=>new st(e),"nodes-on-top":e=>new et(e)};class it{constructor(t,r,s,o,n){a(this,"canvasWrapper",null);a(this,"host",$());a(this,"canvas",O());a(this,"canvasCtx");a(this,"hostResizeObserver");a(this,"nodesResizeObserver");a(this,"nodeElementToIdMap",new Map);a(this,"nodeWrapperElementToIdMap",new Map);a(this,"nodeIdToWrapperElementMap",new Map);a(this,"edgeIdToElementMap",new Map);a(this,"layer");this.graphStore=t,this.viewportTransformer=r,this.publicViewportTransformer=s,this.layersMode=o,this.backgroundDrawingFn=n;const i=this.canvas.getContext("2d");if(i===null)throw new Error("unable to get canvas context");this.canvasCtx=i,this.host.appendChild(this.canvas),this.layer=ot[this.layersMode](this.host),this.hostResizeObserver=this.createHostResizeObserver(),this.hostResizeObserver.observe(this.host),this.nodesResizeObserver=this.createNodesResizeObserver()}clear(){Array.from(this.edgeIdToElementMap.keys()).forEach(t=>{this.detachEdge(t)}),Array.from(this.nodeElementToIdMap.values()).forEach(t=>{this.detachNode(t)})}attach(t){this.detach(),this.canvasWrapper=t,this.canvasWrapper.appendChild(this.host)}detach(){this.canvasWrapper!==null&&(this.canvasWrapper.removeChild(this.host),this.canvasWrapper=null)}destroy(){this.hostResizeObserver.disconnect(),this.nodesResizeObserver.disconnect(),this.host.removeChild(this.canvas),this.layer.destroy(),this.detach()}applyTransform(){this.backgroundDrawingFn(this.canvasCtx,this.publicViewportTransformer);const[t,r]=this.viewportTransformer.getViewCoords(0,0),s=this.viewportTransformer.getViewScale();this.layer.update(s,t,r)}attachNode(t){const r=this.graphStore.getNode(t),s=document.createElement("div");s.appendChild(r.element),s.style.position="absolute",s.style.top="0",s.style.left="0",s.style.visibility="hidden",this.layer.appendNodeElement(s),this.nodeElementToIdMap.set(r.element,t),this.nodeWrapperElementToIdMap.set(s,t),this.nodeIdToWrapperElementMap.set(t,s),this.updateNodeCoords(t),this.updateNodePriority(t),this.nodesResizeObserver.observe(s),s.style.visibility="visible"}detachNode(t){const r=this.graphStore.getNode(t);this.nodesResizeObserver.unobserve(r.element);const s=this.nodeIdToWrapperElementMap.get(t);s.removeChild(r.element),this.layer.removeNodeElement(s),this.nodeElementToIdMap.delete(r.element),this.nodeWrapperElementToIdMap.delete(s),this.nodeIdToWrapperElementMap.delete(t)}attachEdge(t){const s=this.graphStore.getEdge(t).controller.svg;s.style.position="absolute",s.style.top="0",s.style.left="0",this.edgeIdToElementMap.set(t,s),this.updateEdgeCoords(t),this.updateEdgePriority(t),this.layer.appendEdgeElement(s)}detachEdge(t){const r=this.edgeIdToElementMap.get(t);this.edgeIdToElementMap.delete(t),this.layer.removeEdgeElement(r)}updateNodePriority(t){const r=this.graphStore.getNode(t),s=this.nodeIdToWrapperElementMap.get(t);s.style.zIndex=`${r.priority}`}updateEdgePriority(t){const r=this.graphStore.getEdge(t);this.edgeIdToElementMap.get(t).style.zIndex=`${r.priority}`}updateNodeCoordinates(t){const r=this.graphStore.getNodeAdjacentEdges(t);this.updateNodeCoords(t),r.forEach(s=>{this.updateEdgeCoords(s)})}updatePortEdges(t){this.graphStore.getPortAdjacentEdges(t).forEach(s=>{this.updateEdgeCoords(s)})}getViewportDimensions(){const t=this.host.getBoundingClientRect();return[t.width,t.height]}createHostResizeObserver(){return new ResizeObserver(()=>{this.updateCanvasDimensions(),this.applyTransform()})}createNodesResizeObserver(){return new ResizeObserver(t=>{t.forEach(r=>{const s=r.target,o=this.nodeWrapperElementToIdMap.get(s);this.updateNodeCoords(o),this.graphStore.getNodeAdjacentEdges(o).forEach(i=>{this.updateEdgeCoords(i)})})})}updateCanvasDimensions(){const{width:t,height:r}=this.host.getBoundingClientRect();this.canvas.width=t,this.canvas.height=r}updateNodeCoords(t){const r=this.nodeIdToWrapperElementMap.get(t),{width:s,height:o}=r.getBoundingClientRect(),n=this.viewportTransformer.getAbsScale(),i=this.graphStore.getNode(t),[h,c]=i.centerFn(s,o);r.style.transform=`matrix(1, 0, 0, 1, ${i.x-n*h}, ${i.y-n*c})`}updateEdgeCoords(t){const r=this.graphStore.getEdge(t),s=this.graphStore.getPort(r.from),o=this.graphStore.getPort(r.to),n=s.element.getBoundingClientRect(),i=o.element.getBoundingClientRect(),h=this.host.getBoundingClientRect(),[c,l]=this.viewportTransformer.getAbsCoords(n.left-h.left,n.top-h.top),[d,g]=this.viewportTransformer.getAbsCoords(i.left-h.left,i.top-h.top),u=this.viewportTransformer.getAbsScale(),[v,w]=s.centerFn(n.width*u,n.height*u),[f,m]=o.centerFn(i.width*u,i.height*u),E=v+c,p=w+l,y=f+d,b=m+g,S=Math.min(E,y),M=Math.min(p,b),D=Math.abs(y-E),V=Math.abs(b-p);r.controller.update(S,M,D,V,s,o)}}class nt{constructor(){a(this,"state",{scale:1,x:0,y:0})}getViewCoords(t,r){return[(t-this.state.x)/this.state.scale,(r-this.state.y)/this.state.scale]}getViewScale(){return 1/this.state.scale}getAbsCoords(t,r){return[t*this.state.scale+this.state.x,r*this.state.scale+this.state.y]}getAbsScale(){return this.state.scale}patchState(t,r,s){this.state={scale:t??this.state.scale,x:r??this.state.x,y:s??this.state.y}}}class at{constructor(t){this.transformer=t}getViewCoords(t,r){return this.transformer.getViewCoords(t,r)}getViewScale(){return this.transformer.getViewScale()}getAbsCoords(t,r){return this.transformer.getAbsCoords(t,r)}getAbsScale(){return this.transformer.getAbsScale()}}class ht{constructor(){a(this,"nodes",new Map);a(this,"ports",new Map);a(this,"nodePorts",new Map);a(this,"portNodeId",new Map);a(this,"edges",new Map);a(this,"incommingEdges",new Map);a(this,"outcommingEdges",new Map);a(this,"cycleEdges",new Map)}addNode(t,r,s,o,n,i){this.nodes.set(t,{element:r,x:s,y:o,centerFn:n,priority:i}),this.nodePorts.set(t,new Map)}getNode(t){return this.nodes.get(t)}removeNode(t){this.getNodeAdjacentEdges(t).forEach(o=>{this.removeEdge(o)}),this.nodes.delete(t),this.nodePorts.get(t).forEach((o,n)=>{this.portNodeId.delete(n)}),this.nodePorts.delete(t)}addPort(t,r,s,o,n){this.ports.set(t,{element:r,centerFn:o,direction:n}),this.cycleEdges.set(t,new Set),this.incommingEdges.set(t,new Set),this.outcommingEdges.set(t,new Set),this.portNodeId.set(t,s),this.nodePorts.get(s).set(t,r)}getPort(t){return this.ports.get(t)}getPortNode(t){return this.portNodeId.get(t)}removePort(t){this.cycleEdges.get(t).forEach(s=>{this.removeEdge(s)}),this.cycleEdges.delete(t),this.incommingEdges.get(t).forEach(s=>{this.removeEdge(s)}),this.incommingEdges.delete(t),this.outcommingEdges.get(t).forEach(s=>{this.removeEdge(s)}),this.outcommingEdges.get(t);const r=this.portNodeId.get(t);this.portNodeId.delete(t),this.nodePorts.get(r).delete(t),this.ports.delete(t)}addEdge(t,r,s,o,n){this.edges.set(t,{from:r,to:s,controller:o,priority:n}),r!==s?(this.outcommingEdges.get(r).add(t),this.incommingEdges.get(s).add(t)):this.cycleEdges.get(r).add(t)}getEdge(t){return this.edges.get(t)}removeEdge(t){const r=this.edges.get(t),s=r.from,o=r.to;this.cycleEdges.get(s).delete(t),this.cycleEdges.get(o).delete(t),this.incommingEdges.get(s).delete(t),this.incommingEdges.get(o).delete(t),this.outcommingEdges.get(s).delete(t),this.outcommingEdges.get(o).delete(t),this.edges.delete(t)}getPortAdjacentEdges(t){return[...this.getPortIncomingEdges(t),...this.getPortOutcomingEdges(t),...this.getPortCycleEdges(t)]}getNodeAdjacentEdges(t){return[...this.getNodeIncomingEdges(t),...this.getNodeOutcomingEdges(t),...this.getNodeCycleEdges(t)]}clear(){this.nodes.clear(),this.ports.clear(),this.nodePorts.clear(),this.portNodeId.clear(),this.edges.clear(),this.incommingEdges.clear(),this.outcommingEdges.clear(),this.cycleEdges.clear()}getPortIncomingEdges(t){return Array.from(this.incommingEdges.get(t))}getPortOutcomingEdges(t){return Array.from(this.outcommingEdges.get(t))}getPortCycleEdges(t){return Array.from(this.cycleEdges.get(t))}getNodeIncomingEdges(t){const r=Array.from(this.nodePorts.get(t).keys());let s=[];return r.forEach(o=>{s=[...s,...this.getPortIncomingEdges(o)]}),s}getNodeOutcomingEdges(t){const r=Array.from(this.nodePorts.get(t).keys());let s=[];return r.forEach(o=>{s=[...s,...this.getPortOutcomingEdges(o)]}),s}getNodeCycleEdges(t){const r=Array.from(this.nodePorts.get(t).keys());let s=[];return r.forEach(o=>{s=[...s,...this.getPortCycleEdges(o)]}),s}}const L=e=>{const{top:t,left:r,width:s,height:o}=e.element.getBoundingClientRect(),n=e.centerFn(s,o);return[r+n[0],t+n[1]]},A=(e,t,r)=>[t[0]*e[0]-t[1]*e[1]+((1-t[0])*r[0]+t[1]*r[1]),t[1]*e[0]+t[0]*e[1]+((1-t[0])*r[1]-t[1]*r[0])],N=(e,t,r)=>[t*Math.cos(e),r*Math.sin(e)],T=(e,t,r,s,o)=>{const i=[[0,0],[s,o],[s,-o]].map(d=>A(d,e,[0,0])).map(d=>[d[0]+t,d[1]+r]),h=`M ${i[0][0]} ${i[0][1]}`,c=`L ${i[1][0]} ${i[1][1]}`,l=`L ${i[2][0]} ${i[2][1]}`;return`${h} ${c} ${l}`};class z{constructor(t,r,s,o,n,i,h){a(this,"svg");a(this,"group");a(this,"line");a(this,"sourceArrow",null);a(this,"targetArrow",null);this.color=t,this.width=r,this.curvature=s,this.arrowLength=o,this.arrowWidth=n,this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.style.pointerEvents="none",this.group=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svg.appendChild(this.group),this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("stroke",this.color),this.line.setAttribute("stroke-width",`${this.width}`),this.line.setAttribute("fill","none"),this.group.appendChild(this.line),this.group.style.transformOrigin="50% 50%",i&&(this.sourceArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.sourceArrow.setAttribute("fill",this.color),this.group.appendChild(this.sourceArrow)),h&&(this.targetArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.targetArrow.setAttribute("fill",this.color),this.group.appendChild(this.targetArrow)),this.svg.style.overflow="visible"}update(t,r,s,o,n,i){this.svg.style.width=`${s}px`,this.svg.style.height=`${o}px`;const h=L(n),c=L(i),l=h[0]<=c[0]?1:-1,d=h[1]<=c[1]?1:-1;this.svg.style.transform=`translate(${t}px, ${r}px)`,this.group.style.transform=`scale(${l}, ${d})`;const g=N(n.direction,l,d),u=N(i.direction,l,d),v=A([this.arrowLength,0],g,[0,0]),w=A([s-this.arrowLength,o],u,[s,o]),f=[v[0]+g[0]*this.curvature,v[1]+g[1]*this.curvature],m=[w[0]-u[0]*this.curvature,w[1]-u[1]*this.curvature],E=`M ${v[0]} ${v[1]} C ${f[0]} ${f[1]}, ${m[0]} ${m[1]}, ${w[0]} ${w[1]}`,p=this.sourceArrow?"":`M 0 0 L ${v[0]} ${v[1]} `,y=this.targetArrow?"":` M ${w[0]} ${w[1]} L ${s} ${o}`,b=`${p}${E}${y}`;if(this.line.setAttribute("d",b),this.sourceArrow){const S=T(g,0,0,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",S)}if(this.targetArrow){const S=T(u,s,o,-this.arrowLength,this.arrowWidth);this.targetArrow.setAttribute("d",S)}}}const W=(e,t)=>{const r=[];if(e.length>0&&r.push(`M ${e[0][0]} ${e[0][1]}`),e.length===2&&r.push(`L ${e[1][0]} ${e[1][1]}`),e.length>2){const s=e.length-1;let o=0,n=0,i=0;e.forEach((h,c)=>{let l=0,d=0,g=0;const u=c>0,v=c<s,w=u&&v;if(u&&(l=-o,d=-n,g=i),v){const S=e[c+1];o=S[0]-h[0],n=S[1]-h[1],i=Math.sqrt(o*o+n*n)}const m=Math.min((w?t:0)/i,c<s-1?.5:1),E=w?[h[0]+o*m,h[1]+n*m]:h,y=Math.min((w?t:0)/g,c>1?.5:1),b=w?[h[0]+l*y,h[1]+d*y]:h;c>0&&r.push(`L ${b[0]} ${b[1]}`),w&&r.push(`C ${h[0]} ${h[1]} ${h[0]} ${h[1]} ${E[0]} ${E[1]}`)})}return r.join(" ")};class X{constructor(t,r,s,o,n,i,h,c){a(this,"svg");a(this,"group");a(this,"line");a(this,"sourceArrow",null);a(this,"targetArrow",null);this.color=t,this.width=r,this.arrowLength=s,this.arrowWidth=o,this.arrowOffset=n,this.roundness=c,this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.style.pointerEvents="none",this.group=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svg.appendChild(this.group),this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("stroke",this.color),this.line.setAttribute("stroke-width",`${this.width}`),this.line.setAttribute("fill","none"),this.group.appendChild(this.line),this.group.style.transformOrigin="50% 50%",i&&(this.sourceArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.sourceArrow.setAttribute("fill",this.color),this.group.appendChild(this.sourceArrow)),h&&(this.targetArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.targetArrow.setAttribute("fill",this.color),this.group.appendChild(this.targetArrow)),this.svg.style.overflow="visible"}update(t,r,s,o,n,i){this.svg.style.width=`${s}px`,this.svg.style.height=`${o}px`;const h=L(n),c=L(i),l=h[0]<=c[0]?1:-1,d=h[1]<=c[1]?1:-1;this.svg.style.transform=`translate(${t}px, ${r}px)`,this.group.style.transform=`scale(${l}, ${d})`;const g=N(n.direction,l,d),u=N(i.direction,l,d),v=this.sourceArrow?A([this.arrowLength,0],g,[0,0]):[0,0],w=this.targetArrow?A([s-this.arrowLength,o],u,[s,o]):[s,o],f=this.arrowLength+this.arrowOffset,m=A([f,0],g,[0,0]),E=A([s-f,o],u,[s,o]),p=W([v,m,E,w],this.roundness);if(this.line.setAttribute("d",p),this.sourceArrow){const y=T(g,0,0,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",y)}if(this.targetArrow){const y=T(u,s,o,-this.arrowLength,this.arrowWidth);this.targetArrow.setAttribute("d",y)}}}class G{constructor(t,r,s,o,n,i,h,c){a(this,"svg");a(this,"group");a(this,"line");a(this,"sourceArrow",null);a(this,"targetArrow",null);this.color=t,this.width=r,this.arrowLength=s,this.arrowWidth=o,this.arrowOffset=n,this.roundness=c,this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.style.pointerEvents="none",this.group=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svg.appendChild(this.group),this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("stroke",this.color),this.line.setAttribute("stroke-width",`${this.width}`),this.line.setAttribute("fill","none"),this.group.appendChild(this.line),this.group.style.transformOrigin="50% 50%",i&&(this.sourceArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.sourceArrow.setAttribute("fill",this.color),this.group.appendChild(this.sourceArrow)),h&&(this.targetArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.targetArrow.setAttribute("fill",this.color),this.group.appendChild(this.targetArrow)),this.svg.style.overflow="visible"}update(t,r,s,o,n,i){this.svg.style.width=`${s}px`,this.svg.style.height=`${o}px`;const h=L(n),c=L(i),l=h[0]<=c[0]?1:-1,d=h[1]<=c[1]?1:-1;this.svg.style.transform=`translate(${t}px, ${r}px)`,this.group.style.transform=`scale(${l}, ${d})`;const g=N(n.direction,l,d),u=N(i.direction,l,d),v=this.sourceArrow?A([this.arrowLength,0],g,[0,0]):[0,0],w=this.targetArrow?A([s-this.arrowLength,o],u,[s,o]):[s,o],f=this.arrowLength+this.arrowOffset,m=A([f,0],g,[0,0]),E=A([s-f,o],u,[s,o]),p=Math.max((m[0]+E[0])/2,f),y=o/2,b=[l>0?p:-f,m[1]],S=[b[0],y],M=[l>0?s-p:s+f,E[1]],D=[M[0],y],V=W([v,m,b,S,D,M,E,w],this.roundness);if(this.line.setAttribute("d",V),this.sourceArrow){const x=T(g,0,0,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",x)}if(this.targetArrow){const x=T(u,s,o,-this.arrowLength,this.arrowWidth);this.targetArrow.setAttribute("d",x)}}}class j{constructor(t,r,s,o,n,i,h,c){a(this,"svg");a(this,"group");a(this,"line");a(this,"sourceArrow",null);a(this,"targetArrow",null);this.color=t,this.width=r,this.arrowLength=s,this.arrowWidth=o,this.arrowOffset=n,this.roundness=c,this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.style.pointerEvents="none",this.group=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svg.appendChild(this.group),this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("stroke",this.color),this.line.setAttribute("stroke-width",`${this.width}`),this.line.setAttribute("fill","none"),this.group.appendChild(this.line),this.group.style.transformOrigin="50% 50%",i&&(this.sourceArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.sourceArrow.setAttribute("fill",this.color),this.group.appendChild(this.sourceArrow)),h&&(this.targetArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.targetArrow.setAttribute("fill",this.color),this.group.appendChild(this.targetArrow)),this.svg.style.overflow="visible"}update(t,r,s,o,n,i){this.svg.style.width=`${s}px`,this.svg.style.height=`${o}px`;const h=L(n),c=L(i),l=h[0]<=c[0]?1:-1,d=h[1]<=c[1]?1:-1;this.svg.style.transform=`translate(${t}px, ${r}px)`,this.group.style.transform=`scale(${l}, ${d})`;const g=N(n.direction,l,d),u=N(i.direction,l,d),v=this.sourceArrow?A([this.arrowLength,0],g,[0,0]):[0,0],w=this.targetArrow?A([s-this.arrowLength,o],u,[s,o]):[s,o],f=this.arrowLength+this.arrowOffset,m=A([f,0],g,[0,0]),E=A([s-f,o],u,[s,o]),p=Math.max((m[1]+E[1])/2,f),y=s/2,b=[m[0],d>0?p:-f],S=[y,b[1]],M=[E[0],d>0?o-p:o+f],D=[y,M[1]],V=W([v,m,b,S,D,M,E,w],this.roundness);if(this.line.setAttribute("d",V),this.sourceArrow){const x=T(g,0,0,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",x)}if(this.targetArrow){const x=T(u,s,o,-this.arrowLength,this.arrowWidth);this.targetArrow.setAttribute("d",x)}}}class H{constructor(t,r,s,o,n,i,h){a(this,"svg");a(this,"group");a(this,"line");a(this,"arrow",null);this.color=t,this.width=r,this.arrowLength=s,this.arrowWidth=o,this.radius=i,this.smallRadius=h,this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.style.pointerEvents="none",this.group=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svg.appendChild(this.group),this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("stroke",this.color),this.line.setAttribute("stroke-width",`${this.width}`),this.line.setAttribute("fill","none"),this.group.appendChild(this.line),this.group.style.transformOrigin="50% 50%",n&&(this.arrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.arrow.setAttribute("fill",this.color),this.group.appendChild(this.arrow)),this.svg.style.overflow="visible",this.svg.style.width="0px",this.svg.style.height="0px"}update(t,r,s,o,n){this.svg.style.transform=`translate(${t}px, ${r}px)`;const i=N(n.direction,1,1),h=this.smallRadius,c=this.radius,l=Math.sqrt(h*h+c*c),d=h+c,g=this.arrowLength+l*(1-c/d),u=h*c/d,w=[[this.arrowLength,0],[g,u],[g,-u]].map(p=>A(p,i,[0,0])),f=[`M ${w[0][0]} ${w[0][1]}`,`A ${h} ${h} 0 0 1 ${w[1][0]} ${w[1][1]}`,`A ${c} ${c} 0 1 0 ${w[2][0]} ${w[2][1]}`,`A ${h} ${h} 0 0 1 ${w[0][0]} ${w[0][1]}`].join(" "),m=`M 0 0 L ${w[0][0]} ${w[0][1]} `,E=`${this.arrow!==null?"":m}${f}`;if(this.line.setAttribute("d",E),this.arrow){const p=T(i,0,0,this.arrowLength,this.arrowWidth);this.arrow.setAttribute("d",p)}}}class k{constructor(t,r,s,o,n,i,h,c){a(this,"svg");a(this,"group");a(this,"line");a(this,"arrow",null);a(this,"roundness");a(this,"linePoints");this.color=t,this.width=r,this.arrowLength=s,this.arrowWidth=o,this.side=i,this.minPortOffset=h,this.roundness=Math.min(c,this.minPortOffset,this.side/2),this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.style.pointerEvents="none",this.group=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svg.appendChild(this.group),this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("stroke",this.color),this.line.setAttribute("stroke-width",`${this.width}`),this.line.setAttribute("fill","none"),this.group.appendChild(this.line),this.group.style.transformOrigin="50% 50%",n&&(this.arrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.arrow.setAttribute("fill",this.color),this.group.appendChild(this.arrow)),this.svg.style.overflow="visible",this.svg.style.width="0px",this.svg.style.height="0px";const l=this.minPortOffset,d=this.side,g=this.arrowLength+l,u=this.roundness,v=g+2*d;console.log(u),this.linePoints=[[this.arrowLength,0],[g,0],[g,this.side],[v,this.side],[v,-this.side],[g,-this.side],[g,0],[this.arrowLength,0]]}update(t,r,s,o,n){this.svg.style.transform=`translate(${t}px, ${r}px)`;const i=N(n.direction,1,1),h=this.linePoints.map(d=>A(d,i,[0,0])),c=`M 0 0 L ${h[0][0]} ${h[0][1]} `,l=`${this.arrow?"":c}${W(h,this.roundness)}`;if(this.line.setAttribute("d",l),this.arrow){const d=T(i,0,0,this.arrowLength,this.arrowWidth);this.arrow.setAttribute("d",d)}}}class R{constructor(t,r,s,o,n,i,h,c,l,d){a(this,"svg");a(this,"group");a(this,"line");a(this,"sourceArrow",null);a(this,"targetArrow",null);a(this,"detourX");a(this,"detourY");this.color=t,this.width=r,this.arrowLength=s,this.arrowWidth=o,this.arrowOffset=n,this.roundness=c,this.detourX=Math.cos(d)*l,this.detourY=Math.sin(d)*l,this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.style.pointerEvents="none",this.group=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svg.appendChild(this.group),this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("stroke",this.color),this.line.setAttribute("stroke-width",`${this.width}`),this.line.setAttribute("fill","none"),this.group.appendChild(this.line),this.group.style.transformOrigin="50% 50%",i&&(this.sourceArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.sourceArrow.setAttribute("fill",this.color),this.group.appendChild(this.sourceArrow)),h&&(this.targetArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.targetArrow.setAttribute("fill",this.color),this.group.appendChild(this.targetArrow)),this.svg.style.overflow="visible"}update(t,r,s,o,n,i){this.svg.style.width=`${s}px`,this.svg.style.height=`${o}px`;const h=L(n),c=L(i),l=h[0]<=c[0]?1:-1,d=h[1]<=c[1]?1:-1;this.svg.style.transform=`translate(${t}px, ${r}px)`,this.group.style.transform=`scale(${l}, ${d})`;const g=N(n.direction,l,d),u=N(i.direction,l,d),v=this.sourceArrow?A([this.arrowLength,0],g,[0,0]):[0,0],w=this.targetArrow?A([s-this.arrowLength,o],u,[s,o]):[s,o],f=this.arrowLength+this.arrowOffset,m=A([f,0],g,[0,0]),E=[m[0]+this.detourX,m[1]+this.detourY],p=A([s-f,o],u,[s,o]),y=[p[0]+this.detourX,p[1]+this.detourY],b=W([v,m,E,y,p,w],this.roundness);if(this.line.setAttribute("d",b),this.sourceArrow){const S=T(g,0,0,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",S)}if(this.targetArrow){const S=T(u,s,o,-this.arrowLength,this.arrowWidth);this.targetArrow.setAttribute("d",S)}}}var P=(e=>(e.Regular="regular",e.PortCycle="port-cycle",e.NodeCycle="node-cycle",e))(P||{});class ct{constructor(t,r,s,o,n,i,h,c,l){a(this,"svg");a(this,"group");a(this,"line");a(this,"sourceArrow",null);a(this,"targetArrow",null);a(this,"detourX");a(this,"detourY");this.color=t,this.width=r,this.curvature=s,this.arrowLength=o,this.arrowWidth=n,this.detourX=Math.cos(l)*c,this.detourY=Math.sin(l)*c,this.svg=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svg.style.pointerEvents="none",this.group=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svg.appendChild(this.group),this.line=document.createElementNS("http://www.w3.org/2000/svg","path"),this.line.setAttribute("stroke",this.color),this.line.setAttribute("stroke-width",`${this.width}`),this.line.setAttribute("fill","none"),this.group.appendChild(this.line),this.group.style.transformOrigin="50% 50%",i&&(this.sourceArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.sourceArrow.setAttribute("fill",this.color),this.group.appendChild(this.sourceArrow)),h&&(this.targetArrow=document.createElementNS("http://www.w3.org/2000/svg","path"),this.targetArrow.setAttribute("fill",this.color),this.group.appendChild(this.targetArrow)),this.svg.style.overflow="visible"}update(t,r,s,o,n,i){this.svg.style.width=`${s}px`,this.svg.style.height=`${o}px`;const h=L(n),c=L(i),l=h[0]<=c[0]?1:-1,d=h[1]<=c[1]?1:-1;this.svg.style.transform=`translate(${t}px, ${r}px)`,this.group.style.transform=`scale(${l}, ${d})`;const g=N(n.direction,l,d),u=N(i.direction,l,d),v=this.sourceArrow?A([this.arrowLength,0],g,[0,0]):[0,0],w=this.targetArrow?A([s-this.arrowLength,o],u,[s,o]):[s,o],f=this.arrowLength,m=A([f,0],g,[0,0]),E=[m[0]+this.detourX,m[1]+this.detourY],p=A([s-f,o],u,[s,o]),y=[p[0]+this.detourX,p[1]+this.detourY],b=[(E[0]+y[0])/2,(E[1]+y[1])/2],S=[m[0]-this.curvature*Math.cos(n.direction),m[1]-this.curvature*Math.sin(n.direction)],M=[p[0]+this.curvature*Math.cos(i.direction),p[1]+this.curvature*Math.sin(i.direction)],D=[m[0]+this.detourX,m[1]+this.detourY],V=[p[0]+this.detourX,p[1]+this.detourY],x=[`M ${v[0]} ${v[1]}`,`L ${m[0]} ${m[1]}`,`C ${S[0]} ${S[1]} ${D[0]} ${D[1]} ${b[0]} ${b[1]}`,`C ${V[0]} ${V[1]} ${M[0]} ${M[1]} ${p[0]} ${p[1]}`,`L ${w[0]} ${w[1]}`].join(" ");if(this.line.setAttribute("d",x),this.sourceArrow){const Y=T(g,0,0,this.arrowLength,this.arrowWidth);this.sourceArrow.setAttribute("d",Y)}if(this.targetArrow){const Y=T(u,s,o,-this.arrowLength,this.arrowWidth);this.targetArrow.setAttribute("d",Y)}}}const U=e=>t=>t===P.PortCycle?new H(e.color,e.width,e.arrowLength,e.arrowWidth,e.hasSourceArrow||e.hasTargetArrow,e.cycleRadius,e.smallCycleRadius):t===P.NodeCycle?new ct(e.color,e.width,e.curvature,e.arrowLength,e.arrowWidth,e.hasSourceArrow,e.hasTargetArrow,e.detourDistance,e.detourDirection):new z(e.color,e.width,e.curvature,e.arrowLength,e.arrowWidth,e.hasSourceArrow,e.hasTargetArrow),_=e=>t=>t===P.PortCycle?new k(e.color,e.width,e.arrowLength,e.arrowWidth,e.hasSourceArrow||e.hasTargetArrow,e.cycleSquareSide,e.arrowOffset,e.roundness):t===P.NodeCycle?new R(e.color,e.width,e.arrowLength,e.arrowWidth,e.arrowOffset,e.hasSourceArrow,e.hasTargetArrow,e.roundness,e.detourDistance,e.detourDirection):new X(e.color,e.width,e.arrowLength,e.arrowWidth,e.arrowOffset,e.hasSourceArrow,e.hasTargetArrow,e.roundness),J=e=>t=>t===P.PortCycle?new k(e.color,e.width,e.arrowLength,e.arrowWidth,e.hasSourceArrow||e.hasTargetArrow,e.cycleSquareSide,e.arrowOffset,e.roundness):t===P.NodeCycle?new R(e.color,e.width,e.arrowLength,e.arrowWidth,e.arrowOffset,e.hasSourceArrow,e.hasTargetArrow,e.roundness,e.detourDistance,e.detourDirection):new G(e.color,e.width,e.arrowLength,e.arrowWidth,e.arrowOffset,e.hasSourceArrow,e.hasTargetArrow,e.roundness),K=e=>t=>t===P.PortCycle?new k(e.color,e.width,e.arrowLength,e.arrowWidth,e.hasSourceArrow||e.hasTargetArrow,e.cycleSquareSide,e.arrowOffset,e.roundness):t===P.NodeCycle?new R(e.color,e.width,e.arrowLength,e.arrowWidth,e.arrowOffset,e.hasSourceArrow,e.hasTargetArrow,e.roundness,e.detourDistance,e.detourDirection):new j(e.color,e.width,e.arrowLength,e.arrowWidth,e.arrowOffset,e.hasSourceArrow,e.hasTargetArrow,e.roundness);class B{constructor(){a(this,"counter",0)}create(){const t=`${this.counter}`;return this.counter++,t}reset(){this.counter=0}}class lt{constructor(){a(this,"priority",0)}create(){const t=this.priority;return this.priority++,t}push(t){this.priority=Math.max(this.priority,t)}reset(){this.priority=0}}class dt{constructor(t,r,s,o,n,i){a(this,"nodeIdGenerator",new B);a(this,"portIdGenerator",new B);a(this,"edgeIdGenerator",new B);a(this,"priorityGenerator",new lt);this.graphStore=t,this.htmlController=r,this.viewportTransformer=s,this.nodesCenterFn=o,this.portsCenterFn=n,this.portsDirection=i}moveNodeOnTop(t){const r=this.graphStore.getNode(t);if(r===void 0)throw new Error("failed to move on top nonexisting node");const s=this.priorityGenerator.create(),o=this.priorityGenerator.create();r.priority=o,this.htmlController.updateNodePriority(t),this.graphStore.getNodeAdjacentEdges(t).forEach(i=>{const h=this.graphStore.getEdge(i);h.priority=s,this.htmlController.updateEdgePriority(i)})}addNode(t,r,s,o,n,i,h){if(t===void 0)do t=this.nodeIdGenerator.create();while(this.graphStore.getNode(t)!==void 0);if(this.graphStore.getNode(t)!==void 0)throw new Error("failed to add node with existing id");h!==void 0&&this.priorityGenerator.push(h),this.graphStore.addNode(t,r,s,o,i??this.nodesCenterFn,h??this.priorityGenerator.create()),this.htmlController.attachNode(t),n!==void 0&&n.forEach((c,l)=>{c instanceof HTMLElement?this.markPort(l,c,t,this.portsCenterFn,this.portsDirection):this.markPort(l,c.element,t,c.centerFn??this.portsCenterFn,c.direction??this.portsDirection)})}updateNode(t,r,s,o,n){const i=this.graphStore.getNode(t);if(i===void 0)throw new Error("failed to update nonexisting node");i.x=r??i.x,i.y=s??i.y,i.centerFn=n??i.centerFn,this.htmlController.updateNodeCoordinates(t),o!==void 0&&(i.priority=o,this.htmlController.updateNodePriority(t))}markPort(t,r,s,o,n){if(t===void 0)do t=this.portIdGenerator.create();while(this.graphStore.getPort(t)!==void 0);if(this.graphStore.getNode(s)===void 0)throw new Error("failed to set port on nonexisting node");if(this.graphStore.getPort(t)!==void 0)throw new Error("failed to add port with existing id");this.graphStore.addPort(t,r,s,o??this.portsCenterFn,n??0)}updatePort(t,r){const s=this.graphStore.getPort(t);if(s===void 0)throw new Error("failed to unset nonexisting port");s.direction=(r==null?void 0:r.direction)??s.direction,s.centerFn=(r==null?void 0:r.centerFn)??s.centerFn,this.htmlController.updatePortEdges(t)}unmarkPort(t){if(this.graphStore.getPort(t)===void 0)throw new Error("failed to unset nonexisting port");this.graphStore.getPortAdjacentEdges(t).forEach(r=>{this.removeEdge(r)}),this.graphStore.removePort(t)}addEdge(t,r,s,o,n){if(t===void 0)do t=this.edgeIdGenerator.create();while(this.graphStore.getEdge(t)!==void 0);if(this.graphStore.getPort(r)===void 0)throw new Error("failed to add edge from nonexisting port");if(this.graphStore.getPort(s)===void 0)throw new Error("failed to add edge to nonexisting port");if(this.graphStore.getEdge(t)!==void 0)throw new Error("failed to add edge with existing id");let i=P.Regular;const h=this.graphStore.getPortNode(r),c=this.graphStore.getPortNode(s);r===s?i=P.PortCycle:h===c&&(i=P.NodeCycle),n!==void 0&&this.priorityGenerator.push(n),this.graphStore.addEdge(t,r,s,o(i),n??this.priorityGenerator.create()),this.htmlController.attachEdge(t)}updateEdge(t,r){const s=this.graphStore.getEdge(t);if(s===void 0)throw new Error("failed to update nonexisting edge");r.controller!==void 0&&(this.htmlController.detachEdge(t),s.controller=r.controller,this.htmlController.attachEdge(t)),r.priority!==void 0&&(s.priority=r.priority,this.htmlController.updateEdgePriority(t))}removeEdge(t){if(this.graphStore.getEdge(t)===void 0)throw new Error("failed to remove nonexisting edge");this.htmlController.detachEdge(t),this.graphStore.removeEdge(t)}removeNode(t){if(this.graphStore.getNode(t)===void 0)throw new Error("failed to remove nonexisting node");this.htmlController.detachNode(t),this.graphStore.removeNode(t)}patchViewportState(t,r,s){this.viewportTransformer.patchState(t,r,s),this.htmlController.applyTransform()}moveToNodes(t){if(t.length===0)return;const r=t.map(u=>this.graphStore.getNode(u)).filter(u=>u!==void 0);if(r.length<t.length)throw new Error("failed to move to nonexisting node");const[s,o]=r.reduce((u,v)=>[u[0]+v.x,u[1]+v.y],[0,0]),n=s/r.length,i=o/r.length,[h,c]=this.htmlController.getViewportDimensions(),l=this.viewportTransformer.getAbsScale(),d=n-l*h/2,g=i-l*c/2;this.patchViewportState(null,d,g)}attach(t){this.htmlController.attach(t)}detach(){this.htmlController.detach()}clear(){this.htmlController.clear(),this.graphStore.clear(),this.nodeIdGenerator.reset(),this.portIdGenerator.reset(),this.edgeIdGenerator.reset(),this.priorityGenerator.reset()}destroy(){this.htmlController.destroy()}}class gt{constructor(t,r,s,o,n){a(this,"publicViewportTransformer");a(this,"canvasController");const i=new nt,h=new ht;this.publicViewportTransformer=new at(i);const c=new it(h,i,this.publicViewportTransformer,t,r);this.canvasController=new dt(h,c,i,s,o,n)}}const Q=(e,t)=>[e/2,t/2],ut=()=>()=>{},wt=(e,t,r,s,o,n)=>{e.clearRect(0,0,e.canvas.width,e.canvas.height),e.fillStyle=n,e.fillRect(0,0,e.canvas.width,e.canvas.height);const i=t.getViewCoords(0,0),h=t.getViewScale(),c=s*h;let l=0,d=0,g=c/2;do g*=2,l=e.canvas.width/g,d=e.canvas.height/g;while(l*d>1e4);const u=i[0]-Math.floor(i[0]/g)*g,v=i[1]-Math.floor(i[1]/g)*g,w=o*h,f=2*Math.PI,m=u-g,E=v-g,p=e.canvas.width+u,y=e.canvas.height+v;e.fillStyle=r;for(let b=m;b<=p;b+=g)for(let S=E;S<=y;S+=g)e.beginPath(),e.arc(b,S,w,0,f),e.closePath(),e.fill()},mt=(e,t,r,s)=>(o,n)=>{wt(o,n,e,t,r,s)},vt=(e,t)=>{e.fillStyle=t,e.fillRect(0,0,e.canvas.width,e.canvas.height)},pt=e=>t=>{vt(t,e)},ft=e=>{switch(e==null?void 0:e.type){case"custom":return e.drawingFn;case"dots":return mt(e.dotColor??"#d8d8d8",e.dotGap??25,e.dotRadius??1.5,e.color??"#ffffff");case"color":return pt(e.color??"#ffffff");default:return ut()}},Z=e=>{switch(e==null?void 0:e.type){case"custom":return e.controllerFactory;case"straight":return _({color:e.color??"#5c5c5c",width:e.width??1,arrowLength:e.arrowLength??15,arrowWidth:e.arrowWidth??4,arrowOffset:e.arrowOffset??15,hasSourceArrow:e.hasSourceArrow??!1,hasTargetArrow:e.hasTargetArrow??!1,cycleSquareSide:e.cycleSquareSide??30,roundness:e.roundness??10,detourDistance:e.detourDistance??100,detourDirection:e.detourDirection??-Math.PI/2});case"horizontal":return J({color:e.color??"#5c5c5c",width:e.width??1,arrowLength:e.arrowLength??15,arrowWidth:e.arrowWidth??4,arrowOffset:e.arrowOffset??15,hasSourceArrow:e.hasSourceArrow??!1,hasTargetArrow:e.hasTargetArrow??!1,cycleSquareSide:e.cycleSquareSide??30,roundness:e.roundness??10,detourDistance:e.detourDistance??100,detourDirection:e.detourDirection??-Math.PI/2});case"vertical":return K({color:e.color??"#5c5c5c",width:e.width??1,arrowLength:e.arrowLength??15,arrowWidth:e.arrowWidth??4,arrowOffset:e.arrowOffset??15,hasSourceArrow:e.hasSourceArrow??!1,hasTargetArrow:e.hasTargetArrow??!1,cycleSquareSide:e.cycleSquareSide??30,roundness:e.roundness??10,detourDistance:e.detourDistance??100,detourDirection:e.detourDirection??-Math.PI/2});default:return U({color:e.color??"#5c5c5c",width:e.width??1,curvature:e.curvature??90,arrowLength:e.arrowLength??15,arrowWidth:e.arrowWidth??4,hasSourceArrow:e.hasSourceArrow??!1,hasTargetArrow:e.hasTargetArrow??!1,cycleRadius:e.cycleRadius??30,smallCycleRadius:e.smallCycleRadius??15,detourDistance:e.detourDistance??100,detourDirection:e.detourDirection??-Math.PI/2})}},Et=e=>{var t,r,s,o;return{background:{drawingFn:ft(e.background??{type:"none"})},nodes:{centerFn:((t=e.nodes)==null?void 0:t.centerFn)??Q},ports:{centerFn:((r=e.ports)==null?void 0:r.centerFn)??Q,direction:((s=e.ports)==null?void 0:s.direction)??0},edges:{controllerFactory:Z(e.edges??{})},layers:{mode:((o=e.layers)==null?void 0:o.mode)??"edges-follow-node"}}};class q{constructor(t){a(this,"transformation");a(this,"di");a(this,"edgeControllerFactory");this.apiOptions=t;const r=Et(this.apiOptions??{});this.di=new gt(r.layers.mode,r.background.drawingFn,r.nodes.centerFn,r.ports.centerFn,r.ports.direction),this.transformation=this.di.publicViewportTransformer,this.edgeControllerFactory=r.edges.controllerFactory}addNode(t){return this.di.canvasController.addNode(t.id,t.element,t.x,t.y,t.ports,t.centerFn,t.priority),this}updateNode(t,r){return this.di.canvasController.updateNode(t,r.x,r.y,r.priority,r.centerFn),this}removeNode(t){return this.di.canvasController.removeNode(t),this}moveNodeOnTop(t){return this.di.canvasController.moveNodeOnTop(t),this}markPort(t){return this.di.canvasController.markPort(t.id,t.element,t.nodeId,t.centerFn,t.direction),this}updatePort(t,r){return this.di.canvasController.updatePort(t,r),this}unmarkPort(t){return this.di.canvasController.unmarkPort(t),this}addEdge(t){const r=t.options!==void 0?Z(t.options):this.edgeControllerFactory;return this.di.canvasController.addEdge(t.id,t.from,t.to,r,t.priority),this}updateEdge(t,r){return this.di.canvasController.updateEdge(t,r),this}removeEdge(t){return this.di.canvasController.removeEdge(t),this}patchViewportState(t){return this.di.canvasController.patchViewportState(t.scale??null,t.x??null,t.y??null),this}moveToNodes(t){return this.di.canvasController.moveToNodes(t),this}clear(){return this.di.canvasController.clear(),this}attach(t){return this.di.canvasController.attach(t),this}detach(){return this.di.canvasController.detach(),this}destroy(){this.di.canvasController.destroy()}}class I{constructor(t,r){a(this,"transformation");a(this,"nodes",new Map);a(this,"grabbedNodeId",null);a(this,"onNodeDrag");a(this,"onBeforeNodeDrag");a(this,"nodeIdGenerator",new B);a(this,"element",null);a(this,"onCanvasMouseUp",()=>{this.setCursor(null),this.grabbedNodeId=null});a(this,"onCanvasMouseMove",t=>{this.grabbedNodeId!==null&&(t.stopPropagation(),this.dragNode(this.grabbedNodeId,t.movementX,t.movementY))});a(this,"onCanvasTouchStart",t=>{this.previousTouchCoords=[t.touches[0].clientX,t.touches[0].clientY]});a(this,"onCanvasTouchMove",t=>{if(this.grabbedNodeId===null||t.touches.length!==1||this.previousTouchCoords===null)return;t.stopImmediatePropagation();const[r,s]=[t.touches[0].clientX-this.previousTouchCoords[0],t.touches[0].clientY-this.previousTouchCoords[1]];this.dragNode(this.grabbedNodeId,r,s),this.previousTouchCoords=[t.touches[0].clientX,t.touches[0].clientY]});a(this,"onCanvasTouchEnd",t=>{t.touches.length>0?this.previousTouchCoords=[t.touches[0].clientX,t.touches[0].clientY]:(this.previousTouchCoords=null,this.grabbedNodeId=null)});a(this,"previousTouchCoords",null);var n,i;this.canvas=t,this.transformation=this.canvas.transformation;const s=()=>{};this.onNodeDrag=((n=r==null?void 0:r.events)==null?void 0:n.onNodeDrag)??s;const o=()=>!0;this.onBeforeNodeDrag=((i=r==null?void 0:r.events)==null?void 0:i.onBeforeNodeDrag)??o}addNode(t){let r=t.id;if(r===void 0)do r=this.nodeIdGenerator.create();while(this.nodes.has(r));this.canvas.addNode(t);const s=n=>{this.onBeforeNodeDrag({nodeId:r,element:t.element,x:t.x,y:t.y})&&(n.stopImmediatePropagation(),this.grabbedNodeId=r,this.setCursor("grab"),this.canvas.moveNodeOnTop(r))},o=n=>{this.onBeforeNodeDrag({nodeId:r,element:t.element,x:t.x,y:t.y})&&n.touches.length===1&&(this.grabbedNodeId=r,this.canvas.moveNodeOnTop(r))};return this.nodes.set(r,{element:t.element,onMouseDown:s,onTouchStart:o,x:t.x,y:t.y}),t.element.addEventListener("mousedown",s),t.element.addEventListener("touchstart",o),this}updateNode(t,r){return this.canvas.updateNode(t,r),this}removeNode(t){const r=this.nodes.get(t);return r!==void 0&&(r.element.removeEventListener("mousedown",r.onMouseDown),r.element.removeEventListener("touchstart",r.onTouchStart)),this.canvas.removeNode(t),this.nodes.delete(t),this}markPort(t){return this.canvas.markPort(t),this}updatePort(t,r){return this.canvas.updatePort(t,r),this}unmarkPort(t){return this.canvas.unmarkPort(t),this}addEdge(t){return this.canvas.addEdge(t),this}updateEdge(t,r){return this.canvas.updateEdge(t,r),this}removeEdge(t){return this.canvas.removeEdge(t),this}patchViewportState(t){return this.canvas.patchViewportState(t),this}moveToNodes(t){return this.canvas.moveToNodes(t),this}moveNodeOnTop(t){return this.canvas.moveNodeOnTop(t),this}clear(){return this.canvas.clear(),this.nodes.forEach(t=>{t.element.removeEventListener("mousedown",t.onMouseDown),t.element.removeEventListener("touchstart",t.onTouchStart)}),this.nodes.clear(),this}attach(t){return this.detach(),this.element=t,this.canvas.attach(this.element),this.element.addEventListener("mouseup",this.onCanvasMouseUp),this.element.addEventListener("mousemove",this.onCanvasMouseMove),this.element.addEventListener("touchstart",this.onCanvasTouchStart),this.element.addEventListener("touchmove",this.onCanvasTouchMove),this.element.addEventListener("touchend",this.onCanvasTouchEnd),this.element.addEventListener("touchcancel",this.onCanvasTouchEnd),this}detach(){return this.canvas.detach(),this.element!==null&&(this.element.removeEventListener("mouseup",this.onCanvasMouseUp),this.element.removeEventListener("mousemove",this.onCanvasMouseMove),this.element.removeEventListener("touchstart",this.onCanvasTouchStart),this.element.removeEventListener("touchmove",this.onCanvasTouchMove),this.element.removeEventListener("touchend",this.onCanvasTouchEnd),this.element.removeEventListener("touchcancel",this.onCanvasTouchEnd),this.element=null),this}destroy(){this.detach(),this.nodes.forEach(t=>{t.element.removeEventListener("mousedown",t.onMouseDown),t.element.removeEventListener("touchstart",t.onTouchStart)}),this.nodes.clear(),this.canvas.destroy()}setCursor(t){this.element!==null&&(t!==null?this.element.style.cursor=t:this.element.style.removeProperty("cursor"))}dragNode(t,r,s){const o=this.nodes.get(t);if(o===void 0)throw new Error("failed to drag nonexisting node");const[n,i]=this.transformation.getViewCoords(o.x,o.y),h=n+r,c=i+s,[l,d]=this.transformation.getAbsCoords(h,c);o.x=l,o.y=d,this.canvas.updateNode(t,{x:l,y:d}),this.onNodeDrag({nodeId:t,element:o.element,x:o.x,y:o.y})}}class tt{constructor(t,r){a(this,"transformation");a(this,"element",null);a(this,"isMoving",!1);a(this,"prevTouches",null);a(this,"onTransform");a(this,"onBeforeTransform");a(this,"isScalable");a(this,"isShiftable");a(this,"minViewScale");a(this,"maxViewScale");a(this,"wheelSensitivity");a(this,"onMouseDown",()=>{this.setCursor("grab"),this.isMoving=!0});a(this,"onMouseMove",t=>{if(!this.isMoving||!this.isShiftable)return;const r=-t.movementX,s=-t.movementY;this.moveViewport(r,s)});a(this,"onMouseUp",()=>{this.setCursor(null),this.isMoving=!1});a(this,"onWheelScroll",t=>{if(this.element===null||this.isScalable===!1)return;t.preventDefault();const{left:r,top:s}=this.element.getBoundingClientRect(),o=t.clientX-r,n=t.clientY-s,h=1/(t.deltaY<0?this.wheelSensitivity:1/this.wheelSensitivity);this.scaleViewport(h,o,n)});a(this,"onTouchStart",t=>{this.prevTouches=this.getAverageTouch(t)});a(this,"onTouchMove",t=>{if(this.prevTouches===null||this.element===null||!this.isShiftable)return;const r=this.getAverageTouch(t);if((r.touchesCnt===1||r.touchesCnt===2)&&this.moveViewport(-(r.x-this.prevTouches.x),-(r.y-this.prevTouches.y)),r.touchesCnt===2&&this.isScalable){const{left:s,top:o}=this.element.getBoundingClientRect(),n=this.prevTouches.x-s,i=this.prevTouches.y-o,c=1/(r.scale/this.prevTouches.scale);this.scaleViewport(c,n,i)}this.prevTouches=r,t.preventDefault()});a(this,"onTouchEnd",t=>{t.touches.length>0?this.prevTouches=this.getAverageTouch(t):this.prevTouches=null});var c,l,d,g,u,v,w,f,m,E,p,y;this.canvas=t,this.options=r,this.transformation=this.canvas.transformation;const s=((l=(c=this.options)==null?void 0:c.scale)==null?void 0:l.min)??null,o=((g=(d=this.options)==null?void 0:d.scale)==null?void 0:g.max)??null;this.isScalable=((v=(u=this.options)==null?void 0:u.scale)==null?void 0:v.enabled)!==!1,this.minViewScale=o!==null?1/o:null,this.maxViewScale=s!==null?1/s:null,this.isShiftable=((f=(w=this.options)==null?void 0:w.shift)==null?void 0:f.enabled)!==!1;const n=(E=(m=this.options)==null?void 0:m.scale)==null?void 0:E.wheelSensitivity;this.wheelSensitivity=n!==void 0?n:1.2;const i=()=>{};this.onTransform=((p=r==null?void 0:r.events)==null?void 0:p.onTransform)??i;const h=()=>!0;this.onBeforeTransform=((y=r==null?void 0:r.events)==null?void 0:y.onBeforeTransform)??h}addNode(t){return this.canvas.addNode(t),this}updateNode(t,r){return this.canvas.updateNode(t,r),this}removeNode(t){return this.canvas.removeNode(t),this}markPort(t){return this.canvas.markPort(t),this}updatePort(t,r){return this.canvas.updatePort(t,r),this}unmarkPort(t){return this.canvas.unmarkPort(t),this}addEdge(t){return this.canvas.addEdge(t),this}updateEdge(t,r){return this.canvas.updateEdge(t,r),this}removeEdge(t){return this.canvas.removeEdge(t),this}patchViewportState(t){return this.canvas.patchViewportState(t),this}moveToNodes(t){return this.canvas.moveToNodes(t),this}moveNodeOnTop(t){return this.canvas.moveNodeOnTop(t),this}clear(){return this.canvas.clear(),this}attach(t){return this.detach(),this.element=t,this.canvas.attach(this.element),this.element.addEventListener("mousedown",this.onMouseDown),this.element.addEventListener("mousemove",this.onMouseMove),this.element.addEventListener("mouseup",this.onMouseUp),this.element.addEventListener("wheel",this.onWheelScroll),this.element.addEventListener("touchstart",this.onTouchStart),this.element.addEventListener("touchmove",this.onTouchMove),this.element.addEventListener("touchend",this.onTouchEnd),this.element.addEventListener("touchcancel",this.onTouchEnd),this}detach(){return this.canvas.detach(),this.element!==null&&(this.element.removeEventListener("mousedown",this.onMouseDown),this.element.removeEventListener("mousemove",this.onMouseMove),this.element.removeEventListener("mouseup",this.onMouseUp),this.element.removeEventListener("wheel",this.onWheelScroll),this.element.removeEventListener("touchstart",this.onTouchStart),this.element.removeEventListener("touchmove",this.onTouchMove),this.element.removeEventListener("touchend",this.onTouchEnd),this.element.removeEventListener("touchcancel",this.onTouchEnd),this.element=null),this}destroy(){this.detach(),this.canvas.destroy()}getAverageTouch(t){const r=[],s=t.touches.length;for(let c=0;c<s;c++)r.push([t.touches[c].clientX,t.touches[c].clientY]);const o=r.reduce((c,l)=>[c[0]+l[0],c[1]+l[1]],[0,0]),n=[o[0]/s,o[1]/s],h=r.map(c=>[c[0]-n[0],c[1]-n[1]]).reduce((c,l)=>c+Math.sqrt(l[0]*l[0]+l[1]*l[1]),0);return{x:n[0],y:n[1],scale:h/s,touchesCnt:s}}setCursor(t){this.element!==null&&(t!==null?this.element.style.cursor=t:this.element.style.removeProperty("cursor"))}moveViewport(t,r){const[s,o]=this.transformation.getAbsCoords(0,0),n=this.canvas.transformation.getAbsScale(),i={scale:n,x:s+n*t,y:o+n*r};this.onBeforeTransform({...i})&&(this.canvas.patchViewportState(i),this.onTransform(i))}scaleViewport(t,r,s){const[o,n]=this.canvas.transformation.getAbsCoords(0,0),i=this.canvas.transformation.getAbsScale(),h=i*t,c=i*(1-t)*r+o,l=i*(1-t)*s+n;if(this.maxViewScale!==null&&h>this.maxViewScale&&h>i||this.minViewScale!==null&&h<this.minViewScale&&h<i)return;const d={scale:h,x:c,y:l};this.onBeforeTransform({...d})&&(this.canvas.patchViewportState(d),this.onTransform(d))}}class Ct{constructor(){a(this,"coreOptions");a(this,"dragOptions");a(this,"transformOptions");a(this,"isDraggable",!1);a(this,"isTransformable",!1)}setOptions(t){return this.coreOptions=t,this}setUserDraggableNodes(t){return this.isDraggable=!0,this.dragOptions=t,this}setUserTransformableCanvas(t){return this.isTransformable=!0,this.transformOptions=t,this}build(){let t=new q(this.coreOptions);return this.isDraggable&&(t=new I(t,this.dragOptions)),this.isTransformable&&(t=new tt(t,this.transformOptions)),t}}C.BezierEdgeController=z,C.CanvasCore=q,C.CycleCircleEdgeController=H,C.CycleSquareEdgeController=k,C.DetourStraightEdgeController=R,C.EdgeType=P,C.HorizontalEdgeController=G,C.HtmlGraphBuilder=Ct,C.StraightEdgeController=X,C.UserDraggableNodesCanvas=I,C.UserTransformableCanvas=tt,C.VerticalEdgeController=j,C.createBezierEdgeControllerFactory=U,C.createHorizontalEdgeControllerFactory=J,C.createStraightEdgeControllerFactory=_,C.createVerticalEdgeControllerFactory=K,Object.defineProperty(C,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@html-graph/html-graph",
3
- "author": "Dmitry Marov",
3
+ "author": "Dmitry Marov <d.marov94@gmail.com>",
4
4
  "private": false,
5
- "version": "0.0.51",
5
+ "version": "0.0.52",
6
6
  "type": "module",
7
7
  "main": "dist/main.js",
8
8
  "types": "dist/main.d.ts",