drawui-core 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +17 -17
- package/dist/index.d.cts +35 -6
- package/dist/index.d.ts +35 -6
- package/dist/index.js +17 -17
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
'use strict';var q={identity(){return {a:1,b:0,c:0,d:1,e:0,f:0}},translation(a,e){return {a:1,b:0,c:0,d:1,e:a,f:e}},rotation(a){let e=Math.cos(a),t=Math.sin(a);return {a:e,b:t,c:-t,d:e,e:0,f:0}},scale(a,e){return {a,b:0,c:0,d:e,e:0,f:0}},multiply(a,e){return {a:a.a*e.a+a.b*e.c,b:a.a*e.b+a.b*e.d,c:a.c*e.a+a.d*e.c,d:a.c*e.b+a.d*e.d,e:a.e*e.a+a.f*e.c+e.e,f:a.e*e.b+a.f*e.d+e.f}},apply(a,e){return {x:a.a*e.x+a.c*e.y+a.e,y:a.b*e.x+a.d*e.y+a.f}},invert(a){let e=a.a*a.d-a.b*a.c;if(Math.abs(e)<1e-10)return null;let t=1/e;return {a:a.d*t,b:-a.b*t,c:-a.c*t,d:a.a*t,e:(a.c*a.f-a.d*a.e)*t,f:(a.b*a.e-a.a*a.f)*t}},createCameraMatrix(a){let e=q.scale(a.zoom,a.zoom),t=q.translation(a.x,a.y);return q.multiply(t,e)}};var se=class{x=0;y=0;zoom=1;minZoom=.1;maxZoom=10;getMatrix(){return q.createCameraMatrix({x:this.x,y:this.y,zoom:this.zoom})}getInverseMatrix(){return q.invert(this.getMatrix())}screenToPage(e){let t=this.getInverseMatrix();return t?q.apply(t,e):e}pageToScreen(e){return q.apply(this.getMatrix(),e)}translate(e,t){this.x+=e,this.y+=t;}zoomAt(e,t){let i=this.screenToPage(e);this.zoom=Math.max(this.minZoom,Math.min(this.maxZoom,this.zoom*t));let n=this.pageToScreen(i);this.x+=e.x-n.x,this.y+=e.y-n.y;}reset(){this.x=0,this.y=0,this.zoom=1;}getZoom(){return this.zoom}fitBounds(e,t=50){let i=window.innerWidth,n=window.innerHeight,s=(i-t*2)/e.width,r=(n-t*2)/e.height,o=Math.min(s,r,this.maxZoom);this.zoom=Math.max(this.minZoom,o),this.x=(i-e.width*this.zoom)/2-e.x*this.zoom,this.y=(n-e.height*this.zoom)/2-e.y*this.zoom;}};var ht={primary:"#3b82f6",selection:{outline:"#3b82f6",handleStroke:"#3b82f6",handleFill:"#ffffff",outlineFill:"rgba(59, 130, 246, 0.1)",shadow:"#3b82f6"},grid:"#e5e7eb",text:"#1f2937",editBorder:"#3b82f6",brushFill:"rgba(59, 130, 246, 0.08)",brushStroke:"rgba(59, 130, 246, 0.8)",guide:"rgba(59, 130, 246, 0.6)",lineHighlight:"rgba(59, 130, 246, 0.4)",genDot:"#3b82f6",genLine:"rgba(59, 130, 246, 0.55)"},L={...ht,selection:{...ht.selection}},ze=new Set;function Tt(a){return ze.add(a),()=>ze.delete(a)}function Ie(a){a.selection&&Object.assign(L.selection,a.selection);for(let e of Object.keys(a)){if(e==="selection")continue;let t=a[e];t!==void 0&&(L[e]=t);}ze.forEach(e=>e());}function It(){return L}function F(a,e){let t=lt(a);if(!t)return a;let i=parseInt(t,16),n=i>>16&255,s=i>>8&255,r=i&255;return `rgba(${n}, ${s}, ${r}, ${e})`}function Lt(a,e){let t=lt(a);if(!t)return a;let i=parseInt(t,16),n=e<0?0:255,s=Math.abs(e),r=o=>Math.round((n-o)*s+o).toString(16).padStart(2,"0");return `#${r(i>>16&255)}${r(i>>8&255)}${r(i&255)}`}function lt(a){let e=a.replace("#","").trim();return /^[0-9a-fA-F]{3}$|^[0-9a-fA-F]{6}$/.test(e)?e.length===3?e.split("").map(t=>t+t).join(""):e:null}var V=class{context;constructor(e){this.context=e;}getCursor(e){return null}dispose(){}};var ct={"zh-CN":{"save.success":"\u5DF2\u4FDD\u5B58","save.fail":"\u4FDD\u5B58\u5931\u8D25","tool.select":"\u9009\u62E9\u5DE5\u5177","tool.hand":"\u62D6\u62FD\u89C6\u53E3","tool.rectangle":"\u77E9\u5F62","tool.rounded-rectangle":"\u5706\u89D2\u77E9\u5F62","tool.circle":"\u5706\u5F62","tool.ellipse":"\u692D\u5706","tool.diamond":"\u83F1\u5F62","tool.triangle":"\u4E09\u89D2\u5F62","tool.hexagon":"\u516D\u8FB9\u5F62","tool.parallelogram":"\u5E73\u884C\u56DB\u8FB9\u5F62","tool.trapezoid":"\u68AF\u5F62","tool.pentagon":"\u4E94\u8FB9\u5F62","tool.octagon":"\u516B\u8FB9\u5F62","tool.star":"\u661F\u5F62","tool.cylinder":"\u5706\u67F1\u4F53","tool.cloud":"\u4E91\u5F62","tool.heart":"\u5FC3\u5F62","tool.block-arrow-right":"\u53F3\u7BAD\u5934","tool.block-arrow-left":"\u5DE6\u7BAD\u5934","tool.block-arrow-up":"\u4E0A\u7BAD\u5934","tool.block-arrow-down":"\u4E0B\u7BAD\u5934","tool.block-arrow-double":"\u53CC\u5411\u7BAD\u5934","tool.right-triangle":"\u76F4\u89D2\u4E09\u89D2\u5F62","tool.cross":"\u5341\u5B57","tool.speech-rect":"\u65B9\u5F62\u5BF9\u8BDD\u6846","tool.speech-round":"\u5706\u5F62\u5BF9\u8BDD\u6846","tool.cube":"\u7ACB\u65B9\u4F53","tool.sector":"\u6247\u5F62","tool.delay":"D \u5F62","tool.predefined-process":"\u9884\u5B9A\u4E49\u6D41\u7A0B","tool.document":"\u6587\u6863","tool.stadium":"\u5168\u5706\u89D2\u77E9\u5F62","tool.class-catalog":"\u7C7B\u76EE","class.plain":"\u7C7B","flow.process":"\u5904\u7406","flow.step":"\u6D41\u7A0B","flow.startEnd":"\u5F00\u59CB/\u7ED3\u675F","flow.decision":"\u5224\u5B9A","flow.data":"\u6570\u636E","flow.manual":"\u624B\u52A8\u64CD\u4F5C","flow.preparation":"\u51C6\u5907","flow.database":"\u6570\u636E\u5E93","flow.delay":"\u5EF6\u65F6","shapeGroup.basic":"\u57FA\u7840","shapeGroup.flowchart":"\u6D41\u7A0B\u56FE","shapeGroup.other":"\u5176\u4ED6","shapeGroup.uml":"\u7C7B\u56FE","shapeMenu.hint":"\u60AC\u505C\u67E5\u770B\u540D\u79F0","tool.chevron":"\u5C16\u89D2","tool.brace-left":"\u5DE6\u62EC\u53F7","tool.brace-right":"\u53F3\u62EC\u53F7","tool.uml-class":"UML \u7C7B\u56FE","tool.uml-interface":"UML \u63A5\u53E3","tool.line":"\u76F4\u7EBF","tool.arrow":"\u7EBF\u6761","tool.text":"\u6587\u672C","tool.draw":"\u753B\u7B14","tool.pen":"\u753B\u7B14","tool.highlighter":"\u8367\u5149\u7B14","tool.eraser":"\u6A61\u76AE\u64E6","status.none":"\u672A\u9009\u62E9","status.selected":"\u5DF2\u9009\u62E9 {n} \u4E2A\u56FE\u5F62","toolbar.select":"\u9009\u62E9 (V)","toolbar.hand":"\u62D6\u62FD\u89C6\u53E3 (H)","toolbar.shapes":"\u5F62\u72B6","toolbar.grid":"\u7F51\u683C","toolbar.undo":"\u64A4\u9500 (Ctrl+Z)","toolbar.redo":"\u91CD\u505A (Ctrl+Shift+Z)","toolbar.arrow":"\u7BAD\u5934","toolbar.text":"\u6587\u672C","toolbar.pen":"\u753B\u7B14","toolbar.highlighter":"\u8367\u5149\u7B14","toolbar.eraser":"\u6A61\u76AE\u64E6","toolbar.uml-class":"UML \u7C7B\u56FE","toolbar.uml-interface":"UML \u63A5\u53E3","zoom.in":"\u653E\u5927","zoom.out":"\u7F29\u5C0F","zoom.reset":"\u91CD\u7F6E","style.stroke":"\u8FB9\u6846","style.line":"\u7EBF\u6761","style.fill":"\u586B\u5145","style.text":"\u6587\u5B57","style.highlight":"\u9AD8\u4EAE","style.prefix":"\u524D\u7F00\u7B26\u53F7","style.prefix.soon":"\u524D\u7F00\u7B26\u53F7\u5F00\u53D1\u4E2D\uFF0C\u656C\u8BF7\u671F\u5F85","style.none":"\u65E0","style.addColor":"\u6DFB\u52A0\u989C\u8272","style.connection":"\u8FDE\u7EBF","style.arrow":"\u7BAD\u5934","style.dashed":"\u865A\u7EBF","style.solid":"\u5B9E\u7EBF","style.opacity":"\u4E0D\u900F\u660E\u5EA6","style.connection.straight":"\u76F4\u7EBF","style.connection.polyline":"\u6298\u7EBF","style.connection.orthogonal":"\u76F4\u89D2\u6298\u7EBF","style.connection.curve":"\u66F2\u7EBF","style.arrowhead.none":"\u65E0\u7BAD\u5934","style.arrowhead.end":"\u7EC8\u70B9\u7BAD\u5934","style.arrowhead.start":"\u8D77\u70B9\u7BAD\u5934","style.arrowhead.both":"\u53CC\u7BAD\u5934","style.arrowhead.none-dashed":"\u65E0\u7BAD\u5934\u865A\u7EBF","style.arrowhead.both-dashed":"\u53CC\u7BAD\u5934\u865A\u7EBF","style.uml":"UML","style.arrowhead.inheritance":"\u7EE7\u627F","style.arrowhead.composition":"\u7EC4\u5408","style.arrowhead.aggregation":"\u805A\u5408","style.arrowhead.dependency":"\u4F9D\u8D56","style.arrowhead.realization":"\u5B9E\u73B0","menu.title":"\u9009\u62E9\u65B0\u56FE\u5F62","menu.cancel":"\u70B9\u51FB\u7A7A\u767D\u5904\u53D6\u6D88","shape.rectangle":"\u77E9\u5F62","shape.rounded-rectangle":"\u5706\u89D2","shape.circle":"\u5706\u5F62","shape.ellipse":"\u692D\u5706","shape.diamond":"\u83F1\u5F62","shape.triangle":"\u4E09\u89D2","shape.hexagon":"\u516D\u8FB9\u5F62","shape.parallelogram":"\u5E73\u884C","shape.trapezoid":"\u68AF\u5F62","shape.pentagon":"\u4E94\u8FB9\u5F62","shape.octagon":"\u516B\u8FB9\u5F62","shape.star":"\u661F\u5F62","shape.uml-class":"\u7C7B\u56FE","shape.uml-interface":"\u63A5\u53E3","uml.drag":"\u62D6\u62FD\u6392\u5E8F","uml.delete":"\u5220\u9664\u8BE5\u884C","uml.textColor":"\u6587\u5B57\u989C\u8272","uml.defaultColor":"\u9ED8\u8BA4\u989C\u8272","uml.fontSize":"\u5B57\u53F7","uml.strikethrough":"\u5220\u9664\u7EBF","uml.attribute":"\u5C5E\u6027","uml.method":"\u65B9\u6CD5","minimap.title":"\u5C0F\u5730\u56FE","minimap.expand":"\u5C55\u5F00\u5C0F\u5730\u56FE","minimap.collapse":"\u6536\u8D77\u5C0F\u5730\u56FE","tool.mind-map":"\u601D\u7EF4\u5BFC\u56FE","toolbar.mind-map":"\u601D\u7EF4\u5BFC\u56FE","toolbar.export":"\u5BFC\u51FA","toolbar.export.png":"PNG \u56FE\u7247","toolbar.export.svg":"SVG \u77E2\u91CF\u56FE","toolbar.export.pdf":"PDF \u6587\u6863","shape.mind-map":"\u601D\u7EF4\u5BFC\u56FE","mindmap.defaultRoot":"\u4E2D\u5FC3\u4E3B\u9898","mindmap.defaultBranch":"\u5206\u652F\u4E3B\u9898","mindmap.nodeText":"\u8282\u70B9\u6587\u672C","mindmap.addChild":"\u5B50\u8282\u70B9","mindmap.addSibling":"\u5144\u5F1F\u8282\u70B9","mindmap.deleteNode":"\u5220\u9664\u8282\u70B9","mindmap.color":"\u989C\u8272","mindmap.levelColor":"\u9ED8\u8BA4\u5C42\u7EA7\u8272","mindmap.icon":"\u56FE\u6807","mindmap.noIcon":"\u65E0\u56FE\u6807","mindmap.note":"\u5C5E\u6027\u8BF4\u660E","mindmap.notePlaceholder":"\u8BF4\u660E\u5185\u5BB9\uFF08\u53EF\u9009\uFF09","mindmap.noteColor":"\u8BF4\u660E\u989C\u8272","mindmap.noteColorDefault":"\u540C\u8282\u70B9\u8272","mindmap.collapse":"\u6298\u53E0","mindmap.expand":"\u5C55\u5F00","mindmap.close":"\u5173\u95ED\u7F16\u8F91","mindmap.more":"\u66F4\u591A\u8BBE\u7F6E"},"en-US":{"save.success":"Saved","save.fail":"Save failed","tool.select":"Select tool","tool.hand":"Pan view","tool.rectangle":"Rectangle","tool.rounded-rectangle":"Rounded rectangle","tool.circle":"Circle","tool.ellipse":"Ellipse","tool.diamond":"Diamond","tool.triangle":"Triangle","tool.hexagon":"Hexagon","tool.parallelogram":"Parallelogram","tool.trapezoid":"Trapezoid","tool.pentagon":"Pentagon","tool.octagon":"Octagon","tool.star":"Star","tool.cylinder":"Cylinder","tool.cloud":"Cloud","tool.heart":"Heart","tool.block-arrow-right":"Arrow right","tool.block-arrow-left":"Arrow left","tool.block-arrow-up":"Arrow up","tool.block-arrow-down":"Arrow down","tool.block-arrow-double":"Double arrow","tool.right-triangle":"Right triangle","tool.cross":"Cross","tool.speech-rect":"Speech bubble","tool.speech-round":"Round bubble","tool.cube":"Cube","tool.sector":"Sector","tool.delay":"Delay","tool.predefined-process":"Predefined process","tool.document":"Document","tool.stadium":"Stadium","tool.class-catalog":"Category","class.plain":"Class","flow.process":"Process","flow.step":"Step","flow.startEnd":"Start / End","flow.decision":"Decision","flow.data":"Data","flow.manual":"Manual operation","flow.preparation":"Preparation","flow.database":"Database","flow.delay":"Delay","shapeGroup.basic":"Basic","shapeGroup.flowchart":"Flowchart","shapeGroup.other":"Other","shapeGroup.uml":"Class diagram","shapeMenu.hint":"Hover to see name","tool.chevron":"Chevron","tool.brace-left":"Left brace","tool.brace-right":"Right brace","tool.uml-class":"UML class","tool.uml-interface":"UML interface","tool.line":"Line","tool.arrow":"Line","tool.text":"Text","tool.draw":"Pen","tool.pen":"Pen","tool.highlighter":"Highlighter","tool.eraser":"Eraser","status.none":"Nothing selected","status.selected":"{n} shapes selected","toolbar.select":"Select (V)","toolbar.hand":"Pan view (H)","toolbar.shapes":"Shapes","toolbar.grid":"Grid","toolbar.undo":"Undo (Ctrl+Z)","toolbar.redo":"Redo (Ctrl+Shift+Z)","toolbar.arrow":"Arrow","toolbar.text":"Text","toolbar.pen":"Pen","toolbar.highlighter":"Highlighter","toolbar.eraser":"Eraser","toolbar.uml-class":"UML class","toolbar.uml-interface":"UML interface","zoom.in":"Zoom in","zoom.out":"Zoom out","zoom.reset":"Reset","style.stroke":"Border","style.line":"Line","style.fill":"Fill","style.text":"Text","style.highlight":"Highlight","style.prefix":"Prefix","style.prefix.soon":"Prefix symbols are coming soon","style.none":"None","style.addColor":"Add color","style.connection":"Connection","style.arrow":"Arrow head","style.dashed":"Dashed","style.solid":"Solid","style.opacity":"Opacity","style.connection.straight":"Straight","style.connection.polyline":"Polyline","style.connection.orthogonal":"Orthogonal","style.connection.curve":"Curve","style.arrowhead.none":"None","style.arrowhead.end":"End","style.arrowhead.start":"Start","style.arrowhead.both":"Both","style.arrowhead.none-dashed":"Dashed","style.arrowhead.both-dashed":"Dashed both","style.uml":"UML","style.arrowhead.inheritance":"Inheritance","style.arrowhead.composition":"Composition","style.arrowhead.aggregation":"Aggregation","style.arrowhead.dependency":"Dependency","style.arrowhead.realization":"Realization","menu.title":"Create shape","menu.cancel":"Click empty space to cancel","shape.rectangle":"Rectangle","shape.rounded-rectangle":"Rounded","shape.circle":"Circle","shape.ellipse":"Ellipse","shape.diamond":"Diamond","shape.triangle":"Triangle","shape.hexagon":"Hexagon","shape.parallelogram":"Parallelogram","shape.trapezoid":"Trapezoid","shape.pentagon":"Pentagon","shape.octagon":"Octagon","shape.star":"Star","shape.uml-class":"Class","shape.uml-interface":"Interface","uml.drag":"Drag to reorder","uml.delete":"Delete row","uml.textColor":"Text color","uml.defaultColor":"Default color","uml.fontSize":"Font size","uml.strikethrough":"Strikethrough","uml.attribute":"Attributes","uml.method":"Methods","minimap.title":"Minimap","minimap.expand":"Expand minimap","minimap.collapse":"Collapse minimap","tool.mind-map":"Mind map","toolbar.mind-map":"Mind map","toolbar.export":"Export","toolbar.export.png":"PNG image","toolbar.export.svg":"SVG vector","toolbar.export.pdf":"PDF document","shape.mind-map":"Mind map","mindmap.defaultRoot":"Central topic","mindmap.defaultBranch":"Branch topic","mindmap.nodeText":"Node text","mindmap.addChild":"Child","mindmap.addSibling":"Sibling","mindmap.deleteNode":"Delete node","mindmap.color":"Color","mindmap.levelColor":"Default level color","mindmap.icon":"Icon","mindmap.noIcon":"No icon","mindmap.note":"Note","mindmap.notePlaceholder":"Note content (optional)","mindmap.noteColor":"Note color","mindmap.noteColorDefault":"Same as node","mindmap.collapse":"Collapse","mindmap.expand":"Expand","mindmap.close":"Close editor","mindmap.more":"More settings"}},Le="zh-CN",Ye=new Set;function ke(a){Le!==a&&(Le=a,Ye.forEach(e=>e()));}function kt(){return Le}function Dt(a){return Ye.add(a),()=>Ye.delete(a)}function Z(a,e){let i=ct[Le][a]??ct["zh-CN"][a];if(e)for(let[n,s]of Object.entries(e))i=i.replace(new RegExp(`\\{${n}\\}`,"g"),String(s));return i}var Xe=["#ef4444","#f59e0b","#10b981","#3b82f6","#8b5cf6","#ec4899"],J=14,Et=40,$t=80,At=240;function re(a,e){return a.color??Xe[e%Xe.length]}function dt(a){let e=a.text||"",t=0;for(let n of e)t+=/[\u2e80-\u9fff\uf900-\ufaff\uff00-\uffef\u3000-\u303f]/.test(n)?1:.6;let i=Math.max(80,Math.min(240,Math.ceil(t*14)+24));return a.icon&&(i+=26),{width:i,height:40}}function De(a){let e={...a};return a.children&&(e.children=a.children.map(t=>De(t))),e}function Ee(a,e,t){if(a.id===e)return {...a,...t};let i=a.children??[],n=i.map(s=>Ee(s,e,t));return n.some((s,r)=>s!==i[r])?{...a,children:n}:a}function We(a,e,t){if(a.id===e)return {...a,children:[...a.children??[],t],collapsed:false};let i=a.children??[],n=i.map(s=>We(s,e,t));return n.some((s,r)=>s!==i[r])?{...a,children:n}:a}function Ke(a,e,t){let i=a.children??[];for(let n=0;n<i.length;n++){if(i[n].id===e){let r=[...i];return r.splice(n+1,0,t),{...a,children:r}}let s=Ke(i[n],e,t);if(s!==i[n])return {...a,children:i.map(r=>r===i[n]?s:r)}}return a}function Fe(a,e){if(a.id===e)return null;let t=a.children??[],i=t.map(n=>Fe(n,e)).filter(n=>n!==null);return i.length!==t.length||i.some((n,s)=>n!==t[s])?{...a,children:i}:a}function Q(a,e){if(a.id===e)return a;for(let t of a.children??[]){let i=Q(t,e);if(i)return i}return null}function ut(a,e){if(a.id===e)return 0;for(let t of a.children??[]){let i=ut(t,e);if(i!==-1)return i+1}return -1}function ye(a,e){for(let t of a.children??[]){if(t.id===e)return a;let i=ye(t,e);if(i)return i}return null}function pt(a){let e=1;for(let t of a.children??[])e+=pt(t);return e}function gt(a,e){e.set(a.id,dt(a));for(let t of a.children??[])gt(t,e);}function B(a){let e=new Map;gt(a,e);let t=new Map,i=d=>{let p=d.collapsed?[]:d.children??[];if(p.length===0)t.set(d.id,e.get(d.id).height);else {let g=0;for(let y of p)g+=i(y);g+=16*(p.length-1),t.set(d.id,Math.max(e.get(d.id).height,g));}return t.get(d.id)};i(a);let n=new Map,s=(d,p,g,y)=>{let m=t.get(d.id),v=e.get(d.id);if(n.set(d.id,{x:g,y:y+m/2,depth:p,width:v.width,height:v.height}),d.collapsed)return;let x=y;for(let M of d.children??[]){let S=e.get(M.id);s(M,p+1,g+v.width/2+48+S.width/2,x),x+=t.get(M.id)+16;}};s(a,0,0,-t.get(a.id)/2);let r=1/0,o=-1/0,h=1/0,l=-1/0;n.forEach(d=>{r=Math.min(r,d.x-d.width/2),o=Math.max(o,d.x+d.width/2),h=Math.min(h,d.y-d.height/2),l=Math.max(l,d.y+d.height/2);});let c=(r+o)/2,u=(h+l)/2;return n.forEach(d=>{d.x-=c,d.y-=u;}),{positions:n,width:o-r,height:l-h}}var Rt={strokeColor:"#1f2937",fillColor:"#f8fafc",fillOpacity:.9,strokeWidth:2,strokeDash:null},Ht={rectangle:{width:120,height:60},"rounded-rectangle":{width:120,height:60,borderRadius:8},circle:{width:80,height:80},ellipse:{width:100,height:70},diamond:{width:100,height:80},triangle:{width:100,height:90},hexagon:{width:100,height:90},parallelogram:{width:120,height:60},trapezoid:{width:120,height:70},pentagon:{width:100,height:90},octagon:{width:100,height:90},star:{width:100,height:100},cylinder:{width:100,height:110},cloud:{width:140,height:90},heart:{width:100,height:95},"block-arrow-right":{width:140,height:80},"block-arrow-left":{width:140,height:80},"block-arrow-up":{width:80,height:140},"block-arrow-down":{width:80,height:140},"block-arrow-double":{width:150,height:80},"right-triangle":{width:100,height:90},cross:{width:90,height:90},"speech-rect":{width:130,height:92},"speech-round":{width:130,height:92},cube:{width:110,height:110},sector:{width:100,height:100},delay:{width:120,height:80},"predefined-process":{width:130,height:80},document:{width:120,height:90},stadium:{width:130,height:60},"class-catalog":{width:160,height:90},chevron:{width:110,height:80},"brace-left":{width:220,height:120},"brace-right":{width:220,height:120}},G=14,A=22,j=36,O=14,E=8,fe=160,Nt=["rectangle","rounded-rectangle","circle","ellipse","diamond","triangle","hexagon","parallelogram","trapezoid","pentagon","octagon","star","cylinder","cloud","heart","block-arrow-right","block-arrow-left","block-arrow-up","block-arrow-down","block-arrow-double","right-triangle","cross","speech-rect","speech-round","cube","sector","delay","predefined-process","document","stadium","class-catalog","chevron","brace-left","brace-right","uml-class","uml-interface","mind-map"];function $e(a){return Nt.includes(a)}function Ze(a,e,t){let i=`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,n=Ht[a]||{width:120,height:60},s=n.width,r=n.height,o=n.radius??s/2,h=n.borderRadius??8,l=t??{...Rt};switch(a){case "rounded-rectangle":return {id:i,type:"rounded-rectangle",x:e.x,y:e.y,width:s,height:r,borderRadius:h,rotation:0,style:l};case "circle":return {id:i,type:"circle",x:e.x,y:e.y,radius:o,rotation:0,style:l};case "ellipse":return {id:i,type:"ellipse",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "diamond":return {id:i,type:"diamond",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "triangle":return {id:i,type:"triangle",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "hexagon":return {id:i,type:"hexagon",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "parallelogram":return {id:i,type:"parallelogram",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "trapezoid":return {id:i,type:"trapezoid",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "pentagon":return {id:i,type:"pentagon",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "octagon":return {id:i,type:"octagon",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "star":return {id:i,type:"star",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":return {id:i,type:a,x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "uml-class":{let c="ClassName",u=[{id:`attr_${Date.now()}_1`,visibility:"+",name:"field",type:"string"}],d=[{id:`meth_${Date.now()}_1`,visibility:"+",name:"method",params:"param",returnType:"void"}],{width:p,height:g}=Ae(c,u,d);return {id:i,type:"uml-class",x:e.x,y:e.y,width:p,height:g,className:c,attributes:u,methods:d,rotation:0,style:{...l,fillColor:"#ffffff"}}}case "uml-interface":{let c="Interface",u=[{id:`meth_${Date.now()}_1`,visibility:"+",name:"method",params:"type",returnType:"type"}],d=G*.6,p=u.map(x=>`${x.visibility} ${x.name}(${x.params}): ${x.returnType}`),g=Math.max(c.length,...p.map(x=>x.length),0),y=Math.max(fe,g*d+O*2),m=u.length>0?u.length*A+E*2:E*2,v=j+m;return {id:i,type:"uml-interface",x:e.x,y:e.y,width:y,height:v,className:c,methods:u,rotation:0,style:{...l,fillColor:"#ffffff"}}}case "mind-map":{let c=g=>`mm_${g}_${Date.now()}_${Math.random().toString(36).substr(2,6)}`,u={id:c("root"),text:Z("mindmap.defaultRoot"),children:[{id:c("b1"),text:Z("mindmap.defaultBranch")},{id:c("b2"),text:Z("mindmap.defaultBranch")},{id:c("b3"),text:Z("mindmap.defaultBranch")}]},{width:d,height:p}=B(u);return {id:i,type:"mind-map",x:e.x,y:e.y,width:d,height:p,root:u,rotation:0,style:{...l,fillColor:"#ffffff"}}}default:return {id:i,type:"rectangle",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l}}}function Ae(a,e,t){let i=G*.6,n=[a];for(let u of e)n.push(`${u.visibility} ${u.name}: ${u.type}`);for(let u of t)n.push(`${u.visibility} ${u.name}(${u.params}): ${u.returnType}`);let r=Math.max(...n.map(u=>u.length),0)*i+O*2,o=Math.max(fe,r),h=e.length>0?e.length*A+E*2:E*2,l=t.length>0?t.length*A+E*2:E*2,c=j+h+l;return {width:o,height:c}}var T=class extends V{type;constructor(e,t){super(e),this.type=t;}onPointerDown(e,t){let i=Ze(this.type,e);this.context.commitHistory(),this.context.addShape(i),this.context.selectShape(i.id,false);}onPointerMove(e,t){}onPointerUp(e,t){this.context.setActiveTool("select");}onKeyDown(e){}onKeyUp(e){}getCursor(e){return this.context.getMindMapCursorHitAtPoint(e)?"pointer":"crosshair"}};var je=14,Re=class extends V{type="eraser";erasing=false;lastPoint=null;erasedIds=new Set;onPointerDown(e){this.erasing=true,this.lastPoint=e,this.erasedIds.clear(),this.context.commitHistory(),this.context.setEraserPoint(e),this.eraseAt(e);}onPointerMove(e){if(!this.erasing||!this.lastPoint)return;let t=this.lastPoint,i=Math.hypot(e.x-t.x,e.y-t.y),n=Math.max(1,Math.ceil(i/Math.max(this.brushRadius()*.5,1)));for(let s=1;s<=n;s++){let r=s/n;this.eraseAt({x:t.x+(e.x-t.x)*r,y:t.y+(e.y-t.y)*r});}this.lastPoint=e,this.context.setEraserPoint(e);}onPointerUp(){this.erasing=false,this.lastPoint=null,this.erasedIds.clear(),this.context.setEraserPoint(null);}onKeyDown(){}onKeyUp(){}getCursor(){return this.erasing?"grabbing":"crosshair"}dispose(){this.erasing=false,this.lastPoint=null,this.erasedIds.clear(),this.context.setEraserPoint(null);}brushRadius(){return je/this.context.getCamera().getZoom()}eraseAt(e){let t=this.context.getShapesInBrushRadius(e,this.brushRadius());for(let i of t)this.erasedIds.has(i.id)||(this.erasedIds.add(i.id),this.context.deleteShape(i.id));}};function ue(a,e,t){let i=s=>(s-.5)*e,n=s=>(s-.5)*t;return a.map(s=>s[0]==="Z"?s:s[0]==="M"||s[0]==="L"?[s[0],i(s[1]),n(s[2])]:["C",i(s[1]),n(s[2]),i(s[3]),n(s[4]),i(s[5]),n(s[6])])}function de(a,e,t,i,n,s,r){let o=[],h=s-n,l=Math.max(1,Math.ceil(Math.abs(h)/90)),c=h/l,u=4/3*Math.tan(c*Math.PI/180/4),d=y=>{let m=y*Math.PI/180;return [a+t*Math.cos(m),e+i*Math.sin(m)]},[p,g]=d(n);o.push(r?["M",p,g]:["L",p,g]);for(let y=0;y<l;y++){let m=n+c*y,v=m+c,x=m*Math.PI/180,M=v*Math.PI/180,[S,w]=d(m),[C,I]=d(v);o.push(["C",S-u*t*Math.sin(x),w+u*i*Math.cos(x),C+u*t*Math.sin(M),I-u*i*Math.cos(M),C,I]);}return o}function Vt(a){let e=1/0,t=1/0,i=-1/0,n=-1/0,s=(p,g)=>{p<e&&(e=p),g<t&&(t=g),p>i&&(i=p),g>n&&(n=g);},r=0,o=0,h=0,l=0;for(let p of a){if(p[0]==="Z"){s(h,l);continue}if(p[0]==="M"||p[0]==="L"){r=p[1],o=p[2],p[0]==="M"&&(h=r,l=o),s(r,o);continue}let g=p[1],y=p[2],m=p[3],v=p[4],x=p[5],M=p[6];for(let S=0;S<=12;S++){let w=S/12,C=1-w,I=C*C*C,H=3*C*C*w,U=3*C*w*w,W=w*w*w;s(I*r+H*g+U*m+W*x,I*o+H*y+U*v+W*M);}r=x,o=M;}let c=i-e||1,u=n-t||1,d=(p,g)=>[(p-e)/c,(g-t)/u];return a.map(p=>{if(p[0]==="Z")return p;if(p[0]==="M"||p[0]==="L"){let[S,w]=d(p[1],p[2]);return [p[0],S,w]}let[g,y]=d(p[1],p[2]),[m,v]=d(p[3],p[4]),[x,M]=d(p[5],p[6]);return ["C",g,y,m,v,x,M]})}function Bt(){let a={x:.24,y:.7,r:.24},e={x:.5,y:.48,r:.36},t={x:.76,y:.7,r:.24},i=[...de(a.x,a.y,a.r,a.r,90,246,true),...de(e.x,e.y,e.r,e.r,180,360,false),...de(t.x,t.y,t.r,t.r,294,450,false),["Z"]];return Vt(i)}var X=.14,yt=.5523,le=X*yt,He=1-X,ce=.5-.5*yt,_t={outline:[["M",0,X],["L",0,He],["C",0,He+le,ce,1,.5,1],["C",1-ce,1,1,He+le,1,He],["L",1,X],["C",1,X-le,1-ce,0,.5,0],["C",ce,0,0,X-le,0,X],["Z"]],seams:[[["M",0,X],["C",0,X+le,ce,X*2,.5,X*2],["C",1-ce,X*2,1,X+le,1,X]]]},Ot={outline:Bt()},Ut={outline:[["M",.5,.97],["C",.5,.97,.03,.66,.03,.35],["C",.03,.17,.16,.04,.31,.04],["C",.41,.04,.47,.1,.5,.17],["C",.53,.1,.59,.04,.69,.04],["C",.84,.04,.97,.17,.97,.35],["C",.97,.66,.5,.97,.5,.97],["Z"]]},Ne=[["M",0,.25],["L",.58,.25],["L",.58,0],["L",1,.5],["L",.58,1],["L",.58,.75],["L",0,.75],["Z"]];function qe(a,e){let t=(i,n)=>{switch(e){case "down":return [1-n,i];case "left":return [1-i,1-n];case "up":return [n,1-i];default:return [i,n]}};return a.map(i=>{if(i[0]==="Z")return i;if(i[0]==="M"||i[0]==="L"){let[c,u]=t(i[1],i[2]);return [i[0],c,u]}let[n,s]=t(i[1],i[2]),[r,o]=t(i[3],i[4]),[h,l]=t(i[5],i[6]);return ["C",n,s,r,o,h,l]})}var Ve={right:Ne,left:qe(Ne,"left"),up:qe(Ne,"up"),down:qe(Ne,"down")},Gt={outline:[["M",0,0],["L",0,1],["L",1,1],["Z"]]},zt={outline:[["M",.34,0],["L",.66,0],["L",.66,.34],["L",1,.34],["L",1,.66],["L",.66,.66],["L",.66,1],["L",.34,1],["L",.34,.66],["L",0,.66],["L",0,.34],["L",.34,.34],["Z"]]},Yt={outline:[["M",0,.5],["L",.22,.13],["L",.22,.34],["L",.78,.34],["L",.78,.13],["L",1,.5],["L",.78,.87],["L",.78,.66],["L",.22,.66],["L",.22,.87],["Z"]]},Xt={outline:[["M",0,0],["L",1,0],["L",1,.8],["L",.32,.8],["L",.2,1],["L",.15,.8],["L",0,.8],["Z"]]},Wt={outline:[...de(.5,.4,.5,.4,100,-228,true),["L",.19,1],["Z"]]},Kt={outline:[["M",.5,0],["L",1,.25],["L",1,.75],["L",.5,1],["L",0,.75],["L",0,.25],["Z"]],seams:[[["M",.5,.5],["L",.5,1]],[["M",.5,.5],["L",0,.25]],[["M",.5,.5],["L",1,.25]]]},Ft={outline:[...de(.5,.5,.5,.5,0,-270,true),["L",.5,.5],["Z"]]},Zt={outline:[["M",0,0],...de(.6,.5,.4,.5,270,450,false),["L",0,1],["Z"]]},jt={outline:[["M",0,0],["L",1,0],["L",1,1],["L",0,1],["Z"]],seams:[[["M",.12,0],["L",.12,1]],[["M",.88,0],["L",.88,1]]]},qt={outline:[["M",0,0],["L",1,0],["L",1,.84],["C",.72,1.06,.28,.62,0,.84],["Z"]]},Jt={outline:[["M",0,0],["L",.45,0],["L",1,.5],["L",.45,1],["L",0,1],["L",.42,.5],["Z"]]};function Qt(a){let e=t=>1-t;return a.map(t=>t[0]==="Z"?t:t[0]==="M"||t[0]==="L"?[t[0],e(t[1]),t[2]]:["C",e(t[1]),t[2],e(t[3]),t[4],e(t[5]),t[6]])}var me=.16,ft={outline:[["M",.015,.02],["C",.075,.02,.1,.08,.1,.2],["C",.1,.36,.125,.5,.16,.5],["C",.125,.5,.1,.64,.1,.8],["C",.1,.92,.075,.98,.015,.98]],strokeOnly:true},en={outline:Qt(ft.outline),strokeOnly:true};function Be(a){if(a.type!=="brace-left"&&a.type!=="brace-right")return 0;let e=(a.width??0)*me/2;return a.type==="brace-left"?e:-e}var tn={outline:[["M",0,0],["L",1,0],["L",1,1],["L",0,1],["Z"]],seams:[[["M",.1,0],["L",.1,1]],[["M",.9,0],["L",.9,1]]]};function _e(a){switch(a){case "cylinder":return _t;case "cloud":return Ot;case "heart":return Ut;case "class-catalog":return tn;case "block-arrow-right":return {outline:Ve.right};case "block-arrow-left":return {outline:Ve.left};case "block-arrow-up":return {outline:Ve.up};case "block-arrow-down":return {outline:Ve.down};case "block-arrow-double":return Yt;case "right-triangle":return Gt;case "cross":return zt;case "speech-rect":return Xt;case "speech-round":return Wt;case "cube":return Kt;case "sector":return Ft;case "delay":return Zt;case "predefined-process":return jt;case "document":return qt;case "chevron":return Jt;case "brace-left":return ft;case "brace-right":return en;default:return}}var oe=class a{ctx;width=0;height=0;dpr=window.devicePixelRatio||1;showGrid=false;textLayoutCache=new Map;static TEXT_CACHE_LIMIT=500;constructor(e,t){let i=e.getContext("2d");if(!i)throw new Error("Failed to get Canvas 2D context");this.ctx=i,typeof t=="number"&&t>0&&(this.dpr=t);}resize(e,t){this.width=e,this.height=t;let i=Math.ceil(e*this.dpr),n=Math.ceil(t*this.dpr);(this.ctx.canvas.width!==i||this.ctx.canvas.height!==n)&&(this.ctx.canvas.width=i,this.ctx.canvas.height=n,this.ctx.canvas.style.width=`${e}px`,this.ctx.canvas.style.height=`${t}px`);}setShowGrid(e){this.showGrid=e;}render(e,t,i,n,s,r,o,h,l,c,u){let d=this.ctx,p=t.getZoom(),g=t.x,y=t.y;d.setTransform(1,0,0,1,0,0),d.clearRect(0,0,this.ctx.canvas.width,this.ctx.canvas.height),u&&(d.fillStyle=u,d.fillRect(0,0,this.ctx.canvas.width,this.ctx.canvas.height));let m=this.dpr*p;d.setTransform(m,0,0,m,m*g,m*y),this.showGrid&&this.drawGrid(d,p,g,y);let v=null;for(let M of e)if(M.type==="arrow"&&(M.startBinding||M.endBinding)){v=new Map,e.forEach(S=>v.set(S.id,S));break}let x=v??new Map;e.forEach(M=>{d.save(),d.translate(M.x,M.y),d.rotate(M.rotation);let S=i.has(M.id);this.drawShape(d,M,S,p,x,l,c),d.restore();}),o&&this.drawGenerationPreview(d,o,p),n&&this.drawBrushRect(d,n,p),s&&s.length>0&&this.drawSnapGuides(d,s,p),r&&this.drawConnectingTargetHighlight(d,r,e,p),h&&this.drawEraserBrush(d,h,p),d.setTransform(1,0,0,1,0,0);}drawEraserBrush(e,t,i){let n=je/i;e.beginPath(),e.arc(t.x,t.y,n,0,Math.PI*2),e.fillStyle=F(L.primary,.12),e.fill(),e.strokeStyle=F(L.primary,.55),e.lineWidth=1.5/i,e.stroke();}drawConnectingTargetHighlight(e,t,i,n){let s=i.find(l=>l.id===t);if(!s||s.type==="line"||s.type==="arrow"||s.type==="draw")return;let r=this.getShapeWorldBounds(s),o=[{x:r.x+r.width/2,y:r.y},{x:r.x+r.width,y:r.y+r.height/2},{x:r.x+r.width/2,y:r.y+r.height},{x:r.x,y:r.y+r.height/2}],h=6/n;e.save(),o.forEach(l=>{e.beginPath(),e.arc(l.x,l.y,h,0,Math.PI*2),e.fillStyle=L.selection.handleFill,e.fill(),e.strokeStyle=L.primary,e.lineWidth=2/n,e.stroke();}),e.restore();}getShapeWorldBounds(e){let t=this.getShapeBounds(e);return {x:e.x+t.x,y:e.y+t.y,width:t.width,height:t.height}}drawBrushRect(e,t,i){e.save(),e.fillStyle=L.brushFill,e.strokeStyle=L.brushStroke,e.lineWidth=1.5/i,e.setLineDash([4/i,4/i]),e.beginPath(),e.rect(t.x,t.y,t.width,t.height),e.fill(),e.stroke(),e.restore();}drawGrid(e,t,i,n){let s=-i,r=-n,o=s+this.width/t,h=r+this.height/t,l=20;for(;l*t<10;)l*=2;e.strokeStyle=L.grid,e.lineWidth=1/t,e.beginPath();for(let c=Math.floor(s/l)*l;c<=o;c+=l)e.moveTo(c,r),e.lineTo(c,h);for(let c=Math.floor(r/l)*l;c<=h;c+=l)e.moveTo(s,c),e.lineTo(o,c);e.stroke();}drawShape(e,t,i,n,s,r,o){switch(this.applyStyle(e,t.style,n),t.type){case "rectangle":this.drawRectangle(e,t,i);break;case "rounded-rectangle":this.drawRoundedRectangle(e,t,i);break;case "circle":this.drawCircle(e,t,i);break;case "ellipse":this.drawEllipse(e,t,i);break;case "diamond":this.drawDiamond(e,t,i);break;case "triangle":this.drawTriangle(e,t,i);break;case "hexagon":this.drawHexagon(e,t,i);break;case "parallelogram":this.drawParallelogram(e,t,i);break;case "trapezoid":this.drawTrapezoid(e,t,i);break;case "pentagon":this.drawPentagon(e,t,i);break;case "octagon":this.drawOctagon(e,t,i);break;case "star":this.drawStar(e,t,i);break;case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "class-catalog":case "chevron":case "brace-left":case "brace-right":this.drawPathShape(e,t,i);break;case "stadium":this.drawStadium(e,t,i);break;case "uml-class":this.drawUmlClass(e,t,i);break;case "uml-interface":this.drawUmlInterface(e,t,i);break;case "mind-map":this.drawMindMap(e,t,i,r,o);break;case "line":this.drawLine(e,t,s,i,n);break;case "arrow":this.drawArrow(e,t,s,i,n);break;case "text":this.drawText(e,t);break;case "draw":this.drawFreehand(e,t);break}if(t.text&&t.type!=="text"&&t.type!=="line"&&t.type!=="arrow"&&t.type!=="draw"&&this.drawShapeText(e,t),i){let h=t.type==="line"||t.type==="arrow";t.type==="mind-map"||(h?this.drawSelectionHandles(e,t,n):(this.drawSelectionOutline(e,t,n),this.drawSelectionHandles(e,t,n)));}}applyStyle(e,t,i){let n=t.strokeOpacity??1;e.strokeStyle=n<1&&t.strokeColor!=="transparent"?F(t.strokeColor,n):t.strokeColor,e.lineWidth=t.strokeWidth/i,e.lineCap="round",e.lineJoin="round";let s=t.fillOpacity??1;s<1&&t.fillColor!=="transparent"?e.fillStyle=F(t.fillColor,s):e.fillStyle=t.fillColor,t.strokeDash?e.setLineDash(t.strokeDash):e.setLineDash([]);}drawRectangle(e,t,i){e.beginPath(),e.rect(-t.width/2,-t.height/2,t.width,t.height),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawRoundedRectangle(e,t,i){let{width:n,height:s,borderRadius:r}=t;e.beginPath(),e.roundRect(-n/2,-s/2,n,s,r),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawStadium(e,t,i){let{width:n,height:s}=t;e.beginPath(),e.roundRect(-n/2,-s/2,n,s,Math.min(n,s)/2),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawCircle(e,t,i){e.beginPath(),e.arc(0,0,t.radius,0,Math.PI*2),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawEllipse(e,t,i){e.beginPath(),e.ellipse(0,0,t.width/2,t.height/2,0,0,Math.PI*2),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawDiamond(e,t,i){e.beginPath(),e.moveTo(0,-t.height/2),e.lineTo(t.width/2,0),e.lineTo(0,t.height/2),e.lineTo(-t.width/2,0),e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawTriangle(e,t,i){e.beginPath(),e.moveTo(0,-t.height/2),e.lineTo(t.width/2,t.height/2),e.lineTo(-t.width/2,t.height/2),e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawHexagon(e,t,i){let n=t.width/2,s=t.height/2;e.beginPath();for(let r=0;r<6;r++){let o=r*Math.PI/3-Math.PI/2,h=n*Math.cos(o),l=s*Math.sin(o);r===0?e.moveTo(h,l):e.lineTo(h,l);}e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawParallelogram(e,t,i){let{width:n,height:s}=t,r=n*.2;e.beginPath(),e.moveTo(-n/2+r,-s/2),e.lineTo(n/2,-s/2),e.lineTo(n/2-r,s/2),e.lineTo(-n/2,s/2),e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawTrapezoid(e,t,i){let{width:n,height:s}=t,r=n*.15;e.beginPath(),e.moveTo(-n/2+r,-s/2),e.lineTo(n/2-r,-s/2),e.lineTo(n/2,s/2),e.lineTo(-n/2,s/2),e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawPentagon(e,t,i){let n=t.width/2,s=t.height/2;e.beginPath();for(let r=0;r<5;r++){let o=r*2*Math.PI/5-Math.PI/2,h=n*Math.cos(o),l=s*Math.sin(o);r===0?e.moveTo(h,l):e.lineTo(h,l);}e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawOctagon(e,t,i){let n=t.width/2,s=t.height/2;e.beginPath();for(let r=0;r<8;r++){let o=r*Math.PI/4-Math.PI/8,h=n*Math.cos(o),l=s*Math.sin(o);r===0?e.moveTo(h,l):e.lineTo(h,l);}e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawStar(e,t,i){let n=t.width/2,s=t.height/2,r=5,o=.4;e.beginPath();for(let h=0;h<r*2;h++){let l=h*Math.PI/r-Math.PI/2,c=h%2===0?1:o,u=n*c*Math.cos(l),d=s*c*Math.sin(l);h===0?e.moveTo(u,d):e.lineTo(u,d);}e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawPathShape(e,t,i){let n=_e(t.type);if(n&&(this.tracePath(e,ue(n.outline,t.width,t.height)),!n.strokeOnly&&t.style.fillColor!=="transparent"&&e.fill(),e.stroke(),n.seams))for(let s of n.seams)this.tracePath(e,ue(s,t.width,t.height)),e.stroke();}tracePath(e,t){e.beginPath();for(let i of t)switch(i[0]){case "M":e.moveTo(i[1],i[2]);break;case "L":e.lineTo(i[1],i[2]);break;case "C":e.bezierCurveTo(i[1],i[2],i[3],i[4],i[5],i[6]);break;case "Z":e.closePath();break}}drawUmlClass(e,t,i){let{width:n,height:s,className:r,attributes:o,methods:h}=t,l=n/2,c=s/2,u=j,d=o.length>0?o.length*A+E*2:E*2;e.beginPath(),e.rect(-l,-c,n,s),t.style.fillColor!=="transparent"&&(e.fillStyle=t.style.fillColor,e.fill()),e.stroke(),e.beginPath(),e.moveTo(-l,-c+u),e.lineTo(l,-c+u),e.moveTo(-l,-c+u+d),e.lineTo(l,-c+u+d),e.stroke(),e.save(),e.textBaseline="middle";let p=n-O*2,g=x=>{if(e.measureText(x).width<=p)return x;let M="\u2026",S=0,w=x.length;for(;S<w;){let C=S+w+1>>1;e.measureText(x.slice(0,C)+M).width<=p?S=C:w=C-1;}return x.slice(0,S)+M},y=(x,M,S,w,C,I,H)=>{let U=w||G,W=C||"#1f2937";e.font=`${U}px sans-serif`,e.fillStyle=W,e.textAlign=H;let R=g(x);if(e.fillText(R,M,S),I){let N=e.measureText(R).width,Ce=M;e.strokeStyle=W,e.beginPath(),e.moveTo(Ce,S),e.lineTo(Ce+N,S),e.stroke();}};e.font=`bold ${t.fontSize||G}px sans-serif`,e.textAlign="center",e.fillStyle=t.textColor||"#1f2937",e.fillText(g(r),0,-c+u/2);let m=-c+u+E+A/2;o.forEach((x,M)=>{let S=`${x.visibility} ${x.name}: ${x.type}`;y(S,-l+O,m+M*A,x.fontSize||t.fontSize||0,x.textColor||t.textColor||"",!!x.strikethrough,"left");});let v=-c+u+d+E+A/2;h.forEach((x,M)=>{let S=`${x.visibility} ${x.name}(${x.params}): ${x.returnType}`;y(S,-l+O,v+M*A,x.fontSize||t.fontSize||0,x.textColor||t.textColor||"",!!x.strikethrough,"left");}),e.restore();}drawUmlInterface(e,t,i){let{width:n,height:s,className:r,methods:o}=t,h=n/2,l=s/2,c=j;e.beginPath(),e.rect(-h,-l,n,s),t.style.fillColor!=="transparent"&&(e.fillStyle=t.style.fillColor,e.fill()),e.stroke(),e.beginPath(),e.moveTo(-h,-l+c),e.lineTo(h,-l+c),e.stroke(),e.save(),e.textBaseline="middle";let u=n-O*2,d=m=>{if(e.measureText(m).width<=u)return m;let v="\u2026",x=0,M=m.length;for(;x<M;){let S=x+M+1>>1;e.measureText(m.slice(0,S)+v).width<=u?x=S:M=S-1;}return m.slice(0,x)+v},p=(m,v,x,M,S,w,C)=>{let I=M||G,H=S||"#1f2937";e.font=`${I}px sans-serif`,e.fillStyle=H,e.textAlign=C;let U=d(m);if(e.fillText(U,v,x),w){let W=e.measureText(U).width,R=v;e.strokeStyle=H,e.beginPath(),e.moveTo(R,x),e.lineTo(R+W,x),e.stroke();}};e.font=`bold ${t.fontSize||G}px sans-serif`,e.textAlign="center",e.fillStyle=t.textColor||"#1f2937";let g=`<<${r||"Interface"}>>`;e.fillText(d(g),0,-l+c/2);let y=-l+c+E+A/2;o.forEach((m,v)=>{let x=`${m.visibility} ${m.name}(${m.params}): ${m.returnType}`;p(x,-h+O,y+v*A,m.fontSize||t.fontSize||0,m.textColor||t.textColor||"",!!m.strikethrough,"left");}),e.restore();}static DEFAULT_CORNER_RADIUS=10;static ROUTE_CLEARANCE=30;static CONNECTION_LEAD=24;static CONNECTED_PAD=8;drawMindMap(e,t,i,n,s){let o=B(t.root).positions;this.drawMindMapConnections(e,t.root,o),this.drawMindMapNodes(e,t,o,n,s);}drawMindMapConnections(e,t,i){let n=s=>{if(s.collapsed)return;let r=i.get(s.id);if(r)for(let o of s.children??[]){let h=i.get(o.id);if(!h)continue;let l=re(o,h.depth);e.strokeStyle=l,e.lineWidth=2,e.lineCap="round",e.beginPath(),e.moveTo(r.x+r.width/2,r.y);let c=(r.x+r.width/2+(h.x-h.width/2))/2;e.bezierCurveTo(c,r.y,c,h.y,h.x-h.width/2,h.y),e.stroke(),n(o);}};n(t);}drawMindMapNodes(e,t,i,n,s){let r=t.root,o=h=>{let l=i.get(h.id);if(!l)return;let c=re(h,l.depth),u=l.width/2,d=l.height/2;if(e.fillStyle="#ffffff",e.strokeStyle=c,e.lineWidth=2,e.beginPath(),e.roundRect(l.x-u,l.y-d,l.width,l.height,6),e.fill(),e.stroke(),s&&s.shapeId===t.id&&s.nodeId===h.id&&(e.fillStyle=F(L.primary,.06),e.strokeStyle=F(L.primary,.55),e.lineWidth=2,e.beginPath(),e.roundRect(l.x-u,l.y-d,l.width,l.height,6),e.fill(),e.stroke()),n&&n.shapeId===t.id&&n.nodeId===h.id&&(e.fillStyle=F(L.primary,.12),e.strokeStyle=L.primary,e.lineWidth=2.5,e.beginPath(),e.roundRect(l.x-u,l.y-d,l.width,l.height,6),e.fill(),e.stroke()),h.children&&h.children.length>0&&(e.fillStyle="#ffffff",e.strokeStyle=c,e.lineWidth=2,e.beginPath(),e.arc(l.x+u+10,l.y,8,0,Math.PI*2),e.fill(),e.stroke(),e.fillStyle=c,e.font="bold 13px sans-serif",e.textAlign="center",e.textBaseline="middle",e.fillText(h.collapsed?"+":"\u2212",l.x+u+10,l.y+.5)),h.note){let C=h.noteColor??c;e.font="11px sans-serif";let I=Math.min(e.measureText(h.note).width+12,130),H=16;e.fillStyle=C,e.beginPath(),e.roundRect(l.x-u,l.y-d-H+2,I,H,4),e.fill(),e.fillStyle="#ffffff",e.font="11px sans-serif",e.textAlign="left",e.textBaseline="middle",e.fillText(e.measureText(h.note).width+12>130?h.note.slice(0,12)+"\u2026":h.note,l.x-u+6,l.y-d-H/2+2);}e.font=`${14}px sans-serif`;let m=h.icon?18:0,v=l.width-m-(h.icon?8:0)-16,x=this.ellipsizeText(e,h.text||"",v),M=e.measureText(x).width,S=m+(m?8:0)+M,w=l.x-S/2;if(e.textBaseline="middle",h.icon&&(e.font="16px sans-serif",e.textAlign="left",e.fillStyle="#1f2937",e.fillText(h.icon,w,l.y+1),w+=m+8),e.font=`${14}px sans-serif`,e.textAlign="left",e.fillStyle="#1f2937",e.fillText(x,w,l.y),!h.collapsed)for(let C of h.children??[])o(C);};o(r);}ellipsizeText(e,t,i){if(i<=0)return "";if(e.measureText(t).width<=i)return t;let n="\u2026",s=0,r=t.length;for(;s<r;){let o=s+r+1>>1;e.measureText(t.slice(0,o)+n).width<=i?s=o:r=o-1;}return t.slice(0,s)+n}drawLine(e,t,i,n,s){if(t.points.length<2)return;let r=t.points.map(c=>({x:c.x,y:c.y}));r.length>=2&&(r[0]=this.clampToShapeOutline(r[0],i,t.id,r[r.length-1]),r[r.length-1]=this.clampToShapeOutline(r[r.length-1],i,t.id,r[0]));let o=r.map(c=>({x:c.x-t.x,y:c.y-t.y})),h=t.connectionType||"straight",l=h==="straight"?this.getAlignedPoints(o):this.buildPathPoints(o,h,this.collectRoutingObstacles(t.id,o[0],o[o.length-1],i,{x:t.x,y:t.y}),{fromN:this.getDockNormal(r[0],i,t.id),toN:this.getDockNormal(r[r.length-1],i,t.id)});if(h==="straight"){e.beginPath(),e.moveTo(o[0].x,o[0].y);for(let c=1;c<o.length;c++)e.lineTo(o[c].x,o[c].y);e.stroke(),e.setLineDash([]);}else h==="orthogonal"?this.drawSharpPolyline(e,l):this.drawRoundedPolyline(e,l,a.DEFAULT_CORNER_RADIUS),e.stroke(),e.setLineDash([]);n&&this.drawLinearHighlight(e,l,s);}drawArrow(e,t,i,n,s){if(t.points.length<2)return;let r=t.points.map(d=>({x:d.x,y:d.y}));if(t.startBinding){let d=i.get(t.startBinding.toShapeId);d&&(r[0]=this.getBindingPoint(d,t.startBinding));}if(t.endBinding){let d=i.get(t.endBinding.toShapeId);d&&(r[r.length-1]=this.getBindingPoint(d,t.endBinding));}r[0]=this.clampToShapeOutline(r[0],i,t.id,r[r.length-1]),r[r.length-1]=this.clampToShapeOutline(r[r.length-1],i,t.id,r[0]);let o=r.map(d=>({x:d.x-t.x,y:d.y-t.y})),h=t.connectionType||"straight",l=t.arrowHead??"end";!!t.dashed||l==="dependency"||l==="realization"?e.setLineDash([8,5]):e.setLineDash([]);let u;if(h==="straight"){let d=this.getAlignedPoints(o);u=d,e.beginPath(),e.moveTo(d[0].x,d[0].y);for(let p=1;p<d.length;p++)e.lineTo(d[p].x,d[p].y);e.stroke(),this.drawArrowHeads(e,d,l);}else u=this.buildPathPoints(o,h,this.collectRoutingObstacles(t.id,o[0],o[o.length-1],i,{x:t.x,y:t.y}),{fromN:this.getDockNormal(r[0],i,t.id),toN:this.getDockNormal(r[r.length-1],i,t.id)}),h==="orthogonal"?this.drawSharpPolyline(e,u):this.drawRoundedPolyline(e,u,a.DEFAULT_CORNER_RADIUS),e.stroke(),this.drawArrowHeads(e,u,l);e.setLineDash([]),n&&this.drawLinearHighlight(e,u,s);}drawLinearHighlight(e,t,i){if(!(!t||t.length<2)){e.save(),e.strokeStyle=L.lineHighlight,e.lineWidth=7/i,e.lineCap="round",e.lineJoin="round",e.setLineDash([]),e.beginPath(),e.moveTo(t[0].x,t[0].y);for(let n=1;n<t.length;n++)e.lineTo(t[n].x,t[n].y);e.stroke(),e.restore();}}drawGenerationPreview(e,t,i){let{from:n,to:s,type:r}=t;e.save(),e.beginPath(),e.arc(n.x,n.y,7/i,0,Math.PI*2),e.fillStyle=L.genDot,e.fill(),e.strokeStyle=L.genLine,e.lineWidth=2/i,e.setLineDash([5/i,4/i]),e.lineCap="round",e.beginPath(),e.moveTo(n.x,n.y),e.lineTo(s.x,s.y),e.stroke(),r&&this.drawPreviewShapeOutline(e,s,r,i),e.restore();}drawPreviewShapeOutline(e,t,i,n){let l=t.x,c=t.y;switch(e.strokeStyle=L.genLine,e.lineWidth=1.5/n,e.setLineDash([6/n,4/n]),e.beginPath(),i){case "circle":case "ellipse":e.ellipse(l,c,i==="circle"?40:120/2,i==="circle"?40:60/2,0,0,Math.PI*2);break;case "diamond":e.moveTo(l,c-30),e.lineTo(l+60,c),e.lineTo(l,c+30),e.lineTo(l-60,c),e.closePath();break;case "triangle":e.moveTo(l,c-30),e.lineTo(l+60,c+30),e.lineTo(l-60,c+30),e.closePath();break;case "hexagon":for(let u=0;u<6;u++){let d=u*Math.PI/3-Math.PI/2,p=l+60*Math.cos(d),g=c+30*Math.sin(d);u===0?e.moveTo(p,g):e.lineTo(p,g);}e.closePath();break;case "parallelogram":e.moveTo(l-60+18,c-30),e.lineTo(l+60,c-30),e.lineTo(l+60-18,c+30),e.lineTo(l-60,c+30),e.closePath();break;default:e.roundRect(l-60,c-30,120,60,i==="rounded-rectangle"?8:4);break}e.stroke();}static ALIGN_TOLERANCE=8;getAlignedPoints(e){if(e.length<2)return e;let t=e[0],i=e[e.length-1],n=i.x-t.x,s=i.y-t.y,r=a.ALIGN_TOLERANCE;if(Math.abs(s)<=r){let o=(t.y+i.y)/2;return e.map(h=>({x:h.x,y:o}))}if(Math.abs(n)<=r){let o=(t.x+i.x)/2;return e.map(h=>({x:o,y:h.y}))}return e}buildPathPoints(e,t,i=[],n={}){if(e.length<2||t==="straight")return e;switch(t){case "polyline":return this.generatePolylinePoints(e,i,n);case "orthogonal":return this.generateOrthogonalPoints(e,i,n);case "curve":return this.generateCurvePoints(e);default:return e}}drawRoundedPolyline(e,t,i){if(e.beginPath(),!(t.length<2)){if(t.length===2||i<=0){e.moveTo(t[0].x,t[0].y);for(let n=1;n<t.length;n++)e.lineTo(t[n].x,t[n].y);return}e.moveTo(t[0].x,t[0].y);for(let n=1;n<t.length-1;n++){let s=t[n-1],r=t[n],o=t[n+1],h=r.x-s.x,l=r.y-s.y,c=o.x-r.x,u=o.y-r.y,d=Math.hypot(h,l),p=Math.hypot(c,u);if(d<1e-6||p<1e-6){e.lineTo(r.x,r.y);continue}let g=Math.min(i,d*.5,p*.5),y=r.x-h/d*g,m=r.y-l/d*g,v=r.x+c/p*g,x=r.y+u/p*g;e.lineTo(y,m),e.arcTo(r.x,r.y,v,x,g);}e.lineTo(t[t.length-1].x,t[t.length-1].y);}}drawSharpPolyline(e,t){if(e.beginPath(),!(t.length<2)){e.moveTo(t[0].x,t[0].y);for(let i=1;i<t.length;i++)e.lineTo(t[i].x,t[i].y);}}drawSnapGuides(e,t,i){e.save(),e.strokeStyle=L.guide,e.lineWidth=1/i,e.setLineDash([6/i,4/i]),e.beginPath();for(let n of t)n.type==="horizontal"?(e.moveTo(n.start,n.position),e.lineTo(n.end,n.position)):(e.moveTo(n.position,n.start),e.lineTo(n.position,n.end));e.stroke(),e.restore();}generatePolylinePoints(e,t=[],i={}){if(e.length<2)return e;let n=[];n.push(e[0]);let s=e.length-1,r=a.CONNECTION_LEAD;for(let o=1;o<e.length;o++){let h=e[o-1],l=e[o],c=o===1,u=o===s,d=c&&i.fromN?{x:h.x+i.fromN.x*r,y:h.y+i.fromN.y*r}:h,p=u&&i.toN?{x:l.x+i.toN.x*r,y:l.y+i.toN.y*r}:l;c&&i.fromN&&n.push(d);let g=this.computeOrthogonalRoute(d,p,t);for(let y of g)n.push(y);u&&i.toN&&n.push(l);}return this.collapseLinePoints(n)}generateOrthogonalPoints(e,t=[],i={}){return this.generatePolylinePoints(e,t,i)}collapseLinePoints(e){let t=[];for(let i of e){let n=t.length;if(!(n>0&&Math.abs(t[n-1].x-i.x)<1e-6&&Math.abs(t[n-1].y-i.y)<1e-6)){if(n>=2){let s=t[n-2],r=t[n-1];if(Math.abs(s.y-r.y)<1e-6&&Math.abs(r.y-i.y)<1e-6||Math.abs(s.x-r.x)<1e-6&&Math.abs(r.x-i.x)<1e-6){t[n-1]=i;continue}}t.push(i);}}return t}computeOrthogonalRoute(e,t,i=[]){let n=t.x-e.x,s=t.y-e.y;if(Math.abs(n)<1||Math.abs(s)<1)return [t];let r=e.x+n/2,o=e.y+s/2,h=i.length>0,l=[[{x:r,y:e.y},{x:r,y:t.y},t],[{x:e.x,y:o},{x:t.x,y:o},t],[{x:t.x,y:e.y},t],[{x:e.x,y:t.y},t]];if(h){for(let u of l)if(!this.routeHitsObstacles(e,u,i))return u;let c=this.routeOrthogonalAvoidance(e,t,i);return c&&c.length>0?c:i.length>0&&!this.routeHitsObstacles(e,l[0],i)?l[0]:this.routeHitsObstacles(e,l[1],i)?[t]:l[1]}return Math.abs(n)>=Math.abs(s)?[{x:r,y:e.y},{x:r,y:t.y},t]:[{x:e.x,y:o},{x:t.x,y:o},t]}collectRoutingObstacles(e,t,i,n,s){if(!n||n.size===0)return [];let r=[];return n.forEach((o,h)=>{if(h===e)return;let l=this.getShapeBounds(o),c={x:o.x+l.x-s.x,y:o.y+l.y-s.y,width:l.width,height:l.height};c.pad=this.pointInRect(t,c)||this.pointInRect(i,c)?a.CONNECTED_PAD:a.ROUTE_CLEARANCE,r.push(c);}),r}pointInRect(e,t){return e.x>=t.x&&e.x<=t.x+t.width&&e.y>=t.y&&e.y<=t.y+t.height}routeHitsObstacles(e,t,i){let n=[e,...t];for(let s=1;s<n.length;s++)if(this.segmentHitsObstacle(n[s-1],n[s],i))return true;return false}segmentHitsObstacle(e,t,i){for(let n of i){let s=n.pad??a.ROUTE_CLEARANCE,r=n.y-s,o=n.y+n.height+s,h=n.x-s,l=n.x+n.width+s;if(Math.abs(e.y-t.y)<.5){if(e.y>=r&&e.y<=o){let c=Math.min(e.x,t.x);if(Math.max(e.x,t.x)>=h&&c<=l)return true}}else if(e.x>=h&&e.x<=l){let c=Math.min(e.y,t.y);if(Math.max(e.y,t.y)>=r&&c<=o)return true}}return false}routeOrthogonalAvoidance(e,t,i){let n=new Set([e.x,t.x]),s=new Set([e.y,t.y]);for(let b of i){let P=b.pad??a.ROUTE_CLEARANCE;n.add(b.x-P),n.add(b.x+b.width+P),s.add(b.y-P),s.add(b.y+b.height+P);}let r=[...n].sort((b,P)=>b-P),o=[...s].sort((b,P)=>b-P),h=r.length,l=o.length,c=b=>{for(let P=0;P<h;P++)if(Math.abs(r[P]-b)<1e-6)return P;return 0},u=b=>{for(let P=0;P<l;P++)if(Math.abs(o[P]-b)<1e-6)return P;return 0},d=c(e.x),p=u(e.y),g=c(t.x),y=u(t.y),m=[],v=[];for(let b=0;b<h;b++)m.push(new Array(l).fill(true)),v.push(new Array(l).fill(true));for(let b of i){let P=b.pad??a.ROUTE_CLEARANCE,k=b.x-P,D=b.x+b.width+P,z=b.y-P,Y=b.y+b.height+P;for(let $=0;$<h-1;$++)if(!(r[$+1]<=k||r[$]>=D))for(let K=0;K<l;K++)o[K]>z&&o[K]<Y&&(m[$][K]=false);for(let $=0;$<l-1;$++)if(!(o[$+1]<=z||o[$]>=Y))for(let K=0;K<h;K++)r[K]>k&&r[K]<D&&(v[K][$]=false);}let x=h*l,M=1/0,S=new Float64Array(x).fill(M),w=new Int32Array(x).fill(-1),C=new Uint8Array(x),I=b=>{let P=b%h,k=b/h|0;return Math.abs(r[P]-t.x)+Math.abs(o[k]-t.y)},H=(b,P)=>b%h===P%h,U=p*h+d,W=y*h+g;S[U]=0;let R=[U],N=[I(U)],Ce=b=>{let P=S[b]+I(b);R.push(b),N.push(P);let k=R.length-1;for(;k>0;){let D=k-1>>1;if(N[D]<=N[k])break;[R[D],R[k]]=[R[k],R[D]],[N[D],N[k]]=[N[k],N[D]],k=D;}},Ct=()=>{let b=R[0],P=R.pop(),k=N.pop();if(R.length){R[0]=P,N[0]=k;let D=0;for(;;){let z=D*2+1,Y=z+1,$=D;if(z<R.length&&N[z]<N[$]&&($=z),Y<R.length&&N[Y]<N[$]&&($=Y),$===D)break;[R[$],R[D]]=[R[D],R[$]],[N[$],N[D]]=[N[D],N[$]],D=$;}}return b},Te=(b,P,k)=>{let D=b%h,z=P%h,Y=S[b]+(Math.abs(z-D)===0?Math.abs(o[P/h|0]-o[b/h|0]):Math.abs(r[z]-r[D]))+k;Y<S[P]&&(S[P]=Y,w[P]=b,C[P]||Ce(P));};for(;R.length;){let b=Ct();if(b===W||C[b]){if(b===W)break;continue}C[b]=1;let P=b%h,k=b/h|0,D=w[b],z=D>=0?H(D,b)?"v":"h":"",Y=$=>{let K=H(b,$)?"v":"h";return z!==""&&z!==K?24:0};P+1<h&&m[P][k]&&Te(b,k*h+P+1,Y(k*h+P+1)),P-1>=0&&m[P-1][k]&&Te(b,k*h+P-1,Y(k*h+P-1)),k+1<l&&v[P][k]&&Te(b,(k+1)*h+P,Y((k+1)*h+P)),k-1>=0&&v[P][k-1]&&Te(b,(k-1)*h+P,Y((k-1)*h+P));}if(S[W]===M)return null;let Ge=[];for(let b=W;b!==-1;b=w[b])Ge.push({x:r[b%h],y:o[b/h|0]});Ge.reverse();let he=[];for(let b of Ge){let P=he.length;if(P>=2){let k=he[P-2],D=he[P-1];if(Math.abs(k.y-D.y)<1e-6&&Math.abs(D.y-b.y)<1e-6||Math.abs(k.x-D.x)<1e-6&&Math.abs(D.x-b.x)<1e-6){he[P-1]=b;continue}}he.push(b);}return he.slice(1)}generateCurvePoints(e){if(e.length<2)return e;let t=[],i=20,n=e[0],s=n;t.push(n);for(let r=1;r<e.length;r++){let o=e[r],h=this.computeCurveControlPoint(s,o);for(let l=1;l<=i;l++){let c=l/i,u=(1-c)*(1-c)*s.x+2*(1-c)*c*h.x+c*c*o.x,d=(1-c)*(1-c)*s.y+2*(1-c)*c*h.y+c*c*o.y;t.push({x:u,y:d});}s=o;}return t}computeCurveControlPoint(e,t){let i=t.x-e.x,n=t.y-e.y,s=Math.sqrt(i*i+n*n);if(s<1)return {x:e.x,y:e.y};let r=(e.x+t.x)/2,o=(e.y+t.y)/2,h=-n/s,l=i/s,c=Math.min(s*.2,30);return {x:r+h*c,y:o+l*c}}getDisplayLines(e,t,i,n,s,r){let o=n*1.3,h=Math.max(1,Math.floor(r/o)),l=`${t.id}\0${i}\0${n}\0${s}x${r}\0${h}`,c=this.textLayoutCache.get(l);if(c)return c;let u=i.split(`
|
|
2
|
-
`),d=[];for(let g of u){let y=this.wrapLine(e,g,s);for(let m of y)d.push(m);}let p=d.slice(0,h);return d.length>h&&p.length>0&&(p[p.length-1]=this.truncateLine(e,p[p.length-1],s)),this.textLayoutCache.size>=a.TEXT_CACHE_LIMIT&&this.textLayoutCache.clear(),this.textLayoutCache.set(l,p),p}drawShapeText(e,t){let i=t.text||"";if(!i)return;let n=t.fontSize||16;e.font=`${n}px sans-serif`,e.fillStyle=t.textColor||"#1f2937";let s=this.getTextBounds(t),r=n*1.3,o=this.getDisplayLines(e,t,i,n,s.width,s.height),l=-(o.length*r)/2+r/2,c=Be(t);e.textAlign="center",e.textBaseline="middle",o.forEach((u,d)=>{let p=l+d*r;e.fillText(u,c,p);});}getTextBounds(e){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "uml-class":case "uml-interface":return {width:e.width*.85,height:e.height*.85};case "brace-left":case "brace-right":return {width:e.width*(1-me)*.85,height:e.height*.85};case "circle":return {width:e.radius*2*.85,height:e.radius*2*.85};default:return {width:100,height:40}}}wrapLine(e,t,i){if(!t)return [""];let n=[],s="";for(let r of t){let o=s+r;e.measureText(o).width>i&&s?(n.push(s),s=r):s=o;}return s&&n.push(s),n}truncateLine(e,t,i){if(!t)return "...";let n="...",s=t;for(;s.length>0&&e.measureText(s+n).width>i;)s=s.slice(0,-1);return s+n}getBindingPoint(e,t){if(t.isPrecise&&t.normalizedAnchor){let i=this.getShapePageBounds(e),n={x:i.x+i.width*t.normalizedAnchor.x,y:i.y+i.height*t.normalizedAnchor.y},s={x:e.x,y:e.y};return this.nearestPointOnShapeOutline(e,s,n)}if(t.handle){let i=this.getShapeBounds(e);switch(t.handle){case "top":return {x:e.x,y:e.y+i.y};case "bottom":return {x:e.x,y:e.y+i.y+i.height};case "left":return {x:e.x+i.x,y:e.y};case "right":return {x:e.x+i.x+i.width,y:e.y}}}return {x:e.x,y:e.y}}getDockNormal(e,t,i){if(!t||t.size===0)return;let n=null,s=1/0,r=.5;if(t.forEach((c,u)=>{if(u===i)return;let d=this.getShapePageBounds(c);if(e.x>=d.x-r&&e.x<=d.x+d.width+r&&e.y>=d.y-r&&e.y<=d.y+d.height+r){let p=(e.x-c.x)**2+(e.y-c.y)**2;p<s&&(s=p,n=c);}}),!n)return;let o=n,h=e.x-o.x,l=e.y-o.y;return Math.abs(l)>Math.abs(h)?{x:0,y:l<0?-1:1}:{x:h<0?-1:1,y:0}}getShapePageBounds(e){let t=this.getShapeBounds(e);return {x:e.x+t.x,y:e.y+t.y,width:t.width,height:t.height}}clampToShapeOutline(e,t,i,n){if(!t||t.size===0)return e;let s=null,r=1/0;if(t.forEach((h,l)=>{if(l===i)return;let c=this.getShapePageBounds(h);if(e.x>c.x&&e.x<c.x+c.width&&e.y>c.y&&e.y<c.y+c.height){let u=(h.x-n.x)**2+(h.y-n.y)**2;u<r&&(r=u,s=h);}}),!s)return e;let o=s;return this.nearestPointOnShapeOutline(o,{x:o.x,y:o.y},e)}nearestPointOnShapeOutline(e,t,i){let n=i.x-t.x,s=i.y-t.y,r=Math.hypot(n,s);if(r<1e-6)return {x:t.x,y:t.y};let o=n/r,h=s/r,l=Math.max(1e4,r*2),c={x:t.x+o*l,y:t.y+h*l},u=this.intersectShapeOutlineWithSegment(e,t,c);if(u.length===0)return i;let d=u[0],p=(d.x-t.x)**2+(d.y-t.y)**2;for(let g=1;g<u.length;g++){let y=(u[g].x-t.x)**2+(u[g].y-t.y)**2;y<p&&(p=y,d=u[g]);}return d}intersectShapeOutlineWithSegment(e,t,i){let n={x:t.x-e.x,y:t.y-e.y},s={x:i.x-e.x,y:i.y-e.y};return this.intersectLocalShapeOutlineWithSegment(e,n,s).map(o=>({x:o.x+e.x,y:o.y+e.y}))}intersectLocalShapeOutlineWithSegment(e,t,i){switch(e.type){case "rectangle":case "rounded-rectangle":{let n=e.width,s=e.height,r={x:-n/2,y:-s/2,width:n,height:s};return this.intersectSegmentRect(t,i,r)}case "ellipse":case "diamond":case "triangle":{let n=e.width,s=e.height,r={x:-n/2,y:-s/2,width:n,height:s};return this.intersectSegmentRect(t,i,r)}case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":{let n=e.width,s=e.height,r={x:-n/2,y:-s/2,width:n,height:s};return this.intersectSegmentRect(t,i,r)}case "circle":{let n=e.radius;return this.intersectSegmentCircle(t,i,{x:0,y:0},n)}default:return []}}intersectSegmentRect(e,t,i){let n=i.x,s=i.y,r=i.width,o=i.height,h=[[{x:n,y:s},{x:n+r,y:s}],[{x:n+r,y:s},{x:n+r,y:s+o}],[{x:n+r,y:s+o},{x:n,y:s+o}],[{x:n,y:s+o},{x:n,y:s}]],l=[];for(let[c,u]of h){let d=this.segmentIntersection(e,t,c,u);d&&l.push(d);}return l}segmentIntersection(e,t,i,n){let s=e.x,r=e.y,o=t.x,h=t.y,l=i.x,c=i.y,u=n.x,d=n.y,p=(s-o)*(c-d)-(r-h)*(l-u);if(Math.abs(p)<1e-9)return null;let g=((s-l)*(c-d)-(r-c)*(l-u))/p,y=-((s-o)*(r-c)-(r-h)*(s-l))/p;return g<0||g>1||y<0||y>1?null:{x:s+g*(o-s),y:r+g*(h-r)}}intersectSegmentCircle(e,t,i,n){let s=t.x-e.x,r=t.y-e.y,o=e.x-i.x,h=e.y-i.y,l=s*s+r*r,c=2*(o*s+h*r),u=o*o+h*h-n*n,d=c*c-4*l*u;if(d<0)return [];d=Math.sqrt(d);let p=(-c-d)/(2*l),g=(-c+d)/(2*l),y=[];return p>=0&&p<=1&&y.push({x:e.x+p*s,y:e.y+p*r}),g>=0&&g<=1&&Math.abs(g-p)>1e-6&&y.push({x:e.x+g*s,y:e.y+g*r}),y}drawArrowHead(e,t,i){let n=Math.atan2(i.y-t.y,i.x-t.x),s=12;e.save(),e.setLineDash([]),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(i.x-s*Math.cos(n-Math.PI/6),i.y-s*Math.sin(n-Math.PI/6)),e.moveTo(i.x,i.y),e.lineTo(i.x-s*Math.cos(n+Math.PI/6),i.y-s*Math.sin(n+Math.PI/6)),e.stroke(),e.restore();}drawInheritanceHead(e,t,i){let n=Math.atan2(i.y-t.y,i.x-t.x),s=14,r=i.x,o=i.y,h=i.x-s*Math.cos(n),l=i.y-s*Math.sin(n),c=h+s*.5*Math.cos(n-Math.PI/2),u=l+s*.5*Math.sin(n-Math.PI/2),d=h+s*.5*Math.cos(n+Math.PI/2),p=l+s*.5*Math.sin(n+Math.PI/2);e.save(),e.setLineDash([]),e.fillStyle="#ffffff",e.beginPath(),e.moveTo(r,o),e.lineTo(c,u),e.lineTo(d,p),e.closePath(),e.fill(),e.stroke(),e.restore();}drawDiamondHead(e,t,i,n){let s=Math.atan2(i.y-t.y,i.x-t.x),r=14,o=i.x,h=i.y,l=i.x-r*.5*Math.cos(s),c=i.y-r*.5*Math.sin(s),u=i.x-r*Math.cos(s),d=i.y-r*Math.sin(s),p=l+r*.4*Math.cos(s-Math.PI/2),g=c+r*.4*Math.sin(s-Math.PI/2),y=l+r*.4*Math.cos(s+Math.PI/2),m=c+r*.4*Math.sin(s+Math.PI/2);e.save(),e.setLineDash([]),e.fillStyle=n?"#1f2937":"#ffffff",e.beginPath(),e.moveTo(o,h),e.lineTo(p,g),e.lineTo(u,d),e.lineTo(y,m),e.closePath(),e.fill(),e.stroke(),e.restore();}drawArrowHeads(e,t,i){if(i!=="none"){if(i==="start"||i==="both"){let n=t[0],s=t[1];this.drawArrowHead(e,s,n);}if(i==="end"||i==="both"){let n=t.length,s=t[n-2],r=t[n-1];this.drawArrowHead(e,s,r);}if(i==="inheritance"){let n=t.length,s=t[n-2],r=t[n-1];this.drawInheritanceHead(e,s,r);}if(i==="aggregation"){let n=t[0],s=t[1];this.drawDiamondHead(e,s,n,false);}if(i==="composition"){let n=t[0],s=t[1];this.drawDiamondHead(e,s,n,true);}if(i==="dependency"){let n=t.length;this.drawArrowHead(e,t[n-2],t[n-1]);}if(i==="realization"){let n=t.length;this.drawInheritanceHead(e,t[n-2],t[n-1]);}}}drawText(e,t){if(!t.text)return;let i=t.fontSize;e.font=`${i}px sans-serif`,e.textAlign="center",e.textBaseline="middle";let n=t.width,s=t.height,r=i*1.3,o=this.getDisplayLines(e,t,t.text,i,n,s),h=o.length*r,l=-h/2+r/2;if(t.highlightColor&&t.highlightColor!=="transparent"){let c=t.highlightOpacity??1;e.fillStyle=c<1?F(t.highlightColor,c):t.highlightColor;let u=i*.15;e.beginPath(),e.roundRect(-n/2-u,-h/2-u,n+u*2,h+u*2,4),e.fill();}e.fillStyle=t.textColor||"#1f2937",o.forEach((c,u)=>{let d=l+u*r;e.fillText(c,0,d);});}drawFreehand(e,t){if(t.points.length<2)return;let i=t.points;e.beginPath(),e.moveTo(i[0].x,i[0].y);for(let n=1;n<i.length-1;n++){let s=(i[n].x+i[n+1].x)/2,r=(i[n].y+i[n+1].y)/2;e.quadraticCurveTo(i[n].x,i[n].y,s,r);}e.lineTo(i[i.length-1].x,i[i.length-1].y),e.stroke();}drawSelectionOutline(e,t,i){let n=this.getShapeBounds(t);e.strokeStyle=L.selection.outline,e.lineWidth=1/i,e.setLineDash([]),e.save(),e.shadowColor=L.selection.shadow,e.shadowBlur=8,e.shadowOffsetX=0,e.shadowOffsetY=0,e.beginPath(),e.roundRect(n.x-2,n.y-2,n.width+4,n.height+4,4),e.stroke(),e.restore(),e.fillStyle=L.selection.outlineFill,e.beginPath(),e.roundRect(n.x-2,n.y-2,n.width+4,n.height+4,4),e.fill();}drawSelectionHandles(e,t,i){let n=8/i;if((t.type==="line"||t.type==="arrow")&&t.points&&t.points.length>=2){let r=t.points.map(u=>({x:u.x-t.x,y:u.y-t.y})),o=r[0],h=r[r.length-1];e.fillStyle=L.selection.handleFill,e.strokeStyle=L.selection.handleStroke,e.lineWidth=2/i,e.setLineDash([]),[o,h].forEach(u=>{e.beginPath(),e.arc(u.x,u.y,n/2,0,Math.PI*2),e.fill(),e.stroke();});let l=(o.x+h.x)/2,c=(o.y+h.y)/2;e.beginPath(),e.arc(l,c,7/i,0,Math.PI*2),e.fillStyle=L.selection.handleFill,e.fill(),e.strokeStyle=L.primary,e.lineWidth=1.5/i,e.stroke(),e.beginPath(),e.arc(l,c,3/i,0,Math.PI*2),e.fillStyle=L.genDot,e.fill();}else {let r=this.getShapeBounds(t),o=n,h=[{x:r.x,y:r.y},{x:r.x+r.width,y:r.y},{x:r.x+r.width,y:r.y+r.height},{x:r.x,y:r.y+r.height}];e.fillStyle=L.selection.handleFill,e.strokeStyle=L.selection.handleStroke,e.lineWidth=2/i,e.setLineDash([]),h.forEach(g=>{e.beginPath(),e.roundRect(g.x-o/2,g.y-o/2,o,o,2),e.fill(),e.stroke();});let l=9/i;[{x:r.x+r.width/2,y:r.y},{x:r.x+r.width,y:r.y+r.height/2},{x:r.x+r.width/2,y:r.y+r.height},{x:r.x,y:r.y+r.height/2}].forEach(g=>{e.beginPath(),e.arc(g.x,g.y,l,0,Math.PI*2),e.fillStyle=L.selection.handleFill,e.fill(),e.strokeStyle=L.primary,e.lineWidth=1.5/i,e.stroke(),e.beginPath(),e.arc(g.x,g.y,l*.45,0,Math.PI*2),e.fillStyle=L.primary,e.fill();});let u=26/i,d=r.x+r.width/2,p=r.y-u;e.strokeStyle="#94a3b8",e.lineWidth=1.5/i,e.setLineDash([]),e.beginPath(),e.moveTo(d,r.y),e.lineTo(d,p),e.stroke(),e.beginPath(),e.arc(d,p,8/i,0,Math.PI*2),e.fillStyle=L.selection.handleFill,e.fill(),e.strokeStyle=L.primary,e.lineWidth=1.6/i,e.stroke(),this.drawRotateIcon(e,d,p,i);}}drawRotateIcon(e,t,i,n){let s=8/n;e.save(),e.translate(t,i),e.strokeStyle=L.primary,e.lineWidth=1.8/n,e.lineCap="round",e.beginPath(),e.arc(0,0,s*.5,Math.PI*.18,Math.PI*1.6,false),e.stroke();let r=Math.PI*.18,o=Math.cos(r)*s*.5,h=Math.sin(r)*s*.5,l=1;e.beginPath(),e.moveTo(o,h),e.lineTo(o-Math.cos(r)*s*.26+l*Math.sin(r)*s*.16,h-Math.sin(r)*s*.26-l*Math.cos(r)*s*.16),e.moveTo(o,h),e.lineTo(o-Math.cos(r)*s*.26-l*Math.sin(r)*s*.16,h-Math.sin(r)*s*.26+l*Math.cos(r)*s*.16),e.stroke(),e.restore();}getShapeBounds(e){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":case "mind-map":return {x:-e.width/2,y:-e.height/2,width:e.width,height:e.height};case "circle":return {x:-e.radius,y:-e.radius,width:e.radius*2,height:e.radius*2};case "line":case "arrow":{if(e.points.length===0)return {x:0,y:0,width:0,height:0};let t=1/0,i=1/0,n=-1/0,s=-1/0;return e.points.forEach(r=>{t=Math.min(t,r.x-e.x),i=Math.min(i,r.y-e.y),n=Math.max(n,r.x-e.x),s=Math.max(s,r.y-e.y);}),{x:t,y:i,width:n-t,height:s-i}}case "draw":{let t=e;if(typeof t.width=="number"&&typeof t.height=="number")return {x:-t.width/2,y:-t.height/2,width:t.width,height:t.height};if(t.points.length===0)return {x:0,y:0,width:0,height:0};let i=1/0,n=1/0,s=-1/0,r=-1/0;return t.points.forEach(o=>{let h=o.x-t.x,l=o.y-t.y;i=Math.min(i,h),n=Math.min(n,l),s=Math.max(s,h),r=Math.max(r,l);}),{x:i,y:n,width:s-i,height:r-n}}case "text":{let t=e;return {x:-t.width/2,y:-t.height/2,width:t.width,height:t.height}}default:return {x:0,y:0,width:0,height:0}}}};var ae={create(a=0,e=0){return {x:a,y:e}},add(a,e){return {x:a.x+e.x,y:a.y+e.y}},subtract(a,e){return {x:a.x-e.x,y:a.y-e.y}},multiply(a,e){return {x:a.x*e,y:a.y*e}},divide(a,e){return {x:a.x/e,y:a.y/e}},dot(a,e){return a.x*e.x+a.y*e.y},length(a){return Math.sqrt(a.x*a.x+a.y*a.y)},normalize(a){let e=ae.length(a);return e===0?{x:0,y:0}:{x:a.x/e,y:a.y/e}},distance(a,e){return ae.length(ae.subtract(a,e))},rotate(a,e){let t=Math.cos(e),i=Math.sin(e);return {x:a.x*t-a.y*i,y:a.x*i+a.y*t}},clone(a){return {x:a.x,y:a.y}},equals(a,e){return a.x===e.x&&a.y===e.y}};function ee(a,e,t,i=8){let n=[];if(t==="horizontal"){let s=Math.min(a.x,e.x),r=Math.max(a.x+a.width,e.x+e.width),o=Math.abs(a.y-e.y)<=i,h=Math.abs(a.y+a.height-(e.y+e.height))<=i,l=Math.abs(a.y+a.height/2-(e.y+e.height/2))<=i;o&&n.push({type:"horizontal",position:(a.y+e.y)/2,start:s,end:r}),h&&n.push({type:"horizontal",position:(a.y+a.height+e.y+e.height)/2,start:s,end:r}),l&&!(o&&h)&&n.push({type:"horizontal",position:(a.y+a.height/2+e.y+e.height/2)/2,start:s,end:r});}else {let s=Math.min(a.y,e.y),r=Math.max(a.y+a.height,e.y+e.height),o=Math.abs(a.x-e.x)<=i,h=Math.abs(a.x+a.width-(e.x+e.width))<=i,l=Math.abs(a.x+a.width/2-(e.x+e.width/2))<=i;o&&n.push({type:"vertical",position:(a.x+e.x)/2,start:s,end:r}),h&&n.push({type:"vertical",position:(a.x+a.width+e.x+e.width)/2,start:s,end:r}),l&&!(o&&h)&&n.push({type:"vertical",position:(a.x+a.width/2+e.x+e.width/2)/2,start:s,end:r});}return n}var xe=class a extends V{type="select";state="idle";translatingState=null;resizingState=null;generatingState=null;hoverGenPoint=null;rotatingState=null;draggingArrowId=null;draggingTerminal=null;pendingHistoryCommit=false;brushStart={x:0,y:0};brushShiftKey=false;defaultGenType(e){if(e){let t=this.context.getShapes().find(i=>i.id===e);if(t&&$e(t.type))return t.type}return "rectangle"}genPreviewOf(e,t){return {from:e.point,to:t,type:this.defaultGenType(e.sourceShapeId)}}genContext(e,t){return {from:e.point,to:t,sourceShapeId:e.sourceShapeId,edge:e.edge}}onPointerDown(e,t){let i=this.context.getShapes(),n=this.context.getSelectedIds(),s=this.hitTestArrowTerminal(e,i);if(s&&n.includes(s.shapeId)){this.state="dragging-terminal",this.draggingArrowId=s.shapeId,this.draggingTerminal=s.terminal,this.pendingHistoryCommit=true,this.context.updateArrowBinding(s.shapeId,s.terminal,null);return}let r=this.hitGenPoint(e,i,n);if(r){this.state="generating",this.generatingState={source:r,startPoint:e,hasMoved:false},this.pendingHistoryCommit=true,this.context.setGenerationPreview(this.genPreviewOf(r,this.genPreviewTarget(r,e,false)));return}let o=this.hitTest(e,i,n);if(o.handle==="rotation"){this.state="rotating",this.pendingHistoryCommit=true;let h=o.shape;this.rotatingState={shapeId:h.id,center:{x:h.x,y:h.y},startAngle:Math.atan2(e.y-h.y,e.x-h.x),startRotation:h.rotation};return}if(o.handle&&o.shape&&o.shape.type!=="mind-map"){this.state="resizing",this.resizingState={startPoint:e,startShape:o.shape,handle:o.handle},this.pendingHistoryCommit=true;return}if(o.shape){let h=o.shape,l=h.type==="line"||h.type==="arrow",c=n.includes(h.id);if(l&&!c){this.context.selectShape(h.id,t.shiftKey);return}this.state="translating",this.pendingHistoryCommit=true,c||this.context.selectShape(h.id,t.shiftKey);let u=this.context.getSelectedIds(),d=new Map,p=new Map,g=new Map;u.forEach(y=>{let m=i.find(v=>v.id===y);m&&(d.set(y,{x:m.x,y:m.y}),p.set(y,m.rotation),g.set(y,this.getShapeWorldBounds(m)));}),this.translatingState={startPoint:e,startPositions:d,startRotation:p,startBounds:g};}else this.state="brushing",this.brushStart=e,this.brushShiftKey=t.shiftKey,t.shiftKey||this.context.selectShape("",false);}onPointerMove(e,t){let i=this.context.getShapes();if(this.state==="translating"&&this.translatingState)this.pendingHistoryCommit&&(this.context.commitHistory(),this.pendingHistoryCommit=false),this.handleTranslating(e,t);else if(this.state==="generating"&&this.generatingState)this.pendingHistoryCommit&&(this.context.commitHistory(),this.pendingHistoryCommit=false),this.handleGenerating(e,i);else if(this.state==="rotating"&&this.rotatingState)this.pendingHistoryCommit&&(this.context.commitHistory(),this.pendingHistoryCommit=false),this.handleRotating(e,t);else if(this.state==="resizing"&&this.resizingState)this.pendingHistoryCommit&&(this.context.commitHistory(),this.pendingHistoryCommit=false),this.handleResizing(e);else if(this.state==="dragging-terminal"&&this.draggingArrowId&&this.draggingTerminal){this.pendingHistoryCommit&&(this.context.commitHistory(),this.pendingHistoryCommit=false);let n=i.find(s=>s.id===this.draggingArrowId);if(n){let s=this.draggingTerminal==="start"?e:n.points[n.points.length-1],r=this.draggingTerminal==="start"?n.points[0]:e,o=this.computeSnapGuidesForArrow(s,r,e,n,i);this.context.setSnapGuides(o);let h=e;if(o.length>0){let u=o.filter(p=>p.type==="horizontal"),d=o.filter(p=>p.type==="vertical");if(u.length>0){let p=u[0].position,g=Math.abs(e.y-p);for(let y of u){let m=Math.abs(e.y-y.position);m<g&&(g=m,p=y.position);}h={...h,y:p};}else if(d.length>0){let p=d[0].position,g=Math.abs(e.x-p);for(let y of d){let m=Math.abs(e.x-y.position);m<g&&(g=m,p=y.position);}h={...h,x:p};}}let l=this.draggingTerminal==="start"?0:n.points.length-1,c=[...n.points];c[l]=h,this.context.updateShape(this.draggingArrowId,{points:c});}}else if(this.state==="brushing")this.handleBrushing(e);else {let n=this.context.getSelectedIds(),s=this.hitGenPoint(e,i,n);s?(this.hoverGenPoint=s,this.context.setGenerationPreview(this.genPreviewOf(s,this.genPreviewTarget(s,e,false)))):this.hoverGenPoint&&(this.hoverGenPoint=null,this.context.setGenerationPreview(null));}}onPointerUp(e,t){if(this.state==="dragging-terminal"&&this.draggingArrowId&&this.draggingTerminal){let i=this.context.getShapes(),n=this.findBindingNearPoint(e,i,this.draggingArrowId);n&&this.context.updateArrowBinding(this.draggingArrowId,this.draggingTerminal,n.toShapeId,n.binding);}if(this.state==="generating"&&this.generatingState){let i=this.generatingState,n=this.context.getShapes(),s=i.source,r=this.findBindingNearPoint(e,n,s.sourceShapeId);if(r)this.context.connectToExisting(this.genContext(s,e),r.toShapeId,r.binding);else if(!i.hasMoved)this.context.createGeneratedConnection(this.genContext(s,this.genPreviewTarget(s,s.point,false)),{type:this.defaultGenType(s.sourceShapeId)});else {let o=this.context.createFloatingLine(this.genContext(s,e));this.context.openGenerationMenu({context:this.genContext(s,e),existingArrowId:o,defaultShapeType:this.defaultGenType(s.sourceShapeId)});}this.generatingState=null,this.context.setGenerationPreview(null);}this.state==="brushing"&&this.context.setBrushRect(null),this.context.setConnectingTarget(null),this.hoverGenPoint=null,this.pendingHistoryCommit=false,this.state="idle",this.draggingArrowId=null,this.draggingTerminal=null,this.translatingState=null,this.resizingState=null,this.rotatingState=null,this.context.setSnapGuides([]);}onKeyDown(e){let t=e.target;if(!(t&&(t.tagName==="INPUT"||t.tagName==="TEXTAREA"||t.isContentEditable))){if(e.key==="Delete"||e.key==="Backspace"){let i=this.context.getSelectedIds();i.length>0&&(this.context.commitHistory(),i.forEach(n=>{this.context.deleteShape(n);}));}e.key==="Escape"&&(this.context.selectShape("",false),this.context.setSnapGuides([]),this.state="idle"),(e.key==="+"||e.key==="=")&&e.shiftKey&&(this.context.commitHistory(),this.handleRotation(15)),e.key==="-"&&e.shiftKey&&(this.context.commitHistory(),this.handleRotation(-15)),e.key==="ArrowUp"&&(e.preventDefault(),this.context.commitHistory(),this.handleNudge(0,-5)),e.key==="ArrowDown"&&(e.preventDefault(),this.context.commitHistory(),this.handleNudge(0,5)),e.key==="ArrowLeft"&&(e.preventDefault(),this.context.commitHistory(),this.handleNudge(-5,0)),e.key==="ArrowRight"&&(e.preventDefault(),this.context.commitHistory(),this.handleNudge(5,0));}}onKeyUp(e){}getCursor(e){switch(this.state){case "translating":return "grabbing";case "generating":return "crosshair";case "rotating":return "grabbing";case "resizing":return this.resizingState?this.getHandleCursor(this.resizingState.handle,this.resizingState.startShape):"default";case "dragging-terminal":return "crosshair";case "brushing":return "crosshair"}let t=this.context.getShapes(),i=this.context.getSelectedIds();if(this.context.getMindMapCursorHitAtPoint(e))return "pointer";if(this.hitGenPoint(e,t,i))return "crosshair";if(this.hitTestArrowTerminal(e,t)&&i.includes(this.hitTestArrowTerminal(e,t).shapeId))return "move";let n=this.hitTest(e,t,i);return n.handle==="rotation"?"grab":n.handle?this.getHandleCursor(n.handle,n.shape):n.shape?"move":null}getHandleCursor(e,t){let n={e:0,se:45,s:90,sw:135,w:180,nw:225,n:270,ne:315}[e];if(n===void 0)return "grab";let s=((n+t.rotation*180/Math.PI)%360+360)%360,r=Math.round(s/45)%4;return ["ew-resize","nwse-resize","ns-resize","nesw-resize"][r]}static ALIGN_SNAP_THRESHOLD=8;handleTranslating(e,t){if(!this.translatingState)return;let i=ae.subtract(e,this.translatingState.startPoint),n={...i};t.shiftKey&&(Math.abs(i.x)>Math.abs(i.y)?n.y=0:n.x=0);let s=this.context.getShapes(),r=this.context.getSelectedIds(),o=new Set(r),h=[],l=[];if(this.translatingState.startBounds.forEach(c=>{l.push({x:c.x+n.x,y:c.y+n.y,width:c.width,height:c.height});}),!t.shiftKey&&l.length>0){let c=[];for(let p of s)o.has(p.id)||p.type==="line"||p.type==="arrow"||p.type==="draw"||c.push(this.getShapeWorldBounds(p));let u=[],d=[];for(let p of l){let g=[p.y,p.y+p.height/2,p.y+p.height],y=[p.x,p.x+p.width/2,p.x+p.width];for(let m of c){let v=[m.y,m.y+m.height/2,m.y+m.height];for(let M of g)for(let S of v){let w=S-M;Math.abs(w)<=a.ALIGN_SNAP_THRESHOLD&&u.push(w);}let x=[m.x,m.x+m.width/2,m.x+m.width];for(let M of y)for(let S of x){let w=S-M;Math.abs(w)<=a.ALIGN_SNAP_THRESHOLD&&d.push(w);}}}if(u.length>0){let p=u[0];for(let g of u)Math.abs(g)<Math.abs(p)&&(p=g);n.y+=p;}if(d.length>0){let p=d[0];for(let g of d)Math.abs(g)<Math.abs(p)&&(p=g);n.x+=p;}this.translatingState.startBounds.forEach(p=>{let g={x:p.x+n.x,y:p.y+n.y,width:p.width,height:p.height};for(let y of c)h.push(...ee(g,y,"horizontal",1)),h.push(...ee(g,y,"vertical",1));});}this.translatingState.startPositions.forEach((c,u)=>{this.context.updateShape(u,{x:c.x+n.x,y:c.y+n.y});}),this.context.setSnapGuides(this.mergeGuides(h));}handleResizing(e){if(!this.resizingState)return;let t=this.resizingState.startShape,i=this.resizingState.handle,n=this.resizingState.startPoint;if(!i||i==="rotation")return;let r={nw:[-1,-1],n:[0,-1],ne:[1,-1],w:[-1,0],e:[1,0],sw:[-1,1],s:[0,1],se:[1,1]}[i];if(!r)return;let[o,h]=r,l=ae.subtract(e,n),c=[];for(let w of this.context.getShapes())w.id!==t.id&&(w.type==="line"||w.type==="arrow"||w.type==="draw"||c.push(this.getShapeWorldBounds(w)));let u=20,d=this.getShapeWorldBounds(t),p=d.x,g=d.y,y=d.x+d.width,m=d.y+d.height;o===1?y=Math.max(p+u,y+l.x):o===-1&&(p=Math.min(y-u,p+l.x)),h===1?m=Math.max(g+u,m+l.y):h===-1&&(g=Math.min(m-u,g+l.y));let v=(w,C)=>{let I=null;for(let H of C){let U=H-w;Math.abs(U)<=a.ALIGN_SNAP_THRESHOLD&&(I===null||Math.abs(U)<Math.abs(I))&&(I=U);}return I};if(o!==0&&c.length>0){let w=[];for(let I of c)w.push(I.x,I.x+I.width/2,I.x+I.width);let C=v(o===1?y:p,w);C!==null&&(o===1?y+=C:p+=C);}if(h!==0&&c.length>0){let w=[];for(let I of c)w.push(I.y,I.y+I.height/2,I.y+I.height);let C=v(h===1?m:g,w);C!==null&&(h===1?m+=C:g+=C);}let x={x:p,y:g,width:y-p,height:m-g},M=[];for(let w of c)o!==0&&M.push(...ee(x,w,"vertical",1)),h!==0&&M.push(...ee(x,w,"horizontal",1));this.context.setSnapGuides(this.mergeGuides(M));let S={};if(t.type==="circle"){let w=Math.max(10,Math.max(o!==0?x.width/2:0,h!==0?x.height/2:0));if(S.radius=w,o!==0){let C=o===1?d.x:d.x+d.width;S.x=o===1?C+w:C-w;}if(h!==0){let C=h===1?d.y:d.y+d.height;S.y=h===1?C+w:C-w;}}else if("width"in t&&o!==0&&(S.width=x.width,S.x=x.x+x.width/2),"height"in t&&h!==0&&(S.height=x.height,S.y=x.y+x.height/2),t.type==="draw"){let w=this.getShapeWorldBounds(t),C=o!==0?x.width/Math.max(1,w.width):1,I=h!==0?x.height/Math.max(1,w.height):1;S.points=t.points.map(H=>({x:H.x*C,y:H.y*I}));}Object.keys(S).length>0&&this.context.updateShape(t.id,S);}mergeGuides(e){let t=new Map;for(let i of e){let n=`${i.type}\0${Math.round(i.position*2)/2}`,s=t.get(n);s?(s.start=Math.min(s.start,i.start),s.end=Math.max(s.end,i.end)):t.set(n,{...i});}return [...t.values()]}handleRotation(e){let t=this.context.getSelectedIds(),i=this.context.getShapes();t.forEach(n=>{let s=i.find(r=>r.id===n);s&&this.context.updateShape(n,{rotation:s.rotation+e*Math.PI/180});});}handleNudge(e,t){let i=this.context.getSelectedIds(),n=this.context.getShapes();i.forEach(s=>{let r=n.find(o=>o.id===s);r&&this.context.updateShape(s,{x:r.x+e,y:r.y+t});});}handleBrushing(e){let t=Math.min(this.brushStart.x,e.x),i=Math.min(this.brushStart.y,e.y),n=Math.abs(e.x-this.brushStart.x),s=Math.abs(e.y-this.brushStart.y);this.context.setBrushRect({x:t,y:i,width:n,height:s});let r=this.context.getShapes(),o=[];for(let h of r){let l=this.getShapeWorldBounds(h);this.rectsIntersect({x:t,y:i,width:n,height:s},l)&&o.push(h.id);}if(this.brushShiftKey){let h=this.context.getSelectedIds(),l=new Set([...h,...o]);this.context.selectShapes([...l]);}else this.context.selectShapes(o);}getShapeWorldBounds(e){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":case "mind-map":return {x:e.x-e.width/2,y:e.y-e.height/2,width:e.width,height:e.height};case "circle":return {x:e.x-e.radius,y:e.y-e.radius,width:e.radius*2,height:e.radius*2};case "line":case "arrow":{if(!e.points||e.points.length===0)return {x:e.x,y:e.y,width:0,height:0};let t=1/0,i=1/0,n=-1/0,s=-1/0;return e.points.forEach(r=>{t=Math.min(t,r.x),i=Math.min(i,r.y),n=Math.max(n,r.x),s=Math.max(s,r.y);}),{x:t,y:i,width:n-t,height:s-i}}case "draw":{let t=e,i=t.width,n=t.height;if(typeof i=="number"&&typeof n=="number"&&i>0&&n>0)return {x:t.x-i/2,y:t.y-n/2,width:i,height:n};if(!t.points||t.points.length===0)return {x:t.x,y:t.y,width:0,height:0};let s=1/0,r=1/0,o=-1/0,h=-1/0;return t.points.forEach(l=>{let c=l.x+t.x,u=l.y+t.y;s=Math.min(s,c),r=Math.min(r,u),o=Math.max(o,c),h=Math.max(h,u);}),{x:s,y:r,width:o-s,height:h-r}}case "text":{let t=e;return {x:e.x-t.width/2,y:e.y-t.height/2,width:t.width,height:t.height}}default:{let t=e;return {x:t.x,y:t.y,width:0,height:0}}}}rectsIntersect(e,t){return e.x<t.x+t.width&&e.x+e.width>t.x&&e.y<t.y+t.height&&e.y+e.height>t.y}hitTest(e,t,i){for(let n=t.length-1;n>=0;n--){let s=t[n];if(i.includes(s.id)){let r=this.getHitHandle(e,s);if(r)return {shape:s,handle:r}}if(this.isPointInShape(e,s))return {shape:s,handle:null}}return {shape:null,handle:null}}getHitHandle(e,t){if(t.type==="mind-map")return null;let i=14,n=this.getShapeBounds(t),s=Math.cos(-t.rotation),r=Math.sin(-t.rotation),o=e.x-t.x,h=e.y-t.y,l=o*s-h*r,c=o*r+h*s,u=[{type:"nw",x:n.x,y:n.y},{type:"ne",x:n.x+n.width,y:n.y},{type:"se",x:n.x+n.width,y:n.y+n.height},{type:"sw",x:n.x,y:n.y+n.height},{type:"rotation",x:n.x+n.width/2,y:n.y-26}];for(let d of u)if(Math.abs(l-d.x)<i&&Math.abs(c-d.y)<i)return d.type;return null}static GEN_HIT_THRESHOLD=14;static CLICK_MOVE_THRESHOLD=6;computeGenPoints(e,t){let i=[];for(let n of t){let s=e.find(r=>r.id===n);if(s){if(s.type==="line"||s.type==="arrow"){if(s.points&&s.points.length>=2){let r=this.getShapeWorldBounds(s);i.push({point:{x:r.x+r.width/2,y:r.y+r.height/2},sourceShapeId:s.id});}}else if(s.type==="rectangle"||s.type==="rounded-rectangle"||s.type==="ellipse"||s.type==="diamond"||s.type==="triangle"||s.type==="hexagon"||s.type==="parallelogram"||s.type==="trapezoid"||s.type==="pentagon"||s.type==="octagon"||s.type==="star"||s.type==="uml-class"||s.type==="uml-interface"||s.type==="circle"||s.type==="draw"||s.type==="cylinder"||s.type==="cloud"||s.type==="heart"||s.type==="block-arrow-right"||s.type==="block-arrow-left"||s.type==="block-arrow-up"||s.type==="block-arrow-down"||s.type==="block-arrow-double"||s.type==="right-triangle"||s.type==="cross"||s.type==="speech-rect"||s.type==="speech-round"||s.type==="cube"||s.type==="sector"||s.type==="delay"||s.type==="predefined-process"||s.type==="document"||s.type==="stadium"||s.type==="class-catalog"||s.type==="chevron"||s.type==="brace-left"||s.type==="brace-right"){let r=this.getShapeBounds(s),o={x:s.x,y:s.y};i.push({point:{x:o.x+r.x+r.width/2,y:o.y+r.y},edge:"n",sourceShapeId:s.id}),i.push({point:{x:o.x+r.x+r.width,y:o.y+r.y+r.height/2},edge:"e",sourceShapeId:s.id}),i.push({point:{x:o.x+r.x+r.width/2,y:o.y+r.y+r.height},edge:"s",sourceShapeId:s.id}),i.push({point:{x:o.x+r.x,y:o.y+r.y+r.height/2},edge:"w",sourceShapeId:s.id});}}}return i}hitGenPoint(e,t,i){let n=a.GEN_HIT_THRESHOLD,s=null,r=n;for(let o of this.computeGenPoints(t,i)){let h=Math.hypot(o.point.x-e.x,o.point.y-e.y);h<=r&&(r=h,s=o);}return s}genPreviewTarget(e,t,i){if(i)return t;let n=96;if(e.edge)switch(e.edge){case "n":return {x:e.point.x,y:e.point.y-n};case "s":return {x:e.point.x,y:e.point.y+n};case "e":return {x:e.point.x+n,y:e.point.y};case "w":return {x:e.point.x-n,y:e.point.y}}return {x:e.point.x,y:e.point.y-n}}handleGenerating(e,t){if(!this.generatingState)return;let i=this.generatingState,n=i.source;Math.hypot(e.x-i.startPoint.x,e.y-i.startPoint.y)>a.CLICK_MOVE_THRESHOLD&&(i.hasMoved=true);let r=this.findBindingNearPoint(e,t,n.sourceShapeId),o=r?this.bindingPosition(r.toShapeId,r.binding,t):i.hasMoved?e:this.genPreviewTarget(n,e,false);this.context.setConnectingTarget(r?r.toShapeId:null),this.context.setGenerationPreview({from:n.point,to:o,type:r?void 0:this.defaultGenType(n.sourceShapeId)});}handleRotating(e,t){if(!this.rotatingState)return;let i=this.rotatingState,n=Math.atan2(e.y-i.center.y,e.x-i.center.x),s=i.startRotation+(n-i.startAngle);t.shiftKey&&(s=Math.round(s/(Math.PI/12))*(Math.PI/12)),this.context.updateShape(i.shapeId,{rotation:s});}bindingPosition(e,t,i){let n=i.find(r=>r.id===e);if(!n)return {x:0,y:0};let s=this.getShapeWorldBounds(n);if(t.handle)switch(t.handle){case "top":return {x:n.x,y:s.y};case "bottom":return {x:n.x,y:s.y+s.height};case "left":return {x:s.x,y:n.y};case "right":return {x:s.x+s.width,y:n.y}}return t.normalizedAnchor?{x:s.x+s.width*t.normalizedAnchor.x,y:s.y+s.height*t.normalizedAnchor.y}:{x:n.x,y:n.y}}isPointInShape(e,t){let i=e.x-t.x,n=e.y-t.y;switch(t.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":case "mind-map":return Math.abs(i)<=t.width/2&&Math.abs(n)<=t.height/2;case "circle":return i*i+n*n<=t.radius*t.radius;case "hexagon":return this.isPointInHexagon(i,n,t.width,t.height);case "line":case "arrow":for(let s=0;s<t.points.length-1;s++){let r=t.points[s],o=t.points[s+1];if(this.pointToLineDistance(e,r,o)<10)return true}return false;case "draw":{let s=this.getShapeBounds(t);return Math.abs(i)<=s.width/2&&Math.abs(n)<=s.height/2}case "text":return Math.abs(i)<=50&&Math.abs(n)<=t.fontSize/2;default:return false}}isPointInHexagon(e,t,i,n){let s=Math.abs(e),r=Math.abs(t),o=i/2,h=n/2;return s<=o&&r<=h&&s/o+r/h<=1.5}pointToLineDistance(e,t,i){let n=e.x-t.x,s=e.y-t.y,r=i.x-t.x,o=i.y-t.y,h=n*r+s*o,l=r*r+o*o,c=-1;l!==0&&(c=h/l);let u,d;c<0?(u=t.x,d=t.y):c>1?(u=i.x,d=i.y):(u=t.x+c*r,d=t.y+c*o);let p=e.x-u,g=e.y-d;return Math.sqrt(p*p+g*g)}getShapeBounds(e){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":case "mind-map":return {x:-e.width/2,y:-e.height/2,width:e.width,height:e.height};case "circle":return {x:-e.radius,y:-e.radius,width:e.radius*2,height:e.radius*2};case "line":case "arrow":{if(e.points.length===0)return {x:0,y:0,width:0,height:0};let t=1/0,i=1/0,n=-1/0,s=-1/0;return e.points.forEach(r=>{t=Math.min(t,r.x-e.x),i=Math.min(i,r.y-e.y),n=Math.max(n,r.x-e.x),s=Math.max(s,r.y-e.y);}),{x:t,y:i,width:n-t,height:s-i}}case "draw":{let t=e;if(typeof t.width=="number"&&typeof t.height=="number")return {x:-t.width/2,y:-t.height/2,width:t.width,height:t.height};if(t.points.length===0)return {x:0,y:0,width:0,height:0};let i=1/0,n=1/0,s=-1/0,r=-1/0;return t.points.forEach(o=>{let h=o.x-t.x,l=o.y-t.y;i=Math.min(i,h),n=Math.min(n,l),s=Math.max(s,h),r=Math.max(r,l);}),{x:i,y:n,width:s-i,height:r-n}}case "text":return {x:-50,y:-e.fontSize/2,width:100,height:e.fontSize};default:return {x:0,y:0,width:0,height:0}}}hitTestArrowTerminal(e,t){for(let n of t){if(n.type!=="arrow")continue;let s=n;if(s.points.length<2)continue;let r=s.points[0],o=s.points[s.points.length-1],h=Math.sqrt(Math.pow(e.x-r.x,2)+Math.pow(e.y-r.y,2)),l=Math.sqrt(Math.pow(e.x-o.x,2)+Math.pow(e.y-o.y,2));if(h<20)return {shapeId:s.id,terminal:"start"};if(l<20)return {shapeId:s.id,terminal:"end"}}return null}findShapeNearPoint(e,t,i){for(let s=t.length-1;s>=0;s--){let r=t[s];if(i&&r.id===i||r.type==="arrow"||r.type==="line"||r.type==="draw")continue;let o;switch(r.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":o=this.pointToRectDistance(e,{x:r.x-r.width/2,y:r.y-r.height/2,width:r.width,height:r.height});break;case "circle":o=Math.sqrt(Math.pow(e.x-r.x,2)+Math.pow(e.y-r.y,2))-r.radius;break;case "hexagon":{let h=r.width/2,l=r.height/2,c=Math.abs(e.x-r.x),u=Math.abs(e.y-r.y);o=c/h+u/l-1;break}default:continue}if(o<=30)return r}return null}pointToRectDistance(e,t){let i=Math.max(t.x,Math.min(e.x,t.x+t.width)),n=Math.max(t.y,Math.min(e.y,t.y+t.height));return Math.sqrt(Math.pow(e.x-i,2)+Math.pow(e.y-n,2))}static SNAP_THRESHOLD=32;static HANDLE_SNAP_DISTANCE=18;static ANCHOR_EPS=.001;computeSnapGuidesForArrow(e,t,i,n,s){let r=this.draggingTerminal==="start"?n.endBinding:n.startBinding,o=r?s.find(g=>g.id===r.toShapeId):null;if(!o)return [];let h=this.findShapeNearPoint(i,s,n.id);if(!h)return [];let l=this.getShapeWorldBounds(o),c=this.getShapeWorldBounds(h),u=t.x-e.x,d=t.y-e.y,p=Math.abs(u)>=Math.abs(d)?"horizontal":"vertical";return ee(l,c,p)}findBindingNearPoint(e,t,i){let n;for(let s=t.length-1;s>=0;s--){let r=t[s];if(i&&r.id===i||r.type==="arrow"||r.type==="line"||r.type==="draw")continue;let o=this.getPageBounds(r),h=a.SNAP_THRESHOLD;if(e.x<o.x-h||e.x>o.x+o.width+h||e.y<o.y-h||e.y>o.y+o.height+h)continue;let l={x:r.x,y:r.y},c=[{handle:"top",pos:{x:l.x,y:o.y}},{handle:"bottom",pos:{x:l.x,y:o.y+o.height}},{handle:"left",pos:{x:o.x,y:l.y}},{handle:"right",pos:{x:o.x+o.width,y:l.y}}];for(let d of c){let p=Math.hypot(d.pos.x-e.x,d.pos.y-e.y);p<=a.HANDLE_SNAP_DISTANCE&&(!n||p<n.distance)&&(n={toShapeId:r.id,binding:{toShapeId:r.id,handle:d.handle,isPrecise:false},distance:p});}let u=this.nearestPointOnShapeOutline(r,e);if(u&&u.distance<=a.SNAP_THRESHOLD&&(!n||u.distance<n.distance)){let d=this.computeNormalizedAnchor(o,u.point);n={toShapeId:r.id,binding:{toShapeId:r.id,isPrecise:true,normalizedAnchor:d},distance:u.distance};}}if(n)return {toShapeId:n.toShapeId,binding:n.binding}}getPageBounds(e){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":return {x:e.x-e.width/2,y:e.y-e.height/2,width:e.width,height:e.height};case "circle":return {x:e.x-e.radius,y:e.y-e.radius,width:e.radius*2,height:e.radius*2};default:return {x:e.x-50,y:e.y-50,width:100,height:100}}}computeNormalizedAnchor(e,t){let i=a.ANCHOR_EPS,n=e.width>0?(t.x-e.x)/e.width:.5,s=e.height>0?(t.y-e.y)/e.height:.5;return n=Math.max(i,Math.min(1-i,n)),s=Math.max(i,Math.min(1-i,s)),{x:n,y:s}}nearestPointOnShapeOutline(e,t){let i={x:t.x-e.x,y:t.y-e.y},n=this.nearestLocalPointOnShapeOutline(e,i);if(!n)return null;let s={x:n.point.x+e.x,y:n.point.y+e.y};return {point:s,distance:Math.hypot(s.x-t.x,s.y-t.y)}}nearestLocalPointOnShapeOutline(e,t){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":{let i=e.width,n=e.height;return this.nearestPointOnRectOutlineLocal(t,{x:-i/2,y:-n/2,width:i,height:n})}case "circle":{let i=e.radius;return this.nearestPointOnCircleOutlineLocal(t,{x:0,y:0},i)}default:return null}}nearestPointOnRectOutlineLocal(e,t){let i=[[{x:t.x,y:t.y},{x:t.x+t.width,y:t.y}],[{x:t.x+t.width,y:t.y},{x:t.x+t.width,y:t.y+t.height}],[{x:t.x+t.width,y:t.y+t.height},{x:t.x,y:t.y+t.height}],[{x:t.x,y:t.y+t.height},{x:t.x,y:t.y}]],n=null;for(let[s,r]of i){let o=this.nearestPointOnSegment(e,s,r);(!n||o.distance<n.distance)&&(n=o);}return n}nearestPointOnCircleOutlineLocal(e,t,i){let n=e.x-t.x,s=e.y-t.y,r=Math.hypot(n,s);if(r<1e-9)return {point:{x:t.x+i,y:t.y},distance:i};let o=t.x+n/r*i,h=t.y+s/r*i,l={x:o,y:h};return {point:l,distance:Math.hypot(l.x-e.x,l.y-e.y)}}nearestPointOnSegment(e,t,i){let n=i.x-t.x,s=i.y-t.y,r=n*n+s*s,o=r>0?((e.x-t.x)*n+(e.y-t.y)*s)/r:0;o=Math.max(0,Math.min(1,o));let h=t.x+o*n,l=t.y+o*s,c={x:h,y:l};return {point:c,distance:Math.hypot(c.x-e.x,c.y-e.y)}}};var we=class a extends V{type="hand";state="idle";startScreenPoint={x:0,y:0};initialCameraPos={x:0,y:0};static DRAG_THRESHOLD=4;onPointerDown(e,t){this.startScreenPoint={x:t.clientX,y:t.clientY};let i=this.context.getCamera();this.initialCameraPos={x:i.x,y:i.y},this.state="pointing";}onPointerMove(e,t){if(this.state!=="idle"){if(this.state==="pointing"){let i=t.clientX-this.startScreenPoint.x,n=t.clientY-this.startScreenPoint.y;if(Math.hypot(i,n)<a.DRAG_THRESHOLD)return;this.state="dragging",this.applyCameraDelta(i,n);return}if(this.state==="dragging"){let i=t.clientX-this.startScreenPoint.x,n=t.clientY-this.startScreenPoint.y;this.applyCameraDelta(i,n);}}}onPointerUp(e,t){if(this.state!=="idle"){if(this.state==="dragging"){let i=this.context.getCamera();this.initialCameraPos={x:i.x,y:i.y},this.startScreenPoint={x:t.clientX,y:t.clientY};}this.state="idle";}}onKeyDown(e){e.key==="Escape"&&this.state!=="idle"&&(this.context.setCameraPosition(this.initialCameraPos.x,this.initialCameraPos.y),this.state="idle");}onKeyUp(e){}getCursor(e){return this.state==="dragging"||this.state==="pointing"?"grabbing":"grab"}applyCameraDelta(e,t){this.context.setCameraPosition(this.initialCameraPos.x+e,this.initialCameraPos.y+t);}};var nn={strokeColor:"#1f2937",fillColor:"transparent",strokeWidth:2,strokeDash:null},be=class extends V{type="line";isDrawing=false;startPoint={x:0,y:0};currentShape=null;onPointerDown(e,t){this.isDrawing=true,this.startPoint=e,this.currentShape={id:`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,type:"line",x:e.x,y:e.y,points:[e],rotation:0,style:{...nn}},this.context.commitHistory(),this.context.addShape(this.currentShape);}onPointerMove(e,t){!this.isDrawing||!this.currentShape||this.context.updateShape(this.currentShape.id,{points:[this.startPoint,e]});}onPointerUp(e,t){this.isDrawing=false,this.currentShape=null,this.context.setActiveTool("select");}onKeyDown(e){}onKeyUp(e){}getCursor(){return "crosshair"}};var sn={strokeColor:"#1f2937",fillColor:"transparent",fillOpacity:1,strokeWidth:4,strokeDash:null},rn=.5,on=16,Oe="#ffeb3b",an=4200,te=class extends V{type;mode;isDrawing=false;points=[];currentShapeId=null;committed=false;fadeTimer=null;highlighterColor=Oe;constructor(e,t){super(e),this.mode=t,this.type=t==="pen"?"pen":"highlighter";}setHighlighterColor(e){this.highlighterColor=e;}getHighlighterColor(){return this.highlighterColor}buildStyle(){return this.mode==="pen"?{...sn}:{strokeColor:F(this.highlighterColor,rn),fillColor:"transparent",fillOpacity:1,strokeWidth:on,strokeDash:null}}onPointerDown(e,t){this.isDrawing=true,this.committed=false,this.currentShapeId=null,this.points=[{...e}];}buildGeometry(e){let t=1/0,i=1/0,n=-1/0,s=-1/0;for(let d of e)d.x<t&&(t=d.x),d.y<i&&(i=d.y),d.x>n&&(n=d.x),d.y>s&&(s=d.y);let r=(t+n)/2,o=(i+s)/2,h=6,l=Math.max(1,n-t)+h*2,c=Math.max(1,s-i)+h*2,u=e.map(d=>({x:d.x-r,y:d.y-o}));return {x:r,y:o,width:l,height:c,points:u}}onPointerMove(e,t){if(this.isDrawing){if(this.points.push({...e}),!this.committed&&this.points.length>=2){this.context.commitHistory();let i=`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`;this.currentShapeId=i;let n=this.buildGeometry(this.points),s=this.buildStyle(),r={id:i,type:"draw",x:n.x,y:n.y,width:n.width,height:n.height,points:n.points,rotation:0,style:s};this.context.addShape(r),this.committed=true;}else if(this.committed&&this.currentShapeId){let i=this.buildGeometry(this.points);this.context.updateShape(this.currentShapeId,{x:i.x,y:i.y,width:i.width,height:i.height,points:i.points});}}}onPointerUp(e,t){if(!this.isDrawing||(this.isDrawing=false,!this.committed||!this.currentShapeId))return;let i=this.currentShapeId;this.currentShapeId=null,this.mode==="highlighter"&&(this.fadeTimer=setTimeout(()=>{this.context.deleteShape(i);},an));}onKeyDown(e){}onKeyUp(e){}getCursor(){return "crosshair"}dispose(){this.fadeTimer&&(clearTimeout(this.fadeTimer),this.fadeTimer=null);}};var hn={strokeColor:"#1f2937",fillColor:"transparent",strokeWidth:2,strokeDash:null},ln=4,Se=class extends V{type="arrow";isDrawing=false;startPoint={x:0,y:0};currentShapeId=null;startBindingInfo=null;startBinding;onPointerDown(e,t){this.isDrawing=true,this.startPoint=e,this.startBindingInfo=null,this.currentShapeId=null;let i=this.context.getShapes(),n=this.findShapeBinding(e,i),s=e;n&&(s=this.getBindingPosition(n.toShapeId,n.handle,i),this.startBindingInfo={...n,position:s}),this.startPoint=s,this.startBinding=n?{toShapeId:n.toShapeId,handle:n.handle}:void 0;}onPointerMove(e,t){if(!this.isDrawing)return;let i=this.context.getShapes(),n=this.findShapeBinding(e,i),s=e;n&&(s=this.getBindingPosition(n.toShapeId,n.handle,i));let r=this.startPoint;if(!this.currentShapeId){if(Math.hypot(s.x-r.x,s.y-r.y)<ln)return;let l=`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,c=this.context.getArrowStyle(),u={id:l,type:"arrow",x:r.x,y:r.y,points:[r,s],rotation:0,style:{...hn},startBinding:this.startBinding,connectionType:c.connectionType,arrowHead:c.arrowHead,dashed:c.dashed};this.context.commitHistory(),this.context.addShape(u),this.currentShapeId=l;}let o=this.computeSnapGuides(r,s,n,i);this.context.setSnapGuides(o),this.context.updateShape(this.currentShapeId,{points:[r,s],endBinding:n?{toShapeId:n.toShapeId,handle:n.handle}:void 0});}onPointerUp(e,t){let i=!!this.currentShapeId;this.isDrawing=false,this.currentShapeId=null,this.startBindingInfo=null,this.startBinding=void 0,this.context.setSnapGuides([]),i&&this.context.setActiveTool("select");}onKeyDown(e){}onKeyUp(e){}getCursor(){return "crosshair"}computeSnapGuides(e,t,i,n){let s=this.startBindingInfo?n.find(d=>d.id===this.startBindingInfo.toShapeId):null,r=i?n.find(d=>d.id===i.toShapeId):null;if(!s||!r)return [];let o=this.getShapeBounds(s),h=this.getShapeBounds(r),l=t.x-e.x,c=t.y-e.y,u=Math.abs(l)>=Math.abs(c)?"horizontal":"vertical";return ee(o,h,u)}getShapeBounds(e){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":return {x:e.x-e.width/2,y:e.y-e.height/2,width:e.width,height:e.height};case "circle":return {x:e.x-e.radius,y:e.y-e.radius,width:e.radius*2,height:e.radius*2};case "text":return {x:e.x-e.width/2,y:e.y-e.height/2,width:e.width,height:e.height};default:return {x:e.x-50,y:e.y-20,width:100,height:40}}}findShapeBinding(e,t){for(let n=t.length-1;n>=0;n--){let s=t[n];if(s.type==="arrow"||s.type==="line"||s.type==="draw")continue;let r;switch(s.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "uml-class":case "uml-interface":r=this.pointToRectDistance(e,{x:s.x-s.width/2,y:s.y-s.height/2,width:s.width,height:s.height});break;case "circle":{let o=Math.sqrt(Math.pow(e.x-s.x,2)+Math.pow(e.y-s.y,2));r=Math.abs(o-s.radius);break}case "hexagon":{let o=s.width/2,h=s.height/2,l=Math.abs(e.x-s.x),c=Math.abs(e.y-s.y);r=l/o+c/h-1;break}default:continue}if(r<=40){let o=this.getClosestHandle(e,s);return {toShapeId:s.id,handle:o}}}}pointToRectDistance(e,t){let i=Math.max(t.x,Math.min(e.x,t.x+t.width)),n=Math.max(t.y,Math.min(e.y,t.y+t.height));return Math.sqrt(Math.pow(e.x-i,2)+Math.pow(e.y-n,2))}getClosestHandle(e,t){let i=e.x-t.x,n=e.y-t.y;if(t.type==="circle"||t.type==="hexagon"){let l=Math.atan2(n,i);return l>=-Math.PI/4&&l<Math.PI/4?"right":l>=Math.PI/4&&l<3*Math.PI/4?"bottom":l>=3*Math.PI/4||l<-(3*Math.PI)/4?"left":"top"}let s="width"in t?t.width:100,r="height"in t?t.height:100,o=Math.abs(i)/(s/2),h=Math.abs(n)/(r/2);return o>h?i>0?"right":"left":n>0?"bottom":"top"}getBindingPosition(e,t,i){let n=i.find(s=>s.id===e);if(!n)return {x:0,y:0};switch(n.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":return this.getRectBindingPosition(n,t);case "circle":return this.getCircleBindingPosition(n,t);case "hexagon":return this.getRectBindingPosition(n,t);default:return {x:n.x,y:n.y}}}getRectBindingPosition(e,t){switch(t){case "top":return {x:e.x,y:e.y-e.height/2};case "bottom":return {x:e.x,y:e.y+e.height/2};case "left":return {x:e.x-e.width/2,y:e.y};case "right":return {x:e.x+e.width/2,y:e.y}}}getCircleBindingPosition(e,t){switch(t){case "top":return {x:e.x,y:e.y-e.radius};case "bottom":return {x:e.x,y:e.y+e.radius};case "left":return {x:e.x-e.radius,y:e.y};case "right":return {x:e.x+e.radius,y:e.y}}}};var cn={strokeColor:"transparent",fillColor:"transparent",strokeWidth:0,strokeDash:null},Ue=class extends V{type="text";onPointerDown(e,t){let i=`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,n={id:i,type:"text",x:e.x,y:e.y,rotation:0,style:{...cn},text:"",fontSize:16,textColor:"#1f2937",width:200,height:24};this.context.commitHistory(),this.context.addShape(n),this.context.enterEditing(i);}onPointerMove(e,t){}onPointerUp(e,t){}onKeyDown(e){}onKeyUp(e){}getCursor(){return "text"}};var pe="#1f2937";function et(a){switch(a.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":case "mind-map":case "text":return {x:-(a.width/2),y:-(a.height/2),width:a.width,height:a.height};case "circle":return {x:-a.radius,y:-a.radius,width:a.radius*2,height:a.radius*2};case "line":case "arrow":{let e=a.points;if(!e||e.length===0)return {x:0,y:0,width:0,height:0};let t=1/0,i=1/0,n=-1/0,s=-1/0;for(let r of e){let o=r.x-a.x,h=r.y-a.y;t=Math.min(t,o),i=Math.min(i,h),n=Math.max(n,o),s=Math.max(s,h);}return {x:t,y:i,width:n-t,height:s-i}}case "draw":{let e=a,t=e.width,i=e.height;if(typeof t=="number"&&typeof i=="number")return {x:-t/2,y:-i/2,width:t,height:i};let n=e.points;if(!n||n.length===0)return {x:0,y:0,width:0,height:0};let s=1/0,r=1/0,o=-1/0,h=-1/0;for(let l of n)s=Math.min(s,l.x),r=Math.min(r,l.y),o=Math.max(o,l.x),h=Math.max(h,l.y);return {x:s,y:r,width:o-s,height:h-r}}default:return {x:0,y:0,width:0,height:0}}}function dn(a){let e=et(a),t=Math.cos(a.rotation),i=Math.sin(a.rotation),n=1/0,s=1/0,r=-1/0,o=-1/0,h=[[e.x,e.y],[e.x+e.width,e.y],[e.x+e.width,e.y+e.height],[e.x,e.y+e.height]];for(let[l,c]of h){let u=l*t-c*i+a.x,d=l*i+c*t+a.y;n=Math.min(n,u),s=Math.min(s,d),r=Math.max(r,u),o=Math.max(o,d);}return {x:n,y:s,width:r-n,height:o-s}}function Pe(a,e=24){if(!a||a.length===0)return {x:0,y:0,width:400,height:300};let t=1/0,i=1/0,n=-1/0,s=-1/0;for(let r of a){let o=dn(r);t=Math.min(t,o.x),i=Math.min(i,o.y),n=Math.max(n,o.x+o.width),s=Math.max(s,o.y+o.height);}return {x:t-e,y:i-e,width:n-t+e*2,height:s-i+e*2}}function Mt(a,e={}){let t=e.padding??24,i=e.scale??2,n=e.background===void 0?"#ffffff":e.background??null,s=Pe(a,t),r=Math.max(1,Math.ceil(s.width)),o=Math.max(1,Math.ceil(s.height)),h=document.createElement("canvas");h.width=Math.ceil(r*i),h.height=Math.ceil(o*i);let l=new oe(h,i);l.resize(r*i,o*i);let c=new se;return c.zoom=1,c.x=-s.x,c.y=-s.y,l.render(a,c,new Set,void 0,void 0,void 0,void 0,void 0,void 0,void 0,n),h}function un(a,e,t){return new Promise((i,n)=>{a.toBlob(s=>{s?i(s):n(new Error("canvas.toBlob failed"));},e,t);})}function pn(a,e,t){return a.toDataURL(e,t)}var Me=null;function ie(a,e){return Me||(Me=document.createElement("canvas").getContext("2d")),Me?(Me.font=e,Me.measureText(a).width):a.length*8}function ge(a){return a.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")}function tt(a,e,t,i){let n=e*1.3,s=Math.max(1,Math.floor(i/n)),r=`${e}px sans-serif`,o=a.split(`
|
|
3
|
-
`),h=[];for(let c of o){let u="";for(let d of c){let p=u+d;ie(p,r)>t&&u?(h.push(u),u=d):u=p;}u&&h.push(u);}let l=h.slice(0,s);if(h.length>s&&l.length>0){let c=l[l.length-1],u="...";for(;c.length>0&&ie(c+u,r)>t;)c=c.slice(0,-1);l[l.length-1]=c+u;}return l}function nt(a,e,t){if(ie(a,t)<=e)return a;let i="\u2026",n=0,s=a.length;for(;n<s;){let r=n+s+1>>1;ie(a.slice(0,r)+i,t)<=e?n=r:s=r-1;}return a.slice(0,n)+i}function ne(a){return a.map(([e,t])=>`${f(e)},${f(t)}`).join(" ")}function mt(a){return a.map(e=>e[0]==="Z"?"Z":e[0]==="M"||e[0]==="L"?`${e[0]} ${f(e[1])} ${f(e[2])}`:`C ${f(e[1])} ${f(e[2])} ${f(e[3])} ${f(e[4])} ${f(e[5])} ${f(e[6])}`).join(" ")}function gn(a){let e=a;if(!e.text||a.type==="text"||a.type==="line"||a.type==="arrow"||a.type==="draw")return "";let t=e.fontSize||16,i=a.type==="brace-left"||a.type==="brace-right",n=e.width??(e.radius?e.radius*2:100),s=e.height??(e.radius?e.radius*2:40),r=n*(i?1-me:1)*.85,o=s*.85,h=tt(String(e.text),t,r,o);return it(h,t,e.textColor||pe,f(Be(a)))}function f(a){return Math.round(a*100)/100}function vt(a){return a.strokeDash&&a.strokeDash.length?a.strokeDash.join(","):void 0}function yn(a,e){let t=e.x-a.x,i=e.y-a.y,n=Math.cos(a.rotation),s=Math.sin(a.rotation);return {x:t*n-i*s+a.x,y:t*s+i*n+a.y}}function xt(a,e){if(e.isPrecise&&e.normalizedAnchor){let t=et(a),i=a.x+t.x+t.width*e.normalizedAnchor.x,n=a.y+t.y+t.height*e.normalizedAnchor.y;return {x:i,y:n}}if(e.handle){let t=et(a);switch(e.handle){case "top":return {x:a.x,y:a.y+t.y};case "bottom":return {x:a.x,y:a.y+t.y+t.height};case "left":return {x:a.x+t.x,y:a.y};case "right":return {x:a.x+t.x+t.width,y:a.y}}}return {x:a.x,y:a.y}}function fn(a,e){let t=a.points.map(i=>({...i}));if(t.length===0)return t;if(a.startBinding){let i=e.get(a.startBinding.toShapeId);i&&(t[0]=xt(i,a.startBinding));}if(a.endBinding){let i=e.get(a.endBinding.toShapeId);i&&(t[t.length-1]=xt(i,a.endBinding));}return t.map(i=>yn(a,i))}function mn(a){if(a.length<2)return a;let e=a[0],t=a[a.length-1],i=t.x-e.x,n=t.y-e.y,s=8;if(Math.abs(n)<=s){let r=(e.y+t.y)/2;return a.map(o=>({x:o.x,y:r}))}if(Math.abs(i)<=s){let r=(e.x+t.x)/2;return a.map(o=>({x:r,y:o.y}))}return a}function xn(a){if(a.length<2)return a;let e=[a[0]];for(let t=1;t<a.length;t++){let i=a[t-1],n=a[t],s=(i.x+n.x)/2,r=(i.y+n.y)/2;Math.abs(n.x-i.x)>=Math.abs(n.y-i.y)?(e.push({x:s,y:i.y}),e.push({x:s,y:n.y})):(e.push({x:i.x,y:r}),e.push({x:n.x,y:r})),e.push(n);}return e}function wn(a){if(a.length<2)return "";let e=`M ${f(a[0].x)} ${f(a[0].y)}`,t=a[0];for(let i=1;i<a.length;i++){let n=a[i],s=n.x-t.x,r=n.y-t.y,o=Math.hypot(s,r),h={x:(t.x+n.x)/2,y:(t.y+n.y)/2};if(o>=1){let l=-r/o,c=s/o,u=Math.min(o*.2,30);h={x:h.x+l*u,y:h.y+c*u};}e+=` Q ${f(h.x)} ${f(h.y)} ${f(n.x)} ${f(n.y)}`,t=n;}return e}function bn(a,e){if(e==="curve")return wn(a);let t=e==="straight"?mn(a):xn(a);if(t.length<2)return "";let i=`M ${f(t[0].x)} ${f(t[0].y)}`;for(let n=1;n<t.length;n++)i+=` L ${f(t[n].x)} ${f(t[n].y)}`;return i}function Je(a,e,t,i){let n=Math.atan2(e.y-a.y,e.x-a.x),s=12,r={x:e.x-s*Math.cos(n-Math.PI/6),y:e.y-s*Math.sin(n-Math.PI/6)},o={x:e.x-s*Math.cos(n+Math.PI/6),y:e.y-s*Math.sin(n+Math.PI/6)};return `<path d="M ${f(e.x)} ${f(e.y)} L ${f(r.x)} ${f(r.y)} M ${f(e.x)} ${f(e.y)} L ${f(o.x)} ${f(o.y)}" stroke="${t}" stroke-width="${i}" stroke-linecap="round" fill="none" />`}function wt(a,e,t,i){let n=Math.atan2(e.y-a.y,e.x-a.x),s=14,r=e.x,o=e.y,h=e.x-s*Math.cos(n),l=e.y-s*Math.sin(n),c=h+s*.5*Math.cos(n-Math.PI/2),u=l+s*.5*Math.sin(n-Math.PI/2),d=h+s*.5*Math.cos(n+Math.PI/2),p=l+s*.5*Math.sin(n+Math.PI/2);return `<polygon points="${f(r)},${f(o)} ${f(c)},${f(u)} ${f(d)},${f(p)}" fill="#ffffff" stroke="${t}" stroke-width="${i}" />`}function bt(a,e,t,i,n){let s=Math.atan2(e.y-a.y,e.x-a.x),r=14,o=e.x,h=e.y,l=e.x-r*.5*Math.cos(s),c=e.y-r*.5*Math.sin(s),u=e.x-r*Math.cos(s),d=e.y-r*Math.sin(s),p=l+r*.4*Math.cos(s-Math.PI/2),g=c+r*.4*Math.sin(s-Math.PI/2),y=l+r*.4*Math.cos(s+Math.PI/2),m=c+r*.4*Math.sin(s+Math.PI/2);return `<polygon points="${f(o)},${f(h)} ${f(p)},${f(g)} ${f(u)},${f(d)} ${f(y)},${f(m)}" fill="${n?t:"#ffffff"}" stroke="${t}" stroke-width="${i}" />`}function Sn(a,e,t,i){if(e==="none"||a.length<2)return "";let n="";return (e==="start"||e==="both")&&(n+=Je(a[1],a[0],t,i)),(e==="end"||e==="both")&&(n+=Je(a[a.length-2],a[a.length-1],t,i)),e==="inheritance"&&(n+=wt(a[a.length-2],a[a.length-1],t,i)),e==="aggregation"&&(n+=bt(a[1],a[0],t,i,false)),e==="composition"&&(n+=bt(a[1],a[0],t,i,true)),e==="dependency"&&(n+=Je(a[a.length-2],a[a.length-1],t,i)),e==="realization"&&(n+=wt(a[a.length-2],a[a.length-1],t,i)),n}function it(a,e,t,i=0,n=0){if(a.length===0)return "";let s=e*1.3,o=-(a.length*s)/2+s/2,h=a.map((l,c)=>`<tspan x="${f(i)}" y="${f(n+o+c*s)}">${ge(l)}</tspan>`).join("");return `<text text-anchor="middle" dominant-baseline="central" font-family="sans-serif" font-size="${e}" fill="${t}">${h}</text>`}function _(a,e){let t=vt(a),i=e&&a.fillColor!=="transparent",n=a.fillOpacity??1,s=a.strokeOpacity??1;return [i?`fill="${a.fillColor}"`:'fill="none"',i&&n<1?`fill-opacity="${f(n)}"`:"",`stroke="${a.strokeColor}"`,s<1&&a.strokeColor!=="transparent"?`stroke-opacity="${f(s)}"`:"",`stroke-width="${a.strokeWidth}"`,'stroke-linecap="round"','stroke-linejoin="round"',t?`stroke-dasharray="${t}"`:""].filter(Boolean).join(" ")}function St(a,e){let{width:t,height:i}=a,n=t/2,s=i/2,r=`${G}px sans-serif`,o=j,h=e?0:a.attributes.length>0?a.attributes.length*A+E*2:E*2,l=t-O*2,c=[];c.push(`<rect x="${f(-n)}" y="${f(-s)}" width="${t}" height="${i}" ${_(a.style,true)} />`),c.push(`<line x1="${f(-n)}" y1="${f(-s+o)}" x2="${f(n)}" y2="${f(-s+o)}" stroke="${a.style.strokeColor}" stroke-width="${a.style.strokeWidth}" />`),e||c.push(`<line x1="${f(-n)}" y1="${f(-s+o+h)}" x2="${f(n)}" y2="${f(-s+o+h)}" stroke="${a.style.strokeColor}" stroke-width="${a.style.strokeWidth}" />`);let u=e?`<<${a.className||"Interface"}>>`:a.className;c.push(`<text text-anchor="middle" dominant-baseline="central" font-family="sans-serif" font-weight="bold" font-size="${G}" fill="${a.textColor||pe}" x="0" y="${f(-s+o/2)}">${ge(nt(u,l,r))}</text>`);let d=(g,y,m,v,x)=>{let M=`${m||G}px sans-serif`,S=nt(g,l,M),w=`<text text-anchor="start" dominant-baseline="central" font-family="sans-serif" font-size="${m||G}" fill="${v||pe}" x="${f(-n+O)}" y="${f(y)}">${ge(S)}</text>`;if(x){let C=ie(S,M);w+=`<line x1="${f(-n+O)}" y1="${f(y)}" x2="${f(-n+O+C)}" y2="${f(y)}" stroke="${v||pe}" stroke-width="1" />`;}return w};if(!e){let g=-s+o+E+A/2;a.attributes.forEach((y,m)=>{c.push(d(`${y.visibility} ${y.name}: ${y.type}`,g+m*A,y.fontSize||a.fontSize||0,y.textColor||a.textColor||"",!!y.strikethrough));});}let p=-s+o+h+E+A/2;return a.methods.forEach((g,y)=>{c.push(d(`${g.visibility} ${g.name}(${g.params}): ${g.returnType}`,p+y*A,g.fontSize||a.fontSize||0,g.textColor||a.textColor||"",!!g.strikethrough));}),c.join("")}function Mn(a){let t=B(a.root).positions,i=[],n=r=>{if(r.collapsed)return;let o=t.get(r.id);if(o)for(let h of r.children??[]){let l=t.get(h.id);if(!l)continue;let c=re(h,l.depth),u=a.x+o.x+o.width/2,d=a.y+o.y,p=a.x+l.x-l.width/2,g=a.y+l.y,y=(u+p)/2;i.push(`<path d="M ${f(u)} ${f(d)} C ${f(y)} ${f(d)} ${f(y)} ${f(g)} ${f(p)} ${f(g)}" stroke="${c}" stroke-width="2" fill="none" stroke-linecap="round" />`),n(h);}};n(a.root);let s=r=>{let o=t.get(r.id);if(!o)return;let h=re(r,o.depth),l=o.width/2,c=o.height/2,u=a.x+o.x,d=a.y+o.y;if(i.push(`<rect x="${f(u-l)}" y="${f(d-c)}" width="${o.width}" height="${o.height}" rx="6" ry="6" fill="#ffffff" stroke="${h}" stroke-width="2" />`),r.note){let M="11px sans-serif",S=r.noteColor??h,w=Math.min(ie(r.note,M)+12,130),C=16,I=ie(r.note,M)+12>130?r.note.slice(0,12)+"\u2026":r.note;i.push(`<rect x="${f(u-l)}" y="${f(d-c-C+2)}" width="${w}" height="${C}" rx="4" ry="4" fill="${S}" />`),i.push(`<text text-anchor="start" dominant-baseline="central" font-family="sans-serif" font-size="11" fill="#ffffff" x="${f(u-l+6)}" y="${f(d-c-C/2+2)}">${ge(I)}</text>`);}let p=r.icon?18:0,g=o.width-p-(r.icon?8:0)-16,y=nt(r.text||"",g,`${14}px sans-serif`),m=ie(y,`${14}px sans-serif`),v=p+(p?8:0)+m,x=u-v/2;if(r.icon&&(i.push(`<text text-anchor="start" dominant-baseline="central" font-family="sans-serif" font-size="16" fill="#1f2937" x="${f(x)}" y="${f(d)}">${ge(r.icon)}</text>`),x+=p+8),i.push(`<text text-anchor="start" dominant-baseline="central" font-family="sans-serif" font-size="${14}" fill="#1f2937" x="${f(x)}" y="${f(d)}">${ge(y)}</text>`),r.children&&r.children.length>0&&!r.collapsed)for(let M of r.children)s(M);};return s(a.root),i.join("")}function vn(a,e){let t=a.rotation*180/Math.PI,i=`<g transform="translate(${f(a.x)} ${f(a.y)}) rotate(${f(t)})">`,n=`${gn(a)}</g>`,s=a.style;switch(a.type){case "rectangle":{let r=a.width,o=a.height;return `${i}<rect x="${f(-r/2)}" y="${f(-o/2)}" width="${r}" height="${o}" ${_(s,true)} />${n}`}case "rounded-rectangle":{let r=a.width,o=a.height,h=a.borderRadius||8;return `${i}<rect x="${f(-r/2)}" y="${f(-o/2)}" width="${r}" height="${o}" rx="${h}" ry="${h}" ${_(s,true)} />${n}`}case "stadium":{let r=a.width,o=a.height,h=Math.min(r,o)/2;return `${i}<rect x="${f(-r/2)}" y="${f(-o/2)}" width="${r}" height="${o}" rx="${h}" ry="${h}" ${_(s,true)} />${n}`}case "circle":{let r=a.radius;return `${i}<circle cx="0" cy="0" r="${r}" ${_(s,true)} />${n}`}case "ellipse":{let r=a.width,o=a.height;return `${i}<ellipse cx="0" cy="0" rx="${r/2}" ry="${o/2}" ${_(s,true)} />${n}`}case "diamond":{let r=a.width,o=a.height;return `${i}<polygon points="${ne([[0,-o/2],[r/2,0],[0,o/2],[-r/2,0]])}" ${_(s,true)} />${n}`}case "triangle":{let r=a.width,o=a.height;return `${i}<polygon points="${ne([[0,-o/2],[r/2,o/2],[-r/2,o/2]])}" ${_(s,true)} />${n}`}case "hexagon":{let r=a.width,o=a.height;return `${i}<polygon points="${ne(Qe(r,o,6,-Math.PI/2))}" ${_(s,true)} />${n}`}case "pentagon":{let r=a.width,o=a.height;return `${i}<polygon points="${ne(Qe(r,o,5,-Math.PI/2))}" ${_(s,true)} />${n}`}case "octagon":{let r=a.width,o=a.height;return `${i}<polygon points="${ne(Qe(r,o,8,-Math.PI/8))}" ${_(s,true)} />${n}`}case "star":{let r=a.width,o=a.height;return `${i}<polygon points="${ne(Pn(r,o))}" ${_(s,true)} />${n}`}case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "class-catalog":case "chevron":case "brace-left":case "brace-right":{let r=a.width,o=a.height,h=_e(a.type);if(!h)return "";let l=`<path d="${mt(ue(h.outline,r,o))}" ${_(s,!h.strokeOnly)} />`,c=(h.seams??[]).map(u=>`<path d="${mt(ue(u,r,o))}" ${_({...s,fillColor:"transparent"},true)} />`).join("");return `${i}${l}${c}${n}`}case "parallelogram":{let r=a.width,o=a.height,h=r*.2;return `${i}<polygon points="${ne([[-r/2+h,-o/2],[r/2,-o/2],[r/2-h,o/2],[-r/2,o/2]])}" ${_(s,true)} />${n}`}case "trapezoid":{let r=a.width,o=a.height,h=r*.15;return `${i}<polygon points="${ne([[-r/2+h,-o/2],[r/2-h,-o/2],[r/2,o/2],[-r/2,o/2]])}" ${_(s,true)} />${n}`}case "uml-class":return `${i}${St(a,false)}${n}`;case "uml-interface":return `${i}${St(a,true)}${n}`;case "mind-map":return Mn(a);case "line":case "arrow":{let r=a,o=r.connectionType||"straight",h=fn(r,e);if(h.length<2)return "";let l=r.style.strokeColor,c=r.style.strokeWidth,d=r.type==="arrow"&&(!!r.dashed||r.arrowHead==="dependency"||r.arrowHead==="realization")?"8,5":vt(r.style),p=`<path d="${bn(h,o)}" fill="none" stroke="${l}" stroke-width="${c}" stroke-linecap="round" stroke-linejoin="round"${d?` stroke-dasharray="${d}"`:""} />`,g=Sn(h,r.arrowHead||"end",l,c);return `${p}${g}`}case "draw":{let r=a,o=r.points;if(!o||o.length<2)return "";let h=Math.cos(r.rotation||0),l=Math.sin(r.rotation||0),c=o.map(d=>({x:d.x*h-d.y*l+r.x,y:d.x*l+d.y*h+r.y}));if(c.length<2)return "";let u=`M ${f(c[0].x)} ${f(c[0].y)}`;for(let d=1;d<c.length-1;d++){let p=(c[d].x+c[d+1].x)/2,g=(c[d].y+c[d+1].y)/2;u+=` Q ${f(c[d].x)} ${f(c[d].y)} ${f(p)} ${f(g)}`;}return u+=` L ${f(c[c.length-1].x)} ${f(c[c.length-1].y)}`,`<path d="${u}" fill="none" stroke="${s.strokeColor}" stroke-width="${s.strokeWidth}" stroke-linecap="round" stroke-linejoin="round" />`}case "text":{let r=a,o=r.fontSize||16,h=r.textColor||pe,l=tt(r.text||"",o,r.width,r.height),c="";if(r.highlightColor&&r.highlightColor!=="transparent"){let u=o*.15,d=typeof r.highlightOpacity=="number"?r.highlightOpacity:1,p=d<1?` fill-opacity="${f(d)}"`:"";c+=`<rect x="${f(-r.width/2-u)}" y="${f(-(l.length*o*1.3)/2-u)}" width="${r.width+u*2}" height="${l.length*o*1.3+u*2}" rx="4" fill="${r.highlightColor}"${p} />`;}return c+=it(l,o,h),`${i}${c}${n}`}default:{let r=a,o=r.width||100,h=r.height||40,l="";if(r.text){let c=r.fontSize||16,u=tt(r.text,c,o*.85,h*.85);l+=it(u,c,r.textColor||pe);}return `${i}<rect x="${f(-o/2)}" y="${f(-h/2)}" width="${o}" height="${h}" ${_(s,true)} />${l}${n}`}}}function Qe(a,e,t,i){let n=a/2,s=e/2,r=[];for(let o=0;o<t;o++){let h=o*2*Math.PI/t+i;r.push([n*Math.cos(h),s*Math.sin(h)]);}return r}function Pn(a,e){let t=a/2,i=e/2,n=5,s=.4,r=[];for(let o=0;o<n*2;o++){let h=o*Math.PI/n-Math.PI/2,l=o%2===0?1:s;r.push([t*l*Math.cos(h),i*l*Math.sin(h)]);}return r}function Pt(a,e={}){let t=e.padding??24,i=e.background===void 0?"#ffffff":e.background??null,n=Pe(a,t),s=Math.max(1,Math.ceil(n.width)),r=Math.max(1,Math.ceil(n.height)),o=new Map(a.map(c=>[c.id,c])),h=a.map(c=>vn(c,o)).filter(Boolean).join(`
|
|
4
|
-
`),l=i?`<rect x="${
|
|
5
|
-
<svg xmlns="http://www.w3.org/2000/svg" width="${s}" height="${r}" viewBox="${
|
|
1
|
+
'use strict';var q={identity(){return {a:1,b:0,c:0,d:1,e:0,f:0}},translation(a,e){return {a:1,b:0,c:0,d:1,e:a,f:e}},rotation(a){let e=Math.cos(a),t=Math.sin(a);return {a:e,b:t,c:-t,d:e,e:0,f:0}},scale(a,e){return {a,b:0,c:0,d:e,e:0,f:0}},multiply(a,e){return {a:a.a*e.a+a.b*e.c,b:a.a*e.b+a.b*e.d,c:a.c*e.a+a.d*e.c,d:a.c*e.b+a.d*e.d,e:a.e*e.a+a.f*e.c+e.e,f:a.e*e.b+a.f*e.d+e.f}},apply(a,e){return {x:a.a*e.x+a.c*e.y+a.e,y:a.b*e.x+a.d*e.y+a.f}},invert(a){let e=a.a*a.d-a.b*a.c;if(Math.abs(e)<1e-10)return null;let t=1/e;return {a:a.d*t,b:-a.b*t,c:-a.c*t,d:a.a*t,e:(a.c*a.f-a.d*a.e)*t,f:(a.b*a.e-a.a*a.f)*t}},createCameraMatrix(a){let e=q.scale(a.zoom,a.zoom),t=q.translation(a.x,a.y);return q.multiply(t,e)}};var se=class{x=0;y=0;zoom=1;minZoom=.1;maxZoom=10;getMatrix(){return q.createCameraMatrix({x:this.x,y:this.y,zoom:this.zoom})}getInverseMatrix(){return q.invert(this.getMatrix())}screenToPage(e){let t=this.getInverseMatrix();return t?q.apply(t,e):e}pageToScreen(e){return q.apply(this.getMatrix(),e)}translate(e,t){this.x+=e,this.y+=t;}zoomAt(e,t){let i=this.screenToPage(e);this.zoom=Math.max(this.minZoom,Math.min(this.maxZoom,this.zoom*t));let n=this.pageToScreen(i);this.x+=e.x-n.x,this.y+=e.y-n.y;}reset(){this.x=0,this.y=0,this.zoom=1;}getZoom(){return this.zoom}fitBounds(e,t=50){let i=window.innerWidth,n=window.innerHeight,s=(i-t*2)/e.width,r=(n-t*2)/e.height,o=Math.min(s,r,this.maxZoom);this.zoom=Math.max(this.minZoom,o),this.x=(i-e.width*this.zoom)/2-e.x*this.zoom,this.y=(n-e.height*this.zoom)/2-e.y*this.zoom;}};var lt={primary:"#3b82f6",selection:{outline:"#3b82f6",handleStroke:"#3b82f6",handleFill:"#ffffff",outlineFill:"rgba(59, 130, 246, 0.1)",shadow:"#3b82f6"},grid:"#e5e7eb",text:"#1f2937",editBorder:"#3b82f6",brushFill:"rgba(59, 130, 246, 0.08)",brushStroke:"rgba(59, 130, 246, 0.8)",guide:"rgba(59, 130, 246, 0.6)",lineHighlight:"rgba(59, 130, 246, 0.4)",genDot:"#3b82f6",genLine:"rgba(59, 130, 246, 0.55)"},L={...lt,selection:{...lt.selection}},ze=new Set;function It(a){return ze.add(a),()=>ze.delete(a)}function Ie(a){a.selection&&Object.assign(L.selection,a.selection);for(let e of Object.keys(a)){if(e==="selection")continue;let t=a[e];t!==void 0&&(L[e]=t);}ze.forEach(e=>e());}function Lt(){return L}function F(a,e){let t=ct(a);if(!t)return a;let i=parseInt(t,16),n=i>>16&255,s=i>>8&255,r=i&255;return `rgba(${n}, ${s}, ${r}, ${e})`}function kt(a,e){let t=ct(a);if(!t)return a;let i=parseInt(t,16),n=e<0?0:255,s=Math.abs(e),r=o=>Math.round((n-o)*s+o).toString(16).padStart(2,"0");return `#${r(i>>16&255)}${r(i>>8&255)}${r(i&255)}`}function ct(a){let e=a.replace("#","").trim();return /^[0-9a-fA-F]{3}$|^[0-9a-fA-F]{6}$/.test(e)?e.length===3?e.split("").map(t=>t+t).join(""):e:null}var N=class{context;constructor(e){this.context=e;}getCursor(e){return null}dispose(){}};var dt={"zh-CN":{"save.success":"\u5DF2\u4FDD\u5B58","save.fail":"\u4FDD\u5B58\u5931\u8D25","tool.select":"\u9009\u62E9\u5DE5\u5177","tool.hand":"\u62D6\u62FD\u89C6\u53E3","tool.rectangle":"\u77E9\u5F62","tool.rounded-rectangle":"\u5706\u89D2\u77E9\u5F62","tool.circle":"\u5706\u5F62","tool.ellipse":"\u692D\u5706","tool.diamond":"\u83F1\u5F62","tool.triangle":"\u4E09\u89D2\u5F62","tool.hexagon":"\u516D\u8FB9\u5F62","tool.parallelogram":"\u5E73\u884C\u56DB\u8FB9\u5F62","tool.trapezoid":"\u68AF\u5F62","tool.pentagon":"\u4E94\u8FB9\u5F62","tool.octagon":"\u516B\u8FB9\u5F62","tool.star":"\u661F\u5F62","tool.cylinder":"\u5706\u67F1\u4F53","tool.cloud":"\u4E91\u5F62","tool.heart":"\u5FC3\u5F62","tool.block-arrow-right":"\u53F3\u7BAD\u5934","tool.block-arrow-left":"\u5DE6\u7BAD\u5934","tool.block-arrow-up":"\u4E0A\u7BAD\u5934","tool.block-arrow-down":"\u4E0B\u7BAD\u5934","tool.block-arrow-double":"\u53CC\u5411\u7BAD\u5934","tool.right-triangle":"\u76F4\u89D2\u4E09\u89D2\u5F62","tool.cross":"\u5341\u5B57","tool.speech-rect":"\u65B9\u5F62\u5BF9\u8BDD\u6846","tool.speech-round":"\u5706\u5F62\u5BF9\u8BDD\u6846","tool.cube":"\u7ACB\u65B9\u4F53","tool.sector":"\u6247\u5F62","tool.delay":"D \u5F62","tool.predefined-process":"\u9884\u5B9A\u4E49\u6D41\u7A0B","tool.document":"\u6587\u6863","tool.stadium":"\u5168\u5706\u89D2\u77E9\u5F62","tool.class-catalog":"\u7C7B\u76EE","class.plain":"\u7C7B","flow.process":"\u5904\u7406","flow.step":"\u6D41\u7A0B","flow.startEnd":"\u5F00\u59CB/\u7ED3\u675F","flow.decision":"\u5224\u5B9A","flow.data":"\u6570\u636E","flow.manual":"\u624B\u52A8\u64CD\u4F5C","flow.preparation":"\u51C6\u5907","flow.database":"\u6570\u636E\u5E93","flow.delay":"\u5EF6\u65F6","shapeGroup.basic":"\u57FA\u7840","shapeGroup.flowchart":"\u6D41\u7A0B\u56FE","shapeGroup.other":"\u5176\u4ED6","shapeGroup.uml":"\u7C7B\u56FE","shapeMenu.hint":"\u60AC\u505C\u67E5\u770B\u540D\u79F0","tool.chevron":"\u5C16\u89D2","tool.brace-left":"\u5DE6\u62EC\u53F7","tool.brace-right":"\u53F3\u62EC\u53F7","tool.uml-class":"UML \u7C7B\u56FE","tool.uml-interface":"UML \u63A5\u53E3","tool.line":"\u76F4\u7EBF","tool.arrow":"\u7EBF\u6761","tool.text":"\u6587\u672C","tool.draw":"\u753B\u7B14","tool.pen":"\u753B\u7B14","tool.highlighter":"\u8367\u5149\u7B14","tool.eraser":"\u6A61\u76AE\u64E6","status.none":"\u672A\u9009\u62E9","status.selected":"\u5DF2\u9009\u62E9 {n} \u4E2A\u56FE\u5F62","toolbar.select":"\u9009\u62E9 (V)","toolbar.hand":"\u62D6\u62FD\u89C6\u53E3 (H)","toolbar.shapes":"\u5F62\u72B6","toolbar.grid":"\u7F51\u683C","toolbar.undo":"\u64A4\u9500 (Ctrl+Z)","toolbar.redo":"\u91CD\u505A (Ctrl+Shift+Z)","toolbar.arrow":"\u7BAD\u5934","toolbar.text":"\u6587\u672C","toolbar.pen":"\u753B\u7B14","toolbar.highlighter":"\u8367\u5149\u7B14","toolbar.eraser":"\u6A61\u76AE\u64E6","toolbar.uml-class":"UML \u7C7B\u56FE","toolbar.uml-interface":"UML \u63A5\u53E3","zoom.in":"\u653E\u5927","zoom.out":"\u7F29\u5C0F","zoom.reset":"\u91CD\u7F6E","style.stroke":"\u8FB9\u6846","style.line":"\u7EBF\u6761","style.fill":"\u586B\u5145","style.text":"\u6587\u5B57","style.highlight":"\u9AD8\u4EAE","style.prefix":"\u524D\u7F00\u7B26\u53F7","style.prefix.soon":"\u524D\u7F00\u7B26\u53F7\u5F00\u53D1\u4E2D\uFF0C\u656C\u8BF7\u671F\u5F85","style.none":"\u65E0","style.addColor":"\u6DFB\u52A0\u989C\u8272","style.connection":"\u8FDE\u7EBF","style.arrow":"\u7BAD\u5934","style.dashed":"\u865A\u7EBF","style.solid":"\u5B9E\u7EBF","style.opacity":"\u4E0D\u900F\u660E\u5EA6","style.connection.straight":"\u76F4\u7EBF","style.connection.polyline":"\u6298\u7EBF","style.connection.orthogonal":"\u76F4\u89D2\u6298\u7EBF","style.connection.curve":"\u66F2\u7EBF","style.arrowhead.none":"\u65E0\u7BAD\u5934","style.arrowhead.end":"\u7EC8\u70B9\u7BAD\u5934","style.arrowhead.start":"\u8D77\u70B9\u7BAD\u5934","style.arrowhead.both":"\u53CC\u7BAD\u5934","style.arrowhead.none-dashed":"\u65E0\u7BAD\u5934\u865A\u7EBF","style.arrowhead.both-dashed":"\u53CC\u7BAD\u5934\u865A\u7EBF","style.uml":"UML","style.arrowhead.inheritance":"\u7EE7\u627F","style.arrowhead.composition":"\u7EC4\u5408","style.arrowhead.aggregation":"\u805A\u5408","style.arrowhead.dependency":"\u4F9D\u8D56","style.arrowhead.realization":"\u5B9E\u73B0","menu.title":"\u9009\u62E9\u65B0\u56FE\u5F62","menu.cancel":"\u70B9\u51FB\u7A7A\u767D\u5904\u53D6\u6D88","shape.rectangle":"\u77E9\u5F62","shape.rounded-rectangle":"\u5706\u89D2","shape.circle":"\u5706\u5F62","shape.ellipse":"\u692D\u5706","shape.diamond":"\u83F1\u5F62","shape.triangle":"\u4E09\u89D2","shape.hexagon":"\u516D\u8FB9\u5F62","shape.parallelogram":"\u5E73\u884C","shape.trapezoid":"\u68AF\u5F62","shape.pentagon":"\u4E94\u8FB9\u5F62","shape.octagon":"\u516B\u8FB9\u5F62","shape.star":"\u661F\u5F62","shape.uml-class":"\u7C7B\u56FE","shape.uml-interface":"\u63A5\u53E3","uml.drag":"\u62D6\u62FD\u6392\u5E8F","uml.delete":"\u5220\u9664\u8BE5\u884C","uml.textColor":"\u6587\u5B57\u989C\u8272","uml.defaultColor":"\u9ED8\u8BA4\u989C\u8272","uml.fontSize":"\u5B57\u53F7","uml.strikethrough":"\u5220\u9664\u7EBF","uml.attribute":"\u5C5E\u6027","uml.method":"\u65B9\u6CD5","minimap.title":"\u5C0F\u5730\u56FE","minimap.expand":"\u5C55\u5F00\u5C0F\u5730\u56FE","minimap.collapse":"\u6536\u8D77\u5C0F\u5730\u56FE","tool.mind-map":"\u601D\u7EF4\u5BFC\u56FE","toolbar.mind-map":"\u601D\u7EF4\u5BFC\u56FE","toolbar.export":"\u5BFC\u51FA","toolbar.export.png":"PNG \u56FE\u7247","toolbar.export.svg":"SVG \u77E2\u91CF\u56FE","toolbar.export.pdf":"PDF \u6587\u6863","shape.mind-map":"\u601D\u7EF4\u5BFC\u56FE","mindmap.defaultRoot":"\u4E2D\u5FC3\u4E3B\u9898","mindmap.defaultBranch":"\u5206\u652F\u4E3B\u9898","mindmap.nodeText":"\u8282\u70B9\u6587\u672C","mindmap.addChild":"\u5B50\u8282\u70B9","mindmap.addSibling":"\u5144\u5F1F\u8282\u70B9","mindmap.deleteNode":"\u5220\u9664\u8282\u70B9","mindmap.color":"\u989C\u8272","mindmap.levelColor":"\u9ED8\u8BA4\u5C42\u7EA7\u8272","mindmap.icon":"\u56FE\u6807","mindmap.noIcon":"\u65E0\u56FE\u6807","mindmap.note":"\u5C5E\u6027\u8BF4\u660E","mindmap.notePlaceholder":"\u8BF4\u660E\u5185\u5BB9\uFF08\u53EF\u9009\uFF09","mindmap.noteColor":"\u8BF4\u660E\u989C\u8272","mindmap.noteColorDefault":"\u540C\u8282\u70B9\u8272","mindmap.collapse":"\u6298\u53E0","mindmap.expand":"\u5C55\u5F00","mindmap.close":"\u5173\u95ED\u7F16\u8F91","mindmap.more":"\u66F4\u591A\u8BBE\u7F6E"},"en-US":{"save.success":"Saved","save.fail":"Save failed","tool.select":"Select tool","tool.hand":"Pan view","tool.rectangle":"Rectangle","tool.rounded-rectangle":"Rounded rectangle","tool.circle":"Circle","tool.ellipse":"Ellipse","tool.diamond":"Diamond","tool.triangle":"Triangle","tool.hexagon":"Hexagon","tool.parallelogram":"Parallelogram","tool.trapezoid":"Trapezoid","tool.pentagon":"Pentagon","tool.octagon":"Octagon","tool.star":"Star","tool.cylinder":"Cylinder","tool.cloud":"Cloud","tool.heart":"Heart","tool.block-arrow-right":"Arrow right","tool.block-arrow-left":"Arrow left","tool.block-arrow-up":"Arrow up","tool.block-arrow-down":"Arrow down","tool.block-arrow-double":"Double arrow","tool.right-triangle":"Right triangle","tool.cross":"Cross","tool.speech-rect":"Speech bubble","tool.speech-round":"Round bubble","tool.cube":"Cube","tool.sector":"Sector","tool.delay":"Delay","tool.predefined-process":"Predefined process","tool.document":"Document","tool.stadium":"Stadium","tool.class-catalog":"Category","class.plain":"Class","flow.process":"Process","flow.step":"Step","flow.startEnd":"Start / End","flow.decision":"Decision","flow.data":"Data","flow.manual":"Manual operation","flow.preparation":"Preparation","flow.database":"Database","flow.delay":"Delay","shapeGroup.basic":"Basic","shapeGroup.flowchart":"Flowchart","shapeGroup.other":"Other","shapeGroup.uml":"Class diagram","shapeMenu.hint":"Hover to see name","tool.chevron":"Chevron","tool.brace-left":"Left brace","tool.brace-right":"Right brace","tool.uml-class":"UML class","tool.uml-interface":"UML interface","tool.line":"Line","tool.arrow":"Line","tool.text":"Text","tool.draw":"Pen","tool.pen":"Pen","tool.highlighter":"Highlighter","tool.eraser":"Eraser","status.none":"Nothing selected","status.selected":"{n} shapes selected","toolbar.select":"Select (V)","toolbar.hand":"Pan view (H)","toolbar.shapes":"Shapes","toolbar.grid":"Grid","toolbar.undo":"Undo (Ctrl+Z)","toolbar.redo":"Redo (Ctrl+Shift+Z)","toolbar.arrow":"Arrow","toolbar.text":"Text","toolbar.pen":"Pen","toolbar.highlighter":"Highlighter","toolbar.eraser":"Eraser","toolbar.uml-class":"UML class","toolbar.uml-interface":"UML interface","zoom.in":"Zoom in","zoom.out":"Zoom out","zoom.reset":"Reset","style.stroke":"Border","style.line":"Line","style.fill":"Fill","style.text":"Text","style.highlight":"Highlight","style.prefix":"Prefix","style.prefix.soon":"Prefix symbols are coming soon","style.none":"None","style.addColor":"Add color","style.connection":"Connection","style.arrow":"Arrow head","style.dashed":"Dashed","style.solid":"Solid","style.opacity":"Opacity","style.connection.straight":"Straight","style.connection.polyline":"Polyline","style.connection.orthogonal":"Orthogonal","style.connection.curve":"Curve","style.arrowhead.none":"None","style.arrowhead.end":"End","style.arrowhead.start":"Start","style.arrowhead.both":"Both","style.arrowhead.none-dashed":"Dashed","style.arrowhead.both-dashed":"Dashed both","style.uml":"UML","style.arrowhead.inheritance":"Inheritance","style.arrowhead.composition":"Composition","style.arrowhead.aggregation":"Aggregation","style.arrowhead.dependency":"Dependency","style.arrowhead.realization":"Realization","menu.title":"Create shape","menu.cancel":"Click empty space to cancel","shape.rectangle":"Rectangle","shape.rounded-rectangle":"Rounded","shape.circle":"Circle","shape.ellipse":"Ellipse","shape.diamond":"Diamond","shape.triangle":"Triangle","shape.hexagon":"Hexagon","shape.parallelogram":"Parallelogram","shape.trapezoid":"Trapezoid","shape.pentagon":"Pentagon","shape.octagon":"Octagon","shape.star":"Star","shape.uml-class":"Class","shape.uml-interface":"Interface","uml.drag":"Drag to reorder","uml.delete":"Delete row","uml.textColor":"Text color","uml.defaultColor":"Default color","uml.fontSize":"Font size","uml.strikethrough":"Strikethrough","uml.attribute":"Attributes","uml.method":"Methods","minimap.title":"Minimap","minimap.expand":"Expand minimap","minimap.collapse":"Collapse minimap","tool.mind-map":"Mind map","toolbar.mind-map":"Mind map","toolbar.export":"Export","toolbar.export.png":"PNG image","toolbar.export.svg":"SVG vector","toolbar.export.pdf":"PDF document","shape.mind-map":"Mind map","mindmap.defaultRoot":"Central topic","mindmap.defaultBranch":"Branch topic","mindmap.nodeText":"Node text","mindmap.addChild":"Child","mindmap.addSibling":"Sibling","mindmap.deleteNode":"Delete node","mindmap.color":"Color","mindmap.levelColor":"Default level color","mindmap.icon":"Icon","mindmap.noIcon":"No icon","mindmap.note":"Note","mindmap.notePlaceholder":"Note content (optional)","mindmap.noteColor":"Note color","mindmap.noteColorDefault":"Same as node","mindmap.collapse":"Collapse","mindmap.expand":"Expand","mindmap.close":"Close editor","mindmap.more":"More settings"}},Le="zh-CN",Ye=new Set;function ke(a){Le!==a&&(Le=a,Ye.forEach(e=>e()));}function Dt(){return Le}function Et(a){return Ye.add(a),()=>Ye.delete(a)}function Z(a,e){let i=dt[Le][a]??dt["zh-CN"][a];if(e)for(let[n,s]of Object.entries(e))i=i.replace(new RegExp(`\\{${n}\\}`,"g"),String(s));return i}var Xe=["#ef4444","#f59e0b","#10b981","#3b82f6","#8b5cf6","#ec4899"],J=14,At=40,$t=80,Rt=240;function re(a,e){return a.color??Xe[e%Xe.length]}function ut(a){let e=a.text||"",t=0;for(let n of e)t+=/[\u2e80-\u9fff\uf900-\ufaff\uff00-\uffef\u3000-\u303f]/.test(n)?1:.6;let i=Math.max(80,Math.min(240,Math.ceil(t*14)+24));return a.icon&&(i+=26),{width:i,height:40}}function De(a){let e={...a};return a.children&&(e.children=a.children.map(t=>De(t))),e}function Ee(a,e,t){if(a.id===e)return {...a,...t};let i=a.children??[],n=i.map(s=>Ee(s,e,t));return n.some((s,r)=>s!==i[r])?{...a,children:n}:a}function We(a,e,t){if(a.id===e)return {...a,children:[...a.children??[],t],collapsed:false};let i=a.children??[],n=i.map(s=>We(s,e,t));return n.some((s,r)=>s!==i[r])?{...a,children:n}:a}function Ke(a,e,t){let i=a.children??[];for(let n=0;n<i.length;n++){if(i[n].id===e){let r=[...i];return r.splice(n+1,0,t),{...a,children:r}}let s=Ke(i[n],e,t);if(s!==i[n])return {...a,children:i.map(r=>r===i[n]?s:r)}}return a}function Fe(a,e){if(a.id===e)return null;let t=a.children??[],i=t.map(n=>Fe(n,e)).filter(n=>n!==null);return i.length!==t.length||i.some((n,s)=>n!==t[s])?{...a,children:i}:a}function Q(a,e){if(a.id===e)return a;for(let t of a.children??[]){let i=Q(t,e);if(i)return i}return null}function gt(a,e){if(a.id===e)return 0;for(let t of a.children??[]){let i=gt(t,e);if(i!==-1)return i+1}return -1}function ye(a,e){for(let t of a.children??[]){if(t.id===e)return a;let i=ye(t,e);if(i)return i}return null}function pt(a){let e=1;for(let t of a.children??[])e+=pt(t);return e}function yt(a,e){e.set(a.id,ut(a));for(let t of a.children??[])yt(t,e);}function B(a){let e=new Map;yt(a,e);let t=new Map,i=d=>{let g=d.collapsed?[]:d.children??[];if(g.length===0)t.set(d.id,e.get(d.id).height);else {let p=0;for(let y of g)p+=i(y);p+=16*(g.length-1),t.set(d.id,Math.max(e.get(d.id).height,p));}return t.get(d.id)};i(a);let n=new Map,s=(d,g,p,y)=>{let f=t.get(d.id),w=e.get(d.id);if(n.set(d.id,{x:p,y:y+f/2,depth:g,width:w.width,height:w.height}),d.collapsed)return;let x=y;for(let v of d.children??[]){let M=e.get(v.id);s(v,g+1,p+w.width/2+48+M.width/2,x),x+=t.get(v.id)+16;}};s(a,0,0,-t.get(a.id)/2);let r=1/0,o=-1/0,h=1/0,l=-1/0;n.forEach(d=>{r=Math.min(r,d.x-d.width/2),o=Math.max(o,d.x+d.width/2),h=Math.min(h,d.y-d.height/2),l=Math.max(l,d.y+d.height/2);});let c=(r+o)/2,u=(h+l)/2;return n.forEach(d=>{d.x-=c,d.y-=u;}),{positions:n,width:o-r,height:l-h}}var Ht={strokeColor:"#1f2937",fillColor:"#f8fafc",fillOpacity:.9,strokeWidth:2,strokeDash:null},Vt={rectangle:{width:120,height:60},"rounded-rectangle":{width:120,height:60,borderRadius:8},circle:{width:80,height:80},ellipse:{width:100,height:70},diamond:{width:100,height:80},triangle:{width:100,height:90},hexagon:{width:100,height:90},parallelogram:{width:120,height:60},trapezoid:{width:120,height:70},pentagon:{width:100,height:90},octagon:{width:100,height:90},star:{width:100,height:100},cylinder:{width:100,height:110},cloud:{width:140,height:90},heart:{width:100,height:95},"block-arrow-right":{width:140,height:80},"block-arrow-left":{width:140,height:80},"block-arrow-up":{width:80,height:140},"block-arrow-down":{width:80,height:140},"block-arrow-double":{width:150,height:80},"right-triangle":{width:100,height:90},cross:{width:90,height:90},"speech-rect":{width:130,height:92},"speech-round":{width:130,height:92},cube:{width:110,height:110},sector:{width:100,height:100},delay:{width:120,height:80},"predefined-process":{width:130,height:80},document:{width:120,height:90},stadium:{width:130,height:60},"class-catalog":{width:160,height:90},chevron:{width:110,height:80},"brace-left":{width:220,height:120},"brace-right":{width:220,height:120}},G=14,$=22,j=36,O=14,E=8,fe=160,Nt=["rectangle","rounded-rectangle","circle","ellipse","diamond","triangle","hexagon","parallelogram","trapezoid","pentagon","octagon","star","cylinder","cloud","heart","block-arrow-right","block-arrow-left","block-arrow-up","block-arrow-down","block-arrow-double","right-triangle","cross","speech-rect","speech-round","cube","sector","delay","predefined-process","document","stadium","class-catalog","chevron","brace-left","brace-right","uml-class","uml-interface","mind-map"];function Ae(a){return Nt.includes(a)}function Ze(a,e,t){let i=`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,n=Vt[a]||{width:120,height:60},s=n.width,r=n.height,o=n.radius??s/2,h=n.borderRadius??8,l=t??{...Ht};switch(a){case "rounded-rectangle":return {id:i,type:"rounded-rectangle",x:e.x,y:e.y,width:s,height:r,borderRadius:h,rotation:0,style:l};case "circle":return {id:i,type:"circle",x:e.x,y:e.y,radius:o,rotation:0,style:l};case "ellipse":return {id:i,type:"ellipse",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "diamond":return {id:i,type:"diamond",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "triangle":return {id:i,type:"triangle",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "hexagon":return {id:i,type:"hexagon",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "parallelogram":return {id:i,type:"parallelogram",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "trapezoid":return {id:i,type:"trapezoid",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "pentagon":return {id:i,type:"pentagon",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "octagon":return {id:i,type:"octagon",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "star":return {id:i,type:"star",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":return {id:i,type:a,x:e.x,y:e.y,width:s,height:r,rotation:0,style:l};case "uml-class":{let c="ClassName",u=[{id:`attr_${Date.now()}_1`,visibility:"+",name:"field",type:"string"}],d=[{id:`meth_${Date.now()}_1`,visibility:"+",name:"method",params:"param",returnType:"void"}],{width:g,height:p}=$e(c,u,d);return {id:i,type:"uml-class",x:e.x,y:e.y,width:g,height:p,className:c,attributes:u,methods:d,rotation:0,style:{...l,fillColor:"#ffffff"}}}case "uml-interface":{let c="Interface",u=[{id:`meth_${Date.now()}_1`,visibility:"+",name:"method",params:"type",returnType:"type"}],d=G*.6,g=u.map(x=>`${x.visibility} ${x.name}(${x.params}): ${x.returnType}`),p=Math.max(c.length,...g.map(x=>x.length),0),y=Math.max(fe,p*d+O*2),f=u.length>0?u.length*$+E*2:E*2,w=j+f;return {id:i,type:"uml-interface",x:e.x,y:e.y,width:y,height:w,className:c,methods:u,rotation:0,style:{...l,fillColor:"#ffffff"}}}case "mind-map":{let c=p=>`mm_${p}_${Date.now()}_${Math.random().toString(36).substr(2,6)}`,u={id:c("root"),text:Z("mindmap.defaultRoot"),children:[{id:c("b1"),text:Z("mindmap.defaultBranch")},{id:c("b2"),text:Z("mindmap.defaultBranch")},{id:c("b3"),text:Z("mindmap.defaultBranch")}]},{width:d,height:g}=B(u);return {id:i,type:"mind-map",x:e.x,y:e.y,width:d,height:g,root:u,rotation:0,style:{...l,fillColor:"#ffffff"}}}default:return {id:i,type:"rectangle",x:e.x,y:e.y,width:s,height:r,rotation:0,style:l}}}function $e(a,e,t){let i=G*.6,n=[a];for(let u of e)n.push(`${u.visibility} ${u.name}: ${u.type}`);for(let u of t)n.push(`${u.visibility} ${u.name}(${u.params}): ${u.returnType}`);let r=Math.max(...n.map(u=>u.length),0)*i+O*2,o=Math.max(fe,r),h=e.length>0?e.length*$+E*2:E*2,l=t.length>0?t.length*$+E*2:E*2,c=j+h+l;return {width:o,height:c}}var T=class extends N{type;constructor(e,t){super(e),this.type=t;}onPointerDown(e,t){let i=Ze(this.type,e);this.context.commitHistory(),this.context.addShape(i),this.context.selectShape(i.id,false);}onPointerMove(e,t){}onPointerUp(e,t){this.context.setActiveTool("select");}onKeyDown(e){}onKeyUp(e){}getCursor(e){return this.context.getMindMapCursorHitAtPoint(e)?"pointer":"crosshair"}};var je=14,Re=class extends N{type="eraser";erasing=false;lastPoint=null;erasedIds=new Set;onPointerDown(e){this.erasing=true,this.lastPoint=e,this.erasedIds.clear(),this.context.commitHistory(),this.context.setEraserPoint(e),this.eraseAt(e);}onPointerMove(e){if(!this.erasing||!this.lastPoint)return;let t=this.lastPoint,i=Math.hypot(e.x-t.x,e.y-t.y),n=Math.max(1,Math.ceil(i/Math.max(this.brushRadius()*.5,1)));for(let s=1;s<=n;s++){let r=s/n;this.eraseAt({x:t.x+(e.x-t.x)*r,y:t.y+(e.y-t.y)*r});}this.lastPoint=e,this.context.setEraserPoint(e);}onPointerUp(){this.erasing=false,this.lastPoint=null,this.erasedIds.clear(),this.context.setEraserPoint(null);}onKeyDown(){}onKeyUp(){}getCursor(){return this.erasing?"grabbing":"crosshair"}dispose(){this.erasing=false,this.lastPoint=null,this.erasedIds.clear(),this.context.setEraserPoint(null);}brushRadius(){return je/this.context.getCamera().getZoom()}eraseAt(e){let t=this.context.getShapesInBrushRadius(e,this.brushRadius());for(let i of t)this.erasedIds.has(i.id)||(this.erasedIds.add(i.id),this.context.deleteShape(i.id));}};function ue(a,e,t){let i=s=>(s-.5)*e,n=s=>(s-.5)*t;return a.map(s=>s[0]==="Z"?s:s[0]==="M"||s[0]==="L"?[s[0],i(s[1]),n(s[2])]:["C",i(s[1]),n(s[2]),i(s[3]),n(s[4]),i(s[5]),n(s[6])])}function de(a,e,t,i,n,s,r){let o=[],h=s-n,l=Math.max(1,Math.ceil(Math.abs(h)/90)),c=h/l,u=4/3*Math.tan(c*Math.PI/180/4),d=y=>{let f=y*Math.PI/180;return [a+t*Math.cos(f),e+i*Math.sin(f)]},[g,p]=d(n);o.push(r?["M",g,p]:["L",g,p]);for(let y=0;y<l;y++){let f=n+c*y,w=f+c,x=f*Math.PI/180,v=w*Math.PI/180,[M,b]=d(f),[C,I]=d(w);o.push(["C",M-u*t*Math.sin(x),b+u*i*Math.cos(x),C+u*t*Math.sin(v),I-u*i*Math.cos(v),C,I]);}return o}function Bt(a){let e=1/0,t=1/0,i=-1/0,n=-1/0,s=(g,p)=>{g<e&&(e=g),p<t&&(t=p),g>i&&(i=g),p>n&&(n=p);},r=0,o=0,h=0,l=0;for(let g of a){if(g[0]==="Z"){s(h,l);continue}if(g[0]==="M"||g[0]==="L"){r=g[1],o=g[2],g[0]==="M"&&(h=r,l=o),s(r,o);continue}let p=g[1],y=g[2],f=g[3],w=g[4],x=g[5],v=g[6];for(let M=0;M<=12;M++){let b=M/12,C=1-b,I=C*C*C,H=3*C*C*b,U=3*C*b*b,W=b*b*b;s(I*r+H*p+U*f+W*x,I*o+H*y+U*w+W*v);}r=x,o=v;}let c=i-e||1,u=n-t||1,d=(g,p)=>[(g-e)/c,(p-t)/u];return a.map(g=>{if(g[0]==="Z")return g;if(g[0]==="M"||g[0]==="L"){let[M,b]=d(g[1],g[2]);return [g[0],M,b]}let[p,y]=d(g[1],g[2]),[f,w]=d(g[3],g[4]),[x,v]=d(g[5],g[6]);return ["C",p,y,f,w,x,v]})}function _t(){let a={x:.24,y:.7,r:.24},e={x:.5,y:.48,r:.36},t={x:.76,y:.7,r:.24},i=[...de(a.x,a.y,a.r,a.r,90,246,true),...de(e.x,e.y,e.r,e.r,180,360,false),...de(t.x,t.y,t.r,t.r,294,450,false),["Z"]];return Bt(i)}var X=.14,ft=.5523,le=X*ft,He=1-X,ce=.5-.5*ft,Ot={outline:[["M",0,X],["L",0,He],["C",0,He+le,ce,1,.5,1],["C",1-ce,1,1,He+le,1,He],["L",1,X],["C",1,X-le,1-ce,0,.5,0],["C",ce,0,0,X-le,0,X],["Z"]],seams:[[["M",0,X],["C",0,X+le,ce,X*2,.5,X*2],["C",1-ce,X*2,1,X+le,1,X]]]},Ut={outline:_t()},Gt={outline:[["M",.5,.97],["C",.5,.97,.03,.66,.03,.35],["C",.03,.17,.16,.04,.31,.04],["C",.41,.04,.47,.1,.5,.17],["C",.53,.1,.59,.04,.69,.04],["C",.84,.04,.97,.17,.97,.35],["C",.97,.66,.5,.97,.5,.97],["Z"]]},Ve=[["M",0,.25],["L",.58,.25],["L",.58,0],["L",1,.5],["L",.58,1],["L",.58,.75],["L",0,.75],["Z"]];function qe(a,e){let t=(i,n)=>{switch(e){case "down":return [1-n,i];case "left":return [1-i,1-n];case "up":return [n,1-i];default:return [i,n]}};return a.map(i=>{if(i[0]==="Z")return i;if(i[0]==="M"||i[0]==="L"){let[c,u]=t(i[1],i[2]);return [i[0],c,u]}let[n,s]=t(i[1],i[2]),[r,o]=t(i[3],i[4]),[h,l]=t(i[5],i[6]);return ["C",n,s,r,o,h,l]})}var Ne={right:Ve,left:qe(Ve,"left"),up:qe(Ve,"up"),down:qe(Ve,"down")},zt={outline:[["M",0,0],["L",0,1],["L",1,1],["Z"]]},Yt={outline:[["M",.34,0],["L",.66,0],["L",.66,.34],["L",1,.34],["L",1,.66],["L",.66,.66],["L",.66,1],["L",.34,1],["L",.34,.66],["L",0,.66],["L",0,.34],["L",.34,.34],["Z"]]},Xt={outline:[["M",0,.5],["L",.22,.13],["L",.22,.34],["L",.78,.34],["L",.78,.13],["L",1,.5],["L",.78,.87],["L",.78,.66],["L",.22,.66],["L",.22,.87],["Z"]]},Wt={outline:[["M",0,0],["L",1,0],["L",1,.8],["L",.32,.8],["L",.2,1],["L",.15,.8],["L",0,.8],["Z"]]},Kt={outline:[...de(.5,.4,.5,.4,100,-228,true),["L",.19,1],["Z"]]},Ft={outline:[["M",.5,0],["L",1,.25],["L",1,.75],["L",.5,1],["L",0,.75],["L",0,.25],["Z"]],seams:[[["M",.5,.5],["L",.5,1]],[["M",.5,.5],["L",0,.25]],[["M",.5,.5],["L",1,.25]]]},Zt={outline:[...de(.5,.5,.5,.5,0,-270,true),["L",.5,.5],["Z"]]},jt={outline:[["M",0,0],...de(.6,.5,.4,.5,270,450,false),["L",0,1],["Z"]]},qt={outline:[["M",0,0],["L",1,0],["L",1,1],["L",0,1],["Z"]],seams:[[["M",.12,0],["L",.12,1]],[["M",.88,0],["L",.88,1]]]},Jt={outline:[["M",0,0],["L",1,0],["L",1,.84],["C",.72,1.06,.28,.62,0,.84],["Z"]]},Qt={outline:[["M",0,0],["L",.45,0],["L",1,.5],["L",.45,1],["L",0,1],["L",.42,.5],["Z"]]};function en(a){let e=t=>1-t;return a.map(t=>t[0]==="Z"?t:t[0]==="M"||t[0]==="L"?[t[0],e(t[1]),t[2]]:["C",e(t[1]),t[2],e(t[3]),t[4],e(t[5]),t[6]])}var me=.16,mt={outline:[["M",.015,.02],["C",.075,.02,.1,.08,.1,.2],["C",.1,.36,.125,.5,.16,.5],["C",.125,.5,.1,.64,.1,.8],["C",.1,.92,.075,.98,.015,.98]],strokeOnly:true},tn={outline:en(mt.outline),strokeOnly:true};function Be(a){if(a.type!=="brace-left"&&a.type!=="brace-right")return 0;let e=(a.width??0)*me/2;return a.type==="brace-left"?e:-e}var nn={outline:[["M",0,0],["L",1,0],["L",1,1],["L",0,1],["Z"]],seams:[[["M",.1,0],["L",.1,1]],[["M",.9,0],["L",.9,1]]]};function _e(a){switch(a){case "cylinder":return Ot;case "cloud":return Ut;case "heart":return Gt;case "class-catalog":return nn;case "block-arrow-right":return {outline:Ne.right};case "block-arrow-left":return {outline:Ne.left};case "block-arrow-up":return {outline:Ne.up};case "block-arrow-down":return {outline:Ne.down};case "block-arrow-double":return Xt;case "right-triangle":return zt;case "cross":return Yt;case "speech-rect":return Wt;case "speech-round":return Kt;case "cube":return Ft;case "sector":return Zt;case "delay":return jt;case "predefined-process":return qt;case "document":return Jt;case "chevron":return Qt;case "brace-left":return mt;case "brace-right":return tn;default:return}}var oe=class a{ctx;width=0;height=0;dpr=window.devicePixelRatio||1;showGrid=false;textLayoutCache=new Map;static TEXT_CACHE_LIMIT=500;constructor(e,t){let i=e.getContext("2d");if(!i)throw new Error("Failed to get Canvas 2D context");this.ctx=i,typeof t=="number"&&t>0&&(this.dpr=t);}resize(e,t){this.width=e,this.height=t;let i=Math.ceil(e*this.dpr),n=Math.ceil(t*this.dpr);(this.ctx.canvas.width!==i||this.ctx.canvas.height!==n)&&(this.ctx.canvas.width=i,this.ctx.canvas.height=n,this.ctx.canvas.style.width=`${e}px`,this.ctx.canvas.style.height=`${t}px`);}setShowGrid(e){this.showGrid=e;}render(e,t,i,n,s,r,o,h,l,c,u){let d=this.ctx,g=t.getZoom(),p=t.x,y=t.y;d.setTransform(1,0,0,1,0,0),d.clearRect(0,0,this.ctx.canvas.width,this.ctx.canvas.height),u&&(d.fillStyle=u,d.fillRect(0,0,this.ctx.canvas.width,this.ctx.canvas.height));let f=this.dpr*g;d.setTransform(f,0,0,f,f*p,f*y),this.showGrid&&this.drawGrid(d,g,p,y);let w=null;for(let v of e)if(v.type==="arrow"&&(v.startBinding||v.endBinding)){w=new Map,e.forEach(M=>w.set(M.id,M));break}let x=w??new Map;e.forEach(v=>{d.save(),d.translate(v.x,v.y),d.rotate(v.rotation);let M=i.has(v.id);this.drawShape(d,v,M,g,x,l,c),d.restore();}),o&&this.drawGenerationPreview(d,o,g),n&&this.drawBrushRect(d,n,g),s&&s.length>0&&this.drawSnapGuides(d,s,g),r&&this.drawConnectingTargetHighlight(d,r,e,g),h&&this.drawEraserBrush(d,h,g),d.setTransform(1,0,0,1,0,0);}drawEraserBrush(e,t,i){let n=je/i;e.beginPath(),e.arc(t.x,t.y,n,0,Math.PI*2),e.fillStyle=F(L.primary,.12),e.fill(),e.strokeStyle=F(L.primary,.55),e.lineWidth=1.5/i,e.stroke();}drawConnectingTargetHighlight(e,t,i,n){let s=i.find(l=>l.id===t);if(!s||s.type==="line"||s.type==="arrow"||s.type==="draw")return;let r=this.getShapeWorldBounds(s),o=[{x:r.x+r.width/2,y:r.y},{x:r.x+r.width,y:r.y+r.height/2},{x:r.x+r.width/2,y:r.y+r.height},{x:r.x,y:r.y+r.height/2}],h=6/n;e.save(),o.forEach(l=>{e.beginPath(),e.arc(l.x,l.y,h,0,Math.PI*2),e.fillStyle=L.selection.handleFill,e.fill(),e.strokeStyle=L.primary,e.lineWidth=2/n,e.stroke();}),e.restore();}getShapeWorldBounds(e){let t=this.getShapeBounds(e);return {x:e.x+t.x,y:e.y+t.y,width:t.width,height:t.height}}drawBrushRect(e,t,i){e.save(),e.fillStyle=L.brushFill,e.strokeStyle=L.brushStroke,e.lineWidth=1.5/i,e.setLineDash([4/i,4/i]),e.beginPath(),e.rect(t.x,t.y,t.width,t.height),e.fill(),e.stroke(),e.restore();}drawGrid(e,t,i,n){let s=-i,r=-n,o=s+this.width/t,h=r+this.height/t,l=20;for(;l*t<10;)l*=2;e.strokeStyle=L.grid,e.lineWidth=1/t,e.beginPath();for(let c=Math.floor(s/l)*l;c<=o;c+=l)e.moveTo(c,r),e.lineTo(c,h);for(let c=Math.floor(r/l)*l;c<=h;c+=l)e.moveTo(s,c),e.lineTo(o,c);e.stroke();}drawShape(e,t,i,n,s,r,o){switch(this.applyStyle(e,t.style,n),t.type){case "rectangle":this.drawRectangle(e,t,i);break;case "rounded-rectangle":this.drawRoundedRectangle(e,t,i);break;case "circle":this.drawCircle(e,t,i);break;case "ellipse":this.drawEllipse(e,t,i);break;case "diamond":this.drawDiamond(e,t,i);break;case "triangle":this.drawTriangle(e,t,i);break;case "hexagon":this.drawHexagon(e,t,i);break;case "parallelogram":this.drawParallelogram(e,t,i);break;case "trapezoid":this.drawTrapezoid(e,t,i);break;case "pentagon":this.drawPentagon(e,t,i);break;case "octagon":this.drawOctagon(e,t,i);break;case "star":this.drawStar(e,t,i);break;case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "class-catalog":case "chevron":case "brace-left":case "brace-right":this.drawPathShape(e,t,i);break;case "stadium":this.drawStadium(e,t,i);break;case "uml-class":this.drawUmlClass(e,t,i);break;case "uml-interface":this.drawUmlInterface(e,t,i);break;case "mind-map":this.drawMindMap(e,t,i,r,o);break;case "line":this.drawLine(e,t,s,i,n);break;case "arrow":this.drawArrow(e,t,s,i,n);break;case "text":this.drawText(e,t);break;case "draw":this.drawFreehand(e,t);break}if(t.text&&t.type!=="text"&&t.type!=="line"&&t.type!=="arrow"&&t.type!=="draw"&&this.drawShapeText(e,t),i){let h=t.type==="line"||t.type==="arrow";t.type==="mind-map"||(h?this.drawSelectionHandles(e,t,n):(this.drawSelectionOutline(e,t,n),this.drawSelectionHandles(e,t,n)));}}applyStyle(e,t,i){let n=t.strokeOpacity??1;e.strokeStyle=n<1&&t.strokeColor!=="transparent"?F(t.strokeColor,n):t.strokeColor,e.lineWidth=t.strokeWidth/i,e.lineCap="round",e.lineJoin="round";let s=t.fillOpacity??1;s<1&&t.fillColor!=="transparent"?e.fillStyle=F(t.fillColor,s):e.fillStyle=t.fillColor,t.strokeDash?e.setLineDash(t.strokeDash):e.setLineDash([]);}drawRectangle(e,t,i){e.beginPath(),e.rect(-t.width/2,-t.height/2,t.width,t.height),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawRoundedRectangle(e,t,i){let{width:n,height:s,borderRadius:r}=t;e.beginPath(),e.roundRect(-n/2,-s/2,n,s,r),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawStadium(e,t,i){let{width:n,height:s}=t;e.beginPath(),e.roundRect(-n/2,-s/2,n,s,Math.min(n,s)/2),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawCircle(e,t,i){e.beginPath(),e.arc(0,0,t.radius,0,Math.PI*2),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawEllipse(e,t,i){e.beginPath(),e.ellipse(0,0,t.width/2,t.height/2,0,0,Math.PI*2),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawDiamond(e,t,i){e.beginPath(),e.moveTo(0,-t.height/2),e.lineTo(t.width/2,0),e.lineTo(0,t.height/2),e.lineTo(-t.width/2,0),e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawTriangle(e,t,i){e.beginPath(),e.moveTo(0,-t.height/2),e.lineTo(t.width/2,t.height/2),e.lineTo(-t.width/2,t.height/2),e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawHexagon(e,t,i){let n=t.width/2,s=t.height/2;e.beginPath();for(let r=0;r<6;r++){let o=r*Math.PI/3-Math.PI/2,h=n*Math.cos(o),l=s*Math.sin(o);r===0?e.moveTo(h,l):e.lineTo(h,l);}e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawParallelogram(e,t,i){let{width:n,height:s}=t,r=n*.2;e.beginPath(),e.moveTo(-n/2+r,-s/2),e.lineTo(n/2,-s/2),e.lineTo(n/2-r,s/2),e.lineTo(-n/2,s/2),e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawTrapezoid(e,t,i){let{width:n,height:s}=t,r=n*.15;e.beginPath(),e.moveTo(-n/2+r,-s/2),e.lineTo(n/2-r,-s/2),e.lineTo(n/2,s/2),e.lineTo(-n/2,s/2),e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawPentagon(e,t,i){let n=t.width/2,s=t.height/2;e.beginPath();for(let r=0;r<5;r++){let o=r*2*Math.PI/5-Math.PI/2,h=n*Math.cos(o),l=s*Math.sin(o);r===0?e.moveTo(h,l):e.lineTo(h,l);}e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawOctagon(e,t,i){let n=t.width/2,s=t.height/2;e.beginPath();for(let r=0;r<8;r++){let o=r*Math.PI/4-Math.PI/8,h=n*Math.cos(o),l=s*Math.sin(o);r===0?e.moveTo(h,l):e.lineTo(h,l);}e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawStar(e,t,i){let n=t.width/2,s=t.height/2,r=5,o=.4;e.beginPath();for(let h=0;h<r*2;h++){let l=h*Math.PI/r-Math.PI/2,c=h%2===0?1:o,u=n*c*Math.cos(l),d=s*c*Math.sin(l);h===0?e.moveTo(u,d):e.lineTo(u,d);}e.closePath(),t.style.fillColor!=="transparent"&&e.fill(),e.stroke();}drawPathShape(e,t,i){let n=_e(t.type);if(n&&(this.tracePath(e,ue(n.outline,t.width,t.height)),!n.strokeOnly&&t.style.fillColor!=="transparent"&&e.fill(),e.stroke(),n.seams))for(let s of n.seams)this.tracePath(e,ue(s,t.width,t.height)),e.stroke();}tracePath(e,t){e.beginPath();for(let i of t)switch(i[0]){case "M":e.moveTo(i[1],i[2]);break;case "L":e.lineTo(i[1],i[2]);break;case "C":e.bezierCurveTo(i[1],i[2],i[3],i[4],i[5],i[6]);break;case "Z":e.closePath();break}}drawUmlClass(e,t,i){let{width:n,height:s,className:r,attributes:o,methods:h}=t,l=n/2,c=s/2,u=j,d=o.length>0?o.length*$+E*2:E*2;e.beginPath(),e.rect(-l,-c,n,s),t.style.fillColor!=="transparent"&&(e.fillStyle=t.style.fillColor,e.fill()),e.stroke(),e.beginPath(),e.moveTo(-l,-c+u),e.lineTo(l,-c+u),e.moveTo(-l,-c+u+d),e.lineTo(l,-c+u+d),e.stroke(),e.save(),e.textBaseline="middle";let g=n-O*2,p=x=>{if(e.measureText(x).width<=g)return x;let v="\u2026",M=0,b=x.length;for(;M<b;){let C=M+b+1>>1;e.measureText(x.slice(0,C)+v).width<=g?M=C:b=C-1;}return x.slice(0,M)+v},y=(x,v,M,b,C,I,H)=>{let U=b||G,W=C||"#1f2937";e.font=`${U}px sans-serif`,e.fillStyle=W,e.textAlign=H;let R=p(x);if(e.fillText(R,v,M),I){let V=e.measureText(R).width,Ce=v;e.strokeStyle=W,e.beginPath(),e.moveTo(Ce,M),e.lineTo(Ce+V,M),e.stroke();}};e.font=`bold ${t.fontSize||G}px sans-serif`,e.textAlign="center",e.fillStyle=t.textColor||"#1f2937",e.fillText(p(r),0,-c+u/2);let f=-c+u+E+$/2;o.forEach((x,v)=>{let M=`${x.visibility} ${x.name}: ${x.type}`;y(M,-l+O,f+v*$,x.fontSize||t.fontSize||0,x.textColor||t.textColor||"",!!x.strikethrough,"left");});let w=-c+u+d+E+$/2;h.forEach((x,v)=>{let M=`${x.visibility} ${x.name}(${x.params}): ${x.returnType}`;y(M,-l+O,w+v*$,x.fontSize||t.fontSize||0,x.textColor||t.textColor||"",!!x.strikethrough,"left");}),e.restore();}drawUmlInterface(e,t,i){let{width:n,height:s,className:r,methods:o}=t,h=n/2,l=s/2,c=j;e.beginPath(),e.rect(-h,-l,n,s),t.style.fillColor!=="transparent"&&(e.fillStyle=t.style.fillColor,e.fill()),e.stroke(),e.beginPath(),e.moveTo(-h,-l+c),e.lineTo(h,-l+c),e.stroke(),e.save(),e.textBaseline="middle";let u=n-O*2,d=f=>{if(e.measureText(f).width<=u)return f;let w="\u2026",x=0,v=f.length;for(;x<v;){let M=x+v+1>>1;e.measureText(f.slice(0,M)+w).width<=u?x=M:v=M-1;}return f.slice(0,x)+w},g=(f,w,x,v,M,b,C)=>{let I=v||G,H=M||"#1f2937";e.font=`${I}px sans-serif`,e.fillStyle=H,e.textAlign=C;let U=d(f);if(e.fillText(U,w,x),b){let W=e.measureText(U).width,R=w;e.strokeStyle=H,e.beginPath(),e.moveTo(R,x),e.lineTo(R+W,x),e.stroke();}};e.font=`bold ${t.fontSize||G}px sans-serif`,e.textAlign="center",e.fillStyle=t.textColor||"#1f2937";let p=`<<${r||"Interface"}>>`;e.fillText(d(p),0,-l+c/2);let y=-l+c+E+$/2;o.forEach((f,w)=>{let x=`${f.visibility} ${f.name}(${f.params}): ${f.returnType}`;g(x,-h+O,y+w*$,f.fontSize||t.fontSize||0,f.textColor||t.textColor||"",!!f.strikethrough,"left");}),e.restore();}static DEFAULT_CORNER_RADIUS=10;static ROUTE_CLEARANCE=30;static CONNECTION_LEAD=24;static CONNECTED_PAD=8;drawMindMap(e,t,i,n,s){let o=B(t.root).positions;this.drawMindMapConnections(e,t.root,o),this.drawMindMapNodes(e,t,o,n,s);}drawMindMapConnections(e,t,i){let n=s=>{if(s.collapsed)return;let r=i.get(s.id);if(r)for(let o of s.children??[]){let h=i.get(o.id);if(!h)continue;let l=re(o,h.depth);e.strokeStyle=l,e.lineWidth=2,e.lineCap="round",e.beginPath(),e.moveTo(r.x+r.width/2,r.y);let c=(r.x+r.width/2+(h.x-h.width/2))/2;e.bezierCurveTo(c,r.y,c,h.y,h.x-h.width/2,h.y),e.stroke(),n(o);}};n(t);}drawMindMapNodes(e,t,i,n,s){let r=t.root,o=h=>{let l=i.get(h.id);if(!l)return;let c=re(h,l.depth),u=l.width/2,d=l.height/2;if(e.fillStyle="#ffffff",e.strokeStyle=c,e.lineWidth=2,e.beginPath(),e.roundRect(l.x-u,l.y-d,l.width,l.height,6),e.fill(),e.stroke(),s&&s.shapeId===t.id&&s.nodeId===h.id&&(e.fillStyle=F(L.primary,.06),e.strokeStyle=F(L.primary,.55),e.lineWidth=2,e.beginPath(),e.roundRect(l.x-u,l.y-d,l.width,l.height,6),e.fill(),e.stroke()),n&&n.shapeId===t.id&&n.nodeId===h.id&&(e.fillStyle=F(L.primary,.12),e.strokeStyle=L.primary,e.lineWidth=2.5,e.beginPath(),e.roundRect(l.x-u,l.y-d,l.width,l.height,6),e.fill(),e.stroke()),h.children&&h.children.length>0&&(e.fillStyle="#ffffff",e.strokeStyle=c,e.lineWidth=2,e.beginPath(),e.arc(l.x+u+10,l.y,8,0,Math.PI*2),e.fill(),e.stroke(),e.fillStyle=c,e.font="bold 13px sans-serif",e.textAlign="center",e.textBaseline="middle",e.fillText(h.collapsed?"+":"\u2212",l.x+u+10,l.y+.5)),h.note){let C=h.noteColor??c;e.font="11px sans-serif";let I=Math.min(e.measureText(h.note).width+12,130),H=16;e.fillStyle=C,e.beginPath(),e.roundRect(l.x-u,l.y-d-H+2,I,H,4),e.fill(),e.fillStyle="#ffffff",e.font="11px sans-serif",e.textAlign="left",e.textBaseline="middle",e.fillText(e.measureText(h.note).width+12>130?h.note.slice(0,12)+"\u2026":h.note,l.x-u+6,l.y-d-H/2+2);}e.font=`${14}px sans-serif`;let f=h.icon?18:0,w=l.width-f-(h.icon?8:0)-16,x=this.ellipsizeText(e,h.text||"",w),v=e.measureText(x).width,M=f+(f?8:0)+v,b=l.x-M/2;if(e.textBaseline="middle",h.icon&&(e.font="16px sans-serif",e.textAlign="left",e.fillStyle="#1f2937",e.fillText(h.icon,b,l.y+1),b+=f+8),e.font=`${14}px sans-serif`,e.textAlign="left",e.fillStyle="#1f2937",e.fillText(x,b,l.y),!h.collapsed)for(let C of h.children??[])o(C);};o(r);}ellipsizeText(e,t,i){if(i<=0)return "";if(e.measureText(t).width<=i)return t;let n="\u2026",s=0,r=t.length;for(;s<r;){let o=s+r+1>>1;e.measureText(t.slice(0,o)+n).width<=i?s=o:r=o-1;}return t.slice(0,s)+n}drawLine(e,t,i,n,s){if(t.points.length<2)return;let r=t.points.map(c=>({x:c.x,y:c.y}));r.length>=2&&(r[0]=this.clampToShapeOutline(r[0],i,t.id,r[r.length-1]),r[r.length-1]=this.clampToShapeOutline(r[r.length-1],i,t.id,r[0]));let o=r.map(c=>({x:c.x-t.x,y:c.y-t.y})),h=t.connectionType||"straight",l=h==="straight"?this.getAlignedPoints(o):this.buildPathPoints(o,h,this.collectRoutingObstacles(t.id,o[0],o[o.length-1],i,{x:t.x,y:t.y}),{fromN:this.getDockNormal(r[0],i,t.id),toN:this.getDockNormal(r[r.length-1],i,t.id)});if(h==="straight"){e.beginPath(),e.moveTo(o[0].x,o[0].y);for(let c=1;c<o.length;c++)e.lineTo(o[c].x,o[c].y);e.stroke(),e.setLineDash([]);}else h==="orthogonal"?this.drawSharpPolyline(e,l):this.drawRoundedPolyline(e,l,a.DEFAULT_CORNER_RADIUS),e.stroke(),e.setLineDash([]);n&&this.drawLinearHighlight(e,l,s);}drawArrow(e,t,i,n,s){if(t.points.length<2)return;let r=t.points.map(d=>({x:d.x,y:d.y}));if(t.startBinding){let d=i.get(t.startBinding.toShapeId);d&&(r[0]=this.getBindingPoint(d,t.startBinding));}if(t.endBinding){let d=i.get(t.endBinding.toShapeId);if(d){let g=this.resolveAutoEndBinding(t.endBinding,t,r[0],d);r[r.length-1]=this.getBindingPoint(d,g);}}r[0]=this.clampToShapeOutline(r[0],i,t.id,r[r.length-1]),r[r.length-1]=this.clampToShapeOutline(r[r.length-1],i,t.id,r[0]);let o=r.map(d=>({x:d.x-t.x,y:d.y-t.y})),h=t.connectionType||"straight",l=t.arrowHead??"end";!!t.dashed||l==="dependency"||l==="realization"?e.setLineDash([8,5]):e.setLineDash([]);let u;if(h==="straight"){let d=this.getAlignedPoints(o);u=d,e.beginPath(),e.moveTo(d[0].x,d[0].y);for(let g=1;g<d.length;g++)e.lineTo(d[g].x,d[g].y);e.stroke(),this.drawArrowHeads(e,d,l);}else u=this.buildPathPoints(o,h,this.collectRoutingObstacles(t.id,o[0],o[o.length-1],i,{x:t.x,y:t.y}),{fromN:this.getDockNormal(r[0],i,t.id),toN:this.getDockNormal(r[r.length-1],i,t.id)}),h==="orthogonal"?this.drawSharpPolyline(e,u):this.drawRoundedPolyline(e,u,a.DEFAULT_CORNER_RADIUS),e.stroke(),this.drawArrowHeads(e,u,l);e.setLineDash([]),n&&this.drawLinearHighlight(e,u,s);}drawLinearHighlight(e,t,i){if(!(!t||t.length<2)){e.save(),e.strokeStyle=L.lineHighlight,e.lineWidth=7/i,e.lineCap="round",e.lineJoin="round",e.setLineDash([]),e.beginPath(),e.moveTo(t[0].x,t[0].y);for(let n=1;n<t.length;n++)e.lineTo(t[n].x,t[n].y);e.stroke(),e.restore();}}drawGenerationPreview(e,t,i){let{from:n,to:s,type:r}=t;e.save(),e.beginPath(),e.arc(n.x,n.y,7/i,0,Math.PI*2),e.fillStyle=L.genDot,e.fill(),e.strokeStyle=L.genLine,e.lineWidth=2/i,e.setLineDash([5/i,4/i]),e.lineCap="round",e.beginPath(),e.moveTo(n.x,n.y),e.lineTo(s.x,s.y),e.stroke(),r&&this.drawPreviewShapeOutline(e,s,r,i),e.restore();}drawPreviewShapeOutline(e,t,i,n){let l=t.x,c=t.y;switch(e.strokeStyle=L.genLine,e.lineWidth=1.5/n,e.setLineDash([6/n,4/n]),e.beginPath(),i){case "circle":case "ellipse":e.ellipse(l,c,i==="circle"?40:120/2,i==="circle"?40:60/2,0,0,Math.PI*2);break;case "diamond":e.moveTo(l,c-30),e.lineTo(l+60,c),e.lineTo(l,c+30),e.lineTo(l-60,c),e.closePath();break;case "triangle":e.moveTo(l,c-30),e.lineTo(l+60,c+30),e.lineTo(l-60,c+30),e.closePath();break;case "hexagon":for(let u=0;u<6;u++){let d=u*Math.PI/3-Math.PI/2,g=l+60*Math.cos(d),p=c+30*Math.sin(d);u===0?e.moveTo(g,p):e.lineTo(g,p);}e.closePath();break;case "parallelogram":e.moveTo(l-60+18,c-30),e.lineTo(l+60,c-30),e.lineTo(l+60-18,c+30),e.lineTo(l-60,c+30),e.closePath();break;default:e.roundRect(l-60,c-30,120,60,i==="rounded-rectangle"?8:4);break}e.stroke();}static ALIGN_TOLERANCE=8;getAlignedPoints(e){if(e.length<2)return e;let t=e[0],i=e[e.length-1],n=i.x-t.x,s=i.y-t.y,r=a.ALIGN_TOLERANCE;if(Math.abs(s)<=r){let o=(t.y+i.y)/2;return e.map(h=>({x:h.x,y:o}))}if(Math.abs(n)<=r){let o=(t.x+i.x)/2;return e.map(h=>({x:o,y:h.y}))}return e}buildPathPoints(e,t,i=[],n={}){if(e.length<2||t==="straight")return e;switch(t){case "polyline":return this.generatePolylinePoints(e,i,n);case "orthogonal":return this.generateOrthogonalPoints(e,i,n);case "curve":return this.generateCurvePoints(e,n);default:return e}}drawRoundedPolyline(e,t,i){if(e.beginPath(),!(t.length<2)){if(t.length===2||i<=0){e.moveTo(t[0].x,t[0].y);for(let n=1;n<t.length;n++)e.lineTo(t[n].x,t[n].y);return}e.moveTo(t[0].x,t[0].y);for(let n=1;n<t.length-1;n++){let s=t[n-1],r=t[n],o=t[n+1],h=r.x-s.x,l=r.y-s.y,c=o.x-r.x,u=o.y-r.y,d=Math.hypot(h,l),g=Math.hypot(c,u);if(d<1e-6||g<1e-6){e.lineTo(r.x,r.y);continue}let p=Math.min(i,d*.5,g*.5),y=r.x-h/d*p,f=r.y-l/d*p,w=r.x+c/g*p,x=r.y+u/g*p;e.lineTo(y,f),e.arcTo(r.x,r.y,w,x,p);}e.lineTo(t[t.length-1].x,t[t.length-1].y);}}drawSharpPolyline(e,t){if(e.beginPath(),!(t.length<2)){e.moveTo(t[0].x,t[0].y);for(let i=1;i<t.length;i++)e.lineTo(t[i].x,t[i].y);}}drawSnapGuides(e,t,i){e.save(),e.strokeStyle=L.guide,e.lineWidth=1/i,e.setLineDash([6/i,4/i]),e.beginPath();for(let n of t)n.type==="horizontal"?(e.moveTo(n.start,n.position),e.lineTo(n.end,n.position)):(e.moveTo(n.position,n.start),e.lineTo(n.position,n.end));e.stroke(),e.restore();}generatePolylinePoints(e,t=[],i={}){if(e.length<2)return e;let n=[];n.push(e[0]);let s=e.length-1,r=a.CONNECTION_LEAD;for(let o=1;o<e.length;o++){let h=e[o-1],l=e[o],c=o===1,u=o===s,d=c&&i.fromN?{x:h.x+i.fromN.x*r,y:h.y+i.fromN.y*r}:h,g=u&&i.toN?{x:l.x+i.toN.x*r,y:l.y+i.toN.y*r}:l;c&&i.fromN&&n.push(d);let p=c?i.fromN:void 0,y=u?i.toN:void 0,f=p&&y&&p.x*y.x+p.y*y.y<0?Math.abs(p.x)>Math.abs(p.y)?"H":"V":void 0,w=this.computeOrthogonalRoute(d,g,t,f);for(let x of w)n.push(x);u&&i.toN&&n.push(l);}return this.collapseLinePoints(n)}generateOrthogonalPoints(e,t=[],i={}){return this.generatePolylinePoints(e,t,i)}collapseLinePoints(e){let t=[];for(let i of e){let n=t.length;if(!(n>0&&Math.abs(t[n-1].x-i.x)<1e-6&&Math.abs(t[n-1].y-i.y)<1e-6)){if(n>=2){let s=t[n-2],r=t[n-1];if(Math.abs(s.y-r.y)<1e-6&&Math.abs(r.y-i.y)<1e-6||Math.abs(s.x-r.x)<1e-6&&Math.abs(r.x-i.x)<1e-6){t[n-1]=i;continue}}t.push(i);}}return t}computeOrthogonalRoute(e,t,i=[],n){let s=t.x-e.x,r=t.y-e.y;if(Math.abs(s)<1||Math.abs(r)<1)return [t];let o=e.x+s/2,h=e.y+r/2,l=i.length>0,c=Math.max(e.x,Math.min(o,t.x)),u=Math.max(e.y,Math.min(h,t.y)),d=n==="H"?[[{x:c,y:e.y},{x:c,y:t.y},t],[{x:e.x,y:u},{x:t.x,y:u},t],[{x:t.x,y:e.y},t],[{x:e.x,y:t.y},t]]:n==="V"?[[{x:e.x,y:u},{x:t.x,y:u},t],[{x:c,y:e.y},{x:c,y:t.y},t],[{x:t.x,y:e.y},t],[{x:e.x,y:t.y},t]]:[[{x:o,y:e.y},{x:o,y:t.y},t],[{x:e.x,y:h},{x:t.x,y:h},t],[{x:t.x,y:e.y},t],[{x:e.x,y:t.y},t]];if(l){if(n){let p=this.findClearBus(e,t,i,n);if(p!==null)return p}for(let p of d)if(!this.routeHitsObstacles(e,p,i))return p;let g=this.routeOrthogonalAvoidance(e,t,i);return g&&g.length>0?g:i.length>0&&!this.routeHitsObstacles(e,d[0],i)?d[0]:this.routeHitsObstacles(e,d[1],i)?[t]:d[1]}return n==="H"?[{x:c,y:e.y},{x:c,y:t.y},t]:n==="V"?[{x:e.x,y:u},{x:t.x,y:u},t]:Math.abs(s)>=Math.abs(r)?[{x:o,y:e.y},{x:o,y:t.y},t]:[{x:e.x,y:h},{x:t.x,y:h},t]}findClearBus(e,t,i,n){let s=n==="H"?e.x:e.y,r=n==="H"?t.x:t.y;if(r-s<1)return null;let o=Math.max(6,(r-s)/24);for(let h=s;h<=r;h+=o){let l=n==="H"?[{x:h,y:e.y},{x:h,y:t.y},t]:[{x:e.x,y:h},{x:t.x,y:h},t];if(!this.routeHitsObstacles(e,l,i))return l}return null}collectRoutingObstacles(e,t,i,n,s){if(!n||n.size===0)return [];let r=[];return n.forEach((o,h)=>{if(h===e||o.type==="line"||o.type==="arrow")return;let l=this.getShapeBounds(o),c={x:o.x+l.x-s.x,y:o.y+l.y-s.y,width:l.width,height:l.height};c.pad=this.pointInRect(t,c)||this.pointInRect(i,c)?a.CONNECTED_PAD:a.ROUTE_CLEARANCE,r.push(c);}),r}pointInRect(e,t){return e.x>=t.x&&e.x<=t.x+t.width&&e.y>=t.y&&e.y<=t.y+t.height}routeHitsObstacles(e,t,i){let n=[e,...t];for(let s=1;s<n.length;s++)if(this.segmentHitsObstacle(n[s-1],n[s],i))return true;return false}segmentHitsObstacle(e,t,i){for(let n of i){let s=n.pad??a.ROUTE_CLEARANCE,r=n.y-s,o=n.y+n.height+s,h=n.x-s,l=n.x+n.width+s;if(Math.abs(e.y-t.y)<.5){if(e.y>=r&&e.y<=o){let c=Math.min(e.x,t.x);if(Math.max(e.x,t.x)>=h&&c<=l)return true}}else if(e.x>=h&&e.x<=l){let c=Math.min(e.y,t.y);if(Math.max(e.y,t.y)>=r&&c<=o)return true}}return false}routeOrthogonalAvoidance(e,t,i){let n=new Set([e.x,t.x]),s=new Set([e.y,t.y]);for(let S of i){let P=S.pad??a.ROUTE_CLEARANCE;n.add(S.x-P),n.add(S.x+S.width+P),s.add(S.y-P),s.add(S.y+S.height+P);}let r=[...n].sort((S,P)=>S-P),o=[...s].sort((S,P)=>S-P),h=r.length,l=o.length,c=S=>{for(let P=0;P<h;P++)if(Math.abs(r[P]-S)<1e-6)return P;return 0},u=S=>{for(let P=0;P<l;P++)if(Math.abs(o[P]-S)<1e-6)return P;return 0},d=c(e.x),g=u(e.y),p=c(t.x),y=u(t.y),f=[],w=[];for(let S=0;S<h;S++)f.push(new Array(l).fill(true)),w.push(new Array(l).fill(true));for(let S of i){let P=S.pad??a.ROUTE_CLEARANCE,k=S.x-P,D=S.x+S.width+P,z=S.y-P,Y=S.y+S.height+P;for(let A=0;A<h-1;A++)if(!(r[A+1]<=k||r[A]>=D))for(let K=0;K<l;K++)o[K]>z&&o[K]<Y&&(f[A][K]=false);for(let A=0;A<l-1;A++)if(!(o[A+1]<=z||o[A]>=Y))for(let K=0;K<h;K++)r[K]>k&&r[K]<D&&(w[K][A]=false);}let x=h*l,v=1/0,M=new Float64Array(x).fill(v),b=new Int32Array(x).fill(-1),C=new Uint8Array(x),I=S=>{let P=S%h,k=S/h|0;return Math.abs(r[P]-t.x)+Math.abs(o[k]-t.y)},H=(S,P)=>S%h===P%h,U=g*h+d,W=y*h+p;M[U]=0;let R=[U],V=[I(U)],Ce=S=>{let P=M[S]+I(S);R.push(S),V.push(P);let k=R.length-1;for(;k>0;){let D=k-1>>1;if(V[D]<=V[k])break;[R[D],R[k]]=[R[k],R[D]],[V[D],V[k]]=[V[k],V[D]],k=D;}},Tt=()=>{let S=R[0],P=R.pop(),k=V.pop();if(R.length){R[0]=P,V[0]=k;let D=0;for(;;){let z=D*2+1,Y=z+1,A=D;if(z<R.length&&V[z]<V[A]&&(A=z),Y<R.length&&V[Y]<V[A]&&(A=Y),A===D)break;[R[A],R[D]]=[R[D],R[A]],[V[A],V[D]]=[V[D],V[A]],D=A;}}return S},Te=(S,P,k)=>{let D=S%h,z=P%h,Y=M[S]+(Math.abs(z-D)===0?Math.abs(o[P/h|0]-o[S/h|0]):Math.abs(r[z]-r[D]))+k;Y<M[P]&&(M[P]=Y,b[P]=S,C[P]||Ce(P));};for(;R.length;){let S=Tt();if(S===W||C[S]){if(S===W)break;continue}C[S]=1;let P=S%h,k=S/h|0,D=b[S],z=D>=0?H(D,S)?"v":"h":"",Y=A=>{let K=H(S,A)?"v":"h";return z!==""&&z!==K?24:0};P+1<h&&f[P][k]&&Te(S,k*h+P+1,Y(k*h+P+1)),P-1>=0&&f[P-1][k]&&Te(S,k*h+P-1,Y(k*h+P-1)),k+1<l&&w[P][k]&&Te(S,(k+1)*h+P,Y((k+1)*h+P)),k-1>=0&&w[P][k-1]&&Te(S,(k-1)*h+P,Y((k-1)*h+P));}if(M[W]===v)return null;let Ge=[];for(let S=W;S!==-1;S=b[S])Ge.push({x:r[S%h],y:o[S/h|0]});Ge.reverse();let he=[];for(let S of Ge){let P=he.length;if(P>=2){let k=he[P-2],D=he[P-1];if(Math.abs(k.y-D.y)<1e-6&&Math.abs(D.y-S.y)<1e-6||Math.abs(k.x-D.x)<1e-6&&Math.abs(D.x-S.x)<1e-6){he[P-1]=S;continue}}he.push(S);}return he.slice(1)}generateCurvePoints(e,t={}){if(e.length<2)return e;let i=e.length,n=[e[0]],s=h=>{let l=e[h],c=e[h+1];return Math.hypot(c.x-l.x,c.y-l.y)},r=h=>Math.max(16,Math.min(h*.32,150)),o=h=>{let l=e[Math.max(0,h-1)],c=e[Math.min(i-1,h+1)],u=c.x-l.x,d=c.y-l.y,g=Math.hypot(u,d);return g<1e-6?{x:1,y:0}:{x:u/g,y:d/g}};for(let h=0;h<i-1;h++){let l=e[h],c=e[h+1],u=s(h),d;if(h===0&&t.fromN)d={x:l.x+t.fromN.x*r(u),y:l.y+t.fromN.y*r(u)};else {let y=o(h);d={x:l.x+y.x*r(u),y:l.y+y.y*r(u)};}let g;if(h===i-2&&t.toN)g={x:c.x+t.toN.x*r(u),y:c.y+t.toN.y*r(u)};else {let y=o(h+1);g={x:c.x-y.x*r(u),y:c.y-y.y*r(u)};}let p=20;for(let y=1;y<=p;y++){let f=y/p,w=1-f,x=w*w*w*l.x+3*w*w*f*d.x+3*w*f*f*g.x+f*f*f*c.x,v=w*w*w*l.y+3*w*w*f*d.y+3*w*f*f*g.y+f*f*f*c.y;n.push({x,y:v});}}return n}getDisplayLines(e,t,i,n,s,r){let o=n*1.3,h=Math.max(1,Math.floor(r/o)),l=`${t.id}\0${i}\0${n}\0${s}x${r}\0${h}`,c=this.textLayoutCache.get(l);if(c)return c;let u=i.split(`
|
|
2
|
+
`),d=[];for(let p of u){let y=this.wrapLine(e,p,s);for(let f of y)d.push(f);}let g=d.slice(0,h);return d.length>h&&g.length>0&&(g[g.length-1]=this.truncateLine(e,g[g.length-1],s)),this.textLayoutCache.size>=a.TEXT_CACHE_LIMIT&&this.textLayoutCache.clear(),this.textLayoutCache.set(l,g),g}drawShapeText(e,t){let i=t.text||"";if(!i)return;let n=t.fontSize||16;e.font=`${n}px sans-serif`,e.fillStyle=t.textColor||"#1f2937";let s=this.getTextBounds(t),r=n*1.3,o=this.getDisplayLines(e,t,i,n,s.width,s.height),l=-(o.length*r)/2+r/2,c=Be(t);e.textAlign="center",e.textBaseline="middle",o.forEach((u,d)=>{let g=l+d*r;e.fillText(u,c,g);});}getTextBounds(e){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "uml-class":case "uml-interface":return {width:e.width*.85,height:e.height*.85};case "brace-left":case "brace-right":return {width:e.width*(1-me)*.85,height:e.height*.85};case "circle":return {width:e.radius*2*.85,height:e.radius*2*.85};default:return {width:100,height:40}}}wrapLine(e,t,i){if(!t)return [""];let n=[],s="";for(let r of t){let o=s+r;e.measureText(o).width>i&&s?(n.push(s),s=r):s=o;}return s&&n.push(s),n}truncateLine(e,t,i){if(!t)return "...";let n="...",s=t;for(;s.length>0&&e.measureText(s+n).width>i;)s=s.slice(0,-1);return s+n}static HANDLE_NORMALS={top:{x:0,y:-1},bottom:{x:0,y:1},left:{x:-1,y:0},right:{x:1,y:0}};pickAutoHandle(e,t,i){let n=t.x-e.x,s=t.y-e.y;return i&&i.x!==0?n>=0?"left":"right":i&&i.y!==0?s>=0?"top":"bottom":Math.abs(n)>=Math.abs(s)?n>=0?"left":"right":s>=0?"top":"bottom"}resolveAutoEndBinding(e,t,i,n){if(!e.auto||e.isPrecise)return e;let s=t.startBinding?.handle?a.HANDLE_NORMALS[t.startBinding.handle]:void 0;return {...e,handle:this.pickAutoHandle(i,{x:n.x,y:n.y},s)}}getBindingPoint(e,t){if(t.isPrecise&&t.normalizedAnchor){let i=this.getShapePageBounds(e),n={x:i.x+i.width*t.normalizedAnchor.x,y:i.y+i.height*t.normalizedAnchor.y},s={x:e.x,y:e.y};return this.nearestPointOnShapeOutline(e,s,n)}if(t.handle){let i=this.getShapeBounds(e);switch(t.handle){case "top":return {x:e.x,y:e.y+i.y};case "bottom":return {x:e.x,y:e.y+i.y+i.height};case "left":return {x:e.x+i.x,y:e.y};case "right":return {x:e.x+i.x+i.width,y:e.y}}}return {x:e.x,y:e.y}}getDockNormal(e,t,i){if(!t||t.size===0)return;let n=null,s=1/0,r=.5;if(t.forEach((c,u)=>{if(u===i||c.type==="line"||c.type==="arrow")return;let d=this.getShapePageBounds(c);if(e.x>=d.x-r&&e.x<=d.x+d.width+r&&e.y>=d.y-r&&e.y<=d.y+d.height+r){let g=(e.x-c.x)**2+(e.y-c.y)**2;g<s&&(s=g,n=c);}}),!n)return;let o=n,h=e.x-o.x,l=e.y-o.y;return Math.abs(l)>Math.abs(h)?{x:0,y:l<0?-1:1}:{x:h<0?-1:1,y:0}}getShapePageBounds(e){let t=this.getShapeBounds(e);return {x:e.x+t.x,y:e.y+t.y,width:t.width,height:t.height}}clampToShapeOutline(e,t,i,n){if(!t||t.size===0)return e;let s=null,r=1/0;if(t.forEach((h,l)=>{if(l===i)return;let c=this.getShapePageBounds(h);if(e.x>c.x&&e.x<c.x+c.width&&e.y>c.y&&e.y<c.y+c.height){let u=(h.x-n.x)**2+(h.y-n.y)**2;u<r&&(r=u,s=h);}}),!s)return e;let o=s;return this.nearestPointOnShapeOutline(o,{x:o.x,y:o.y},e)}nearestPointOnShapeOutline(e,t,i){let n=i.x-t.x,s=i.y-t.y,r=Math.hypot(n,s);if(r<1e-6)return {x:t.x,y:t.y};let o=n/r,h=s/r,l=Math.max(1e4,r*2),c={x:t.x+o*l,y:t.y+h*l},u=this.intersectShapeOutlineWithSegment(e,t,c);if(u.length===0)return i;let d=u[0],g=(d.x-t.x)**2+(d.y-t.y)**2;for(let p=1;p<u.length;p++){let y=(u[p].x-t.x)**2+(u[p].y-t.y)**2;y<g&&(g=y,d=u[p]);}return d}intersectShapeOutlineWithSegment(e,t,i){let n={x:t.x-e.x,y:t.y-e.y},s={x:i.x-e.x,y:i.y-e.y};return this.intersectLocalShapeOutlineWithSegment(e,n,s).map(o=>({x:o.x+e.x,y:o.y+e.y}))}intersectLocalShapeOutlineWithSegment(e,t,i){switch(e.type){case "rectangle":case "rounded-rectangle":{let n=e.width,s=e.height,r={x:-n/2,y:-s/2,width:n,height:s};return this.intersectSegmentRect(t,i,r)}case "ellipse":case "diamond":case "triangle":{let n=e.width,s=e.height,r={x:-n/2,y:-s/2,width:n,height:s};return this.intersectSegmentRect(t,i,r)}case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":{let n=e.width,s=e.height,r={x:-n/2,y:-s/2,width:n,height:s};return this.intersectSegmentRect(t,i,r)}case "circle":{let n=e.radius;return this.intersectSegmentCircle(t,i,{x:0,y:0},n)}default:return []}}intersectSegmentRect(e,t,i){let n=i.x,s=i.y,r=i.width,o=i.height,h=[[{x:n,y:s},{x:n+r,y:s}],[{x:n+r,y:s},{x:n+r,y:s+o}],[{x:n+r,y:s+o},{x:n,y:s+o}],[{x:n,y:s+o},{x:n,y:s}]],l=[];for(let[c,u]of h){let d=this.segmentIntersection(e,t,c,u);d&&l.push(d);}return l}segmentIntersection(e,t,i,n){let s=e.x,r=e.y,o=t.x,h=t.y,l=i.x,c=i.y,u=n.x,d=n.y,g=(s-o)*(c-d)-(r-h)*(l-u);if(Math.abs(g)<1e-9)return null;let p=((s-l)*(c-d)-(r-c)*(l-u))/g,y=-((s-o)*(r-c)-(r-h)*(s-l))/g;return p<0||p>1||y<0||y>1?null:{x:s+p*(o-s),y:r+p*(h-r)}}intersectSegmentCircle(e,t,i,n){let s=t.x-e.x,r=t.y-e.y,o=e.x-i.x,h=e.y-i.y,l=s*s+r*r,c=2*(o*s+h*r),u=o*o+h*h-n*n,d=c*c-4*l*u;if(d<0)return [];d=Math.sqrt(d);let g=(-c-d)/(2*l),p=(-c+d)/(2*l),y=[];return g>=0&&g<=1&&y.push({x:e.x+g*s,y:e.y+g*r}),p>=0&&p<=1&&Math.abs(p-g)>1e-6&&y.push({x:e.x+p*s,y:e.y+p*r}),y}drawArrowHead(e,t,i){let n=Math.atan2(i.y-t.y,i.x-t.x),s=12;e.save(),e.setLineDash([]),e.beginPath(),e.moveTo(i.x,i.y),e.lineTo(i.x-s*Math.cos(n-Math.PI/6),i.y-s*Math.sin(n-Math.PI/6)),e.moveTo(i.x,i.y),e.lineTo(i.x-s*Math.cos(n+Math.PI/6),i.y-s*Math.sin(n+Math.PI/6)),e.stroke(),e.restore();}drawInheritanceHead(e,t,i){let n=Math.atan2(i.y-t.y,i.x-t.x),s=14,r=i.x,o=i.y,h=i.x-s*Math.cos(n),l=i.y-s*Math.sin(n),c=h+s*.5*Math.cos(n-Math.PI/2),u=l+s*.5*Math.sin(n-Math.PI/2),d=h+s*.5*Math.cos(n+Math.PI/2),g=l+s*.5*Math.sin(n+Math.PI/2);e.save(),e.setLineDash([]),e.fillStyle="#ffffff",e.beginPath(),e.moveTo(r,o),e.lineTo(c,u),e.lineTo(d,g),e.closePath(),e.fill(),e.stroke(),e.restore();}drawDiamondHead(e,t,i,n){let s=Math.atan2(i.y-t.y,i.x-t.x),r=14,o=i.x,h=i.y,l=i.x-r*.5*Math.cos(s),c=i.y-r*.5*Math.sin(s),u=i.x-r*Math.cos(s),d=i.y-r*Math.sin(s),g=l+r*.4*Math.cos(s-Math.PI/2),p=c+r*.4*Math.sin(s-Math.PI/2),y=l+r*.4*Math.cos(s+Math.PI/2),f=c+r*.4*Math.sin(s+Math.PI/2);e.save(),e.setLineDash([]),e.fillStyle=n?"#1f2937":"#ffffff",e.beginPath(),e.moveTo(o,h),e.lineTo(g,p),e.lineTo(u,d),e.lineTo(y,f),e.closePath(),e.fill(),e.stroke(),e.restore();}drawArrowHeads(e,t,i){if(i!=="none"){if(i==="start"||i==="both"){let n=t[0],s=t[1];this.drawArrowHead(e,s,n);}if(i==="end"||i==="both"){let n=t.length,s=t[n-2],r=t[n-1];this.drawArrowHead(e,s,r);}if(i==="inheritance"){let n=t.length,s=t[n-2],r=t[n-1];this.drawInheritanceHead(e,s,r);}if(i==="aggregation"){let n=t[0],s=t[1];this.drawDiamondHead(e,s,n,false);}if(i==="composition"){let n=t[0],s=t[1];this.drawDiamondHead(e,s,n,true);}if(i==="dependency"){let n=t.length;this.drawArrowHead(e,t[n-2],t[n-1]);}if(i==="realization"){let n=t.length;this.drawInheritanceHead(e,t[n-2],t[n-1]);}}}drawText(e,t){if(!t.text)return;let i=t.fontSize;e.font=`${i}px sans-serif`,e.textAlign="center",e.textBaseline="middle";let n=t.width,s=t.height,r=i*1.3,o=this.getDisplayLines(e,t,t.text,i,n,s),h=o.length*r,l=-h/2+r/2;if(t.highlightColor&&t.highlightColor!=="transparent"){let c=t.highlightOpacity??1;e.fillStyle=c<1?F(t.highlightColor,c):t.highlightColor;let u=i*.15;e.beginPath(),e.roundRect(-n/2-u,-h/2-u,n+u*2,h+u*2,4),e.fill();}e.fillStyle=t.textColor||"#1f2937",o.forEach((c,u)=>{let d=l+u*r;e.fillText(c,0,d);});}drawFreehand(e,t){if(t.points.length<2)return;let i=t.points;e.beginPath(),e.moveTo(i[0].x,i[0].y);for(let n=1;n<i.length-1;n++){let s=(i[n].x+i[n+1].x)/2,r=(i[n].y+i[n+1].y)/2;e.quadraticCurveTo(i[n].x,i[n].y,s,r);}e.lineTo(i[i.length-1].x,i[i.length-1].y),e.stroke();}drawSelectionOutline(e,t,i){let n=this.getShapeBounds(t);e.strokeStyle=L.selection.outline,e.lineWidth=1/i,e.setLineDash([]),e.save(),e.shadowColor=L.selection.shadow,e.shadowBlur=8,e.shadowOffsetX=0,e.shadowOffsetY=0,e.beginPath(),e.roundRect(n.x-2,n.y-2,n.width+4,n.height+4,4),e.stroke(),e.restore(),e.fillStyle=L.selection.outlineFill,e.beginPath(),e.roundRect(n.x-2,n.y-2,n.width+4,n.height+4,4),e.fill();}drawSelectionHandles(e,t,i){let n=8/i;if((t.type==="line"||t.type==="arrow")&&t.points&&t.points.length>=2){let r=t.points.map(l=>({x:l.x-t.x,y:l.y-t.y})),o=r[0],h=r[r.length-1];e.fillStyle=L.selection.handleFill,e.strokeStyle=L.selection.handleStroke,e.lineWidth=2/i,e.setLineDash([]),[o,h].forEach(l=>{e.beginPath(),e.arc(l.x,l.y,n/2,0,Math.PI*2),e.fill(),e.stroke();});}else {let r=this.getShapeBounds(t),o=n,h=[{x:r.x,y:r.y},{x:r.x+r.width,y:r.y},{x:r.x+r.width,y:r.y+r.height},{x:r.x,y:r.y+r.height}];e.fillStyle=L.selection.handleFill,e.strokeStyle=L.selection.handleStroke,e.lineWidth=2/i,e.setLineDash([]),h.forEach(p=>{e.beginPath(),e.roundRect(p.x-o/2,p.y-o/2,o,o,2),e.fill(),e.stroke();});let l=9/i;[{x:r.x+r.width/2,y:r.y},{x:r.x+r.width,y:r.y+r.height/2},{x:r.x+r.width/2,y:r.y+r.height},{x:r.x,y:r.y+r.height/2}].forEach(p=>{e.beginPath(),e.arc(p.x,p.y,l,0,Math.PI*2),e.fillStyle=L.selection.handleFill,e.fill(),e.strokeStyle=L.primary,e.lineWidth=1.5/i,e.stroke(),e.beginPath(),e.arc(p.x,p.y,l*.45,0,Math.PI*2),e.fillStyle=L.primary,e.fill();});let u=26/i,d=r.x+r.width/2,g=r.y-u;e.strokeStyle="#94a3b8",e.lineWidth=1.5/i,e.setLineDash([]),e.beginPath(),e.moveTo(d,r.y),e.lineTo(d,g),e.stroke(),e.beginPath(),e.arc(d,g,8/i,0,Math.PI*2),e.fillStyle=L.selection.handleFill,e.fill(),e.strokeStyle=L.primary,e.lineWidth=1.6/i,e.stroke(),this.drawRotateIcon(e,d,g,i);}}drawRotateIcon(e,t,i,n){let s=8/n;e.save(),e.translate(t,i),e.strokeStyle=L.primary,e.lineWidth=1.8/n,e.lineCap="round",e.beginPath(),e.arc(0,0,s*.5,Math.PI*.18,Math.PI*1.6,false),e.stroke();let r=Math.PI*.18,o=Math.cos(r)*s*.5,h=Math.sin(r)*s*.5,l=1;e.beginPath(),e.moveTo(o,h),e.lineTo(o-Math.cos(r)*s*.26+l*Math.sin(r)*s*.16,h-Math.sin(r)*s*.26-l*Math.cos(r)*s*.16),e.moveTo(o,h),e.lineTo(o-Math.cos(r)*s*.26-l*Math.sin(r)*s*.16,h-Math.sin(r)*s*.26+l*Math.cos(r)*s*.16),e.stroke(),e.restore();}getShapeBounds(e){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":case "mind-map":return {x:-e.width/2,y:-e.height/2,width:e.width,height:e.height};case "circle":return {x:-e.radius,y:-e.radius,width:e.radius*2,height:e.radius*2};case "line":case "arrow":{if(e.points.length===0)return {x:0,y:0,width:0,height:0};let t=1/0,i=1/0,n=-1/0,s=-1/0;return e.points.forEach(r=>{t=Math.min(t,r.x-e.x),i=Math.min(i,r.y-e.y),n=Math.max(n,r.x-e.x),s=Math.max(s,r.y-e.y);}),{x:t,y:i,width:n-t,height:s-i}}case "draw":{let t=e;if(typeof t.width=="number"&&typeof t.height=="number")return {x:-t.width/2,y:-t.height/2,width:t.width,height:t.height};if(t.points.length===0)return {x:0,y:0,width:0,height:0};let i=1/0,n=1/0,s=-1/0,r=-1/0;return t.points.forEach(o=>{let h=o.x-t.x,l=o.y-t.y;i=Math.min(i,h),n=Math.min(n,l),s=Math.max(s,h),r=Math.max(r,l);}),{x:i,y:n,width:s-i,height:r-n}}case "text":{let t=e;return {x:-t.width/2,y:-t.height/2,width:t.width,height:t.height}}default:return {x:0,y:0,width:0,height:0}}}};var ae={create(a=0,e=0){return {x:a,y:e}},add(a,e){return {x:a.x+e.x,y:a.y+e.y}},subtract(a,e){return {x:a.x-e.x,y:a.y-e.y}},multiply(a,e){return {x:a.x*e,y:a.y*e}},divide(a,e){return {x:a.x/e,y:a.y/e}},dot(a,e){return a.x*e.x+a.y*e.y},length(a){return Math.sqrt(a.x*a.x+a.y*a.y)},normalize(a){let e=ae.length(a);return e===0?{x:0,y:0}:{x:a.x/e,y:a.y/e}},distance(a,e){return ae.length(ae.subtract(a,e))},rotate(a,e){let t=Math.cos(e),i=Math.sin(e);return {x:a.x*t-a.y*i,y:a.x*i+a.y*t}},clone(a){return {x:a.x,y:a.y}},equals(a,e){return a.x===e.x&&a.y===e.y}};function ee(a,e,t,i=8){let n=[];if(t==="horizontal"){let s=Math.min(a.x,e.x),r=Math.max(a.x+a.width,e.x+e.width),o=Math.abs(a.y-e.y)<=i,h=Math.abs(a.y+a.height-(e.y+e.height))<=i,l=Math.abs(a.y+a.height/2-(e.y+e.height/2))<=i;o&&n.push({type:"horizontal",position:(a.y+e.y)/2,start:s,end:r}),h&&n.push({type:"horizontal",position:(a.y+a.height+e.y+e.height)/2,start:s,end:r}),l&&!(o&&h)&&n.push({type:"horizontal",position:(a.y+a.height/2+e.y+e.height/2)/2,start:s,end:r});}else {let s=Math.min(a.y,e.y),r=Math.max(a.y+a.height,e.y+e.height),o=Math.abs(a.x-e.x)<=i,h=Math.abs(a.x+a.width-(e.x+e.width))<=i,l=Math.abs(a.x+a.width/2-(e.x+e.width/2))<=i;o&&n.push({type:"vertical",position:(a.x+e.x)/2,start:s,end:r}),h&&n.push({type:"vertical",position:(a.x+a.width+e.x+e.width)/2,start:s,end:r}),l&&!(o&&h)&&n.push({type:"vertical",position:(a.x+a.width/2+e.x+e.width/2)/2,start:s,end:r});}return n}var xe=class a extends N{type="select";state="idle";translatingState=null;resizingState=null;generatingState=null;hoverGenPoint=null;rotatingState=null;draggingArrowId=null;draggingTerminal=null;pendingHistoryCommit=false;brushStart={x:0,y:0};brushShiftKey=false;defaultGenType(e){if(e){let t=this.context.getShapes().find(i=>i.id===e);if(t&&Ae(t.type))return t.type}return "rectangle"}genPreviewOf(e,t){return {from:e.point,to:t,type:this.defaultGenType(e.sourceShapeId)}}genContext(e,t){return {from:e.point,to:t,sourceShapeId:e.sourceShapeId,edge:e.edge}}onPointerDown(e,t){let i=this.context.getShapes(),n=this.context.getSelectedIds(),s=this.hitTestArrowTerminal(e,i);if(s&&n.includes(s.shapeId)){this.state="dragging-terminal",this.draggingArrowId=s.shapeId,this.draggingTerminal=s.terminal,this.pendingHistoryCommit=true,this.context.updateArrowBinding(s.shapeId,s.terminal,null);return}let r=this.hitGenPoint(e,i,n);if(r){this.state="generating",this.generatingState={source:r,startPoint:e,hasMoved:false},this.pendingHistoryCommit=true,this.context.setGenerationPreview(this.genPreviewOf(r,this.genPreviewTarget(r,e,false)));return}let o=this.hitTest(e,i,n);if(o.handle==="rotation"){this.state="rotating",this.pendingHistoryCommit=true;let h=o.shape;this.rotatingState={shapeId:h.id,center:{x:h.x,y:h.y},startAngle:Math.atan2(e.y-h.y,e.x-h.x),startRotation:h.rotation};return}if(o.handle&&o.shape&&o.shape.type!=="mind-map"){this.state="resizing",this.resizingState={startPoint:e,startShape:o.shape,handle:o.handle},this.pendingHistoryCommit=true;return}if(o.shape){let h=o.shape,l=h.type==="line"||h.type==="arrow",c=n.includes(h.id);if(l&&!c){this.context.selectShape(h.id,t.shiftKey);return}this.state="translating",this.pendingHistoryCommit=true,c||this.context.selectShape(h.id,t.shiftKey);let u=this.context.getSelectedIds(),d=new Map,g=new Map,p=new Map;u.forEach(y=>{let f=i.find(w=>w.id===y);f&&(d.set(y,{x:f.x,y:f.y}),g.set(y,f.rotation),p.set(y,this.getShapeWorldBounds(f)));}),this.translatingState={startPoint:e,startPositions:d,startRotation:g,startBounds:p};}else this.state="brushing",this.brushStart=e,this.brushShiftKey=t.shiftKey,t.shiftKey||this.context.selectShape("",false);}onPointerMove(e,t){let i=this.context.getShapes();if(this.state==="translating"&&this.translatingState)this.pendingHistoryCommit&&(this.context.commitHistory(),this.pendingHistoryCommit=false),this.handleTranslating(e,t);else if(this.state==="generating"&&this.generatingState)this.pendingHistoryCommit&&(this.context.commitHistory(),this.pendingHistoryCommit=false),this.handleGenerating(e,i);else if(this.state==="rotating"&&this.rotatingState)this.pendingHistoryCommit&&(this.context.commitHistory(),this.pendingHistoryCommit=false),this.handleRotating(e,t);else if(this.state==="resizing"&&this.resizingState)this.pendingHistoryCommit&&(this.context.commitHistory(),this.pendingHistoryCommit=false),this.handleResizing(e);else if(this.state==="dragging-terminal"&&this.draggingArrowId&&this.draggingTerminal){this.pendingHistoryCommit&&(this.context.commitHistory(),this.pendingHistoryCommit=false);let n=i.find(s=>s.id===this.draggingArrowId);if(n){let s=this.draggingTerminal==="start"?e:n.points[n.points.length-1],r=this.draggingTerminal==="start"?n.points[0]:e,o=this.computeSnapGuidesForArrow(s,r,e,n,i);this.context.setSnapGuides(o);let h=e;if(o.length>0){let u=o.filter(g=>g.type==="horizontal"),d=o.filter(g=>g.type==="vertical");if(u.length>0){let g=u[0].position,p=Math.abs(e.y-g);for(let y of u){let f=Math.abs(e.y-y.position);f<p&&(p=f,g=y.position);}h={...h,y:g};}else if(d.length>0){let g=d[0].position,p=Math.abs(e.x-g);for(let y of d){let f=Math.abs(e.x-y.position);f<p&&(p=f,g=y.position);}h={...h,x:g};}}let l=this.draggingTerminal==="start"?0:n.points.length-1,c=[...n.points];c[l]=h,this.context.updateShape(this.draggingArrowId,{points:c});}}else if(this.state==="brushing")this.handleBrushing(e);else {let n=this.context.getSelectedIds(),s=this.hitGenPoint(e,i,n);s?(this.hoverGenPoint=s,this.context.setGenerationPreview(this.genPreviewOf(s,this.genPreviewTarget(s,e,false)))):this.hoverGenPoint&&(this.hoverGenPoint=null,this.context.setGenerationPreview(null));}}onPointerUp(e,t){if(this.state==="dragging-terminal"&&this.draggingArrowId&&this.draggingTerminal){let i=this.context.getShapes(),n=this.findBindingNearPoint(e,i,this.draggingArrowId);n&&this.context.updateArrowBinding(this.draggingArrowId,this.draggingTerminal,n.toShapeId,n.binding);}if(this.state==="generating"&&this.generatingState){let i=this.generatingState,n=this.context.getShapes(),s=i.source,r=this.findBindingNearPoint(e,n,s.sourceShapeId);if(r)this.context.connectToExisting(this.genContext(s,e),r.toShapeId,r.binding);else if(!i.hasMoved)this.context.createGeneratedConnection(this.genContext(s,this.genPreviewTarget(s,s.point,false)),{type:this.defaultGenType(s.sourceShapeId)});else {let o=this.context.createFloatingLine(this.genContext(s,e));this.context.openGenerationMenu({context:this.genContext(s,e),existingArrowId:o,defaultShapeType:this.defaultGenType(s.sourceShapeId)});}this.generatingState=null,this.context.setGenerationPreview(null);}this.state==="brushing"&&this.context.setBrushRect(null),this.context.setConnectingTarget(null),this.hoverGenPoint=null,this.pendingHistoryCommit=false,this.state="idle",this.draggingArrowId=null,this.draggingTerminal=null,this.translatingState=null,this.resizingState=null,this.rotatingState=null,this.context.setSnapGuides([]);}onKeyDown(e){let t=e.target;if(!(t&&(t.tagName==="INPUT"||t.tagName==="TEXTAREA"||t.isContentEditable))){if(e.key==="Delete"||e.key==="Backspace"){let i=this.context.getSelectedIds();i.length>0&&(this.context.commitHistory(),i.forEach(n=>{this.context.deleteShape(n);}));}e.key==="Escape"&&(this.context.selectShape("",false),this.context.setSnapGuides([]),this.state="idle"),(e.key==="+"||e.key==="=")&&e.shiftKey&&(this.context.commitHistory(),this.handleRotation(15)),e.key==="-"&&e.shiftKey&&(this.context.commitHistory(),this.handleRotation(-15)),e.key==="ArrowUp"&&(e.preventDefault(),this.context.commitHistory(),this.handleNudge(0,-5)),e.key==="ArrowDown"&&(e.preventDefault(),this.context.commitHistory(),this.handleNudge(0,5)),e.key==="ArrowLeft"&&(e.preventDefault(),this.context.commitHistory(),this.handleNudge(-5,0)),e.key==="ArrowRight"&&(e.preventDefault(),this.context.commitHistory(),this.handleNudge(5,0));}}onKeyUp(e){}getCursor(e){switch(this.state){case "translating":return "grabbing";case "generating":return "crosshair";case "rotating":return "grabbing";case "resizing":return this.resizingState?this.getHandleCursor(this.resizingState.handle,this.resizingState.startShape):"default";case "dragging-terminal":return "crosshair";case "brushing":return "crosshair"}let t=this.context.getShapes(),i=this.context.getSelectedIds();if(this.context.getMindMapCursorHitAtPoint(e))return "pointer";if(this.hitGenPoint(e,t,i))return "crosshair";if(this.hitTestArrowTerminal(e,t)&&i.includes(this.hitTestArrowTerminal(e,t).shapeId))return "move";let n=this.hitTest(e,t,i);return n.handle==="rotation"?"grab":n.handle?this.getHandleCursor(n.handle,n.shape):n.shape?"move":null}getHandleCursor(e,t){let n={e:0,se:45,s:90,sw:135,w:180,nw:225,n:270,ne:315}[e];if(n===void 0)return "grab";let s=((n+t.rotation*180/Math.PI)%360+360)%360,r=Math.round(s/45)%4;return ["ew-resize","nwse-resize","ns-resize","nesw-resize"][r]}static ALIGN_SNAP_THRESHOLD=8;handleTranslating(e,t){if(!this.translatingState)return;let i=ae.subtract(e,this.translatingState.startPoint),n={...i};t.shiftKey&&(Math.abs(i.x)>Math.abs(i.y)?n.y=0:n.x=0);let s=this.context.getShapes(),r=this.context.getSelectedIds(),o=new Set(r),h=[],l=[];if(this.translatingState.startBounds.forEach(c=>{l.push({x:c.x+n.x,y:c.y+n.y,width:c.width,height:c.height});}),!t.shiftKey&&l.length>0){let c=[];for(let g of s)o.has(g.id)||g.type==="line"||g.type==="arrow"||g.type==="draw"||c.push(this.getShapeWorldBounds(g));let u=[],d=[];for(let g of l){let p=[g.y,g.y+g.height/2,g.y+g.height],y=[g.x,g.x+g.width/2,g.x+g.width];for(let f of c){let w=[f.y,f.y+f.height/2,f.y+f.height];for(let v of p)for(let M of w){let b=M-v;Math.abs(b)<=a.ALIGN_SNAP_THRESHOLD&&u.push(b);}let x=[f.x,f.x+f.width/2,f.x+f.width];for(let v of y)for(let M of x){let b=M-v;Math.abs(b)<=a.ALIGN_SNAP_THRESHOLD&&d.push(b);}}}if(u.length>0){let g=u[0];for(let p of u)Math.abs(p)<Math.abs(g)&&(g=p);n.y+=g;}if(d.length>0){let g=d[0];for(let p of d)Math.abs(p)<Math.abs(g)&&(g=p);n.x+=g;}this.translatingState.startBounds.forEach(g=>{let p={x:g.x+n.x,y:g.y+n.y,width:g.width,height:g.height};for(let y of c)h.push(...ee(p,y,"horizontal",1)),h.push(...ee(p,y,"vertical",1));});}this.translatingState.startPositions.forEach((c,u)=>{this.context.updateShape(u,{x:c.x+n.x,y:c.y+n.y});}),this.context.setSnapGuides(this.mergeGuides(h));}handleResizing(e){if(!this.resizingState)return;let t=this.resizingState.startShape,i=this.resizingState.handle,n=this.resizingState.startPoint;if(!i||i==="rotation")return;let r={nw:[-1,-1],n:[0,-1],ne:[1,-1],w:[-1,0],e:[1,0],sw:[-1,1],s:[0,1],se:[1,1]}[i];if(!r)return;let[o,h]=r,l=ae.subtract(e,n),c=[];for(let b of this.context.getShapes())b.id!==t.id&&(b.type==="line"||b.type==="arrow"||b.type==="draw"||c.push(this.getShapeWorldBounds(b)));let u=20,d=this.getShapeWorldBounds(t),g=d.x,p=d.y,y=d.x+d.width,f=d.y+d.height;o===1?y=Math.max(g+u,y+l.x):o===-1&&(g=Math.min(y-u,g+l.x)),h===1?f=Math.max(p+u,f+l.y):h===-1&&(p=Math.min(f-u,p+l.y));let w=(b,C)=>{let I=null;for(let H of C){let U=H-b;Math.abs(U)<=a.ALIGN_SNAP_THRESHOLD&&(I===null||Math.abs(U)<Math.abs(I))&&(I=U);}return I};if(o!==0&&c.length>0){let b=[];for(let I of c)b.push(I.x,I.x+I.width/2,I.x+I.width);let C=w(o===1?y:g,b);C!==null&&(o===1?y+=C:g+=C);}if(h!==0&&c.length>0){let b=[];for(let I of c)b.push(I.y,I.y+I.height/2,I.y+I.height);let C=w(h===1?f:p,b);C!==null&&(h===1?f+=C:p+=C);}let x={x:g,y:p,width:y-g,height:f-p},v=[];for(let b of c)o!==0&&v.push(...ee(x,b,"vertical",1)),h!==0&&v.push(...ee(x,b,"horizontal",1));this.context.setSnapGuides(this.mergeGuides(v));let M={};if(t.type==="circle"){let b=Math.max(10,Math.max(o!==0?x.width/2:0,h!==0?x.height/2:0));if(M.radius=b,o!==0){let C=o===1?d.x:d.x+d.width;M.x=o===1?C+b:C-b;}if(h!==0){let C=h===1?d.y:d.y+d.height;M.y=h===1?C+b:C-b;}}else if("width"in t&&o!==0&&(M.width=x.width,M.x=x.x+x.width/2),"height"in t&&h!==0&&(M.height=x.height,M.y=x.y+x.height/2),t.type==="draw"){let b=this.getShapeWorldBounds(t),C=o!==0?x.width/Math.max(1,b.width):1,I=h!==0?x.height/Math.max(1,b.height):1;M.points=t.points.map(H=>({x:H.x*C,y:H.y*I}));}Object.keys(M).length>0&&this.context.updateShape(t.id,M);}mergeGuides(e){let t=new Map;for(let i of e){let n=`${i.type}\0${Math.round(i.position*2)/2}`,s=t.get(n);s?(s.start=Math.min(s.start,i.start),s.end=Math.max(s.end,i.end)):t.set(n,{...i});}return [...t.values()]}handleRotation(e){let t=this.context.getSelectedIds(),i=this.context.getShapes();t.forEach(n=>{let s=i.find(r=>r.id===n);s&&this.context.updateShape(n,{rotation:s.rotation+e*Math.PI/180});});}handleNudge(e,t){let i=this.context.getSelectedIds(),n=this.context.getShapes();i.forEach(s=>{let r=n.find(o=>o.id===s);r&&this.context.updateShape(s,{x:r.x+e,y:r.y+t});});}handleBrushing(e){let t=Math.min(this.brushStart.x,e.x),i=Math.min(this.brushStart.y,e.y),n=Math.abs(e.x-this.brushStart.x),s=Math.abs(e.y-this.brushStart.y);this.context.setBrushRect({x:t,y:i,width:n,height:s});let r=this.context.getShapes(),o=[];for(let h of r){let l=this.getShapeWorldBounds(h);this.rectsIntersect({x:t,y:i,width:n,height:s},l)&&o.push(h.id);}if(this.brushShiftKey){let h=this.context.getSelectedIds(),l=new Set([...h,...o]);this.context.selectShapes([...l]);}else this.context.selectShapes(o);}getShapeWorldBounds(e){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":case "mind-map":return {x:e.x-e.width/2,y:e.y-e.height/2,width:e.width,height:e.height};case "circle":return {x:e.x-e.radius,y:e.y-e.radius,width:e.radius*2,height:e.radius*2};case "line":case "arrow":{if(!e.points||e.points.length===0)return {x:e.x,y:e.y,width:0,height:0};let t=1/0,i=1/0,n=-1/0,s=-1/0;return e.points.forEach(r=>{t=Math.min(t,r.x),i=Math.min(i,r.y),n=Math.max(n,r.x),s=Math.max(s,r.y);}),{x:t,y:i,width:n-t,height:s-i}}case "draw":{let t=e,i=t.width,n=t.height;if(typeof i=="number"&&typeof n=="number"&&i>0&&n>0)return {x:t.x-i/2,y:t.y-n/2,width:i,height:n};if(!t.points||t.points.length===0)return {x:t.x,y:t.y,width:0,height:0};let s=1/0,r=1/0,o=-1/0,h=-1/0;return t.points.forEach(l=>{let c=l.x+t.x,u=l.y+t.y;s=Math.min(s,c),r=Math.min(r,u),o=Math.max(o,c),h=Math.max(h,u);}),{x:s,y:r,width:o-s,height:h-r}}case "text":{let t=e;return {x:e.x-t.width/2,y:e.y-t.height/2,width:t.width,height:t.height}}default:{let t=e;return {x:t.x,y:t.y,width:0,height:0}}}}rectsIntersect(e,t){return e.x<t.x+t.width&&e.x+e.width>t.x&&e.y<t.y+t.height&&e.y+e.height>t.y}hitTest(e,t,i){for(let n=t.length-1;n>=0;n--){let s=t[n];if(i.includes(s.id)){let r=this.getHitHandle(e,s);if(r)return {shape:s,handle:r}}if(this.isPointInShape(e,s))return {shape:s,handle:null}}return {shape:null,handle:null}}getHitHandle(e,t){if(t.type==="mind-map")return null;let i=14,n=this.getShapeBounds(t),s=Math.cos(-t.rotation),r=Math.sin(-t.rotation),o=e.x-t.x,h=e.y-t.y,l=o*s-h*r,c=o*r+h*s,u=[{type:"nw",x:n.x,y:n.y},{type:"ne",x:n.x+n.width,y:n.y},{type:"se",x:n.x+n.width,y:n.y+n.height},{type:"sw",x:n.x,y:n.y+n.height},{type:"rotation",x:n.x+n.width/2,y:n.y-26}];for(let d of u)if(Math.abs(l-d.x)<i&&Math.abs(c-d.y)<i)return d.type;return null}static GEN_HIT_THRESHOLD=14;static CLICK_MOVE_THRESHOLD=6;computeGenPoints(e,t){let i=[];for(let n of t){let s=e.find(r=>r.id===n);if(s&&(s.type==="rectangle"||s.type==="rounded-rectangle"||s.type==="ellipse"||s.type==="diamond"||s.type==="triangle"||s.type==="hexagon"||s.type==="parallelogram"||s.type==="trapezoid"||s.type==="pentagon"||s.type==="octagon"||s.type==="star"||s.type==="uml-class"||s.type==="uml-interface"||s.type==="circle"||s.type==="draw"||s.type==="cylinder"||s.type==="cloud"||s.type==="heart"||s.type==="block-arrow-right"||s.type==="block-arrow-left"||s.type==="block-arrow-up"||s.type==="block-arrow-down"||s.type==="block-arrow-double"||s.type==="right-triangle"||s.type==="cross"||s.type==="speech-rect"||s.type==="speech-round"||s.type==="cube"||s.type==="sector"||s.type==="delay"||s.type==="predefined-process"||s.type==="document"||s.type==="stadium"||s.type==="class-catalog"||s.type==="chevron"||s.type==="brace-left"||s.type==="brace-right")){let r=this.getShapeBounds(s),o={x:s.x,y:s.y};i.push({point:{x:o.x+r.x+r.width/2,y:o.y+r.y},edge:"n",sourceShapeId:s.id}),i.push({point:{x:o.x+r.x+r.width,y:o.y+r.y+r.height/2},edge:"e",sourceShapeId:s.id}),i.push({point:{x:o.x+r.x+r.width/2,y:o.y+r.y+r.height},edge:"s",sourceShapeId:s.id}),i.push({point:{x:o.x+r.x,y:o.y+r.y+r.height/2},edge:"w",sourceShapeId:s.id});}}return i}hitGenPoint(e,t,i){let n=a.GEN_HIT_THRESHOLD,s=null,r=n;for(let o of this.computeGenPoints(t,i)){let h=Math.hypot(o.point.x-e.x,o.point.y-e.y);h<=r&&(r=h,s=o);}return s}genPreviewTarget(e,t,i){if(i)return t;let n=96;if(e.edge)switch(e.edge){case "n":return {x:e.point.x,y:e.point.y-n};case "s":return {x:e.point.x,y:e.point.y+n};case "e":return {x:e.point.x+n,y:e.point.y};case "w":return {x:e.point.x-n,y:e.point.y}}return {x:e.point.x,y:e.point.y-n}}handleGenerating(e,t){if(!this.generatingState)return;let i=this.generatingState,n=i.source;Math.hypot(e.x-i.startPoint.x,e.y-i.startPoint.y)>a.CLICK_MOVE_THRESHOLD&&(i.hasMoved=true);let r=this.findBindingNearPoint(e,t,n.sourceShapeId),o=r?this.bindingPosition(r.toShapeId,r.binding,t):i.hasMoved?e:this.genPreviewTarget(n,e,false);this.context.setConnectingTarget(r?r.toShapeId:null),this.context.setGenerationPreview({from:n.point,to:o,type:r?void 0:this.defaultGenType(n.sourceShapeId)});}handleRotating(e,t){if(!this.rotatingState)return;let i=this.rotatingState,n=Math.atan2(e.y-i.center.y,e.x-i.center.x),s=i.startRotation+(n-i.startAngle);t.shiftKey&&(s=Math.round(s/(Math.PI/12))*(Math.PI/12)),this.context.updateShape(i.shapeId,{rotation:s});}bindingPosition(e,t,i){let n=i.find(r=>r.id===e);if(!n)return {x:0,y:0};let s=this.getShapeWorldBounds(n);if(t.handle)switch(t.handle){case "top":return {x:n.x,y:s.y};case "bottom":return {x:n.x,y:s.y+s.height};case "left":return {x:s.x,y:n.y};case "right":return {x:s.x+s.width,y:n.y}}return t.normalizedAnchor?{x:s.x+s.width*t.normalizedAnchor.x,y:s.y+s.height*t.normalizedAnchor.y}:{x:n.x,y:n.y}}isPointInShape(e,t){let i=e.x-t.x,n=e.y-t.y;switch(t.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":case "mind-map":return Math.abs(i)<=t.width/2&&Math.abs(n)<=t.height/2;case "circle":return i*i+n*n<=t.radius*t.radius;case "hexagon":return this.isPointInHexagon(i,n,t.width,t.height);case "line":case "arrow":for(let s=0;s<t.points.length-1;s++){let r=t.points[s],o=t.points[s+1];if(this.pointToLineDistance(e,r,o)<10)return true}return false;case "draw":{let s=this.getShapeBounds(t);return Math.abs(i)<=s.width/2&&Math.abs(n)<=s.height/2}case "text":return Math.abs(i)<=50&&Math.abs(n)<=t.fontSize/2;default:return false}}isPointInHexagon(e,t,i,n){let s=Math.abs(e),r=Math.abs(t),o=i/2,h=n/2;return s<=o&&r<=h&&s/o+r/h<=1.5}pointToLineDistance(e,t,i){let n=e.x-t.x,s=e.y-t.y,r=i.x-t.x,o=i.y-t.y,h=n*r+s*o,l=r*r+o*o,c=-1;l!==0&&(c=h/l);let u,d;c<0?(u=t.x,d=t.y):c>1?(u=i.x,d=i.y):(u=t.x+c*r,d=t.y+c*o);let g=e.x-u,p=e.y-d;return Math.sqrt(g*g+p*p)}getShapeBounds(e){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":case "mind-map":return {x:-e.width/2,y:-e.height/2,width:e.width,height:e.height};case "circle":return {x:-e.radius,y:-e.radius,width:e.radius*2,height:e.radius*2};case "line":case "arrow":{if(e.points.length===0)return {x:0,y:0,width:0,height:0};let t=1/0,i=1/0,n=-1/0,s=-1/0;return e.points.forEach(r=>{t=Math.min(t,r.x-e.x),i=Math.min(i,r.y-e.y),n=Math.max(n,r.x-e.x),s=Math.max(s,r.y-e.y);}),{x:t,y:i,width:n-t,height:s-i}}case "draw":{let t=e;if(typeof t.width=="number"&&typeof t.height=="number")return {x:-t.width/2,y:-t.height/2,width:t.width,height:t.height};if(t.points.length===0)return {x:0,y:0,width:0,height:0};let i=1/0,n=1/0,s=-1/0,r=-1/0;return t.points.forEach(o=>{let h=o.x-t.x,l=o.y-t.y;i=Math.min(i,h),n=Math.min(n,l),s=Math.max(s,h),r=Math.max(r,l);}),{x:i,y:n,width:s-i,height:r-n}}case "text":return {x:-50,y:-e.fontSize/2,width:100,height:e.fontSize};default:return {x:0,y:0,width:0,height:0}}}hitTestArrowTerminal(e,t){for(let n of t){if(n.type!=="arrow")continue;let s=n;if(s.points.length<2)continue;let r=s.points[0],o=s.points[s.points.length-1],h=Math.sqrt(Math.pow(e.x-r.x,2)+Math.pow(e.y-r.y,2)),l=Math.sqrt(Math.pow(e.x-o.x,2)+Math.pow(e.y-o.y,2));if(h<20)return {shapeId:s.id,terminal:"start"};if(l<20)return {shapeId:s.id,terminal:"end"}}return null}findShapeNearPoint(e,t,i){for(let s=t.length-1;s>=0;s--){let r=t[s];if(i&&r.id===i||r.type==="arrow"||r.type==="line"||r.type==="draw")continue;let o;switch(r.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":o=this.pointToRectDistance(e,{x:r.x-r.width/2,y:r.y-r.height/2,width:r.width,height:r.height});break;case "circle":o=Math.sqrt(Math.pow(e.x-r.x,2)+Math.pow(e.y-r.y,2))-r.radius;break;case "hexagon":{let h=r.width/2,l=r.height/2,c=Math.abs(e.x-r.x),u=Math.abs(e.y-r.y);o=c/h+u/l-1;break}default:continue}if(o<=30)return r}return null}pointToRectDistance(e,t){let i=Math.max(t.x,Math.min(e.x,t.x+t.width)),n=Math.max(t.y,Math.min(e.y,t.y+t.height));return Math.sqrt(Math.pow(e.x-i,2)+Math.pow(e.y-n,2))}static SNAP_THRESHOLD=32;static HANDLE_SNAP_DISTANCE=18;static ANCHOR_EPS=.001;computeSnapGuidesForArrow(e,t,i,n,s){let r=this.draggingTerminal==="start"?n.endBinding:n.startBinding,o=r?s.find(p=>p.id===r.toShapeId):null;if(!o)return [];let h=this.findShapeNearPoint(i,s,n.id);if(!h)return [];let l=this.getShapeWorldBounds(o),c=this.getShapeWorldBounds(h),u=t.x-e.x,d=t.y-e.y,g=Math.abs(u)>=Math.abs(d)?"horizontal":"vertical";return ee(l,c,g)}findBindingNearPoint(e,t,i){let n;for(let s=t.length-1;s>=0;s--){let r=t[s];if(i&&r.id===i||r.type==="arrow"||r.type==="line"||r.type==="draw")continue;let o=this.getPageBounds(r),h=a.SNAP_THRESHOLD;if(e.x<o.x-h||e.x>o.x+o.width+h||e.y<o.y-h||e.y>o.y+o.height+h)continue;let l={x:r.x,y:r.y},c=[{handle:"top",pos:{x:l.x,y:o.y}},{handle:"bottom",pos:{x:l.x,y:o.y+o.height}},{handle:"left",pos:{x:o.x,y:l.y}},{handle:"right",pos:{x:o.x+o.width,y:l.y}}];for(let d of c){let g=Math.hypot(d.pos.x-e.x,d.pos.y-e.y);g<=a.HANDLE_SNAP_DISTANCE&&(!n||g<n.distance)&&(n={toShapeId:r.id,binding:{toShapeId:r.id,handle:d.handle,isPrecise:false},distance:g});}let u=this.nearestPointOnShapeOutline(r,e);if(u&&u.distance<=a.SNAP_THRESHOLD&&(!n||u.distance<n.distance)){let d=this.computeNormalizedAnchor(o,u.point);n={toShapeId:r.id,binding:{toShapeId:r.id,isPrecise:true,normalizedAnchor:d},distance:u.distance};}}if(n)return {toShapeId:n.toShapeId,binding:n.binding}}getPageBounds(e){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":return {x:e.x-e.width/2,y:e.y-e.height/2,width:e.width,height:e.height};case "circle":return {x:e.x-e.radius,y:e.y-e.radius,width:e.radius*2,height:e.radius*2};default:return {x:e.x-50,y:e.y-50,width:100,height:100}}}computeNormalizedAnchor(e,t){let i=a.ANCHOR_EPS,n=e.width>0?(t.x-e.x)/e.width:.5,s=e.height>0?(t.y-e.y)/e.height:.5;return n=Math.max(i,Math.min(1-i,n)),s=Math.max(i,Math.min(1-i,s)),{x:n,y:s}}nearestPointOnShapeOutline(e,t){let i={x:t.x-e.x,y:t.y-e.y},n=this.nearestLocalPointOnShapeOutline(e,i);if(!n)return null;let s={x:n.point.x+e.x,y:n.point.y+e.y};return {point:s,distance:Math.hypot(s.x-t.x,s.y-t.y)}}nearestLocalPointOnShapeOutline(e,t){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":{let i=e.width,n=e.height;return this.nearestPointOnRectOutlineLocal(t,{x:-i/2,y:-n/2,width:i,height:n})}case "circle":{let i=e.radius;return this.nearestPointOnCircleOutlineLocal(t,{x:0,y:0},i)}default:return null}}nearestPointOnRectOutlineLocal(e,t){let i=[[{x:t.x,y:t.y},{x:t.x+t.width,y:t.y}],[{x:t.x+t.width,y:t.y},{x:t.x+t.width,y:t.y+t.height}],[{x:t.x+t.width,y:t.y+t.height},{x:t.x,y:t.y+t.height}],[{x:t.x,y:t.y+t.height},{x:t.x,y:t.y}]],n=null;for(let[s,r]of i){let o=this.nearestPointOnSegment(e,s,r);(!n||o.distance<n.distance)&&(n=o);}return n}nearestPointOnCircleOutlineLocal(e,t,i){let n=e.x-t.x,s=e.y-t.y,r=Math.hypot(n,s);if(r<1e-9)return {point:{x:t.x+i,y:t.y},distance:i};let o=t.x+n/r*i,h=t.y+s/r*i,l={x:o,y:h};return {point:l,distance:Math.hypot(l.x-e.x,l.y-e.y)}}nearestPointOnSegment(e,t,i){let n=i.x-t.x,s=i.y-t.y,r=n*n+s*s,o=r>0?((e.x-t.x)*n+(e.y-t.y)*s)/r:0;o=Math.max(0,Math.min(1,o));let h=t.x+o*n,l=t.y+o*s,c={x:h,y:l};return {point:c,distance:Math.hypot(c.x-e.x,c.y-e.y)}}};var we=class a extends N{type="hand";state="idle";startScreenPoint={x:0,y:0};initialCameraPos={x:0,y:0};static DRAG_THRESHOLD=4;onPointerDown(e,t){this.startScreenPoint={x:t.clientX,y:t.clientY};let i=this.context.getCamera();this.initialCameraPos={x:i.x,y:i.y},this.state="pointing";}onPointerMove(e,t){if(this.state!=="idle"){if(this.state==="pointing"){let i=t.clientX-this.startScreenPoint.x,n=t.clientY-this.startScreenPoint.y;if(Math.hypot(i,n)<a.DRAG_THRESHOLD)return;this.state="dragging",this.applyCameraDelta(i,n);return}if(this.state==="dragging"){let i=t.clientX-this.startScreenPoint.x,n=t.clientY-this.startScreenPoint.y;this.applyCameraDelta(i,n);}}}onPointerUp(e,t){if(this.state!=="idle"){if(this.state==="dragging"){let i=this.context.getCamera();this.initialCameraPos={x:i.x,y:i.y},this.startScreenPoint={x:t.clientX,y:t.clientY};}this.state="idle";}}onKeyDown(e){e.key==="Escape"&&this.state!=="idle"&&(this.context.setCameraPosition(this.initialCameraPos.x,this.initialCameraPos.y),this.state="idle");}onKeyUp(e){}getCursor(e){return this.state==="dragging"||this.state==="pointing"?"grabbing":"grab"}applyCameraDelta(e,t){this.context.setCameraPosition(this.initialCameraPos.x+e,this.initialCameraPos.y+t);}};var sn={strokeColor:"#1f2937",fillColor:"transparent",strokeWidth:2,strokeDash:null},be=class extends N{type="line";isDrawing=false;startPoint={x:0,y:0};currentShape=null;onPointerDown(e,t){this.isDrawing=true,this.startPoint=e,this.currentShape={id:`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,type:"line",x:e.x,y:e.y,points:[e],rotation:0,style:{...sn}},this.context.commitHistory(),this.context.addShape(this.currentShape);}onPointerMove(e,t){!this.isDrawing||!this.currentShape||this.context.updateShape(this.currentShape.id,{points:[this.startPoint,e]});}onPointerUp(e,t){this.isDrawing=false,this.currentShape=null,this.context.setActiveTool("select");}onKeyDown(e){}onKeyUp(e){}getCursor(){return "crosshair"}};var rn={strokeColor:"#1f2937",fillColor:"transparent",fillOpacity:1,strokeWidth:4,strokeDash:null},on=.5,an=16,Oe="#ffeb3b",hn=4200,te=class extends N{type;mode;isDrawing=false;points=[];currentShapeId=null;committed=false;fadeTimer=null;highlighterColor=Oe;constructor(e,t){super(e),this.mode=t,this.type=t==="pen"?"pen":"highlighter";}setHighlighterColor(e){this.highlighterColor=e;}getHighlighterColor(){return this.highlighterColor}buildStyle(){return this.mode==="pen"?{...rn}:{strokeColor:F(this.highlighterColor,on),fillColor:"transparent",fillOpacity:1,strokeWidth:an,strokeDash:null}}onPointerDown(e,t){this.isDrawing=true,this.committed=false,this.currentShapeId=null,this.points=[{...e}];}buildGeometry(e){let t=1/0,i=1/0,n=-1/0,s=-1/0;for(let d of e)d.x<t&&(t=d.x),d.y<i&&(i=d.y),d.x>n&&(n=d.x),d.y>s&&(s=d.y);let r=(t+n)/2,o=(i+s)/2,h=6,l=Math.max(1,n-t)+h*2,c=Math.max(1,s-i)+h*2,u=e.map(d=>({x:d.x-r,y:d.y-o}));return {x:r,y:o,width:l,height:c,points:u}}onPointerMove(e,t){if(this.isDrawing){if(this.points.push({...e}),!this.committed&&this.points.length>=2){this.context.commitHistory();let i=`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`;this.currentShapeId=i;let n=this.buildGeometry(this.points),s=this.buildStyle(),r={id:i,type:"draw",x:n.x,y:n.y,width:n.width,height:n.height,points:n.points,rotation:0,style:s};this.context.addShape(r),this.committed=true;}else if(this.committed&&this.currentShapeId){let i=this.buildGeometry(this.points);this.context.updateShape(this.currentShapeId,{x:i.x,y:i.y,width:i.width,height:i.height,points:i.points});}}}onPointerUp(e,t){if(!this.isDrawing||(this.isDrawing=false,!this.committed||!this.currentShapeId))return;let i=this.currentShapeId;this.currentShapeId=null,this.mode==="highlighter"&&(this.fadeTimer=setTimeout(()=>{this.context.deleteShape(i);},hn));}onKeyDown(e){}onKeyUp(e){}getCursor(){return "crosshair"}dispose(){this.fadeTimer&&(clearTimeout(this.fadeTimer),this.fadeTimer=null);}};var ln={strokeColor:"#1f2937",fillColor:"transparent",strokeWidth:2,strokeDash:null},cn=4,Se=class extends N{type="arrow";isDrawing=false;startPoint={x:0,y:0};currentShapeId=null;startBindingInfo=null;startBinding;onPointerDown(e,t){this.isDrawing=true,this.startPoint=e,this.startBindingInfo=null,this.currentShapeId=null;let i=this.context.getShapes(),n=this.findShapeBinding(e,i),s=e;n&&(s=this.getBindingPosition(n.toShapeId,n.handle,i),this.startBindingInfo={...n,position:s}),this.startPoint=s,this.startBinding=n?{toShapeId:n.toShapeId,handle:n.handle}:void 0;}onPointerMove(e,t){if(!this.isDrawing)return;let i=this.context.getShapes(),n=this.findShapeBinding(e,i),s=e;n&&(s=this.getBindingPosition(n.toShapeId,n.handle,i));let r=this.startPoint;if(!this.currentShapeId){if(Math.hypot(s.x-r.x,s.y-r.y)<cn)return;let l=`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,c=this.context.getArrowStyle(),u={id:l,type:"arrow",x:r.x,y:r.y,points:[r,s],rotation:0,style:{...ln},startBinding:this.startBinding,connectionType:c.connectionType,arrowHead:c.arrowHead,dashed:c.dashed};this.context.commitHistory(),this.context.addShape(u),this.currentShapeId=l;}let o=this.computeSnapGuides(r,s,n,i);this.context.setSnapGuides(o),this.context.updateShape(this.currentShapeId,{points:[r,s],endBinding:n?{toShapeId:n.toShapeId,handle:n.handle,auto:true}:void 0});}onPointerUp(e,t){let i=!!this.currentShapeId;this.isDrawing=false,this.currentShapeId=null,this.startBindingInfo=null,this.startBinding=void 0,this.context.setSnapGuides([]),i&&this.context.setActiveTool("select");}onKeyDown(e){}onKeyUp(e){}getCursor(){return "crosshair"}computeSnapGuides(e,t,i,n){let s=this.startBindingInfo?n.find(d=>d.id===this.startBindingInfo.toShapeId):null,r=i?n.find(d=>d.id===i.toShapeId):null;if(!s||!r)return [];let o=this.getShapeBounds(s),h=this.getShapeBounds(r),l=t.x-e.x,c=t.y-e.y,u=Math.abs(l)>=Math.abs(c)?"horizontal":"vertical";return ee(o,h,u)}getShapeBounds(e){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":return {x:e.x-e.width/2,y:e.y-e.height/2,width:e.width,height:e.height};case "circle":return {x:e.x-e.radius,y:e.y-e.radius,width:e.radius*2,height:e.radius*2};case "text":return {x:e.x-e.width/2,y:e.y-e.height/2,width:e.width,height:e.height};default:return {x:e.x-50,y:e.y-20,width:100,height:40}}}findShapeBinding(e,t){for(let n=t.length-1;n>=0;n--){let s=t[n];if(s.type==="arrow"||s.type==="line"||s.type==="draw")continue;let r;switch(s.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "uml-class":case "uml-interface":r=this.pointToRectDistance(e,{x:s.x-s.width/2,y:s.y-s.height/2,width:s.width,height:s.height});break;case "circle":{let o=Math.sqrt(Math.pow(e.x-s.x,2)+Math.pow(e.y-s.y,2));r=Math.abs(o-s.radius);break}case "hexagon":{let o=s.width/2,h=s.height/2,l=Math.abs(e.x-s.x),c=Math.abs(e.y-s.y);r=l/o+c/h-1;break}default:continue}if(r<=40){let o=this.getClosestHandle(e,s);return {toShapeId:s.id,handle:o}}}}pointToRectDistance(e,t){let i=Math.max(t.x,Math.min(e.x,t.x+t.width)),n=Math.max(t.y,Math.min(e.y,t.y+t.height));return Math.sqrt(Math.pow(e.x-i,2)+Math.pow(e.y-n,2))}getClosestHandle(e,t){let i=e.x-t.x,n=e.y-t.y;if(t.type==="circle"||t.type==="hexagon"){let l=Math.atan2(n,i);return l>=-Math.PI/4&&l<Math.PI/4?"right":l>=Math.PI/4&&l<3*Math.PI/4?"bottom":l>=3*Math.PI/4||l<-(3*Math.PI)/4?"left":"top"}let s="width"in t?t.width:100,r="height"in t?t.height:100,o=Math.abs(i)/(s/2),h=Math.abs(n)/(r/2);return o>h?i>0?"right":"left":n>0?"bottom":"top"}getBindingPosition(e,t,i){let n=i.find(s=>s.id===e);if(!n)return {x:0,y:0};switch(n.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":return this.getRectBindingPosition(n,t);case "circle":return this.getCircleBindingPosition(n,t);case "hexagon":return this.getRectBindingPosition(n,t);default:return {x:n.x,y:n.y}}}getRectBindingPosition(e,t){switch(t){case "top":return {x:e.x,y:e.y-e.height/2};case "bottom":return {x:e.x,y:e.y+e.height/2};case "left":return {x:e.x-e.width/2,y:e.y};case "right":return {x:e.x+e.width/2,y:e.y}}}getCircleBindingPosition(e,t){switch(t){case "top":return {x:e.x,y:e.y-e.radius};case "bottom":return {x:e.x,y:e.y+e.radius};case "left":return {x:e.x-e.radius,y:e.y};case "right":return {x:e.x+e.radius,y:e.y}}}};var dn={strokeColor:"transparent",fillColor:"transparent",strokeWidth:0,strokeDash:null},Ue=class extends N{type="text";onPointerDown(e,t){let i=`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,n={id:i,type:"text",x:e.x,y:e.y,rotation:0,style:{...dn},text:"",fontSize:16,textColor:"#1f2937",width:200,height:24};this.context.commitHistory(),this.context.addShape(n),this.context.enterEditing(i);}onPointerMove(e,t){}onPointerUp(e,t){}onKeyDown(e){}onKeyUp(e){}getCursor(){return "text"}};var ge="#1f2937";function et(a){switch(a.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":case "mind-map":case "text":return {x:-(a.width/2),y:-(a.height/2),width:a.width,height:a.height};case "circle":return {x:-a.radius,y:-a.radius,width:a.radius*2,height:a.radius*2};case "line":case "arrow":{let e=a.points;if(!e||e.length===0)return {x:0,y:0,width:0,height:0};let t=1/0,i=1/0,n=-1/0,s=-1/0;for(let r of e){let o=r.x-a.x,h=r.y-a.y;t=Math.min(t,o),i=Math.min(i,h),n=Math.max(n,o),s=Math.max(s,h);}return {x:t,y:i,width:n-t,height:s-i}}case "draw":{let e=a,t=e.width,i=e.height;if(typeof t=="number"&&typeof i=="number")return {x:-t/2,y:-i/2,width:t,height:i};let n=e.points;if(!n||n.length===0)return {x:0,y:0,width:0,height:0};let s=1/0,r=1/0,o=-1/0,h=-1/0;for(let l of n)s=Math.min(s,l.x),r=Math.min(r,l.y),o=Math.max(o,l.x),h=Math.max(h,l.y);return {x:s,y:r,width:o-s,height:h-r}}default:return {x:0,y:0,width:0,height:0}}}function un(a){let e=et(a),t=Math.cos(a.rotation),i=Math.sin(a.rotation),n=1/0,s=1/0,r=-1/0,o=-1/0,h=[[e.x,e.y],[e.x+e.width,e.y],[e.x+e.width,e.y+e.height],[e.x,e.y+e.height]];for(let[l,c]of h){let u=l*t-c*i+a.x,d=l*i+c*t+a.y;n=Math.min(n,u),s=Math.min(s,d),r=Math.max(r,u),o=Math.max(o,d);}return {x:n,y:s,width:r-n,height:o-s}}function Pe(a,e=24){if(!a||a.length===0)return {x:0,y:0,width:400,height:300};let t=1/0,i=1/0,n=-1/0,s=-1/0;for(let r of a){let o=un(r);t=Math.min(t,o.x),i=Math.min(i,o.y),n=Math.max(n,o.x+o.width),s=Math.max(s,o.y+o.height);}return {x:t-e,y:i-e,width:n-t+e*2,height:s-i+e*2}}function vt(a,e={}){let t=e.padding??24,i=e.scale??2,n=e.background===void 0?"#ffffff":e.background??null,s=Pe(a,t),r=Math.max(1,Math.ceil(s.width)),o=Math.max(1,Math.ceil(s.height)),h=document.createElement("canvas");h.width=Math.ceil(r*i),h.height=Math.ceil(o*i);let l=new oe(h,i);l.resize(r*i,o*i);let c=new se;return c.zoom=1,c.x=-s.x,c.y=-s.y,l.render(a,c,new Set,void 0,void 0,void 0,void 0,void 0,void 0,void 0,n),h}function gn(a,e,t){return new Promise((i,n)=>{a.toBlob(s=>{s?i(s):n(new Error("canvas.toBlob failed"));},e,t);})}function pn(a,e,t){return a.toDataURL(e,t)}var Me=null;function ie(a,e){return Me||(Me=document.createElement("canvas").getContext("2d")),Me?(Me.font=e,Me.measureText(a).width):a.length*8}function pe(a){return a.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")}function tt(a,e,t,i){let n=e*1.3,s=Math.max(1,Math.floor(i/n)),r=`${e}px sans-serif`,o=a.split(`
|
|
3
|
+
`),h=[];for(let c of o){let u="";for(let d of c){let g=u+d;ie(g,r)>t&&u?(h.push(u),u=d):u=g;}u&&h.push(u);}let l=h.slice(0,s);if(h.length>s&&l.length>0){let c=l[l.length-1],u="...";for(;c.length>0&&ie(c+u,r)>t;)c=c.slice(0,-1);l[l.length-1]=c+u;}return l}function nt(a,e,t){if(ie(a,t)<=e)return a;let i="\u2026",n=0,s=a.length;for(;n<s;){let r=n+s+1>>1;ie(a.slice(0,r)+i,t)<=e?n=r:s=r-1;}return a.slice(0,n)+i}function ne(a){return a.map(([e,t])=>`${m(e)},${m(t)}`).join(" ")}function xt(a){return a.map(e=>e[0]==="Z"?"Z":e[0]==="M"||e[0]==="L"?`${e[0]} ${m(e[1])} ${m(e[2])}`:`C ${m(e[1])} ${m(e[2])} ${m(e[3])} ${m(e[4])} ${m(e[5])} ${m(e[6])}`).join(" ")}function yn(a){let e=a;if(!e.text||a.type==="text"||a.type==="line"||a.type==="arrow"||a.type==="draw")return "";let t=e.fontSize||16,i=a.type==="brace-left"||a.type==="brace-right",n=e.width??(e.radius?e.radius*2:100),s=e.height??(e.radius?e.radius*2:40),r=n*(i?1-me:1)*.85,o=s*.85,h=tt(String(e.text),t,r,o);return st(h,t,e.textColor||ge,m(Be(a)))}function m(a){return Math.round(a*100)/100}function Pt(a){return a.strokeDash&&a.strokeDash.length?a.strokeDash.join(","):void 0}function fn(a,e){let t=e.x-a.x,i=e.y-a.y,n=Math.cos(a.rotation),s=Math.sin(a.rotation);return {x:t*n-i*s+a.x,y:t*s+i*n+a.y}}var it={top:{x:0,y:-1},bottom:{x:0,y:1},left:{x:-1,y:0},right:{x:1,y:0}};function mn(a,e,t){let i=e.x-a.x,n=e.y-a.y;return t&&t.x!==0?i>=0?"left":"right":t&&t.y!==0?n>=0?"top":"bottom":Math.abs(i)>=Math.abs(n)?i>=0?"left":"right":n>=0?"top":"bottom"}function xn(a,e,t){let i=a.endBinding;if(!i||!i.auto||i.isPrecise)return i;let n=a.startBinding?.handle?it[a.startBinding.handle]:void 0;return {...i,handle:mn(e,{x:t.x,y:t.y},n)}}function wt(a,e){if(e.isPrecise&&e.normalizedAnchor){let t=et(a),i=a.x+t.x+t.width*e.normalizedAnchor.x,n=a.y+t.y+t.height*e.normalizedAnchor.y;return {x:i,y:n}}if(e.handle){let t=et(a);switch(e.handle){case "top":return {x:a.x,y:a.y+t.y};case "bottom":return {x:a.x,y:a.y+t.y+t.height};case "left":return {x:a.x+t.x,y:a.y};case "right":return {x:a.x+t.x+t.width,y:a.y}}}return {x:a.x,y:a.y}}function wn(a,e){let t=a.points.map(s=>({...s})),i,n;if(t.length===0)return {pts:t,fromN:i,toN:n};if(a.startBinding){let s=e.get(a.startBinding.toShapeId);s&&(t[0]=wt(s,a.startBinding),!a.startBinding.isPrecise&&a.startBinding.handle&&(i=it[a.startBinding.handle]));}if(a.endBinding){let s=e.get(a.endBinding.toShapeId);if(s){let r=xn(a,t[0],s)??a.endBinding;t[t.length-1]=wt(s,r),!r.isPrecise&&r.handle&&(n=it[r.handle]);}}return {pts:t.map(s=>fn(a,s)),fromN:i,toN:n}}function bn(a){if(a.length<2)return a;let e=a[0],t=a[a.length-1],i=t.x-e.x,n=t.y-e.y,s=8;if(Math.abs(n)<=s){let r=(e.y+t.y)/2;return a.map(o=>({x:o.x,y:r}))}if(Math.abs(i)<=s){let r=(e.x+t.x)/2;return a.map(o=>({x:r,y:o.y}))}return a}function Sn(a){if(a.length<2)return a;let e=[a[0]];for(let t=1;t<a.length;t++){let i=a[t-1],n=a[t],s=(i.x+n.x)/2,r=(i.y+n.y)/2;Math.abs(n.x-i.x)>=Math.abs(n.y-i.y)?(e.push({x:s,y:i.y}),e.push({x:s,y:n.y})):(e.push({x:i.x,y:r}),e.push({x:n.x,y:r})),e.push(n);}return e}function Mn(a){if(a.length<2)return "";let e=`M ${m(a[0].x)} ${m(a[0].y)}`,t=a[0];for(let i=1;i<a.length;i++){let n=a[i],s=n.x-t.x,r=n.y-t.y,o=Math.hypot(s,r),h={x:(t.x+n.x)/2,y:(t.y+n.y)/2};if(o>=1){let l=-r/o,c=s/o,u=Math.min(o*.2,30);h={x:h.x+l*u,y:h.y+c*u};}e+=` Q ${m(h.x)} ${m(h.y)} ${m(n.x)} ${m(n.y)}`,t=n;}return e}var vn=24;function Pn(a,e,t,i){let n=vn,s=t?{x:a.x+t.x*n,y:a.y+t.y*n}:a,r=i?{x:e.x+i.x*n,y:e.y+i.y*n}:e,o=[a];t&&o.push(s);let h=r.x-s.x,l=r.y-s.y;if(Math.abs(h)<1||Math.abs(l)<1)o.push(r);else {let c=s.x+h/2,u=s.y+l/2,d=Math.max(s.x,Math.min(c,r.x)),g=Math.max(s.y,Math.min(u,r.y)),y=t&&i&&t.x*i.x+t.y*i.y<0?Math.abs(t.x)>Math.abs(t.y)?"H":"V":void 0;y==="H"?o.push({x:d,y:s.y},{x:d,y:r.y},r):y==="V"?o.push({x:s.x,y:g},{x:r.x,y:g},r):Math.abs(h)>=Math.abs(l)?o.push({x:c,y:s.y},{x:c,y:r.y},r):o.push({x:s.x,y:u},{x:r.x,y:u},r);}return i&&o.push(e),o}function Cn(a){let e=[];for(let t of a){let i=e.length;if(!(i>0&&Math.abs(e[i-1].x-t.x)<1e-6&&Math.abs(e[i-1].y-t.y)<1e-6)){if(i>=2){let n=e[i-2],s=e[i-1];if(Math.abs(n.y-s.y)<1e-6&&Math.abs(s.y-t.y)<1e-6||Math.abs(n.x-s.x)<1e-6&&Math.abs(s.x-t.x)<1e-6){e[i-1]=t;continue}}e.push(t);}}return e}function Tn(a,e,t,i){if(e==="curve")return Mn(a);let n;if(e==="straight"?n=bn(a):a.length===2?n=Cn(Pn(a[0],a[1],t,i)):n=Sn(a),n.length<2)return "";let s=`M ${m(n[0].x)} ${m(n[0].y)}`;for(let r=1;r<n.length;r++)s+=` L ${m(n[r].x)} ${m(n[r].y)}`;return s}function Je(a,e,t,i){let n=Math.atan2(e.y-a.y,e.x-a.x),s=12,r={x:e.x-s*Math.cos(n-Math.PI/6),y:e.y-s*Math.sin(n-Math.PI/6)},o={x:e.x-s*Math.cos(n+Math.PI/6),y:e.y-s*Math.sin(n+Math.PI/6)};return `<path d="M ${m(e.x)} ${m(e.y)} L ${m(r.x)} ${m(r.y)} M ${m(e.x)} ${m(e.y)} L ${m(o.x)} ${m(o.y)}" stroke="${t}" stroke-width="${i}" stroke-linecap="round" fill="none" />`}function bt(a,e,t,i){let n=Math.atan2(e.y-a.y,e.x-a.x),s=14,r=e.x,o=e.y,h=e.x-s*Math.cos(n),l=e.y-s*Math.sin(n),c=h+s*.5*Math.cos(n-Math.PI/2),u=l+s*.5*Math.sin(n-Math.PI/2),d=h+s*.5*Math.cos(n+Math.PI/2),g=l+s*.5*Math.sin(n+Math.PI/2);return `<polygon points="${m(r)},${m(o)} ${m(c)},${m(u)} ${m(d)},${m(g)}" fill="#ffffff" stroke="${t}" stroke-width="${i}" />`}function St(a,e,t,i,n){let s=Math.atan2(e.y-a.y,e.x-a.x),r=14,o=e.x,h=e.y,l=e.x-r*.5*Math.cos(s),c=e.y-r*.5*Math.sin(s),u=e.x-r*Math.cos(s),d=e.y-r*Math.sin(s),g=l+r*.4*Math.cos(s-Math.PI/2),p=c+r*.4*Math.sin(s-Math.PI/2),y=l+r*.4*Math.cos(s+Math.PI/2),f=c+r*.4*Math.sin(s+Math.PI/2);return `<polygon points="${m(o)},${m(h)} ${m(g)},${m(p)} ${m(u)},${m(d)} ${m(y)},${m(f)}" fill="${n?t:"#ffffff"}" stroke="${t}" stroke-width="${i}" />`}function In(a,e,t,i){if(e==="none"||a.length<2)return "";let n="";return (e==="start"||e==="both")&&(n+=Je(a[1],a[0],t,i)),(e==="end"||e==="both")&&(n+=Je(a[a.length-2],a[a.length-1],t,i)),e==="inheritance"&&(n+=bt(a[a.length-2],a[a.length-1],t,i)),e==="aggregation"&&(n+=St(a[1],a[0],t,i,false)),e==="composition"&&(n+=St(a[1],a[0],t,i,true)),e==="dependency"&&(n+=Je(a[a.length-2],a[a.length-1],t,i)),e==="realization"&&(n+=bt(a[a.length-2],a[a.length-1],t,i)),n}function st(a,e,t,i=0,n=0){if(a.length===0)return "";let s=e*1.3,o=-(a.length*s)/2+s/2,h=a.map((l,c)=>`<tspan x="${m(i)}" y="${m(n+o+c*s)}">${pe(l)}</tspan>`).join("");return `<text text-anchor="middle" dominant-baseline="central" font-family="sans-serif" font-size="${e}" fill="${t}">${h}</text>`}function _(a,e){let t=Pt(a),i=e&&a.fillColor!=="transparent",n=a.fillOpacity??1,s=a.strokeOpacity??1;return [i?`fill="${a.fillColor}"`:'fill="none"',i&&n<1?`fill-opacity="${m(n)}"`:"",`stroke="${a.strokeColor}"`,s<1&&a.strokeColor!=="transparent"?`stroke-opacity="${m(s)}"`:"",`stroke-width="${a.strokeWidth}"`,'stroke-linecap="round"','stroke-linejoin="round"',t?`stroke-dasharray="${t}"`:""].filter(Boolean).join(" ")}function Mt(a,e){let{width:t,height:i}=a,n=t/2,s=i/2,r=`${G}px sans-serif`,o=j,h=e?0:a.attributes.length>0?a.attributes.length*$+E*2:E*2,l=t-O*2,c=[];c.push(`<rect x="${m(-n)}" y="${m(-s)}" width="${t}" height="${i}" ${_(a.style,true)} />`),c.push(`<line x1="${m(-n)}" y1="${m(-s+o)}" x2="${m(n)}" y2="${m(-s+o)}" stroke="${a.style.strokeColor}" stroke-width="${a.style.strokeWidth}" />`),e||c.push(`<line x1="${m(-n)}" y1="${m(-s+o+h)}" x2="${m(n)}" y2="${m(-s+o+h)}" stroke="${a.style.strokeColor}" stroke-width="${a.style.strokeWidth}" />`);let u=e?`<<${a.className||"Interface"}>>`:a.className;c.push(`<text text-anchor="middle" dominant-baseline="central" font-family="sans-serif" font-weight="bold" font-size="${G}" fill="${a.textColor||ge}" x="0" y="${m(-s+o/2)}">${pe(nt(u,l,r))}</text>`);let d=(p,y,f,w,x)=>{let v=`${f||G}px sans-serif`,M=nt(p,l,v),b=`<text text-anchor="start" dominant-baseline="central" font-family="sans-serif" font-size="${f||G}" fill="${w||ge}" x="${m(-n+O)}" y="${m(y)}">${pe(M)}</text>`;if(x){let C=ie(M,v);b+=`<line x1="${m(-n+O)}" y1="${m(y)}" x2="${m(-n+O+C)}" y2="${m(y)}" stroke="${w||ge}" stroke-width="1" />`;}return b};if(!e){let p=-s+o+E+$/2;a.attributes.forEach((y,f)=>{c.push(d(`${y.visibility} ${y.name}: ${y.type}`,p+f*$,y.fontSize||a.fontSize||0,y.textColor||a.textColor||"",!!y.strikethrough));});}let g=-s+o+h+E+$/2;return a.methods.forEach((p,y)=>{c.push(d(`${p.visibility} ${p.name}(${p.params}): ${p.returnType}`,g+y*$,p.fontSize||a.fontSize||0,p.textColor||a.textColor||"",!!p.strikethrough));}),c.join("")}function Ln(a){let t=B(a.root).positions,i=[],n=r=>{if(r.collapsed)return;let o=t.get(r.id);if(o)for(let h of r.children??[]){let l=t.get(h.id);if(!l)continue;let c=re(h,l.depth),u=a.x+o.x+o.width/2,d=a.y+o.y,g=a.x+l.x-l.width/2,p=a.y+l.y,y=(u+g)/2;i.push(`<path d="M ${m(u)} ${m(d)} C ${m(y)} ${m(d)} ${m(y)} ${m(p)} ${m(g)} ${m(p)}" stroke="${c}" stroke-width="2" fill="none" stroke-linecap="round" />`),n(h);}};n(a.root);let s=r=>{let o=t.get(r.id);if(!o)return;let h=re(r,o.depth),l=o.width/2,c=o.height/2,u=a.x+o.x,d=a.y+o.y;if(i.push(`<rect x="${m(u-l)}" y="${m(d-c)}" width="${o.width}" height="${o.height}" rx="6" ry="6" fill="#ffffff" stroke="${h}" stroke-width="2" />`),r.note){let v="11px sans-serif",M=r.noteColor??h,b=Math.min(ie(r.note,v)+12,130),C=16,I=ie(r.note,v)+12>130?r.note.slice(0,12)+"\u2026":r.note;i.push(`<rect x="${m(u-l)}" y="${m(d-c-C+2)}" width="${b}" height="${C}" rx="4" ry="4" fill="${M}" />`),i.push(`<text text-anchor="start" dominant-baseline="central" font-family="sans-serif" font-size="11" fill="#ffffff" x="${m(u-l+6)}" y="${m(d-c-C/2+2)}">${pe(I)}</text>`);}let g=r.icon?18:0,p=o.width-g-(r.icon?8:0)-16,y=nt(r.text||"",p,`${14}px sans-serif`),f=ie(y,`${14}px sans-serif`),w=g+(g?8:0)+f,x=u-w/2;if(r.icon&&(i.push(`<text text-anchor="start" dominant-baseline="central" font-family="sans-serif" font-size="16" fill="#1f2937" x="${m(x)}" y="${m(d)}">${pe(r.icon)}</text>`),x+=g+8),i.push(`<text text-anchor="start" dominant-baseline="central" font-family="sans-serif" font-size="${14}" fill="#1f2937" x="${m(x)}" y="${m(d)}">${pe(y)}</text>`),r.children&&r.children.length>0&&!r.collapsed)for(let v of r.children)s(v);};return s(a.root),i.join("")}function kn(a,e){let t=a.rotation*180/Math.PI,i=`<g transform="translate(${m(a.x)} ${m(a.y)}) rotate(${m(t)})">`,n=`${yn(a)}</g>`,s=a.style;switch(a.type){case "rectangle":{let r=a.width,o=a.height;return `${i}<rect x="${m(-r/2)}" y="${m(-o/2)}" width="${r}" height="${o}" ${_(s,true)} />${n}`}case "rounded-rectangle":{let r=a.width,o=a.height,h=a.borderRadius||8;return `${i}<rect x="${m(-r/2)}" y="${m(-o/2)}" width="${r}" height="${o}" rx="${h}" ry="${h}" ${_(s,true)} />${n}`}case "stadium":{let r=a.width,o=a.height,h=Math.min(r,o)/2;return `${i}<rect x="${m(-r/2)}" y="${m(-o/2)}" width="${r}" height="${o}" rx="${h}" ry="${h}" ${_(s,true)} />${n}`}case "circle":{let r=a.radius;return `${i}<circle cx="0" cy="0" r="${r}" ${_(s,true)} />${n}`}case "ellipse":{let r=a.width,o=a.height;return `${i}<ellipse cx="0" cy="0" rx="${r/2}" ry="${o/2}" ${_(s,true)} />${n}`}case "diamond":{let r=a.width,o=a.height;return `${i}<polygon points="${ne([[0,-o/2],[r/2,0],[0,o/2],[-r/2,0]])}" ${_(s,true)} />${n}`}case "triangle":{let r=a.width,o=a.height;return `${i}<polygon points="${ne([[0,-o/2],[r/2,o/2],[-r/2,o/2]])}" ${_(s,true)} />${n}`}case "hexagon":{let r=a.width,o=a.height;return `${i}<polygon points="${ne(Qe(r,o,6,-Math.PI/2))}" ${_(s,true)} />${n}`}case "pentagon":{let r=a.width,o=a.height;return `${i}<polygon points="${ne(Qe(r,o,5,-Math.PI/2))}" ${_(s,true)} />${n}`}case "octagon":{let r=a.width,o=a.height;return `${i}<polygon points="${ne(Qe(r,o,8,-Math.PI/8))}" ${_(s,true)} />${n}`}case "star":{let r=a.width,o=a.height;return `${i}<polygon points="${ne(Dn(r,o))}" ${_(s,true)} />${n}`}case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "class-catalog":case "chevron":case "brace-left":case "brace-right":{let r=a.width,o=a.height,h=_e(a.type);if(!h)return "";let l=`<path d="${xt(ue(h.outline,r,o))}" ${_(s,!h.strokeOnly)} />`,c=(h.seams??[]).map(u=>`<path d="${xt(ue(u,r,o))}" ${_({...s,fillColor:"transparent"},true)} />`).join("");return `${i}${l}${c}${n}`}case "parallelogram":{let r=a.width,o=a.height,h=r*.2;return `${i}<polygon points="${ne([[-r/2+h,-o/2],[r/2,-o/2],[r/2-h,o/2],[-r/2,o/2]])}" ${_(s,true)} />${n}`}case "trapezoid":{let r=a.width,o=a.height,h=r*.15;return `${i}<polygon points="${ne([[-r/2+h,-o/2],[r/2-h,-o/2],[r/2,o/2],[-r/2,o/2]])}" ${_(s,true)} />${n}`}case "uml-class":return `${i}${Mt(a,false)}${n}`;case "uml-interface":return `${i}${Mt(a,true)}${n}`;case "mind-map":return Ln(a);case "line":case "arrow":{let r=a,o=r.connectionType||"straight",{pts:h,fromN:l,toN:c}=wn(r,e);if(h.length<2)return "";let u=r.style.strokeColor,d=r.style.strokeWidth,p=r.type==="arrow"&&(!!r.dashed||r.arrowHead==="dependency"||r.arrowHead==="realization")?"8,5":Pt(r.style),y=`<path d="${Tn(h,o,l,c)}" fill="none" stroke="${u}" stroke-width="${d}" stroke-linecap="round" stroke-linejoin="round"${p?` stroke-dasharray="${p}"`:""} />`,f=In(h,r.arrowHead||"end",u,d);return `${y}${f}`}case "draw":{let r=a,o=r.points;if(!o||o.length<2)return "";let h=Math.cos(r.rotation||0),l=Math.sin(r.rotation||0),c=o.map(d=>({x:d.x*h-d.y*l+r.x,y:d.x*l+d.y*h+r.y}));if(c.length<2)return "";let u=`M ${m(c[0].x)} ${m(c[0].y)}`;for(let d=1;d<c.length-1;d++){let g=(c[d].x+c[d+1].x)/2,p=(c[d].y+c[d+1].y)/2;u+=` Q ${m(c[d].x)} ${m(c[d].y)} ${m(g)} ${m(p)}`;}return u+=` L ${m(c[c.length-1].x)} ${m(c[c.length-1].y)}`,`<path d="${u}" fill="none" stroke="${s.strokeColor}" stroke-width="${s.strokeWidth}" stroke-linecap="round" stroke-linejoin="round" />`}case "text":{let r=a,o=r.fontSize||16,h=r.textColor||ge,l=tt(r.text||"",o,r.width,r.height),c="";if(r.highlightColor&&r.highlightColor!=="transparent"){let u=o*.15,d=typeof r.highlightOpacity=="number"?r.highlightOpacity:1,g=d<1?` fill-opacity="${m(d)}"`:"";c+=`<rect x="${m(-r.width/2-u)}" y="${m(-(l.length*o*1.3)/2-u)}" width="${r.width+u*2}" height="${l.length*o*1.3+u*2}" rx="4" fill="${r.highlightColor}"${g} />`;}return c+=st(l,o,h),`${i}${c}${n}`}default:{let r=a,o=r.width||100,h=r.height||40,l="";if(r.text){let c=r.fontSize||16,u=tt(r.text,c,o*.85,h*.85);l+=st(u,c,r.textColor||ge);}return `${i}<rect x="${m(-o/2)}" y="${m(-h/2)}" width="${o}" height="${h}" ${_(s,true)} />${l}${n}`}}}function Qe(a,e,t,i){let n=a/2,s=e/2,r=[];for(let o=0;o<t;o++){let h=o*2*Math.PI/t+i;r.push([n*Math.cos(h),s*Math.sin(h)]);}return r}function Dn(a,e){let t=a/2,i=e/2,n=5,s=.4,r=[];for(let o=0;o<n*2;o++){let h=o*Math.PI/n-Math.PI/2,l=o%2===0?1:s;r.push([t*l*Math.cos(h),i*l*Math.sin(h)]);}return r}function Ct(a,e={}){let t=e.padding??24,i=e.background===void 0?"#ffffff":e.background??null,n=Pe(a,t),s=Math.max(1,Math.ceil(n.width)),r=Math.max(1,Math.ceil(n.height)),o=new Map(a.map(c=>[c.id,c])),h=a.map(c=>kn(c,o)).filter(Boolean).join(`
|
|
4
|
+
`),l=i?`<rect x="${m(n.x)}" y="${m(n.y)}" width="${s}" height="${r}" fill="${i}" />`:"";return `<?xml version="1.0" encoding="UTF-8"?>
|
|
5
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="${s}" height="${r}" viewBox="${m(n.x)} ${m(n.y)} ${s} ${r}" font-family="sans-serif">
|
|
6
6
|
${l}
|
|
7
7
|
${h}
|
|
8
|
-
</svg>`}function ve(a){return a.toString().padStart(10,"0")}function
|
|
8
|
+
</svg>`}function ve(a){return a.toString().padStart(10,"0")}function En(a,e,t){let i=a.split(",")[1]||"",n=atob(i),s=new Uint8Array(n.length);for(let I=0;I<n.length;I++)s[I]=n.charCodeAt(I);let r=Math.round(e),o=Math.round(t),h=`<< /Type /XObject /Subtype /Image /Width ${r} /Height ${o} /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /DCTDecode /Length ${s.length} >>`,l=`q ${r} 0 0 ${o} 0 0 cm /Im0 Do Q`,c=`1 0 obj
|
|
9
9
|
<< /Type /Catalog /Pages 2 0 R >>
|
|
10
10
|
endobj
|
|
11
11
|
`,u=`2 0 obj
|
|
@@ -14,10 +14,10 @@ endobj
|
|
|
14
14
|
`,d=`3 0 obj
|
|
15
15
|
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 ${r} ${o}] /Resources << /XObject << /Im0 4 0 R >> >> /Contents 5 0 R >>
|
|
16
16
|
endobj
|
|
17
|
-
`,
|
|
17
|
+
`,g=`4 0 obj
|
|
18
18
|
${h}
|
|
19
19
|
stream
|
|
20
|
-
`,
|
|
20
|
+
`,p=`endstream
|
|
21
21
|
endobj
|
|
22
22
|
`,y=`5 0 obj
|
|
23
23
|
<< /Length ${l.length} >>
|
|
@@ -25,19 +25,19 @@ stream
|
|
|
25
25
|
${l}
|
|
26
26
|
endstream
|
|
27
27
|
endobj
|
|
28
|
-
`,
|
|
29
|
-
`,
|
|
28
|
+
`,f=`%PDF-1.4
|
|
29
|
+
`,w=f.length,x=w+c.length,v=x+u.length,M=v+d.length,b=M+g.length+s.length+p.length,C=`xref
|
|
30
30
|
0 6
|
|
31
31
|
0000000000 65535 f
|
|
32
|
-
`+ve(
|
|
32
|
+
`+ve(w)+` 00000 n
|
|
33
33
|
`+ve(x)+` 00000 n
|
|
34
|
+
`+ve(v)+` 00000 n
|
|
34
35
|
`+ve(M)+` 00000 n
|
|
35
|
-
`+ve(
|
|
36
|
-
`+ve(w)+` 00000 n
|
|
36
|
+
`+ve(b)+` 00000 n
|
|
37
37
|
trailer
|
|
38
38
|
<< /Size 6 /Root 1 0 R >>
|
|
39
39
|
startxref
|
|
40
|
-
${
|
|
41
|
-
%%EOF`;return new Blob([m+c+u+d+p,s,g+y+C],{type:"application/pdf"})}async function st(a,e,t={}){if(e==="svg"){let s=Pt(a,t);return new Blob([s],{type:"image/svg+xml;charset=utf-8"})}let i=Mt(a,t);if(e==="png")return un(i,"image/png");let n=pn(i,"image/jpeg",.92);return Cn(n,i.width,i.height)}function rt(a,e){let t=URL.createObjectURL(a),i=document.createElement("a");i.href=t,i.download=e,document.body.appendChild(i),i.click(),document.body.removeChild(i),setTimeout(()=>URL.revokeObjectURL(t),1e3);}var ot="drawui-canvas-data",Tn=50,at=class{canvas;renderer;camera;tools=new Map;activeTool="select";shapes=[];selectedIds=[];listeners=new Set;notificationListeners=new Set;uiListeners=new Set;isPanning=false;panStart={x:0,y:0};dragStartPositions=new Map;lastClickTime=0;lastClickShapeId=null;editingShapeId=null;textInput=null;textOverlay=null;isNewTextShape=false;originalTextValue="";undoStack=[];redoStack=[];isUndoing=false;brushRect=null;snapGuides=[];connectingTargetId=null;genPreview=null;eraserPoint=null;mindMapEditingShapeId=null;mindMapSelectedNode=null;mindMapHoverNode=null;renderScheduled=false;pointerIsDown=false;notifyScheduled=false;persist=true;arrowStyle={connectionType:"straight",arrowHead:"end",dashed:false};constructor(e,t={}){this.canvas=e,this.persist=t.persist??!t.shapes,t.theme&&Ie(t.theme),t.language&&ke(t.language);let i=e.getBoundingClientRect();this.renderer=new oe(e),this.renderer.resize(i.width,i.height),this.camera=new se;let n={addShape:s=>this.addShape(s),updateShape:(s,r)=>this.updateShape(s,r),deleteShape:s=>this.deleteShape(s),selectShape:(s,r)=>this.selectShape(s,r),selectShapes:s=>this.selectShapes(s),getShapes:()=>[...this.shapes],getSelectedIds:()=>[...this.selectedIds],setActiveTool:s=>this.setActiveTool(s),screenToPage:s=>this.screenToPage(s),getCamera:()=>this.camera,panBy:(s,r)=>this.panBy(s,r),setCameraPosition:(s,r)=>this.setCameraPosition(s,r),getShapeAtPoint:(s,r)=>this.getShapeAtPoint(s,r),getShapesInBrushRadius:(s,r)=>this.getShapesInBrushRadius(s,r),setEraserPoint:s=>this.setEraserPoint(s),updateArrowBinding:(s,r,o,h)=>this.updateArrowBinding(s,r,o,h),enterEditing:s=>this.enterEditing(s),exitEditing:()=>this.exitEditing(),commitHistory:()=>this.commitHistory(),setBrushRect:s=>this.setBrushRect(s),setSnapGuides:s=>this.setSnapGuides(s),setConnectingTarget:s=>this.setConnectingTarget(s),setGenerationPreview:s=>this.setGenerationPreview(s),openGenerationMenu:s=>this.openGenerationMenu(s),createGeneratedConnection:(s,r)=>this.createGeneratedConnection(s,r),createFloatingLine:s=>this.createFloatingLine(s),connectToExisting:(s,r,o)=>this.connectToExisting(s,r,o),updateSelectedShapesStyle:s=>this.updateSelectedShapesStyle(s),updateSelectedShapesText:s=>this.updateSelectedShapesText(s),updateConnectionType:s=>this.updateConnectionType(s),getArrowStyle:()=>this.getArrowStyle(),getMindMapCursorHitAtPoint:s=>this.getMindMapCursorHitAtPoint(s),selectMindMapNode:(s,r)=>this.selectMindMapNode(s,r)};this.tools.set("select",new xe(n)),this.tools.set("hand",new we(n)),this.tools.set("rectangle",new T(n,"rectangle")),this.tools.set("rounded-rectangle",new T(n,"rounded-rectangle")),this.tools.set("circle",new T(n,"circle")),this.tools.set("ellipse",new T(n,"ellipse")),this.tools.set("diamond",new T(n,"diamond")),this.tools.set("triangle",new T(n,"triangle")),this.tools.set("hexagon",new T(n,"hexagon")),this.tools.set("parallelogram",new T(n,"parallelogram")),this.tools.set("trapezoid",new T(n,"trapezoid")),this.tools.set("pentagon",new T(n,"pentagon")),this.tools.set("octagon",new T(n,"octagon")),this.tools.set("star",new T(n,"star")),this.tools.set("cylinder",new T(n,"cylinder")),this.tools.set("cloud",new T(n,"cloud")),this.tools.set("heart",new T(n,"heart")),this.tools.set("block-arrow-right",new T(n,"block-arrow-right")),this.tools.set("block-arrow-left",new T(n,"block-arrow-left")),this.tools.set("block-arrow-up",new T(n,"block-arrow-up")),this.tools.set("block-arrow-down",new T(n,"block-arrow-down")),this.tools.set("block-arrow-double",new T(n,"block-arrow-double")),this.tools.set("right-triangle",new T(n,"right-triangle")),this.tools.set("cross",new T(n,"cross")),this.tools.set("speech-rect",new T(n,"speech-rect")),this.tools.set("speech-round",new T(n,"speech-round")),this.tools.set("cube",new T(n,"cube")),this.tools.set("sector",new T(n,"sector")),this.tools.set("delay",new T(n,"delay")),this.tools.set("predefined-process",new T(n,"predefined-process")),this.tools.set("document",new T(n,"document")),this.tools.set("stadium",new T(n,"stadium")),this.tools.set("class-catalog",new T(n,"class-catalog")),this.tools.set("chevron",new T(n,"chevron")),this.tools.set("brace-left",new T(n,"brace-left")),this.tools.set("brace-right",new T(n,"brace-right")),this.tools.set("uml-class",new T(n,"uml-class")),this.tools.set("uml-interface",new T(n,"uml-interface")),this.tools.set("line",new be(n)),this.tools.set("arrow",new Se(n)),this.tools.set("text",new Ue(n)),this.tools.set("pen",new te(n,"pen")),this.tools.set("highlighter",new te(n,"highlighter")),this.tools.set("eraser",new Re(n)),this.tools.set("mind-map",new T(n,"mind-map")),this.setupEventListeners(),t.shapes?this.shapes=t.shapes.map(s=>({...s,style:{...s.style}})):this.persist&&this.loadFromStorage(),this.render();}setupEventListeners(){this.canvas.addEventListener("pointerdown",this.handlePointerDown),this.canvas.addEventListener("pointerleave",this.handlePointerUp),window.addEventListener("pointermove",this.handlePointerMove),window.addEventListener("pointerup",this.handlePointerUp),window.addEventListener("pointercancel",this.handlePointerUp),this.canvas.addEventListener("wheel",this.handleWheel,{passive:false}),window.addEventListener("keydown",this.handleKeyDown),window.addEventListener("keyup",this.handleKeyUp),window.addEventListener("resize",this.handleResize);}handlePointerDown=e=>{if(this.editingShapeId)return this.textInput&&e.target===this.textInput,void 0;this.pointerIsDown=true;try{this.canvas.hasPointerCapture(e.pointerId)||this.canvas.setPointerCapture(e.pointerId);}catch{}if(e.button===1||e.button===0&&e.altKey){this.isPanning=true,this.panStart={x:e.clientX,y:e.clientY},this.canvas.setPointerCapture(e.pointerId),this.canvas.style.cursor="grabbing";return}let t=this.screenToPage({x:e.clientX,y:e.clientY}),i=this.activeTool==="select"?this.getMindMapCollapseHitAtPoint(t):null;if(i){this.toggleMindMapCollapse(i.shapeId,i.nodeId);return}if(this.activeTool==="select"){let s=Date.now(),r=this.getShapeAtPoint(t);if(s-this.lastClickTime<400&&this.lastClickShapeId&&r&&r.id===this.lastClickShapeId&&this.canEditShape(r)){this.enterEditing(r.id),this.lastClickTime=0,this.lastClickShapeId=null;return}if(this.lastClickTime=s,this.lastClickShapeId=r?.id||null,r&&r.type==="mind-map"&&this.mindMapEditingShapeId===r.id){let o=this.getMindMapNodeAtPoint(t);o&&(this.mindMapSelectedNode=o,this.notifyListeners(),this.render());return}r&&(this.dragStartPositions.clear(),this.selectedIds.forEach(o=>{let h=this.shapes.find(l=>l.id===o);h&&this.dragStartPositions.set(o,{x:h.x,y:h.y});}));}this.tools.get(this.activeTool)?.onPointerDown(t,e),this.updateCursor(t),this.render();};handlePointerMove=e=>{if(this.editingShapeId||!this.pointerIsDown&&e.target!==this.canvas)return;if(this.isPanning){let n=e.clientX-this.panStart.x,s=e.clientY-this.panStart.y;this.camera.translate(n,s),this.panStart={x:e.clientX,y:e.clientY},this.scheduleRender(),this.notifyListeners();return}let t=this.screenToPage({x:e.clientX,y:e.clientY});this.tools.get(this.activeTool)?.onPointerMove(t,e),this.updateCursor(t),this.scheduleRender();};handlePointerUp=e=>{if(e.type==="pointerleave"){if(this.mindMapHoverNode=null,this.pointerIsDown){this.scheduleRender();return}}else {if(!this.pointerIsDown&&e.target!==this.canvas)return;this.canvas.hasPointerCapture(e.pointerId)&&this.canvas.releasePointerCapture(e.pointerId);}if(this.pointerIsDown=false,this.editingShapeId)return;if(this.isPanning){this.isPanning=false,this.updateCursor(this.screenToPage({x:e.clientX,y:e.clientY}));return}let t=this.screenToPage({x:e.clientX,y:e.clientY});this.tools.get(this.activeTool)?.onPointerUp(t,e),this.updateCursor(t),this.scheduleRender();};updateCursor(e){let i=this.tools.get(this.activeTool)?.getCursor(e)??"default";if(this.canvas.style.cursor!==i&&(this.canvas.style.cursor=i),!this.pointerIsDown){let n=this.getMindMapCursorHitAtPoint(e);(n?.shapeId!==this.mindMapHoverNode?.shapeId||n?.nodeId!==this.mindMapHoverNode?.nodeId)&&(this.mindMapHoverNode=n,this.scheduleRender());}}handleWheel=e=>{if(e.preventDefault(),e.ctrlKey||e.metaKey){let n=e.deltaY>0?.9:1.1;this.camera.zoomAt({x:e.clientX,y:e.clientY},n),this.scheduleRender(),this.notifyListeners();return}let t=e.deltaX||0,i=e.deltaY||0;e.deltaMode===1?(t*=40,i*=40):e.deltaMode===2&&(t*=120,i*=120),e.shiftKey&&(t=t||i,i=0),this.camera.translate(-t,-i),this.scheduleRender(),this.notifyListeners();};handleKeyDown=e=>{if(this.editingShapeId)return;if((e.ctrlKey||e.metaKey)&&e.key.toLowerCase()==="z"){e.preventDefault(),e.shiftKey?this.redo():this.undo();return}if((e.ctrlKey||e.metaKey)&&e.key.toLowerCase()==="y"){e.preventDefault(),this.redo();return}if((e.ctrlKey||e.metaKey)&&e.key.toLowerCase()==="s"){e.preventDefault(),this.saveToStorage();return}if(!e.ctrlKey&&!e.metaKey&&!e.altKey){if(e.key==="v"||e.key==="V"){this.setActiveTool("select"),e.preventDefault();return}if(e.key==="h"||e.key==="H"){this.setActiveTool("hand"),e.preventDefault();return}}this.tools.get(this.activeTool)?.onKeyDown(e),this.scheduleRender();};handleKeyUp=e=>{if(this.editingShapeId)return;this.tools.get(this.activeTool)?.onKeyUp(e),this.scheduleRender();};handleResize=()=>{this.renderer.resize(window.innerWidth,window.innerHeight),this.scheduleRender();};screenToPage(e){let t=this.canvas.getBoundingClientRect();return this.camera.screenToPage({x:e.x-t.left,y:e.y-t.top})}addShape(e){this.shapes.push(e),this.notifyListeners();}updateShape(e,t){let i=this.shapes.findIndex(n=>n.id===e);i!==-1&&(this.shapes[i]={...this.shapes[i],...t},(t.x!==void 0||t.y!==void 0)&&this.updateBoundArrows(e),this.notifyListeners());}updateBoundArrows(e){let t=this.shapes.find(i=>i.id===e);t&&this.shapes.forEach(i=>{if(i.type==="arrow"){let n=i,s={};if(n.startBinding?.toShapeId===e){let r=this.getBindingPosition(t,n.startBinding.handle||"top");s.points=n.points.map((o,h)=>h===0?r:o);}if(n.endBinding?.toShapeId===e){let r=this.getBindingPosition(t,n.endBinding.handle||"top"),o=s.points||[...n.points];s.points=o.map((h,l)=>l===o.length-1?r:h);}s.points&&(this.shapes[this.shapes.indexOf(i)]={...n,...s});}});}getBindingPosition(e,t){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":switch(t){case "top":return {x:e.x,y:e.y-e.height/2};case "bottom":return {x:e.x,y:e.y+e.height/2};case "left":return {x:e.x-e.width/2,y:e.y};case "right":return {x:e.x+e.width/2,y:e.y}}case "circle":switch(t){case "top":return {x:e.x,y:e.y-e.radius};case "bottom":return {x:e.x,y:e.y+e.radius};case "left":return {x:e.x-e.radius,y:e.y};case "right":return {x:e.x+e.radius,y:e.y}}case "hexagon":switch(t){case "top":return {x:e.x,y:e.y-e.height/2};case "bottom":return {x:e.x,y:e.y+e.height/2};case "left":return {x:e.x-e.width/2,y:e.y};case "right":return {x:e.x+e.width/2,y:e.y}}default:return {x:e.x,y:e.y}}}deleteShape(e){this.shapes=this.shapes.filter(t=>t.id!==e),this.selectedIds=this.selectedIds.filter(t=>t!==e),this.shapes=this.shapes.map(t=>{if(t.type==="arrow"){let i=t;if(i.startBinding?.toShapeId===e)return {...i,startBinding:void 0};if(i.endBinding?.toShapeId===e)return {...i,endBinding:void 0}}return t}),this.notifyListeners(),this.scheduleRender();}selectShape(e,t){if(!e)this.selectedIds=[];else if(t){let i=this.selectedIds.indexOf(e);i===-1?this.selectedIds.push(e):this.selectedIds.splice(i,1);}else this.selectedIds=[e];this.notifyListeners(),this.scheduleRender();}selectShapes(e){this.selectedIds=[...e],this.notifyListeners(),this.scheduleRender();}setBrushRect(e){this.brushRect=e,this.scheduleRender();}setSnapGuides(e){this.snapGuides=e;}setConnectingTarget(e){this.connectingTargetId!==e&&(this.connectingTargetId=e,this.scheduleRender());}setGenerationPreview(e){this.genPreview?.from.x===e?.from.x&&this.genPreview?.from.y===e?.from.y&&this.genPreview?.to.x===e?.to.x&&this.genPreview?.to.y===e?.to.y&&this.genPreview?.type===e?.type||(this.genPreview=e,this.scheduleRender());}openGenerationMenu(e){let t=this.camera.getZoom(),i=(e.context.to.x+this.camera.x)*t,n=(e.context.to.y+this.camera.y)*t;this.emitUi({kind:"shapeTypeMenu",screenX:i,screenY:n,context:e.context,existingArrowId:e.existingArrowId,defaultShapeType:e.defaultShapeType});}lineStyle(){return {strokeColor:"#1f2937",fillColor:"transparent",strokeWidth:2,strokeDash:null}}computeSourceBinding(e){if(!e.sourceShapeId||!e.edge)return;let t=this.shapes.find(n=>n.id===e.sourceShapeId);if(!t)return;let i={n:"top",e:"right",s:"bottom",w:"left"};return {toShapeId:t.id,handle:i[e.edge]}}connectHandle(e,t){let i=t.x-e.x,n=t.y-e.y;return Math.abs(i)>=Math.abs(n)?i>=0?"left":"right":n>=0?"top":"bottom"}createFloatingLine(e){this.commitHistory();let t=e.from,i=e.to,n=`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,s={id:n,type:"arrow",x:t.x,y:t.y,points:[{x:t.x,y:t.y},{x:i.x,y:i.y}],rotation:0,style:this.lineStyle(),startBinding:this.computeSourceBinding(e),connectionType:"polyline",arrowHead:this.arrowStyle.arrowHead,dashed:this.arrowStyle.dashed};return this.addShape(s),this.selectedIds=[n],this.notifyListeners(),this.render(),n}createGeneratedConnection(e,t){this.commitHistory();let i=t.type,n;if(e.sourceShapeId){let h=this.shapes.find(l=>l.id===e.sourceShapeId);h&&$e(h.type)&&(n={...h.style});}let s=Ze(i,e.to,n);this.addShape(s);let r=e.from,o={toShapeId:s.id,handle:this.connectHandle(r,e.to)};if(t.existingArrowId)this.updateArrowBinding(t.existingArrowId,"end",s.id,{toShapeId:s.id,handle:this.connectHandle(r,e.to)});else {let l={id:`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,type:"arrow",x:r.x,y:r.y,points:[{x:r.x,y:r.y},{x:e.to.x,y:e.to.y}],rotation:0,style:this.lineStyle(),startBinding:this.computeSourceBinding(e),endBinding:o,connectionType:"polyline",arrowHead:this.arrowStyle.arrowHead,dashed:this.arrowStyle.dashed};this.addShape(l);}this.selectedIds=[s.id],this.notifyListeners(),this.render();}connectToExisting(e,t,i){this.commitHistory();let n=this.shapes.find(c=>c.id===t);if(!n)return;let s=e.from,r=i?.handle??this.connectHandle(s,{x:n.x,y:n.y}),o=this.getBindingPosition(n,r),h=`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,l={id:h,type:"arrow",x:s.x,y:s.y,points:[{x:s.x,y:s.y},{x:o.x,y:o.y}],rotation:0,style:this.lineStyle(),startBinding:this.computeSourceBinding(e),endBinding:{toShapeId:t,handle:r},connectionType:"polyline",arrowHead:this.arrowStyle.arrowHead,dashed:this.arrowStyle.dashed};this.addShape(l),this.selectedIds=[h],this.notifyListeners(),this.render();}subscribeUi(e){return this.uiListeners.add(e),()=>this.uiListeners.delete(e)}emitUi(e){this.uiListeners.forEach(t=>t(e));}updateSelectedShapesStyle(e){this.selectedIds.length!==0&&(this.commitHistory(),this.selectedIds.forEach(t=>{let i=this.shapes.findIndex(n=>n.id===t);i!==-1&&(this.shapes[i]={...this.shapes[i],style:{...this.shapes[i].style,...e}});}),this.notifyListeners(),this.render());}updateSelectedShapesText(e){this.selectedIds.length!==0&&(this.commitHistory(),this.selectedIds.forEach(t=>{let i=this.shapes.findIndex(n=>n.id===t);i!==-1&&(this.shapes[i]={...this.shapes[i],...e});}),this.notifyListeners(),this.render());}updateConnectionType(e){this.selectedIds.length!==0&&(this.commitHistory(),this.selectedIds.forEach(t=>{let i=this.shapes.findIndex(n=>n.id===t);if(i!==-1){let n=this.shapes[i];(n.type==="line"||n.type==="arrow")&&(this.shapes[i]={...n,connectionType:e});}}),this.notifyListeners(),this.render());}updateArrowHeadType(e){if(this.selectedIds.length===0)return;this.commitHistory();let t=e==="dependency"||e==="realization";this.selectedIds.forEach(i=>{let n=this.shapes.findIndex(s=>s.id===i);if(n!==-1){let s=this.shapes[n];s.type==="arrow"&&(this.shapes[n]=t?{...s,arrowHead:e,dashed:true}:{...s,arrowHead:e});}}),this.notifyListeners(),this.render();}updateArrowStyle(e){this.selectedIds.length!==0&&(this.commitHistory(),this.selectedIds.forEach(t=>{let i=this.shapes.findIndex(o=>o.id===t);if(i===-1)return;let n=this.shapes[i];if(n.type!=="arrow")return;let s=e.arrowHead??n.arrowHead??"end",r=s==="dependency"||s==="realization";this.shapes[i]={...n,...e,arrowHead:s,dashed:r?true:e.dashed??n.dashed??false};}),this.notifyListeners(),this.render());}getArrowStyle(){return {...this.arrowStyle}}setArrowStyle(e){let t={...this.arrowStyle,...e};(t.arrowHead==="dependency"||t.arrowHead==="realization")&&(t.dashed=true),this.arrowStyle=t,this.notifyListeners();}updateArrowDashed(e){this.selectedIds.length!==0&&(this.commitHistory(),this.selectedIds.forEach(t=>{let i=this.shapes.findIndex(n=>n.id===t);if(i!==-1){let n=this.shapes[i];n.type==="arrow"&&(this.shapes[i]={...n,dashed:e});}}),this.notifyListeners(),this.render());}updateUmlClass(e,t){let i=this.shapes.findIndex(c=>c.id===e);if(i===-1)return;let n=this.shapes[i];if(n.type!=="uml-class")return;this.commitHistory();let s=t.className??n.className,r=t.attributes??n.attributes,o=t.methods??n.methods,{width:h,height:l}=Ae(s,r,o);this.shapes[i]={...n,className:s,attributes:r,methods:o,width:h,height:l},this.notifyListeners(),this.render();}updateUmlInterface(e,t){let i=this.shapes.findIndex(g=>g.id===e);if(i===-1)return;let n=this.shapes[i];if(n.type!=="uml-interface")return;this.commitHistory();let s=n,r=t.className??s.className,o=t.methods??s.methods,h=G*.6,l=o.map(g=>`${g.visibility} ${g.name}(${g.params}): ${g.returnType}`),c=Math.max(r.length,...l.map(g=>g.length),0),u=Math.max(fe,c*h+O*2),d=o.length>0?o.length*A+E*2:E*2,p=j+d;this.shapes[i]={...n,className:r,methods:o,width:u,height:p},this.notifyListeners(),this.render();}getSelectedUml(){if(this.selectedIds.length!==1)return null;let e=this.shapes.find(t=>t.id===this.selectedIds[0]);return e&&(e.type==="uml-class"||e.type==="uml-interface")?e:null}getUmlById(e){let t=this.shapes.find(i=>i.id===e);return t&&(t.type==="uml-class"||t.type==="uml-interface")?t:null}getSelectedMindMap(){if(this.selectedIds.length!==1)return null;let e=this.shapes.find(t=>t.id===this.selectedIds[0]);return e&&e.type==="mind-map"?e:null}getMindMapNodeAtPoint(e){for(let t=this.shapes.length-1;t>=0;t--){let i=this.shapes[t];if(i.type!=="mind-map")continue;let n=i,s=e.x-n.x,r=e.y-n.y,o=Math.cos(n.rotation),h=Math.sin(n.rotation),l=s*o+r*h,c=-s*h+r*o;if(Math.abs(l)>n.width/2||Math.abs(c)>n.height/2)continue;let u=B(n.root);for(let[d,p]of u.positions)if(Math.abs(l-p.x)<=p.width/2&&Math.abs(c-p.y)<=p.height/2)return {shapeId:n.id,nodeId:d};return {shapeId:n.id,nodeId:n.root.id}}return null}getMindMapNodeAtScreenPoint(e){return this.getMindMapNodeAtPoint(this.screenToPage(e))}getMindMapCursorHitAtPoint(e){let t=this.getMindMapCollapseHitAtPoint(e);if(t)return t;for(let i=this.shapes.length-1;i>=0;i--){let n=this.shapes[i];if(n.type!=="mind-map")continue;let s=n,r=e.x-s.x,o=e.y-s.y,h=Math.cos(s.rotation),l=Math.sin(s.rotation),c=r*h+o*l,u=-r*l+o*h,d=B(s.root);for(let[p,g]of d.positions)if(Math.abs(c-g.x)<=g.width/2&&Math.abs(u-g.y)<=g.height/2)return {shapeId:s.id,nodeId:p}}return null}selectMindMapNode(e,t){let i=this.shapes.find(n=>n.id===e);!i||i.type!=="mind-map"||(this.selectedIds=[e],this.mindMapSelectedNode={shapeId:e,nodeId:t},this.notifyListeners(),this.render());}getMindMapEditingShapeId(){return this.mindMapEditingShapeId}getMindMapSelectedNode(){return this.mindMapSelectedNode}getMindMapNode(e,t){let i=this.shapes.find(n=>n.id===e);return !i||i.type!=="mind-map"?null:Q(i.root,t)}getMindMapNodeScreenRect(e,t){let i=this.shapes.find(v=>v.id===e);if(!i||i.type!=="mind-map")return null;let n=i,r=B(n.root).positions.get(t);if(!r)return null;let o=this.camera.getZoom(),h=r.x,l=r.y,c=Math.cos(n.rotation),u=Math.sin(n.rotation),d=h*c-l*u,p=h*u+l*c,g=this.camera.pageToScreen({x:n.x,y:n.y}),y=g.x+d*o,m=g.y+p*o;return {x:y-r.width*o/2,y:m-r.height*o/2,width:r.width*o,height:r.height*o}}getMindMapCollapseHitAtPoint(e){for(let t of this.shapes){if(t.type!=="mind-map")continue;let i=t,n=B(i.root),s=e.x-i.x,r=e.y-i.y,o=Math.cos(i.rotation),h=Math.sin(i.rotation),l=s*o+r*h,c=-s*h+r*o;for(let[u,d]of n.positions){let p=Q(i.root,u);if(!p||!p.children||p.children.length===0)continue;let g=d.x+d.width/2+10,y=d.y,m=l-g,v=c-y;if(m*m+v*v<=121)return {shapeId:t.id,nodeId:u}}}return null}beginMindMapEdit(e,t){let i=this.shapes.find(s=>s.id===e);if(!i||i.type!=="mind-map")return;let n=i;this.mindMapEditingShapeId=e,this.mindMapSelectedNode={shapeId:e,nodeId:t&&Q(n.root,t)?t:n.root.id},this.selectedIds=[e],this.notifyListeners(),this.render();}exitMindMapEdit(){this.mindMapEditingShapeId=null,this.mindMapSelectedNode=null,this.notifyListeners(),this.render();}updateMindMapNode(e,t,i,n=true){let s=this.shapes.findIndex(p=>p.id===e);if(s===-1)return;let r=this.shapes[s];if(r.type!=="mind-map")return;let o=r,h=Q(o.root,t);if(!h||!Object.keys(i).some(p=>h[p]!==i[p]))return;n&&this.commitHistory();let c=Ee(o.root,t,i),{width:u,height:d}=B(c);this.shapes[s]={...o,root:c,width:u,height:d},this.notifyListeners(),this.render();}addMindMapChild(e,t){let i=this.shapes.findIndex(c=>c.id===e);if(i===-1)return;let n=this.shapes[i];if(n.type!=="mind-map")return;let s=n,r={id:`mm_${Date.now()}_${Math.random().toString(36).substr(2,6)}`,text:Z("mindmap.defaultBranch")},o=We(s.root,t,r);this.commitHistory();let{width:h,height:l}=B(o);this.shapes[i]={...s,root:o,width:h,height:l},this.mindMapSelectedNode={shapeId:e,nodeId:r.id},this.notifyListeners(),this.render();}addMindMapSibling(e,t){let i=this.shapes.findIndex(c=>c.id===e);if(i===-1)return;let n=this.shapes[i];if(n.type!=="mind-map")return;let s=n;if(!ye(s.root,t))return;let r={id:`mm_${Date.now()}_${Math.random().toString(36).substr(2,6)}`,text:Z("mindmap.defaultBranch")},o=Ke(s.root,t,r);this.commitHistory();let{width:h,height:l}=B(o);this.shapes[i]={...s,root:o,width:h,height:l},this.mindMapSelectedNode={shapeId:e,nodeId:r.id},this.notifyListeners(),this.render();}deleteMindMapNode(e,t){let i=this.shapes.findIndex(c=>c.id===e);if(i===-1)return;let n=this.shapes[i];if(n.type!=="mind-map")return;let s=n;if(s.root.id===t){this.commitHistory(),this.shapes=this.shapes.filter(c=>c.id!==e),this.selectedIds=this.selectedIds.filter(c=>c!==e),this.mindMapEditingShapeId=null,this.mindMapSelectedNode=null,this.notifyListeners(),this.render();return}let r=Fe(s.root,t);if(!r)return;this.commitHistory();let{width:o,height:h}=B(r);this.shapes[i]={...s,root:r,width:o,height:h};let l=ye(s.root,t);this.mindMapSelectedNode=l?{shapeId:e,nodeId:l.id}:{shapeId:e,nodeId:r.id},this.notifyListeners(),this.render();}toggleMindMapCollapse(e,t){let i=this.shapes.findIndex(c=>c.id===e);if(i===-1)return;let n=this.shapes[i];if(n.type!=="mind-map")return;let s=n,r=Q(s.root,t);if(!r||!r.children||r.children.length===0)return;this.commitHistory();let o=Ee(s.root,t,{collapsed:!r.collapsed}),{width:h,height:l}=B(o);this.shapes[i]={...s,root:o,width:h,height:l},this.notifyListeners(),this.render();}addUmlAttribute(e){let t=this.shapes.find(n=>n.id===e);if(!t||t.type!=="uml-class")return;let i={id:`attr_${Date.now()}_${Math.random().toString(36).slice(2,8)}`,visibility:"+",name:"newField",type:"string"};this.updateUmlClass(e,{attributes:[...t.attributes,i]});}addUmlMethod(e){let t=this.getUmlById(e);if(!t)return;let i={id:`meth_${Date.now()}_${Math.random().toString(36).slice(2,8)}`,visibility:"+",name:"newMethod",params:"",returnType:"void"};t.type==="uml-class"?this.updateUmlClass(e,{methods:[...t.methods,i]}):this.updateUmlInterface(e,{methods:[...t.methods,i]});}removeUmlAttribute(e,t){let i=this.shapes.find(n=>n.id===e);!i||i.type!=="uml-class"||this.updateUmlClass(e,{attributes:i.attributes.filter(n=>n.id!==t)});}removeUmlMethod(e,t){let i=this.getUmlById(e);if(!i)return;let n=i.methods.filter(s=>s.id!==t);i.type==="uml-class"?this.updateUmlClass(e,{methods:n}):this.updateUmlInterface(e,{methods:n});}reorderUmlAttribute(e,t,i){let n=this.shapes.find(o=>o.id===e);if(!n||n.type!=="uml-class"||t===i)return;let s=[...n.attributes];if(t<0||t>=s.length||i<0||i>=s.length)return;let[r]=s.splice(t,1);s.splice(i,0,r),this.updateUmlClass(e,{attributes:s});}reorderUmlMethod(e,t,i){let n=this.getUmlById(e);if(!n||t===i)return;let s=[...n.methods];if(t<0||t>=s.length||i<0||i>=s.length)return;let[r]=s.splice(t,1);s.splice(i,0,r),n.type==="uml-class"?this.updateUmlClass(e,{methods:s}):this.updateUmlInterface(e,{methods:s});}updateUmlRowStyle(e,t,i,n){let s=this.getUmlById(e);if(s)if(t==="attribute"){if(s.type!=="uml-class")return;let r=s.attributes.map(o=>o.id===i?{...o,textColor:n.textColor===null?void 0:n.textColor??o.textColor,fontSize:n.fontSize===null?void 0:n.fontSize??o.fontSize,strikethrough:n.strikethrough===null?void 0:n.strikethrough??o.strikethrough}:o);this.updateUmlClass(e,{attributes:r});}else {let r=s.methods.map(o=>o.id===i?{...o,textColor:n.textColor===null?void 0:n.textColor??o.textColor,fontSize:n.fontSize===null?void 0:n.fontSize??o.fontSize,strikethrough:n.strikethrough===null?void 0:n.strikethrough??o.strikethrough}:o);s.type==="uml-class"?this.updateUmlClass(e,{methods:r}):this.updateUmlInterface(e,{methods:r});}}getUmlRowAtScreenPoint(e){let t=this.camera.screenToPage(e);for(let i=this.shapes.length-1;i>=0;i--){let n=this.shapes[i];if(n.type!=="uml-class"&&n.type!=="uml-interface")continue;let s=n.width/2,r=n.height/2;if(t.x<n.x-s||t.x>n.x+s||t.y<n.y-r||t.y>n.y+r)continue;let o=j,h=t.y-n.y;if(h>=-r&&h<=-r+o)return {shape:n,section:"className",rowIndex:-1,text:n.className};if(n.type==="uml-interface"){let g=-r+o,y=r;if(n.methods.length>0&&h>=g&&h<=y){let m=Math.floor((h-g-E)/A);if(m>=0&&m<n.methods.length){let v=n.methods[m];return {shape:n,section:"method",rowIndex:m,text:`${v.visibility} ${v.name}(${v.params}): ${v.returnType}`}}return {shape:n,section:"method",rowIndex:-1,text:""}}return {shape:n,section:"className",rowIndex:-1,text:n.className}}let l=n.attributes.length>0?n.attributes.length*A+E*2:E*2,c=-r+o,u=c+l;if(n.attributes.length>0&&h>=c&&h<=u){let g=Math.floor((h-c-E)/A);if(g>=0&&g<n.attributes.length){let y=n.attributes[g];return {shape:n,section:"attribute",rowIndex:g,text:`${y.visibility} ${y.name}: ${y.type}`}}return {shape:n,section:"attribute",rowIndex:-1,text:""}}let d=u,p=r;if(n.methods.length>0&&h>=d&&h<=p){let g=Math.floor((h-d-E)/A);if(g>=0&&g<n.methods.length){let y=n.methods[g];return {shape:n,section:"method",rowIndex:g,text:`${y.visibility} ${y.name}(${y.params}): ${y.returnType}`}}return {shape:n,section:"method",rowIndex:-1,text:""}}return {shape:n,section:"className",rowIndex:-1,text:n.className}}return null}setActiveTool(e){this.tools.has(e)&&(this.activeTool=e,this.eraserPoint=null,this.notifyListeners());}getActiveTool(){return this.activeTool}setHighlighterColor(e){let t=this.tools.get("highlighter");t instanceof te&&t.setHighlighterColor(e);}getHighlighterColor(){let e=this.tools.get("highlighter");return e instanceof te?e.getHighlighterColor():Oe}getShapes(){return [...this.shapes]}getData(){return {shapes:this.getShapes(),camera:{x:this.camera.x,y:this.camera.y,zoom:this.camera.zoom}}}setShapes(e){return Array.isArray(e)?(this.shapes=e.map(t=>({...t,style:{...t.style}})),this.selectedIds=[],this.undoStack=[],this.redoStack=[],this.notifyListeners(),this.render(),true):false}setTheme(e){Ie(e),this.scheduleRender();}setLanguage(e){ke(e);}getSelectedIds(){return [...this.selectedIds]}getCamera(){return this.camera}isPointerDown(){return this.pointerIsDown}getContentBounds(e=24){return Pe(this.shapes,e)}async exportImage(e,t={}){return st(this.getShapes(),e,t)}async downloadImage(e="drawui",t="png",i={}){let n=await this.exportImage(t,i);rt(n,`${e}.${t}`);}zoomIn(){this.camera.zoomAt({x:window.innerWidth/2,y:window.innerHeight/2},1.2),this.scheduleRender(),this.notifyListeners();}zoomOut(){this.camera.zoomAt({x:window.innerWidth/2,y:window.innerHeight/2},.8),this.scheduleRender(),this.notifyListeners();}resetZoom(){this.camera.reset(),this.scheduleRender(),this.notifyListeners();}panBy(e,t){this.camera.translate(e,t),this.scheduleRender(),this.notifyListeners();}setCameraPosition(e,t){this.camera.x=e,this.camera.y=t,this.scheduleRender(),this.notifyListeners();}requestRender(){this.scheduleRender(),this.notifyListeners();}setShowGrid(e){this.renderer.setShowGrid(e),this.scheduleRender();}render(){this.renderer.render(this.shapes,this.camera,new Set(this.selectedIds),this.brushRect,this.snapGuides,this.connectingTargetId,this.genPreview,this.eraserPoint,this.mindMapSelectedNode,this.mindMapHoverNode);}scheduleRender(){this.renderScheduled||(this.renderScheduled=true,requestAnimationFrame(()=>{this.renderScheduled=false,this.render();}));}subscribe(e){return this.listeners.add(e),()=>this.listeners.delete(e)}subscribeNotifications(e){return this.notificationListeners.add(e),()=>this.notificationListeners.delete(e)}notify(e){this.notificationListeners.forEach(t=>t(e));}notifyListeners(){this.notifyScheduled||(this.notifyScheduled=true,queueMicrotask(()=>{this.notifyScheduled=false,this.mindMapEditingShapeId&&!this.selectedIds.includes(this.mindMapEditingShapeId)&&(this.mindMapEditingShapeId=null,this.mindMapSelectedNode=null),this.listeners.forEach(e=>e());}));}cloneShapes(e){return e.map(t=>{let i={...t,style:{...t.style}};return Array.isArray(t.points)&&(i.points=t.points.map(n=>({...n}))),t.startBinding&&(i.startBinding={...t.startBinding}),t.endBinding&&(i.endBinding={...t.endBinding}),t.root&&(i.root=De(t.root)),i})}commitHistory(){if(this.isUndoing)return;let e={shapes:this.cloneShapes(this.shapes),selectedIds:[...this.selectedIds]};this.undoStack.push(e),this.undoStack.length>Tn&&this.undoStack.shift(),this.redoStack=[];}mark(){this.commitHistory();}undo(){if(this.undoStack.length!==0){this.isUndoing=true;try{this.redoStack.push({shapes:this.cloneShapes(this.shapes),selectedIds:[...this.selectedIds]});let e=this.undoStack.pop();this.shapes=this.cloneShapes(e.shapes),this.selectedIds=[...e.selectedIds];}finally{this.isUndoing=false;}this.notifyListeners(),this.render();}}redo(){if(this.redoStack.length!==0){this.isUndoing=true;try{this.undoStack.push({shapes:this.cloneShapes(this.shapes),selectedIds:[...this.selectedIds]});let e=this.redoStack.pop();this.shapes=this.cloneShapes(e.shapes),this.selectedIds=[...e.selectedIds];}finally{this.isUndoing=false;}this.notifyListeners(),this.render();}}canUndo(){return this.undoStack.length>0}canRedo(){return this.redoStack.length>0}saveToStorage(){if(!this.persist)return false;try{let e=JSON.stringify({version:1,shapes:this.shapes,savedAt:Date.now()});return localStorage.setItem(ot,e),this.notifyListeners(),this.notify(Z("save.success")),!0}catch(e){return console.error("\u4FDD\u5B58\u5931\u8D25:",e),this.notify(Z("save.fail")),false}}loadFromStorage(){if(!this.persist)return false;try{let e=localStorage.getItem(ot);if(!e)return !1;let t=JSON.parse(e);return t.shapes&&Array.isArray(t.shapes)?(this.shapes=t.shapes,this.selectedIds=[],this.undoStack=[],this.redoStack=[],this.notifyListeners(),this.scheduleRender(),!0):!1}catch(e){return console.error("\u52A0\u8F7D\u5931\u8D25:",e),false}}clearStorage(){localStorage.removeItem(ot);}exportJSON(){return JSON.stringify({version:1,shapes:this.shapes,savedAt:Date.now()},null,2)}importJSON(e){try{let t=JSON.parse(e);return t.shapes&&Array.isArray(t.shapes)?(this.commitHistory(),this.shapes=t.shapes,this.selectedIds=[],this.notifyListeners(),this.render(),!0):!1}catch(t){return console.error("\u5BFC\u5165\u5931\u8D25:",t),false}}getEditingShapeId(){return this.editingShapeId}canEditShape(e){return !(e.type==="line"||e.type==="arrow"||e.type==="draw"||e.type==="uml-class"||e.type==="uml-interface"||e.type==="mind-map")}enterEditing(e){let t=this.shapes.find(i=>i.id===e);!t||!this.canEditShape(t)||(this.editingShapeId=e,this.selectedIds=[e],this.isNewTextShape=t.type==="text"&&!t.text,this.originalTextValue=t.text||"",this.notifyListeners(),this.scheduleRender(),requestAnimationFrame(()=>{this.createTextInput(t);}));}exitEditing(){if(!this.editingShapeId)return;let e=this.editingShapeId,t=this.shapes.find(n=>n.id===e),i=this.textInput?this.textInput.value:"";if(this.removeTextInput(),t)if(t.type==="text")if(this.commitHistory(),!i.trim()&&this.isNewTextShape)this.shapes=this.shapes.filter(n=>n.id!==e),this.selectedIds=this.selectedIds.filter(n=>n!==e);else {let n=t,s=n.fontSize||16,r=s*1.3,o=n.width||200,h=i.split(`
|
|
42
|
-
`),l=0;for(let d of h){let
|
|
43
|
-
`+c.value.substring(
|
|
40
|
+
${b+y.length}
|
|
41
|
+
%%EOF`;return new Blob([f+c+u+d+g,s,p+y+C],{type:"application/pdf"})}async function rt(a,e,t={}){if(e==="svg"){let s=Ct(a,t);return new Blob([s],{type:"image/svg+xml;charset=utf-8"})}let i=vt(a,t);if(e==="png")return gn(i,"image/png");let n=pn(i,"image/jpeg",.92);return En(n,i.width,i.height)}function ot(a,e){let t=URL.createObjectURL(a),i=document.createElement("a");i.href=t,i.download=e,document.body.appendChild(i),i.click(),document.body.removeChild(i),setTimeout(()=>URL.revokeObjectURL(t),1e3);}var at="drawui-canvas-data",An=50,ht=class{canvas;renderer;camera;tools=new Map;activeTool="select";shapes=[];selectedIds=[];listeners=new Set;notificationListeners=new Set;uiListeners=new Set;isPanning=false;panStart={x:0,y:0};dragStartPositions=new Map;lastClickTime=0;lastClickShapeId=null;editingShapeId=null;textInput=null;textOverlay=null;isNewTextShape=false;originalTextValue="";undoStack=[];redoStack=[];isUndoing=false;brushRect=null;snapGuides=[];connectingTargetId=null;genPreview=null;eraserPoint=null;mindMapEditingShapeId=null;mindMapSelectedNode=null;mindMapHoverNode=null;renderScheduled=false;pointerIsDown=false;notifyScheduled=false;persist=true;arrowStyle={connectionType:"straight",arrowHead:"end",dashed:false};constructor(e,t={}){this.canvas=e,this.persist=t.persist??!t.shapes,t.theme&&Ie(t.theme),t.language&&ke(t.language);let i=e.getBoundingClientRect();this.renderer=new oe(e),this.renderer.resize(i.width,i.height),this.camera=new se;let n={addShape:s=>this.addShape(s),updateShape:(s,r)=>this.updateShape(s,r),deleteShape:s=>this.deleteShape(s),selectShape:(s,r)=>this.selectShape(s,r),selectShapes:s=>this.selectShapes(s),getShapes:()=>[...this.shapes],getSelectedIds:()=>[...this.selectedIds],setActiveTool:s=>this.setActiveTool(s),screenToPage:s=>this.screenToPage(s),getCamera:()=>this.camera,panBy:(s,r)=>this.panBy(s,r),setCameraPosition:(s,r)=>this.setCameraPosition(s,r),getShapeAtPoint:(s,r)=>this.getShapeAtPoint(s,r),getShapesInBrushRadius:(s,r)=>this.getShapesInBrushRadius(s,r),setEraserPoint:s=>this.setEraserPoint(s),updateArrowBinding:(s,r,o,h)=>this.updateArrowBinding(s,r,o,h),enterEditing:s=>this.enterEditing(s),exitEditing:()=>this.exitEditing(),commitHistory:()=>this.commitHistory(),setBrushRect:s=>this.setBrushRect(s),setSnapGuides:s=>this.setSnapGuides(s),setConnectingTarget:s=>this.setConnectingTarget(s),setGenerationPreview:s=>this.setGenerationPreview(s),openGenerationMenu:s=>this.openGenerationMenu(s),createGeneratedConnection:(s,r)=>this.createGeneratedConnection(s,r),createFloatingLine:s=>this.createFloatingLine(s),connectToExisting:(s,r,o)=>this.connectToExisting(s,r,o),updateSelectedShapesStyle:s=>this.updateSelectedShapesStyle(s),updateSelectedShapesText:s=>this.updateSelectedShapesText(s),updateConnectionType:s=>this.updateConnectionType(s),getArrowStyle:()=>this.getArrowStyle(),getMindMapCursorHitAtPoint:s=>this.getMindMapCursorHitAtPoint(s),selectMindMapNode:(s,r)=>this.selectMindMapNode(s,r)};this.tools.set("select",new xe(n)),this.tools.set("hand",new we(n)),this.tools.set("rectangle",new T(n,"rectangle")),this.tools.set("rounded-rectangle",new T(n,"rounded-rectangle")),this.tools.set("circle",new T(n,"circle")),this.tools.set("ellipse",new T(n,"ellipse")),this.tools.set("diamond",new T(n,"diamond")),this.tools.set("triangle",new T(n,"triangle")),this.tools.set("hexagon",new T(n,"hexagon")),this.tools.set("parallelogram",new T(n,"parallelogram")),this.tools.set("trapezoid",new T(n,"trapezoid")),this.tools.set("pentagon",new T(n,"pentagon")),this.tools.set("octagon",new T(n,"octagon")),this.tools.set("star",new T(n,"star")),this.tools.set("cylinder",new T(n,"cylinder")),this.tools.set("cloud",new T(n,"cloud")),this.tools.set("heart",new T(n,"heart")),this.tools.set("block-arrow-right",new T(n,"block-arrow-right")),this.tools.set("block-arrow-left",new T(n,"block-arrow-left")),this.tools.set("block-arrow-up",new T(n,"block-arrow-up")),this.tools.set("block-arrow-down",new T(n,"block-arrow-down")),this.tools.set("block-arrow-double",new T(n,"block-arrow-double")),this.tools.set("right-triangle",new T(n,"right-triangle")),this.tools.set("cross",new T(n,"cross")),this.tools.set("speech-rect",new T(n,"speech-rect")),this.tools.set("speech-round",new T(n,"speech-round")),this.tools.set("cube",new T(n,"cube")),this.tools.set("sector",new T(n,"sector")),this.tools.set("delay",new T(n,"delay")),this.tools.set("predefined-process",new T(n,"predefined-process")),this.tools.set("document",new T(n,"document")),this.tools.set("stadium",new T(n,"stadium")),this.tools.set("class-catalog",new T(n,"class-catalog")),this.tools.set("chevron",new T(n,"chevron")),this.tools.set("brace-left",new T(n,"brace-left")),this.tools.set("brace-right",new T(n,"brace-right")),this.tools.set("uml-class",new T(n,"uml-class")),this.tools.set("uml-interface",new T(n,"uml-interface")),this.tools.set("line",new be(n)),this.tools.set("arrow",new Se(n)),this.tools.set("text",new Ue(n)),this.tools.set("pen",new te(n,"pen")),this.tools.set("highlighter",new te(n,"highlighter")),this.tools.set("eraser",new Re(n)),this.tools.set("mind-map",new T(n,"mind-map")),this.setupEventListeners(),t.shapes?this.shapes=t.shapes.map(s=>({...s,style:{...s.style}})):this.persist&&this.loadFromStorage(),this.render();}setupEventListeners(){this.canvas.addEventListener("pointerdown",this.handlePointerDown),this.canvas.addEventListener("pointerleave",this.handlePointerUp),window.addEventListener("pointermove",this.handlePointerMove),window.addEventListener("pointerup",this.handlePointerUp),window.addEventListener("pointercancel",this.handlePointerUp),this.canvas.addEventListener("wheel",this.handleWheel,{passive:false}),window.addEventListener("keydown",this.handleKeyDown),window.addEventListener("keyup",this.handleKeyUp),window.addEventListener("resize",this.handleResize);}handlePointerDown=e=>{if(this.editingShapeId)return this.textInput&&e.target===this.textInput,void 0;this.pointerIsDown=true;try{this.canvas.hasPointerCapture(e.pointerId)||this.canvas.setPointerCapture(e.pointerId);}catch{}if(e.button===1||e.button===0&&e.altKey){this.isPanning=true,this.panStart={x:e.clientX,y:e.clientY},this.canvas.setPointerCapture(e.pointerId),this.canvas.style.cursor="grabbing";return}let t=this.screenToPage({x:e.clientX,y:e.clientY}),i=this.activeTool==="select"?this.getMindMapCollapseHitAtPoint(t):null;if(i){this.toggleMindMapCollapse(i.shapeId,i.nodeId);return}if(this.activeTool==="select"){let s=Date.now(),r=this.getShapeAtPoint(t);if(s-this.lastClickTime<400&&this.lastClickShapeId&&r&&r.id===this.lastClickShapeId&&this.canEditShape(r)){this.enterEditing(r.id),this.lastClickTime=0,this.lastClickShapeId=null;return}if(this.lastClickTime=s,this.lastClickShapeId=r?.id||null,r&&r.type==="mind-map"&&this.mindMapEditingShapeId===r.id){let o=this.getMindMapNodeAtPoint(t);o&&(this.mindMapSelectedNode=o,this.notifyListeners(),this.render());return}r&&(this.dragStartPositions.clear(),this.selectedIds.forEach(o=>{let h=this.shapes.find(l=>l.id===o);h&&this.dragStartPositions.set(o,{x:h.x,y:h.y});}));}this.tools.get(this.activeTool)?.onPointerDown(t,e),this.updateCursor(t),this.render();};handlePointerMove=e=>{if(this.editingShapeId||!this.pointerIsDown&&e.target!==this.canvas)return;if(this.isPanning){let n=e.clientX-this.panStart.x,s=e.clientY-this.panStart.y;this.camera.translate(n,s),this.panStart={x:e.clientX,y:e.clientY},this.scheduleRender(),this.notifyListeners();return}let t=this.screenToPage({x:e.clientX,y:e.clientY});this.tools.get(this.activeTool)?.onPointerMove(t,e),this.updateCursor(t),this.scheduleRender();};handlePointerUp=e=>{if(e.type==="pointerleave"){if(this.mindMapHoverNode=null,this.pointerIsDown){this.scheduleRender();return}}else {if(!this.pointerIsDown&&e.target!==this.canvas)return;this.canvas.hasPointerCapture(e.pointerId)&&this.canvas.releasePointerCapture(e.pointerId);}if(this.pointerIsDown=false,this.editingShapeId)return;if(this.isPanning){this.isPanning=false,this.updateCursor(this.screenToPage({x:e.clientX,y:e.clientY}));return}let t=this.screenToPage({x:e.clientX,y:e.clientY});this.tools.get(this.activeTool)?.onPointerUp(t,e),this.updateCursor(t),this.scheduleRender();};updateCursor(e){let i=this.tools.get(this.activeTool)?.getCursor(e)??"default";if(this.canvas.style.cursor!==i&&(this.canvas.style.cursor=i),!this.pointerIsDown){let n=this.getMindMapCursorHitAtPoint(e);(n?.shapeId!==this.mindMapHoverNode?.shapeId||n?.nodeId!==this.mindMapHoverNode?.nodeId)&&(this.mindMapHoverNode=n,this.scheduleRender());}}handleWheel=e=>{if(e.preventDefault(),e.ctrlKey||e.metaKey){let n=e.deltaY>0?.9:1.1;this.camera.zoomAt({x:e.clientX,y:e.clientY},n),this.scheduleRender(),this.notifyListeners();return}let t=e.deltaX||0,i=e.deltaY||0;e.deltaMode===1?(t*=40,i*=40):e.deltaMode===2&&(t*=120,i*=120),e.shiftKey&&(t=t||i,i=0),this.camera.translate(-t,-i),this.scheduleRender(),this.notifyListeners();};handleKeyDown=e=>{if(this.editingShapeId)return;if((e.ctrlKey||e.metaKey)&&e.key.toLowerCase()==="z"){e.preventDefault(),e.shiftKey?this.redo():this.undo();return}if((e.ctrlKey||e.metaKey)&&e.key.toLowerCase()==="y"){e.preventDefault(),this.redo();return}if((e.ctrlKey||e.metaKey)&&e.key.toLowerCase()==="s"){e.preventDefault(),this.saveToStorage();return}if(!e.ctrlKey&&!e.metaKey&&!e.altKey){if(e.key==="v"||e.key==="V"){this.setActiveTool("select"),e.preventDefault();return}if(e.key==="h"||e.key==="H"){this.setActiveTool("hand"),e.preventDefault();return}}this.tools.get(this.activeTool)?.onKeyDown(e),this.scheduleRender();};handleKeyUp=e=>{if(this.editingShapeId)return;this.tools.get(this.activeTool)?.onKeyUp(e),this.scheduleRender();};handleResize=()=>{this.renderer.resize(window.innerWidth,window.innerHeight),this.scheduleRender();};screenToPage(e){let t=this.canvas.getBoundingClientRect();return this.camera.screenToPage({x:e.x-t.left,y:e.y-t.top})}addShape(e){this.shapes.push(e),this.notifyListeners();}updateShape(e,t){let i=this.shapes.findIndex(n=>n.id===e);i!==-1&&(this.shapes[i]={...this.shapes[i],...t},(t.x!==void 0||t.y!==void 0)&&this.updateBoundArrows(e),this.notifyListeners());}updateBoundArrows(e){let t=this.shapes.find(i=>i.id===e);t&&this.shapes.forEach(i=>{if(i.type==="arrow"){let n=i,s={};if(n.startBinding?.toShapeId===e){let r=this.getBindingPosition(t,n.startBinding.handle||"top");s.points=n.points.map((o,h)=>h===0?r:o);}if(n.endBinding?.toShapeId===e){let r=this.getBindingPosition(t,n.endBinding.handle||"top"),o=s.points||[...n.points];s.points=o.map((h,l)=>l===o.length-1?r:h);}s.points&&(this.shapes[this.shapes.indexOf(i)]={...n,...s});}});}getBindingPosition(e,t){switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":switch(t){case "top":return {x:e.x,y:e.y-e.height/2};case "bottom":return {x:e.x,y:e.y+e.height/2};case "left":return {x:e.x-e.width/2,y:e.y};case "right":return {x:e.x+e.width/2,y:e.y}}case "circle":switch(t){case "top":return {x:e.x,y:e.y-e.radius};case "bottom":return {x:e.x,y:e.y+e.radius};case "left":return {x:e.x-e.radius,y:e.y};case "right":return {x:e.x+e.radius,y:e.y}}case "hexagon":switch(t){case "top":return {x:e.x,y:e.y-e.height/2};case "bottom":return {x:e.x,y:e.y+e.height/2};case "left":return {x:e.x-e.width/2,y:e.y};case "right":return {x:e.x+e.width/2,y:e.y}}default:return {x:e.x,y:e.y}}}deleteShape(e){this.shapes=this.shapes.filter(t=>t.id!==e),this.selectedIds=this.selectedIds.filter(t=>t!==e),this.shapes=this.shapes.map(t=>{if(t.type==="arrow"){let i=t;if(i.startBinding?.toShapeId===e)return {...i,startBinding:void 0};if(i.endBinding?.toShapeId===e)return {...i,endBinding:void 0}}return t}),this.notifyListeners(),this.scheduleRender();}selectShape(e,t){if(!e)this.selectedIds=[];else if(t){let i=this.selectedIds.indexOf(e);i===-1?this.selectedIds.push(e):this.selectedIds.splice(i,1);}else this.selectedIds=[e];this.notifyListeners(),this.scheduleRender();}selectShapes(e){this.selectedIds=[...e],this.notifyListeners(),this.scheduleRender();}setBrushRect(e){this.brushRect=e,this.scheduleRender();}setSnapGuides(e){this.snapGuides=e;}setConnectingTarget(e){this.connectingTargetId!==e&&(this.connectingTargetId=e,this.scheduleRender());}setGenerationPreview(e){this.genPreview?.from.x===e?.from.x&&this.genPreview?.from.y===e?.from.y&&this.genPreview?.to.x===e?.to.x&&this.genPreview?.to.y===e?.to.y&&this.genPreview?.type===e?.type||(this.genPreview=e,this.scheduleRender());}openGenerationMenu(e){let t=this.camera.getZoom(),i=(e.context.to.x+this.camera.x)*t,n=(e.context.to.y+this.camera.y)*t;this.emitUi({kind:"shapeTypeMenu",screenX:i,screenY:n,context:e.context,existingArrowId:e.existingArrowId,defaultShapeType:e.defaultShapeType});}lineStyle(){return {strokeColor:"#1f2937",fillColor:"transparent",strokeWidth:2,strokeDash:null}}computeSourceBinding(e){if(!e.sourceShapeId||!e.edge)return;let t=this.shapes.find(n=>n.id===e.sourceShapeId);if(!t)return;let i={n:"top",e:"right",s:"bottom",w:"left"};return {toShapeId:t.id,handle:i[e.edge]}}connectHandle(e,t,i){let n=t.x-e.x,s=t.y-e.y;return i==="e"||i==="w"?n>=0?"left":"right":i==="n"||i==="s"?s>=0?"top":"bottom":Math.abs(n)>=Math.abs(s)?n>=0?"left":"right":s>=0?"top":"bottom"}createFloatingLine(e){this.commitHistory();let t=e.from,i=e.to,n=`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,s={id:n,type:"arrow",x:t.x,y:t.y,points:[{x:t.x,y:t.y},{x:i.x,y:i.y}],rotation:0,style:this.lineStyle(),startBinding:this.computeSourceBinding(e),connectionType:"polyline",arrowHead:this.arrowStyle.arrowHead,dashed:this.arrowStyle.dashed};return this.addShape(s),this.selectedIds=[n],this.notifyListeners(),this.render(),n}createGeneratedConnection(e,t){this.commitHistory();let i=t.type,n;if(e.sourceShapeId){let h=this.shapes.find(l=>l.id===e.sourceShapeId);h&&Ae(h.type)&&(n={...h.style});}let s=Ze(i,e.to,n);this.addShape(s);let r=e.from,o={toShapeId:s.id,handle:this.connectHandle(r,e.to,e.edge),auto:true};if(t.existingArrowId)this.updateArrowBinding(t.existingArrowId,"end",s.id,{toShapeId:s.id,handle:this.connectHandle(r,e.to,e.edge),auto:true});else {let l={id:`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,type:"arrow",x:r.x,y:r.y,points:[{x:r.x,y:r.y},{x:e.to.x,y:e.to.y}],rotation:0,style:this.lineStyle(),startBinding:this.computeSourceBinding(e),endBinding:o,connectionType:"polyline",arrowHead:this.arrowStyle.arrowHead,dashed:this.arrowStyle.dashed};this.addShape(l);}this.selectedIds=[s.id],this.notifyListeners(),this.render();}connectToExisting(e,t,i){this.commitHistory();let n=this.shapes.find(c=>c.id===t);if(!n)return;let s=e.from,r=i?.handle??this.connectHandle(s,{x:n.x,y:n.y},e.edge),o=this.getBindingPosition(n,r),h=`shape_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,l={id:h,type:"arrow",x:s.x,y:s.y,points:[{x:s.x,y:s.y},{x:o.x,y:o.y}],rotation:0,style:this.lineStyle(),startBinding:this.computeSourceBinding(e),endBinding:{toShapeId:t,handle:r,auto:!i},connectionType:"polyline",arrowHead:this.arrowStyle.arrowHead,dashed:this.arrowStyle.dashed};this.addShape(l),this.selectedIds=[h],this.notifyListeners(),this.render();}subscribeUi(e){return this.uiListeners.add(e),()=>this.uiListeners.delete(e)}emitUi(e){this.uiListeners.forEach(t=>t(e));}updateSelectedShapesStyle(e){this.selectedIds.length!==0&&(this.commitHistory(),this.selectedIds.forEach(t=>{let i=this.shapes.findIndex(n=>n.id===t);i!==-1&&(this.shapes[i]={...this.shapes[i],style:{...this.shapes[i].style,...e}});}),this.notifyListeners(),this.render());}updateSelectedShapesText(e){this.selectedIds.length!==0&&(this.commitHistory(),this.selectedIds.forEach(t=>{let i=this.shapes.findIndex(n=>n.id===t);i!==-1&&(this.shapes[i]={...this.shapes[i],...e});}),this.notifyListeners(),this.render());}updateConnectionType(e){this.selectedIds.length!==0&&(this.commitHistory(),this.selectedIds.forEach(t=>{let i=this.shapes.findIndex(n=>n.id===t);if(i!==-1){let n=this.shapes[i];(n.type==="line"||n.type==="arrow")&&(this.shapes[i]={...n,connectionType:e});}}),this.notifyListeners(),this.render());}updateArrowHeadType(e){if(this.selectedIds.length===0)return;this.commitHistory();let t=e==="dependency"||e==="realization";this.selectedIds.forEach(i=>{let n=this.shapes.findIndex(s=>s.id===i);if(n!==-1){let s=this.shapes[n];s.type==="arrow"&&(this.shapes[n]=t?{...s,arrowHead:e,dashed:true}:{...s,arrowHead:e});}}),this.notifyListeners(),this.render();}updateArrowStyle(e){this.selectedIds.length!==0&&(this.commitHistory(),this.selectedIds.forEach(t=>{let i=this.shapes.findIndex(o=>o.id===t);if(i===-1)return;let n=this.shapes[i];if(n.type!=="arrow")return;let s=e.arrowHead??n.arrowHead??"end",r=s==="dependency"||s==="realization";this.shapes[i]={...n,...e,arrowHead:s,dashed:r?true:e.dashed??n.dashed??false};}),this.notifyListeners(),this.render());}getArrowStyle(){return {...this.arrowStyle}}setArrowStyle(e){let t={...this.arrowStyle,...e};(t.arrowHead==="dependency"||t.arrowHead==="realization")&&(t.dashed=true),this.arrowStyle=t,this.notifyListeners();}updateArrowDashed(e){this.selectedIds.length!==0&&(this.commitHistory(),this.selectedIds.forEach(t=>{let i=this.shapes.findIndex(n=>n.id===t);if(i!==-1){let n=this.shapes[i];n.type==="arrow"&&(this.shapes[i]={...n,dashed:e});}}),this.notifyListeners(),this.render());}updateUmlClass(e,t){let i=this.shapes.findIndex(c=>c.id===e);if(i===-1)return;let n=this.shapes[i];if(n.type!=="uml-class")return;this.commitHistory();let s=t.className??n.className,r=t.attributes??n.attributes,o=t.methods??n.methods,{width:h,height:l}=$e(s,r,o);this.shapes[i]={...n,className:s,attributes:r,methods:o,width:h,height:l},this.notifyListeners(),this.render();}updateUmlInterface(e,t){let i=this.shapes.findIndex(p=>p.id===e);if(i===-1)return;let n=this.shapes[i];if(n.type!=="uml-interface")return;this.commitHistory();let s=n,r=t.className??s.className,o=t.methods??s.methods,h=G*.6,l=o.map(p=>`${p.visibility} ${p.name}(${p.params}): ${p.returnType}`),c=Math.max(r.length,...l.map(p=>p.length),0),u=Math.max(fe,c*h+O*2),d=o.length>0?o.length*$+E*2:E*2,g=j+d;this.shapes[i]={...n,className:r,methods:o,width:u,height:g},this.notifyListeners(),this.render();}getSelectedUml(){if(this.selectedIds.length!==1)return null;let e=this.shapes.find(t=>t.id===this.selectedIds[0]);return e&&(e.type==="uml-class"||e.type==="uml-interface")?e:null}getUmlById(e){let t=this.shapes.find(i=>i.id===e);return t&&(t.type==="uml-class"||t.type==="uml-interface")?t:null}getSelectedMindMap(){if(this.selectedIds.length!==1)return null;let e=this.shapes.find(t=>t.id===this.selectedIds[0]);return e&&e.type==="mind-map"?e:null}getMindMapNodeAtPoint(e){for(let t=this.shapes.length-1;t>=0;t--){let i=this.shapes[t];if(i.type!=="mind-map")continue;let n=i,s=e.x-n.x,r=e.y-n.y,o=Math.cos(n.rotation),h=Math.sin(n.rotation),l=s*o+r*h,c=-s*h+r*o;if(Math.abs(l)>n.width/2||Math.abs(c)>n.height/2)continue;let u=B(n.root);for(let[d,g]of u.positions)if(Math.abs(l-g.x)<=g.width/2&&Math.abs(c-g.y)<=g.height/2)return {shapeId:n.id,nodeId:d};return {shapeId:n.id,nodeId:n.root.id}}return null}getMindMapNodeAtScreenPoint(e){return this.getMindMapNodeAtPoint(this.screenToPage(e))}getMindMapCursorHitAtPoint(e){let t=this.getMindMapCollapseHitAtPoint(e);if(t)return t;for(let i=this.shapes.length-1;i>=0;i--){let n=this.shapes[i];if(n.type!=="mind-map")continue;let s=n,r=e.x-s.x,o=e.y-s.y,h=Math.cos(s.rotation),l=Math.sin(s.rotation),c=r*h+o*l,u=-r*l+o*h,d=B(s.root);for(let[g,p]of d.positions)if(Math.abs(c-p.x)<=p.width/2&&Math.abs(u-p.y)<=p.height/2)return {shapeId:s.id,nodeId:g}}return null}selectMindMapNode(e,t){let i=this.shapes.find(n=>n.id===e);!i||i.type!=="mind-map"||(this.selectedIds=[e],this.mindMapSelectedNode={shapeId:e,nodeId:t},this.notifyListeners(),this.render());}getMindMapEditingShapeId(){return this.mindMapEditingShapeId}getMindMapSelectedNode(){return this.mindMapSelectedNode}getMindMapNode(e,t){let i=this.shapes.find(n=>n.id===e);return !i||i.type!=="mind-map"?null:Q(i.root,t)}getMindMapNodeScreenRect(e,t){let i=this.shapes.find(w=>w.id===e);if(!i||i.type!=="mind-map")return null;let n=i,r=B(n.root).positions.get(t);if(!r)return null;let o=this.camera.getZoom(),h=r.x,l=r.y,c=Math.cos(n.rotation),u=Math.sin(n.rotation),d=h*c-l*u,g=h*u+l*c,p=this.camera.pageToScreen({x:n.x,y:n.y}),y=p.x+d*o,f=p.y+g*o;return {x:y-r.width*o/2,y:f-r.height*o/2,width:r.width*o,height:r.height*o}}getMindMapCollapseHitAtPoint(e){for(let t of this.shapes){if(t.type!=="mind-map")continue;let i=t,n=B(i.root),s=e.x-i.x,r=e.y-i.y,o=Math.cos(i.rotation),h=Math.sin(i.rotation),l=s*o+r*h,c=-s*h+r*o;for(let[u,d]of n.positions){let g=Q(i.root,u);if(!g||!g.children||g.children.length===0)continue;let p=d.x+d.width/2+10,y=d.y,f=l-p,w=c-y;if(f*f+w*w<=121)return {shapeId:t.id,nodeId:u}}}return null}beginMindMapEdit(e,t){let i=this.shapes.find(s=>s.id===e);if(!i||i.type!=="mind-map")return;let n=i;this.mindMapEditingShapeId=e,this.mindMapSelectedNode={shapeId:e,nodeId:t&&Q(n.root,t)?t:n.root.id},this.selectedIds=[e],this.notifyListeners(),this.render();}exitMindMapEdit(){this.mindMapEditingShapeId=null,this.mindMapSelectedNode=null,this.notifyListeners(),this.render();}updateMindMapNode(e,t,i,n=true){let s=this.shapes.findIndex(g=>g.id===e);if(s===-1)return;let r=this.shapes[s];if(r.type!=="mind-map")return;let o=r,h=Q(o.root,t);if(!h||!Object.keys(i).some(g=>h[g]!==i[g]))return;n&&this.commitHistory();let c=Ee(o.root,t,i),{width:u,height:d}=B(c);this.shapes[s]={...o,root:c,width:u,height:d},this.notifyListeners(),this.render();}addMindMapChild(e,t){let i=this.shapes.findIndex(c=>c.id===e);if(i===-1)return;let n=this.shapes[i];if(n.type!=="mind-map")return;let s=n,r={id:`mm_${Date.now()}_${Math.random().toString(36).substr(2,6)}`,text:Z("mindmap.defaultBranch")},o=We(s.root,t,r);this.commitHistory();let{width:h,height:l}=B(o);this.shapes[i]={...s,root:o,width:h,height:l},this.mindMapSelectedNode={shapeId:e,nodeId:r.id},this.notifyListeners(),this.render();}addMindMapSibling(e,t){let i=this.shapes.findIndex(c=>c.id===e);if(i===-1)return;let n=this.shapes[i];if(n.type!=="mind-map")return;let s=n;if(!ye(s.root,t))return;let r={id:`mm_${Date.now()}_${Math.random().toString(36).substr(2,6)}`,text:Z("mindmap.defaultBranch")},o=Ke(s.root,t,r);this.commitHistory();let{width:h,height:l}=B(o);this.shapes[i]={...s,root:o,width:h,height:l},this.mindMapSelectedNode={shapeId:e,nodeId:r.id},this.notifyListeners(),this.render();}deleteMindMapNode(e,t){let i=this.shapes.findIndex(c=>c.id===e);if(i===-1)return;let n=this.shapes[i];if(n.type!=="mind-map")return;let s=n;if(s.root.id===t){this.commitHistory(),this.shapes=this.shapes.filter(c=>c.id!==e),this.selectedIds=this.selectedIds.filter(c=>c!==e),this.mindMapEditingShapeId=null,this.mindMapSelectedNode=null,this.notifyListeners(),this.render();return}let r=Fe(s.root,t);if(!r)return;this.commitHistory();let{width:o,height:h}=B(r);this.shapes[i]={...s,root:r,width:o,height:h};let l=ye(s.root,t);this.mindMapSelectedNode=l?{shapeId:e,nodeId:l.id}:{shapeId:e,nodeId:r.id},this.notifyListeners(),this.render();}toggleMindMapCollapse(e,t){let i=this.shapes.findIndex(c=>c.id===e);if(i===-1)return;let n=this.shapes[i];if(n.type!=="mind-map")return;let s=n,r=Q(s.root,t);if(!r||!r.children||r.children.length===0)return;this.commitHistory();let o=Ee(s.root,t,{collapsed:!r.collapsed}),{width:h,height:l}=B(o);this.shapes[i]={...s,root:o,width:h,height:l},this.notifyListeners(),this.render();}addUmlAttribute(e){let t=this.shapes.find(n=>n.id===e);if(!t||t.type!=="uml-class")return;let i={id:`attr_${Date.now()}_${Math.random().toString(36).slice(2,8)}`,visibility:"+",name:"newField",type:"string"};this.updateUmlClass(e,{attributes:[...t.attributes,i]});}addUmlMethod(e){let t=this.getUmlById(e);if(!t)return;let i={id:`meth_${Date.now()}_${Math.random().toString(36).slice(2,8)}`,visibility:"+",name:"newMethod",params:"",returnType:"void"};t.type==="uml-class"?this.updateUmlClass(e,{methods:[...t.methods,i]}):this.updateUmlInterface(e,{methods:[...t.methods,i]});}removeUmlAttribute(e,t){let i=this.shapes.find(n=>n.id===e);!i||i.type!=="uml-class"||this.updateUmlClass(e,{attributes:i.attributes.filter(n=>n.id!==t)});}removeUmlMethod(e,t){let i=this.getUmlById(e);if(!i)return;let n=i.methods.filter(s=>s.id!==t);i.type==="uml-class"?this.updateUmlClass(e,{methods:n}):this.updateUmlInterface(e,{methods:n});}reorderUmlAttribute(e,t,i){let n=this.shapes.find(o=>o.id===e);if(!n||n.type!=="uml-class"||t===i)return;let s=[...n.attributes];if(t<0||t>=s.length||i<0||i>=s.length)return;let[r]=s.splice(t,1);s.splice(i,0,r),this.updateUmlClass(e,{attributes:s});}reorderUmlMethod(e,t,i){let n=this.getUmlById(e);if(!n||t===i)return;let s=[...n.methods];if(t<0||t>=s.length||i<0||i>=s.length)return;let[r]=s.splice(t,1);s.splice(i,0,r),n.type==="uml-class"?this.updateUmlClass(e,{methods:s}):this.updateUmlInterface(e,{methods:s});}updateUmlRowStyle(e,t,i,n){let s=this.getUmlById(e);if(s)if(t==="attribute"){if(s.type!=="uml-class")return;let r=s.attributes.map(o=>o.id===i?{...o,textColor:n.textColor===null?void 0:n.textColor??o.textColor,fontSize:n.fontSize===null?void 0:n.fontSize??o.fontSize,strikethrough:n.strikethrough===null?void 0:n.strikethrough??o.strikethrough}:o);this.updateUmlClass(e,{attributes:r});}else {let r=s.methods.map(o=>o.id===i?{...o,textColor:n.textColor===null?void 0:n.textColor??o.textColor,fontSize:n.fontSize===null?void 0:n.fontSize??o.fontSize,strikethrough:n.strikethrough===null?void 0:n.strikethrough??o.strikethrough}:o);s.type==="uml-class"?this.updateUmlClass(e,{methods:r}):this.updateUmlInterface(e,{methods:r});}}getUmlRowAtScreenPoint(e){let t=this.camera.screenToPage(e);for(let i=this.shapes.length-1;i>=0;i--){let n=this.shapes[i];if(n.type!=="uml-class"&&n.type!=="uml-interface")continue;let s=n.width/2,r=n.height/2;if(t.x<n.x-s||t.x>n.x+s||t.y<n.y-r||t.y>n.y+r)continue;let o=j,h=t.y-n.y;if(h>=-r&&h<=-r+o)return {shape:n,section:"className",rowIndex:-1,text:n.className};if(n.type==="uml-interface"){let p=-r+o,y=r;if(n.methods.length>0&&h>=p&&h<=y){let f=Math.floor((h-p-E)/$);if(f>=0&&f<n.methods.length){let w=n.methods[f];return {shape:n,section:"method",rowIndex:f,text:`${w.visibility} ${w.name}(${w.params}): ${w.returnType}`}}return {shape:n,section:"method",rowIndex:-1,text:""}}return {shape:n,section:"className",rowIndex:-1,text:n.className}}let l=n.attributes.length>0?n.attributes.length*$+E*2:E*2,c=-r+o,u=c+l;if(n.attributes.length>0&&h>=c&&h<=u){let p=Math.floor((h-c-E)/$);if(p>=0&&p<n.attributes.length){let y=n.attributes[p];return {shape:n,section:"attribute",rowIndex:p,text:`${y.visibility} ${y.name}: ${y.type}`}}return {shape:n,section:"attribute",rowIndex:-1,text:""}}let d=u,g=r;if(n.methods.length>0&&h>=d&&h<=g){let p=Math.floor((h-d-E)/$);if(p>=0&&p<n.methods.length){let y=n.methods[p];return {shape:n,section:"method",rowIndex:p,text:`${y.visibility} ${y.name}(${y.params}): ${y.returnType}`}}return {shape:n,section:"method",rowIndex:-1,text:""}}return {shape:n,section:"className",rowIndex:-1,text:n.className}}return null}setActiveTool(e){this.tools.has(e)&&(this.activeTool=e,this.eraserPoint=null,this.notifyListeners());}getActiveTool(){return this.activeTool}setHighlighterColor(e){let t=this.tools.get("highlighter");t instanceof te&&t.setHighlighterColor(e);}getHighlighterColor(){let e=this.tools.get("highlighter");return e instanceof te?e.getHighlighterColor():Oe}getShapes(){return [...this.shapes]}getData(){return {shapes:this.getShapes(),camera:{x:this.camera.x,y:this.camera.y,zoom:this.camera.zoom}}}setShapes(e){return Array.isArray(e)?(this.shapes=e.map(t=>({...t,style:{...t.style}})),this.selectedIds=[],this.undoStack=[],this.redoStack=[],this.notifyListeners(),this.render(),true):false}setTheme(e){Ie(e),this.scheduleRender();}setLanguage(e){ke(e);}getSelectedIds(){return [...this.selectedIds]}getCamera(){return this.camera}isPointerDown(){return this.pointerIsDown}getContentBounds(e=24){return Pe(this.shapes,e)}async exportImage(e,t={}){return rt(this.getShapes(),e,t)}async downloadImage(e="drawui",t="png",i={}){let n=await this.exportImage(t,i);ot(n,`${e}.${t}`);}zoomIn(){this.camera.zoomAt({x:window.innerWidth/2,y:window.innerHeight/2},1.2),this.scheduleRender(),this.notifyListeners();}zoomOut(){this.camera.zoomAt({x:window.innerWidth/2,y:window.innerHeight/2},.8),this.scheduleRender(),this.notifyListeners();}resetZoom(){this.camera.reset(),this.scheduleRender(),this.notifyListeners();}panBy(e,t){this.camera.translate(e,t),this.scheduleRender(),this.notifyListeners();}setCameraPosition(e,t){this.camera.x=e,this.camera.y=t,this.scheduleRender(),this.notifyListeners();}requestRender(){this.scheduleRender(),this.notifyListeners();}setShowGrid(e){this.renderer.setShowGrid(e),this.scheduleRender();}render(){this.renderer.render(this.shapes,this.camera,new Set(this.selectedIds),this.brushRect,this.snapGuides,this.connectingTargetId,this.genPreview,this.eraserPoint,this.mindMapSelectedNode,this.mindMapHoverNode);}scheduleRender(){this.renderScheduled||(this.renderScheduled=true,requestAnimationFrame(()=>{this.renderScheduled=false,this.render();}));}subscribe(e){return this.listeners.add(e),()=>this.listeners.delete(e)}subscribeNotifications(e){return this.notificationListeners.add(e),()=>this.notificationListeners.delete(e)}notify(e){this.notificationListeners.forEach(t=>t(e));}notifyListeners(){this.notifyScheduled||(this.notifyScheduled=true,queueMicrotask(()=>{this.notifyScheduled=false,this.mindMapEditingShapeId&&!this.selectedIds.includes(this.mindMapEditingShapeId)&&(this.mindMapEditingShapeId=null,this.mindMapSelectedNode=null),this.listeners.forEach(e=>e());}));}cloneShapes(e){return e.map(t=>{let i={...t,style:{...t.style}};return Array.isArray(t.points)&&(i.points=t.points.map(n=>({...n}))),t.startBinding&&(i.startBinding={...t.startBinding}),t.endBinding&&(i.endBinding={...t.endBinding}),t.root&&(i.root=De(t.root)),i})}commitHistory(){if(this.isUndoing)return;let e={shapes:this.cloneShapes(this.shapes),selectedIds:[...this.selectedIds]};this.undoStack.push(e),this.undoStack.length>An&&this.undoStack.shift(),this.redoStack=[];}mark(){this.commitHistory();}undo(){if(this.undoStack.length!==0){this.isUndoing=true;try{this.redoStack.push({shapes:this.cloneShapes(this.shapes),selectedIds:[...this.selectedIds]});let e=this.undoStack.pop();this.shapes=this.cloneShapes(e.shapes),this.selectedIds=[...e.selectedIds];}finally{this.isUndoing=false;}this.notifyListeners(),this.render();}}redo(){if(this.redoStack.length!==0){this.isUndoing=true;try{this.undoStack.push({shapes:this.cloneShapes(this.shapes),selectedIds:[...this.selectedIds]});let e=this.redoStack.pop();this.shapes=this.cloneShapes(e.shapes),this.selectedIds=[...e.selectedIds];}finally{this.isUndoing=false;}this.notifyListeners(),this.render();}}canUndo(){return this.undoStack.length>0}canRedo(){return this.redoStack.length>0}saveToStorage(){if(!this.persist)return false;try{let e=JSON.stringify({version:1,shapes:this.shapes,savedAt:Date.now()});return localStorage.setItem(at,e),this.notifyListeners(),this.notify(Z("save.success")),!0}catch(e){return console.error("\u4FDD\u5B58\u5931\u8D25:",e),this.notify(Z("save.fail")),false}}loadFromStorage(){if(!this.persist)return false;try{let e=localStorage.getItem(at);if(!e)return !1;let t=JSON.parse(e);return t.shapes&&Array.isArray(t.shapes)?(this.shapes=t.shapes,this.selectedIds=[],this.undoStack=[],this.redoStack=[],this.notifyListeners(),this.scheduleRender(),!0):!1}catch(e){return console.error("\u52A0\u8F7D\u5931\u8D25:",e),false}}clearStorage(){localStorage.removeItem(at);}exportJSON(){return JSON.stringify({version:1,shapes:this.shapes,savedAt:Date.now()},null,2)}importJSON(e){try{let t=JSON.parse(e);return t.shapes&&Array.isArray(t.shapes)?(this.commitHistory(),this.shapes=t.shapes,this.selectedIds=[],this.notifyListeners(),this.render(),!0):!1}catch(t){return console.error("\u5BFC\u5165\u5931\u8D25:",t),false}}getEditingShapeId(){return this.editingShapeId}canEditShape(e){return !(e.type==="line"||e.type==="arrow"||e.type==="draw"||e.type==="uml-class"||e.type==="uml-interface"||e.type==="mind-map")}enterEditing(e){let t=this.shapes.find(i=>i.id===e);!t||!this.canEditShape(t)||(this.editingShapeId=e,this.selectedIds=[e],this.isNewTextShape=t.type==="text"&&!t.text,this.originalTextValue=t.text||"",this.notifyListeners(),this.scheduleRender(),requestAnimationFrame(()=>{this.createTextInput(t);}));}exitEditing(){if(!this.editingShapeId)return;let e=this.editingShapeId,t=this.shapes.find(n=>n.id===e),i=this.textInput?this.textInput.value:"";if(this.removeTextInput(),t)if(t.type==="text")if(this.commitHistory(),!i.trim()&&this.isNewTextShape)this.shapes=this.shapes.filter(n=>n.id!==e),this.selectedIds=this.selectedIds.filter(n=>n!==e);else {let n=t,s=n.fontSize||16,r=s*1.3,o=n.width||200,h=i.split(`
|
|
42
|
+
`),l=0;for(let d of h){let g="";for(let p of d){let y=g+p;y.length*s*.6>o&&g?(l++,g=p):g=y;}g&&l++;}let c={...n,text:i,height:Math.max(l*r,r)},u=this.shapes.indexOf(t);u!==-1&&(this.shapes[u]=c),i.trim()||(this.shapes=this.shapes.filter(d=>d.id!==e),this.selectedIds=this.selectedIds.filter(d=>d!==e));}else this.commitHistory(),this.updateShape(e,{text:i,textColor:t.textColor||L.text,fontSize:t.fontSize||16});this.editingShapeId=null,this.isNewTextShape=false,this.originalTextValue="",this.setActiveTool("select"),this.notifyListeners(),this.render();}removeTextInput(){this.textOverlay&&(this.textOverlay.remove(),this.textOverlay=null),this.textInput&&(this.textInput.remove(),this.textInput=null);}createTextInput(e){this.removeTextInput();let t=this.canvas.parentElement;if(!t)return;let i=this.canvas.getBoundingClientRect(),n=t.getBoundingClientRect(),s=i.left-n.left,r=i.top-n.top;this.textOverlay=document.createElement("div"),this.textOverlay.style.position="absolute",this.textOverlay.style.left=`${s}px`,this.textOverlay.style.top=`${r}px`,this.textOverlay.style.width=`${i.width}px`,this.textOverlay.style.height=`${i.height}px`,this.textOverlay.style.background="transparent",this.textOverlay.style.zIndex="1000",this.textOverlay.style.cursor="text",this.textOverlay.addEventListener("pointerdown",u=>{this.textInput&&u.target!==this.textInput&&(u.preventDefault(),this.exitEditing());}),t.appendChild(this.textOverlay);let o=this.getShapeScreenBounds(e),h=this.camera.getZoom(),l=(e.fontSize||16)*h,c=document.createElement("textarea");c.style.position="absolute",c.style.left=`${s+o.x}px`,c.style.top=`${r+o.y}px`,c.style.width=`${Math.max(o.width,60)}px`,c.style.height=`${Math.max(o.height,24)}px`,c.style.fontSize=`${l}px`,c.style.color=e.textColor||"#1f2937",c.style.textAlign="center",c.style.border=`2px solid ${L.editBorder}`,c.style.borderRadius="4px",c.style.background="rgba(255, 255, 255, 0.95)",c.style.resize="none",c.style.padding="4px 8px",c.style.boxSizing="border-box",c.style.lineHeight="1.4",c.style.fontFamily="sans-serif",c.style.outline="none",c.style.whiteSpace="pre-wrap",c.style.wordBreak="break-word",c.style.overflow="hidden",c.style.zIndex="1001",c.value=e.text||"",c.dataset.shapeId=e.id,c.addEventListener("pointerdown",u=>{u.stopPropagation();}),c.addEventListener("mousedown",u=>{u.stopPropagation();}),c.addEventListener("keydown",u=>{if(u.stopPropagation(),u.key==="Escape"&&(u.preventDefault(),this.textInput&&(this.textInput.value=this.originalTextValue),this.exitEditing()),u.key==="Enter"&&!u.shiftKey&&(u.preventDefault(),this.exitEditing()),u.key==="Enter"&&u.shiftKey){u.preventDefault();let d=c.selectionStart,g=c.selectionEnd;c.value=c.value.substring(0,d)+`
|
|
43
|
+
`+c.value.substring(g),c.selectionStart=c.selectionEnd=d+1;}}),t.appendChild(c),c.focus(),e.type==="text"&&!e.text&&(c.placeholder="\u8F93\u5165\u6587\u672C..."),this.textInput=c;}getShapeScreenBounds(e){let t=this.camera.getZoom(),i=this.camera.pageToScreen({x:e.x,y:e.y});switch(e.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "hexagon":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":return {x:i.x-e.width*t/2,y:i.y-e.height*t/2,width:e.width*t,height:e.height*t};case "circle":return {x:i.x-e.radius*t,y:i.y-e.radius*t,width:e.radius*2*t,height:e.radius*2*t};case "text":{let n=e,s=n.width*t,r=n.height*t;return {x:i.x-s/2,y:i.y-r/2,width:s,height:r}}default:return {x:i.x-50*t,y:i.y-20*t,width:100*t,height:40*t}}}getShapeAtScreenPoint(e){let t=this.screenToPage(e);return this.getShapeAtPoint(t)}getShapeAtPoint(e,t){for(let i=this.shapes.length-1;i>=0;i--){let n=this.shapes[i];if(!(t&&n.id===t)&&this.isPointInShape(e,n))return n}return null}getShapesInBrushRadius(e,t){let i=[];for(let n of this.shapes)this.shapeIntersectsBrush(n,e,t)&&i.push(n);return i}shapeIntersectsBrush(e,t,i){switch(e.type){case "line":case "arrow":{let n=e.points,s=i+4;for(let r=1;r<n.length;r++)if(this.pointToSegmentDist(t,n[r-1],n[r])<=s)return true;return n.length>0&&Math.hypot(t.x-n[0].x,t.y-n[0].y)<=s}case "draw":{let n=e.points,s=i+4;for(let r=1;r<n.length;r++){let o={x:e.x+n[r-1].x,y:e.y+n[r-1].y},h={x:e.x+n[r].x,y:e.y+n[r].y};if(this.pointToSegmentDist(t,o,h)<=s)return true}return n.length>0&&Math.hypot(t.x-(e.x+n[0].x),t.y-(e.y+n[0].y))<=s}case "circle":return Math.hypot(t.x-e.x,t.y-e.y)<=e.radius+i;case "text":{let n=e;return Math.abs(t.x-e.x)<=n.width/2+i&&Math.abs(t.y-e.y)<=n.height/2+i}default:return Math.abs(t.x-e.x)<=e.width/2+i&&Math.abs(t.y-e.y)<=e.height/2+i}}pointToSegmentDist(e,t,i){let n=i.x-t.x,s=i.y-t.y,r=n*n+s*s,o=r===0?0:Math.max(0,Math.min(1,((e.x-t.x)*n+(e.y-t.y)*s)/r));return Math.hypot(e.x-(t.x+o*n),e.y-(t.y+o*s))}setEraserPoint(e){this.eraserPoint=e,this.scheduleRender();}isPointInShape(e,t){let i=e.x-t.x,n=e.y-t.y;switch(t.type){case "rectangle":case "rounded-rectangle":case "ellipse":case "diamond":case "triangle":case "parallelogram":case "trapezoid":case "pentagon":case "octagon":case "star":case "cylinder":case "cloud":case "heart":case "block-arrow-right":case "block-arrow-left":case "block-arrow-up":case "block-arrow-down":case "block-arrow-double":case "right-triangle":case "cross":case "speech-rect":case "speech-round":case "cube":case "sector":case "delay":case "predefined-process":case "document":case "stadium":case "class-catalog":case "chevron":case "brace-left":case "brace-right":case "uml-class":case "uml-interface":return Math.abs(i)<=t.width/2&&Math.abs(n)<=t.height/2;case "circle":return i*i+n*n<=t.radius*t.radius;case "hexagon":{let s=t.width/2,r=t.height/2;return Math.abs(i)<=s&&Math.abs(n)<=r&&Math.abs(i)/s+Math.abs(n)/r<=1.5}case "mind-map":return Math.abs(i)<=t.width/2&&Math.abs(n)<=t.height/2;case "text":{let s=t;return Math.abs(i)<=s.width/2&&Math.abs(n)<=s.height/2}default:return false}}updateArrowBinding(e,t,i,n){let s=this.shapes.find(h=>h.id===e);if(!s||s.type!=="arrow")return;let r=s,o={};if(i){let h=this.shapes.find(d=>d.id===i);if(!h)return;let l=n?.handle||this.getClosestHandle(r,h),c=this.getBindingPosition(h,l),u=n?{...n,toShapeId:i}:{toShapeId:i,handle:l,auto:true};t==="start"?(o.startBinding=u,o.points=r.points.map((d,g)=>g===0?c:d)):(o.endBinding=u,o.points=r.points.map((d,g)=>g===r.points.length-1?c:d));}else t==="start"?o.startBinding=void 0:o.endBinding=void 0;this.updateShape(e,o);}getClosestHandle(e,t){let i=e.points[0],n=e.points[e.points.length-1],s=e.endBinding?n:i,r=s.x-t.x,o=s.y-t.y;return Math.abs(r)>Math.abs(o)?r>0?"right":"left":o>0?"bottom":"top"}dispose(){this.canvas.removeEventListener("pointerdown",this.handlePointerDown),this.canvas.removeEventListener("pointerleave",this.handlePointerUp),window.removeEventListener("pointermove",this.handlePointerMove),window.removeEventListener("pointerup",this.handlePointerUp),window.removeEventListener("pointercancel",this.handlePointerUp),this.canvas.removeEventListener("wheel",this.handleWheel),window.removeEventListener("keydown",this.handleKeyDown),window.removeEventListener("keyup",this.handleKeyUp),window.removeEventListener("resize",this.handleResize),this.removeTextInput(),this.tools.forEach(e=>e.dispose()),this.listeners.clear();}};exports.ArrowTool=Se;exports.Camera=se;exports.DEFAULT_HIGHLIGHTER_COLOR=Oe;exports.Editor=ht;exports.FreehandTool=te;exports.HandTool=we;exports.LineTool=be;exports.MINDMAP_COLORS=Xe;exports.MINDMAP_FONT_SIZE=J;exports.MINDMAP_MAX_WIDTH=Rt;exports.MINDMAP_MIN_WIDTH=$t;exports.MINDMAP_NODE_HEIGHT=At;exports.Mat2d=q;exports.Renderer=oe;exports.SelectTool=xe;exports.ShapeTool=T;exports.THEME=L;exports.UML_FONT_SIZE=G;exports.UML_HEADER_HEIGHT=j;exports.UML_LINE_HEIGHT=$;exports.UML_MIN_WIDTH=fe;exports.UML_PADDING_X=O;exports.UML_PADDING_Y=E;exports.Vec=ae;exports.calcUmlClassSize=$e;exports.cloneMindMapNode=De;exports.countMindMapNodes=pt;exports.downloadBlob=ot;exports.exportToBlob=rt;exports.findMindMapNode=Q;exports.getContentBounds=Pe;exports.getLanguage=Dt;exports.getMindMapNodeDepth=gt;exports.getMindMapParent=ye;exports.getTheme=Lt;exports.hexToRgba=F;exports.layoutMindMap=B;exports.mindMapNodeColor=re;exports.mindMapNodeSize=ut;exports.renderSVGString=Ct;exports.renderToCanvas=vt;exports.setLanguage=ke;exports.setTheme=Ie;exports.shadeColor=kt;exports.subscribeLanguage=Et;exports.subscribeTheme=It;exports.t=Z;
|