@html-graph/html-graph 5.1.0 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- (function(v,E){typeof exports=="object"&&typeof module<"u"?E(exports):typeof define=="function"&&define.amd?define(["exports"],E):(v=typeof globalThis<"u"?globalThis:v||self,E(v.HtmlGraph={}))})(this,function(v){"use strict";var Lt=Object.defineProperty;var Ct=(v,E,T)=>E in v?Lt(v,E,{enumerable:!0,configurable:!0,writable:!0,value:T}):v[E]=T;var s=(v,E,T)=>Ct(v,typeof E!="symbol"?E+"":E,T);const E=(r,e)=>({x:r.scale*e.x+r.x,y:r.scale*e.y+r.y});var T=(r=>(r.Line="line",r.NodeCycle="node-cycle",r.PortCycle="port-cycle",r))(T||{});const Ie=()=>{const r=document.createElement("div");return r.style.width="100%",r.style.height="100%",r.style.position="relative",r.style.overflow="hidden",r},Be=()=>{const r=document.createElement("div");return r.style.position="absolute",r.style.top="0",r.style.left="0",r.style.width="0",r.style.height="0",r},Fe=r=>{r.style.position="absolute",r.style.top="0",r.style.left="0",r.style.visibility="hidden"};class se{constructor(e,t,o){s(this,"host",Ie());s(this,"container",Be());s(this,"edgeIdToElementMap",new Map);s(this,"attachedNodeIds",new Set);s(this,"applyTransform",()=>{const e=this.viewportStore.getContentMatrix();this.container.style.transform=`matrix(${e.scale}, 0, 0, ${e.scale}, ${e.x}, ${e.y})`});this.graphStore=e,this.viewportStore=t,this.element=o,this.element.appendChild(this.host),this.host.appendChild(this.container),this.viewportStore.onAfterUpdated.subscribe(this.applyTransform)}attachNode(e){const t=this.graphStore.getNode(e);Fe(t.element),this.attachedNodeIds.add(e),this.container.appendChild(t.element),this.updateNodePosition(e),this.updateNodePriority(e),t.element.style.visibility="visible"}detachNode(e){const t=this.graphStore.getNode(e);this.container.removeChild(t.element),this.attachedNodeIds.delete(e)}attachEdge(e){const t=this.graphStore.getEdge(e).payload.shape.svg;this.edgeIdToElementMap.set(e,t),this.container.appendChild(t),this.renderEdge(e),this.updateEdgePriority(e)}detachEdge(e){const t=this.edgeIdToElementMap.get(e);this.container.removeChild(t),this.edgeIdToElementMap.delete(e)}clear(){this.edgeIdToElementMap.forEach((e,t)=>{this.detachEdge(t)}),this.attachedNodeIds.forEach(e=>{this.detachNode(e)})}destroy(){this.viewportStore.onAfterUpdated.unsubscribe(this.applyTransform),this.element.removeChild(this.host),this.host.removeChild(this.container)}updateNodePosition(e){const t=this.graphStore.getNode(e),{width:o,height:i}=t.element.getBoundingClientRect(),n=this.viewportStore.getViewportMatrix().scale,{payload:a}=t,h=a.centerFn(o,i),d=a.x-n*h.x,c=a.y-n*h.y;t.element.style.transform=`translate(${d}px, ${c}px)`}updateNodePriority(e){const t=this.graphStore.getNode(e);t.element.style.zIndex=`${t.payload.priority}`}updateEdgeShape(e){const t=this.edgeIdToElementMap.get(e);this.container.removeChild(t);const i=this.graphStore.getEdge(e).payload.shape.svg;this.edgeIdToElementMap.set(e,i),this.container.appendChild(i)}renderEdge(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from),i=this.graphStore.getPort(t.to),n=o.element.getBoundingClientRect(),a=i.element.getBoundingClientRect(),h=this.host.getBoundingClientRect(),d=this.viewportStore.getViewportMatrix(),c=this.createEdgeRenderPort(o,n,h,d),l=this.createEdgeRenderPort(i,a,h,d);let g=T.Line;o.element===i.element?g=T.PortCycle:o.nodeId===i.nodeId&&(g=T.NodeCycle),t.payload.shape.render({from:c,to:l,category:g})}updateEdgePriority(e){const t=this.graphStore.getEdge(e);t.payload.shape.svg.style.zIndex=`${t.payload.priority}`}createEdgeRenderPort(e,t,o,i){const n={x:t.left-o.left,y:t.top-o.top},a=E(i,n);return{x:a.x,y:a.y,width:t.width*i.scale,height:t.height*i.scale,direction:e.payload.direction}}}class Oe{constructor(e){s(this,"xFrom",1/0);s(this,"yFrom",1/0);s(this,"xTo",1/0);s(this,"yTo",1/0);this.graphStore=e}setRenderingBox(e){this.xFrom=e.x,this.xTo=e.x+e.width,this.yFrom=e.y,this.yTo=e.y+e.height}hasNode(e){const t=this.graphStore.getNode(e).payload,{x:o,y:i}=t;return o>=this.xFrom&&o<=this.xTo&&i>=this.yFrom&&i<=this.yTo}hasEdge(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from).nodeId,i=this.graphStore.getPort(t.to).nodeId,n=this.graphStore.getNode(o).payload,a=this.graphStore.getNode(i).payload,h=Math.min(n.x,a.x),d=Math.max(n.x,a.x),c=Math.min(n.y,a.y),l=Math.max(n.y,a.y);return h<=this.xTo&&d>=this.xFrom&&c<=this.yTo&&l>=this.yFrom}}class We{constructor(e,t,o,i){s(this,"attachedNodes",new Set);s(this,"attachedEdges",new Set);s(this,"renderingBox");s(this,"updateViewport",e=>{this.renderingBox.setRenderingBox(e);const t=new Set,o=new Set,i=new Set,n=new Set;this.graphStore.getAllNodeIds().forEach(a=>{const h=this.renderingBox.hasNode(a),d=this.attachedNodes.has(a);h&&!d?t.add(a):!h&&d&&o.add(a)}),this.graphStore.getAllEdgeIds().forEach(a=>{const h=this.renderingBox.hasEdge(a),d=this.attachedEdges.has(a),c=this.graphStore.getEdge(a),l=this.graphStore.getPort(c.from).nodeId,g=this.graphStore.getPort(c.to).nodeId;h&&(this.renderingBox.hasNode(l)||(t.add(l),o.delete(l)),this.renderingBox.hasNode(g)||(t.add(g),o.delete(g))),h&&!d?i.add(a):!h&&d&&n.add(a)}),n.forEach(a=>{this.handleDetachEdge(a)}),o.forEach(a=>{this.handleDetachNode(a)}),t.forEach(a=>{this.attachedNodes.has(a)||this.handleAttachNode(a)}),i.forEach(a=>{this.handleAttachEdge(a)})});this.htmlView=e,this.graphStore=t,this.trigger=o,this.params=i,this.renderingBox=new Oe(this.graphStore),this.trigger.subscribe(this.updateViewport)}attachNode(e){this.renderingBox.hasNode(e)&&this.handleAttachNode(e)}detachNode(e){this.attachedNodes.has(e)&&this.handleDetachNode(e)}attachEdge(e){this.renderingBox.hasEdge(e)&&this.attachEdgeEntities(e)}detachEdge(e){this.attachedEdges.has(e)&&this.handleDetachEdge(e)}updateNodePosition(e){this.attachedNodes.has(e)?this.htmlView.updateNodePosition(e):this.renderingBox.hasNode(e)&&(this.handleAttachNode(e),this.graphStore.getNodeAdjacentEdgeIds(e).forEach(t=>{this.attachEdgeEntities(t)}))}updateNodePriority(e){this.attachedNodes.has(e)&&this.htmlView.updateNodePriority(e)}updateEdgeShape(e){this.attachedEdges.has(e)&&this.htmlView.updateEdgeShape(e)}renderEdge(e){this.attachedEdges.has(e)&&this.htmlView.renderEdge(e)}updateEdgePriority(e){this.attachedEdges.has(e)&&this.htmlView.updateEdgePriority(e)}clear(){this.htmlView.clear(),this.attachedNodes.clear(),this.attachedEdges.clear()}destroy(){this.clear(),this.htmlView.destroy(),this.trigger.unsubscribe(this.updateViewport)}attachEdgeEntities(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from).nodeId,i=this.graphStore.getPort(t.to).nodeId;this.attachedNodes.has(o)||this.handleAttachNode(o),this.attachedNodes.has(i)||this.handleAttachNode(i),this.handleAttachEdge(e)}handleAttachNode(e){this.params.onBeforeNodeAttached(e),this.attachedNodes.add(e),this.htmlView.attachNode(e)}handleDetachNode(e){this.htmlView.detachNode(e),this.attachedNodes.delete(e),this.params.onAfterNodeDetached(e)}handleAttachEdge(e){this.attachedEdges.add(e),this.htmlView.attachEdge(e)}handleDetachEdge(e){this.htmlView.detachEdge(e),this.attachedEdges.delete(e)}}class ke{constructor(e,t){s(this,"deferredNodes",new Set);s(this,"deferredEdges",new Set);this.htmlView=e,this.graphStore=t}attachNode(e){this.isNodeValid(e)?this.htmlView.attachNode(e):this.deferredNodes.add(e)}detachNode(e){this.deferredNodes.has(e)?this.deferredNodes.delete(e):this.htmlView.detachNode(e)}attachEdge(e){this.isEdgeValid(e)?this.htmlView.attachEdge(e):this.deferredEdges.add(e)}detachEdge(e){this.deferredEdges.has(e)?this.deferredEdges.delete(e):this.htmlView.detachEdge(e)}updateNodePosition(e){this.deferredNodes.has(e)?this.tryAttachNode(e):this.htmlView.updateNodePosition(e)}updateNodePriority(e){this.deferredNodes.has(e)?this.tryAttachNode(e):this.htmlView.updateNodePriority(e)}updateEdgeShape(e){this.deferredEdges.has(e)?this.tryAttachEdge(e):this.htmlView.updateEdgeShape(e)}renderEdge(e){this.deferredEdges.has(e)?this.tryAttachEdge(e):this.htmlView.renderEdge(e)}updateEdgePriority(e){this.deferredEdges.has(e)?this.tryAttachEdge(e):this.htmlView.updateEdgePriority(e)}clear(){this.deferredNodes.clear(),this.deferredEdges.clear(),this.htmlView.clear()}destroy(){this.htmlView.destroy()}isNodeValid(e){const t=this.graphStore.getNode(e);return!(t.payload.x===null||t.payload.y===null)}tryAttachNode(e){this.isNodeValid(e)&&(this.deferredNodes.delete(e),this.htmlView.attachNode(e))}isEdgeValid(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from),i=this.graphStore.getPort(t.to);return!(this.deferredNodes.has(o.nodeId)||this.deferredNodes.has(i.nodeId))}tryAttachEdge(e){this.isEdgeValid(e)&&(this.deferredEdges.delete(e),this.htmlView.attachEdge(e))}}class ie{constructor(){s(this,"callbacks",new Set)}subscribe(e){this.callbacks.add(e)}unsubscribe(e){this.callbacks.delete(e)}emit(e){this.callbacks.forEach(t=>{t(e)})}}const x=()=>{const r=new ie;return[r,r]};class H{constructor(e){s(this,"counter",0);this.checkExists=e}create(e){if(e!==void 0)return e;for(;this.checkExists(this.counter);)this.counter++;return this.counter}reset(){this.counter=0}}class P extends Error{constructor(){super(...arguments);s(this,"name","CanvasError")}}class ne{constructor(e,t,o,i,n,a){s(this,"nodeIdGenerator",new H(e=>this.graphStore.getNode(e)!==void 0));s(this,"portIdGenerator",new H(e=>this.graphStore.getPort(e)!==void 0));s(this,"edgeIdGenerator",new H(e=>this.graphStore.getEdge(e)!==void 0));s(this,"onAfterNodeAdded",e=>{this.htmlView.attachNode(e)});s(this,"onAfterNodeUpdated",e=>{this.htmlView.updateNodePosition(e),this.graphStore.getNodeAdjacentEdgeIds(e).forEach(o=>{this.htmlView.renderEdge(o)})});s(this,"onAfterNodePriorityUpdated",e=>{this.htmlView.updateNodePriority(e)});s(this,"onBeforeNodeRemoved",e=>{this.graphStore.getNodePortIds(e).forEach(t=>{this.unmarkPort(t)}),this.htmlView.detachNode(e)});s(this,"onAfterPortUpdated",e=>{this.graphStore.getPortAdjacentEdgeIds(e).forEach(o=>{this.htmlView.renderEdge(o)})});s(this,"onBeforePortUnmarked",e=>{this.graphStore.getPortAdjacentEdgeIds(e).forEach(t=>{this.removeEdge(t)})});s(this,"onAfterEdgeAdded",e=>{this.htmlView.attachEdge(e)});s(this,"onAfterEdgeShapeUpdated",e=>{this.htmlView.updateEdgeShape(e)});s(this,"onAfterEdgeUpdated",e=>{this.htmlView.renderEdge(e)});s(this,"onAfterEdgePriorityUpdated",e=>{this.htmlView.updateEdgePriority(e)});s(this,"onBeforeEdgeRemoved",e=>{this.htmlView.detachEdge(e)});s(this,"onBeforeClear",()=>{this.nodeIdGenerator.reset(),this.portIdGenerator.reset(),this.edgeIdGenerator.reset(),this.htmlView.clear()});s(this,"onBeforeDestroyEmitter");s(this,"destroyed",!1);s(this,"onBeforeDestroy");this.graph=e,this.viewport=t,this.graphStore=o,this.viewportStore=i,this.htmlView=n,this.params=a,this.graphStore.onAfterNodeAdded.subscribe(this.onAfterNodeAdded),this.graphStore.onAfterNodeUpdated.subscribe(this.onAfterNodeUpdated),this.graphStore.onAfterNodePriorityUpdated.subscribe(this.onAfterNodePriorityUpdated),this.graphStore.onBeforeNodeRemoved.subscribe(this.onBeforeNodeRemoved),this.graphStore.onAfterPortUpdated.subscribe(this.onAfterPortUpdated),this.graphStore.onBeforePortRemoved.subscribe(this.onBeforePortUnmarked),this.graphStore.onAfterEdgeAdded.subscribe(this.onAfterEdgeAdded),this.graphStore.onAfterEdgeShapeUpdated.subscribe(this.onAfterEdgeShapeUpdated),this.graphStore.onAfterEdgeUpdated.subscribe(this.onAfterEdgeUpdated),this.graphStore.onAfterEdgePriorityUpdated.subscribe(this.onAfterEdgePriorityUpdated),this.graphStore.onBeforeEdgeRemoved.subscribe(this.onBeforeEdgeRemoved),this.graphStore.onBeforeClear.subscribe(this.onBeforeClear),[this.onBeforeDestroyEmitter,this.onBeforeDestroy]=x()}addNode(e){const t=this.nodeIdGenerator.create(e.id);if(this.graphStore.getNode(t)!==void 0)throw new P("failed to add node with existing id");if(this.graphStore.getElementNodeId(e.element)!==void 0)throw new P("failed to add node with html element already in use by another node");if(this.graphStore.addNode({id:t,element:e.element,x:e.x??null,y:e.y??null,centerFn:e.centerFn??this.params.nodes.centerFn,priority:e.priority??this.params.nodes.priorityFn()}),e.ports!==void 0)for(const o of e.ports)this.markPort({id:o.id,element:o.element,nodeId:t,direction:o.direction});return this}updateNode(e,t){if(this.graphStore.getNode(e)===void 0)throw new P("failed to update nonexistent node");return this.graphStore.updateNode(e,t??{}),this}removeNode(e){if(this.graphStore.getNode(e)===void 0)throw new P("failed to remove nonexistent node");return this.graphStore.removeNode(e),this}markPort(e){const t=this.portIdGenerator.create(e.id);if(this.graphStore.getPort(t)!==void 0)throw new P("failed to add port with existing id");if(this.graphStore.getNode(e.nodeId)===void 0)throw new P("failed to mark port for nonexistent node");return this.graphStore.addPort({id:t,element:e.element,nodeId:e.nodeId,direction:e.direction??this.params.ports.direction}),this}updatePort(e,t){if(this.graphStore.getPort(e)===void 0)throw new P("failed to update nonexistent port");return this.graphStore.updatePort(e,t??{}),this}unmarkPort(e){if(this.graphStore.getPort(e)===void 0)throw new P("failed to unmark nonexistent port");return this.graphStore.removePort(e),this}addEdge(e){const t=this.edgeIdGenerator.create(e.id);if(this.graphStore.getEdge(t)!==void 0)throw new P("failed to add edge with existing id");if(this.graphStore.getPort(e.from)===void 0)throw new P("failed to add edge from nonexistent port");if(this.graphStore.getPort(e.to)===void 0)throw new P("failed to add edge to nonexistent port");return this.graphStore.addEdge({id:t,from:e.from,to:e.to,shape:e.shape??this.params.edges.shapeFactory(t),priority:e.priority??this.params.edges.priorityFn()}),this}updateEdge(e,t){if(this.graphStore.getEdge(e)===void 0)throw new P("failed to update nonexistent edge");return this.graphStore.updateEdge(e,t??{}),this}removeEdge(e){if(this.graphStore.getEdge(e)===void 0)throw new P("failed to remove nonexistent edge");return this.graphStore.removeEdge(e),this}clear(){return this.graphStore.clear(),this}patchViewportMatrix(e){return this.viewportStore.patchViewportMatrix(e),this}patchContentMatrix(e){return this.viewportStore.patchContentMatrix(e),this}destroy(){this.destroyed||(this.clear(),this.onBeforeDestroyEmitter.emit(),this.graphStore.onAfterNodeAdded.unsubscribe(this.onAfterNodeAdded),this.graphStore.onAfterNodeUpdated.unsubscribe(this.onAfterNodeUpdated),this.graphStore.onAfterNodePriorityUpdated.unsubscribe(this.onAfterNodePriorityUpdated),this.graphStore.onBeforeNodeRemoved.unsubscribe(this.onBeforeNodeRemoved),this.graphStore.onAfterPortUpdated.unsubscribe(this.onAfterPortUpdated),this.graphStore.onBeforePortRemoved.unsubscribe(this.onBeforePortUnmarked),this.graphStore.onAfterEdgeAdded.unsubscribe(this.onAfterEdgeAdded),this.graphStore.onAfterEdgeShapeUpdated.unsubscribe(this.onAfterEdgeShapeUpdated),this.graphStore.onAfterEdgeUpdated.unsubscribe(this.onAfterEdgeUpdated),this.graphStore.onAfterEdgePriorityUpdated.unsubscribe(this.onAfterEdgePriorityUpdated),this.graphStore.onBeforeEdgeRemoved.unsubscribe(this.onBeforeEdgeRemoved),this.graphStore.onBeforeClear.unsubscribe(this.onBeforeClear),this.htmlView.destroy(),this.destroyed=!0)}}class ze{constructor(){s(this,"singleToMultiMap",new Map);s(this,"multiToSingleMap",new Map)}addRecord(e,t){const o=this.singleToMultiMap.get(e);o===void 0?this.singleToMultiMap.set(e,new Set([t])):o.add(t),this.multiToSingleMap.set(t,e)}getMultiBySingle(e){const t=this.singleToMultiMap.get(e)??new Set;return Array.from(t.values())}removeByMulti(e){const t=this.multiToSingleMap.get(e),o=this.singleToMultiMap.get(t);o.delete(e),o.size===0&&this.singleToMultiMap.delete(t),this.multiToSingleMap.delete(e)}getByMulti(e){return this.multiToSingleMap.get(e)}removeBySingle(e){this.singleToMultiMap.get(e).forEach(o=>{this.multiToSingleMap.delete(o)}),this.singleToMultiMap.delete(e)}clear(){this.singleToMultiMap.clear(),this.multiToSingleMap.clear()}forEachSingle(e){this.singleToMultiMap.forEach((t,o)=>{e(o)})}hasSingle(e){return this.singleToMultiMap.get(e)!==void 0}hasMulti(e){return this.multiToSingleMap.get(e)!==void 0}}class ae{constructor(){s(this,"nodes",new Map);s(this,"ports",new Map);s(this,"edges",new Map);s(this,"nodesElementsMap",new Map);s(this,"incomingEdges",new Map);s(this,"outcomingEdges",new Map);s(this,"cycleEdges",new Map);s(this,"elementPorts",new ze);s(this,"afterNodeAddedEmitter");s(this,"onAfterNodeAdded");s(this,"afterNodeUpdatedEmitter");s(this,"onAfterNodeUpdated");s(this,"afterNodePriorityUpdatedEmitter");s(this,"onAfterNodePriorityUpdated");s(this,"beforeNodeRemovedEmitter");s(this,"onBeforeNodeRemoved");s(this,"afterPortAddedEmitter");s(this,"onAfterPortAdded");s(this,"afterPortUpdatedEmitter");s(this,"onAfterPortUpdated");s(this,"beforePortRemovedEmitter");s(this,"onBeforePortRemoved");s(this,"afterEdgeAddedEmitter");s(this,"onAfterEdgeAdded");s(this,"afterEdgeShapeUpdatedEmitter");s(this,"onAfterEdgeShapeUpdated");s(this,"afterEdgeUpdatedEmitter");s(this,"onAfterEdgeUpdated");s(this,"afterEdgePriorityUpdatedEmitter");s(this,"onAfterEdgePriorityUpdated");s(this,"beforeEdgeRemovedEmitter");s(this,"onBeforeEdgeRemoved");s(this,"beforeClearEmitter");s(this,"onBeforeClear");[this.afterNodeAddedEmitter,this.onAfterNodeAdded]=x(),[this.afterNodeUpdatedEmitter,this.onAfterNodeUpdated]=x(),[this.afterNodePriorityUpdatedEmitter,this.onAfterNodePriorityUpdated]=x(),[this.beforeNodeRemovedEmitter,this.onBeforeNodeRemoved]=x(),[this.afterPortAddedEmitter,this.onAfterPortAdded]=x(),[this.afterPortUpdatedEmitter,this.onAfterPortUpdated]=x(),[this.beforePortRemovedEmitter,this.onBeforePortRemoved]=x(),[this.afterEdgeAddedEmitter,this.onAfterEdgeAdded]=x(),[this.afterEdgeShapeUpdatedEmitter,this.onAfterEdgeShapeUpdated]=x(),[this.afterEdgeUpdatedEmitter,this.onAfterEdgeUpdated]=x(),[this.afterEdgePriorityUpdatedEmitter,this.onAfterEdgePriorityUpdated]=x(),[this.beforeEdgeRemovedEmitter,this.onBeforeEdgeRemoved]=x(),[this.beforeClearEmitter,this.onBeforeClear]=x()}addNode(e){const t=new Map,o={element:e.element,payload:{x:e.x,y:e.y,centerFn:e.centerFn,priority:e.priority},ports:t};this.nodes.set(e.id,o),this.nodesElementsMap.set(e.element,e.id),this.afterNodeAddedEmitter.emit(e.id)}getAllNodeIds(){return Array.from(this.nodes.keys())}getNode(e){return this.nodes.get(e)}getElementNodeId(e){return this.nodesElementsMap.get(e)}updateNode(e,t){const o=this.nodes.get(e).payload;o.x=t.x??o.x,o.y=t.y??o.y,o.centerFn=t.centerFn??o.centerFn,t.priority!==void 0&&(o.priority=t.priority,this.afterNodePriorityUpdatedEmitter.emit(e)),this.afterNodeUpdatedEmitter.emit(e)}removeNode(e){this.beforeNodeRemovedEmitter.emit(e);const t=this.nodes.get(e);this.nodesElementsMap.delete(t.element),this.nodes.delete(e)}addPort(e){this.ports.set(e.id,{element:e.element,payload:{direction:e.direction},nodeId:e.nodeId}),this.elementPorts.addRecord(e.element,e.id),this.cycleEdges.set(e.id,new Set),this.incomingEdges.set(e.id,new Set),this.outcomingEdges.set(e.id,new Set),this.nodes.get(e.nodeId).ports.set(e.id,e.element),this.afterPortAddedEmitter.emit(e.id)}getPort(e){return this.ports.get(e)}updatePort(e,t){const o=this.ports.get(e).payload;o.direction=t.direction??o.direction,this.afterPortUpdatedEmitter.emit(e)}getAllPortIds(){return Array.from(this.ports.keys())}getElementPortIds(e){return this.elementPorts.getMultiBySingle(e)}getNodePortIds(e){const t=this.nodes.get(e);if(t!==void 0)return Array.from(t.ports.keys())}removePort(e){const t=this.ports.get(e).nodeId;this.beforePortRemovedEmitter.emit(e),this.nodes.get(t).ports.delete(e),this.ports.delete(e),this.elementPorts.removeByMulti(e)}addEdge(e){this.addEdgeInternal(e),this.afterEdgeAddedEmitter.emit(e.id)}updateEdge(e,t){if(t.from!==void 0||t.to!==void 0){const i=this.edges.get(e),n=i.payload;this.removeEdgeInternal(e),this.addEdgeInternal({id:e,from:t.from??i.from,to:t.to??i.to,shape:n.shape,priority:n.priority})}const o=this.edges.get(e);t.shape!==void 0&&(o.payload.shape=t.shape,this.afterEdgeShapeUpdatedEmitter.emit(e)),t.priority!==void 0&&(o.payload.priority=t.priority,this.afterEdgePriorityUpdatedEmitter.emit(e)),this.afterEdgeUpdatedEmitter.emit(e)}getAllEdgeIds(){return Array.from(this.edges.keys())}getEdge(e){return this.edges.get(e)}removeEdge(e){this.beforeEdgeRemovedEmitter.emit(e),this.removeEdgeInternal(e)}clear(){this.beforeClearEmitter.emit(),this.incomingEdges.clear(),this.outcomingEdges.clear(),this.cycleEdges.clear(),this.elementPorts.clear(),this.nodesElementsMap.clear(),this.edges.clear(),this.ports.clear(),this.nodes.clear()}getPortIncomingEdgeIds(e){return Array.from(this.incomingEdges.get(e))}getPortOutgoingEdgeIds(e){return Array.from(this.outcomingEdges.get(e))}getPortCycleEdgeIds(e){return Array.from(this.cycleEdges.get(e))}getPortAdjacentEdgeIds(e){return[...this.getPortIncomingEdgeIds(e),...this.getPortOutgoingEdgeIds(e),...this.getPortCycleEdgeIds(e)]}getNodeIncomingEdgeIds(e){const t=Array.from(this.nodes.get(e).ports.keys());let o=[];return t.forEach(i=>{o=[...o,...this.getPortIncomingEdgeIds(i)]}),o}getNodeOutgoingEdgeIds(e){const t=Array.from(this.nodes.get(e).ports.keys());let o=[];return t.forEach(i=>{o=[...o,...this.getPortOutgoingEdgeIds(i)]}),o}getNodeCycleEdgeIds(e){const t=Array.from(this.nodes.get(e).ports.keys());let o=[];return t.forEach(i=>{o=[...o,...this.getPortCycleEdgeIds(i)]}),o}getNodeAdjacentEdgeIds(e){return[...this.getNodeIncomingEdgeIds(e),...this.getNodeOutgoingEdgeIds(e),...this.getNodeCycleEdgeIds(e)]}addEdgeInternal(e){this.edges.set(e.id,{from:e.from,to:e.to,payload:{shape:e.shape,priority:e.priority}}),e.from!==e.to?(this.outcomingEdges.get(e.from).add(e.id),this.incomingEdges.get(e.to).add(e.id)):this.cycleEdges.get(e.from).add(e.id)}removeEdgeInternal(e){const t=this.edges.get(e),o=t.from,i=t.to;this.cycleEdges.get(o).delete(e),this.cycleEdges.get(i).delete(e),this.incomingEdges.get(o).delete(e),this.incomingEdges.get(i).delete(e),this.outcomingEdges.get(o).delete(e),this.outcomingEdges.get(i).delete(e),this.edges.delete(e)}}const he=r=>({scale:1/r.scale,x:-r.x/r.scale,y:-r.y/r.scale}),de={scale:1,x:0,y:0};class Xe{constructor(){s(this,"viewportMatrix",de);s(this,"contentMatrix",de);s(this,"afterUpdateEmitter");s(this,"onAfterUpdated");[this.afterUpdateEmitter,this.onAfterUpdated]=x()}getViewportMatrix(){return this.viewportMatrix}getContentMatrix(){return this.contentMatrix}patchViewportMatrix(e){this.viewportMatrix={scale:e.scale??this.viewportMatrix.scale,x:e.x??this.viewportMatrix.x,y:e.y??this.viewportMatrix.y},this.contentMatrix=he(this.viewportMatrix),this.afterUpdateEmitter.emit()}patchContentMatrix(e){this.contentMatrix={scale:e.scale??this.contentMatrix.scale,x:e.x??this.contentMatrix.x,y:e.y??this.contentMatrix.y},this.viewportMatrix=he(this.contentMatrix),this.afterUpdateEmitter.emit()}}class Y{constructor(e){s(this,"elementToNodeId",new Map);s(this,"nodesResizeObserver");s(this,"onAfterNodeAdded",e=>{const t=this.canvas.graph.getNode(e);this.elementToNodeId.set(t.element,e),this.nodesResizeObserver.observe(t.element)});s(this,"onBeforeNodeRemoved",e=>{const t=this.canvas.graph.getNode(e);this.elementToNodeId.delete(t.element),this.nodesResizeObserver.unobserve(t.element)});s(this,"onBeforeClear",()=>{this.nodesResizeObserver.disconnect(),this.elementToNodeId.clear()});this.canvas=e,this.nodesResizeObserver=new ResizeObserver(t=>{t.forEach(o=>{const i=o.target;this.handleNodeResize(i)})}),this.canvas.graph.onAfterNodeAdded.subscribe(this.onAfterNodeAdded),this.canvas.graph.onBeforeNodeRemoved.subscribe(this.onBeforeNodeRemoved),this.canvas.graph.onBeforeClear.subscribe(this.onBeforeClear)}static configure(e){new Y(e)}handleNodeResize(e){const t=this.elementToNodeId.get(e);this.canvas.updateNode(t)}}const He=(r,e,t)=>{const{x:o,y:i,width:n,height:a}=r.getBoundingClientRect();return e>=o&&e<=o+n&&t>=i&&t<=i+a},Ye=(r,e,t)=>e>=0&&e<=r.innerWidth&&t>=0&&t<=r.innerHeight,C=(r,e,t,o)=>He(e,t,o)&&Ye(r,t,o),I=(r,e)=>{e!==null?r.style.cursor=e:r.style.removeProperty("cursor")},B=r=>{const e=document.createElement("div");return{id:r.overlayId,element:e,x:r.portCoords.x,y:r.portCoords.y,ports:[{id:r.overlayId,element:e,direction:r.portDirection}]}},je=(r,e)=>{let t=e,o=null;for(;t!==null&&(o=r.getElementPortIds(t)[0]??null,o===null);)t=t.parentElement;return o},ce=(r,e)=>{const t=document.elementsFromPoint(e.x,e.y);for(const o of t){const i=je(r,o);if(i!==null)return i}return null};var D=(r=>(r.Static="static",r.Dragging="dragging",r.Edge="edge",r))(D||{});const ge=(r,e)=>({x:r/2,y:e/2}),u={x:0,y:0},m=(r,e,t)=>({x:e.x*r.x-e.y*r.y+((1-e.x)*t.x+e.y*t.y),y:e.y*r.x+e.x*r.y+((1-e.x)*t.y-e.y*t.x)}),le=(r,e)=>{const t={x:r.x+r.width/2,y:r.y+r.height/2},o={x:e.x+e.width/2,y:e.y+e.height/2},i=Math.min(t.x,o.x),n=Math.min(t.y,o.y),a=Math.abs(o.x-t.x),h=Math.abs(o.y-t.y),d=t.x<=o.x?1:-1,c=t.y<=o.y?1:-1;return{x:i,y:n,width:a,height:h,flipX:d,flipY:c}},F=(r,e,t,o)=>({x:e*r.x+(1-e)/2*o.x,y:t*r.y+(1-t)/2*o.y});class Ge{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.to;this.midpoint={x:t.x/2,y:t.y/2};const o=m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u),i=m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to),n={x:o.x+this.params.sourceDirection.x*this.params.curvature,y:o.y+this.params.sourceDirection.y*this.params.curvature},a={x:i.x-this.params.targetDirection.x*this.params.curvature,y:i.y-this.params.targetDirection.y*this.params.curvature},h=`M ${o.x} ${o.y} C ${n.x} ${n.y}, ${a.x} ${a.y}, ${i.x} ${i.y}`,d=this.params.hasSourceArrow?"":`M ${u.x} ${u.y} L ${o.x} ${o.y} `,c=this.params.hasTargetArrow?"":` M ${i.x} ${i.y} L ${this.params.to.x} ${this.params.to.y}`;this.path=`${d}${h}${c}`}}class Ke{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.hasSourceArrow?m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u):u,o=this.params.hasTargetArrow?m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to):this.params.to,i=this.params.arrowLength,n=Math.cos(this.params.detourDirection)*this.params.detourDistance,a=Math.sin(this.params.detourDirection)*this.params.detourDistance,h=n*this.params.flipX,d=a*this.params.flipY,c=m({x:i,y:u.y},this.params.sourceDirection,u),l={x:c.x+h,y:c.y+d},g=m({x:this.params.to.x-i,y:this.params.to.y},this.params.targetDirection,this.params.to),p={x:g.x+h,y:g.y+d},f={x:(l.x+p.x)/2,y:(l.y+p.y)/2},y={x:c.x+this.params.curvature*this.params.sourceDirection.x,y:c.y+this.params.curvature*this.params.sourceDirection.y},A={x:g.x-this.params.curvature*this.params.targetDirection.x,y:g.y-this.params.curvature*this.params.targetDirection.y},b={x:c.x+h,y:c.y+d},S={x:g.x+h,y:g.y+d};this.path=[`M ${t.x} ${t.y}`,`L ${c.x} ${c.y}`,`C ${y.x} ${y.y} ${b.x} ${b.y} ${f.x} ${f.y}`,`C ${S.x} ${S.y} ${A.x} ${A.y} ${g.x} ${g.y}`,`L ${o.x} ${o.y}`].join(" "),this.midpoint=F(f,e.flipX,e.flipY,e.to)}}const j=Object.freeze({edgeColor:"--edge-color"}),ue=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","svg");return e.style.pointerEvents="none",e.style.position="absolute",e.style.top="0",e.style.left="0",e.style.overflow="visible",e.style.setProperty(j.edgeColor,r),e},pe=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","path");return e.setAttribute("stroke",`var(${j.edgeColor})`),e.setAttribute("stroke-width",`${r}`),e.setAttribute("fill","none"),e},O=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","path");return r.setAttribute("fill",`var(${j.edgeColor})`),r},we=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","g");return r.style.transformOrigin="50% 50%",r},fe=(r,e)=>{r.style.transform=`translate(${e.x}px, ${e.y}px)`,r.style.width=`${Math.max(e.width,1)}px`,r.style.height=`${Math.max(e.height,1)}px`},M=(r,e)=>{const t=[];if(r.length>0&&t.push(`M ${r[0].x} ${r[0].y}`),r.length===2&&t.push(`L ${r[1].x} ${r[1].y}`),r.length>2){const o=r.length-1;let i=0,n=0,a=0;r.forEach((h,d)=>{let c=0,l=0,g=0;const p=d>0,f=d<o,y=p&&f;if(p&&(c=-i,l=-n,g=a),f){const U=r[d+1];i=U.x-h.x,n=U.y-h.y,a=Math.sqrt(i*i+n*n)}const b=a!==0?Math.min((y?e:0)/a,d<o-1?.5:1):0,S=y?{x:h.x+i*b,y:h.y+n*b}:h,N=g!==0?Math.min((y?e:0)/g,d>1?.5:1):0,R=y?{x:h.x+c*N,y:h.y+l*N}:h;d>0&&t.push(`L ${R.x} ${R.y}`),y&&t.push(`C ${h.x} ${h.y} ${h.x} ${h.y} ${S.x} ${S.y}`)})}return t.join(" ")};class Ze{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.to;this.midpoint={x:t.x/2,y:t.y/2};const o=this.params.hasSourceArrow?m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u):u,i=this.params.hasTargetArrow?m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to):this.params.to,n=this.params.arrowLength+this.params.arrowOffset,a=n-this.params.roundness,h=m({x:a,y:u.y},this.params.sourceDirection,u),d=m({x:this.params.to.x-a,y:this.params.to.y},this.params.targetDirection,this.params.to),c=Math.max((h.x+d.x)/2,n),l=this.params.to.y/2,g={x:this.params.flipX>0?c:-n,y:h.y},p={x:g.x,y:l},f={x:this.params.flipX>0?this.params.to.x-c:this.params.to.x+n,y:d.y},y={x:f.x,y:l};this.path=M([o,h,g,p,y,f,d,i],this.params.roundness)}}class Je{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.hasSourceArrow?m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u):u,o=this.params.hasTargetArrow?m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to):this.params.to,i=this.params.arrowLength+this.params.arrowOffset,n=m({x:i,y:u.y},this.params.sourceDirection,u),a=Math.cos(this.params.detourDirection)*this.params.detourDistance,h=Math.sin(this.params.detourDirection)*this.params.detourDistance,d=a*this.params.flipX,c=h*this.params.flipY,l={x:n.x+d,y:n.y+c},g=m({x:this.params.to.x-i,y:this.params.to.y},this.params.targetDirection,this.params.to),p={x:g.x+d,y:g.y+c},f={x:(l.x+p.x)/2,y:(l.y+p.y)/2};this.midpoint=F(f,e.flipX,e.flipY,e.to),this.path=M([t,n,l,p,g,o],this.params.roundness)}}class Qe{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.to;this.midpoint={x:t.x/2,y:t.y/2};const o=this.params.hasSourceArrow?m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u):u,i=this.params.hasTargetArrow?m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to):this.params.to,n=this.params.arrowLength+this.params.arrowOffset,a=m({x:n,y:u.y},this.params.sourceDirection,u),h=m({x:this.params.to.x-n,y:this.params.to.y},this.params.targetDirection,this.params.to);this.path=M([o,a,h,i],this.params.roundness)}}class _e{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.to;this.midpoint={x:t.x/2,y:t.y/2};const o=this.params.hasSourceArrow?m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u):u,i=this.params.hasTargetArrow?m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to):this.params.to,n=this.params.arrowLength+this.params.arrowOffset,a=n-this.params.roundness,h=m({x:a,y:u.y},this.params.sourceDirection,u),d=m({x:this.params.to.x-a,y:this.params.to.y},this.params.targetDirection,this.params.to),c=Math.max((h.y+d.y)/2,n),l=this.params.to.x/2,g={x:h.x,y:this.params.flipY>0?c:-n},p={x:l,y:g.y},f={x:d.x,y:this.params.flipY>0?this.params.to.y-c:this.params.to.y+n},y={x:l,y:f.y};this.path=M([o,h,g,p,y,f,d,i],this.params.roundness)}}class G{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.arrowOffset,o=this.params.side,i=this.params.arrowLength+t,n=i+2*o,h=[{x:this.params.arrowLength,y:u.y},{x:i,y:u.y},{x:i,y:this.params.side},{x:n,y:this.params.side},{x:n,y:-this.params.side},{x:i,y:-this.params.side},{x:i,y:u.y},{x:this.params.arrowLength,y:u.y}].map(c=>m(c,this.params.sourceDirection,u)),d=`M ${u.x} ${u.y} L ${h[0].x} ${h[0].y} `;this.path=`${this.params.hasSourceArrow||this.params.hasTargetArrow?"":d}${M(h,this.params.roundness)}`,this.midpoint={x:(h[3].x+h[4].x)/2,y:(h[3].y+h[4].y)/2}}}class qe{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.smallRadius,o=this.params.radius,i=t+o,n=t*o/i,a=Math.sqrt(i*i-t*t),h=a*t/i,d=a+o+this.params.arrowLength,c=this.params.arrowLength+h,g=[{x:this.params.arrowLength,y:u.y},{x:c,y:n},{x:c,y:-n},{x:d,y:0}].map(y=>m(y,this.params.sourceDirection,u)),p=[`M ${g[0].x} ${g[0].y}`,`A ${t} ${t} 0 0 1 ${g[1].x} ${g[1].y}`,`A ${o} ${o} 0 1 0 ${g[2].x} ${g[2].y}`,`A ${t} ${t} 0 0 1 ${g[0].x} ${g[0].y}`].join(" "),f=`M 0 0 L ${g[0].x} ${g[0].y} `;this.path=`${this.params.hasSourceArrow||this.params.hasTargetArrow?"":f}${p}`,this.midpoint=g[3]}}class et{constructor(e){s(this,"path");s(this,"midpoint");s(this,"diagonalDistance");this.params=e;const t=this.params.to;if(this.midpoint={x:t.x/2,y:t.y/2},this.diagonalDistance=Math.sqrt(this.params.to.x*this.params.to.x+this.params.to.y*this.params.to.y),Math.sqrt(this.params.to.x*this.params.to.x+this.params.to.y*this.params.to.y)===0){this.path="";return}const i=this.createDirectLinePoint({offset:this.params.sourceOffset,hasArrow:this.params.hasSourceArrow,flip:1,shift:u}),n=this.createDirectLinePoint({offset:this.params.targetOffset,hasArrow:this.params.hasTargetArrow,flip:-1,shift:this.params.to});this.path=`M ${i.x} ${i.y} L ${n.x} ${n.y}`}createDirectLinePoint(e){const t=e.hasArrow?this.params.arrowLength:0,o=e.offset+t,i=e.flip*o/this.diagonalDistance;return{x:this.params.to.x*i+e.shift.x,y:this.params.to.y*i+e.shift.y}}}class tt{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.hasSourceArrow?m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u):u,o=this.params.hasTargetArrow?m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to):this.params.to,i=this.params.arrowLength+this.params.arrowOffset,n=m({x:i,y:u.y},this.params.sourceDirection,u),a=m({x:this.params.to.x-i,y:this.params.to.y},this.params.targetDirection,this.params.to),h=this.params.detourDistance>0?1:-1,d=this.params.to.y/2,c=d+Math.abs(this.params.detourDistance),l=d+c*this.params.flipY*h,g={x:(n.x+a.x)/2,y:l};this.midpoint=F(g,e.flipX,e.flipY,e.to),this.path=M([t,n,{x:n.x,y:l},{x:a.x,y:l},a,o],this.params.roundness)}}class rt{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.hasSourceArrow?m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u):u,o=this.params.hasTargetArrow?m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to):this.params.to,i=this.params.arrowLength+this.params.arrowOffset,n=m({x:i,y:u.y},this.params.sourceDirection,u),a=m({x:this.params.to.x-i,y:this.params.to.y},this.params.targetDirection,this.params.to),h=this.params.detourDistance>0?1:-1,d=this.params.to.x/2,c=d+Math.abs(this.params.detourDistance),l=d+c*this.params.flipX*h,g={x:l,y:(n.y+a.y)/2};this.midpoint=F(g,e.flipX,e.flipY,e.to),this.path=M([t,n,{x:l,y:n.y},{x:l,y:a.y},a,o],this.params.roundness)}}const w=Object.freeze({color:"#777777",width:1,arrowLength:20,polygonArrowRadius:4,circleArrowRadius:8,wedgeArrowSmallRadius:20,wedgeArrowRadius:100,wedgeArrowAngle:Math.PI/12,arrowOffset:15,hasSourceArrow:!1,hasTargetArrow:!1,cycleRadius:30,cycleSquareSide:30,roundness:10,detourDistance:100,detourDirection:-Math.PI/2,detourDirectionVertical:0,smallCycleRadius:15,curvature:90,interactiveWidth:10,preOffset:0}),ye=(r,e,t)=>({x:e*Math.cos(r),y:t*Math.sin(r)});class W{constructor(e){s(this,"svg");s(this,"group",we());s(this,"line");s(this,"sourceArrow",null);s(this,"targetArrow",null);s(this,"onAfterRender");s(this,"afterRenderEmitter");s(this,"arrowRenderer");this.params=e,[this.afterRenderEmitter,this.onAfterRender]=x(),this.arrowRenderer=this.params.arrowRenderer,this.svg=ue(e.color),this.svg.appendChild(this.group),this.line=pe(e.width),this.group.appendChild(this.line),e.hasSourceArrow&&(this.sourceArrow=O(),this.group.appendChild(this.sourceArrow)),e.hasTargetArrow&&(this.targetArrow=O(),this.group.appendChild(this.targetArrow))}render(e){const{x:t,y:o,width:i,height:n,flipX:a,flipY:h}=le(e.from,e.to);fe(this.svg,{x:t,y:o,width:i,height:n}),this.group.style.transform=`scale(${a}, ${h})`;const d=ye(e.from.direction,a,h),c=ye(e.to.direction,a,h),l={x:i,y:n};let g={x:-c.x,y:-c.y},p;e.category===T.PortCycle?(p=this.params.createCyclePath,g=d):e.category===T.NodeCycle?p=this.params.createDetourPath:p=this.params.createLinePath;const f=p(d,c,l,a,h);this.line.setAttribute("d",f.path);let y=null;this.sourceArrow&&(y=this.arrowRenderer({direction:d,shift:u,arrowLength:this.params.arrowLength}),this.sourceArrow.setAttribute("d",y));let A=null;this.targetArrow&&(A=this.arrowRenderer({direction:g,shift:l,arrowLength:this.params.arrowLength}),this.targetArrow.setAttribute("d",A)),this.afterRenderEmitter.emit({edgePath:f,sourceArrowPath:y,targetArrowPath:A})}}const ot=r=>e=>{const o=[u,{x:e.arrowLength,y:r.radius},{x:e.arrowLength,y:-r.radius}].map(h=>m(h,e.direction,u)).map(h=>({x:h.x+e.shift.x,y:h.y+e.shift.y})),i=`M ${o[0].x} ${o[0].y}`,n=`L ${o[1].x} ${o[1].y}`,a=`L ${o[2].x} ${o[2].y}`;return`${i} ${n} ${a} Z`},st=r=>e=>{const t=r.radius,o=e.arrowLength,i=(o*o+2*o*t)/(2*t),n=i+t,a=o+t-t*(o+t)/n,h=t*i/n,c=[u,{x:a,y:-h},{x:a,y:h}].map(y=>m(y,e.direction,u)).map(y=>({x:y.x+e.shift.x,y:y.y+e.shift.y})),l=`M ${c[0].x} ${c[0].y}`,g=`A ${i} ${i} 0 0 0 ${c[1].x} ${c[1].y}`,p=`A ${t} ${t} 0 0 0 ${c[2].x} ${c[2].y}`,f=`A ${i} ${i} 0 0 0 ${c[0].x} ${c[0].y}`;return`${l} ${g} ${p} ${f}`},it=r=>e=>{const t=r.smallRadius,o=r.radius,i=m({x:e.arrowLength,y:0},{x:Math.cos(r.angle),y:Math.sin(r.angle)},{x:e.arrowLength+r.smallRadius,y:0}),a=[u,{x:i.x,y:-i.y},i].map(g=>m(g,e.direction,u)).map(g=>({x:g.x+e.shift.x,y:g.y+e.shift.y})),h=`M ${a[0].x} ${a[0].y}`,d=`A ${o} ${o} 0 0 1 ${a[1].x} ${a[1].y}`,c=`A ${t} ${t} 0 0 1 ${a[2].x} ${a[2].y}`,l=`A ${o} ${o} 0 0 1 ${a[0].x} ${a[0].y}`;return`${h} ${d} ${c} ${l}`},$=r=>{if(typeof r=="function")return r;switch(r.type){case"triangle":return ot({radius:r.radius??w.polygonArrowRadius});case"arc":return st({radius:r.radius??w.circleArrowRadius});default:return it({smallRadius:r.smallRadius??w.wedgeArrowSmallRadius,angle:r.angle??w.wedgeArrowAngle,radius:r.radius??w.wedgeArrowRadius})}};class me{constructor(e){s(this,"svg");s(this,"group");s(this,"line");s(this,"sourceArrow");s(this,"targetArrow");s(this,"onAfterRender");s(this,"arrowLength");s(this,"curvature");s(this,"portCycleRadius");s(this,"portCycleSmallRadius");s(this,"detourDirection");s(this,"detourDistance");s(this,"hasSourceArrow");s(this,"hasTargetArrow");s(this,"pathShape");s(this,"createCyclePath",e=>new qe({sourceDirection:e,radius:this.portCycleRadius,smallRadius:this.portCycleSmallRadius,arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createDetourPath",(e,t,o,i,n)=>new Ke({to:o,sourceDirection:e,targetDirection:t,flipX:i,flipY:n,arrowLength:this.arrowLength,detourDirection:this.detourDirection,detourDistance:this.detourDistance,curvature:this.curvature,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createLinePath",(e,t,o)=>new Ge({to:o,sourceDirection:e,targetDirection:t,arrowLength:this.arrowLength,curvature:this.curvature,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??w.arrowLength,this.curvature=(e==null?void 0:e.curvature)??w.curvature,this.portCycleRadius=(e==null?void 0:e.cycleRadius)??w.cycleRadius,this.portCycleSmallRadius=(e==null?void 0:e.smallCycleRadius)??w.smallCycleRadius,this.detourDirection=(e==null?void 0:e.detourDirection)??w.detourDirection,this.detourDistance=(e==null?void 0:e.detourDistance)??w.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??w.hasTargetArrow,this.pathShape=new W({color:(e==null?void 0:e.color)??w.color,width:(e==null?void 0:e.width)??w.width,arrowRenderer:$((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){this.pathShape.render(e)}}class ve{constructor(e){s(this,"svg");s(this,"group");s(this,"line");s(this,"sourceArrow");s(this,"targetArrow");s(this,"onAfterRender");s(this,"arrowLength");s(this,"arrowOffset");s(this,"roundness");s(this,"cycleSquareSide");s(this,"detourDistance");s(this,"hasSourceArrow");s(this,"hasTargetArrow");s(this,"pathShape");s(this,"createCyclePath",e=>new G({sourceDirection:e,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createDetourPath",(e,t,o,i,n)=>new tt({to:o,sourceDirection:e,targetDirection:t,flipX:i,flipY:n,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createLinePath",(e,t,o,i)=>new Ze({to:o,sourceDirection:e,targetDirection:t,flipX:i,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??w.arrowLength,this.arrowOffset=(e==null?void 0:e.arrowOffset)??w.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??w.cycleSquareSide;const t=(e==null?void 0:e.roundness)??w.roundness;this.roundness=Math.min(t,this.arrowOffset,this.cycleSquareSide/2),this.detourDistance=(e==null?void 0:e.detourDistance)??w.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??w.hasTargetArrow,this.pathShape=new W({color:(e==null?void 0:e.color)??w.color,width:(e==null?void 0:e.width)??w.width,arrowRenderer:$((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){this.pathShape.render(e)}}class Ee{constructor(e){s(this,"svg");s(this,"group");s(this,"line");s(this,"sourceArrow");s(this,"targetArrow");s(this,"onAfterRender");s(this,"arrowLength");s(this,"arrowOffset");s(this,"roundness");s(this,"cycleSquareSide");s(this,"detourDirection");s(this,"detourDistance");s(this,"hasSourceArrow");s(this,"hasTargetArrow");s(this,"pathShape");s(this,"createCyclePath",e=>new G({sourceDirection:e,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createDetourPath",(e,t,o,i,n)=>new Je({to:o,sourceDirection:e,targetDirection:t,flipX:i,flipY:n,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDirection:this.detourDirection,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createLinePath",(e,t,o)=>new Qe({to:o,sourceDirection:e,targetDirection:t,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??w.arrowLength,this.arrowOffset=(e==null?void 0:e.arrowOffset)??w.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??w.cycleSquareSide;const t=(e==null?void 0:e.roundness)??w.roundness;this.roundness=Math.min(t,this.arrowOffset,this.cycleSquareSide/2),this.detourDirection=(e==null?void 0:e.detourDirection)??w.detourDirection,this.detourDistance=(e==null?void 0:e.detourDistance)??w.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??w.hasTargetArrow,this.pathShape=new W({color:(e==null?void 0:e.color)??w.color,width:(e==null?void 0:e.width)??w.width,arrowRenderer:$((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){this.pathShape.render(e)}}class Ae{constructor(e){s(this,"svg");s(this,"group");s(this,"line");s(this,"sourceArrow");s(this,"targetArrow");s(this,"onAfterRender");s(this,"arrowLength");s(this,"arrowOffset");s(this,"roundness");s(this,"cycleSquareSide");s(this,"detourDistance");s(this,"hasSourceArrow");s(this,"hasTargetArrow");s(this,"pathShape");s(this,"createCyclePath",e=>new G({sourceDirection:e,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createDetourPath",(e,t,o,i,n)=>new rt({to:o,sourceDirection:e,targetDirection:t,flipX:i,flipY:n,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createLinePath",(e,t,o,i,n)=>new _e({to:o,sourceDirection:e,targetDirection:t,flipY:n,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??w.arrowLength,this.arrowOffset=(e==null?void 0:e.arrowOffset)??w.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??w.cycleSquareSide;const t=(e==null?void 0:e.roundness)??w.roundness;this.roundness=Math.min(t,this.arrowOffset,this.cycleSquareSide/2),this.detourDistance=(e==null?void 0:e.detourDistance)??w.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??w.hasTargetArrow,this.pathShape=new W({color:(e==null?void 0:e.color)??w.color,width:(e==null?void 0:e.width)??w.width,arrowRenderer:$((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){this.pathShape.render(e)}}class K{constructor(e){s(this,"svg");s(this,"group",we());s(this,"line");s(this,"sourceArrow",null);s(this,"targetArrow",null);s(this,"color");s(this,"width");s(this,"arrowLength");s(this,"sourceOffset");s(this,"targetOffset");s(this,"onAfterRender");s(this,"afterRenderEmitter");s(this,"arrowRenderer");[this.afterRenderEmitter,this.onAfterRender]=x(),this.color=(e==null?void 0:e.color)??w.color,this.width=(e==null?void 0:e.width)??w.width,this.arrowLength=(e==null?void 0:e.arrowLength)??w.arrowLength,this.arrowRenderer=$((e==null?void 0:e.arrowRenderer)??{}),this.sourceOffset=(e==null?void 0:e.sourceOffset)??w.preOffset,this.targetOffset=(e==null?void 0:e.targetOffset)??w.preOffset,this.svg=ue(this.color),this.svg.appendChild(this.group),this.line=pe(this.width),this.group.appendChild(this.line),e!=null&&e.hasSourceArrow&&(this.sourceArrow=O(),this.group.appendChild(this.sourceArrow)),e!=null&&e.hasTargetArrow&&(this.targetArrow=O(),this.group.appendChild(this.targetArrow))}render(e){const{x:t,y:o,width:i,height:n,flipX:a,flipY:h}=le(e.from,e.to);fe(this.svg,{x:t,y:o,width:i,height:n}),this.group.style.transform=`scale(${a}, ${h})`;const d={x:i,y:n},c=new et({to:d,sourceOffset:this.sourceOffset,targetOffset:this.targetOffset,hasSourceArrow:this.sourceArrow!==null,hasTargetArrow:this.targetArrow!==null,arrowLength:this.arrowLength});this.line.setAttribute("d",c.path);let l=null,g=null;const p=c.diagonalDistance;if(p===0)this.sourceArrow!==null&&(l="",this.sourceArrow.setAttribute("d",l)),this.targetArrow!==null&&(g="",this.targetArrow.setAttribute("d",g));else{const f={x:d.x/p,y:d.y/p};if(this.sourceArrow){const y={x:f.x*this.sourceOffset,y:f.y*this.sourceOffset};l=this.arrowRenderer({direction:f,shift:y,arrowLength:this.arrowLength}),this.sourceArrow.setAttribute("d",l)}if(this.targetArrow){const y={x:f.x*this.targetOffset,y:f.y*this.targetOffset};g=this.arrowRenderer({direction:{x:-f.x,y:-f.y},shift:{x:d.x-y.x,y:d.y-y.y},arrowLength:this.arrowLength}),this.targetArrow.setAttribute("d",g)}}this.afterRenderEmitter.emit({edgePath:c,sourceArrowPath:l,targetArrowPath:g})}}const nt=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","g");return r.style.pointerEvents="auto",r.style.cursor="pointer",r},at=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","path");return e.setAttribute("stroke","transparent"),e.setAttribute("stroke-width",`${r}`),e.setAttribute("fill","none"),e.setAttribute("stroke-linecap","round"),e},xe=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","path");return e.setAttribute("stroke-linejoin","round"),e.setAttribute("stroke-width",`${r}`),e.setAttribute("fill","transparent"),e.setAttribute("stroke","transparent"),e};class Se extends Error{constructor(e){super(e),this.name="InteractiveEdgeError"}}class Z{constructor(e,t){s(this,"svg");s(this,"group");s(this,"line");s(this,"sourceArrow");s(this,"targetArrow");s(this,"handle",nt());s(this,"onAfterRender");s(this,"interactiveLine");s(this,"interactiveSourceArrow",null);s(this,"interactiveTargetArrow",null);if(this.baseEdge=e,e instanceof Z)throw new Se("interactive edge can be configured only once");this.svg=this.baseEdge.svg,this.group=this.baseEdge.group,this.line=this.baseEdge.line,this.sourceArrow=this.baseEdge.sourceArrow,this.targetArrow=this.baseEdge.targetArrow,this.onAfterRender=this.baseEdge.onAfterRender;const o=(t==null?void 0:t.distance)??w.interactiveWidth;this.interactiveLine=at(o),this.handle.appendChild(this.interactiveLine),this.sourceArrow&&(this.interactiveSourceArrow=xe(o),this.handle.appendChild(this.interactiveSourceArrow)),this.targetArrow&&(this.interactiveTargetArrow=xe(o),this.handle.appendChild(this.interactiveTargetArrow)),this.group.appendChild(this.handle),this.baseEdge.onAfterRender.subscribe(i=>{this.interactiveLine.setAttribute("d",i.edgePath.path),this.interactiveSourceArrow&&this.interactiveSourceArrow.setAttribute("d",i.sourceArrowPath),this.interactiveTargetArrow&&this.interactiveTargetArrow.setAttribute("d",i.targetArrowPath)})}render(e){this.baseEdge.render(e)}}class ht{constructor(e,t){s(this,"group");s(this,"line");s(this,"sourceArrow");s(this,"targetArrow");s(this,"onAfterRender");s(this,"svg");this.baseShape=e,this.midpointElement=t,this.svg=this.baseShape.svg,this.group=this.baseShape.group,this.line=this.baseShape.line,this.sourceArrow=this.baseShape.sourceArrow,this.targetArrow=this.baseShape.targetArrow,this.onAfterRender=this.baseShape.onAfterRender,this.svg.append(this.midpointElement),this.baseShape.onAfterRender.subscribe(o=>{const i=o.edgePath.midpoint,n=`translate(${i.x}px, ${i.y}px)`;this.midpointElement.style.setProperty("transform",n)})}render(e){this.baseShape.render(e)}}class Pe{constructor(e){s(this,"onAfterNodeAdded");s(this,"onAfterNodeUpdated");s(this,"onAfterNodePriorityUpdated");s(this,"onBeforeNodeRemoved");s(this,"onAfterPortMarked");s(this,"onAfterPortUpdated");s(this,"onBeforePortUnmarked");s(this,"onAfterEdgeAdded");s(this,"onAfterEdgeShapeUpdated");s(this,"onAfterEdgeUpdated");s(this,"onAfterEdgePriorityUpdated");s(this,"onBeforeEdgeRemoved");s(this,"onBeforeClear");this.graphStore=e,this.onAfterNodeAdded=this.graphStore.onAfterNodeAdded,this.onAfterNodeUpdated=this.graphStore.onAfterNodeUpdated,this.onAfterNodePriorityUpdated=this.graphStore.onAfterNodePriorityUpdated,this.onBeforeNodeRemoved=this.graphStore.onBeforeNodeRemoved,this.onAfterPortMarked=this.graphStore.onAfterPortAdded,this.onAfterPortUpdated=this.graphStore.onAfterPortUpdated,this.onBeforePortUnmarked=this.graphStore.onBeforePortRemoved,this.onAfterEdgeAdded=this.graphStore.onAfterEdgeAdded,this.onAfterEdgeShapeUpdated=this.graphStore.onAfterEdgeShapeUpdated,this.onAfterEdgeUpdated=this.graphStore.onAfterEdgeUpdated,this.onAfterEdgePriorityUpdated=this.graphStore.onAfterEdgePriorityUpdated,this.onBeforeEdgeRemoved=this.graphStore.onBeforeEdgeRemoved,this.onBeforeClear=this.graphStore.onBeforeClear}getNode(e){const t=this.graphStore.getNode(e);if(t===void 0)return null;const o=t.payload;return{element:t.element,x:o.x,y:o.y,centerFn:o.centerFn,priority:o.priority}}getElementNodeId(e){return this.graphStore.getElementNodeId(e)??null}getAllNodeIds(){return this.graphStore.getAllNodeIds()}getPort(e){const t=this.graphStore.getPort(e);return t===void 0?null:{element:t.element,direction:t.payload.direction,nodeId:t.nodeId}}getAllPortIds(){return this.graphStore.getAllPortIds()}getNodePortIds(e){return this.graphStore.getNodePortIds(e)??null}getElementPortIds(e){return[...this.graphStore.getElementPortIds(e)]}getAllEdgeIds(){return this.graphStore.getAllEdgeIds()}getEdge(e){const t=this.graphStore.getEdge(e);if(t===void 0)return null;const o=t.payload;return{from:t.from,to:t.to,priority:o.priority,shape:o.shape}}getPortIncomingEdgeIds(e){return this.graphStore.getPort(e)===void 0?null:this.graphStore.getPortIncomingEdgeIds(e)}getPortOutgoingEdgeIds(e){return this.graphStore.getPort(e)===void 0?null:this.graphStore.getPortOutgoingEdgeIds(e)}getPortCycleEdgeIds(e){return this.graphStore.getPort(e)===void 0?null:this.graphStore.getPortCycleEdgeIds(e)}getPortAdjacentEdgeIds(e){return this.graphStore.getPort(e)===void 0?null:this.graphStore.getPortAdjacentEdgeIds(e)}getNodeIncomingEdgeIds(e){return this.graphStore.getNode(e)===void 0?null:this.graphStore.getNodeIncomingEdgeIds(e)}getNodeOutgoingEdgeIds(e){return this.graphStore.getNode(e)===void 0?null:this.graphStore.getNodeOutgoingEdgeIds(e)}getNodeCycleEdgeIds(e){return this.graphStore.getNode(e)===void 0?null:this.graphStore.getNodeCycleEdgeIds(e)}getNodeAdjacentEdgeIds(e){return this.graphStore.getNode(e)===void 0?null:this.graphStore.getNodeAdjacentEdgeIds(e)}}class be{constructor(e){s(this,"onAfterUpdated");this.viewportStore=e,this.onAfterUpdated=this.viewportStore.onAfterUpdated}getViewportMatrix(){return{...this.viewportStore.getViewportMatrix()}}getContentMatrix(){return{...this.viewportStore.getContentMatrix()}}}const Te=(r,e)=>{const t=new ae,o=new Pe(t),i=new be(e),n=new se(t,e,r),a={nodes:{centerFn:ge,priorityFn:()=>0},edges:{shapeFactory:()=>new K,priorityFn:()=>0},ports:{direction:0}};return new ne(o,i,t,e,n,a)};class k{constructor(e,t,o,i){s(this,"onAfterPortMarked",e=>{const t=this.canvas.graph.getPort(e);this.canvas.graph.getElementPortIds(t.element).length===1&&this.hookPortEvents(t.element)});s(this,"onBeforePortUnmarked",e=>{const t=this.canvas.graph.getPort(e);this.canvas.graph.getElementPortIds(t.element).length===1&&this.unhookPortEvents(t.element)});s(this,"onPortMouseDown",e=>{if(!this.params.mouseDownEventVerifier(e))return;const t=e.currentTarget,o=this.canvas.graph.getElementPortIds(t)[0];this.params.onPortPointerDown(o,{x:e.clientX,y:e.clientY})&&(e.stopPropagation(),this.window.addEventListener("mousemove",this.onWindowMouseMove,{passive:!0}),this.window.addEventListener("mouseup",this.onWindowMouseUp,{passive:!0}))});s(this,"onWindowMouseMove",e=>{if(!C(this.window,this.element,e.clientX,e.clientY)){this.stopMouseDrag();return}this.params.onPointerMove({x:e.clientX,y:e.clientY})});s(this,"onWindowMouseUp",e=>{this.params.mouseUpEventVerifier(e)&&(this.params.onPointerUp({x:e.clientX,y:e.clientY}),this.stopMouseDrag())});s(this,"onPortTouchStart",e=>{if(e.touches.length!==1)return;const t=e.touches[0],o=e.currentTarget,i=this.canvas.graph.getElementPortIds(o)[0];this.params.onPortPointerDown(i,{x:t.clientX,y:t.clientY})&&(e.stopPropagation(),this.window.addEventListener("touchmove",this.onWindowTouchMove,{passive:!0}),this.window.addEventListener("touchend",this.onWindowTouchFinish,{passive:!0}),this.window.addEventListener("touchcancel",this.onWindowTouchFinish,{passive:!0}))});s(this,"onWindowTouchMove",e=>{const t=e.touches[0];if(!C(this.window,this.element,t.clientX,t.clientY)){this.stopTouchDrag();return}this.params.onPointerMove({x:t.clientX,y:t.clientY})});s(this,"onWindowTouchFinish",e=>{const t=e.changedTouches[0];this.params.onPointerUp({x:t.clientX,y:t.clientY}),this.stopTouchDrag()});s(this,"onBeforeClear",()=>{this.canvas.graph.getAllPortIds().forEach(e=>{const t=this.canvas.graph.getPort(e);this.unhookPortEvents(t.element)})});s(this,"onBeforeDestroy",()=>{this.params.onStopDrag(),this.removeWindowMouseListeners(),this.removeWindowTouchListeners()});this.canvas=e,this.element=t,this.window=o,this.params=i,this.canvas.graph.onAfterPortMarked.subscribe(this.onAfterPortMarked),this.canvas.graph.onBeforePortUnmarked.subscribe(this.onBeforePortUnmarked),this.canvas.graph.onBeforeClear.subscribe(this.onBeforeClear),this.canvas.onBeforeDestroy.subscribe(this.onBeforeDestroy)}static configure(e,t,o,i){new k(e,t,o,i)}hookPortEvents(e){e.addEventListener("mousedown",this.onPortMouseDown,{passive:!0}),e.addEventListener("touchstart",this.onPortTouchStart,{passive:!0})}unhookPortEvents(e){e.removeEventListener("mousedown",this.onPortMouseDown),e.removeEventListener("touchstart",this.onPortTouchStart)}stopMouseDrag(){this.params.onStopDrag(),this.removeWindowMouseListeners()}stopTouchDrag(){this.params.onStopDrag(),this.removeWindowTouchListeners()}removeWindowMouseListeners(){this.window.removeEventListener("mousemove",this.onWindowMouseMove),this.window.removeEventListener("mouseup",this.onWindowMouseUp)}removeWindowTouchListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}}class J{constructor(e,t,o,i){s(this,"grabbedNode",null);s(this,"maxNodePriority",0);s(this,"graph");s(this,"onAfterNodeAdded",e=>{this.updateMaxNodePriority(e);const t=this.graph.getNode(e);t.element.addEventListener("mousedown",this.onMouseDown,{passive:!0}),t.element.addEventListener("touchstart",this.onTouchStart,{passive:!0})});s(this,"onAfterNodeUpdated",e=>{this.updateMaxNodePriority(e)});s(this,"onBeforeNodeRemoved",e=>{const t=this.graph.getNode(e);t.element.removeEventListener("mousedown",this.onMouseDown),t.element.removeEventListener("touchstart",this.onTouchStart)});s(this,"onBeforeDestroy",()=>{this.removeMouseDragListeners(),this.removeTouchDragListeners()});s(this,"onBeforeClear",()=>{this.canvas.graph.getAllNodeIds().forEach(e=>{const t=this.canvas.graph.getNode(e);t.element.removeEventListener("mousedown",this.onMouseDown),t.element.removeEventListener("touchstart",this.onTouchStart)}),this.maxNodePriority=0});s(this,"onMouseDown",e=>{if(!this.params.mouseDownEventVerifier(e))return;const t=e.currentTarget,o=this.graph.getElementNodeId(t),i=this.graph.getNode(o);if(!this.params.nodeDragVerifier(o))return;e.stopPropagation();const a=this.calculateContentPoint({x:e.clientX,y:e.clientY});this.grabbedNode={nodeId:o,dx:a.x-i.x,dy:a.y-i.y},I(this.element,this.params.dragCursor),this.moveNodeOnTop(o),this.window.addEventListener("mousemove",this.onWindowMouseMove,{passive:!0}),this.window.addEventListener("mouseup",this.onWindowMouseUp,{passive:!0})});s(this,"onTouchStart",e=>{if(e.touches.length!==1)return;e.stopPropagation();const t=e.touches[0],o=e.currentTarget,i=this.canvas.graph.getElementNodeId(o),n=this.graph.getNode(i);if(!this.params.nodeDragVerifier({nodeId:i,element:n.element,x:n.x,y:n.y}))return;const h=this.calculateContentPoint({x:t.clientX,y:t.clientY});this.grabbedNode={nodeId:i,dx:h.x-n.x,dy:h.y-n.y},this.moveNodeOnTop(i),this.window.addEventListener("touchmove",this.onWindowTouchMove,{passive:!0}),this.window.addEventListener("touchend",this.onWindowTouchFinish,{passive:!0}),this.window.addEventListener("touchcancel",this.onWindowTouchFinish,{passive:!0})});s(this,"onWindowMouseMove",e=>{if(!C(this.window,this.element,e.clientX,e.clientY)){this.cancelMouseDrag();return}this.grabbedNode!==null&&this.moveNode(this.grabbedNode,{x:e.clientX,y:e.clientY})});s(this,"onWindowMouseUp",e=>{this.params.mouseUpEventVerifier(e)&&this.cancelMouseDrag()});s(this,"onWindowTouchMove",e=>{if(e.touches.length!==1)return;const t=e.touches[0];if(!C(this.window,this.element,t.clientX,t.clientY)){this.cancelTouchDrag();return}this.grabbedNode!==null&&this.moveNode(this.grabbedNode,{x:t.clientX,y:t.clientY})});s(this,"onWindowTouchFinish",()=>{this.cancelTouchDrag()});this.canvas=e,this.element=t,this.window=o,this.params=i,this.graph=e.graph,this.graph.onAfterNodeAdded.subscribe(this.onAfterNodeAdded),this.graph.onAfterNodeUpdated.subscribe(this.onAfterNodeUpdated),this.graph.onBeforeNodeRemoved.subscribe(this.onBeforeNodeRemoved),this.graph.onBeforeClear.subscribe(this.onBeforeClear),this.canvas.onBeforeDestroy.subscribe(this.onBeforeDestroy)}static configure(e,t,o,i){new J(e,t,o,i)}moveNode(e,t){if(this.graph.getNode(e.nodeId)===null)return;const i=this.calculateContentPoint(t),n={x:i.x-e.dx,y:i.y-e.dy},a=this.adjustNodeCoords(n);this.canvas.updateNode(e.nodeId,{x:a.x,y:a.y}),this.params.onNodeDrag(e.nodeId)}moveNodeOnTop(e){if(this.params.moveOnTop){if(this.maxNodePriority++,this.params.moveEdgesOnTop){const t=this.maxNodePriority;this.maxNodePriority++,this.graph.getNodeAdjacentEdgeIds(e).forEach(i=>{this.canvas.updateEdge(i,{priority:t})})}this.canvas.updateNode(e,{priority:this.maxNodePriority})}}cancelMouseDrag(){this.grabbedNode!==null&&this.graph.getNode(this.grabbedNode.nodeId)!==null&&this.params.onNodeDragFinished(this.grabbedNode.nodeId),this.grabbedNode=null,I(this.element,null),this.removeMouseDragListeners()}removeMouseDragListeners(){this.window.removeEventListener("mouseup",this.onWindowMouseUp),this.window.removeEventListener("mousemove",this.onWindowMouseMove)}cancelTouchDrag(){if(this.grabbedNode!==null){const e=this.graph.getNode(this.grabbedNode.nodeId);e!==null&&this.params.onNodeDragFinished({nodeId:this.grabbedNode.nodeId,element:e.element,x:e.x,y:e.y})}this.grabbedNode=null,this.removeTouchDragListeners()}removeTouchDragListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}updateMaxNodePriority(e){const t=this.graph.getNode(e).priority;this.maxNodePriority=Math.max(this.maxNodePriority,t)}calculateContentPoint(e){const t=this.element.getBoundingClientRect(),o={x:e.x-t.x,y:e.y-t.y},i=this.canvas.viewport.getViewportMatrix();return E(i,o)}adjustNodeCoords(e){const t=this.params.gridSize;if(t!==null){const o=t/2;return{x:Math.floor((e.x+o)/t)*t,y:Math.floor((e.y+o)/t)*t}}return e}}const dt=(r,e,t)=>({scale:r.scale,x:r.x+r.scale*e,y:r.y+r.scale*t}),ct=(r,e,t,o)=>({scale:r.scale*e,x:r.scale*(1-e)*t+r.x,y:r.scale*(1-e)*o+r.y}),z=r=>{const e=[],t=r.touches.length;for(let h=0;h<t;h++)e.push([r.touches[h].clientX,r.touches[h].clientY]);const o=e.reduce((h,d)=>[h[0]+d[0],h[1]+d[1]],[0,0]),i=[o[0]/t,o[1]/t],a=e.map(h=>[h[0]-i[0],h[1]-i[1]]).reduce((h,d)=>h+Math.sqrt(d[0]*d[0]+d[1]*d[1]),0);return{x:i[0],y:i[1],scale:a/t,touchesCnt:t,touches:e}};class X{constructor(e,t,o,i){s(this,"viewport");s(this,"prevTouches",null);s(this,"wheelFinishTimer",null);s(this,"transformInProgress",!1);s(this,"onBeforeDestroy",()=>{this.removeMouseDragListeners(),this.removeTouchDragListeners()});s(this,"onMouseDown",e=>{this.element===null||!this.params.mouseDownEventVerifier(e)||(I(this.element,this.params.shiftCursor),this.window.addEventListener("mousemove",this.onWindowMouseMove,{passive:!0}),this.window.addEventListener("mouseup",this.onWindowMouseUp,{passive:!0}),this.startRegisteredTransform())});s(this,"onWindowMouseMove",e=>{const t=C(this.window,this.element,e.clientX,e.clientY);if(this.element===null||!t){this.stopMouseDrag();return}const o=-e.movementX,i=-e.movementY;this.moveViewport(o,i)});s(this,"onWindowMouseUp",e=>{this.element===null||!this.params.mouseUpEventVerifier(e)||this.stopMouseDrag()});s(this,"onWheelScroll",e=>{if(!this.params.mouseWheelEventVerifier(e))return;const{left:t,top:o}=this.element.getBoundingClientRect(),i=e.clientX-t,n=e.clientY-o,h=1/(e.deltaY<0?this.params.wheelSensitivity:1/this.params.wheelSensitivity);this.wheelFinishTimer===null&&this.params.onTransformStarted(),this.scaleViewport(h,i,n),this.wheelFinishTimer!==null&&clearTimeout(this.wheelFinishTimer),this.wheelFinishTimer=setTimeout(()=>{this.transformInProgress||this.params.onTransformFinished(),this.wheelFinishTimer=null},this.params.scaleWheelFinishTimeout)});s(this,"onTouchStart",e=>{if(this.prevTouches!==null){this.prevTouches=z(e);return}this.prevTouches=z(e),this.window.addEventListener("touchmove",this.onWindowTouchMove,{passive:!0}),this.window.addEventListener("touchend",this.onWindowTouchFinish,{passive:!0}),this.window.addEventListener("touchcancel",this.onWindowTouchFinish,{passive:!0}),this.startRegisteredTransform()});s(this,"onWindowTouchMove",e=>{const t=z(e);if(!t.touches.every(i=>C(this.window,this.element,i[0],i[1]))){this.stopTouchDrag();return}if((t.touchesCnt===1||t.touchesCnt===2)&&this.moveViewport(-(t.x-this.prevTouches.x),-(t.y-this.prevTouches.y)),t.touchesCnt===2){const{left:i,top:n}=this.element.getBoundingClientRect(),a=this.prevTouches.x-i,h=this.prevTouches.y-n,c=1/(t.scale/this.prevTouches.scale);this.scaleViewport(c,a,h)}this.prevTouches=t});s(this,"onWindowTouchFinish",e=>{e.touches.length>0?this.prevTouches=z(e):this.stopTouchDrag()});s(this,"observer",new ResizeObserver(()=>{const e=this.viewport.getViewportMatrix(),{width:t,height:o}=this.element.getBoundingClientRect(),i=this.params.transformPreprocessor({prevTransform:e,nextTransform:e,canvasWidth:t,canvasHeight:o});this.params.onResizeTransformStarted(),this.canvas.patchViewportMatrix(i),this.params.onResizeTransformFinished()}));s(this,"preventWheelScaleListener",e=>{e.preventDefault()});this.canvas=e,this.element=t,this.window=o,this.params=i,this.element.addEventListener("wheel",this.preventWheelScaleListener,{passive:!1}),this.viewport=e.viewport,this.observer.observe(this.element),this.element.addEventListener("mousedown",this.onMouseDown,{passive:!0}),this.element.addEventListener("wheel",this.onWheelScroll,{passive:!0}),this.element.addEventListener("touchstart",this.onTouchStart,{passive:!0}),e.onBeforeDestroy.subscribe(this.onBeforeDestroy)}static configure(e,t,o,i){new X(e,t,o,i)}moveViewport(e,t){const o=this.viewport.getViewportMatrix(),i=dt(o,e,t),{width:n,height:a}=this.element.getBoundingClientRect(),h=this.params.transformPreprocessor({prevTransform:o,nextTransform:i,canvasWidth:n,canvasHeight:a});this.performTransform(h)}scaleViewport(e,t,o){const i=this.canvas.viewport.getViewportMatrix(),n=ct(i,e,t,o),{width:a,height:h}=this.element.getBoundingClientRect(),d=this.params.transformPreprocessor({prevTransform:i,nextTransform:n,canvasWidth:a,canvasHeight:h});this.performTransform(d)}stopMouseDrag(){I(this.element,null),this.removeMouseDragListeners(),this.finishRegisteredTransform()}removeMouseDragListeners(){this.window.removeEventListener("mousemove",this.onWindowMouseMove),this.window.removeEventListener("mouseup",this.onWindowMouseUp)}stopTouchDrag(){this.prevTouches=null,this.removeTouchDragListeners(),this.finishRegisteredTransform()}removeTouchDragListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}performTransform(e){this.params.onBeforeTransformChange(),this.canvas.patchViewportMatrix(e),this.params.onTransformChange()}startRegisteredTransform(){this.transformInProgress=!0,this.params.onTransformStarted()}finishRegisteredTransform(){this.transformInProgress=!1,this.params.onTransformFinished()}}class Q{constructor(e,t,o,i,n,a){s(this,"canvasResizeObserver");s(this,"nodeHorizontal");s(this,"nodeVertical");s(this,"viewport");s(this,"viewportWidth",0);s(this,"viewportHeight",0);s(this,"viewportMatrix");s(this,"loadedArea",{xFrom:1/0,xTo:1/0,yFrom:1/0,yTo:1/0});s(this,"updateLoadedArea",e=>{this.loadedArea={xFrom:e.x,xTo:e.x+e.width,yFrom:e.y,yTo:e.y+e.height}});s(this,"onAfterViewportUpdated",()=>{this.userTransformInProgress||(this.viewportMatrix=this.viewport.getViewportMatrix(),this.loadAreaAroundViewport())});s(this,"userTransformInProgress",!1);this.canvas=e,this.element=t,this.window=o,this.trigger=n,this.params=a,this.nodeHorizontal=this.params.nodeVerticalRadius,this.nodeVertical=this.params.nodeHorizontalRadius,this.canvasResizeObserver=new ResizeObserver(d=>{const c=d[0];this.viewportWidth=c.contentRect.width,this.viewportHeight=c.contentRect.height,this.scheduleLoadAreaAroundViewport()}),this.viewport=e.viewport;const h={...i,onResizeTransformStarted:()=>{this.userTransformInProgress=!0,i.onResizeTransformStarted()},onResizeTransformFinished:()=>{this.userTransformInProgress=!1,i.onResizeTransformFinished()},onBeforeTransformChange:()=>{this.userTransformInProgress=!0,i.onBeforeTransformChange()},onTransformChange:()=>{this.userTransformInProgress=!1;const d=this.viewportMatrix;this.viewportMatrix=this.viewport.getViewportMatrix(),d.scale!==this.viewportMatrix.scale&&this.scheduleEnsureViewportAreaLoaded(),i.onTransformChange()},onTransformFinished:()=>{this.scheduleLoadAreaAroundViewport(),i.onTransformFinished()}};X.configure(e,this.element,this.window,h),this.viewportMatrix=this.viewport.getViewportMatrix(),this.trigger.subscribe(this.updateLoadedArea),this.canvasResizeObserver.observe(this.element),this.canvas.viewport.onAfterUpdated.subscribe(this.onAfterViewportUpdated)}static configure(e,t,o,i,n,a){new Q(e,t,o,i,n,a)}scheduleLoadAreaAroundViewport(){setTimeout(()=>{this.loadAreaAroundViewport()})}scheduleEnsureViewportAreaLoaded(){const e=this.viewportWidth*this.viewportMatrix.scale,t=this.viewportHeight*this.viewportMatrix.scale,o=this.viewportMatrix.x-this.nodeHorizontal,i=this.viewportMatrix.y-this.nodeVertical,n=this.viewportMatrix.x+e+this.nodeHorizontal,a=this.viewportMatrix.y+t+this.nodeVertical;this.loadedArea.xFrom<o&&this.loadedArea.xTo>n&&this.loadedArea.yFrom<i&&this.loadedArea.yTo>a||this.scheduleLoadAreaAroundViewport()}loadAreaAroundViewport(){const e=this.viewportWidth*this.viewportMatrix.scale,t=this.viewportHeight*this.viewportMatrix.scale,o=this.viewportMatrix.x-e-this.nodeHorizontal,i=this.viewportMatrix.y-t-this.nodeVertical,n=3*e+2*this.nodeHorizontal,a=3*t+2*this.nodeVertical;this.trigger.emit({x:o,y:i,width:n,height:a})}}const gt=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","svg");return r.style.position="absolute",r.style.inset="0",r},lt=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","rect");return r.setAttribute("fill","url(#pattern)"),r},ut=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","pattern");return r.setAttribute("id","pattern"),r};class _{constructor(e,t,o){s(this,"svg",gt());s(this,"patternRenderingRectangle",lt());s(this,"pattern",ut());s(this,"patternContent");s(this,"tileWidth");s(this,"tileHeight");s(this,"halfTileWidth");s(this,"halfTileHeight");s(this,"maxViewportScale");s(this,"visible",!1);s(this,"resizeObserver",new ResizeObserver(e=>{const t=e[0],{width:o,height:i}=t.contentRect;this.svg.setAttribute("width",`${o}`),this.svg.setAttribute("height",`${i}`),this.patternRenderingRectangle.setAttribute("width",`${o}`),this.patternRenderingRectangle.setAttribute("height",`${i}`);const n=this.tileWidth/o,a=this.tileHeight/i;this.pattern.setAttribute("width",`${n}`),this.pattern.setAttribute("height",`${a}`)}));s(this,"onAfterTransformUpdated",()=>{const e=this.canvas.viewport.getContentMatrix(),t=e.x-this.halfTileWidth*e.scale,o=e.y-this.halfTileHeight*e.scale,i=`matrix(${e.scale}, 0, 0, ${e.scale}, ${t}, ${o})`;this.pattern.setAttribute("patternTransform",i),this.updateVisibility()});this.canvas=e,this.backgroundHost=o,this.tileWidth=t.tileWidth,this.tileHeight=t.tileHeight,this.halfTileWidth=this.tileWidth/2,this.halfTileHeight=this.tileHeight/2,this.patternContent=t.renderer,this.maxViewportScale=t.maxViewportScale;const i=`translate(${this.halfTileWidth}, ${this.halfTileHeight})`;this.patternContent.setAttribute("transform",i),this.pattern.appendChild(this.patternContent);const n=document.createElementNS("http://www.w3.org/2000/svg","defs");n.appendChild(this.pattern),this.svg.appendChild(n),this.svg.appendChild(this.patternRenderingRectangle),this.resizeObserver.observe(this.backgroundHost),this.canvas.viewport.onAfterUpdated.subscribe(this.onAfterTransformUpdated),this.onAfterTransformUpdated()}static configure(e,t,o){new _(e,t,o)}updateVisibility(){const t=this.canvas.viewport.getViewportMatrix().scale>this.maxViewportScale;t&&this.visible?(this.visible=!1,this.backgroundHost.removeChild(this.svg)):!t&&!this.visible&&(this.visible=!0,this.backgroundHost.appendChild(this.svg))}}class q{constructor(e,t,o,i,n){s(this,"overlayCanvas");s(this,"staticPortId",null);s(this,"isTargetDragging",!0);s(this,"onEdgeCreated",e=>{this.params.onAfterEdgeCreated(e)});this.canvas=e,this.overlayLayer=t,this.viewportStore=o,this.window=i,this.params=n,this.overlayCanvas=Te(this.overlayLayer,this.viewportStore),k.configure(this.canvas,this.overlayLayer,this.window,{mouseDownEventVerifier:this.params.mouseDownEventVerifier,mouseUpEventVerifier:this.params.mouseUpEventVerifier,onStopDrag:()=>{this.resetDragState()},onPortPointerDown:(a,h)=>{const d=this.params.connectionTypeResolver(a);return d===null?!1:(this.grabPort(a,h,d),!0)},onPointerMove:a=>{this.moveDraggingPort(a)},onPointerUp:a=>{this.tryCreateConnection(a)}})}static configure(e,t,o,i,n){new q(e,t,o,i,n)}grabPort(e,t,o){const i=this.canvas.graph.getPort(e);this.staticPortId=e;const n=i.element.getBoundingClientRect(),a=n.x+n.width/2,h=n.y+n.height/2,d=this.overlayLayer.getBoundingClientRect(),c=this.canvas.viewport.getViewportMatrix(),l=E(c,{x:a-d.x,y:h-d.y}),g=E(c,{x:t.x-d.x,y:t.y-d.y}),p={overlayId:D.Static,portCoords:l,portDirection:i.direction},f={overlayId:D.Dragging,portCoords:g,portDirection:this.params.dragPortDirection};this.isTargetDragging=o==="direct";const[y,A]=this.isTargetDragging?[p,f]:[f,p];this.overlayCanvas.addNode(B(y)),this.overlayCanvas.addNode(B(A)),this.overlayCanvas.addEdge({from:y.overlayId,to:A.overlayId,shape:this.params.edgeShapeFactory(D.Edge)})}resetDragState(){this.staticPortId=null,this.isTargetDragging=!0,this.overlayCanvas.clear()}tryCreateConnection(e){const t=ce(this.canvas.graph,e),o=this.staticPortId;if(t===null){this.params.onEdgeCreationInterrupted({staticPortId:o,isDirect:this.isTargetDragging});return}const i=this.isTargetDragging?o:t,n=this.isTargetDragging?t:o,a={from:i,to:n},h=this.params.connectionPreprocessor(a);h!==null?(this.canvas.graph.onAfterEdgeAdded.subscribe(this.onEdgeCreated),this.canvas.addEdge(h),this.canvas.graph.onAfterEdgeAdded.unsubscribe(this.onEdgeCreated)):this.params.onEdgeCreationPrevented(a)}moveDraggingPort(e){const t=this.overlayLayer.getBoundingClientRect(),o={x:e.x-t.x,y:e.y-t.y},i=this.canvas.viewport.getViewportMatrix(),n=E(i,o);this.overlayCanvas.updateNode(D.Dragging,{x:n.x,y:n.y})}}class ee{constructor(e,t,o,i,n){s(this,"overlayCanvas");s(this,"staticPortId",null);s(this,"isTargetDragging",!0);s(this,"draggingEdgePayload",null);s(this,"onEdgeReattached",e=>{this.params.onAfterEdgeReattached(e)});this.canvas=e,this.overlayLayer=t,this.viewportStore=o,this.window=i,this.params=n,this.overlayCanvas=Te(this.overlayLayer,this.viewportStore),k.configure(this.canvas,this.overlayLayer,this.window,{mouseDownEventVerifier:this.params.mouseDownEventVerifier,mouseUpEventVerifier:this.params.mouseUpEventVerifier,onStopDrag:()=>{this.resetDragState()},onPortPointerDown:(a,h)=>this.tryStartEdgeDragging(a,h),onPointerMove:a=>{this.moveDraggingPort(a)},onPointerUp:a=>{this.tryCreateConnection(a)}})}static configure(e,t,o,i,n){new ee(e,t,o,i,n)}tryStartEdgeDragging(e,t){const o=this.params.draggingEdgeResolver(e);if(o===null)return!1;const i=this.canvas.graph.getEdge(o);if(i===null)return!1;const n=e===i.from,a=e===i.to,h=n?i.to:i.from;this.staticPortId=h,this.isTargetDragging=a;const d=this.canvas.graph.getPort(e),c=this.canvas.graph.getPort(h),l=c.element.getBoundingClientRect(),g={x:l.x+l.width/2,y:l.y+l.height/2},p=this.canvas.viewport.getViewportMatrix(),f=this.overlayLayer.getBoundingClientRect(),y=E(p,{x:g.x-f.x,y:g.y-f.y}),A=E(p,{x:t.x-f.x,y:t.y-f.y});this.draggingEdgePayload={id:o,from:i.from,to:i.to,shape:i.shape,priority:i.priority},this.canvas.removeEdge(o);const b={overlayId:D.Static,portCoords:y,portDirection:c.direction},S={overlayId:D.Dragging,portCoords:A,portDirection:d.direction},[V,N]=this.isTargetDragging?[b,S]:[S,b];this.overlayCanvas.addNode(B(V)),this.overlayCanvas.addNode(B(N));const R=this.params.draggingEdgeShapeFactory!==null?this.params.draggingEdgeShapeFactory(D.Edge):i.shape;return this.overlayCanvas.addEdge({id:D.Edge,from:V.overlayId,to:N.overlayId,shape:R}),!0}resetDragState(){this.draggingEdgePayload=null,this.staticPortId=null,this.isTargetDragging=!0,this.overlayCanvas.clear()}moveDraggingPort(e){const t=this.overlayLayer.getBoundingClientRect(),o={x:e.x-t.x,y:e.y-t.y},i=this.canvas.viewport.getViewportMatrix(),n=E(i,o);this.overlayCanvas.updateNode(D.Dragging,{x:n.x,y:n.y})}tryCreateConnection(e){const t=ce(this.canvas.graph,e);if(this.overlayCanvas.removeEdge(D.Edge),t===null){const d=this.draggingEdgePayload;this.params.onEdgeReattachInterrupted({id:d.id,from:d.from,to:d.to,shape:d.shape,priority:d.priority});return}const[o,i]=this.isTargetDragging?[this.staticPortId,t]:[t,this.staticPortId],n=this.draggingEdgePayload,a={id:n.id,from:o,to:i,shape:n.shape,priority:n.priority},h=this.params.connectionPreprocessor(a);if(h!==null)this.canvas.graph.onAfterEdgeAdded.subscribe(this.onEdgeReattached),this.canvas.addEdge(h),this.canvas.graph.onAfterEdgeAdded.unsubscribe(this.onEdgeReattached);else{const d=this.draggingEdgePayload;this.params.onEdgeReattachPrevented({id:d.id,from:d.from,to:d.to,shape:d.shape,priority:d.priority})}}}const pt=()=>{const r=document.createElement("div");return r.style.width="100%",r.style.height="100%",r.style.position="relative",r},te=()=>{const r=document.createElement("div");return r.style.position="absolute",r.style.inset="0",r},De=()=>{const r=te();return r.style.pointerEvents="none",r};class wt{constructor(e){s(this,"background",te());s(this,"main",te());s(this,"overlayConnectablePorts",De());s(this,"overlayDraggableEdges",De());s(this,"host",pt());this.element=e,this.element.appendChild(this.host),this.host.appendChild(this.background),this.host.appendChild(this.main),this.host.appendChild(this.overlayConnectablePorts),this.host.appendChild(this.overlayDraggableEdges)}destroy(){this.host.removeChild(this.background),this.host.removeChild(this.main),this.host.removeChild(this.overlayConnectablePorts),this.host.removeChild(this.overlayDraggableEdges),this.element.removeChild(this.host)}}const re=r=>()=>r,Ne=re(0),ft=()=>{let r=0;return()=>r++},yt=(r,e)=>{let t=Ne,o=Ne;const i=ft();return r==="incremental"&&(t=i),e==="incremental"&&(o=i),typeof r=="number"&&(t=re(r)),typeof e=="number"&&(o=re(e)),typeof r=="function"&&(t=r),typeof e=="function"&&(o=e),{nodesPriorityFn:t,edgesPriorityFn:o}},oe=r=>{if(typeof r=="function")return r;switch(r.type){case"straight":return()=>new Ee({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowOffset:r.arrowOffset,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleSquareSide:r.cycleSquareSide,roundness:r.roundness,detourDistance:r.detourDistance,detourDirection:r.detourDirection});case"horizontal":return()=>new ve({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowOffset:r.arrowOffset,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleSquareSide:r.cycleSquareSide,roundness:r.roundness,detourDistance:r.detourDistance});case"vertical":return()=>new Ae({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowOffset:r.arrowOffset,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleSquareSide:r.cycleSquareSide,roundness:r.roundness,detourDistance:r.detourDistance});case"direct":return()=>new K({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,sourceOffset:r.sourceOffset,targetOffset:r.targetOffset});default:return()=>new me({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleRadius:r.cycleRadius,smallCycleRadius:r.smallCycleRadius,curvature:r.curvature,detourDistance:r.detourDistance,detourDirection:r.detourDirection})}},mt=r=>{var t,o,i,n,a;const e=yt((t=r.nodes)==null?void 0:t.priority,(o=r.edges)==null?void 0:o.priority);return{nodes:{centerFn:((i=r.nodes)==null?void 0:i.centerFn)??ge,priorityFn:e.nodesPriorityFn},ports:{direction:((n=r.ports)==null?void 0:n.direction)??0},edges:{shapeFactory:oe(((a=r.edges)==null?void 0:a.shape)??{}),priorityFn:e.edgesPriorityFn}}},vt=r=>{var p,f,y,A,b;const e=((p=r.events)==null?void 0:p.onNodeDrag)??(()=>{}),t=r.nodeDragVerifier??(()=>!0),o=((f=r.events)==null?void 0:f.onNodeDragFinished)??(()=>{}),i=r.moveOnTop!==!1,n=r.moveEdgesOnTop!==!1&&i,a=(y=r.mouse)==null?void 0:y.dragCursor,h=a!==void 0?a:"grab",d=(A=r.mouse)==null?void 0:A.mouseDownEventVerifier,c=d!==void 0?d:S=>S.button===0,l=(b=r.mouse)==null?void 0:b.mouseUpEventVerifier,g=l!==void 0?l:S=>S.button===0;return{moveOnTop:i,moveEdgesOnTop:n,dragCursor:h,gridSize:r.gridSize??null,mouseDownEventVerifier:c,mouseUpEventVerifier:g,onNodeDrag:e,nodeDragVerifier:t,onNodeDragFinished:o}},Et=r=>{const e=r.minX!==null?r.minX:-1/0,t=r.maxX!==null?r.maxX:1/0,o=r.minY!==null?r.minY:-1/0,i=r.maxY!==null?r.maxY:1/0;return n=>{let a=n.nextTransform.x,h=n.nextTransform.y;a<e&&a<n.prevTransform.x&&(a=Math.min(n.prevTransform.x,e));const d=n.canvasWidth*n.prevTransform.scale,c=t-d;a>c&&a>n.prevTransform.x&&(a=Math.max(n.prevTransform.x,c)),h<o&&h<n.prevTransform.y&&(h=Math.min(n.prevTransform.y,o));const l=n.canvasHeight*n.prevTransform.scale,g=i-l;return h>g&&h>n.prevTransform.y&&(h=Math.max(n.prevTransform.y,g)),{scale:n.nextTransform.scale,x:a,y:h}}},At=r=>{const e=r.maxContentScale,t=r.minContentScale,o=e!==null?1/e:0,i=t!==null?1/t:1/0;return n=>{const a=n.prevTransform,h=n.nextTransform;let d=h.scale,c=h.x,l=h.y;if(h.scale>i&&h.scale>a.scale){d=Math.max(a.scale,i),c=a.x,l=a.y;const g=(d-a.scale)/(h.scale-a.scale);c=a.x+(h.x-a.x)*g,l=a.y+(h.y-a.y)*g}if(h.scale<o&&h.scale<a.scale){d=Math.min(a.scale,o),c=a.x,l=a.y;const g=(d-a.scale)/(h.scale-a.scale);c=a.x+(h.x-a.x)*g,l=a.y+(h.y-a.y)*g}return{scale:d,x:c,y:l}}},xt=r=>e=>r.reduce((t,o)=>o({prevTransform:e.prevTransform,nextTransform:t,canvasWidth:e.canvasWidth,canvasHeight:e.canvasHeight}),e.nextTransform),Me=r=>{if(typeof r=="function")return r;switch(r.type){case"scale-limit":return At({minContentScale:r.minContentScale??0,maxContentScale:r.maxContentScale??1/0});case"shift-limit":return Et({minX:r.minX??-1/0,maxX:r.maxX??1/0,minY:r.minY??-1/0,maxY:r.maxY??1/0})}},Re=r=>{var y,A,b,S,V,N,R,U,Ce,Ve,$e,Ue;const e=(y=r==null?void 0:r.scale)==null?void 0:y.mouseWheelSensitivity,t=e!==void 0?e:1.2,o=r==null?void 0:r.transformPreprocessor;let i;o!==void 0?Array.isArray(o)?i=xt(o.map(L=>Me(L))):i=Me(o):i=L=>L.nextTransform;const n=((A=r==null?void 0:r.shift)==null?void 0:A.cursor)!==void 0?r.shift.cursor:"grab",a=((b=r==null?void 0:r.events)==null?void 0:b.onBeforeTransformChange)??(()=>{}),h=((S=r==null?void 0:r.events)==null?void 0:S.onTransformChange)??(()=>{}),d=(V=r==null?void 0:r.shift)==null?void 0:V.mouseDownEventVerifier,c=d!==void 0?d:L=>L.button===0,l=(N=r==null?void 0:r.shift)==null?void 0:N.mouseUpEventVerifier,g=l!==void 0?l:L=>L.button===0,p=(R=r==null?void 0:r.scale)==null?void 0:R.mouseWheelEventVerifier,f=p!==void 0?p:()=>!0;return{wheelSensitivity:t,onTransformStarted:((U=r==null?void 0:r.events)==null?void 0:U.onTransformStarted)??(()=>{}),onTransformFinished:((Ce=r==null?void 0:r.events)==null?void 0:Ce.onTransformFinished)??(()=>{}),onBeforeTransformChange:a,onTransformChange:h,transformPreprocessor:i,shiftCursor:n,mouseDownEventVerifier:c,mouseUpEventVerifier:g,mouseWheelEventVerifier:f,scaleWheelFinishTimeout:((Ve=r==null?void 0:r.scale)==null?void 0:Ve.wheelFinishTimeout)??500,onResizeTransformStarted:(($e=r==null?void 0:r.events)==null?void 0:$e.onResizeTransformStarted)??(()=>{}),onResizeTransformFinished:((Ue=r==null?void 0:r.events)==null?void 0:Ue.onResizeTransformFinished)??(()=>{})}},St=(r,e)=>{const t=document.createElementNS("http://www.w3.org/2000/svg","circle");return t.setAttribute("cx","0"),t.setAttribute("cy","0"),t.setAttribute("r",`${r}`),t.setAttribute("fill",`${e}`),t},Pt=r=>r instanceof SVGElement?r:St((r==null?void 0:r.radius)??1.5,(r==null?void 0:r.color)??"#d8d8d8"),bt=r=>{const e=r.tileDimensions,t=(e==null?void 0:e.width)??25,o=(e==null?void 0:e.height)??25,i=Pt(r.renderer??{});return{tileWidth:t,tileHeight:o,renderer:i,maxViewportScale:r.maxViewportScale??10}},Tt=(r,e,t)=>{var c,l,g;const o=()=>"direct",i=p=>p,n=p=>p.button===0,a=()=>{},h=()=>{},d=()=>{};return{connectionTypeResolver:r.connectionTypeResolver??o,connectionPreprocessor:r.connectionPreprocessor??i,mouseDownEventVerifier:r.mouseDownEventVerifier??n,mouseUpEventVerifier:r.mouseUpEventVerifier??n,onAfterEdgeCreated:((c=r.events)==null?void 0:c.onAfterEdgeCreated)??a,onEdgeCreationInterrupted:((l=r.events)==null?void 0:l.onEdgeCreationInterrupted)??d,onEdgeCreationPrevented:((g=r.events)==null?void 0:g.onEdgeCreationPrevented)??h,dragPortDirection:r.dragPortDirection??t,edgeShapeFactory:r.edgeShape!==void 0?oe(r.edgeShape):e}},Dt=(r,e)=>{var c,l,g;const t=p=>p,o=p=>p.button===0&&p.ctrlKey,i=p=>p.button===0,n=p=>{const f=e.getPortAdjacentEdgeIds(p);return f.length>0?f[f.length-1]:null},a=()=>{},h=()=>{},d=()=>{};return{connectionPreprocessor:r.connectionPreprocessor??t,mouseDownEventVerifier:r.mouseDownEventVerifier??o,mouseUpEventVerifier:r.mouseUpEventVerifier??i,draggingEdgeResolver:r.draggingEdgeResolver??n,draggingEdgeShapeFactory:r.draggingEdgeShape!==void 0?oe(r.draggingEdgeShape):null,onAfterEdgeReattached:((c=r.events)==null?void 0:c.onAfterEdgeReattached)??a,onEdgeReattachInterrupted:((l=r.events)==null?void 0:l.onEdgeReattachInterrupted)??d,onEdgeReattachPrevented:((g=r.events)==null?void 0:g.onEdgeReattachPrevented)??h}},Nt=r=>({nodeVerticalRadius:r.nodeContainingRadius.vertical,nodeHorizontalRadius:r.nodeContainingRadius.horizontal}),Mt=r=>{var e,t;return{onAfterNodeDetached:((e=r==null?void 0:r.events)==null?void 0:e.onAfterNodeDetached)??(()=>{}),onBeforeNodeAttached:((t=r==null?void 0:r.events)==null?void 0:t.onBeforeNodeAttached)??(()=>{})}};class Le extends Error{constructor(){super(...arguments);s(this,"name","CanvasBuilderError")}}class Rt{constructor(e){s(this,"used",!1);s(this,"canvasDefaults",{});s(this,"dragConfig",{});s(this,"transformConfig",{});s(this,"backgroundConfig",{});s(this,"connectablePortsConfig",{});s(this,"draggableEdgesConfig",{});s(this,"virtualScrollConfig");s(this,"hasDraggableNode",!1);s(this,"hasTransformableViewport",!1);s(this,"hasNodeResizeReactiveEdges",!1);s(this,"hasBackground",!1);s(this,"hasUserConnectablePorts",!1);s(this,"hasUserDraggableEdges",!1);s(this,"boxRenderingTrigger",new ie);s(this,"graphStore",new ae);s(this,"viewportStore",new Xe);s(this,"graph",new Pe(this.graphStore));s(this,"viewport",new be(this.viewportStore));s(this,"window",window);this.element=e}setDefaults(e){return this.canvasDefaults=e,this}enableUserDraggableNodes(e){return this.hasDraggableNode=!0,this.dragConfig=e??{},this}enableUserTransformableViewport(e){return this.hasTransformableViewport=!0,this.transformConfig=e??{},this}enableNodeResizeReactiveEdges(){return this.hasNodeResizeReactiveEdges=!0,this}enableVirtualScroll(e){return this.virtualScrollConfig=e,this}enableBackground(e){return this.hasBackground=!0,this.backgroundConfig=e??{},this}enableUserConnectablePorts(e){return this.hasUserConnectablePorts=!0,this.connectablePortsConfig=e??{},this}enableUserDraggableEdges(e){return this.hasUserDraggableEdges=!0,this.draggableEdgesConfig=e??{},this}build(){if(this.used)throw new Le("CanvasBuilder is a single use object");this.used=!0;const e=new wt(this.element);let t=new se(this.graphStore,this.viewportStore,e.main);this.virtualScrollConfig!==void 0&&(t=new We(t,this.graphStore,this.boxRenderingTrigger,Mt(this.virtualScrollConfig))),t=new ke(t,this.graphStore);const o=mt(this.canvasDefaults),i=new ne(this.graph,this.viewport,this.graphStore,this.viewportStore,t,o);if(this.hasBackground&&_.configure(i,bt(this.backgroundConfig),e.background),this.hasNodeResizeReactiveEdges&&Y.configure(i),this.hasDraggableNode&&J.configure(i,e.main,this.window,vt(this.dragConfig)),this.hasUserConnectablePorts){const a=Tt(this.connectablePortsConfig,o.edges.shapeFactory,o.ports.direction);q.configure(i,e.overlayConnectablePorts,this.viewportStore,this.window,a)}if(this.hasUserDraggableEdges){const a=Dt(this.draggableEdgesConfig,i.graph);ee.configure(i,e.overlayDraggableEdges,this.viewportStore,this.window,a)}this.virtualScrollConfig!==void 0?Q.configure(i,e.main,this.window,Re(this.transformConfig),this.boxRenderingTrigger,Nt(this.virtualScrollConfig)):this.hasTransformableViewport&&X.configure(i,e.main,this.window,Re(this.transformConfig));const n=()=>{e.destroy(),i.onBeforeDestroy.unsubscribe(n)};return i.onBeforeDestroy.subscribe(n),i}}v.BezierEdgeShape=me,v.CanvasBuilder=Rt,v.CanvasBuilderError=Le,v.CanvasError=P,v.DirectEdgeShape=K,v.HorizontalEdgeShape=ve,v.InteractiveEdgeError=Se,v.InteractiveEdgeShape=Z,v.MidpointEdgeShape=ht,v.StraightEdgeShape=Ee,v.VerticalEdgeShape=Ae,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})});
1
+ (function(v,E){typeof exports=="object"&&typeof module<"u"?E(exports):typeof define=="function"&&define.amd?define(["exports"],E):(v=typeof globalThis<"u"?globalThis:v||self,E(v.HtmlGraph={}))})(this,function(v){"use strict";var kt=Object.defineProperty;var zt=(v,E,T)=>E in v?kt(v,E,{enumerable:!0,configurable:!0,writable:!0,value:T}):v[E]=T;var s=(v,E,T)=>zt(v,typeof E!="symbol"?E+"":E,T);const E=(r,e)=>({x:r.scale*e.x+r.x,y:r.scale*e.y+r.y});var T=(r=>(r.Line="line",r.NodeCycle="node-cycle",r.PortCycle="port-cycle",r))(T||{});const Fe=()=>{const r=document.createElement("div");return r.style.width="100%",r.style.height="100%",r.style.position="relative",r.style.overflow="hidden",r},We=()=>{const r=document.createElement("div");return r.style.position="absolute",r.style.top="0",r.style.left="0",r.style.width="0",r.style.height="0",r},ke=r=>{r.style.position="absolute",r.style.top="0",r.style.left="0",r.style.visibility="hidden"};class he{constructor(e,t,o){s(this,"host",Fe());s(this,"container",We());s(this,"edgeIdToElementMap",new Map);s(this,"attachedNodeIds",new Set);s(this,"applyTransform",()=>{const e=this.viewportStore.getContentMatrix();this.container.style.transform=`matrix(${e.scale}, 0, 0, ${e.scale}, ${e.x}, ${e.y})`});this.graphStore=e,this.viewportStore=t,this.element=o,this.element.appendChild(this.host),this.host.appendChild(this.container),this.viewportStore.onAfterUpdated.subscribe(this.applyTransform)}attachNode(e){const t=this.graphStore.getNode(e);ke(t.element),this.attachedNodeIds.add(e),this.container.appendChild(t.element),this.updateNodePosition(e),this.updateNodePriority(e),t.element.style.visibility="visible"}detachNode(e){const t=this.graphStore.getNode(e);this.container.removeChild(t.element),this.attachedNodeIds.delete(e)}attachEdge(e){const t=this.graphStore.getEdge(e).payload.shape.svg;this.edgeIdToElementMap.set(e,t),this.container.appendChild(t),this.renderEdge(e),this.updateEdgePriority(e)}detachEdge(e){const t=this.edgeIdToElementMap.get(e);this.container.removeChild(t),this.edgeIdToElementMap.delete(e)}clear(){this.edgeIdToElementMap.forEach((e,t)=>{this.detachEdge(t)}),this.attachedNodeIds.forEach(e=>{this.detachNode(e)})}destroy(){this.viewportStore.onAfterUpdated.unsubscribe(this.applyTransform),this.element.removeChild(this.host),this.host.removeChild(this.container)}updateNodePosition(e){const t=this.graphStore.getNode(e),{width:o,height:i}=t.element.getBoundingClientRect(),n=this.viewportStore.getViewportMatrix().scale,{payload:a}=t,h=a.centerFn(o,i),d=a.x-n*h.x,c=a.y-n*h.y;t.element.style.transform=`translate(${d}px, ${c}px)`}updateNodePriority(e){const t=this.graphStore.getNode(e);t.element.style.zIndex=`${t.payload.priority}`}updateEdgeShape(e){const t=this.edgeIdToElementMap.get(e);this.container.removeChild(t);const i=this.graphStore.getEdge(e).payload.shape.svg;this.edgeIdToElementMap.set(e,i),this.container.appendChild(i)}renderEdge(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from),i=this.graphStore.getPort(t.to),n=o.element.getBoundingClientRect(),a=i.element.getBoundingClientRect(),h=this.host.getBoundingClientRect(),d=this.viewportStore.getViewportMatrix(),c=this.createEdgeRenderPort(o,n,h,d),l=this.createEdgeRenderPort(i,a,h,d);let g=T.Line;o.element===i.element?g=T.PortCycle:o.nodeId===i.nodeId&&(g=T.NodeCycle),t.payload.shape.render({from:c,to:l,category:g})}updateEdgePriority(e){const t=this.graphStore.getEdge(e);t.payload.shape.svg.style.zIndex=`${t.payload.priority}`}createEdgeRenderPort(e,t,o,i){const n={x:t.left-o.left,y:t.top-o.top},a=E(i,n);return{x:a.x,y:a.y,width:t.width*i.scale,height:t.height*i.scale,direction:e.payload.direction}}}class ze{constructor(e){s(this,"xFrom",1/0);s(this,"yFrom",1/0);s(this,"xTo",1/0);s(this,"yTo",1/0);this.graphStore=e}setRenderingBox(e){this.xFrom=e.x,this.xTo=e.x+e.width,this.yFrom=e.y,this.yTo=e.y+e.height}hasNode(e){const t=this.graphStore.getNode(e).payload,{x:o,y:i}=t;return o>=this.xFrom&&o<=this.xTo&&i>=this.yFrom&&i<=this.yTo}hasEdge(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from).nodeId,i=this.graphStore.getPort(t.to).nodeId,n=this.graphStore.getNode(o).payload,a=this.graphStore.getNode(i).payload,h=Math.min(n.x,a.x),d=Math.max(n.x,a.x),c=Math.min(n.y,a.y),l=Math.max(n.y,a.y);return h<=this.xTo&&d>=this.xFrom&&c<=this.yTo&&l>=this.yFrom}}class Xe{constructor(e,t,o,i){s(this,"attachedNodes",new Set);s(this,"attachedEdges",new Set);s(this,"renderingBox");s(this,"updateViewport",e=>{this.renderingBox.setRenderingBox(e);const t=new Set,o=new Set,i=new Set,n=new Set;this.graphStore.getAllNodeIds().forEach(a=>{const h=this.renderingBox.hasNode(a),d=this.attachedNodes.has(a);h&&!d?t.add(a):!h&&d&&o.add(a)}),this.graphStore.getAllEdgeIds().forEach(a=>{const h=this.renderingBox.hasEdge(a),d=this.attachedEdges.has(a),c=this.graphStore.getEdge(a),l=this.graphStore.getPort(c.from).nodeId,g=this.graphStore.getPort(c.to).nodeId;h&&(this.renderingBox.hasNode(l)||(t.add(l),o.delete(l)),this.renderingBox.hasNode(g)||(t.add(g),o.delete(g))),h&&!d?i.add(a):!h&&d&&n.add(a)}),n.forEach(a=>{this.handleDetachEdge(a)}),o.forEach(a=>{this.handleDetachNode(a)}),t.forEach(a=>{this.attachedNodes.has(a)||this.handleAttachNode(a)}),i.forEach(a=>{this.handleAttachEdge(a)})});this.htmlView=e,this.graphStore=t,this.trigger=o,this.params=i,this.renderingBox=new ze(this.graphStore),this.trigger.subscribe(this.updateViewport)}attachNode(e){this.renderingBox.hasNode(e)&&this.handleAttachNode(e)}detachNode(e){this.attachedNodes.has(e)&&this.handleDetachNode(e)}attachEdge(e){this.renderingBox.hasEdge(e)&&this.attachEdgeEntities(e)}detachEdge(e){this.attachedEdges.has(e)&&this.handleDetachEdge(e)}updateNodePosition(e){this.attachedNodes.has(e)?this.htmlView.updateNodePosition(e):this.renderingBox.hasNode(e)&&(this.handleAttachNode(e),this.graphStore.getNodeAdjacentEdgeIds(e).forEach(t=>{this.attachEdgeEntities(t)}))}updateNodePriority(e){this.attachedNodes.has(e)&&this.htmlView.updateNodePriority(e)}updateEdgeShape(e){this.attachedEdges.has(e)&&this.htmlView.updateEdgeShape(e)}renderEdge(e){this.attachedEdges.has(e)&&this.htmlView.renderEdge(e)}updateEdgePriority(e){this.attachedEdges.has(e)&&this.htmlView.updateEdgePriority(e)}clear(){this.htmlView.clear(),this.attachedNodes.clear(),this.attachedEdges.clear()}destroy(){this.clear(),this.htmlView.destroy(),this.trigger.unsubscribe(this.updateViewport)}attachEdgeEntities(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from).nodeId,i=this.graphStore.getPort(t.to).nodeId;this.attachedNodes.has(o)||this.handleAttachNode(o),this.attachedNodes.has(i)||this.handleAttachNode(i),this.handleAttachEdge(e)}handleAttachNode(e){this.params.onBeforeNodeAttached(e),this.attachedNodes.add(e),this.htmlView.attachNode(e)}handleDetachNode(e){this.htmlView.detachNode(e),this.attachedNodes.delete(e),this.params.onAfterNodeDetached(e)}handleAttachEdge(e){this.attachedEdges.add(e),this.htmlView.attachEdge(e)}handleDetachEdge(e){this.htmlView.detachEdge(e),this.attachedEdges.delete(e)}}class He{constructor(e,t){s(this,"deferredNodes",new Set);s(this,"deferredEdges",new Set);this.htmlView=e,this.graphStore=t}attachNode(e){this.isNodeValid(e)?this.htmlView.attachNode(e):this.deferredNodes.add(e)}detachNode(e){this.deferredNodes.has(e)?this.deferredNodes.delete(e):this.htmlView.detachNode(e)}attachEdge(e){this.isEdgeValid(e)?this.htmlView.attachEdge(e):this.deferredEdges.add(e)}detachEdge(e){this.deferredEdges.has(e)?this.deferredEdges.delete(e):this.htmlView.detachEdge(e)}updateNodePosition(e){this.deferredNodes.has(e)?this.tryAttachNode(e):this.htmlView.updateNodePosition(e)}updateNodePriority(e){this.deferredNodes.has(e)?this.tryAttachNode(e):this.htmlView.updateNodePriority(e)}updateEdgeShape(e){this.deferredEdges.has(e)?this.tryAttachEdge(e):this.htmlView.updateEdgeShape(e)}renderEdge(e){this.deferredEdges.has(e)?this.tryAttachEdge(e):this.htmlView.renderEdge(e)}updateEdgePriority(e){this.deferredEdges.has(e)?this.tryAttachEdge(e):this.htmlView.updateEdgePriority(e)}clear(){this.deferredNodes.clear(),this.deferredEdges.clear(),this.htmlView.clear()}destroy(){this.htmlView.destroy()}isNodeValid(e){const t=this.graphStore.getNode(e);return!(t.payload.x===null||t.payload.y===null)}tryAttachNode(e){this.isNodeValid(e)&&(this.deferredNodes.delete(e),this.htmlView.attachNode(e))}isEdgeValid(e){const t=this.graphStore.getEdge(e),o=this.graphStore.getPort(t.from),i=this.graphStore.getPort(t.to);return!(this.deferredNodes.has(o.nodeId)||this.deferredNodes.has(i.nodeId))}tryAttachEdge(e){this.isEdgeValid(e)&&(this.deferredEdges.delete(e),this.htmlView.attachEdge(e))}}class I{constructor(){s(this,"callbacks",new Set)}subscribe(e){this.callbacks.add(e)}unsubscribe(e){this.callbacks.delete(e)}emit(e){this.callbacks.forEach(t=>{t(e)})}}const x=()=>{const r=new I;return[r,r]};class Y{constructor(e){s(this,"counter",0);this.checkExists=e}create(e){if(e!==void 0)return e;for(;this.checkExists(this.counter);)this.counter++;return this.counter}reset(){this.counter=0}}class P extends Error{constructor(){super(...arguments);s(this,"name","CanvasError")}}class de{constructor(e,t,o,i,n,a){s(this,"nodeIdGenerator",new Y(e=>this.graphStore.getNode(e)!==void 0));s(this,"portIdGenerator",new Y(e=>this.graphStore.getPort(e)!==void 0));s(this,"edgeIdGenerator",new Y(e=>this.graphStore.getEdge(e)!==void 0));s(this,"onAfterNodeAdded",e=>{this.htmlView.attachNode(e)});s(this,"onAfterNodeUpdated",e=>{this.htmlView.updateNodePosition(e),this.graphStore.getNodeAdjacentEdgeIds(e).forEach(o=>{this.htmlView.renderEdge(o)})});s(this,"onAfterNodePriorityUpdated",e=>{this.htmlView.updateNodePriority(e)});s(this,"onBeforeNodeRemoved",e=>{this.graphStore.getNodePortIds(e).forEach(t=>{this.unmarkPort(t)}),this.htmlView.detachNode(e)});s(this,"onAfterPortUpdated",e=>{this.graphStore.getPortAdjacentEdgeIds(e).forEach(o=>{this.htmlView.renderEdge(o)})});s(this,"onBeforePortUnmarked",e=>{this.graphStore.getPortAdjacentEdgeIds(e).forEach(t=>{this.removeEdge(t)})});s(this,"onAfterEdgeAdded",e=>{this.htmlView.attachEdge(e)});s(this,"onAfterEdgeShapeUpdated",e=>{this.htmlView.updateEdgeShape(e)});s(this,"onAfterEdgeUpdated",e=>{this.htmlView.renderEdge(e)});s(this,"onAfterEdgePriorityUpdated",e=>{this.htmlView.updateEdgePriority(e)});s(this,"onBeforeEdgeRemoved",e=>{this.htmlView.detachEdge(e)});s(this,"onBeforeClear",()=>{this.nodeIdGenerator.reset(),this.portIdGenerator.reset(),this.edgeIdGenerator.reset(),this.htmlView.clear()});s(this,"onBeforeDestroyEmitter");s(this,"destroyed",!1);s(this,"onBeforeDestroy");this.graph=e,this.viewport=t,this.graphStore=o,this.viewportStore=i,this.htmlView=n,this.params=a,this.graphStore.onAfterNodeAdded.subscribe(this.onAfterNodeAdded),this.graphStore.onAfterNodeUpdated.subscribe(this.onAfterNodeUpdated),this.graphStore.onAfterNodePriorityUpdated.subscribe(this.onAfterNodePriorityUpdated),this.graphStore.onBeforeNodeRemoved.subscribe(this.onBeforeNodeRemoved),this.graphStore.onAfterPortUpdated.subscribe(this.onAfterPortUpdated),this.graphStore.onBeforePortRemoved.subscribe(this.onBeforePortUnmarked),this.graphStore.onAfterEdgeAdded.subscribe(this.onAfterEdgeAdded),this.graphStore.onAfterEdgeShapeUpdated.subscribe(this.onAfterEdgeShapeUpdated),this.graphStore.onAfterEdgeUpdated.subscribe(this.onAfterEdgeUpdated),this.graphStore.onAfterEdgePriorityUpdated.subscribe(this.onAfterEdgePriorityUpdated),this.graphStore.onBeforeEdgeRemoved.subscribe(this.onBeforeEdgeRemoved),this.graphStore.onBeforeClear.subscribe(this.onBeforeClear),[this.onBeforeDestroyEmitter,this.onBeforeDestroy]=x()}addNode(e){const t=this.nodeIdGenerator.create(e.id);if(this.graphStore.getNode(t)!==void 0)throw new P("failed to add node with existing id");if(this.graphStore.getElementNodeId(e.element)!==void 0)throw new P("failed to add node with html element already in use by another node");if(this.graphStore.addNode({id:t,element:e.element,x:e.x??null,y:e.y??null,centerFn:e.centerFn??this.params.nodes.centerFn,priority:e.priority??this.params.nodes.priorityFn()}),e.ports!==void 0)for(const o of e.ports)this.markPort({id:o.id,element:o.element,nodeId:t,direction:o.direction});return this}updateNode(e,t){if(this.graphStore.getNode(e)===void 0)throw new P("failed to update nonexistent node");return this.graphStore.updateNode(e,t??{}),this}removeNode(e){if(this.graphStore.getNode(e)===void 0)throw new P("failed to remove nonexistent node");return this.graphStore.removeNode(e),this}markPort(e){const t=this.portIdGenerator.create(e.id);if(this.graphStore.getPort(t)!==void 0)throw new P("failed to add port with existing id");if(this.graphStore.getNode(e.nodeId)===void 0)throw new P("failed to mark port for nonexistent node");return this.graphStore.addPort({id:t,element:e.element,nodeId:e.nodeId,direction:e.direction??this.params.ports.direction}),this}updatePort(e,t){if(this.graphStore.getPort(e)===void 0)throw new P("failed to update nonexistent port");return this.graphStore.updatePort(e,t??{}),this}unmarkPort(e){if(this.graphStore.getPort(e)===void 0)throw new P("failed to unmark nonexistent port");return this.graphStore.removePort(e),this}addEdge(e){const t=this.edgeIdGenerator.create(e.id);if(this.graphStore.getEdge(t)!==void 0)throw new P("failed to add edge with existing id");if(this.graphStore.getPort(e.from)===void 0)throw new P("failed to add edge from nonexistent port");if(this.graphStore.getPort(e.to)===void 0)throw new P("failed to add edge to nonexistent port");return this.graphStore.addEdge({id:t,from:e.from,to:e.to,shape:e.shape??this.params.edges.shapeFactory(t),priority:e.priority??this.params.edges.priorityFn()}),this}updateEdge(e,t){if(this.graphStore.getEdge(e)===void 0)throw new P("failed to update nonexistent edge");return this.graphStore.updateEdge(e,t??{}),this}removeEdge(e){if(this.graphStore.getEdge(e)===void 0)throw new P("failed to remove nonexistent edge");return this.graphStore.removeEdge(e),this}clear(){return this.graphStore.clear(),this}patchViewportMatrix(e){return this.viewportStore.patchViewportMatrix(e),this}patchContentMatrix(e){return this.viewportStore.patchContentMatrix(e),this}destroy(){this.destroyed||(this.clear(),this.onBeforeDestroyEmitter.emit(),this.graphStore.onAfterNodeAdded.unsubscribe(this.onAfterNodeAdded),this.graphStore.onAfterNodeUpdated.unsubscribe(this.onAfterNodeUpdated),this.graphStore.onAfterNodePriorityUpdated.unsubscribe(this.onAfterNodePriorityUpdated),this.graphStore.onBeforeNodeRemoved.unsubscribe(this.onBeforeNodeRemoved),this.graphStore.onAfterPortUpdated.unsubscribe(this.onAfterPortUpdated),this.graphStore.onBeforePortRemoved.unsubscribe(this.onBeforePortUnmarked),this.graphStore.onAfterEdgeAdded.unsubscribe(this.onAfterEdgeAdded),this.graphStore.onAfterEdgeShapeUpdated.unsubscribe(this.onAfterEdgeShapeUpdated),this.graphStore.onAfterEdgeUpdated.unsubscribe(this.onAfterEdgeUpdated),this.graphStore.onAfterEdgePriorityUpdated.unsubscribe(this.onAfterEdgePriorityUpdated),this.graphStore.onBeforeEdgeRemoved.unsubscribe(this.onBeforeEdgeRemoved),this.graphStore.onBeforeClear.unsubscribe(this.onBeforeClear),this.htmlView.destroy(),this.destroyed=!0)}}class Ye{constructor(){s(this,"singleToMultiMap",new Map);s(this,"multiToSingleMap",new Map)}addRecord(e,t){const o=this.singleToMultiMap.get(e);o===void 0?this.singleToMultiMap.set(e,new Set([t])):o.add(t),this.multiToSingleMap.set(t,e)}getMultiBySingle(e){const t=this.singleToMultiMap.get(e)??new Set;return Array.from(t.values())}removeByMulti(e){const t=this.multiToSingleMap.get(e),o=this.singleToMultiMap.get(t);o.delete(e),o.size===0&&this.singleToMultiMap.delete(t),this.multiToSingleMap.delete(e)}getByMulti(e){return this.multiToSingleMap.get(e)}removeBySingle(e){this.singleToMultiMap.get(e).forEach(o=>{this.multiToSingleMap.delete(o)}),this.singleToMultiMap.delete(e)}clear(){this.singleToMultiMap.clear(),this.multiToSingleMap.clear()}forEachSingle(e){this.singleToMultiMap.forEach((t,o)=>{e(o)})}hasSingle(e){return this.singleToMultiMap.get(e)!==void 0}hasMulti(e){return this.multiToSingleMap.get(e)!==void 0}}class ce{constructor(){s(this,"nodes",new Map);s(this,"ports",new Map);s(this,"edges",new Map);s(this,"nodesElementsMap",new Map);s(this,"portIncomingEdges",new Map);s(this,"portOutcomingEdges",new Map);s(this,"portCycleEdges",new Map);s(this,"elementPorts",new Ye);s(this,"afterNodeAddedEmitter");s(this,"onAfterNodeAdded");s(this,"afterNodeUpdatedEmitter");s(this,"onAfterNodeUpdated");s(this,"afterNodePriorityUpdatedEmitter");s(this,"onAfterNodePriorityUpdated");s(this,"beforeNodeRemovedEmitter");s(this,"onBeforeNodeRemoved");s(this,"afterPortAddedEmitter");s(this,"onAfterPortAdded");s(this,"afterPortUpdatedEmitter");s(this,"onAfterPortUpdated");s(this,"beforePortRemovedEmitter");s(this,"onBeforePortRemoved");s(this,"afterEdgeAddedEmitter");s(this,"onAfterEdgeAdded");s(this,"afterEdgeShapeUpdatedEmitter");s(this,"onAfterEdgeShapeUpdated");s(this,"afterEdgeUpdatedEmitter");s(this,"onAfterEdgeUpdated");s(this,"afterEdgePriorityUpdatedEmitter");s(this,"onAfterEdgePriorityUpdated");s(this,"beforeEdgeRemovedEmitter");s(this,"onBeforeEdgeRemoved");s(this,"beforeClearEmitter");s(this,"onBeforeClear");[this.afterNodeAddedEmitter,this.onAfterNodeAdded]=x(),[this.afterNodeUpdatedEmitter,this.onAfterNodeUpdated]=x(),[this.afterNodePriorityUpdatedEmitter,this.onAfterNodePriorityUpdated]=x(),[this.beforeNodeRemovedEmitter,this.onBeforeNodeRemoved]=x(),[this.afterPortAddedEmitter,this.onAfterPortAdded]=x(),[this.afterPortUpdatedEmitter,this.onAfterPortUpdated]=x(),[this.beforePortRemovedEmitter,this.onBeforePortRemoved]=x(),[this.afterEdgeAddedEmitter,this.onAfterEdgeAdded]=x(),[this.afterEdgeShapeUpdatedEmitter,this.onAfterEdgeShapeUpdated]=x(),[this.afterEdgeUpdatedEmitter,this.onAfterEdgeUpdated]=x(),[this.afterEdgePriorityUpdatedEmitter,this.onAfterEdgePriorityUpdated]=x(),[this.beforeEdgeRemovedEmitter,this.onBeforeEdgeRemoved]=x(),[this.beforeClearEmitter,this.onBeforeClear]=x()}addNode(e){const t=new Map,o={element:e.element,payload:{x:e.x,y:e.y,centerFn:e.centerFn,priority:e.priority},ports:t};this.nodes.set(e.id,o),this.nodesElementsMap.set(e.element,e.id),this.afterNodeAddedEmitter.emit(e.id)}getAllNodeIds(){return Array.from(this.nodes.keys())}getNode(e){return this.nodes.get(e)}getElementNodeId(e){return this.nodesElementsMap.get(e)}updateNode(e,t){const o=this.nodes.get(e).payload;o.x=t.x??o.x,o.y=t.y??o.y,o.centerFn=t.centerFn??o.centerFn,t.priority!==void 0&&(o.priority=t.priority,this.afterNodePriorityUpdatedEmitter.emit(e)),this.afterNodeUpdatedEmitter.emit(e)}removeNode(e){this.beforeNodeRemovedEmitter.emit(e);const t=this.nodes.get(e);this.nodesElementsMap.delete(t.element),this.nodes.delete(e)}addPort(e){this.ports.set(e.id,{element:e.element,payload:{direction:e.direction},nodeId:e.nodeId}),this.elementPorts.addRecord(e.element,e.id),this.portCycleEdges.set(e.id,new Set),this.portIncomingEdges.set(e.id,new Set),this.portOutcomingEdges.set(e.id,new Set),this.nodes.get(e.nodeId).ports.set(e.id,e.element),this.afterPortAddedEmitter.emit(e.id)}getPort(e){return this.ports.get(e)}updatePort(e,t){const o=this.ports.get(e).payload;o.direction=t.direction??o.direction,this.afterPortUpdatedEmitter.emit(e)}getAllPortIds(){return Array.from(this.ports.keys())}getElementPortIds(e){return this.elementPorts.getMultiBySingle(e)}getNodePortIds(e){const t=this.nodes.get(e);if(t!==void 0)return Array.from(t.ports.keys())}removePort(e){const t=this.ports.get(e).nodeId;this.beforePortRemovedEmitter.emit(e),this.nodes.get(t).ports.delete(e),this.ports.delete(e),this.elementPorts.removeByMulti(e)}addEdge(e){this.addEdgeInternal(e),this.afterEdgeAddedEmitter.emit(e.id)}updateEdge(e,t){if(t.from!==void 0||t.to!==void 0){const i=this.edges.get(e),n=i.payload;this.removeEdgeInternal(e),this.addEdgeInternal({id:e,from:t.from??i.from,to:t.to??i.to,shape:n.shape,priority:n.priority})}const o=this.edges.get(e);t.shape!==void 0&&(o.payload.shape=t.shape,this.afterEdgeShapeUpdatedEmitter.emit(e)),t.priority!==void 0&&(o.payload.priority=t.priority,this.afterEdgePriorityUpdatedEmitter.emit(e)),this.afterEdgeUpdatedEmitter.emit(e)}getAllEdgeIds(){return Array.from(this.edges.keys())}getEdge(e){return this.edges.get(e)}removeEdge(e){this.beforeEdgeRemovedEmitter.emit(e),this.removeEdgeInternal(e)}clear(){this.beforeClearEmitter.emit(),this.portIncomingEdges.clear(),this.portOutcomingEdges.clear(),this.portCycleEdges.clear(),this.elementPorts.clear(),this.nodesElementsMap.clear(),this.edges.clear(),this.ports.clear(),this.nodes.clear()}getPortIncomingEdgeIds(e){return Array.from(this.portIncomingEdges.get(e))}getPortOutgoingEdgeIds(e){return Array.from(this.portOutcomingEdges.get(e))}getPortCycleEdgeIds(e){return Array.from(this.portCycleEdges.get(e))}getPortAdjacentEdgeIds(e){return[...this.getPortIncomingEdgeIds(e),...this.getPortOutgoingEdgeIds(e),...this.getPortCycleEdgeIds(e)]}getNodeIncomingEdgeIds(e){const t=Array.from(this.nodes.get(e).ports.keys()),o=[];return t.forEach(i=>{this.getPortIncomingEdgeIds(i).filter(n=>{const a=this.getEdge(n);return this.getPort(a.from).nodeId!==e}).forEach(n=>{o.push(n)})}),o}getNodeOutgoingEdgeIds(e){const t=Array.from(this.nodes.get(e).ports.keys()),o=[];return t.forEach(i=>{this.getPortOutgoingEdgeIds(i).filter(n=>{const a=this.getEdge(n);return this.getPort(a.to).nodeId!==e}).forEach(n=>{o.push(n)})}),o}getNodeCycleEdgeIds(e){const t=Array.from(this.nodes.get(e).ports.keys()),o=[];return t.forEach(i=>{this.getPortCycleEdgeIds(i).forEach(n=>{o.push(n)}),this.getPortIncomingEdgeIds(i).filter(n=>{const a=this.getEdge(n);return this.getPort(a.to).nodeId===e}).forEach(n=>{o.push(n)})}),o}getNodeAdjacentEdgeIds(e){const t=Array.from(this.nodes.get(e).ports.keys()),o=[];return t.forEach(i=>{this.getPortIncomingEdgeIds(i).forEach(n=>{o.push(n)}),this.getPortOutgoingEdgeIds(i).forEach(n=>{o.push(n)}),this.getPortCycleEdgeIds(i).forEach(n=>{o.push(n)})}),o}addEdgeInternal(e){this.edges.set(e.id,{from:e.from,to:e.to,payload:{shape:e.shape,priority:e.priority}}),e.from!==e.to?(this.portOutcomingEdges.get(e.from).add(e.id),this.portIncomingEdges.get(e.to).add(e.id)):this.portCycleEdges.get(e.from).add(e.id)}removeEdgeInternal(e){const t=this.edges.get(e),o=t.from,i=t.to;this.portCycleEdges.get(o).delete(e),this.portCycleEdges.get(i).delete(e),this.portIncomingEdges.get(o).delete(e),this.portIncomingEdges.get(i).delete(e),this.portOutcomingEdges.get(o).delete(e),this.portOutcomingEdges.get(i).delete(e),this.edges.delete(e)}}const ge=r=>({scale:1/r.scale,x:-r.x/r.scale,y:-r.y/r.scale}),le={scale:1,x:0,y:0};class je{constructor(){s(this,"viewportMatrix",le);s(this,"contentMatrix",le);s(this,"afterUpdateEmitter");s(this,"onAfterUpdated");[this.afterUpdateEmitter,this.onAfterUpdated]=x()}getViewportMatrix(){return this.viewportMatrix}getContentMatrix(){return this.contentMatrix}patchViewportMatrix(e){this.viewportMatrix={scale:e.scale??this.viewportMatrix.scale,x:e.x??this.viewportMatrix.x,y:e.y??this.viewportMatrix.y},this.contentMatrix=ge(this.viewportMatrix),this.afterUpdateEmitter.emit()}patchContentMatrix(e){this.contentMatrix={scale:e.scale??this.contentMatrix.scale,x:e.x??this.contentMatrix.x,y:e.y??this.contentMatrix.y},this.viewportMatrix=ge(this.contentMatrix),this.afterUpdateEmitter.emit()}}class j{constructor(e){s(this,"elementToNodeId",new Map);s(this,"nodesResizeObserver");s(this,"onAfterNodeAdded",e=>{const t=this.canvas.graph.getNode(e);this.elementToNodeId.set(t.element,e),this.nodesResizeObserver.observe(t.element)});s(this,"onBeforeNodeRemoved",e=>{const t=this.canvas.graph.getNode(e);this.elementToNodeId.delete(t.element),this.nodesResizeObserver.unobserve(t.element)});s(this,"onBeforeClear",()=>{this.nodesResizeObserver.disconnect(),this.elementToNodeId.clear()});this.canvas=e,this.nodesResizeObserver=new ResizeObserver(t=>{t.forEach(o=>{const i=o.target;this.handleNodeResize(i)})}),this.canvas.graph.onAfterNodeAdded.subscribe(this.onAfterNodeAdded),this.canvas.graph.onBeforeNodeRemoved.subscribe(this.onBeforeNodeRemoved),this.canvas.graph.onBeforeClear.subscribe(this.onBeforeClear)}static configure(e){new j(e)}handleNodeResize(e){const t=this.elementToNodeId.get(e);this.canvas.updateNode(t)}}const Ge=(r,e,t)=>{const{x:o,y:i,width:n,height:a}=r.getBoundingClientRect();return e>=o&&e<=o+n&&t>=i&&t<=i+a},Ke=(r,e,t)=>e>=0&&e<=r.innerWidth&&t>=0&&t<=r.innerHeight,C=(r,e,t,o)=>Ge(e,t,o)&&Ke(r,t,o),B=(r,e)=>{e!==null?r.style.cursor=e:r.style.removeProperty("cursor")},O=r=>{const e=document.createElement("div");return{id:r.overlayId,element:e,x:r.portCoords.x,y:r.portCoords.y,ports:[{id:r.overlayId,element:e,direction:r.portDirection}]}},Ze=(r,e)=>{let t=e,o=null;for(;t!==null&&(o=r.getElementPortIds(t)[0]??null,o===null);)t=t.parentElement;return o},ue=(r,e)=>{const t=document.elementsFromPoint(e.x,e.y);for(const o of t){const i=Ze(r,o);if(i!==null)return i}return null};var N=(r=>(r.Static="static",r.Dragging="dragging",r.Edge="edge",r))(N||{});const pe=(r,e)=>({x:r/2,y:e/2}),u={x:0,y:0},m=(r,e,t)=>({x:e.x*r.x-e.y*r.y+((1-e.x)*t.x+e.y*t.y),y:e.y*r.x+e.x*r.y+((1-e.x)*t.y-e.y*t.x)}),we=(r,e)=>{const t={x:r.x+r.width/2,y:r.y+r.height/2},o={x:e.x+e.width/2,y:e.y+e.height/2},i=Math.min(t.x,o.x),n=Math.min(t.y,o.y),a=Math.abs(o.x-t.x),h=Math.abs(o.y-t.y),d=t.x<=o.x?1:-1,c=t.y<=o.y?1:-1;return{x:i,y:n,width:a,height:h,flipX:d,flipY:c}},F=(r,e,t,o)=>({x:e*r.x+(1-e)/2*o.x,y:t*r.y+(1-t)/2*o.y});class Je{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.to;this.midpoint={x:t.x/2,y:t.y/2};const o=m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u),i=m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to),n={x:o.x+this.params.sourceDirection.x*this.params.curvature,y:o.y+this.params.sourceDirection.y*this.params.curvature},a={x:i.x-this.params.targetDirection.x*this.params.curvature,y:i.y-this.params.targetDirection.y*this.params.curvature},h=`M ${o.x} ${o.y} C ${n.x} ${n.y}, ${a.x} ${a.y}, ${i.x} ${i.y}`,d=this.params.hasSourceArrow?"":`M ${u.x} ${u.y} L ${o.x} ${o.y} `,c=this.params.hasTargetArrow?"":` M ${i.x} ${i.y} L ${this.params.to.x} ${this.params.to.y}`;this.path=`${d}${h}${c}`}}class Qe{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.hasSourceArrow?m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u):u,o=this.params.hasTargetArrow?m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to):this.params.to,i=this.params.arrowLength,n=Math.cos(this.params.detourDirection)*this.params.detourDistance,a=Math.sin(this.params.detourDirection)*this.params.detourDistance,h=n*this.params.flipX,d=a*this.params.flipY,c=m({x:i,y:u.y},this.params.sourceDirection,u),l={x:c.x+h,y:c.y+d},g=m({x:this.params.to.x-i,y:this.params.to.y},this.params.targetDirection,this.params.to),p={x:g.x+h,y:g.y+d},f={x:(l.x+p.x)/2,y:(l.y+p.y)/2},y={x:c.x+this.params.curvature*this.params.sourceDirection.x,y:c.y+this.params.curvature*this.params.sourceDirection.y},A={x:g.x-this.params.curvature*this.params.targetDirection.x,y:g.y-this.params.curvature*this.params.targetDirection.y},b={x:c.x+h,y:c.y+d},S={x:g.x+h,y:g.y+d};this.path=[`M ${t.x} ${t.y}`,`L ${c.x} ${c.y}`,`C ${y.x} ${y.y} ${b.x} ${b.y} ${f.x} ${f.y}`,`C ${S.x} ${S.y} ${A.x} ${A.y} ${g.x} ${g.y}`,`L ${o.x} ${o.y}`].join(" "),this.midpoint=F(f,e.flipX,e.flipY,e.to)}}const G=Object.freeze({edgeColor:"--edge-color"}),fe=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","svg");return e.style.pointerEvents="none",e.style.position="absolute",e.style.top="0",e.style.left="0",e.style.overflow="visible",e.style.setProperty(G.edgeColor,r),e},ye=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","path");return e.setAttribute("stroke",`var(${G.edgeColor})`),e.setAttribute("stroke-width",`${r}`),e.setAttribute("fill","none"),e},W=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","path");return r.setAttribute("fill",`var(${G.edgeColor})`),r},me=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","g");return r.style.transformOrigin="50% 50%",r},ve=(r,e)=>{r.style.transform=`translate(${e.x}px, ${e.y}px)`,r.style.width=`${Math.max(e.width,1)}px`,r.style.height=`${Math.max(e.height,1)}px`},M=(r,e)=>{const t=[];if(r.length>0&&t.push(`M ${r[0].x} ${r[0].y}`),r.length===2&&t.push(`L ${r[1].x} ${r[1].y}`),r.length>2){const o=r.length-1;let i=0,n=0,a=0;r.forEach((h,d)=>{let c=0,l=0,g=0;const p=d>0,f=d<o,y=p&&f;if(p&&(c=-i,l=-n,g=a),f){const U=r[d+1];i=U.x-h.x,n=U.y-h.y,a=Math.sqrt(i*i+n*n)}const b=a!==0?Math.min((y?e:0)/a,d<o-1?.5:1):0,S=y?{x:h.x+i*b,y:h.y+n*b}:h,D=g!==0?Math.min((y?e:0)/g,d>1?.5:1):0,L=y?{x:h.x+c*D,y:h.y+l*D}:h;d>0&&t.push(`L ${L.x} ${L.y}`),y&&t.push(`C ${h.x} ${h.y} ${h.x} ${h.y} ${S.x} ${S.y}`)})}return t.join(" ")};class _e{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.to;this.midpoint={x:t.x/2,y:t.y/2};const o=this.params.hasSourceArrow?m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u):u,i=this.params.hasTargetArrow?m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to):this.params.to,n=this.params.arrowLength+this.params.arrowOffset,a=n-this.params.roundness,h=m({x:a,y:u.y},this.params.sourceDirection,u),d=m({x:this.params.to.x-a,y:this.params.to.y},this.params.targetDirection,this.params.to),c=Math.max((h.x+d.x)/2,n),l=this.params.to.y/2,g={x:this.params.flipX>0?c:-n,y:h.y},p={x:g.x,y:l},f={x:this.params.flipX>0?this.params.to.x-c:this.params.to.x+n,y:d.y},y={x:f.x,y:l};this.path=M([o,h,g,p,y,f,d,i],this.params.roundness)}}class qe{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.hasSourceArrow?m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u):u,o=this.params.hasTargetArrow?m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to):this.params.to,i=this.params.arrowLength+this.params.arrowOffset,n=m({x:i,y:u.y},this.params.sourceDirection,u),a=Math.cos(this.params.detourDirection)*this.params.detourDistance,h=Math.sin(this.params.detourDirection)*this.params.detourDistance,d=a*this.params.flipX,c=h*this.params.flipY,l={x:n.x+d,y:n.y+c},g=m({x:this.params.to.x-i,y:this.params.to.y},this.params.targetDirection,this.params.to),p={x:g.x+d,y:g.y+c},f={x:(l.x+p.x)/2,y:(l.y+p.y)/2};this.midpoint=F(f,e.flipX,e.flipY,e.to),this.path=M([t,n,l,p,g,o],this.params.roundness)}}class et{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.to;this.midpoint={x:t.x/2,y:t.y/2};const o=this.params.hasSourceArrow?m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u):u,i=this.params.hasTargetArrow?m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to):this.params.to,n=this.params.arrowLength+this.params.arrowOffset,a=m({x:n,y:u.y},this.params.sourceDirection,u),h=m({x:this.params.to.x-n,y:this.params.to.y},this.params.targetDirection,this.params.to);this.path=M([o,a,h,i],this.params.roundness)}}class tt{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.to;this.midpoint={x:t.x/2,y:t.y/2};const o=this.params.hasSourceArrow?m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u):u,i=this.params.hasTargetArrow?m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to):this.params.to,n=this.params.arrowLength+this.params.arrowOffset,a=n-this.params.roundness,h=m({x:a,y:u.y},this.params.sourceDirection,u),d=m({x:this.params.to.x-a,y:this.params.to.y},this.params.targetDirection,this.params.to),c=Math.max((h.y+d.y)/2,n),l=this.params.to.x/2,g={x:h.x,y:this.params.flipY>0?c:-n},p={x:l,y:g.y},f={x:d.x,y:this.params.flipY>0?this.params.to.y-c:this.params.to.y+n},y={x:l,y:f.y};this.path=M([o,h,g,p,y,f,d,i],this.params.roundness)}}class K{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.arrowOffset,o=this.params.side,i=this.params.arrowLength+t,n=i+2*o,h=[{x:this.params.arrowLength,y:u.y},{x:i,y:u.y},{x:i,y:this.params.side},{x:n,y:this.params.side},{x:n,y:-this.params.side},{x:i,y:-this.params.side},{x:i,y:u.y},{x:this.params.arrowLength,y:u.y}].map(c=>m(c,this.params.sourceDirection,u)),d=`M ${u.x} ${u.y} L ${h[0].x} ${h[0].y} `;this.path=`${this.params.hasSourceArrow||this.params.hasTargetArrow?"":d}${M(h,this.params.roundness)}`,this.midpoint={x:(h[3].x+h[4].x)/2,y:(h[3].y+h[4].y)/2}}}class rt{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.smallRadius,o=this.params.radius,i=t+o,n=t*o/i,a=Math.sqrt(i*i-t*t),h=a*t/i,d=a+o+this.params.arrowLength,c=this.params.arrowLength+h,g=[{x:this.params.arrowLength,y:u.y},{x:c,y:n},{x:c,y:-n},{x:d,y:0}].map(y=>m(y,this.params.sourceDirection,u)),p=[`M ${g[0].x} ${g[0].y}`,`A ${t} ${t} 0 0 1 ${g[1].x} ${g[1].y}`,`A ${o} ${o} 0 1 0 ${g[2].x} ${g[2].y}`,`A ${t} ${t} 0 0 1 ${g[0].x} ${g[0].y}`].join(" "),f=`M 0 0 L ${g[0].x} ${g[0].y} `;this.path=`${this.params.hasSourceArrow||this.params.hasTargetArrow?"":f}${p}`,this.midpoint=g[3]}}class ot{constructor(e){s(this,"path");s(this,"midpoint");s(this,"diagonalDistance");this.params=e;const t=this.params.to;if(this.midpoint={x:t.x/2,y:t.y/2},this.diagonalDistance=Math.sqrt(this.params.to.x*this.params.to.x+this.params.to.y*this.params.to.y),Math.sqrt(this.params.to.x*this.params.to.x+this.params.to.y*this.params.to.y)===0){this.path="";return}const i=this.createDirectLinePoint({offset:this.params.sourceOffset,hasArrow:this.params.hasSourceArrow,flip:1,shift:u}),n=this.createDirectLinePoint({offset:this.params.targetOffset,hasArrow:this.params.hasTargetArrow,flip:-1,shift:this.params.to});this.path=`M ${i.x} ${i.y} L ${n.x} ${n.y}`}createDirectLinePoint(e){const t=e.hasArrow?this.params.arrowLength:0,o=e.offset+t,i=e.flip*o/this.diagonalDistance;return{x:this.params.to.x*i+e.shift.x,y:this.params.to.y*i+e.shift.y}}}class st{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.hasSourceArrow?m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u):u,o=this.params.hasTargetArrow?m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to):this.params.to,i=this.params.arrowLength+this.params.arrowOffset,n=m({x:i,y:u.y},this.params.sourceDirection,u),a=m({x:this.params.to.x-i,y:this.params.to.y},this.params.targetDirection,this.params.to),h=this.params.detourDistance>0?1:-1,d=this.params.to.y/2,c=d+Math.abs(this.params.detourDistance),l=d+c*this.params.flipY*h,g={x:(n.x+a.x)/2,y:l};this.midpoint=F(g,e.flipX,e.flipY,e.to),this.path=M([t,n,{x:n.x,y:l},{x:a.x,y:l},a,o],this.params.roundness)}}class it{constructor(e){s(this,"path");s(this,"midpoint");this.params=e;const t=this.params.hasSourceArrow?m({x:this.params.arrowLength,y:u.y},this.params.sourceDirection,u):u,o=this.params.hasTargetArrow?m({x:this.params.to.x-this.params.arrowLength,y:this.params.to.y},this.params.targetDirection,this.params.to):this.params.to,i=this.params.arrowLength+this.params.arrowOffset,n=m({x:i,y:u.y},this.params.sourceDirection,u),a=m({x:this.params.to.x-i,y:this.params.to.y},this.params.targetDirection,this.params.to),h=this.params.detourDistance>0?1:-1,d=this.params.to.x/2,c=d+Math.abs(this.params.detourDistance),l=d+c*this.params.flipX*h,g={x:l,y:(n.y+a.y)/2};this.midpoint=F(g,e.flipX,e.flipY,e.to),this.path=M([t,n,{x:l,y:n.y},{x:l,y:a.y},a,o],this.params.roundness)}}const w=Object.freeze({color:"#777777",width:1,arrowLength:20,polygonArrowRadius:4,circleArrowRadius:8,wedgeArrowSmallRadius:20,wedgeArrowRadius:100,wedgeArrowAngle:Math.PI/12,arrowOffset:15,hasSourceArrow:!1,hasTargetArrow:!1,cycleRadius:30,cycleSquareSide:30,roundness:10,detourDistance:100,detourDirection:-Math.PI/2,detourDirectionVertical:0,smallCycleRadius:15,curvature:90,interactiveWidth:10,preOffset:0}),Ee=(r,e,t)=>({x:e*Math.cos(r),y:t*Math.sin(r)});class k{constructor(e){s(this,"svg");s(this,"group",me());s(this,"line");s(this,"sourceArrow",null);s(this,"targetArrow",null);s(this,"onAfterRender");s(this,"afterRenderEmitter");s(this,"arrowRenderer");this.params=e,[this.afterRenderEmitter,this.onAfterRender]=x(),this.arrowRenderer=this.params.arrowRenderer,this.svg=fe(e.color),this.svg.appendChild(this.group),this.line=ye(e.width),this.group.appendChild(this.line),e.hasSourceArrow&&(this.sourceArrow=W(),this.group.appendChild(this.sourceArrow)),e.hasTargetArrow&&(this.targetArrow=W(),this.group.appendChild(this.targetArrow))}render(e){const{x:t,y:o,width:i,height:n,flipX:a,flipY:h}=we(e.from,e.to);ve(this.svg,{x:t,y:o,width:i,height:n}),this.group.style.transform=`scale(${a}, ${h})`;const d=Ee(e.from.direction,a,h),c=Ee(e.to.direction,a,h),l={x:i,y:n};let g={x:-c.x,y:-c.y},p;e.category===T.PortCycle?(p=this.params.createCyclePath,g=d):e.category===T.NodeCycle?p=this.params.createDetourPath:p=this.params.createLinePath;const f=p(d,c,l,a,h);this.line.setAttribute("d",f.path);let y=null;this.sourceArrow&&(y=this.arrowRenderer({direction:d,shift:u,arrowLength:this.params.arrowLength}),this.sourceArrow.setAttribute("d",y));let A=null;this.targetArrow&&(A=this.arrowRenderer({direction:g,shift:l,arrowLength:this.params.arrowLength}),this.targetArrow.setAttribute("d",A)),this.afterRenderEmitter.emit({edgePath:f,sourceArrowPath:y,targetArrowPath:A})}}const nt=r=>e=>{const o=[u,{x:e.arrowLength,y:r.radius},{x:e.arrowLength,y:-r.radius}].map(h=>m(h,e.direction,u)).map(h=>({x:h.x+e.shift.x,y:h.y+e.shift.y})),i=`M ${o[0].x} ${o[0].y}`,n=`L ${o[1].x} ${o[1].y}`,a=`L ${o[2].x} ${o[2].y}`;return`${i} ${n} ${a} Z`},at=r=>e=>{const t=r.radius,o=e.arrowLength,i=(o*o+2*o*t)/(2*t),n=i+t,a=o+t-t*(o+t)/n,h=t*i/n,c=[u,{x:a,y:-h},{x:a,y:h}].map(y=>m(y,e.direction,u)).map(y=>({x:y.x+e.shift.x,y:y.y+e.shift.y})),l=`M ${c[0].x} ${c[0].y}`,g=`A ${i} ${i} 0 0 0 ${c[1].x} ${c[1].y}`,p=`A ${t} ${t} 0 0 0 ${c[2].x} ${c[2].y}`,f=`A ${i} ${i} 0 0 0 ${c[0].x} ${c[0].y}`;return`${l} ${g} ${p} ${f}`},ht=r=>e=>{const t=r.smallRadius,o=r.radius,i=m({x:e.arrowLength,y:0},{x:Math.cos(r.angle),y:Math.sin(r.angle)},{x:e.arrowLength+r.smallRadius,y:0}),a=[u,{x:i.x,y:-i.y},i].map(g=>m(g,e.direction,u)).map(g=>({x:g.x+e.shift.x,y:g.y+e.shift.y})),h=`M ${a[0].x} ${a[0].y}`,d=`A ${o} ${o} 0 0 1 ${a[1].x} ${a[1].y}`,c=`A ${t} ${t} 0 0 1 ${a[2].x} ${a[2].y}`,l=`A ${o} ${o} 0 0 1 ${a[0].x} ${a[0].y}`;return`${h} ${d} ${c} ${l}`},$=r=>{if(typeof r=="function")return r;switch(r.type){case"triangle":return nt({radius:r.radius??w.polygonArrowRadius});case"arc":return at({radius:r.radius??w.circleArrowRadius});default:return ht({smallRadius:r.smallRadius??w.wedgeArrowSmallRadius,angle:r.angle??w.wedgeArrowAngle,radius:r.radius??w.wedgeArrowRadius})}};class Ae{constructor(e){s(this,"svg");s(this,"group");s(this,"line");s(this,"sourceArrow");s(this,"targetArrow");s(this,"onAfterRender");s(this,"arrowLength");s(this,"curvature");s(this,"portCycleRadius");s(this,"portCycleSmallRadius");s(this,"detourDirection");s(this,"detourDistance");s(this,"hasSourceArrow");s(this,"hasTargetArrow");s(this,"pathShape");s(this,"createCyclePath",e=>new rt({sourceDirection:e,radius:this.portCycleRadius,smallRadius:this.portCycleSmallRadius,arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createDetourPath",(e,t,o,i,n)=>new Qe({to:o,sourceDirection:e,targetDirection:t,flipX:i,flipY:n,arrowLength:this.arrowLength,detourDirection:this.detourDirection,detourDistance:this.detourDistance,curvature:this.curvature,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createLinePath",(e,t,o)=>new Je({to:o,sourceDirection:e,targetDirection:t,arrowLength:this.arrowLength,curvature:this.curvature,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??w.arrowLength,this.curvature=(e==null?void 0:e.curvature)??w.curvature,this.portCycleRadius=(e==null?void 0:e.cycleRadius)??w.cycleRadius,this.portCycleSmallRadius=(e==null?void 0:e.smallCycleRadius)??w.smallCycleRadius,this.detourDirection=(e==null?void 0:e.detourDirection)??w.detourDirection,this.detourDistance=(e==null?void 0:e.detourDistance)??w.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??w.hasTargetArrow,this.pathShape=new k({color:(e==null?void 0:e.color)??w.color,width:(e==null?void 0:e.width)??w.width,arrowRenderer:$((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){this.pathShape.render(e)}}class xe{constructor(e){s(this,"svg");s(this,"group");s(this,"line");s(this,"sourceArrow");s(this,"targetArrow");s(this,"onAfterRender");s(this,"arrowLength");s(this,"arrowOffset");s(this,"roundness");s(this,"cycleSquareSide");s(this,"detourDistance");s(this,"hasSourceArrow");s(this,"hasTargetArrow");s(this,"pathShape");s(this,"createCyclePath",e=>new K({sourceDirection:e,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createDetourPath",(e,t,o,i,n)=>new st({to:o,sourceDirection:e,targetDirection:t,flipX:i,flipY:n,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createLinePath",(e,t,o,i)=>new _e({to:o,sourceDirection:e,targetDirection:t,flipX:i,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??w.arrowLength,this.arrowOffset=(e==null?void 0:e.arrowOffset)??w.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??w.cycleSquareSide;const t=(e==null?void 0:e.roundness)??w.roundness;this.roundness=Math.min(t,this.arrowOffset,this.cycleSquareSide/2),this.detourDistance=(e==null?void 0:e.detourDistance)??w.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??w.hasTargetArrow,this.pathShape=new k({color:(e==null?void 0:e.color)??w.color,width:(e==null?void 0:e.width)??w.width,arrowRenderer:$((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){this.pathShape.render(e)}}class Se{constructor(e){s(this,"svg");s(this,"group");s(this,"line");s(this,"sourceArrow");s(this,"targetArrow");s(this,"onAfterRender");s(this,"arrowLength");s(this,"arrowOffset");s(this,"roundness");s(this,"cycleSquareSide");s(this,"detourDirection");s(this,"detourDistance");s(this,"hasSourceArrow");s(this,"hasTargetArrow");s(this,"pathShape");s(this,"createCyclePath",e=>new K({sourceDirection:e,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createDetourPath",(e,t,o,i,n)=>new qe({to:o,sourceDirection:e,targetDirection:t,flipX:i,flipY:n,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDirection:this.detourDirection,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createLinePath",(e,t,o)=>new et({to:o,sourceDirection:e,targetDirection:t,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??w.arrowLength,this.arrowOffset=(e==null?void 0:e.arrowOffset)??w.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??w.cycleSquareSide;const t=(e==null?void 0:e.roundness)??w.roundness;this.roundness=Math.min(t,this.arrowOffset,this.cycleSquareSide/2),this.detourDirection=(e==null?void 0:e.detourDirection)??w.detourDirection,this.detourDistance=(e==null?void 0:e.detourDistance)??w.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??w.hasTargetArrow,this.pathShape=new k({color:(e==null?void 0:e.color)??w.color,width:(e==null?void 0:e.width)??w.width,arrowRenderer:$((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){this.pathShape.render(e)}}class Pe{constructor(e){s(this,"svg");s(this,"group");s(this,"line");s(this,"sourceArrow");s(this,"targetArrow");s(this,"onAfterRender");s(this,"arrowLength");s(this,"arrowOffset");s(this,"roundness");s(this,"cycleSquareSide");s(this,"detourDistance");s(this,"hasSourceArrow");s(this,"hasTargetArrow");s(this,"pathShape");s(this,"createCyclePath",e=>new K({sourceDirection:e,arrowLength:this.arrowLength,side:this.cycleSquareSide,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createDetourPath",(e,t,o,i,n)=>new it({to:o,sourceDirection:e,targetDirection:t,flipX:i,flipY:n,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,detourDistance:this.detourDistance,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));s(this,"createLinePath",(e,t,o,i,n)=>new tt({to:o,sourceDirection:e,targetDirection:t,flipY:n,arrowLength:this.arrowLength,arrowOffset:this.arrowOffset,roundness:this.roundness,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow}));this.arrowLength=(e==null?void 0:e.arrowLength)??w.arrowLength,this.arrowOffset=(e==null?void 0:e.arrowOffset)??w.arrowOffset,this.cycleSquareSide=(e==null?void 0:e.cycleSquareSide)??w.cycleSquareSide;const t=(e==null?void 0:e.roundness)??w.roundness;this.roundness=Math.min(t,this.arrowOffset,this.cycleSquareSide/2),this.detourDistance=(e==null?void 0:e.detourDistance)??w.detourDistance,this.hasSourceArrow=(e==null?void 0:e.hasSourceArrow)??w.hasSourceArrow,this.hasTargetArrow=(e==null?void 0:e.hasTargetArrow)??w.hasTargetArrow,this.pathShape=new k({color:(e==null?void 0:e.color)??w.color,width:(e==null?void 0:e.width)??w.width,arrowRenderer:$((e==null?void 0:e.arrowRenderer)??{}),arrowLength:this.arrowLength,hasSourceArrow:this.hasSourceArrow,hasTargetArrow:this.hasTargetArrow,createCyclePath:this.createCyclePath,createDetourPath:this.createDetourPath,createLinePath:this.createLinePath}),this.svg=this.pathShape.svg,this.group=this.pathShape.group,this.line=this.pathShape.line,this.sourceArrow=this.pathShape.sourceArrow,this.targetArrow=this.pathShape.targetArrow,this.onAfterRender=this.pathShape.onAfterRender}render(e){this.pathShape.render(e)}}class Z{constructor(e){s(this,"svg");s(this,"group",me());s(this,"line");s(this,"sourceArrow",null);s(this,"targetArrow",null);s(this,"color");s(this,"width");s(this,"arrowLength");s(this,"sourceOffset");s(this,"targetOffset");s(this,"onAfterRender");s(this,"afterRenderEmitter");s(this,"arrowRenderer");[this.afterRenderEmitter,this.onAfterRender]=x(),this.color=(e==null?void 0:e.color)??w.color,this.width=(e==null?void 0:e.width)??w.width,this.arrowLength=(e==null?void 0:e.arrowLength)??w.arrowLength,this.arrowRenderer=$((e==null?void 0:e.arrowRenderer)??{}),this.sourceOffset=(e==null?void 0:e.sourceOffset)??w.preOffset,this.targetOffset=(e==null?void 0:e.targetOffset)??w.preOffset,this.svg=fe(this.color),this.svg.appendChild(this.group),this.line=ye(this.width),this.group.appendChild(this.line),e!=null&&e.hasSourceArrow&&(this.sourceArrow=W(),this.group.appendChild(this.sourceArrow)),e!=null&&e.hasTargetArrow&&(this.targetArrow=W(),this.group.appendChild(this.targetArrow))}render(e){const{x:t,y:o,width:i,height:n,flipX:a,flipY:h}=we(e.from,e.to);ve(this.svg,{x:t,y:o,width:i,height:n}),this.group.style.transform=`scale(${a}, ${h})`;const d={x:i,y:n},c=new ot({to:d,sourceOffset:this.sourceOffset,targetOffset:this.targetOffset,hasSourceArrow:this.sourceArrow!==null,hasTargetArrow:this.targetArrow!==null,arrowLength:this.arrowLength});this.line.setAttribute("d",c.path);let l=null,g=null;const p=c.diagonalDistance;if(p===0)this.sourceArrow!==null&&(l="",this.sourceArrow.setAttribute("d",l)),this.targetArrow!==null&&(g="",this.targetArrow.setAttribute("d",g));else{const f={x:d.x/p,y:d.y/p};if(this.sourceArrow){const y={x:f.x*this.sourceOffset,y:f.y*this.sourceOffset};l=this.arrowRenderer({direction:f,shift:y,arrowLength:this.arrowLength}),this.sourceArrow.setAttribute("d",l)}if(this.targetArrow){const y={x:f.x*this.targetOffset,y:f.y*this.targetOffset};g=this.arrowRenderer({direction:{x:-f.x,y:-f.y},shift:{x:d.x-y.x,y:d.y-y.y},arrowLength:this.arrowLength}),this.targetArrow.setAttribute("d",g)}}this.afterRenderEmitter.emit({edgePath:c,sourceArrowPath:l,targetArrowPath:g})}}const dt=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","g");return r.style.pointerEvents="auto",r.style.cursor="pointer",r},ct=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","path");return e.setAttribute("stroke","transparent"),e.setAttribute("stroke-width",`${r}`),e.setAttribute("fill","none"),e.setAttribute("stroke-linecap","round"),e},be=r=>{const e=document.createElementNS("http://www.w3.org/2000/svg","path");return e.setAttribute("stroke-linejoin","round"),e.setAttribute("stroke-width",`${r}`),e.setAttribute("fill","transparent"),e.setAttribute("stroke","transparent"),e};class Te extends Error{constructor(e){super(e),this.name="InteractiveEdgeError"}}class J{constructor(e,t){s(this,"svg");s(this,"group");s(this,"line");s(this,"sourceArrow");s(this,"targetArrow");s(this,"handle",dt());s(this,"onAfterRender");s(this,"interactiveLine");s(this,"interactiveSourceArrow",null);s(this,"interactiveTargetArrow",null);if(this.baseEdge=e,e instanceof J)throw new Te("interactive edge can be configured only once");this.svg=this.baseEdge.svg,this.group=this.baseEdge.group,this.line=this.baseEdge.line,this.sourceArrow=this.baseEdge.sourceArrow,this.targetArrow=this.baseEdge.targetArrow,this.onAfterRender=this.baseEdge.onAfterRender;const o=(t==null?void 0:t.distance)??w.interactiveWidth;this.interactiveLine=ct(o),this.handle.appendChild(this.interactiveLine),this.sourceArrow&&(this.interactiveSourceArrow=be(o),this.handle.appendChild(this.interactiveSourceArrow)),this.targetArrow&&(this.interactiveTargetArrow=be(o),this.handle.appendChild(this.interactiveTargetArrow)),this.group.appendChild(this.handle),this.baseEdge.onAfterRender.subscribe(i=>{this.interactiveLine.setAttribute("d",i.edgePath.path),this.interactiveSourceArrow&&this.interactiveSourceArrow.setAttribute("d",i.sourceArrowPath),this.interactiveTargetArrow&&this.interactiveTargetArrow.setAttribute("d",i.targetArrowPath)})}render(e){this.baseEdge.render(e)}}class gt{constructor(e,t){s(this,"group");s(this,"line");s(this,"sourceArrow");s(this,"targetArrow");s(this,"onAfterRender");s(this,"svg");this.baseShape=e,this.midpointElement=t,this.svg=this.baseShape.svg,this.group=this.baseShape.group,this.line=this.baseShape.line,this.sourceArrow=this.baseShape.sourceArrow,this.targetArrow=this.baseShape.targetArrow,this.onAfterRender=this.baseShape.onAfterRender,this.svg.append(this.midpointElement),this.baseShape.onAfterRender.subscribe(o=>{const i=o.edgePath.midpoint,n=`translate(${i.x}px, ${i.y}px)`;this.midpointElement.style.setProperty("transform",n)})}render(e){this.baseShape.render(e)}}class Ne{constructor(e){s(this,"onAfterNodeAdded");s(this,"onAfterNodeUpdated");s(this,"onAfterNodePriorityUpdated");s(this,"onBeforeNodeRemoved");s(this,"onAfterPortMarked");s(this,"onAfterPortUpdated");s(this,"onBeforePortUnmarked");s(this,"onAfterEdgeAdded");s(this,"onAfterEdgeShapeUpdated");s(this,"onAfterEdgeUpdated");s(this,"onAfterEdgePriorityUpdated");s(this,"onBeforeEdgeRemoved");s(this,"onBeforeClear");this.graphStore=e,this.onAfterNodeAdded=this.graphStore.onAfterNodeAdded,this.onAfterNodeUpdated=this.graphStore.onAfterNodeUpdated,this.onAfterNodePriorityUpdated=this.graphStore.onAfterNodePriorityUpdated,this.onBeforeNodeRemoved=this.graphStore.onBeforeNodeRemoved,this.onAfterPortMarked=this.graphStore.onAfterPortAdded,this.onAfterPortUpdated=this.graphStore.onAfterPortUpdated,this.onBeforePortUnmarked=this.graphStore.onBeforePortRemoved,this.onAfterEdgeAdded=this.graphStore.onAfterEdgeAdded,this.onAfterEdgeShapeUpdated=this.graphStore.onAfterEdgeShapeUpdated,this.onAfterEdgeUpdated=this.graphStore.onAfterEdgeUpdated,this.onAfterEdgePriorityUpdated=this.graphStore.onAfterEdgePriorityUpdated,this.onBeforeEdgeRemoved=this.graphStore.onBeforeEdgeRemoved,this.onBeforeClear=this.graphStore.onBeforeClear}getNode(e){const t=this.graphStore.getNode(e);if(t===void 0)return null;const o=t.payload;return{element:t.element,x:o.x,y:o.y,centerFn:o.centerFn,priority:o.priority}}getElementNodeId(e){return this.graphStore.getElementNodeId(e)??null}getAllNodeIds(){return this.graphStore.getAllNodeIds()}getPort(e){const t=this.graphStore.getPort(e);return t===void 0?null:{element:t.element,direction:t.payload.direction,nodeId:t.nodeId}}getAllPortIds(){return this.graphStore.getAllPortIds()}getNodePortIds(e){return this.graphStore.getNodePortIds(e)??null}getElementPortIds(e){return[...this.graphStore.getElementPortIds(e)]}getAllEdgeIds(){return this.graphStore.getAllEdgeIds()}getEdge(e){const t=this.graphStore.getEdge(e);if(t===void 0)return null;const o=t.payload;return{from:t.from,to:t.to,priority:o.priority,shape:o.shape}}getPortIncomingEdgeIds(e){return this.graphStore.getPort(e)===void 0?null:this.graphStore.getPortIncomingEdgeIds(e)}getPortOutgoingEdgeIds(e){return this.graphStore.getPort(e)===void 0?null:this.graphStore.getPortOutgoingEdgeIds(e)}getPortCycleEdgeIds(e){return this.graphStore.getPort(e)===void 0?null:this.graphStore.getPortCycleEdgeIds(e)}getPortAdjacentEdgeIds(e){return this.graphStore.getPort(e)===void 0?null:this.graphStore.getPortAdjacentEdgeIds(e)}getNodeIncomingEdgeIds(e){return this.graphStore.getNode(e)===void 0?null:this.graphStore.getNodeIncomingEdgeIds(e)}getNodeOutgoingEdgeIds(e){return this.graphStore.getNode(e)===void 0?null:this.graphStore.getNodeOutgoingEdgeIds(e)}getNodeCycleEdgeIds(e){return this.graphStore.getNode(e)===void 0?null:this.graphStore.getNodeCycleEdgeIds(e)}getNodeAdjacentEdgeIds(e){return this.graphStore.getNode(e)===void 0?null:this.graphStore.getNodeAdjacentEdgeIds(e)}}class De{constructor(e){s(this,"onAfterUpdated");this.viewportStore=e,this.onAfterUpdated=this.viewportStore.onAfterUpdated}getViewportMatrix(){return{...this.viewportStore.getViewportMatrix()}}getContentMatrix(){return{...this.viewportStore.getContentMatrix()}}}const Me=(r,e)=>{const t=new ce,o=new Ne(t),i=new De(e),n=new he(t,e,r),a={nodes:{centerFn:pe,priorityFn:()=>0},edges:{shapeFactory:()=>new Z,priorityFn:()=>0},ports:{direction:0}};return new de(o,i,t,e,n,a)};class z{constructor(e,t,o,i){s(this,"onAfterPortMarked",e=>{const t=this.canvas.graph.getPort(e);this.canvas.graph.getElementPortIds(t.element).length===1&&this.hookPortEvents(t.element)});s(this,"onBeforePortUnmarked",e=>{const t=this.canvas.graph.getPort(e);this.canvas.graph.getElementPortIds(t.element).length===1&&this.unhookPortEvents(t.element)});s(this,"onPortMouseDown",e=>{if(!this.params.mouseDownEventVerifier(e))return;const t=e.currentTarget,o=this.canvas.graph.getElementPortIds(t)[0];this.params.onPortPointerDown(o,{x:e.clientX,y:e.clientY})&&(e.stopPropagation(),this.window.addEventListener("mousemove",this.onWindowMouseMove,{passive:!0}),this.window.addEventListener("mouseup",this.onWindowMouseUp,{passive:!0}))});s(this,"onWindowMouseMove",e=>{if(!C(this.window,this.element,e.clientX,e.clientY)){this.stopMouseDrag();return}this.params.onPointerMove({x:e.clientX,y:e.clientY})});s(this,"onWindowMouseUp",e=>{this.params.mouseUpEventVerifier(e)&&(this.params.onPointerUp({x:e.clientX,y:e.clientY}),this.stopMouseDrag())});s(this,"onPortTouchStart",e=>{if(e.touches.length!==1)return;const t=e.touches[0],o=e.currentTarget,i=this.canvas.graph.getElementPortIds(o)[0];this.params.onPortPointerDown(i,{x:t.clientX,y:t.clientY})&&(e.stopPropagation(),this.window.addEventListener("touchmove",this.onWindowTouchMove,{passive:!0}),this.window.addEventListener("touchend",this.onWindowTouchFinish,{passive:!0}),this.window.addEventListener("touchcancel",this.onWindowTouchFinish,{passive:!0}))});s(this,"onWindowTouchMove",e=>{const t=e.touches[0];if(!C(this.window,this.element,t.clientX,t.clientY)){this.stopTouchDrag();return}this.params.onPointerMove({x:t.clientX,y:t.clientY})});s(this,"onWindowTouchFinish",e=>{const t=e.changedTouches[0];this.params.onPointerUp({x:t.clientX,y:t.clientY}),this.stopTouchDrag()});s(this,"onBeforeClear",()=>{this.canvas.graph.getAllPortIds().forEach(e=>{const t=this.canvas.graph.getPort(e);this.unhookPortEvents(t.element)})});s(this,"onBeforeDestroy",()=>{this.params.onStopDrag(),this.removeWindowMouseListeners(),this.removeWindowTouchListeners()});this.canvas=e,this.element=t,this.window=o,this.params=i,this.canvas.graph.onAfterPortMarked.subscribe(this.onAfterPortMarked),this.canvas.graph.onBeforePortUnmarked.subscribe(this.onBeforePortUnmarked),this.canvas.graph.onBeforeClear.subscribe(this.onBeforeClear),this.canvas.onBeforeDestroy.subscribe(this.onBeforeDestroy)}static configure(e,t,o,i){new z(e,t,o,i)}hookPortEvents(e){e.addEventListener("mousedown",this.onPortMouseDown,{passive:!0}),e.addEventListener("touchstart",this.onPortTouchStart,{passive:!0})}unhookPortEvents(e){e.removeEventListener("mousedown",this.onPortMouseDown),e.removeEventListener("touchstart",this.onPortTouchStart)}stopMouseDrag(){this.params.onStopDrag(),this.removeWindowMouseListeners()}stopTouchDrag(){this.params.onStopDrag(),this.removeWindowTouchListeners()}removeWindowMouseListeners(){this.window.removeEventListener("mousemove",this.onWindowMouseMove),this.window.removeEventListener("mouseup",this.onWindowMouseUp)}removeWindowTouchListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}}class Q{constructor(e,t,o,i){s(this,"grabbedNode",null);s(this,"maxNodePriority",0);s(this,"graph");s(this,"onAfterNodeAdded",e=>{this.updateMaxNodePriority(e);const t=this.graph.getNode(e);t.element.addEventListener("mousedown",this.onMouseDown,{passive:!0}),t.element.addEventListener("touchstart",this.onTouchStart,{passive:!0})});s(this,"onAfterNodeUpdated",e=>{this.updateMaxNodePriority(e)});s(this,"onBeforeNodeRemoved",e=>{const t=this.graph.getNode(e);t.element.removeEventListener("mousedown",this.onMouseDown),t.element.removeEventListener("touchstart",this.onTouchStart)});s(this,"onBeforeDestroy",()=>{this.removeMouseDragListeners(),this.removeTouchDragListeners()});s(this,"onBeforeClear",()=>{this.canvas.graph.getAllNodeIds().forEach(e=>{const t=this.canvas.graph.getNode(e);t.element.removeEventListener("mousedown",this.onMouseDown),t.element.removeEventListener("touchstart",this.onTouchStart)}),this.maxNodePriority=0});s(this,"onMouseDown",e=>{if(!this.params.mouseDownEventVerifier(e))return;const t=e.currentTarget,o=this.graph.getElementNodeId(t),i=this.graph.getNode(o);if(!this.params.nodeDragVerifier(o))return;e.stopPropagation();const a=this.calculateContentPoint({x:e.clientX,y:e.clientY});this.grabbedNode={nodeId:o,dx:a.x-i.x,dy:a.y-i.y},B(this.element,this.params.dragCursor),this.moveNodeOnTop(o),this.window.addEventListener("mousemove",this.onWindowMouseMove,{passive:!0}),this.window.addEventListener("mouseup",this.onWindowMouseUp,{passive:!0})});s(this,"onTouchStart",e=>{if(e.touches.length!==1)return;e.stopPropagation();const t=e.touches[0],o=e.currentTarget,i=this.canvas.graph.getElementNodeId(o),n=this.graph.getNode(i);if(!this.params.nodeDragVerifier({nodeId:i,element:n.element,x:n.x,y:n.y}))return;const h=this.calculateContentPoint({x:t.clientX,y:t.clientY});this.grabbedNode={nodeId:i,dx:h.x-n.x,dy:h.y-n.y},this.moveNodeOnTop(i),this.window.addEventListener("touchmove",this.onWindowTouchMove,{passive:!0}),this.window.addEventListener("touchend",this.onWindowTouchFinish,{passive:!0}),this.window.addEventListener("touchcancel",this.onWindowTouchFinish,{passive:!0})});s(this,"onWindowMouseMove",e=>{if(!C(this.window,this.element,e.clientX,e.clientY)){this.cancelMouseDrag();return}this.grabbedNode!==null&&this.moveNode(this.grabbedNode,{x:e.clientX,y:e.clientY})});s(this,"onWindowMouseUp",e=>{this.params.mouseUpEventVerifier(e)&&this.cancelMouseDrag()});s(this,"onWindowTouchMove",e=>{if(e.touches.length!==1)return;const t=e.touches[0];if(!C(this.window,this.element,t.clientX,t.clientY)){this.cancelTouchDrag();return}this.grabbedNode!==null&&this.moveNode(this.grabbedNode,{x:t.clientX,y:t.clientY})});s(this,"onWindowTouchFinish",()=>{this.cancelTouchDrag()});this.canvas=e,this.element=t,this.window=o,this.params=i,this.graph=e.graph,this.graph.onAfterNodeAdded.subscribe(this.onAfterNodeAdded),this.graph.onAfterNodeUpdated.subscribe(this.onAfterNodeUpdated),this.graph.onBeforeNodeRemoved.subscribe(this.onBeforeNodeRemoved),this.graph.onBeforeClear.subscribe(this.onBeforeClear),this.canvas.onBeforeDestroy.subscribe(this.onBeforeDestroy)}static configure(e,t,o,i){new Q(e,t,o,i)}moveNode(e,t){if(this.graph.getNode(e.nodeId)===null)return;const i=this.calculateContentPoint(t),n={x:i.x-e.dx,y:i.y-e.dy},a=this.adjustNodeCoords(n);this.canvas.updateNode(e.nodeId,{x:a.x,y:a.y}),this.params.onNodeDrag(e.nodeId)}moveNodeOnTop(e){if(this.params.moveOnTop){if(this.maxNodePriority++,this.params.moveEdgesOnTop){const t=this.maxNodePriority;this.maxNodePriority++,this.graph.getNodeAdjacentEdgeIds(e).forEach(i=>{this.canvas.updateEdge(i,{priority:t})})}this.canvas.updateNode(e,{priority:this.maxNodePriority})}}cancelMouseDrag(){this.grabbedNode!==null&&this.graph.getNode(this.grabbedNode.nodeId)!==null&&this.params.onNodeDragFinished(this.grabbedNode.nodeId),this.grabbedNode=null,B(this.element,null),this.removeMouseDragListeners()}removeMouseDragListeners(){this.window.removeEventListener("mouseup",this.onWindowMouseUp),this.window.removeEventListener("mousemove",this.onWindowMouseMove)}cancelTouchDrag(){if(this.grabbedNode!==null){const e=this.graph.getNode(this.grabbedNode.nodeId);e!==null&&this.params.onNodeDragFinished({nodeId:this.grabbedNode.nodeId,element:e.element,x:e.x,y:e.y})}this.grabbedNode=null,this.removeTouchDragListeners()}removeTouchDragListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}updateMaxNodePriority(e){const t=this.graph.getNode(e).priority;this.maxNodePriority=Math.max(this.maxNodePriority,t)}calculateContentPoint(e){const t=this.element.getBoundingClientRect(),o={x:e.x-t.x,y:e.y-t.y},i=this.canvas.viewport.getViewportMatrix();return E(i,o)}adjustNodeCoords(e){const t=this.params.gridSize;if(t!==null){const o=t/2;return{x:Math.floor((e.x+o)/t)*t,y:Math.floor((e.y+o)/t)*t}}return e}}const lt=(r,e,t)=>({scale:r.scale,x:r.x+r.scale*e,y:r.y+r.scale*t}),ut=(r,e,t,o)=>({scale:r.scale*e,x:r.scale*(1-e)*t+r.x,y:r.scale*(1-e)*o+r.y}),X=r=>{const e=[],t=r.touches.length;for(let h=0;h<t;h++)e.push([r.touches[h].clientX,r.touches[h].clientY]);const o=e.reduce((h,d)=>[h[0]+d[0],h[1]+d[1]],[0,0]),i=[o[0]/t,o[1]/t],a=e.map(h=>[h[0]-i[0],h[1]-i[1]]).reduce((h,d)=>h+Math.sqrt(d[0]*d[0]+d[1]*d[1]),0);return{x:i[0],y:i[1],scale:a/t,touchesCnt:t,touches:e}};class H{constructor(e,t,o,i){s(this,"viewport");s(this,"prevTouches",null);s(this,"wheelFinishTimer",null);s(this,"transformInProgress",!1);s(this,"onBeforeDestroy",()=>{this.removeMouseDragListeners(),this.removeTouchDragListeners()});s(this,"onMouseDown",e=>{this.element===null||!this.params.mouseDownEventVerifier(e)||(B(this.element,this.params.shiftCursor),this.window.addEventListener("mousemove",this.onWindowMouseMove,{passive:!0}),this.window.addEventListener("mouseup",this.onWindowMouseUp,{passive:!0}),this.startRegisteredTransform())});s(this,"onWindowMouseMove",e=>{const t=C(this.window,this.element,e.clientX,e.clientY);if(this.element===null||!t){this.stopMouseDrag();return}const o=-e.movementX,i=-e.movementY;this.moveViewport(o,i)});s(this,"onWindowMouseUp",e=>{this.element===null||!this.params.mouseUpEventVerifier(e)||this.stopMouseDrag()});s(this,"onWheelScroll",e=>{if(!this.params.mouseWheelEventVerifier(e))return;const{left:t,top:o}=this.element.getBoundingClientRect(),i=e.clientX-t,n=e.clientY-o,h=1/(e.deltaY<0?this.params.wheelSensitivity:1/this.params.wheelSensitivity);this.wheelFinishTimer===null&&this.params.onTransformStarted(),this.scaleViewport(h,i,n),this.wheelFinishTimer!==null&&clearTimeout(this.wheelFinishTimer),this.wheelFinishTimer=setTimeout(()=>{this.transformInProgress||this.params.onTransformFinished(),this.wheelFinishTimer=null},this.params.scaleWheelFinishTimeout)});s(this,"onTouchStart",e=>{if(this.prevTouches!==null){this.prevTouches=X(e);return}this.prevTouches=X(e),this.window.addEventListener("touchmove",this.onWindowTouchMove,{passive:!0}),this.window.addEventListener("touchend",this.onWindowTouchFinish,{passive:!0}),this.window.addEventListener("touchcancel",this.onWindowTouchFinish,{passive:!0}),this.startRegisteredTransform()});s(this,"onWindowTouchMove",e=>{const t=X(e);if(!t.touches.every(i=>C(this.window,this.element,i[0],i[1]))){this.stopTouchDrag();return}if((t.touchesCnt===1||t.touchesCnt===2)&&this.moveViewport(-(t.x-this.prevTouches.x),-(t.y-this.prevTouches.y)),t.touchesCnt===2){const{left:i,top:n}=this.element.getBoundingClientRect(),a=this.prevTouches.x-i,h=this.prevTouches.y-n,c=1/(t.scale/this.prevTouches.scale);this.scaleViewport(c,a,h)}this.prevTouches=t});s(this,"onWindowTouchFinish",e=>{e.touches.length>0?this.prevTouches=X(e):this.stopTouchDrag()});s(this,"observer",new ResizeObserver(()=>{const e=this.viewport.getViewportMatrix(),{width:t,height:o}=this.element.getBoundingClientRect(),i=this.params.transformPreprocessor({prevTransform:e,nextTransform:e,canvasWidth:t,canvasHeight:o});this.params.onResizeTransformStarted(),this.canvas.patchViewportMatrix(i),this.params.onResizeTransformFinished()}));s(this,"preventWheelScaleListener",e=>{e.preventDefault()});this.canvas=e,this.element=t,this.window=o,this.params=i,this.element.addEventListener("wheel",this.preventWheelScaleListener,{passive:!1}),this.viewport=e.viewport,this.observer.observe(this.element),this.element.addEventListener("mousedown",this.onMouseDown,{passive:!0}),this.element.addEventListener("wheel",this.onWheelScroll,{passive:!0}),this.element.addEventListener("touchstart",this.onTouchStart,{passive:!0}),e.onBeforeDestroy.subscribe(this.onBeforeDestroy)}static configure(e,t,o,i){new H(e,t,o,i)}moveViewport(e,t){const o=this.viewport.getViewportMatrix(),i=lt(o,e,t),{width:n,height:a}=this.element.getBoundingClientRect(),h=this.params.transformPreprocessor({prevTransform:o,nextTransform:i,canvasWidth:n,canvasHeight:a});this.performTransform(h)}scaleViewport(e,t,o){const i=this.canvas.viewport.getViewportMatrix(),n=ut(i,e,t,o),{width:a,height:h}=this.element.getBoundingClientRect(),d=this.params.transformPreprocessor({prevTransform:i,nextTransform:n,canvasWidth:a,canvasHeight:h});this.performTransform(d)}stopMouseDrag(){B(this.element,null),this.removeMouseDragListeners(),this.finishRegisteredTransform()}removeMouseDragListeners(){this.window.removeEventListener("mousemove",this.onWindowMouseMove),this.window.removeEventListener("mouseup",this.onWindowMouseUp)}stopTouchDrag(){this.prevTouches=null,this.removeTouchDragListeners(),this.finishRegisteredTransform()}removeTouchDragListeners(){this.window.removeEventListener("touchmove",this.onWindowTouchMove),this.window.removeEventListener("touchend",this.onWindowTouchFinish),this.window.removeEventListener("touchcancel",this.onWindowTouchFinish)}performTransform(e){this.params.onBeforeTransformChange(),this.canvas.patchViewportMatrix(e),this.params.onTransformChange()}startRegisteredTransform(){this.transformInProgress=!0,this.params.onTransformStarted()}finishRegisteredTransform(){this.transformInProgress=!1,this.params.onTransformFinished()}}class _{constructor(e,t,o,i,n,a){s(this,"canvasResizeObserver");s(this,"nodeHorizontal");s(this,"nodeVertical");s(this,"viewport");s(this,"viewportWidth",0);s(this,"viewportHeight",0);s(this,"viewportMatrix");s(this,"loadedArea",{xFrom:1/0,xTo:1/0,yFrom:1/0,yTo:1/0});s(this,"updateLoadedArea",e=>{this.loadedArea={xFrom:e.x,xTo:e.x+e.width,yFrom:e.y,yTo:e.y+e.height}});s(this,"onAfterViewportUpdated",()=>{this.userTransformInProgress||(this.viewportMatrix=this.viewport.getViewportMatrix(),this.loadAreaAroundViewport())});s(this,"userTransformInProgress",!1);this.canvas=e,this.element=t,this.window=o,this.trigger=n,this.params=a,this.nodeHorizontal=this.params.nodeVerticalRadius,this.nodeVertical=this.params.nodeHorizontalRadius,this.canvasResizeObserver=new ResizeObserver(d=>{const c=d[0];this.viewportWidth=c.contentRect.width,this.viewportHeight=c.contentRect.height,this.scheduleLoadAreaAroundViewport()}),this.viewport=e.viewport;const h={...i,onResizeTransformStarted:()=>{this.userTransformInProgress=!0,i.onResizeTransformStarted()},onResizeTransformFinished:()=>{this.userTransformInProgress=!1,i.onResizeTransformFinished()},onBeforeTransformChange:()=>{this.userTransformInProgress=!0,i.onBeforeTransformChange()},onTransformChange:()=>{this.userTransformInProgress=!1;const d=this.viewportMatrix;this.viewportMatrix=this.viewport.getViewportMatrix(),d.scale!==this.viewportMatrix.scale&&this.scheduleEnsureViewportAreaLoaded(),i.onTransformChange()},onTransformFinished:()=>{this.scheduleLoadAreaAroundViewport(),i.onTransformFinished()}};H.configure(e,this.element,this.window,h),this.viewportMatrix=this.viewport.getViewportMatrix(),this.trigger.subscribe(this.updateLoadedArea),this.canvasResizeObserver.observe(this.element),this.canvas.viewport.onAfterUpdated.subscribe(this.onAfterViewportUpdated)}static configure(e,t,o,i,n,a){new _(e,t,o,i,n,a)}scheduleLoadAreaAroundViewport(){setTimeout(()=>{this.loadAreaAroundViewport()})}scheduleEnsureViewportAreaLoaded(){const e=this.viewportWidth*this.viewportMatrix.scale,t=this.viewportHeight*this.viewportMatrix.scale,o=this.viewportMatrix.x-this.nodeHorizontal,i=this.viewportMatrix.y-this.nodeVertical,n=this.viewportMatrix.x+e+this.nodeHorizontal,a=this.viewportMatrix.y+t+this.nodeVertical;this.loadedArea.xFrom<o&&this.loadedArea.xTo>n&&this.loadedArea.yFrom<i&&this.loadedArea.yTo>a||this.scheduleLoadAreaAroundViewport()}loadAreaAroundViewport(){const e=this.viewportWidth*this.viewportMatrix.scale,t=this.viewportHeight*this.viewportMatrix.scale,o=this.viewportMatrix.x-e-this.nodeHorizontal,i=this.viewportMatrix.y-t-this.nodeVertical,n=3*e+2*this.nodeHorizontal,a=3*t+2*this.nodeVertical;this.trigger.emit({x:o,y:i,width:n,height:a})}}const pt=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","svg");return r.style.position="absolute",r.style.inset="0",r},wt=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","rect");return r.setAttribute("fill","url(#pattern)"),r},ft=()=>{const r=document.createElementNS("http://www.w3.org/2000/svg","pattern");return r.setAttribute("id","pattern"),r};class q{constructor(e,t,o){s(this,"svg",pt());s(this,"patternRenderingRectangle",wt());s(this,"pattern",ft());s(this,"patternContent");s(this,"tileWidth");s(this,"tileHeight");s(this,"halfTileWidth");s(this,"halfTileHeight");s(this,"maxViewportScale");s(this,"visible",!1);s(this,"resizeObserver",new ResizeObserver(e=>{const t=e[0],{width:o,height:i}=t.contentRect;this.svg.setAttribute("width",`${o}`),this.svg.setAttribute("height",`${i}`),this.patternRenderingRectangle.setAttribute("width",`${o}`),this.patternRenderingRectangle.setAttribute("height",`${i}`);const n=this.tileWidth/o,a=this.tileHeight/i;this.pattern.setAttribute("width",`${n}`),this.pattern.setAttribute("height",`${a}`)}));s(this,"onAfterTransformUpdated",()=>{const e=this.canvas.viewport.getContentMatrix(),t=e.x-this.halfTileWidth*e.scale,o=e.y-this.halfTileHeight*e.scale,i=`matrix(${e.scale}, 0, 0, ${e.scale}, ${t}, ${o})`;this.pattern.setAttribute("patternTransform",i),this.updateVisibility()});this.canvas=e,this.backgroundHost=o,this.tileWidth=t.tileWidth,this.tileHeight=t.tileHeight,this.halfTileWidth=this.tileWidth/2,this.halfTileHeight=this.tileHeight/2,this.patternContent=t.renderer,this.maxViewportScale=t.maxViewportScale;const i=`translate(${this.halfTileWidth}, ${this.halfTileHeight})`;this.patternContent.setAttribute("transform",i),this.pattern.appendChild(this.patternContent);const n=document.createElementNS("http://www.w3.org/2000/svg","defs");n.appendChild(this.pattern),this.svg.appendChild(n),this.svg.appendChild(this.patternRenderingRectangle),this.resizeObserver.observe(this.backgroundHost),this.canvas.viewport.onAfterUpdated.subscribe(this.onAfterTransformUpdated),this.onAfterTransformUpdated()}static configure(e,t,o){new q(e,t,o)}updateVisibility(){const t=this.canvas.viewport.getViewportMatrix().scale>this.maxViewportScale;t&&this.visible?(this.visible=!1,this.backgroundHost.removeChild(this.svg)):!t&&!this.visible&&(this.visible=!0,this.backgroundHost.appendChild(this.svg))}}class ee{constructor(e,t,o,i,n){s(this,"overlayCanvas");s(this,"staticPortId",null);s(this,"isTargetDragging",!0);s(this,"onEdgeCreated",e=>{this.params.onAfterEdgeCreated(e)});this.canvas=e,this.overlayLayer=t,this.viewportStore=o,this.window=i,this.params=n,this.overlayCanvas=Me(this.overlayLayer,this.viewportStore),z.configure(this.canvas,this.overlayLayer,this.window,{mouseDownEventVerifier:this.params.mouseDownEventVerifier,mouseUpEventVerifier:this.params.mouseUpEventVerifier,onStopDrag:()=>{this.resetDragState()},onPortPointerDown:(a,h)=>{const d=this.params.connectionTypeResolver(a);return d===null?!1:(this.grabPort(a,h,d),!0)},onPointerMove:a=>{this.moveDraggingPort(a)},onPointerUp:a=>{this.tryCreateConnection(a)}})}static configure(e,t,o,i,n){new ee(e,t,o,i,n)}grabPort(e,t,o){const i=this.canvas.graph.getPort(e);this.staticPortId=e;const n=i.element.getBoundingClientRect(),a=n.x+n.width/2,h=n.y+n.height/2,d=this.overlayLayer.getBoundingClientRect(),c=this.canvas.viewport.getViewportMatrix(),l=E(c,{x:a-d.x,y:h-d.y}),g=E(c,{x:t.x-d.x,y:t.y-d.y}),p={overlayId:N.Static,portCoords:l,portDirection:i.direction},f={overlayId:N.Dragging,portCoords:g,portDirection:this.params.dragPortDirection};this.isTargetDragging=o==="direct";const[y,A]=this.isTargetDragging?[p,f]:[f,p];this.overlayCanvas.addNode(O(y)),this.overlayCanvas.addNode(O(A)),this.overlayCanvas.addEdge({from:y.overlayId,to:A.overlayId,shape:this.params.edgeShapeFactory(N.Edge)})}resetDragState(){this.staticPortId=null,this.isTargetDragging=!0,this.overlayCanvas.clear()}tryCreateConnection(e){const t=ue(this.canvas.graph,e),o=this.staticPortId;if(t===null){this.params.onEdgeCreationInterrupted({staticPortId:o,isDirect:this.isTargetDragging});return}const i=this.isTargetDragging?o:t,n=this.isTargetDragging?t:o,a={from:i,to:n},h=this.params.connectionPreprocessor(a);h!==null?(this.canvas.graph.onAfterEdgeAdded.subscribe(this.onEdgeCreated),this.canvas.addEdge(h),this.canvas.graph.onAfterEdgeAdded.unsubscribe(this.onEdgeCreated)):this.params.onEdgeCreationPrevented(a)}moveDraggingPort(e){const t=this.overlayLayer.getBoundingClientRect(),o={x:e.x-t.x,y:e.y-t.y},i=this.canvas.viewport.getViewportMatrix(),n=E(i,o);this.overlayCanvas.updateNode(N.Dragging,{x:n.x,y:n.y})}}class te{constructor(e,t,o,i,n){s(this,"overlayCanvas");s(this,"staticPortId",null);s(this,"isTargetDragging",!0);s(this,"draggingEdgePayload",null);s(this,"onEdgeReattached",e=>{this.params.onAfterEdgeReattached(e)});this.canvas=e,this.overlayLayer=t,this.viewportStore=o,this.window=i,this.params=n,this.overlayCanvas=Me(this.overlayLayer,this.viewportStore),z.configure(this.canvas,this.overlayLayer,this.window,{mouseDownEventVerifier:this.params.mouseDownEventVerifier,mouseUpEventVerifier:this.params.mouseUpEventVerifier,onStopDrag:()=>{this.resetDragState()},onPortPointerDown:(a,h)=>this.tryStartEdgeDragging(a,h),onPointerMove:a=>{this.moveDraggingPort(a)},onPointerUp:a=>{this.tryCreateConnection(a)}})}static configure(e,t,o,i,n){new te(e,t,o,i,n)}tryStartEdgeDragging(e,t){const o=this.params.draggingEdgeResolver(e);if(o===null)return!1;const i=this.canvas.graph.getEdge(o);if(i===null)return!1;const n=e===i.from,a=e===i.to,h=n?i.to:i.from;this.staticPortId=h,this.isTargetDragging=a;const d=this.canvas.graph.getPort(e),c=this.canvas.graph.getPort(h),l=c.element.getBoundingClientRect(),g={x:l.x+l.width/2,y:l.y+l.height/2},p=this.canvas.viewport.getViewportMatrix(),f=this.overlayLayer.getBoundingClientRect(),y=E(p,{x:g.x-f.x,y:g.y-f.y}),A=E(p,{x:t.x-f.x,y:t.y-f.y});this.draggingEdgePayload={id:o,from:i.from,to:i.to,shape:i.shape,priority:i.priority},this.canvas.removeEdge(o);const b={overlayId:N.Static,portCoords:y,portDirection:c.direction},S={overlayId:N.Dragging,portCoords:A,portDirection:d.direction},[V,D]=this.isTargetDragging?[b,S]:[S,b];this.overlayCanvas.addNode(O(V)),this.overlayCanvas.addNode(O(D));const L=this.params.draggingEdgeShapeFactory!==null?this.params.draggingEdgeShapeFactory(N.Edge):i.shape;return this.overlayCanvas.addEdge({id:N.Edge,from:V.overlayId,to:D.overlayId,shape:L}),!0}resetDragState(){this.draggingEdgePayload=null,this.staticPortId=null,this.isTargetDragging=!0,this.overlayCanvas.clear()}moveDraggingPort(e){const t=this.overlayLayer.getBoundingClientRect(),o={x:e.x-t.x,y:e.y-t.y},i=this.canvas.viewport.getViewportMatrix(),n=E(i,o);this.overlayCanvas.updateNode(N.Dragging,{x:n.x,y:n.y})}tryCreateConnection(e){const t=ue(this.canvas.graph,e);if(this.overlayCanvas.removeEdge(N.Edge),t===null){const d=this.draggingEdgePayload;this.params.onEdgeReattachInterrupted({id:d.id,from:d.from,to:d.to,shape:d.shape,priority:d.priority});return}const[o,i]=this.isTargetDragging?[this.staticPortId,t]:[t,this.staticPortId],n=this.draggingEdgePayload,a={id:n.id,from:o,to:i,shape:n.shape,priority:n.priority},h=this.params.connectionPreprocessor(a);if(h!==null)this.canvas.graph.onAfterEdgeAdded.subscribe(this.onEdgeReattached),this.canvas.addEdge(h),this.canvas.graph.onAfterEdgeAdded.unsubscribe(this.onEdgeReattached);else{const d=this.draggingEdgePayload;this.params.onEdgeReattachPrevented({id:d.id,from:d.from,to:d.to,shape:d.shape,priority:d.priority})}}}class re{constructor(e,t,o){this.canvas=e,this.layoutAlgorithm=t,this.trigger=o,this.trigger.subscribe(()=>{this.applyLayout()})}static configure(e,t,o){new re(e,t,o)}applyLayout(){this.layoutAlgorithm.calculateCoordinates(this.canvas.graph).forEach((t,o)=>{this.canvas.updateNode(o,t)})}}class oe{constructor(e,t){s(this,"applyScheduled",!1);this.canvas=e,this.layoutAlgorithm=t,this.canvas.graph.onAfterNodeAdded.subscribe(()=>{this.scheduleApply()}),this.canvas.graph.onBeforeNodeRemoved.subscribe(()=>{this.scheduleApply()}),this.canvas.graph.onAfterEdgeAdded.subscribe(()=>{this.scheduleApply()}),this.canvas.graph.onBeforeEdgeRemoved.subscribe(()=>{this.scheduleApply()})}static configure(e,t){new oe(e,t)}scheduleApply(){this.applyScheduled||(this.applyScheduled=!0,setTimeout(()=>{this.applyScheduled=!1,this.applyLayout()}))}applyLayout(){this.layoutAlgorithm.calculateCoordinates(this.canvas.graph).forEach((t,o)=>{this.canvas.updateNode(o,t)})}}class yt{static configure(e,t){const o=t.applyOn;switch(o.type){case"manual":{re.configure(e,t.algorithm,o.trigger);break}case"topologyChangeTimeout":{oe.configure(e,t.algorithm);break}}}}class mt{constructor(e,t){s(this,"previousTimeStamp");s(this,"step",e=>{if(this.previousTimeStamp===void 0)this.previousTimeStamp=e;else{const t=(e-this.previousTimeStamp)/1e3;this.previousTimeStamp=e,this.callback(t)}this.win.requestAnimationFrame(this.step)});this.win=e,this.callback=t,this.win.requestAnimationFrame(this.step)}}class se{constructor(e,t,o,i){s(this,"step",e=>{this.params.algorithm.calculateNextCoordinates(this.canvas.graph,e).forEach((o,i)=>{this.staticNodes.has(i)||this.canvas.updateNode(i,{x:o.x,y:o.y})})});this.canvas=e,this.params=t,this.staticNodes=o,this.win=i,new mt(this.win,this.step)}static configure(e,t,o,i){new se(e,t,o,i)}}const vt=()=>{const r=document.createElement("div");return r.style.width="100%",r.style.height="100%",r.style.position="relative",r},ie=()=>{const r=document.createElement("div");return r.style.position="absolute",r.style.inset="0",r},Le=()=>{const r=ie();return r.style.pointerEvents="none",r};class Et{constructor(e){s(this,"background",ie());s(this,"main",ie());s(this,"overlayConnectablePorts",Le());s(this,"overlayDraggableEdges",Le());s(this,"host",vt());this.element=e,this.element.appendChild(this.host),this.host.appendChild(this.background),this.host.appendChild(this.main),this.host.appendChild(this.overlayConnectablePorts),this.host.appendChild(this.overlayDraggableEdges)}destroy(){this.host.removeChild(this.background),this.host.removeChild(this.main),this.host.removeChild(this.overlayConnectablePorts),this.host.removeChild(this.overlayDraggableEdges),this.element.removeChild(this.host)}}const ne=r=>()=>r,Re=ne(0),At=()=>{let r=0;return()=>r++},xt=(r,e)=>{let t=Re,o=Re;const i=At();return r==="incremental"&&(t=i),e==="incremental"&&(o=i),typeof r=="number"&&(t=ne(r)),typeof e=="number"&&(o=ne(e)),typeof r=="function"&&(t=r),typeof e=="function"&&(o=e),{nodesPriorityFn:t,edgesPriorityFn:o}},ae=r=>{if(typeof r=="function")return r;switch(r.type){case"straight":return()=>new Se({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowOffset:r.arrowOffset,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleSquareSide:r.cycleSquareSide,roundness:r.roundness,detourDistance:r.detourDistance,detourDirection:r.detourDirection});case"horizontal":return()=>new xe({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowOffset:r.arrowOffset,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleSquareSide:r.cycleSquareSide,roundness:r.roundness,detourDistance:r.detourDistance});case"vertical":return()=>new Pe({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowOffset:r.arrowOffset,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleSquareSide:r.cycleSquareSide,roundness:r.roundness,detourDistance:r.detourDistance});case"direct":return()=>new Z({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,sourceOffset:r.sourceOffset,targetOffset:r.targetOffset});default:return()=>new Ae({color:r.color,width:r.width,arrowLength:r.arrowLength,arrowRenderer:r.arrowRenderer,hasSourceArrow:r.hasSourceArrow,hasTargetArrow:r.hasTargetArrow,cycleRadius:r.cycleRadius,smallCycleRadius:r.smallCycleRadius,curvature:r.curvature,detourDistance:r.detourDistance,detourDirection:r.detourDirection})}},St=r=>{var t,o,i,n,a;const e=xt((t=r.nodes)==null?void 0:t.priority,(o=r.edges)==null?void 0:o.priority);return{nodes:{centerFn:((i=r.nodes)==null?void 0:i.centerFn)??pe,priorityFn:e.nodesPriorityFn},ports:{direction:((n=r.ports)==null?void 0:n.direction)??0},edges:{shapeFactory:ae(((a=r.edges)==null?void 0:a.shape)??{}),priorityFn:e.edgesPriorityFn}}},Pt=r=>{var p,f,y,A,b;const e=((p=r.events)==null?void 0:p.onNodeDrag)??(()=>{}),t=r.nodeDragVerifier??(()=>!0),o=((f=r.events)==null?void 0:f.onNodeDragFinished)??(()=>{}),i=r.moveOnTop!==!1,n=r.moveEdgesOnTop!==!1&&i,a=(y=r.mouse)==null?void 0:y.dragCursor,h=a!==void 0?a:"grab",d=(A=r.mouse)==null?void 0:A.mouseDownEventVerifier,c=d!==void 0?d:S=>S.button===0,l=(b=r.mouse)==null?void 0:b.mouseUpEventVerifier,g=l!==void 0?l:S=>S.button===0;return{moveOnTop:i,moveEdgesOnTop:n,dragCursor:h,gridSize:r.gridSize??null,mouseDownEventVerifier:c,mouseUpEventVerifier:g,onNodeDrag:e,nodeDragVerifier:t,onNodeDragFinished:o}},bt=r=>{const e=r.minX!==null?r.minX:-1/0,t=r.maxX!==null?r.maxX:1/0,o=r.minY!==null?r.minY:-1/0,i=r.maxY!==null?r.maxY:1/0;return n=>{let a=n.nextTransform.x,h=n.nextTransform.y;a<e&&a<n.prevTransform.x&&(a=Math.min(n.prevTransform.x,e));const d=n.canvasWidth*n.prevTransform.scale,c=t-d;a>c&&a>n.prevTransform.x&&(a=Math.max(n.prevTransform.x,c)),h<o&&h<n.prevTransform.y&&(h=Math.min(n.prevTransform.y,o));const l=n.canvasHeight*n.prevTransform.scale,g=i-l;return h>g&&h>n.prevTransform.y&&(h=Math.max(n.prevTransform.y,g)),{scale:n.nextTransform.scale,x:a,y:h}}},Tt=r=>{const e=r.maxContentScale,t=r.minContentScale,o=e!==null?1/e:0,i=t!==null?1/t:1/0;return n=>{const a=n.prevTransform,h=n.nextTransform;let d=h.scale,c=h.x,l=h.y;if(h.scale>i&&h.scale>a.scale){d=Math.max(a.scale,i),c=a.x,l=a.y;const g=(d-a.scale)/(h.scale-a.scale);c=a.x+(h.x-a.x)*g,l=a.y+(h.y-a.y)*g}if(h.scale<o&&h.scale<a.scale){d=Math.min(a.scale,o),c=a.x,l=a.y;const g=(d-a.scale)/(h.scale-a.scale);c=a.x+(h.x-a.x)*g,l=a.y+(h.y-a.y)*g}return{scale:d,x:c,y:l}}},Nt=r=>e=>r.reduce((t,o)=>o({prevTransform:e.prevTransform,nextTransform:t,canvasWidth:e.canvasWidth,canvasHeight:e.canvasHeight}),e.nextTransform),Ce=r=>{if(typeof r=="function")return r;switch(r.type){case"scale-limit":return Tt({minContentScale:r.minContentScale??0,maxContentScale:r.maxContentScale??1/0});case"shift-limit":return bt({minX:r.minX??-1/0,maxX:r.maxX??1/0,minY:r.minY??-1/0,maxY:r.maxY??1/0})}},Ve=r=>{var y,A,b,S,V,D,L,U,Ue,Ie,Be,Oe;const e=(y=r==null?void 0:r.scale)==null?void 0:y.mouseWheelSensitivity,t=e!==void 0?e:1.2,o=r==null?void 0:r.transformPreprocessor;let i;o!==void 0?Array.isArray(o)?i=Nt(o.map(R=>Ce(R))):i=Ce(o):i=R=>R.nextTransform;const n=((A=r==null?void 0:r.shift)==null?void 0:A.cursor)!==void 0?r.shift.cursor:"grab",a=((b=r==null?void 0:r.events)==null?void 0:b.onBeforeTransformChange)??(()=>{}),h=((S=r==null?void 0:r.events)==null?void 0:S.onTransformChange)??(()=>{}),d=(V=r==null?void 0:r.shift)==null?void 0:V.mouseDownEventVerifier,c=d!==void 0?d:R=>R.button===0,l=(D=r==null?void 0:r.shift)==null?void 0:D.mouseUpEventVerifier,g=l!==void 0?l:R=>R.button===0,p=(L=r==null?void 0:r.scale)==null?void 0:L.mouseWheelEventVerifier,f=p!==void 0?p:()=>!0;return{wheelSensitivity:t,onTransformStarted:((U=r==null?void 0:r.events)==null?void 0:U.onTransformStarted)??(()=>{}),onTransformFinished:((Ue=r==null?void 0:r.events)==null?void 0:Ue.onTransformFinished)??(()=>{}),onBeforeTransformChange:a,onTransformChange:h,transformPreprocessor:i,shiftCursor:n,mouseDownEventVerifier:c,mouseUpEventVerifier:g,mouseWheelEventVerifier:f,scaleWheelFinishTimeout:((Ie=r==null?void 0:r.scale)==null?void 0:Ie.wheelFinishTimeout)??500,onResizeTransformStarted:((Be=r==null?void 0:r.events)==null?void 0:Be.onResizeTransformStarted)??(()=>{}),onResizeTransformFinished:((Oe=r==null?void 0:r.events)==null?void 0:Oe.onResizeTransformFinished)??(()=>{})}},Dt=(r,e)=>{const t=document.createElementNS("http://www.w3.org/2000/svg","circle");return t.setAttribute("cx","0"),t.setAttribute("cy","0"),t.setAttribute("r",`${r}`),t.setAttribute("fill",`${e}`),t},Mt=r=>r instanceof SVGElement?r:Dt((r==null?void 0:r.radius)??1.5,(r==null?void 0:r.color)??"#d8d8d8"),Lt=r=>{const e=r.tileDimensions,t=(e==null?void 0:e.width)??25,o=(e==null?void 0:e.height)??25,i=Mt(r.renderer??{});return{tileWidth:t,tileHeight:o,renderer:i,maxViewportScale:r.maxViewportScale??10}},Rt=(r,e,t)=>{var c,l,g;const o=()=>"direct",i=p=>p,n=p=>p.button===0,a=()=>{},h=()=>{},d=()=>{};return{connectionTypeResolver:r.connectionTypeResolver??o,connectionPreprocessor:r.connectionPreprocessor??i,mouseDownEventVerifier:r.mouseDownEventVerifier??n,mouseUpEventVerifier:r.mouseUpEventVerifier??n,onAfterEdgeCreated:((c=r.events)==null?void 0:c.onAfterEdgeCreated)??a,onEdgeCreationInterrupted:((l=r.events)==null?void 0:l.onEdgeCreationInterrupted)??d,onEdgeCreationPrevented:((g=r.events)==null?void 0:g.onEdgeCreationPrevented)??h,dragPortDirection:r.dragPortDirection??t,edgeShapeFactory:r.edgeShape!==void 0?ae(r.edgeShape):e}},Ct=(r,e)=>{var c,l,g;const t=p=>p,o=p=>p.button===0&&p.ctrlKey,i=p=>p.button===0,n=p=>{const f=e.getPortAdjacentEdgeIds(p);return f.length>0?f[f.length-1]:null},a=()=>{},h=()=>{},d=()=>{};return{connectionPreprocessor:r.connectionPreprocessor??t,mouseDownEventVerifier:r.mouseDownEventVerifier??o,mouseUpEventVerifier:r.mouseUpEventVerifier??i,draggingEdgeResolver:r.draggingEdgeResolver??n,draggingEdgeShapeFactory:r.draggingEdgeShape!==void 0?ae(r.draggingEdgeShape):null,onAfterEdgeReattached:((c=r.events)==null?void 0:c.onAfterEdgeReattached)??a,onEdgeReattachInterrupted:((l=r.events)==null?void 0:l.onEdgeReattachInterrupted)??d,onEdgeReattachPrevented:((g=r.events)==null?void 0:g.onEdgeReattachPrevented)??h}},Vt=r=>({nodeVerticalRadius:r.nodeContainingRadius.vertical,nodeHorizontalRadius:r.nodeContainingRadius.horizontal}),$t=r=>{var e,t;return{onAfterNodeDetached:((e=r==null?void 0:r.events)==null?void 0:e.onAfterNodeDetached)??(()=>{}),onBeforeNodeAttached:((t=r==null?void 0:r.events)==null?void 0:t.onBeforeNodeAttached)??(()=>{})}};class $e extends Error{constructor(){super(...arguments);s(this,"name","CanvasBuilderError")}}const Ut=r=>({algorithm:r.algorithm}),It=r=>r instanceof I?{type:"manual",trigger:r}:{type:"topologyChangeTimeout"},Bt=r=>({algorithm:r.algorithm,applyOn:It(r.applyOn)}),Ot=(r,e)=>({...r,nodeDragVerifier:t=>(e.add(t),r.nodeDragVerifier(t)),onNodeDragFinished:t=>{e.delete(t),r.onNodeDragFinished(t)}}),Ft=(r,e)=>{r.onBeforeNodeRemoved.subscribe(t=>{e.delete(t)}),r.onBeforeClear.subscribe(()=>{e.clear()})};class Wt{constructor(e){s(this,"used",!1);s(this,"canvasDefaults",{});s(this,"dragConfig",{});s(this,"transformConfig",{});s(this,"backgroundConfig",{});s(this,"connectablePortsConfig",{});s(this,"draggableEdgesConfig",{});s(this,"virtualScrollConfig");s(this,"layoutConfig");s(this,"animatedLayoutConfig");s(this,"hasDraggableNodes",!1);s(this,"hasTransformableViewport",!1);s(this,"hasNodeResizeReactiveEdges",!1);s(this,"hasBackground",!1);s(this,"hasUserConnectablePorts",!1);s(this,"hasUserDraggableEdges",!1);s(this,"boxRenderingTrigger",new I);s(this,"graphStore",new ce);s(this,"viewportStore",new je);s(this,"graph",new Ne(this.graphStore));s(this,"viewport",new De(this.viewportStore));s(this,"window",window);s(this,"animationStaticNodes",new Set);this.element=e}setDefaults(e){return this.canvasDefaults=e,this}enableUserDraggableNodes(e){return this.hasDraggableNodes=!0,this.dragConfig=e??{},this}enableUserTransformableViewport(e){return this.hasTransformableViewport=!0,this.transformConfig=e??{},this}enableNodeResizeReactiveEdges(){return this.hasNodeResizeReactiveEdges=!0,this}enableVirtualScroll(e){return this.virtualScrollConfig=e,this}enableBackground(e){return this.hasBackground=!0,this.backgroundConfig=e??{},this}enableUserConnectablePorts(e){return this.hasUserConnectablePorts=!0,this.connectablePortsConfig=e??{},this}enableUserDraggableEdges(e){return this.hasUserDraggableEdges=!0,this.draggableEdgesConfig=e??{},this}enableLayout(e){return this.layoutConfig=e,this.animatedLayoutConfig=void 0,this}enableAnimatedLayout(e){return this.animatedLayoutConfig=e,this.layoutConfig=void 0,this}build(){if(this.used)throw new $e("CanvasBuilder is a single use object");this.used=!0;const e=new Et(this.element);let t=new he(this.graphStore,this.viewportStore,e.main);this.virtualScrollConfig!==void 0&&(t=new Xe(t,this.graphStore,this.boxRenderingTrigger,$t(this.virtualScrollConfig))),t=new He(t,this.graphStore);const o=St(this.canvasDefaults),i=new de(this.graph,this.viewport,this.graphStore,this.viewportStore,t,o);if(this.hasBackground&&q.configure(i,Lt(this.backgroundConfig),e.background),this.hasNodeResizeReactiveEdges&&j.configure(i),this.hasDraggableNodes){let a=Pt(this.dragConfig);this.animatedLayoutConfig!==void 0&&(a=Ot(a,this.animationStaticNodes)),Q.configure(i,e.main,this.window,a)}if(this.hasUserConnectablePorts){const a=Rt(this.connectablePortsConfig,o.edges.shapeFactory,o.ports.direction);ee.configure(i,e.overlayConnectablePorts,this.viewportStore,this.window,a)}if(this.hasUserDraggableEdges){const a=Ct(this.draggableEdgesConfig,i.graph);te.configure(i,e.overlayDraggableEdges,this.viewportStore,this.window,a)}this.virtualScrollConfig!==void 0?_.configure(i,e.main,this.window,Ve(this.transformConfig),this.boxRenderingTrigger,Vt(this.virtualScrollConfig)):this.hasTransformableViewport&&H.configure(i,e.main,this.window,Ve(this.transformConfig)),this.layoutConfig!==void 0&&yt.configure(i,Bt(this.layoutConfig)),this.animatedLayoutConfig!==void 0&&(Ft(i.graph,this.animationStaticNodes),se.configure(i,Ut(this.animatedLayoutConfig),this.animationStaticNodes,this.window));const n=()=>{e.destroy(),i.onBeforeDestroy.unsubscribe(n)};return i.onBeforeDestroy.subscribe(n),i}}v.BezierEdgeShape=Ae,v.CanvasBuilder=Wt,v.CanvasBuilderError=$e,v.CanvasError=P,v.DirectEdgeShape=Z,v.EventSubject=I,v.HorizontalEdgeShape=xe,v.InteractiveEdgeError=Te,v.InteractiveEdgeShape=J,v.MidpointEdgeShape=gt,v.StraightEdgeShape=Se,v.VerticalEdgeShape=Pe,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@html-graph/html-graph",
3
3
  "author": "Dmitry Marov <d.marov94@gmail.com>",
4
4
  "private": false,
5
- "version": "5.1.0",
5
+ "version": "6.0.0",
6
6
  "type": "module",
7
7
  "main": "dist/html-graph.js",
8
8
  "types": "dist/html-graph.d.ts",