@linkurious/ogma-annotations 2.0.2 → 2.0.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.js CHANGED
@@ -3,12 +3,12 @@
3
3
  </svg>`,re=`<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
4
4
  <path d="M12 6.00015H7.33333C6.97971 6.00015 6.64057 6.14063 6.39052 6.39068C6.14048 6.64072 6 6.97986 6 7.33348V16.6668C6 17.0204 6.14048 17.3596 6.39052 17.6096C6.64057 17.8597 6.97971 18.0002 7.33333 18.0002H16.6667C17.0203 18.0002 17.3594 17.8597 17.6095 17.6096C17.8595 17.3596 18 17.0204 18 16.6668V12.0002M16.25 5.75015C16.5152 5.48493 16.8749 5.33594 17.25 5.33594C17.6251 5.33594 17.9848 5.48493 18.25 5.75015C18.5152 6.01537 18.6642 6.37508 18.6642 6.75015C18.6642 7.12522 18.5152 7.48493 18.25 7.75015L12.2413 13.7595C12.083 13.9176 11.8875 14.0334 11.6727 14.0962L9.75733 14.6562C9.69997 14.6729 9.63916 14.6739 9.58127 14.6591C9.52339 14.6442 9.47055 14.6141 9.4283 14.5719C9.38604 14.5296 9.35593 14.4768 9.3411 14.4189C9.32627 14.361 9.32727 14.3002 9.344 14.2428L9.904 12.3275C9.96702 12.1129 10.083 11.9175 10.2413 11.7595L16.25 5.75015Z" stroke="#1A70E5" stroke-width="1.33333" stroke-linecap="round" stroke-linejoin="round"/>
5
5
  </svg>
6
- `;let ut=(i=21)=>crypto.getRandomValues(new Uint8Array(i)).reduce((e,t)=>(t&=63,t<36?e+=t.toString(36):t<62?e+=(t-26).toString(36).toUpperCase():t>62?e+="-":e+="_",e),"");function os(i,e){const t=i[0]-e[0],n=i[1]-e[1];return t*t+n*n}function as(i,e,t){let n=e[0],s=e[1],r=t[0]-n,o=t[1]-s;if(r!==0||o!==0){const a=((i[0]-n)*r+(i[1]-s)*o)/(r*r+o*o);a>1?(n=t[0],s=t[1]):a>0&&(n+=r*a,s+=o*a)}return r=i[0]-n,o=i[1]-s,r*r+o*o}function ls(i,e){let t=i[0];const n=[t];let s;for(let r=1,o=i.length;r<o;r++)s=i[r],os(s,t)>e&&(n.push(s),t=s);return t!==s&&n.push(s),n}function oe(i,e,t,n,s){let r=n,o=0;for(let a=e+1;a<t;a++){const c=as(i[a],i[e],i[t]);c>r&&(o=a,r=c)}r>n&&(o-e>1&&oe(i,e,o,n,s),s.push(i[o]),t-o>1&&oe(i,o,t,n,s))}function cs(i,e){const t=i.length-1,n=[i[0]];return oe(i,0,t,e,n),n.push(i[t]),n}function ae(i,e,t){if(i.length<=2)return i;const n=e!==void 0?e*e:1;return i=t?i:ls(i,n),i=cs(i,n),i}function le(i){if(i.geometry.bbox)return i.geometry.bbox;const e=i.geometry.coordinates[0];let t=1/0,n=1/0,s=-1/0,r=-1/0;for(const[o,a]of e)o<t&&(t=o),o>s&&(s=o),a<n&&(n=a),a>r&&(r=a);return[t,n,s,r]}function ce(i){const e=i.geometry.coordinates[0];let t=0,n=0;const s=e.length-1;for(let r=0;r<s;r++)t+=e[r][0],n+=e[r][1];return{x:t/s,y:n/s}}function hs(i,e,t){const n=i.geometry.coordinates.map(o=>o.map(([a,c])=>[a+e,c+t])),s=i.geometry.bbox,r=s?[s[0]+e,s[1]+t,s[2]+e,s[3]+t]:void 0;return{...i,geometry:{...i.geometry,coordinates:n,bbox:r}}}function he(i){const e=i.geometry.coordinates[0];let t=1/0,n=1/0,s=-1/0,r=-1/0;for(const[o,a]of e)o<t&&(t=o),o>s&&(s=o),a<n&&(n=a),a>r&&(r=a);i.geometry.bbox=[t,n,s,r]}function ds(i,e,t,n){const s=i.geometry.coordinates.map(o=>o.map(([a,c])=>{const h=a-t,l=c-n;return[t+h*e,n+l*e]})),r={...i,geometry:{...i.geometry,coordinates:s}};return he(r),r}const us="http://www.w3.org/2000/svg";function k(i){return document.createElementNS(us,i)}function st(i){return i.geometry.bbox||ft(i),i.geometry.bbox}function Y(i){if("width"in i.properties&&"height"in i.properties)return{width:i.properties.width,height:i.properties.height};const e=st(i);return{width:e[2]-e[0],height:e[3]-e[1]}}function gt(i,e=!1,t=1){if(i.geometry.type==="Point"&&"width"in i.properties&&"height"in i.properties){const[s,r]=i.geometry.coordinates;let o=i.properties.width,a=i.properties.height;return e&&(o/=t,a/=t),{x:s-o/2,y:r-a/2}}const n=st(i);return{x:n[0],y:n[1]}}function J(i){if(i.geometry.type==="Point")return{x:i.geometry.coordinates[0],y:i.geometry.coordinates[1]};const e=st(i);return{x:(e[0]+e[2])/2,y:(e[1]+e[3])/2}}function ft(i){if(tt(i)){const[e,t]=i.geometry.coordinates[0],[n,s]=i.geometry.coordinates[1];i.geometry.bbox=[Math.min(e,n),Math.min(t,s),Math.max(e,n),Math.max(t,s)]}else if(i.geometry.type==="Point"&&"width"in i.properties&&"height"in i.properties){const[e,t]=i.geometry.coordinates,n=i.properties.width/2,s=i.properties.height/2;i.geometry.bbox=[e-n,t-s,e+n,t+s]}else if(i.geometry.type==="Polygon"){const e=i.geometry.coordinates[0];let t=Number.POSITIVE_INFINITY,n=Number.POSITIVE_INFINITY,s=Number.NEGATIVE_INFINITY,r=Number.NEGATIVE_INFINITY;for(const o of e){const a=o[0],c=o[1];a<t&&(t=a),c<n&&(n=c),a>s&&(s=a),c>r&&(r=c)}i.geometry.bbox=[t,n,s,r]}}function Ke(i,e,t,n,s){i.geometry.coordinates=[e+n/2,t+s/2],i.geometry.bbox=[e,t,e+n,t+s],i.properties.width=n,i.properties.height=s}function de(i){const[e,t]=i.geometry.coordinates[0];return{x:e,y:t}}function Dt(i,e){const[t,n]=i.geometry.coordinates[e===O?0:1];return{x:t,y:n}}function ue(i){const[e,t]=i.geometry.coordinates[1];return{x:e,y:t}}function Je(i,e,t){i.geometry.coordinates[0]=[e,t]}function Qe(i,e,t){i.geometry.coordinates[1]=[e,t]}function Ut(i){return{start:de(i),end:ue(i)}}function tn(i,e,t,n){e===O?Je(i,t,n):Qe(i,t,n)}const gs=i=>parseInt(i.getAttribute("data-handle-id")||"-1");function fs(i){return Ft(i).reduce((e,t)=>(e[0]=Math.min(t[0],e[0]),e[1]=Math.min(t[1],e[1]),e[2]=Math.max(t[0],e[2]),e[3]=Math.max(t[1],e[3]),e),[Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY,Number.NEGATIVE_INFINITY,Number.NEGATIVE_INFINITY])}function ps(i){return Array.isArray(i)&&i.length===2&&i.every(isFinite)}function ms(i,e,t,n){for(let s=0;s<i.coordinates.length;s++){const r=i.coordinates[s];if(ps(r))r[0]=t+(r[0]-t)*e,r[1]=n+(r[1]-n)*e;else for(let o=0;o<r.length;o++){const a=r[o];a[0]=t+(a[0]-t)*e,a[1]=n+(a[1]-n)*e}}return i}function Ft(i){let e=[];if(i.type=="Point")i.bbox?e=[[i.bbox[0],i.bbox[1]],[i.bbox[2],i.bbox[3]]]:e=[i.coordinates];else if(i.type=="LineString"||i.type=="MultiPoint")e=i.coordinates;else if(i.type=="Polygon"||i.type=="MultiLineString")e=i.coordinates.reduce(function(t,n){return t.concat(n)},[]);else if(i.type=="MultiPolygon")e=i.coordinates.reduce((t,n)=>t.concat(n.reduce((s,r)=>s.concat(r),[])),[]);else if(i.type=="Feature")if(L(i)||W(i)){const t=st(i);e=[[t[0],t[1]],[t[2],t[3]]]}else e=Ft(i.geometry);else i.type=="GeometryCollection"?e=i.geometries.reduce((t,n)=>t.concat(Ft(n)),[]):i.type=="FeatureCollection"&&(e=i.features.reduce((t,n)=>t.concat(Ft(n)),[]));return e}function ys(i,e,t){const n=Math.atan2(i.y-e.y,i.x-e.x);return{x:e.x+t*Math.cos(n),y:e.y+t*Math.sin(n)}}function Lt(i,e){if(!e)return{x:i.clientX,y:i.clientY};const t=e.getBoundingClientRect();return{x:i.clientX-t.left-e.clientLeft,y:i.clientY-t.top-e.clientTop}}function ws(i,e){return i.startsWith("#")?en(i,e):i.startsWith("rgb")?nn(i,e):i}function xs(i){if(i.startsWith("#")){const e=qt(i),t=parseInt(e.slice(1,3),16),n=parseInt(e.slice(3,5),16),s=parseInt(e.slice(5,7),16);return{r:t,g:n,b:s,a:1}}else{const e=i.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([0-9.]+))?\)/);if(!e)throw new Error(`Invalid RGB color: ${i}`);return{r:parseInt(e[1]),g:parseInt(e[2]),b:parseInt(e[3]),a:e[4]!==void 0?parseFloat(e[4]):1}}}function qt(i){return i.length===4?i.split("").map(e=>e+e).join(""):i}function en(i,e){const[t,n,s]=qt(i).match(/\w\w/g).map(r=>parseInt(r,16));return`rgba(${t}, ${n}, ${s}, ${e})`}function nn(i,e){const[t,n,s]=i.match(/\d+/g).map(r=>parseInt(r,10));return`rgba(${t}, ${n}, ${s}, ${e})`}function ge(){return typeof window<"u"?window:void 0}const fe=(i,e=16,t=!1)=>{let n=!1,s=null;const r=Math.max(1,Math.round(e*60/1e3)),o=a=>{requestAnimationFrame(()=>{if(a>1)o(a-1);else if(n=!1,t&&s!==null){const c=s;s=null,i(...c),n=!0,o(r)}})};return(...a)=>{if(n){t&&(s=a);return}i(...a),n=!0,o(r)}},bs=(i,e)=>{let t;return(...s)=>{clearTimeout(t),t=setTimeout(()=>i(...s),e)}};function vs(i,e){let t;return function(...n){clearTimeout(t),t=setTimeout(()=>{i.apply(this,n)},e)}}function sn(i){if((W(i)||L(i))&&i.geometry.type==="Polygon"){const e=i.geometry.coordinates[0],t=e[0][0],n=e[0][1],s=e[2][0],r=e[2][1],o=s-t,a=r-n,c=t+o/2,h=n+a/2;return{...i,geometry:{type:"Point",coordinates:[c,h],bbox:[t,n,s,r]},properties:{...i.properties,width:o,height:a}}}return i}function pe(i,e){let t,n,s,r=1;const o=i.trim();if(o.startsWith("#")){const d=(o.length===4?qt(o):o).match(/#?(\w{2})(\w{2})(\w{2})/);if(!d)return i;t=parseInt(d[1],16),n=parseInt(d[2],16),s=parseInt(d[3],16)}else{const l=o.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([0-9.]+))?\)/);if(!l)return i;t=parseInt(l[1]),n=parseInt(l[2]),s=parseInt(l[3]),r=l[4]!==void 0?parseFloat(l[4]):1}const a=e*100,h=.299*t+.587*n+.114*s>186?-a/2:a;return t=Math.max(0,Math.min(255,t+Math.round(2.55*h))),n=Math.max(0,Math.min(255,n+Math.round(2.55*h))),s=Math.max(0,Math.min(255,s+Math.round(2.55*h))),`rgba(${t},${n},${s},${r})`}const bt=i=>pe(i,se),Ss=i=>pe(i,-se),X=(i,e)=>({x:i.x-e.x,y:i.y-e.y}),Q=i=>Math.sqrt(i.x*i.x+i.y*i.y),Es=i=>({x:-i.x,y:-i.y}),Wt=i=>{const e=Q(i);return e===0?{x:0,y:0}:{x:i.x/e,y:i.y/e}},pt=(i,e)=>({x:i.x+e.x,y:i.y+e.y}),ct=(i,e)=>({x:i.x*e,y:i.y*e}),rn=(i,e)=>{const t=Math.sin(e),n=Math.cos(e);return{x:i.x*n-i.y*t,y:i.x*t+i.y*n}},vt=(i,e)=>i.x*e.x+i.y*e.y,As=(i,e)=>i.x*e.y-i.y*e.x,tt=i=>i.properties.type==="arrow";function me(i,e,t){const{start:n,end:s}=Ut(i),r=X(e,n),o=i.properties.style.strokeWidth,a=X(s,n),c=Q(a),h=vt(r,Wt(a));return h>0&&h<c&&Math.abs(As(r,Wt(a)))<o/2+t}const mt={strokeType:"plain",strokeColor:"#202020",strokeWidth:1,head:"none",tail:"none"},Cs={id:void 0,type:"Feature",properties:{type:"arrow",style:{...mt}},geometry:{type:"LineString",coordinates:[[0,0],[100,100]]}},_t=(i=0,e=0,t=0,n=0,s={...mt})=>({id:ut(),type:"Feature",properties:{type:"arrow",style:{...mt,...s}},geometry:{type:"LineString",coordinates:[[i,e],[t,n]]}}),W=i=>i.properties.type==="box";function on(i,e,t=0,n=1,s=0){const r=gt(i),{width:o,height:a}=Y(i),c=e.x-r.x,h=e.y-r.y,l=c*n-h*t,d=c*t+h*n;return l>-s&&l<o+s&&d>-s&&d<a+s}const St={background:"#f5f5f5",strokeWidth:0,borderRadius:8,boxShadow:"",padding:16,strokeType:"plain"},Ts={id:void 0,type:"Feature",properties:{type:"box",width:100,height:50,style:{...St}},geometry:{type:"Point",coordinates:[50,25],bbox:[0,0,100,50]}},ye=(i=0,e=0,t=100,n=50,s={...St})=>({id:ut(),type:"Feature",properties:{type:"box",width:t,height:n,style:{...St,...s}},geometry:{type:"Point",coordinates:[i+t/2,e+n/2]}}),L=i=>i.properties.type==="text",V={font:"sans-serif",fontSize:18,color:"#505050",background:"#f5f5f5",strokeWidth:0,borderRadius:8,padding:16,strokeType:"plain",fixedSize:!1},ks={id:ut(),type:"Feature",properties:{type:"text",content:"",width:100,height:50,style:{...V}},geometry:{type:"Point",coordinates:[50,25],bbox:[0,0,100,50]}},we=(i=0,e=0,t=100,n=50,s="",r={...V})=>({id:ut(),type:"Feature",properties:{type:"text",content:s,width:t,height:n,style:{...V,...r}},geometry:{type:"Point",coordinates:[i+t/2,e+n/2],bbox:[i,e,i+t,e+n]}});function xe(i,e,t=0,n=0,s=1,r=1){var m;const[o,a]=i.geometry.coordinates;let{width:c,height:h}=Y(i);(m=i.properties.style)!=null&&m.fixedSize&&(c/=r,h/=r);const l=c/2,d=h/2,u=e.x-o,g=e.y-a,f=u*s-g*n,p=u*n+g*s;return f>-l-t&&f<l+t&&p>-d-t&&p<d+t}const D=i=>i.properties.type==="comment",K={background:"#FFFACD",padding:8,borderRadius:4,strokeColor:"#DDD",strokeWidth:1,strokeType:"plain",iconColor:"#FFFACD",iconSymbol:"💬",iconBorderColor:"#aaa",iconBorderWidth:1,minHeight:60,maxHeight:480,iconSize:32,color:"#333",font:"Arial, sans-serif",fontSize:12,showSendButton:!0,autoGrow:!0,shadow:!0,expandOnSelect:!1,fixedSize:!0},jt={mode:"expanded",width:200,height:60,style:K};function be(i,e,t,n){const s={type:"comment",content:t,mode:(n==null?void 0:n.mode)??jt.mode,width:(n==null?void 0:n.width)??jt.width,height:(n==null?void 0:n.height)??jt.height,author:n==null?void 0:n.author,timestamp:n==null?void 0:n.timestamp,style:{...K,...n==null?void 0:n.style}};return{id:ut(),type:"Feature",properties:s,geometry:{type:"Point",coordinates:[i,e]}}}function Ms(i){return{...i,properties:{...i.properties,mode:i.properties.mode===ot?Mt:ot}}}function an(i,e,t=0,n,s,r=1){if(i.properties.mode!==ot)return xe(i,e,t,n,s,r);const o=i.properties,a={...K,...o.style};let c=a.iconSize,h=a.iconSize;a.fixedSize&&(c/=r,h/=r);const l=c/2,d=h/2,u=e.x-i.geometry.coordinates[0],g=e.y-i.geometry.coordinates[1];return u>=-l-t&&u<=l+t&&g>=-d-t&&g<=d+t}function Ps(i){const[e,t]=i.geometry.coordinates;return{x:e,y:t}}function Ds(i){const e=i.properties,t={...K,...e.style};return e.mode===ot?{width:t.iconSize,height:t.iconSize}:{width:e.width,height:e.height}}function ln(i,e=80){const t=e/i.properties.width;return Math.max(.1,Math.min(1,t))}function Fs(i){const e={...K,...i.properties.style};return e.collapseZoomThreshold!==void 0?e.collapseZoomThreshold:ln(i)}function Ls(i,e,t,n,s="",r){const o=be(t,n,s,r==null?void 0:r.commentStyle),a={...mt,head:"arrow",...r==null?void 0:r.arrowStyle},c=_t(t,n,i,e,a);return c.properties.link={[O]:{id:o.id,side:O,type:"comment",magnet:{x:0,y:0}}},{comment:o,arrow:c}}function _s(i,e,t,n,s,r,o,a,c){const h=i-o,l=e-a,d=o+h*r-l*s,u=a+h*s+l*r,g=o+(h+t)*r-l*s,f=a+(h+t)*s+l*r,p=o+(h+t)*r-(l+n)*s,m=a+(h+t)*s+(l+n)*r,x=o+h*r-(l+n)*s,v=a+h*s+(l+n)*r;return c[0]=Math.min(d,g,p,x),c[1]=Math.min(u,f,m,v),c[2]=Math.max(d,g,p,x),c[3]=Math.max(u,f,m,v),c}function Hs(i,e,t){const n=t.x-e.x,s=t.y-e.y,r=n*n+s*s;if(r===0){const d=i.x-e.x,u=i.y-e.y;return Math.sqrt(d*d+u*u)}let o=((i.x-e.x)*n+(i.y-e.y)*s)/r;o=Math.max(0,Math.min(1,o));const a=e.x+o*n,c=e.y+o*s,h=i.x-a,l=i.y-c;return Math.sqrt(h*h+l*l)}const it=i=>i.properties.type==="polygon";function ve(i,e,t=0){const n=i.geometry.coordinates[0],s=i.geometry.bbox;if(s){const[o,a,c,h]=s;if(e.x<o-t||e.x>c+t||e.y<a-t||e.y>h+t)return!1}let r=!1;for(let o=0,a=n.length-1;o<n.length;a=o++){const c=n[o][0],h=n[o][1],l=n[a][0],d=n[a][1];h>e.y!=d>e.y&&e.x<(l-c)*(e.y-h)/(d-h)+c&&(r=!r)}if(r)return!0;if(t>0)for(let o=0;o<n.length-1;o++){const[a,c]=n[o],[h,l]=n[o+1];if(Hs(e,{x:a,y:c},{x:h,y:l})<=t)return!0}return!1}function cn(i,e){const t=i[0];t.length>0&&(t[0][0]!==t[t.length-1][0]||t[0][1]!==t[t.length-1][1])&&t.push([t[0][0],t[0][1]]);let n=1/0,s=1/0,r=-1/0,o=-1/0;for(const[d,u]of t)d<n&&(n=d),d>r&&(r=d),u<s&&(s=u),u>o&&(o=u);const a=[n,s,r,o],{id:c,style:h,...l}=e||{};return{type:"Feature",id:c||ut(),geometry:{type:"Polygon",coordinates:i,bbox:a},properties:{type:"polygon",style:{...ht,...h},...l}}}const ht={...St,strokeColor:"#000000",strokeWidth:2,background:"transparent"},Is={type:"polygon",style:{...ht}},Se=i=>i.type==="FeatureCollection";function Ee(i){return/^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/.test(i.trim())}function Ae(i){return/^rgb\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*\)$/.test(i.trim())}function Ce(i){return/^rgba\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*,\s*[0-9.]+\s*\)$/.test(i.trim())}function hn(i){return Ee(i)||Ae(i)||Ce(i)}function Bs(i){if(hn(i))return i;throw new Error(`Invalid color format: ${i}`)}function zs(i){if(Ee(i))return i;throw new Error(`Invalid hex color format: ${i}`)}function Rs(i){if(Ae(i))return i;throw new Error(`Invalid RGB color format: ${i}`)}function $s(i){if(Ce(i))return i;throw new Error(`Invalid RGBA color format: ${i}`)}class Ns{constructor(e){y(this,"previousZoom",-1);this.store=e}toggleComment(e){const t=this.store.getState().getFeature(e);if(!t||!D(t))return;const n=t;this.store.getState().applyLiveUpdate(e,{properties:{...n.properties,mode:n.properties.mode==="collapsed"?"expanded":"collapsed"}})}updateCommentModesForZoom(e){if(Math.abs(this.previousZoom-e)<5e-4)return;this.previousZoom=e;const t=this.store.getState(),n=t.features,s={};Object.values(n).forEach(r=>{if(D(r)){const o=r,a=this.getCommentZoomThreshold(o),c=e<a?ot:Mt;if(o.properties.mode===c)return;s[o.id]={properties:{...o.properties,mode:c}}}}),t.applyLiveUpdates(s)}getCommentZoomThreshold(e){const t={...e.properties.style};if(t.collapseZoomThreshold!==void 0)return t.collapseZoomThreshold;const s=80/e.properties.width;return Math.max(.1,Math.min(1,s))}}class Gt extends EventTarget{constructor(t,n){super();y(this,"annotation",null);y(this,"ogma");y(this,"dragging",!1);y(this,"dragStartPoint");y(this,"hoveredHandle");y(this,"ogmaPanningOption",!1);y(this,"store");y(this,"draggingWasEnabled",!0);y(this,"handleMouseMove",t=>{if(t.target instanceof HTMLTextAreaElement||!this.isActive())return;const n=this.store.getState();if(!this.dragging&&n.mousePressed&&n.mousePressPoint&&(this.detectHandle(t,this.ogma.view.getZoom()),this.hoveredHandle)){this.dragStartPoint=n.mousePressPoint,this.onDragStart(t),this.dispatchEvent(new CustomEvent(xt,{detail:{id:this.annotation,position:{x:t.clientX,y:t.clientY}}})),this.disablePanning();return}this.dragging?this.dragStartPoint&&this.onDrag(t):this.detectHandle(t,this.ogma.view.getZoom())});y(this,"handleMouseDown",t=>{t.target instanceof HTMLTextAreaElement||!this.isActive()||this.dragging||this.hoveredHandle||this.detectHandle(t,this.ogma.view.getZoom())});y(this,"disablePanning",()=>{var t,n;this.ogmaPanningOption=!!((n=(t=this.ogma.getOptions().interactions)==null?void 0:t.pan)!=null&&n.enabled),this.ogma.setOptions({interactions:{pan:{enabled:!1},drag:{enabled:!1}}})});y(this,"restorePanning",()=>{this.ogma.setOptions({interactions:{pan:{enabled:!0},drag:{enabled:!0}}})});y(this,"handleMouseUp",t=>{this.isActive()&&this.dragging&&(this.restorePanning(),this.onDragEnd(t),this.dispatchEvent(new CustomEvent(kt,{detail:{id:this.annotation,position:{x:t.clientX,y:t.clientY}}})))});y(this,"onClick",t=>{});this.store=n,this.ogma=t,this.store.subscribe(s=>s.features,s=>{this.isActive()&&!s[this.annotation]&&this.stopEditing()})}cancelEdit(){!this.isActive()||this.annotation===null||this.clearDragState()}clearDragState(){this.dragging=!1,this.dragStartPoint=void 0,this.hoveredHandle=void 0,this.restorePanning(),this.setCursor(H.default)}commitChange(){this.getAnnotation()&&this.store.getState().commitLiveUpdates()}onDrag(t){this.dispatchEvent(new Event(Tt))}onDragStart(t){return this.isActive()?(this.dragging=!0,this.dragStartPoint=this.clientToCanvas(t),this.disablePanning(),!0):!1}onDragEnd(t){return this.isActive()?(this.restorePanning(),this.dragging=!1,!0):!1}clientToCanvas(t){const n=this.ogma,s=Lt(t,n.getContainer());return n.view.screenToGraphCoordinates(s)}setAnnotation(t){if(this.annotation=t?t.id:null,this.annotation!==null){const n=this.ogma.getContainer();if(n){const s=ge()||n;s.addEventListener("mousemove",this.handleMouseMove),s.addEventListener("mouseup",this.handleMouseUp,!1),n.addEventListener("mousedown",this.handleMouseDown,!0),s.addEventListener("click",this.onClick,!0)}}else{const n=this.ogma.getContainer();if(n){const s=ge()||n;s.removeEventListener("mousemove",this.handleMouseMove),s.removeEventListener("mouseup",this.handleMouseUp),n.removeEventListener("mousedown",this.handleMouseDown),s.removeEventListener("click",this.onClick)}this.clearDragState(),this.setCursor(H.default)}}getAnnotation(t){const n=this.store.getState(),s=n.getFeature(this.annotation);if(!t)return s;const r=n.liveUpdates[this.annotation];return s&&r?{...s,...r}:s}setCursor(t){var s;const n=(s=this.ogma.getContainer())==null?void 0:s.firstChild;n&&(n.style.cursor=t)}stopEditing(){this.isActive()&&this.setAnnotation(null)}cancelDrawing(){if(!this.isActive())return;const t=this.store.getState();t.drawingFeature===this.annotation&&t.removeFeature(this.annotation),this.stopEditing()}isActive(){return this.annotation!==null}}class Os extends Gt{constructor(t,n,s,r,o,a,c){super(t,n);y(this,"links");y(this,"snapping");y(this,"arrowHandler");y(this,"arrowStyle");y(this,"offsetX");y(this,"offsetY");y(this,"comment");y(this,"startX",0);y(this,"startY",0);y(this,"onArrowComplete",()=>{var x;this.arrowHandler.removeEventListener("dragend",this.onArrowComplete);const t=this.store.getState(),n=Array.from(t.selectedFeatures);if(n.length===0)return;const s=t.getFeature(n[0]);if(!s||!tt(s))return;const r=s.geometry.coordinates[0],o=s.geometry.coordinates[1],a=o[0]-this.startX,c=o[1]-this.startY,h=Math.sqrt(a*a+c*c);let l,d;h<5?(l=this.startX+this.offsetX,d=this.startY+this.offsetY):(l=o[0],d=o[1]);const u=this.comment;u.geometry.coordinates=[l,d],t.addFeature(u),this.store.setState({drawingFeature:u.id});const g=u.properties.height,f=l,p=d+g*.5;this.snapArrowStart(s,r[0],r[1]);const m=(x=s.properties.link)==null?void 0:x.start;t.updateFeature(s.id,{...s,geometry:{...s.geometry,coordinates:[[f,p],r]},properties:{...s.properties,link:{start:{side:O,id:u.id,type:P.COMMENT,magnet:{x:0,y:.5}},end:m?{side:rt,id:m.id,type:m.type,magnet:m.magnet}:void 0}}}),this.links.add(s,O,u.id,P.COMMENT,{x:0,y:.5}),m&&m.magnet&&this.links.add(s,rt,m.id,m.type,m.magnet),t.setSelectedFeatures([u.id]),this.store.setState({drawingFeature:null})});this.links=s,this.arrowHandler=o,this.snapping=r,this.offsetX=(c==null?void 0:c.offsetX)??100,this.offsetY=(c==null?void 0:c.offsetY)??-50,this.arrowStyle=c==null?void 0:c.arrowStyle,this.comment=a}detectHandle(t,n){}startDrawing(t,n,s){var o;this.startX=n,this.startY=s;const r=_t(n,s,n,s,{...mt,head:"arrow",...(o=this.arrowStyle)==null?void 0:o.style});this.store.getState().addFeature(r),this.store.setState({drawingFeature:r.id}),this.arrowHandler.addEventListener("dragend",this.onArrowComplete),this.store.getState().setSelectedFeatures([r.id]),this.arrowHandler.startDrawing(r.id,n,s)}snapArrowStart(t,n,s){const r=this.snapping.snap({x:n,y:s});r&&(t.geometry.coordinates[0]=[r.point.x,r.point.y],t.properties.link={start:{side:O,id:r.id,type:r.type,magnet:r.magnet}},this.links.add(t,O,r.id,r.type,r.magnet))}}function Ys(i,e,t,n,s){const{width:r,height:o}=n.view.getSize(),a=8,c=16,h=a*c,l={width:150,height:50},d=l.width,u=l.height,g=Math.max(r,o),f=200,p={minX:0,minY:0,maxX:0,maxY:0},m=n.view.graphToScreenCoordinates({x:i,y:e});for(let x=0;x<h;x++){const v=x%a,S=Math.floor(x/a),E=2*Math.PI*(v/a),b=f+S/c*(g-f),A=Math.cos(E)*b,T=Math.sin(E)*b,C=m.x+A,M=m.y+T;if(p.minX=C,p.minY=M-u/2,p.maxX=C+d,p.maxY=M+u/2,A<0&&(p.minX-=d,p.maxX-=d),p.minX<0||p.maxX>r||p.minY<0||p.maxY>o)continue;const F=n.view.screenToGraphCoordinates({x:p.minX,y:p.minY}),j=n.view.screenToGraphCoordinates({x:p.maxX,y:p.maxY});if(p.minX=F.x,p.minY=F.y,p.maxX=j.x,p.maxY=j.y,!t.query(p).filter(R=>R.properties.type!=="arrow").length)return n.view.screenToGraphCoordinates({x:C,y:M})}return n.view.screenToGraphCoordinates({x:r/2,y:o/2})}class Xs{constructor(e,t,n,s,r,o,a){y(this,"ogma");y(this,"store");y(this,"editor");y(this,"interactions");y(this,"links");y(this,"control");y(this,"index");y(this,"pendingDrawingListener",null);y(this,"placementCleanup",null);this.ogma=e,this.store=t,this.editor=n,this.interactions=s,this.links=r,this.control=o,this.index=a}cancelPendingDrawing(){this.pendingDrawingListener&&(this.ogma.events.off(this.pendingDrawingListener),this.pendingDrawingListener=null),this.placementCleanup&&(this.placementCleanup(),this.placementCleanup=null)}isDrawing(){return this.store.getState().drawingFeature!==null}enableDrawingMode(e){this.control.unselect().cancelDrawing();const t=n=>{this.ogma.events.off(t),this.pendingDrawingListener=null;const{x:s,y:r}=this.ogma.view.screenToGraphCoordinates(n);e(s,r)};return this.pendingDrawingListener=t,this.ogma.events.once("mousedown",t),this.control}enableArrowDrawing(e){return this.enableDrawingMode((t,n)=>{const s=_t(t,n,t,n,e);this.startArrow(t,n,s)})}enableTextDrawing(e){return this.enableDrawingMode((t,n)=>{const s=we(t,n,0,0,void 0,e);this.startText(t,n,s)})}enableBoxDrawing(e){return this.enableDrawingMode((t,n)=>{const s=ye(t,n,0,0,e);this.startBox(t,n,s)})}enablePolygonDrawing(e){return this.enableDrawingMode((t,n)=>{const s=cn([[[t,n]]],{style:e});this.startPolygon(t,n,s)})}enableCommentDrawing(e={}){return this.enableDrawingMode((t,n)=>{let s=e.offsetX,r=e.offsetY;if(s===void 0&&r===void 0){const a=Ys(t,n,this.index,this.ogma);s=a.x,r=a.y}const o=be(t,n,"",e==null?void 0:e.commentStyle);this.startComment(t,n,o,{...e,offsetX:s,offsetY:r})})}enablePlacement(e){this.control.unselect().cancelDrawing(),this.store.setState({drawingFeature:e.id}),this.control.add(e);const t=this.ogma.getContainer();if(!t)return this.control;const n=a=>{const{x:c,y:h}=this.ogma.view.screenToGraphCoordinates(a),l=e.properties.width,d=e.properties.height;this.store.getState().applyLiveUpdate(e.id,{geometry:{...e.geometry,coordinates:[c,h],bbox:[c-l/2,h-d/2,c+l/2,h+d/2]}})},s=a=>{o();const{x:c,y:h}=this.ogma.view.screenToGraphCoordinates(a),l=e.properties.width,d=e.properties.height;this.store.getState().updateFeature(e.id,{geometry:{...e.geometry,coordinates:[c,h],bbox:[c-l/2,h-d/2,c+l/2,h+d/2]}}),this.store.setState({drawingFeature:null}),this.interactions.suppressClicksTemporarily(200),this.control.select(e.id)},r=a=>{a.key==="Escape"&&(o(),this.control.remove(e),this.store.setState({drawingFeature:null}))},o=()=>{t.removeEventListener("mousemove",n),this.ogma.events.off(s),document.removeEventListener("keydown",r),this.placementCleanup=null};return t.addEventListener("mousemove",n,{passive:!0}),this.ogma.events.once("mousedown",s),document.addEventListener("keydown",r),this.placementCleanup=o,this.control}startComment(e,t,n,s){this.editor.getActiveHandler()&&this.editor.getActiveHandler().stopEditing(),this.control.cancelDrawing(),this.store.setState({drawingFeature:n.id}),this.interactions.suppressClicksTemporarily(200);const r=new Os(this.ogma,this.store,this.links,this.editor.getSnapping(),this.editor.getArrowHandler(),n,s),o=a=>{var c;a.id===n.id&&(this.control.select(a.id),this.control.off(Yt,o),(c=this.editor.getActiveHandler())==null||c.startEditingText())};return this.control.on(Yt,o),r.startDrawing(n.id,e,t),this.control}startBox(e,t,n){return n||(n=ye(e,t)),this.store.setState({drawingFeature:n.id}),this.control.add(n),this.interactions.suppressClicksTemporarily(200),this.control.select(n.id),this.editor.getActiveHandler().startDrawing(n.id,e,t),this.control}startArrow(e,t,n){return n||(n=_t(e,t)),this.editor.getActiveHandler()&&this.editor.getActiveHandler().stopEditing(),this.control.cancelDrawing(),this.store.setState({drawingFeature:n.id}),this.control.add(n),this.interactions.suppressClicksTemporarily(200),this.control.select(n.id),this.editor.getActiveHandler().startDrawing(n.id,e,t),this.control}startText(e,t,n){return n||(n=we(e,t)),this.store.setState({drawingFeature:n.id}),this.control.add(n),this.interactions.suppressClicksTemporarily(200),this.control.select(n.id),this.editor.getActiveHandler().startDrawing(n.id,e,t),this.control}startPolygon(e,t,n){return this.store.setState({drawingFeature:n.id}),this.control.add(n),this.interactions.suppressClicksTemporarily(200),this.control.select(n.id),this.editor.getActiveHandler().startDrawing(n.id,e,t),this.control}}class Us{constructor(e,t){this.store=e,this.links=t}undo(){return this.canUndo()?(this.store.temporal.getState().undo(),this.links.refresh(),!0):!1}redo(){return this.canRedo()?(this.store.temporal.getState().redo(),this.links.refresh(),!0):!1}canUndo(){return this.store.temporal.getState().pastStates.length>0}canRedo(){return this.store.temporal.getState().futureStates.length>0}clearHistory(){this.store.temporal.getState().clear()}}class qs{constructor(e){this.store=e}select(e){const t=Array.isArray(e)?e:[e];this.store.getState().setSelectedFeatures(t)}unselect(e){const t=Array.isArray(e)?e:[e];if(e===void 0)this.store.getState().setSelectedFeatures([]);else{const n=new Set(t),s=Array.from(this.store.getState().selectedFeatures).filter(r=>!n.has(r));this.store.getState().setSelectedFeatures(s)}}getSelectedAnnotations(){const e=this.store.getState();return{type:"FeatureCollection",features:Array.from(e.selectedFeatures).map(t=>e.features[t])}}getSelected(){const e=this.store.getState(),t=Array.from(e.selectedFeatures)[0];return t?e.features[t]:null}}class Ws{constructor(e){this.store=e}add(e){if(Se(e))for(const t of e.features)this.add(t);else{const t=sn(e);this.store.getState().addFeature(t)}}remove(e){if(Se(e))for(const t of e.features)this.remove(t);else this.store.getState().removeFeature(e.id)}getAnnotations(){const e=this.store.getState().features;return{type:"FeatureCollection",features:Object.values(e)}}getAnnotation(e){return this.store.getState().getFeature(e)}updateStyle(e,t){const n=this.store.getState().getFeature(e);n&&this.store.getState().updateFeature(e,{id:e,properties:{...n.properties,style:{...n.properties.style,...t}}})}update(e){var s;const t=this.store.getState(),n=t.getFeature(e.id);n&&t.updateFeature(e.id,{...n,...e,properties:{...n.properties,...e.properties,style:{...n.properties.style,...(s=e.properties)==null?void 0:s.style}},geometry:{...n.geometry,...e.geometry}})}setScale(e,t,n,s){const r=this.store.getState().getFeature(e);if(!r)return;const o=this.store.getState();if(tt(r)){const a=r.geometry.coordinates.map(([c,h])=>{const l=c-n,d=h-s;return[n+l*t,s+d*t]});o.updateFeature(e,{geometry:{...r.geometry,coordinates:a}})}else if(L(r)||W(r)){const[a,c]=r.geometry.coordinates,h=a-n,l=c-s,d=n+h*t,u=s+l*t,g=r.properties.width*t,f=r.properties.height*t;o.updateFeature(e,{properties:{...r.properties,width:g,height:f},geometry:{...r.geometry,coordinates:[d,u]}})}}}function dn(i){var t,n;const e=i.properties.link;return e?((t=e.start)==null?void 0:t.type)===P.COMMENT||((n=e.end)==null?void 0:n.type)===P.COMMENT:!1}function un(i){var e,t;return((t=(e=i.properties.link)==null?void 0:e.start)==null?void 0:t.type)!==P.COMMENT}function js(i){return!0}function Te(i,e,t,n,s=!1){const r=i.getState(),o=r.getFeature(t);if(!o)return;const a=new Set,c=new Set;let h="";if(tt(o)){const d=o;if(s){const u=d.properties.link||{};u.start&&Vt(u.start.type)&&a.add(u.start.id),u.end&&Vt(u.end.type)&&a.add(u.end.id)}h=d.id,c.add(d.id)}else a.add(o.id);const l={};for(const d of a){const u=r.getFeature(d);if(!u)continue;const g=Gs(u,n);g&&(l[d]=g,e.updateLinkedArrowsDuringDrag(d,n,l))}for(const d of c){const u=r.getFeature(d);if(!u)continue;const g=u.properties.link||{},f=u.geometry.coordinates,p=[...f];(g.start&&Vt(g.start.type)||h===d)&&(p[0]=[f[0][0]+n.x,f[0][1]+n.y]),(g.end&&Vt(g.end.type)||h===d)&&(p[1]=[f[1][0]+n.x,f[1][1]+n.y]),l[d]={geometry:{...u.geometry,coordinates:p}}}r.applyLiveUpdates(l)}function Vt(i){return i!==P.NODE&&i!==P.EDGE}function Gs(i,e){if(L(i)||W(i)||D(i)){const t=J(i);return{geometry:{type:i.geometry.type,coordinates:[t.x+e.x,t.y+e.y]}}}if(it(i)){const t=i,n=t.geometry.coordinates.map(s=>s.map(r=>[r[0]+e.x,r[1]+e.y]));return{geometry:{type:t.geometry.type,coordinates:n}}}return null}class Vs extends Gt{constructor(t,n,s,r){super(t,n);y(this,"snapping");y(this,"links");y(this,"snap",null);this.snapping=s,this.links=r}detectHandle(t){const n=this.getAnnotation(),s=this.clientToCanvas(t),r=ne,o=n.geometry.coordinates[0],a=n.geometry.coordinates[1],c=Math.sqrt(Math.pow(s.x-o[0],2)+Math.pow(s.y-o[1],2)),h=Math.sqrt(Math.pow(s.x-a[0],2)+Math.pow(s.y-a[1],2));c<r?this.grabHandle("start",o[0],o[1]):h<r?this.grabHandle("end",a[0],a[1]):me(n,s,r)?(this.setCursor(H.grab),this.hoveredHandle={type:"body",point:s},this.store.setState({hoveredHandle:2,hoveredFeature:this.annotation})):(this.store.setState({hoveredHandle:-1,hoveredFeature:null}),this.hoveredHandle=void 0)}grabHandle(t,n,s){this.hoveredHandle={type:t,point:{x:n,y:s}},this.store.setState({hoveredHandle:t==="start"?0:1,hoveredFeature:this.annotation}),this.setCursor(H.move)}onDrag(t){var l;if(!(this.dragStartPoint||!this.hoveredHandle)||!this.isActive())return;t.stopPropagation(),t.stopImmediatePropagation();const n=this.hoveredHandle,s=this.getAnnotation(),r=s.properties.link||{},o=this.clientToCanvas(t);this.snap=this.snapping.snap(o);const a=((l=this.snap)==null?void 0:l.point)||o,c=new CustomEvent(Tt,{detail:{point:a,annotation:this.annotation,handle:n}}),h=[...s.geometry.coordinates];if(n.type==="body"){const d=a.x-n.point.x,u=a.y-n.point.y;Te(this.store,this.links,s.id,{x:d,y:u},!0),this.dispatchEvent(c);return}else n.type==="start"?(h[0]=[a.x,a.y],this.snap&&(r.start={side:n.type,id:this.snap.id,type:this.snap.type,magnet:this.snap.magnet})):n.type==="end"&&(h[1]=[a.x,a.y],this.snap&&(r.end={side:n.type,id:this.snap.id,type:this.snap.type,magnet:this.snap.magnet}));this.store.getState().applyLiveUpdate(s.id,{id:s.id,properties:{...s.properties,link:r},geometry:{type:s.geometry.type,coordinates:h}}),this.dispatchEvent(c)}onDragStart(t){if(!super.onDragStart(t))return!1;const n=this.getAnnotation(),s=this.hoveredHandle;return s.type==="end"&&!1||s.type==="start"&&!un(n)?(this.clearDragState(),!1):(this.store.getState().startLiveUpdate([this.annotation]),!0)}onDragEnd(t){var r,o,a,c;if(!super.onDragEnd(t))return!1;const n=this.getAnnotation(!0);if(this.hoveredHandle){const h=n.geometry.coordinates;if(this.hoveredHandle.type==="body"){const l=this.snapping.snap({x:h[0][0],y:h[0][1]}),d=this.snapping.snap({x:h[1][0],y:h[1][1]});l?this.links.add(n,"start",l.id,l.type,l.magnet):((o=(r=n.properties.link)==null?void 0:r.start)==null?void 0:o.type)!=="comment"&&this.links.remove(n,O),d?this.links.add(n,"end",d.id,d.type,d.magnet):((c=(a=n.properties.link)==null?void 0:a.end)==null?void 0:c.type)!=="comment"&&this.links.remove(n,rt)}else if(this.snap){const l=this.hoveredHandle;l.type!=="body"&&this.links.add(n,l.type,this.snap.id,this.snap.type,this.snap.magnet)}else if(this.snap===null){const l=this.hoveredHandle.type;if(n.properties.link&&n.properties.link[l]){this.links.remove(n,l);const d={...n.properties.link};delete d[l],this.store.getState().applyLiveUpdate(n.id,{properties:{...n.properties,link:d}})}}}return this.store.getState().drawingFeature===this.annotation&&this.store.setState({drawingFeature:null}),this.commitChange(),this.clearDragState(),this.snap=null,!0}snapDrawingStart(t,n,s){const r=this.snapping.snap({x:n,y:s});if(r){const o={geometry:{...t.geometry,coordinates:[[r.point.x,r.point.y],t.geometry.coordinates[1]]},properties:{...t.properties,link:{start:{side:O,id:r.id,type:r.type,magnet:r.magnet}}}},a=this.store.getState();a.applyLiveUpdate(t.id,o),a.updateFeature(t.id,o),this.links.add(t,"start",r.id,r.type,r.magnet)}}startDrawing(t,n,s){this.annotation=t;const r=this.getAnnotation();this.snapDrawingStart(r,n,s),this.grabHandle("end",n,s),this.dragging=!0,this.dragStartPoint={x:n,y:s};const o=this.ogma.view.graphToScreenCoordinates({x:n,y:s});this.onDragStart({clientX:o.x,clientY:o.y})}link(t,n,s){let r=Dt(t,s);const o=t.properties.link||{};let a=null;if(n instanceof q.Node)r=n.getPosition(),a=this.snapping.snapToNodes(r,n.toList());else{const c=this.store.getState().getFeature(n);if(!c)throw new Error(`Annotation with id ${n} not found`);if(L(c)||W(c))r=J(c),a=this.snapping.snapToText(r,[c]);else if(it(c))r=ce(c),a=this.snapping.snapToPolygon(r,[c]);else throw new Error(`Cannot link arrow to annotation of type ${c.properties.type}`)}a&&(o[s]={side:s,id:a.id,type:a.type,magnet:a.magnet},this.links.add(t,s,a.id,a.type,a.magnet),tn(t,s,r.x,r.y),this.store.getState().updateFeature(t.id,{properties:{...t.properties,link:o},geometry:{type:t.geometry.type,coordinates:t.geometry.coordinates}}))}}class Zs extends Gt{constructor(t,n,s){super(t,n);y(this,"links");y(this,"isDrawingMode",!1);y(this,"simplificationTolerance",5);y(this,"handleMouseDown",t=>{!this.isActive()||this.dragging||!this.hoveredHandle||(t.preventDefault(),t.stopPropagation(),this.dragStartPoint=this.clientToCanvas(t),this.onDragStart(t),this.dispatchEvent(new CustomEvent(xt,{detail:{id:this.annotation,position:{x:t.clientX,y:t.clientY}}})),this.disablePanning())});this.links=s}startDrawing(t,n,s){this.annotation=t,this.isDrawingMode=!0;const r=this.store.getState();r.startLiveUpdate([t]),r.setDrawingPoints([]);const o=this.ogma.view.graphToScreenCoordinates({x:n,y:s});return this.dragStartPoint=o,this.disablePanning(),this.onDragStart({clientX:o.x,clientY:o.y}),this}cancelDrawing(){this.isDrawingMode&&(this.isDrawingMode=!1,this.annotation&&(this.store.getState().removeFeature(this.annotation),this.annotation=null)),this.restorePanning()}detectHandle(t,n=1){const s=this.getAnnotation();if(!s)return;const r=this.clientToCanvas(t),o=ne/n,a=s.geometry.coordinates[0];for(let c=0;c<a.length-1;c++){const[h,l]=a[c];if(Math.sqrt(Math.pow(r.x-h,2)+Math.pow(r.y-l,2))<o*2){this.grabHandle("vertex",h,l,c);return}}ve(s,r,o)?(this.setCursor(H.grab),this.hoveredHandle={type:"body",point:r},this.store.setState({hoveredHandle:-1,hoveredFeature:this.annotation})):(this.store.setState({hoveredHandle:-1,hoveredFeature:null}),this.hoveredHandle=void 0)}grabHandle(t,n,s,r){this.setCursor(t==="vertex"?H.pointer:H.grab),this.hoveredHandle={type:t,point:{x:n,y:s},vertexIndex:r},this.store.setState({hoveredHandle:r??0,hoveredFeature:this.annotation})}onDragStart(t){if(this.isDrawingMode){const n=this.clientToCanvas(t);this.dragging=!0;const s=this.getAnnotation();if(s){const r=[[n.x,n.y],[n.x,n.y]],o=this.store.getState();o.setDrawingPoints(r),o.applyLiveUpdate(s.id,{...s,geometry:{type:"Polygon",coordinates:[r],bbox:[n.x,n.y,n.x,n.y]}})}return this.dragStartPoint=n,!0}return this.hoveredHandle?(this.dragging=!0,this.setCursor(H.grabbing),this.store.getState().startLiveUpdate([this.annotation]),!0):!1}onDrag(t){if(this.isDrawingMode&&this.dragging){const a=this.clientToCanvas(t),c=this.store.getState(),h=this.getAnnotation();if(!h)return;const l=c.liveUpdates[this.annotation],d=l?{...h,...l}:h,g=c.drawingPoints.slice(0,-1),f=[...g,[a.x,a.y],g[0]];c.setDrawingPoints(f);const p=ae(f,this.simplificationTolerance,!1),m=[...p,p[0]];c.applyLiveUpdate(this.annotation,{...d,geometry:{type:"Polygon",coordinates:[m]}});return}if(!this.dragging||!this.dragStartPoint||!this.hoveredHandle)return;const n=this.clientToCanvas(t),s=n.x-this.dragStartPoint.x,r=n.y-this.dragStartPoint.y,o=this.getAnnotation();if(o){if(this.hoveredHandle.type==="body")Te(this.store,this.links,o.id,{x:s,y:r});else if(this.hoveredHandle.type==="vertex"&&this.hoveredHandle.vertexIndex!==void 0){const a=o.geometry.coordinates[0].map((h,l)=>l===this.hoveredHandle.vertexIndex?[this.hoveredHandle.point.x+s,this.hoveredHandle.point.y+r]:l===o.geometry.coordinates[0].length-1&&this.hoveredHandle.vertexIndex===0?[this.hoveredHandle.point.x+s,this.hoveredHandle.point.y+r]:h),c={...o,geometry:{...o.geometry,coordinates:[a]}};he(c),this.store.getState().applyLiveUpdate(o.id,c),this.links.snapLinkedArrowsDuringDrag(o.id)}this.dispatchEvent(new Event(Tt))}}onDragEnd(){if(this.isDrawingMode&&this.dragging){const n=this.store.getState(),s=this.getAnnotation();if(s){const r=n.drawingPoints;if(r.length<3)this.cancelDrawing();else{const o=ae(r.slice(1,-1),this.simplificationTolerance,!0),a={...s,geometry:{type:"Polygon",coordinates:[o]}};ft(a),this.store.getState().applyLiveUpdate(s.id,a),this.store.setState({drawingFeature:null}),this.commitChange(),this.isDrawingMode=!1,this.dragging=!1,this.restorePanning(),this.dispatchEvent(new CustomEvent(ee,{detail:a}))}}return!0}return!this.dragging||(this.dragging=!1,this.dragStartPoint=void 0,!this.getAnnotation())?!1:(this.commitChange(),this.setCursor(H.grab),!0)}getAnnotation(){if(this.annotation)return this.store.getState().getFeature(this.annotation)}}class Ks{constructor(e,t,n,s=()=>{}){y(this,"layer");y(this,"textarea");y(this,"sendButton",null);y(this,"isFocused");y(this,"unsubscribe");y(this,"onFocus",()=>{this.isFocused=!0});y(this,"onBlur",()=>{this.getAnnotation()&&this.updateContent(),this.isFocused=!1});y(this,"onKeyup",e=>{e.key==="Escape"&&this.textarea.blur(),e.stopPropagation()});y(this,"onKeydown",e=>{e.stopPropagation()});y(this,"onMouseEvent",e=>{e.stopPropagation()});y(this,"onInput",()=>{this.updateContent(),this.updateStyle(),this.updatePosition(),this.layer.setSize(this.getSize()),this.updateSendButtonState()});y(this,"onSendClick",e=>{e.preventDefault(),e.stopPropagation(),!(!this.sendButton||this.sendButton.disabled)&&(this.updateContent(),this.textarea.blur(),this.onSendHandler())});y(this,"update",()=>{this.getAnnotation()&&(this.updateStyle(),this.updatePosition(),this.layer.setSize(this.getSize()))});var u,g,f;this.ogma=e,this.store=t,this.annotation=n,this.onSendHandler=s;const r=this.getPosition(),o=this.getSize(),a=this.getAnnotation(),c=this.store.getState(),h=D(a)&&(((u=c.options)==null?void 0:u.showSendButton)??!0),l=((g=c.options)==null?void 0:g.sendButtonIcon)||"",d=((f=c.options)==null?void 0:f.textPlaceholder)||"Enter text";this.layer=this.ogma.layers.addOverlay({element:`<div class="ogma-annotation-text-editor">
6
+ `;let ut=(i=21)=>crypto.getRandomValues(new Uint8Array(i)).reduce((e,t)=>(t&=63,t<36?e+=t.toString(36):t<62?e+=(t-26).toString(36).toUpperCase():t>62?e+="-":e+="_",e),"");function os(i,e){const t=i[0]-e[0],n=i[1]-e[1];return t*t+n*n}function as(i,e,t){let n=e[0],s=e[1],r=t[0]-n,o=t[1]-s;if(r!==0||o!==0){const a=((i[0]-n)*r+(i[1]-s)*o)/(r*r+o*o);a>1?(n=t[0],s=t[1]):a>0&&(n+=r*a,s+=o*a)}return r=i[0]-n,o=i[1]-s,r*r+o*o}function ls(i,e){let t=i[0];const n=[t];let s;for(let r=1,o=i.length;r<o;r++)s=i[r],os(s,t)>e&&(n.push(s),t=s);return t!==s&&n.push(s),n}function oe(i,e,t,n,s){let r=n,o=0;for(let a=e+1;a<t;a++){const c=as(i[a],i[e],i[t]);c>r&&(o=a,r=c)}r>n&&(o-e>1&&oe(i,e,o,n,s),s.push(i[o]),t-o>1&&oe(i,o,t,n,s))}function cs(i,e){const t=i.length-1,n=[i[0]];return oe(i,0,t,e,n),n.push(i[t]),n}function ae(i,e,t){if(i.length<=2)return i;const n=e!==void 0?e*e:1;return i=t?i:ls(i,n),i=cs(i,n),i}function le(i){if(i.geometry.bbox)return i.geometry.bbox;const e=i.geometry.coordinates[0];let t=1/0,n=1/0,s=-1/0,r=-1/0;for(const[o,a]of e)o<t&&(t=o),o>s&&(s=o),a<n&&(n=a),a>r&&(r=a);return[t,n,s,r]}function ce(i){const e=i.geometry.coordinates[0];let t=0,n=0;const s=e.length-1;for(let r=0;r<s;r++)t+=e[r][0],n+=e[r][1];return{x:t/s,y:n/s}}function hs(i,e,t){const n=i.geometry.coordinates.map(o=>o.map(([a,c])=>[a+e,c+t])),s=i.geometry.bbox,r=s?[s[0]+e,s[1]+t,s[2]+e,s[3]+t]:void 0;return{...i,geometry:{...i.geometry,coordinates:n,bbox:r}}}function he(i){const e=i.geometry.coordinates[0];let t=1/0,n=1/0,s=-1/0,r=-1/0;for(const[o,a]of e)o<t&&(t=o),o>s&&(s=o),a<n&&(n=a),a>r&&(r=a);i.geometry.bbox=[t,n,s,r]}function ds(i,e,t,n){const s=i.geometry.coordinates.map(o=>o.map(([a,c])=>{const h=a-t,l=c-n;return[t+h*e,n+l*e]})),r={...i,geometry:{...i.geometry,coordinates:s}};return he(r),r}const us="http://www.w3.org/2000/svg";function k(i){return document.createElementNS(us,i)}function st(i){return i.geometry.bbox||ft(i),i.geometry.bbox}function Y(i){if("width"in i.properties&&"height"in i.properties)return{width:i.properties.width,height:i.properties.height};const e=st(i);return{width:e[2]-e[0],height:e[3]-e[1]}}function gt(i,e=!1,t=1){if(i.geometry.type==="Point"&&"width"in i.properties&&"height"in i.properties){const[s,r]=i.geometry.coordinates;let o=i.properties.width,a=i.properties.height;return e&&(o/=t,a/=t),{x:s-o/2,y:r-a/2}}const n=st(i);return{x:n[0],y:n[1]}}function J(i){if(i.geometry.type==="Point")return{x:i.geometry.coordinates[0],y:i.geometry.coordinates[1]};const e=st(i);return{x:(e[0]+e[2])/2,y:(e[1]+e[3])/2}}function ft(i){if(tt(i)){const[e,t]=i.geometry.coordinates[0],[n,s]=i.geometry.coordinates[1];i.geometry.bbox=[Math.min(e,n),Math.min(t,s),Math.max(e,n),Math.max(t,s)]}else if(i.geometry.type==="Point"&&"width"in i.properties&&"height"in i.properties){const[e,t]=i.geometry.coordinates,n=i.properties.width/2,s=i.properties.height/2;i.geometry.bbox=[e-n,t-s,e+n,t+s]}else if(i.geometry.type==="Polygon"){const e=i.geometry.coordinates[0];let t=Number.POSITIVE_INFINITY,n=Number.POSITIVE_INFINITY,s=Number.NEGATIVE_INFINITY,r=Number.NEGATIVE_INFINITY;for(const o of e){const a=o[0],c=o[1];a<t&&(t=a),c<n&&(n=c),a>s&&(s=a),c>r&&(r=c)}i.geometry.bbox=[t,n,s,r]}}function Ke(i,e,t,n,s){i.geometry.coordinates=[e+n/2,t+s/2],i.geometry.bbox=[e,t,e+n,t+s],i.properties.width=n,i.properties.height=s}function de(i){const[e,t]=i.geometry.coordinates[0];return{x:e,y:t}}function Dt(i,e){const[t,n]=i.geometry.coordinates[e===O?0:1];return{x:t,y:n}}function ue(i){const[e,t]=i.geometry.coordinates[1];return{x:e,y:t}}function Je(i,e,t){i.geometry.coordinates[0]=[e,t]}function Qe(i,e,t){i.geometry.coordinates[1]=[e,t]}function Ut(i){return{start:de(i),end:ue(i)}}function tn(i,e,t,n){e===O?Je(i,t,n):Qe(i,t,n)}const gs=i=>parseInt(i.getAttribute("data-handle-id")||"-1");function fs(i){return Ft(i).reduce((e,t)=>(e[0]=Math.min(t[0],e[0]),e[1]=Math.min(t[1],e[1]),e[2]=Math.max(t[0],e[2]),e[3]=Math.max(t[1],e[3]),e),[Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY,Number.NEGATIVE_INFINITY,Number.NEGATIVE_INFINITY])}function ps(i){return Array.isArray(i)&&i.length===2&&i.every(isFinite)}function ms(i,e,t,n){for(let s=0;s<i.coordinates.length;s++){const r=i.coordinates[s];if(ps(r))r[0]=t+(r[0]-t)*e,r[1]=n+(r[1]-n)*e;else for(let o=0;o<r.length;o++){const a=r[o];a[0]=t+(a[0]-t)*e,a[1]=n+(a[1]-n)*e}}return i}function Ft(i){let e=[];if(i.type=="Point")i.bbox?e=[[i.bbox[0],i.bbox[1]],[i.bbox[2],i.bbox[3]]]:e=[i.coordinates];else if(i.type=="LineString"||i.type=="MultiPoint")e=i.coordinates;else if(i.type=="Polygon"||i.type=="MultiLineString")e=i.coordinates.reduce(function(t,n){return t.concat(n)},[]);else if(i.type=="MultiPolygon")e=i.coordinates.reduce((t,n)=>t.concat(n.reduce((s,r)=>s.concat(r),[])),[]);else if(i.type=="Feature")if(L(i)||W(i)){const t=st(i);e=[[t[0],t[1]],[t[2],t[3]]]}else e=Ft(i.geometry);else i.type=="GeometryCollection"?e=i.geometries.reduce((t,n)=>t.concat(Ft(n)),[]):i.type=="FeatureCollection"&&(e=i.features.reduce((t,n)=>t.concat(Ft(n)),[]));return e}function ys(i,e,t){const n=Math.atan2(i.y-e.y,i.x-e.x);return{x:e.x+t*Math.cos(n),y:e.y+t*Math.sin(n)}}function Lt(i,e){if(!e)return{x:i.clientX,y:i.clientY};const t=e.getBoundingClientRect();return{x:i.clientX-t.left-e.clientLeft,y:i.clientY-t.top-e.clientTop}}function ws(i,e){return i.startsWith("#")?en(i,e):i.startsWith("rgb")?nn(i,e):i}function xs(i){if(i.startsWith("#")){const e=qt(i),t=parseInt(e.slice(1,3),16),n=parseInt(e.slice(3,5),16),s=parseInt(e.slice(5,7),16);return{r:t,g:n,b:s,a:1}}else{const e=i.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([0-9.]+))?\)/);if(!e)throw new Error(`Invalid RGB color: ${i}`);return{r:parseInt(e[1]),g:parseInt(e[2]),b:parseInt(e[3]),a:e[4]!==void 0?parseFloat(e[4]):1}}}function qt(i){return i.length===4?i.split("").map(e=>e+e).join(""):i}function en(i,e){const[t,n,s]=qt(i).match(/\w\w/g).map(r=>parseInt(r,16));return`rgba(${t}, ${n}, ${s}, ${e})`}function nn(i,e){const[t,n,s]=i.match(/\d+/g).map(r=>parseInt(r,10));return`rgba(${t}, ${n}, ${s}, ${e})`}function ge(){return typeof window<"u"?window:void 0}const fe=(i,e=16,t=!1)=>{let n=!1,s=null;const r=Math.max(1,Math.round(e*60/1e3)),o=a=>{requestAnimationFrame(()=>{if(a>1)o(a-1);else if(n=!1,t&&s!==null){const c=s;s=null,i(...c),n=!0,o(r)}})};return(...a)=>{if(n){t&&(s=a);return}i(...a),n=!0,o(r)}},bs=(i,e)=>{let t;return(...s)=>{clearTimeout(t),t=setTimeout(()=>i(...s),e)}};function vs(i,e){let t;return function(...n){clearTimeout(t),t=setTimeout(()=>{i.apply(this,n)},e)}}function sn(i){if((W(i)||L(i))&&i.geometry.type==="Polygon"){const e=i.geometry.coordinates[0],t=e[0][0],n=e[0][1],s=e[2][0],r=e[2][1],o=s-t,a=r-n,c=t+o/2,h=n+a/2;return{...i,geometry:{type:"Point",coordinates:[c,h],bbox:[t,n,s,r]},properties:{...i.properties,width:o,height:a}}}return i}function pe(i,e){let t,n,s,r=1;const o=i.trim();if(o.startsWith("#")){const d=(o.length===4?qt(o):o).match(/#?(\w{2})(\w{2})(\w{2})/);if(!d)return i;t=parseInt(d[1],16),n=parseInt(d[2],16),s=parseInt(d[3],16)}else{const l=o.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([0-9.]+))?\)/);if(!l)return i;t=parseInt(l[1]),n=parseInt(l[2]),s=parseInt(l[3]),r=l[4]!==void 0?parseFloat(l[4]):1}const a=e*100,h=.299*t+.587*n+.114*s>186?-a/2:a;return t=Math.max(0,Math.min(255,t+Math.round(2.55*h))),n=Math.max(0,Math.min(255,n+Math.round(2.55*h))),s=Math.max(0,Math.min(255,s+Math.round(2.55*h))),`rgba(${t},${n},${s},${r})`}const bt=i=>pe(i,se),Ss=i=>pe(i,-se),X=(i,e)=>({x:i.x-e.x,y:i.y-e.y}),Q=i=>Math.sqrt(i.x*i.x+i.y*i.y),Es=i=>({x:-i.x,y:-i.y}),Wt=i=>{const e=Q(i);return e===0?{x:0,y:0}:{x:i.x/e,y:i.y/e}},pt=(i,e)=>({x:i.x+e.x,y:i.y+e.y}),ct=(i,e)=>({x:i.x*e,y:i.y*e}),rn=(i,e)=>{const t=Math.sin(e),n=Math.cos(e);return{x:i.x*n-i.y*t,y:i.x*t+i.y*n}},vt=(i,e)=>i.x*e.x+i.y*e.y,As=(i,e)=>i.x*e.y-i.y*e.x,tt=i=>i.properties.type==="arrow";function me(i,e,t){const{start:n,end:s}=Ut(i),r=X(e,n),o=i.properties.style.strokeWidth,a=X(s,n),c=Q(a),h=vt(r,Wt(a));return h>0&&h<c&&Math.abs(As(r,Wt(a)))<o/2+t}const mt={strokeType:"plain",strokeColor:"#202020",strokeWidth:1,head:"none",tail:"none"},Cs={id:void 0,type:"Feature",properties:{type:"arrow",style:{...mt}},geometry:{type:"LineString",coordinates:[[0,0],[100,100]]}},_t=(i=0,e=0,t=0,n=0,s={...mt})=>({id:ut(),type:"Feature",properties:{type:"arrow",style:{...mt,...s}},geometry:{type:"LineString",coordinates:[[i,e],[t,n]]}}),W=i=>i.properties.type==="box";function on(i,e,t=0,n=1,s=0){const r=gt(i),{width:o,height:a}=Y(i),c=e.x-r.x,h=e.y-r.y,l=c*n-h*t,d=c*t+h*n;return l>-s&&l<o+s&&d>-s&&d<a+s}const St={background:"#f5f5f5",strokeWidth:0,borderRadius:8,boxShadow:"",padding:16,strokeType:"plain"},Ts={id:void 0,type:"Feature",properties:{type:"box",width:100,height:50,style:{...St}},geometry:{type:"Point",coordinates:[50,25],bbox:[0,0,100,50]}},ye=(i=0,e=0,t=100,n=50,s={...St})=>({id:ut(),type:"Feature",properties:{type:"box",width:t,height:n,style:{...St,...s}},geometry:{type:"Point",coordinates:[i+t/2,e+n/2]}}),L=i=>i.properties.type==="text",V={font:"sans-serif",fontSize:18,color:"#505050",background:"#f5f5f5",strokeWidth:0,borderRadius:8,padding:16,strokeType:"plain",fixedSize:!1},ks={id:ut(),type:"Feature",properties:{type:"text",content:"",width:100,height:50,style:{...V}},geometry:{type:"Point",coordinates:[50,25],bbox:[0,0,100,50]}},we=(i=0,e=0,t=100,n=50,s="",r={...V})=>({id:ut(),type:"Feature",properties:{type:"text",content:s,width:t,height:n,style:{...V,...r}},geometry:{type:"Point",coordinates:[i+t/2,e+n/2],bbox:[i,e,i+t,e+n]}});function xe(i,e,t=0,n=0,s=1,r=1){var m;const[o,a]=i.geometry.coordinates;let{width:c,height:h}=Y(i);(m=i.properties.style)!=null&&m.fixedSize&&(c/=r,h/=r);const l=c/2,d=h/2,u=e.x-o,g=e.y-a,f=u*s-g*n,p=u*n+g*s;return f>-l-t&&f<l+t&&p>-d-t&&p<d+t}const D=i=>i.properties.type==="comment",K={background:"#FFFACD",padding:8,borderRadius:4,strokeColor:"#DDD",strokeWidth:1,strokeType:"plain",iconColor:"#FFFACD",iconSymbol:"💬",iconBorderColor:"#aaa",iconBorderWidth:1,minHeight:60,maxHeight:480,iconSize:32,color:"#333",font:"Arial, sans-serif",fontSize:12,showSendButton:!0,autoGrow:!0,shadow:!0,expandOnSelect:!1,fixedSize:!0},jt={mode:"expanded",width:200,height:60,style:K};function be(i,e,t,n){const s={type:"comment",content:t,mode:(n==null?void 0:n.mode)??jt.mode,width:(n==null?void 0:n.width)??jt.width,height:(n==null?void 0:n.height)??jt.height,author:n==null?void 0:n.author,timestamp:n==null?void 0:n.timestamp,style:{...K,...n==null?void 0:n.style}};return{id:ut(),type:"Feature",properties:s,geometry:{type:"Point",coordinates:[i,e]}}}function Ms(i){return{...i,properties:{...i.properties,mode:i.properties.mode===ot?Mt:ot}}}function an(i,e,t=0,n,s,r=1){if(i.properties.mode!==ot)return xe(i,e,t,n,s,r);const o=i.properties,a={...K,...o.style};let c=a.iconSize,h=a.iconSize;a.fixedSize&&(c/=r,h/=r);const l=c/2,d=h/2,u=e.x-i.geometry.coordinates[0],g=e.y-i.geometry.coordinates[1];return u>=-l-t&&u<=l+t&&g>=-d-t&&g<=d+t}function Ps(i){const[e,t]=i.geometry.coordinates;return{x:e,y:t}}function Ds(i){const e=i.properties,t={...K,...e.style};return e.mode===ot?{width:t.iconSize,height:t.iconSize}:{width:e.width,height:e.height}}function ln(i,e=80){const t=e/i.properties.width;return Math.max(.1,Math.min(1,t))}function Fs(i){const e={...K,...i.properties.style};return e.collapseZoomThreshold!==void 0?e.collapseZoomThreshold:ln(i)}function Ls(i,e,t,n,s="",r){const o=be(t,n,s,r==null?void 0:r.commentStyle),a={...mt,head:"arrow",...r==null?void 0:r.arrowStyle},c=_t(t,n,i,e,a);return c.properties.link={[O]:{id:o.id,side:O,type:"comment",magnet:{x:0,y:0}}},{comment:o,arrow:c}}function _s(i,e,t,n,s,r,o,a,c){const h=i-o,l=e-a,d=o+h*r-l*s,u=a+h*s+l*r,g=o+(h+t)*r-l*s,f=a+(h+t)*s+l*r,p=o+(h+t)*r-(l+n)*s,m=a+(h+t)*s+(l+n)*r,x=o+h*r-(l+n)*s,v=a+h*s+(l+n)*r;return c[0]=Math.min(d,g,p,x),c[1]=Math.min(u,f,m,v),c[2]=Math.max(d,g,p,x),c[3]=Math.max(u,f,m,v),c}function Hs(i,e,t){const n=t.x-e.x,s=t.y-e.y,r=n*n+s*s;if(r===0){const d=i.x-e.x,u=i.y-e.y;return Math.sqrt(d*d+u*u)}let o=((i.x-e.x)*n+(i.y-e.y)*s)/r;o=Math.max(0,Math.min(1,o));const a=e.x+o*n,c=e.y+o*s,h=i.x-a,l=i.y-c;return Math.sqrt(h*h+l*l)}const it=i=>i.properties.type==="polygon";function ve(i,e,t=0){const n=i.geometry.coordinates[0],s=i.geometry.bbox;if(s){const[o,a,c,h]=s;if(e.x<o-t||e.x>c+t||e.y<a-t||e.y>h+t)return!1}let r=!1;for(let o=0,a=n.length-1;o<n.length;a=o++){const c=n[o][0],h=n[o][1],l=n[a][0],d=n[a][1];h>e.y!=d>e.y&&e.x<(l-c)*(e.y-h)/(d-h)+c&&(r=!r)}if(r)return!0;if(t>0)for(let o=0;o<n.length-1;o++){const[a,c]=n[o],[h,l]=n[o+1];if(Hs(e,{x:a,y:c},{x:h,y:l})<=t)return!0}return!1}function cn(i,e){const t=i[0];t.length>0&&(t[0][0]!==t[t.length-1][0]||t[0][1]!==t[t.length-1][1])&&t.push([t[0][0],t[0][1]]);let n=1/0,s=1/0,r=-1/0,o=-1/0;for(const[d,u]of t)d<n&&(n=d),d>r&&(r=d),u<s&&(s=u),u>o&&(o=u);const a=[n,s,r,o],{id:c,style:h,...l}=e||{};return{type:"Feature",id:c||ut(),geometry:{type:"Polygon",coordinates:i,bbox:a},properties:{type:"polygon",style:{...ht,...h},...l}}}const ht={...St,strokeColor:"#000000",strokeWidth:2,background:"transparent"},Is={type:"polygon",style:{...ht}},Se=i=>i.type==="FeatureCollection";function Ee(i){return/^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/.test(i.trim())}function Ae(i){return/^rgb\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*\)$/.test(i.trim())}function Ce(i){return/^rgba\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*,\s*[0-9.]+\s*\)$/.test(i.trim())}function hn(i){return Ee(i)||Ae(i)||Ce(i)}function Bs(i){if(hn(i))return i;throw new Error(`Invalid color format: ${i}`)}function zs(i){if(Ee(i))return i;throw new Error(`Invalid hex color format: ${i}`)}function Rs(i){if(Ae(i))return i;throw new Error(`Invalid RGB color format: ${i}`)}function $s(i){if(Ce(i))return i;throw new Error(`Invalid RGBA color format: ${i}`)}class Ns{constructor(e){y(this,"previousZoom",-1);this.store=e}toggleComment(e){const t=this.store.getState().getFeature(e);if(!t||!D(t))return;const n=t;this.store.getState().applyLiveUpdate(e,{properties:{...n.properties,mode:n.properties.mode==="collapsed"?"expanded":"collapsed"}})}updateCommentModesForZoom(e){if(Math.abs(this.previousZoom-e)<5e-4)return;this.previousZoom=e;const t=this.store.getState(),n=t.features,s={};Object.values(n).forEach(r=>{if(D(r)){const o=r,a=this.getCommentZoomThreshold(o),c=e<a?ot:Mt;if(o.properties.mode===c)return;s[o.id]={properties:{...o.properties,mode:c}}}}),t.applyLiveUpdates(s)}getCommentZoomThreshold(e){const t={...e.properties.style};if(t.collapseZoomThreshold!==void 0)return t.collapseZoomThreshold;const s=80/e.properties.width;return Math.max(.1,Math.min(1,s))}}class Gt extends EventTarget{constructor(t,n){super();y(this,"annotation",null);y(this,"ogma");y(this,"dragging",!1);y(this,"dragStartPoint");y(this,"hoveredHandle");y(this,"ogmaPanningOption",!1);y(this,"store");y(this,"draggingWasEnabled",!0);y(this,"handleMouseMove",t=>{if(t.target instanceof HTMLTextAreaElement||!this.isActive())return;const n=this.store.getState();if(!this.dragging&&n.mousePressed&&n.mousePressPoint&&(this.detectHandle(t,this.ogma.view.getZoom()),this.hoveredHandle)){this.dragStartPoint=n.mousePressPoint,this.onDragStart(t),this.dispatchEvent(new CustomEvent(xt,{detail:{id:this.annotation,position:{x:t.clientX,y:t.clientY}}})),this.disablePanning();return}this.dragging?this.dragStartPoint&&this.onDrag(t):this.detectHandle(t,this.ogma.view.getZoom())});y(this,"handleMouseDown",t=>{t.target instanceof HTMLTextAreaElement||!this.isActive()||this.dragging||this.hoveredHandle||this.detectHandle(t,this.ogma.view.getZoom())});y(this,"disablePanning",()=>{var t,n;this.ogmaPanningOption=!!((n=(t=this.ogma.getOptions().interactions)==null?void 0:t.pan)!=null&&n.enabled),this.ogma.setOptions({interactions:{pan:{enabled:!1},drag:{enabled:!1}}})});y(this,"restorePanning",()=>{this.ogma.setOptions({interactions:{pan:{enabled:!0},drag:{enabled:!0}}})});y(this,"handleMouseUp",t=>{this.isActive()&&this.dragging&&(this.restorePanning(),this.onDragEnd(t),this.dispatchEvent(new CustomEvent(kt,{detail:{id:this.annotation,position:{x:t.clientX,y:t.clientY}}})))});y(this,"onClick",t=>{});this.store=n,this.ogma=t,this.store.subscribe(s=>s.features,s=>{this.isActive()&&!s[this.annotation]&&this.stopEditing()})}cancelEdit(){!this.isActive()||this.annotation===null||this.clearDragState()}clearDragState(){this.dragging=!1,this.dragStartPoint=void 0,this.hoveredHandle=void 0,this.restorePanning(),this.setCursor(H.default)}commitChange(){this.getAnnotation()&&this.store.getState().commitLiveUpdates()}onDrag(t){this.dispatchEvent(new Event(Tt))}onDragStart(t){return this.isActive()?(this.dragging=!0,this.dragStartPoint=this.clientToCanvas(t),this.disablePanning(),!0):!1}onDragEnd(t){return this.isActive()?(this.restorePanning(),this.dragging=!1,!0):!1}clientToCanvas(t){const n=this.ogma,s=Lt(t,n.getContainer());return n.view.screenToGraphCoordinates(s)}setAnnotation(t){if(this.annotation=t?t.id:null,this.annotation!==null){const n=this.ogma.getContainer();if(n){const s=ge()||n;s.addEventListener("mousemove",this.handleMouseMove),s.addEventListener("mouseup",this.handleMouseUp,!1),n.addEventListener("mousedown",this.handleMouseDown,!0),s.addEventListener("click",this.onClick,!0)}}else{const n=this.ogma.getContainer();if(n){const s=ge()||n;s.removeEventListener("mousemove",this.handleMouseMove),s.removeEventListener("mouseup",this.handleMouseUp),n.removeEventListener("mousedown",this.handleMouseDown),s.removeEventListener("click",this.onClick)}this.clearDragState(),this.setCursor(H.default)}}getAnnotation(t){const n=this.store.getState(),s=n.getFeature(this.annotation);if(!t)return s;const r=n.liveUpdates[this.annotation];return s&&r?{...s,...r}:s}setCursor(t){var s;const n=(s=this.ogma.getContainer())==null?void 0:s.firstChild;n&&(n.style.cursor=t)}stopEditing(){this.isActive()&&this.setAnnotation(null)}cancelDrawing(){if(!this.isActive())return;const t=this.store.getState();t.drawingFeature===this.annotation&&t.removeFeature(this.annotation),this.stopEditing()}isActive(){return this.annotation!==null}}class Os extends Gt{constructor(t,n,s,r,o,a,c){super(t,n);y(this,"links");y(this,"snapping");y(this,"arrowHandler");y(this,"arrowStyle");y(this,"offsetX");y(this,"offsetY");y(this,"comment");y(this,"startX",0);y(this,"startY",0);y(this,"onArrowComplete",()=>{var S;this.arrowHandler.removeEventListener("dragend",this.onArrowComplete);const t=this.store.getState(),n=Array.from(t.selectedFeatures);if(n.length===0)return;const s=t.getFeature(n[0]);if(!s||!tt(s))return;const r=s.geometry.coordinates[0],o=s.geometry.coordinates[1],a=o[0]-this.startX,c=o[1]-this.startY,h=Math.sqrt(a*a+c*c);let l,d;h<5?(l=this.startX+this.offsetX,d=this.startY+this.offsetY):(l=o[0],d=o[1]);const u=this.comment;u.geometry.coordinates=[l,d],t.addFeature(u),this.store.setState({drawingFeature:u.id});const g=u.properties.height,f=t.zoom,p=g/f,m=l,x=d+p*.5;this.snapArrowStart(s,r[0],r[1]);const v=(S=s.properties.link)==null?void 0:S.start;t.updateFeature(s.id,{...s,geometry:{...s.geometry,coordinates:[[m,x],r]},properties:{...s.properties,link:{start:{side:O,id:u.id,type:P.COMMENT,magnet:{x:0,y:.5}},end:v?{side:rt,id:v.id,type:v.type,magnet:v.magnet}:void 0}}}),this.links.add(s,O,u.id,P.COMMENT,{x:0,y:.5}),v&&v.magnet&&this.links.add(s,rt,v.id,v.type,v.magnet),t.setSelectedFeatures([u.id]),this.store.setState({drawingFeature:null})});this.links=s,this.arrowHandler=o,this.snapping=r,this.offsetX=(c==null?void 0:c.offsetX)??100,this.offsetY=(c==null?void 0:c.offsetY)??-50,this.arrowStyle=c==null?void 0:c.arrowStyle,this.comment=a}detectHandle(t,n){}startDrawing(t,n,s){var o;this.startX=n,this.startY=s;const r=_t(n,s,n,s,{...mt,head:"arrow",...(o=this.arrowStyle)==null?void 0:o.style});this.store.getState().addFeature(r),this.store.setState({drawingFeature:r.id}),this.arrowHandler.addEventListener("dragend",this.onArrowComplete),this.store.getState().setSelectedFeatures([r.id]),this.arrowHandler.startDrawing(r.id,n,s)}snapArrowStart(t,n,s){const r=this.snapping.snap({x:n,y:s});r&&(t.geometry.coordinates[0]=[r.point.x,r.point.y],t.properties.link={start:{side:O,id:r.id,type:r.type,magnet:r.magnet}},this.links.add(t,O,r.id,r.type,r.magnet))}}function Ys(i,e,t,n,s){const{width:r,height:o}=n.view.getSize(),a=8,c=16,h=a*c,l={width:150,height:50},d=l.width,u=l.height,g=Math.max(r,o),f=200,p={minX:0,minY:0,maxX:0,maxY:0},m=n.view.graphToScreenCoordinates({x:i,y:e});for(let x=0;x<h;x++){const v=x%a,S=Math.floor(x/a),E=2*Math.PI*(v/a),b=f+S/c*(g-f),A=Math.cos(E)*b,T=Math.sin(E)*b,C=m.x+A,M=m.y+T;if(p.minX=C,p.minY=M-u/2,p.maxX=C+d,p.maxY=M+u/2,A<0&&(p.minX-=d,p.maxX-=d),p.minX<0||p.maxX>r||p.minY<0||p.maxY>o)continue;const F=n.view.screenToGraphCoordinates({x:p.minX,y:p.minY}),j=n.view.screenToGraphCoordinates({x:p.maxX,y:p.maxY});if(p.minX=F.x,p.minY=F.y,p.maxX=j.x,p.maxY=j.y,!t.query(p).filter(R=>R.properties.type!=="arrow").length)return n.view.screenToGraphCoordinates({x:C,y:M})}return n.view.screenToGraphCoordinates({x:r/2,y:o/2})}class Xs{constructor(e,t,n,s,r,o,a){y(this,"ogma");y(this,"store");y(this,"editor");y(this,"interactions");y(this,"links");y(this,"control");y(this,"index");y(this,"pendingDrawingListener",null);y(this,"placementCleanup",null);this.ogma=e,this.store=t,this.editor=n,this.interactions=s,this.links=r,this.control=o,this.index=a}cancelPendingDrawing(){this.pendingDrawingListener&&(this.ogma.events.off(this.pendingDrawingListener),this.pendingDrawingListener=null),this.placementCleanup&&(this.placementCleanup(),this.placementCleanup=null)}isDrawing(){return this.store.getState().drawingFeature!==null}enableDrawingMode(e){this.control.unselect().cancelDrawing();const t=n=>{this.ogma.events.off(t),this.pendingDrawingListener=null;const{x:s,y:r}=this.ogma.view.screenToGraphCoordinates(n);e(s,r)};return this.pendingDrawingListener=t,this.ogma.events.once("mousedown",t),this.control}enableArrowDrawing(e){return this.enableDrawingMode((t,n)=>{const s=_t(t,n,t,n,e);this.startArrow(t,n,s)})}enableTextDrawing(e){return this.enableDrawingMode((t,n)=>{const s=we(t,n,0,0,void 0,e);this.startText(t,n,s)})}enableBoxDrawing(e){return this.enableDrawingMode((t,n)=>{const s=ye(t,n,0,0,e);this.startBox(t,n,s)})}enablePolygonDrawing(e){return this.enableDrawingMode((t,n)=>{const s=cn([[[t,n]]],{style:e});this.startPolygon(t,n,s)})}enableCommentDrawing(e={}){return this.enableDrawingMode((t,n)=>{let s=e.offsetX,r=e.offsetY;if(s===void 0&&r===void 0){const a=Ys(t,n,this.index,this.ogma);s=a.x,r=a.y}const o=be(t,n,"",e==null?void 0:e.commentStyle);this.startComment(t,n,o,{...e,offsetX:s,offsetY:r})})}enablePlacement(e){this.control.unselect().cancelDrawing(),this.store.setState({drawingFeature:e.id}),this.control.add(e);const t=this.ogma.getContainer();if(!t)return this.control;const n=a=>{const{x:c,y:h}=this.ogma.view.screenToGraphCoordinates(a),l=e.properties.width,d=e.properties.height;this.store.getState().applyLiveUpdate(e.id,{geometry:{...e.geometry,coordinates:[c,h],bbox:[c-l/2,h-d/2,c+l/2,h+d/2]}})},s=a=>{o();const{x:c,y:h}=this.ogma.view.screenToGraphCoordinates(a),l=e.properties.width,d=e.properties.height;this.store.getState().updateFeature(e.id,{geometry:{...e.geometry,coordinates:[c,h],bbox:[c-l/2,h-d/2,c+l/2,h+d/2]}}),this.store.setState({drawingFeature:null}),this.interactions.suppressClicksTemporarily(200),this.control.select(e.id)},r=a=>{a.key==="Escape"&&(o(),this.control.remove(e),this.store.setState({drawingFeature:null}))},o=()=>{t.removeEventListener("mousemove",n),this.ogma.events.off(s),document.removeEventListener("keydown",r),this.placementCleanup=null};return t.addEventListener("mousemove",n,{passive:!0}),this.ogma.events.once("mousedown",s),document.addEventListener("keydown",r),this.placementCleanup=o,this.control}startComment(e,t,n,s){this.editor.getActiveHandler()&&this.editor.getActiveHandler().stopEditing(),this.control.cancelDrawing(),this.store.setState({drawingFeature:n.id}),this.interactions.suppressClicksTemporarily(200);const r=new Os(this.ogma,this.store,this.links,this.editor.getSnapping(),this.editor.getArrowHandler(),n,s),o=a=>{var c;a.id===n.id&&(this.control.select(a.id),this.control.off(Yt,o),(c=this.editor.getActiveHandler())==null||c.startEditingText())};return this.control.on(Yt,o),r.startDrawing(n.id,e,t),this.control}startBox(e,t,n){return n||(n=ye(e,t)),this.store.setState({drawingFeature:n.id}),this.control.add(n),this.interactions.suppressClicksTemporarily(200),this.control.select(n.id),this.editor.getActiveHandler().startDrawing(n.id,e,t),this.control}startArrow(e,t,n){return n||(n=_t(e,t)),this.editor.getActiveHandler()&&this.editor.getActiveHandler().stopEditing(),this.control.cancelDrawing(),this.store.setState({drawingFeature:n.id}),this.control.add(n),this.interactions.suppressClicksTemporarily(200),this.control.select(n.id),this.editor.getActiveHandler().startDrawing(n.id,e,t),this.control}startText(e,t,n){return n||(n=we(e,t)),this.store.setState({drawingFeature:n.id}),this.control.add(n),this.interactions.suppressClicksTemporarily(200),this.control.select(n.id),this.editor.getActiveHandler().startDrawing(n.id,e,t),this.control}startPolygon(e,t,n){return this.store.setState({drawingFeature:n.id}),this.control.add(n),this.interactions.suppressClicksTemporarily(200),this.control.select(n.id),this.editor.getActiveHandler().startDrawing(n.id,e,t),this.control}}class Us{constructor(e,t){this.store=e,this.links=t}undo(){return this.canUndo()?(this.store.temporal.getState().undo(),this.links.refresh(),!0):!1}redo(){return this.canRedo()?(this.store.temporal.getState().redo(),this.links.refresh(),!0):!1}canUndo(){return this.store.temporal.getState().pastStates.length>0}canRedo(){return this.store.temporal.getState().futureStates.length>0}clearHistory(){this.store.temporal.getState().clear()}}class qs{constructor(e){this.store=e}select(e){const t=Array.isArray(e)?e:[e];this.store.getState().setSelectedFeatures(t)}unselect(e){const t=Array.isArray(e)?e:[e];if(e===void 0)this.store.getState().setSelectedFeatures([]);else{const n=new Set(t),s=Array.from(this.store.getState().selectedFeatures).filter(r=>!n.has(r));this.store.getState().setSelectedFeatures(s)}}getSelectedAnnotations(){const e=this.store.getState();return{type:"FeatureCollection",features:Array.from(e.selectedFeatures).map(t=>e.features[t])}}getSelected(){const e=this.store.getState(),t=Array.from(e.selectedFeatures)[0];return t?e.features[t]:null}}class Ws{constructor(e){this.store=e}add(e){if(Se(e))for(const t of e.features)this.add(t);else{const t=sn(e);this.store.getState().addFeature(t)}}remove(e){if(Se(e))for(const t of e.features)this.remove(t);else this.store.getState().removeFeature(e.id)}getAnnotations(){const e=this.store.getState().features;return{type:"FeatureCollection",features:Object.values(e)}}getAnnotation(e){return this.store.getState().getFeature(e)}updateStyle(e,t){const n=this.store.getState().getFeature(e);n&&this.store.getState().updateFeature(e,{id:e,properties:{...n.properties,style:{...n.properties.style,...t}}})}update(e){var s;const t=this.store.getState(),n=t.getFeature(e.id);n&&t.updateFeature(e.id,{...n,...e,properties:{...n.properties,...e.properties,style:{...n.properties.style,...(s=e.properties)==null?void 0:s.style}},geometry:{...n.geometry,...e.geometry}})}setScale(e,t,n,s){const r=this.store.getState().getFeature(e);if(!r)return;const o=this.store.getState();if(tt(r)){const a=r.geometry.coordinates.map(([c,h])=>{const l=c-n,d=h-s;return[n+l*t,s+d*t]});o.updateFeature(e,{geometry:{...r.geometry,coordinates:a}})}else if(L(r)||W(r)){const[a,c]=r.geometry.coordinates,h=a-n,l=c-s,d=n+h*t,u=s+l*t,g=r.properties.width*t,f=r.properties.height*t;o.updateFeature(e,{properties:{...r.properties,width:g,height:f},geometry:{...r.geometry,coordinates:[d,u]}})}}}function dn(i){var t,n;const e=i.properties.link;return e?((t=e.start)==null?void 0:t.type)===P.COMMENT||((n=e.end)==null?void 0:n.type)===P.COMMENT:!1}function un(i){var e,t;return((t=(e=i.properties.link)==null?void 0:e.start)==null?void 0:t.type)!==P.COMMENT}function js(i){return!0}function Te(i,e,t,n,s=!1){const r=i.getState(),o=r.getFeature(t);if(!o)return;const a=new Set,c=new Set;let h="";if(tt(o)){const d=o;if(s){const u=d.properties.link||{};u.start&&Vt(u.start.type)&&a.add(u.start.id),u.end&&Vt(u.end.type)&&a.add(u.end.id)}h=d.id,c.add(d.id)}else a.add(o.id);const l={};for(const d of a){const u=r.getFeature(d);if(!u)continue;const g=Gs(u,n);g&&(l[d]=g,e.updateLinkedArrowsDuringDrag(d,n,l))}for(const d of c){const u=r.getFeature(d);if(!u)continue;const g=u.properties.link||{},f=u.geometry.coordinates,p=[...f];(g.start&&Vt(g.start.type)||h===d)&&(p[0]=[f[0][0]+n.x,f[0][1]+n.y]),(g.end&&Vt(g.end.type)||h===d)&&(p[1]=[f[1][0]+n.x,f[1][1]+n.y]),l[d]={geometry:{...u.geometry,coordinates:p}}}r.applyLiveUpdates(l)}function Vt(i){return i!==P.NODE&&i!==P.EDGE}function Gs(i,e){if(L(i)||W(i)||D(i)){const t=J(i);return{geometry:{type:i.geometry.type,coordinates:[t.x+e.x,t.y+e.y]}}}if(it(i)){const t=i,n=t.geometry.coordinates.map(s=>s.map(r=>[r[0]+e.x,r[1]+e.y]));return{geometry:{type:t.geometry.type,coordinates:n}}}return null}class Vs extends Gt{constructor(t,n,s,r){super(t,n);y(this,"snapping");y(this,"links");y(this,"snap",null);this.snapping=s,this.links=r}detectHandle(t){const n=this.getAnnotation(),s=this.clientToCanvas(t),r=ne,o=n.geometry.coordinates[0],a=n.geometry.coordinates[1],c=Math.sqrt(Math.pow(s.x-o[0],2)+Math.pow(s.y-o[1],2)),h=Math.sqrt(Math.pow(s.x-a[0],2)+Math.pow(s.y-a[1],2));c<r?this.grabHandle("start",o[0],o[1]):h<r?this.grabHandle("end",a[0],a[1]):me(n,s,r)?(this.setCursor(H.grab),this.hoveredHandle={type:"body",point:s},this.store.setState({hoveredHandle:2,hoveredFeature:this.annotation})):(this.store.setState({hoveredHandle:-1,hoveredFeature:null}),this.hoveredHandle=void 0)}grabHandle(t,n,s){this.hoveredHandle={type:t,point:{x:n,y:s}},this.store.setState({hoveredHandle:t==="start"?0:1,hoveredFeature:this.annotation}),this.setCursor(H.move)}onDrag(t){var l;if(!(this.dragStartPoint||!this.hoveredHandle)||!this.isActive())return;t.stopPropagation(),t.stopImmediatePropagation();const n=this.hoveredHandle,s=this.getAnnotation(),r=s.properties.link||{},o=this.clientToCanvas(t);this.snap=this.snapping.snap(o);const a=((l=this.snap)==null?void 0:l.point)||o,c=new CustomEvent(Tt,{detail:{point:a,annotation:this.annotation,handle:n}}),h=[...s.geometry.coordinates];if(n.type==="body"){const d=a.x-n.point.x,u=a.y-n.point.y;Te(this.store,this.links,s.id,{x:d,y:u},!0),this.dispatchEvent(c);return}else n.type==="start"?(h[0]=[a.x,a.y],this.snap&&(r.start={side:n.type,id:this.snap.id,type:this.snap.type,magnet:this.snap.magnet})):n.type==="end"&&(h[1]=[a.x,a.y],this.snap&&(r.end={side:n.type,id:this.snap.id,type:this.snap.type,magnet:this.snap.magnet}));this.store.getState().applyLiveUpdate(s.id,{id:s.id,properties:{...s.properties,link:r},geometry:{type:s.geometry.type,coordinates:h}}),this.dispatchEvent(c)}onDragStart(t){if(!super.onDragStart(t))return!1;const n=this.getAnnotation(),s=this.hoveredHandle;return s.type==="end"&&!1||s.type==="start"&&!un(n)?(this.clearDragState(),!1):(this.store.getState().startLiveUpdate([this.annotation]),!0)}onDragEnd(t){var r,o,a,c;if(!super.onDragEnd(t))return!1;const n=this.getAnnotation(!0);if(this.hoveredHandle){const h=n.geometry.coordinates;if(this.hoveredHandle.type==="body"){const l=this.snapping.snap({x:h[0][0],y:h[0][1]}),d=this.snapping.snap({x:h[1][0],y:h[1][1]});l?this.links.add(n,"start",l.id,l.type,l.magnet):((o=(r=n.properties.link)==null?void 0:r.start)==null?void 0:o.type)!=="comment"&&this.links.remove(n,O),d?this.links.add(n,"end",d.id,d.type,d.magnet):((c=(a=n.properties.link)==null?void 0:a.end)==null?void 0:c.type)!=="comment"&&this.links.remove(n,rt)}else if(this.snap){const l=this.hoveredHandle;l.type!=="body"&&this.links.add(n,l.type,this.snap.id,this.snap.type,this.snap.magnet)}else if(this.snap===null){const l=this.hoveredHandle.type;if(n.properties.link&&n.properties.link[l]){this.links.remove(n,l);const d={...n.properties.link};delete d[l],this.store.getState().applyLiveUpdate(n.id,{properties:{...n.properties,link:d}})}}}return this.store.getState().drawingFeature===this.annotation&&this.store.setState({drawingFeature:null}),this.commitChange(),this.clearDragState(),this.snap=null,!0}snapDrawingStart(t,n,s){const r=this.snapping.snap({x:n,y:s});if(r){const o={geometry:{...t.geometry,coordinates:[[r.point.x,r.point.y],t.geometry.coordinates[1]]},properties:{...t.properties,link:{start:{side:O,id:r.id,type:r.type,magnet:r.magnet}}}},a=this.store.getState();a.applyLiveUpdate(t.id,o),a.updateFeature(t.id,o),this.links.add(t,"start",r.id,r.type,r.magnet)}}startDrawing(t,n,s){this.annotation=t;const r=this.getAnnotation();this.snapDrawingStart(r,n,s),this.grabHandle("end",n,s),this.dragging=!0,this.dragStartPoint={x:n,y:s};const o=this.ogma.view.graphToScreenCoordinates({x:n,y:s});this.onDragStart({clientX:o.x,clientY:o.y})}link(t,n,s){let r=Dt(t,s);const o=t.properties.link||{};let a=null;if(n instanceof q.Node)r=n.getPosition(),a=this.snapping.snapToNodes(r,n.toList());else{const c=this.store.getState().getFeature(n);if(!c)throw new Error(`Annotation with id ${n} not found`);if(L(c)||W(c))r=J(c),a=this.snapping.snapToText(r,[c]);else if(it(c))r=ce(c),a=this.snapping.snapToPolygon(r,[c]);else throw new Error(`Cannot link arrow to annotation of type ${c.properties.type}`)}a&&(o[s]={side:s,id:a.id,type:a.type,magnet:a.magnet},this.links.add(t,s,a.id,a.type,a.magnet),tn(t,s,r.x,r.y),this.store.getState().updateFeature(t.id,{properties:{...t.properties,link:o},geometry:{type:t.geometry.type,coordinates:t.geometry.coordinates}}))}}class Zs extends Gt{constructor(t,n,s){super(t,n);y(this,"links");y(this,"isDrawingMode",!1);y(this,"simplificationTolerance",5);y(this,"handleMouseDown",t=>{!this.isActive()||this.dragging||!this.hoveredHandle||(t.preventDefault(),t.stopPropagation(),this.dragStartPoint=this.clientToCanvas(t),this.onDragStart(t),this.dispatchEvent(new CustomEvent(xt,{detail:{id:this.annotation,position:{x:t.clientX,y:t.clientY}}})),this.disablePanning())});this.links=s}startDrawing(t,n,s){this.annotation=t,this.isDrawingMode=!0;const r=this.store.getState();r.startLiveUpdate([t]),r.setDrawingPoints([]);const o=this.ogma.view.graphToScreenCoordinates({x:n,y:s});return this.dragStartPoint=o,this.disablePanning(),this.onDragStart({clientX:o.x,clientY:o.y}),this}cancelDrawing(){this.isDrawingMode&&(this.isDrawingMode=!1,this.annotation&&(this.store.getState().removeFeature(this.annotation),this.annotation=null)),this.restorePanning()}detectHandle(t,n=1){const s=this.getAnnotation();if(!s)return;const r=this.clientToCanvas(t),o=ne/n,a=s.geometry.coordinates[0];for(let c=0;c<a.length-1;c++){const[h,l]=a[c];if(Math.sqrt(Math.pow(r.x-h,2)+Math.pow(r.y-l,2))<o*2){this.grabHandle("vertex",h,l,c);return}}ve(s,r,o)?(this.setCursor(H.grab),this.hoveredHandle={type:"body",point:r},this.store.setState({hoveredHandle:-1,hoveredFeature:this.annotation})):(this.store.setState({hoveredHandle:-1,hoveredFeature:null}),this.hoveredHandle=void 0)}grabHandle(t,n,s,r){this.setCursor(t==="vertex"?H.pointer:H.grab),this.hoveredHandle={type:t,point:{x:n,y:s},vertexIndex:r},this.store.setState({hoveredHandle:r??0,hoveredFeature:this.annotation})}onDragStart(t){if(this.isDrawingMode){const n=this.clientToCanvas(t);this.dragging=!0;const s=this.getAnnotation();if(s){const r=[[n.x,n.y],[n.x,n.y]],o=this.store.getState();o.setDrawingPoints(r),o.applyLiveUpdate(s.id,{...s,geometry:{type:"Polygon",coordinates:[r],bbox:[n.x,n.y,n.x,n.y]}})}return this.dragStartPoint=n,!0}return this.hoveredHandle?(this.dragging=!0,this.setCursor(H.grabbing),this.store.getState().startLiveUpdate([this.annotation]),!0):!1}onDrag(t){if(this.isDrawingMode&&this.dragging){const a=this.clientToCanvas(t),c=this.store.getState(),h=this.getAnnotation();if(!h)return;const l=c.liveUpdates[this.annotation],d=l?{...h,...l}:h,g=c.drawingPoints.slice(0,-1),f=[...g,[a.x,a.y],g[0]];c.setDrawingPoints(f);const p=ae(f,this.simplificationTolerance,!1),m=[...p,p[0]];c.applyLiveUpdate(this.annotation,{...d,geometry:{type:"Polygon",coordinates:[m]}});return}if(!this.dragging||!this.dragStartPoint||!this.hoveredHandle)return;const n=this.clientToCanvas(t),s=n.x-this.dragStartPoint.x,r=n.y-this.dragStartPoint.y,o=this.getAnnotation();if(o){if(this.hoveredHandle.type==="body")Te(this.store,this.links,o.id,{x:s,y:r});else if(this.hoveredHandle.type==="vertex"&&this.hoveredHandle.vertexIndex!==void 0){const a=o.geometry.coordinates[0].map((h,l)=>l===this.hoveredHandle.vertexIndex?[this.hoveredHandle.point.x+s,this.hoveredHandle.point.y+r]:l===o.geometry.coordinates[0].length-1&&this.hoveredHandle.vertexIndex===0?[this.hoveredHandle.point.x+s,this.hoveredHandle.point.y+r]:h),c={...o,geometry:{...o.geometry,coordinates:[a]}};he(c),this.store.getState().applyLiveUpdate(o.id,c),this.links.snapLinkedArrowsDuringDrag(o.id)}this.dispatchEvent(new Event(Tt))}}onDragEnd(){if(this.isDrawingMode&&this.dragging){const n=this.store.getState(),s=this.getAnnotation();if(s){const r=n.drawingPoints;if(r.length<3)this.cancelDrawing();else{const o=ae(r.slice(1,-1),this.simplificationTolerance,!0),a={...s,geometry:{type:"Polygon",coordinates:[o]}};ft(a),this.store.getState().applyLiveUpdate(s.id,a),this.store.setState({drawingFeature:null}),this.commitChange(),this.isDrawingMode=!1,this.dragging=!1,this.restorePanning(),this.dispatchEvent(new CustomEvent(ee,{detail:a}))}}return!0}return!this.dragging||(this.dragging=!1,this.dragStartPoint=void 0,!this.getAnnotation())?!1:(this.commitChange(),this.setCursor(H.grab),!0)}getAnnotation(){if(this.annotation)return this.store.getState().getFeature(this.annotation)}}class Ks{constructor(e,t,n,s=()=>{}){y(this,"layer");y(this,"textarea");y(this,"sendButton",null);y(this,"isFocused");y(this,"unsubscribe");y(this,"onFocus",()=>{this.isFocused=!0});y(this,"onBlur",()=>{this.getAnnotation()&&this.updateContent(),this.isFocused=!1});y(this,"onKeyup",e=>{e.key==="Escape"&&this.textarea.blur(),e.stopPropagation()});y(this,"onKeydown",e=>{e.stopPropagation()});y(this,"onMouseEvent",e=>{e.stopPropagation()});y(this,"onInput",()=>{this.updateContent(),this.updateStyle(),this.updatePosition(),this.layer.setSize(this.getSize()),this.updateSendButtonState()});y(this,"onSendClick",e=>{e.preventDefault(),e.stopPropagation(),!(!this.sendButton||this.sendButton.disabled)&&(this.updateContent(),this.textarea.blur(),this.onSendHandler())});y(this,"update",()=>{this.getAnnotation()&&(this.updateStyle(),this.updatePosition(),this.layer.setSize(this.getSize()))});var u,g,f;this.ogma=e,this.store=t,this.annotation=n,this.onSendHandler=s;const r=this.getPosition(),o=this.getSize(),a=this.getAnnotation(),c=this.store.getState(),h=D(a)&&(((u=c.options)==null?void 0:u.showSendButton)??!0),l=((g=c.options)==null?void 0:g.sendButtonIcon)||"",d=((f=c.options)==null?void 0:f.textPlaceholder)||"Enter text";this.layer=this.ogma.layers.addOverlay({element:`<div class="ogma-annotation-text-editor">
7
7
  <textarea wrap="on" name="annotation-text--input" spellcheck="false" placeholder="${d}"></textarea>
8
8
  ${h?`<button class="ogma-send-button" type="button" title="Send">
9
9
  <span class="ogma-send-button-icon">${l}</span>
10
10
  </button>`:""}
11
- </div>`,position:r,size:o},Xt.EDITOR),this.textarea=this.layer.element.querySelector("textarea"),this.textarea.setAttribute("wrap","on"),this.textarea.setAttribute("spellcheck","false"),this.textarea.value=a.properties.content||"",h&&(this.sendButton=this.layer.element.querySelector(".ogma-send-button"),this.sendButton.addEventListener("click",this.onSendClick),this.updateSendButtonState()),this.isFocused=!1,this.textarea.addEventListener("focus",this.onFocus),this.textarea.addEventListener("blur",this.onBlur),this.textarea.addEventListener("input",this.onInput),this.textarea.addEventListener("keyup",this.onKeyup),this.textarea.addEventListener("keydown",this.onKeydown),this.textarea.addEventListener("wheel",this.onMouseEvent),this.textarea.addEventListener("mouseup",this.onMouseEvent),this.textarea.addEventListener("mousedown",this.onMouseEvent),this.textarea.addEventListener("click",this.onMouseEvent),this.textarea.addEventListener("drag",this.onMouseEvent),this.updateStyle(),this.updatePosition(),this.textarea.focus(),this.unsubscribe=this.store.subscribe(p=>({rotation:p.rotation,zoom:p.zoom,annotation:p.features[this.annotation]}),this.update,{equalityFn:(p,m)=>p.rotation===m.rotation&&p.zoom===m.zoom&&p.annotation===m.annotation})}getAnnotation(){const e=this.store.getState();e.liveUpdates[this.annotation]||e.startLiveUpdate([this.annotation]);const t=e.getFeature(this.annotation),n=e.liveUpdates[this.annotation];return t&&n?{...t,...n}:t}getPosition(){const e=this.getAnnotation();if(!e)return{x:0,y:0};const t=e.properties.style,n=(t==null?void 0:t.fixedSize)||!1,s=t==null?void 0:t.maxHeight,r=this.store.getState().zoom,o=ke(e),[a,c]=e.geometry.coordinates,h=e.properties.width;let l=e.properties.height;s&&(l=Math.min(l,s));const d=n?1/r:1;return{x:a-h*d/2+o*d,y:c-l*d/2+o*d}}getSize(){var g;const e=this.getAnnotation();if(!e)return{width:0,height:0};const t=Y(e),n=ke(e),s=e.properties.style,r=(s==null?void 0:s.fixedSize)||!1,o=s==null?void 0:s.maxHeight,a=this.store.getState().zoom,c=this.store.getState(),h=D(e)&&(((g=c.options)==null?void 0:g.showSendButton)??!0),l=r?1/a:1;let d=(t.height-n*2)*l;if(o){const f=(o-n*2)*l;d=Math.min(d,f)}const u=h?28*l:0;return{width:(t.width-n*2)*l,height:d+u}}updateStyle(){var v;const e=this.getAnnotation();if(!e)return;const t=D(e)?K:V,{font:n,fontSize:s=t.fontSize,borderRadius:r,color:o,background:a=t.background,padding:c=0,fixedSize:h=t.fixedSize}=e.properties.style||t,l=this.textarea,d=this.layer.element,u=this.store.getState().zoom,g=h?1/u:1,f=parseFloat(s.toString())*g,p=c*g,m=d.style;if(m.display="grid",m.gridTemplateRows="1fr auto",m.boxSizing="border-box",m.background=a||"transparent",m.borderRadius=`${(r||0)*g}px`,m.padding=`${p}px`,m.transformOrigin="center",m.transform=`rotate(${this.store.getState().rotation}rad)`,e.properties.type==="comment"){const S=e.properties.style,E=(S==null?void 0:S.shadow)!==!1;m.boxShadow=E?"0 2px 8px rgba(0, 0, 0, 0.15)":"none"}const x=l.style;if(x.font=`${f} ${n}`,x.fontFamily=n||"sans-serif",x.fontSize=`${f}px`,x.lineHeight=`${f*Pt}px`,x.padding="0",x.border="none",x.outline="none",x.boxSizing="border-box",x.color=o||"black",x.background="transparent",x.flex="1",x.minHeight="0",x.resize="none",this.sendButton){const S=24*g,E=4*g;this.sendButton.style.width=`${S}px`,this.sendButton.style.height=`${S}px`,this.sendButton.style.padding=`${E}px`,this.sendButton.style.transform="";const b=this.sendButton.querySelector(".ogma-send-button-icon");if(b){const A=16*g;b.style.width=`${A}px`,b.style.height=`${A}px`}}if(h){const S=(v=e.properties.style)==null?void 0:v.maxHeight,E=S&&e.properties.height>=S;x.overflowY=E?"auto":"hidden",x.overflowX="hidden"}}updatePosition(){this.layer.setPosition(this.getPosition())}updateContent(){var o,a,c,h;const e=this.getAnnotation();if(!e)return;let t=e.properties.height,n=e.geometry.coordinates;const s=(o=e.properties.style)==null?void 0:o.fixedSize;if(s){const l=this.textarea.style.height;this.textarea.style.height="0px";const d=this.textarea.scrollHeight;this.textarea.style.height=l;const u=ke(e),g=this.store.getState().zoom,f=((a=e.properties.style)==null?void 0:a.padding)||0,p=(d+u*2/g)*g+f*2,m=((c=e.properties.style)==null?void 0:c.minHeight)||50;t=Math.max(m,p);const x=(h=e.properties.style)==null?void 0:h.maxHeight,v=e.properties.height,S=t-v;if(Math.abs(S)>1){const[E,b]=e.geometry.coordinates;if(x&&t>x){const A=x-v;t=x,Math.abs(A)>1&&(n=[E,b+A/2])}else n=[E,b+S/2]}}const r={...e,properties:{...e.properties,content:this.textarea.value,height:t},geometry:{...e.geometry,coordinates:n}};s&&Math.abs(e.properties.height-t)>1&&(this.layer.setSize(this.getSize()),this.layer.setPosition(this.getPosition())),this.store.getState().applyLiveUpdate(this.annotation,r)}updateSendButtonState(){if(!this.sendButton)return;const e=this.textarea.value.trim().length===0;this.sendButton.disabled=e}destroy(){this.store.getState().commitLiveUpdates(new Set([this.annotation])),this.unsubscribe(),this.sendButton&&this.sendButton.removeEventListener("click",this.onSendClick),this.layer.destroy()}}function ke(i){var e;return((e=i.properties.style)==null?void 0:e.strokeWidth)||0}const gn={x:1,y:0},fn={x:0,y:1},Js=[["top",fn,-.5,-.5,.5,-.5],["right",gn,.5,-.5,.5,.5],["bottom",fn,-.5,.5,.5,.5],["left",gn,-.5,-.5,-.5,.5]],Qs={top:[0,1],right:[1,2],bottom:[2,3],left:[3,0]},pn=[[-.5,-.5],[.5,-.5],[.5,.5],[-.5,.5]],ti=[H.nwResize,H.neResize,H.seResize,H.swResize];class Me extends Gt{constructor(t,n,s){super(t,n);y(this,"links");y(this,"textEditor",null);y(this,"justActivated",!1);y(this,"onClick",t=>{const n=this.getAnnotation();if(n){if(D(n)&&n.properties.mode===ot){this.store.getState().updateFeature(n.id,{properties:{...n.properties,mode:Mt}}),this.ogma.view.afterNextFrame(),this.justActivated=!1;return}if(this.store.getState().selectedFeatures.has(n.id)){if(D(n)&&this.justActivated){this.justActivated=!1;return}n&&(L(n)||D(n))&&this.startEditingText()}}});y(this,"stopEditingText",()=>{this.textEditor&&(this.store.getState().drawingFeature===this.annotation&&this.store.setState({drawingFeature:null}),this.textEditor.destroy()),this.textEditor=null,this.store.setState({editingFeature:null})});this.links=s}detectHandle(t,n){var E;const s=this.getAnnotation(),{x:r,y:o}=this.clientToCanvas(t);let{width:a,height:c}=Y(s);const h=5/n,l=J(s),d=this.store.getState();let{revSin:u,revCos:g}=d;(W(s)||D(s))&&(u=0,g=1),this.hoveredHandle=void 0;const f=Ze*(1/n),p=r-l.x,m=o-l.y,x=p*g-m*u,v=p*u+m*g,S=((E=s.properties.style)==null?void 0:E.fixedSize)===!0||D(s);if(S&&(a/=n,c/=n),!S){for(let b=0;b<pn.length;b++){const[A,T]=pn[b],C=a*A,M=c*T;if(x>=C-f-h&&x<=C+f+h&&v>=M-f-h&&v<=M+f+h){this.hoveredHandle={type:"corner",corner:b},this.store.setState({hoveredHandle:b,hoveredFeature:this.annotation}),this.setCursor(this.getCornerCursor(b));return}}for(const[b,A,T,C,M,F]of Js){const j=a*T,U=c*C,R=a*M,at=c*F,lt=vt(A,{x:x-j,y:v-U});if(Math.abs(lt)<h&&x>=j-h&&x<=R+h&&v>=U-h&&v<=at+h){this.hoveredHandle={type:"edge",edge:b,corner:-1},this.store.setState({hoveredHandle:Qs[b][0]+4,hoveredFeature:this.annotation}),this.setCursor(this.getEdgeCursor(b));return}}}if(x>=-a/2-h&&x<=a/2+h&&v>=-c/2-h&&v<=c/2+h){this.store.setState({hoveredHandle:8,hoveredFeature:this.annotation}),this.hoveredHandle={type:"body",corner:-1},this.setCursor(H.grab);return}this.store.setState({hoveredHandle:-1,hoveredFeature:null}),this.setCursor(H.default)}onDrag(t){if(!this.dragStartPoint||!this.hoveredHandle||!this.isActive())return;t.stopPropagation(),t.stopImmediatePropagation();const n=this.getAnnotation(),s=this.clientToCanvas(t),r={x:s.x-this.dragStartPoint.x,y:s.y-this.dragStartPoint.y},o=this.hoveredHandle,a=this.getAnnotation();let c=null;o.type==="corner"?c=this.dragCorner(a,r,o.corner):o.type==="edge"&&o.edge?c=this.dragEdge(a,r,o):o.type==="body"&&(Te(this.store,this.links,n.id,r),this.textEditor&&this.textEditor.update()),c&&(this.store.getState().applyLiveUpdate(n.id,c),this.links.updateLinkedArrowsDuringDrag(n.id,r),this.textEditor&&this.textEditor.update()),this.dispatchEvent(new CustomEvent(Tt,{detail:{point:s,annotation:this.annotation,handle:o}}))}dragCorner(t,n,s){const r=s===0||s===1,o=s===0||s===3,a=s===1||s===2,c=s===2||s===3,{width:h,height:l}=Y(t),d=this.store.getState();let{revSin:u,revCos:g}=d;(W(t)||D(t))&&(u=0,g=1);const f=n.x*g-n.y*u,p=n.x*u+n.y*g;let m=0,x=0;o?m=-f:a&&(m=f),r?x=-p:c&&(x=p);const v=Math.max(0,h+m),S=Math.max(0,l+x),E=J(t),b=E.x+n.x/2,A=E.y+n.y/2;return{...t,properties:{...t.properties,width:v,height:S},geometry:{type:t.geometry.type,coordinates:[b,A]}}}dragEdge(t,n,s){const{width:r,height:o}=Y(t),a=J(t),c=this.store.getState();let{revSin:h,revCos:l}=c;(W(t)||D(t))&&(h=0,l=1);const d=n.x*l-n.y*h,u=n.x*h+n.y*l;let g=r,f=o,p=0,m=0;switch(s.edge){case"top":f=Math.max(0,o-u),m=u/2;break;case"bottom":f=Math.max(0,o+u),m=u/2;break;case"left":g=Math.max(0,r-d),p=d/2;break;case"right":g=Math.max(0,r+d),p=d/2;break}const x=p*l+m*h,v=-p*h+m*l;return{...t,properties:{...t.properties,width:g,height:f},geometry:{type:t.geometry.type,coordinates:[a.x+x,a.y+v]}}}onDragStart(t){return super.onDragStart(t)?(this.stopEditingText(),this.store.getState().startLiveUpdate([this.annotation]),!0):!1}startEditingText(){this.textEditor===null&&(this.store.setState({editingFeature:this.annotation}),this.textEditor=new Ks(this.ogma,this.store,this.annotation,this.stopEditingText))}onDragEnd(t){var a,c;if(!super.onDragEnd(t))return!1;this.store.getState().drawingFeature===this.annotation&&this.store.setState({drawingFeature:null});const s=this.clientToCanvas(t),r=s.x-(((a=this.dragStartPoint)==null?void 0:a.x)||0),o=s.y-(((c=this.dragStartPoint)==null?void 0:c.y)||0);return Math.abs(r)<1&&Math.abs(o)<1?(this.clearDragState(),this.onClick({clientX:t.clientX,clientY:t.clientY})):this.commitChange(),this.hoveredHandle=void 0,this.dragStartPoint=void 0,this.dragging=!1,!0}getCornerCursor(t){return ti[t]}getEdgeCursor(t){switch(t){case"top":case"bottom":return H.nsResize;case"left":case"right":return H.ewResize;default:return H.default}}stopEditing(){super.stopEditing(),this.commitChange(),this.stopEditingText()}startDrawing(t,n,s){this.annotation=t,this.hoveredHandle={type:"corner",corner:2},this.store.setState({hoveredHandle:2,hoveredFeature:this.annotation}),this.dragging=!0;const r=this.ogma.view.graphToScreenCoordinates({x:n,y:s});this.dragStartPoint=r,this.disablePanning(),this.onDragStart({clientX:r.x,clientY:r.y})}setAnnotation(t){t&&D(t)&&(this.justActivated=!0),super.setAnnotation(t),t&&D(t)&&t.properties.mode===ot&&this.store.getState().updateFeature(t.id,{properties:{...t.properties,mode:Mt}})}}class ei extends EventTarget{constructor(t,n,s,r,o){super();y(this,"handlers",new Map);y(this,"activeHandler");y(this,"interaction");y(this,"ogma");y(this,"snapping");y(this,"store");this.ogma=t,this.store=n,this.snapping=s,this.interaction=o,this.handlers.set("box",new Me(this.ogma,this.store,r)),this.handlers.set("text",new Me(this.ogma,this.store,r)),this.handlers.set("comment",new Me(this.ogma,this.store,r)),this.handlers.set("arrow",new Vs(this.ogma,this.store,this.snapping,r)),this.handlers.set("polygon",new Zs(this.ogma,this.store,r)),this.handlers.forEach(a=>{a.addEventListener("dragstart",(c=>{this.dispatchEvent(new CustomEvent(xt,{detail:{...c.detail}})),this.store.setState({isDragging:!0}),this.interaction.setMode("edit")})),a.addEventListener("dragend",(c=>{this.dispatchEvent(new CustomEvent(kt,{detail:{...c.detail}})),this.store.setState({isDragging:!1}),this.interaction.suppressClicksTemporarily()}))}),this.store.subscribe(a=>a.selectedFeatures,(a,c)=>{const h=Array.from(a.keys()).filter(d=>!c.has(d)),l=Array.from(c.keys()).filter(d=>!a.has(d));!h.length&&!l.length||(l.forEach(d=>this.stopEditingFeature(d)),h.forEach(d=>this.editFeature(d)))})}stopEditingFeature(t){const n=this.store.getState().features[t];if(!n)return;const s=n.properties.type,r=this.handlers.get(s);r&&r.stopEditing()}editFeature(t){var s;const n=this.store.getState().features[t];n&&(this.setActiveHandler(n.properties.type),(s=this.activeHandler)==null||s.setAnnotation(n))}getSnapping(){return this.snapping}getArrowHandler(){return this.handlers.get("arrow")}getActiveHandler(){return this.activeHandler}setActiveHandler(t){const n=this.handlers.get(t);if(!n)throw new Error(`Handler for type ${t} not found`);return this.activeHandler=n,this}destroy(){}}const ni=["x","y","radius"],si=1;class ii{constructor(e,t,n,s){y(this,"links",new Map);y(this,"nodeToLink",new Map);y(this,"edgeToLink",new Map);y(this,"annotationToLink",new Map);y(this,"linksByArrowId",new Map);y(this,"store");y(this,"ogma");y(this,"snapping");y(this,"updatedItems",new Set);y(this,"onLinkCreated");y(this,"commitTimeout");y(this,"onSetMultipleAttributes",({elements:e,updatedAttributes:t})=>{const n=new Set(t);!e.isNode||!n.has("x")&&!n.has("y")&&!n.has("radius")||this.requestUpdateFromNodePositions(e.toList())});y(this,"refresh",()=>{const e=this.store.getState(),t=new Map;this.annotationToLink.forEach((n,s)=>{var a;const r=e.getFeature(s);if(!r)return;(L(r)&&((a=r.properties.style)==null?void 0:a.fixedSize)||D(r))&&n.forEach(c=>{const h=this.links.get(c);if(!h)return;const l=h.arrow;t.set(l,this.linksByArrowId.get(l))})}),t.size>0&&this.update(t)});y(this,"throttledRefresh",fe(()=>this.refresh(),20));y(this,"onAddRemoveEdges",e=>{const t=e.edges;if(!t.size||!this.edgeToLink.size)return;const n=new Map,s=new Map;t.getParallelEdges().getId().forEach(r=>{const o=this.edgeToLink.get(r);o&&o.forEach(a=>{const c=this.links.get(a);if(!c)return;const h=c.arrow;n.set(h,this.linksByArrowId.get(h)),s.set(h,this.linksByArrowId.get(h))})}),s.size!==0&&this.update(s)});y(this,"commit",()=>{this.store.getState().batchUpdate(this.commitLiveUpdates),this.updatedItems.clear()});y(this,"commitLiveUpdates",()=>{this.store.getState().commitLiveUpdates(this.updatedItems)});y(this,"onAddArrow",(e,t)=>{const n=this.store.getState(),s=new Set(Object.keys(t)),r=Object.keys(e).filter(a=>!s.has(a)),o=Object.keys(t).filter(a=>!e[a]);r.forEach(a=>{var l,d;const c=n.getFeature(a);if(!c||!tt(c))return;const h=c;if((l=h.properties.link)!=null&&l.start){const u=h.properties.link.start;this.add(h,O,u.id,u.type,u.magnet)}if((d=h.properties.link)!=null&&d.end){const u=h.properties.link.end;this.add(h,rt,u.id,u.type,u.magnet)}}),o.forEach(a=>{const c=t[a];if(tt(c)){const h=c;this.remove(h,O),this.remove(h,rt)}else{const h=this.annotationToLink.get(a);if(!h)return;for(const l of h){const d=this.links.get(l);if(!d)continue;const u=n.getFeature(d.arrow);u?this.remove(u,d.side):this.remove(d.arrow,d.side)}}})});this.ogma=e,this.store=n,this.snapping=t,this.onLinkCreated=s,this.store.subscribe(r=>r.features,this.onAddArrow),this.store.subscribe(r=>({zoom:r.zoom,rotation:r.rotation}),this.throttledRefresh,{equalityFn:(r,o)=>r.zoom===o.zoom&&r.rotation===o.rotation}),this.ogma.events.on("setMultipleAttributes",this.onSetMultipleAttributes).on(["addEdges","removeEdges"],this.onAddRemoveEdges).on("viewChanged",this.refresh)}updateLinkedArrowsDuringDrag(e,t,n){const s=this.store.getState();if(!s.getFeature(e))return;const o=this.annotationToLink.get(e);if(o)for(const a of o){const c=this.links.get(a);if(!c)continue;const h=s.getFeature(c.arrow),l=Dt(h,c.side),d=pt(l,t),u={...h.geometry,coordinates:h.geometry.coordinates.map((g,f)=>c.side===O&&f===0||c.side===rt&&f===1?[d.x,d.y]:[...g])};n?n[h.id]={geometry:u}:s.applyLiveUpdate(h.id,{geometry:u}),this.updatedItems.add(h.id)}}snapLinkedArrowsDuringDrag(e,t){const n=this.store.getState();let s=n.getFeature(e);if(!s)return;const r=n.liveUpdates[e];s=r?{...s,...r}:s;const o=this.annotationToLink.get(e);if(o)for(const a of o){const c=this.links.get(a);if(!c)continue;let h=n.getFeature(c.arrow);const l=n.liveUpdates[h.id];h=l?{...h,...l}:h;const d=h.geometry.coordinates[c.side===O?0:1],u={x:d[0],y:d[1]};let g;if(L(s)||D(s)||W(s)?g=this.snapping.snapToText(u,[s]):it(s)&&(g=this.snapping.snapToPolygon(u,[s])),!g)continue;const f=g.point,p={...h.geometry,coordinates:h.geometry.coordinates.map((m,x)=>c.side===O&&x===0||c.side===rt&&x===1?[f.x,f.y]:[...m])};t?t[h.id]={geometry:p}:n.applyLiveUpdate(h.id,{geometry:p})}}add(e,t,n,s,r){const o=ut(),a=e.id;let c=r;if(s===P.POLYGON){const u=this.store.getState().getFeature(n);if(u&&it(u)){const g=le(u),f=g[2]-g[0],p=g[3]-g[1],m=r.x-g[0],x=r.y-g[1],v=f>0?m/f:.5,S=p>0?x/p:.5;c={x:v,y:S}}}const h={id:o,arrow:a,target:n,targetType:s,magnet:c,side:t};if(s===P.NODE&&!this.ogma.getNode(n)||s===P.EDGE&&!this.ogma.getEdge(n))return;this.remove(e,t),this.links.set(o,h);const l=s===P.NODE?this.nodeToLink:s===P.EDGE?this.edgeToLink:this.annotationToLink;return l.has(n)||l.set(n,new Set),l.get(n).add(o),this.linksByArrowId.has(a)||this.linksByArrowId.set(a,{}),this.linksByArrowId.get(a)[t]=o,e.properties.link=e.properties.link||{},e.properties.link[t]={id:n,side:t,type:s,magnet:c},this.onLinkCreated&&this.onLinkCreated(e,h),this}remove(e,t){var o,a,c,h,l;const n=typeof e=="object"?e.id:e,s=(o=this.linksByArrowId.get(n))==null?void 0:o[t];if(typeof e=="object"&&((a=e.properties.link)==null||delete a[t]),!s)return this;const r=this.links.get(s);return r?(this.links.delete(s),(c=this.nodeToLink.get(r.target))==null||c.delete(s),(h=this.edgeToLink.get(r.target))==null||h.delete(s),(l=this.annotationToLink.get(r.target))==null||l.delete(s),this.linksByArrowId.has(n)&&(this.linksByArrowId.get(n)[t]=void 0),this):this}requestUpdateFromNodePositions(e){setTimeout(()=>this.updateFromNodePositions(e),1)}updateFromNodePositions(e){const t=e.getId(),n=new Map;t.forEach(h=>{const l=this.nodeToLink.get(h);l&&l.forEach(d=>{const u=this.links.get(d);if(!u)return;const g=u.arrow;n.set(g,this.linksByArrowId.get(g))})});const s=new Map;e.getAdjacentEdges().getId().forEach(h=>{const l=this.edgeToLink.get(h);l&&l.forEach(d=>{const u=this.links.get(d);if(!u)return;const g=u.arrow;n.set(g,this.linksByArrowId.get(g)),s.set(g,this.linksByArrowId.get(g))})});const o=e.getAttributes(ni),a=this.store.getState(),c={};for(let h=0;h<t.length;h++){const l=t[h],d=this.nodeToLink.get(l);if(d)for(const u of d){const g=this.links.get(u);if(!g)continue;const f=g.arrow,p=this.store.getState().getFeature(f),m=p.geometry.coordinates.slice(),x=Dt(p,rt),v=Dt(p,O),S=o[h],E=this._getNodeSnapPoint(S,ct(X(x,v),-1),this._isLinkedToCenter(g));m[g.side===O?0:1]=E,c[f]={...p,geometry:{coordinates:m}},this.updatedItems.add(f),g.magnet={x:E[0]-S.x,y:E[1]-S.y},ft(c[f])}}if(a.applyLiveUpdates(c),s.size>0){this.update(s);return}this.requestCommit()}update(e=this.linksByArrowId){const t=this.store.getState(),n=Array.from(this.nodeToLink.keys()),s=new Map;n.forEach((c,h)=>s.set(c,h));const o=this.ogma.getNodes(n).getAttributes(["x","y","radius"]),a={};e.forEach((c,h)=>{const l=this.links.get(c.start),d=this.links.get(c.end),u=t.getFeature(h);let g=u.geometry.coordinates[0],f=u.geometry.coordinates[1];const p=l?l.targetType===P.NODE?o[s.get(l.target)]:l.targetType===P.EDGE?this._getEdgeSnapPoint(l.target,l.magnet.x,!0):this._getAnnotationCenter(t.getFeature(l.target)):{x:g[0],y:g[1]},m=d?d.targetType===P.NODE?o[s.get(d.target)]:d.targetType===P.EDGE?this._getEdgeSnapPoint(d.target,d.magnet.x,!0):this._getAnnotationCenter(t.getFeature(d.target)):{x:f[0],y:f[1]},x=X(m,p);if(l)if(l.targetType===P.NODE)g=this._getNodeSnapPoint(p,x,this._isLinkedToCenter(l));else if(l.targetType===P.EDGE)g=this._getEdgeSnapPoint(l.target,l.magnet.x);else{const v=t.getFeature(l.target);g=this._getAnnotationSnapPoint(v,m,l,t.zoom)}if(d)if(d.targetType===P.NODE)f=this._getNodeSnapPoint(m,ct(x,-1),this._isLinkedToCenter(d));else if(d.targetType===P.EDGE)f=this._getEdgeSnapPoint(d.target,d.magnet.x);else{const v=t.getFeature(d.target);f=this._getAnnotationSnapPoint(v,p,d,t.zoom)}a[u.id]={properties:u.properties,geometry:{coordinates:[g,f]}},ft(a[u.id]),this.updatedItems.add(u.id)}),t.applyLiveUpdates(a),this.requestCommit()}requestCommit(){clearTimeout(this.commitTimeout),this.commitTimeout=setTimeout(this.commit,si)}_isLinkedToCenter(e){return e.magnet.x===0&&e.magnet.y===0}_getAnnotationCenter(e){return it(e)?ce(e):J(e)}_getAnnotationSnapPoint(e,t,n,s){if(it(e)){const r=le(e),o=r[0]+n.magnet.x*(r[2]-r[0]),a=r[1]+n.magnet.y*(r[3]-r[1]);return[o,a]}return this._getBoxSnapPoint(e,t,n,s)}_getBoxSnapPoint(e,t,n,s){var d;const r=J(e);let{width:o,height:a}=Y(e);(L(e)&&((d=e.properties.style)==null?void 0:d.fixedSize)||D(e))&&(o/=s,a/=s);let h=n.magnet.x*o,l=n.magnet.y*a;if(L(e)&&!W(e)){const{sin:u,cos:g}=this.store.getState(),f=h*g-l*u,p=h*u+l*g;h=f,l=p}else if(D(e)){const g={...e.properties.style}.iconSize||32,f=g/16*.5;h=n.magnet.x*g*f,l=n.magnet.y*g*f}return[r.x+h,r.y+l]}_getNodeSnapPoint(e,t,n){if(n)return[e.x,e.y];const s=Math.sqrt(t.x*t.x+t.y*t.y),r=ct(t,1/s),o=s<Number(e.radius)/2?{x:e.x,y:e.y}:pt({x:e.x,y:e.y},ct(r,-Number(e.radius)));return[o.x,o.y]}_getEdgeSnapPoint(e,t,n=!1){const s=this.ogma.getEdge(e);if(!s)return[0,0];const r=s.getAttribute("curvature"),a=s.getExtremities().getPosition(),c=a[0],h=a[1];if(r===0||r===void 0){const u=[c.x+t*(h.x-c.x),c.y+t*(h.y-c.y)];return n?{x:u[0],y:u[1]}:u}const l=q.geometry.getQuadraticCurveControlPoint(c.x,c.y,h.x,h.y,r),d=q.geometry.getPointOnQuadraticCurve(t,c.x,c.y,h.x,h.y,l.x,l.y);return n?{x:d.x,y:d.y}:[d.x,d.y]}destroy(){this.ogma.events.off(this.onSetMultipleAttributes)}}function ri(i,e,t,n,s=20){let r=0,o=1/0,a=null;for(let c=0;c<=s;c++){const h=c/s,l=q.geometry.getPointOnQuadraticCurve(h,e.x,e.y,t.x,t.y,n.x,n.y),d=Q(X(l,i));d<o&&(o=d,a=l,r=h)}return a?{t:r,point:a,distance:o}:null}function oi(i,e,t){const n=t.x-e.x,s=t.y-e.y,r=n*n+s*s;if(r===0)return{t:0,point:{x:e.x,y:e.y},distance:Q(X(e,i))};const o=Math.max(0,Math.min(1,((i.x-e.x)*n+(i.y-e.y)*s)/r)),a={x:e.x+o*n,y:e.y+o*s};return{t:o,point:a,distance:Q(X(a,i))}}function ai(i,e,t){let n=null,s=t;const r=e.getAttribute("curvature"),a=e.getExtremities().getPosition();for(let c=0;c<e.size;c++){const h=e.get(c),l=r[c],d=a[2*c],u=a[2*c+1];let g;if(l===0||l===void 0)g=oi(i,d,u);else{const f=q.geometry.getQuadraticCurveControlPoint(d.x,d.y,u.x,u.y,l);g=ri(i,d,u,f)}g&&g.distance<s&&(s=g.distance,n={point:g.point,id:h.getId(),magnet:{x:g.t,y:0},type:P.EDGE})}return n}function li(i,e,t){const n=e.getAttributes(["x","y","radius"]);for(let s=0;s<n.length;s++){const r=n[s],o=X({x:r.x,y:r.y},i),a=Q(o);if(a>=Number(r.radius)+Number(t))continue;const c=ct(o,1/a),h=a<Number(r.radius)/2,l=h?{x:r.x,y:r.y}:pt({x:r.x,y:r.y},ct(c,-Number(r.radius))),d=h?{x:0,y:0}:c;return{point:l,id:e.get(s).getId(),magnet:d,type:P.NODE}}return null}function ci(i,e){let t=!1;for(let n=0,s=e.length-1;n<e.length;s=n++){const r=e[n][0],o=e[n][1],a=e[s][0],c=e[s][1];o>i.y!=c>i.y&&i.x<(a-r)*(i.y-o)/(c-o)+r&&(t=!t)}return t}function hi(i,e,t,n,s){const r=i*i,o=r*i,a=1-i,c=a*a,h=c*a;return{x:h*e.x+3*c*i*t.x+3*a*r*n.x+o*s.x,y:h*e.y+3*c*i*t.y+3*a*r*n.y+o*s.y}}function di(i,e,t,n,s){let r=1/0,o=null;const a=20;for(let c=0;c<=a;c++){const h=c/a,l=hi(h,e,t,n,s),d=Q(X(l,i));d<r&&(r=d,o=l)}return o?{point:o,distance:r}:null}function ui(i,e,t){const n=e.geometry.coordinates[0];for(let s=0;s<n.length-1;s++){const[r,o]=n[s];if(Q(X({x:r,y:o},i))<t)return{point:{x:r,y:o},magnet:{x:r,y:o},type:P.POLYGON,id:e.id}}for(let s=0;s<n.length-1;s++){const[r,o]=n[s],[a,c]=n[s+1],h={x:r,y:o},d=X({x:a,y:c},h),u=Q(d),g={x:d.x/u,y:d.y/u},f={x:-g.y,y:g.x},p=X(i,h),m=Math.abs(vt(f,p)),x=vt(g,p);if(x>=0&&x<=u&&m<t){const v={x:h.x+g.x*x,y:h.y+g.y*x};return{point:v,magnet:v,type:P.POLYGON,id:e.id}}}return null}function gi(i,e,t){for(const s of e){const o=s.geometry.coordinates[0].slice(0,-1);if(o.length<3)return ui(i,s,t);for(let l=0;l<o.length;l++){const[d,u]=o[l];if(Q(X({x:d,y:u},i))<t)return{point:{x:d,y:u},magnet:{x:d,y:u},type:P.POLYGON,id:s.id}}const a=ci(i,o);let c=null,h=a?1/0:t;for(let l=0;l<o.length;l++){const d=o[(l-1+o.length)%o.length],u=o[l],g=o[(l+1)%o.length],f=o[(l+2)%o.length],p=u[0]+(g[0]-d[0])/6*.5,m=u[1]+(g[1]-d[1])/6*.5,x=g[0]-(f[0]-u[0])/6*.5,v=g[1]-(f[1]-u[1])/6*.5,S=di(i,{x:u[0],y:u[1]},{x:p,y:m},{x,y:v},{x:g[0],y:g[1]});if(S){const E=S.distance;if(a)E<h&&(h=E,c=S.point);else if(E<t)return{point:S.point,magnet:S.point,type:P.POLYGON,id:s.id}}}if(a&&c)return{point:c,magnet:c,type:P.POLYGON,id:s.id}}return null}const mn=[{x:-.5,y:-.5},{x:0,y:-.5},{x:.5,y:-.5},{x:-.5,y:0},{x:.5,y:0},{x:-.5,y:.5},{x:0,y:.5},{x:.5,y:.5},{x:0,y:0}],fi=[[-.5,-.5,.5,-.5],[.5,-.5,.5,.5],[.5,.5,-.5,.5],[-.5,.5,-.5,-.5]];function yn(i,e,t,n,s,r,o,a,c){const h=i*s,l=e*r;return c?{x:t+(h*o-l*a),y:n+(h*a+l*o)}:{x:t+h,y:n+l}}function pi(i,e,t,n,s){var l;const r=J(e);let{width:o,height:a}=Y(e);const{sin:c,cos:h}=s.getState();(l=e.properties.style)!=null&&l.fixedSize&&(o/=t,a/=t);for(const d of mn){let u=d.x*o,g=d.y*a;if(!W(e)){const m=u*h-g*c,x=u*c+g*h;u=m,g=x}const f={x:r.x+u,y:r.y+g};if(!(Q(X(f,i))>=n))return{point:{x:f.x,y:f.y},type:P.TEXT,magnet:d,id:e.id}}return null}function mi(i,e,t,n,s){var f;const{x:r,y:o}=J(e);let{width:a,height:c}=Y(e);(f=e.properties.style)!=null&&f.fixedSize&&(a/=t,c/=t);const{sin:h,cos:l}=s.getState(),d=!W(e);let u=null,g=1/0;for(let p=0;p<4;p++){const[m,x,v,S]=fi[p],E=yn(m,x,r,o,a,c,l,h,d),b=yn(v,S,r,o,a,c,l,h,d),A=X(b,E),T=Q(A),C={x:A.x/T,y:A.y/T},M={x:-C.y,y:C.x},F=X(i,E),j=vt(M,F),U=vt(C,F);if(U>=0&&U<=T){const R=Math.abs(j);if(R<n||R<g){const at=U/T,lt={point:{x:E.x+C.x*U,y:E.y+C.y*U},magnet:{x:m+(v-m)*at,y:x+(S-x)*at},type:P.TEXT,id:e.id};if(R<n)return lt;R<g&&(g=R,u=lt)}}}return u&&g<Math.max(a,c)?u:null}function yi(i,e,t,n,s){for(const r of e){const o=pi(i,r,t,n,s)||mi(i,r,t,n,s);if(o)return o}return null}class wi extends EventTarget{constructor(t,n,s,r){super();y(this,"ogma");y(this,"options");y(this,"spatialIndex");y(this,"hoveredNode",null);y(this,"store");this.ogma=t,this.options=n,this.spatialIndex=s,this.store=r}snap(t){const n=this.findMagnet(t);return n||null}findMagnet(t){const n=this.options.detectMargin,s={minX:t.x-n,minY:t.y-n,maxX:t.x+n,maxY:t.y+n},r=this.spatialIndex.search(s),o=r.filter(f=>L(f)||W(f)),a=r.filter(f=>it(f)),{nodes:c,edges:h}=this.ogma.view.getElementsInside(s.minX,s.minY,s.maxX,s.maxY),l=this.snapToNodes(t,c);if(l)return l;const d=this.snapToEdges(t,h);if(d)return d;const u=this.snapToText(t,o);if(u)return u;const g=this.snapToPolygon(t,a);return g||null}snapToText(t,n){const{zoom:s}=this.store.getState();return yi(t,n,s,this.options.magnetRadius,this.store)}snapToPolygon(t,n){return gi(t,n,this.options.magnetRadius)}snapToNodes(t,n){return li(t,n,this.options.detectMargin)}snapToEdges(t,n){return ai(t,n,this.options.magnetRadius)}getHoveredNode(){return this.hoveredNode}clearHoveredNode(){var t;(t=this.hoveredNode)==null||t.setSelected(!1),this.hoveredNode=null}getMagnets(){return mn}}class xi extends EventTarget{constructor(t,n,s){super();y(this,"query",{minX:1/0,minY:1/0,maxX:1/0,maxY:1/0});y(this,"suppressClickUntil",0);y(this,"mouseDownState",null);y(this,"DRAG_THRESHOLD",3);y(this,"onMouseMove",t=>{const n=Lt(t,this.ogma.getContainer()),s=this.store.getState();if(this.mouseDownState&&!s.isDragging){const l=t.clientX-this.mouseDownState.screenX,d=t.clientY-this.mouseDownState.screenY;Math.sqrt(l*l+d*d)>this.DRAG_THRESHOLD&&(this.mouseDownState.hasMoved=!0)}if(s.isDragging)return;const{x:r,y:o}=this.ogma.view.screenToGraphCoordinates(n),a=this.detect(r,o),c=(a==null?void 0:a.id)??null,h=s.hoveredFeature;c!==h&&s.setHoveredFeature(c),this.setCursor(c===null?H.default:H.pointer)});y(this,"onMouseClick",()=>{Date.now()<this.suppressClickUntil});y(this,"onMouseDown",t=>{if(Date.now()<this.suppressClickUntil)return;const n=Lt(t,this.ogma.getContainer()),{x:s,y:r}=this.ogma.view.screenToGraphCoordinates(n),o=this.detect(s,r);this.mouseDownState={annotation:o,screenX:t.clientX,screenY:t.clientY,hasMoved:!1};const a=this.store.getState();this.store.setState({mousePressed:!0,mousePressPoint:{x:s,y:r}}),o&&!a.selectedFeatures.has(o.id)&&(t.ctrlKey||t.metaKey?a.toggleSelection(o.id):a.setSelectedFeatures([o.id]))});y(this,"onMouseUp",t=>{const n=this.store.getState();if(this.store.setState({mousePressed:!1,mousePressPoint:null}),this.mouseDownState&&!this.mouseDownState.hasMoved&&!n.isDragging){const s=this.mouseDownState.annotation;s?t.ctrlKey||t.metaKey?n.toggleSelection(s.id):n.selectedFeatures.has(s.id)||n.setSelectedFeatures([s.id]):!t.ctrlKey&&!t.metaKey&&n.clearSelection(),this.dispatchEvent(new CustomEvent(Ot,{detail:{id:(s==null?void 0:s.id)??null,position:{x:t.clientX,y:t.clientY}}}))}this.mouseDownState=null});y(this,"onWheel",t=>{var d;if(t.target instanceof HTMLTextAreaElement)return;const n=Lt(t,this.ogma.getContainer()),{x:s,y:r}=this.ogma.view.screenToGraphCoordinates(n),o=this.detect(s,r);if(!o||!D(o))return;const a=(d=o.properties.style)==null?void 0:d.maxHeight,c=o.properties.height;if(!a||c<=a)return;const h=this.ogma.getContainer(),l=h==null?void 0:h.querySelector(`[data-annotation="${o.id}"] .comment-box foreignObject div`);l&&(t.stopPropagation(),t.preventDefault(),l.scrollTop+=t.deltaY)});this.ogma=t,this.store=n,this.index=s;const r=this.ogma.getContainer();r&&(r.addEventListener("mousemove",this.onMouseMove,{passive:!0,capture:!0}),r.addEventListener("click",this.onMouseClick,{passive:!0,capture:!0}),r.addEventListener("mousedown",this.onMouseDown,{passive:!0,capture:!1}),r.addEventListener("mouseup",this.onMouseUp,{passive:!0,capture:!0}),r.addEventListener("wheel",this.onWheel,{passive:!1,capture:!0}))}detect(t,n,s){let r=null;const o=this.store.getState(),a=s??o.options.detectMargin;this.query.minX=t-a,this.query.minY=n-a,this.query.maxX=t+a,this.query.maxY=n+a;const c=this.index.query(this.query);if(c.length===0)return null;for(const h of c){const l=o.getFeature(h.id);if(tt(l)){if(me(l,{x:t,y:n},a)){r=l;break}}else if(W(l)){if(on(l,{x:t,y:n},0,1,a)){r=l;break}}else if(it(l)){if(ve(l,{x:t,y:n},a)){r=l;break}}else if(D(l)){if(an(l,{x:t,y:n},a,o.sin,o.cos,o.zoom)){r=l;break}}else if(L(l)&&xe(l,{x:t,y:n},a,o.revSin,o.revCos,o.zoom)){r=l;break}}return r}setCursor(t){var s;const n=(s=this.ogma.getContainer())==null?void 0:s.firstChild;n&&(n.style.cursor=t)}setMode(t){var n;t==="default"?this.setCursor(((n=this.ogma.getOptions().cursor)==null?void 0:n.default)||"default"):t==="add"||t==="link"?this.setCursor(H.crosshair):t==="rotate"&&this.setCursor(H.alias)}suppressClicksTemporarily(t=100){this.suppressClickUntil=Date.now()+t}destroy(){const t=this.ogma.getContainer();t&&(t.removeEventListener("mousemove",this.onMouseMove),t.removeEventListener("click",this.onMouseClick))}}function wn(i,e,t=0,n=i.length-1,s=bi){for(;n>t;){if(n-t>600){const c=n-t+1,h=e-t+1,l=Math.log(c),d=.5*Math.exp(2*l/3),u=.5*Math.sqrt(l*d*(c-d)/c)*(h-c/2<0?-1:1),g=Math.max(t,Math.floor(e-h*d/c+u)),f=Math.min(n,Math.floor(e+(c-h)*d/c+u));wn(i,e,g,f,s)}const r=i[e];let o=t,a=n;for(Ht(i,t,e),s(i[n],r)>0&&Ht(i,t,n);o<a;){for(Ht(i,o,a),o++,a--;s(i[o],r)<0;)o++;for(;s(i[a],r)>0;)a--}s(i[t],r)===0?Ht(i,t,a):(a++,Ht(i,a,n)),a<=e&&(t=a+1),e<=a&&(n=a-1)}}function Ht(i,e,t){const n=i[e];i[e]=i[t],i[t]=n}function bi(i,e){return i<e?-1:i>e?1:0}class vi{constructor(e=9){this._maxEntries=Math.max(4,e),this._minEntries=Math.max(2,Math.ceil(this._maxEntries*.4)),this.clear()}all(){return this._all(this.data,[])}search(e){let t=this.data;const n=[];if(!Kt(e,t))return n;const s=this.toBBox,r=[];for(;t;){for(let o=0;o<t.children.length;o++){const a=t.children[o],c=t.leaf?s(a):a;Kt(e,c)&&(t.leaf?n.push(a):De(e,c)?this._all(a,n):r.push(a))}t=r.pop()}return n}collides(e){let t=this.data;if(!Kt(e,t))return!1;const n=[];for(;t;){for(let s=0;s<t.children.length;s++){const r=t.children[s],o=t.leaf?this.toBBox(r):r;if(Kt(e,o)){if(t.leaf||De(e,o))return!0;n.push(r)}}t=n.pop()}return!1}load(e){if(!(e&&e.length))return this;if(e.length<this._minEntries){for(let n=0;n<e.length;n++)this.insert(e[n]);return this}let t=this._build(e.slice(),0,e.length-1,0);if(!this.data.children.length)this.data=t;else if(this.data.height===t.height)this._splitRoot(this.data,t);else{if(this.data.height<t.height){const n=this.data;this.data=t,t=n}this._insert(t,this.data.height-t.height-1,!0)}return this}insert(e){return e&&this._insert(e,this.data.height-1),this}clear(){return this.data=At([]),this}remove(e,t){if(!e)return this;let n=this.data;const s=this.toBBox(e),r=[],o=[];let a,c,h;for(;n||r.length;){if(n||(n=r.pop(),c=r[r.length-1],a=o.pop(),h=!0),n.leaf){const l=Si(e,n.children,t);if(l!==-1)return n.children.splice(l,1),r.push(n),this._condense(r),this}!h&&!n.leaf&&De(n,s)?(r.push(n),o.push(a),a=0,c=n,n=n.children[0]):c?(a++,n=c.children[a],h=!1):n=null}return this}toBBox(e){return e}compareMinX(e,t){return e.minX-t.minX}compareMinY(e,t){return e.minY-t.minY}toJSON(){return this.data}fromJSON(e){return this.data=e,this}_all(e,t){const n=[];for(;e;)e.leaf?t.push(...e.children):n.push(...e.children),e=n.pop();return t}_build(e,t,n,s){const r=n-t+1;let o=this._maxEntries,a;if(r<=o)return a=At(e.slice(t,n+1)),Et(a,this.toBBox),a;s||(s=Math.ceil(Math.log(r)/Math.log(o)),o=Math.ceil(r/Math.pow(o,s-1))),a=At([]),a.leaf=!1,a.height=s;const c=Math.ceil(r/o),h=c*Math.ceil(Math.sqrt(o));xn(e,t,n,h,this.compareMinX);for(let l=t;l<=n;l+=h){const d=Math.min(l+h-1,n);xn(e,l,d,c,this.compareMinY);for(let u=l;u<=d;u+=c){const g=Math.min(u+c-1,d);a.children.push(this._build(e,u,g,s-1))}}return Et(a,this.toBBox),a}_chooseSubtree(e,t,n,s){for(;s.push(t),!(t.leaf||s.length-1===n);){let r=1/0,o=1/0,a;for(let c=0;c<t.children.length;c++){const h=t.children[c],l=Pe(h),d=Ci(e,h)-l;d<o?(o=d,r=l<r?l:r,a=h):d===o&&l<r&&(r=l,a=h)}t=a||t.children[0]}return t}_insert(e,t,n){const s=n?e:this.toBBox(e),r=[],o=this._chooseSubtree(s,this.data,t,r);for(o.children.push(e),Bt(o,s);t>=0&&r[t].children.length>this._maxEntries;)this._split(r,t),t--;this._adjustParentBBoxes(s,r,t)}_split(e,t){const n=e[t],s=n.children.length,r=this._minEntries;this._chooseSplitAxis(n,r,s);const o=this._chooseSplitIndex(n,r,s),a=At(n.children.splice(o,n.children.length-o));a.height=n.height,a.leaf=n.leaf,Et(n,this.toBBox),Et(a,this.toBBox),t?e[t-1].children.push(a):this._splitRoot(n,a)}_splitRoot(e,t){this.data=At([e,t]),this.data.height=e.height+1,this.data.leaf=!1,Et(this.data,this.toBBox)}_chooseSplitIndex(e,t,n){let s,r=1/0,o=1/0;for(let a=t;a<=n-t;a++){const c=It(e,0,a,this.toBBox),h=It(e,a,n,this.toBBox),l=Ti(c,h),d=Pe(c)+Pe(h);l<r?(r=l,s=a,o=d<o?d:o):l===r&&d<o&&(o=d,s=a)}return s||n-t}_chooseSplitAxis(e,t,n){const s=e.leaf?this.compareMinX:Ei,r=e.leaf?this.compareMinY:Ai,o=this._allDistMargin(e,t,n,s),a=this._allDistMargin(e,t,n,r);o<a&&e.children.sort(s)}_allDistMargin(e,t,n,s){e.children.sort(s);const r=this.toBBox,o=It(e,0,t,r),a=It(e,n-t,n,r);let c=Zt(o)+Zt(a);for(let h=t;h<n-t;h++){const l=e.children[h];Bt(o,e.leaf?r(l):l),c+=Zt(o)}for(let h=n-t-1;h>=t;h--){const l=e.children[h];Bt(a,e.leaf?r(l):l),c+=Zt(a)}return c}_adjustParentBBoxes(e,t,n){for(let s=n;s>=0;s--)Bt(t[s],e)}_condense(e){for(let t=e.length-1,n;t>=0;t--)e[t].children.length===0?t>0?(n=e[t-1].children,n.splice(n.indexOf(e[t]),1)):this.clear():Et(e[t],this.toBBox)}}function Si(i,e,t){if(!t)return e.indexOf(i);for(let n=0;n<e.length;n++)if(t(i,e[n]))return n;return-1}function Et(i,e){It(i,0,i.children.length,e,i)}function It(i,e,t,n,s){s||(s=At(null)),s.minX=1/0,s.minY=1/0,s.maxX=-1/0,s.maxY=-1/0;for(let r=e;r<t;r++){const o=i.children[r];Bt(s,i.leaf?n(o):o)}return s}function Bt(i,e){return i.minX=Math.min(i.minX,e.minX),i.minY=Math.min(i.minY,e.minY),i.maxX=Math.max(i.maxX,e.maxX),i.maxY=Math.max(i.maxY,e.maxY),i}function Ei(i,e){return i.minX-e.minX}function Ai(i,e){return i.minY-e.minY}function Pe(i){return(i.maxX-i.minX)*(i.maxY-i.minY)}function Zt(i){return i.maxX-i.minX+(i.maxY-i.minY)}function Ci(i,e){return(Math.max(e.maxX,i.maxX)-Math.min(e.minX,i.minX))*(Math.max(e.maxY,i.maxY)-Math.min(e.minY,i.minY))}function Ti(i,e){const t=Math.max(i.minX,e.minX),n=Math.max(i.minY,e.minY),s=Math.min(i.maxX,e.maxX),r=Math.min(i.maxY,e.maxY);return Math.max(0,s-t)*Math.max(0,r-n)}function De(i,e){return i.minX<=e.minX&&i.minY<=e.minY&&e.maxX<=i.maxX&&e.maxY<=i.maxY}function Kt(i,e){return e.minX<=i.maxX&&e.minY<=i.maxY&&e.maxX>=i.minX&&e.maxY>=i.minY}function At(i){return{children:i,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function xn(i,e,t,n,s){const r=[e,t];for(;r.length;){if(t=r.pop(),e=r.pop(),t-e<=n)continue;const o=e+Math.ceil((t-e)/n/2)*n;wn(i,o,e,t,s),r.push(e,o,o,t)}}const zt={minX:0,minY:0,maxX:0,maxY:0},bn=(i,e)=>i.id===e.id;class ki extends vi{constructor(t){super();y(this,"store");y(this,"onRotationChange",()=>{const t=this.store.getState().getAllFeatures().filter(n=>L(n)||D(n));for(const n of t)this.updateRotatedText(n)});y(this,"onZoomChange",()=>{const t=this.store.getState().getAllFeatures().filter(n=>{var s;return(L(n)||D(n))&&((s=n.properties.style)==null?void 0:s.fixedSize)});for(const n of t)this.updateRotatedText(n)});this.store=t,this.store.subscribe(n=>n.features,n=>{this.clear(),Object.values(n).forEach(s=>{L(s)||D(s)?this.updateRotatedText(s):this.insert(s)})}),this.store.subscribe(n=>({features:n.features,isDragging:n.isDragging,lastChangedFeatures:n.lastChangedFeatures,rotation:n.rotation}),(n,s)=>{s&&s.isDragging&&!n.isDragging&&n.lastChangedFeatures.length>0&&n.lastChangedFeatures.forEach(r=>{const o=n.features[r];o&&(ft(o),L(o)||D(o)?this.updateRotatedText(o):(this.remove(o,bn),this.insert(o)))})},{equalityFn:(n,s)=>n.isDragging===s.isDragging}),this.store.subscribe(n=>n.rotation,this.onRotationChange),this.store.subscribe(n=>n.zoom,this.onZoomChange)}updateRotatedText(t){var d;const n=this.store.getState();this.remove(t,bn);const s=J(t);let{width:r,height:o}=Y(t);if(((d=t.properties.style)==null?void 0:d.fixedSize)===!0){const u=n.invZoom;r*=u,o*=u}const c=r/2,h=o/2,l=n.getRotatedBBox(s.x-c,s.y-h,s.x+c,s.y+h);this.insert({...t,geometry:{...t.geometry,bbox:l.slice()}})}compareMinX(t,n){return st(t)[0]-st(n)[0]}compareMinY(t,n){return st(t)[1]-st(n)[1]}toBBox(t){const n=st(t);return zt.minX=n[0],zt.minY=n[1],zt.maxX=n[2],zt.maxY=n[3],zt}query(t){return super.search(t)}}class vn{constructor(e,t){y(this,"layer");this.ogma=e,this.store=t}destroy(){}}function Mi(i,e,t,n,s,r){const o=Math.min(n/2,s/2),a=Math.min(r,o);i.beginPath(),i.moveTo(e+a,t),i.lineTo(e+n-a,t),i.quadraticCurveTo(e+n,t,e+n,t+a),i.lineTo(e+n,t+s-a),i.quadraticCurveTo(e+n,t+s,e+n-a,t+s),i.lineTo(e+a,t+s),i.quadraticCurveTo(e,t+s,e,t+s-a),i.lineTo(e,t+a),i.quadraticCurveTo(e,t,e+a,t),i.closePath()}const Sn=[[-.5,-.5],[.5,-.5],[.5,.5],[-.5,.5]];class Pi extends vn{constructor(t,n){super(t,n);y(this,"handleFill","#fff");y(this,"handleStroke","#1A70E5");y(this,"handleStrokeWidth",1.5);y(this,"handleRadius",3);y(this,"handleMagnifier",1.5);y(this,"refresh",()=>{this.layer.refresh()});y(this,"render",t=>{const n=this.store.getState(),s=1/n.zoom,{hoveredHandle:r,rotation:o,liveUpdates:a,features:c}=n,h=this.handleRadius*s;t.fillStyle=this.handleFill,t.lineWidth=this.handleStrokeWidth*s,t.strokeStyle=this.handleStroke,Object.values(c).forEach(l=>{if(!n.isSelected(l.id))return;const d=a[l.id]?{...l,...a[l.id]}:l;if(tt(d))this.renderArrowHandles(d,t,h,r);else if(W(d)||L(d)){const u=L(d)?o:0;this.renderOutline(d,t,u,r,n.zoom),this.renderBoxHandles(d,t,h,r,u)}else it(d)&&this.renderPolygonHandles(d,t,h,r,n.drawingFeature===d.id)})});this.layer=t.layers.addCanvasLayer(this.render,void 0,Xt.HANDLES),this.store.subscribe(s=>({selectedFeatures:s.selectedFeatures,liveUpdates:s.liveUpdates}),this.refresh),t.events.on("zoom",this.refresh)}renderOutline(t,n,s,r,o){var m,x;const a=L(t)&&((m=t.properties.style)==null?void 0:m.fixedSize);let{width:c,height:h}=Y(t);a&&(c/=o,h/=o);const l=gt(t,a,o),d=c/2,u=h/2,g=l.x+d,f=l.y+u;n.save(),n.translate(g,f),n.rotate(s);const p=(x=t.properties.style)==null?void 0:x.borderRadius;if(p&&p>0?(Mi(n,-d,-u,c,h,p),n.stroke()):n.strokeRect(-d,-u,c,h),r>=4&&r<8){let v,S,E,b;const A=p&&p>0?p:this.handleRadius/o;switch(r){case 4:v=-d+A,S=-u,E=d-A,b=-u;break;case 5:v=d,S=-u+A,E=d,b=u-A;break;case 6:v=d-A,S=u,E=-d+A,b=u;break;case 7:v=-d,S=u-A,E=-d,b=-u+A;break}n.lineWidth=this.handleStrokeWidth*this.handleMagnifier/o,n.beginPath(),n.moveTo(v,S),n.lineTo(E,b),n.stroke(),n.lineWidth=this.handleStrokeWidth}n.restore()}renderArrowHandles(t,n,s,r){var u,g,f,p;const o=de(t),a=ue(t),c=+(r===0),h=+(r===1),l=s*(1+(this.handleMagnifier-1)*c),d=s*(1+(this.handleMagnifier-1)*h);n.beginPath(),n.moveTo(o.x,o.y),n.lineTo(a.x,a.y),n.closePath(),n.stroke(),n.beginPath(),((g=(u=t.properties.link)==null?void 0:u.start)==null?void 0:g.type)!=="comment"&&(n.moveTo(o.x-l,o.y-l),n.rect(o.x-l,o.y-l,l*2,l*2)),((p=(f=t.properties.link)==null?void 0:f.end)==null?void 0:p.type)!=="comment"&&(n.moveTo(a.x-d,a.y-d),n.rect(a.x-d,a.y-d,d*2,d*2)),n.closePath(),n.fill(),n.stroke()}renderBoxHandles(t,n,s,r,o){var f;if(L(t)&&((f=t.properties.style)!=null&&f.fixedSize))return;const{width:a,height:c}=Y(t),h=gt(t),l=a/2,d=c/2,u=h.x+l,g=h.y+d;n.save(),n.translate(u,g),n.rotate(o),n.beginPath();for(let p=0;p<Sn.length;p++){const[m,x]=Sn[p],v=a*m,S=c*x,E=r===p?s*this.handleMagnifier:s;n.rect(v-E,S-E,E*2,E*2)}n.closePath(),n.fill(),n.stroke(),n.restore()}renderPolygonHandles(t,n,s,r,o){const a=t.geometry.coordinates[0];if(!o){a.length>2&&(n.save(),n.strokeStyle=this.handleStroke,n.lineWidth=this.handleStrokeWidth/this.store.getState().zoom,this.drawSmoothPolygon(n,a,.5),n.stroke(),n.restore()),n.beginPath();for(let c=0;c<a.length-1;c++){const[h,l]=a[c],d=r===c?s*this.handleMagnifier:s;n.moveTo(h+d,l),n.arc(h,l,d,0,2*Math.PI)}n.closePath(),n.fill(),n.stroke()}}drawSmoothPolygon(t,n,s){const r=n.slice(0,-1);if(r.length<3)return;t.beginPath();const o=r.length;for(let a=0;a<o;a++){const c=r[(a-1+o)%o],h=r[a],l=r[(a+1)%o],d=r[(a+2)%o],u=h[0]+(l[0]-c[0])/6*s,g=h[1]+(l[1]-c[1])/6*s,f=l[0]-(d[0]-h[0])/6*s,p=l[1]-(d[1]-h[1])/6*s;a===0&&t.moveTo(h[0],h[1]),t.bezierCurveTo(u,g,f,p,l[0],l[1])}t.closePath()}renderCenterPoint(t,n){var r;const s=this.store.getState();if(L(t)){const o=gt(t,(r=t.properties.style)==null?void 0:r.fixedSize,s.zoom),a=Y(t);n.moveTo(o.x+a.width/2+5,o.y+a.height/2),n.arc(o.x+a.width/2,o.y+a.height/2,5,0,Math.PI*2,!0)}}destroy(){this.layer.destroy(),super.destroy()}}const Di=.5;function Fi(i,e,t,n){const{start:s,end:r}=Ut(i),o=X(r,s);return Math.min(n,Math.max(2*e,Q(o)*.01,t))}function En(i,e){return!i||i==="none"?0:i==="dot"?e*2:i==="halo-dot"?e*4:i==="arrow"||i==="arrow-plain"?e:0}function Li(i,e){if(!i){i=k("g");const t=k("path");i.classList.add("annotation-arrow"),i.setAttribute("data-annotation",`${e}`),i.setAttribute("data-annotation-type","arrow"),i.appendChild(t);const n=k("g");n.classList.add("annotation-arrow-endpoints"),i.appendChild(n)}return i}function _i(i,e,t,n,s,r){const{start:o,end:a}=Ut(e),c=e.properties.style||mt,{tail:h,strokeWidth:l=0,head:d,strokeColor:u,strokeType:g}=c,f=X(a,o),p=dn(e)?1/r.zoom:1,m=Fi(e,l,t,n)*p,x=Li(s,e.id),v=x.firstChild,S=u||"none";v.setAttribute("stroke",S),v.setAttribute("stroke-width",`${p*l}`),v.setAttribute("fill","none"),v.setAttribute("stroke-linecap","round"),v.setAttribute("stroke-linejoin","round");const E=En(h,l*p),b=En(d,l*p),A=Wt(f),T=pt(o,ct(A,E)),C=pt(a,ct(A,-b)),M=`M ${T.x} ${T.y} L ${C.x} ${C.y}`;v.setAttribute("d",M);const F=x.children[1];return F.innerHTML="",g==="dashed"?v.setAttribute("stroke-dasharray","5,10"):v.removeAttribute("stroke-dasharray"),An(F,o,f,S,h,l,p,m),An(F,a,Es(f),S,d,l,p,m),x.setAttribute("transform",`rotate(${-r.rotation*(180/Math.PI)})`),i.appendChild(x),x}function An(i,e,t,n,s,r,o,a){s==="halo-dot"&&Cn(i,e,Hi(n),Di,r*o*4),(s==="dot"||s==="halo-dot")&&Cn(i,e,n,1,r*o*2),(s==="arrow"||s==="arrow-plain")&&Ii(i,e,t,n,s==="arrow-plain",r*o,a)}function Hi(i){return i==="none"?"none":i}function Cn(i,e,t,n,s){const r=k("circle");r.setAttribute("cx",`${e.x}`),r.setAttribute("cy",`${e.y}`),r.setAttribute("r",`${s}`),r.setAttribute("fill-opacity",`${n}`),r.setAttribute("fill",t),i.appendChild(r)}function Ii(i,e,t,n,s,r,o){const a=ct(Wt(t),o),c=pt(e,rn(a,Math.PI/6)),h=pt(e,rn(a,-Math.PI/6)),l=k("path"),d=`${e.x} ${e.y}`,u=`M ${c.x} ${c.y} L ${d} L ${h.x} ${h.y}${s?" Z":""}`;l.setAttribute("d",u),l.setAttribute("stroke",n),l.setAttribute("stroke-width",`${r}`),l.setAttribute("fill",s?n:"none"),l.setAttribute("stroke-linecap","round"),l.setAttribute("stroke-linejoin","round"),i.appendChild(l)}function Bi(i,e,t="box"){if(!i){i=k("g"),i.setAttribute("data-annotation",`${e}`),i.setAttribute("data-annotation-type",t),i.classList.add(`annotation-${t}`);const n=k("rect");i.appendChild(n)}return i}function Tn(i,e,t,n){const s=e.id,r=`class${s}`,o=Y(e),a=Bi(t,s,e.properties.type),{strokeColor:c,strokeWidth:h,strokeType:l,background:d,borderRadius:u,boxShadow:g}=e.properties.style||St;a.classList.add("annotation-box"),a.setAttribute("fill",`${d||"transparent"}`);const f=a.firstChild;u&&(f.setAttribute("rx",`${u}`),f.setAttribute("ry",`${u}`)),l&&l!=="none"&&(f.setAttribute("stroke",c||"black"),f.setAttribute("stroke-width",`${h}`),l==="dashed"&&f.setAttribute("stroke-dasharray","5,5")),d&&d.length&&(n.hoveredFeature===e.id?f.setAttribute("fill",bt(d)):f.setAttribute("fill",d));const p=gt(e);return f.setAttribute("width",`${o.width}`),f.setAttribute("height",`${o.height}`),f.setAttribute("x",`${p.x}`),f.setAttribute("y",`${p.y}`),a.appendChild(f),g?a.style.filter=`drop-shadow(${g})`:a.style.filter="",a.setAttribute("transform",n.getRotationTransform(0,0)),a.classList.add(r),a.setAttribute("data-annotation",`${e.id}`),a.setAttribute("data-annotation-type",e.properties.type),i.appendChild(a),a}let Fe=null;function kn(){return Fe||(Fe=document.createElement("canvas").getContext("2d")),Fe}function Mn(i,e,t,n,s){if(!i||i.length===0)return!0;if(i.includes(`
11
+ </div>`,position:r,size:o},Xt.EDITOR),this.textarea=this.layer.element.querySelector("textarea"),this.textarea.setAttribute("wrap","on"),this.textarea.setAttribute("spellcheck","false"),this.textarea.value=a.properties.content||"",h&&(this.sendButton=this.layer.element.querySelector(".ogma-send-button"),this.sendButton.addEventListener("click",this.onSendClick),this.updateSendButtonState()),this.isFocused=!1,this.textarea.addEventListener("focus",this.onFocus),this.textarea.addEventListener("blur",this.onBlur),this.textarea.addEventListener("input",this.onInput),this.textarea.addEventListener("keyup",this.onKeyup),this.textarea.addEventListener("keydown",this.onKeydown),this.textarea.addEventListener("wheel",this.onMouseEvent),this.textarea.addEventListener("mouseup",this.onMouseEvent),this.textarea.addEventListener("mousedown",this.onMouseEvent),this.textarea.addEventListener("click",this.onMouseEvent),this.textarea.addEventListener("drag",this.onMouseEvent),this.updateStyle(),this.updatePosition(),this.textarea.focus(),this.unsubscribe=this.store.subscribe(p=>({rotation:p.rotation,zoom:p.zoom,annotation:p.features[this.annotation]}),this.update,{equalityFn:(p,m)=>p.rotation===m.rotation&&p.zoom===m.zoom&&p.annotation===m.annotation})}getAnnotation(){const e=this.store.getState();e.liveUpdates[this.annotation]||e.startLiveUpdate([this.annotation]);const t=e.getFeature(this.annotation),n=e.liveUpdates[this.annotation];return t&&n?{...t,...n}:t}getPosition(){const e=this.getAnnotation();if(!e)return{x:0,y:0};const t=e.properties.style,n=(t==null?void 0:t.fixedSize)||!1,s=t==null?void 0:t.maxHeight,r=this.store.getState().zoom,o=ke(e),[a,c]=e.geometry.coordinates,h=e.properties.width;let l=e.properties.height;s&&(l=Math.min(l,s));const d=n?1/r:1;return{x:a-h*d/2+o*d,y:c-l*d/2+o*d}}getSize(){var g;const e=this.getAnnotation();if(!e)return{width:0,height:0};const t=Y(e),n=ke(e),s=e.properties.style,r=(s==null?void 0:s.fixedSize)||!1,o=s==null?void 0:s.maxHeight,a=this.store.getState().zoom,c=this.store.getState(),h=D(e)&&(((g=c.options)==null?void 0:g.showSendButton)??!0),l=r?1/a:1;let d=(t.height-n*2)*l;if(o){const f=(o-n*2)*l;d=Math.min(d,f)}const u=h?28*l:0;return{width:(t.width-n*2)*l,height:d+u}}updateStyle(){var v;const e=this.getAnnotation();if(!e)return;const t=D(e)?K:V,{font:n,fontSize:s=t.fontSize,borderRadius:r,color:o,background:a=t.background,padding:c=0,fixedSize:h=t.fixedSize}=e.properties.style||t,l=this.textarea,d=this.layer.element,u=this.store.getState().zoom,g=h?1/u:1,f=parseFloat(s.toString())*g,p=c*g,m=d.style;if(m.display="grid",m.gridTemplateRows="1fr auto",m.boxSizing="border-box",m.background=a||"transparent",m.borderRadius=`${(r||0)*g}px`,m.padding=`${p}px`,m.transformOrigin="center",m.transform=`rotate(${this.store.getState().rotation}rad)`,e.properties.type==="comment"){const S=e.properties.style,E=(S==null?void 0:S.shadow)!==!1;m.boxShadow=E?"0 2px 8px rgba(0, 0, 0, 0.15)":"none"}const x=l.style;if(x.font=`${f} ${n}`,x.fontFamily=n||"sans-serif",x.fontSize=`${f}px`,x.lineHeight=`${f*Pt}px`,x.padding="0",x.border="none",x.outline="none",x.boxSizing="border-box",x.color=o||"black",x.background="transparent",x.flex="1",x.minHeight="0",x.resize="none",this.sendButton){const S=24*g,E=4*g;this.sendButton.style.width=`${S}px`,this.sendButton.style.height=`${S}px`,this.sendButton.style.padding=`${E}px`,this.sendButton.style.transform="";const b=this.sendButton.querySelector(".ogma-send-button-icon");if(b){const A=16*g;b.style.width=`${A}px`,b.style.height=`${A}px`}}if(h){const S=(v=e.properties.style)==null?void 0:v.maxHeight,E=S&&e.properties.height>=S;x.overflowY=E?"auto":"hidden",x.overflowX="hidden"}}updatePosition(){this.layer.setPosition(this.getPosition())}updateContent(){var o,a,c,h;const e=this.getAnnotation();if(!e)return;let t=e.properties.height,n=e.geometry.coordinates;const s=(o=e.properties.style)==null?void 0:o.fixedSize;if(s){const l=this.textarea.style.height;this.textarea.style.height="0px";const d=this.textarea.scrollHeight;this.textarea.style.height=l;const u=ke(e),g=this.store.getState().zoom,f=((a=e.properties.style)==null?void 0:a.padding)||0,p=(d+u*2/g)*g+f*2,m=((c=e.properties.style)==null?void 0:c.minHeight)||50;t=Math.max(m,p);const x=(h=e.properties.style)==null?void 0:h.maxHeight,v=e.properties.height,S=t-v;if(Math.abs(S)>1){const[E,b]=e.geometry.coordinates;if(x&&t>x){const A=x-v;t=x,Math.abs(A)>1&&(n=[E,b+A/2])}else n=[E,b+S/2]}}const r={...e,properties:{...e.properties,content:this.textarea.value,height:t},geometry:{...e.geometry,coordinates:n}};s&&Math.abs(e.properties.height-t)>1&&(this.layer.setSize(this.getSize()),this.layer.setPosition(this.getPosition())),this.store.getState().applyLiveUpdate(this.annotation,r)}updateSendButtonState(){if(!this.sendButton)return;const e=this.textarea.value.trim().length===0;this.sendButton.disabled=e}destroy(){this.store.getState().commitLiveUpdates(new Set([this.annotation])),this.unsubscribe(),this.sendButton&&this.sendButton.removeEventListener("click",this.onSendClick),this.layer.destroy()}}function ke(i){var e;return((e=i.properties.style)==null?void 0:e.strokeWidth)||0}const gn={x:1,y:0},fn={x:0,y:1},Js=[["top",fn,-.5,-.5,.5,-.5],["right",gn,.5,-.5,.5,.5],["bottom",fn,-.5,.5,.5,.5],["left",gn,-.5,-.5,-.5,.5]],Qs={top:[0,1],right:[1,2],bottom:[2,3],left:[3,0]},pn=[[-.5,-.5],[.5,-.5],[.5,.5],[-.5,.5]],ti=[H.nwResize,H.neResize,H.seResize,H.swResize];class Me extends Gt{constructor(t,n,s){super(t,n);y(this,"links");y(this,"textEditor",null);y(this,"justActivated",!1);y(this,"onClick",t=>{const n=this.getAnnotation();if(n){if(D(n)&&n.properties.mode===ot){this.store.getState().updateFeature(n.id,{properties:{...n.properties,mode:Mt}}),this.ogma.view.afterNextFrame(),this.justActivated=!1;return}if(this.store.getState().selectedFeatures.has(n.id)){if(D(n)&&this.justActivated){this.justActivated=!1;return}n&&(L(n)||D(n))&&this.startEditingText()}}});y(this,"stopEditingText",()=>{this.textEditor&&(this.store.getState().drawingFeature===this.annotation&&this.store.setState({drawingFeature:null}),this.textEditor.destroy()),this.textEditor=null,this.store.setState({editingFeature:null})});this.links=s}detectHandle(t,n){var E;const s=this.getAnnotation(),{x:r,y:o}=this.clientToCanvas(t);let{width:a,height:c}=Y(s);const h=5/n,l=J(s),d=this.store.getState();let{revSin:u,revCos:g}=d;(W(s)||D(s))&&(u=0,g=1),this.hoveredHandle=void 0;const f=Ze*(1/n),p=r-l.x,m=o-l.y,x=p*g-m*u,v=p*u+m*g,S=((E=s.properties.style)==null?void 0:E.fixedSize)===!0||D(s);if(S&&(a/=n,c/=n),!S){for(let b=0;b<pn.length;b++){const[A,T]=pn[b],C=a*A,M=c*T;if(x>=C-f-h&&x<=C+f+h&&v>=M-f-h&&v<=M+f+h){this.hoveredHandle={type:"corner",corner:b},this.store.setState({hoveredHandle:b,hoveredFeature:this.annotation}),this.setCursor(this.getCornerCursor(b));return}}for(const[b,A,T,C,M,F]of Js){const j=a*T,U=c*C,R=a*M,at=c*F,lt=vt(A,{x:x-j,y:v-U});if(Math.abs(lt)<h&&x>=j-h&&x<=R+h&&v>=U-h&&v<=at+h){this.hoveredHandle={type:"edge",edge:b,corner:-1},this.store.setState({hoveredHandle:Qs[b][0]+4,hoveredFeature:this.annotation}),this.setCursor(this.getEdgeCursor(b));return}}}if(x>=-a/2-h&&x<=a/2+h&&v>=-c/2-h&&v<=c/2+h){this.store.setState({hoveredHandle:8,hoveredFeature:this.annotation}),this.hoveredHandle={type:"body",corner:-1},this.setCursor(H.grab);return}this.store.setState({hoveredHandle:-1,hoveredFeature:null}),this.setCursor(H.default)}onDrag(t){if(!this.dragStartPoint||!this.hoveredHandle||!this.isActive())return;t.stopPropagation(),t.stopImmediatePropagation();const n=this.getAnnotation(),s=this.clientToCanvas(t),r={x:s.x-this.dragStartPoint.x,y:s.y-this.dragStartPoint.y},o=this.hoveredHandle,a=this.getAnnotation();let c=null;o.type==="corner"?c=this.dragCorner(a,r,o.corner):o.type==="edge"&&o.edge?c=this.dragEdge(a,r,o):o.type==="body"&&(Te(this.store,this.links,n.id,r),this.textEditor&&this.textEditor.update()),c&&(this.store.getState().applyLiveUpdate(n.id,c),this.links.updateLinkedArrowsDuringDrag(n.id,r),this.textEditor&&this.textEditor.update()),this.dispatchEvent(new CustomEvent(Tt,{detail:{point:s,annotation:this.annotation,handle:o}}))}dragCorner(t,n,s){const r=s===0||s===1,o=s===0||s===3,a=s===1||s===2,c=s===2||s===3,{width:h,height:l}=Y(t),d=this.store.getState();let{revSin:u,revCos:g}=d;(W(t)||D(t))&&(u=0,g=1);const f=n.x*g-n.y*u,p=n.x*u+n.y*g;let m=0,x=0;o?m=-f:a&&(m=f),r?x=-p:c&&(x=p);const v=Math.max(0,h+m),S=Math.max(0,l+x),E=J(t),b=E.x+n.x/2,A=E.y+n.y/2;return{...t,properties:{...t.properties,width:v,height:S},geometry:{type:t.geometry.type,coordinates:[b,A]}}}dragEdge(t,n,s){const{width:r,height:o}=Y(t),a=J(t),c=this.store.getState();let{revSin:h,revCos:l}=c;(W(t)||D(t))&&(h=0,l=1);const d=n.x*l-n.y*h,u=n.x*h+n.y*l;let g=r,f=o,p=0,m=0;switch(s.edge){case"top":f=Math.max(0,o-u),m=u/2;break;case"bottom":f=Math.max(0,o+u),m=u/2;break;case"left":g=Math.max(0,r-d),p=d/2;break;case"right":g=Math.max(0,r+d),p=d/2;break}const x=p*l+m*h,v=-p*h+m*l;return{...t,properties:{...t.properties,width:g,height:f},geometry:{type:t.geometry.type,coordinates:[a.x+x,a.y+v]}}}onDragStart(t){return super.onDragStart(t)?(this.stopEditingText(),this.store.getState().startLiveUpdate([this.annotation]),!0):!1}startEditingText(){this.textEditor===null&&(this.store.setState({editingFeature:this.annotation}),this.textEditor=new Ks(this.ogma,this.store,this.annotation,this.stopEditingText))}onDragEnd(t){var a,c;if(!super.onDragEnd(t))return!1;this.store.getState().drawingFeature===this.annotation&&this.store.setState({drawingFeature:null});const s=this.clientToCanvas(t),r=s.x-(((a=this.dragStartPoint)==null?void 0:a.x)||0),o=s.y-(((c=this.dragStartPoint)==null?void 0:c.y)||0);return Math.abs(r)<1&&Math.abs(o)<1?(this.clearDragState(),this.onClick({clientX:t.clientX,clientY:t.clientY})):this.commitChange(),this.hoveredHandle=void 0,this.dragStartPoint=void 0,this.dragging=!1,!0}getCornerCursor(t){return ti[t]}getEdgeCursor(t){switch(t){case"top":case"bottom":return H.nsResize;case"left":case"right":return H.ewResize;default:return H.default}}stopEditing(){super.stopEditing(),this.commitChange(),this.stopEditingText()}startDrawing(t,n,s){this.annotation=t,this.hoveredHandle={type:"corner",corner:2},this.store.setState({hoveredHandle:2,hoveredFeature:this.annotation}),this.dragging=!0;const r=this.ogma.view.graphToScreenCoordinates({x:n,y:s});this.dragStartPoint=r,this.disablePanning(),this.onDragStart({clientX:r.x,clientY:r.y})}setAnnotation(t){t&&D(t)&&(this.justActivated=!0),super.setAnnotation(t),t&&D(t)&&t.properties.mode===ot&&this.store.getState().updateFeature(t.id,{properties:{...t.properties,mode:Mt}})}}class ei extends EventTarget{constructor(t,n,s,r,o){super();y(this,"handlers",new Map);y(this,"activeHandler");y(this,"interaction");y(this,"ogma");y(this,"snapping");y(this,"store");this.ogma=t,this.store=n,this.snapping=s,this.interaction=o,this.handlers.set("box",new Me(this.ogma,this.store,r)),this.handlers.set("text",new Me(this.ogma,this.store,r)),this.handlers.set("comment",new Me(this.ogma,this.store,r)),this.handlers.set("arrow",new Vs(this.ogma,this.store,this.snapping,r)),this.handlers.set("polygon",new Zs(this.ogma,this.store,r)),this.handlers.forEach(a=>{a.addEventListener("dragstart",(c=>{this.dispatchEvent(new CustomEvent(xt,{detail:{...c.detail}})),this.store.setState({isDragging:!0}),this.interaction.setMode("edit")})),a.addEventListener("dragend",(c=>{this.dispatchEvent(new CustomEvent(kt,{detail:{...c.detail}})),this.store.setState({isDragging:!1}),this.interaction.suppressClicksTemporarily()}))}),this.store.subscribe(a=>a.selectedFeatures,(a,c)=>{const h=Array.from(a.keys()).filter(d=>!c.has(d)),l=Array.from(c.keys()).filter(d=>!a.has(d));!h.length&&!l.length||(l.forEach(d=>this.stopEditingFeature(d)),h.forEach(d=>this.editFeature(d)))})}stopEditingFeature(t){const n=this.store.getState().features[t];if(!n)return;const s=n.properties.type,r=this.handlers.get(s);r&&r.stopEditing()}editFeature(t){var s;const n=this.store.getState().features[t];n&&(this.setActiveHandler(n.properties.type),(s=this.activeHandler)==null||s.setAnnotation(n))}getSnapping(){return this.snapping}getArrowHandler(){return this.handlers.get("arrow")}getActiveHandler(){return this.activeHandler}setActiveHandler(t){const n=this.handlers.get(t);if(!n)throw new Error(`Handler for type ${t} not found`);return this.activeHandler=n,this}destroy(){}}const ni=["x","y","radius"],si=1;class ii{constructor(e,t,n,s){y(this,"links",new Map);y(this,"nodeToLink",new Map);y(this,"edgeToLink",new Map);y(this,"annotationToLink",new Map);y(this,"linksByArrowId",new Map);y(this,"store");y(this,"ogma");y(this,"snapping");y(this,"updatedItems",new Set);y(this,"onLinkCreated");y(this,"commitTimeout");y(this,"onSetMultipleAttributes",({elements:e,updatedAttributes:t})=>{const n=new Set(t);!e.isNode||!n.has("x")&&!n.has("y")&&!n.has("radius")||this.requestUpdateFromNodePositions(e.toList())});y(this,"refresh",()=>{const e=this.store.getState(),t=new Map;this.annotationToLink.forEach((n,s)=>{var a;const r=e.getFeature(s);if(!r)return;(L(r)&&((a=r.properties.style)==null?void 0:a.fixedSize)||D(r))&&n.forEach(c=>{const h=this.links.get(c);if(!h)return;const l=h.arrow;t.set(l,this.linksByArrowId.get(l))})}),t.size>0&&this.update(t)});y(this,"throttledRefresh",fe(()=>this.refresh(),20));y(this,"onAddRemoveEdges",e=>{const t=e.edges;if(!t.size||!this.edgeToLink.size)return;const n=new Map,s=new Map;t.getParallelEdges().getId().forEach(r=>{const o=this.edgeToLink.get(r);o&&o.forEach(a=>{const c=this.links.get(a);if(!c)return;const h=c.arrow;n.set(h,this.linksByArrowId.get(h)),s.set(h,this.linksByArrowId.get(h))})}),s.size!==0&&this.update(s)});y(this,"commit",()=>{this.store.getState().batchUpdate(this.commitLiveUpdates),this.updatedItems.clear()});y(this,"commitLiveUpdates",()=>{this.store.getState().commitLiveUpdates(this.updatedItems)});y(this,"onAddArrow",(e,t)=>{const n=this.store.getState(),s=new Set(Object.keys(t)),r=Object.keys(e).filter(a=>!s.has(a)),o=Object.keys(t).filter(a=>!e[a]);r.forEach(a=>{var l,d;const c=n.getFeature(a);if(!c||!tt(c))return;const h=c;if((l=h.properties.link)!=null&&l.start){const u=h.properties.link.start;this.add(h,O,u.id,u.type,u.magnet)}if((d=h.properties.link)!=null&&d.end){const u=h.properties.link.end;this.add(h,rt,u.id,u.type,u.magnet)}}),o.forEach(a=>{const c=t[a];if(tt(c)){const h=c;this.remove(h,O),this.remove(h,rt)}else{const h=this.annotationToLink.get(a);if(!h)return;for(const l of h){const d=this.links.get(l);if(!d)continue;const u=n.getFeature(d.arrow);u?this.remove(u,d.side):this.remove(d.arrow,d.side)}}})});this.ogma=e,this.store=n,this.snapping=t,this.onLinkCreated=s,this.store.subscribe(r=>r.features,this.onAddArrow),this.store.subscribe(r=>({zoom:r.zoom,rotation:r.rotation}),this.throttledRefresh,{equalityFn:(r,o)=>r.zoom===o.zoom&&r.rotation===o.rotation}),this.ogma.events.on("setMultipleAttributes",this.onSetMultipleAttributes).on(["addEdges","removeEdges"],this.onAddRemoveEdges).on("viewChanged",this.refresh)}updateLinkedArrowsDuringDrag(e,t,n){const s=this.store.getState();if(!s.getFeature(e))return;const o=this.annotationToLink.get(e);if(o)for(const a of o){const c=this.links.get(a);if(!c)continue;const h=s.getFeature(c.arrow),l=Dt(h,c.side),d=pt(l,t),u={...h.geometry,coordinates:h.geometry.coordinates.map((g,f)=>c.side===O&&f===0||c.side===rt&&f===1?[d.x,d.y]:[...g])};n?n[h.id]={geometry:u}:s.applyLiveUpdate(h.id,{geometry:u}),this.updatedItems.add(h.id)}}snapLinkedArrowsDuringDrag(e,t){const n=this.store.getState();let s=n.getFeature(e);if(!s)return;const r=n.liveUpdates[e];s=r?{...s,...r}:s;const o=this.annotationToLink.get(e);if(o)for(const a of o){const c=this.links.get(a);if(!c)continue;let h=n.getFeature(c.arrow);const l=n.liveUpdates[h.id];h=l?{...h,...l}:h;const d=h.geometry.coordinates[c.side===O?0:1],u={x:d[0],y:d[1]};let g;if(L(s)||D(s)||W(s)?g=this.snapping.snapToText(u,[s]):it(s)&&(g=this.snapping.snapToPolygon(u,[s])),!g)continue;const f=g.point,p={...h.geometry,coordinates:h.geometry.coordinates.map((m,x)=>c.side===O&&x===0||c.side===rt&&x===1?[f.x,f.y]:[...m])};t?t[h.id]={geometry:p}:n.applyLiveUpdate(h.id,{geometry:p})}}add(e,t,n,s,r){const o=ut(),a=e.id;let c=r;if(s===P.POLYGON){const u=this.store.getState().getFeature(n);if(u&&it(u)){const g=le(u),f=g[2]-g[0],p=g[3]-g[1],m=r.x-g[0],x=r.y-g[1],v=f>0?m/f:.5,S=p>0?x/p:.5;c={x:v,y:S}}}const h={id:o,arrow:a,target:n,targetType:s,magnet:c,side:t};if(s===P.NODE&&!this.ogma.getNode(n)||s===P.EDGE&&!this.ogma.getEdge(n))return;this.remove(e,t),this.links.set(o,h);const l=s===P.NODE?this.nodeToLink:s===P.EDGE?this.edgeToLink:this.annotationToLink;return l.has(n)||l.set(n,new Set),l.get(n).add(o),this.linksByArrowId.has(a)||this.linksByArrowId.set(a,{}),this.linksByArrowId.get(a)[t]=o,e.properties.link=e.properties.link||{},e.properties.link[t]={id:n,side:t,type:s,magnet:c},this.onLinkCreated&&this.onLinkCreated(e,h),this}remove(e,t){var o,a,c,h,l;const n=typeof e=="object"?e.id:e,s=(o=this.linksByArrowId.get(n))==null?void 0:o[t];if(typeof e=="object"&&((a=e.properties.link)==null||delete a[t]),!s)return this;const r=this.links.get(s);return r?(this.links.delete(s),(c=this.nodeToLink.get(r.target))==null||c.delete(s),(h=this.edgeToLink.get(r.target))==null||h.delete(s),(l=this.annotationToLink.get(r.target))==null||l.delete(s),this.linksByArrowId.has(n)&&(this.linksByArrowId.get(n)[t]=void 0),this):this}requestUpdateFromNodePositions(e){setTimeout(()=>this.updateFromNodePositions(e),1)}updateFromNodePositions(e){const t=e.getId(),n=new Map;t.forEach(h=>{const l=this.nodeToLink.get(h);l&&l.forEach(d=>{const u=this.links.get(d);if(!u)return;const g=u.arrow;n.set(g,this.linksByArrowId.get(g))})});const s=new Map;e.getAdjacentEdges().getId().forEach(h=>{const l=this.edgeToLink.get(h);l&&l.forEach(d=>{const u=this.links.get(d);if(!u)return;const g=u.arrow;n.set(g,this.linksByArrowId.get(g)),s.set(g,this.linksByArrowId.get(g))})});const o=e.getAttributes(ni),a=this.store.getState(),c={};for(let h=0;h<t.length;h++){const l=t[h],d=this.nodeToLink.get(l);if(d)for(const u of d){const g=this.links.get(u);if(!g)continue;const f=g.arrow,p=this.store.getState().getFeature(f),m=p.geometry.coordinates.slice(),x=Dt(p,rt),v=Dt(p,O),S=o[h],E=this._getNodeSnapPoint(S,ct(X(x,v),-1),this._isLinkedToCenter(g));m[g.side===O?0:1]=E,c[f]={...p,geometry:{coordinates:m}},this.updatedItems.add(f),g.magnet={x:E[0]-S.x,y:E[1]-S.y},ft(c[f])}}if(a.applyLiveUpdates(c),s.size>0){this.update(s);return}this.requestCommit()}update(e=this.linksByArrowId){const t=this.store.getState(),n=Array.from(this.nodeToLink.keys()),s=new Map;n.forEach((c,h)=>s.set(c,h));const o=this.ogma.getNodes(n).getAttributes(["x","y","radius"]),a={};e.forEach((c,h)=>{const l=this.links.get(c.start),d=this.links.get(c.end),u=t.getFeature(h);let g=u.geometry.coordinates[0],f=u.geometry.coordinates[1];const p=l?l.targetType===P.NODE?o[s.get(l.target)]:l.targetType===P.EDGE?this._getEdgeSnapPoint(l.target,l.magnet.x,!0):this._getAnnotationCenter(t.getFeature(l.target)):{x:g[0],y:g[1]},m=d?d.targetType===P.NODE?o[s.get(d.target)]:d.targetType===P.EDGE?this._getEdgeSnapPoint(d.target,d.magnet.x,!0):this._getAnnotationCenter(t.getFeature(d.target)):{x:f[0],y:f[1]},x=X(m,p);if(l)if(l.targetType===P.NODE)g=this._getNodeSnapPoint(p,x,this._isLinkedToCenter(l));else if(l.targetType===P.EDGE)g=this._getEdgeSnapPoint(l.target,l.magnet.x);else{const v=t.getFeature(l.target);g=this._getAnnotationSnapPoint(v,m,l,t.zoom)}if(d)if(d.targetType===P.NODE)f=this._getNodeSnapPoint(m,ct(x,-1),this._isLinkedToCenter(d));else if(d.targetType===P.EDGE)f=this._getEdgeSnapPoint(d.target,d.magnet.x);else{const v=t.getFeature(d.target);f=this._getAnnotationSnapPoint(v,p,d,t.zoom)}a[u.id]={properties:u.properties,geometry:{coordinates:[g,f]}},ft(a[u.id]),this.updatedItems.add(u.id)}),t.applyLiveUpdates(a),this.requestCommit()}requestCommit(){clearTimeout(this.commitTimeout),this.commitTimeout=setTimeout(this.commit,si)}_isLinkedToCenter(e){return e.magnet.x===0&&e.magnet.y===0}_getAnnotationCenter(e){return it(e)?ce(e):J(e)}_getAnnotationSnapPoint(e,t,n,s){if(it(e)){const r=le(e),o=r[0]+n.magnet.x*(r[2]-r[0]),a=r[1]+n.magnet.y*(r[3]-r[1]);return[o,a]}return this._getBoxSnapPoint(e,t,n,s)}_getBoxSnapPoint(e,t,n,s){var d;const r=J(e);let{width:o,height:a}=Y(e);(L(e)&&((d=e.properties.style)==null?void 0:d.fixedSize)||D(e))&&(o/=s,a/=s);let h=n.magnet.x*o,l=n.magnet.y*a;if(L(e)&&!W(e)){const{sin:u,cos:g}=this.store.getState(),f=h*g-l*u,p=h*u+l*g;h=f,l=p}return[r.x+h,r.y+l]}_getNodeSnapPoint(e,t,n){if(n)return[e.x,e.y];const s=Math.sqrt(t.x*t.x+t.y*t.y),r=ct(t,1/s),o=s<Number(e.radius)/2?{x:e.x,y:e.y}:pt({x:e.x,y:e.y},ct(r,-Number(e.radius)));return[o.x,o.y]}_getEdgeSnapPoint(e,t,n=!1){const s=this.ogma.getEdge(e);if(!s)return[0,0];const r=s.getAttribute("curvature"),a=s.getExtremities().getPosition(),c=a[0],h=a[1];if(r===0||r===void 0){const u=[c.x+t*(h.x-c.x),c.y+t*(h.y-c.y)];return n?{x:u[0],y:u[1]}:u}const l=q.geometry.getQuadraticCurveControlPoint(c.x,c.y,h.x,h.y,r),d=q.geometry.getPointOnQuadraticCurve(t,c.x,c.y,h.x,h.y,l.x,l.y);return n?{x:d.x,y:d.y}:[d.x,d.y]}destroy(){this.ogma.events.off(this.onSetMultipleAttributes)}}function ri(i,e,t,n,s=20){let r=0,o=1/0,a=null;for(let c=0;c<=s;c++){const h=c/s,l=q.geometry.getPointOnQuadraticCurve(h,e.x,e.y,t.x,t.y,n.x,n.y),d=Q(X(l,i));d<o&&(o=d,a=l,r=h)}return a?{t:r,point:a,distance:o}:null}function oi(i,e,t){const n=t.x-e.x,s=t.y-e.y,r=n*n+s*s;if(r===0)return{t:0,point:{x:e.x,y:e.y},distance:Q(X(e,i))};const o=Math.max(0,Math.min(1,((i.x-e.x)*n+(i.y-e.y)*s)/r)),a={x:e.x+o*n,y:e.y+o*s};return{t:o,point:a,distance:Q(X(a,i))}}function ai(i,e,t){let n=null,s=t;const r=e.getAttribute("curvature"),a=e.getExtremities().getPosition();for(let c=0;c<e.size;c++){const h=e.get(c),l=r[c],d=a[2*c],u=a[2*c+1];let g;if(l===0||l===void 0)g=oi(i,d,u);else{const f=q.geometry.getQuadraticCurveControlPoint(d.x,d.y,u.x,u.y,l);g=ri(i,d,u,f)}g&&g.distance<s&&(s=g.distance,n={point:g.point,id:h.getId(),magnet:{x:g.t,y:0},type:P.EDGE})}return n}function li(i,e,t){const n=e.getAttributes(["x","y","radius"]);for(let s=0;s<n.length;s++){const r=n[s],o=X({x:r.x,y:r.y},i),a=Q(o);if(a>=Number(r.radius)+Number(t))continue;const c=ct(o,1/a),h=a<Number(r.radius)/2,l=h?{x:r.x,y:r.y}:pt({x:r.x,y:r.y},ct(c,-Number(r.radius))),d=h?{x:0,y:0}:c;return{point:l,id:e.get(s).getId(),magnet:d,type:P.NODE}}return null}function ci(i,e){let t=!1;for(let n=0,s=e.length-1;n<e.length;s=n++){const r=e[n][0],o=e[n][1],a=e[s][0],c=e[s][1];o>i.y!=c>i.y&&i.x<(a-r)*(i.y-o)/(c-o)+r&&(t=!t)}return t}function hi(i,e,t,n,s){const r=i*i,o=r*i,a=1-i,c=a*a,h=c*a;return{x:h*e.x+3*c*i*t.x+3*a*r*n.x+o*s.x,y:h*e.y+3*c*i*t.y+3*a*r*n.y+o*s.y}}function di(i,e,t,n,s){let r=1/0,o=null;const a=20;for(let c=0;c<=a;c++){const h=c/a,l=hi(h,e,t,n,s),d=Q(X(l,i));d<r&&(r=d,o=l)}return o?{point:o,distance:r}:null}function ui(i,e,t){const n=e.geometry.coordinates[0];for(let s=0;s<n.length-1;s++){const[r,o]=n[s];if(Q(X({x:r,y:o},i))<t)return{point:{x:r,y:o},magnet:{x:r,y:o},type:P.POLYGON,id:e.id}}for(let s=0;s<n.length-1;s++){const[r,o]=n[s],[a,c]=n[s+1],h={x:r,y:o},d=X({x:a,y:c},h),u=Q(d),g={x:d.x/u,y:d.y/u},f={x:-g.y,y:g.x},p=X(i,h),m=Math.abs(vt(f,p)),x=vt(g,p);if(x>=0&&x<=u&&m<t){const v={x:h.x+g.x*x,y:h.y+g.y*x};return{point:v,magnet:v,type:P.POLYGON,id:e.id}}}return null}function gi(i,e,t){for(const s of e){const o=s.geometry.coordinates[0].slice(0,-1);if(o.length<3)return ui(i,s,t);for(let l=0;l<o.length;l++){const[d,u]=o[l];if(Q(X({x:d,y:u},i))<t)return{point:{x:d,y:u},magnet:{x:d,y:u},type:P.POLYGON,id:s.id}}const a=ci(i,o);let c=null,h=a?1/0:t;for(let l=0;l<o.length;l++){const d=o[(l-1+o.length)%o.length],u=o[l],g=o[(l+1)%o.length],f=o[(l+2)%o.length],p=u[0]+(g[0]-d[0])/6*.5,m=u[1]+(g[1]-d[1])/6*.5,x=g[0]-(f[0]-u[0])/6*.5,v=g[1]-(f[1]-u[1])/6*.5,S=di(i,{x:u[0],y:u[1]},{x:p,y:m},{x,y:v},{x:g[0],y:g[1]});if(S){const E=S.distance;if(a)E<h&&(h=E,c=S.point);else if(E<t)return{point:S.point,magnet:S.point,type:P.POLYGON,id:s.id}}}if(a&&c)return{point:c,magnet:c,type:P.POLYGON,id:s.id}}return null}const mn=[{x:-.5,y:-.5},{x:0,y:-.5},{x:.5,y:-.5},{x:-.5,y:0},{x:.5,y:0},{x:-.5,y:.5},{x:0,y:.5},{x:.5,y:.5},{x:0,y:0}],fi=[[-.5,-.5,.5,-.5],[.5,-.5,.5,.5],[.5,.5,-.5,.5],[-.5,.5,-.5,-.5]];function yn(i,e,t,n,s,r,o,a,c){const h=i*s,l=e*r;return c?{x:t+(h*o-l*a),y:n+(h*a+l*o)}:{x:t+h,y:n+l}}function pi(i,e,t,n,s){var l;const r=J(e);let{width:o,height:a}=Y(e);const{sin:c,cos:h}=s.getState();(l=e.properties.style)!=null&&l.fixedSize&&(o/=t,a/=t);for(const d of mn){let u=d.x*o,g=d.y*a;if(!W(e)){const m=u*h-g*c,x=u*c+g*h;u=m,g=x}const f={x:r.x+u,y:r.y+g};if(!(Q(X(f,i))>=n))return{point:{x:f.x,y:f.y},type:P.TEXT,magnet:d,id:e.id}}return null}function mi(i,e,t,n,s){var f;const{x:r,y:o}=J(e);let{width:a,height:c}=Y(e);(f=e.properties.style)!=null&&f.fixedSize&&(a/=t,c/=t);const{sin:h,cos:l}=s.getState(),d=!W(e);let u=null,g=1/0;for(let p=0;p<4;p++){const[m,x,v,S]=fi[p],E=yn(m,x,r,o,a,c,l,h,d),b=yn(v,S,r,o,a,c,l,h,d),A=X(b,E),T=Q(A),C={x:A.x/T,y:A.y/T},M={x:-C.y,y:C.x},F=X(i,E),j=vt(M,F),U=vt(C,F);if(U>=0&&U<=T){const R=Math.abs(j);if(R<n||R<g){const at=U/T,lt={point:{x:E.x+C.x*U,y:E.y+C.y*U},magnet:{x:m+(v-m)*at,y:x+(S-x)*at},type:P.TEXT,id:e.id};if(R<n)return lt;R<g&&(g=R,u=lt)}}}return u&&g<Math.max(a,c)?u:null}function yi(i,e,t,n,s){for(const r of e){const o=pi(i,r,t,n,s)||mi(i,r,t,n,s);if(o)return o}return null}class wi extends EventTarget{constructor(t,n,s,r){super();y(this,"ogma");y(this,"options");y(this,"spatialIndex");y(this,"hoveredNode",null);y(this,"store");this.ogma=t,this.options=n,this.spatialIndex=s,this.store=r}snap(t){const n=this.findMagnet(t);return n||null}findMagnet(t){const n=this.options.detectMargin,s={minX:t.x-n,minY:t.y-n,maxX:t.x+n,maxY:t.y+n},r=this.spatialIndex.search(s),o=r.filter(f=>L(f)||W(f)),a=r.filter(f=>it(f)),{nodes:c,edges:h}=this.ogma.view.getElementsInside(s.minX,s.minY,s.maxX,s.maxY),l=this.snapToNodes(t,c);if(l)return l;const d=this.snapToEdges(t,h);if(d)return d;const u=this.snapToText(t,o);if(u)return u;const g=this.snapToPolygon(t,a);return g||null}snapToText(t,n){const{zoom:s}=this.store.getState();return yi(t,n,s,this.options.magnetRadius,this.store)}snapToPolygon(t,n){return gi(t,n,this.options.magnetRadius)}snapToNodes(t,n){return li(t,n,this.options.detectMargin)}snapToEdges(t,n){return ai(t,n,this.options.magnetRadius)}getHoveredNode(){return this.hoveredNode}clearHoveredNode(){var t;(t=this.hoveredNode)==null||t.setSelected(!1),this.hoveredNode=null}getMagnets(){return mn}}class xi extends EventTarget{constructor(t,n,s){super();y(this,"query",{minX:1/0,minY:1/0,maxX:1/0,maxY:1/0});y(this,"suppressClickUntil",0);y(this,"mouseDownState",null);y(this,"DRAG_THRESHOLD",3);y(this,"onMouseMove",t=>{const n=Lt(t,this.ogma.getContainer()),s=this.store.getState();if(this.mouseDownState&&!s.isDragging){const l=t.clientX-this.mouseDownState.screenX,d=t.clientY-this.mouseDownState.screenY;Math.sqrt(l*l+d*d)>this.DRAG_THRESHOLD&&(this.mouseDownState.hasMoved=!0)}if(s.isDragging)return;const{x:r,y:o}=this.ogma.view.screenToGraphCoordinates(n),a=this.detect(r,o),c=(a==null?void 0:a.id)??null,h=s.hoveredFeature;c!==h&&s.setHoveredFeature(c),this.setCursor(c===null?H.default:H.pointer)});y(this,"onMouseClick",()=>{Date.now()<this.suppressClickUntil});y(this,"onMouseDown",t=>{if(Date.now()<this.suppressClickUntil)return;const n=Lt(t,this.ogma.getContainer()),{x:s,y:r}=this.ogma.view.screenToGraphCoordinates(n),o=this.detect(s,r);this.mouseDownState={annotation:o,screenX:t.clientX,screenY:t.clientY,hasMoved:!1};const a=this.store.getState();this.store.setState({mousePressed:!0,mousePressPoint:{x:s,y:r}}),o&&!a.selectedFeatures.has(o.id)&&(t.ctrlKey||t.metaKey?a.toggleSelection(o.id):a.setSelectedFeatures([o.id]))});y(this,"onMouseUp",t=>{const n=this.store.getState();if(this.store.setState({mousePressed:!1,mousePressPoint:null}),this.mouseDownState&&!this.mouseDownState.hasMoved&&!n.isDragging){const s=this.mouseDownState.annotation;s?t.ctrlKey||t.metaKey?n.toggleSelection(s.id):n.selectedFeatures.has(s.id)||n.setSelectedFeatures([s.id]):!t.ctrlKey&&!t.metaKey&&n.clearSelection(),this.dispatchEvent(new CustomEvent(Ot,{detail:{id:(s==null?void 0:s.id)??null,position:{x:t.clientX,y:t.clientY}}}))}this.mouseDownState=null});y(this,"onWheel",t=>{var d;if(t.target instanceof HTMLTextAreaElement)return;const n=Lt(t,this.ogma.getContainer()),{x:s,y:r}=this.ogma.view.screenToGraphCoordinates(n),o=this.detect(s,r);if(!o||!D(o))return;const a=(d=o.properties.style)==null?void 0:d.maxHeight,c=o.properties.height;if(!a||c<=a)return;const h=this.ogma.getContainer(),l=h==null?void 0:h.querySelector(`[data-annotation="${o.id}"] .comment-box foreignObject div`);l&&(t.stopPropagation(),t.preventDefault(),l.scrollTop+=t.deltaY)});this.ogma=t,this.store=n,this.index=s;const r=this.ogma.getContainer();r&&(r.addEventListener("mousemove",this.onMouseMove,{passive:!0,capture:!0}),r.addEventListener("click",this.onMouseClick,{passive:!0,capture:!0}),r.addEventListener("mousedown",this.onMouseDown,{passive:!0,capture:!1}),r.addEventListener("mouseup",this.onMouseUp,{passive:!0,capture:!0}),r.addEventListener("wheel",this.onWheel,{passive:!1,capture:!0}))}detect(t,n,s){let r=null;const o=this.store.getState(),a=s??o.options.detectMargin;this.query.minX=t-a,this.query.minY=n-a,this.query.maxX=t+a,this.query.maxY=n+a;const c=this.index.query(this.query);if(c.length===0)return null;for(const h of c){const l=o.getFeature(h.id);if(tt(l)){if(me(l,{x:t,y:n},a)){r=l;break}}else if(W(l)){if(on(l,{x:t,y:n},0,1,a)){r=l;break}}else if(it(l)){if(ve(l,{x:t,y:n},a)){r=l;break}}else if(D(l)){if(an(l,{x:t,y:n},a,o.sin,o.cos,o.zoom)){r=l;break}}else if(L(l)&&xe(l,{x:t,y:n},a,o.revSin,o.revCos,o.zoom)){r=l;break}}return r}setCursor(t){var s;const n=(s=this.ogma.getContainer())==null?void 0:s.firstChild;n&&(n.style.cursor=t)}setMode(t){var n;t==="default"?this.setCursor(((n=this.ogma.getOptions().cursor)==null?void 0:n.default)||"default"):t==="add"||t==="link"?this.setCursor(H.crosshair):t==="rotate"&&this.setCursor(H.alias)}suppressClicksTemporarily(t=100){this.suppressClickUntil=Date.now()+t}destroy(){const t=this.ogma.getContainer();t&&(t.removeEventListener("mousemove",this.onMouseMove),t.removeEventListener("click",this.onMouseClick))}}function wn(i,e,t=0,n=i.length-1,s=bi){for(;n>t;){if(n-t>600){const c=n-t+1,h=e-t+1,l=Math.log(c),d=.5*Math.exp(2*l/3),u=.5*Math.sqrt(l*d*(c-d)/c)*(h-c/2<0?-1:1),g=Math.max(t,Math.floor(e-h*d/c+u)),f=Math.min(n,Math.floor(e+(c-h)*d/c+u));wn(i,e,g,f,s)}const r=i[e];let o=t,a=n;for(Ht(i,t,e),s(i[n],r)>0&&Ht(i,t,n);o<a;){for(Ht(i,o,a),o++,a--;s(i[o],r)<0;)o++;for(;s(i[a],r)>0;)a--}s(i[t],r)===0?Ht(i,t,a):(a++,Ht(i,a,n)),a<=e&&(t=a+1),e<=a&&(n=a-1)}}function Ht(i,e,t){const n=i[e];i[e]=i[t],i[t]=n}function bi(i,e){return i<e?-1:i>e?1:0}class vi{constructor(e=9){this._maxEntries=Math.max(4,e),this._minEntries=Math.max(2,Math.ceil(this._maxEntries*.4)),this.clear()}all(){return this._all(this.data,[])}search(e){let t=this.data;const n=[];if(!Kt(e,t))return n;const s=this.toBBox,r=[];for(;t;){for(let o=0;o<t.children.length;o++){const a=t.children[o],c=t.leaf?s(a):a;Kt(e,c)&&(t.leaf?n.push(a):De(e,c)?this._all(a,n):r.push(a))}t=r.pop()}return n}collides(e){let t=this.data;if(!Kt(e,t))return!1;const n=[];for(;t;){for(let s=0;s<t.children.length;s++){const r=t.children[s],o=t.leaf?this.toBBox(r):r;if(Kt(e,o)){if(t.leaf||De(e,o))return!0;n.push(r)}}t=n.pop()}return!1}load(e){if(!(e&&e.length))return this;if(e.length<this._minEntries){for(let n=0;n<e.length;n++)this.insert(e[n]);return this}let t=this._build(e.slice(),0,e.length-1,0);if(!this.data.children.length)this.data=t;else if(this.data.height===t.height)this._splitRoot(this.data,t);else{if(this.data.height<t.height){const n=this.data;this.data=t,t=n}this._insert(t,this.data.height-t.height-1,!0)}return this}insert(e){return e&&this._insert(e,this.data.height-1),this}clear(){return this.data=At([]),this}remove(e,t){if(!e)return this;let n=this.data;const s=this.toBBox(e),r=[],o=[];let a,c,h;for(;n||r.length;){if(n||(n=r.pop(),c=r[r.length-1],a=o.pop(),h=!0),n.leaf){const l=Si(e,n.children,t);if(l!==-1)return n.children.splice(l,1),r.push(n),this._condense(r),this}!h&&!n.leaf&&De(n,s)?(r.push(n),o.push(a),a=0,c=n,n=n.children[0]):c?(a++,n=c.children[a],h=!1):n=null}return this}toBBox(e){return e}compareMinX(e,t){return e.minX-t.minX}compareMinY(e,t){return e.minY-t.minY}toJSON(){return this.data}fromJSON(e){return this.data=e,this}_all(e,t){const n=[];for(;e;)e.leaf?t.push(...e.children):n.push(...e.children),e=n.pop();return t}_build(e,t,n,s){const r=n-t+1;let o=this._maxEntries,a;if(r<=o)return a=At(e.slice(t,n+1)),Et(a,this.toBBox),a;s||(s=Math.ceil(Math.log(r)/Math.log(o)),o=Math.ceil(r/Math.pow(o,s-1))),a=At([]),a.leaf=!1,a.height=s;const c=Math.ceil(r/o),h=c*Math.ceil(Math.sqrt(o));xn(e,t,n,h,this.compareMinX);for(let l=t;l<=n;l+=h){const d=Math.min(l+h-1,n);xn(e,l,d,c,this.compareMinY);for(let u=l;u<=d;u+=c){const g=Math.min(u+c-1,d);a.children.push(this._build(e,u,g,s-1))}}return Et(a,this.toBBox),a}_chooseSubtree(e,t,n,s){for(;s.push(t),!(t.leaf||s.length-1===n);){let r=1/0,o=1/0,a;for(let c=0;c<t.children.length;c++){const h=t.children[c],l=Pe(h),d=Ci(e,h)-l;d<o?(o=d,r=l<r?l:r,a=h):d===o&&l<r&&(r=l,a=h)}t=a||t.children[0]}return t}_insert(e,t,n){const s=n?e:this.toBBox(e),r=[],o=this._chooseSubtree(s,this.data,t,r);for(o.children.push(e),Bt(o,s);t>=0&&r[t].children.length>this._maxEntries;)this._split(r,t),t--;this._adjustParentBBoxes(s,r,t)}_split(e,t){const n=e[t],s=n.children.length,r=this._minEntries;this._chooseSplitAxis(n,r,s);const o=this._chooseSplitIndex(n,r,s),a=At(n.children.splice(o,n.children.length-o));a.height=n.height,a.leaf=n.leaf,Et(n,this.toBBox),Et(a,this.toBBox),t?e[t-1].children.push(a):this._splitRoot(n,a)}_splitRoot(e,t){this.data=At([e,t]),this.data.height=e.height+1,this.data.leaf=!1,Et(this.data,this.toBBox)}_chooseSplitIndex(e,t,n){let s,r=1/0,o=1/0;for(let a=t;a<=n-t;a++){const c=It(e,0,a,this.toBBox),h=It(e,a,n,this.toBBox),l=Ti(c,h),d=Pe(c)+Pe(h);l<r?(r=l,s=a,o=d<o?d:o):l===r&&d<o&&(o=d,s=a)}return s||n-t}_chooseSplitAxis(e,t,n){const s=e.leaf?this.compareMinX:Ei,r=e.leaf?this.compareMinY:Ai,o=this._allDistMargin(e,t,n,s),a=this._allDistMargin(e,t,n,r);o<a&&e.children.sort(s)}_allDistMargin(e,t,n,s){e.children.sort(s);const r=this.toBBox,o=It(e,0,t,r),a=It(e,n-t,n,r);let c=Zt(o)+Zt(a);for(let h=t;h<n-t;h++){const l=e.children[h];Bt(o,e.leaf?r(l):l),c+=Zt(o)}for(let h=n-t-1;h>=t;h--){const l=e.children[h];Bt(a,e.leaf?r(l):l),c+=Zt(a)}return c}_adjustParentBBoxes(e,t,n){for(let s=n;s>=0;s--)Bt(t[s],e)}_condense(e){for(let t=e.length-1,n;t>=0;t--)e[t].children.length===0?t>0?(n=e[t-1].children,n.splice(n.indexOf(e[t]),1)):this.clear():Et(e[t],this.toBBox)}}function Si(i,e,t){if(!t)return e.indexOf(i);for(let n=0;n<e.length;n++)if(t(i,e[n]))return n;return-1}function Et(i,e){It(i,0,i.children.length,e,i)}function It(i,e,t,n,s){s||(s=At(null)),s.minX=1/0,s.minY=1/0,s.maxX=-1/0,s.maxY=-1/0;for(let r=e;r<t;r++){const o=i.children[r];Bt(s,i.leaf?n(o):o)}return s}function Bt(i,e){return i.minX=Math.min(i.minX,e.minX),i.minY=Math.min(i.minY,e.minY),i.maxX=Math.max(i.maxX,e.maxX),i.maxY=Math.max(i.maxY,e.maxY),i}function Ei(i,e){return i.minX-e.minX}function Ai(i,e){return i.minY-e.minY}function Pe(i){return(i.maxX-i.minX)*(i.maxY-i.minY)}function Zt(i){return i.maxX-i.minX+(i.maxY-i.minY)}function Ci(i,e){return(Math.max(e.maxX,i.maxX)-Math.min(e.minX,i.minX))*(Math.max(e.maxY,i.maxY)-Math.min(e.minY,i.minY))}function Ti(i,e){const t=Math.max(i.minX,e.minX),n=Math.max(i.minY,e.minY),s=Math.min(i.maxX,e.maxX),r=Math.min(i.maxY,e.maxY);return Math.max(0,s-t)*Math.max(0,r-n)}function De(i,e){return i.minX<=e.minX&&i.minY<=e.minY&&e.maxX<=i.maxX&&e.maxY<=i.maxY}function Kt(i,e){return e.minX<=i.maxX&&e.minY<=i.maxY&&e.maxX>=i.minX&&e.maxY>=i.minY}function At(i){return{children:i,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function xn(i,e,t,n,s){const r=[e,t];for(;r.length;){if(t=r.pop(),e=r.pop(),t-e<=n)continue;const o=e+Math.ceil((t-e)/n/2)*n;wn(i,o,e,t,s),r.push(e,o,o,t)}}const zt={minX:0,minY:0,maxX:0,maxY:0},bn=(i,e)=>i.id===e.id;class ki extends vi{constructor(t){super();y(this,"store");y(this,"onRotationChange",()=>{const t=this.store.getState().getAllFeatures().filter(n=>L(n)||D(n));for(const n of t)this.updateRotatedText(n)});y(this,"onZoomChange",()=>{const t=this.store.getState().getAllFeatures().filter(n=>{var s;return(L(n)||D(n))&&((s=n.properties.style)==null?void 0:s.fixedSize)});for(const n of t)this.updateRotatedText(n)});this.store=t,this.store.subscribe(n=>n.features,n=>{this.clear(),Object.values(n).forEach(s=>{L(s)||D(s)?this.updateRotatedText(s):this.insert(s)})}),this.store.subscribe(n=>({features:n.features,isDragging:n.isDragging,lastChangedFeatures:n.lastChangedFeatures,rotation:n.rotation}),(n,s)=>{s&&s.isDragging&&!n.isDragging&&n.lastChangedFeatures.length>0&&n.lastChangedFeatures.forEach(r=>{const o=n.features[r];o&&(ft(o),L(o)||D(o)?this.updateRotatedText(o):(this.remove(o,bn),this.insert(o)))})},{equalityFn:(n,s)=>n.isDragging===s.isDragging}),this.store.subscribe(n=>n.rotation,this.onRotationChange),this.store.subscribe(n=>n.zoom,this.onZoomChange)}updateRotatedText(t){var d;const n=this.store.getState();this.remove(t,bn);const s=J(t);let{width:r,height:o}=Y(t);if(((d=t.properties.style)==null?void 0:d.fixedSize)===!0){const u=n.invZoom;r*=u,o*=u}const c=r/2,h=o/2,l=n.getRotatedBBox(s.x-c,s.y-h,s.x+c,s.y+h);this.insert({...t,geometry:{...t.geometry,bbox:l.slice()}})}compareMinX(t,n){return st(t)[0]-st(n)[0]}compareMinY(t,n){return st(t)[1]-st(n)[1]}toBBox(t){const n=st(t);return zt.minX=n[0],zt.minY=n[1],zt.maxX=n[2],zt.maxY=n[3],zt}query(t){return super.search(t)}}class vn{constructor(e,t){y(this,"layer");this.ogma=e,this.store=t}destroy(){}}function Mi(i,e,t,n,s,r){const o=Math.min(n/2,s/2),a=Math.min(r,o);i.beginPath(),i.moveTo(e+a,t),i.lineTo(e+n-a,t),i.quadraticCurveTo(e+n,t,e+n,t+a),i.lineTo(e+n,t+s-a),i.quadraticCurveTo(e+n,t+s,e+n-a,t+s),i.lineTo(e+a,t+s),i.quadraticCurveTo(e,t+s,e,t+s-a),i.lineTo(e,t+a),i.quadraticCurveTo(e,t,e+a,t),i.closePath()}const Sn=[[-.5,-.5],[.5,-.5],[.5,.5],[-.5,.5]];class Pi extends vn{constructor(t,n){super(t,n);y(this,"handleFill","#fff");y(this,"handleStroke","#1A70E5");y(this,"handleStrokeWidth",1.5);y(this,"handleRadius",3);y(this,"handleMagnifier",1.5);y(this,"refresh",()=>{this.layer.refresh()});y(this,"render",t=>{const n=this.store.getState(),s=1/n.zoom,{hoveredHandle:r,rotation:o,liveUpdates:a,features:c}=n,h=this.handleRadius*s;t.fillStyle=this.handleFill,t.lineWidth=this.handleStrokeWidth*s,t.strokeStyle=this.handleStroke,Object.values(c).forEach(l=>{if(!n.isSelected(l.id))return;const d=a[l.id]?{...l,...a[l.id]}:l;if(tt(d))this.renderArrowHandles(d,t,h,r);else if(W(d)||L(d)){const u=L(d)?o:0;this.renderOutline(d,t,u,r,n.zoom),this.renderBoxHandles(d,t,h,r,u)}else it(d)&&this.renderPolygonHandles(d,t,h,r,n.drawingFeature===d.id)})});this.layer=t.layers.addCanvasLayer(this.render,void 0,Xt.HANDLES),this.store.subscribe(s=>({selectedFeatures:s.selectedFeatures,liveUpdates:s.liveUpdates}),this.refresh),t.events.on("zoom",this.refresh)}renderOutline(t,n,s,r,o){var m,x;const a=L(t)&&((m=t.properties.style)==null?void 0:m.fixedSize);let{width:c,height:h}=Y(t);a&&(c/=o,h/=o);const l=gt(t,a,o),d=c/2,u=h/2,g=l.x+d,f=l.y+u;n.save(),n.translate(g,f),n.rotate(s);const p=(x=t.properties.style)==null?void 0:x.borderRadius;if(p&&p>0?(Mi(n,-d,-u,c,h,p),n.stroke()):n.strokeRect(-d,-u,c,h),r>=4&&r<8){let v,S,E,b;const A=p&&p>0?p:this.handleRadius/o;switch(r){case 4:v=-d+A,S=-u,E=d-A,b=-u;break;case 5:v=d,S=-u+A,E=d,b=u-A;break;case 6:v=d-A,S=u,E=-d+A,b=u;break;case 7:v=-d,S=u-A,E=-d,b=-u+A;break}n.lineWidth=this.handleStrokeWidth*this.handleMagnifier/o,n.beginPath(),n.moveTo(v,S),n.lineTo(E,b),n.stroke(),n.lineWidth=this.handleStrokeWidth}n.restore()}renderArrowHandles(t,n,s,r){var u,g,f,p;const o=de(t),a=ue(t),c=+(r===0),h=+(r===1),l=s*(1+(this.handleMagnifier-1)*c),d=s*(1+(this.handleMagnifier-1)*h);n.beginPath(),n.moveTo(o.x,o.y),n.lineTo(a.x,a.y),n.closePath(),n.stroke(),n.beginPath(),((g=(u=t.properties.link)==null?void 0:u.start)==null?void 0:g.type)!=="comment"&&(n.moveTo(o.x-l,o.y-l),n.rect(o.x-l,o.y-l,l*2,l*2)),((p=(f=t.properties.link)==null?void 0:f.end)==null?void 0:p.type)!=="comment"&&(n.moveTo(a.x-d,a.y-d),n.rect(a.x-d,a.y-d,d*2,d*2)),n.closePath(),n.fill(),n.stroke()}renderBoxHandles(t,n,s,r,o){var f;if(L(t)&&((f=t.properties.style)!=null&&f.fixedSize))return;const{width:a,height:c}=Y(t),h=gt(t),l=a/2,d=c/2,u=h.x+l,g=h.y+d;n.save(),n.translate(u,g),n.rotate(o),n.beginPath();for(let p=0;p<Sn.length;p++){const[m,x]=Sn[p],v=a*m,S=c*x,E=r===p?s*this.handleMagnifier:s;n.rect(v-E,S-E,E*2,E*2)}n.closePath(),n.fill(),n.stroke(),n.restore()}renderPolygonHandles(t,n,s,r,o){const a=t.geometry.coordinates[0];if(!o){a.length>2&&(n.save(),n.strokeStyle=this.handleStroke,n.lineWidth=this.handleStrokeWidth/this.store.getState().zoom,this.drawSmoothPolygon(n,a,.5),n.stroke(),n.restore()),n.beginPath();for(let c=0;c<a.length-1;c++){const[h,l]=a[c],d=r===c?s*this.handleMagnifier:s;n.moveTo(h+d,l),n.arc(h,l,d,0,2*Math.PI)}n.closePath(),n.fill(),n.stroke()}}drawSmoothPolygon(t,n,s){const r=n.slice(0,-1);if(r.length<3)return;t.beginPath();const o=r.length;for(let a=0;a<o;a++){const c=r[(a-1+o)%o],h=r[a],l=r[(a+1)%o],d=r[(a+2)%o],u=h[0]+(l[0]-c[0])/6*s,g=h[1]+(l[1]-c[1])/6*s,f=l[0]-(d[0]-h[0])/6*s,p=l[1]-(d[1]-h[1])/6*s;a===0&&t.moveTo(h[0],h[1]),t.bezierCurveTo(u,g,f,p,l[0],l[1])}t.closePath()}renderCenterPoint(t,n){var r;const s=this.store.getState();if(L(t)){const o=gt(t,(r=t.properties.style)==null?void 0:r.fixedSize,s.zoom),a=Y(t);n.moveTo(o.x+a.width/2+5,o.y+a.height/2),n.arc(o.x+a.width/2,o.y+a.height/2,5,0,Math.PI*2,!0)}}destroy(){this.layer.destroy(),super.destroy()}}const Di=.5;function Fi(i,e,t,n){const{start:s,end:r}=Ut(i),o=X(r,s);return Math.min(n,Math.max(2*e,Q(o)*.01,t))}function En(i,e){return!i||i==="none"?0:i==="dot"?e*2:i==="halo-dot"?e*4:i==="arrow"||i==="arrow-plain"?e:0}function Li(i,e){if(!i){i=k("g");const t=k("path");i.classList.add("annotation-arrow"),i.setAttribute("data-annotation",`${e}`),i.setAttribute("data-annotation-type","arrow"),i.appendChild(t);const n=k("g");n.classList.add("annotation-arrow-endpoints"),i.appendChild(n)}return i}function _i(i,e,t,n,s,r){const{start:o,end:a}=Ut(e),c=e.properties.style||mt,{tail:h,strokeWidth:l=0,head:d,strokeColor:u,strokeType:g}=c,f=X(a,o),p=dn(e)?1/r.zoom:1,m=Fi(e,l,t,n)*p,x=Li(s,e.id),v=x.firstChild,S=u||"none";v.setAttribute("stroke",S),v.setAttribute("stroke-width",`${p*l}`),v.setAttribute("fill","none"),v.setAttribute("stroke-linecap","round"),v.setAttribute("stroke-linejoin","round");const E=En(h,l*p),b=En(d,l*p),A=Wt(f),T=pt(o,ct(A,E)),C=pt(a,ct(A,-b)),M=`M ${T.x} ${T.y} L ${C.x} ${C.y}`;v.setAttribute("d",M);const F=x.children[1];return F.innerHTML="",g==="dashed"?v.setAttribute("stroke-dasharray","5,10"):v.removeAttribute("stroke-dasharray"),An(F,o,f,S,h,l,p,m),An(F,a,Es(f),S,d,l,p,m),x.setAttribute("transform",`rotate(${-r.rotation*(180/Math.PI)})`),i.appendChild(x),x}function An(i,e,t,n,s,r,o,a){s==="halo-dot"&&Cn(i,e,Hi(n),Di,r*o*4),(s==="dot"||s==="halo-dot")&&Cn(i,e,n,1,r*o*2),(s==="arrow"||s==="arrow-plain")&&Ii(i,e,t,n,s==="arrow-plain",r*o,a)}function Hi(i){return i==="none"?"none":i}function Cn(i,e,t,n,s){const r=k("circle");r.setAttribute("cx",`${e.x}`),r.setAttribute("cy",`${e.y}`),r.setAttribute("r",`${s}`),r.setAttribute("fill-opacity",`${n}`),r.setAttribute("fill",t),i.appendChild(r)}function Ii(i,e,t,n,s,r,o){const a=ct(Wt(t),o),c=pt(e,rn(a,Math.PI/6)),h=pt(e,rn(a,-Math.PI/6)),l=k("path"),d=`${e.x} ${e.y}`,u=`M ${c.x} ${c.y} L ${d} L ${h.x} ${h.y}${s?" Z":""}`;l.setAttribute("d",u),l.setAttribute("stroke",n),l.setAttribute("stroke-width",`${r}`),l.setAttribute("fill",s?n:"none"),l.setAttribute("stroke-linecap","round"),l.setAttribute("stroke-linejoin","round"),i.appendChild(l)}function Bi(i,e,t="box"){if(!i){i=k("g"),i.setAttribute("data-annotation",`${e}`),i.setAttribute("data-annotation-type",t),i.classList.add(`annotation-${t}`);const n=k("rect");i.appendChild(n)}return i}function Tn(i,e,t,n){const s=e.id,r=`class${s}`,o=Y(e),a=Bi(t,s,e.properties.type),{strokeColor:c,strokeWidth:h,strokeType:l,background:d,borderRadius:u,boxShadow:g}=e.properties.style||St;a.classList.add("annotation-box"),a.setAttribute("fill",`${d||"transparent"}`);const f=a.firstChild;u&&(f.setAttribute("rx",`${u}`),f.setAttribute("ry",`${u}`)),l&&l!=="none"&&(f.setAttribute("stroke",c||"black"),f.setAttribute("stroke-width",`${h}`),l==="dashed"&&f.setAttribute("stroke-dasharray","5,5")),d&&d.length&&(n.hoveredFeature===e.id?f.setAttribute("fill",bt(d)):f.setAttribute("fill",d));const p=gt(e);return f.setAttribute("width",`${o.width}`),f.setAttribute("height",`${o.height}`),f.setAttribute("x",`${p.x}`),f.setAttribute("y",`${p.y}`),a.appendChild(f),g?a.style.filter=`drop-shadow(${g})`:a.style.filter="",a.setAttribute("transform",n.getRotationTransform(0,0)),a.classList.add(r),a.setAttribute("data-annotation",`${e.id}`),a.setAttribute("data-annotation-type",e.properties.type),i.appendChild(a),a}let Fe=null;function kn(){return Fe||(Fe=document.createElement("canvas").getContext("2d")),Fe}function Mn(i,e,t,n,s){if(!i||i.length===0)return!0;if(i.includes(`
12
12
  `))return!1;const r=kn();r.font=`${t}px ${e}`;const o=r.measureText(i).width,a=n-s*2;return o<=a}function zi(i,e,t,n,s){if(!i||i.length===0)return 60;const r=kn();r.font=`${t}px ${e}`;const o=i.split(`
13
13
  `);let a=0;for(const h of o){if(h.length===0)continue;const l=h.split(/\s+/);let d=0;const u=r.measureText(" ").width,g=n-s*2;for(let f=0;f<l.length;f++){const p=l[f],m=r.measureText(p).width;d===0?d=m:d+u+m<=g?d+=u+m:(a=Math.max(a,d),d=m)}a=Math.max(a,d)}const c=a+s*2+4;return Math.min(c,n)}function Ri(){const i=k("style");return i.textContent=`
14
14
  /* Comment Animation Styles */
package/dist/index.mjs CHANGED
@@ -681,7 +681,7 @@ function ln(i, e, t = 0, n = 0, s = 1, r = 1) {
681
681
  const c = l / 2, d = h / 2, u = e.x - o, g = e.y - a, f = u * s - g * n, p = u * n + g * s;
682
682
  return f > -c - t && f < c + t && p > -d - t && p < d + t;
683
683
  }
684
- const F = (i) => i.properties.type === "comment", V = {
684
+ const P = (i) => i.properties.type === "comment", V = {
685
685
  // Box styling
686
686
  background: "#FFFACD",
687
687
  // Light yellow (sticky note color)
@@ -919,7 +919,7 @@ class ds {
919
919
  */
920
920
  toggleComment(e) {
921
921
  const t = this.store.getState().getFeature(e);
922
- if (!t || !F(t)) return;
922
+ if (!t || !P(t)) return;
923
923
  const n = t;
924
924
  this.store.getState().applyLiveUpdate(e, {
925
925
  properties: {
@@ -938,7 +938,7 @@ class ds {
938
938
  this.previousZoom = e;
939
939
  const t = this.store.getState(), n = t.features, s = {};
940
940
  Object.values(n).forEach((r) => {
941
- if (F(r)) {
941
+ if (P(r)) {
942
942
  const o = r, a = this.getCommentZoomThreshold(o), l = e < a ? at : Rt;
943
943
  if (o.properties.mode === l) return;
944
944
  s[o.id] = {
@@ -1105,7 +1105,7 @@ class us extends qt {
1105
1105
  y(this, "startX", 0);
1106
1106
  y(this, "startY", 0);
1107
1107
  y(this, "onArrowComplete", () => {
1108
- var w;
1108
+ var v;
1109
1109
  this.arrowHandler.removeEventListener("dragend", this.onArrowComplete);
1110
1110
  const t = this.store.getState(), n = Array.from(t.selectedFeatures);
1111
1111
  if (n.length === 0) return;
@@ -1116,15 +1116,15 @@ class us extends qt {
1116
1116
  h < 5 ? (c = this.startX + this.offsetX, d = this.startY + this.offsetY) : (c = o[0], d = o[1]);
1117
1117
  const u = this.comment;
1118
1118
  u.geometry.coordinates = [c, d], t.addFeature(u), this.store.setState({ drawingFeature: u.id });
1119
- const g = u.properties.height, f = c, p = d + g * 0.5;
1119
+ const g = u.properties.height, f = t.zoom, p = g / f, m = c, w = d + p * 0.5;
1120
1120
  this.snapArrowStart(s, r[0], r[1]);
1121
- const m = (w = s.properties.link) == null ? void 0 : w.start;
1121
+ const b = (v = s.properties.link) == null ? void 0 : v.start;
1122
1122
  t.updateFeature(s.id, {
1123
1123
  ...s,
1124
1124
  geometry: {
1125
1125
  ...s.geometry,
1126
1126
  coordinates: [
1127
- [f, p],
1127
+ [m, w],
1128
1128
  // Start: comment bottom edge
1129
1129
  r
1130
1130
  // End: original mousedown point (with any snapping from ArrowHandler)
@@ -1141,23 +1141,23 @@ class us extends qt {
1141
1141
  },
1142
1142
  // If ArrowHandler snapped to something at the original start point,
1143
1143
  // that becomes the end point now
1144
- end: m ? {
1144
+ end: b ? {
1145
1145
  side: it,
1146
- id: m.id,
1147
- type: m.type,
1148
- magnet: m.magnet
1146
+ id: b.id,
1147
+ type: b.type,
1148
+ magnet: b.magnet
1149
1149
  } : void 0
1150
1150
  }
1151
1151
  }
1152
1152
  }), this.links.add(s, X, u.id, T.COMMENT, {
1153
1153
  x: 0,
1154
1154
  y: 0.5
1155
- }), m && m.magnet && this.links.add(
1155
+ }), b && b.magnet && this.links.add(
1156
1156
  s,
1157
1157
  it,
1158
- m.id,
1159
- m.type,
1160
- m.magnet
1158
+ b.id,
1159
+ b.type,
1160
+ b.magnet
1161
1161
  ), t.setSelectedFeatures([u.id]), this.store.setState({ drawingFeature: null });
1162
1162
  });
1163
1163
  this.links = s, this.arrowHandler = o, this.snapping = r, this.offsetX = (l == null ? void 0 : l.offsetX) ?? 100, this.offsetY = (l == null ? void 0 : l.offsetY) ?? -50, this.arrowStyle = l == null ? void 0 : l.arrowStyle, this.comment = a;
@@ -1197,14 +1197,14 @@ function gs(i, e, t, n, s) {
1197
1197
  const b = w % a, v = Math.floor(w / a), S = 2 * Math.PI * (b / a), x = f + v / l * (g - f), A = Math.cos(S) * x, C = Math.sin(S) * x, E = m.x + A, k = m.y + C;
1198
1198
  if (p.minX = E, p.minY = k - u / 2, p.maxX = E + d, p.maxY = k + u / 2, A < 0 && (p.minX -= d, p.maxX -= d), p.minX < 0 || p.maxX > r || p.minY < 0 || p.maxY > o)
1199
1199
  continue;
1200
- const P = n.view.screenToGraphCoordinates({
1200
+ const F = n.view.screenToGraphCoordinates({
1201
1201
  x: p.minX,
1202
1202
  y: p.minY
1203
1203
  }), O = n.view.screenToGraphCoordinates({
1204
1204
  x: p.maxX,
1205
1205
  y: p.maxY
1206
1206
  });
1207
- if (p.minX = P.x, p.minY = P.y, p.maxX = O.x, p.maxY = O.y, !t.query(p).filter((B) => B.properties.type !== "arrow").length)
1207
+ if (p.minX = F.x, p.minY = F.y, p.maxX = O.x, p.maxY = O.y, !t.query(p).filter((B) => B.properties.type !== "arrow").length)
1208
1208
  return n.view.screenToGraphCoordinates({ x: E, y: k });
1209
1209
  }
1210
1210
  return n.view.screenToGraphCoordinates({ x: r / 2, y: o / 2 });
@@ -1670,7 +1670,7 @@ function Pt(i) {
1670
1670
  return i !== T.NODE && i !== T.EDGE;
1671
1671
  }
1672
1672
  function bs(i, e) {
1673
- if (_(i) || W(i) || F(i)) {
1673
+ if (_(i) || W(i) || P(i)) {
1674
1674
  const t = Q(i);
1675
1675
  return {
1676
1676
  geometry: {
@@ -2085,7 +2085,7 @@ class As {
2085
2085
  });
2086
2086
  var u, g, f;
2087
2087
  this.ogma = e, this.store = t, this.annotation = n, this.onSendHandler = s;
2088
- const r = this.getPosition(), o = this.getSize(), a = this.getAnnotation(), l = this.store.getState(), h = F(a) && (((u = l.options) == null ? void 0 : u.showSendButton) ?? !0), c = ((g = l.options) == null ? void 0 : g.sendButtonIcon) || "", d = ((f = l.options) == null ? void 0 : f.textPlaceholder) || "Enter text";
2088
+ const r = this.getPosition(), o = this.getSize(), a = this.getAnnotation(), l = this.store.getState(), h = P(a) && (((u = l.options) == null ? void 0 : u.showSendButton) ?? !0), c = ((g = l.options) == null ? void 0 : g.sendButtonIcon) || "", d = ((f = l.options) == null ? void 0 : f.textPlaceholder) || "Enter text";
2089
2089
  this.layer = this.ogma.layers.addOverlay(
2090
2090
  {
2091
2091
  element: `<div class="ogma-annotation-text-editor">
@@ -2132,7 +2132,7 @@ class As {
2132
2132
  var g;
2133
2133
  const e = this.getAnnotation();
2134
2134
  if (!e) return { width: 0, height: 0 };
2135
- const t = q(e), n = Vt(e), s = e.properties.style, r = (s == null ? void 0 : s.fixedSize) || !1, o = s == null ? void 0 : s.maxHeight, a = this.store.getState().zoom, l = this.store.getState(), h = F(e) && (((g = l.options) == null ? void 0 : g.showSendButton) ?? !0), c = r ? 1 / a : 1;
2135
+ const t = q(e), n = Vt(e), s = e.properties.style, r = (s == null ? void 0 : s.fixedSize) || !1, o = s == null ? void 0 : s.maxHeight, a = this.store.getState().zoom, l = this.store.getState(), h = P(e) && (((g = l.options) == null ? void 0 : g.showSendButton) ?? !0), c = r ? 1 / a : 1;
2136
2136
  let d = (t.height - n * 2) * c;
2137
2137
  if (o) {
2138
2138
  const f = (o - n * 2) * c;
@@ -2148,7 +2148,7 @@ class As {
2148
2148
  var b;
2149
2149
  const e = this.getAnnotation();
2150
2150
  if (!e) return;
2151
- const t = F(e) ? V : G, {
2151
+ const t = P(e) ? V : G, {
2152
2152
  font: n,
2153
2153
  fontSize: s = t.fontSize,
2154
2154
  borderRadius: r,
@@ -2269,7 +2269,7 @@ class Zt extends qt {
2269
2269
  y(this, "onClick", (t) => {
2270
2270
  const n = this.getAnnotation();
2271
2271
  if (n) {
2272
- if (F(n) && n.properties.mode === at) {
2272
+ if (P(n) && n.properties.mode === at) {
2273
2273
  this.store.getState().updateFeature(n.id, {
2274
2274
  properties: {
2275
2275
  ...n.properties,
@@ -2279,11 +2279,11 @@ class Zt extends qt {
2279
2279
  return;
2280
2280
  }
2281
2281
  if (this.store.getState().selectedFeatures.has(n.id)) {
2282
- if (F(n) && this.justActivated) {
2282
+ if (P(n) && this.justActivated) {
2283
2283
  this.justActivated = !1;
2284
2284
  return;
2285
2285
  }
2286
- n && (_(n) || F(n)) && this.startEditingText();
2286
+ n && (_(n) || P(n)) && this.startEditingText();
2287
2287
  }
2288
2288
  }
2289
2289
  });
@@ -2298,8 +2298,8 @@ class Zt extends qt {
2298
2298
  let { width: a, height: l } = q(s);
2299
2299
  const h = 5 / n, c = Q(s), d = this.store.getState();
2300
2300
  let { revSin: u, revCos: g } = d;
2301
- (W(s) || F(s)) && (u = 0, g = 1), this.hoveredHandle = void 0;
2302
- const f = Xn * (1 / n), p = r - c.x, m = o - c.y, w = p * g - m * u, b = p * u + m * g, v = ((S = s.properties.style) == null ? void 0 : S.fixedSize) === !0 || F(s);
2301
+ (W(s) || P(s)) && (u = 0, g = 1), this.hoveredHandle = void 0;
2302
+ const f = Xn * (1 / n), p = r - c.x, m = o - c.y, w = p * g - m * u, b = p * u + m * g, v = ((S = s.properties.style) == null ? void 0 : S.fixedSize) === !0 || P(s);
2303
2303
  if (v && (a /= n, l /= n), !v) {
2304
2304
  for (let x = 0; x < ke.length; x++) {
2305
2305
  const [A, C] = ke[x], E = a * A, k = l * C;
@@ -2311,8 +2311,8 @@ class Zt extends qt {
2311
2311
  return;
2312
2312
  }
2313
2313
  }
2314
- for (const [x, A, C, E, k, P] of Es) {
2315
- const O = a * C, N = l * E, B = a * k, et = l * P, nt = mt(A, { x: w - O, y: b - N });
2314
+ for (const [x, A, C, E, k, F] of Es) {
2315
+ const O = a * C, N = l * E, B = a * k, et = l * F, nt = mt(A, { x: w - O, y: b - N });
2316
2316
  if (Math.abs(nt) < h && w >= O - h && w <= B + h && b >= N - h && b <= et + h) {
2317
2317
  this.hoveredHandle = { type: "edge", edge: x, corner: -1 }, this.store.setState({ hoveredHandle: Cs[x][0] + 4, hoveredFeature: this.annotation }), this.setCursor(this.getEdgeCursor(x));
2318
2318
  return;
@@ -2346,7 +2346,7 @@ class Zt extends qt {
2346
2346
  dragCorner(t, n, s) {
2347
2347
  const r = s === 0 || s === 1, o = s === 0 || s === 3, a = s === 1 || s === 2, l = s === 2 || s === 3, { width: h, height: c } = q(t), d = this.store.getState();
2348
2348
  let { revSin: u, revCos: g } = d;
2349
- (W(t) || F(t)) && (u = 0, g = 1);
2349
+ (W(t) || P(t)) && (u = 0, g = 1);
2350
2350
  const f = n.x * g - n.y * u, p = n.x * u + n.y * g;
2351
2351
  let m = 0, w = 0;
2352
2352
  o ? m = -f : a && (m = f), r ? w = -p : l && (w = p);
@@ -2367,7 +2367,7 @@ class Zt extends qt {
2367
2367
  dragEdge(t, n, s) {
2368
2368
  const { width: r, height: o } = q(t), a = Q(t), l = this.store.getState();
2369
2369
  let { revSin: h, revCos: c } = l;
2370
- (W(t) || F(t)) && (h = 0, c = 1);
2370
+ (W(t) || P(t)) && (h = 0, c = 1);
2371
2371
  const d = n.x * c - n.y * h, u = n.x * h + n.y * c;
2372
2372
  let g = r, f = o, p = 0, m = 0;
2373
2373
  switch (s.edge) {
@@ -2448,7 +2448,7 @@ class Zt extends qt {
2448
2448
  this.dragStartPoint = r, this.disablePanning(), this.onDragStart({ clientX: r.x, clientY: r.y });
2449
2449
  }
2450
2450
  setAnnotation(t) {
2451
- t && F(t) && (this.justActivated = !0), super.setAnnotation(t), t && F(t) && t.properties.mode === at && this.store.getState().updateFeature(t.id, {
2451
+ t && P(t) && (this.justActivated = !0), super.setAnnotation(t), t && P(t) && t.properties.mode === at && this.store.getState().updateFeature(t.id, {
2452
2452
  properties: {
2453
2453
  ...t.properties,
2454
2454
  mode: Rt
@@ -2557,7 +2557,7 @@ class Ps {
2557
2557
  var a;
2558
2558
  const r = e.getFeature(s);
2559
2559
  if (!r) return;
2560
- (_(r) && ((a = r.properties.style) == null ? void 0 : a.fixedSize) || F(r)) && n.forEach((l) => {
2560
+ (_(r) && ((a = r.properties.style) == null ? void 0 : a.fixedSize) || P(r)) && n.forEach((l) => {
2561
2561
  const h = this.links.get(l);
2562
2562
  if (!h) return;
2563
2563
  const c = h.arrow;
@@ -2607,13 +2607,7 @@ class Ps {
2607
2607
  }
2608
2608
  if ((d = h.properties.link) != null && d.end) {
2609
2609
  const u = h.properties.link.end;
2610
- this.add(
2611
- h,
2612
- it,
2613
- u.id,
2614
- u.type,
2615
- u.magnet
2616
- );
2610
+ this.add(h, it, u.id, u.type, u.magnet);
2617
2611
  }
2618
2612
  }), o.forEach((a) => {
2619
2613
  const l = t[a];
@@ -2682,7 +2676,7 @@ class Ps {
2682
2676
  y: d[1]
2683
2677
  };
2684
2678
  let g;
2685
- if (_(s) || F(s) || W(s) ? g = this.snapping.snapToText(u, [s]) : st(s) && (g = this.snapping.snapToPolygon(u, [s])), !g) continue;
2679
+ if (_(s) || P(s) || W(s) ? g = this.snapping.snapToText(u, [s]) : st(s) && (g = this.snapping.snapToPolygon(u, [s])), !g) continue;
2686
2680
  const f = g.point, p = {
2687
2681
  ...h.geometry,
2688
2682
  coordinates: h.geometry.coordinates.map((m, w) => l.side === X && w === 0 || l.side === it && w === 1 ? [f.x, f.y] : [...m])
@@ -2790,7 +2784,11 @@ class Ps {
2790
2784
  e.forEach((l, h) => {
2791
2785
  const c = this.links.get(l.start), d = this.links.get(l.end), u = t.getFeature(h);
2792
2786
  let g = u.geometry.coordinates[0], f = u.geometry.coordinates[1];
2793
- const p = c ? c.targetType === T.NODE ? o[s.get(c.target)] : c.targetType === T.EDGE ? this._getEdgeSnapPoint(c.target, c.magnet.x, !0) : this._getAnnotationCenter(t.getFeature(c.target)) : { x: g[0], y: g[1] }, m = d ? d.targetType === T.NODE ? o[s.get(d.target)] : d.targetType === T.EDGE ? this._getEdgeSnapPoint(d.target, d.magnet.x, !0) : this._getAnnotationCenter(t.getFeature(d.target)) : { x: f[0], y: f[1] }, w = Y(m, p);
2787
+ const p = c ? c.targetType === T.NODE ? o[s.get(c.target)] : c.targetType === T.EDGE ? this._getEdgeSnapPoint(
2788
+ c.target,
2789
+ c.magnet.x,
2790
+ !0
2791
+ ) : this._getAnnotationCenter(t.getFeature(c.target)) : { x: g[0], y: g[1] }, m = d ? d.targetType === T.NODE ? o[s.get(d.target)] : d.targetType === T.EDGE ? this._getEdgeSnapPoint(d.target, d.magnet.x, !0) : this._getAnnotationCenter(t.getFeature(d.target)) : { x: f[0], y: f[1] }, w = Y(m, p);
2794
2792
  if (c)
2795
2793
  if (c.targetType === T.NODE)
2796
2794
  g = this._getNodeSnapPoint(
@@ -2799,7 +2797,10 @@ class Ps {
2799
2797
  this._isLinkedToCenter(c)
2800
2798
  );
2801
2799
  else if (c.targetType === T.EDGE)
2802
- g = this._getEdgeSnapPoint(c.target, c.magnet.x);
2800
+ g = this._getEdgeSnapPoint(
2801
+ c.target,
2802
+ c.magnet.x
2803
+ );
2803
2804
  else {
2804
2805
  const b = t.getFeature(c.target);
2805
2806
  g = this._getAnnotationSnapPoint(
@@ -2855,14 +2856,11 @@ class Ps {
2855
2856
  var d;
2856
2857
  const r = Q(e);
2857
2858
  let { width: o, height: a } = q(e);
2858
- (_(e) && ((d = e.properties.style) == null ? void 0 : d.fixedSize) || F(e)) && (o /= s, a /= s);
2859
+ (_(e) && ((d = e.properties.style) == null ? void 0 : d.fixedSize) || P(e)) && (o /= s, a /= s);
2859
2860
  let h = n.magnet.x * o, c = n.magnet.y * a;
2860
2861
  if (_(e) && !W(e)) {
2861
2862
  const { sin: u, cos: g } = this.store.getState(), f = h * g - c * u, p = h * u + c * g;
2862
2863
  h = f, c = p;
2863
- } else if (F(e)) {
2864
- const g = { ...e.properties.style }.iconSize || 32, f = g / 16 * 0.5;
2865
- h = n.magnet.x * g * f, c = n.magnet.y * g * f;
2866
2864
  }
2867
2865
  return [r.x + h, r.y + c];
2868
2866
  }
@@ -3153,7 +3151,7 @@ function Os(i, e, t, n, s) {
3153
3151
  const { sin: h, cos: c } = s.getState(), d = !W(e);
3154
3152
  let u = null, g = 1 / 0;
3155
3153
  for (let p = 0; p < 4; p++) {
3156
- const [m, w, b, v] = Ns[p], S = Me(m, w, r, o, a, l, c, h, d), x = Me(b, v, r, o, a, l, c, h, d), A = Y(x, S), C = J(A), E = { x: A.x / C, y: A.y / C }, k = { x: -E.y, y: E.x }, P = Y(i, S), O = mt(k, P), N = mt(E, P);
3154
+ const [m, w, b, v] = Ns[p], S = Me(m, w, r, o, a, l, c, h, d), x = Me(b, v, r, o, a, l, c, h, d), A = Y(x, S), C = J(A), E = { x: A.x / C, y: A.y / C }, k = { x: -E.y, y: E.x }, F = Y(i, S), O = mt(k, F), N = mt(E, F);
3157
3155
  if (N >= 0 && N <= C) {
3158
3156
  const B = Math.abs(O);
3159
3157
  if (B < n || B < g) {
@@ -3316,7 +3314,7 @@ class qs extends EventTarget {
3316
3314
  t,
3317
3315
  this.ogma.getContainer()
3318
3316
  ), { x: s, y: r } = this.ogma.view.screenToGraphCoordinates(n), o = this.detect(s, r);
3319
- if (!o || !F(o)) return;
3317
+ if (!o || !P(o)) return;
3320
3318
  const a = (d = o.properties.style) == null ? void 0 : d.maxHeight, l = o.properties.height;
3321
3319
  if (!a || l <= a) return;
3322
3320
  const h = this.ogma.getContainer(), c = h == null ? void 0 : h.querySelector(
@@ -3366,7 +3364,7 @@ class qs extends EventTarget {
3366
3364
  r = c;
3367
3365
  break;
3368
3366
  }
3369
- } else if (F(c)) {
3367
+ } else if (P(c)) {
3370
3368
  if (rs(
3371
3369
  c,
3372
3370
  { x: t, y: n },
@@ -3689,14 +3687,14 @@ class Qs extends js {
3689
3687
  super();
3690
3688
  y(this, "store");
3691
3689
  y(this, "onRotationChange", () => {
3692
- const t = this.store.getState().getAllFeatures().filter((n) => _(n) || F(n));
3690
+ const t = this.store.getState().getAllFeatures().filter((n) => _(n) || P(n));
3693
3691
  for (const n of t) this.updateRotatedText(n);
3694
3692
  });
3695
3693
  y(this, "onZoomChange", () => {
3696
3694
  const t = this.store.getState().getAllFeatures().filter(
3697
3695
  (n) => {
3698
3696
  var s;
3699
- return (_(n) || F(n)) && ((s = n.properties.style) == null ? void 0 : s.fixedSize);
3697
+ return (_(n) || P(n)) && ((s = n.properties.style) == null ? void 0 : s.fixedSize);
3700
3698
  }
3701
3699
  );
3702
3700
  for (const n of t) this.updateRotatedText(n);
@@ -3705,7 +3703,7 @@ class Qs extends js {
3705
3703
  (n) => n.features,
3706
3704
  (n) => {
3707
3705
  this.clear(), Object.values(n).forEach((s) => {
3708
- _(s) || F(s) ? this.updateRotatedText(s) : this.insert(s);
3706
+ _(s) || P(s) ? this.updateRotatedText(s) : this.insert(s);
3709
3707
  });
3710
3708
  }
3711
3709
  ), this.store.subscribe(
@@ -3718,7 +3716,7 @@ class Qs extends js {
3718
3716
  (n, s) => {
3719
3717
  s && s.isDragging && !n.isDragging && n.lastChangedFeatures.length > 0 && n.lastChangedFeatures.forEach((r) => {
3720
3718
  const o = n.features[r];
3721
- o && (kt(o), _(o) || F(o) ? this.updateRotatedText(o) : (this.remove(o, Fe), this.insert(o)));
3719
+ o && (kt(o), _(o) || P(o) ? this.updateRotatedText(o) : (this.remove(o, Fe), this.insert(o)));
3722
3720
  });
3723
3721
  },
3724
3722
  { equalityFn: (n, s) => n.isDragging === s.isDragging }
@@ -3950,9 +3948,9 @@ function ri(i, e, t, n, s, r) {
3950
3948
  b.setAttribute("stroke", v), b.setAttribute("stroke-width", `${p * c}`), b.setAttribute("fill", "none"), b.setAttribute("stroke-linecap", "round"), b.setAttribute("stroke-linejoin", "round");
3951
3949
  const S = De(h, c * p), x = De(d, c * p), A = Nt(f), C = dt(o, ot(A, S)), E = dt(a, ot(A, -x)), k = `M ${C.x} ${C.y} L ${E.x} ${E.y}`;
3952
3950
  b.setAttribute("d", k);
3953
- const P = w.children[1];
3954
- return P.innerHTML = "", g === "dashed" ? b.setAttribute("stroke-dasharray", "5,10") : b.removeAttribute("stroke-dasharray"), Le(
3955
- P,
3951
+ const F = w.children[1];
3952
+ return F.innerHTML = "", g === "dashed" ? b.setAttribute("stroke-dasharray", "5,10") : b.removeAttribute("stroke-dasharray"), Le(
3953
+ F,
3956
3954
  o,
3957
3955
  f,
3958
3956
  v,
@@ -3961,7 +3959,7 @@ function ri(i, e, t, n, s, r) {
3961
3959
  p,
3962
3960
  m
3963
3961
  ), Le(
3964
- P,
3962
+ F,
3965
3963
  a,
3966
3964
  ns(f),
3967
3965
  v,
@@ -4169,7 +4167,7 @@ function ui(i, e, t, n = 0, s = !1) {
4169
4167
  w,
4170
4168
  p,
4171
4169
  l
4172
- ), E = w * $t + l * 2, k = C ? Math.min(A, E) : A, P = f ? Math.min(k, f) : k, O = !C && f ? A > f : !1, B = P + (s ? 28 : 0);
4170
+ ), E = w * $t + l * 2, k = C ? Math.min(A, E) : A, F = f ? Math.min(k, f) : k, O = !C && f ? A > f : !1, B = F + (s ? 28 : 0);
4173
4171
  i.innerHTML = "";
4174
4172
  const et = -x / 2, nt = -B / 2, U = M("rect");
4175
4173
  U.setAttribute("x", `${et}`), U.setAttribute("y", `${nt}`), U.setAttribute("width", `${x}`), U.setAttribute("height", `${B}`), U.setAttribute("rx", `${d}`), U.setAttribute("ry", `${d}`), U.setAttribute(
@@ -4487,8 +4485,8 @@ function Pi(i, e, t) {
4487
4485
  /** @type {Token} */
4488
4486
  i[C.index]
4489
4487
  );
4490
- let P;
4491
- k instanceof ht && (P = ct("-", k.font || t), C.width + P > s(c) && (E = !g.length));
4488
+ let F;
4489
+ k instanceof ht && (F = ct("-", k.font || t), C.width + F > s(c) && (E = !g.length));
4492
4490
  } while (!E);
4493
4491
  u.push(C.index + 1), d = 0, c++, h = C.index, g = [];
4494
4492
  } else if (o === "break-word") {
@@ -4515,11 +4513,11 @@ function Pi(i, e, t) {
4515
4513
  for (; E > A && (C = i[E - 1]) && (C.whitespace || !(C.value || C instanceof ht)); )
4516
4514
  C instanceof K && (k = C), E--;
4517
4515
  C instanceof ht && (C.value = "-", C.width = ct("-", C.font || t)), p = x;
4518
- const P = Object.assign(
4516
+ const F = Object.assign(
4519
4517
  i.slice(A, E).filter((O) => O.value),
4520
4518
  { width: 0 }
4521
4519
  );
4522
- return k && P.push(k), P.width = P.reduce((O, N) => O + N.width, 0), P.width > m && (m = P.width), P;
4520
+ return k && F.push(k), F.width = F.reduce((O, N) => O + N.width, 0), F.width > m && (m = F.width), F;
4523
4521
  });
4524
4522
  let b = !1;
4525
4523
  if (r) {
@@ -4580,7 +4578,7 @@ function Hi(i, e) {
4580
4578
  S = "tspan", x = null, A = "";
4581
4579
  };
4582
4580
  for (let E = 0, k = i.length; E < k; E++) {
4583
- let P = "", O = "", N = 0;
4581
+ let F = "", O = "", N = 0;
4584
4582
  const B = i[E];
4585
4583
  if (!B.length) {
4586
4584
  t.push(
@@ -4595,7 +4593,7 @@ function Hi(i, e) {
4595
4593
  let et = 0, nt = 0, U;
4596
4594
  for (let j = 0, L = B.length; j < L; j++) {
4597
4595
  const D = B[j], z = D.font;
4598
- D.whitespace && et++, nt += D.width, j && !D.tracking && !N && ne(z.id, P) && ne(D.class, O) && ne(U, D.href) ? A += D.value : (C(), A = D.value, x = {
4596
+ D.whitespace && et++, nt += D.width, j && !D.tracking && !N && ne(z.id, F) && ne(D.class, O) && ne(U, D.href) ? A += D.value : (C(), A = D.value, x = {
4599
4597
  fontFamily: z.family !== r ? z.family : null,
4600
4598
  fontSize: z.size !== s ? z.size : null,
4601
4599
  fontWeight: z.weight !== 400 && z.weight || null,
@@ -4608,7 +4606,7 @@ function Hi(i, e) {
4608
4606
  href: null,
4609
4607
  rel: null,
4610
4608
  target: null
4611
- }, N && (x.dx = ft(N), N = 0), D.tracking && (N = z.size * D.tracking), D.href && !U ? (U = D.href, S = "a", x.href = U, x.rel = D.rel, x.target = D.target) : U = null, P = z.id, O = D.class);
4609
+ }, N && (x.dx = ft(N), N = 0), D.tracking && (N = z.size * D.tracking), D.href && !U ? (U = D.href, S = "a", x.href = U, x.rel = D.rel, x.target = D.target) : U = null, F = z.id, O = D.class);
4612
4610
  }
4613
4611
  if (C(), u)
4614
4612
  t.push(...v);
@@ -5877,7 +5875,7 @@ class dr extends yn {
5877
5875
  p,
5878
5876
  m,
5879
5877
  d
5880
- ) : F(p) ? m = fi(
5878
+ ) : P(p) ? m = fi(
5881
5879
  h,
5882
5880
  p,
5883
5881
  m,
@@ -6173,7 +6171,7 @@ const yr = (i) => {
6173
6171
  const o = r.features[s];
6174
6172
  if (!o) return r;
6175
6173
  const { features: a, liveUpdates: l } = r, h = /* @__PURE__ */ new Set([s]);
6176
- if ((F(o) || _(o)) && Object.values(a).forEach((m) => {
6174
+ if ((P(o) || _(o)) && Object.values(a).forEach((m) => {
6177
6175
  var w, b, v, S;
6178
6176
  tt(m) && (((b = (w = m.properties.link) == null ? void 0 : w.end) == null ? void 0 : b.id) === s || ((S = (v = m.properties.link) == null ? void 0 : v.start) == null ? void 0 : S.id) === s) && h.add(m.id);
6179
6177
  }), tt(o)) {
@@ -7096,7 +7094,7 @@ export {
7096
7094
  tt as isArrow,
7097
7095
  W as isBox,
7098
7096
  hs as isColor,
7099
- F as isComment,
7097
+ P as isComment,
7100
7098
  ws as isCommentArrow,
7101
7099
  un as isHexColor,
7102
7100
  st as isPolygon,
@@ -14,10 +14,16 @@ import { Position } from 'geojson';
14
14
  import { Size } from '@linkurious/ogma';
15
15
 
16
16
  /**
17
- * Automatically lightens or darkens a color (hex or rgba) for highlight purposes.
17
+ * Adjusts the brightness of a color (hex or rgba) based on its perceived luminance.
18
+ * For bright colors, the adjustment is applied as darkening; for dark colors, as lightening.
19
+ *
18
20
  * @param color - Color string in hex (#RRGGBB or #RGB) or rgba format
19
- * @param amount - Amount to lighten/darken (default 20 for lighter and -10 for darker)
20
- * @returns Highlighted color in rgba format
21
+ * @param amount - Adjustment factor between -1 and 1:
22
+ * - Positive values (0 to 1): lighten dark colors, darken bright colors
23
+ * - Negative values (-1 to 0): darken dark colors, lighten bright colors
24
+ * - 0: no change
25
+ * - Example: 0.2 applies a 20% adjustment, -0.1 applies a -10% adjustment
26
+ * @returns Adjusted color in rgba format
21
27
  */
22
28
  export declare function adjustColorBrightness(color: Color, amount: number): RgbaColor;
23
29
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@linkurious/ogma-annotations",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "Headless annotation plugin for Ogma",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",
@@ -51,8 +51,8 @@
51
51
  "bump:minor": "bump2version minor && npm version --no-git-tag-version minor",
52
52
  "bump:major": "bump2version major && npm version --no-git-tag-version major",
53
53
  "build": "vite build",
54
- "lint:ci": "eslint -f checkstyle -o reports/checkstyle.xml --ext ts --ext js src",
55
- "lint": "eslint --ext ts --ext js src",
54
+ "lint": "npx oxlint -c oxlint.json src",
55
+ "lint:ci": "mkdir -p reports && npx oxlint -c oxlint.json -f checkstyle src > reports/checkstyle.xml",
56
56
  "types": "tsc --d -emitDeclarationOnly --outDir dist/types",
57
57
  "preview": "vite preview",
58
58
  "docs:build": "typedoc && node scripts/organize-sidebar.mjs",
@@ -85,7 +85,7 @@
85
85
  "author": "Linkurious SAS",
86
86
  "license": "Apache-2.0",
87
87
  "peerDependencies": {
88
- "@linkurious/ogma": ">=4.5.6 || ^5.0.0"
88
+ "@linkurious/ogma": ">=5.3.11"
89
89
  },
90
90
  "dependencies": {
91
91
  "@borgar/textbox": "1.8.0",
@@ -96,7 +96,8 @@
96
96
  "zustand": "5.0.7"
97
97
  },
98
98
  "devDependencies": {
99
- "@linkurious/eslint-config-ogma": "1.0.14",
99
+ "@linkurious/eslint-config-ogma": "2.1.0",
100
+ "oxlint": "0.16.0",
100
101
  "@types/geojson": "7946.0.13",
101
102
  "@types/rbush": "4.0.0",
102
103
  "get-port": "7.0.0",
@@ -107,25 +108,11 @@
107
108
  "typedoc-vitepress-theme": "1.1.2",
108
109
  "typescript": "5.3.3",
109
110
  "vanilla-colorful": "0.7.2",
110
- "vite": "latest",
111
+ "vite": "6.3.5",
111
112
  "vite-plugin-dts": "^4.1.0",
112
- "vitest": "latest",
113
+ "vitest": "3.2.4",
113
114
  "vitest-canvas-mock": "1.1.3"
114
115
  },
115
- "eslintConfig": {
116
- "extends": [
117
- "@linkurious/eslint-config-ogma"
118
- ],
119
- "rules": {
120
- "@typescript-eslint/no-unused-vars": [
121
- "error",
122
- {
123
- "argsIgnorePattern": "^_",
124
- "varsIgnorePattern": "^_"
125
- }
126
- ]
127
- }
128
- },
129
116
  "prettier": {
130
117
  "printWidth": 80,
131
118
  "trailingComma": "none"