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

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