@recogito/text-annotator 3.0.0-rc.5 → 3.0.0-rc.7

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(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:u,height:b})=>n.fillRect(r,d-2.5,u,b+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)}},fe="",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 u=ut(n);e.classList.add("r6o-annotatable");const b=tt("r6o-highlight-layer bg"),h=tt("r6o-highlight-layer fg",!0),E=b.getContext("2d"),m=h.getContext("2d");e.insertBefore(b,e.firstChild),e.appendChild(h),e.addEventListener("pointermove",w=>{const{x:T,y:M}=e.getBoundingClientRect(),f=o.getAt(w.clientX-T,w.clientY-M);f&&(!r||r(f))?s.current!==f.id&&(e.classList.add("hovered"),s.set(f.id)):s.current&&(e.classList.remove("hovered"),s.set(null))});const v=()=>{const{top:w,left:T}=e.getBoundingClientRect(),{innerWidth:M,innerHeight:f}=window,x=-T,A=-w,S=M-T,B=f-w;return{top:w,left:T,minX:x,minY:A,maxX:S,maxY:B}},L=()=>c();document.addEventListener("scroll",L,{capture:!0,passive:!0});const y=ht(()=>{et(b),et(h,!0),o.recalculatePositions(),c()});window.addEventListener("resize",y),new ResizeObserver(y).observe(e);const c=()=>requestAnimationFrame(()=>{const{top:w,left:T,minX:M,minY:f,maxX:x,maxY:A}=v(),S=r?o.getIntersectingRects(M,f,x,A).filter(({annotation:Y})=>r(Y)):o.getIntersectingRects(M,f,x,A),{width:B,height:R}=h,U=new Set(i.selected.map(({id:Y})=>Y));m.clearRect(-.5,-.5,B+1,R+1),E.clearRect(-.5,-.5,B+1,R+1),S.forEach(({annotation:Y,rects:ae})=>{const re=ae.map(({x:ce,y:de,width:ue,height:he})=>({x:ce+T,y:de+w,width:ue,height:he})),le=U.has(Y.id);d.paint(Y,re,E,m,le,a)}),setTimeout(()=>u(S.map(({annotation:Y})=>Y)),1)});return o.observe(()=>c()),i.subscribe(()=>c()),{redraw:c,setDrawingStyle:w=>{a=w,c()},setFilter:w=>{r=w,c()},setPainter:w=>d=w}},I=(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 u=!i;for(;d!==null;){const b=d.textContent.length;if(!u&&i&&(u=i.contains(d)),u){if(a+b>n){r.setStart(d,n-a);break}a+=b}d=s.nextNode()}for(;d!==null;){const b=d.textContent.length;if(a+b>o){r.setEnd(d,o-a);break}a+=b,d=s.nextNode()}return{...e,selector:{...e.selector,range:r}}};var ot=Object.prototype.hasOwnProperty;function D(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--&&D(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)||!D(e[n],t[n]))return!1;return Object.keys(t).length===o}}return e!==e&&t!==t}function J(){}function ft(e,t){return e!=e?t==t:e!==t||e&&typeof e=="object"||typeof e=="function"}const V=[];function K(e,t=J){let n;const o=new Set;function i(r){if(ft(e,r)&&(e=r,n)){const d=!V.length;for(const u of o)u[1](),V.push(u,e);if(d){for(let u=0;u<V.length;u+=2)V[u][0](V[u+1]);V.length=0}}}function s(r){i(r(e))}function a(r,d=J){const u=[r,d];return o.add(u),o.size===1&&(n=t(i)||J),r(e),()=>{o.delete(u),o.size===0&&n&&(n(),n=null)}}return{set:i,update:s,subscribe:a}}const gt=e=>{const{subscribe:t,set:n}=K(null);let o=null;return t(i=>o=i),e.observe(({changes:i})=>{if(o){i.deleted.some(a=>a.id===o)&&n(null);const s=i.updated.find(({oldValue:a})=>a.id===o);s&&n(s.newValue.id)}}),{get current(){return o},subscribe:t,set:n}},G={selected:[]},pt=(e,t="EDIT")=>{const{subscribe:n,set:o}=K(G);let i=G;n(h=>i=h);const s=()=>o(G),a=()=>{var h;return((h=i.selected)==null?void 0:h.length)===0},r=h=>{if(i.selected.length===0)return!1;const E=typeof h=="string"?h:h.id;return i.selected.some(m=>m.id===E)},d=(h,E)=>{const m=e.getAnnotation(h);if(m){const v=mt(m,t);o(v==="EDIT"?{selected:[{id:h,editable:!0}],pointerEvent:E}:v==="SELECT"?{selected:[{id:h}],pointerEvent:E}:{selected:[],pointerEvent:E})}else console.warn("Invalid selection: "+h)},u=(h,E=!0)=>{const m=Array.isArray(h)?h:[h],v=m.map(L=>e.getAnnotation(L)).filter(L=>L);o({selected:v.map(({id:L})=>({id:L,editable:E}))}),v.length!==m.length&&console.warn("Invalid selection",h)},b=h=>{if(i.selected.length===0)return!1;const{selected:E}=i;E.filter(({id:m})=>h.includes(m)).length>0&&o({selected:E.filter(({id:m})=>!h.includes(m))})};return e.observe(({changes:h})=>b(h.deleted.map(E=>E.id))),{clear:s,clickSelect:d,get selected(){return i?[...i.selected]:null},get pointerEvent(){return i?i.pointerEvent:null},isEmpty:a,isSelected:r,setSelected:u,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 At=(e,t)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},xt=(e,t)=>{const n=new Set(t.bodies.map(o=>o.id));return e.bodies.filter(o=>!n.has(o.id))},vt=(e,t)=>t.bodies.map(n=>{const o=e.bodies.find(i=>i.id===n.id);return{newBody:n,oldBody:o&&!D(o,n)?o:void 0}}).filter(({oldBody:n})=>n),yt=(e,t)=>!D(e.target,t.target),it=(e,t)=>{const n=At(e,t),o=xt(e,t),i=vt(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 wt=(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==null?void 0:d.length)>0;if(!(r(i.created)||r(i.deleted))){const d=(n=i.updated)==null?void 0:n.some(b=>r(b.bodiesCreated)||r(b.bodiesDeleted)||r(b.bodiesUpdated)),u=(o=i.updated)==null?void 0:o.some(b=>b.targetUpdated);if(a==="BODY_ONLY"&&d&&!u||a==="TARGET_ONLY"&&u&&!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},Et=(e,t)=>{const n=new Set((e.created||[]).map(h=>h.id)),o=new Set((e.updated||[]).map(({newValue:h})=>h.id)),i=new Set((t.created||[]).map(h=>h.id)),s=new Set((t.deleted||[]).map(h=>h.id)),a=new Set((t.updated||[]).map(({oldValue:h})=>h.id)),r=new Set((t.updated||[]).filter(({oldValue:h})=>n.has(h.id)||o.has(h.id)).map(({oldValue:h})=>h.id)),d=[...(e.created||[]).filter(h=>!s.has(h.id)).map(h=>a.has(h.id)?t.updated.find(({oldValue:E})=>E.id===h.id).newValue:h),...t.created||[]],u=[...(e.deleted||[]).filter(h=>!i.has(h.id)),...(t.deleted||[]).filter(h=>!n.has(h.id))],b=[...(e.updated||[]).filter(({newValue:h})=>!s.has(h.id)).map(h=>{const{oldValue:E,newValue:m}=h;if(a.has(m.id)){const v=t.updated.find(L=>L.oldValue.id===m.id).newValue;return it(E,v)}else return h}),...(t.updated||[]).filter(({oldValue:h})=>!r.has(h.id))];return{created:d,deleted:u,updated:b}},Tt=e=>e.id!==void 0,St=()=>{const e=new Map,t=new Map,n=[],o=(f,x={})=>n.push({onChange:f,options:x}),i=f=>{const x=n.findIndex(A=>A.onChange==f);x>-1&&n.splice(x,1)},s=(f,x)=>{const A={origin:f,changes:{created:x.created||[],updated:x.updated||[],deleted:x.deleted||[]},state:[...e.values()]};n.forEach(S=>{wt(S,A)&&S.onChange(A)})},a=(f,x=C.LOCAL)=>{if(e.get(f.id))throw Error(`Cannot add annotation ${f.id} - exists already`);e.set(f.id,f),f.bodies.forEach(A=>t.set(A.id,f.id)),s(x,{created:[f]})},r=(f,x)=>{const A=typeof f=="string"?x:f,S=typeof f=="string"?f:f.id,B=e.get(S);if(B){const R=it(B,A);return S===A.id?e.set(S,A):(e.delete(S),e.set(A.id,A)),B.bodies.forEach(U=>t.delete(U.id)),A.bodies.forEach(U=>t.set(U.id,A.id)),R}else console.warn(`Cannot update annotation ${S} - does not exist`)},d=(f,x=C.LOCAL,A=C.LOCAL)=>{const S=Tt(x)?A:x,B=r(f,x);B&&s(S,{updated:[B]})},u=(f,x=C.LOCAL)=>{const A=f.reduce((S,B)=>{const R=r(B);return R?[...S,R]:S},[]);A.length>0&&s(x,{updated:A})},b=(f,x=C.LOCAL)=>{const A=e.get(f.annotation);if(A){const S={...A,bodies:[...A.bodies,f]};e.set(A.id,S),t.set(f.id,S.id),s(x,{updated:[{oldValue:A,newValue:S,bodiesCreated:[f]}]})}else console.warn(`Attempt to add body to missing annotation: ${f.annotation}`)},h=()=>[...e.values()],E=(f=C.LOCAL)=>{const x=[...e.values()];e.clear(),t.clear(),s(f,{deleted:x})},m=(f,x=!0,A=C.LOCAL)=>{if(x){const S=[...e.values()];e.clear(),t.clear(),f.forEach(B=>{e.set(B.id,B),B.bodies.forEach(R=>t.set(R.id,B.id))}),s(A,{created:f,deleted:S})}else{const S=f.reduce((B,R)=>{const U=e.get(R.id);return U?[...B,U]:B},[]);if(S.length>0)throw Error(`Bulk insert would overwrite the following annotations: ${S.map(B=>B.id).join(", ")}`);f.forEach(B=>{e.set(B.id,B),B.bodies.forEach(R=>t.set(R.id,B.id))}),s(A,{created:f})}},v=f=>{const x=typeof f=="string"?f:f.id,A=e.get(x);if(A)return e.delete(x),A.bodies.forEach(S=>t.delete(S.id)),A;console.warn(`Attempt to delete missing annotation: ${x}`)},L=(f,x=C.LOCAL)=>{const A=v(f);A&&s(x,{deleted:[A]})},y=(f,x=C.LOCAL)=>{const A=f.reduce((S,B)=>{const R=v(B);return R?[...S,R]:S},[]);A.length>0&&s(x,{deleted:A})},l=(f,x=C.LOCAL)=>{const A=e.get(f.annotation);if(A){const S=A.bodies.find(B=>B.id===f.id);if(S){t.delete(S.id);const B={...A,bodies:A.bodies.filter(R=>R.id!==f.id)};e.set(A.id,B),s(x,{updated:[{oldValue:A,newValue:B,bodiesDeleted:[S]}]})}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}`)},c=f=>{const x=e.get(f);return x?{...x}:void 0},g=f=>{const x=t.get(f);if(x){const A=c(x).bodies.find(S=>S.id===f);if(A)return A;console.error(`Store integrity error: body ${f} in index, but not in annotation`)}else console.warn(`Attempt to retrieve missing body: ${f}`)},p=(f,x)=>{if(f.annotation!==x.annotation)throw"Annotation integrity violation: annotation ID must be the same when updating bodies";const A=e.get(f.annotation);if(A){const S=A.bodies.find(R=>R.id===f.id),B={...A,bodies:A.bodies.map(R=>R.id===S.id?x:R)};return e.set(A.id,B),S.id!==x.id&&(t.delete(S.id),t.set(x.id,B.id)),{oldValue:A,newValue:B,bodiesUpdated:[{oldBody:S,newBody:x}]}}else console.warn(`Attempt to add body to missing annotation ${f.annotation}`)},w=(f,x,A=C.LOCAL)=>{const S=p(f,x);s(A,{updated:[S]})},T=(f,x=C.LOCAL)=>{const A=f.map(S=>p({id:S.id,annotation:S.annotation},S));s(x,{updated:A})},M=f=>{const x=e.get(f.annotation);if(x){const A={...x,target:{...x.target,...f}};return e.set(x.id,A),{oldValue:x,newValue:A,targetUpdated:{oldTarget:x.target,newTarget:f}}}else console.warn(`Attempt to update target on missing annotation: ${f.annotation}`)};return{addAnnotation:a,addBody:b,all:h,bulkAddAnnotation:m,bulkDeleteAnnotation:y,bulkUpdateAnnotation:u,bulkUpdateBodies:T,bulkUpdateTargets:(f,x=C.LOCAL)=>{const A=f.map(M).filter(S=>S);A.length>0&&s(x,{updated:A})},clear:E,deleteAnnotation:L,deleteBody:l,getAnnotation:c,getBody:g,observe:o,unobserve:i,updateAnnotation:d,updateBody:w,updateTarget:(f,x=C.LOCAL)=>{const A=M(f);A&&s(x,{updated:[A]})}}};let Bt=()=>({emit(e,...t){let n=this.events[e]||[];for(let o=0,i=n.length;o<i;o++)n[o](...t)},events:{},on(e,t){var n;return(n=this.events[e])!=null&&n.push(t)||(this.events[e]=[t]),()=>{var o;this.events[e]=(o=this.events[e])==null?void 0:o.filter(i=>t!==i)}}});const Lt=250,Mt=e=>{const t=Bt(),n=[];let o=-1,i=!1,s=0;const a=m=>{if(!i){const{changes:v}=m,L=performance.now();if(L-s>Lt)n.splice(o+1),n.push(v),o=n.length-1;else{const y=n.length-1;n[y]=Et(n[y],v)}s=L}i=!1};e.observe(a,{origin:C.LOCAL});const r=m=>(m==null?void 0:m.length)>0&&e.bulkDeleteAnnotation(m),d=m=>(m==null?void 0:m.length)>0&&e.bulkAddAnnotation(m,!1),u=m=>(m==null?void 0:m.length)>0&&e.bulkUpdateAnnotation(m.map(({oldValue:v})=>v)),b=m=>(m==null?void 0:m.length)>0&&e.bulkUpdateAnnotation(m.map(({newValue:v})=>v)),h=m=>(m==null?void 0:m.length)>0&&e.bulkAddAnnotation(m,!1),E=m=>(m==null?void 0:m.length)>0&&e.bulkDeleteAnnotation(m);return{canRedo:()=>n.length-1>o,canUndo:()=>o>-1,destroy:()=>e.unobserve(a),on:(m,v)=>t.on(m,v),redo:()=>{if(n.length-1>o){i=!0;const{created:m,updated:v,deleted:L}=n[o+1];d(m),b(v),E(L),t.emit("redo",n[o+1]),o+=1}},undo:()=>{if(o>-1){i=!0;const{created:m,updated:v,deleted:L}=n[o];r(m),u(v),h(L),t.emit("undo",n[o]),o-=1}}}},Ct=()=>{const{subscribe:e,set:t}=K([]);return{subscribe:e,set:t}},Rt=(e,t,n,o)=>{const{store:i,selection:s,hover:a,viewport:r}=e,d=new Map;let u=[],b,h;const E=(l,c)=>{d.has(l)?d.get(l).push(c):d.set(l,[c])},m=(l,c)=>{const g=d.get(l);g&&g.indexOf(c)>0&&g.splice(g.indexOf(c),1)},v=(l,c,g)=>{d.has(l)&&setTimeout(()=>{d.get(l).forEach(p=>{if(n){const w=Array.isArray(c)?c.map(M=>n.serialize(M)):n.serialize(c),T=g?g instanceof PointerEvent?g:n.serialize(g):void 0;p(w,T)}else p(c,g)})},1)},L=()=>{const{selected:l}=s,c=l.map(({id:g})=>i.getAnnotation(g));c.forEach(g=>{const p=u.find(w=>w.id===g.id);(!p||!D(p,g))&&v("updateAnnotation",g,p)}),u=u.map(g=>c.find(({id:w})=>w===g.id)||g)};s.subscribe(({selected:l})=>{if(!(u.length===0&&l.length===0)){if(u.length===0&&l.length>0)u=l.map(({id:c})=>i.getAnnotation(c));else if(u.length>0&&l.length===0)u.forEach(c=>{const g=i.getAnnotation(c.id);g&&!D(g,c)&&v("updateAnnotation",g,c)}),u=[];else{const c=new Set(u.map(p=>p.id)),g=new Set(l.map(({id:p})=>p));u.filter(p=>!g.has(p.id)).forEach(p=>{const w=i.getAnnotation(p.id);w&&!D(w,p)&&v("updateAnnotation",w,p)}),u=[...u.filter(p=>g.has(p.id)),...l.filter(({id:p})=>!c.has(p)).map(({id:p})=>i.getAnnotation(p))]}v("selectionChanged",u)}}),a.subscribe(l=>{!b&&l?v("mouseEnterAnnotation",i.getAnnotation(l)):b&&!l?v("mouseLeaveAnnotation",i.getAnnotation(b)):b&&l&&(v("mouseLeaveAnnotation",i.getAnnotation(b)),v("mouseEnterAnnotation",i.getAnnotation(l))),b=l}),r==null||r.subscribe(l=>v("viewportIntersect",l.map(i.getAnnotation))),i.observe(l=>{o&&(h&&clearTimeout(h),h=setTimeout(L,1e3));const{created:c,deleted:g}=l.changes;c.forEach(p=>v("createAnnotation",p)),g.forEach(p=>v("deleteAnnotation",p)),l.changes.updated.filter(p=>[...p.bodiesCreated||[],...p.bodiesDeleted||[],...p.bodiesUpdated||[]].length>0).forEach(({oldValue:p,newValue:w})=>{const T=u.find(M=>M.id===p.id)||p;u=u.map(M=>M.id===p.id?w:M),v("updateAnnotation",w,T)})},{origin:C.LOCAL}),i.observe(l=>{if(u){const c=new Set(u.map(p=>p.id)),g=l.changes.updated.filter(({newValue:p})=>c.has(p.id)).map(({newValue:p})=>p);g.length>0&&(u=u.map(p=>g.find(T=>T.id===p.id)||p))}},{origin:C.REMOTE});const y=l=>c=>{const{created:g,deleted:p,updated:w}=c;g.forEach(T=>v("createAnnotation",T)),p.forEach(T=>v("deleteAnnotation",T)),l?w.forEach(T=>v("updateAnnotation",T.oldValue,T.newValue)):w.forEach(T=>v("updateAnnotation",T.newValue,T.oldValue))};return t.on("undo",y(!0)),t.on("redo",y(!1)),{on:E,off:m,emit:v}},Ot=e=>t=>t.reduce((n,o)=>{const{parsed:i,error:s}=e.parse(o);return s?{parsed:n.parsed,failed:[...n.failed,o]}:{parsed:[...n.parsed,i],failed:n.failed}},{parsed:[],failed:[]}),Xt=(e,t,n)=>{const{store:o,selection:i}=e,s=y=>{if(n){const{parsed:l,error:c}=n.parse(y);l?o.addAnnotation(l,C.REMOTE):console.error(c)}else o.addAnnotation(y,C.REMOTE)},a=()=>i.clear(),r=()=>o.clear(),d=y=>{const l=o.getAnnotation(y);return n&&l?n.serialize(l):l},u=()=>n?o.all().map(n.serialize):o.all(),b=()=>{var y;const l=(((y=i.selected)==null?void 0:y.map(c=>c.id))||[]).map(c=>o.getAnnotation(c));return n?l.map(n.serialize):l},h=y=>fetch(y).then(l=>l.json()).then(l=>(m(l),l)),E=y=>{if(typeof y=="string"){const l=o.getAnnotation(y);return o.deleteAnnotation(y),n?n.serialize(l):l}else{const l=n?n.parse(y).parsed:y;return o.deleteAnnotation(l),y}},m=y=>{if(n){const{parsed:l,failed:c}=Ot(n)(y);c.length>0&&console.warn(`Discarded ${c.length} invalid annotations`,c),o.bulkAddAnnotation(l,!0,C.REMOTE)}else o.bulkAddAnnotation(y,!0,C.REMOTE)},v=y=>{y?i.setSelected(y):i.clear()},L=y=>{if(n){const l=n.parse(y).parsed,c=n.serialize(o.getAnnotation(l.id));return o.updateAnnotation(l),c}else{const l=o.getAnnotation(y.id);return o.updateAnnotation(y),l}};return{addAnnotation:s,cancelSelected:a,canRedo:t.canRedo,canUndo:t.canUndo,clearAnnotations:r,getAnnotationById:d,getAnnotations:u,getSelected:b,loadAnnotations:h,redo:t.redo,removeAnnotation:E,setAnnotations:m,setSelected:v,undo:t.undo,updateAnnotation:L}};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),u=.5*Math.sqrt(r*d*(s-d)/s)*(a-s/2<0?-1:1),b=Math.max(n,Math.floor(t-a*d/s+u)),h=Math.min(o,Math.floor(t+(s-a)*d/s+u));st(e,t,b,h,i)}var E=e[t],m=n,v=o;for(P(e,n,t),i(e[o],E)>0&&P(e,n,o);m<v;){for(P(e,m,v),m++,v--;i(e[m],E)<0;)m++;for(;i(e[v],E)>0;)v--}i(e[n],E)===0?P(e,n,v):(v++,P(e,v,o)),v<=t&&(n=v+1),t<=v&&(o=v-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,u;for(;o||s.length;){if(o||(o=s.pop(),d=s[s.length-1],r=a.pop(),u=!0),o.leaf){const b=Pt(t,o.children,n);if(b!==-1)return o.children.splice(b,1),s.push(o),this._condense(s),this}!u&&!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],u=!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),u=d*Math.ceil(Math.sqrt(a));at(t,n,o,u,this.compareMinX);for(let b=n;b<=o;b+=u){const h=Math.min(b+u-1,o);at(t,b,h,d,this.compareMinY);for(let E=b;E<=h;E+=d){const m=Math.min(E+d-1,h);r.children.push(this._build(t,E,m,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 u=n.children[d],b=Z(u),h=Ft(t,u)-b;h<a?(a=h,s=b<s?b:s,r=u):h===a&&b<s&&(s=b,r=u)}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),u=H(t,r,o,this.toBBox),b=$t(d,u),h=Z(d)+Z(u);b<s?(s=b,i=r,a=h<a?h:a):b===s&&h<a&&(a=h,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 u=n;u<o-n;u++){const b=t.children[u];z(a,t.leaf?s(b):b),d+=F(a)}for(let u=o-n-1;u>=n;u--){const b=t.children[u];z(r,t.leaf?s(b):b),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]},[]),Jt=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},Kt=(e,t)=>{const n=new Nt,o=new Map,i=l=>{const c=t.getBoundingClientRect(),p=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:I(l,t),w=Array.from(p.selector.range.getClientRects()),T=jt(w);return T.map(M=>{const{x:f,y:x,width:A,height:S}=M;return{minX:f-c.x,minY:x-c.y,maxX:f-c.x+A,maxY:x-c.y+S,annotation:{id:l.annotation,rects:T}}})},s=()=>[...o.values()],a=()=>{n.clear(),o.clear()},r=l=>{const c=i(l);c.forEach(g=>n.insert(g)),o.set(l.annotation,c)},d=l=>{o.get(l.annotation).forEach(g=>n.remove(g)),o.delete(l.annotation)},u=l=>{d(l),r(l)},b=(l,c=!0)=>{c&&a();const g=l.map(w=>({target:w,rects:i(w)}));g.forEach(({target:w,rects:T})=>o.set(w.annotation,T));const p=g.reduce((w,{rects:T})=>[...w,...T],[]);n.load(p)},h=(l,c)=>{const g=n.search({minX:l,minY:c,maxX:l,maxY:c}),p=w=>w.annotation.rects.reduce((T,M)=>T+M.width*M.height,0);if(g.length>0)return g.sort((w,T)=>p(w)-p(T)),g[0].annotation.id},E=l=>{const c=m(l);if(c.length===0)return;let g=c[0].left,p=c[0].top,w=c[0].right,T=c[0].bottom;for(let M=1;M<c.length;M++){const f=c[M];g=Math.min(g,f.left),p=Math.min(p,f.top),w=Math.max(w,f.right),T=Math.max(T,f.bottom)}return new DOMRect(g,p,w-g,T-p)},m=l=>{const c=o.get(l);return c?c[0].annotation.rects:[]};return{all:s,clear:a,getAt:h,getBoundsForAnnotation:E,getDOMRectsForAnnotation:m,getIntersectingRects:(l,c,g,p)=>n.search({minX:l,minY:c,maxX:g,maxY:p}),insert:r,recalculate:()=>b(e.all().map(l=>l.target),!0),remove:d,set:b,size:()=>n.all().length,update:u}},W=e=>{const{range:t}=e.target.selector;return t instanceof Range&&!t.collapsed},rt=(e,t)=>{const n=St(),o=Kt(n,e),i=pt(n,t),s=gt(n),a=Ct(),r=(y,l=C.LOCAL)=>{const c=y.target.selector.range instanceof Range?y:{...y,target:I(y.target,e)},{range:g}=c.target.selector,p=g&&!g.collapsed;return p&&n.addAnnotation(c,l),p},d=(y,l=!0,c=C.LOCAL)=>{const g=y.map(w=>W(w)?w:{...w,target:I(w.target,e)});if(g.some(w=>w.target.selector.range.collapsed)){const w=g.filter(T=>T.target.selector.range.collapsed);return console.warn("Could not revive all targets"),console.warn(w),n.bulkAddAnnotation(g,l,c),w}else return n.bulkAddAnnotation(g,l,c),[]},u=(y,l=C.LOCAL)=>{const c=y.selector.range instanceof Range?y:I(y,e);n.updateTarget(c,l)},b=(y,l=C.LOCAL)=>{const c=y.map(g=>g.selector.range instanceof Range?g:I(g,e));n.bulkUpdateTargets(c,l)},h=(y,l)=>{const c=o.getAt(y,l);return c?n.getAnnotation(c):void 0},E=(y,l,c,g)=>{const p=o.getIntersectingRects(y,l,c,g);return Array.from(new Set(p.map(T=>T.annotation.id))).map(T=>n.getAnnotation(T)).filter(T=>T)},m=(y,l,c,g=5)=>{const p=o.getDOMRectsForAnnotation(y);if(p.length>0){if(l&&c){const w=p.find(({top:T,right:M,bottom:f,left:x})=>l>=x-g&&l<=M+g&&c>=T-g&&c<=f+g);if(w)return w}return o.getBoundsForAnnotation(y)}},v=(y,l,c,g)=>{const w=o.getIntersectingRects(y,l,c,g).reduce((T,M)=>((T[M.annotation.id]=T[M.annotation.id]||[]).push(M),T),{});return Object.entries(w).map(([T,M])=>({annotation:n.getAnnotation(T),rects:M.map(({minX:f,minY:x,maxX:A,maxY:S})=>({x:f,y:x,width:A-f,height:S-x}))}))},L=()=>o.recalculate();return n.observe(({changes:y})=>{const l=(y.created||[]).filter(W),c=(y.deleted||[]).filter(W),g=(y.updated||[]).filter(p=>W(p.newValue));l.length>0&&o.set(l.map(p=>p.target),!1),(c==null?void 0:c.length)>0&&c.forEach(p=>o.remove(p.target)),(g==null?void 0:g.length)>0&&g.forEach(({newValue:p})=>o.update(p.target))}),{store:{...n,addAnnotation:r,bulkAddAnnotation:d,bulkUpdateTargets:b,getAnnotationBounds:m,getAt:h,getIntersecting:E,getIntersectingRects:v,recalculatePositions:L,updateTarget:u},selection:i,hover:s,viewport:a}},Gt=(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,u,b)=>{t.font&&(d.font=t.font);const h=n.get(s.id);if(h){const{x:E,y:m,height:v}=a[0];d.fillStyle=h.appearance.color,d.fillRect(E-2,m-2.5,2,v+5);const L=d.measureText(h.appearance.label),y=L.width+6,l=L.actualBoundingBoxAscent+L.actualBoundingBoxDescent+8,c=L.fontBoundingBoxAscent?8:6.5;d.fillRect(E-2,m-2.5-l,y,l),d.fillStyle="#fff",d.fillText(h.appearance.label,E+1,m-c),r.fillStyle=h.appearance.color,r.globalAlpha=u?.45:.18,a.forEach(({x:g,y:p,width:w,height:T})=>r.fillRect(g,p-2.5,w,T+5))}else j.paint(s,a,r,d,u,b)}}},lt=e=>e===null?null:e.scrollHeight>e.clientHeight?e:lt(e.parentElement),Zt=(e,t)=>n=>{const o=lt(e);if(o){const i=t.getAnnotation(n.id),{range:s}=i.target.selector;if(!s||s.collapsed)return!1;const a=o.getBoundingClientRect(),r=o.clientHeight,d=o.clientWidth,u=i.target.selector.range.getBoundingClientRect(),{width:b,height:h}=t.getAnnotationBounds(n.id),E=u.top-a.top,m=u.left-a.left,v=o.parentElement?o.scrollTop:0,L=o.parentElement?o.scrollLeft:0,y=E+v-(r-h)/2,l=m+L-(d-b)/2;return o.scroll({top:y,left:l,behavior:"smooth"}),!0}};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=null;const r=E=>s=E;let d=!1,u;e.addEventListener("selectstart",E=>{if(!d)return;!E.target.parentElement.closest(".not-annotatable")?a={annotation:ne(),selector:void 0,creator:s,created:new Date}:(a=null,u=void 0)});let b;document.addEventListener("selectionchange",E=>{b&&clearTimeout(b),b=setTimeout(()=>h(),50)});const h=()=>{var m,v;const E=document.getSelection();if(!E.isCollapsed&&d&&a){const L=Array.from(Array(E.rangeCount).keys()).map(c=>E.getRangeAt(c));Jt(L[0]).toString()!==((v=(m=a.selector)==null?void 0:m.range)==null?void 0:v.toString())&&(a={...a,selector:oe(L[0],e,n)},o.getAnnotation(a.annotation)?o.updateTarget(a,C.LOCAL):(o.addAnnotation({id:a.annotation,bodies:[],target:a}),i.clickSelect(a.annotation,u)))}};return e.addEventListener("pointerdown",E=>{u=E,d=E.button===0}),document.addEventListener("pointerup",E=>{var v;u=E,!((v=E.target.parentElement)!=null&&v.closest(".not-annotatable")||!d)&&setTimeout(()=>{if(a!=null&&a.selector)o.updateTarget(a,C.LOCAL),i.clickSelect(a.annotation,E),a=null,u=void 0;else{const{x:L,y}=e.getBoundingClientRect(),l=o.getAt(E.clientX-L,E.clientY-y);if(l){const{selected:c}=i;(c.length!==1||c[0].id!==l.id)&&(i.clickSelect(l.id,E),u=void 0)}else i.isEmpty()||i.clear()}},50)}),{setUser:r}},pe="",se=(e,t={})=>{const n=rt(e,t.pointerAction),{selection:o,viewport:i}=n,s=n.store,a=Mt(s),r=Rt(n,a,t.adapter);let d=Vt();const u=nt(e,n,i);t.style&&u.setDrawingStyle(t.style);const b=ie(e,n,t.offsetReferenceSelector);return b.setUser(d),{...Xt(n,a,t.adapter),destroy:()=>{throw"Not implemented yet"},element:e,getUser:()=>d,setFilter:g=>u.setFilter(g),setStyle:g=>u.setDrawingStyle(g),setUser:g=>{d=g,b.setUser(g)},setSelected:g=>{g?o.setSelected(g):o.clear()},setPresenceProvider:g=>{g&&(u.setPainter(Gt(g,t.presence)),g.on("selectionChange",()=>u.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=I,Object.defineProperty(X,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:u,height:b})=>n.fillRect(r,d-2.5,u,b+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)}},fe="",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 u=ut(n);e.classList.add("r6o-annotatable");const b=tt("r6o-highlight-layer bg"),h=tt("r6o-highlight-layer fg",!0),v=b.getContext("2d"),m=h.getContext("2d");e.insertBefore(b,e.firstChild),e.appendChild(h),e.addEventListener("pointermove",E=>{const{x:T,y:L}=e.getBoundingClientRect(),f=o.getAt(E.clientX-T,E.clientY-L);f&&(!r||r(f))?s.current!==f.id&&(e.classList.add("hovered"),s.set(f.id)):s.current&&(e.classList.remove("hovered"),s.set(null))});const x=()=>{const{top:E,left:T}=e.getBoundingClientRect(),{innerWidth:L,innerHeight:f}=window,w=-T,A=-E,S=L-T,B=f-E;return{top:E,left:T,minX:w,minY:A,maxX:S,maxY:B}},M=()=>c();document.addEventListener("scroll",M,{capture:!0,passive:!0});const y=ht(()=>{et(b),et(h,!0),o.recalculatePositions(),c()});window.addEventListener("resize",y),new ResizeObserver(y).observe(e);const c=()=>requestAnimationFrame(()=>{const{top:E,left:T,minX:L,minY:f,maxX:w,maxY:A}=x(),S=r?o.getIntersectingRects(L,f,w,A).filter(({annotation:Y})=>r(Y)):o.getIntersectingRects(L,f,w,A),{width:B,height:R}=h,U=new Set(i.selected.map(({id:Y})=>Y));m.clearRect(-.5,-.5,B+1,R+1),v.clearRect(-.5,-.5,B+1,R+1),S.forEach(({annotation:Y,rects:ae})=>{const re=ae.map(({x:ce,y:de,width:ue,height:he})=>({x:ce+T,y:de+E,width:ue,height:he})),le=U.has(Y.id);d.paint(Y,re,v,m,le,a)}),setTimeout(()=>u(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}},I=(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 u=!i;for(;d!==null;){const b=d.textContent.length;if(!u&&i&&(u=i.contains(d)),u){if(a+b>n){r.setStart(d,n-a);break}a+=b}d=s.nextNode()}for(;d!==null;){const b=d.textContent.length;if(a+b>o){r.setEnd(d,o-a);break}a+=b,d=s.nextNode()}return{...e,selector:{...e.selector,range:r}}};var ot=Object.prototype.hasOwnProperty;function D(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--&&D(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)||!D(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 u of o)u[1](),V.push(u,e);if(d){for(let u=0;u<V.length;u+=2)V[u][0](V[u+1]);V.length=0}}}function s(r){i(r(e))}function a(r,d=G){const u=[r,d];return o.add(u),o.size===1&&(n=t(i,s)||G),r(e),()=>{o.delete(u),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(h=>i=h);const s=()=>o(K),a=()=>{var h;return((h=i.selected)==null?void 0:h.length)===0},r=h=>{if(i.selected.length===0)return!1;const v=typeof h=="string"?h:h.id;return i.selected.some(m=>m.id===v)},d=(h,v)=>{const m=e.getAnnotation(h);if(m){const x=mt(m,t);o(x==="EDIT"?{selected:[{id:h,editable:!0}],pointerEvent:v}:x==="SELECT"?{selected:[{id:h}],pointerEvent:v}:{selected:[],pointerEvent:v})}else console.warn("Invalid selection: "+h)},u=(h,v=!0)=>{const m=Array.isArray(h)?h:[h],x=m.map(M=>e.getAnnotation(M)).filter(Boolean);o({selected:x.map(({id:M})=>({id:M,editable:v}))}),x.length!==m.length&&console.warn("Invalid selection",h)},b=h=>{if(i.selected.length===0)return!1;const{selected:v}=i;v.filter(({id:m})=>h.includes(m)).length>0&&o({selected:v.filter(({id:m})=>!h.includes(m))})};return e.observe(({changes:h})=>b((h.deleted||[]).map(v=>v.id))),{clear:s,clickSelect:d,get selected(){return i?[...i.selected]:null},get pointerEvent(){return i?i.pointerEvent:null},isEmpty:a,isSelected:r,setSelected:u,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 At=(e,t)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},wt=(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&&!D(o,n)?o:void 0}}).filter(({oldBody:n})=>n).map(({oldBody:n,newBody:o})=>({oldBody:n,newBody:o})),yt=(e,t)=>!D(e.target,t.target),it=(e,t)=>{const n=At(e,t),o=wt(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(b=>r(b.bodiesCreated)||r(b.bodiesDeleted)||r(b.bodiesUpdated)),u=(o=i.updated)==null?void 0:o.some(b=>b.targetUpdated);if(a==="BODY_ONLY"&&d&&!u||a==="TARGET_ONLY"&&u&&!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(h=>h.id)),o=new Set((e.updated||[]).map(({newValue:h})=>h.id)),i=new Set((t.created||[]).map(h=>h.id)),s=new Set((t.deleted||[]).map(h=>h.id)),a=new Set((t.updated||[]).map(({oldValue:h})=>h.id)),r=new Set((t.updated||[]).filter(({oldValue:h})=>n.has(h.id)||o.has(h.id)).map(({oldValue:h})=>h.id)),d=[...(e.created||[]).filter(h=>!s.has(h.id)).map(h=>a.has(h.id)?t.updated.find(({oldValue:v})=>v.id===h.id).newValue:h),...t.created||[]],u=[...(e.deleted||[]).filter(h=>!i.has(h.id)),...(t.deleted||[]).filter(h=>!n.has(h.id))],b=[...(e.updated||[]).filter(({newValue:h})=>!s.has(h.id)).map(h=>{const{oldValue:v,newValue:m}=h;if(a.has(m.id)){const x=t.updated.find(M=>M.oldValue.id===m.id).newValue;return it(v,x)}else return h}),...(t.updated||[]).filter(({oldValue:h})=>!r.has(h.id))];return{created:d,deleted:u,updated:b}},Tt=e=>e.id!==void 0,St=()=>{const e=new Map,t=new Map,n=[],o=(f,w={})=>n.push({onChange:f,options:w}),i=f=>{const w=n.findIndex(A=>A.onChange==f);w>-1&&n.splice(w,1)},s=(f,w)=>{const A={origin:f,changes:{created:w.created||[],updated:w.updated||[],deleted:w.deleted||[]},state:[...e.values()]};n.forEach(S=>{Et(S,A)&&S.onChange(A)})},a=(f,w=C.LOCAL)=>{if(e.get(f.id))throw Error(`Cannot add annotation ${f.id} - exists already`);e.set(f.id,f),f.bodies.forEach(A=>t.set(A.id,f.id)),s(w,{created:[f]})},r=(f,w)=>{const A=typeof f=="string"?w:f,S=typeof f=="string"?f:f.id,B=e.get(S);if(B){const R=it(B,A);return S===A.id?e.set(S,A):(e.delete(S),e.set(A.id,A)),B.bodies.forEach(U=>t.delete(U.id)),A.bodies.forEach(U=>t.set(U.id,A.id)),R}else console.warn(`Cannot update annotation ${S} - does not exist`)},d=(f,w=C.LOCAL,A=C.LOCAL)=>{const S=Tt(w)?A:w,B=r(f,w);B&&s(S,{updated:[B]})},u=(f,w=C.LOCAL)=>{const A=f.reduce((S,B)=>{const R=r(B);return R?[...S,R]:S},[]);A.length>0&&s(w,{updated:A})},b=(f,w=C.LOCAL)=>{const A=e.get(f.annotation);if(A){const S={...A,bodies:[...A.bodies,f]};e.set(A.id,S),t.set(f.id,S.id),s(w,{updated:[{oldValue:A,newValue:S,bodiesCreated:[f]}]})}else console.warn(`Attempt to add body to missing annotation: ${f.annotation}`)},h=()=>[...e.values()],v=(f=C.LOCAL)=>{const w=[...e.values()];e.clear(),t.clear(),s(f,{deleted:w})},m=(f,w=!0,A=C.LOCAL)=>{if(w){const S=[...e.values()];e.clear(),t.clear(),f.forEach(B=>{e.set(B.id,B),B.bodies.forEach(R=>t.set(R.id,B.id))}),s(A,{created:f,deleted:S})}else{const S=f.reduce((B,R)=>{const U=e.get(R.id);return U?[...B,U]:B},[]);if(S.length>0)throw Error(`Bulk insert would overwrite the following annotations: ${S.map(B=>B.id).join(", ")}`);f.forEach(B=>{e.set(B.id,B),B.bodies.forEach(R=>t.set(R.id,B.id))}),s(A,{created:f})}},x=f=>{const w=typeof f=="string"?f:f.id,A=e.get(w);if(A)return e.delete(w),A.bodies.forEach(S=>t.delete(S.id)),A;console.warn(`Attempt to delete missing annotation: ${w}`)},M=(f,w=C.LOCAL)=>{const A=x(f);A&&s(w,{deleted:[A]})},y=(f,w=C.LOCAL)=>{const A=f.reduce((S,B)=>{const R=x(B);return R?[...S,R]:S},[]);A.length>0&&s(w,{deleted:A})},l=(f,w=C.LOCAL)=>{const A=e.get(f.annotation);if(A){const S=A.bodies.find(B=>B.id===f.id);if(S){t.delete(S.id);const B={...A,bodies:A.bodies.filter(R=>R.id!==f.id)};e.set(A.id,B),s(w,{updated:[{oldValue:A,newValue:B,bodiesDeleted:[S]}]})}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}`)},c=f=>{const w=e.get(f);return w?{...w}:void 0},g=f=>{const w=t.get(f);if(w){const A=c(w).bodies.find(S=>S.id===f);if(A)return A;console.error(`Store integrity error: body ${f} in index, but not in annotation`)}else console.warn(`Attempt to retrieve missing body: ${f}`)},p=(f,w)=>{if(f.annotation!==w.annotation)throw"Annotation integrity violation: annotation ID must be the same when updating bodies";const A=e.get(f.annotation);if(A){const S=A.bodies.find(R=>R.id===f.id),B={...A,bodies:A.bodies.map(R=>R.id===S.id?w:R)};return e.set(A.id,B),S.id!==w.id&&(t.delete(S.id),t.set(w.id,B.id)),{oldValue:A,newValue:B,bodiesUpdated:[{oldBody:S,newBody:w}]}}else console.warn(`Attempt to add body to missing annotation ${f.annotation}`)},E=(f,w,A=C.LOCAL)=>{const S=p(f,w);S&&s(A,{updated:[S]})},T=(f,w=C.LOCAL)=>{const A=f.map(S=>p({id:S.id,annotation:S.annotation},S)).filter(Boolean);s(w,{updated:A})},L=f=>{const w=e.get(f.annotation);if(w){const A={...w,target:{...w.target,...f}};return e.set(w.id,A),{oldValue:w,newValue:A,targetUpdated:{oldTarget:w.target,newTarget:f}}}else console.warn(`Attempt to update target on missing annotation: ${f.annotation}`)};return{addAnnotation:a,addBody:b,all:h,bulkAddAnnotation:m,bulkDeleteAnnotation:y,bulkUpdateAnnotation:u,bulkUpdateBodies:T,bulkUpdateTargets:(f,w=C.LOCAL)=>{const A=f.map(S=>L(S)).filter(Boolean);A.length>0&&s(w,{updated:A})},clear:v,deleteAnnotation:M,deleteBody:l,getAnnotation:c,getBody:g,observe:o,unobserve:i,updateAnnotation:d,updateBody:E,updateTarget:(f,w=C.LOCAL)=>{const A=L(f);A&&s(w,{updated:[A]})}}};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=m=>{if(!i){const{changes:x}=m,M=performance.now();if(M-s>Mt)n.splice(o+1),n.push(x),o=n.length-1;else{const y=n.length-1;n[y]=vt(n[y],x)}s=M}i=!1};e.observe(a,{origin:C.LOCAL});const r=m=>m&&m.length>0&&e.bulkDeleteAnnotation(m),d=m=>m&&m.length>0&&e.bulkAddAnnotation(m,!1),u=m=>m&&m.length>0&&e.bulkUpdateAnnotation(m.map(({oldValue:x})=>x)),b=m=>m&&m.length>0&&e.bulkUpdateAnnotation(m.map(({newValue:x})=>x)),h=m=>m&&m.length>0&&e.bulkAddAnnotation(m,!1),v=m=>m&&m.length>0&&e.bulkDeleteAnnotation(m);return{canRedo:()=>n.length-1>o,canUndo:()=>o>-1,destroy:()=>e.unobserve(a),on:(m,x)=>t.on(m,x),redo:()=>{if(n.length-1>o){i=!0;const{created:m,updated:x,deleted:M}=n[o+1];d(m),b(x),v(M),t.emit("redo",n[o+1]),o+=1}},undo:()=>{if(o>-1){i=!0;const{created:m,updated:x,deleted:M}=n[o];r(m),u(x),h(M),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 u=[],b,h;const v=(l,c)=>{d.has(l)?d.get(l).push(c):d.set(l,[c])},m=(l,c)=>{const g=d.get(l);g&&g.indexOf(c)>0&&g.splice(g.indexOf(c),1)},x=(l,c,g)=>{d.has(l)&&setTimeout(()=>{d.get(l).forEach(p=>{if(n){const E=Array.isArray(c)?c.map(L=>n.serialize(L)):n.serialize(c),T=g?g instanceof PointerEvent?g:n.serialize(g):void 0;p(E,T)}else p(c,g)})},1)},M=()=>{const{selected:l}=s,c=(l||[]).map(({id:g})=>i.getAnnotation(g));c.forEach(g=>{const p=u.find(E=>E.id===g.id);(!p||!D(p,g))&&x("updateAnnotation",g,p)}),u=u.map(g=>c.find(({id:E})=>E===g.id)||g)};s.subscribe(({selected:l})=>{if(!(u.length===0&&l.length===0)){if(u.length===0&&l.length>0)u=l.map(({id:c})=>i.getAnnotation(c));else if(u.length>0&&l.length===0)u.forEach(c=>{const g=i.getAnnotation(c.id);g&&!D(g,c)&&x("updateAnnotation",g,c)}),u=[];else{const c=new Set(u.map(p=>p.id)),g=new Set(l.map(({id:p})=>p));u.filter(p=>!g.has(p.id)).forEach(p=>{const E=i.getAnnotation(p.id);E&&!D(E,p)&&x("updateAnnotation",E,p)}),u=[...u.filter(p=>g.has(p.id)),...l.filter(({id:p})=>!c.has(p)).map(({id:p})=>i.getAnnotation(p))]}x("selectionChanged",u)}}),a.subscribe(l=>{!b&&l?x("mouseEnterAnnotation",i.getAnnotation(l)):b&&!l?x("mouseLeaveAnnotation",i.getAnnotation(b)):b&&l&&(x("mouseLeaveAnnotation",i.getAnnotation(b)),x("mouseEnterAnnotation",i.getAnnotation(l))),b=l}),r==null||r.subscribe(l=>x("viewportIntersect",l.map(c=>i.getAnnotation(c)))),i.observe(l=>{o&&(h&&clearTimeout(h),h=setTimeout(M,1e3));const{created:c,deleted:g}=l.changes;(c||[]).forEach(p=>x("createAnnotation",p)),(g||[]).forEach(p=>x("deleteAnnotation",p)),(l.changes.updated||[]).filter(p=>[...p.bodiesCreated||[],...p.bodiesDeleted||[],...p.bodiesUpdated||[]].length>0).forEach(({oldValue:p,newValue:E})=>{const T=u.find(L=>L.id===p.id)||p;u=u.map(L=>L.id===p.id?E:L),x("updateAnnotation",E,T)})},{origin:C.LOCAL}),i.observe(l=>{if(u){const c=new Set(u.map(p=>p.id)),g=(l.changes.updated||[]).filter(({newValue:p})=>c.has(p.id)).map(({newValue:p})=>p);g.length>0&&(u=u.map(p=>g.find(T=>T.id===p.id)||p))}},{origin:C.REMOTE});const y=l=>c=>{const{created:g,deleted:p,updated:E}=c;(g||[]).forEach(T=>x("createAnnotation",T)),(p||[]).forEach(T=>x("deleteAnnotation",T)),l?(E||[]).forEach(T=>x("updateAnnotation",T.oldValue,T.newValue)):(E||[]).forEach(T=>x("updateAnnotation",T.newValue,T.oldValue))};return t.on("undo",y(!0)),t.on("redo",y(!1)),{on:v,off:m,emit:x}},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=y=>{if(n){const{parsed:l,error:c}=n.parse(y);l?o.addAnnotation(l,C.REMOTE):console.error(c)}else o.addAnnotation(y,C.REMOTE)},a=()=>i.clear(),r=()=>o.clear(),d=y=>{const l=o.getAnnotation(y);return n&&l?n.serialize(l):l},u=()=>n?o.all().map(n.serialize):o.all(),b=()=>{var y;const l=(((y=i.selected)==null?void 0:y.map(c=>c.id))||[]).map(c=>o.getAnnotation(c)).filter(Boolean);return n?l.map(n.serialize):l},h=y=>fetch(y).then(l=>l.json()).then(l=>(m(l),l)),v=y=>{if(typeof y=="string"){const l=o.getAnnotation(y);if(o.deleteAnnotation(y),l)return n?n.serialize(l):l}else{const l=n?n.parse(y).parsed:y;if(l)return o.deleteAnnotation(l),y}},m=y=>{if(n){const{parsed:l,failed:c}=Ot(n)(y);c.length>0&&console.warn(`Discarded ${c.length} invalid annotations`,c),o.bulkAddAnnotation(l,!0,C.REMOTE)}else o.bulkAddAnnotation(y,!0,C.REMOTE)},x=y=>{y?i.setSelected(y):i.clear()},M=y=>{if(n){const l=n.parse(y).parsed,c=n.serialize(o.getAnnotation(l.id));return o.updateAnnotation(l),c}else{const l=o.getAnnotation(y.id);return o.updateAnnotation(y),l}};return{addAnnotation:s,cancelSelected:a,canRedo:t.canRedo,canUndo:t.canUndo,clearAnnotations:r,getAnnotationById:d,getAnnotations:u,getSelected:b,loadAnnotations:h,redo:t.redo,removeAnnotation:v,setAnnotations:m,setSelected:x,undo:t.undo,updateAnnotation:M}};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),u=.5*Math.sqrt(r*d*(s-d)/s)*(a-s/2<0?-1:1),b=Math.max(n,Math.floor(t-a*d/s+u)),h=Math.min(o,Math.floor(t+(s-a)*d/s+u));st(e,t,b,h,i)}var v=e[t],m=n,x=o;for(P(e,n,t),i(e[o],v)>0&&P(e,n,o);m<x;){for(P(e,m,x),m++,x--;i(e[m],v)<0;)m++;for(;i(e[x],v)>0;)x--}i(e[n],v)===0?P(e,n,x):(x++,P(e,x,o)),x<=t&&(n=x+1),t<=x&&(o=x-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,u;for(;o||s.length;){if(o||(o=s.pop(),d=s[s.length-1],r=a.pop(),u=!0),o.leaf){const b=Pt(t,o.children,n);if(b!==-1)return o.children.splice(b,1),s.push(o),this._condense(s),this}!u&&!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],u=!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),u=d*Math.ceil(Math.sqrt(a));at(t,n,o,u,this.compareMinX);for(let b=n;b<=o;b+=u){const h=Math.min(b+u-1,o);at(t,b,h,d,this.compareMinY);for(let v=b;v<=h;v+=d){const m=Math.min(v+d-1,h);r.children.push(this._build(t,v,m,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 u=n.children[d],b=Z(u),h=Ft(t,u)-b;h<a?(a=h,s=b<s?b:s,r=u):h===a&&b<s&&(s=b,r=u)}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),u=H(t,r,o,this.toBBox),b=$t(d,u),h=Z(d)+Z(u);b<s?(s=b,i=r,a=h<a?h:a):b===s&&h<a&&(a=h,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 u=n;u<o-n;u++){const b=t.children[u];z(a,t.leaf?s(b):b),d+=F(a)}for(let u=o-n-1;u>=n;u--){const b=t.children[u];z(r,t.leaf?s(b):b),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(),p=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:I(l,t),E=Array.from(p.selector.range.getClientRects()),T=jt(E);return T.map(L=>{const{x:f,y:w,width:A,height:S}=L;return{minX:f-c.x,minY:w-c.y,maxX:f-c.x+A,maxY:w-c.y+S,annotation:{id:l.annotation,rects:T}}})},s=()=>[...o.values()],a=()=>{n.clear(),o.clear()},r=l=>{const c=i(l);c.forEach(g=>n.insert(g)),o.set(l.annotation,c)},d=l=>{o.get(l.annotation).forEach(g=>n.remove(g)),o.delete(l.annotation)},u=l=>{d(l),r(l)},b=(l,c=!0)=>{c&&a();const g=l.map(E=>({target:E,rects:i(E)}));g.forEach(({target:E,rects:T})=>o.set(E.annotation,T));const p=g.reduce((E,{rects:T})=>[...E,...T],[]);n.load(p)},h=(l,c)=>{const g=n.search({minX:l,minY:c,maxX:l,maxY:c}),p=E=>E.annotation.rects.reduce((T,L)=>T+L.width*L.height,0);if(g.length>0)return g.sort((E,T)=>p(E)-p(T)),g[0].annotation.id},v=l=>{const c=m(l);if(c.length===0)return;let g=c[0].left,p=c[0].top,E=c[0].right,T=c[0].bottom;for(let L=1;L<c.length;L++){const f=c[L];g=Math.min(g,f.left),p=Math.min(p,f.top),E=Math.max(E,f.right),T=Math.max(T,f.bottom)}return new DOMRect(g,p,E-g,T-p)},m=l=>{const c=o.get(l);return c?c[0].annotation.rects:[]};return{all:s,clear:a,getAt:h,getBoundsForAnnotation:v,getDOMRectsForAnnotation:m,getIntersectingRects:(l,c,g,p)=>n.search({minX:l,minY:c,maxX:g,maxY:p}),insert:r,recalculate:()=>b(e.all().map(l=>l.target),!0),remove:d,set:b,size:()=>n.all().length,update:u}},W=e=>{const{range:t}=e.target.selector;return t instanceof Range&&!t.collapsed},rt=(e,t)=>{const n=St(),o=Jt(n,e),i=pt(n,t),s=gt(n),a=Ct(),r=(y,l=C.LOCAL)=>{const c=y.target.selector.range instanceof Range?y:{...y,target:I(y.target,e)},{range:g}=c.target.selector,p=g&&!g.collapsed;return p&&n.addAnnotation(c,l),p},d=(y,l=!0,c=C.LOCAL)=>{const g=y.map(E=>W(E)?E:{...E,target:I(E.target,e)});if(g.some(E=>E.target.selector.range.collapsed)){const E=g.filter(T=>T.target.selector.range.collapsed);return console.warn("Could not revive all targets"),console.warn(E),n.bulkAddAnnotation(g,l,c),E}else return n.bulkAddAnnotation(g,l,c),[]},u=(y,l=C.LOCAL)=>{const c=y.selector.range instanceof Range?y:I(y,e);n.updateTarget(c,l)},b=(y,l=C.LOCAL)=>{const c=y.map(g=>g.selector.range instanceof Range?g:I(g,e));n.bulkUpdateTargets(c,l)},h=(y,l)=>{const c=o.getAt(y,l);return c?n.getAnnotation(c):void 0},v=(y,l,c,g)=>{const p=o.getIntersectingRects(y,l,c,g);return Array.from(new Set(p.map(T=>T.annotation.id))).map(T=>n.getAnnotation(T)).filter(T=>T)},m=(y,l,c,g=5)=>{const p=o.getDOMRectsForAnnotation(y);if(p.length>0){if(l&&c){const E=p.find(({top:T,right:L,bottom:f,left:w})=>l>=w-g&&l<=L+g&&c>=T-g&&c<=f+g);if(E)return E}return o.getBoundsForAnnotation(y)}},x=(y,l,c,g)=>{const E=o.getIntersectingRects(y,l,c,g).reduce((T,L)=>((T[L.annotation.id]=T[L.annotation.id]||[]).push(L),T),{});return Object.entries(E).map(([T,L])=>({annotation:n.getAnnotation(T),rects:L.map(({minX:f,minY:w,maxX:A,maxY:S})=>({x:f,y:w,width:A-f,height:S-w}))}))},M=()=>o.recalculate();return n.observe(({changes:y})=>{const l=(y.created||[]).filter(W),c=(y.deleted||[]).filter(W),g=(y.updated||[]).filter(p=>W(p.newValue));l.length>0&&o.set(l.map(p=>p.target),!1),(c==null?void 0:c.length)>0&&c.forEach(p=>o.remove(p.target)),(g==null?void 0:g.length)>0&&g.forEach(({newValue:p})=>o.update(p.target))}),{store:{...n,addAnnotation:r,bulkAddAnnotation:d,bulkUpdateTargets:b,getAnnotationBounds:m,getAt:h,getIntersecting:v,getIntersectingRects:x,recalculatePositions:M,updateTarget:u},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,u,b)=>{t.font&&(d.font=t.font);const h=n.get(s.id);if(h){const{x:v,y:m,height:x}=a[0];d.fillStyle=h.appearance.color,d.fillRect(v-2,m-2.5,2,x+5);const M=d.measureText(h.appearance.label),y=M.width+6,l=M.actualBoundingBoxAscent+M.actualBoundingBoxDescent+8,c=M.fontBoundingBoxAscent?8:6.5;d.fillRect(v-2,m-2.5-l,y,l),d.fillStyle="#fff",d.fillText(h.appearance.label,v+1,m-c),r.fillStyle=h.appearance.color,r.globalAlpha=u?.45:.18,a.forEach(({x:g,y:p,width:E,height:T})=>r.fillRect(g,p-2.5,E,T+5))}else j.paint(s,a,r,d,u,b)}}},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=lt(e);if(o){const i=t.getAnnotation(n.id),{range:s}=i.target.selector;if(!s||s.collapsed)return!1;const a=o.getBoundingClientRect(),r=o.clientHeight,d=o.clientWidth,u=i.target.selector.range.getBoundingClientRect(),{width:b,height:h}=t.getAnnotationBounds(n.id),v=u.top-a.top,m=u.left-a.left,x=o.parentElement?o.scrollTop:0,M=o.parentElement?o.scrollLeft:0,y=v+x-(r-h)/2,l=m+M-(d-b)/2;return o.scroll({top:y,left:l,behavior:"smooth"}),!0}};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=null;const r=v=>s=v;let d=!1,u;e.addEventListener("selectstart",v=>{if(!d)return;!v.target.parentElement.closest(".not-annotatable")?a={annotation:ne(),selector:void 0,creator:s,created:new Date}:(a=null,u=void 0)});let b;document.addEventListener("selectionchange",v=>{b&&clearTimeout(b),b=setTimeout(()=>h(),50)});const h=()=>{var m,x;const v=document.getSelection();if(!v.isCollapsed&&d&&a){const M=Array.from(Array(v.rangeCount).keys()).map(c=>v.getRangeAt(c));Gt(M[0]).toString()!==((x=(m=a.selector)==null?void 0:m.range)==null?void 0:x.toString())&&(a={...a,selector:oe(M[0],e,n)},o.getAnnotation(a.annotation)?o.updateTarget(a,C.LOCAL):(o.addAnnotation({id:a.annotation,bodies:[],target:a}),i.clickSelect(a.annotation,u)))}};return e.addEventListener("pointerdown",v=>{u=v,d=v.button===0}),document.addEventListener("pointerup",v=>{var x;u=v,!((x=v.target.parentElement)!=null&&x.closest(".not-annotatable")||!d)&&setTimeout(()=>{if(a!=null&&a.selector)o.updateTarget(a,C.LOCAL),i.clickSelect(a.annotation,v),a=null,u=void 0;else{const{x:M,y}=e.getBoundingClientRect(),l=o.getAt(v.clientX-M,v.clientY-y);if(l){const{selected:c}=i;(c.length!==1||c[0].id!==l.id)&&(i.clickSelect(l.id,v),u=void 0)}else i.isEmpty()||i.clear()}},50)}),{setUser:r}},pe="",se=(e,t={})=>{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 u=nt(e,n,i);t.style&&u.setDrawingStyle(t.style);const b=ie(e,n,t.offsetReferenceSelector);return b.setUser(d),{...Xt(n,a,t.adapter),destroy:()=>{throw"Not implemented yet"},element:e,getUser:()=>d,setFilter:g=>u.setFilter(g),setStyle:g=>u.setDrawingStyle(g),setUser:g=>{d=g,b.setUser(g)},setSelected:g=>{g?o.setSelected(g):o.clear()},setPresenceProvider:g=>{g&&(u.setPainter(Kt(g,t.presence)),g.on("selectionChange",()=>u.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=I,Object.defineProperty(X,Symbol.toStringTag,{value:"Module"})});
2
2
  //# sourceMappingURL=text-annotator.umd.js.map