@recogito/text-annotator 3.0.0-rc.17 → 3.0.0-rc.18

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,3 +1,3 @@
1
- (function(B,P){typeof exports=="object"&&typeof module<"u"?P(exports):typeof define=="function"&&define.amd?define(["exports"],P):(B=typeof globalThis<"u"?globalThis:B||self,P(B.RecogitoJS={}))})(this,function(B){"use strict";const P=t=>{const{top:e,left:n}=t.getBoundingClientRect(),{innerWidth:o,innerHeight:i}=window,r=-n,s=-e,a=o-n,d=i-e;return{top:e,left:n,minX:r,minY:s,maxX:a,maxY:d}},yt=t=>{let e=new Set;return o=>{const i=o.map(r=>r.id);(e.size!==i.length||i.some(r=>!e.has(r)))&&t.set(i),e=new Set(i)}},J=(t,e=10)=>{let n;return(...o)=>{clearTimeout(n),n=setTimeout(()=>t.apply(void 0,o),e)}},At="not-annotatable",H=`.${At}`,ne=t=>{var n;const e=t.commonAncestorContainer;return e instanceof HTMLElement?!e.closest(H):!((n=e.parentElement)!=null&&n.closest(H))},oe=function*(t){const e=document.createNodeIterator(t.commonAncestorContainer,NodeFilter.SHOW_ELEMENT,o=>o instanceof HTMLElement&&o.classList.contains(At)&&t.intersectsNode(o)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP);let n;for(;n=e.nextNode();)n instanceof HTMLElement&&(yield n)},wt=t=>{if(!ne(t))return[];const e=[];let n=null;for(const o of oe(t)){let i;n?(i=document.createRange(),i.setStartAfter(n),i.setEndBefore(o)):(i=t.cloneRange(),i.setEndBefore(o)),i.collapsed||e.push(i),n=o}if(n){const o=t.cloneRange();o.setStartAfter(n),o.collapsed||e.push(o)}return e.length>0?e:[t]},G=t=>{const e=t.cloneContents();return e.querySelectorAll(H).forEach(n=>n.remove()),e},ie=t=>{const e=t.cloneContents();return e.querySelectorAll(H).forEach(n=>n.remove()),e},xt=(t,e)=>{const n=document.createNodeIterator(e);let o=0,i=n.nextNode();for(;i!==null;){if(i===t)return o;o+=1,i=n.nextNode()}},Et=(t,e)=>{const n=document.createNodeIterator(e);let o=null;for(let i=0;i<t+1;i++)o=n.nextNode();return o},re=t=>{const{commonAncestorContainer:e,startContainer:n,startOffset:o,endContainer:i,endOffset:r}=t,s=Array.from(e.childNodes).map(c=>{const b=c.cloneNode(!0);return c.nodeName==="CANVAS"?c:b}),a=xt(n,e),d=xt(i,e),u=()=>{const c=e;c.replaceChildren(...s);const b=Et(a,c),p=Et(d,c);return t.setStart(b,o),t.setEnd(p,r),t},y=c=>{const b=document.createElement("SPAN");return c.surroundContents(b),b};if(n===i)throw"Not implemented";{const c=document.createRange();c.selectNodeContents(n),c.setStart(n,o);const b=y(c),p=document.createRange();p.selectNode(i),p.setEnd(i,r);const v=y(p),E=se(t).reverse().map(l=>{var m;const h=document.createElement("SPAN");return(m=l.parentNode)==null||m.insertBefore(h,l),h.appendChild(l),h});return{unwrap:u,nodes:[b,...E,v]}}},se=t=>{const{commonAncestorContainer:e,startContainer:n,endContainer:o}=t,i=document.createNodeIterator(e,NodeFilter.SHOW_TEXT);let r=i.nextNode(),s=!1;const a=[];for(;r!=null;)r===o&&(s=!1),s&&a.push(r),r===n&&(s=!0),r=i.nextNode();return a},ae=t=>{const{startContainer:e,endContainer:n}=t;if(e===n)return Array.from(t.getClientRects());{const{unwrap:o,nodes:i}=re(t),r=i.reduce((s,a)=>[...s,...a.getClientRects()],[]);return o(),r}},St=(t,e,n=10,o)=>{const i=o?t.startContainer.parentElement.closest(o):e,r=document.createRange();r.setStart(i,0),r.setEnd(t.startContainer,t.startOffset);const s=G(r).textContent,a=document.createRange();a.setStart(t.endContainer,t.endOffset),i===document.body?a.setEnd(i,i.childNodes.length):a.setEndAfter(i);const d=G(a).textContent;return{prefix:s.substring(s.length-n),suffix:d.substring(0,n)}},D=t=>t.every(e=>e.range instanceof Range&&!e.range.collapsed),ce=(t,e)=>{const n=r=>Math.round(r*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),r=Math.max(t.bottom,e.bottom);return new DOMRect(n,i,o-n,r-i)},Tt=t=>t.reduce((e,n)=>{if(n.width===0||n.height===0)return e;let o=[...e],i=!1;for(const r of e){const s=ce(n,r);if(s==="inline-adjacent"){o=o.map(a=>a===r?le(n,r):a),i=!0;break}else if(s==="inline-contains"){o=o.map(a=>a===r?n:a),i=!0;break}else if(s==="inline-is-contained"){i=!0;break}else if(s==="block-contains"||s==="block-is-contained"){n.width<r.width&&(o=o.map(a=>a===r?n:a)),i=!0;break}}return i?o:[...o,n]},[]),Ct=(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 r=G(o).textContent,s=t.toString(),a=r.length||0,d=a+s.length;return n?{quote:s,start:a,end:d,range:t,offsetReference:i}:{quote:s,start:a,end:d,range:t}},rt=(t,e)=>{var y,c;const{start:n,end:o}=t,i=t.offsetReference||e,r=document.createNodeIterator(e,NodeFilter.SHOW_TEXT,b=>{var p;return(p=b.parentElement)!=null&&p.closest(H)?NodeFilter.FILTER_SKIP:NodeFilter.FILTER_ACCEPT});let s=0;const a=document.createRange();let d=r.nextNode();d===null&&console.error("Could not revive annotation target. Content missing.");let u=!i;for(;d!==null;){if(u||(u=i==null?void 0:i.contains(d)),u){const b=((y=d.textContent)==null?void 0:y.length)||0;if(s+b>n){a.setStart(d,n-s);break}s+=b}d=r.nextNode()}for(;d!==null;){const b=((c=d.textContent)==null?void 0:c.length)||0;if(s+b>=o){a.setEnd(d,o-s);break}s+=b,d=r.nextNode()}return{...t,range:a}},W=(t,e)=>D(t.selector)?t:{...t,selector:t.selector.map(n=>n.range instanceof Range&&!n.range.collapsed?n:rt(n,e))},Z=(t,e)=>D(t.target.selector)?t:{...t,target:W(t.target,e)},Rt=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 r=e.nextSibling||e.parentNode,s=(r==null?void 0:r.nodeType)===Node.TEXT_NODE?r:Array.from(r.childNodes).filter(a=>a.nodeType===Node.TEXT_NODE).shift();t.setEnd(s,0)}if(n.nodeType!==Node.TEXT_NODE){const r=n.previousSibling||n.parentNode,s=(r==null?void 0:r.nodeType)===Node.TEXT_NODE?r:Array.from(r.childNodes).filter(a=>a.nodeType===Node.TEXT_NODE).pop();t.setEnd(s,((i=s==null?void 0:s.textContent)==null?void 0:i.length)||0)}return t},tt={fill:"rgb(0, 128, 255)",fillOpacity:.18},Lt={fill:"rgb(0, 128, 255)",fillOpacity:.45},de=()=>{const t=document.createElement("canvas");return t.width=window.innerWidth,t.height=window.innerHeight,t.className="r6o-highlight-layer bg",t},ue=(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)}},Nt=(t,e,n)=>{const{store:o,selection:i,hover:r}=e;let s,a,d;const u=yt(n);t.classList.add("r6o-annotatable");const y=de(),c=y.getContext("2d");t.insertBefore(y,t.firstChild);const b=g=>{const{x,y:w}=t.getBoundingClientRect(),S=o.getAt(g.clientX-x,g.clientY-w);S&&(!a||a(S))?r.current!==S.id&&(t.classList.add("hovered"),r.set(S.id)):r.current&&(t.classList.remove("hovered"),r.set(null))};t.addEventListener("pointermove",b);const p=()=>requestAnimationFrame(()=>{const g=P(t),{top:x,left:w,minX:S,minY:L,maxX:O,maxY:X}=g,Zt=a?o.getIntersectingRects(S,L,O,X).filter(({annotation:Y})=>a(Y)):o.getIntersectingRects(S,L,O,X),{width:mn,height:bn}=y,vn=new Set(i.selected.map(({id:Y})=>Y));c.clearRect(-.5,-.5,mn+1,bn+1),d&&d.clear(),Zt.forEach(Y=>{const gt=vn.has(Y.annotation.id),te=s?typeof s=="function"?s(Y.annotation,gt):s:gt?Lt:tt,ee=d&&d.paint(Y,g,gt)||te,yn=Y.rects.map(({x:pt,y:mt,width:bt,height:vt})=>({x:pt+w,y:mt+x,width:bt,height:vt}));c.fillStyle=ee.fill,c.globalAlpha=ee.fillOpacity||1,yn.forEach(({x:pt,y:mt,width:bt,height:vt})=>c.fillRect(pt,mt-2.5,bt,vt+5))}),setTimeout(()=>u(Zt.map(({annotation:Y})=>Y)),1)}),v=g=>{s=g,p()},T=g=>{a=g,p()},E=()=>p();o.observe(E);const l=i.subscribe(()=>p()),h=()=>p();document.addEventListener("scroll",h,{capture:!0,passive:!0});const m=J(()=>{ue(y),o.recalculatePositions(),d&&d.reset(),p()});window.addEventListener("resize",m);const f=new ResizeObserver(m);f.observe(t);const A={attributes:!0,childList:!0,subtree:!0},C=new MutationObserver(p);return C.observe(document.body,A),{destroy:()=>{t.removeEventListener("pointermove",b),t.removeChild(y),o.unobserve(E),l(),document.removeEventListener("scroll",h),window.removeEventListener("resize",m),f.disconnect(),C.disconnect()},refresh:p,setDrawingStyle:v,setFilter:T,setPainter:g=>d=g}};var he={grad:.9,turn:360,rad:360/(2*Math.PI)},V=function(t){return typeof t=="string"?t.length>0:typeof t=="number"},M=function(t,e,n){return e===void 0&&(e=0),n===void 0&&(n=Math.pow(10,e)),Math.round(n*t)/n+0},_=function(t,e,n){return e===void 0&&(e=0),n===void 0&&(n=1),t>n?n:t>e?t:e},Bt=function(t){return(t=isFinite(t)?t%360:0)>0?t:t+360},Ot=function(t){return{r:_(t.r,0,255),g:_(t.g,0,255),b:_(t.b,0,255),a:_(t.a)}},st=function(t){return{r:M(t.r),g:M(t.g),b:M(t.b),a:M(t.a,3)}},fe=/^#([0-9a-f]{3,8})$/i,et=function(t){var e=t.toString(16);return e.length<2?"0"+e:e},Mt=function(t){var e=t.r,n=t.g,o=t.b,i=t.a,r=Math.max(e,n,o),s=r-Math.min(e,n,o),a=s?r===e?(n-o)/s:r===n?2+(o-e)/s:4+(e-n)/s:0;return{h:60*(a<0?a+6:a),s:r?s/r*100:0,v:r/255*100,a:i}},It=function(t){var e=t.h,n=t.s,o=t.v,i=t.a;e=e/360*6,n/=100,o/=100;var r=Math.floor(e),s=o*(1-n),a=o*(1-(e-r)*n),d=o*(1-(1-e+r)*n),u=r%6;return{r:255*[o,a,s,s,d,o][u],g:255*[d,o,o,a,s,s][u],b:255*[s,s,d,o,o,a][u],a:i}},Xt=function(t){return{h:Bt(t.h),s:_(t.s,0,100),l:_(t.l,0,100),a:_(t.a)}},_t=function(t){return{h:M(t.h),s:M(t.s),l:M(t.l),a:M(t.a,3)}},Ut=function(t){return It((n=(e=t).s,{h:e.h,s:(n*=((o=e.l)<50?o:100-o)/100)>0?2*n/(o+n)*100:0,v:o+n,a:e.a}));var e,n,o},q=function(t){return{h:(e=Mt(t)).h,s:(i=(200-(n=e.s))*(o=e.v)/100)>0&&i<200?n*o/100/(i<=100?i:200-i)*100:0,l:i/2,a:e.a};var e,n,o,i},ge=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*,\s*([+-]?\d*\.?\d+)%\s*,\s*([+-]?\d*\.?\d+)%\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,pe=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,me=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,be=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,Yt={string:[[function(t){var e=fe.exec(t);return e?(t=e[1]).length<=4?{r:parseInt(t[0]+t[0],16),g:parseInt(t[1]+t[1],16),b:parseInt(t[2]+t[2],16),a:t.length===4?M(parseInt(t[3]+t[3],16)/255,2):1}:t.length===6||t.length===8?{r:parseInt(t.substr(0,2),16),g:parseInt(t.substr(2,2),16),b:parseInt(t.substr(4,2),16),a:t.length===8?M(parseInt(t.substr(6,2),16)/255,2):1}:null:null},"hex"],[function(t){var e=me.exec(t)||be.exec(t);return e?e[2]!==e[4]||e[4]!==e[6]?null:Ot({r:Number(e[1])/(e[2]?100/255:1),g:Number(e[3])/(e[4]?100/255:1),b:Number(e[5])/(e[6]?100/255:1),a:e[7]===void 0?1:Number(e[7])/(e[8]?100:1)}):null},"rgb"],[function(t){var e=ge.exec(t)||pe.exec(t);if(!e)return null;var n,o,i=Xt({h:(n=e[1],o=e[2],o===void 0&&(o="deg"),Number(n)*(he[o]||1)),s:Number(e[3]),l:Number(e[4]),a:e[5]===void 0?1:Number(e[5])/(e[6]?100:1)});return Ut(i)},"hsl"]],object:[[function(t){var e=t.r,n=t.g,o=t.b,i=t.a,r=i===void 0?1:i;return V(e)&&V(n)&&V(o)?Ot({r:Number(e),g:Number(n),b:Number(o),a:Number(r)}):null},"rgb"],[function(t){var e=t.h,n=t.s,o=t.l,i=t.a,r=i===void 0?1:i;if(!V(e)||!V(n)||!V(o))return null;var s=Xt({h:Number(e),s:Number(n),l:Number(o),a:Number(r)});return Ut(s)},"hsl"],[function(t){var e=t.h,n=t.s,o=t.v,i=t.a,r=i===void 0?1:i;if(!V(e)||!V(n)||!V(o))return null;var s=function(a){return{h:Bt(a.h),s:_(a.s,0,100),v:_(a.v,0,100),a:_(a.a)}}({h:Number(e),s:Number(n),v:Number(o),a:Number(r)});return It(s)},"hsv"]]},Dt=function(t,e){for(var n=0;n<e.length;n++){var o=e[n][0](t);if(o)return[o,e[n][1]]}return[null,void 0]},ve=function(t){return typeof t=="string"?Dt(t.trim(),Yt.string):typeof t=="object"&&t!==null?Dt(t,Yt.object):[null,void 0]},at=function(t,e){var n=q(t);return{h:n.h,s:_(n.s+100*e,0,100),l:n.l,a:n.a}},ct=function(t){return(299*t.r+587*t.g+114*t.b)/1e3/255},Vt=function(t,e){var n=q(t);return{h:n.h,s:n.s,l:_(n.l+100*e,0,100),a:n.a}},kt=function(){function t(e){this.parsed=ve(e)[0],this.rgba=this.parsed||{r:0,g:0,b:0,a:1}}return t.prototype.isValid=function(){return this.parsed!==null},t.prototype.brightness=function(){return M(ct(this.rgba),2)},t.prototype.isDark=function(){return ct(this.rgba)<.5},t.prototype.isLight=function(){return ct(this.rgba)>=.5},t.prototype.toHex=function(){return e=st(this.rgba),n=e.r,o=e.g,i=e.b,s=(r=e.a)<1?et(M(255*r)):"","#"+et(n)+et(o)+et(i)+s;var e,n,o,i,r,s},t.prototype.toRgb=function(){return st(this.rgba)},t.prototype.toRgbString=function(){return e=st(this.rgba),n=e.r,o=e.g,i=e.b,(r=e.a)<1?"rgba("+n+", "+o+", "+i+", "+r+")":"rgb("+n+", "+o+", "+i+")";var e,n,o,i,r},t.prototype.toHsl=function(){return _t(q(this.rgba))},t.prototype.toHslString=function(){return e=_t(q(this.rgba)),n=e.h,o=e.s,i=e.l,(r=e.a)<1?"hsla("+n+", "+o+"%, "+i+"%, "+r+")":"hsl("+n+", "+o+"%, "+i+"%)";var e,n,o,i,r},t.prototype.toHsv=function(){return e=Mt(this.rgba),{h:M(e.h),s:M(e.s),v:M(e.v),a:M(e.a,3)};var e},t.prototype.invert=function(){return U({r:255-(e=this.rgba).r,g:255-e.g,b:255-e.b,a:e.a});var e},t.prototype.saturate=function(e){return e===void 0&&(e=.1),U(at(this.rgba,e))},t.prototype.desaturate=function(e){return e===void 0&&(e=.1),U(at(this.rgba,-e))},t.prototype.grayscale=function(){return U(at(this.rgba,-1))},t.prototype.lighten=function(e){return e===void 0&&(e=.1),U(Vt(this.rgba,e))},t.prototype.darken=function(e){return e===void 0&&(e=.1),U(Vt(this.rgba,-e))},t.prototype.rotate=function(e){return e===void 0&&(e=15),this.hue(this.hue()+e)},t.prototype.alpha=function(e){return typeof e=="number"?U({r:(n=this.rgba).r,g:n.g,b:n.b,a:e}):M(this.rgba.a,3);var n},t.prototype.hue=function(e){var n=q(this.rgba);return typeof e=="number"?U({h:e,s:n.s,l:n.l,a:n.a}):M(n.h)},t.prototype.isEqual=function(e){return this.toHex()===U(e).toHex()},t}(),U=function(t){return t instanceof kt?t:new kt(t)};const ye=t=>`background-color: ${U(t.fill||tt.fill).alpha(t.fillOpacity||tt.fillOpacity).toHex()};`,Ae=()=>{const t=document.createElement("style");document.getElementsByTagName("head")[0].appendChild(t);let e,n=new Set;return{destroy:()=>{CSS.highlights.clear(),t.remove()},refresh:(s,a,d,u)=>{e&&e.clear();const y=new Set(s.map(v=>v.annotation.id)),c=new Set(d),b=Array.from(n).filter(v=>!y.has(v)),p=s.map(v=>{const T=c.has(v.annotation.id),E=u?typeof u=="function"?u(v.annotation,T):u:T?Lt:tt,l=e&&e.paint(v,a,T)||E;return`::highlight(_${v.annotation.id}) { ${ye(l)} }`});t.innerHTML=p.join(`
2
- `),b.forEach(v=>CSS.highlights.delete(`_${v}`)),s.forEach(({annotation:v})=>{const T=v.target.selector.map(l=>l.range),E=new Highlight(...T);CSS.highlights.set(`_${v.id}`,E)}),n=y},setPainter:s=>e=s}},Pt=(t,e,n)=>{const{store:o,selection:i,hover:r}=e;let s,a,d;const u=Ae(),y=yt(n),c=R=>{const{x:g,y:x}=t.getBoundingClientRect(),w=o.getAt(R.clientX-g,R.clientY-x);w&&(!d||d(w))?r.current!==w.id&&(t.classList.add("hovered"),r.set(w.id)):r.current&&(t.classList.remove("hovered"),r.set(null))};t.addEventListener("pointermove",c);const b=()=>{const R=P(t),{minX:g,minY:x,maxX:w,maxY:S}=R,L=d?o.getIntersectingRects(g,x,w,S).filter(({annotation:X})=>d(X)):o.getIntersectingRects(g,x,w,S),O=i.selected.map(({id:X})=>X);u.refresh(L,R,O,a),setTimeout(()=>y(L.map(({annotation:X})=>X)),1)},p=R=>{s=R,u.setPainter(R)},v=R=>{a=R,b()},T=R=>{d=R,b()},E=()=>b();o.observe(E);const l=i.subscribe(()=>b());document.addEventListener("scroll",b,{capture:!0,passive:!0});const h=J(()=>{o.recalculatePositions(),s&&s.reset(),b()});window.addEventListener("resize",h);const m=new ResizeObserver(h);m.observe(t);const f={attributes:!0,childList:!0,subtree:!0},A=new MutationObserver(b);return A.observe(document.body,f),{destroy:()=>{t.removeEventListener("pointermove",c),u.destroy(),o.unobserve(E),l(),document.removeEventListener("scroll",b),window.removeEventListener("resize",h),m.disconnect(),A.disconnect()},refresh:b,setDrawingStyle:v,setFilter:T,setPainter:p}};let nt;const we=new Uint8Array(16);function xe(){if(!nt&&(nt=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!nt))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return nt(we)}const I=[];for(let t=0;t<256;++t)I.push((t+256).toString(16).slice(1));function Ee(t,e=0){return I[t[e+0]]+I[t[e+1]]+I[t[e+2]]+I[t[e+3]]+"-"+I[t[e+4]]+I[t[e+5]]+"-"+I[t[e+6]]+I[t[e+7]]+"-"+I[t[e+8]]+I[t[e+9]]+"-"+I[t[e+10]]+I[t[e+11]]+I[t[e+12]]+I[t[e+13]]+I[t[e+14]]+I[t[e+15]]}const Ht={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function $t(t,e,n){if(Ht.randomUUID&&!e&&!t)return Ht.randomUUID();t=t||{};const o=t.random||(t.rng||xe)();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 Ee(o)}var Ft=Object.prototype.hasOwnProperty;function k(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--&&k(t[o],e[o]););return o===-1}if(!n||typeof t=="object"){o=0;for(n in t)if(Ft.call(t,n)&&++o&&!Ft.call(e,n)||!(n in e)||!k(t[n],e[n]))return!1;return Object.keys(e).length===o}}return t!==t&&e!==e}function lt(){}function Se(t,e){return t!=t?e==e:t!==e||t&&typeof t=="object"||typeof t=="function"}const $=[];function dt(t,e=lt){let n;const o=new Set;function i(a){if(Se(t,a)&&(t=a,n)){const d=!$.length;for(const u of o)u[1](),$.push(u,t);if(d){for(let u=0;u<$.length;u+=2)$[u][0]($[u+1]);$.length=0}}}function r(a){i(a(t))}function s(a,d=lt){const u=[a,d];return o.add(u),o.size===1&&(n=e(i,r)||lt),a(t),()=>{o.delete(u),o.size===0&&n&&(n(),n=null)}}return{set:i,update:r,subscribe:s}}const Te=t=>{const{subscribe:e,set:n}=dt();let o;return e(i=>o=i),t.observe(({changes:i})=>{if(o){(i.deleted||[]).some(s=>s.id===o)&&n(void 0);const r=(i.updated||[]).find(({oldValue:s})=>s.id===o);r&&n(r.newValue.id)}}),{get current(){return o},subscribe:e,set:n}},ut={selected:[]},Ce=(t,e="EDIT")=>{const{subscribe:n,set:o}=dt(ut);let i=ut;n(c=>i=c);const r=()=>o(ut),s=()=>{var c;return((c=i.selected)==null?void 0:c.length)===0},a=c=>{if(i.selected.length===0)return!1;const b=typeof c=="string"?c:c.id;return i.selected.some(p=>p.id===b)},d=(c,b)=>{const p=t.getAnnotation(c);if(p){const v=Re(p,e);o(v==="EDIT"?{selected:[{id:c,editable:!0}],pointerEvent:b}:v==="SELECT"?{selected:[{id:c}],pointerEvent:b}:{selected:[],pointerEvent:b})}else console.warn("Invalid selection: "+c)},u=(c,b=!0)=>{const p=Array.isArray(c)?c:[c],v=p.map(T=>t.getAnnotation(T)).filter(Boolean);o({selected:v.map(({id:T})=>({id:T,editable:b}))}),v.length!==p.length&&console.warn("Invalid selection",c)},y=c=>{if(i.selected.length===0)return!1;const{selected:b}=i;b.filter(({id:p})=>c.includes(p)).length>0&&o({selected:b.filter(({id:p})=>!c.includes(p))})};return t.observe(({changes:c})=>y((c.deleted||[]).map(b=>b.id))),{clear:r,clickSelect:d,get selected(){return i?[...i.selected]:null},get pointerEvent(){return i?i.pointerEvent:null},isEmpty:s,isSelected:a,setSelected:u,subscribe:n}},Re=(t,e)=>typeof e=="function"?e(t)||"EDIT":e||"EDIT",Le=[];for(let t=0;t<256;++t)Le.push((t+256).toString(16).slice(1));typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto);const Ne=(t,e)=>{const n=new Set(t.bodies.map(o=>o.id));return e.bodies.filter(o=>!n.has(o.id))},Be=(t,e)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},Oe=(t,e)=>e.bodies.map(n=>{const o=t.bodies.find(i=>i.id===n.id);return{newBody:n,oldBody:o&&!k(o,n)?o:void 0}}).filter(({oldBody:n})=>n).map(({oldBody:n,newBody:o})=>({oldBody:n,newBody:o})),Me=(t,e)=>!k(t.target,e.target),zt=(t,e)=>{const n=Ne(t,e),o=Be(t,e),i=Oe(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:Me(t,e)?{oldTarget:t.target,newTarget:e.target}:void 0}};var N=(t=>(t.LOCAL="LOCAL",t.REMOTE="REMOTE",t))(N||{});const Ie=(t,e)=>{var n,o;const{changes:i,origin:r}=e;if(!(!t.options.origin||t.options.origin===r))return!1;if(t.options.ignore){const{ignore:s}=t.options,a=d=>d&&d.length>0;if(!(a(i.created)||a(i.deleted))){const d=(n=i.updated)==null?void 0:n.some(y=>a(y.bodiesCreated)||a(y.bodiesDeleted)||a(y.bodiesUpdated)),u=(o=i.updated)==null?void 0:o.some(y=>y.targetUpdated);if(s==="BODY_ONLY"&&d&&!u||s==="TARGET_ONLY"&&u&&!d)return!1}}if(t.options.annotations){const s=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=>s.has(a))}else return!0},Xe=(t,e)=>{const n=new Set((t.created||[]).map(c=>c.id)),o=new Set((t.updated||[]).map(({newValue:c})=>c.id)),i=new Set((e.created||[]).map(c=>c.id)),r=new Set((e.deleted||[]).map(c=>c.id)),s=new Set((e.updated||[]).map(({oldValue:c})=>c.id)),a=new Set((e.updated||[]).filter(({oldValue:c})=>n.has(c.id)||o.has(c.id)).map(({oldValue:c})=>c.id)),d=[...(t.created||[]).filter(c=>!r.has(c.id)).map(c=>s.has(c.id)?e.updated.find(({oldValue:b})=>b.id===c.id).newValue:c),...e.created||[]],u=[...(t.deleted||[]).filter(c=>!i.has(c.id)),...(e.deleted||[]).filter(c=>!n.has(c.id))],y=[...(t.updated||[]).filter(({newValue:c})=>!r.has(c.id)).map(c=>{const{oldValue:b,newValue:p}=c;if(s.has(p.id)){const v=e.updated.find(T=>T.oldValue.id===p.id).newValue;return zt(b,v)}else return c}),...(e.updated||[]).filter(({oldValue:c})=>!a.has(c.id))];return{created:d,deleted:u,updated:y}},_e=t=>t.id!==void 0,Ue=()=>{const t=new Map,e=new Map,n=[],o=(g,x={})=>n.push({onChange:g,options:x}),i=g=>{const x=n.findIndex(w=>w.onChange==g);x>-1&&n.splice(x,1)},r=(g,x)=>{const w={origin:g,changes:{created:x.created||[],updated:x.updated||[],deleted:x.deleted||[]},state:[...t.values()]};n.forEach(S=>{Ie(S,w)&&S.onChange(w)})},s=(g,x=N.LOCAL)=>{if(t.get(g.id))throw Error(`Cannot add annotation ${g.id} - exists already`);t.set(g.id,g),g.bodies.forEach(w=>e.set(w.id,g.id)),r(x,{created:[g]})},a=(g,x)=>{const w=typeof g=="string"?x:g,S=typeof g=="string"?g:g.id,L=t.get(S);if(L){const O=zt(L,w);return S===w.id?t.set(S,w):(t.delete(S),t.set(w.id,w)),L.bodies.forEach(X=>e.delete(X.id)),w.bodies.forEach(X=>e.set(X.id,w.id)),O}else console.warn(`Cannot update annotation ${S} - does not exist`)},d=(g,x=N.LOCAL,w=N.LOCAL)=>{const S=_e(x)?w:x,L=a(g,x);L&&r(S,{updated:[L]})},u=(g,x=N.LOCAL)=>{const w=g.reduce((S,L)=>{const O=a(L);return O?[...S,O]:S},[]);w.length>0&&r(x,{updated:w})},y=(g,x=N.LOCAL)=>{const w=t.get(g.annotation);if(w){const S={...w,bodies:[...w.bodies,g]};t.set(w.id,S),e.set(g.id,S.id),r(x,{updated:[{oldValue:w,newValue:S,bodiesCreated:[g]}]})}else console.warn(`Attempt to add body to missing annotation: ${g.annotation}`)},c=()=>[...t.values()],b=(g=N.LOCAL)=>{const x=[...t.values()];t.clear(),e.clear(),r(g,{deleted:x})},p=(g,x=!0,w=N.LOCAL)=>{if(x){const S=[...t.values()];t.clear(),e.clear(),g.forEach(L=>{t.set(L.id,L),L.bodies.forEach(O=>e.set(O.id,L.id))}),r(w,{created:g,deleted:S})}else{const S=g.reduce((L,O)=>{const X=t.get(O.id);return X?[...L,X]:L},[]);if(S.length>0)throw Error(`Bulk insert would overwrite the following annotations: ${S.map(L=>L.id).join(", ")}`);g.forEach(L=>{t.set(L.id,L),L.bodies.forEach(O=>e.set(O.id,L.id))}),r(w,{created:g})}},v=g=>{const x=typeof g=="string"?g:g.id,w=t.get(x);if(w)return t.delete(x),w.bodies.forEach(S=>e.delete(S.id)),w;console.warn(`Attempt to delete missing annotation: ${x}`)},T=(g,x=N.LOCAL)=>{const w=v(g);w&&r(x,{deleted:[w]})},E=(g,x=N.LOCAL)=>{const w=g.reduce((S,L)=>{const O=v(L);return O?[...S,O]:S},[]);w.length>0&&r(x,{deleted:w})},l=(g,x=N.LOCAL)=>{const w=t.get(g.annotation);if(w){const S=w.bodies.find(L=>L.id===g.id);if(S){e.delete(S.id);const L={...w,bodies:w.bodies.filter(O=>O.id!==g.id)};t.set(w.id,L),r(x,{updated:[{oldValue:w,newValue:L,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}`)},h=g=>{const x=t.get(g);return x?{...x}:void 0},m=g=>{const x=e.get(g);if(x){const w=h(x).bodies.find(S=>S.id===g);if(w)return w;console.error(`Store integrity error: body ${g} in index, but not in annotation`)}else console.warn(`Attempt to retrieve missing body: ${g}`)},f=(g,x)=>{if(g.annotation!==x.annotation)throw"Annotation integrity violation: annotation ID must be the same when updating bodies";const w=t.get(g.annotation);if(w){const S=w.bodies.find(O=>O.id===g.id),L={...w,bodies:w.bodies.map(O=>O.id===S.id?x:O)};return t.set(w.id,L),S.id!==x.id&&(e.delete(S.id),e.set(x.id,L.id)),{oldValue:w,newValue:L,bodiesUpdated:[{oldBody:S,newBody:x}]}}else console.warn(`Attempt to add body to missing annotation ${g.annotation}`)},A=(g,x,w=N.LOCAL)=>{const S=f(g,x);S&&r(w,{updated:[S]})},C=(g,x=N.LOCAL)=>{const w=g.map(S=>f({id:S.id,annotation:S.annotation},S)).filter(Boolean);r(x,{updated:w})},R=g=>{const x=t.get(g.annotation);if(x){const w={...x,target:{...x.target,...g}};return t.set(x.id,w),{oldValue:x,newValue:w,targetUpdated:{oldTarget:x.target,newTarget:g}}}else console.warn(`Attempt to update target on missing annotation: ${g.annotation}`)};return{addAnnotation:s,addBody:y,all:c,bulkAddAnnotation:p,bulkDeleteAnnotation:E,bulkUpdateAnnotation:u,bulkUpdateBodies:C,bulkUpdateTargets:(g,x=N.LOCAL)=>{const w=g.map(S=>R(S)).filter(Boolean);w.length>0&&r(x,{updated:w})},clear:b,deleteAnnotation:T,deleteBody:l,getAnnotation:h,getBody:m,observe:o,unobserve:i,updateAnnotation:d,updateBody:A,updateTarget:(g,x=N.LOCAL)=>{const w=R(g);w&&r(x,{updated:[w]})}}};let Ye=()=>({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 De=250,Ve=t=>{const e=Ye(),n=[];let o=-1,i=!1,r=0;const s=p=>{if(!i){const{changes:v}=p,T=performance.now();if(T-r>De)n.splice(o+1),n.push(v),o=n.length-1;else{const E=n.length-1;n[E]=Xe(n[E],v)}r=T}i=!1};t.observe(s,{origin:N.LOCAL});const a=p=>p&&p.length>0&&t.bulkDeleteAnnotation(p),d=p=>p&&p.length>0&&t.bulkAddAnnotation(p,!1),u=p=>p&&p.length>0&&t.bulkUpdateAnnotation(p.map(({oldValue:v})=>v)),y=p=>p&&p.length>0&&t.bulkUpdateAnnotation(p.map(({newValue:v})=>v)),c=p=>p&&p.length>0&&t.bulkAddAnnotation(p,!1),b=p=>p&&p.length>0&&t.bulkDeleteAnnotation(p);return{canRedo:()=>n.length-1>o,canUndo:()=>o>-1,destroy:()=>t.unobserve(s),on:(p,v)=>e.on(p,v),redo:()=>{if(n.length-1>o){i=!0;const{created:p,updated:v,deleted:T}=n[o+1];d(p),y(v),b(T),e.emit("redo",n[o+1]),o+=1}},undo:()=>{if(o>-1){i=!0;const{created:p,updated:v,deleted:T}=n[o];a(p),u(v),c(T),e.emit("undo",n[o]),o-=1}}}},ke=()=>{const{subscribe:t,set:e}=dt([]);return{subscribe:t,set:e}},Pe=(t,e,n,o)=>{const{store:i,selection:r,hover:s,viewport:a}=t,d=new Map;let u=[],y,c;const b=(l,h)=>{d.has(l)?d.get(l).push(h):d.set(l,[h])},p=(l,h)=>{const m=d.get(l);m&&m.indexOf(h)>0&&m.splice(m.indexOf(h),1)},v=(l,h,m)=>{d.has(l)&&setTimeout(()=>{d.get(l).forEach(f=>{if(n){const A=Array.isArray(h)?h.map(R=>n.serialize(R)):n.serialize(h),C=m?m instanceof PointerEvent?m:n.serialize(m):void 0;f(A,C)}else f(h,m)})},1)},T=()=>{const{selected:l}=r,h=(l||[]).map(({id:m})=>i.getAnnotation(m));h.forEach(m=>{const f=u.find(A=>A.id===m.id);(!f||!k(f,m))&&v("updateAnnotation",m,f)}),u=u.map(m=>h.find(({id:A})=>A===m.id)||m)};r.subscribe(({selected:l})=>{if(!(u.length===0&&l.length===0)){if(u.length===0&&l.length>0)u=l.map(({id:h})=>i.getAnnotation(h));else if(u.length>0&&l.length===0)u.forEach(h=>{const m=i.getAnnotation(h.id);m&&!k(m,h)&&v("updateAnnotation",m,h)}),u=[];else{const h=new Set(u.map(f=>f.id)),m=new Set(l.map(({id:f})=>f));u.filter(f=>!m.has(f.id)).forEach(f=>{const A=i.getAnnotation(f.id);A&&!k(A,f)&&v("updateAnnotation",A,f)}),u=[...u.filter(f=>m.has(f.id)),...l.filter(({id:f})=>!h.has(f)).map(({id:f})=>i.getAnnotation(f))]}v("selectionChanged",u)}}),s.subscribe(l=>{!y&&l?v("mouseEnterAnnotation",i.getAnnotation(l)):y&&!l?v("mouseLeaveAnnotation",i.getAnnotation(y)):y&&l&&(v("mouseLeaveAnnotation",i.getAnnotation(y)),v("mouseEnterAnnotation",i.getAnnotation(l))),y=l}),a==null||a.subscribe(l=>v("viewportIntersect",l.map(h=>i.getAnnotation(h)))),i.observe(l=>{o&&(c&&clearTimeout(c),c=setTimeout(T,1e3));const{created:h,deleted:m}=l.changes;(h||[]).forEach(f=>v("createAnnotation",f)),(m||[]).forEach(f=>v("deleteAnnotation",f)),(l.changes.updated||[]).filter(f=>[...f.bodiesCreated||[],...f.bodiesDeleted||[],...f.bodiesUpdated||[]].length>0).forEach(({oldValue:f,newValue:A})=>{const C=u.find(R=>R.id===f.id)||f;u=u.map(R=>R.id===f.id?A:R),v("updateAnnotation",A,C)})},{origin:N.LOCAL}),i.observe(l=>{if(u){const h=new Set(u.map(f=>f.id)),m=(l.changes.updated||[]).filter(({newValue:f})=>h.has(f.id)).map(({newValue:f})=>f);m.length>0&&(u=u.map(f=>m.find(C=>C.id===f.id)||f))}},{origin:N.REMOTE});const E=l=>h=>{const{created:m,deleted:f,updated:A}=h;(m||[]).forEach(C=>v("createAnnotation",C)),(f||[]).forEach(C=>v("deleteAnnotation",C)),l?(A||[]).forEach(C=>v("updateAnnotation",C.oldValue,C.newValue)):(A||[]).forEach(C=>v("updateAnnotation",C.newValue,C.oldValue))};return e.on("undo",E(!0)),e.on("redo",E(!1)),{on:b,off:p,emit:v}},He=t=>e=>e.reduce((n,o)=>{const{parsed:i,error:r}=t.parse(o);return r?{parsed:n.parsed,failed:[...n.failed,o]}:i?{parsed:[...n.parsed,i],failed:n.failed}:{...n}},{parsed:[],failed:[]}),$e=(t,e,n)=>{const{store:o,selection:i}=t,r=E=>{if(n){const{parsed:l,error:h}=n.parse(E);l?o.addAnnotation(l,N.REMOTE):console.error(h)}else o.addAnnotation(E,N.REMOTE)},s=()=>i.clear(),a=()=>o.clear(),d=E=>{const l=o.getAnnotation(E);return n&&l?n.serialize(l):l},u=()=>n?o.all().map(n.serialize):o.all(),y=()=>{var E;const l=(((E=i.selected)==null?void 0:E.map(h=>h.id))||[]).map(h=>o.getAnnotation(h)).filter(Boolean);return n?l.map(n.serialize):l},c=E=>fetch(E).then(l=>l.json()).then(l=>(p(l),l)),b=E=>{if(typeof E=="string"){const l=o.getAnnotation(E);if(o.deleteAnnotation(E),l)return n?n.serialize(l):l}else{const l=n?n.parse(E).parsed:E;if(l)return o.deleteAnnotation(l),E}},p=E=>{if(n){const{parsed:l,failed:h}=He(n)(E);h.length>0&&console.warn(`Discarded ${h.length} invalid annotations`,h),o.bulkAddAnnotation(l,!0,N.REMOTE)}else o.bulkAddAnnotation(E,!0,N.REMOTE)},v=E=>{E?i.setSelected(E):i.clear()},T=E=>{if(n){const l=n.parse(E).parsed,h=n.serialize(o.getAnnotation(l.id));return o.updateAnnotation(l),h}else{const l=o.getAnnotation(E.id);return o.updateAnnotation(E),l}};return{addAnnotation:r,cancelSelected:s,canRedo:e.canRedo,canUndo:e.canUndo,clearAnnotations:a,getAnnotationById:d,getAnnotations:u,getSelected:y,loadAnnotations:c,redo:e.redo,removeAnnotation:b,setAnnotations:p,setSelected:v,undo:e.undo,updateAnnotation:T}};let Fe=t=>crypto.getRandomValues(new Uint8Array(t)),ze=(t,e,n)=>{let o=(2<<Math.log(t.length-1)/Math.LN2)-1,i=-~(1.6*o*e/t.length);return(r=e)=>{let s="";for(;;){let a=n(i),d=i;for(;d--;)if(s+=t[a[d]&o]||"",s.length===r)return s}}},We=(t,e=21)=>ze(t,e,Fe),qe=(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 je=()=>({isGuest:!0,id:We("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",20)()}),Ke=t=>{const e=JSON.stringify(t);let n=0;for(let o=0,i=e.length;o<i;o++){let r=e.charCodeAt(o);n=(n<<5)-n+r,n|=0}return`${n}`},Wt=t=>t?typeof t=="object"?{...t}:t:void 0,Qe=(t,e)=>(Array.isArray(t)?t:[t]).map(n=>{const{id:o,type:i,purpose:r,value:s,created:a,creator:d,...u}=n;return{id:o||`temp-${Ke(n)}`,annotation:e,type:i,purpose:r,value:s,created:a?new Date(a):void 0,creator:Wt(d),...u}}),Je=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()}});qe();const Ge=(t,e)=>({parse:n=>qt(n),serialize:n=>jt(n,t,e)}),Ze=t=>t.quote!==void 0&&t.start!==void 0&&t.end!==void 0,tn=(t,e)=>{const{id:n,creator:o,created:i,modified:r,target:s}=t,a=Array.isArray(s)?s:[s],d={creator:Wt(o),created:i?new Date(i):void 0,updated:r?new Date(r):void 0,annotation:n,selector:[]};for(const u of a){const c=(Array.isArray(u.selector)?u.selector:[u.selector]).reduce((b,p)=>{switch(p.type){case"TextQuoteSelector":b.quote=p.exact;break;case"TextPositionSelector":b.start=p.start,b.end=p.end;break}return b},{});if(Ze(c))d.selector.push(c);else{const b=[c.start?void 0:"TextPositionSelector",c.quote?void 0:"TextQuoteSelector"].filter(Boolean);return{error:Error(`Missing selector types: ${b.join(" and ")} for annotation: ${t.id}`)}}}return{parsed:d}},qt=(t,e)=>{const n=t.id||$t(),{creator:o,created:i,modified:r,body:s,...a}=t,d=Qe(s,n),u=tn(t);return"error"in u?{error:u.error}:{parsed:{...a,id:n,bodies:d,target:u.parsed}}},jt=(t,e,n)=>{const{bodies:o,target:i,...r}=t,{selector:s,creator:a,created:d,updated:u,...y}=i,c=s.map(b=>{const{quote:p,start:v,end:T,range:E}=b,{prefix:l,suffix:h}=St(E,n);return{...y,source:e,selector:[{type:"TextQuoteSelector",exact:p,prefix:l,suffix:h},{type:"TextPositionSelector",start:v,end:T}]}});return{...r,"@context":"http://www.w3.org/ns/anno.jsonld",id:t.id,type:"Annotation",body:Je(t.bodies),creator:a,created:d==null?void 0:d.toISOString(),modified:u==null?void 0:u.toISOString(),target:c}};function en(t,e,n,o,i){Kt(t,e,n||0,o||t.length-1,i||nn)}function Kt(t,e,n,o,i){for(;o>n;){if(o-n>600){var r=o-n+1,s=e-n+1,a=Math.log(r),d=.5*Math.exp(2*a/3),u=.5*Math.sqrt(a*d*(r-d)/r)*(s-r/2<0?-1:1),y=Math.max(n,Math.floor(e-s*d/r+u)),c=Math.min(o,Math.floor(e+(r-s)*d/r+u));Kt(t,e,y,c,i)}var b=t[e],p=n,v=o;for(j(t,n,e),i(t[o],b)>0&&j(t,n,o);p<v;){for(j(t,p,v),p++,v--;i(t[p],b)<0;)p++;for(;i(t[v],b)>0;)v--}i(t[n],b)===0?j(t,n,v):(v++,j(t,v,o)),v<=e&&(n=v+1),e<=v&&(o=v-1)}}function j(t,e,n){var o=t[e];t[e]=t[n],t[n]=o}function nn(t,e){return t<e?-1:t>e?1:0}class on{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(!it(e,n))return o;const i=this.toBBox,r=[];for(;n;){for(let s=0;s<n.children.length;s++){const a=n.children[s],d=n.leaf?i(a):a;it(e,d)&&(n.leaf?o.push(a):ft(e,d)?this._all(a,o):r.push(a))}n=r.pop()}return o}collides(e){let n=this.data;if(!it(e,n))return!1;const o=[];for(;n;){for(let i=0;i<n.children.length;i++){const r=n.children[i],s=n.leaf?this.toBBox(r):r;if(it(e,s)){if(n.leaf||ft(e,s))return!0;o.push(r)}}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=z([]),this}remove(e,n){if(!e)return this;let o=this.data;const i=this.toBBox(e),r=[],s=[];let a,d,u;for(;o||r.length;){if(o||(o=r.pop(),d=r[r.length-1],a=s.pop(),u=!0),o.leaf){const y=rn(e,o.children,n);if(y!==-1)return o.children.splice(y,1),r.push(o),this._condense(r),this}!u&&!o.leaf&&ft(o,i)?(r.push(o),s.push(a),a=0,d=o,o=o.children[0]):d?(a++,o=d.children[a],u=!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 r=o-n+1;let s=this._maxEntries,a;if(r<=s)return a=z(e.slice(n,o+1)),F(a,this.toBBox),a;i||(i=Math.ceil(Math.log(r)/Math.log(s)),s=Math.ceil(r/Math.pow(s,i-1))),a=z([]),a.leaf=!1,a.height=i;const d=Math.ceil(r/s),u=d*Math.ceil(Math.sqrt(s));Qt(e,n,o,u,this.compareMinX);for(let y=n;y<=o;y+=u){const c=Math.min(y+u-1,o);Qt(e,y,c,d,this.compareMinY);for(let b=y;b<=c;b+=d){const p=Math.min(b+d-1,c);a.children.push(this._build(e,b,p,i-1))}}return F(a,this.toBBox),a}_chooseSubtree(e,n,o,i){for(;i.push(n),!(n.leaf||i.length-1===o);){let r=1/0,s=1/0,a;for(let d=0;d<n.children.length;d++){const u=n.children[d],y=ht(u),c=cn(e,u)-y;c<s?(s=c,r=y<r?y:r,a=u):c===s&&y<r&&(r=y,a=u)}n=a||n.children[0]}return n}_insert(e,n,o){const i=o?e:this.toBBox(e),r=[],s=this._chooseSubtree(i,this.data,n,r);for(s.children.push(e),Q(s,i);n>=0&&r[n].children.length>this._maxEntries;)this._split(r,n),n--;this._adjustParentBBoxes(i,r,n)}_split(e,n){const o=e[n],i=o.children.length,r=this._minEntries;this._chooseSplitAxis(o,r,i);const s=this._chooseSplitIndex(o,r,i),a=z(o.children.splice(s,o.children.length-s));a.height=o.height,a.leaf=o.leaf,F(o,this.toBBox),F(a,this.toBBox),n?e[n-1].children.push(a):this._splitRoot(o,a)}_splitRoot(e,n){this.data=z([e,n]),this.data.height=e.height+1,this.data.leaf=!1,F(this.data,this.toBBox)}_chooseSplitIndex(e,n,o){let i,r=1/0,s=1/0;for(let a=n;a<=o-n;a++){const d=K(e,0,a,this.toBBox),u=K(e,a,o,this.toBBox),y=ln(d,u),c=ht(d)+ht(u);y<r?(r=y,i=a,s=c<s?c:s):y===r&&c<s&&(s=c,i=a)}return i||o-n}_chooseSplitAxis(e,n,o){const i=e.leaf?this.compareMinX:sn,r=e.leaf?this.compareMinY:an,s=this._allDistMargin(e,n,o,i),a=this._allDistMargin(e,n,o,r);s<a&&e.children.sort(i)}_allDistMargin(e,n,o,i){e.children.sort(i);const r=this.toBBox,s=K(e,0,n,r),a=K(e,o-n,o,r);let d=ot(s)+ot(a);for(let u=n;u<o-n;u++){const y=e.children[u];Q(s,e.leaf?r(y):y),d+=ot(s)}for(let u=o-n-1;u>=n;u--){const y=e.children[u];Q(a,e.leaf?r(y):y),d+=ot(a)}return d}_adjustParentBBoxes(e,n,o){for(let i=o;i>=0;i--)Q(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():F(e[n],this.toBBox)}}function rn(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 F(t,e){K(t,0,t.children.length,e,t)}function K(t,e,n,o,i){i||(i=z(null)),i.minX=1/0,i.minY=1/0,i.maxX=-1/0,i.maxY=-1/0;for(let r=e;r<n;r++){const s=t.children[r];Q(i,t.leaf?o(s):s)}return i}function Q(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 sn(t,e){return t.minX-e.minX}function an(t,e){return t.minY-e.minY}function ht(t){return(t.maxX-t.minX)*(t.maxY-t.minY)}function ot(t){return t.maxX-t.minX+(t.maxY-t.minY)}function cn(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 ln(t,e){const n=Math.max(t.minX,e.minX),o=Math.max(t.minY,e.minY),i=Math.min(t.maxX,e.maxX),r=Math.min(t.maxY,e.maxY);return Math.max(0,i-n)*Math.max(0,r-o)}function ft(t,e){return t.minX<=e.minX&&t.minY<=e.minY&&e.maxX<=t.maxX&&e.maxY<=t.maxY}function it(t,e){return e.minX<=t.maxX&&e.minY<=t.maxY&&e.maxX>=t.minX&&e.maxY>=t.minY}function z(t){return{children:t,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function Qt(t,e,n,o,i){const r=[e,n];for(;r.length;){if(n=r.pop(),e=r.pop(),n-e<=o)continue;const s=e+Math.ceil((n-e)/o/2)*o;en(t,s,e,n,i),r.push(e,s,s,n)}}const dn=(t,e)=>{const n=new on,o=new Map,i=l=>{const h=e.getBoundingClientRect(),m=l.selector.flatMap(A=>{const R=A.range instanceof Range&&!A.range.collapsed&&A.range.startContainer.nodeType===Node.TEXT_NODE&&A.range.endContainer.nodeType===Node.TEXT_NODE?A.range:rt(A,e).range;return Array.from(R.getClientRects())}),f=Tt(m);return f.map(A=>{const{x:C,y:R,width:g,height:x}=A;return{minX:C-h.x,minY:R-h.y,maxX:C-h.x+g,maxY:R-h.y+x,annotation:{id:l.annotation,rects:f}}})},r=()=>[...o.values()],s=()=>{n.clear(),o.clear()},a=l=>{const h=i(l);h.forEach(m=>n.insert(m)),o.set(l.annotation,h)},d=l=>{const h=o.get(l.annotation);h&&(h.forEach(m=>n.remove(m)),o.delete(l.annotation))},u=l=>{d(l),a(l)},y=(l,h=!0)=>{h&&s();const m=l.map(A=>({target:A,rects:i(A)}));m.forEach(({target:A,rects:C})=>o.set(A.annotation,C));const f=m.reduce((A,{rects:C})=>[...A,...C],[]);n.load(f)},c=(l,h)=>{const m=n.search({minX:l,minY:h,maxX:l,maxY:h}),f=A=>A.annotation.rects.reduce((C,R)=>C+R.width*R.height,0);if(m.length>0)return m.sort((A,C)=>f(A)-f(C)),m[0].annotation.id},b=l=>{const h=p(l);if(h.length===0)return;let m=h[0].left,f=h[0].top,A=h[0].right,C=h[0].bottom;for(let R=1;R<h.length;R++){const g=h[R];m=Math.min(m,g.left),f=Math.min(f,g.top),A=Math.max(A,g.right),C=Math.max(C,g.bottom)}return new DOMRect(m,f,A-m,C-f)},p=l=>{const h=o.get(l);return h?h[0].annotation.rects:[]};return{all:r,clear:s,getAt:c,getBoundsForAnnotation:b,getDOMRectsForAnnotation:p,getIntersectingRects:(l,h,m,f)=>n.search({minX:l,minY:h,maxX:m,maxY:f}),insert:a,recalculate:()=>y(t.all().map(l=>l.target),!0),remove:d,set:y,size:()=>n.all().length,update:u}},Jt=(t,e)=>{const n=Ue(),o=dn(n,t),i=Ce(n,e),r=Te(n),s=ke(),a=(l,h=N.LOCAL)=>{const m=Z(l,t),f=D(m.target.selector);return f&&n.addAnnotation(m,h),f},d=(l,h=!0,m=N.LOCAL)=>{const f=l.map(C=>Z(C,t)),A=f.filter(C=>!D(C.target.selector));return A.length>0?(console.warn("Could not revive all targets for these annotations:",A),n.bulkAddAnnotation(f,h,m),A):(n.bulkAddAnnotation(f,h,m),[])},u=(l,h=N.LOCAL)=>{const m=l.map(A=>Z(A,t)),f=m.filter(A=>!D(A.target.selector));return f.length>0&&console.warn("Could not revive all targets for these annotations:",f),m.forEach(A=>{n.getAnnotation(A.id)?n.updateAnnotation(A,h):n.addAnnotation(A,h)}),f},y=(l,h=N.LOCAL)=>{const m=W(l,t);n.updateTarget(m,h)},c=(l,h=N.LOCAL)=>{const m=l.map(f=>W(f,t));n.bulkUpdateTargets(m,h)},b=(l,h)=>{const m=o.getAt(l,h);return m?n.getAnnotation(m):void 0},p=(l,h,m,f)=>{const A=o.getIntersectingRects(l,h,m,f);return Array.from(new Set(A.map(R=>R.annotation.id))).map(R=>n.getAnnotation(R)).filter(Boolean)},v=(l,h,m,f=5)=>{const A=o.getDOMRectsForAnnotation(l);if(A.length!==0){if(h&&m){const C=A.find(({top:R,right:g,bottom:x,left:w})=>h>=w-f&&h<=g+f&&m>=R-f&&m<=x+f);if(C)return C}return o.getBoundsForAnnotation(l)}},T=(l,h,m,f)=>{const C=o.getIntersectingRects(l,h,m,f).reduce((R,g)=>((R[g.annotation.id]=R[g.annotation.id]||[]).push(g),R),{});return Object.entries(C).map(([R,g])=>({annotation:n.getAnnotation(R),rects:g.map(({minX:x,minY:w,maxX:S,maxY:L})=>({x,y:w,width:S-x,height:L-w}))}))},E=()=>o.recalculate();return n.observe(({changes:l})=>{const h=(l.created||[]).filter(A=>D(A.target.selector)),m=(l.deleted||[]).filter(A=>D(A.target.selector)),f=(l.updated||[]).filter(A=>D(A.newValue.target.selector));h.length>0&&o.set(h.map(A=>A.target),!1),(m==null?void 0:m.length)>0&&m.forEach(A=>o.remove(A.target)),(f==null?void 0:f.length)>0&&f.forEach(({newValue:A})=>o.update(A.target))}),{store:{...n,addAnnotation:a,bulkAddAnnotation:d,bulkUpdateTargets:c,bulkUpsertAnnotations:u,getAnnotationBounds:v,getAt:b,getIntersecting:p,getIntersectingRects:T,recalculatePositions:E,updateTarget:y},selection:i,hover:r,viewport:s}},un=()=>{const t=document.createElement("canvas");t.width=2*window.innerWidth,t.height=2*window.innerHeight,t.className="r6o-highlight-layer presence";const e=t.getContext("2d");return e.scale(2,2),e.translate(.5,.5),t},hn=(t,e,n={})=>{const o=un(),i=o.getContext("2d");t.appendChild(o);const r=new Map,s=c=>Array.from(r.entries()).filter(([b,p])=>p.presenceKey===c.presenceKey).map(([b,p])=>b);return e.on("selectionChange",(c,b)=>{s(c).forEach(v=>r.delete(v)),b&&b.forEach(v=>r.set(v,c))}),{clear:()=>{const{width:c,height:b}=o;i.clearRect(-.5,-.5,c+1,b+1)},destroy:()=>{o.remove()},paint:(c,b,p)=>{n.font&&(i.font=n.font);const v=r.get(c.annotation.id);if(v){const{height:T}=c.rects[0],E=c.rects[0].x+b.left,l=c.rects[0].y+b.top;i.fillStyle=v.appearance.color,i.fillRect(E-2,l-2.5,2,T+5);const h=i.measureText(v.appearance.label),m=h.width+6,f=h.actualBoundingBoxAscent+h.actualBoundingBoxDescent+8,A=h.fontBoundingBoxAscent?8:6.5;return i.fillRect(E-2,l-2.5-f,m,f),i.fillStyle="#fff",i.fillText(v.appearance.label,E+1,l-A),{fill:v.appearance.color,fillOpacity:p?.45:.18}}},reset:()=>{o.width=2*window.innerWidth,o.height=2*window.innerHeight;const c=o.getContext("2d");c.scale(2,2),c.translate(.5,.5)}}},Gt=t=>{if(t===null)return document.scrollingElement;const{overflowY:e}=window.getComputedStyle(t);return e!=="visible"&&e!=="hidden"&&t.scrollHeight>t.clientHeight?t:Gt(t.parentElement)},fn=(t,e)=>n=>{const o=r=>{const s=i.getBoundingClientRect(),a=i.clientHeight,d=i.clientWidth,u=r.selector[0].range.getBoundingClientRect(),{width:y,height:c}=e.getAnnotationBounds(n.id),b=u.top-s.top,p=u.left-s.left,v=i.parentElement?i.scrollTop:0,T=i.parentElement?i.scrollLeft:0,E=b+v-(a-c)/2,l=p+T-(d-y)/2;i.scroll({top:E,left:l,behavior:"smooth"})},i=Gt(t);if(i){const r=e.getAnnotation(n.id),{range:s}=r.target.selector[0];if(s&&!s.collapsed)return o(r.target),!0;{const a=W(r.target,t),{range:d}=a.selector[0];if(d&&!d.collapsed)return o(a),!0}}return!1},gn=(t,e,n)=>{const{store:o,selection:i}=e;let r,s;const a=T=>r=T;let d=!1,u;const y=T=>{var l;if(!d)return;!((l=T.target.parentElement)!=null&&l.closest(".not-annotatable"))?s={annotation:$t(),selector:[],creator:r,created:new Date}:s=void 0};t.addEventListener("selectstart",y);const c=J(T=>{const E=document.getSelection();if(T.timeStamp-((u==null?void 0:u.timeStamp)||T.timeStamp)<1e3&&!s&&y(u),E.isCollapsed||!d||!s)return;const l=E.getRangeAt(0),h=Rt(l.cloneRange()),m=wt(h);(m.length!==s.selector.length||m.some((A,C)=>{var R;return A.toString()!==((R=s.selector[C])==null?void 0:R.quote)}))&&(s={...s,selector:m.map(A=>Ct(A,t,n))},o.getAnnotation(s.annotation)?o.updateTarget(s,N.LOCAL):(o.addAnnotation({id:s.annotation,bodies:[],target:s}),i.clickSelect(s.annotation,u)))});document.addEventListener("selectionchange",c);const b=T=>{const{target:E,timeStamp:l,offsetX:h,offsetY:m,type:f}=T;u={...T,target:E,timeStamp:l,offsetX:h,offsetY:m,type:f},d=T.button===0};t.addEventListener("pointerdown",b);const p=T=>{var m;if(!!((m=T.target.parentElement)!=null&&m.closest(".not-annotatable"))||!d)return;const l=()=>{const{x:f,y:A}=t.getBoundingClientRect(),C=o.getAt(T.clientX-f,T.clientY-A);if(C){const{selected:R}=i;(R.length!==1||R[0].id!==C.id)&&i.clickSelect(C.id,T)}else i.isEmpty()||i.clear()},h=T.timeStamp-u.timeStamp;document.getSelection().isCollapsed&&h<300?(s=void 0,l()):s&&i.clickSelect(s.annotation,T)};return document.addEventListener("pointerup",p),{destroy:()=>{t.removeEventListener("selectstart",y),document.removeEventListener("selectionchange",c),t.removeEventListener("pointerdown",b),document.removeEventListener("pointerup",p)},setUser:a}},pn=(t,e={})=>{t.addEventListener("click",f=>!f.target.closest("a")&&f.preventDefault());const n=Jt(t,e.pointerAction),{selection:o,viewport:i}=n,r=n.store,s=Ve(r),a=Pe(n,s,e.adapter);let d=je();const u=e.experimentalCSSRenderer&&!!CSS.highlights;u&&console.log("Using experimental CSS Custom Highlight API renderer");const y=u?Pt(t,n,i):Nt(t,n,i);e.style&&y.setDrawingStyle(e.style);const c=gn(t,n,e.offsetReferenceSelector);return c.setUser(d),{...$e(n,s,e.adapter),destroy:()=>{y.destroy(),c.destroy(),s.destroy()},element:t,getUser:()=>d,setFilter:f=>y.setFilter(f),setStyle:f=>y.setDrawingStyle(f),setUser:f=>{d=f,c.setUser(f)},setSelected:f=>{f?o.setSelected(f):o.clear()},setPresenceProvider:f=>{f&&(y.setPainter(hn(t,f,e.presence)),f.on("selectionChange",()=>y.refresh()))},on:a.on,off:a.off,scrollIntoView:fn(t,r),state:n}};B.NOT_ANNTOTATABLE_SELECTOR=H,B.Origin=N,B.W3CTextFormat=Ge,B.createCSSHighlightRenderer=Pt,B.createCanvasHighlightRenderer=Nt,B.createTextAnnotator=pn,B.createTextAnnotatorState=Jt,B.debounce=J,B.getAnnotatableFragment=ie,B.getClientRectsPonyfill=ae,B.getQuoteContext=St,B.getRangeAnnotatableContents=G,B.isRevived=D,B.mergeClientRects=Tt,B.parseW3CTextAnnotation=qt,B.rangeToSelector=Ct,B.reviveAnnotation=Z,B.reviveSelector=rt,B.reviveTarget=W,B.serializeW3CTextAnnotation=jt,B.splitAnnotatableRanges=wt,B.trimRange=Rt,Object.defineProperty(B,Symbol.toStringTag,{value:"Module"})});
1
+ (function(N,P){typeof exports=="object"&&typeof module<"u"?P(exports):typeof define=="function"&&define.amd?define(["exports"],P):(N=typeof globalThis<"u"?globalThis:N||self,P(N.RecogitoJS={}))})(this,function(N){"use strict";const P=t=>{const{top:e,left:n}=t.getBoundingClientRect(),{innerWidth:o,innerHeight:i}=window,r=-n,s=-e,a=o-n,d=i-e;return{top:e,left:n,minX:r,minY:s,maxX:a,maxY:d}},yt=t=>{let e=new Set;return o=>{const i=o.map(r=>r.id);(e.size!==i.length||i.some(r=>!e.has(r)))&&t.set(i),e=new Set(i)}},J=(t,e=10)=>{let n;return(...o)=>{clearTimeout(n),n=setTimeout(()=>t.apply(void 0,o),e)}},At="not-annotatable",H=`.${At}`,ne=t=>{var n;const e=t.commonAncestorContainer;return e instanceof HTMLElement?!e.closest(H):!((n=e.parentElement)!=null&&n.closest(H))},oe=function*(t){const e=document.createNodeIterator(t.commonAncestorContainer,NodeFilter.SHOW_ELEMENT,o=>o instanceof HTMLElement&&o.classList.contains(At)&&t.intersectsNode(o)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP);let n;for(;n=e.nextNode();)n instanceof HTMLElement&&(yield n)},wt=t=>{if(!ne(t))return[];const e=[];let n=null;for(const o of oe(t)){let i;n?(i=document.createRange(),i.setStartAfter(n),i.setEndBefore(o)):(i=t.cloneRange(),i.setEndBefore(o)),i.collapsed||e.push(i),n=o}if(n){const o=t.cloneRange();o.setStartAfter(n),o.collapsed||e.push(o)}return e.length>0?e:[t]},G=t=>{const e=t.cloneContents();return e.querySelectorAll(H).forEach(n=>n.remove()),e},ie=t=>{const e=t.cloneContents();return e.querySelectorAll(H).forEach(n=>n.remove()),e},xt=(t,e)=>{const n=document.createNodeIterator(e);let o=0,i=n.nextNode();for(;i!==null;){if(i===t)return o;o+=1,i=n.nextNode()}},Et=(t,e)=>{const n=document.createNodeIterator(e);let o=null;for(let i=0;i<t+1;i++)o=n.nextNode();return o},re=t=>{const{commonAncestorContainer:e,startContainer:n,startOffset:o,endContainer:i,endOffset:r}=t,s=Array.from(e.childNodes).map(c=>{const b=c.cloneNode(!0);return c.nodeName==="CANVAS"?c:b}),a=xt(n,e),d=xt(i,e),u=()=>{const c=e;c.replaceChildren(...s);const b=Et(a,c),p=Et(d,c);return t.setStart(b,o),t.setEnd(p,r),t},y=c=>{const b=document.createElement("SPAN");return c.surroundContents(b),b};if(n===i)throw"Not implemented";{const c=document.createRange();c.selectNodeContents(n),c.setStart(n,o);const b=y(c),p=document.createRange();p.selectNode(i),p.setEnd(i,r);const v=y(p),E=se(t).reverse().map(l=>{var m;const h=document.createElement("SPAN");return(m=l.parentNode)==null||m.insertBefore(h,l),h.appendChild(l),h});return{unwrap:u,nodes:[b,...E,v]}}},se=t=>{const{commonAncestorContainer:e,startContainer:n,endContainer:o}=t,i=document.createNodeIterator(e,NodeFilter.SHOW_TEXT);let r=i.nextNode(),s=!1;const a=[];for(;r!=null;)r===o&&(s=!1),s&&a.push(r),r===n&&(s=!0),r=i.nextNode();return a},ae=t=>{const{startContainer:e,endContainer:n}=t;if(e===n)return Array.from(t.getClientRects());{const{unwrap:o,nodes:i}=re(t),r=i.reduce((s,a)=>[...s,...a.getClientRects()],[]);return o(),r}},St=(t,e,n=10,o)=>{const i=o?t.startContainer.parentElement.closest(o):e,r=document.createRange();r.setStart(i,0),r.setEnd(t.startContainer,t.startOffset);const s=G(r).textContent,a=document.createRange();a.setStart(t.endContainer,t.endOffset),i===document.body?a.setEnd(i,i.childNodes.length):a.setEndAfter(i);const d=G(a).textContent;return{prefix:s.substring(s.length-n),suffix:d.substring(0,n)}},D=t=>t.every(e=>e.range instanceof Range&&!e.range.collapsed),ce=(t,e)=>{const n=r=>Math.round(r*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),r=Math.max(t.bottom,e.bottom);return new DOMRect(n,i,o-n,r-i)},Ct=t=>t.reduce((e,n)=>{if(n.width===0||n.height===0)return e;let o=[...e],i=!1;for(const r of e){const s=ce(n,r);if(s==="inline-adjacent"){o=o.map(a=>a===r?le(n,r):a),i=!0;break}else if(s==="inline-contains"){o=o.map(a=>a===r?n:a),i=!0;break}else if(s==="inline-is-contained"){i=!0;break}else if(s==="block-contains"||s==="block-is-contained"){n.width<r.width&&(o=o.map(a=>a===r?n:a)),i=!0;break}}return i?o:[...o,n]},[]),Tt=(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 r=G(o).textContent,s=t.toString(),a=r.length||0,d=a+s.length;return n?{quote:s,start:a,end:d,range:t,offsetReference:i}:{quote:s,start:a,end:d,range:t}},rt=(t,e)=>{var y,c;const{start:n,end:o}=t,i=t.offsetReference||e,r=document.createNodeIterator(e,NodeFilter.SHOW_TEXT,b=>{var p;return(p=b.parentElement)!=null&&p.closest(H)?NodeFilter.FILTER_SKIP:NodeFilter.FILTER_ACCEPT});let s=0;const a=document.createRange();let d=r.nextNode();d===null&&console.error("Could not revive annotation target. Content missing.");let u=!i;for(;d!==null;){if(u||(u=i==null?void 0:i.contains(d)),u){const b=((y=d.textContent)==null?void 0:y.length)||0;if(s+b>n){a.setStart(d,n-s);break}s+=b}d=r.nextNode()}for(;d!==null;){const b=((c=d.textContent)==null?void 0:c.length)||0;if(s+b>=o){a.setEnd(d,o-s);break}s+=b,d=r.nextNode()}return{...t,range:a}},W=(t,e)=>D(t.selector)?t:{...t,selector:t.selector.map(n=>n.range instanceof Range&&!n.range.collapsed?n:rt(n,e))},Z=(t,e)=>D(t.target.selector)?t:{...t,target:W(t.target,e)},Rt=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 r=e.nextSibling||e.parentNode,s=(r==null?void 0:r.nodeType)===Node.TEXT_NODE?r:Array.from(r.childNodes).filter(a=>a.nodeType===Node.TEXT_NODE).shift();t.setEnd(s,0)}if(n.nodeType!==Node.TEXT_NODE){const r=n.previousSibling||n.parentNode,s=(r==null?void 0:r.nodeType)===Node.TEXT_NODE?r:Array.from(r.childNodes).filter(a=>a.nodeType===Node.TEXT_NODE).pop();t.setEnd(s,((i=s==null?void 0:s.textContent)==null?void 0:i.length)||0)}return t},tt={fill:"rgb(0, 128, 255)",fillOpacity:.18},Bt={fill:"rgb(0, 128, 255)",fillOpacity:.45},de=()=>{const t=document.createElement("canvas");return t.width=window.innerWidth,t.height=window.innerHeight,t.className="r6o-highlight-layer bg",t},ue=(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)}},Lt=(t,e,n)=>{const{store:o,selection:i,hover:r}=e;let s,a,d;const u=yt(n);t.classList.add("r6o-annotatable");const y=de(),c=y.getContext("2d");t.insertBefore(y,t.firstChild);const b=g=>{const{x,y:w}=t.getBoundingClientRect(),S=o.getAt(g.clientX-x,g.clientY-w);S&&(!a||a(S))?r.current!==S.id&&(t.classList.add("hovered"),r.set(S.id)):r.current&&(t.classList.remove("hovered"),r.set(null))};t.addEventListener("pointermove",b);const p=()=>requestAnimationFrame(()=>{const g=P(t),{top:x,left:w,minX:S,minY:B,maxX:O,maxY:X}=g,Zt=a?o.getIntersectingRects(S,B,O,X).filter(({annotation:Y})=>a(Y)):o.getIntersectingRects(S,B,O,X),{width:mn,height:bn}=y,vn=new Set(i.selected.map(({id:Y})=>Y));c.clearRect(-.5,-.5,mn+1,bn+1),d&&d.clear(),Zt.forEach(Y=>{const gt=vn.has(Y.annotation.id),te=s?typeof s=="function"?s(Y.annotation,gt):s:gt?Bt:tt,ee=d&&d.paint(Y,g,gt)||te,yn=Y.rects.map(({x:pt,y:mt,width:bt,height:vt})=>({x:pt+w,y:mt+x,width:bt,height:vt}));c.fillStyle=ee.fill,c.globalAlpha=ee.fillOpacity||1,yn.forEach(({x:pt,y:mt,width:bt,height:vt})=>c.fillRect(pt,mt-2.5,bt,vt+5))}),setTimeout(()=>u(Zt.map(({annotation:Y})=>Y)),1)}),v=g=>{s=g,p()},C=g=>{a=g,p()},E=()=>p();o.observe(E);const l=i.subscribe(()=>p()),h=()=>p();document.addEventListener("scroll",h,{capture:!0,passive:!0});const m=J(()=>{ue(y),o.recalculatePositions(),d&&d.reset(),p()});window.addEventListener("resize",m);const f=new ResizeObserver(m);f.observe(t);const A={attributes:!0,childList:!0,subtree:!0},R=new MutationObserver(p);return R.observe(document.body,A),{destroy:()=>{t.removeEventListener("pointermove",b),t.removeChild(y),o.unobserve(E),l(),document.removeEventListener("scroll",h),window.removeEventListener("resize",m),f.disconnect(),R.disconnect()},refresh:p,setDrawingStyle:v,setFilter:C,setPainter:g=>d=g}};var he={grad:.9,turn:360,rad:360/(2*Math.PI)},V=function(t){return typeof t=="string"?t.length>0:typeof t=="number"},M=function(t,e,n){return e===void 0&&(e=0),n===void 0&&(n=Math.pow(10,e)),Math.round(n*t)/n+0},_=function(t,e,n){return e===void 0&&(e=0),n===void 0&&(n=1),t>n?n:t>e?t:e},Nt=function(t){return(t=isFinite(t)?t%360:0)>0?t:t+360},Ot=function(t){return{r:_(t.r,0,255),g:_(t.g,0,255),b:_(t.b,0,255),a:_(t.a)}},st=function(t){return{r:M(t.r),g:M(t.g),b:M(t.b),a:M(t.a,3)}},fe=/^#([0-9a-f]{3,8})$/i,et=function(t){var e=t.toString(16);return e.length<2?"0"+e:e},Mt=function(t){var e=t.r,n=t.g,o=t.b,i=t.a,r=Math.max(e,n,o),s=r-Math.min(e,n,o),a=s?r===e?(n-o)/s:r===n?2+(o-e)/s:4+(e-n)/s:0;return{h:60*(a<0?a+6:a),s:r?s/r*100:0,v:r/255*100,a:i}},It=function(t){var e=t.h,n=t.s,o=t.v,i=t.a;e=e/360*6,n/=100,o/=100;var r=Math.floor(e),s=o*(1-n),a=o*(1-(e-r)*n),d=o*(1-(1-e+r)*n),u=r%6;return{r:255*[o,a,s,s,d,o][u],g:255*[d,o,o,a,s,s][u],b:255*[s,s,d,o,o,a][u],a:i}},Xt=function(t){return{h:Nt(t.h),s:_(t.s,0,100),l:_(t.l,0,100),a:_(t.a)}},_t=function(t){return{h:M(t.h),s:M(t.s),l:M(t.l),a:M(t.a,3)}},Ut=function(t){return It((n=(e=t).s,{h:e.h,s:(n*=((o=e.l)<50?o:100-o)/100)>0?2*n/(o+n)*100:0,v:o+n,a:e.a}));var e,n,o},q=function(t){return{h:(e=Mt(t)).h,s:(i=(200-(n=e.s))*(o=e.v)/100)>0&&i<200?n*o/100/(i<=100?i:200-i)*100:0,l:i/2,a:e.a};var e,n,o,i},ge=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*,\s*([+-]?\d*\.?\d+)%\s*,\s*([+-]?\d*\.?\d+)%\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,pe=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,me=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,be=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,Yt={string:[[function(t){var e=fe.exec(t);return e?(t=e[1]).length<=4?{r:parseInt(t[0]+t[0],16),g:parseInt(t[1]+t[1],16),b:parseInt(t[2]+t[2],16),a:t.length===4?M(parseInt(t[3]+t[3],16)/255,2):1}:t.length===6||t.length===8?{r:parseInt(t.substr(0,2),16),g:parseInt(t.substr(2,2),16),b:parseInt(t.substr(4,2),16),a:t.length===8?M(parseInt(t.substr(6,2),16)/255,2):1}:null:null},"hex"],[function(t){var e=me.exec(t)||be.exec(t);return e?e[2]!==e[4]||e[4]!==e[6]?null:Ot({r:Number(e[1])/(e[2]?100/255:1),g:Number(e[3])/(e[4]?100/255:1),b:Number(e[5])/(e[6]?100/255:1),a:e[7]===void 0?1:Number(e[7])/(e[8]?100:1)}):null},"rgb"],[function(t){var e=ge.exec(t)||pe.exec(t);if(!e)return null;var n,o,i=Xt({h:(n=e[1],o=e[2],o===void 0&&(o="deg"),Number(n)*(he[o]||1)),s:Number(e[3]),l:Number(e[4]),a:e[5]===void 0?1:Number(e[5])/(e[6]?100:1)});return Ut(i)},"hsl"]],object:[[function(t){var e=t.r,n=t.g,o=t.b,i=t.a,r=i===void 0?1:i;return V(e)&&V(n)&&V(o)?Ot({r:Number(e),g:Number(n),b:Number(o),a:Number(r)}):null},"rgb"],[function(t){var e=t.h,n=t.s,o=t.l,i=t.a,r=i===void 0?1:i;if(!V(e)||!V(n)||!V(o))return null;var s=Xt({h:Number(e),s:Number(n),l:Number(o),a:Number(r)});return Ut(s)},"hsl"],[function(t){var e=t.h,n=t.s,o=t.v,i=t.a,r=i===void 0?1:i;if(!V(e)||!V(n)||!V(o))return null;var s=function(a){return{h:Nt(a.h),s:_(a.s,0,100),v:_(a.v,0,100),a:_(a.a)}}({h:Number(e),s:Number(n),v:Number(o),a:Number(r)});return It(s)},"hsv"]]},Dt=function(t,e){for(var n=0;n<e.length;n++){var o=e[n][0](t);if(o)return[o,e[n][1]]}return[null,void 0]},ve=function(t){return typeof t=="string"?Dt(t.trim(),Yt.string):typeof t=="object"&&t!==null?Dt(t,Yt.object):[null,void 0]},at=function(t,e){var n=q(t);return{h:n.h,s:_(n.s+100*e,0,100),l:n.l,a:n.a}},ct=function(t){return(299*t.r+587*t.g+114*t.b)/1e3/255},Vt=function(t,e){var n=q(t);return{h:n.h,s:n.s,l:_(n.l+100*e,0,100),a:n.a}},kt=function(){function t(e){this.parsed=ve(e)[0],this.rgba=this.parsed||{r:0,g:0,b:0,a:1}}return t.prototype.isValid=function(){return this.parsed!==null},t.prototype.brightness=function(){return M(ct(this.rgba),2)},t.prototype.isDark=function(){return ct(this.rgba)<.5},t.prototype.isLight=function(){return ct(this.rgba)>=.5},t.prototype.toHex=function(){return e=st(this.rgba),n=e.r,o=e.g,i=e.b,s=(r=e.a)<1?et(M(255*r)):"","#"+et(n)+et(o)+et(i)+s;var e,n,o,i,r,s},t.prototype.toRgb=function(){return st(this.rgba)},t.prototype.toRgbString=function(){return e=st(this.rgba),n=e.r,o=e.g,i=e.b,(r=e.a)<1?"rgba("+n+", "+o+", "+i+", "+r+")":"rgb("+n+", "+o+", "+i+")";var e,n,o,i,r},t.prototype.toHsl=function(){return _t(q(this.rgba))},t.prototype.toHslString=function(){return e=_t(q(this.rgba)),n=e.h,o=e.s,i=e.l,(r=e.a)<1?"hsla("+n+", "+o+"%, "+i+"%, "+r+")":"hsl("+n+", "+o+"%, "+i+"%)";var e,n,o,i,r},t.prototype.toHsv=function(){return e=Mt(this.rgba),{h:M(e.h),s:M(e.s),v:M(e.v),a:M(e.a,3)};var e},t.prototype.invert=function(){return U({r:255-(e=this.rgba).r,g:255-e.g,b:255-e.b,a:e.a});var e},t.prototype.saturate=function(e){return e===void 0&&(e=.1),U(at(this.rgba,e))},t.prototype.desaturate=function(e){return e===void 0&&(e=.1),U(at(this.rgba,-e))},t.prototype.grayscale=function(){return U(at(this.rgba,-1))},t.prototype.lighten=function(e){return e===void 0&&(e=.1),U(Vt(this.rgba,e))},t.prototype.darken=function(e){return e===void 0&&(e=.1),U(Vt(this.rgba,-e))},t.prototype.rotate=function(e){return e===void 0&&(e=15),this.hue(this.hue()+e)},t.prototype.alpha=function(e){return typeof e=="number"?U({r:(n=this.rgba).r,g:n.g,b:n.b,a:e}):M(this.rgba.a,3);var n},t.prototype.hue=function(e){var n=q(this.rgba);return typeof e=="number"?U({h:e,s:n.s,l:n.l,a:n.a}):M(n.h)},t.prototype.isEqual=function(e){return this.toHex()===U(e).toHex()},t}(),U=function(t){return t instanceof kt?t:new kt(t)};const ye=t=>`background-color: ${U(t.fill||tt.fill).alpha(t.fillOpacity||tt.fillOpacity).toHex()};`,Ae=()=>{const t=document.createElement("style");document.getElementsByTagName("head")[0].appendChild(t);let e,n=new Set;return{destroy:()=>{CSS.highlights.clear(),t.remove()},refresh:(s,a,d,u)=>{e&&e.clear();const y=new Set(s.map(v=>v.annotation.id)),c=new Set(d),b=Array.from(n).filter(v=>!y.has(v)),p=s.map(v=>{const C=c.has(v.annotation.id),E=u?typeof u=="function"?u(v.annotation,C):u:C?Bt:tt,l=e&&e.paint(v,a,C)||E;return`::highlight(_${v.annotation.id}) { ${ye(l)} }`});t.innerHTML=p.join(`
2
+ `),b.forEach(v=>CSS.highlights.delete(`_${v}`)),s.forEach(({annotation:v})=>{const C=v.target.selector.map(l=>l.range),E=new Highlight(...C);CSS.highlights.set(`_${v.id}`,E)}),n=y},setPainter:s=>e=s}},Pt=(t,e,n)=>{const{store:o,selection:i,hover:r}=e;let s,a,d;const u=Ae(),y=yt(n),c=T=>{const{x:g,y:x}=t.getBoundingClientRect(),w=o.getAt(T.clientX-g,T.clientY-x);w&&(!d||d(w))?r.current!==w.id&&(t.classList.add("hovered"),r.set(w.id)):r.current&&(t.classList.remove("hovered"),r.set(null))};t.addEventListener("pointermove",c);const b=()=>{const T=P(t),{minX:g,minY:x,maxX:w,maxY:S}=T,B=d?o.getIntersectingRects(g,x,w,S).filter(({annotation:X})=>d(X)):o.getIntersectingRects(g,x,w,S),O=i.selected.map(({id:X})=>X);u.refresh(B,T,O,a),setTimeout(()=>y(B.map(({annotation:X})=>X)),1)},p=T=>{s=T,u.setPainter(T)},v=T=>{a=T,b()},C=T=>{d=T,b()},E=()=>b();o.observe(E);const l=i.subscribe(()=>b());document.addEventListener("scroll",b,{capture:!0,passive:!0});const h=J(()=>{o.recalculatePositions(),s&&s.reset(),b()});window.addEventListener("resize",h);const m=new ResizeObserver(h);m.observe(t);const f={attributes:!0,childList:!0,subtree:!0},A=new MutationObserver(b);return A.observe(document.body,f),{destroy:()=>{t.removeEventListener("pointermove",c),u.destroy(),o.unobserve(E),l(),document.removeEventListener("scroll",b),window.removeEventListener("resize",h),m.disconnect(),A.disconnect()},refresh:b,setDrawingStyle:v,setFilter:C,setPainter:p}};let nt;const we=new Uint8Array(16);function xe(){if(!nt&&(nt=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!nt))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return nt(we)}const I=[];for(let t=0;t<256;++t)I.push((t+256).toString(16).slice(1));function Ee(t,e=0){return I[t[e+0]]+I[t[e+1]]+I[t[e+2]]+I[t[e+3]]+"-"+I[t[e+4]]+I[t[e+5]]+"-"+I[t[e+6]]+I[t[e+7]]+"-"+I[t[e+8]]+I[t[e+9]]+"-"+I[t[e+10]]+I[t[e+11]]+I[t[e+12]]+I[t[e+13]]+I[t[e+14]]+I[t[e+15]]}const Ht={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function $t(t,e,n){if(Ht.randomUUID&&!e&&!t)return Ht.randomUUID();t=t||{};const o=t.random||(t.rng||xe)();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 Ee(o)}var Ft=Object.prototype.hasOwnProperty;function k(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--&&k(t[o],e[o]););return o===-1}if(!n||typeof t=="object"){o=0;for(n in t)if(Ft.call(t,n)&&++o&&!Ft.call(e,n)||!(n in e)||!k(t[n],e[n]))return!1;return Object.keys(e).length===o}}return t!==t&&e!==e}function lt(){}function Se(t,e){return t!=t?e==e:t!==e||t&&typeof t=="object"||typeof t=="function"}const $=[];function dt(t,e=lt){let n;const o=new Set;function i(a){if(Se(t,a)&&(t=a,n)){const d=!$.length;for(const u of o)u[1](),$.push(u,t);if(d){for(let u=0;u<$.length;u+=2)$[u][0]($[u+1]);$.length=0}}}function r(a){i(a(t))}function s(a,d=lt){const u=[a,d];return o.add(u),o.size===1&&(n=e(i,r)||lt),a(t),()=>{o.delete(u),o.size===0&&n&&(n(),n=null)}}return{set:i,update:r,subscribe:s}}const Ce=t=>{const{subscribe:e,set:n}=dt();let o;return e(i=>o=i),t.observe(({changes:i})=>{if(o){(i.deleted||[]).some(s=>s.id===o)&&n(void 0);const r=(i.updated||[]).find(({oldValue:s})=>s.id===o);r&&n(r.newValue.id)}}),{get current(){return o},subscribe:e,set:n}},ut={selected:[]},Te=(t,e="EDIT")=>{const{subscribe:n,set:o}=dt(ut);let i=ut;n(c=>i=c);const r=()=>o(ut),s=()=>{var c;return((c=i.selected)==null?void 0:c.length)===0},a=c=>{if(i.selected.length===0)return!1;const b=typeof c=="string"?c:c.id;return i.selected.some(p=>p.id===b)},d=(c,b)=>{const p=t.getAnnotation(c);if(p){const v=Re(p,e);o(v==="EDIT"?{selected:[{id:c,editable:!0}],pointerEvent:b}:v==="SELECT"?{selected:[{id:c}],pointerEvent:b}:{selected:[],pointerEvent:b})}else console.warn("Invalid selection: "+c)},u=(c,b=!0)=>{const p=Array.isArray(c)?c:[c],v=p.map(C=>t.getAnnotation(C)).filter(Boolean);o({selected:v.map(({id:C})=>({id:C,editable:b}))}),v.length!==p.length&&console.warn("Invalid selection",c)},y=c=>{if(i.selected.length===0)return!1;const{selected:b}=i;b.filter(({id:p})=>c.includes(p)).length>0&&o({selected:b.filter(({id:p})=>!c.includes(p))})};return t.observe(({changes:c})=>y((c.deleted||[]).map(b=>b.id))),{clear:r,clickSelect:d,get selected(){return i?[...i.selected]:null},get pointerEvent(){return i?i.pointerEvent:null},isEmpty:s,isSelected:a,setSelected:u,subscribe:n}},Re=(t,e)=>typeof e=="function"?e(t)||"EDIT":e||"EDIT",Be=[];for(let t=0;t<256;++t)Be.push((t+256).toString(16).slice(1));typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto);const Le=(t,e)=>{const n=new Set(t.bodies.map(o=>o.id));return e.bodies.filter(o=>!n.has(o.id))},Ne=(t,e)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},Oe=(t,e)=>e.bodies.map(n=>{const o=t.bodies.find(i=>i.id===n.id);return{newBody:n,oldBody:o&&!k(o,n)?o:void 0}}).filter(({oldBody:n})=>n).map(({oldBody:n,newBody:o})=>({oldBody:n,newBody:o})),Me=(t,e)=>!k(t.target,e.target),zt=(t,e)=>{const n=Le(t,e),o=Ne(t,e),i=Oe(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:Me(t,e)?{oldTarget:t.target,newTarget:e.target}:void 0}};var L=(t=>(t.LOCAL="LOCAL",t.REMOTE="REMOTE",t))(L||{});const Ie=(t,e)=>{var n,o;const{changes:i,origin:r}=e;if(!(!t.options.origin||t.options.origin===r))return!1;if(t.options.ignore){const{ignore:s}=t.options,a=d=>d&&d.length>0;if(!(a(i.created)||a(i.deleted))){const d=(n=i.updated)==null?void 0:n.some(y=>a(y.bodiesCreated)||a(y.bodiesDeleted)||a(y.bodiesUpdated)),u=(o=i.updated)==null?void 0:o.some(y=>y.targetUpdated);if(s==="BODY_ONLY"&&d&&!u||s==="TARGET_ONLY"&&u&&!d)return!1}}if(t.options.annotations){const s=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=>s.has(a))}else return!0},Xe=(t,e)=>{const n=new Set((t.created||[]).map(c=>c.id)),o=new Set((t.updated||[]).map(({newValue:c})=>c.id)),i=new Set((e.created||[]).map(c=>c.id)),r=new Set((e.deleted||[]).map(c=>c.id)),s=new Set((e.updated||[]).map(({oldValue:c})=>c.id)),a=new Set((e.updated||[]).filter(({oldValue:c})=>n.has(c.id)||o.has(c.id)).map(({oldValue:c})=>c.id)),d=[...(t.created||[]).filter(c=>!r.has(c.id)).map(c=>s.has(c.id)?e.updated.find(({oldValue:b})=>b.id===c.id).newValue:c),...e.created||[]],u=[...(t.deleted||[]).filter(c=>!i.has(c.id)),...(e.deleted||[]).filter(c=>!n.has(c.id))],y=[...(t.updated||[]).filter(({newValue:c})=>!r.has(c.id)).map(c=>{const{oldValue:b,newValue:p}=c;if(s.has(p.id)){const v=e.updated.find(C=>C.oldValue.id===p.id).newValue;return zt(b,v)}else return c}),...(e.updated||[]).filter(({oldValue:c})=>!a.has(c.id))];return{created:d,deleted:u,updated:y}},_e=t=>t.id!==void 0,Ue=()=>{const t=new Map,e=new Map,n=[],o=(g,x={})=>n.push({onChange:g,options:x}),i=g=>{const x=n.findIndex(w=>w.onChange==g);x>-1&&n.splice(x,1)},r=(g,x)=>{const w={origin:g,changes:{created:x.created||[],updated:x.updated||[],deleted:x.deleted||[]},state:[...t.values()]};n.forEach(S=>{Ie(S,w)&&S.onChange(w)})},s=(g,x=L.LOCAL)=>{if(t.get(g.id))throw Error(`Cannot add annotation ${g.id} - exists already`);t.set(g.id,g),g.bodies.forEach(w=>e.set(w.id,g.id)),r(x,{created:[g]})},a=(g,x)=>{const w=typeof g=="string"?x:g,S=typeof g=="string"?g:g.id,B=t.get(S);if(B){const O=zt(B,w);return S===w.id?t.set(S,w):(t.delete(S),t.set(w.id,w)),B.bodies.forEach(X=>e.delete(X.id)),w.bodies.forEach(X=>e.set(X.id,w.id)),O}else console.warn(`Cannot update annotation ${S} - does not exist`)},d=(g,x=L.LOCAL,w=L.LOCAL)=>{const S=_e(x)?w:x,B=a(g,x);B&&r(S,{updated:[B]})},u=(g,x=L.LOCAL)=>{const w=g.reduce((S,B)=>{const O=a(B);return O?[...S,O]:S},[]);w.length>0&&r(x,{updated:w})},y=(g,x=L.LOCAL)=>{const w=t.get(g.annotation);if(w){const S={...w,bodies:[...w.bodies,g]};t.set(w.id,S),e.set(g.id,S.id),r(x,{updated:[{oldValue:w,newValue:S,bodiesCreated:[g]}]})}else console.warn(`Attempt to add body to missing annotation: ${g.annotation}`)},c=()=>[...t.values()],b=(g=L.LOCAL)=>{const x=[...t.values()];t.clear(),e.clear(),r(g,{deleted:x})},p=(g,x=!0,w=L.LOCAL)=>{if(x){const S=[...t.values()];t.clear(),e.clear(),g.forEach(B=>{t.set(B.id,B),B.bodies.forEach(O=>e.set(O.id,B.id))}),r(w,{created:g,deleted:S})}else{const S=g.reduce((B,O)=>{const X=t.get(O.id);return X?[...B,X]:B},[]);if(S.length>0)throw Error(`Bulk insert would overwrite the following annotations: ${S.map(B=>B.id).join(", ")}`);g.forEach(B=>{t.set(B.id,B),B.bodies.forEach(O=>e.set(O.id,B.id))}),r(w,{created:g})}},v=g=>{const x=typeof g=="string"?g:g.id,w=t.get(x);if(w)return t.delete(x),w.bodies.forEach(S=>e.delete(S.id)),w;console.warn(`Attempt to delete missing annotation: ${x}`)},C=(g,x=L.LOCAL)=>{const w=v(g);w&&r(x,{deleted:[w]})},E=(g,x=L.LOCAL)=>{const w=g.reduce((S,B)=>{const O=v(B);return O?[...S,O]:S},[]);w.length>0&&r(x,{deleted:w})},l=(g,x=L.LOCAL)=>{const w=t.get(g.annotation);if(w){const S=w.bodies.find(B=>B.id===g.id);if(S){e.delete(S.id);const B={...w,bodies:w.bodies.filter(O=>O.id!==g.id)};t.set(w.id,B),r(x,{updated:[{oldValue:w,newValue:B,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}`)},h=g=>{const x=t.get(g);return x?{...x}:void 0},m=g=>{const x=e.get(g);if(x){const w=h(x).bodies.find(S=>S.id===g);if(w)return w;console.error(`Store integrity error: body ${g} in index, but not in annotation`)}else console.warn(`Attempt to retrieve missing body: ${g}`)},f=(g,x)=>{if(g.annotation!==x.annotation)throw"Annotation integrity violation: annotation ID must be the same when updating bodies";const w=t.get(g.annotation);if(w){const S=w.bodies.find(O=>O.id===g.id),B={...w,bodies:w.bodies.map(O=>O.id===S.id?x:O)};return t.set(w.id,B),S.id!==x.id&&(e.delete(S.id),e.set(x.id,B.id)),{oldValue:w,newValue:B,bodiesUpdated:[{oldBody:S,newBody:x}]}}else console.warn(`Attempt to add body to missing annotation ${g.annotation}`)},A=(g,x,w=L.LOCAL)=>{const S=f(g,x);S&&r(w,{updated:[S]})},R=(g,x=L.LOCAL)=>{const w=g.map(S=>f({id:S.id,annotation:S.annotation},S)).filter(Boolean);r(x,{updated:w})},T=g=>{const x=t.get(g.annotation);if(x){const w={...x,target:{...x.target,...g}};return t.set(x.id,w),{oldValue:x,newValue:w,targetUpdated:{oldTarget:x.target,newTarget:g}}}else console.warn(`Attempt to update target on missing annotation: ${g.annotation}`)};return{addAnnotation:s,addBody:y,all:c,bulkAddAnnotation:p,bulkDeleteAnnotation:E,bulkUpdateAnnotation:u,bulkUpdateBodies:R,bulkUpdateTargets:(g,x=L.LOCAL)=>{const w=g.map(S=>T(S)).filter(Boolean);w.length>0&&r(x,{updated:w})},clear:b,deleteAnnotation:C,deleteBody:l,getAnnotation:h,getBody:m,observe:o,unobserve:i,updateAnnotation:d,updateBody:A,updateTarget:(g,x=L.LOCAL)=>{const w=T(g);w&&r(x,{updated:[w]})}}};let Ye=()=>({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 De=250,Ve=t=>{const e=Ye(),n=[];let o=-1,i=!1,r=0;const s=p=>{if(!i){const{changes:v}=p,C=performance.now();if(C-r>De)n.splice(o+1),n.push(v),o=n.length-1;else{const E=n.length-1;n[E]=Xe(n[E],v)}r=C}i=!1};t.observe(s,{origin:L.LOCAL});const a=p=>p&&p.length>0&&t.bulkDeleteAnnotation(p),d=p=>p&&p.length>0&&t.bulkAddAnnotation(p,!1),u=p=>p&&p.length>0&&t.bulkUpdateAnnotation(p.map(({oldValue:v})=>v)),y=p=>p&&p.length>0&&t.bulkUpdateAnnotation(p.map(({newValue:v})=>v)),c=p=>p&&p.length>0&&t.bulkAddAnnotation(p,!1),b=p=>p&&p.length>0&&t.bulkDeleteAnnotation(p);return{canRedo:()=>n.length-1>o,canUndo:()=>o>-1,destroy:()=>t.unobserve(s),on:(p,v)=>e.on(p,v),redo:()=>{if(n.length-1>o){i=!0;const{created:p,updated:v,deleted:C}=n[o+1];d(p),y(v),b(C),e.emit("redo",n[o+1]),o+=1}},undo:()=>{if(o>-1){i=!0;const{created:p,updated:v,deleted:C}=n[o];a(p),u(v),c(C),e.emit("undo",n[o]),o-=1}}}},ke=()=>{const{subscribe:t,set:e}=dt([]);return{subscribe:t,set:e}},Pe=(t,e,n,o)=>{const{store:i,selection:r,hover:s,viewport:a}=t,d=new Map;let u=[],y,c;const b=(l,h)=>{d.has(l)?d.get(l).push(h):d.set(l,[h])},p=(l,h)=>{const m=d.get(l);m&&m.indexOf(h)>0&&m.splice(m.indexOf(h),1)},v=(l,h,m)=>{d.has(l)&&setTimeout(()=>{d.get(l).forEach(f=>{if(n){const A=Array.isArray(h)?h.map(T=>n.serialize(T)):n.serialize(h),R=m?m instanceof PointerEvent?m:n.serialize(m):void 0;f(A,R)}else f(h,m)})},1)},C=()=>{const{selected:l}=r,h=(l||[]).map(({id:m})=>i.getAnnotation(m));h.forEach(m=>{const f=u.find(A=>A.id===m.id);(!f||!k(f,m))&&v("updateAnnotation",m,f)}),u=u.map(m=>h.find(({id:A})=>A===m.id)||m)};r.subscribe(({selected:l})=>{if(!(u.length===0&&l.length===0)){if(u.length===0&&l.length>0)u=l.map(({id:h})=>i.getAnnotation(h));else if(u.length>0&&l.length===0)u.forEach(h=>{const m=i.getAnnotation(h.id);m&&!k(m,h)&&v("updateAnnotation",m,h)}),u=[];else{const h=new Set(u.map(f=>f.id)),m=new Set(l.map(({id:f})=>f));u.filter(f=>!m.has(f.id)).forEach(f=>{const A=i.getAnnotation(f.id);A&&!k(A,f)&&v("updateAnnotation",A,f)}),u=[...u.filter(f=>m.has(f.id)),...l.filter(({id:f})=>!h.has(f)).map(({id:f})=>i.getAnnotation(f))]}v("selectionChanged",u)}}),s.subscribe(l=>{!y&&l?v("mouseEnterAnnotation",i.getAnnotation(l)):y&&!l?v("mouseLeaveAnnotation",i.getAnnotation(y)):y&&l&&(v("mouseLeaveAnnotation",i.getAnnotation(y)),v("mouseEnterAnnotation",i.getAnnotation(l))),y=l}),a==null||a.subscribe(l=>v("viewportIntersect",l.map(h=>i.getAnnotation(h)))),i.observe(l=>{o&&(c&&clearTimeout(c),c=setTimeout(C,1e3));const{created:h,deleted:m}=l.changes;(h||[]).forEach(f=>v("createAnnotation",f)),(m||[]).forEach(f=>v("deleteAnnotation",f)),(l.changes.updated||[]).filter(f=>[...f.bodiesCreated||[],...f.bodiesDeleted||[],...f.bodiesUpdated||[]].length>0).forEach(({oldValue:f,newValue:A})=>{const R=u.find(T=>T.id===f.id)||f;u=u.map(T=>T.id===f.id?A:T),v("updateAnnotation",A,R)})},{origin:L.LOCAL}),i.observe(l=>{if(u){const h=new Set(u.map(f=>f.id)),m=(l.changes.updated||[]).filter(({newValue:f})=>h.has(f.id)).map(({newValue:f})=>f);m.length>0&&(u=u.map(f=>m.find(R=>R.id===f.id)||f))}},{origin:L.REMOTE});const E=l=>h=>{const{created:m,deleted:f,updated:A}=h;(m||[]).forEach(R=>v("createAnnotation",R)),(f||[]).forEach(R=>v("deleteAnnotation",R)),l?(A||[]).forEach(R=>v("updateAnnotation",R.oldValue,R.newValue)):(A||[]).forEach(R=>v("updateAnnotation",R.newValue,R.oldValue))};return e.on("undo",E(!0)),e.on("redo",E(!1)),{on:b,off:p,emit:v}},He=t=>e=>e.reduce((n,o)=>{const{parsed:i,error:r}=t.parse(o);return r?{parsed:n.parsed,failed:[...n.failed,o]}:i?{parsed:[...n.parsed,i],failed:n.failed}:{...n}},{parsed:[],failed:[]}),$e=(t,e,n)=>{const{store:o,selection:i}=t,r=E=>{if(n){const{parsed:l,error:h}=n.parse(E);l?o.addAnnotation(l,L.REMOTE):console.error(h)}else o.addAnnotation(E,L.REMOTE)},s=()=>i.clear(),a=()=>o.clear(),d=E=>{const l=o.getAnnotation(E);return n&&l?n.serialize(l):l},u=()=>n?o.all().map(n.serialize):o.all(),y=()=>{var E;const l=(((E=i.selected)==null?void 0:E.map(h=>h.id))||[]).map(h=>o.getAnnotation(h)).filter(Boolean);return n?l.map(n.serialize):l},c=E=>fetch(E).then(l=>l.json()).then(l=>(p(l),l)),b=E=>{if(typeof E=="string"){const l=o.getAnnotation(E);if(o.deleteAnnotation(E),l)return n?n.serialize(l):l}else{const l=n?n.parse(E).parsed:E;if(l)return o.deleteAnnotation(l),E}},p=E=>{if(n){const{parsed:l,failed:h}=He(n)(E);h.length>0&&console.warn(`Discarded ${h.length} invalid annotations`,h),o.bulkAddAnnotation(l,!0,L.REMOTE)}else o.bulkAddAnnotation(E,!0,L.REMOTE)},v=E=>{E?i.setSelected(E):i.clear()},C=E=>{if(n){const l=n.parse(E).parsed,h=n.serialize(o.getAnnotation(l.id));return o.updateAnnotation(l),h}else{const l=o.getAnnotation(E.id);return o.updateAnnotation(E),l}};return{addAnnotation:r,cancelSelected:s,canRedo:e.canRedo,canUndo:e.canUndo,clearAnnotations:a,getAnnotationById:d,getAnnotations:u,getSelected:y,loadAnnotations:c,redo:e.redo,removeAnnotation:b,setAnnotations:p,setSelected:v,undo:e.undo,updateAnnotation:C}};let Fe=t=>crypto.getRandomValues(new Uint8Array(t)),ze=(t,e,n)=>{let o=(2<<Math.log(t.length-1)/Math.LN2)-1,i=-~(1.6*o*e/t.length);return(r=e)=>{let s="";for(;;){let a=n(i),d=i;for(;d--;)if(s+=t[a[d]&o]||"",s.length===r)return s}}},We=(t,e=21)=>ze(t,e,Fe),qe=(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 je=()=>({isGuest:!0,id:We("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",20)()}),Ke=t=>{const e=JSON.stringify(t);let n=0;for(let o=0,i=e.length;o<i;o++){let r=e.charCodeAt(o);n=(n<<5)-n+r,n|=0}return`${n}`},Wt=t=>t?typeof t=="object"?{...t}:t:void 0,Qe=(t,e)=>(Array.isArray(t)?t:[t]).map(n=>{const{id:o,type:i,purpose:r,value:s,created:a,creator:d,...u}=n;return{id:o||`temp-${Ke(n)}`,annotation:e,type:i,purpose:r,value:s,created:a?new Date(a):void 0,creator:Wt(d),...u}}),Je=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()}});qe();const Ge=(t,e)=>({parse:n=>qt(n),serialize:n=>jt(n,t,e)}),Ze=t=>t.quote!==void 0&&t.start!==void 0&&t.end!==void 0,tn=(t,e)=>{const{id:n,creator:o,created:i,modified:r,target:s}=t,a=Array.isArray(s)?s:[s],d={creator:Wt(o),created:i?new Date(i):void 0,updated:r?new Date(r):void 0,annotation:n,selector:[]};for(const u of a){const c=(Array.isArray(u.selector)?u.selector:[u.selector]).reduce((b,p)=>{switch(p.type){case"TextQuoteSelector":b.quote=p.exact;break;case"TextPositionSelector":b.start=p.start,b.end=p.end;break}return b},{});if(Ze(c))d.selector.push(c);else{const b=[c.start?void 0:"TextPositionSelector",c.quote?void 0:"TextQuoteSelector"].filter(Boolean);return{error:Error(`Missing selector types: ${b.join(" and ")} for annotation: ${t.id}`)}}}return{parsed:d}},qt=(t,e)=>{const n=t.id||$t(),{creator:o,created:i,modified:r,body:s,...a}=t,d=Qe(s,n),u=tn(t);return"error"in u?{error:u.error}:{parsed:{...a,id:n,bodies:d,target:u.parsed}}},jt=(t,e,n)=>{const{bodies:o,target:i,...r}=t,{selector:s,creator:a,created:d,updated:u,...y}=i,c=s.map(b=>{const{quote:p,start:v,end:C,range:E}=b,{prefix:l,suffix:h}=St(E,n);return{...y,source:e,selector:[{type:"TextQuoteSelector",exact:p,prefix:l,suffix:h},{type:"TextPositionSelector",start:v,end:C}]}});return{...r,"@context":"http://www.w3.org/ns/anno.jsonld",id:t.id,type:"Annotation",body:Je(t.bodies),creator:a,created:d==null?void 0:d.toISOString(),modified:u==null?void 0:u.toISOString(),target:c}};function en(t,e,n,o,i){Kt(t,e,n||0,o||t.length-1,i||nn)}function Kt(t,e,n,o,i){for(;o>n;){if(o-n>600){var r=o-n+1,s=e-n+1,a=Math.log(r),d=.5*Math.exp(2*a/3),u=.5*Math.sqrt(a*d*(r-d)/r)*(s-r/2<0?-1:1),y=Math.max(n,Math.floor(e-s*d/r+u)),c=Math.min(o,Math.floor(e+(r-s)*d/r+u));Kt(t,e,y,c,i)}var b=t[e],p=n,v=o;for(j(t,n,e),i(t[o],b)>0&&j(t,n,o);p<v;){for(j(t,p,v),p++,v--;i(t[p],b)<0;)p++;for(;i(t[v],b)>0;)v--}i(t[n],b)===0?j(t,n,v):(v++,j(t,v,o)),v<=e&&(n=v+1),e<=v&&(o=v-1)}}function j(t,e,n){var o=t[e];t[e]=t[n],t[n]=o}function nn(t,e){return t<e?-1:t>e?1:0}class on{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(!it(e,n))return o;const i=this.toBBox,r=[];for(;n;){for(let s=0;s<n.children.length;s++){const a=n.children[s],d=n.leaf?i(a):a;it(e,d)&&(n.leaf?o.push(a):ft(e,d)?this._all(a,o):r.push(a))}n=r.pop()}return o}collides(e){let n=this.data;if(!it(e,n))return!1;const o=[];for(;n;){for(let i=0;i<n.children.length;i++){const r=n.children[i],s=n.leaf?this.toBBox(r):r;if(it(e,s)){if(n.leaf||ft(e,s))return!0;o.push(r)}}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=z([]),this}remove(e,n){if(!e)return this;let o=this.data;const i=this.toBBox(e),r=[],s=[];let a,d,u;for(;o||r.length;){if(o||(o=r.pop(),d=r[r.length-1],a=s.pop(),u=!0),o.leaf){const y=rn(e,o.children,n);if(y!==-1)return o.children.splice(y,1),r.push(o),this._condense(r),this}!u&&!o.leaf&&ft(o,i)?(r.push(o),s.push(a),a=0,d=o,o=o.children[0]):d?(a++,o=d.children[a],u=!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 r=o-n+1;let s=this._maxEntries,a;if(r<=s)return a=z(e.slice(n,o+1)),F(a,this.toBBox),a;i||(i=Math.ceil(Math.log(r)/Math.log(s)),s=Math.ceil(r/Math.pow(s,i-1))),a=z([]),a.leaf=!1,a.height=i;const d=Math.ceil(r/s),u=d*Math.ceil(Math.sqrt(s));Qt(e,n,o,u,this.compareMinX);for(let y=n;y<=o;y+=u){const c=Math.min(y+u-1,o);Qt(e,y,c,d,this.compareMinY);for(let b=y;b<=c;b+=d){const p=Math.min(b+d-1,c);a.children.push(this._build(e,b,p,i-1))}}return F(a,this.toBBox),a}_chooseSubtree(e,n,o,i){for(;i.push(n),!(n.leaf||i.length-1===o);){let r=1/0,s=1/0,a;for(let d=0;d<n.children.length;d++){const u=n.children[d],y=ht(u),c=cn(e,u)-y;c<s?(s=c,r=y<r?y:r,a=u):c===s&&y<r&&(r=y,a=u)}n=a||n.children[0]}return n}_insert(e,n,o){const i=o?e:this.toBBox(e),r=[],s=this._chooseSubtree(i,this.data,n,r);for(s.children.push(e),Q(s,i);n>=0&&r[n].children.length>this._maxEntries;)this._split(r,n),n--;this._adjustParentBBoxes(i,r,n)}_split(e,n){const o=e[n],i=o.children.length,r=this._minEntries;this._chooseSplitAxis(o,r,i);const s=this._chooseSplitIndex(o,r,i),a=z(o.children.splice(s,o.children.length-s));a.height=o.height,a.leaf=o.leaf,F(o,this.toBBox),F(a,this.toBBox),n?e[n-1].children.push(a):this._splitRoot(o,a)}_splitRoot(e,n){this.data=z([e,n]),this.data.height=e.height+1,this.data.leaf=!1,F(this.data,this.toBBox)}_chooseSplitIndex(e,n,o){let i,r=1/0,s=1/0;for(let a=n;a<=o-n;a++){const d=K(e,0,a,this.toBBox),u=K(e,a,o,this.toBBox),y=ln(d,u),c=ht(d)+ht(u);y<r?(r=y,i=a,s=c<s?c:s):y===r&&c<s&&(s=c,i=a)}return i||o-n}_chooseSplitAxis(e,n,o){const i=e.leaf?this.compareMinX:sn,r=e.leaf?this.compareMinY:an,s=this._allDistMargin(e,n,o,i),a=this._allDistMargin(e,n,o,r);s<a&&e.children.sort(i)}_allDistMargin(e,n,o,i){e.children.sort(i);const r=this.toBBox,s=K(e,0,n,r),a=K(e,o-n,o,r);let d=ot(s)+ot(a);for(let u=n;u<o-n;u++){const y=e.children[u];Q(s,e.leaf?r(y):y),d+=ot(s)}for(let u=o-n-1;u>=n;u--){const y=e.children[u];Q(a,e.leaf?r(y):y),d+=ot(a)}return d}_adjustParentBBoxes(e,n,o){for(let i=o;i>=0;i--)Q(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():F(e[n],this.toBBox)}}function rn(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 F(t,e){K(t,0,t.children.length,e,t)}function K(t,e,n,o,i){i||(i=z(null)),i.minX=1/0,i.minY=1/0,i.maxX=-1/0,i.maxY=-1/0;for(let r=e;r<n;r++){const s=t.children[r];Q(i,t.leaf?o(s):s)}return i}function Q(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 sn(t,e){return t.minX-e.minX}function an(t,e){return t.minY-e.minY}function ht(t){return(t.maxX-t.minX)*(t.maxY-t.minY)}function ot(t){return t.maxX-t.minX+(t.maxY-t.minY)}function cn(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 ln(t,e){const n=Math.max(t.minX,e.minX),o=Math.max(t.minY,e.minY),i=Math.min(t.maxX,e.maxX),r=Math.min(t.maxY,e.maxY);return Math.max(0,i-n)*Math.max(0,r-o)}function ft(t,e){return t.minX<=e.minX&&t.minY<=e.minY&&e.maxX<=t.maxX&&e.maxY<=t.maxY}function it(t,e){return e.minX<=t.maxX&&e.minY<=t.maxY&&e.maxX>=t.minX&&e.maxY>=t.minY}function z(t){return{children:t,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function Qt(t,e,n,o,i){const r=[e,n];for(;r.length;){if(n=r.pop(),e=r.pop(),n-e<=o)continue;const s=e+Math.ceil((n-e)/o/2)*o;en(t,s,e,n,i),r.push(e,s,s,n)}}const dn=(t,e)=>{const n=new on,o=new Map,i=l=>{const h=e.getBoundingClientRect(),m=l.selector.flatMap(A=>{const T=A.range instanceof Range&&!A.range.collapsed&&A.range.startContainer.nodeType===Node.TEXT_NODE&&A.range.endContainer.nodeType===Node.TEXT_NODE?A.range:rt(A,e).range;return Array.from(T.getClientRects())}),f=Ct(m);return f.map(A=>{const{x:R,y:T,width:g,height:x}=A;return{minX:R-h.x,minY:T-h.y,maxX:R-h.x+g,maxY:T-h.y+x,annotation:{id:l.annotation,rects:f}}})},r=()=>[...o.values()],s=()=>{n.clear(),o.clear()},a=l=>{const h=i(l);h.forEach(m=>n.insert(m)),o.set(l.annotation,h)},d=l=>{const h=o.get(l.annotation);h&&(h.forEach(m=>n.remove(m)),o.delete(l.annotation))},u=l=>{d(l),a(l)},y=(l,h=!0)=>{h&&s();const m=l.map(A=>({target:A,rects:i(A)}));m.forEach(({target:A,rects:R})=>o.set(A.annotation,R));const f=m.reduce((A,{rects:R})=>[...A,...R],[]);n.load(f)},c=(l,h)=>{const m=n.search({minX:l,minY:h,maxX:l,maxY:h}),f=A=>A.annotation.rects.reduce((R,T)=>R+T.width*T.height,0);if(m.length>0)return m.sort((A,R)=>f(A)-f(R)),m[0].annotation.id},b=l=>{const h=p(l);if(h.length===0)return;let m=h[0].left,f=h[0].top,A=h[0].right,R=h[0].bottom;for(let T=1;T<h.length;T++){const g=h[T];m=Math.min(m,g.left),f=Math.min(f,g.top),A=Math.max(A,g.right),R=Math.max(R,g.bottom)}return new DOMRect(m,f,A-m,R-f)},p=l=>{const h=o.get(l);return h?h[0].annotation.rects:[]};return{all:r,clear:s,getAt:c,getBoundsForAnnotation:b,getDOMRectsForAnnotation:p,getIntersectingRects:(l,h,m,f)=>n.search({minX:l,minY:h,maxX:m,maxY:f}),insert:a,recalculate:()=>y(t.all().map(l=>l.target),!0),remove:d,set:y,size:()=>n.all().length,update:u}},Jt=(t,e)=>{const n=Ue(),o=dn(n,t),i=Te(n,e),r=Ce(n),s=ke(),a=(l,h=L.LOCAL)=>{const m=Z(l,t),f=D(m.target.selector);return f&&n.addAnnotation(m,h),f},d=(l,h=!0,m=L.LOCAL)=>{const f=l.map(R=>Z(R,t)),A=f.filter(R=>!D(R.target.selector));return A.length>0?(console.warn("Could not revive all targets for these annotations:",A),n.bulkAddAnnotation(f,h,m),A):(n.bulkAddAnnotation(f,h,m),[])},u=(l,h=L.LOCAL)=>{const m=l.map(A=>Z(A,t)),f=m.filter(A=>!D(A.target.selector));return f.length>0&&console.warn("Could not revive all targets for these annotations:",f),m.forEach(A=>{n.getAnnotation(A.id)?n.updateAnnotation(A,h):n.addAnnotation(A,h)}),f},y=(l,h=L.LOCAL)=>{const m=W(l,t);n.updateTarget(m,h)},c=(l,h=L.LOCAL)=>{const m=l.map(f=>W(f,t));n.bulkUpdateTargets(m,h)},b=(l,h)=>{const m=o.getAt(l,h);return m?n.getAnnotation(m):void 0},p=(l,h,m,f)=>{const A=o.getIntersectingRects(l,h,m,f);return Array.from(new Set(A.map(T=>T.annotation.id))).map(T=>n.getAnnotation(T)).filter(Boolean)},v=(l,h,m,f=5)=>{const A=o.getDOMRectsForAnnotation(l);if(A.length!==0){if(h&&m){const R=A.find(({top:T,right:g,bottom:x,left:w})=>h>=w-f&&h<=g+f&&m>=T-f&&m<=x+f);if(R)return R}return o.getBoundsForAnnotation(l)}},C=(l,h,m,f)=>{const R=o.getIntersectingRects(l,h,m,f).reduce((T,g)=>((T[g.annotation.id]=T[g.annotation.id]||[]).push(g),T),{});return Object.entries(R).map(([T,g])=>({annotation:n.getAnnotation(T),rects:g.map(({minX:x,minY:w,maxX:S,maxY:B})=>({x,y:w,width:S-x,height:B-w}))})).filter(T=>!!T.annotation)},E=()=>o.recalculate();return n.observe(({changes:l})=>{const h=(l.created||[]).filter(A=>D(A.target.selector)),m=(l.deleted||[]).filter(A=>D(A.target.selector)),f=(l.updated||[]).filter(A=>D(A.newValue.target.selector));h.length>0&&o.set(h.map(A=>A.target),!1),(m==null?void 0:m.length)>0&&m.forEach(A=>o.remove(A.target)),(f==null?void 0:f.length)>0&&f.forEach(({newValue:A})=>o.update(A.target))}),{store:{...n,addAnnotation:a,bulkAddAnnotation:d,bulkUpdateTargets:c,bulkUpsertAnnotations:u,getAnnotationBounds:v,getAt:b,getIntersecting:p,getIntersectingRects:C,recalculatePositions:E,updateTarget:y},selection:i,hover:r,viewport:s}},un=()=>{const t=document.createElement("canvas");t.width=2*window.innerWidth,t.height=2*window.innerHeight,t.className="r6o-highlight-layer presence";const e=t.getContext("2d");return e.scale(2,2),e.translate(.5,.5),t},hn=(t,e,n={})=>{const o=un(),i=o.getContext("2d");t.appendChild(o);const r=new Map,s=c=>Array.from(r.entries()).filter(([b,p])=>p.presenceKey===c.presenceKey).map(([b,p])=>b);return e.on("selectionChange",(c,b)=>{s(c).forEach(v=>r.delete(v)),b&&b.forEach(v=>r.set(v,c))}),{clear:()=>{const{width:c,height:b}=o;i.clearRect(-.5,-.5,c+1,b+1)},destroy:()=>{o.remove()},paint:(c,b,p)=>{n.font&&(i.font=n.font);const v=r.get(c.annotation.id);if(v){const{height:C}=c.rects[0],E=c.rects[0].x+b.left,l=c.rects[0].y+b.top;i.fillStyle=v.appearance.color,i.fillRect(E-2,l-2.5,2,C+5);const h=i.measureText(v.appearance.label),m=h.width+6,f=h.actualBoundingBoxAscent+h.actualBoundingBoxDescent+8,A=h.fontBoundingBoxAscent?8:6.5;return i.fillRect(E-2,l-2.5-f,m,f),i.fillStyle="#fff",i.fillText(v.appearance.label,E+1,l-A),{fill:v.appearance.color,fillOpacity:p?.45:.18}}},reset:()=>{o.width=2*window.innerWidth,o.height=2*window.innerHeight;const c=o.getContext("2d");c.scale(2,2),c.translate(.5,.5)}}},Gt=t=>{if(t===null)return document.scrollingElement;const{overflowY:e}=window.getComputedStyle(t);return e!=="visible"&&e!=="hidden"&&t.scrollHeight>t.clientHeight?t:Gt(t.parentElement)},fn=(t,e)=>n=>{const o=r=>{const s=i.getBoundingClientRect(),a=i.clientHeight,d=i.clientWidth,u=r.selector[0].range.getBoundingClientRect(),{width:y,height:c}=e.getAnnotationBounds(n.id),b=u.top-s.top,p=u.left-s.left,v=i.parentElement?i.scrollTop:0,C=i.parentElement?i.scrollLeft:0,E=b+v-(a-c)/2,l=p+C-(d-y)/2;i.scroll({top:E,left:l,behavior:"smooth"})},i=Gt(t);if(i){const r=e.getAnnotation(n.id),{range:s}=r.target.selector[0];if(s&&!s.collapsed)return o(r.target),!0;{const a=W(r.target,t),{range:d}=a.selector[0];if(d&&!d.collapsed)return o(a),!0}}return!1},gn=(t,e,n)=>{const{store:o,selection:i}=e;let r,s;const a=C=>r=C;let d=!1,u;const y=C=>{var l;if(!d)return;!((l=C.target.parentElement)!=null&&l.closest(".not-annotatable"))?s={annotation:$t(),selector:[],creator:r,created:new Date}:s=void 0};t.addEventListener("selectstart",y);const c=J(C=>{const E=document.getSelection();if(C.timeStamp-((u==null?void 0:u.timeStamp)||C.timeStamp)<1e3&&!s&&y(u),E.isCollapsed||!d||!s)return;const l=E.getRangeAt(0),h=Rt(l.cloneRange()),m=wt(h);(m.length!==s.selector.length||m.some((A,R)=>{var T;return A.toString()!==((T=s.selector[R])==null?void 0:T.quote)}))&&(s={...s,selector:m.map(A=>Tt(A,t,n))},o.getAnnotation(s.annotation)?o.updateTarget(s,L.LOCAL):(o.addAnnotation({id:s.annotation,bodies:[],target:s}),i.clickSelect(s.annotation,u)))});document.addEventListener("selectionchange",c);const b=C=>{const{target:E,timeStamp:l,offsetX:h,offsetY:m,type:f}=C;u={...C,target:E,timeStamp:l,offsetX:h,offsetY:m,type:f},d=C.button===0};t.addEventListener("pointerdown",b);const p=C=>{var m;if(!!((m=C.target.parentElement)!=null&&m.closest(".not-annotatable"))||!d)return;const l=()=>{const{x:f,y:A}=t.getBoundingClientRect(),R=o.getAt(C.clientX-f,C.clientY-A);if(R){const{selected:T}=i;(T.length!==1||T[0].id!==R.id)&&i.clickSelect(R.id,C)}else i.isEmpty()||i.clear()},h=C.timeStamp-u.timeStamp;document.getSelection().isCollapsed&&h<300?(s=void 0,l()):s&&i.clickSelect(s.annotation,C)};return document.addEventListener("pointerup",p),{destroy:()=>{t.removeEventListener("selectstart",y),document.removeEventListener("selectionchange",c),t.removeEventListener("pointerdown",b),document.removeEventListener("pointerup",p)},setUser:a}},pn=(t,e={})=>{t.addEventListener("click",f=>!f.target.closest("a")&&f.preventDefault());const n=Jt(t,e.pointerAction),{selection:o,viewport:i}=n,r=n.store,s=Ve(r),a=Pe(n,s,e.adapter);let d=je();const u=e.experimentalCSSRenderer&&!!CSS.highlights;u&&console.log("Using experimental CSS Custom Highlight API renderer");const y=u?Pt(t,n,i):Lt(t,n,i);e.style&&y.setDrawingStyle(e.style);const c=gn(t,n,e.offsetReferenceSelector);return c.setUser(d),{...$e(n,s,e.adapter),destroy:()=>{y.destroy(),c.destroy(),s.destroy()},element:t,getUser:()=>d,setFilter:f=>y.setFilter(f),setStyle:f=>y.setDrawingStyle(f),setUser:f=>{d=f,c.setUser(f)},setSelected:f=>{f?o.setSelected(f):o.clear()},setPresenceProvider:f=>{f&&(y.setPainter(hn(t,f,e.presence)),f.on("selectionChange",()=>y.refresh()))},on:a.on,off:a.off,scrollIntoView:fn(t,r),state:n}};N.NOT_ANNOTATABLE_SELECTOR=H,N.Origin=L,N.W3CTextFormat=Ge,N.createCSSHighlightRenderer=Pt,N.createCanvasHighlightRenderer=Lt,N.createTextAnnotator=pn,N.createTextAnnotatorState=Jt,N.debounce=J,N.getAnnotatableFragment=ie,N.getClientRectsPonyfill=ae,N.getQuoteContext=St,N.getRangeAnnotatableContents=G,N.isRevived=D,N.mergeClientRects=Ct,N.parseW3CTextAnnotation=qt,N.rangeToSelector=Tt,N.reviveAnnotation=Z,N.reviveSelector=rt,N.reviveTarget=W,N.serializeW3CTextAnnotation=jt,N.splitAnnotatableRanges=wt,N.trimRange=Rt,Object.defineProperty(N,Symbol.toStringTag,{value:"Module"})});
3
3
  //# sourceMappingURL=text-annotator.umd.js.map