avoid-nodes-edge 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -105,6 +105,9 @@ export default defineConfig({
105
105
  plugins: [react()],
106
106
  worker: {
107
107
  format: 'es',
108
+ rollupOptions: {
109
+ output: { entryFileNames: '[name].js' },
110
+ },
108
111
  },
109
112
  optimizeDeps: {
110
113
  exclude: ['avoid-nodes-edge'],
@@ -233,7 +236,7 @@ const { updateRoutingOnNodesChange, resetRouting, refreshRouting, updateRoutingF
233
236
  | `edgeToEdgeSpacing` | `number` | `10` | Distance (px) between parallel edge segments |
234
237
  | `edgeRounding` | `number` | `0` | Corner radius (px) for rounded orthogonal bends |
235
238
  | `diagramGridSize` | `number` | `0` | Snap edge waypoints to a grid of this size (0 = no grid) |
236
- | `shouldSplitEdgesNearHandle` | `boolean` | `undefined` | Split edges near connection handles |
239
+ | `shouldSplitEdgesNearHandle` | `boolean` | `false` | When `false`, edges share a common exit point near the handle and diverge after a small gap. When `true`, edges spread out along the node border. |
237
240
 
238
241
  #### Return Value
239
242
 
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _chunkHK5MLBM7cjs = require('./chunk-HK5MLBM7.cjs');var ee="/libavoid.wasm";var y=class y{static async load(o=ee){if(y.lib!=null)return!0;if(typeof globalThis>"u")return!1;for(let s=1;s<=5;s++){if(await y.loadOnce(o))return!0;s<5&&await new Promise(e=>setTimeout(e,2e3))}return!1}static async loadOnce(o){let s=_optionalChain([globalThis, 'access', _2 => _2.location, 'optionalAccess', _3 => _3.origin]),t=s&&o.startsWith("/")?`${s}${o}`:o;try{let e=await Promise.resolve().then(() => _interopRequireWildcard(require("libavoid-js"))),n=_nullishCoalesce(e.AvoidLib, () => (e.default));if(!_optionalChain([n, 'optionalAccess', _4 => _4.load]))return!1;await n.load(t);let a=_optionalChain([n, 'access', _5 => _5.getInstance, 'optionalCall', _6 => _6()]);return a==null?!1:(y.lib=a,!0)}catch (e2){return!1}}static getInstance(){if(y.instance==null&&(y.instance=new y),y.lib==null)throw new Error("AvoidRouter.load() must be called first.");return y.instance}routeAll(o,s,t){let e=y.lib;if(!e)return{};let n=_nullishCoalesce(_optionalChain([t, 'optionalAccess', _7 => _7.shapeBufferDistance]), () => (8)),a=_nullishCoalesce(_optionalChain([t, 'optionalAccess', _8 => _8.idealNudgingDistance]), () => (10)),b=_nullishCoalesce(_optionalChain([t, 'optionalAccess', _9 => _9.edgeRounding]), () => (0)),c=_nullishCoalesce(_optionalChain([t, 'optionalAccess', _10 => _10.diagramGridSize]), () => (0)),p=o.filter(r=>r.type!=="group"),g=new Map(o.map(r=>[r.id,r])),i=new Map(p.map(r=>[r.id,this.getNodeBoundsAbsolute(r,g)])),u=new e.Router(e.OrthogonalRouting);u.setRoutingParameter(e.shapeBufferDistance,n),u.setRoutingParameter(e.idealNudgingDistance,a),u.setRoutingOption(e.nudgeOrthogonalSegmentsConnectedToShapes,!0),u.setRoutingOption(e.nudgeSharedPathsWithCommonEndPoint,!0),u.setRoutingOption(e.performUnifyingNudgingPreprocessingStep,!0);let d=1,w=2,R=3,A=4,k=5,T={top:w,bottom:R,left:A,right:k},P={[d]:{x:.5,y:.5,dir:e.ConnDirAll},[w]:{x:.5,y:0,dir:e.ConnDirUp},[R]:{x:.5,y:1,dir:e.ConnDirDown},[A]:{x:0,y:.5,dir:e.ConnDirLeft},[k]:{x:1,y:.5,dir:e.ConnDirRight}},E=new Map,l=[];for(let r of p){let v=i.get(r.id),f=new e.Point(v.x,v.y),O=new e.Point(v.x+v.w,v.y+v.h),L=new e.Rectangle(f,O),C=new e.ShapeRef(u,L);l.push({ref:C}),E.set(r.id,C);for(let N of[d,w,R,A,k]){let S=P[N];new e.ShapeConnectionPin(C,N,S.x,S.y,!0,0,S.dir).setExclusive(!1)}}let h=[];for(let r of s){let v=g.get(r.source),f=g.get(r.target);if(!v||!f)continue;let O=E.get(r.source),L=E.get(r.target),C=this.getHandlePosition(v,"source"),N=this.getHandlePosition(f,"target"),S,D;if(O){let W=_nullishCoalesce(T[C], () => (d));S=new e.ConnEnd(O,W)}else{let W=this.getNodeBoundsAbsolute(v,g),B=this.getHandlePoint(W,C);S=new e.ConnEnd(new e.Point(B.x,B.y))}if(L){let W=_nullishCoalesce(T[N], () => (d));D=new e.ConnEnd(L,W)}else{let W=this.getNodeBoundsAbsolute(f,g),B=this.getHandlePoint(W,N);D=new e.ConnEnd(new e.Point(B.x,B.y))}let U=new e.ConnRef(u,S,D);U.setRoutingType(e.ConnType_Orthogonal),h.push({edgeId:r.id,connRef:U})}try{u.processTransaction()}catch (e3){return this.cleanup(u,h,l),{}}let x={};for(let{edgeId:r,connRef:v}of h)try{let f=v.displayRoute(),O=f.size();if(O<2)continue;let L=this.polylineToPath(O,D=>{let U=f.get_ps(D);return{x:U.x,y:U.y}},{gridSize:c||void 0,cornerRadius:b}),C=Math.floor(O/2),N=f.get_ps(C),S=c>0?this.snapToGrid(N.x,N.y,c):{x:N.x,y:N.y};x[r]={path:L,labelX:S.x,labelY:S.y}}catch (e4){}return this.cleanup(u,h,l),x}getNodeBounds(o){let s=_nullishCoalesce(_optionalChain([o, 'access', _11 => _11.position, 'optionalAccess', _12 => _12.x]), () => (0)),t=_nullishCoalesce(_optionalChain([o, 'access', _13 => _13.position, 'optionalAccess', _14 => _14.y]), () => (0)),e=Number(_nullishCoalesce(_nullishCoalesce(_nullishCoalesce(_optionalChain([o, 'access', _15 => _15.measured, 'optionalAccess', _16 => _16.width]), () => (o.width)), () => (_optionalChain([o, 'access', _17 => _17.style, 'optionalAccess', _18 => _18.width]))), () => (150))),n=Number(_nullishCoalesce(_nullishCoalesce(_nullishCoalesce(_optionalChain([o, 'access', _19 => _19.measured, 'optionalAccess', _20 => _20.height]), () => (o.height)), () => (_optionalChain([o, 'access', _21 => _21.style, 'optionalAccess', _22 => _22.height]))), () => (50)));return{x:s,y:t,w:e,h:n}}getNodeBoundsAbsolute(o,s){let t=this.getNodeBounds(o),e=o;for(;_optionalChain([e, 'optionalAccess', _23 => _23.parentId]);){let n=s.get(e.parentId);if(!n)break;t.x+=_nullishCoalesce(_optionalChain([n, 'access', _24 => _24.position, 'optionalAccess', _25 => _25.x]), () => (0)),t.y+=_nullishCoalesce(_optionalChain([n, 'access', _26 => _26.position, 'optionalAccess', _27 => _27.y]), () => (0)),e=n}return t}getHandlePosition(o,s){let t=s==="source"?_nullishCoalesce(o.sourcePosition, () => (_optionalChain([o, 'access', _28 => _28.data, 'optionalAccess', _29 => _29.sourcePosition]))):_nullishCoalesce(o.targetPosition, () => (_optionalChain([o, 'access', _30 => _30.data, 'optionalAccess', _31 => _31.targetPosition]))),e=String(_nullishCoalesce(t, () => (""))).toLowerCase();return e==="left"||e==="right"||e==="top"||e==="bottom"?e:s==="source"?"right":"left"}getHandlePoint(o,s){let{x:t,y:e,w:n,h:a}=o,b=t+n/2,c=e+a/2;switch(s){case"left":return{x:t,y:c};case"right":return{x:t+n,y:c};case"top":return{x:b,y:e};case"bottom":return{x:b,y:e+a};default:return{x:t+n,y:c}}}snapToGrid(o,s,t){return t<=0?{x:o,y:s}:{x:Math.round(o/t)*t,y:Math.round(s/t)*t}}polylineToPath(o,s,t={}){if(o<2)return"";let e=_nullishCoalesce(t.gridSize, () => (0)),n=Math.max(0,_nullishCoalesce(t.cornerRadius, () => (0))),a=i=>{let u=s(i);return e>0?this.snapToGrid(u.x,u.y,e):u};if(n<=0){let i=`M ${a(0).x} ${a(0).y}`;for(let u=1;u<o;u++){let d=a(u);i+=` L ${d.x} ${d.y}`}return i}let b=(i,u)=>Math.hypot(u.x-i.x,u.y-i.y),c=(i,u)=>{let d=b(i,u);return d<1e-6?{x:0,y:0}:{x:(u.x-i.x)/d,y:(u.y-i.y)/d}},p=`M ${a(0).x} ${a(0).y}`;for(let i=1;i<o-1;i++){let u=a(i-1),d=a(i),w=a(i+1),R=c(d,u),A=c(d,w),k=b(d,u),T=b(d,w),P=Math.min(n,k/2,T/2),E=Math.min(n,k/2,T/2),l={x:d.x+R.x*P,y:d.y+R.y*P},h={x:d.x+A.x*E,y:d.y+A.y*E};p+=` L ${l.x} ${l.y} Q ${d.x} ${d.y} ${h.x} ${h.y}`}let g=a(o-1);return p+=` L ${g.x} ${g.y}`,p}cleanup(o,s,t){try{for(let{connRef:e}of s)o.deleteConnector(e);for(let{ref:e}of t)o.deleteShape(e)}catch (e5){}}};_chunkHK5MLBM7cjs.a.call(void 0, y,"lib",null),_chunkHK5MLBM7cjs.a.call(void 0, y,"instance",null);var H=y;async function oe(){return H.load()}function te(m,o,s){try{return H.getInstance().routeAll(m,o,s)}catch (e6){return{}}}var _react = require('react');function Y(m,o={}){let{onRouted:s,onLoaded:t}=o,e=_chunkHK5MLBM7cjs.b.getState().setLoaded,n=_chunkHK5MLBM7cjs.b.getState().setRoutes,a=b=>{let c=b.data;if(!(!c||typeof c!="object"||!("command"in c)))switch(c.command){case"loaded":e(c.success),_optionalChain([t, 'optionalCall', _32 => _32(c.success)]);break;case"routed":n(c.routes),_optionalChain([s, 'optionalCall', _33 => _33(c.routes)]);break;default:break}};return m.addEventListener("message",a),()=>m.removeEventListener("message",a)}function j(m){let o=_react.useRef.call(void 0, null),[s,t]=_react.useState.call(void 0, !1),e=_react.useRef.call(void 0, _optionalChain([m, 'optionalAccess', _34 => _34.onRouted])),n=_react.useRef.call(void 0, _optionalChain([m, 'optionalAccess', _35 => _35.onLoaded]));e.current=_optionalChain([m, 'optionalAccess', _36 => _36.onRouted]),n.current=_optionalChain([m, 'optionalAccess', _37 => _37.onLoaded]);let a=_optionalChain([m, 'optionalAccess', _38 => _38.create])!==!1;_react.useEffect.call(void 0, ()=>{if(!a){o.current=null,t(!1);return}let p;try{p=new Worker(new URL("./workers/avoid-router.worker.js",import.meta.url),{type:"module"})}catch(i){console.error("[avoid-worker] Failed to create worker:",i);return}o.current=p,p.addEventListener("error",i=>{console.error("[avoid-worker] Worker error:",i.message)});let g=Y(p,{onRouted:i=>_optionalChain([e, 'access', _39 => _39.current, 'optionalCall', _40 => _40(i)]),onLoaded:i=>{t(i),_optionalChain([n, 'access', _41 => _41.current, 'optionalCall', _42 => _42(i)])}});return()=>{g(),p.postMessage({command:"close"}),p.terminate(),o.current=null,t(!1)}},[a]);let b=_react.useCallback.call(void 0, p=>{o.current&&o.current.postMessage(p)},[]),c=_react.useCallback.call(void 0, ()=>{o.current&&(o.current.postMessage({command:"close"}),o.current.terminate(),o.current=null,t(!1))},[]);return{workerLoaded:s,post:b,close:c}}var V={edgeToEdgeSpacing:10,edgeToNodeSpacing:8};function se(m){return{idealNudgingDistance:_nullishCoalesce(_optionalChain([m, 'optionalAccess', _43 => _43.edgeToEdgeSpacing]), () => (V.edgeToEdgeSpacing)),shapeBufferDistance:_nullishCoalesce(_optionalChain([m, 'optionalAccess', _44 => _44.edgeToNodeSpacing]), () => (V.edgeToNodeSpacing)),edgeRounding:_optionalChain([m, 'optionalAccess', _45 => _45.edgeRounding]),diagramGridSize:_optionalChain([m, 'optionalAccess', _46 => _46.diagramGridSize]),shouldSplitEdgesNearHandle:_optionalChain([m, 'optionalAccess', _47 => _47.shouldSplitEdgesNearHandle])}}function ie(m,o,s){let t=_react.useRef.call(void 0, m),e=_react.useRef.call(void 0, o),n=se(s),a=_react.useRef.call(void 0, n);_react.useEffect.call(void 0, ()=>{t.current=m},[m]),_react.useEffect.call(void 0, ()=>{e.current=o},[o]),_react.useEffect.call(void 0, ()=>{a.current=n});let b=_chunkHK5MLBM7cjs.b.call(void 0, l=>l.setRoutes),c=_chunkHK5MLBM7cjs.c.call(void 0, l=>l.setActions),{post:p,workerLoaded:g}=j({create:!0}),i=_react.useRef.call(void 0, !1),u=_react.useRef.call(void 0, !1),d=_react.useCallback.call(void 0, ()=>{if(!g)return;let l=t.current;if(!(l.length===0||l.some(r=>_optionalChain([r, 'access', _48 => _48.measured, 'optionalAccess', _49 => _49.width])!=null)))return;u.current=!0;let x=e.current.filter(r=>r.type==="avoidNodes");if(x.length===0){b({});return}p({command:"reset",nodes:l,edges:x,options:a.current}),i.current=!0},[p,b,g]),w=_react.useCallback.call(void 0, l=>{if(!g||!i.current)return;let h=new Map(t.current.map(r=>[r.id,r])),x=l.map(r=>h.get(r)).filter(r=>r!=null);x.length!==0&&p({command:"updateNodes",nodes:x})},[p,g]),R=_react.useRef.call(void 0, null),A=_react.useRef.call(void 0, new Set),k=_react.useCallback.call(void 0, ()=>{d()},[d]),T=_react.useCallback.call(void 0, ()=>{d()},[d]),P=_react.useCallback.call(void 0, l=>{w(l)},[w]),E=_react.useCallback.call(void 0, l=>{if(!g)return;let h=!1,x=!1,r=!1;for(let f of l)f.type==="position"?(h=!0,A.current.add(f.id)):f.type==="dimensions"?(x=!0,A.current.add(f.id)):(f.type==="add"||f.type==="remove")&&(r=!0);if(!h&&!x&&!r)return;if(r||x&&!u.current){R.current&&(clearTimeout(R.current),R.current=null),A.current.clear(),R.current=setTimeout(()=>{R.current=null,requestAnimationFrame(()=>d())},0);return}i.current&&(R.current&&clearTimeout(R.current),R.current=setTimeout(()=>{R.current=null,requestAnimationFrame(()=>{let f=Array.from(A.current);A.current.clear(),f.length>0&&w(f)})},0))},[g,d,w]);return _react.useEffect.call(void 0, ()=>(c({resetRouting:k,updateRoutesForNodeId:l=>P([l])}),()=>c({resetRouting:()=>{},updateRoutesForNodeId:()=>{}})),[k,P,c]),_react.useEffect.call(void 0, ()=>{g&&d()},[g,m.length,o.length,n.shapeBufferDistance,n.idealNudgingDistance,n.edgeRounding,n.diagramGridSize,n.shouldSplitEdgesNearHandle,d]),{updateRoutingOnNodesChange:E,resetRouting:k,refreshRouting:T,updateRoutingForNodeIds:P}}exports.AvoidRouter = H; exports.DEBOUNCE_ROUTING_MS = _chunkHK5MLBM7cjs.e; exports.DEV_LOG_WEB_WORKER_MESSAGES = _chunkHK5MLBM7cjs.d; exports.EDGE_BORDER_RADIUS = _chunkHK5MLBM7cjs.g; exports.SHOULD_START_EDGE_AT_HANDLE_BORDER = _chunkHK5MLBM7cjs.f; exports.attachAvoidWorkerListener = Y; exports.loadAvoidRouter = oe; exports.routeAll = te; exports.useAvoidNodesPath = _chunkHK5MLBM7cjs.h; exports.useAvoidNodesRouterFromWorker = ie; exports.useAvoidRouterActionsStore = _chunkHK5MLBM7cjs.c; exports.useAvoidRoutesStore = _chunkHK5MLBM7cjs.b; exports.useAvoidWorker = j;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _chunkHK5MLBM7cjs = require('./chunk-HK5MLBM7.cjs');var ne="/libavoid.wasm";var y=class y{static async load(o=ne){if(y.lib!=null)return!0;if(typeof globalThis>"u")return!1;for(let i=1;i<=5;i++){if(await y.loadOnce(o))return!0;i<5&&await new Promise(e=>setTimeout(e,2e3))}return!1}static async loadOnce(o){let i=_optionalChain([globalThis, 'access', _2 => _2.location, 'optionalAccess', _3 => _3.origin]),t=i&&o.startsWith("/")?`${i}${o}`:o;try{let e=await Promise.resolve().then(() => _interopRequireWildcard(require("libavoid-js"))),s=_nullishCoalesce(e.AvoidLib, () => (e.default));if(!_optionalChain([s, 'optionalAccess', _4 => _4.load]))return!1;await s.load(t);let u=_optionalChain([s, 'access', _5 => _5.getInstance, 'optionalCall', _6 => _6()]);return u==null?!1:(y.lib=u,!0)}catch (e2){return!1}}static getInstance(){if(y.instance==null&&(y.instance=new y),y.lib==null)throw new Error("AvoidRouter.load() must be called first.");return y.instance}routeAll(o,i,t){let e=y.lib;if(!e)return{};let s=_nullishCoalesce(_optionalChain([t, 'optionalAccess', _7 => _7.shapeBufferDistance]), () => (8)),u=_nullishCoalesce(_optionalChain([t, 'optionalAccess', _8 => _8.idealNudgingDistance]), () => (10)),h=_nullishCoalesce(_optionalChain([t, 'optionalAccess', _9 => _9.edgeRounding]), () => (0)),d=_nullishCoalesce(_optionalChain([t, 'optionalAccess', _10 => _10.diagramGridSize]), () => (0)),p=_nullishCoalesce(_optionalChain([t, 'optionalAccess', _11 => _11.shouldSplitEdgesNearHandle]), () => (!1)),R=o.filter(g=>g.type!=="group"),n=new Map(o.map(g=>[g.id,g])),c=new Map(R.map(g=>[g.id,this.getNodeBoundsAbsolute(g,n)])),r=new e.Router(e.OrthogonalRouting);r.setRoutingParameter(e.shapeBufferDistance,s),r.setRoutingParameter(e.idealNudgingDistance,u),r.setRoutingOption(e.nudgeOrthogonalSegmentsConnectedToShapes,!0),r.setRoutingOption(e.nudgeSharedPathsWithCommonEndPoint,!0),r.setRoutingOption(e.performUnifyingNudgingPreprocessingStep,!0);let A=1,b=2,x=3,P=4,T=5,E={top:b,bottom:x,left:P,right:T},U={[A]:{x:.5,y:.5,dir:e.ConnDirAll},[b]:{x:.5,y:0,dir:e.ConnDirUp},[x]:{x:.5,y:1,dir:e.ConnDirDown},[P]:{x:0,y:.5,dir:e.ConnDirLeft},[T]:{x:1,y:.5,dir:e.ConnDirRight}},l=new Map,w=[];for(let g of R){let a=c.get(g.id),O=new e.Point(a.x,a.y),M=new e.Point(a.x+a.w,a.y+a.h),S=new e.Rectangle(O,M),C=new e.ShapeRef(r,S);w.push({ref:C}),l.set(g.id,C);for(let W of[A,b,x,P,T]){let N=U[W];new e.ShapeConnectionPin(C,W,N.x,N.y,!0,0,N.dir).setExclusive(!1)}}let v=[];for(let g of i){let a=n.get(g.source),O=n.get(g.target);if(!a||!O)continue;let M=l.get(g.source),S=l.get(g.target),C=this.getHandlePosition(a,"source"),W=this.getHandlePosition(O,"target"),N,I;if(p){if(M){let k=_nullishCoalesce(E[C], () => (A));N=new e.ConnEnd(M,k)}else{let k=this.getNodeBoundsAbsolute(a,n),D=this.getHandlePoint(k,C);N=new e.ConnEnd(new e.Point(D.x,D.y))}if(S){let k=_nullishCoalesce(E[W], () => (A));I=new e.ConnEnd(S,k)}else{let k=this.getNodeBoundsAbsolute(O,n),D=this.getHandlePoint(k,W);I=new e.ConnEnd(new e.Point(D.x,D.y))}}else{let k=this.getNodeBoundsAbsolute(a,n),D=this.getHandlePoint(k,C);N=new e.ConnEnd(new e.Point(D.x,D.y));let J=this.getNodeBoundsAbsolute(O,n),V=this.getHandlePoint(J,W);I=new e.ConnEnd(new e.Point(V.x,V.y))}let L=new e.ConnRef(r,N,I);L.setRoutingType(e.ConnType_Orthogonal),v.push({edgeId:g.id,connRef:L})}try{r.processTransaction()}catch (e3){return this.cleanup(r,v,w),{}}let f={};for(let{edgeId:g,connRef:a}of v)try{let O=a.displayRoute(),M=O.size();if(M<2)continue;let S=[];for(let L=0;L<M;L++){let k=O.get_ps(L);S.push({x:k.x,y:k.y})}let C=this.polylineToPath(S.length,L=>S[L],{gridSize:d||void 0,cornerRadius:h}),W=Math.floor(S.length/2),N=S[W],I=d>0?this.snapToGrid(N.x,N.y,d):N;f[g]={path:C,labelX:I.x,labelY:I.y}}catch (e4){}return this.cleanup(r,v,w),f}getNodeBounds(o){let i=_nullishCoalesce(_optionalChain([o, 'access', _12 => _12.position, 'optionalAccess', _13 => _13.x]), () => (0)),t=_nullishCoalesce(_optionalChain([o, 'access', _14 => _14.position, 'optionalAccess', _15 => _15.y]), () => (0)),e=Number(_nullishCoalesce(_nullishCoalesce(_nullishCoalesce(_optionalChain([o, 'access', _16 => _16.measured, 'optionalAccess', _17 => _17.width]), () => (o.width)), () => (_optionalChain([o, 'access', _18 => _18.style, 'optionalAccess', _19 => _19.width]))), () => (150))),s=Number(_nullishCoalesce(_nullishCoalesce(_nullishCoalesce(_optionalChain([o, 'access', _20 => _20.measured, 'optionalAccess', _21 => _21.height]), () => (o.height)), () => (_optionalChain([o, 'access', _22 => _22.style, 'optionalAccess', _23 => _23.height]))), () => (50)));return{x:i,y:t,w:e,h:s}}getNodeBoundsAbsolute(o,i){let t=this.getNodeBounds(o),e=o;for(;_optionalChain([e, 'optionalAccess', _24 => _24.parentId]);){let s=i.get(e.parentId);if(!s)break;t.x+=_nullishCoalesce(_optionalChain([s, 'access', _25 => _25.position, 'optionalAccess', _26 => _26.x]), () => (0)),t.y+=_nullishCoalesce(_optionalChain([s, 'access', _27 => _27.position, 'optionalAccess', _28 => _28.y]), () => (0)),e=s}return t}getHandlePosition(o,i){let t=i==="source"?_nullishCoalesce(o.sourcePosition, () => (_optionalChain([o, 'access', _29 => _29.data, 'optionalAccess', _30 => _30.sourcePosition]))):_nullishCoalesce(o.targetPosition, () => (_optionalChain([o, 'access', _31 => _31.data, 'optionalAccess', _32 => _32.targetPosition]))),e=String(_nullishCoalesce(t, () => (""))).toLowerCase();return e==="left"||e==="right"||e==="top"||e==="bottom"?e:i==="source"?"right":"left"}getHandlePoint(o,i){let{x:t,y:e,w:s,h:u}=o,h=t+s/2,d=e+u/2;switch(i){case"left":return{x:t,y:d};case"right":return{x:t+s,y:d};case"top":return{x:h,y:e};case"bottom":return{x:h,y:e+u};default:return{x:t+s,y:d}}}snapToGrid(o,i,t){return t<=0?{x:o,y:i}:{x:Math.round(o/t)*t,y:Math.round(i/t)*t}}polylineToPath(o,i,t={}){if(o<2)return"";let e=_nullishCoalesce(t.gridSize, () => (0)),s=Math.max(0,_nullishCoalesce(t.cornerRadius, () => (0))),u=n=>{let c=i(n);return e>0?this.snapToGrid(c.x,c.y,e):c};if(s<=0){let n=`M ${u(0).x} ${u(0).y}`;for(let c=1;c<o;c++){let r=u(c);n+=` L ${r.x} ${r.y}`}return n}let h=(n,c)=>Math.hypot(c.x-n.x,c.y-n.y),d=(n,c)=>{let r=h(n,c);return r<1e-6?{x:0,y:0}:{x:(c.x-n.x)/r,y:(c.y-n.y)/r}},p=`M ${u(0).x} ${u(0).y}`;for(let n=1;n<o-1;n++){let c=u(n-1),r=u(n),A=u(n+1),b=d(r,c),x=d(r,A),P=h(r,c),T=h(r,A),E=Math.min(s,P/2,T/2),U=Math.min(s,P/2,T/2),l={x:r.x+b.x*E,y:r.y+b.y*E},w={x:r.x+x.x*U,y:r.y+x.y*U};p+=` L ${l.x} ${l.y} Q ${r.x} ${r.y} ${w.x} ${w.y}`}let R=u(o-1);return p+=` L ${R.x} ${R.y}`,p}cleanup(o,i,t){try{for(let{connRef:e}of i)o.deleteConnector(e);for(let{ref:e}of t)o.deleteShape(e)}catch (e5){}}};_chunkHK5MLBM7cjs.a.call(void 0, y,"lib",null),_chunkHK5MLBM7cjs.a.call(void 0, y,"instance",null);var G=y;async function re(){return G.load()}function se(m,o,i){try{return G.getInstance().routeAll(m,o,i)}catch (e6){return{}}}var _react = require('react');function X(m,o={}){let{onRouted:i,onLoaded:t}=o,e=_chunkHK5MLBM7cjs.b.getState().setLoaded,s=_chunkHK5MLBM7cjs.b.getState().setRoutes,u=h=>{let d=h.data;if(!(!d||typeof d!="object"||!("command"in d)))switch(d.command){case"loaded":e(d.success),_optionalChain([t, 'optionalCall', _33 => _33(d.success)]);break;case"routed":s(d.routes),_optionalChain([i, 'optionalCall', _34 => _34(d.routes)]);break;default:break}};return m.addEventListener("message",u),()=>m.removeEventListener("message",u)}function q(m){let o=_react.useRef.call(void 0, null),[i,t]=_react.useState.call(void 0, !1),e=_react.useRef.call(void 0, _optionalChain([m, 'optionalAccess', _35 => _35.onRouted])),s=_react.useRef.call(void 0, _optionalChain([m, 'optionalAccess', _36 => _36.onLoaded]));e.current=_optionalChain([m, 'optionalAccess', _37 => _37.onRouted]),s.current=_optionalChain([m, 'optionalAccess', _38 => _38.onLoaded]);let u=_optionalChain([m, 'optionalAccess', _39 => _39.create])!==!1;_react.useEffect.call(void 0, ()=>{if(!u){o.current=null,t(!1);return}let p;try{p=new Worker(new URL("./workers/avoid-router.worker.js",import.meta.url),{type:"module"})}catch(n){console.error("[avoid-worker] Failed to create worker:",n);return}o.current=p,p.addEventListener("error",n=>{console.error("[avoid-worker] Worker error:",n.message)});let R=X(p,{onRouted:n=>_optionalChain([e, 'access', _40 => _40.current, 'optionalCall', _41 => _41(n)]),onLoaded:n=>{t(n),_optionalChain([s, 'access', _42 => _42.current, 'optionalCall', _43 => _43(n)])}});return()=>{R(),p.postMessage({command:"close"}),p.terminate(),o.current=null,t(!1)}},[u]);let h=_react.useCallback.call(void 0, p=>{o.current&&o.current.postMessage(p)},[]),d=_react.useCallback.call(void 0, ()=>{o.current&&(o.current.postMessage({command:"close"}),o.current.terminate(),o.current=null,t(!1))},[]);return{workerLoaded:i,post:h,close:d}}var Q={edgeToEdgeSpacing:10,edgeToNodeSpacing:8};function de(m){return{idealNudgingDistance:_nullishCoalesce(_optionalChain([m, 'optionalAccess', _44 => _44.edgeToEdgeSpacing]), () => (Q.edgeToEdgeSpacing)),shapeBufferDistance:_nullishCoalesce(_optionalChain([m, 'optionalAccess', _45 => _45.edgeToNodeSpacing]), () => (Q.edgeToNodeSpacing)),edgeRounding:_optionalChain([m, 'optionalAccess', _46 => _46.edgeRounding]),diagramGridSize:_optionalChain([m, 'optionalAccess', _47 => _47.diagramGridSize]),shouldSplitEdgesNearHandle:_optionalChain([m, 'optionalAccess', _48 => _48.shouldSplitEdgesNearHandle])}}function ae(m,o,i){let t=_react.useRef.call(void 0, m),e=_react.useRef.call(void 0, o),s=de(i),u=_react.useRef.call(void 0, s);_react.useEffect.call(void 0, ()=>{t.current=m},[m]),_react.useEffect.call(void 0, ()=>{e.current=o},[o]),_react.useEffect.call(void 0, ()=>{u.current=s});let h=_chunkHK5MLBM7cjs.b.call(void 0, l=>l.setRoutes),d=_chunkHK5MLBM7cjs.c.call(void 0, l=>l.setActions),{post:p,workerLoaded:R}=q({create:!0}),n=_react.useRef.call(void 0, !1),c=_react.useRef.call(void 0, !1),r=_react.useCallback.call(void 0, ()=>{if(!R)return;let l=t.current;if(!(l.length===0||l.some(f=>_optionalChain([f, 'access', _49 => _49.measured, 'optionalAccess', _50 => _50.width])!=null)))return;c.current=!0;let v=e.current.filter(f=>f.type==="avoidNodes");if(v.length===0){h({});return}p({command:"reset",nodes:l,edges:v,options:u.current}),n.current=!0},[p,h,R]),A=_react.useCallback.call(void 0, l=>{if(!R||!n.current)return;let w=new Map(t.current.map(f=>[f.id,f])),v=l.map(f=>w.get(f)).filter(f=>f!=null);v.length!==0&&p({command:"updateNodes",nodes:v})},[p,R]),b=_react.useRef.call(void 0, null),x=_react.useRef.call(void 0, new Set),P=_react.useCallback.call(void 0, ()=>{r()},[r]),T=_react.useCallback.call(void 0, ()=>{r()},[r]),E=_react.useCallback.call(void 0, l=>{A(l)},[A]),U=_react.useCallback.call(void 0, l=>{if(!R)return;let w=!1,v=!1,f=!1;for(let a of l)a.type==="position"?(w=!0,x.current.add(a.id)):a.type==="dimensions"?(v=!0,x.current.add(a.id)):(a.type==="add"||a.type==="remove")&&(f=!0);if(!w&&!v&&!f)return;if(f||v&&!c.current){b.current&&(clearTimeout(b.current),b.current=null),x.current.clear(),b.current=setTimeout(()=>{b.current=null,requestAnimationFrame(()=>r())},0);return}n.current&&(b.current&&clearTimeout(b.current),b.current=setTimeout(()=>{b.current=null,requestAnimationFrame(()=>{let a=Array.from(x.current);x.current.clear(),a.length>0&&A(a)})},0))},[R,r,A]);return _react.useEffect.call(void 0, ()=>(d({resetRouting:P,updateRoutesForNodeId:l=>E([l])}),()=>d({resetRouting:()=>{},updateRoutesForNodeId:()=>{}})),[P,E,d]),_react.useEffect.call(void 0, ()=>{R&&r()},[R,m.length,o.length,s.shapeBufferDistance,s.idealNudgingDistance,s.edgeRounding,s.diagramGridSize,s.shouldSplitEdgesNearHandle,r]),{updateRoutingOnNodesChange:U,resetRouting:P,refreshRouting:T,updateRoutingForNodeIds:E}}exports.AvoidRouter = G; exports.DEBOUNCE_ROUTING_MS = _chunkHK5MLBM7cjs.e; exports.DEV_LOG_WEB_WORKER_MESSAGES = _chunkHK5MLBM7cjs.d; exports.EDGE_BORDER_RADIUS = _chunkHK5MLBM7cjs.g; exports.SHOULD_START_EDGE_AT_HANDLE_BORDER = _chunkHK5MLBM7cjs.f; exports.attachAvoidWorkerListener = X; exports.loadAvoidRouter = re; exports.routeAll = se; exports.useAvoidNodesPath = _chunkHK5MLBM7cjs.h; exports.useAvoidNodesRouterFromWorker = ae; exports.useAvoidRouterActionsStore = _chunkHK5MLBM7cjs.c; exports.useAvoidRoutesStore = _chunkHK5MLBM7cjs.b; exports.useAvoidWorker = q;
package/dist/index.d.cts CHANGED
@@ -94,11 +94,11 @@ declare function useAvoidNodesPath(params: UseAvoidNodesPathParams): [path: stri
94
94
  */
95
95
 
96
96
  interface UseAvoidNodesRouterOptions {
97
- shouldSplitEdgesNearHandle?: boolean;
98
97
  edgeToEdgeSpacing?: number;
99
98
  edgeToNodeSpacing?: number;
100
99
  edgeRounding?: number;
101
100
  diagramGridSize?: number;
101
+ shouldSplitEdgesNearHandle?: boolean;
102
102
  }
103
103
  interface UseAvoidNodesRouterResult {
104
104
  updateRoutingOnNodesChange: (changes: NodeChange<Node>[]) => void;
package/dist/index.d.ts CHANGED
@@ -94,11 +94,11 @@ declare function useAvoidNodesPath(params: UseAvoidNodesPathParams): [path: stri
94
94
  */
95
95
 
96
96
  interface UseAvoidNodesRouterOptions {
97
- shouldSplitEdgesNearHandle?: boolean;
98
97
  edgeToEdgeSpacing?: number;
99
98
  edgeToNodeSpacing?: number;
100
99
  edgeRounding?: number;
101
100
  diagramGridSize?: number;
101
+ shouldSplitEdgesNearHandle?: boolean;
102
102
  }
103
103
  interface UseAvoidNodesRouterResult {
104
104
  updateRoutingOnNodesChange: (changes: NodeChange<Node>[]) => void;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{a as F,b as M,c as z,d as K,e as $,f as Q,g as J,h as Z}from"./chunk-2USZC2VI.js";var ee="/libavoid.wasm";var y=class y{static async load(o=ee){if(y.lib!=null)return!0;if(typeof globalThis>"u")return!1;for(let s=1;s<=5;s++){if(await y.loadOnce(o))return!0;s<5&&await new Promise(e=>setTimeout(e,2e3))}return!1}static async loadOnce(o){let s=globalThis.location?.origin,t=s&&o.startsWith("/")?`${s}${o}`:o;try{let e=await import("libavoid-js"),n=e.AvoidLib??e.default;if(!n?.load)return!1;await n.load(t);let a=n.getInstance?.();return a==null?!1:(y.lib=a,!0)}catch{return!1}}static getInstance(){if(y.instance==null&&(y.instance=new y),y.lib==null)throw new Error("AvoidRouter.load() must be called first.");return y.instance}routeAll(o,s,t){let e=y.lib;if(!e)return{};let n=t?.shapeBufferDistance??8,a=t?.idealNudgingDistance??10,b=t?.edgeRounding??0,c=t?.diagramGridSize??0,p=o.filter(r=>r.type!=="group"),g=new Map(o.map(r=>[r.id,r])),i=new Map(p.map(r=>[r.id,this.getNodeBoundsAbsolute(r,g)])),u=new e.Router(e.OrthogonalRouting);u.setRoutingParameter(e.shapeBufferDistance,n),u.setRoutingParameter(e.idealNudgingDistance,a),u.setRoutingOption(e.nudgeOrthogonalSegmentsConnectedToShapes,!0),u.setRoutingOption(e.nudgeSharedPathsWithCommonEndPoint,!0),u.setRoutingOption(e.performUnifyingNudgingPreprocessingStep,!0);let d=1,w=2,R=3,A=4,k=5,T={top:w,bottom:R,left:A,right:k},P={[d]:{x:.5,y:.5,dir:e.ConnDirAll},[w]:{x:.5,y:0,dir:e.ConnDirUp},[R]:{x:.5,y:1,dir:e.ConnDirDown},[A]:{x:0,y:.5,dir:e.ConnDirLeft},[k]:{x:1,y:.5,dir:e.ConnDirRight}},E=new Map,l=[];for(let r of p){let v=i.get(r.id),f=new e.Point(v.x,v.y),O=new e.Point(v.x+v.w,v.y+v.h),L=new e.Rectangle(f,O),C=new e.ShapeRef(u,L);l.push({ref:C}),E.set(r.id,C);for(let N of[d,w,R,A,k]){let S=P[N];new e.ShapeConnectionPin(C,N,S.x,S.y,!0,0,S.dir).setExclusive(!1)}}let h=[];for(let r of s){let v=g.get(r.source),f=g.get(r.target);if(!v||!f)continue;let O=E.get(r.source),L=E.get(r.target),C=this.getHandlePosition(v,"source"),N=this.getHandlePosition(f,"target"),S,D;if(O){let W=T[C]??d;S=new e.ConnEnd(O,W)}else{let W=this.getNodeBoundsAbsolute(v,g),B=this.getHandlePoint(W,C);S=new e.ConnEnd(new e.Point(B.x,B.y))}if(L){let W=T[N]??d;D=new e.ConnEnd(L,W)}else{let W=this.getNodeBoundsAbsolute(f,g),B=this.getHandlePoint(W,N);D=new e.ConnEnd(new e.Point(B.x,B.y))}let U=new e.ConnRef(u,S,D);U.setRoutingType(e.ConnType_Orthogonal),h.push({edgeId:r.id,connRef:U})}try{u.processTransaction()}catch{return this.cleanup(u,h,l),{}}let x={};for(let{edgeId:r,connRef:v}of h)try{let f=v.displayRoute(),O=f.size();if(O<2)continue;let L=this.polylineToPath(O,D=>{let U=f.get_ps(D);return{x:U.x,y:U.y}},{gridSize:c||void 0,cornerRadius:b}),C=Math.floor(O/2),N=f.get_ps(C),S=c>0?this.snapToGrid(N.x,N.y,c):{x:N.x,y:N.y};x[r]={path:L,labelX:S.x,labelY:S.y}}catch{}return this.cleanup(u,h,l),x}getNodeBounds(o){let s=o.position?.x??0,t=o.position?.y??0,e=Number(o.measured?.width??o.width??o.style?.width??150),n=Number(o.measured?.height??o.height??o.style?.height??50);return{x:s,y:t,w:e,h:n}}getNodeBoundsAbsolute(o,s){let t=this.getNodeBounds(o),e=o;for(;e?.parentId;){let n=s.get(e.parentId);if(!n)break;t.x+=n.position?.x??0,t.y+=n.position?.y??0,e=n}return t}getHandlePosition(o,s){let t=s==="source"?o.sourcePosition??o.data?.sourcePosition:o.targetPosition??o.data?.targetPosition,e=String(t??"").toLowerCase();return e==="left"||e==="right"||e==="top"||e==="bottom"?e:s==="source"?"right":"left"}getHandlePoint(o,s){let{x:t,y:e,w:n,h:a}=o,b=t+n/2,c=e+a/2;switch(s){case"left":return{x:t,y:c};case"right":return{x:t+n,y:c};case"top":return{x:b,y:e};case"bottom":return{x:b,y:e+a};default:return{x:t+n,y:c}}}snapToGrid(o,s,t){return t<=0?{x:o,y:s}:{x:Math.round(o/t)*t,y:Math.round(s/t)*t}}polylineToPath(o,s,t={}){if(o<2)return"";let e=t.gridSize??0,n=Math.max(0,t.cornerRadius??0),a=i=>{let u=s(i);return e>0?this.snapToGrid(u.x,u.y,e):u};if(n<=0){let i=`M ${a(0).x} ${a(0).y}`;for(let u=1;u<o;u++){let d=a(u);i+=` L ${d.x} ${d.y}`}return i}let b=(i,u)=>Math.hypot(u.x-i.x,u.y-i.y),c=(i,u)=>{let d=b(i,u);return d<1e-6?{x:0,y:0}:{x:(u.x-i.x)/d,y:(u.y-i.y)/d}},p=`M ${a(0).x} ${a(0).y}`;for(let i=1;i<o-1;i++){let u=a(i-1),d=a(i),w=a(i+1),R=c(d,u),A=c(d,w),k=b(d,u),T=b(d,w),P=Math.min(n,k/2,T/2),E=Math.min(n,k/2,T/2),l={x:d.x+R.x*P,y:d.y+R.y*P},h={x:d.x+A.x*E,y:d.y+A.y*E};p+=` L ${l.x} ${l.y} Q ${d.x} ${d.y} ${h.x} ${h.y}`}let g=a(o-1);return p+=` L ${g.x} ${g.y}`,p}cleanup(o,s,t){try{for(let{connRef:e}of s)o.deleteConnector(e);for(let{ref:e}of t)o.deleteShape(e)}catch{}}};F(y,"lib",null),F(y,"instance",null);var H=y;async function oe(){return H.load()}function te(m,o,s){try{return H.getInstance().routeAll(m,o,s)}catch{return{}}}import{useCallback as _,useEffect as G,useRef as I}from"react";import{useCallback as q,useEffect as ne,useRef as X,useState as re}from"react";function Y(m,o={}){let{onRouted:s,onLoaded:t}=o,e=M.getState().setLoaded,n=M.getState().setRoutes,a=b=>{let c=b.data;if(!(!c||typeof c!="object"||!("command"in c)))switch(c.command){case"loaded":e(c.success),t?.(c.success);break;case"routed":n(c.routes),s?.(c.routes);break;default:break}};return m.addEventListener("message",a),()=>m.removeEventListener("message",a)}function j(m){let o=X(null),[s,t]=re(!1),e=X(m?.onRouted),n=X(m?.onLoaded);e.current=m?.onRouted,n.current=m?.onLoaded;let a=m?.create!==!1;ne(()=>{if(!a){o.current=null,t(!1);return}let p;try{p=new Worker(new URL("./workers/avoid-router.worker.js",import.meta.url),{type:"module"})}catch(i){console.error("[avoid-worker] Failed to create worker:",i);return}o.current=p,p.addEventListener("error",i=>{console.error("[avoid-worker] Worker error:",i.message)});let g=Y(p,{onRouted:i=>e.current?.(i),onLoaded:i=>{t(i),n.current?.(i)}});return()=>{g(),p.postMessage({command:"close"}),p.terminate(),o.current=null,t(!1)}},[a]);let b=q(p=>{o.current&&o.current.postMessage(p)},[]),c=q(()=>{o.current&&(o.current.postMessage({command:"close"}),o.current.terminate(),o.current=null,t(!1))},[]);return{workerLoaded:s,post:b,close:c}}var V={edgeToEdgeSpacing:10,edgeToNodeSpacing:8};function se(m){return{idealNudgingDistance:m?.edgeToEdgeSpacing??V.edgeToEdgeSpacing,shapeBufferDistance:m?.edgeToNodeSpacing??V.edgeToNodeSpacing,edgeRounding:m?.edgeRounding,diagramGridSize:m?.diagramGridSize,shouldSplitEdgesNearHandle:m?.shouldSplitEdgesNearHandle}}function ie(m,o,s){let t=I(m),e=I(o),n=se(s),a=I(n);G(()=>{t.current=m},[m]),G(()=>{e.current=o},[o]),G(()=>{a.current=n});let b=M(l=>l.setRoutes),c=z(l=>l.setActions),{post:p,workerLoaded:g}=j({create:!0}),i=I(!1),u=I(!1),d=_(()=>{if(!g)return;let l=t.current;if(!(l.length===0||l.some(r=>r.measured?.width!=null)))return;u.current=!0;let x=e.current.filter(r=>r.type==="avoidNodes");if(x.length===0){b({});return}p({command:"reset",nodes:l,edges:x,options:a.current}),i.current=!0},[p,b,g]),w=_(l=>{if(!g||!i.current)return;let h=new Map(t.current.map(r=>[r.id,r])),x=l.map(r=>h.get(r)).filter(r=>r!=null);x.length!==0&&p({command:"updateNodes",nodes:x})},[p,g]),R=I(null),A=I(new Set),k=_(()=>{d()},[d]),T=_(()=>{d()},[d]),P=_(l=>{w(l)},[w]),E=_(l=>{if(!g)return;let h=!1,x=!1,r=!1;for(let f of l)f.type==="position"?(h=!0,A.current.add(f.id)):f.type==="dimensions"?(x=!0,A.current.add(f.id)):(f.type==="add"||f.type==="remove")&&(r=!0);if(!h&&!x&&!r)return;if(r||x&&!u.current){R.current&&(clearTimeout(R.current),R.current=null),A.current.clear(),R.current=setTimeout(()=>{R.current=null,requestAnimationFrame(()=>d())},0);return}i.current&&(R.current&&clearTimeout(R.current),R.current=setTimeout(()=>{R.current=null,requestAnimationFrame(()=>{let f=Array.from(A.current);A.current.clear(),f.length>0&&w(f)})},0))},[g,d,w]);return G(()=>(c({resetRouting:k,updateRoutesForNodeId:l=>P([l])}),()=>c({resetRouting:()=>{},updateRoutesForNodeId:()=>{}})),[k,P,c]),G(()=>{g&&d()},[g,m.length,o.length,n.shapeBufferDistance,n.idealNudgingDistance,n.edgeRounding,n.diagramGridSize,n.shouldSplitEdgesNearHandle,d]),{updateRoutingOnNodesChange:E,resetRouting:k,refreshRouting:T,updateRoutingForNodeIds:P}}export{H as AvoidRouter,$ as DEBOUNCE_ROUTING_MS,K as DEV_LOG_WEB_WORKER_MESSAGES,J as EDGE_BORDER_RADIUS,Q as SHOULD_START_EDGE_AT_HANDLE_BORDER,Y as attachAvoidWorkerListener,oe as loadAvoidRouter,te as routeAll,Z as useAvoidNodesPath,ie as useAvoidNodesRouterFromWorker,z as useAvoidRouterActionsStore,M as useAvoidRoutesStore,j as useAvoidWorker};
1
+ import{a as z,b as B,c as Y,d as Z,e as F,f as ee,g as oe,h as te}from"./chunk-2USZC2VI.js";var ne="/libavoid.wasm";var y=class y{static async load(o=ne){if(y.lib!=null)return!0;if(typeof globalThis>"u")return!1;for(let i=1;i<=5;i++){if(await y.loadOnce(o))return!0;i<5&&await new Promise(e=>setTimeout(e,2e3))}return!1}static async loadOnce(o){let i=globalThis.location?.origin,t=i&&o.startsWith("/")?`${i}${o}`:o;try{let e=await import("libavoid-js"),s=e.AvoidLib??e.default;if(!s?.load)return!1;await s.load(t);let u=s.getInstance?.();return u==null?!1:(y.lib=u,!0)}catch{return!1}}static getInstance(){if(y.instance==null&&(y.instance=new y),y.lib==null)throw new Error("AvoidRouter.load() must be called first.");return y.instance}routeAll(o,i,t){let e=y.lib;if(!e)return{};let s=t?.shapeBufferDistance??8,u=t?.idealNudgingDistance??10,h=t?.edgeRounding??0,d=t?.diagramGridSize??0,p=t?.shouldSplitEdgesNearHandle??!1,R=o.filter(g=>g.type!=="group"),n=new Map(o.map(g=>[g.id,g])),c=new Map(R.map(g=>[g.id,this.getNodeBoundsAbsolute(g,n)])),r=new e.Router(e.OrthogonalRouting);r.setRoutingParameter(e.shapeBufferDistance,s),r.setRoutingParameter(e.idealNudgingDistance,u),r.setRoutingOption(e.nudgeOrthogonalSegmentsConnectedToShapes,!0),r.setRoutingOption(e.nudgeSharedPathsWithCommonEndPoint,!0),r.setRoutingOption(e.performUnifyingNudgingPreprocessingStep,!0);let A=1,b=2,x=3,P=4,T=5,E={top:b,bottom:x,left:P,right:T},U={[A]:{x:.5,y:.5,dir:e.ConnDirAll},[b]:{x:.5,y:0,dir:e.ConnDirUp},[x]:{x:.5,y:1,dir:e.ConnDirDown},[P]:{x:0,y:.5,dir:e.ConnDirLeft},[T]:{x:1,y:.5,dir:e.ConnDirRight}},l=new Map,w=[];for(let g of R){let a=c.get(g.id),O=new e.Point(a.x,a.y),M=new e.Point(a.x+a.w,a.y+a.h),S=new e.Rectangle(O,M),C=new e.ShapeRef(r,S);w.push({ref:C}),l.set(g.id,C);for(let W of[A,b,x,P,T]){let N=U[W];new e.ShapeConnectionPin(C,W,N.x,N.y,!0,0,N.dir).setExclusive(!1)}}let v=[];for(let g of i){let a=n.get(g.source),O=n.get(g.target);if(!a||!O)continue;let M=l.get(g.source),S=l.get(g.target),C=this.getHandlePosition(a,"source"),W=this.getHandlePosition(O,"target"),N,I;if(p){if(M){let k=E[C]??A;N=new e.ConnEnd(M,k)}else{let k=this.getNodeBoundsAbsolute(a,n),D=this.getHandlePoint(k,C);N=new e.ConnEnd(new e.Point(D.x,D.y))}if(S){let k=E[W]??A;I=new e.ConnEnd(S,k)}else{let k=this.getNodeBoundsAbsolute(O,n),D=this.getHandlePoint(k,W);I=new e.ConnEnd(new e.Point(D.x,D.y))}}else{let k=this.getNodeBoundsAbsolute(a,n),D=this.getHandlePoint(k,C);N=new e.ConnEnd(new e.Point(D.x,D.y));let J=this.getNodeBoundsAbsolute(O,n),V=this.getHandlePoint(J,W);I=new e.ConnEnd(new e.Point(V.x,V.y))}let L=new e.ConnRef(r,N,I);L.setRoutingType(e.ConnType_Orthogonal),v.push({edgeId:g.id,connRef:L})}try{r.processTransaction()}catch{return this.cleanup(r,v,w),{}}let f={};for(let{edgeId:g,connRef:a}of v)try{let O=a.displayRoute(),M=O.size();if(M<2)continue;let S=[];for(let L=0;L<M;L++){let k=O.get_ps(L);S.push({x:k.x,y:k.y})}let C=this.polylineToPath(S.length,L=>S[L],{gridSize:d||void 0,cornerRadius:h}),W=Math.floor(S.length/2),N=S[W],I=d>0?this.snapToGrid(N.x,N.y,d):N;f[g]={path:C,labelX:I.x,labelY:I.y}}catch{}return this.cleanup(r,v,w),f}getNodeBounds(o){let i=o.position?.x??0,t=o.position?.y??0,e=Number(o.measured?.width??o.width??o.style?.width??150),s=Number(o.measured?.height??o.height??o.style?.height??50);return{x:i,y:t,w:e,h:s}}getNodeBoundsAbsolute(o,i){let t=this.getNodeBounds(o),e=o;for(;e?.parentId;){let s=i.get(e.parentId);if(!s)break;t.x+=s.position?.x??0,t.y+=s.position?.y??0,e=s}return t}getHandlePosition(o,i){let t=i==="source"?o.sourcePosition??o.data?.sourcePosition:o.targetPosition??o.data?.targetPosition,e=String(t??"").toLowerCase();return e==="left"||e==="right"||e==="top"||e==="bottom"?e:i==="source"?"right":"left"}getHandlePoint(o,i){let{x:t,y:e,w:s,h:u}=o,h=t+s/2,d=e+u/2;switch(i){case"left":return{x:t,y:d};case"right":return{x:t+s,y:d};case"top":return{x:h,y:e};case"bottom":return{x:h,y:e+u};default:return{x:t+s,y:d}}}snapToGrid(o,i,t){return t<=0?{x:o,y:i}:{x:Math.round(o/t)*t,y:Math.round(i/t)*t}}polylineToPath(o,i,t={}){if(o<2)return"";let e=t.gridSize??0,s=Math.max(0,t.cornerRadius??0),u=n=>{let c=i(n);return e>0?this.snapToGrid(c.x,c.y,e):c};if(s<=0){let n=`M ${u(0).x} ${u(0).y}`;for(let c=1;c<o;c++){let r=u(c);n+=` L ${r.x} ${r.y}`}return n}let h=(n,c)=>Math.hypot(c.x-n.x,c.y-n.y),d=(n,c)=>{let r=h(n,c);return r<1e-6?{x:0,y:0}:{x:(c.x-n.x)/r,y:(c.y-n.y)/r}},p=`M ${u(0).x} ${u(0).y}`;for(let n=1;n<o-1;n++){let c=u(n-1),r=u(n),A=u(n+1),b=d(r,c),x=d(r,A),P=h(r,c),T=h(r,A),E=Math.min(s,P/2,T/2),U=Math.min(s,P/2,T/2),l={x:r.x+b.x*E,y:r.y+b.y*E},w={x:r.x+x.x*U,y:r.y+x.y*U};p+=` L ${l.x} ${l.y} Q ${r.x} ${r.y} ${w.x} ${w.y}`}let R=u(o-1);return p+=` L ${R.x} ${R.y}`,p}cleanup(o,i,t){try{for(let{connRef:e}of i)o.deleteConnector(e);for(let{ref:e}of t)o.deleteShape(e)}catch{}}};z(y,"lib",null),z(y,"instance",null);var G=y;async function re(){return G.load()}function se(m,o,i){try{return G.getInstance().routeAll(m,o,i)}catch{return{}}}import{useCallback as H,useEffect as $,useRef as _}from"react";import{useCallback as K,useEffect as ie,useRef as j,useState as ue}from"react";function X(m,o={}){let{onRouted:i,onLoaded:t}=o,e=B.getState().setLoaded,s=B.getState().setRoutes,u=h=>{let d=h.data;if(!(!d||typeof d!="object"||!("command"in d)))switch(d.command){case"loaded":e(d.success),t?.(d.success);break;case"routed":s(d.routes),i?.(d.routes);break;default:break}};return m.addEventListener("message",u),()=>m.removeEventListener("message",u)}function q(m){let o=j(null),[i,t]=ue(!1),e=j(m?.onRouted),s=j(m?.onLoaded);e.current=m?.onRouted,s.current=m?.onLoaded;let u=m?.create!==!1;ie(()=>{if(!u){o.current=null,t(!1);return}let p;try{p=new Worker(new URL("./workers/avoid-router.worker.js",import.meta.url),{type:"module"})}catch(n){console.error("[avoid-worker] Failed to create worker:",n);return}o.current=p,p.addEventListener("error",n=>{console.error("[avoid-worker] Worker error:",n.message)});let R=X(p,{onRouted:n=>e.current?.(n),onLoaded:n=>{t(n),s.current?.(n)}});return()=>{R(),p.postMessage({command:"close"}),p.terminate(),o.current=null,t(!1)}},[u]);let h=K(p=>{o.current&&o.current.postMessage(p)},[]),d=K(()=>{o.current&&(o.current.postMessage({command:"close"}),o.current.terminate(),o.current=null,t(!1))},[]);return{workerLoaded:i,post:h,close:d}}var Q={edgeToEdgeSpacing:10,edgeToNodeSpacing:8};function de(m){return{idealNudgingDistance:m?.edgeToEdgeSpacing??Q.edgeToEdgeSpacing,shapeBufferDistance:m?.edgeToNodeSpacing??Q.edgeToNodeSpacing,edgeRounding:m?.edgeRounding,diagramGridSize:m?.diagramGridSize,shouldSplitEdgesNearHandle:m?.shouldSplitEdgesNearHandle}}function ae(m,o,i){let t=_(m),e=_(o),s=de(i),u=_(s);$(()=>{t.current=m},[m]),$(()=>{e.current=o},[o]),$(()=>{u.current=s});let h=B(l=>l.setRoutes),d=Y(l=>l.setActions),{post:p,workerLoaded:R}=q({create:!0}),n=_(!1),c=_(!1),r=H(()=>{if(!R)return;let l=t.current;if(!(l.length===0||l.some(f=>f.measured?.width!=null)))return;c.current=!0;let v=e.current.filter(f=>f.type==="avoidNodes");if(v.length===0){h({});return}p({command:"reset",nodes:l,edges:v,options:u.current}),n.current=!0},[p,h,R]),A=H(l=>{if(!R||!n.current)return;let w=new Map(t.current.map(f=>[f.id,f])),v=l.map(f=>w.get(f)).filter(f=>f!=null);v.length!==0&&p({command:"updateNodes",nodes:v})},[p,R]),b=_(null),x=_(new Set),P=H(()=>{r()},[r]),T=H(()=>{r()},[r]),E=H(l=>{A(l)},[A]),U=H(l=>{if(!R)return;let w=!1,v=!1,f=!1;for(let a of l)a.type==="position"?(w=!0,x.current.add(a.id)):a.type==="dimensions"?(v=!0,x.current.add(a.id)):(a.type==="add"||a.type==="remove")&&(f=!0);if(!w&&!v&&!f)return;if(f||v&&!c.current){b.current&&(clearTimeout(b.current),b.current=null),x.current.clear(),b.current=setTimeout(()=>{b.current=null,requestAnimationFrame(()=>r())},0);return}n.current&&(b.current&&clearTimeout(b.current),b.current=setTimeout(()=>{b.current=null,requestAnimationFrame(()=>{let a=Array.from(x.current);x.current.clear(),a.length>0&&A(a)})},0))},[R,r,A]);return $(()=>(d({resetRouting:P,updateRoutesForNodeId:l=>E([l])}),()=>d({resetRouting:()=>{},updateRoutesForNodeId:()=>{}})),[P,E,d]),$(()=>{R&&r()},[R,m.length,o.length,s.shapeBufferDistance,s.idealNudgingDistance,s.edgeRounding,s.diagramGridSize,s.shouldSplitEdgesNearHandle,r]),{updateRoutingOnNodesChange:U,resetRouting:P,refreshRouting:T,updateRoutingForNodeIds:E}}export{G as AvoidRouter,F as DEBOUNCE_ROUTING_MS,Z as DEV_LOG_WEB_WORKER_MESSAGES,oe as EDGE_BORDER_RADIUS,ee as SHOULD_START_EDGE_AT_HANDLE_BORDER,X as attachAvoidWorkerListener,re as loadAvoidRouter,se as routeAll,te as useAvoidNodesPath,ae as useAvoidNodesRouterFromWorker,Y as useAvoidRouterActionsStore,B as useAvoidRoutesStore,q as useAvoidWorker};
@@ -1 +1 @@
1
- typeof window>"u"&&typeof self<"u"&&(self.window=self);var $="/libavoid.wasm",J=2e3,z=5,W=null;async function K(){if(W!=null)return!0;for(let t=1;t<=z;t++){let o=globalThis.location?.origin,e=o&&$.startsWith("/")?`${o}${$}`:$;try{let n=await import("libavoid-js"),r=n.AvoidLib??n.default;if(!r?.load)return!1;await r.load(e);let u=r.getInstance?.();return u==null?!1:(W=u,!0)}catch{t<z&&await new Promise(n=>setTimeout(n,J))}}return!1}function Z(t){let o=t.position?.x??0,e=t.position?.y??0,n=Number(t.measured?.width??t.width??t.style?.width??150),r=Number(t.measured?.height??t.height??t.style?.height??50);return{x:o,y:e,w:n,h:r}}function B(t,o){let e=Z(t),n=t;for(;n?.parentId;){let r=o.get(n.parentId);if(!r)break;e.x+=r.position?.x??0,e.y+=r.position?.y??0,n=r}return e}function G(t,o){let e=o==="source"?t.sourcePosition??t.data?.sourcePosition:t.targetPosition??t.data?.targetPosition,n=String(e??"").toLowerCase();return n==="left"||n==="right"||n==="top"||n==="bottom"?n:o==="source"?"right":"left"}function Y(t,o){let{x:e,y:n,w:r,h:u}=t,h=e+r/2,m=n+u/2;switch(o){case"left":return{x:e,y:m};case"right":return{x:e+r,y:m};case"top":return{x:h,y:n};case"bottom":return{x:h,y:n+u};default:return{x:e+r,y:m}}}function j(t,o,e){return e<=0?{x:t,y:o}:{x:Math.round(t/e)*e,y:Math.round(o/e)*e}}function nn(t,o,e={}){if(t<2)return"";let n=e.gridSize??0,r=Math.max(0,e.cornerRadius??0),u=a=>{let s=o(a);return n>0?j(s.x,s.y,n):s};if(r<=0){let a=`M ${u(0).x} ${u(0).y}`;for(let s=1;s<t;s++){let d=u(s);a+=` L ${d.x} ${d.y}`}return a}let h=(a,s)=>Math.hypot(s.x-a.x,s.y-a.y),m=(a,s)=>{let d=h(a,s);return d<1e-6?{x:0,y:0}:{x:(s.x-a.x)/d,y:(s.y-a.y)/d}},P=`M ${u(0).x} ${u(0).y}`;for(let a=1;a<t-1;a++){let s=u(a-1),d=u(a),N=u(a+1),v=m(d,s),k=m(d,N),C=h(d,s),O=h(d,N),L=Math.min(r,C/2,O/2),E=Math.min(r,C/2,O/2),I={x:d.x+v.x*L,y:d.y+v.y*L},R={x:d.x+k.x*E,y:d.y+k.y*E};P+=` L ${I.x} ${I.y} Q ${d.x} ${d.y} ${R.x} ${R.y}`}let y=u(t-1);return P+=` L ${y.x} ${y.y}`,P}function Q(t,o,e){let n=W;if(!n)return{};let r=e?.shapeBufferDistance??8,u=e?.idealNudgingDistance??10,h=e?.edgeRounding??0,m=e?.diagramGridSize??0,P=t.filter(i=>i.type!=="group"),y=new Map(t.map(i=>[i.id,i])),a=new Map(P.map(i=>[i.id,B(i,y)])),s=new n.Router(n.OrthogonalRouting);s.setRoutingParameter(n.shapeBufferDistance,r),s.setRoutingParameter(n.idealNudgingDistance,u),s.setRoutingOption(n.nudgeOrthogonalSegmentsConnectedToShapes,!0),s.setRoutingOption(n.nudgeSharedPathsWithCommonEndPoint,!0),s.setRoutingOption(n.performUnifyingNudgingPreprocessingStep,!0);let d=1,N=2,v=3,k=4,C=5,O={top:N,bottom:v,left:k,right:C},L={[d]:{x:.5,y:.5,dir:n.ConnDirAll},[N]:{x:.5,y:0,dir:n.ConnDirUp},[v]:{x:.5,y:1,dir:n.ConnDirDown},[k]:{x:0,y:.5,dir:n.ConnDirLeft},[C]:{x:1,y:.5,dir:n.ConnDirRight}},E=new Map,I=[];for(let i of P){let l=a.get(i.id),b=new n.Point(l.x,l.y),w=new n.Point(l.x+l.w,l.y+l.h),A=new n.Rectangle(b,w),x=new n.ShapeRef(s,A);I.push({ref:x}),E.set(i.id,x);for(let g of[d,N,v,k,C]){let f=L[g];new n.ShapeConnectionPin(x,g,f.x,f.y,!0,0,f.dir).setExclusive(!1)}}let R=[];for(let i of o){let l=y.get(i.source),b=y.get(i.target);if(!l||!b)continue;let w=E.get(i.source),A=E.get(i.target),x=G(l,"source"),g=G(b,"target"),f,M;if(w)f=new n.ConnEnd(w,O[x]??d);else{let _=B(l,y),T=Y(_,x);f=new n.ConnEnd(new n.Point(T.x,T.y))}if(A)M=new n.ConnEnd(A,O[g]??d);else{let _=B(b,y),T=Y(_,g);M=new n.ConnEnd(new n.Point(T.x,T.y))}let F=new n.ConnRef(s,f,M);F.setRoutingType(n.ConnType_Orthogonal),R.push({edgeId:i.id,connRef:F})}try{s.processTransaction()}catch{for(let{connRef:i}of R)try{s.deleteConnector(i)}catch{}for(let{ref:i}of I)try{s.deleteShape(i)}catch{}return{}}let U={};for(let{edgeId:i,connRef:l}of R)try{let b=l.displayRoute(),w=b.size();if(w<2)continue;let A=nn(w,M=>{let F=b.get_ps(M);return{x:F.x,y:F.y}},{gridSize:m||void 0,cornerRadius:h}),x=Math.floor(w/2),g=b.get_ps(x),f=m>0?j(g.x,g.y,m):{x:g.x,y:g.y};U[i]={path:A,labelX:f.x,labelY:f.y}}catch{}for(let{connRef:i}of R)try{s.deleteConnector(i)}catch{}for(let{ref:i}of I)try{s.deleteShape(i)}catch{}return U}var V=K();V.then(t=>{postMessage({command:"loaded",success:t})}).catch(()=>{postMessage({command:"loaded",success:!1})});var c=[],p=[],H={};function X(t){return"position"in t&&("width"in t||"measured"in t||!("source"in t))}function en(){let t=p.filter(o=>o.type==="avoidNodes");if(t.length===0)return{};try{return Q(c,t,H)}catch{return{}}}var tn=0,S=null;function on(){return S!=null}function q(){S!=null&&(clearTimeout(S),S=null)}function D(){q(),S=setTimeout(()=>{S=null;let t=en();setTimeout(()=>{on()||postMessage({command:"routed",routes:t})},0)},tn)}onmessage=async t=>{await V;let o=t.data;if(!(!o||typeof o!="object"||!("command"in o)))switch(o.command){case"reset":c=o.nodes??[],p=o.edges??[],o.options&&(H=o.options),D();break;case"change":{let e=o.cell;if(X(e)){let n=c.findIndex(r=>r.id===e.id);n>=0?c[n]={...c[n],...e}:c.push(e)}else{let n=p.findIndex(r=>r.id===e.id);n>=0?p[n]={...p[n],...e}:p.push(e)}D();break}case"remove":{let e=o.id;c=c.filter(n=>n.id!==e),p=p.filter(n=>n.id!==e),D();break}case"add":{let e=o.cell;X(e)?c.some(n=>n.id===e.id)||c.push(e):p.some(n=>n.id===e.id)||p.push(e),D();break}case"updateNodes":{let e=o.nodes??[];for(let n of e){let r=c.findIndex(u=>u.id===n.id);r>=0?c[r]={...c[r],...n}:c.push(n)}D();break}case"route":{let e=o.nodes??[],n=(o.edges??[]).filter(u=>u.type==="avoidNodes"),r=o.options??H;if(n.length===0){postMessage({command:"routed",routes:{}});break}try{let u=Q(e,n,r);postMessage({command:"routed",routes:u})}catch{postMessage({command:"routed",routes:{}})}break}case"close":q(),self.close();break;default:break}};
1
+ typeof window>"u"&&typeof self<"u"&&(self.window=self);var W="/libavoid.wasm",nn=2e3,X=5,U=null;async function en(){if(U!=null)return!0;for(let t=1;t<=X;t++){let o=globalThis.location?.origin,e=o&&W.startsWith("/")?`${o}${W}`:W;try{let n=await import("libavoid-js"),r=n.AvoidLib??n.default;if(!r?.load)return!1;await r.load(e);let d=r.getInstance?.();return d==null?!1:(U=d,!0)}catch{t<X&&await new Promise(n=>setTimeout(n,nn))}}return!1}function tn(t){let o=t.position?.x??0,e=t.position?.y??0,n=Number(t.measured?.width??t.width??t.style?.width??150),r=Number(t.measured?.height??t.height??t.style?.height??50);return{x:o,y:e,w:n,h:r}}function L(t,o){let e=tn(t),n=t;for(;n?.parentId;){let r=o.get(n.parentId);if(!r)break;e.x+=r.position?.x??0,e.y+=r.position?.y??0,n=r}return e}function j(t,o){let e=o==="source"?t.sourcePosition??t.data?.sourcePosition:t.targetPosition??t.data?.targetPosition,n=String(e??"").toLowerCase();return n==="left"||n==="right"||n==="top"||n==="bottom"?n:o==="source"?"right":"left"}function H(t,o){let{x:e,y:n,w:r,h:d}=t,y=e+r/2,g=n+d/2;switch(o){case"left":return{x:e,y:g};case"right":return{x:e+r,y:g};case"top":return{x:y,y:n};case"bottom":return{x:y,y:n+d};default:return{x:e+r,y:g}}}function V(t,o,e){return e<=0?{x:t,y:o}:{x:Math.round(t/e)*e,y:Math.round(o/e)*e}}function on(t,o,e={}){if(t<2)return"";let n=e.gridSize??0,r=Math.max(0,e.cornerRadius??0),d=u=>{let a=o(u);return n>0?V(a.x,a.y,n):a};if(r<=0){let u=`M ${d(0).x} ${d(0).y}`;for(let a=1;a<t;a++){let s=d(a);u+=` L ${s.x} ${s.y}`}return u}let y=(u,a)=>Math.hypot(a.x-u.x,a.y-u.y),g=(u,a)=>{let s=y(u,a);return s<1e-6?{x:0,y:0}:{x:(a.x-u.x)/s,y:(a.y-u.y)/s}},F=`M ${d(0).x} ${d(0).y}`;for(let u=1;u<t-1;u++){let a=d(u-1),s=d(u),v=d(u+1),E=g(s,a),C=g(s,v),I=y(s,a),A=y(s,v),D=Math.min(r,I/2,A/2),$=Math.min(r,I/2,A/2),S={x:s.x+E.x*D,y:s.y+E.y*D},M={x:s.x+C.x*$,y:s.y+C.y*$};F+=` L ${S.x} ${S.y} Q ${s.x} ${s.y} ${M.x} ${M.y}`}let T=d(t-1);return F+=` L ${T.x} ${T.y}`,F}function q(t,o,e){let n=U;if(!n)return{};let r=e?.shapeBufferDistance??8,d=e?.idealNudgingDistance??10,y=e?.edgeRounding??0,g=e?.diagramGridSize??0,F=e?.shouldSplitEdgesNearHandle??!1,T=t.filter(i=>i.type!=="group"),u=new Map(t.map(i=>[i.id,i])),a=new Map(T.map(i=>[i.id,L(i,u)])),s=new n.Router(n.OrthogonalRouting);s.setRoutingParameter(n.shapeBufferDistance,r),s.setRoutingParameter(n.idealNudgingDistance,d),s.setRoutingOption(n.nudgeOrthogonalSegmentsConnectedToShapes,!0),s.setRoutingOption(n.nudgeSharedPathsWithCommonEndPoint,!0),s.setRoutingOption(n.performUnifyingNudgingPreprocessingStep,!0);let v=1,E=2,C=3,I=4,A=5,D={top:E,bottom:C,left:I,right:A},$={[v]:{x:.5,y:.5,dir:n.ConnDirAll},[E]:{x:.5,y:0,dir:n.ConnDirUp},[C]:{x:.5,y:1,dir:n.ConnDirDown},[I]:{x:0,y:.5,dir:n.ConnDirLeft},[A]:{x:1,y:.5,dir:n.ConnDirRight}},S=new Map,M=[];for(let i of T){let l=a.get(i.id),p=new n.Point(l.x,l.y),k=new n.Point(l.x+l.w,l.y+l.h),f=new n.Rectangle(p,k),h=new n.ShapeRef(s,f);M.push({ref:h}),S.set(i.id,h);for(let w of[v,E,C,I,A]){let m=$[w];new n.ShapeConnectionPin(h,w,m.x,m.y,!0,0,m.dir).setExclusive(!1)}}let B=[];for(let i of o){let l=u.get(i.source),p=u.get(i.target);if(!l||!p)continue;let k=S.get(i.source),f=S.get(i.target),h=j(l,"source"),w=j(p,"target"),m,x;if(F){if(k)m=new n.ConnEnd(k,D[h]??v);else{let P=L(l,u),N=H(P,h);m=new n.ConnEnd(new n.Point(N.x,N.y))}if(f)x=new n.ConnEnd(f,D[w]??v);else{let P=L(p,u),N=H(P,w);x=new n.ConnEnd(new n.Point(N.x,N.y))}}else{let P=L(l,u),N=H(P,h);m=new n.ConnEnd(new n.Point(N.x,N.y));let Z=L(p,u),Y=H(Z,w);x=new n.ConnEnd(new n.Point(Y.x,Y.y))}let R=new n.ConnRef(s,m,x);R.setRoutingType(n.ConnType_Orthogonal),B.push({edgeId:i.id,connRef:R})}try{s.processTransaction()}catch{for(let{connRef:i}of B)try{s.deleteConnector(i)}catch{}for(let{ref:i}of M)try{s.deleteShape(i)}catch{}return{}}let G={};for(let{edgeId:i,connRef:l}of B)try{let p=l.displayRoute(),k=p.size();if(k<2)continue;let f=[];for(let R=0;R<k;R++){let P=p.get_ps(R);f.push({x:P.x,y:P.y})}let h=on(f.length,R=>f[R],{gridSize:g||void 0,cornerRadius:y}),w=Math.floor(f.length/2),m=f[w],x=g>0?V(m.x,m.y,g):m;G[i]={path:h,labelX:x.x,labelY:x.y}}catch{}for(let{connRef:i}of B)try{s.deleteConnector(i)}catch{}for(let{ref:i}of M)try{s.deleteShape(i)}catch{}return G}var J=en();J.then(t=>{postMessage({command:"loaded",success:t})}).catch(()=>{postMessage({command:"loaded",success:!1})});var c=[],b=[],z={};function Q(t){return"position"in t&&("width"in t||"measured"in t||!("source"in t))}function rn(){let t=b.filter(o=>o.type==="avoidNodes");if(t.length===0)return{};try{return q(c,t,z)}catch{return{}}}var sn=0,O=null;function un(){return O!=null}function K(){O!=null&&(clearTimeout(O),O=null)}function _(){K(),O=setTimeout(()=>{O=null;let t=rn();setTimeout(()=>{un()||postMessage({command:"routed",routes:t})},0)},sn)}onmessage=async t=>{await J;let o=t.data;if(!(!o||typeof o!="object"||!("command"in o)))switch(o.command){case"reset":c=o.nodes??[],b=o.edges??[],o.options&&(z=o.options),_();break;case"change":{let e=o.cell;if(Q(e)){let n=c.findIndex(r=>r.id===e.id);n>=0?c[n]={...c[n],...e}:c.push(e)}else{let n=b.findIndex(r=>r.id===e.id);n>=0?b[n]={...b[n],...e}:b.push(e)}_();break}case"remove":{let e=o.id;c=c.filter(n=>n.id!==e),b=b.filter(n=>n.id!==e),_();break}case"add":{let e=o.cell;Q(e)?c.some(n=>n.id===e.id)||c.push(e):b.some(n=>n.id===e.id)||b.push(e),_();break}case"updateNodes":{let e=o.nodes??[];for(let n of e){let r=c.findIndex(d=>d.id===n.id);r>=0?c[r]={...c[r],...n}:c.push(n)}_();break}case"route":{let e=o.nodes??[],n=(o.edges??[]).filter(d=>d.type==="avoidNodes"),r=o.options??z;if(n.length===0){postMessage({command:"routed",routes:{}});break}try{let d=q(e,n,r);postMessage({command:"routed",routes:d})}catch{postMessage({command:"routed",routes:{}})}break}case"close":K(),self.close();break;default:break}};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "avoid-nodes-edge",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Orthogonal edge routing for React Flow — edges avoid overlapping nodes via libavoid-js WASM running in a Web Worker.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",