@linkurious/ogma-annotations
Version:
Headless annotation plugin for Ogma
11 lines • 146 kB
JavaScript
(function(w,q){typeof exports=="object"&&typeof module<"u"?q(exports,require("@linkurious/ogma")):typeof define=="function"&&define.amd?define(["exports","@linkurious/ogma"],q):(w=typeof globalThis<"u"?globalThis:w||self,q(w["@linkurious/ogma-annotations"]={},w.Ogma))})(this,(function(w,q){"use strict";var jr=Object.defineProperty;var Gr=(w,q,wt)=>q in w?jr(w,q,{enumerable:!0,configurable:!0,writable:!0,value:wt}):w[q]=wt;var y=(w,q,wt)=>Gr(w,typeof q!="symbol"?q+"":q,wt);function wt(i){return i&&i.__esModule&&Object.prototype.hasOwnProperty.call(i,"default")?i.default:i}var te={exports:{}},Ye;function Qn(){return Ye||(Ye=1,(function(i){var e=Object.prototype.hasOwnProperty,t="~";function n(){}Object.create&&(n.prototype=Object.create(null),new n().__proto__||(t=!1));function s(c,h,l){this.fn=c,this.context=h,this.once=l||!1}function r(c,h,l,d,u){if(typeof l!="function")throw new TypeError("The listener must be a function");var g=new s(l,d||c,u),f=t?t+h:h;return c._events[f]?c._events[f].fn?c._events[f]=[c._events[f],g]:c._events[f].push(g):(c._events[f]=g,c._eventsCount++),c}function o(c,h){--c._eventsCount===0?c._events=new n:delete c._events[h]}function a(){this._events=new n,this._eventsCount=0}a.prototype.eventNames=function(){var h=[],l,d;if(this._eventsCount===0)return h;for(d in l=this._events)e.call(l,d)&&h.push(t?d.slice(1):d);return Object.getOwnPropertySymbols?h.concat(Object.getOwnPropertySymbols(l)):h},a.prototype.listeners=function(h){var l=t?t+h:h,d=this._events[l];if(!d)return[];if(d.fn)return[d.fn];for(var u=0,g=d.length,f=new Array(g);u<g;u++)f[u]=d[u].fn;return f},a.prototype.listenerCount=function(h){var l=t?t+h:h,d=this._events[l];return d?d.fn?1:d.length:0},a.prototype.emit=function(h,l,d,u,g,f){var p=t?t+h:h;if(!this._events[p])return!1;var m=this._events[p],x=arguments.length,v,S;if(m.fn){switch(m.once&&this.removeListener(h,m.fn,void 0,!0),x){case 1:return m.fn.call(m.context),!0;case 2:return m.fn.call(m.context,l),!0;case 3:return m.fn.call(m.context,l,d),!0;case 4:return m.fn.call(m.context,l,d,u),!0;case 5:return m.fn.call(m.context,l,d,u,g),!0;case 6:return m.fn.call(m.context,l,d,u,g,f),!0}for(S=1,v=new Array(x-1);S<x;S++)v[S-1]=arguments[S];m.fn.apply(m.context,v)}else{var E=m.length,b;for(S=0;S<E;S++)switch(m[S].once&&this.removeListener(h,m[S].fn,void 0,!0),x){case 1:m[S].fn.call(m[S].context);break;case 2:m[S].fn.call(m[S].context,l);break;case 3:m[S].fn.call(m[S].context,l,d);break;case 4:m[S].fn.call(m[S].context,l,d,u);break;default:if(!v)for(b=1,v=new Array(x-1);b<x;b++)v[b-1]=arguments[b];m[S].fn.apply(m[S].context,v)}}return!0},a.prototype.on=function(h,l,d){return r(this,h,l,d,!1)},a.prototype.once=function(h,l,d){return r(this,h,l,d,!0)},a.prototype.removeListener=function(h,l,d,u){var g=t?t+h:h;if(!this._events[g])return this;if(!l)return o(this,g),this;var f=this._events[g];if(f.fn)f.fn===l&&(!u||f.once)&&(!d||f.context===d)&&o(this,g);else{for(var p=0,m=[],x=f.length;p<x;p++)(f[p].fn!==l||u&&!f[p].once||d&&f[p].context!==d)&&m.push(f[p]);m.length?this._events[g]=m.length===1?m[0]:m:o(this,g)}return this},a.prototype.removeAllListeners=function(h){var l;return h?(l=t?t+h:h,this._events[l]&&o(this,l)):(this._events=new n,this._eventsCount=0),this},a.prototype.off=a.prototype.removeListener,a.prototype.addListener=a.prototype.on,a.prefixed=t,a.EventEmitter=a,i.exports=a})(te)),te.exports}var ts=Qn();const es=wt(ts),ns=-1,Tt="dragging",xt="dragstart",kt="dragend",Ot="click",Xe="select",Ue="unselect",ss="hover",is="unhover",qe="remove",Yt="add",We="cancelDrawing",ee="completeDrawing",je="update",Ge="link",Ve="history",rs="data-annotation",ne=5,Ze=3,Xt={SHAPES:1,EDITOR:2,HANDLES:3},O="start",rt="end",H={default:"default",move:"move",pointer:"pointer",crosshair:"crosshair",grab:"grab",grabbing:"grabbing",ewResize:"ew-resize",nsResize:"ns-resize",neswResize:"nesw-resize",nwseResize:"nwse-resize",nwResize:"nw-resize",neResize:"ne-resize",swResize:"sw-resize",seResize:"se-resize",nResize:"n-resize",eResize:"e-resize",sResize:"s-resize",wResize:"w-resize"},ot="collapsed",Mt="expanded",Pt=1.2,se=.2,P={TEXT:"text",NODE:"node",BOX:"box",COMMENT:"comment",POLYGON:"polygon",ANNOTATION:"annotation",EDGE:"edge"},ie=`<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M22 2L11 13M22 2L15 22L11 13M22 2L2 9L11 13" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>`,re=`<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<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"/>
</svg>
`;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">
<textarea wrap="on" name="annotation-text--input" spellcheck="false" placeholder="${d}"></textarea>
${h?`<button class="ogma-send-button" type="button" title="Send">
<span class="ogma-send-button-icon">${l}</span>
</button>`:""}
</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.str