@filekit-studio/diagram 1.0.4 → 1.0.6
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 +31 -20
- package/dist/index.d.mts +10 -2
- package/dist/index.d.ts +10 -2
- package/dist/index.js +4 -4
- package/dist/index.mjs +4 -4
- package/dist/styles.css +933 -0
- package/package.json +7 -3
package/README.md
CHANGED
|
@@ -5,13 +5,17 @@
|
|
|
5
5
|
[](https://www.npmjs.com/package/@filekit-studio/diagram)
|
|
6
6
|
[](https://opensource.org/licenses/MIT)
|
|
7
7
|
|
|
8
|
+
[Documentation](https://sketch.filekitstudio.com/docs/diagram) • [Quickstart Guide](https://sketch.filekitstudio.com/docs/diagram/quickstart) • [Live Playground](https://sketch.filekitstudio.com/playground)
|
|
9
|
+
|
|
8
10
|
`@filekit-studio/diagram` lets you generate and render beautiful hand-drawn whiteboards, architecture diagrams, and flowcharts directly from declarative JSON or Diagram-as-Code in React and Next.js.
|
|
9
11
|
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
12
|
+
- **Zero Coordinates Required**: Provide nodes, edges, and clusters — the deterministic DAG layout engine positions and aligns everything automatically.
|
|
13
|
+
- **Sketch Aesthetics**: Powered by `@filekit-studio/sketch`, rendering organic hand-drawn shapes, rough fills, and magnetic connector arrows.
|
|
14
|
+
- **Obstacle Evasion & Corridor Seeking**: Orthogonal elbow lines automatically navigate around shapes without crossing intermediate services.
|
|
15
|
+
- **Submerged Passthrough Tunneling**: Traversed lines automatically turn into subtle dashed X-ray paths underneath shapes.
|
|
16
|
+
- **Directional Flows**: Supports Left-to-Right (`LR`), Top-to-Bottom (`TB`), Right-to-Left (`RL`), and Bottom-to-Top (`BT`).
|
|
17
|
+
- **Excalidraw Native**: Direct conversion from `.excalidraw` schema files and element arrays.
|
|
18
|
+
- **100% Client-Side**: Zero server calls, zero tracking, works fully offline.
|
|
15
19
|
|
|
16
20
|
---
|
|
17
21
|
|
|
@@ -25,12 +29,17 @@ npm install @filekit-studio/diagram @filekit-studio/sketch
|
|
|
25
29
|
pnpm add @filekit-studio/diagram @filekit-studio/sketch
|
|
26
30
|
```
|
|
27
31
|
|
|
28
|
-
|
|
32
|
+
### Required Stylesheet Import
|
|
33
|
+
|
|
34
|
+
Import the self-contained stylesheet in your root layout (`app/layout.tsx` or `pages/_app.tsx`):
|
|
29
35
|
|
|
30
36
|
```tsx
|
|
31
|
-
import '@filekit-studio/
|
|
37
|
+
import '@filekit-studio/diagram/styles.css'
|
|
38
|
+
// or: import '@filekit-studio/sketch/styles.css'
|
|
32
39
|
```
|
|
33
40
|
|
|
41
|
+
> **Zero CSS Leaks**: All styles are strictly scoped under `.fk-sketch-root`. Importing this stylesheet will never conflict with your app's global CSS, fonts, or Tailwind config.
|
|
42
|
+
|
|
34
43
|
---
|
|
35
44
|
|
|
36
45
|
## Quickstart
|
|
@@ -42,7 +51,7 @@ import '@filekit-studio/sketch/styles.css'
|
|
|
42
51
|
|
|
43
52
|
import React from 'react'
|
|
44
53
|
import { Diagram, type DiagramData } from '@filekit-studio/diagram'
|
|
45
|
-
import '@filekit-studio/
|
|
54
|
+
import '@filekit-studio/diagram/styles.css'
|
|
46
55
|
|
|
47
56
|
const architecture: DiagramData = {
|
|
48
57
|
title: 'Cloud Payment Pipeline',
|
|
@@ -95,12 +104,14 @@ const architecture: DiagramData = {
|
|
|
95
104
|
export default function ArchitectureDoc() {
|
|
96
105
|
return (
|
|
97
106
|
<div className="w-full h-[500px] rounded-xl overflow-hidden border">
|
|
107
|
+
{/* Automatically inherits dark/light mode and sets contrast */}
|
|
98
108
|
<Diagram
|
|
99
109
|
data={architecture}
|
|
100
110
|
mode="viewer"
|
|
101
|
-
theme="system"
|
|
102
111
|
autoFit={true}
|
|
103
112
|
showCopyButton={true}
|
|
113
|
+
copyFormat="elements" // 'elements' for 1-click Ctrl+V paste into Whiteboard, 'image' for PNG, or 'json'
|
|
114
|
+
showZoomControls={true}
|
|
104
115
|
/>
|
|
105
116
|
</div>
|
|
106
117
|
)
|
|
@@ -111,15 +122,15 @@ export default function ArchitectureDoc() {
|
|
|
111
122
|
|
|
112
123
|
### 2. Programmatic Compilation (`diagramToSketch`)
|
|
113
124
|
|
|
114
|
-
If you want to compile graph JSON into a `SketchDocument` to save or
|
|
125
|
+
If you want to compile graph JSON into a `SketchDocument` to save, mutate, or export:
|
|
115
126
|
|
|
116
127
|
```tsx
|
|
117
128
|
import { diagramToSketch } from '@filekit-studio/diagram'
|
|
118
129
|
|
|
119
130
|
const sketchDoc = diagramToSketch(architecture, {
|
|
120
131
|
direction: 'LR',
|
|
121
|
-
nodeSpacing:
|
|
122
|
-
rankSpacing:
|
|
132
|
+
nodeSpacing: 80,
|
|
133
|
+
rankSpacing: 220,
|
|
123
134
|
})
|
|
124
135
|
|
|
125
136
|
console.log(sketchDoc.elements) // Array of SketchBoardElement objects
|
|
@@ -151,20 +162,20 @@ export function ExcalidrawViewer() {
|
|
|
151
162
|
| `data` | `DiagramData \| string` | *required* | Diagram specification object or JSON string. |
|
|
152
163
|
| `mode` | `'viewer' \| 'editor'` | `'viewer'` | Display mode: clean doc viewer or full whiteboard studio. |
|
|
153
164
|
| `direction` | `'TB' \| 'BT' \| 'LR' \| 'RL'` | `'LR'` | Flow direction for the DAG auto-layout engine. |
|
|
154
|
-
| `
|
|
155
|
-
| `
|
|
156
|
-
| `canvasBackground` | `string` | `'#f0f4f8'` | Hex background color override. |
|
|
165
|
+
| `theme` | `'light' \| 'dark' \| 'system'` | `'system'` | Color theme mode. Automatically detects active website dark mode. |
|
|
166
|
+
| `canvasBackground` | `string` | `'#f0f4f8'` | Hex background color override (`#121214` in dark mode). |
|
|
157
167
|
| `gridStyle` | `'grid' \| 'dots' \| 'cross' \| 'none'` | `'dots'` | Canvas grid pattern. |
|
|
158
168
|
| `autoFit` | `boolean` | `true` | Scale and center elements to fit container on mount. |
|
|
159
169
|
| `interactive` | `boolean` | `true` | Allow click-and-drag panning and wheel zooming. |
|
|
160
|
-
| `showCopyButton` | `boolean` | `true` | Display 1-click clipboard
|
|
161
|
-
| `
|
|
162
|
-
| `
|
|
163
|
-
| `
|
|
170
|
+
| `showCopyButton` | `boolean` | `true` | Display 1-click clipboard export button. |
|
|
171
|
+
| `copyFormat` | `'elements' \| 'image' \| 'json'` | `'elements'` | `'elements'` for Ctrl+V paste into Sketch whiteboard, `'image'` for PNG, or `'json'`. |
|
|
172
|
+
| `showZoomControls` | `boolean` | `true` | Display minimal floating zoom percentage and `+`/`-`/`Fit` controls. |
|
|
173
|
+
| `layoutOptions` | `DiagramLayoutOptions` | `{}` | Fine-tuning for spacing (`nodeSpacing`, `rankSpacing`), margins, and padding. |
|
|
174
|
+
| `uiConfig` | `SketchUIConfig` | `{}` | Granular toggle flags for all 54 UI controls and menus (editor mode). |
|
|
175
|
+
| `controllerRef` | `Ref<SketchController>` | `undefined` | Ref to imperative controller methods (`zoomToFit`, `zoomIn`, `zoomOut`, etc.). |
|
|
164
176
|
|
|
165
177
|
---
|
|
166
178
|
|
|
167
179
|
## License
|
|
168
180
|
|
|
169
181
|
MIT © [FileKit Studio](https://filekitstudio.com)
|
|
170
|
-
|
package/dist/index.d.mts
CHANGED
|
@@ -398,11 +398,19 @@ interface DiagramProps {
|
|
|
398
398
|
*/
|
|
399
399
|
autoFit?: boolean;
|
|
400
400
|
/**
|
|
401
|
-
* Display 1-click clipboard
|
|
401
|
+
* Display 1-click clipboard copy button in the top-right corner of viewer mode. Default: true.
|
|
402
402
|
*/
|
|
403
403
|
showCopyButton?: boolean;
|
|
404
404
|
/**
|
|
405
|
-
*
|
|
405
|
+
* Format for clipboard copy action:
|
|
406
|
+
* - 'elements': Copies canvas-ready whiteboard elements JSON (`{"filekitSketchBoardElements": true, "elements": [...]}`) so it pastes directly into any Sketch canvas via Ctrl+V.
|
|
407
|
+
* - 'image': Copies raster PNG to clipboard.
|
|
408
|
+
* - 'json': Copies plain elements JSON.
|
|
409
|
+
* Default: 'elements'.
|
|
410
|
+
*/
|
|
411
|
+
copyFormat?: 'elements' | 'image' | 'json';
|
|
412
|
+
/**
|
|
413
|
+
* Display minimal zoom level percentage and +/- controls in the bottom-left corner of viewer mode. Default: true.
|
|
406
414
|
*/
|
|
407
415
|
showZoomControls?: boolean;
|
|
408
416
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -398,11 +398,19 @@ interface DiagramProps {
|
|
|
398
398
|
*/
|
|
399
399
|
autoFit?: boolean;
|
|
400
400
|
/**
|
|
401
|
-
* Display 1-click clipboard
|
|
401
|
+
* Display 1-click clipboard copy button in the top-right corner of viewer mode. Default: true.
|
|
402
402
|
*/
|
|
403
403
|
showCopyButton?: boolean;
|
|
404
404
|
/**
|
|
405
|
-
*
|
|
405
|
+
* Format for clipboard copy action:
|
|
406
|
+
* - 'elements': Copies canvas-ready whiteboard elements JSON (`{"filekitSketchBoardElements": true, "elements": [...]}`) so it pastes directly into any Sketch canvas via Ctrl+V.
|
|
407
|
+
* - 'image': Copies raster PNG to clipboard.
|
|
408
|
+
* - 'json': Copies plain elements JSON.
|
|
409
|
+
* Default: 'elements'.
|
|
410
|
+
*/
|
|
411
|
+
copyFormat?: 'elements' | 'image' | 'json';
|
|
412
|
+
/**
|
|
413
|
+
* Display minimal zoom level percentage and +/- controls in the bottom-left corner of viewer mode. Default: true.
|
|
406
414
|
*/
|
|
407
415
|
showZoomControls?: boolean;
|
|
408
416
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
'use strict';var react=require('react'),sketch=require('@filekit-studio/sketch'),jsxRuntime=require('react/jsx-runtime');function ut(e,
|
|
1
|
+
'use strict';var react=require('react'),sketch=require('@filekit-studio/sketch'),jsxRuntime=require('react/jsx-runtime');function ut(e,a=210,r=90){if(e.width&&e.height)return {width:e.width,height:e.height};let g=(e.description?`${e.label||""}
|
|
2
2
|
${e.description}`:e.label||"").split(`
|
|
3
|
-
`),w=g.reduce((o,s)=>Math.max(o,s.length),0),p=(e.fontSize||14)*.72,a=Math.round(w*p),c=Math.max(i,a+64),y=g.length,d=(e.fontSize||14)*1.45,l=e.badge?14:0,t=Math.max(r,Math.round(y*d+44+l));if(e.shape==="diamond")return {width:e.width??Math.max(Math.round(c*1.65),240),height:e.height??Math.max(Math.round(t*1.55),160)};if(e.shape==="cylinder"||e.shape==="database")return {width:e.width??Math.max(c+32,230),height:e.height??Math.max(t+(e.description?90:75),180)};if(e.shape==="cloud")return {width:e.width??Math.max(Math.round(c*1.45),230),height:e.height??Math.max(Math.round(t*1.4),125)};if(e.shape==="sticky")return {width:e.width??Math.max(c+20,220),height:e.height??Math.max(t+34,140)};if(e.shape==="circle"||e.shape==="ellipse"){let o=Math.max(c*1.25,t*1.25,140);return {width:e.width??o,height:e.height??Math.max(Math.round(t*1.3),110)}}return {width:e.width??Math.max(c+16,210),height:e.height??Math.max(t+12,90)}}function nt(e,i){let r=new Map,m=new Map;e.forEach(c=>{r.set(c,[]),m.set(c,0);}),i.forEach(c=>{r.has(c.from)&&m.has(c.to)&&(r.get(c.from).push(c.to),m.set(c.to,(m.get(c.to)||0)+1));});let g=new Map,w=new Set,p=new Set;function a(c,y){if(p.has(c))return;let d=g.get(c)??0;g.set(c,Math.max(d,y)),p.add(c),w.add(c);let l=r.get(c)||[];for(let t of l)a(t,y+1);p.delete(c);}return e.filter(c=>(m.get(c)||0)===0).forEach(c=>a(c,0)),e.forEach(c=>{w.has(c)||a(c,0);}),g}function ot(e,i){let r={...e.layout,...i},m=r.direction||e.direction||"LR",g=r.nodeSpacing??95,w=r.rankSpacing??220,p=r.padding??90,a=r.groupPadding??56,c=r.clusterSpacing??220,y=r.intraRankSpacing??120,d=r.defaultNodeWidth??210,l=r.defaultNodeHeight??90,t=e.nodes||[],o=e.edges||[],s=e.groups||[];if(t.length===0)return {nodes:[],edges:[],groups:[],bounds:{minX:0,minY:0,maxX:0,maxY:0,width:0,height:0}};let f=t.map(b=>{let k=ut(b,d,l);return {...b,width:k.width,height:k.height,x:b.x??0,y:b.y??0,relX:0,relY:0}}),u=new Map;f.forEach(b=>u.set(b.id,b));let n=m==="LR"||m==="RL",h=m==="RL"||m==="BT",P=36;if(!(s.length>0)){let b=nt(t.map(x=>x.id),o),k=new Map;f.forEach(x=>{let D=b.get(x.id)??0;k.has(D)||k.set(D,[]),k.get(D).push(x);});let W=Array.from(k.keys()).sort((x,D)=>x-D),S=p,X=0;return W.forEach(x=>{let $=k.get(x).reduce((O,_,z)=>O+(n?_.height:_.width)+(z>0?g:0),0);X=Math.max(X,$);}),W.forEach(x=>{let D=k.get(x),$=0,O=D.reduce((z,J,K)=>z+(n?J.height:J.width)+(K>0?g:0),0),_=p+(X-O)/2;D.forEach(z=>{$=Math.max($,n?z.width:z.height),n?(z.x=Math.round(S),z.y=Math.round(_),_+=z.height+g):(z.x=Math.round(_),z.y=Math.round(S),_+=z.width+g);}),S+=$+w;}),lt(f,o,[],h,n,p,S)}let A=new Map;s.forEach(b=>{b.nodeIds.forEach(k=>A.set(k,b.id));});let v=[],Y=new Map;s.forEach(b=>{let k=b.nodeIds.map(E=>u.get(E)).filter(E=>!!E);if(k.length===0)return;let W=new Set(b.nodeIds),S=o.filter(E=>W.has(E.from)&&W.has(E.to)),X=nt(k.map(E=>E.id),S),x=new Map;k.forEach(E=>{let U=X.get(E.id)??0;x.has(U)||x.set(U,[]),x.get(U).push(E);});let D=Array.from(x.keys()).sort((E,U)=>E-U),$=0,O=0;D.forEach(E=>{let q=x.get(E).reduce((V,Z,j)=>V+(n?Z.height:Z.width)+(j>0?g:0),0);O=Math.max(O,q);}),D.forEach(E=>{let U=x.get(E),q=0,V=U.reduce((j,ct,xt)=>j+(n?ct.height:ct.width)+(xt>0?g:0),0),Z=(O-V)/2;U.forEach(j=>{q=Math.max(q,n?j.width:j.height),n?(j.relX=Math.round($),j.relY=Math.round(Z),Z+=j.height+g):(j.relX=Math.round(Z),j.relY=Math.round($),Z+=j.width+g);}),$+=q+y;});let _=n?$-y:O,z=n?O:$-y,J=Math.round(_+a*2),K=Math.round(z+a*2+P),H={id:b.id,isGroup:true,groupObj:b,memberNodes:k,width:J,height:K,x:0,y:0};v.push(H),Y.set(b.id,H);}),f.filter(b=>!A.has(b.id)).forEach(b=>{let k={id:`standalone_${b.id}`,isGroup:false,memberNodes:[b],width:b.width,height:b.height,x:0,y:0};v.push(k),Y.set(k.id,k);});let N=b=>{let k=A.get(b);return k||`standalone_${b}`},B=[];o.forEach(b=>{let k=N(b.from),W=N(b.to);k!==W&&B.push({from:k,to:W});});let R=nt(v.map(b=>b.id),B),F=new Map;v.forEach(b=>{let k=R.get(b.id)??0;F.has(k)||F.set(k,[]),F.get(k).push(b);});let I=Array.from(F.keys()).sort((b,k)=>b-k),C=p,T=0;I.forEach(b=>{let W=F.get(b).reduce((S,X,x)=>S+(n?X.height:X.width)+(x>0?c:0),0);T=Math.max(T,W);});let G=[];return I.forEach(b=>{let k=F.get(b),W=0,S=k.reduce((x,D,$)=>x+(n?D.height:D.width)+($>0?c:0),0),X=p+(T-S)/2;k.forEach(x=>{if(W=Math.max(W,n?x.width:x.height),n?(x.x=Math.round(C),x.y=Math.round(X),X+=x.height+c):(x.x=Math.round(X),x.y=Math.round(C),X+=x.width+c),x.isGroup&&x.groupObj)G.push({...x.groupObj,x:x.x,y:x.y,width:x.width,height:x.height}),x.memberNodes.forEach(D=>{D.x=Math.round(x.x+a+D.relX),D.y=Math.round(x.y+a+P+D.relY);});else {let D=x.memberNodes[0];D.x=x.x,D.y=x.y;}}),C+=W+c;}),lt(f,o,G,h,n,p,C)}function lt(e,i,r,m,g,w,p){if(m){let l=p-95;e.forEach(t=>{g?t.x=l-(t.x-w)-t.width:t.y=l-(t.y-w)-t.height;}),r.forEach(t=>{g?t.x=l-(t.x-w)-t.width:t.y=l-(t.y-w)-t.height;});}let a=1/0,c=1/0,y=-1/0,d=-1/0;return e.forEach(l=>{a=Math.min(a,l.x),c=Math.min(c,l.y),y=Math.max(y,l.x+l.width),d=Math.max(d,l.y+l.height);}),r.forEach(l=>{a=Math.min(a,l.x),c=Math.min(c,l.y),y=Math.max(y,l.x+l.width),d=Math.max(d,l.y+l.height);}),{nodes:e,edges:i,groups:r,bounds:{minX:Math.round(a),minY:Math.round(c),maxX:Math.round(y),maxY:Math.round(d),width:Math.round(y-a),height:Math.round(d-c)}}}function Q(e,i,r,m,g,w=12){let p=g.x-w,a=g.y-w,c=g.x+g.width+w,y=g.y+g.height+w;if(Math.max(e,r)<p||Math.min(e,r)>c||Math.max(i,m)<a||Math.min(i,m)>y)return false;let d=r-e,l=m-i,t=0,o=1,s=[-d,d,-l,l],f=[e-p,c-e,i-a,y-i];for(let u=0;u<4;u++)if(s[u]===0){if(f[u]<0)return false}else {let n=f[u]/s[u];if(s[u]<0){if(n>o)return false;n>t&&(t=n);}else {if(n<t)return false;n<o&&(o=n);}}return t<=o}function at(e,i,r,m){let g=Array.from(i.values()),w=new Map;function p(d,l){w.has(d)||w.set(d,new Map);let t=w.get(d);return t.has(l)||t.set(l,{sourceEdges:[],targetEdges:[]}),t.get(l)}let a=[];e.forEach((d,l)=>{let t=i.get(d.from),o=i.get(d.to);if(!t||!o)return;let s=false;r==="LR"?s=o.x+o.width<=t.x+30:r==="RL"?s=o.x>=t.x+t.width-30:r==="TB"?s=o.y+o.height<=t.y+30:r==="BT"&&(s=o.y>=t.y+t.height-30);let f,u;d.startAnchor?f=d.startAnchor:s?f=r==="LR"||r==="RL"?"top":"right":f=r==="LR"?"right":r==="RL"?"left":r==="TB"?"bottom":"top",d.endAnchor?u=d.endAnchor:s?u=r==="LR"||r==="RL"?"top":"right":u=r==="LR"?"left":r==="RL"?"right":r==="TB"?"top":"bottom",a.push({edge:d,source:t,target:o,startAnchor:f,endAnchor:u,isBackward:s}),p(t.id,f).sourceEdges.push({edge:d,index:l}),p(o.id,u).targetEdges.push({edge:d,index:l});});function c(d,l,t,o){let s=p(d.id,l),f=o?s.targetEdges:s.sourceEdges,u=f.length,n=Math.max(0,f.findIndex(h=>h.index===t));if(l==="left"||l==="right"){let h=l==="left"?d.x:d.x+d.width;if(u<=1)return {x:h,y:d.y+d.height/2};let P=d.height-24,L=Math.min(28,P/(u+1)),A=d.y+12+(P-(u-1)*L)/2,v=Math.round(A+n*L);return {x:h,y:v}}else {let h=l==="top"?d.y:d.y+d.height;if(u<=1)return {x:d.x+d.width/2,y:h};let P=d.width-24,L=Math.min(32,P/(u+1)),A=d.x+12+(P-(u-1)*L)/2;return {x:Math.round(A+n*L),y:h}}}let y=[];return a.forEach(({edge:d,source:l,target:t,startAnchor:o,endAnchor:s,isBackward:f},u)=>{let n=c(l,o,u,false),h=c(t,s,u,true),P=d.id||`edge_${l.id}_${t.id}_${u}`;if(f)if(r==="LR"||r==="RL"){let Y=Math.min(n.x,h.x),M=Math.max(n.x,h.x),B=g.filter(T=>T.id!==l.id&&T.id!==t.id&&T.x+T.width>Y&&T.x<M).reduce((T,G)=>Math.min(T,G.y),Math.min(n.y,h.y)),R=Math.abs(n.x-h.x),F=Math.max(90,Math.min(260,R*.22)),I=Math.min(B-F,Math.min(n.y,h.y)-F),C={x:Math.round((n.x+h.x)/2),y:Math.round(I)};y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"curved",controlPoint:C,isBackward:true});return}else {let Y=Math.min(n.y,h.y),M=Math.max(n.y,h.y),B=g.filter(T=>T.id!==l.id&&T.id!==t.id&&T.y+T.height>Y&&T.y<M).reduce((T,G)=>Math.max(T,G.x+G.width),Math.max(n.x,h.x)),R=Math.abs(n.y-h.y),F=Math.max(90,Math.min(260,R*.22)),I=Math.max(B+F,Math.max(n.x,h.x)+F),C={x:Math.round(I),y:Math.round((n.y+h.y)/2)};y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"curved",controlPoint:C,isBackward:true});return}let L=d.routing||m?.edgeRouting||"elbow",A=Math.abs(h.y-n.y),v=Math.abs(h.x-n.x);if(L==="elbow"){if((r==="LR"||r==="RL")&&A<14){y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"straight",isBackward:false});return}if((r==="TB"||r==="BT")&&v<14){y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"straight",isBackward:false});return}let Y=g.filter(M=>M.id!==l.id&&M.id!==t.id);if(r==="LR"||r==="RL"){let M=Math.round((n.x+h.x)/2),N=[M,M-36,M+36,M-72,M+72,Math.round(n.x+36),Math.round(h.x-36)],B;for(let C of N)if(!Y.some(G=>Q(n.x,n.y,C,n.y,G,8)||Q(C,n.y,C,h.y,G,8)||Q(C,h.y,h.x,h.y,G,8))){B=C;break}if(B!==void 0){y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:B,y:n.y},controlPoint2:{x:B,y:h.y},isBackward:false});return}let R=(n.y+h.y)/2,I=Y.filter(C=>Q(n.x,n.y,h.x,h.y,C,12))[0];if(I){let T=R<I.y+I.height/2?I.y-36:I.y+I.height+36;y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:M,y:n.y},controlPoint2:{x:M,y:T},isBackward:false});return}y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:M,y:n.y},controlPoint2:{x:M,y:h.y},isBackward:false});return}else {let M=Math.round((n.y+h.y)/2),N=[M,M-36,M+36,M-72,M+72,Math.round(n.y+36),Math.round(h.y-36)],B;for(let R of N)if(!Y.some(I=>Q(n.x,n.y,n.x,R,I,8)||Q(n.x,R,h.x,R,I,8)||Q(h.x,R,h.x,h.y,I,8))){B=R;break}if(B!==void 0){y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:n.x,y:B},controlPoint2:{x:h.x,y:B},isBackward:false});return}y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:n.x,y:M},controlPoint2:{x:h.x,y:M},isBackward:false});return}}if(L==="curved"){let Y=h.x-n.x,M=h.y-n.y,N=Math.hypot(Y,M),B=Math.round((n.x+h.x)/2),R=Math.round((n.y+h.y)/2),F=Math.max(18,Math.min(38,N*.15)),I=Math.round(F*2),C;Math.abs(Y)>=Math.abs(M)?C={x:B,y:R-I}:C={x:B+I,y:R},y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"curved",controlPoint:C,isBackward:false});return}y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"straight",isBackward:false});}),y}function dt(e,i,r,m=0){let g=r.x-m,w=r.y-m,p=r.x+r.width+m,a=r.y+r.height+m;if(e<g||e>p||i<w||i>a)return false;let c=r.x+r.width/2,y=r.y+r.height/2,d=r.width/2+m,l=r.height/2+m,t=r.shape||"rectangle";if(t==="diamond")return Math.abs(e-c)/d+Math.abs(i-y)/l<=1;if(t==="ellipse"||t==="circle"||t==="cloud")return (e-c)*(e-c)/(d*d)+(i-y)*(i-y)/(l*l)<=1;if(t==="triangle"){let o=r.height<=0?0:Math.max(0,Math.min(1,(i-r.y)/r.height)),s=r.width/2*o;return e>=c-s-m&&e<=c+s+m}if(t==="parallelogram"){let o=Math.min(36,r.width*.22),s=r.height<=0?0:Math.max(0,Math.min(1,(i-r.y)/r.height)),f=r.x+o*(1-s),u=r.x+r.width-o*s;return e>=f-m&&e<=u+m}if(t==="trapezoid"){let o=Math.min(r.width*.2,40),s=r.height<=0?0:Math.max(0,Math.min(1,(i-r.y)/r.height)),f=r.x+o*(1-s),u=r.x+r.width-o*(1-s);return e>=f-m&&e<=u+m}if(t==="hexagon"){let o=Math.min(r.width*.22,45),s,f;if(i<=y){let u=r.height<=0?0:Math.max(0,Math.min(1,(i-r.y)/(r.height/2)));s=r.x+o*(1-u),f=r.x+r.width-o*(1-u);}else {let u=r.height<=0?0:Math.max(0,Math.min(1,(i-y)/(r.height/2)));s=r.x+o*u,f=r.x+r.width-o*u;}return e>=s-m&&e<=f+m}if(t==="capsule")if(r.width>=r.height){let o=r.height/2+m,s=r.x+r.height/2,f=r.x+r.width-r.height/2;return e>=s&&e<=f?i>=r.y-m&&i<=r.y+r.height+m:e<s?Math.hypot(e-s,i-y)<=o:Math.hypot(e-f,i-y)<=o}else {let o=r.width/2+m,s=r.y+r.width/2,f=r.y+r.height-r.width/2;return i>=s&&i<=f?e>=r.x-m&&e<=r.x+r.width+m:i<s?Math.hypot(e-c,i-s)<=o:Math.hypot(e-c,i-f)<=o}if(t==="cylinder"){let o=Math.min(24,r.height*.18),s=r.width/2+m,f=o/2+m,u=r.y+o/2,n=r.y+r.height-o/2;if(e>=g&&e<=p&&i>=u&&i<=n)return true;let h=e-c,P=i-u;if(h*h/(s*s)+P*P/(f*f)<=1)return true;let L=e-c,A=i-n;return L*L/(s*s)+A*A/(f*f)<=1}return true}function et(e,i,r,m,g,w,p){if(m==="curved"&&g){let a=1-e;return {x:a*a*i.x+2*a*e*g.x+e*e*r.x,y:a*a*i.y+2*a*e*g.y+e*e*r.y}}if(m==="elbow"&&w&&p){let a=1-e,c=a*a,y=c*a,d=e*e,l=d*e;return {x:y*i.x+3*c*e*w.x+3*a*d*p.x+l*r.x,y:y*i.y+3*c*e*w.y+3*a*d*p.y+l*r.y}}return {x:i.x+e*(r.x-i.x),y:i.y+e*(r.y-i.y)}}function st(e,i,r){let m={x:e.startX,y:e.startY},g={x:e.endX,y:e.endY};if(r?.tunneling===false||i.length===0)return [{startX:m.x,startY:m.y,endX:g.x,endY:g.y,isInside:false,isLast:true,arrowType:e.arrowType,controlPoint:e.controlPoint,controlPoint1:e.controlPoint1,controlPoint2:e.controlPoint2}];let w=32,p=[],a=false;for(let l=0;l<=w;l++){let t=l/w,o=et(t,m,g,e.arrowType,e.controlPoint,e.controlPoint1,e.controlPoint2),s=t>.04&&t<.96&&i.some(f=>dt(o.x,o.y,f,2));s&&(a=true),p.push({pt:o,inside:s,t});}if(!a)return [{startX:m.x,startY:m.y,endX:g.x,endY:g.y,isInside:false,isLast:true,arrowType:e.arrowType,controlPoint:e.controlPoint,controlPoint1:e.controlPoint1,controlPoint2:e.controlPoint2}];let c=[],y={points:[p[0]],isInside:p[0].inside};for(let l=1;l<p.length;l++){let t=p[l];t.inside===y.isInside?y.points.push(t):(c.push(y),y={points:[y.points[y.points.length-1],t],isInside:t.inside});}c.push(y);let d=[];return c.forEach((l,t)=>{let o=l.points[0].pt,s=l.points[l.points.length-1].pt,f=t===c.length-1;d.push({startX:Math.round(o.x),startY:Math.round(o.y),endX:Math.round(s.x),endY:Math.round(s.y),isInside:l.isInside,isLast:f,arrowType:"straight"});}),d}var tt={indigo:{stroke:"#4f46e5",fill:"#e0e7ff",text:"#312e81"},blue:{stroke:"#2563eb",fill:"#dbeafe",text:"#1e3a8a"},emerald:{stroke:"#059669",fill:"#d1fae5",text:"#064e3b"},green:{stroke:"#16a34a",fill:"#dcfce7",text:"#14532d"},amber:{stroke:"#d97706",fill:"#fef3c7",text:"#78350f"},yellow:{stroke:"#ca8a04",fill:"#fef9c3",text:"#713f12"},rose:{stroke:"#e11d48",fill:"#ffe4e6",text:"#881337"},red:{stroke:"#dc2626",fill:"#fee2e2",text:"#7f1d1d"},purple:{stroke:"#7c3aed",fill:"#ede9fe",text:"#4c1d95"},violet:{stroke:"#8b5cf6",fill:"#f3e8ff",text:"#581c87"},slate:{stroke:"#475569",fill:"#f1f5f9",text:"#0f172a"},neutral:{stroke:"#525252",fill:"#f5f5f5",text:"#171717"},sky:{stroke:"#0284c7",fill:"#e0f2fe",text:"#0c4a6e"},cyan:{stroke:"#0891b2",fill:"#cffafe",text:"#164e63"},teal:{stroke:"#0d9488",fill:"#ccfbf1",text:"#134e4a"},orange:{stroke:"#ea580c",fill:"#ffedd5",text:"#7c2d12"}};function rt(e,i,r){return i||r?{stroke:i||"#4f46e5",fill:r||"#e0e7ff",text:i||"#1e293b"}:e&&tt[e]?tt[e]:e&&e.startsWith("#")?{stroke:e,fill:`${e}20`,text:e}:tt.indigo}function mt(e){if(!e)return "#475569";let i=e.trim().toLowerCase();return tt[i]?tt[i].stroke:e}function pt(e){switch(e){case "none":return "transparent";case "solid":return "solid";case "cross-hatch":return "cross-hatch";case "dots":return "dots";default:return "hachure"}}function ft(e){if(typeof e=="string")try{let i=JSON.parse(e);if(typeof i!="object"||i===null)throw new Error("Parsed diagram JSON must be an object");return i}catch(i){return console.error("Failed to parse diagram JSON string:",i),{title:"Empty Diagram",nodes:[{id:"error-node",label:"Invalid Diagram JSON",description:String(i),color:"rose"}]}}return e}function it(e,i){let r=ft(e),m={...r.layout,...i},g=ot(r,m),w=[],p=1;g.groups.forEach(t=>{let o=rt(t.color),s=`frame_${t.id}`;w.push({id:s,type:"frame",x:t.x,y:t.y,width:t.width,height:t.height,strokeColor:o.stroke,strokeWidth:1.5,strokeStyle:t.borderStyle||"dashed",fillColor:t.backgroundColor||"transparent",fillStyle:"solid",opacity:.85,zIndex:p++,frameLabel:t.label});});let a=new Map;g.nodes.forEach(t=>{let o=rt(t.color,t.strokeColor,t.fillColor),s=`shape_${t.id}`,f=`group_node_${t.id}`;a.set(t.id,{id:t.id,x:t.x,y:t.y,width:t.width,height:t.height,shapeId:s,shape:t.shape});let u="rectangle",n=false;switch(t.shape){case "diamond":u="diamond";break;case "ellipse":case "circle":u="ellipse";break;case "cylinder":case "database":u="cylinder";break;case "cloud":u="cloud";break;case "sticky":u="sticky";break;case "rounded":u="rectangle",n=true;break;default:u="rectangle",n=false;break}let h=t.description?`${t.label}
|
|
4
|
-
${t.description}`:t.label,P=t.textColor||o.text;if(u==="sticky"?w.push({id:
|
|
5
|
-
exports.Diagram=gt;exports.SketchDiagram=
|
|
3
|
+
`),w=g.reduce((o,i)=>Math.max(o,i.length),0),p=(e.fontSize||14)*.72,s=Math.round(w*p),c=Math.max(a,s+64),y=g.length,d=(e.fontSize||14)*1.45,l=e.badge?14:0,t=Math.max(r,Math.round(y*d+44+l));if(e.shape==="diamond")return {width:e.width??Math.max(Math.round(c*1.65),240),height:e.height??Math.max(Math.round(t*1.55),160)};if(e.shape==="cylinder"||e.shape==="database")return {width:e.width??Math.max(c+32,230),height:e.height??Math.max(t+(e.description?90:75),180)};if(e.shape==="cloud")return {width:e.width??Math.max(Math.round(c*1.45),230),height:e.height??Math.max(Math.round(t*1.4),125)};if(e.shape==="sticky")return {width:e.width??Math.max(c+20,220),height:e.height??Math.max(t+34,140)};if(e.shape==="circle"||e.shape==="ellipse"){let o=Math.max(c*1.25,t*1.25,140);return {width:e.width??o,height:e.height??Math.max(Math.round(t*1.3),110)}}return {width:e.width??Math.max(c+16,210),height:e.height??Math.max(t+12,90)}}function ot(e,a){let r=new Map,m=new Map;e.forEach(c=>{r.set(c,[]),m.set(c,0);}),a.forEach(c=>{r.has(c.from)&&m.has(c.to)&&(r.get(c.from).push(c.to),m.set(c.to,(m.get(c.to)||0)+1));});let g=new Map,w=new Set,p=new Set;function s(c,y){if(p.has(c))return;let d=g.get(c)??0;g.set(c,Math.max(d,y)),p.add(c),w.add(c);let l=r.get(c)||[];for(let t of l)s(t,y+1);p.delete(c);}return e.filter(c=>(m.get(c)||0)===0).forEach(c=>s(c,0)),e.forEach(c=>{w.has(c)||s(c,0);}),g}function at(e,a){let r={...e.layout,...a},m=r.direction||e.direction||"LR",g=r.nodeSpacing??95,w=r.rankSpacing??220,p=r.padding??90,s=r.groupPadding??56,c=r.clusterSpacing??220,y=r.intraRankSpacing??120,d=r.defaultNodeWidth??210,l=r.defaultNodeHeight??90,t=e.nodes||[],o=e.edges||[],i=e.groups||[];if(t.length===0)return {nodes:[],edges:[],groups:[],bounds:{minX:0,minY:0,maxX:0,maxY:0,width:0,height:0}};let f=t.map(b=>{let k=ut(b,d,l);return {...b,width:k.width,height:k.height,x:b.x??0,y:b.y??0,relX:0,relY:0}}),u=new Map;f.forEach(b=>u.set(b.id,b));let n=m==="LR"||m==="RL",h=m==="RL"||m==="BT",P=36;if(!(i.length>0)){let b=ot(t.map(x=>x.id),o),k=new Map;f.forEach(x=>{let A=b.get(x.id)??0;k.has(A)||k.set(A,[]),k.get(A).push(x);});let z=Array.from(k.keys()).sort((x,A)=>x-A),S=p,R=0;return z.forEach(x=>{let W=k.get(x).reduce((O,_,F)=>O+(n?_.height:_.width)+(F>0?g:0),0);R=Math.max(R,W);}),z.forEach(x=>{let A=k.get(x),W=0,O=A.reduce((F,J,K)=>F+(n?J.height:J.width)+(K>0?g:0),0),_=p+(R-O)/2;A.forEach(F=>{W=Math.max(W,n?F.width:F.height),n?(F.x=Math.round(S),F.y=Math.round(_),_+=F.height+g):(F.x=Math.round(_),F.y=Math.round(S),_+=F.width+g);}),S+=W+w;}),lt(f,o,[],h,n,p,S)}let X=new Map;i.forEach(b=>{b.nodeIds.forEach(k=>X.set(k,b.id));});let N=[],Y=new Map;i.forEach(b=>{let k=b.nodeIds.map(D=>u.get(D)).filter(D=>!!D);if(k.length===0)return;let z=new Set(b.nodeIds),S=o.filter(D=>z.has(D.from)&&z.has(D.to)),R=ot(k.map(D=>D.id),S),x=new Map;k.forEach(D=>{let j=R.get(D.id)??0;x.has(j)||x.set(j,[]),x.get(j).push(D);});let A=Array.from(x.keys()).sort((D,j)=>D-j),W=0,O=0;A.forEach(D=>{let q=x.get(D).reduce((V,Z,U)=>V+(n?Z.height:Z.width)+(U>0?g:0),0);O=Math.max(O,q);}),A.forEach(D=>{let j=x.get(D),q=0,V=j.reduce((U,ct,pt)=>U+(n?ct.height:ct.width)+(pt>0?g:0),0),Z=(O-V)/2;j.forEach(U=>{q=Math.max(q,n?U.width:U.height),n?(U.relX=Math.round(W),U.relY=Math.round(Z),Z+=U.height+g):(U.relX=Math.round(Z),U.relY=Math.round(W),Z+=U.width+g);}),W+=q+y;});let _=n?W-y:O,F=n?O:W-y,J=Math.round(_+s*2),K=Math.round(F+s*2+P),H={id:b.id,isGroup:true,groupObj:b,memberNodes:k,width:J,height:K,x:0,y:0};N.push(H),Y.set(b.id,H);}),f.filter(b=>!X.has(b.id)).forEach(b=>{let k={id:`standalone_${b.id}`,isGroup:false,memberNodes:[b],width:b.width,height:b.height,x:0,y:0};N.push(k),Y.set(k.id,k);});let $=b=>{let k=X.get(b);return k||`standalone_${b}`},E=[];o.forEach(b=>{let k=$(b.from),z=$(b.to);k!==z&&E.push({from:k,to:z});});let C=ot(N.map(b=>b.id),E),v=new Map;N.forEach(b=>{let k=C.get(b.id)??0;v.has(k)||v.set(k,[]),v.get(k).push(b);});let T=Array.from(v.keys()).sort((b,k)=>b-k),B=p,L=0;T.forEach(b=>{let z=v.get(b).reduce((S,R,x)=>S+(n?R.height:R.width)+(x>0?c:0),0);L=Math.max(L,z);});let G=[];return T.forEach(b=>{let k=v.get(b),z=0,S=k.reduce((x,A,W)=>x+(n?A.height:A.width)+(W>0?c:0),0),R=p+(L-S)/2;k.forEach(x=>{if(z=Math.max(z,n?x.width:x.height),n?(x.x=Math.round(B),x.y=Math.round(R),R+=x.height+c):(x.x=Math.round(R),x.y=Math.round(B),R+=x.width+c),x.isGroup&&x.groupObj)G.push({...x.groupObj,x:x.x,y:x.y,width:x.width,height:x.height}),x.memberNodes.forEach(A=>{A.x=Math.round(x.x+s+A.relX),A.y=Math.round(x.y+s+P+A.relY);});else {let A=x.memberNodes[0];A.x=x.x,A.y=x.y;}}),B+=z+c;}),lt(f,o,G,h,n,p,B)}function lt(e,a,r,m,g,w,p){if(m){let l=p-95;e.forEach(t=>{g?t.x=l-(t.x-w)-t.width:t.y=l-(t.y-w)-t.height;}),r.forEach(t=>{g?t.x=l-(t.x-w)-t.width:t.y=l-(t.y-w)-t.height;});}let s=1/0,c=1/0,y=-1/0,d=-1/0;return e.forEach(l=>{s=Math.min(s,l.x),c=Math.min(c,l.y),y=Math.max(y,l.x+l.width),d=Math.max(d,l.y+l.height);}),r.forEach(l=>{s=Math.min(s,l.x),c=Math.min(c,l.y),y=Math.max(y,l.x+l.width),d=Math.max(d,l.y+l.height);}),{nodes:e,edges:a,groups:r,bounds:{minX:Math.round(s),minY:Math.round(c),maxX:Math.round(y),maxY:Math.round(d),width:Math.round(y-s),height:Math.round(d-c)}}}function Q(e,a,r,m,g,w=12){let p=g.x-w,s=g.y-w,c=g.x+g.width+w,y=g.y+g.height+w;if(Math.max(e,r)<p||Math.min(e,r)>c||Math.max(a,m)<s||Math.min(a,m)>y)return false;let d=r-e,l=m-a,t=0,o=1,i=[-d,d,-l,l],f=[e-p,c-e,a-s,y-a];for(let u=0;u<4;u++)if(i[u]===0){if(f[u]<0)return false}else {let n=f[u]/i[u];if(i[u]<0){if(n>o)return false;n>t&&(t=n);}else {if(n<t)return false;n<o&&(o=n);}}return t<=o}function st(e,a,r,m){let g=Array.from(a.values()),w=new Map;function p(d,l){w.has(d)||w.set(d,new Map);let t=w.get(d);return t.has(l)||t.set(l,{sourceEdges:[],targetEdges:[]}),t.get(l)}let s=[];e.forEach((d,l)=>{let t=a.get(d.from),o=a.get(d.to);if(!t||!o)return;let i=false;r==="LR"?i=o.x+o.width<=t.x+30:r==="RL"?i=o.x>=t.x+t.width-30:r==="TB"?i=o.y+o.height<=t.y+30:r==="BT"&&(i=o.y>=t.y+t.height-30);let f,u;d.startAnchor?f=d.startAnchor:i?f=r==="LR"||r==="RL"?"top":"right":f=r==="LR"?"right":r==="RL"?"left":r==="TB"?"bottom":"top",d.endAnchor?u=d.endAnchor:i?u=r==="LR"||r==="RL"?"top":"right":u=r==="LR"?"left":r==="RL"?"right":r==="TB"?"top":"bottom",s.push({edge:d,source:t,target:o,startAnchor:f,endAnchor:u,isBackward:i}),p(t.id,f).sourceEdges.push({edge:d,index:l}),p(o.id,u).targetEdges.push({edge:d,index:l});});function c(d,l,t,o){let i=p(d.id,l),f=o?i.targetEdges:i.sourceEdges,u=f.length,n=Math.max(0,f.findIndex(h=>h.index===t));if(l==="left"||l==="right"){let h=l==="left"?d.x:d.x+d.width;if(u<=1)return {x:h,y:d.y+d.height/2};let P=d.height-24,I=Math.min(28,P/(u+1)),X=d.y+12+(P-(u-1)*I)/2,N=Math.round(X+n*I);return {x:h,y:N}}else {let h=l==="top"?d.y:d.y+d.height;if(u<=1)return {x:d.x+d.width/2,y:h};let P=d.width-24,I=Math.min(32,P/(u+1)),X=d.x+12+(P-(u-1)*I)/2;return {x:Math.round(X+n*I),y:h}}}let y=[];return s.forEach(({edge:d,source:l,target:t,startAnchor:o,endAnchor:i,isBackward:f},u)=>{let n=c(l,o,u,false),h=c(t,i,u,true),P=d.id||`edge_${l.id}_${t.id}_${u}`;if(f)if(r==="LR"||r==="RL"){let Y=Math.min(n.x,h.x),M=Math.max(n.x,h.x),E=g.filter(L=>L.id!==l.id&&L.id!==t.id&&L.x+L.width>Y&&L.x<M).reduce((L,G)=>Math.min(L,G.y),Math.min(n.y,h.y)),C=Math.abs(n.x-h.x),v=Math.max(90,Math.min(260,C*.22)),T=Math.min(E-v,Math.min(n.y,h.y)-v),B={x:Math.round((n.x+h.x)/2),y:Math.round(T)};y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"curved",controlPoint:B,isBackward:true});return}else {let Y=Math.min(n.y,h.y),M=Math.max(n.y,h.y),E=g.filter(L=>L.id!==l.id&&L.id!==t.id&&L.y+L.height>Y&&L.y<M).reduce((L,G)=>Math.max(L,G.x+G.width),Math.max(n.x,h.x)),C=Math.abs(n.y-h.y),v=Math.max(90,Math.min(260,C*.22)),T=Math.max(E+v,Math.max(n.x,h.x)+v),B={x:Math.round(T),y:Math.round((n.y+h.y)/2)};y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"curved",controlPoint:B,isBackward:true});return}let I=d.routing||m?.edgeRouting||"elbow",X=Math.abs(h.y-n.y),N=Math.abs(h.x-n.x);if(I==="elbow"){if((r==="LR"||r==="RL")&&X<14){y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"straight",isBackward:false});return}if((r==="TB"||r==="BT")&&N<14){y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"straight",isBackward:false});return}let Y=g.filter(M=>M.id!==l.id&&M.id!==t.id);if(r==="LR"||r==="RL"){let M=Math.round((n.x+h.x)/2),$=[M,M-36,M+36,M-72,M+72,Math.round(n.x+36),Math.round(h.x-36)],E;for(let B of $)if(!Y.some(G=>Q(n.x,n.y,B,n.y,G,8)||Q(B,n.y,B,h.y,G,8)||Q(B,h.y,h.x,h.y,G,8))){E=B;break}if(E!==void 0){y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:E,y:n.y},controlPoint2:{x:E,y:h.y},isBackward:false});return}let C=(n.y+h.y)/2,T=Y.filter(B=>Q(n.x,n.y,h.x,h.y,B,12))[0];if(T){let L=C<T.y+T.height/2?T.y-36:T.y+T.height+36;y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:M,y:n.y},controlPoint2:{x:M,y:L},isBackward:false});return}y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:M,y:n.y},controlPoint2:{x:M,y:h.y},isBackward:false});return}else {let M=Math.round((n.y+h.y)/2),$=[M,M-36,M+36,M-72,M+72,Math.round(n.y+36),Math.round(h.y-36)],E;for(let C of $)if(!Y.some(T=>Q(n.x,n.y,n.x,C,T,8)||Q(n.x,C,h.x,C,T,8)||Q(h.x,C,h.x,h.y,T,8))){E=C;break}if(E!==void 0){y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:n.x,y:E},controlPoint2:{x:h.x,y:E},isBackward:false});return}y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:n.x,y:M},controlPoint2:{x:h.x,y:M},isBackward:false});return}}if(I==="curved"){let Y=h.x-n.x,M=h.y-n.y,$=Math.hypot(Y,M),E=Math.round((n.x+h.x)/2),C=Math.round((n.y+h.y)/2),v=Math.max(18,Math.min(38,$*.15)),T=Math.round(v*2),B;Math.abs(Y)>=Math.abs(M)?B={x:E,y:C-T}:B={x:E+T,y:C},y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"curved",controlPoint:B,isBackward:false});return}y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"straight",isBackward:false});}),y}function dt(e,a,r,m=0){let g=r.x-m,w=r.y-m,p=r.x+r.width+m,s=r.y+r.height+m;if(e<g||e>p||a<w||a>s)return false;let c=r.x+r.width/2,y=r.y+r.height/2,d=r.width/2+m,l=r.height/2+m,t=r.shape||"rectangle";if(t==="diamond")return Math.abs(e-c)/d+Math.abs(a-y)/l<=1;if(t==="ellipse"||t==="circle"||t==="cloud")return (e-c)*(e-c)/(d*d)+(a-y)*(a-y)/(l*l)<=1;if(t==="triangle"){let o=r.height<=0?0:Math.max(0,Math.min(1,(a-r.y)/r.height)),i=r.width/2*o;return e>=c-i-m&&e<=c+i+m}if(t==="parallelogram"){let o=Math.min(36,r.width*.22),i=r.height<=0?0:Math.max(0,Math.min(1,(a-r.y)/r.height)),f=r.x+o*(1-i),u=r.x+r.width-o*i;return e>=f-m&&e<=u+m}if(t==="trapezoid"){let o=Math.min(r.width*.2,40),i=r.height<=0?0:Math.max(0,Math.min(1,(a-r.y)/r.height)),f=r.x+o*(1-i),u=r.x+r.width-o*(1-i);return e>=f-m&&e<=u+m}if(t==="hexagon"){let o=Math.min(r.width*.22,45),i,f;if(a<=y){let u=r.height<=0?0:Math.max(0,Math.min(1,(a-r.y)/(r.height/2)));i=r.x+o*(1-u),f=r.x+r.width-o*(1-u);}else {let u=r.height<=0?0:Math.max(0,Math.min(1,(a-y)/(r.height/2)));i=r.x+o*u,f=r.x+r.width-o*u;}return e>=i-m&&e<=f+m}if(t==="capsule")if(r.width>=r.height){let o=r.height/2+m,i=r.x+r.height/2,f=r.x+r.width-r.height/2;return e>=i&&e<=f?a>=r.y-m&&a<=r.y+r.height+m:e<i?Math.hypot(e-i,a-y)<=o:Math.hypot(e-f,a-y)<=o}else {let o=r.width/2+m,i=r.y+r.width/2,f=r.y+r.height-r.width/2;return a>=i&&a<=f?e>=r.x-m&&e<=r.x+r.width+m:a<i?Math.hypot(e-c,a-i)<=o:Math.hypot(e-c,a-f)<=o}if(t==="cylinder"){let o=Math.min(24,r.height*.18),i=r.width/2+m,f=o/2+m,u=r.y+o/2,n=r.y+r.height-o/2;if(e>=g&&e<=p&&a>=u&&a<=n)return true;let h=e-c,P=a-u;if(h*h/(i*i)+P*P/(f*f)<=1)return true;let I=e-c,X=a-n;return I*I/(i*i)+X*X/(f*f)<=1}return true}function rt(e,a,r,m,g,w,p){if(m==="curved"&&g){let s=1-e;return {x:s*s*a.x+2*s*e*g.x+e*e*r.x,y:s*s*a.y+2*s*e*g.y+e*e*r.y}}if(m==="elbow"&&w&&p){let s=1-e,c=s*s,y=c*s,d=e*e,l=d*e;return {x:y*a.x+3*c*e*w.x+3*s*d*p.x+l*r.x,y:y*a.y+3*c*e*w.y+3*s*d*p.y+l*r.y}}return {x:a.x+e*(r.x-a.x),y:a.y+e*(r.y-a.y)}}function it(e,a,r){let m={x:e.startX,y:e.startY},g={x:e.endX,y:e.endY};if(r?.tunneling===false||a.length===0)return [{startX:m.x,startY:m.y,endX:g.x,endY:g.y,isInside:false,isLast:true,arrowType:e.arrowType,controlPoint:e.controlPoint,controlPoint1:e.controlPoint1,controlPoint2:e.controlPoint2}];let w=32,p=[],s=false;for(let l=0;l<=w;l++){let t=l/w,o=rt(t,m,g,e.arrowType,e.controlPoint,e.controlPoint1,e.controlPoint2),i=t>.04&&t<.96&&a.some(f=>dt(o.x,o.y,f,2));i&&(s=true),p.push({pt:o,inside:i,t});}if(!s)return [{startX:m.x,startY:m.y,endX:g.x,endY:g.y,isInside:false,isLast:true,arrowType:e.arrowType,controlPoint:e.controlPoint,controlPoint1:e.controlPoint1,controlPoint2:e.controlPoint2}];let c=[],y={points:[p[0]],isInside:p[0].inside};for(let l=1;l<p.length;l++){let t=p[l];t.inside===y.isInside?y.points.push(t):(c.push(y),y={points:[y.points[y.points.length-1],t],isInside:t.inside});}c.push(y);let d=[];return c.forEach((l,t)=>{let o=l.points[0].pt,i=l.points[l.points.length-1].pt,f=t===c.length-1;d.push({startX:Math.round(o.x),startY:Math.round(o.y),endX:Math.round(i.x),endY:Math.round(i.y),isInside:l.isInside,isLast:f,arrowType:"straight"});}),d}var tt={indigo:{stroke:"#4f46e5",fill:"#e0e7ff",text:"#312e81"},blue:{stroke:"#2563eb",fill:"#dbeafe",text:"#1e3a8a"},emerald:{stroke:"#059669",fill:"#d1fae5",text:"#064e3b"},green:{stroke:"#16a34a",fill:"#dcfce7",text:"#14532d"},amber:{stroke:"#d97706",fill:"#fef3c7",text:"#78350f"},yellow:{stroke:"#ca8a04",fill:"#fef9c3",text:"#713f12"},rose:{stroke:"#e11d48",fill:"#ffe4e6",text:"#881337"},red:{stroke:"#dc2626",fill:"#fee2e2",text:"#7f1d1d"},purple:{stroke:"#7c3aed",fill:"#ede9fe",text:"#4c1d95"},violet:{stroke:"#8b5cf6",fill:"#f3e8ff",text:"#581c87"},slate:{stroke:"#475569",fill:"#f1f5f9",text:"#0f172a"},neutral:{stroke:"#525252",fill:"#f5f5f5",text:"#171717"},sky:{stroke:"#0284c7",fill:"#e0f2fe",text:"#0c4a6e"},cyan:{stroke:"#0891b2",fill:"#cffafe",text:"#164e63"},teal:{stroke:"#0d9488",fill:"#ccfbf1",text:"#134e4a"},orange:{stroke:"#ea580c",fill:"#ffedd5",text:"#7c2d12"}};function nt(e,a,r){return a||r?{stroke:a||"#4f46e5",fill:r||"#e0e7ff",text:a||"#1e293b"}:e&&tt[e]?tt[e]:e&&e.startsWith("#")?{stroke:e,fill:`${e}20`,text:e}:tt.indigo}function mt(e){if(!e)return "#475569";let a=e.trim().toLowerCase();return tt[a]?tt[a].stroke:e}function bt(e){switch(e){case "none":return "transparent";case "solid":return "solid";case "cross-hatch":return "cross-hatch";case "dots":return "dots";default:return "hachure"}}function ft(e){if(typeof e=="string")try{let a=JSON.parse(e);if(typeof a!="object"||a===null)throw new Error("Parsed diagram JSON must be an object");return a}catch(a){return console.error("Failed to parse diagram JSON string:",a),{title:"Empty Diagram",nodes:[{id:"error-node",label:"Invalid Diagram JSON",description:String(a),color:"rose"}]}}return e}function et(e,a){let r=ft(e),m={...r.layout,...a},g=at(r,m),w=[],p=1;g.groups.forEach(t=>{let o=nt(t.color),i=`frame_${t.id}`;w.push({id:i,type:"frame",x:t.x,y:t.y,width:t.width,height:t.height,strokeColor:o.stroke,strokeWidth:1.5,strokeStyle:t.borderStyle||"dashed",fillColor:t.backgroundColor||"transparent",fillStyle:"solid",opacity:.85,zIndex:p++,frameLabel:t.label});});let s=new Map;g.nodes.forEach(t=>{let o=nt(t.color,t.strokeColor,t.fillColor),i=`shape_${t.id}`,f=`group_node_${t.id}`;s.set(t.id,{id:t.id,x:t.x,y:t.y,width:t.width,height:t.height,shapeId:i,shape:t.shape});let u="rectangle",n=false;switch(t.shape){case "diamond":u="diamond";break;case "ellipse":case "circle":u="ellipse";break;case "cylinder":case "database":u="cylinder";break;case "cloud":u="cloud";break;case "sticky":u="sticky";break;case "rounded":u="rectangle",n=true;break;default:u="rectangle",n=false;break}let h=t.description?`${t.label}
|
|
4
|
+
${t.description}`:t.label,P=t.textColor||o.text;if(u==="sticky"?w.push({id:i,type:"sticky",x:t.x,y:t.y,width:t.width,height:t.height,strokeColor:o.stroke,strokeWidth:t.strokeWidth||1.5,strokeStyle:t.strokeStyle||"solid",fillColor:o.fill,fillStyle:"solid",stickyColor:o.fill,text:h,textColor:P,fontSize:t.fontSize||14,fontFamily:t.fontFamily||"fuzzy",textAlign:t.textAlign||"center",opacity:t.opacity??1,zIndex:p++,roughness:t.roughness??1}):w.push({id:i,type:u,x:t.x,y:t.y,width:t.width,height:t.height,strokeColor:o.stroke,strokeWidth:t.strokeWidth||2,strokeStyle:t.strokeStyle||"solid",fillColor:o.fill,fillStyle:bt(t.fillStyle),text:h,textColor:P,fontSize:t.fontSize||13.5,fontFamily:t.fontFamily||"sans",textAlign:t.textAlign||"center",opacity:t.opacity??1,zIndex:p++,edgeStyle:n?"round":"sharp",roughness:t.roughness??1,groupIds:[f]}),t.badge){let I=`badge_${t.id}`,X=Math.max(45,t.badge.length*8+14),N=20,Y=t.x+t.width-X-8,M=t.y-8;u==="diamond"?(Y=Math.round(t.x+t.width/2-X/2),M=Math.round(t.y-N-4)):u==="cylinder"?(Y=Math.round(t.x+t.width-X-14),M=Math.round(t.y-6)):u==="cloud"&&(Y=Math.round(t.x+t.width-X-24),M=Math.round(t.y+2)),w.push({id:I,type:"rectangle",x:Y,y:M,width:X,height:N,strokeColor:o.stroke,strokeWidth:1.5,strokeStyle:"solid",fillColor:"#e2e8f0",fillStyle:"solid",text:t.badge,textColor:o.stroke,fontSize:10,fontFamily:"mono",textAlign:"center",opacity:1,zIndex:p++,edgeStyle:"round",groupIds:[f]});}});let c=a?.direction||r.direction||"LR",y=[];function d(t,o=8){for(let i of y)if(t.x<i.x+i.width+o&&t.x+t.width+o>i.x&&t.y<i.y+i.height+o&&t.y+t.height+o>i.y)return true;return false}let l=st(g.edges,s,c,m);return g.edges.forEach((t,o)=>{let i=s.get(t.from),f=s.get(t.to);if(!i||!f)return;let u=l[o],n=u?.startAnchor||"right",h=u?.endAnchor||"left",P=u?.startX??Math.round(i.x+i.width),I=u?.startY??Math.round(i.y+i.height/2),X=u?.endX??Math.round(f.x),N=u?.endY??Math.round(f.y+f.height/2),Y=u?.arrowType??"straight",M=u?.controlPoint,$=u?.controlPoint1,E=u?.controlPoint2,C=t.id||`edge_${t.from}_${t.to}_${o}`,v=X-P,T=N-I,B=Array.from(s.values()).filter(S=>S.id!==i.id&&S.id!==f.id),G=t.tunneling!==false&&m.tunneling!==false&&u?it(u,B,m):[{startX:P,startY:I,endX:X,endY:N,isInside:false,isLast:true,arrowType:Y,controlPoint:M,controlPoint1:$,controlPoint2:E}],b=m.passthroughOpacity??.35,k=m.passthroughStyle??"dotted",z=!!t.label;if(G.forEach((S,R)=>{let x=S.endX-S.startX,A=S.endY-S.startY,W=G.length===1?C:`${C}_sub_${R}`,O=S.isInside,_=O?k:t.style||"solid",F=O?b:1,J=R===0?{elementId:i.shapeId,anchor:n}:void 0,K=S.isLast?{elementId:f.shapeId,anchor:h}:void 0,H=S.isLast&&t.arrowEnd!==false?"arrow":"none",D=R===0&&t.arrowStart?"arrow":"none";w.push({id:W,type:S.isLast?"arrow":"line",x:S.startX,y:S.startY,width:x,height:A,arrowType:S.arrowType,controlPoint:S.controlPoint,controlPoint1:S.controlPoint1,controlPoint2:S.controlPoint2,strokeColor:mt(t.color),strokeWidth:t.strokeWidth||2,strokeStyle:_,fillColor:"transparent",fillStyle:"solid",opacity:F,zIndex:O?2:p++,roughness:t.roughness??1,startBinding:J,endBinding:K,endArrowhead:H,startArrowhead:D,label:z&&S.isLast?t.label:void 0});}),t.label&&!z){let S=Math.max(60,t.label.length*7.5+20),R=22,x={x:P,y:I},A={x:X,y:N},W=[.5,.35,.65,.22,.78,.15,.85],O=Math.round(P+v/2),_=Math.round(I+T/2),F=false;for(let K of W){let H=rt(K,x,A,Y,M,$,E),D={x:Math.round(H.x-S/2),y:Math.round(H.y-R/2),width:S,height:R},j=d(D,6),q=Array.from(s.values()).some(V=>D.x<V.x+V.width+4&&D.x+D.width+4>V.x&&D.y<V.y+V.height+4&&D.y+D.height+4>V.y);if(!j&&!q){O=Math.round(H.x),_=Math.round(H.y),F=true;break}}if(!F){let K=rt(.5,x,A,Y,M,$,E);O=Math.round(K.x),_=Math.round(K.y);let H={x:Math.round(O-S/2),y:Math.round(_-R/2),width:S,height:R};for(let D of [-24,24,-44,44,-64,64]){let j=c==="TB"||c==="BT"?{...H,x:H.x+D}:{...H,y:H.y+D};if(!d(j,4)){c==="TB"||c==="BT"?O+=D:_+=D;break}}}let J={x:Math.round(O-S/2),y:Math.round(_-R/2),width:S,height:R};y.push(J),w.push({id:`label_${C}`,type:"rectangle",x:J.x,y:J.y,width:S,height:R,strokeColor:"#cbd5e1",strokeWidth:1,strokeStyle:"solid",fillColor:"#e2e8f0",fillStyle:"solid",text:t.label,textColor:"#0f172a",fontSize:10.5,fontFamily:"sans",textAlign:"center",opacity:.96,zIndex:p++,edgeStyle:"round"});}}),{version:1,name:r.title||"System Architecture",elements:w,canvasBackground:r.canvasBackground||"#f0f4f8",gridStyle:r.gridStyle||"dots",createdAt:Date.now(),updatedAt:Date.now()}}function wt(e){switch(e){case 1:return "fuzzy";case 2:return "sans";case 3:return "mono";default:return "sans"}}function Mt(e){switch(e){case "rectangle":return "rectangle";case "diamond":return "diamond";case "ellipse":return "ellipse";case "arrow":return "arrow";case "line":return "line";case "text":return "text";case "freedraw":return "pen";case "image":return "image";case "frame":return "frame";default:return "rectangle"}}function ht(e,a="Imported Excalidraw"){let r;if(typeof e=="string")try{r=JSON.parse(e);}catch{return {version:1,name:a,elements:[],canvasBackground:"#ffffff",gridStyle:"none",createdAt:Date.now(),updatedAt:Date.now()}}else r=e;let g=(Array.isArray(r)?r:Array.isArray(r.elements)?r.elements:[]).filter(s=>!s.isDeleted),w=[];g.forEach((s,c)=>{let y=typeof s.opacity=="number"?s.opacity/100:1,d=s.strokeWidth||2,l=s.strokeColor&&s.strokeColor!=="transparent"?s.strokeColor:"#1e293b",t=s.backgroundColor&&s.backgroundColor!=="transparent"?s.backgroundColor:"transparent",o="solid";s.fillStyle==="hachure"?o="hachure":s.fillStyle==="cross-hatch"?o="cross-hatch":s.fillStyle==="dots"?o="dots":t==="transparent"&&(o="transparent");let i=s.strokeStyle==="dashed"?"dashed":s.strokeStyle==="dotted"?"dotted":"solid",f={id:s.id||`excal_${c}`,type:Mt(s.type),x:Math.round(s.x),y:Math.round(s.y),width:Math.round(s.width),height:Math.round(s.height),rotation:s.angle||0,strokeColor:l,strokeWidth:d,strokeStyle:i,fillColor:t,fillStyle:o,opacity:y,zIndex:c+1,roughness:typeof s.roughness=="number"?s.roughness:1,text:s.text,fontSize:s.fontSize||18,fontFamily:wt(s.fontFamily),textAlign:s.textAlign||"left",groupIds:s.groupIds};s.type==="freedraw"&&Array.isArray(s.points)&&(f.points=s.points.map(([u,n])=>({x:u,y:n}))),(s.type==="arrow"||s.type==="line")&&(f.endArrowhead=s.endArrowhead?"arrow":"none",f.startArrowhead=s.startArrowhead?"arrow":"none",s.startBinding?.elementId&&(f.startBinding={elementId:s.startBinding.elementId,anchor:"center"}),s.endBinding?.elementId&&(f.endBinding={elementId:s.endBinding.elementId,anchor:"center"})),w.push(f);});let p=Array.isArray(r)?void 0:r.appState;return {version:1,name:a,elements:w,canvasBackground:p?.viewBackgroundColor||"#ffffff",gridStyle:p?.gridSize?"grid":"none",createdAt:Date.now(),updatedAt:Date.now()}}function Et(e){if(typeof e=="string")return e.includes('"type":"excalidraw"')||e.includes('"source":"https://excalidraw.com"');if(typeof e=="object"&&e!==null){if("type"in e&&e.type==="excalidraw")return true;if("elements"in e&&Array.isArray(e.elements)){let a=e.elements[0];if(a&&typeof a=="object"&&a!==null&&"roughness"in a&&"strokeColor"in a)return true}}return false}var gt=react.forwardRef(function({data:a,mode:r="viewer",layoutOptions:m,uiConfig:g,theme:w="system",canvasBackground:p,gridStyle:s,interactive:c=true,autoFit:y=true,showCopyButton:d=true,copyFormat:l="elements",showZoomControls:t=true,title:o,panelVariant:i="classic",storageKey:f=null,onExit:u,guideHref:n="https://sketch.filekitstudio.com/docs",onOpenGuide:h,onChange:P,controllerRef:I,className:X="",style:N},Y){let M=typeof a=="string"?a:JSON.stringify(a),$=m?JSON.stringify(m):"",E=react.useMemo(()=>Et(a)?ht(a,o||"Imported Diagram"):et(a,m),[M,$,o]),C=p??(typeof a=="object"&&a!==null&&"canvasBackground"in a&&a.canvasBackground?a.canvasBackground:void 0),v=s??E.gridStyle;return r==="viewer"?jsxRuntime.jsx(sketch.SketchViewer,{ref:Y,controllerRef:I,value:E.elements,theme:w,canvasBackground:C,gridStyle:v,interactive:c,autoFit:y,showCopyButton:d,copyFormat:l,showZoomControls:t,title:o??E.name,uiConfig:g,className:X,style:N}):jsxRuntime.jsx(sketch.SketchEditor,{ref:Y,controllerRef:I,value:E.elements,boardName:o??E.name,theme:w,canvasBackground:C,gridStyle:v,panelVariant:i,storageKey:f,uiConfig:g,onExit:u,guideHref:n,onOpenGuide:h,onChange:P,onPastePayload:(T,B)=>{if(B&&typeof B=="object"&&"nodes"in B&&Array.isArray(B.nodes))try{return et(B,m).elements}catch{return null}return null},className:X,style:N})}),Bt=gt;typeof sketch.registerPasteConverter=="function"&&sketch.registerPasteConverter((e,a)=>{if(a&&typeof a=="object"&&"nodes"in a&&Array.isArray(a.nodes))try{return et(a).elements}catch(r){return console.warn("[@filekit-studio/diagram] Failed to compile pasted diagram data:",r),null}return null});
|
|
5
|
+
exports.Diagram=gt;exports.SketchDiagram=Bt;exports.THEME_PALETTES=tt;exports.decomposePassthroughSegments=it;exports.diagramToSketch=et;exports.estimateNodeDimensions=ut;exports.excalidrawToSketch=ht;exports.isPointInsideShape=dt;exports.layoutDiagram=at;exports.parseDiagramData=ft;exports.resolveNodeColors=nt;exports.routeDiagramEdges=st;exports.sampleEdgePoint=rt;
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {forwardRef,useMemo}from'react';import {SketchViewer,SketchEditor}from'@filekit-studio/sketch';import {jsx}from'react/jsx-runtime';function ut(e,
|
|
1
|
+
import {forwardRef,useMemo}from'react';import {SketchViewer,SketchEditor,registerPasteConverter}from'@filekit-studio/sketch';import {jsx}from'react/jsx-runtime';function ut(e,a=210,r=90){if(e.width&&e.height)return {width:e.width,height:e.height};let g=(e.description?`${e.label||""}
|
|
2
2
|
${e.description}`:e.label||"").split(`
|
|
3
|
-
`),w=g.reduce((o,s)=>Math.max(o,s.length),0),p=(e.fontSize||14)*.72,a=Math.round(w*p),c=Math.max(i,a+64),y=g.length,d=(e.fontSize||14)*1.45,l=e.badge?14:0,t=Math.max(r,Math.round(y*d+44+l));if(e.shape==="diamond")return {width:e.width??Math.max(Math.round(c*1.65),240),height:e.height??Math.max(Math.round(t*1.55),160)};if(e.shape==="cylinder"||e.shape==="database")return {width:e.width??Math.max(c+32,230),height:e.height??Math.max(t+(e.description?90:75),180)};if(e.shape==="cloud")return {width:e.width??Math.max(Math.round(c*1.45),230),height:e.height??Math.max(Math.round(t*1.4),125)};if(e.shape==="sticky")return {width:e.width??Math.max(c+20,220),height:e.height??Math.max(t+34,140)};if(e.shape==="circle"||e.shape==="ellipse"){let o=Math.max(c*1.25,t*1.25,140);return {width:e.width??o,height:e.height??Math.max(Math.round(t*1.3),110)}}return {width:e.width??Math.max(c+16,210),height:e.height??Math.max(t+12,90)}}function nt(e,i){let r=new Map,m=new Map;e.forEach(c=>{r.set(c,[]),m.set(c,0);}),i.forEach(c=>{r.has(c.from)&&m.has(c.to)&&(r.get(c.from).push(c.to),m.set(c.to,(m.get(c.to)||0)+1));});let g=new Map,w=new Set,p=new Set;function a(c,y){if(p.has(c))return;let d=g.get(c)??0;g.set(c,Math.max(d,y)),p.add(c),w.add(c);let l=r.get(c)||[];for(let t of l)a(t,y+1);p.delete(c);}return e.filter(c=>(m.get(c)||0)===0).forEach(c=>a(c,0)),e.forEach(c=>{w.has(c)||a(c,0);}),g}function ot(e,i){let r={...e.layout,...i},m=r.direction||e.direction||"LR",g=r.nodeSpacing??95,w=r.rankSpacing??220,p=r.padding??90,a=r.groupPadding??56,c=r.clusterSpacing??220,y=r.intraRankSpacing??120,d=r.defaultNodeWidth??210,l=r.defaultNodeHeight??90,t=e.nodes||[],o=e.edges||[],s=e.groups||[];if(t.length===0)return {nodes:[],edges:[],groups:[],bounds:{minX:0,minY:0,maxX:0,maxY:0,width:0,height:0}};let f=t.map(b=>{let k=ut(b,d,l);return {...b,width:k.width,height:k.height,x:b.x??0,y:b.y??0,relX:0,relY:0}}),u=new Map;f.forEach(b=>u.set(b.id,b));let n=m==="LR"||m==="RL",h=m==="RL"||m==="BT",P=36;if(!(s.length>0)){let b=nt(t.map(x=>x.id),o),k=new Map;f.forEach(x=>{let D=b.get(x.id)??0;k.has(D)||k.set(D,[]),k.get(D).push(x);});let W=Array.from(k.keys()).sort((x,D)=>x-D),S=p,X=0;return W.forEach(x=>{let $=k.get(x).reduce((O,_,z)=>O+(n?_.height:_.width)+(z>0?g:0),0);X=Math.max(X,$);}),W.forEach(x=>{let D=k.get(x),$=0,O=D.reduce((z,J,K)=>z+(n?J.height:J.width)+(K>0?g:0),0),_=p+(X-O)/2;D.forEach(z=>{$=Math.max($,n?z.width:z.height),n?(z.x=Math.round(S),z.y=Math.round(_),_+=z.height+g):(z.x=Math.round(_),z.y=Math.round(S),_+=z.width+g);}),S+=$+w;}),lt(f,o,[],h,n,p,S)}let A=new Map;s.forEach(b=>{b.nodeIds.forEach(k=>A.set(k,b.id));});let v=[],Y=new Map;s.forEach(b=>{let k=b.nodeIds.map(E=>u.get(E)).filter(E=>!!E);if(k.length===0)return;let W=new Set(b.nodeIds),S=o.filter(E=>W.has(E.from)&&W.has(E.to)),X=nt(k.map(E=>E.id),S),x=new Map;k.forEach(E=>{let U=X.get(E.id)??0;x.has(U)||x.set(U,[]),x.get(U).push(E);});let D=Array.from(x.keys()).sort((E,U)=>E-U),$=0,O=0;D.forEach(E=>{let q=x.get(E).reduce((V,Z,j)=>V+(n?Z.height:Z.width)+(j>0?g:0),0);O=Math.max(O,q);}),D.forEach(E=>{let U=x.get(E),q=0,V=U.reduce((j,ct,xt)=>j+(n?ct.height:ct.width)+(xt>0?g:0),0),Z=(O-V)/2;U.forEach(j=>{q=Math.max(q,n?j.width:j.height),n?(j.relX=Math.round($),j.relY=Math.round(Z),Z+=j.height+g):(j.relX=Math.round(Z),j.relY=Math.round($),Z+=j.width+g);}),$+=q+y;});let _=n?$-y:O,z=n?O:$-y,J=Math.round(_+a*2),K=Math.round(z+a*2+P),H={id:b.id,isGroup:true,groupObj:b,memberNodes:k,width:J,height:K,x:0,y:0};v.push(H),Y.set(b.id,H);}),f.filter(b=>!A.has(b.id)).forEach(b=>{let k={id:`standalone_${b.id}`,isGroup:false,memberNodes:[b],width:b.width,height:b.height,x:0,y:0};v.push(k),Y.set(k.id,k);});let N=b=>{let k=A.get(b);return k||`standalone_${b}`},B=[];o.forEach(b=>{let k=N(b.from),W=N(b.to);k!==W&&B.push({from:k,to:W});});let R=nt(v.map(b=>b.id),B),F=new Map;v.forEach(b=>{let k=R.get(b.id)??0;F.has(k)||F.set(k,[]),F.get(k).push(b);});let I=Array.from(F.keys()).sort((b,k)=>b-k),C=p,T=0;I.forEach(b=>{let W=F.get(b).reduce((S,X,x)=>S+(n?X.height:X.width)+(x>0?c:0),0);T=Math.max(T,W);});let G=[];return I.forEach(b=>{let k=F.get(b),W=0,S=k.reduce((x,D,$)=>x+(n?D.height:D.width)+($>0?c:0),0),X=p+(T-S)/2;k.forEach(x=>{if(W=Math.max(W,n?x.width:x.height),n?(x.x=Math.round(C),x.y=Math.round(X),X+=x.height+c):(x.x=Math.round(X),x.y=Math.round(C),X+=x.width+c),x.isGroup&&x.groupObj)G.push({...x.groupObj,x:x.x,y:x.y,width:x.width,height:x.height}),x.memberNodes.forEach(D=>{D.x=Math.round(x.x+a+D.relX),D.y=Math.round(x.y+a+P+D.relY);});else {let D=x.memberNodes[0];D.x=x.x,D.y=x.y;}}),C+=W+c;}),lt(f,o,G,h,n,p,C)}function lt(e,i,r,m,g,w,p){if(m){let l=p-95;e.forEach(t=>{g?t.x=l-(t.x-w)-t.width:t.y=l-(t.y-w)-t.height;}),r.forEach(t=>{g?t.x=l-(t.x-w)-t.width:t.y=l-(t.y-w)-t.height;});}let a=1/0,c=1/0,y=-1/0,d=-1/0;return e.forEach(l=>{a=Math.min(a,l.x),c=Math.min(c,l.y),y=Math.max(y,l.x+l.width),d=Math.max(d,l.y+l.height);}),r.forEach(l=>{a=Math.min(a,l.x),c=Math.min(c,l.y),y=Math.max(y,l.x+l.width),d=Math.max(d,l.y+l.height);}),{nodes:e,edges:i,groups:r,bounds:{minX:Math.round(a),minY:Math.round(c),maxX:Math.round(y),maxY:Math.round(d),width:Math.round(y-a),height:Math.round(d-c)}}}function Q(e,i,r,m,g,w=12){let p=g.x-w,a=g.y-w,c=g.x+g.width+w,y=g.y+g.height+w;if(Math.max(e,r)<p||Math.min(e,r)>c||Math.max(i,m)<a||Math.min(i,m)>y)return false;let d=r-e,l=m-i,t=0,o=1,s=[-d,d,-l,l],f=[e-p,c-e,i-a,y-i];for(let u=0;u<4;u++)if(s[u]===0){if(f[u]<0)return false}else {let n=f[u]/s[u];if(s[u]<0){if(n>o)return false;n>t&&(t=n);}else {if(n<t)return false;n<o&&(o=n);}}return t<=o}function at(e,i,r,m){let g=Array.from(i.values()),w=new Map;function p(d,l){w.has(d)||w.set(d,new Map);let t=w.get(d);return t.has(l)||t.set(l,{sourceEdges:[],targetEdges:[]}),t.get(l)}let a=[];e.forEach((d,l)=>{let t=i.get(d.from),o=i.get(d.to);if(!t||!o)return;let s=false;r==="LR"?s=o.x+o.width<=t.x+30:r==="RL"?s=o.x>=t.x+t.width-30:r==="TB"?s=o.y+o.height<=t.y+30:r==="BT"&&(s=o.y>=t.y+t.height-30);let f,u;d.startAnchor?f=d.startAnchor:s?f=r==="LR"||r==="RL"?"top":"right":f=r==="LR"?"right":r==="RL"?"left":r==="TB"?"bottom":"top",d.endAnchor?u=d.endAnchor:s?u=r==="LR"||r==="RL"?"top":"right":u=r==="LR"?"left":r==="RL"?"right":r==="TB"?"top":"bottom",a.push({edge:d,source:t,target:o,startAnchor:f,endAnchor:u,isBackward:s}),p(t.id,f).sourceEdges.push({edge:d,index:l}),p(o.id,u).targetEdges.push({edge:d,index:l});});function c(d,l,t,o){let s=p(d.id,l),f=o?s.targetEdges:s.sourceEdges,u=f.length,n=Math.max(0,f.findIndex(h=>h.index===t));if(l==="left"||l==="right"){let h=l==="left"?d.x:d.x+d.width;if(u<=1)return {x:h,y:d.y+d.height/2};let P=d.height-24,L=Math.min(28,P/(u+1)),A=d.y+12+(P-(u-1)*L)/2,v=Math.round(A+n*L);return {x:h,y:v}}else {let h=l==="top"?d.y:d.y+d.height;if(u<=1)return {x:d.x+d.width/2,y:h};let P=d.width-24,L=Math.min(32,P/(u+1)),A=d.x+12+(P-(u-1)*L)/2;return {x:Math.round(A+n*L),y:h}}}let y=[];return a.forEach(({edge:d,source:l,target:t,startAnchor:o,endAnchor:s,isBackward:f},u)=>{let n=c(l,o,u,false),h=c(t,s,u,true),P=d.id||`edge_${l.id}_${t.id}_${u}`;if(f)if(r==="LR"||r==="RL"){let Y=Math.min(n.x,h.x),M=Math.max(n.x,h.x),B=g.filter(T=>T.id!==l.id&&T.id!==t.id&&T.x+T.width>Y&&T.x<M).reduce((T,G)=>Math.min(T,G.y),Math.min(n.y,h.y)),R=Math.abs(n.x-h.x),F=Math.max(90,Math.min(260,R*.22)),I=Math.min(B-F,Math.min(n.y,h.y)-F),C={x:Math.round((n.x+h.x)/2),y:Math.round(I)};y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"curved",controlPoint:C,isBackward:true});return}else {let Y=Math.min(n.y,h.y),M=Math.max(n.y,h.y),B=g.filter(T=>T.id!==l.id&&T.id!==t.id&&T.y+T.height>Y&&T.y<M).reduce((T,G)=>Math.max(T,G.x+G.width),Math.max(n.x,h.x)),R=Math.abs(n.y-h.y),F=Math.max(90,Math.min(260,R*.22)),I=Math.max(B+F,Math.max(n.x,h.x)+F),C={x:Math.round(I),y:Math.round((n.y+h.y)/2)};y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"curved",controlPoint:C,isBackward:true});return}let L=d.routing||m?.edgeRouting||"elbow",A=Math.abs(h.y-n.y),v=Math.abs(h.x-n.x);if(L==="elbow"){if((r==="LR"||r==="RL")&&A<14){y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"straight",isBackward:false});return}if((r==="TB"||r==="BT")&&v<14){y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"straight",isBackward:false});return}let Y=g.filter(M=>M.id!==l.id&&M.id!==t.id);if(r==="LR"||r==="RL"){let M=Math.round((n.x+h.x)/2),N=[M,M-36,M+36,M-72,M+72,Math.round(n.x+36),Math.round(h.x-36)],B;for(let C of N)if(!Y.some(G=>Q(n.x,n.y,C,n.y,G,8)||Q(C,n.y,C,h.y,G,8)||Q(C,h.y,h.x,h.y,G,8))){B=C;break}if(B!==void 0){y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:B,y:n.y},controlPoint2:{x:B,y:h.y},isBackward:false});return}let R=(n.y+h.y)/2,I=Y.filter(C=>Q(n.x,n.y,h.x,h.y,C,12))[0];if(I){let T=R<I.y+I.height/2?I.y-36:I.y+I.height+36;y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:M,y:n.y},controlPoint2:{x:M,y:T},isBackward:false});return}y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:M,y:n.y},controlPoint2:{x:M,y:h.y},isBackward:false});return}else {let M=Math.round((n.y+h.y)/2),N=[M,M-36,M+36,M-72,M+72,Math.round(n.y+36),Math.round(h.y-36)],B;for(let R of N)if(!Y.some(I=>Q(n.x,n.y,n.x,R,I,8)||Q(n.x,R,h.x,R,I,8)||Q(h.x,R,h.x,h.y,I,8))){B=R;break}if(B!==void 0){y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:n.x,y:B},controlPoint2:{x:h.x,y:B},isBackward:false});return}y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:n.x,y:M},controlPoint2:{x:h.x,y:M},isBackward:false});return}}if(L==="curved"){let Y=h.x-n.x,M=h.y-n.y,N=Math.hypot(Y,M),B=Math.round((n.x+h.x)/2),R=Math.round((n.y+h.y)/2),F=Math.max(18,Math.min(38,N*.15)),I=Math.round(F*2),C;Math.abs(Y)>=Math.abs(M)?C={x:B,y:R-I}:C={x:B+I,y:R},y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"curved",controlPoint:C,isBackward:false});return}y.push({edgeId:P,startAnchor:o,endAnchor:s,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"straight",isBackward:false});}),y}function dt(e,i,r,m=0){let g=r.x-m,w=r.y-m,p=r.x+r.width+m,a=r.y+r.height+m;if(e<g||e>p||i<w||i>a)return false;let c=r.x+r.width/2,y=r.y+r.height/2,d=r.width/2+m,l=r.height/2+m,t=r.shape||"rectangle";if(t==="diamond")return Math.abs(e-c)/d+Math.abs(i-y)/l<=1;if(t==="ellipse"||t==="circle"||t==="cloud")return (e-c)*(e-c)/(d*d)+(i-y)*(i-y)/(l*l)<=1;if(t==="triangle"){let o=r.height<=0?0:Math.max(0,Math.min(1,(i-r.y)/r.height)),s=r.width/2*o;return e>=c-s-m&&e<=c+s+m}if(t==="parallelogram"){let o=Math.min(36,r.width*.22),s=r.height<=0?0:Math.max(0,Math.min(1,(i-r.y)/r.height)),f=r.x+o*(1-s),u=r.x+r.width-o*s;return e>=f-m&&e<=u+m}if(t==="trapezoid"){let o=Math.min(r.width*.2,40),s=r.height<=0?0:Math.max(0,Math.min(1,(i-r.y)/r.height)),f=r.x+o*(1-s),u=r.x+r.width-o*(1-s);return e>=f-m&&e<=u+m}if(t==="hexagon"){let o=Math.min(r.width*.22,45),s,f;if(i<=y){let u=r.height<=0?0:Math.max(0,Math.min(1,(i-r.y)/(r.height/2)));s=r.x+o*(1-u),f=r.x+r.width-o*(1-u);}else {let u=r.height<=0?0:Math.max(0,Math.min(1,(i-y)/(r.height/2)));s=r.x+o*u,f=r.x+r.width-o*u;}return e>=s-m&&e<=f+m}if(t==="capsule")if(r.width>=r.height){let o=r.height/2+m,s=r.x+r.height/2,f=r.x+r.width-r.height/2;return e>=s&&e<=f?i>=r.y-m&&i<=r.y+r.height+m:e<s?Math.hypot(e-s,i-y)<=o:Math.hypot(e-f,i-y)<=o}else {let o=r.width/2+m,s=r.y+r.width/2,f=r.y+r.height-r.width/2;return i>=s&&i<=f?e>=r.x-m&&e<=r.x+r.width+m:i<s?Math.hypot(e-c,i-s)<=o:Math.hypot(e-c,i-f)<=o}if(t==="cylinder"){let o=Math.min(24,r.height*.18),s=r.width/2+m,f=o/2+m,u=r.y+o/2,n=r.y+r.height-o/2;if(e>=g&&e<=p&&i>=u&&i<=n)return true;let h=e-c,P=i-u;if(h*h/(s*s)+P*P/(f*f)<=1)return true;let L=e-c,A=i-n;return L*L/(s*s)+A*A/(f*f)<=1}return true}function et(e,i,r,m,g,w,p){if(m==="curved"&&g){let a=1-e;return {x:a*a*i.x+2*a*e*g.x+e*e*r.x,y:a*a*i.y+2*a*e*g.y+e*e*r.y}}if(m==="elbow"&&w&&p){let a=1-e,c=a*a,y=c*a,d=e*e,l=d*e;return {x:y*i.x+3*c*e*w.x+3*a*d*p.x+l*r.x,y:y*i.y+3*c*e*w.y+3*a*d*p.y+l*r.y}}return {x:i.x+e*(r.x-i.x),y:i.y+e*(r.y-i.y)}}function st(e,i,r){let m={x:e.startX,y:e.startY},g={x:e.endX,y:e.endY};if(r?.tunneling===false||i.length===0)return [{startX:m.x,startY:m.y,endX:g.x,endY:g.y,isInside:false,isLast:true,arrowType:e.arrowType,controlPoint:e.controlPoint,controlPoint1:e.controlPoint1,controlPoint2:e.controlPoint2}];let w=32,p=[],a=false;for(let l=0;l<=w;l++){let t=l/w,o=et(t,m,g,e.arrowType,e.controlPoint,e.controlPoint1,e.controlPoint2),s=t>.04&&t<.96&&i.some(f=>dt(o.x,o.y,f,2));s&&(a=true),p.push({pt:o,inside:s,t});}if(!a)return [{startX:m.x,startY:m.y,endX:g.x,endY:g.y,isInside:false,isLast:true,arrowType:e.arrowType,controlPoint:e.controlPoint,controlPoint1:e.controlPoint1,controlPoint2:e.controlPoint2}];let c=[],y={points:[p[0]],isInside:p[0].inside};for(let l=1;l<p.length;l++){let t=p[l];t.inside===y.isInside?y.points.push(t):(c.push(y),y={points:[y.points[y.points.length-1],t],isInside:t.inside});}c.push(y);let d=[];return c.forEach((l,t)=>{let o=l.points[0].pt,s=l.points[l.points.length-1].pt,f=t===c.length-1;d.push({startX:Math.round(o.x),startY:Math.round(o.y),endX:Math.round(s.x),endY:Math.round(s.y),isInside:l.isInside,isLast:f,arrowType:"straight"});}),d}var tt={indigo:{stroke:"#4f46e5",fill:"#e0e7ff",text:"#312e81"},blue:{stroke:"#2563eb",fill:"#dbeafe",text:"#1e3a8a"},emerald:{stroke:"#059669",fill:"#d1fae5",text:"#064e3b"},green:{stroke:"#16a34a",fill:"#dcfce7",text:"#14532d"},amber:{stroke:"#d97706",fill:"#fef3c7",text:"#78350f"},yellow:{stroke:"#ca8a04",fill:"#fef9c3",text:"#713f12"},rose:{stroke:"#e11d48",fill:"#ffe4e6",text:"#881337"},red:{stroke:"#dc2626",fill:"#fee2e2",text:"#7f1d1d"},purple:{stroke:"#7c3aed",fill:"#ede9fe",text:"#4c1d95"},violet:{stroke:"#8b5cf6",fill:"#f3e8ff",text:"#581c87"},slate:{stroke:"#475569",fill:"#f1f5f9",text:"#0f172a"},neutral:{stroke:"#525252",fill:"#f5f5f5",text:"#171717"},sky:{stroke:"#0284c7",fill:"#e0f2fe",text:"#0c4a6e"},cyan:{stroke:"#0891b2",fill:"#cffafe",text:"#164e63"},teal:{stroke:"#0d9488",fill:"#ccfbf1",text:"#134e4a"},orange:{stroke:"#ea580c",fill:"#ffedd5",text:"#7c2d12"}};function rt(e,i,r){return i||r?{stroke:i||"#4f46e5",fill:r||"#e0e7ff",text:i||"#1e293b"}:e&&tt[e]?tt[e]:e&&e.startsWith("#")?{stroke:e,fill:`${e}20`,text:e}:tt.indigo}function mt(e){if(!e)return "#475569";let i=e.trim().toLowerCase();return tt[i]?tt[i].stroke:e}function pt(e){switch(e){case "none":return "transparent";case "solid":return "solid";case "cross-hatch":return "cross-hatch";case "dots":return "dots";default:return "hachure"}}function ft(e){if(typeof e=="string")try{let i=JSON.parse(e);if(typeof i!="object"||i===null)throw new Error("Parsed diagram JSON must be an object");return i}catch(i){return console.error("Failed to parse diagram JSON string:",i),{title:"Empty Diagram",nodes:[{id:"error-node",label:"Invalid Diagram JSON",description:String(i),color:"rose"}]}}return e}function it(e,i){let r=ft(e),m={...r.layout,...i},g=ot(r,m),w=[],p=1;g.groups.forEach(t=>{let o=rt(t.color),s=`frame_${t.id}`;w.push({id:s,type:"frame",x:t.x,y:t.y,width:t.width,height:t.height,strokeColor:o.stroke,strokeWidth:1.5,strokeStyle:t.borderStyle||"dashed",fillColor:t.backgroundColor||"transparent",fillStyle:"solid",opacity:.85,zIndex:p++,frameLabel:t.label});});let a=new Map;g.nodes.forEach(t=>{let o=rt(t.color,t.strokeColor,t.fillColor),s=`shape_${t.id}`,f=`group_node_${t.id}`;a.set(t.id,{id:t.id,x:t.x,y:t.y,width:t.width,height:t.height,shapeId:s,shape:t.shape});let u="rectangle",n=false;switch(t.shape){case "diamond":u="diamond";break;case "ellipse":case "circle":u="ellipse";break;case "cylinder":case "database":u="cylinder";break;case "cloud":u="cloud";break;case "sticky":u="sticky";break;case "rounded":u="rectangle",n=true;break;default:u="rectangle",n=false;break}let h=t.description?`${t.label}
|
|
4
|
-
${t.description}`:t.label,P=t.textColor||o.text;if(u==="sticky"?w.push({id:
|
|
5
|
-
export{gt as Diagram,
|
|
3
|
+
`),w=g.reduce((o,i)=>Math.max(o,i.length),0),p=(e.fontSize||14)*.72,s=Math.round(w*p),c=Math.max(a,s+64),y=g.length,d=(e.fontSize||14)*1.45,l=e.badge?14:0,t=Math.max(r,Math.round(y*d+44+l));if(e.shape==="diamond")return {width:e.width??Math.max(Math.round(c*1.65),240),height:e.height??Math.max(Math.round(t*1.55),160)};if(e.shape==="cylinder"||e.shape==="database")return {width:e.width??Math.max(c+32,230),height:e.height??Math.max(t+(e.description?90:75),180)};if(e.shape==="cloud")return {width:e.width??Math.max(Math.round(c*1.45),230),height:e.height??Math.max(Math.round(t*1.4),125)};if(e.shape==="sticky")return {width:e.width??Math.max(c+20,220),height:e.height??Math.max(t+34,140)};if(e.shape==="circle"||e.shape==="ellipse"){let o=Math.max(c*1.25,t*1.25,140);return {width:e.width??o,height:e.height??Math.max(Math.round(t*1.3),110)}}return {width:e.width??Math.max(c+16,210),height:e.height??Math.max(t+12,90)}}function ot(e,a){let r=new Map,m=new Map;e.forEach(c=>{r.set(c,[]),m.set(c,0);}),a.forEach(c=>{r.has(c.from)&&m.has(c.to)&&(r.get(c.from).push(c.to),m.set(c.to,(m.get(c.to)||0)+1));});let g=new Map,w=new Set,p=new Set;function s(c,y){if(p.has(c))return;let d=g.get(c)??0;g.set(c,Math.max(d,y)),p.add(c),w.add(c);let l=r.get(c)||[];for(let t of l)s(t,y+1);p.delete(c);}return e.filter(c=>(m.get(c)||0)===0).forEach(c=>s(c,0)),e.forEach(c=>{w.has(c)||s(c,0);}),g}function at(e,a){let r={...e.layout,...a},m=r.direction||e.direction||"LR",g=r.nodeSpacing??95,w=r.rankSpacing??220,p=r.padding??90,s=r.groupPadding??56,c=r.clusterSpacing??220,y=r.intraRankSpacing??120,d=r.defaultNodeWidth??210,l=r.defaultNodeHeight??90,t=e.nodes||[],o=e.edges||[],i=e.groups||[];if(t.length===0)return {nodes:[],edges:[],groups:[],bounds:{minX:0,minY:0,maxX:0,maxY:0,width:0,height:0}};let f=t.map(b=>{let k=ut(b,d,l);return {...b,width:k.width,height:k.height,x:b.x??0,y:b.y??0,relX:0,relY:0}}),u=new Map;f.forEach(b=>u.set(b.id,b));let n=m==="LR"||m==="RL",h=m==="RL"||m==="BT",P=36;if(!(i.length>0)){let b=ot(t.map(x=>x.id),o),k=new Map;f.forEach(x=>{let A=b.get(x.id)??0;k.has(A)||k.set(A,[]),k.get(A).push(x);});let z=Array.from(k.keys()).sort((x,A)=>x-A),S=p,R=0;return z.forEach(x=>{let W=k.get(x).reduce((O,_,F)=>O+(n?_.height:_.width)+(F>0?g:0),0);R=Math.max(R,W);}),z.forEach(x=>{let A=k.get(x),W=0,O=A.reduce((F,J,K)=>F+(n?J.height:J.width)+(K>0?g:0),0),_=p+(R-O)/2;A.forEach(F=>{W=Math.max(W,n?F.width:F.height),n?(F.x=Math.round(S),F.y=Math.round(_),_+=F.height+g):(F.x=Math.round(_),F.y=Math.round(S),_+=F.width+g);}),S+=W+w;}),lt(f,o,[],h,n,p,S)}let X=new Map;i.forEach(b=>{b.nodeIds.forEach(k=>X.set(k,b.id));});let N=[],Y=new Map;i.forEach(b=>{let k=b.nodeIds.map(D=>u.get(D)).filter(D=>!!D);if(k.length===0)return;let z=new Set(b.nodeIds),S=o.filter(D=>z.has(D.from)&&z.has(D.to)),R=ot(k.map(D=>D.id),S),x=new Map;k.forEach(D=>{let j=R.get(D.id)??0;x.has(j)||x.set(j,[]),x.get(j).push(D);});let A=Array.from(x.keys()).sort((D,j)=>D-j),W=0,O=0;A.forEach(D=>{let q=x.get(D).reduce((V,Z,U)=>V+(n?Z.height:Z.width)+(U>0?g:0),0);O=Math.max(O,q);}),A.forEach(D=>{let j=x.get(D),q=0,V=j.reduce((U,ct,pt)=>U+(n?ct.height:ct.width)+(pt>0?g:0),0),Z=(O-V)/2;j.forEach(U=>{q=Math.max(q,n?U.width:U.height),n?(U.relX=Math.round(W),U.relY=Math.round(Z),Z+=U.height+g):(U.relX=Math.round(Z),U.relY=Math.round(W),Z+=U.width+g);}),W+=q+y;});let _=n?W-y:O,F=n?O:W-y,J=Math.round(_+s*2),K=Math.round(F+s*2+P),H={id:b.id,isGroup:true,groupObj:b,memberNodes:k,width:J,height:K,x:0,y:0};N.push(H),Y.set(b.id,H);}),f.filter(b=>!X.has(b.id)).forEach(b=>{let k={id:`standalone_${b.id}`,isGroup:false,memberNodes:[b],width:b.width,height:b.height,x:0,y:0};N.push(k),Y.set(k.id,k);});let $=b=>{let k=X.get(b);return k||`standalone_${b}`},E=[];o.forEach(b=>{let k=$(b.from),z=$(b.to);k!==z&&E.push({from:k,to:z});});let C=ot(N.map(b=>b.id),E),v=new Map;N.forEach(b=>{let k=C.get(b.id)??0;v.has(k)||v.set(k,[]),v.get(k).push(b);});let T=Array.from(v.keys()).sort((b,k)=>b-k),B=p,L=0;T.forEach(b=>{let z=v.get(b).reduce((S,R,x)=>S+(n?R.height:R.width)+(x>0?c:0),0);L=Math.max(L,z);});let G=[];return T.forEach(b=>{let k=v.get(b),z=0,S=k.reduce((x,A,W)=>x+(n?A.height:A.width)+(W>0?c:0),0),R=p+(L-S)/2;k.forEach(x=>{if(z=Math.max(z,n?x.width:x.height),n?(x.x=Math.round(B),x.y=Math.round(R),R+=x.height+c):(x.x=Math.round(R),x.y=Math.round(B),R+=x.width+c),x.isGroup&&x.groupObj)G.push({...x.groupObj,x:x.x,y:x.y,width:x.width,height:x.height}),x.memberNodes.forEach(A=>{A.x=Math.round(x.x+s+A.relX),A.y=Math.round(x.y+s+P+A.relY);});else {let A=x.memberNodes[0];A.x=x.x,A.y=x.y;}}),B+=z+c;}),lt(f,o,G,h,n,p,B)}function lt(e,a,r,m,g,w,p){if(m){let l=p-95;e.forEach(t=>{g?t.x=l-(t.x-w)-t.width:t.y=l-(t.y-w)-t.height;}),r.forEach(t=>{g?t.x=l-(t.x-w)-t.width:t.y=l-(t.y-w)-t.height;});}let s=1/0,c=1/0,y=-1/0,d=-1/0;return e.forEach(l=>{s=Math.min(s,l.x),c=Math.min(c,l.y),y=Math.max(y,l.x+l.width),d=Math.max(d,l.y+l.height);}),r.forEach(l=>{s=Math.min(s,l.x),c=Math.min(c,l.y),y=Math.max(y,l.x+l.width),d=Math.max(d,l.y+l.height);}),{nodes:e,edges:a,groups:r,bounds:{minX:Math.round(s),minY:Math.round(c),maxX:Math.round(y),maxY:Math.round(d),width:Math.round(y-s),height:Math.round(d-c)}}}function Q(e,a,r,m,g,w=12){let p=g.x-w,s=g.y-w,c=g.x+g.width+w,y=g.y+g.height+w;if(Math.max(e,r)<p||Math.min(e,r)>c||Math.max(a,m)<s||Math.min(a,m)>y)return false;let d=r-e,l=m-a,t=0,o=1,i=[-d,d,-l,l],f=[e-p,c-e,a-s,y-a];for(let u=0;u<4;u++)if(i[u]===0){if(f[u]<0)return false}else {let n=f[u]/i[u];if(i[u]<0){if(n>o)return false;n>t&&(t=n);}else {if(n<t)return false;n<o&&(o=n);}}return t<=o}function st(e,a,r,m){let g=Array.from(a.values()),w=new Map;function p(d,l){w.has(d)||w.set(d,new Map);let t=w.get(d);return t.has(l)||t.set(l,{sourceEdges:[],targetEdges:[]}),t.get(l)}let s=[];e.forEach((d,l)=>{let t=a.get(d.from),o=a.get(d.to);if(!t||!o)return;let i=false;r==="LR"?i=o.x+o.width<=t.x+30:r==="RL"?i=o.x>=t.x+t.width-30:r==="TB"?i=o.y+o.height<=t.y+30:r==="BT"&&(i=o.y>=t.y+t.height-30);let f,u;d.startAnchor?f=d.startAnchor:i?f=r==="LR"||r==="RL"?"top":"right":f=r==="LR"?"right":r==="RL"?"left":r==="TB"?"bottom":"top",d.endAnchor?u=d.endAnchor:i?u=r==="LR"||r==="RL"?"top":"right":u=r==="LR"?"left":r==="RL"?"right":r==="TB"?"top":"bottom",s.push({edge:d,source:t,target:o,startAnchor:f,endAnchor:u,isBackward:i}),p(t.id,f).sourceEdges.push({edge:d,index:l}),p(o.id,u).targetEdges.push({edge:d,index:l});});function c(d,l,t,o){let i=p(d.id,l),f=o?i.targetEdges:i.sourceEdges,u=f.length,n=Math.max(0,f.findIndex(h=>h.index===t));if(l==="left"||l==="right"){let h=l==="left"?d.x:d.x+d.width;if(u<=1)return {x:h,y:d.y+d.height/2};let P=d.height-24,I=Math.min(28,P/(u+1)),X=d.y+12+(P-(u-1)*I)/2,N=Math.round(X+n*I);return {x:h,y:N}}else {let h=l==="top"?d.y:d.y+d.height;if(u<=1)return {x:d.x+d.width/2,y:h};let P=d.width-24,I=Math.min(32,P/(u+1)),X=d.x+12+(P-(u-1)*I)/2;return {x:Math.round(X+n*I),y:h}}}let y=[];return s.forEach(({edge:d,source:l,target:t,startAnchor:o,endAnchor:i,isBackward:f},u)=>{let n=c(l,o,u,false),h=c(t,i,u,true),P=d.id||`edge_${l.id}_${t.id}_${u}`;if(f)if(r==="LR"||r==="RL"){let Y=Math.min(n.x,h.x),M=Math.max(n.x,h.x),E=g.filter(L=>L.id!==l.id&&L.id!==t.id&&L.x+L.width>Y&&L.x<M).reduce((L,G)=>Math.min(L,G.y),Math.min(n.y,h.y)),C=Math.abs(n.x-h.x),v=Math.max(90,Math.min(260,C*.22)),T=Math.min(E-v,Math.min(n.y,h.y)-v),B={x:Math.round((n.x+h.x)/2),y:Math.round(T)};y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"curved",controlPoint:B,isBackward:true});return}else {let Y=Math.min(n.y,h.y),M=Math.max(n.y,h.y),E=g.filter(L=>L.id!==l.id&&L.id!==t.id&&L.y+L.height>Y&&L.y<M).reduce((L,G)=>Math.max(L,G.x+G.width),Math.max(n.x,h.x)),C=Math.abs(n.y-h.y),v=Math.max(90,Math.min(260,C*.22)),T=Math.max(E+v,Math.max(n.x,h.x)+v),B={x:Math.round(T),y:Math.round((n.y+h.y)/2)};y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"curved",controlPoint:B,isBackward:true});return}let I=d.routing||m?.edgeRouting||"elbow",X=Math.abs(h.y-n.y),N=Math.abs(h.x-n.x);if(I==="elbow"){if((r==="LR"||r==="RL")&&X<14){y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"straight",isBackward:false});return}if((r==="TB"||r==="BT")&&N<14){y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"straight",isBackward:false});return}let Y=g.filter(M=>M.id!==l.id&&M.id!==t.id);if(r==="LR"||r==="RL"){let M=Math.round((n.x+h.x)/2),$=[M,M-36,M+36,M-72,M+72,Math.round(n.x+36),Math.round(h.x-36)],E;for(let B of $)if(!Y.some(G=>Q(n.x,n.y,B,n.y,G,8)||Q(B,n.y,B,h.y,G,8)||Q(B,h.y,h.x,h.y,G,8))){E=B;break}if(E!==void 0){y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:E,y:n.y},controlPoint2:{x:E,y:h.y},isBackward:false});return}let C=(n.y+h.y)/2,T=Y.filter(B=>Q(n.x,n.y,h.x,h.y,B,12))[0];if(T){let L=C<T.y+T.height/2?T.y-36:T.y+T.height+36;y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:M,y:n.y},controlPoint2:{x:M,y:L},isBackward:false});return}y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:M,y:n.y},controlPoint2:{x:M,y:h.y},isBackward:false});return}else {let M=Math.round((n.y+h.y)/2),$=[M,M-36,M+36,M-72,M+72,Math.round(n.y+36),Math.round(h.y-36)],E;for(let C of $)if(!Y.some(T=>Q(n.x,n.y,n.x,C,T,8)||Q(n.x,C,h.x,C,T,8)||Q(h.x,C,h.x,h.y,T,8))){E=C;break}if(E!==void 0){y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:n.x,y:E},controlPoint2:{x:h.x,y:E},isBackward:false});return}y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"elbow",controlPoint1:{x:n.x,y:M},controlPoint2:{x:h.x,y:M},isBackward:false});return}}if(I==="curved"){let Y=h.x-n.x,M=h.y-n.y,$=Math.hypot(Y,M),E=Math.round((n.x+h.x)/2),C=Math.round((n.y+h.y)/2),v=Math.max(18,Math.min(38,$*.15)),T=Math.round(v*2),B;Math.abs(Y)>=Math.abs(M)?B={x:E,y:C-T}:B={x:E+T,y:C},y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"curved",controlPoint:B,isBackward:false});return}y.push({edgeId:P,startAnchor:o,endAnchor:i,startX:Math.round(n.x),startY:Math.round(n.y),endX:Math.round(h.x),endY:Math.round(h.y),arrowType:"straight",isBackward:false});}),y}function dt(e,a,r,m=0){let g=r.x-m,w=r.y-m,p=r.x+r.width+m,s=r.y+r.height+m;if(e<g||e>p||a<w||a>s)return false;let c=r.x+r.width/2,y=r.y+r.height/2,d=r.width/2+m,l=r.height/2+m,t=r.shape||"rectangle";if(t==="diamond")return Math.abs(e-c)/d+Math.abs(a-y)/l<=1;if(t==="ellipse"||t==="circle"||t==="cloud")return (e-c)*(e-c)/(d*d)+(a-y)*(a-y)/(l*l)<=1;if(t==="triangle"){let o=r.height<=0?0:Math.max(0,Math.min(1,(a-r.y)/r.height)),i=r.width/2*o;return e>=c-i-m&&e<=c+i+m}if(t==="parallelogram"){let o=Math.min(36,r.width*.22),i=r.height<=0?0:Math.max(0,Math.min(1,(a-r.y)/r.height)),f=r.x+o*(1-i),u=r.x+r.width-o*i;return e>=f-m&&e<=u+m}if(t==="trapezoid"){let o=Math.min(r.width*.2,40),i=r.height<=0?0:Math.max(0,Math.min(1,(a-r.y)/r.height)),f=r.x+o*(1-i),u=r.x+r.width-o*(1-i);return e>=f-m&&e<=u+m}if(t==="hexagon"){let o=Math.min(r.width*.22,45),i,f;if(a<=y){let u=r.height<=0?0:Math.max(0,Math.min(1,(a-r.y)/(r.height/2)));i=r.x+o*(1-u),f=r.x+r.width-o*(1-u);}else {let u=r.height<=0?0:Math.max(0,Math.min(1,(a-y)/(r.height/2)));i=r.x+o*u,f=r.x+r.width-o*u;}return e>=i-m&&e<=f+m}if(t==="capsule")if(r.width>=r.height){let o=r.height/2+m,i=r.x+r.height/2,f=r.x+r.width-r.height/2;return e>=i&&e<=f?a>=r.y-m&&a<=r.y+r.height+m:e<i?Math.hypot(e-i,a-y)<=o:Math.hypot(e-f,a-y)<=o}else {let o=r.width/2+m,i=r.y+r.width/2,f=r.y+r.height-r.width/2;return a>=i&&a<=f?e>=r.x-m&&e<=r.x+r.width+m:a<i?Math.hypot(e-c,a-i)<=o:Math.hypot(e-c,a-f)<=o}if(t==="cylinder"){let o=Math.min(24,r.height*.18),i=r.width/2+m,f=o/2+m,u=r.y+o/2,n=r.y+r.height-o/2;if(e>=g&&e<=p&&a>=u&&a<=n)return true;let h=e-c,P=a-u;if(h*h/(i*i)+P*P/(f*f)<=1)return true;let I=e-c,X=a-n;return I*I/(i*i)+X*X/(f*f)<=1}return true}function rt(e,a,r,m,g,w,p){if(m==="curved"&&g){let s=1-e;return {x:s*s*a.x+2*s*e*g.x+e*e*r.x,y:s*s*a.y+2*s*e*g.y+e*e*r.y}}if(m==="elbow"&&w&&p){let s=1-e,c=s*s,y=c*s,d=e*e,l=d*e;return {x:y*a.x+3*c*e*w.x+3*s*d*p.x+l*r.x,y:y*a.y+3*c*e*w.y+3*s*d*p.y+l*r.y}}return {x:a.x+e*(r.x-a.x),y:a.y+e*(r.y-a.y)}}function it(e,a,r){let m={x:e.startX,y:e.startY},g={x:e.endX,y:e.endY};if(r?.tunneling===false||a.length===0)return [{startX:m.x,startY:m.y,endX:g.x,endY:g.y,isInside:false,isLast:true,arrowType:e.arrowType,controlPoint:e.controlPoint,controlPoint1:e.controlPoint1,controlPoint2:e.controlPoint2}];let w=32,p=[],s=false;for(let l=0;l<=w;l++){let t=l/w,o=rt(t,m,g,e.arrowType,e.controlPoint,e.controlPoint1,e.controlPoint2),i=t>.04&&t<.96&&a.some(f=>dt(o.x,o.y,f,2));i&&(s=true),p.push({pt:o,inside:i,t});}if(!s)return [{startX:m.x,startY:m.y,endX:g.x,endY:g.y,isInside:false,isLast:true,arrowType:e.arrowType,controlPoint:e.controlPoint,controlPoint1:e.controlPoint1,controlPoint2:e.controlPoint2}];let c=[],y={points:[p[0]],isInside:p[0].inside};for(let l=1;l<p.length;l++){let t=p[l];t.inside===y.isInside?y.points.push(t):(c.push(y),y={points:[y.points[y.points.length-1],t],isInside:t.inside});}c.push(y);let d=[];return c.forEach((l,t)=>{let o=l.points[0].pt,i=l.points[l.points.length-1].pt,f=t===c.length-1;d.push({startX:Math.round(o.x),startY:Math.round(o.y),endX:Math.round(i.x),endY:Math.round(i.y),isInside:l.isInside,isLast:f,arrowType:"straight"});}),d}var tt={indigo:{stroke:"#4f46e5",fill:"#e0e7ff",text:"#312e81"},blue:{stroke:"#2563eb",fill:"#dbeafe",text:"#1e3a8a"},emerald:{stroke:"#059669",fill:"#d1fae5",text:"#064e3b"},green:{stroke:"#16a34a",fill:"#dcfce7",text:"#14532d"},amber:{stroke:"#d97706",fill:"#fef3c7",text:"#78350f"},yellow:{stroke:"#ca8a04",fill:"#fef9c3",text:"#713f12"},rose:{stroke:"#e11d48",fill:"#ffe4e6",text:"#881337"},red:{stroke:"#dc2626",fill:"#fee2e2",text:"#7f1d1d"},purple:{stroke:"#7c3aed",fill:"#ede9fe",text:"#4c1d95"},violet:{stroke:"#8b5cf6",fill:"#f3e8ff",text:"#581c87"},slate:{stroke:"#475569",fill:"#f1f5f9",text:"#0f172a"},neutral:{stroke:"#525252",fill:"#f5f5f5",text:"#171717"},sky:{stroke:"#0284c7",fill:"#e0f2fe",text:"#0c4a6e"},cyan:{stroke:"#0891b2",fill:"#cffafe",text:"#164e63"},teal:{stroke:"#0d9488",fill:"#ccfbf1",text:"#134e4a"},orange:{stroke:"#ea580c",fill:"#ffedd5",text:"#7c2d12"}};function nt(e,a,r){return a||r?{stroke:a||"#4f46e5",fill:r||"#e0e7ff",text:a||"#1e293b"}:e&&tt[e]?tt[e]:e&&e.startsWith("#")?{stroke:e,fill:`${e}20`,text:e}:tt.indigo}function mt(e){if(!e)return "#475569";let a=e.trim().toLowerCase();return tt[a]?tt[a].stroke:e}function bt(e){switch(e){case "none":return "transparent";case "solid":return "solid";case "cross-hatch":return "cross-hatch";case "dots":return "dots";default:return "hachure"}}function ft(e){if(typeof e=="string")try{let a=JSON.parse(e);if(typeof a!="object"||a===null)throw new Error("Parsed diagram JSON must be an object");return a}catch(a){return console.error("Failed to parse diagram JSON string:",a),{title:"Empty Diagram",nodes:[{id:"error-node",label:"Invalid Diagram JSON",description:String(a),color:"rose"}]}}return e}function et(e,a){let r=ft(e),m={...r.layout,...a},g=at(r,m),w=[],p=1;g.groups.forEach(t=>{let o=nt(t.color),i=`frame_${t.id}`;w.push({id:i,type:"frame",x:t.x,y:t.y,width:t.width,height:t.height,strokeColor:o.stroke,strokeWidth:1.5,strokeStyle:t.borderStyle||"dashed",fillColor:t.backgroundColor||"transparent",fillStyle:"solid",opacity:.85,zIndex:p++,frameLabel:t.label});});let s=new Map;g.nodes.forEach(t=>{let o=nt(t.color,t.strokeColor,t.fillColor),i=`shape_${t.id}`,f=`group_node_${t.id}`;s.set(t.id,{id:t.id,x:t.x,y:t.y,width:t.width,height:t.height,shapeId:i,shape:t.shape});let u="rectangle",n=false;switch(t.shape){case "diamond":u="diamond";break;case "ellipse":case "circle":u="ellipse";break;case "cylinder":case "database":u="cylinder";break;case "cloud":u="cloud";break;case "sticky":u="sticky";break;case "rounded":u="rectangle",n=true;break;default:u="rectangle",n=false;break}let h=t.description?`${t.label}
|
|
4
|
+
${t.description}`:t.label,P=t.textColor||o.text;if(u==="sticky"?w.push({id:i,type:"sticky",x:t.x,y:t.y,width:t.width,height:t.height,strokeColor:o.stroke,strokeWidth:t.strokeWidth||1.5,strokeStyle:t.strokeStyle||"solid",fillColor:o.fill,fillStyle:"solid",stickyColor:o.fill,text:h,textColor:P,fontSize:t.fontSize||14,fontFamily:t.fontFamily||"fuzzy",textAlign:t.textAlign||"center",opacity:t.opacity??1,zIndex:p++,roughness:t.roughness??1}):w.push({id:i,type:u,x:t.x,y:t.y,width:t.width,height:t.height,strokeColor:o.stroke,strokeWidth:t.strokeWidth||2,strokeStyle:t.strokeStyle||"solid",fillColor:o.fill,fillStyle:bt(t.fillStyle),text:h,textColor:P,fontSize:t.fontSize||13.5,fontFamily:t.fontFamily||"sans",textAlign:t.textAlign||"center",opacity:t.opacity??1,zIndex:p++,edgeStyle:n?"round":"sharp",roughness:t.roughness??1,groupIds:[f]}),t.badge){let I=`badge_${t.id}`,X=Math.max(45,t.badge.length*8+14),N=20,Y=t.x+t.width-X-8,M=t.y-8;u==="diamond"?(Y=Math.round(t.x+t.width/2-X/2),M=Math.round(t.y-N-4)):u==="cylinder"?(Y=Math.round(t.x+t.width-X-14),M=Math.round(t.y-6)):u==="cloud"&&(Y=Math.round(t.x+t.width-X-24),M=Math.round(t.y+2)),w.push({id:I,type:"rectangle",x:Y,y:M,width:X,height:N,strokeColor:o.stroke,strokeWidth:1.5,strokeStyle:"solid",fillColor:"#e2e8f0",fillStyle:"solid",text:t.badge,textColor:o.stroke,fontSize:10,fontFamily:"mono",textAlign:"center",opacity:1,zIndex:p++,edgeStyle:"round",groupIds:[f]});}});let c=a?.direction||r.direction||"LR",y=[];function d(t,o=8){for(let i of y)if(t.x<i.x+i.width+o&&t.x+t.width+o>i.x&&t.y<i.y+i.height+o&&t.y+t.height+o>i.y)return true;return false}let l=st(g.edges,s,c,m);return g.edges.forEach((t,o)=>{let i=s.get(t.from),f=s.get(t.to);if(!i||!f)return;let u=l[o],n=u?.startAnchor||"right",h=u?.endAnchor||"left",P=u?.startX??Math.round(i.x+i.width),I=u?.startY??Math.round(i.y+i.height/2),X=u?.endX??Math.round(f.x),N=u?.endY??Math.round(f.y+f.height/2),Y=u?.arrowType??"straight",M=u?.controlPoint,$=u?.controlPoint1,E=u?.controlPoint2,C=t.id||`edge_${t.from}_${t.to}_${o}`,v=X-P,T=N-I,B=Array.from(s.values()).filter(S=>S.id!==i.id&&S.id!==f.id),G=t.tunneling!==false&&m.tunneling!==false&&u?it(u,B,m):[{startX:P,startY:I,endX:X,endY:N,isInside:false,isLast:true,arrowType:Y,controlPoint:M,controlPoint1:$,controlPoint2:E}],b=m.passthroughOpacity??.35,k=m.passthroughStyle??"dotted",z=!!t.label;if(G.forEach((S,R)=>{let x=S.endX-S.startX,A=S.endY-S.startY,W=G.length===1?C:`${C}_sub_${R}`,O=S.isInside,_=O?k:t.style||"solid",F=O?b:1,J=R===0?{elementId:i.shapeId,anchor:n}:void 0,K=S.isLast?{elementId:f.shapeId,anchor:h}:void 0,H=S.isLast&&t.arrowEnd!==false?"arrow":"none",D=R===0&&t.arrowStart?"arrow":"none";w.push({id:W,type:S.isLast?"arrow":"line",x:S.startX,y:S.startY,width:x,height:A,arrowType:S.arrowType,controlPoint:S.controlPoint,controlPoint1:S.controlPoint1,controlPoint2:S.controlPoint2,strokeColor:mt(t.color),strokeWidth:t.strokeWidth||2,strokeStyle:_,fillColor:"transparent",fillStyle:"solid",opacity:F,zIndex:O?2:p++,roughness:t.roughness??1,startBinding:J,endBinding:K,endArrowhead:H,startArrowhead:D,label:z&&S.isLast?t.label:void 0});}),t.label&&!z){let S=Math.max(60,t.label.length*7.5+20),R=22,x={x:P,y:I},A={x:X,y:N},W=[.5,.35,.65,.22,.78,.15,.85],O=Math.round(P+v/2),_=Math.round(I+T/2),F=false;for(let K of W){let H=rt(K,x,A,Y,M,$,E),D={x:Math.round(H.x-S/2),y:Math.round(H.y-R/2),width:S,height:R},j=d(D,6),q=Array.from(s.values()).some(V=>D.x<V.x+V.width+4&&D.x+D.width+4>V.x&&D.y<V.y+V.height+4&&D.y+D.height+4>V.y);if(!j&&!q){O=Math.round(H.x),_=Math.round(H.y),F=true;break}}if(!F){let K=rt(.5,x,A,Y,M,$,E);O=Math.round(K.x),_=Math.round(K.y);let H={x:Math.round(O-S/2),y:Math.round(_-R/2),width:S,height:R};for(let D of [-24,24,-44,44,-64,64]){let j=c==="TB"||c==="BT"?{...H,x:H.x+D}:{...H,y:H.y+D};if(!d(j,4)){c==="TB"||c==="BT"?O+=D:_+=D;break}}}let J={x:Math.round(O-S/2),y:Math.round(_-R/2),width:S,height:R};y.push(J),w.push({id:`label_${C}`,type:"rectangle",x:J.x,y:J.y,width:S,height:R,strokeColor:"#cbd5e1",strokeWidth:1,strokeStyle:"solid",fillColor:"#e2e8f0",fillStyle:"solid",text:t.label,textColor:"#0f172a",fontSize:10.5,fontFamily:"sans",textAlign:"center",opacity:.96,zIndex:p++,edgeStyle:"round"});}}),{version:1,name:r.title||"System Architecture",elements:w,canvasBackground:r.canvasBackground||"#f0f4f8",gridStyle:r.gridStyle||"dots",createdAt:Date.now(),updatedAt:Date.now()}}function wt(e){switch(e){case 1:return "fuzzy";case 2:return "sans";case 3:return "mono";default:return "sans"}}function Mt(e){switch(e){case "rectangle":return "rectangle";case "diamond":return "diamond";case "ellipse":return "ellipse";case "arrow":return "arrow";case "line":return "line";case "text":return "text";case "freedraw":return "pen";case "image":return "image";case "frame":return "frame";default:return "rectangle"}}function ht(e,a="Imported Excalidraw"){let r;if(typeof e=="string")try{r=JSON.parse(e);}catch{return {version:1,name:a,elements:[],canvasBackground:"#ffffff",gridStyle:"none",createdAt:Date.now(),updatedAt:Date.now()}}else r=e;let g=(Array.isArray(r)?r:Array.isArray(r.elements)?r.elements:[]).filter(s=>!s.isDeleted),w=[];g.forEach((s,c)=>{let y=typeof s.opacity=="number"?s.opacity/100:1,d=s.strokeWidth||2,l=s.strokeColor&&s.strokeColor!=="transparent"?s.strokeColor:"#1e293b",t=s.backgroundColor&&s.backgroundColor!=="transparent"?s.backgroundColor:"transparent",o="solid";s.fillStyle==="hachure"?o="hachure":s.fillStyle==="cross-hatch"?o="cross-hatch":s.fillStyle==="dots"?o="dots":t==="transparent"&&(o="transparent");let i=s.strokeStyle==="dashed"?"dashed":s.strokeStyle==="dotted"?"dotted":"solid",f={id:s.id||`excal_${c}`,type:Mt(s.type),x:Math.round(s.x),y:Math.round(s.y),width:Math.round(s.width),height:Math.round(s.height),rotation:s.angle||0,strokeColor:l,strokeWidth:d,strokeStyle:i,fillColor:t,fillStyle:o,opacity:y,zIndex:c+1,roughness:typeof s.roughness=="number"?s.roughness:1,text:s.text,fontSize:s.fontSize||18,fontFamily:wt(s.fontFamily),textAlign:s.textAlign||"left",groupIds:s.groupIds};s.type==="freedraw"&&Array.isArray(s.points)&&(f.points=s.points.map(([u,n])=>({x:u,y:n}))),(s.type==="arrow"||s.type==="line")&&(f.endArrowhead=s.endArrowhead?"arrow":"none",f.startArrowhead=s.startArrowhead?"arrow":"none",s.startBinding?.elementId&&(f.startBinding={elementId:s.startBinding.elementId,anchor:"center"}),s.endBinding?.elementId&&(f.endBinding={elementId:s.endBinding.elementId,anchor:"center"})),w.push(f);});let p=Array.isArray(r)?void 0:r.appState;return {version:1,name:a,elements:w,canvasBackground:p?.viewBackgroundColor||"#ffffff",gridStyle:p?.gridSize?"grid":"none",createdAt:Date.now(),updatedAt:Date.now()}}function Et(e){if(typeof e=="string")return e.includes('"type":"excalidraw"')||e.includes('"source":"https://excalidraw.com"');if(typeof e=="object"&&e!==null){if("type"in e&&e.type==="excalidraw")return true;if("elements"in e&&Array.isArray(e.elements)){let a=e.elements[0];if(a&&typeof a=="object"&&a!==null&&"roughness"in a&&"strokeColor"in a)return true}}return false}var gt=forwardRef(function({data:a,mode:r="viewer",layoutOptions:m,uiConfig:g,theme:w="system",canvasBackground:p,gridStyle:s,interactive:c=true,autoFit:y=true,showCopyButton:d=true,copyFormat:l="elements",showZoomControls:t=true,title:o,panelVariant:i="classic",storageKey:f=null,onExit:u,guideHref:n="https://sketch.filekitstudio.com/docs",onOpenGuide:h,onChange:P,controllerRef:I,className:X="",style:N},Y){let M=typeof a=="string"?a:JSON.stringify(a),$=m?JSON.stringify(m):"",E=useMemo(()=>Et(a)?ht(a,o||"Imported Diagram"):et(a,m),[M,$,o]),C=p??(typeof a=="object"&&a!==null&&"canvasBackground"in a&&a.canvasBackground?a.canvasBackground:void 0),v=s??E.gridStyle;return r==="viewer"?jsx(SketchViewer,{ref:Y,controllerRef:I,value:E.elements,theme:w,canvasBackground:C,gridStyle:v,interactive:c,autoFit:y,showCopyButton:d,copyFormat:l,showZoomControls:t,title:o??E.name,uiConfig:g,className:X,style:N}):jsx(SketchEditor,{ref:Y,controllerRef:I,value:E.elements,boardName:o??E.name,theme:w,canvasBackground:C,gridStyle:v,panelVariant:i,storageKey:f,uiConfig:g,onExit:u,guideHref:n,onOpenGuide:h,onChange:P,onPastePayload:(T,B)=>{if(B&&typeof B=="object"&&"nodes"in B&&Array.isArray(B.nodes))try{return et(B,m).elements}catch{return null}return null},className:X,style:N})}),Bt=gt;typeof registerPasteConverter=="function"&®isterPasteConverter((e,a)=>{if(a&&typeof a=="object"&&"nodes"in a&&Array.isArray(a.nodes))try{return et(a).elements}catch(r){return console.warn("[@filekit-studio/diagram] Failed to compile pasted diagram data:",r),null}return null});
|
|
5
|
+
export{gt as Diagram,Bt as SketchDiagram,tt as THEME_PALETTES,it as decomposePassthroughSegments,et as diagramToSketch,ut as estimateNodeDimensions,ht as excalidrawToSketch,dt as isPointInsideShape,at as layoutDiagram,ft as parseDiagramData,nt as resolveNodeColors,st as routeDiagramEdges,rt as sampleEdgePoint};
|