@recogito/text-annotator 3.0.0-rc.1 → 3.0.0-rc.10

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.
@@ -1,2 +1,2 @@
1
- (function(O,k){typeof exports=="object"&&typeof module<"u"?k(exports):typeof define=="function"&&define.amd?define(["exports"],k):(O=typeof globalThis<"u"?globalThis:O||self,k(O.RecogitoJS={}))})(this,function(O){"use strict";const k={fill:"rgb(0, 128, 255",fillOpacity:.18},ft={fill:"rgb(0, 128, 255)",fillOpacity:.45},j={paint:(t,e,n,o,i,s)=>{const a=s?typeof s=="function"?s(t,i):s:i?ft:k;n.fillStyle=a.fill,n.globalAlpha=a.fillOpacity||1,e.forEach(({x:r,y:l,width:h,height:g})=>n.fillRect(r,l-2.5,h,g+5))}},gt=t=>{let e=new Set;return o=>{const i=o.map(s=>s.id);(e.size!==i.length||i.some(s=>!e.has(s)))&&t.set(i),e=new Set(i)}},he="",pt=(t,e=10)=>{let n;return(...o)=>{clearTimeout(n),n=setTimeout(()=>t.apply(void 0,o),e)}},et=(t,e)=>{const n=document.createElement("canvas");if(n.width=e?2*window.innerWidth:window.innerWidth,n.height=e?2*window.innerHeight:window.innerHeight,n.className=t,e){const o=n.getContext("2d");o.scale(2,2),o.translate(.5,.5)}return n},nt=(t,e)=>{if(t.width=e?2*window.innerWidth:window.innerWidth,t.height=e?2*window.innerHeight:window.innerHeight,e){const n=t.getContext("2d");n.scale(2,2),n.translate(.5,.5)}},ot=(t,e,n)=>{const{store:o,selection:i,hover:s}=e;let a,r=j;const l=gt(n);t.classList.add("r6o-annotatable");const h=et("r6o-highlight-layer bg"),g=et("r6o-highlight-layer fg",!0),d=h.getContext("2d"),p=g.getContext("2d");t.insertBefore(h,t.firstChild),t.appendChild(g),t.addEventListener("pointermove",m=>{const{x:T,y:b}=t.getBoundingClientRect(),C=o.getAt(m.clientX-T,m.clientY-b);C?s.current!==C.id&&(t.classList.add("hovered"),s.set(C.id)):s.current&&(t.classList.remove("hovered"),s.set(null))});const B=()=>{const{top:m,left:T}=t.getBoundingClientRect(),{innerWidth:b,innerHeight:C}=window,L=-T,f=-m,y=b-T,E=C-m;return{top:m,left:T,minX:L,minY:f,maxX:y,maxY:E}},A=()=>u();document.addEventListener("scroll",A,{capture:!0,passive:!0});const w=pt(()=>{nt(h),nt(g,!0),o.recalculatePositions(),u()});window.addEventListener("resize",w),new ResizeObserver(w).observe(t);const u=()=>requestAnimationFrame(()=>{const{top:m,left:T,minX:b,minY:C,maxX:L,maxY:f}=B(),y=o.getIntersectingRects(b,C,L,f),{width:E,height:v}=g,M=new Set(i.selected.map(({id:R})=>R));p.clearRect(-.5,-.5,E+1,v+1),d.clearRect(-.5,-.5,E+1,v+1),y.forEach(({annotation:R,rects:Y})=>{const tt=Y.map(({x:ae,y:ce,width:le,height:de})=>({x:ae+T,y:ce+m,width:le,height:de})),W=M.has(R.id);r.paint(R,tt,d,p,W,a)}),setTimeout(()=>l(y.map(({annotation:R})=>R)),1)});return o.observe(()=>u()),i.subscribe(()=>u()),{redraw:u,setDrawingStyle:m=>{a=m,u()},setPainter:m=>r=m}},I=(t,e)=>{const{quote:n,start:o,end:i}=t.selector,s=t.selector.offsetReference?t.selector.offsetReference:e;if(!s)return t;const a=document.createNodeIterator(e,NodeFilter.SHOW_TEXT);let r=0,l=document.createRange(),h=a.nextNode();h===null&&console.error("Could not revive annotation target. Content missing.");let g=!s;for(;h!==null;){const d=h.textContent.length;if(!g&&s&&(g=s.contains(h)),g){if(r+d>o){l.setStart(h,o-r);break}r+=d}h=a.nextNode()}for(;h!==null;){const d=h.textContent.length;if(r+d>i){l.setEnd(h,i-r);break}r+=d,h=a.nextNode()}return{...t,selector:{...t.selector,quote:n,start:o,end:i,range:l}}};var it=Object.prototype.hasOwnProperty;function _(t,e){var n,o;if(t===e)return!0;if(t&&e&&(n=t.constructor)===e.constructor){if(n===Date)return t.getTime()===e.getTime();if(n===RegExp)return t.toString()===e.toString();if(n===Array){if((o=t.length)===e.length)for(;o--&&_(t[o],e[o]););return o===-1}if(!n||typeof t=="object"){o=0;for(n in t)if(it.call(t,n)&&++o&&!it.call(e,n)||!(n in e)||!_(t[n],e[n]))return!1;return Object.keys(e).length===o}}return t!==t&&e!==e}function G(){}function mt(t,e){return t!=t?e==e:t!==e||t&&typeof t=="object"||typeof t=="function"}const D=[];function J(t,e=G){let n;const o=new Set;function i(r){if(mt(t,r)&&(t=r,n)){const l=!D.length;for(const h of o)h[1](),D.push(h,t);if(l){for(let h=0;h<D.length;h+=2)D[h][0](D[h+1]);D.length=0}}}function s(r){i(r(t))}function a(r,l=G){const h=[r,l];return o.add(h),o.size===1&&(n=e(i,s)||G),r(t),()=>{o.delete(h),o.size===0&&n&&(n(),n=null)}}return{set:i,update:s,subscribe:a}}const At=t=>{const{subscribe:e,set:n}=J(null);let o=null;return e(i=>o=i),t.observe(({changes:i})=>{if(o){i.deleted.some(r=>r.id===o)&&n(null);const a=i.updated.find(({oldValue:r})=>r.id===o);a&&n(a.newValue.id)}}),{get current(){return o},subscribe:e,set:n}},K={selected:[]},xt=(t,e="EDIT")=>{const{subscribe:n,set:o}=J(K);let i=K;n(d=>i=d);const s=()=>o(K),a=()=>{var d;return((d=i.selected)==null?void 0:d.length)===0},r=d=>{if(i.selected.length===0)return!1;const p=typeof d=="string"?d:d.id;return i.selected.some(B=>B.id===p)},l=(d,p)=>{const B=t.getAnnotation(d);if(B){const A=yt(B,e);o(A==="EDIT"?{selected:[{id:d,editable:!0}],pointerEvent:p}:A==="SELECT"?{selected:[{id:d}],pointerEvent:p}:{selected:[],pointerEvent:p})}else console.warn("Invalid selection: "+d)},h=(d,p=!0)=>{const B=Array.isArray(d)?d:[d],A=B.map(w=>t.getAnnotation(w)).filter(w=>w);o({selected:A.map(({id:w})=>({id:w,editable:p}))}),A.length!==B.length&&console.warn("Invalid selection",d)},g=d=>{if(i.selected.length===0)return!1;const{selected:p}=i;p.filter(({id:A})=>d.includes(A)).length>0&&o({selected:p.filter(({id:A})=>!d.includes(A))})};return t.observe(({changes:d})=>g(d.deleted.map(p=>p.id))),{clear:s,clickSelect:l,get selected(){return i?[...i.selected]:null},get pointerEvent(){return i?i.pointerEvent:null},isEmpty:a,isSelected:r,setSelected:h,subscribe:n}},yt=(t,e)=>typeof e=="function"?e(t)||"EDIT":e||"EDIT";let $;const Et=new Uint8Array(16);function wt(){if(!$&&($=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!$))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return $(Et)}const X=[];for(let t=0;t<256;++t)X.push((t+256).toString(16).slice(1));function bt(t,e=0){return X[t[e+0]]+X[t[e+1]]+X[t[e+2]]+X[t[e+3]]+"-"+X[t[e+4]]+X[t[e+5]]+"-"+X[t[e+6]]+X[t[e+7]]+"-"+X[t[e+8]]+X[t[e+9]]+"-"+X[t[e+10]]+X[t[e+11]]+X[t[e+12]]+X[t[e+13]]+X[t[e+14]]+X[t[e+15]]}const st={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function Tt(t,e,n){if(st.randomUUID&&!e&&!t)return st.randomUUID();t=t||{};const o=t.random||(t.rng||wt)();if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,e){n=n||0;for(let i=0;i<16;++i)e[n+i]=o[i];return e}return bt(o)}const vt=(t,e)=>{const n=new Set(t.bodies.map(o=>o.id));return e.bodies.filter(o=>!n.has(o.id))},Ct=(t,e)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},Bt=(t,e)=>e.bodies.map(n=>{const o=t.bodies.find(i=>i.id===n.id);return{newBody:n,oldBody:o&&!_(o,n)?o:void 0}}).filter(({oldBody:n})=>n),Mt=(t,e)=>!_(t.target,e.target),St=(t,e)=>({oldValue:t,newValue:e,bodiesCreated:vt(t,e),bodiesDeleted:Ct(t,e),bodiesUpdated:Bt(t,e),targetUpdated:Mt(t,e)?{oldTarget:t.target,newTarget:e.target}:void 0});var S=(t=>(t.LOCAL="LOCAL",t.REMOTE="REMOTE",t))(S||{});const Rt=(t,e)=>{var s,a;const{changes:n,origin:o}=e;if(!(!t.options.origin||t.options.origin===o))return!1;if(t.options.ignore){const{ignore:r}=t.options,l=g=>(g==null?void 0:g.length)>0;if(!(l(n.created)||l(n.deleted))){const g=(s=n.updated)==null?void 0:s.some(p=>l(p.bodiesCreated)||l(p.bodiesDeleted)||l(p.bodiesUpdated)),d=(a=n.updated)==null?void 0:a.some(p=>p.targetUpdated);if(r==="BODY_ONLY"&&g&&!d||r==="TARGET_ONLY"&&d&&!g)return!1}}if(t.options.annotations){const r=new Set([...n.created.map(h=>h.id),...n.deleted.map(h=>h.id),...n.updated.map(({oldValue:h})=>h.id)]);return!!(Array.isArray(t.options.annotations)?t.options.annotations:[t.options.annotations]).find(h=>r.has(h))}else return!0},Lt=t=>t.id!==void 0,Ot=()=>{const t=new Map,e=new Map,n=[],o=(f,y={})=>n.push({onChange:f,options:y}),i=f=>{const y=n.findIndex(E=>E.onChange==f);y>-1&&n.splice(y,1)},s=(f,y)=>{const E={origin:f,changes:{created:y.created||[],updated:y.updated||[],deleted:y.deleted||[]},state:[...t.values()]};n.forEach(v=>{Rt(v,E)&&v.onChange(E)})},a=(f,y=S.LOCAL)=>{if(t.get(f.id))throw Error(`Cannot add annotation ${f.id} - exists already`);t.set(f.id,f),f.bodies.forEach(v=>e.set(v.id,f.id)),s(y,{created:[f]})},r=(f,y=S.LOCAL,E=S.LOCAL)=>{const v=Lt(y)?E:y,M=typeof f=="string"?y:f,R=typeof f=="string"?f:f.id,Y=t.get(R);if(Y){const tt=St(Y,M);R===M.id?t.set(R,M):(t.delete(R),t.set(M.id,M)),Y.bodies.forEach(W=>e.delete(W.id)),M.bodies.forEach(W=>e.set(W.id,M.id)),s(v,{updated:[tt]})}else throw Error(`Cannot update annotation ${R} - does not exist`)},l=(f,y=S.LOCAL)=>{const E=t.get(f.annotation);if(E){const v={...E,bodies:[...E.bodies,f]};t.set(E.id,v),e.set(f.id,v.id),s(y,{updated:[{oldValue:E,newValue:v,bodiesCreated:[f]}]})}else console.warn(`Attempt to add body to missing annotation: ${f.annotation}`)},h=()=>[...t.values()],g=(f=S.LOCAL)=>{const y=[...t.values()];t.clear(),e.clear(),s(f,{deleted:y})},d=(f,y=!0,E=S.LOCAL)=>{if(y){const v=[...t.values()];t.clear(),e.clear(),f.forEach(M=>{t.set(M.id,M),M.bodies.forEach(R=>e.set(R.id,M.id))}),s(E,{created:f,deleted:v})}else{const v=f.reduce((M,R)=>{const Y=t.get(R.id);return Y?[...M,Y]:M},[]);if(v.length>0)throw Error(`Bulk insert would overwrite the following annotations: ${v.map(M=>M.id).join(", ")}`);f.forEach(M=>{t.set(M.id,M),M.bodies.forEach(R=>e.set(R.id,M.id))}),s(E,{created:f})}},p=f=>{const y=typeof f=="string"?f:f.id,E=t.get(y);if(E)return t.delete(y),E.bodies.forEach(v=>e.delete(v.id)),E;console.warn(`Attempt to delete missing annotation: ${y}`)},B=(f,y=S.LOCAL)=>{const E=p(f);E&&s(y,{deleted:[E]})},A=(f,y=S.LOCAL)=>{const E=f.reduce((v,M)=>{const R=p(M);return R?[...v,R]:v},[]);E.length>0&&s(y,{deleted:E})},w=(f,y=S.LOCAL)=>{const E=t.get(f.annotation);if(E){const v=E.bodies.find(M=>M.id===f.id);if(v){e.delete(v.id);const M={...E,bodies:E.bodies.filter(Y=>Y.id!==f.id)};t.set(E.id,M),s(y,{updated:[{oldValue:E,newValue:M,bodiesDeleted:[v]}]})}else console.warn(`Attempt to delete missing body ${f.id} from annotation ${f.annotation}`)}else console.warn(`Attempt to delete body from missing annotation ${f.annotation}`)},x=f=>{const y=t.get(f);return y?{...y}:void 0},u=f=>{const y=e.get(f);if(y){const v=x(y).bodies.find(M=>M.id===f);if(v)return v;console.error(`Store integrity error: body ${f} in index, but not in annotation`)}else console.warn(`Attempt to retrieve missing body: ${f}`)},c=(f,y)=>{if(f.annotation!==y.annotation)throw"Annotation integrity violation: annotation ID must be the same when updating bodies";const E=t.get(f.annotation);if(E){const v=E.bodies.find(R=>R.id===f.id),M={...E,bodies:E.bodies.map(R=>R.id===v.id?y:R)};return t.set(E.id,M),v.id!==y.id&&(e.delete(v.id),e.set(y.id,M.id)),{oldValue:E,newValue:M,bodiesUpdated:[{oldBody:v,newBody:y}]}}else console.warn(`Attempt to add body to missing annotation ${f.annotation}`)},m=(f,y,E=S.LOCAL)=>{const v=c(f,y);s(E,{updated:[v]})},T=(f,y=S.LOCAL)=>{const E=f.map(v=>c({id:v.id,annotation:v.annotation},v));s(y,{updated:E})},b=f=>{const y=t.get(f.annotation);if(y){const E={...y,target:{...y.target,...f}};return t.set(y.id,E),{oldValue:y,newValue:E,targetUpdated:{oldTarget:y.target,newTarget:f}}}else console.warn(`Attempt to update target on missing annotation: ${f.annotation}`)};return{addAnnotation:a,addBody:l,all:h,bulkAddAnnotation:d,bulkDeleteAnnotation:A,bulkUpdateBodies:T,bulkUpdateTargets:(f,y=S.LOCAL)=>{const E=f.map(b).filter(v=>v);E.length>0&&s(y,{updated:E})},clear:g,deleteAnnotation:B,deleteBody:w,getAnnotation:x,getBody:u,observe:o,unobserve:i,updateAnnotation:r,updateBody:m,updateTarget:(f,y=S.LOCAL)=>{const E=b(f);E&&s(y,{updated:[E]})}}},Xt=()=>{const{subscribe:t,set:e}=J([]);return{subscribe:t,set:e}},Yt=(t,e,n,o,i,s)=>{const a=new Map;let r=[],l,h;const g=(A,w)=>{a.has(A)?a.get(A).push(w):a.set(A,[w])},d=(A,w)=>{const x=a.get(A);x&&x.indexOf(w)>0&&x.splice(x.indexOf(w),1)},p=(A,w,x)=>{a.has(A)&&setTimeout(()=>{a.get(A).forEach(u=>{if(i){const c=Array.isArray(w)?w.map(T=>i.serialize(T)):i.serialize(w),m=x?x instanceof PointerEvent?x:i.serialize(x):void 0;u(c,m)}else u(w,x)})},1)},B=()=>{const{selected:A}=e,w=A.map(({id:x})=>t.getAnnotation(x));w.forEach(x=>{const u=r.find(c=>c.id===x.id);(!u||!_(u,x))&&p("updateAnnotation",x,u)}),r=r.map(x=>{const u=w.find(({id:c})=>c===x.id);return u||x})};return e.subscribe(({selected:A})=>{if(!(r.length===0&&A.length===0)){if(r.length===0&&A.length>0)r=A.map(({id:w})=>t.getAnnotation(w));else if(r.length>0&&A.length===0)r.forEach(w=>{const x=t.getAnnotation(w.id);x&&!_(x,w)&&p("updateAnnotation",x,w)}),r=[];else{const w=new Set(r.map(c=>c.id)),x=new Set(A.map(({id:c})=>c));r.filter(c=>!x.has(c.id)).forEach(c=>{const m=t.getAnnotation(c.id);m&&!_(m,c)&&p("updateAnnotation",m,c)}),r=[...r.filter(c=>x.has(c.id)),...A.filter(({id:c})=>!w.has(c)).map(({id:c})=>t.getAnnotation(c))]}p("selectionChanged",r)}}),n.subscribe(A=>{!l&&A?p("mouseEnterAnnotation",t.getAnnotation(A)):l&&!A?p("mouseLeaveAnnotation",t.getAnnotation(l)):l&&A&&(p("mouseLeaveAnnotation",t.getAnnotation(l)),p("mouseEnterAnnotation",t.getAnnotation(A))),l=A}),o==null||o.subscribe(A=>p("viewportIntersect",A.map(t.getAnnotation))),t.observe(A=>{s&&(h&&clearTimeout(h),h=setTimeout(B,1e3));const{created:w,deleted:x}=A.changes;w.forEach(c=>p("createAnnotation",c)),x.forEach(c=>p("deleteAnnotation",c)),A.changes.updated.filter(c=>[...c.bodiesCreated||[],...c.bodiesDeleted||[],...c.bodiesUpdated||[]].length>0).forEach(({oldValue:c,newValue:m})=>{const T=r.find(b=>b.id===c.id)||c;r=r.map(b=>b.id===c.id?m:b),p("updateAnnotation",m,T)})},{origin:S.LOCAL}),t.observe(A=>{if(r){const w=new Set(r.map(u=>u.id)),x=A.changes.updated.filter(({newValue:u})=>w.has(u.id)).map(({newValue:u})=>u);x.length>0&&(r=r.map(u=>{const c=x.find(m=>m.id===u.id);return c||u}))}},{origin:S.REMOTE}),{on:g,off:d,emit:p}},_t=t=>e=>e.map(n=>t.serialize(n)),rt=t=>e=>e.reduce((n,o)=>{const{parsed:i,error:s}=t.parse(o);return s?{parsed:n.parsed,failed:[...n.failed,o]}:{parsed:[...n.parsed,i],failed:n.failed}},{parsed:[],failed:[]}),at=(t,e)=>{const n=g=>{if(e){const{parsed:d,error:p}=e.parse(g);d?t.addAnnotation(d,S.REMOTE):console.error(p)}else t.addAnnotation(g,S.REMOTE)},o=()=>t.clear(),i=g=>{const d=t.getAnnotation(g);return e&&d?e.serialize(d):d},s=()=>e?t.all().map(e.serialize):t.all(),a=g=>fetch(g).then(d=>d.json()).then(d=>(l(d),d)),r=g=>{if(typeof g=="string"){const d=t.getAnnotation(g);return t.deleteAnnotation(g),e?e.serialize(d):d}else{const d=e?e.parse(g).parsed:g;return t.deleteAnnotation(d),g}},l=g=>{if(e){const{parsed:d,failed:p}=rt(e)(g);p.length>0&&console.warn(`Discarded ${p.length} invalid annotations`,p),t.bulkAddAnnotation(d,!0,S.REMOTE)}else t.bulkAddAnnotation(g,!0,S.REMOTE)};return{addAnnotation:n,clearAnnotations:o,getAnnotationById:i,getAnnotations:s,loadAnnotations:a,removeAnnotation:r,setAnnotations:l,updateAnnotation:g=>{if(e){const d=e.parse(g).parsed,p=e.serialize(t.getAnnotation(d.id));return t.updateAnnotation(d),p}else{const d=t.getAnnotation(g.id);return t.updateAnnotation(g),d}}}};let It=t=>crypto.getRandomValues(new Uint8Array(t)),Dt=(t,e,n)=>{let o=(2<<Math.log(t.length-1)/Math.LN2)-1,i=-~(1.6*o*e/t.length);return(s=e)=>{let a="";for(;;){let r=n(i),l=i;for(;l--;)if(a+=t[r[l]&o]||"",a.length===s)return a}}},Ut=(t,e=21)=>Dt(t,e,It),Nt=(t=21)=>crypto.getRandomValues(new Uint8Array(t)).reduce((e,n)=>(n&=63,n<36?e+=n.toString(36):n<62?e+=(n-26).toString(36).toUpperCase():n>62?e+="-":e+="_",e),"");const ct=()=>({isGuest:!0,id:Ut("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",20)()}),kt=t=>{const e=JSON.stringify(t);let n=0;for(let o=0,i=e.length;o<i;o++){let s=e.charCodeAt(o);n=(n<<5)-n+s,n|=0}return`${n}`},zt=(t,e)=>(Array.isArray(t)?t:[t]).map(n=>{const{id:o,type:i,purpose:s,value:a,created:r,creator:l,...h}=n;return{id:o||`temp-${kt(n)}`,annotation:e,type:i,purpose:s,value:a,created:r,creator:l?typeof l=="object"?{...l}:l:void 0,...h}}),Pt=t=>t.map(e=>{var o;const n={...e};return delete n.annotation,(o=n.id)!=null&&o.startsWith("temp-")&&delete n.id,n});Nt();function Ht(t,e,n,o,i){lt(t,e,n||0,o||t.length-1,i||Wt)}function lt(t,e,n,o,i){for(;o>n;){if(o-n>600){var s=o-n+1,a=e-n+1,r=Math.log(s),l=.5*Math.exp(2*r/3),h=.5*Math.sqrt(r*l*(s-l)/s)*(a-s/2<0?-1:1),g=Math.max(n,Math.floor(e-a*l/s+h)),d=Math.min(o,Math.floor(e+(s-a)*l/s+h));lt(t,e,g,d,i)}var p=t[e],B=n,A=o;for(z(t,n,e),i(t[o],p)>0&&z(t,n,o);B<A;){for(z(t,B,A),B++,A--;i(t[B],p)<0;)B++;for(;i(t[A],p)>0;)A--}i(t[n],p)===0?z(t,n,A):(A++,z(t,A,o)),A<=e&&(n=A+1),e<=A&&(o=A-1)}}function z(t,e,n){var o=t[e];t[e]=t[n],t[n]=o}function Wt(t,e){return t<e?-1:t>e?1:0}class $t{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 n=this.data;const o=[];if(!q(e,n))return o;const i=this.toBBox,s=[];for(;n;){for(let a=0;a<n.children.length;a++){const r=n.children[a],l=n.leaf?i(r):r;q(e,l)&&(n.leaf?o.push(r):Z(e,l)?this._all(r,o):s.push(r))}n=s.pop()}return o}collides(e){let n=this.data;if(!q(e,n))return!1;const o=[];for(;n;){for(let i=0;i<n.children.length;i++){const s=n.children[i],a=n.leaf?this.toBBox(s):s;if(q(e,a)){if(n.leaf||Z(e,a))return!0;o.push(s)}}n=o.pop()}return!1}load(e){if(!(e&&e.length))return this;if(e.length<this._minEntries){for(let o=0;o<e.length;o++)this.insert(e[o]);return this}let n=this._build(e.slice(),0,e.length-1,0);if(!this.data.children.length)this.data=n;else if(this.data.height===n.height)this._splitRoot(this.data,n);else{if(this.data.height<n.height){const o=this.data;this.data=n,n=o}this._insert(n,this.data.height-n.height-1,!0)}return this}insert(e){return e&&this._insert(e,this.data.height-1),this}clear(){return this.data=N([]),this}remove(e,n){if(!e)return this;let o=this.data;const i=this.toBBox(e),s=[],a=[];let r,l,h;for(;o||s.length;){if(o||(o=s.pop(),l=s[s.length-1],r=a.pop(),h=!0),o.leaf){const g=Vt(e,o.children,n);if(g!==-1)return o.children.splice(g,1),s.push(o),this._condense(s),this}!h&&!o.leaf&&Z(o,i)?(s.push(o),a.push(r),r=0,l=o,o=o.children[0]):l?(r++,o=l.children[r],h=!1):o=null}return this}toBBox(e){return e}compareMinX(e,n){return e.minX-n.minX}compareMinY(e,n){return e.minY-n.minY}toJSON(){return this.data}fromJSON(e){return this.data=e,this}_all(e,n){const o=[];for(;e;)e.leaf?n.push(...e.children):o.push(...e.children),e=o.pop();return n}_build(e,n,o,i){const s=o-n+1;let a=this._maxEntries,r;if(s<=a)return r=N(e.slice(n,o+1)),U(r,this.toBBox),r;i||(i=Math.ceil(Math.log(s)/Math.log(a)),a=Math.ceil(s/Math.pow(a,i-1))),r=N([]),r.leaf=!1,r.height=i;const l=Math.ceil(s/a),h=l*Math.ceil(Math.sqrt(a));dt(e,n,o,h,this.compareMinX);for(let g=n;g<=o;g+=h){const d=Math.min(g+h-1,o);dt(e,g,d,l,this.compareMinY);for(let p=g;p<=d;p+=l){const B=Math.min(p+l-1,d);r.children.push(this._build(e,p,B,i-1))}}return U(r,this.toBBox),r}_chooseSubtree(e,n,o,i){for(;i.push(n),!(n.leaf||i.length-1===o);){let s=1/0,a=1/0,r;for(let l=0;l<n.children.length;l++){const h=n.children[l],g=Q(h),d=jt(e,h)-g;d<a?(a=d,s=g<s?g:s,r=h):d===a&&g<s&&(s=g,r=h)}n=r||n.children[0]}return n}_insert(e,n,o){const i=o?e:this.toBBox(e),s=[],a=this._chooseSubtree(i,this.data,n,s);for(a.children.push(e),H(a,i);n>=0&&s[n].children.length>this._maxEntries;)this._split(s,n),n--;this._adjustParentBBoxes(i,s,n)}_split(e,n){const o=e[n],i=o.children.length,s=this._minEntries;this._chooseSplitAxis(o,s,i);const a=this._chooseSplitIndex(o,s,i),r=N(o.children.splice(a,o.children.length-a));r.height=o.height,r.leaf=o.leaf,U(o,this.toBBox),U(r,this.toBBox),n?e[n-1].children.push(r):this._splitRoot(o,r)}_splitRoot(e,n){this.data=N([e,n]),this.data.height=e.height+1,this.data.leaf=!1,U(this.data,this.toBBox)}_chooseSplitIndex(e,n,o){let i,s=1/0,a=1/0;for(let r=n;r<=o-n;r++){const l=P(e,0,r,this.toBBox),h=P(e,r,o,this.toBBox),g=Gt(l,h),d=Q(l)+Q(h);g<s?(s=g,i=r,a=d<a?d:a):g===s&&d<a&&(a=d,i=r)}return i||o-n}_chooseSplitAxis(e,n,o){const i=e.leaf?this.compareMinX:qt,s=e.leaf?this.compareMinY:Ft,a=this._allDistMargin(e,n,o,i),r=this._allDistMargin(e,n,o,s);a<r&&e.children.sort(i)}_allDistMargin(e,n,o,i){e.children.sort(i);const s=this.toBBox,a=P(e,0,n,s),r=P(e,o-n,o,s);let l=V(a)+V(r);for(let h=n;h<o-n;h++){const g=e.children[h];H(a,e.leaf?s(g):g),l+=V(a)}for(let h=o-n-1;h>=n;h--){const g=e.children[h];H(r,e.leaf?s(g):g),l+=V(r)}return l}_adjustParentBBoxes(e,n,o){for(let i=o;i>=0;i--)H(n[i],e)}_condense(e){for(let n=e.length-1,o;n>=0;n--)e[n].children.length===0?n>0?(o=e[n-1].children,o.splice(o.indexOf(e[n]),1)):this.clear():U(e[n],this.toBBox)}}function Vt(t,e,n){if(!n)return e.indexOf(t);for(let o=0;o<e.length;o++)if(n(t,e[o]))return o;return-1}function U(t,e){P(t,0,t.children.length,e,t)}function P(t,e,n,o,i){i||(i=N(null)),i.minX=1/0,i.minY=1/0,i.maxX=-1/0,i.maxY=-1/0;for(let s=e;s<n;s++){const a=t.children[s];H(i,t.leaf?o(a):a)}return i}function H(t,e){return t.minX=Math.min(t.minX,e.minX),t.minY=Math.min(t.minY,e.minY),t.maxX=Math.max(t.maxX,e.maxX),t.maxY=Math.max(t.maxY,e.maxY),t}function qt(t,e){return t.minX-e.minX}function Ft(t,e){return t.minY-e.minY}function Q(t){return(t.maxX-t.minX)*(t.maxY-t.minY)}function V(t){return t.maxX-t.minX+(t.maxY-t.minY)}function jt(t,e){return(Math.max(e.maxX,t.maxX)-Math.min(e.minX,t.minX))*(Math.max(e.maxY,t.maxY)-Math.min(e.minY,t.minY))}function Gt(t,e){const n=Math.max(t.minX,e.minX),o=Math.max(t.minY,e.minY),i=Math.min(t.maxX,e.maxX),s=Math.min(t.maxY,e.maxY);return Math.max(0,i-n)*Math.max(0,s-o)}function Z(t,e){return t.minX<=e.minX&&t.minY<=e.minY&&e.maxX<=t.maxX&&e.maxY<=t.maxY}function q(t,e){return e.minX<=t.maxX&&e.minY<=t.maxY&&e.maxX>=t.minX&&e.maxY>=t.minY}function N(t){return{children:t,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function dt(t,e,n,o,i){const s=[e,n];for(;s.length;){if(n=s.pop(),e=s.pop(),n-e<=o)continue;const a=e+Math.ceil((n-e)/o/2)*o;Ht(t,a,e,n,i),s.push(e,a,a,n)}}const Jt=(t,e)=>{const n=s=>Math.round(s*10)/10,o={top:n(t.top),bottom:n(t.bottom),left:n(t.left),right:n(t.right)},i={top:n(e.top),bottom:n(e.bottom),left:n(e.left),right:n(e.right)};if(Math.abs(o.top-i.top)<.5&&Math.abs(o.bottom-i.bottom)<.5){if(Math.abs(o.left-i.right)<.5||Math.abs(o.right-i.left)<.5)return"inline-adjacent";if(o.left>=i.left&&o.right<=i.right)return"inline-is-contained";if(o.left<=i.left&&o.right>=i.right)return"inline-contains"}else if(o.top<=i.top&&o.bottom>=i.bottom){if(o.left<=i.left&&o.right>=i.right)return"block-contains"}else if(o.top>=i.top&&o.bottom<=i.bottom&&o.left>=i.left&&o.right<=i.right)return"block-is-contained"},Kt=(t,e)=>{const n=Math.min(t.left,e.left),o=Math.max(t.right,e.right),i=Math.min(t.top,e.top),s=Math.max(t.bottom,e.bottom);return new DOMRect(n,i,o-n,s-i)},Qt=t=>t.reduce((e,n)=>{if(n.width===0||n.height===0)return e;let o=[...e],i=!1;for(const s of e){const a=Jt(n,s);if(a==="inline-adjacent"){o=o.map(r=>r===s?Kt(n,s):r),i=!0;break}else if(a==="inline-contains"){o=o.map(r=>r===s?n:r),i=!0;break}else if(a==="inline-is-contained"){i=!0;break}else if(a==="block-contains"||a==="block-is-contained"){n.width<s.width&&(o=o.map(r=>r===s?n:r)),i=!0;break}}return i?o:[...o,n]},[]),Zt=t=>{const{startContainer:e,endContainer:n}=t;if(e.nodeType===Node.TEXT_NODE&&n.nodeType===Node.TEXT_NODE)return t;if(e.nodeType!==Node.TEXT_NODE){const i=e.nextSibling||e.parentNode,s=i.nodeType===Node.TEXT_NODE?i:Array.from(i.childNodes).filter(a=>a.nodeType===Node.TEXT_NODE).shift();t.setEnd(s,0)}if(n.nodeType!==Node.TEXT_NODE){const i=n.previousSibling||n.parentNode,s=i.nodeType===Node.TEXT_NODE?i:Array.from(i.childNodes).filter(a=>a.nodeType===Node.TEXT_NODE).pop();t.setEnd(s,s.textContent.length)}return t},te=(t,e)=>{const n=new $t,o=new Map,i=u=>{const c=e.getBoundingClientRect(),T=u.selector.range instanceof Range&&!u.selector.range.collapsed&&u.selector.range.startContainer.nodeType===Node.TEXT_NODE&&u.selector.range.endContainer.nodeType===Node.TEXT_NODE?u:I(u,e),b=Array.from(T.selector.range.getClientRects()),C=Qt(b);return C.map(L=>{const{x:f,y,width:E,height:v}=L;return{minX:f-c.x,minY:y-c.y,maxX:f-c.x+E,maxY:y-c.y+v,annotation:{id:u.annotation,rects:C}}})},s=()=>[...o.values()],a=()=>{n.clear(),o.clear()},r=u=>{const c=i(u);c.forEach(m=>n.insert(m)),o.set(u.annotation,c)},l=u=>{o.get(u.annotation).forEach(m=>n.remove(m)),o.delete(u.annotation)},h=u=>{l(u),r(u)},g=(u,c=!0)=>{c&&a();const m=u.map(b=>({target:b,rects:i(b)}));m.forEach(({target:b,rects:C})=>o.set(b.annotation,C));const T=m.reduce((b,{rects:C})=>[...b,...C],[]);n.load(T)},d=(u,c)=>{const m=n.search({minX:u,minY:c,maxX:u,maxY:c}),T=b=>b.annotation.rects.reduce((C,L)=>C+L.width*L.height,0);if(m.length>0)return m.sort((b,C)=>T(b)-T(C)),m[0].annotation.id},p=u=>{const c=B(u);if(c.length===0)return;let m=c[0].left,T=c[0].top,b=c[0].right,C=c[0].bottom;for(let L=1;L<c.length;L++){const f=c[L];m=Math.min(m,f.left),T=Math.min(T,f.top),b=Math.max(b,f.right),C=Math.max(C,f.bottom)}return new DOMRect(m,T,b-m,C-T)},B=u=>{const c=o.get(u);return c?c[0].annotation.rects:[]};return{all:s,clear:a,getAt:d,getBoundsForAnnotation:p,getDOMRectsForAnnotation:B,getIntersectingRects:(u,c,m,T)=>n.search({minX:u,minY:c,maxX:m,maxY:T}),insert:r,recalculate:()=>g(t.all().map(u=>u.target),!0),remove:l,set:g,size:()=>n.all().length,update:h}},F=t=>{const{range:e}=t.target.selector;return e instanceof Range&&!e.collapsed},ht=(t,e)=>{const n=Ot(),o=te(n,t),i=xt(n,e),s=At(n),a=Xt(),r=(x,u=S.LOCAL)=>{const c=x.target.selector.range instanceof Range?x:{...x,target:I(x.target,t)},{range:m}=c.target.selector,T=m&&!m.collapsed;return T&&n.addAnnotation(c,u),T},l=(x,u=!0,c=S.LOCAL)=>{const m=x.map(b=>F(b)?b:{...b,target:I(b.target,t)});if(m.some(b=>b.target.selector.range.collapsed)){const b=m.filter(C=>C.target.selector.range.collapsed);return console.warn("Could not revive all targets"),console.warn(b),n.bulkAddAnnotation(m,u,c),b}else return n.bulkAddAnnotation(m,u,c),[]},h=(x,u=S.LOCAL)=>{const c=x.selector.range instanceof Range?x:I(x,t);n.updateTarget(c,u)},g=(x,u=S.LOCAL)=>{const c=x.map(m=>m.selector.range instanceof Range?m:I(m,t));n.bulkUpdateTargets(c,u)},d=(x,u)=>{const c=o.getAt(x,u);return c?n.getAnnotation(c):void 0},p=(x,u,c,m)=>{const T=o.getIntersectingRects(x,u,c,m);return Array.from(new Set(T.map(C=>C.annotation.id))).map(C=>n.getAnnotation(C)).filter(C=>C)},B=(x,u,c,m=5)=>{const T=o.getDOMRectsForAnnotation(x);if(T.length>0){if(u&&c){const b=T.find(({top:C,right:L,bottom:f,left:y})=>u>=y-m&&u<=L+m&&c>=C-m&&c<=f+m);if(b)return b}return o.getBoundsForAnnotation(x)}},A=(x,u,c,m)=>{const b=o.getIntersectingRects(x,u,c,m).reduce((C,L)=>((C[L.annotation.id]=C[L.annotation.id]||[]).push(L),C),{});return Object.entries(b).map(([C,L])=>({annotation:n.getAnnotation(C),rects:L.map(({minX:f,minY:y,maxX:E,maxY:v})=>({x:f,y,width:E-f,height:v-y}))}))},w=()=>o.recalculate();return n.observe(({changes:x})=>{const u=(x.created||[]).filter(F),c=(x.deleted||[]).filter(F),m=(x.updated||[]).filter(T=>F(T.newValue));u.length>0&&o.set(u.map(T=>T.target),!1),(c==null?void 0:c.length)>0&&c.forEach(T=>o.remove(T.target)),(m==null?void 0:m.length)>0&&m.forEach(({newValue:T})=>o.update(T.target))}),{store:{...n,addAnnotation:r,bulkAddAnnotation:l,bulkUpdateTargets:g,getAnnotationBounds:B,getAt:d,getIntersecting:p,getIntersectingRects:A,recalculatePositions:w,updateTarget:h},selection:i,hover:s,viewport:a}},ee=(t,e={})=>{const n=new Map,o=s=>Array.from(n.entries()).filter(([a,r])=>r.presenceKey===s.presenceKey).map(([a,r])=>a);return t.on("selectionChange",(s,a)=>{o(s).forEach(l=>n.delete(l)),a&&a.forEach(l=>n.set(l,s))}),{paint:(s,a,r,l,h,g)=>{e.font&&(l.font=e.font);const d=n.get(s.id);if(d){const{x:p,y:B,height:A}=a[0];l.fillStyle=d.appearance.color,l.fillRect(p-2,B-2.5,2,A+5);const w=l.measureText(d.appearance.label),x=w.width+6,u=w.actualBoundingBoxAscent+w.actualBoundingBoxDescent+8,c=w.fontBoundingBoxAscent?8:6.5;l.fillRect(p-2,B-2.5-u,x,u),l.fillStyle="#fff",l.fillText(d.appearance.label,p+1,B-c),r.fillStyle=d.appearance.color,r.globalAlpha=h?.45:.18,a.forEach(({x:m,y:T,width:b,height:C})=>r.fillRect(m,T-2.5,b,C+5))}else j.paint(s,a,r,l,h,g)}}},ut=t=>t===null?null:t.scrollHeight>t.clientHeight?t:ut(t.parentElement),ne=(t,e)=>n=>{const o=ut(t);if(o){const i=e.getAnnotation(n.id),{range:s}=i.target.selector;if(!s||s.collapsed)return!1;const a=o.getBoundingClientRect(),r=o.clientHeight,l=o.clientWidth,h=i.target.selector.range.getBoundingClientRect(),{width:g,height:d}=e.getAnnotationBounds(n.id),p=h.top-a.top,B=h.left-a.left,A=o.parentElement?o.scrollTop:0,w=o.parentElement?o.scrollLeft:0,x=p+A-(r-d)/2,u=B+w-(l-g)/2;return o.scroll({top:x,left:u,behavior:"smooth"}),!0}},oe=(t,e,n)=>{const o=document.createRange(),i=n?t.startContainer.parentElement.closest(n):e;o.setStart(i,0),o.setEnd(t.startContainer,t.startOffset);const s=t.toString(),a=o.toString().length,r=a+s.length;return n?{quote:s,start:a,end:r,range:t,offsetReference:i}:{quote:s,start:a,end:r,range:t}},ie=(t,e,n)=>{const{store:o,selection:i}=e;let s,a=null;const r=p=>s=p;let l=!1,h;t.addEventListener("selectstart",p=>{if(!l)return;!p.target.parentElement.closest(".not-annotatable")?a={annotation:Tt(),selector:void 0,creator:s,created:new Date}:(a=null,h=void 0)});let g;document.addEventListener("selectionchange",p=>{g&&clearTimeout(g),g=setTimeout(()=>d(),50)});const d=()=>{var B,A;const p=document.getSelection();if(!p.isCollapsed&&l&&a){const w=Array.from(Array(p.rangeCount).keys()).map(c=>p.getRangeAt(c));Zt(w[0]).toString()!==((A=(B=a.selector)==null?void 0:B.range)==null?void 0:A.toString())&&(a={...a,selector:oe(w[0],t,n)},o.getAnnotation(a.annotation)?o.updateTarget(a,S.LOCAL):(o.addAnnotation({id:a.annotation,bodies:[],target:a}),i.clickSelect(a.annotation,h)))}};return t.addEventListener("pointerdown",p=>{h=p,l=p.button===0}),document.addEventListener("pointerup",p=>{var A;h=p,!((A=p.target.parentElement)!=null&&A.closest(".not-annotatable")||!l)&&setTimeout(()=>{if(a!=null&&a.selector)o.updateTarget(a,S.LOCAL),i.clickSelect(a.annotation,p),a=null,h=void 0;else{const{x:w,y:x}=t.getBoundingClientRect(),u=o.getAt(p.clientX-w,p.clientY-x);if(u){const{selected:c}=i;(c.length!==1||c[0].id!==u.id)&&(i.clickSelect(u.id,p),h=void 0)}else i.isEmpty()||i.clear()}},50)}),{setUser:r}},fe="",se=(t,e={})=>{const n=ht(t,e.pointerAction),{hover:o,selection:i,viewport:s}=n,a=n.store,r=Yt(a,i,o,s);let l=ct(),h=e.style;const g=ot(t,n,s),d=ie(t,n,e.offsetReferenceSelector);d.setUser(l);const p=at(a,e.adapter),B=m=>{h=m,g.setDrawingStyle(m)};return{...p,destroy:()=>{throw"Not implemented yet"},element:t,getUser:()=>l,setUser:m=>{l=m,d.setUser(m)},setSelected:m=>{m?i.setSelected(m):i.clear()},setPresenceProvider:m=>{m&&(g.setPainter(ee(m,e.presence)),m.on("selectionChange",()=>g.redraw()))},on:r.on,off:r.off,scrollIntoView:ne(t,a),state:n,get style(){return h},set style(m){B(m)}}},re=S;O.Origin=re,O.createAnonymousGuest=ct,O.createBaseAnnotator=at,O.createHighlightLayer=ot,O.createTextAnnotator=se,O.createTextAnnotatorState=ht,O.defaultPainter=j,O.parseAll=rt,O.parseW3CBodies=zt,O.reviveTarget=I,O.serializeAll=_t,O.serializeW3CBodies=Pt,Object.defineProperty(O,Symbol.toStringTag,{value:"Module"})});
1
+ (function(X,N){typeof exports=="object"&&typeof module<"u"?N(exports):typeof define=="function"&&define.amd?define(["exports"],N):(X=typeof globalThis<"u"?globalThis:X||self,N(X.RecogitoJS={}))})(this,function(X){"use strict";const N={fill:"rgb(0, 128, 255",fillOpacity:.18},dt={fill:"rgb(0, 128, 255)",fillOpacity:.45},j={paint:(e,t,n,o,i,s)=>{const a=s?typeof s=="function"?s(e,i):s:i?dt:N;n.fillStyle=a.fill,n.globalAlpha=a.fillOpacity||1,t.forEach(({x:r,y:d,width:h,height:w})=>n.fillRect(r,d-2.5,h,w+5))}},ut=e=>{let t=new Set;return o=>{const i=o.map(s=>s.id);(t.size!==i.length||i.some(s=>!t.has(s)))&&e.set(i),t=new Set(i)}},ht=(e,t=10)=>{let n;return(...o)=>{clearTimeout(n),n=setTimeout(()=>e.apply(void 0,o),t)}},tt=(e,t)=>{const n=document.createElement("canvas");if(n.width=t?2*window.innerWidth:window.innerWidth,n.height=t?2*window.innerHeight:window.innerHeight,n.className=e,t){const o=n.getContext("2d");o.scale(2,2),o.translate(.5,.5)}return n},et=(e,t)=>{if(e.width=t?2*window.innerWidth:window.innerWidth,e.height=t?2*window.innerHeight:window.innerHeight,t){const n=e.getContext("2d");n.scale(2,2),n.translate(.5,.5)}},nt=(e,t,n)=>{const{store:o,selection:i,hover:s}=t;let a,r,d=j;const h=ut(n);e.classList.add("r6o-annotatable");const w=tt("r6o-highlight-layer bg"),f=tt("r6o-highlight-layer fg",!0),T=w.getContext("2d"),p=f.getContext("2d");e.insertBefore(w,e.firstChild),e.appendChild(f),e.addEventListener("pointermove",E=>{const{x:v,y:L}=e.getBoundingClientRect(),g=o.getAt(E.clientX-v,E.clientY-L);g&&(!r||r(g))?s.current!==g.id&&(e.classList.add("hovered"),s.set(g.id)):s.current&&(e.classList.remove("hovered"),s.set(null))});const y=()=>{const{top:E,left:v}=e.getBoundingClientRect(),{innerWidth:L,innerHeight:g}=window,x=-v,b=-E,S=L-v,M=g-E;return{top:E,left:v,minX:x,minY:b,maxX:S,maxY:M}},B=()=>c();document.addEventListener("scroll",B,{capture:!0,passive:!0});const A=ht(()=>{et(w),et(f,!0),o.recalculatePositions(),c()});window.addEventListener("resize",A),new ResizeObserver(A).observe(e);const c=()=>requestAnimationFrame(()=>{const{top:E,left:v,minX:L,minY:g,maxX:x,maxY:b}=y(),S=r?o.getIntersectingRects(L,g,x,b).filter(({annotation:Y})=>r(Y)):o.getIntersectingRects(L,g,x,b),{width:M,height:R}=f,U=new Set(i.selected.map(({id:Y})=>Y));p.clearRect(-.5,-.5,M+1,R+1),T.clearRect(-.5,-.5,M+1,R+1),S.forEach(({annotation:Y,rects:ae})=>{const re=ae.map(({x:ce,y:de,width:ue,height:he})=>({x:ce+v,y:de+E,width:ue,height:he})),le=U.has(Y.id);d.paint(Y,re,T,p,le,a)}),setTimeout(()=>h(S.map(({annotation:Y})=>Y)),1)});return o.observe(()=>c()),i.subscribe(()=>c()),{redraw:c,setDrawingStyle:E=>{a=E,c()},setFilter:E=>{r=E,c()},setPainter:E=>d=E}},D=(e,t)=>{const{start:n,end:o}=e.selector,i=e.selector.offsetReference?e.selector.offsetReference:t;if(!i)return e;const s=document.createNodeIterator(t,NodeFilter.SHOW_TEXT);let a=0,r=document.createRange(),d=s.nextNode();d===null&&console.error("Could not revive annotation target. Content missing.");let h=!i;for(;d!==null;){const w=d.textContent.length;if(!h&&i&&(h=i.contains(d)),h){if(a+w>n){r.setStart(d,n-a);break}a+=w}d=s.nextNode()}for(;d!==null;){const w=d.textContent.length;if(a+w>o){r.setEnd(d,o-a);break}a+=w,d=s.nextNode()}return{...e,selector:{...e.selector,range:r}}};var ot=Object.prototype.hasOwnProperty;function I(e,t){var n,o;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((o=e.length)===t.length)for(;o--&&I(e[o],t[o]););return o===-1}if(!n||typeof e=="object"){o=0;for(n in e)if(ot.call(e,n)&&++o&&!ot.call(t,n)||!(n in t)||!I(e[n],t[n]))return!1;return Object.keys(t).length===o}}return e!==e&&t!==t}function G(){}function ft(e,t){return e!=e?t==t:e!==t||e&&typeof e=="object"||typeof e=="function"}const V=[];function J(e,t=G){let n;const o=new Set;function i(r){if(ft(e,r)&&(e=r,n)){const d=!V.length;for(const h of o)h[1](),V.push(h,e);if(d){for(let h=0;h<V.length;h+=2)V[h][0](V[h+1]);V.length=0}}}function s(r){i(r(e))}function a(r,d=G){const h=[r,d];return o.add(h),o.size===1&&(n=t(i,s)||G),r(e),()=>{o.delete(h),o.size===0&&n&&(n(),n=null)}}return{set:i,update:s,subscribe:a}}const gt=e=>{const{subscribe:t,set:n}=J();let o;return t(i=>o=i),e.observe(({changes:i})=>{if(o){(i.deleted||[]).some(a=>a.id===o)&&n(void 0);const s=(i.updated||[]).find(({oldValue:a})=>a.id===o);s&&n(s.newValue.id)}}),{get current(){return o},subscribe:t,set:n}},K={selected:[]},pt=(e,t="EDIT")=>{const{subscribe:n,set:o}=J(K);let i=K;n(f=>i=f);const s=()=>o(K),a=()=>{var f;return((f=i.selected)==null?void 0:f.length)===0},r=f=>{if(i.selected.length===0)return!1;const T=typeof f=="string"?f:f.id;return i.selected.some(p=>p.id===T)},d=(f,T)=>{const p=e.getAnnotation(f);if(p){const y=mt(p,t);o(y==="EDIT"?{selected:[{id:f,editable:!0}],pointerEvent:T}:y==="SELECT"?{selected:[{id:f}],pointerEvent:T}:{selected:[],pointerEvent:T})}else console.warn("Invalid selection: "+f)},h=(f,T=!0)=>{const p=Array.isArray(f)?f:[f],y=p.map(B=>e.getAnnotation(B)).filter(Boolean);o({selected:y.map(({id:B})=>({id:B,editable:T}))}),y.length!==p.length&&console.warn("Invalid selection",f)},w=f=>{if(i.selected.length===0)return!1;const{selected:T}=i;T.filter(({id:p})=>f.includes(p)).length>0&&o({selected:T.filter(({id:p})=>!f.includes(p))})};return e.observe(({changes:f})=>w((f.deleted||[]).map(T=>T.id))),{clear:s,clickSelect:d,get selected(){return i?[...i.selected]:null},get pointerEvent(){return i?i.pointerEvent:null},isEmpty:a,isSelected:r,setSelected:h,subscribe:n}},mt=(e,t)=>typeof t=="function"?t(e)||"EDIT":t||"EDIT",bt=[];for(let e=0;e<256;++e)bt.push((e+256).toString(16).slice(1));typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto);const wt=(e,t)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},At=(e,t)=>{const n=new Set(t.bodies.map(o=>o.id));return e.bodies.filter(o=>!n.has(o.id))},xt=(e,t)=>t.bodies.map(n=>{const o=e.bodies.find(i=>i.id===n.id);return{newBody:n,oldBody:o&&!I(o,n)?o:void 0}}).filter(({oldBody:n})=>n).map(({oldBody:n,newBody:o})=>({oldBody:n,newBody:o})),yt=(e,t)=>!I(e.target,t.target),it=(e,t)=>{const n=wt(e,t),o=At(e,t),i=xt(e,t);return{oldValue:e,newValue:t,bodiesCreated:n.length>0?n:void 0,bodiesDeleted:o.length>0?o:void 0,bodiesUpdated:i.length>0?i:void 0,targetUpdated:yt(e,t)?{oldTarget:e.target,newTarget:t.target}:void 0}};var C=(e=>(e.LOCAL="LOCAL",e.REMOTE="REMOTE",e))(C||{});const Et=(e,t)=>{var n,o;const{changes:i,origin:s}=t;if(!(!e.options.origin||e.options.origin===s))return!1;if(e.options.ignore){const{ignore:a}=e.options,r=d=>d&&d.length>0;if(!(r(i.created)||r(i.deleted))){const d=(n=i.updated)==null?void 0:n.some(w=>r(w.bodiesCreated)||r(w.bodiesDeleted)||r(w.bodiesUpdated)),h=(o=i.updated)==null?void 0:o.some(w=>w.targetUpdated);if(a==="BODY_ONLY"&&d&&!h||a==="TARGET_ONLY"&&h&&!d)return!1}}if(e.options.annotations){const a=new Set([...(i.created||[]).map(r=>r.id),...(i.deleted||[]).map(r=>r.id),...(i.updated||[]).map(({oldValue:r})=>r.id)]);return!!(Array.isArray(e.options.annotations)?e.options.annotations:[e.options.annotations]).find(r=>a.has(r))}else return!0},vt=(e,t)=>{const n=new Set((e.created||[]).map(f=>f.id)),o=new Set((e.updated||[]).map(({newValue:f})=>f.id)),i=new Set((t.created||[]).map(f=>f.id)),s=new Set((t.deleted||[]).map(f=>f.id)),a=new Set((t.updated||[]).map(({oldValue:f})=>f.id)),r=new Set((t.updated||[]).filter(({oldValue:f})=>n.has(f.id)||o.has(f.id)).map(({oldValue:f})=>f.id)),d=[...(e.created||[]).filter(f=>!s.has(f.id)).map(f=>a.has(f.id)?t.updated.find(({oldValue:T})=>T.id===f.id).newValue:f),...t.created||[]],h=[...(e.deleted||[]).filter(f=>!i.has(f.id)),...(t.deleted||[]).filter(f=>!n.has(f.id))],w=[...(e.updated||[]).filter(({newValue:f})=>!s.has(f.id)).map(f=>{const{oldValue:T,newValue:p}=f;if(a.has(p.id)){const y=t.updated.find(B=>B.oldValue.id===p.id).newValue;return it(T,y)}else return f}),...(t.updated||[]).filter(({oldValue:f})=>!r.has(f.id))];return{created:d,deleted:h,updated:w}},St=e=>e.id!==void 0,Tt=()=>{const e=new Map,t=new Map,n=[],o=(g,x={})=>n.push({onChange:g,options:x}),i=g=>{const x=n.findIndex(b=>b.onChange==g);x>-1&&n.splice(x,1)},s=(g,x)=>{const b={origin:g,changes:{created:x.created||[],updated:x.updated||[],deleted:x.deleted||[]},state:[...e.values()]};n.forEach(S=>{Et(S,b)&&S.onChange(b)})},a=(g,x=C.LOCAL)=>{if(e.get(g.id))throw Error(`Cannot add annotation ${g.id} - exists already`);e.set(g.id,g),g.bodies.forEach(b=>t.set(b.id,g.id)),s(x,{created:[g]})},r=(g,x)=>{const b=typeof g=="string"?x:g,S=typeof g=="string"?g:g.id,M=e.get(S);if(M){const R=it(M,b);return S===b.id?e.set(S,b):(e.delete(S),e.set(b.id,b)),M.bodies.forEach(U=>t.delete(U.id)),b.bodies.forEach(U=>t.set(U.id,b.id)),R}else console.warn(`Cannot update annotation ${S} - does not exist`)},d=(g,x=C.LOCAL,b=C.LOCAL)=>{const S=St(x)?b:x,M=r(g,x);M&&s(S,{updated:[M]})},h=(g,x=C.LOCAL)=>{const b=g.reduce((S,M)=>{const R=r(M);return R?[...S,R]:S},[]);b.length>0&&s(x,{updated:b})},w=(g,x=C.LOCAL)=>{const b=e.get(g.annotation);if(b){const S={...b,bodies:[...b.bodies,g]};e.set(b.id,S),t.set(g.id,S.id),s(x,{updated:[{oldValue:b,newValue:S,bodiesCreated:[g]}]})}else console.warn(`Attempt to add body to missing annotation: ${g.annotation}`)},f=()=>[...e.values()],T=(g=C.LOCAL)=>{const x=[...e.values()];e.clear(),t.clear(),s(g,{deleted:x})},p=(g,x=!0,b=C.LOCAL)=>{if(x){const S=[...e.values()];e.clear(),t.clear(),g.forEach(M=>{e.set(M.id,M),M.bodies.forEach(R=>t.set(R.id,M.id))}),s(b,{created:g,deleted:S})}else{const S=g.reduce((M,R)=>{const U=e.get(R.id);return U?[...M,U]:M},[]);if(S.length>0)throw Error(`Bulk insert would overwrite the following annotations: ${S.map(M=>M.id).join(", ")}`);g.forEach(M=>{e.set(M.id,M),M.bodies.forEach(R=>t.set(R.id,M.id))}),s(b,{created:g})}},y=g=>{const x=typeof g=="string"?g:g.id,b=e.get(x);if(b)return e.delete(x),b.bodies.forEach(S=>t.delete(S.id)),b;console.warn(`Attempt to delete missing annotation: ${x}`)},B=(g,x=C.LOCAL)=>{const b=y(g);b&&s(x,{deleted:[b]})},A=(g,x=C.LOCAL)=>{const b=g.reduce((S,M)=>{const R=y(M);return R?[...S,R]:S},[]);b.length>0&&s(x,{deleted:b})},l=(g,x=C.LOCAL)=>{const b=e.get(g.annotation);if(b){const S=b.bodies.find(M=>M.id===g.id);if(S){t.delete(S.id);const M={...b,bodies:b.bodies.filter(R=>R.id!==g.id)};e.set(b.id,M),s(x,{updated:[{oldValue:b,newValue:M,bodiesDeleted:[S]}]})}else console.warn(`Attempt to delete missing body ${g.id} from annotation ${g.annotation}`)}else console.warn(`Attempt to delete body from missing annotation ${g.annotation}`)},c=g=>{const x=e.get(g);return x?{...x}:void 0},u=g=>{const x=t.get(g);if(x){const b=c(x).bodies.find(S=>S.id===g);if(b)return b;console.error(`Store integrity error: body ${g} in index, but not in annotation`)}else console.warn(`Attempt to retrieve missing body: ${g}`)},m=(g,x)=>{if(g.annotation!==x.annotation)throw"Annotation integrity violation: annotation ID must be the same when updating bodies";const b=e.get(g.annotation);if(b){const S=b.bodies.find(R=>R.id===g.id),M={...b,bodies:b.bodies.map(R=>R.id===S.id?x:R)};return e.set(b.id,M),S.id!==x.id&&(t.delete(S.id),t.set(x.id,M.id)),{oldValue:b,newValue:M,bodiesUpdated:[{oldBody:S,newBody:x}]}}else console.warn(`Attempt to add body to missing annotation ${g.annotation}`)},E=(g,x,b=C.LOCAL)=>{const S=m(g,x);S&&s(b,{updated:[S]})},v=(g,x=C.LOCAL)=>{const b=g.map(S=>m({id:S.id,annotation:S.annotation},S)).filter(Boolean);s(x,{updated:b})},L=g=>{const x=e.get(g.annotation);if(x){const b={...x,target:{...x.target,...g}};return e.set(x.id,b),{oldValue:x,newValue:b,targetUpdated:{oldTarget:x.target,newTarget:g}}}else console.warn(`Attempt to update target on missing annotation: ${g.annotation}`)};return{addAnnotation:a,addBody:w,all:f,bulkAddAnnotation:p,bulkDeleteAnnotation:A,bulkUpdateAnnotation:h,bulkUpdateBodies:v,bulkUpdateTargets:(g,x=C.LOCAL)=>{const b=g.map(S=>L(S)).filter(Boolean);b.length>0&&s(x,{updated:b})},clear:T,deleteAnnotation:B,deleteBody:l,getAnnotation:c,getBody:u,observe:o,unobserve:i,updateAnnotation:d,updateBody:E,updateTarget:(g,x=C.LOCAL)=>{const b=L(g);b&&s(x,{updated:[b]})}}};let Bt=()=>({emit(e,...t){for(let n=0,o=this.events[e]||[],i=o.length;n<i;n++)o[n](...t)},events:{},on(e,t){var n;return((n=this.events)[e]||(n[e]=[])).push(t),()=>{var o;this.events[e]=(o=this.events[e])==null?void 0:o.filter(i=>t!==i)}}});const Mt=250,Lt=e=>{const t=Bt(),n=[];let o=-1,i=!1,s=0;const a=p=>{if(!i){const{changes:y}=p,B=performance.now();if(B-s>Mt)n.splice(o+1),n.push(y),o=n.length-1;else{const A=n.length-1;n[A]=vt(n[A],y)}s=B}i=!1};e.observe(a,{origin:C.LOCAL});const r=p=>p&&p.length>0&&e.bulkDeleteAnnotation(p),d=p=>p&&p.length>0&&e.bulkAddAnnotation(p,!1),h=p=>p&&p.length>0&&e.bulkUpdateAnnotation(p.map(({oldValue:y})=>y)),w=p=>p&&p.length>0&&e.bulkUpdateAnnotation(p.map(({newValue:y})=>y)),f=p=>p&&p.length>0&&e.bulkAddAnnotation(p,!1),T=p=>p&&p.length>0&&e.bulkDeleteAnnotation(p);return{canRedo:()=>n.length-1>o,canUndo:()=>o>-1,destroy:()=>e.unobserve(a),on:(p,y)=>t.on(p,y),redo:()=>{if(n.length-1>o){i=!0;const{created:p,updated:y,deleted:B}=n[o+1];d(p),w(y),T(B),t.emit("redo",n[o+1]),o+=1}},undo:()=>{if(o>-1){i=!0;const{created:p,updated:y,deleted:B}=n[o];r(p),h(y),f(B),t.emit("undo",n[o]),o-=1}}}},Ct=()=>{const{subscribe:e,set:t}=J([]);return{subscribe:e,set:t}},Rt=(e,t,n,o)=>{const{store:i,selection:s,hover:a,viewport:r}=e,d=new Map;let h=[],w,f;const T=(l,c)=>{d.has(l)?d.get(l).push(c):d.set(l,[c])},p=(l,c)=>{const u=d.get(l);u&&u.indexOf(c)>0&&u.splice(u.indexOf(c),1)},y=(l,c,u)=>{d.has(l)&&setTimeout(()=>{d.get(l).forEach(m=>{if(n){const E=Array.isArray(c)?c.map(L=>n.serialize(L)):n.serialize(c),v=u?u instanceof PointerEvent?u:n.serialize(u):void 0;m(E,v)}else m(c,u)})},1)},B=()=>{const{selected:l}=s,c=(l||[]).map(({id:u})=>i.getAnnotation(u));c.forEach(u=>{const m=h.find(E=>E.id===u.id);(!m||!I(m,u))&&y("updateAnnotation",u,m)}),h=h.map(u=>c.find(({id:E})=>E===u.id)||u)};s.subscribe(({selected:l})=>{if(!(h.length===0&&l.length===0)){if(h.length===0&&l.length>0)h=l.map(({id:c})=>i.getAnnotation(c));else if(h.length>0&&l.length===0)h.forEach(c=>{const u=i.getAnnotation(c.id);u&&!I(u,c)&&y("updateAnnotation",u,c)}),h=[];else{const c=new Set(h.map(m=>m.id)),u=new Set(l.map(({id:m})=>m));h.filter(m=>!u.has(m.id)).forEach(m=>{const E=i.getAnnotation(m.id);E&&!I(E,m)&&y("updateAnnotation",E,m)}),h=[...h.filter(m=>u.has(m.id)),...l.filter(({id:m})=>!c.has(m)).map(({id:m})=>i.getAnnotation(m))]}y("selectionChanged",h)}}),a.subscribe(l=>{!w&&l?y("mouseEnterAnnotation",i.getAnnotation(l)):w&&!l?y("mouseLeaveAnnotation",i.getAnnotation(w)):w&&l&&(y("mouseLeaveAnnotation",i.getAnnotation(w)),y("mouseEnterAnnotation",i.getAnnotation(l))),w=l}),r==null||r.subscribe(l=>y("viewportIntersect",l.map(c=>i.getAnnotation(c)))),i.observe(l=>{o&&(f&&clearTimeout(f),f=setTimeout(B,1e3));const{created:c,deleted:u}=l.changes;(c||[]).forEach(m=>y("createAnnotation",m)),(u||[]).forEach(m=>y("deleteAnnotation",m)),(l.changes.updated||[]).filter(m=>[...m.bodiesCreated||[],...m.bodiesDeleted||[],...m.bodiesUpdated||[]].length>0).forEach(({oldValue:m,newValue:E})=>{const v=h.find(L=>L.id===m.id)||m;h=h.map(L=>L.id===m.id?E:L),y("updateAnnotation",E,v)})},{origin:C.LOCAL}),i.observe(l=>{if(h){const c=new Set(h.map(m=>m.id)),u=(l.changes.updated||[]).filter(({newValue:m})=>c.has(m.id)).map(({newValue:m})=>m);u.length>0&&(h=h.map(m=>u.find(v=>v.id===m.id)||m))}},{origin:C.REMOTE});const A=l=>c=>{const{created:u,deleted:m,updated:E}=c;(u||[]).forEach(v=>y("createAnnotation",v)),(m||[]).forEach(v=>y("deleteAnnotation",v)),l?(E||[]).forEach(v=>y("updateAnnotation",v.oldValue,v.newValue)):(E||[]).forEach(v=>y("updateAnnotation",v.newValue,v.oldValue))};return t.on("undo",A(!0)),t.on("redo",A(!1)),{on:T,off:p,emit:y}},Ot=e=>t=>t.reduce((n,o)=>{const{parsed:i,error:s}=e.parse(o);return s?{parsed:n.parsed,failed:[...n.failed,o]}:i?{parsed:[...n.parsed,i],failed:n.failed}:{...n}},{parsed:[],failed:[]}),Xt=(e,t,n)=>{const{store:o,selection:i}=e,s=A=>{if(n){const{parsed:l,error:c}=n.parse(A);l?o.addAnnotation(l,C.REMOTE):console.error(c)}else o.addAnnotation(A,C.REMOTE)},a=()=>i.clear(),r=()=>o.clear(),d=A=>{const l=o.getAnnotation(A);return n&&l?n.serialize(l):l},h=()=>n?o.all().map(n.serialize):o.all(),w=()=>{var A;const l=(((A=i.selected)==null?void 0:A.map(c=>c.id))||[]).map(c=>o.getAnnotation(c)).filter(Boolean);return n?l.map(n.serialize):l},f=A=>fetch(A).then(l=>l.json()).then(l=>(p(l),l)),T=A=>{if(typeof A=="string"){const l=o.getAnnotation(A);if(o.deleteAnnotation(A),l)return n?n.serialize(l):l}else{const l=n?n.parse(A).parsed:A;if(l)return o.deleteAnnotation(l),A}},p=A=>{if(n){const{parsed:l,failed:c}=Ot(n)(A);c.length>0&&console.warn(`Discarded ${c.length} invalid annotations`,c),o.bulkAddAnnotation(l,!0,C.REMOTE)}else o.bulkAddAnnotation(A,!0,C.REMOTE)},y=A=>{A?i.setSelected(A):i.clear()},B=A=>{if(n){const l=n.parse(A).parsed,c=n.serialize(o.getAnnotation(l.id));return o.updateAnnotation(l),c}else{const l=o.getAnnotation(A.id);return o.updateAnnotation(A),l}};return{addAnnotation:s,cancelSelected:a,canRedo:t.canRedo,canUndo:t.canUndo,clearAnnotations:r,getAnnotationById:d,getAnnotations:h,getSelected:w,loadAnnotations:f,redo:t.redo,removeAnnotation:T,setAnnotations:p,setSelected:y,undo:t.undo,updateAnnotation:B}};let Ut=e=>crypto.getRandomValues(new Uint8Array(e)),Yt=(e,t,n)=>{let o=(2<<Math.log(e.length-1)/Math.LN2)-1,i=-~(1.6*o*t/e.length);return(s=t)=>{let a="";for(;;){let r=n(i),d=i;for(;d--;)if(a+=e[r[d]&o]||"",a.length===s)return a}}},Dt=(e,t=21)=>Yt(e,t,Ut),It=(e=21)=>crypto.getRandomValues(new Uint8Array(e)).reduce((t,n)=>(n&=63,n<36?t+=n.toString(36):n<62?t+=(n-26).toString(36).toUpperCase():n>62?t+="-":t+="_",t),"");const Vt=()=>({isGuest:!0,id:Dt("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",20)()});It();function _t(e,t,n,o,i){st(e,t,n||0,o||e.length-1,i||kt)}function st(e,t,n,o,i){for(;o>n;){if(o-n>600){var s=o-n+1,a=t-n+1,r=Math.log(s),d=.5*Math.exp(2*r/3),h=.5*Math.sqrt(r*d*(s-d)/s)*(a-s/2<0?-1:1),w=Math.max(n,Math.floor(t-a*d/s+h)),f=Math.min(o,Math.floor(t+(s-a)*d/s+h));st(e,t,w,f,i)}var T=e[t],p=n,y=o;for(P(e,n,t),i(e[o],T)>0&&P(e,n,o);p<y;){for(P(e,p,y),p++,y--;i(e[p],T)<0;)p++;for(;i(e[y],T)>0;)y--}i(e[n],T)===0?P(e,n,y):(y++,P(e,y,o)),y<=t&&(n=y+1),t<=y&&(o=y-1)}}function P(e,t,n){var o=e[t];e[t]=e[n],e[n]=o}function kt(e,t){return e<t?-1:e>t?1:0}class Nt{constructor(t=9){this._maxEntries=Math.max(4,t),this._minEntries=Math.max(2,Math.ceil(this._maxEntries*.4)),this.clear()}all(){return this._all(this.data,[])}search(t){let n=this.data;const o=[];if(!$(t,n))return o;const i=this.toBBox,s=[];for(;n;){for(let a=0;a<n.children.length;a++){const r=n.children[a],d=n.leaf?i(r):r;$(t,d)&&(n.leaf?o.push(r):Q(t,d)?this._all(r,o):s.push(r))}n=s.pop()}return o}collides(t){let n=this.data;if(!$(t,n))return!1;const o=[];for(;n;){for(let i=0;i<n.children.length;i++){const s=n.children[i],a=n.leaf?this.toBBox(s):s;if($(t,a)){if(n.leaf||Q(t,a))return!0;o.push(s)}}n=o.pop()}return!1}load(t){if(!(t&&t.length))return this;if(t.length<this._minEntries){for(let o=0;o<t.length;o++)this.insert(t[o]);return this}let n=this._build(t.slice(),0,t.length-1,0);if(!this.data.children.length)this.data=n;else if(this.data.height===n.height)this._splitRoot(this.data,n);else{if(this.data.height<n.height){const o=this.data;this.data=n,n=o}this._insert(n,this.data.height-n.height-1,!0)}return this}insert(t){return t&&this._insert(t,this.data.height-1),this}clear(){return this.data=k([]),this}remove(t,n){if(!t)return this;let o=this.data;const i=this.toBBox(t),s=[],a=[];let r,d,h;for(;o||s.length;){if(o||(o=s.pop(),d=s[s.length-1],r=a.pop(),h=!0),o.leaf){const w=Pt(t,o.children,n);if(w!==-1)return o.children.splice(w,1),s.push(o),this._condense(s),this}!h&&!o.leaf&&Q(o,i)?(s.push(o),a.push(r),r=0,d=o,o=o.children[0]):d?(r++,o=d.children[r],h=!1):o=null}return this}toBBox(t){return t}compareMinX(t,n){return t.minX-n.minX}compareMinY(t,n){return t.minY-n.minY}toJSON(){return this.data}fromJSON(t){return this.data=t,this}_all(t,n){const o=[];for(;t;)t.leaf?n.push(...t.children):o.push(...t.children),t=o.pop();return n}_build(t,n,o,i){const s=o-n+1;let a=this._maxEntries,r;if(s<=a)return r=k(t.slice(n,o+1)),_(r,this.toBBox),r;i||(i=Math.ceil(Math.log(s)/Math.log(a)),a=Math.ceil(s/Math.pow(a,i-1))),r=k([]),r.leaf=!1,r.height=i;const d=Math.ceil(s/a),h=d*Math.ceil(Math.sqrt(a));at(t,n,o,h,this.compareMinX);for(let w=n;w<=o;w+=h){const f=Math.min(w+h-1,o);at(t,w,f,d,this.compareMinY);for(let T=w;T<=f;T+=d){const p=Math.min(T+d-1,f);r.children.push(this._build(t,T,p,i-1))}}return _(r,this.toBBox),r}_chooseSubtree(t,n,o,i){for(;i.push(n),!(n.leaf||i.length-1===o);){let s=1/0,a=1/0,r;for(let d=0;d<n.children.length;d++){const h=n.children[d],w=Z(h),f=Ft(t,h)-w;f<a?(a=f,s=w<s?w:s,r=h):f===a&&w<s&&(s=w,r=h)}n=r||n.children[0]}return n}_insert(t,n,o){const i=o?t:this.toBBox(t),s=[],a=this._chooseSubtree(i,this.data,n,s);for(a.children.push(t),z(a,i);n>=0&&s[n].children.length>this._maxEntries;)this._split(s,n),n--;this._adjustParentBBoxes(i,s,n)}_split(t,n){const o=t[n],i=o.children.length,s=this._minEntries;this._chooseSplitAxis(o,s,i);const a=this._chooseSplitIndex(o,s,i),r=k(o.children.splice(a,o.children.length-a));r.height=o.height,r.leaf=o.leaf,_(o,this.toBBox),_(r,this.toBBox),n?t[n-1].children.push(r):this._splitRoot(o,r)}_splitRoot(t,n){this.data=k([t,n]),this.data.height=t.height+1,this.data.leaf=!1,_(this.data,this.toBBox)}_chooseSplitIndex(t,n,o){let i,s=1/0,a=1/0;for(let r=n;r<=o-n;r++){const d=H(t,0,r,this.toBBox),h=H(t,r,o,this.toBBox),w=$t(d,h),f=Z(d)+Z(h);w<s?(s=w,i=r,a=f<a?f:a):w===s&&f<a&&(a=f,i=r)}return i||o-n}_chooseSplitAxis(t,n,o){const i=t.leaf?this.compareMinX:Ht,s=t.leaf?this.compareMinY:zt,a=this._allDistMargin(t,n,o,i),r=this._allDistMargin(t,n,o,s);a<r&&t.children.sort(i)}_allDistMargin(t,n,o,i){t.children.sort(i);const s=this.toBBox,a=H(t,0,n,s),r=H(t,o-n,o,s);let d=F(a)+F(r);for(let h=n;h<o-n;h++){const w=t.children[h];z(a,t.leaf?s(w):w),d+=F(a)}for(let h=o-n-1;h>=n;h--){const w=t.children[h];z(r,t.leaf?s(w):w),d+=F(r)}return d}_adjustParentBBoxes(t,n,o){for(let i=o;i>=0;i--)z(n[i],t)}_condense(t){for(let n=t.length-1,o;n>=0;n--)t[n].children.length===0?n>0?(o=t[n-1].children,o.splice(o.indexOf(t[n]),1)):this.clear():_(t[n],this.toBBox)}}function Pt(e,t,n){if(!n)return t.indexOf(e);for(let o=0;o<t.length;o++)if(n(e,t[o]))return o;return-1}function _(e,t){H(e,0,e.children.length,t,e)}function H(e,t,n,o,i){i||(i=k(null)),i.minX=1/0,i.minY=1/0,i.maxX=-1/0,i.maxY=-1/0;for(let s=t;s<n;s++){const a=e.children[s];z(i,e.leaf?o(a):a)}return i}function z(e,t){return e.minX=Math.min(e.minX,t.minX),e.minY=Math.min(e.minY,t.minY),e.maxX=Math.max(e.maxX,t.maxX),e.maxY=Math.max(e.maxY,t.maxY),e}function Ht(e,t){return e.minX-t.minX}function zt(e,t){return e.minY-t.minY}function Z(e){return(e.maxX-e.minX)*(e.maxY-e.minY)}function F(e){return e.maxX-e.minX+(e.maxY-e.minY)}function Ft(e,t){return(Math.max(t.maxX,e.maxX)-Math.min(t.minX,e.minX))*(Math.max(t.maxY,e.maxY)-Math.min(t.minY,e.minY))}function $t(e,t){const n=Math.max(e.minX,t.minX),o=Math.max(e.minY,t.minY),i=Math.min(e.maxX,t.maxX),s=Math.min(e.maxY,t.maxY);return Math.max(0,i-n)*Math.max(0,s-o)}function Q(e,t){return e.minX<=t.minX&&e.minY<=t.minY&&t.maxX<=e.maxX&&t.maxY<=e.maxY}function $(e,t){return t.minX<=e.maxX&&t.minY<=e.maxY&&t.maxX>=e.minX&&t.maxY>=e.minY}function k(e){return{children:e,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function at(e,t,n,o,i){const s=[t,n];for(;s.length;){if(n=s.pop(),t=s.pop(),n-t<=o)continue;const a=t+Math.ceil((n-t)/o/2)*o;_t(e,a,t,n,i),s.push(t,a,a,n)}}const Wt=(e,t)=>{const n=s=>Math.round(s*10)/10,o={top:n(e.top),bottom:n(e.bottom),left:n(e.left),right:n(e.right)},i={top:n(t.top),bottom:n(t.bottom),left:n(t.left),right:n(t.right)};if(Math.abs(o.top-i.top)<.5&&Math.abs(o.bottom-i.bottom)<.5){if(Math.abs(o.left-i.right)<.5||Math.abs(o.right-i.left)<.5)return"inline-adjacent";if(o.left>=i.left&&o.right<=i.right)return"inline-is-contained";if(o.left<=i.left&&o.right>=i.right)return"inline-contains"}else if(o.top<=i.top&&o.bottom>=i.bottom){if(o.left<=i.left&&o.right>=i.right)return"block-contains"}else if(o.top>=i.top&&o.bottom<=i.bottom&&o.left>=i.left&&o.right<=i.right)return"block-is-contained"},qt=(e,t)=>{const n=Math.min(e.left,t.left),o=Math.max(e.right,t.right),i=Math.min(e.top,t.top),s=Math.max(e.bottom,t.bottom);return new DOMRect(n,i,o-n,s-i)},jt=e=>e.reduce((t,n)=>{if(n.width===0||n.height===0)return t;let o=[...t],i=!1;for(const s of t){const a=Wt(n,s);if(a==="inline-adjacent"){o=o.map(r=>r===s?qt(n,s):r),i=!0;break}else if(a==="inline-contains"){o=o.map(r=>r===s?n:r),i=!0;break}else if(a==="inline-is-contained"){i=!0;break}else if(a==="block-contains"||a==="block-is-contained"){n.width<s.width&&(o=o.map(r=>r===s?n:r)),i=!0;break}}return i?o:[...o,n]},[]),Gt=e=>{const{startContainer:t,endContainer:n}=e;if(t.nodeType===Node.TEXT_NODE&&n.nodeType===Node.TEXT_NODE)return e;if(t.nodeType!==Node.TEXT_NODE){const i=t.nextSibling||t.parentNode,s=i.nodeType===Node.TEXT_NODE?i:Array.from(i.childNodes).filter(a=>a.nodeType===Node.TEXT_NODE).shift();e.setEnd(s,0)}if(n.nodeType!==Node.TEXT_NODE){const i=n.previousSibling||n.parentNode,s=i.nodeType===Node.TEXT_NODE?i:Array.from(i.childNodes).filter(a=>a.nodeType===Node.TEXT_NODE).pop();e.setEnd(s,s.textContent.length)}return e},Jt=(e,t)=>{const n=new Nt,o=new Map,i=l=>{const c=t.getBoundingClientRect(),m=l.selector.range instanceof Range&&!l.selector.range.collapsed&&l.selector.range.startContainer.nodeType===Node.TEXT_NODE&&l.selector.range.endContainer.nodeType===Node.TEXT_NODE?l:D(l,t),E=Array.from(m.selector.range.getClientRects()),v=jt(E);return v.map(L=>{const{x:g,y:x,width:b,height:S}=L;return{minX:g-c.x,minY:x-c.y,maxX:g-c.x+b,maxY:x-c.y+S,annotation:{id:l.annotation,rects:v}}})},s=()=>[...o.values()],a=()=>{n.clear(),o.clear()},r=l=>{const c=i(l);c.forEach(u=>n.insert(u)),o.set(l.annotation,c)},d=l=>{o.get(l.annotation).forEach(u=>n.remove(u)),o.delete(l.annotation)},h=l=>{d(l),r(l)},w=(l,c=!0)=>{c&&a();const u=l.map(E=>({target:E,rects:i(E)}));u.forEach(({target:E,rects:v})=>o.set(E.annotation,v));const m=u.reduce((E,{rects:v})=>[...E,...v],[]);n.load(m)},f=(l,c)=>{const u=n.search({minX:l,minY:c,maxX:l,maxY:c}),m=E=>E.annotation.rects.reduce((v,L)=>v+L.width*L.height,0);if(u.length>0)return u.sort((E,v)=>m(E)-m(v)),u[0].annotation.id},T=l=>{const c=p(l);if(c.length===0)return;let u=c[0].left,m=c[0].top,E=c[0].right,v=c[0].bottom;for(let L=1;L<c.length;L++){const g=c[L];u=Math.min(u,g.left),m=Math.min(m,g.top),E=Math.max(E,g.right),v=Math.max(v,g.bottom)}return new DOMRect(u,m,E-u,v-m)},p=l=>{const c=o.get(l);return c?c[0].annotation.rects:[]};return{all:s,clear:a,getAt:f,getBoundsForAnnotation:T,getDOMRectsForAnnotation:p,getIntersectingRects:(l,c,u,m)=>n.search({minX:l,minY:c,maxX:u,maxY:m}),insert:r,recalculate:()=>w(e.all().map(l=>l.target),!0),remove:d,set:w,size:()=>n.all().length,update:h}},W=e=>{const{range:t}=e.target.selector;return t instanceof Range&&!t.collapsed},rt=(e,t)=>{const n=Tt(),o=Jt(n,e),i=pt(n,t),s=gt(n),a=Ct(),r=(A,l=C.LOCAL)=>{const c=A.target.selector.range instanceof Range?A:{...A,target:D(A.target,e)},{range:u}=c.target.selector,m=u&&!u.collapsed;return m&&n.addAnnotation(c,l),m},d=(A,l=!0,c=C.LOCAL)=>{const u=A.map(E=>W(E)?E:{...E,target:D(E.target,e)});if(u.some(E=>E.target.selector.range.collapsed)){const E=u.filter(v=>v.target.selector.range.collapsed);return console.warn("Could not revive all targets"),console.warn(E),n.bulkAddAnnotation(u,l,c),E}else return n.bulkAddAnnotation(u,l,c),[]},h=(A,l=C.LOCAL)=>{const c=A.selector.range instanceof Range?A:D(A,e);n.updateTarget(c,l)},w=(A,l=C.LOCAL)=>{const c=A.map(u=>u.selector.range instanceof Range?u:D(u,e));n.bulkUpdateTargets(c,l)},f=(A,l)=>{const c=o.getAt(A,l);return c?n.getAnnotation(c):void 0},T=(A,l,c,u)=>{const m=o.getIntersectingRects(A,l,c,u);return Array.from(new Set(m.map(v=>v.annotation.id))).map(v=>n.getAnnotation(v)).filter(v=>v)},p=(A,l,c,u=5)=>{const m=o.getDOMRectsForAnnotation(A);if(m.length>0){if(l&&c){const E=m.find(({top:v,right:L,bottom:g,left:x})=>l>=x-u&&l<=L+u&&c>=v-u&&c<=g+u);if(E)return E}return o.getBoundsForAnnotation(A)}},y=(A,l,c,u)=>{const E=o.getIntersectingRects(A,l,c,u).reduce((v,L)=>((v[L.annotation.id]=v[L.annotation.id]||[]).push(L),v),{});return Object.entries(E).map(([v,L])=>({annotation:n.getAnnotation(v),rects:L.map(({minX:g,minY:x,maxX:b,maxY:S})=>({x:g,y:x,width:b-g,height:S-x}))}))},B=()=>o.recalculate();return n.observe(({changes:A})=>{const l=(A.created||[]).filter(W),c=(A.deleted||[]).filter(W),u=(A.updated||[]).filter(m=>W(m.newValue));l.length>0&&o.set(l.map(m=>m.target),!1),(c==null?void 0:c.length)>0&&c.forEach(m=>o.remove(m.target)),(u==null?void 0:u.length)>0&&u.forEach(({newValue:m})=>o.update(m.target))}),{store:{...n,addAnnotation:r,bulkAddAnnotation:d,bulkUpdateTargets:w,getAnnotationBounds:p,getAt:f,getIntersecting:T,getIntersectingRects:y,recalculatePositions:B,updateTarget:h},selection:i,hover:s,viewport:a}},Kt=(e,t={})=>{const n=new Map,o=s=>Array.from(n.entries()).filter(([a,r])=>r.presenceKey===s.presenceKey).map(([a,r])=>a);return e.on("selectionChange",(s,a)=>{o(s).forEach(d=>n.delete(d)),a&&a.forEach(d=>n.set(d,s))}),{paint:(s,a,r,d,h,w)=>{t.font&&(d.font=t.font);const f=n.get(s.id);if(f){const{x:T,y:p,height:y}=a[0];d.fillStyle=f.appearance.color,d.fillRect(T-2,p-2.5,2,y+5);const B=d.measureText(f.appearance.label),A=B.width+6,l=B.actualBoundingBoxAscent+B.actualBoundingBoxDescent+8,c=B.fontBoundingBoxAscent?8:6.5;d.fillRect(T-2,p-2.5-l,A,l),d.fillStyle="#fff",d.fillText(f.appearance.label,T+1,p-c),r.fillStyle=f.appearance.color,r.globalAlpha=h?.45:.18,a.forEach(({x:u,y:m,width:E,height:v})=>r.fillRect(u,m-2.5,E,v+5))}else j.paint(s,a,r,d,h,w)}}},lt=e=>{if(e===null)return document.scrollingElement;const{overflowY:t}=window.getComputedStyle(e);return t!=="visible"&&t!=="hidden"&&e.scrollHeight>e.clientHeight?e:lt(e.parentElement)},Zt=(e,t)=>n=>{const o=s=>{const a=i.getBoundingClientRect(),r=i.clientHeight,d=i.clientWidth,h=s.selector.range.getBoundingClientRect(),{width:w,height:f}=t.getAnnotationBounds(n.id),T=h.top-a.top,p=h.left-a.left,y=i.parentElement?i.scrollTop:0,B=i.parentElement?i.scrollLeft:0,A=T+y-(r-f)/2,l=p+B-(d-w)/2;i.scroll({top:A,left:l,behavior:"smooth"})},i=lt(e);if(i){const s=t.getAnnotation(n.id),{range:a}=s.target.selector;if(a&&!a.collapsed)return o(s.target),!0;{const r=D(s.target,e);if(r.selector.range&&!r.selector.range.collapsed)return o(r),!0}}return!1};let q;const Qt=new Uint8Array(16);function te(){if(!q&&(q=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!q))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return q(Qt)}const O=[];for(let e=0;e<256;++e)O.push((e+256).toString(16).slice(1));function ee(e,t=0){return O[e[t+0]]+O[e[t+1]]+O[e[t+2]]+O[e[t+3]]+"-"+O[e[t+4]]+O[e[t+5]]+"-"+O[e[t+6]]+O[e[t+7]]+"-"+O[e[t+8]]+O[e[t+9]]+"-"+O[e[t+10]]+O[e[t+11]]+O[e[t+12]]+O[e[t+13]]+O[e[t+14]]+O[e[t+15]]}const ct={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function ne(e,t,n){if(ct.randomUUID&&!t&&!e)return ct.randomUUID();e=e||{};const o=e.random||(e.rng||te)();if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,t){n=n||0;for(let i=0;i<16;++i)t[n+i]=o[i];return t}return ee(o)}const oe=(e,t,n)=>{const o=document.createRange(),i=n?e.startContainer.parentElement.closest(n):t;o.setStart(i,0),o.setEnd(e.startContainer,e.startOffset);const s=e.toString(),a=o.toString().length,r=a+s.length;return n?{quote:s,start:a,end:r,range:e,offsetReference:i}:{quote:s,start:a,end:r,range:e}},ie=(e,t,n)=>{const{store:o,selection:i}=t;let s,a;const r=p=>s=p;let d=!1,h;const w=p=>{var B;if(!d)return;!((B=p.target.parentElement)!=null&&B.closest(".not-annotatable"))?a={annotation:ne(),selector:void 0,creator:s,created:new Date}:a=void 0};e.addEventListener("selectstart",w);let f;const T=p=>{var B;const y=document.getSelection();if(p.timeStamp-h.timeStamp<1e3&&!a&&w(h),!y.isCollapsed&&d&&a){const A=Array.from(Array(y.rangeCount).keys()).map(u=>y.getRangeAt(u));Gt(A[0]).toString()!==((B=a.selector)==null?void 0:B.quote)&&(a={...a,selector:oe(A[0],e,n)},o.getAnnotation(a.annotation)?o.updateTarget(a,C.LOCAL):(o.addAnnotation({id:a.annotation,bodies:[],target:a}),i.clickSelect(a.annotation,h)))}};return document.addEventListener("selectionchange",p=>{f&&clearTimeout(f),f=setTimeout(()=>T(p),10)}),e.addEventListener("pointerdown",p=>{const{target:y,timeStamp:B,offsetX:A,offsetY:l,type:c}=p;h={...p,target:y,timeStamp:B,offsetX:A,offsetY:l,type:c},d=p.button===0}),document.addEventListener("pointerup",p=>{var l;if(!!((l=p.target.parentElement)!=null&&l.closest(".not-annotatable"))||!d)return;const B=()=>{const{x:c,y:u}=e.getBoundingClientRect(),m=o.getAt(p.clientX-c,p.clientY-u);if(m){const{selected:E}=i;(E.length!==1||E[0].id!==m.id)&&i.clickSelect(m.id,p)}else i.isEmpty()||i.clear()},A=p.timeStamp-h.timeStamp;document.getSelection().isCollapsed&&A<300?(a=void 0,B()):i.clickSelect(a.annotation,p)}),{setUser:r}},se=(e,t={})=>{e.addEventListener("click",u=>u.preventDefault());const n=rt(e,t.pointerAction),{selection:o,viewport:i}=n,s=n.store,a=Lt(s),r=Rt(n,a,t.adapter);let d=Vt();const h=nt(e,n,i);t.style&&h.setDrawingStyle(t.style);const w=ie(e,n,t.offsetReferenceSelector);return w.setUser(d),{...Xt(n,a,t.adapter),destroy:()=>{throw"Not implemented yet"},element:e,getUser:()=>d,setFilter:u=>h.setFilter(u),setStyle:u=>h.setDrawingStyle(u),setUser:u=>{d=u,w.setUser(u)},setSelected:u=>{u?o.setSelected(u):o.clear()},setPresenceProvider:u=>{u&&(h.setPainter(Kt(u,t.presence)),u.on("selectionChange",()=>h.redraw()))},on:r.on,off:r.off,scrollIntoView:Zt(e,s),state:n}};X.Origin=C,X.createHighlightLayer=nt,X.createTextAnnotator=se,X.createTextAnnotatorState=rt,X.defaultPainter=j,X.reviveTarget=D,Object.defineProperty(X,Symbol.toStringTag,{value:"Module"})});
2
2
  //# sourceMappingURL=text-annotator.umd.js.map