@recogito/text-annotator 3.0.1 → 3.0.3

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(O,J){typeof exports=="object"&&typeof module<"u"?J(exports):typeof define=="function"&&define.amd?define(["exports"],J):(O=typeof globalThis<"u"?globalThis:O||self,J(O.RecogitoJS={}))})(this,function(O){"use strict";const J="not-annotatable",Z=`.${J}`,et=t=>{var n;return!!(t instanceof HTMLElement?t.closest(Z):(n=t.parentElement)==null?void 0:n.closest(Z))},jt=t=>{const e=t.commonAncestorContainer;return!et(e)},Ft=t=>t.addEventListener("click",e=>{!e.target.closest(Z)&&!e.target.closest("a")&&e.preventDefault()}),yt=t=>({...t,type:t.type,x:t.x,y:t.y,clientX:t.clientX,clientY:t.clientY,offsetX:t.offsetX,offsetY:t.offsetY,screenX:t.screenX,screenY:t.screenY,isPrimary:t.isPrimary,altKey:t.altKey,ctrlKey:t.ctrlKey,metaKey:t.metaKey,shiftKey:t.shiftKey,button:t.button,buttons:t.buttons,currentTarget:t.currentTarget,target:t.target,defaultPrevented:t.defaultPrevented,detail:t.detail,eventPhase:t.eventPhase,pointerId:t.pointerId,pointerType:t.pointerType,timeStamp:t.timeStamp}),st=t=>({...t,type:t.type,key:t.key,code:t.code,location:t.location,repeat:t.repeat,altKey:t.altKey,ctrlKey:t.ctrlKey,metaKey:t.metaKey,shiftKey:t.shiftKey,currentTarget:t.currentTarget,target:t.target,defaultPrevented:t.defaultPrevented,detail:t.detail,timeStamp:t.timeStamp}),zt=typeof navigator<"u"&&/mac/i.test(navigator.userAgentData?navigator.userAgentData.platform:navigator.platform),Wt=t=>{!t.hasAttribute("tabindex")&&t.tabIndex<0&&t.setAttribute("tabindex","-1"),t.classList.add("no-focus-outline")},bt=(t,e=10)=>{let n;return(...o)=>{clearTimeout(n),n=setTimeout(()=>t.apply(void 0,o),e)}},qe=function*(t){const e=document.createNodeIterator(t.commonAncestorContainer,NodeFilter.SHOW_ELEMENT,o=>o instanceof HTMLElement&&o.classList.contains(J)&&!o.parentElement.closest(Z)&&t.intersectsNode(o)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP);let n;for(;n=e.nextNode();)n instanceof HTMLElement&&(yield n)},qt=t=>{if(!jt(t))return[];const e=[];let n=null;for(const o of qe(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]},At=t=>{const e=t.cloneContents();return e.querySelectorAll(Z).forEach(n=>n.remove()),e},Gt=(t,e,n=10,o)=>{const i=o?t.startContainer.parentElement.closest(o):e,s=document.createRange();s.setStart(i,0),s.setEnd(t.startContainer,t.startOffset);const a=At(s).textContent,r=document.createRange();r.setStart(t.endContainer,t.endOffset),i===document.body?r.setEnd(i,i.childNodes.length):r.setEndAfter(i);const l=At(r).textContent;return{prefix:a.substring(a.length-n),suffix:l.substring(0,n)}},z=t=>t.every(e=>e.range instanceof Range&&!e.range.collapsed),Qt=/^\s*$/,Jt=t=>Qt.test(t.toString()),Ge=(t,e)=>{const n=s=>Math.round(s*10)/10,o={top:n(t.top),bottom:n(t.bottom),left:n(t.left),right:n(t.right)},i={top:n(e.top),bottom:n(e.bottom),left:n(e.left),right:n(e.right)};if(Math.abs(o.top-i.top)<.5&&Math.abs(o.bottom-i.bottom)<.5){if(Math.abs(o.left-i.right)<.5||Math.abs(o.right-i.left)<.5)return"inline-adjacent";if(o.left>=i.left&&o.right<=i.right)return"inline-is-contained";if(o.left<=i.left&&o.right>=i.right)return"inline-contains"}else if(o.top<=i.top&&o.bottom>=i.bottom){if(o.left<=i.left&&o.right>=i.right)return"block-contains"}else if(o.top>=i.top&&o.bottom<=i.bottom&&o.left>=i.left&&o.right<=i.right)return"block-is-contained"},Qe=(t,e)=>{const n=Math.min(t.left,e.left),o=Math.max(t.right,e.right),i=Math.min(t.top,e.top),s=Math.max(t.bottom,e.bottom);return new DOMRect(n,i,o-n,s-i)},Zt=t=>t.reduce((e,n)=>{if(n.width===0||n.height===0)return e;let o=[...e],i=!1;for(const s of e){const a=Ge(n,s);if(a==="inline-adjacent"){o=o.map(r=>r===s?Qe(n,s):r),i=!0;break}else if(a==="inline-contains"){o=o.map(r=>r===s?n:r),i=!0;break}else if(a==="inline-is-contained"){i=!0;break}else if(a==="block-contains"||a==="block-is-contained"){n.width<s.width&&(o=o.map(r=>r===s?n:r)),i=!0;break}}return i?o:[...o,n]},[]),Je=t=>({length:t.length,item:e=>t[e],[Symbol.iterator]:function*(){for(let e=0;e<this.length;e++)yield this.item(e)}}),te=(t,e,n)=>{const o=document.createRange(),i=n?t.startContainer.parentElement.closest(n):e;o.setStart(i,0),o.setEnd(t.startContainer,t.startOffset);const s=At(o).textContent,a=t.toString(),r=s.length||0,l=r+a.length;return n?{quote:a,start:r,end:l,range:t,offsetReference:i}:{quote:a,start:r,end:l,range:t}},Ot=(t,e)=>{var h,u;const{start:n,end:o}=t,i=t.offsetReference||e,s=document.createNodeIterator(e,NodeFilter.SHOW_TEXT,g=>{var v;return(v=g.parentElement)!=null&&v.closest(Z)?NodeFilter.FILTER_SKIP:NodeFilter.FILTER_ACCEPT});let a=0;const r=document.createRange();let l=s.nextNode();l===null&&console.error("Could not revive annotation target. Content missing.");let f=!i;for(;l!==null;){if(f||(f=typeof(i==null?void 0:i.contains)=="function"?i.contains(l):!1),f){const g=((h=l.textContent)==null?void 0:h.length)||0;if(a+g>n){r.setStart(l,n-a);break}a+=g}l=s.nextNode()}for(;l!==null;){const g=((u=l.textContent)==null?void 0:u.length)||0;if(a+g>=o){r.setEnd(l,o-a);break}a+=g,l=s.nextNode()}return{...t,range:r}},rt=(t,e)=>z(t.selector)?t:{...t,selector:t.selector.map(n=>n.range instanceof Range&&!n.range.collapsed?n:Ot(n,e))},wt=(t,e)=>z(t.target.selector)?t:{...t,target:rt(t.target,e)},ee=(t,e)=>{const n=t.cloneRange();return e.contains(n.startContainer)||n.setStart(e,0),e.contains(n.endContainer)||n.setEnd(e,e.childNodes.length),n},ne=t=>{if(t===null)return document.scrollingElement;const{overflowY:e}=window.getComputedStyle(t);return e!=="visible"&&e!=="hidden"&&t.scrollHeight>t.clientHeight?t:ne(t.parentElement)},oe=(t,e)=>n=>{const o=typeof n=="string"?n:n.id,i=a=>{const r=s.getBoundingClientRect(),l=s.clientHeight,f=s.clientWidth,h=a.selector[0].range.getBoundingClientRect(),{width:u,height:g}=e.getAnnotationBounds(o),v=h.top-r.top,p=h.left-r.left,m=s.parentElement?s.scrollTop:0,w=s.parentElement?s.scrollLeft:0,d=v+m-(l-g)/2,c=p+w-(f-u)/2;s.scroll({top:d,left:c,behavior:"smooth"})},s=ne(t);if(s){const a=e.getAnnotation(o),{range:r}=a.target.selector[0];if(r&&!r.collapsed)return i(a.target),!0;{const l=rt(a.target,t),{range:f}=l.selector[0];if(f&&!f.collapsed)return i(l),!0}}return!1},q={fill:"rgb(0, 128, 255)",fillOpacity:.18},at={fill:"rgb(0, 128, 255)",fillOpacity:.45},ie=(t,e,n,o,i)=>{var a,r;const s=n?typeof n=="function"?n(t.annotation,t.state,i)||((a=t.state)!=null&&a.selected?at:q):n:(r=t.state)!=null&&r.selected?at:q;return o&&o.paint(t,e)||s},Ze=t=>{const{top:e,left:n}=t.getBoundingClientRect(),{innerWidth:o,innerHeight:i}=window,s=-n,a=-e,r=o-n,l=i-e;return{top:e,left:n,minX:s,minY:a,maxX:r,maxY:l}},tn=t=>{let e=new Set;return o=>{const i=o.map(s=>s.id);(e.size!==i.length||i.some(s=>!e.has(s)))&&t.set(i),e=new Set(i)}},Rt=(t,e,n,o)=>{const{store:i,selection:s,hover:a}=e;let r,l,f;const h=tn(n),u=B=>{const{x:T,y:_}=t.getBoundingClientRect(),P=i.getAt(B.clientX-T,B.clientY-_,!1,l);P?a.current!==P.id&&(t.classList.add("hovered"),a.set(P.id)):a.current&&(t.classList.remove("hovered"),a.set(null))};t.addEventListener("pointermove",u);const g=(B=!1)=>{f&&f.clear();const T=Ze(t),{minX:_,minY:P,maxX:y,maxY:E}=T,A=l?i.getIntersecting(_,P,y,E).filter(({annotation:R})=>l(R)):i.getIntersecting(_,P,y,E),L=s.selected.map(({id:R})=>R),I=A.map(({annotation:R,rects:N})=>{const H=L.includes(R.id),vo=R.id===a.current;return{annotation:R,rects:N,state:{selected:H,hover:vo}}});o.redraw(I,T,r,f,B),setTimeout(()=>h(A.map(({annotation:R})=>R)),1)},v=B=>{f=B,g()},p=B=>{r=B,g()},m=B=>{l=B,g(!1)},w=()=>g();i.observe(w);const d=s.subscribe(()=>g()),c=()=>g(!0);document.addEventListener("scroll",c,{capture:!0,passive:!0});const b=bt(()=>{i.recalculatePositions(),f&&f.reset(),g()});window.addEventListener("resize",b);const x=new ResizeObserver(b);x.observe(t);const S={attributes:!0,childList:!0,subtree:!0},C=new MutationObserver(B=>{B.every(_=>_.target===t||t.contains(_.target))||g(!0)});return C.observe(document.body,S),{destroy:()=>{t.removeEventListener("pointermove",u),o.destroy(),i.unobserve(w),d(),document.removeEventListener("scroll",c),window.removeEventListener("resize",b),x.disconnect(),C.disconnect()},redraw:g,setStyle:p,setFilter:m,setPainter:v,setVisible:o.setVisible}},en=()=>{const t=document.createElement("canvas");return t.width=window.innerWidth,t.height=window.innerHeight,t.className="r6o-canvas-highlight-layer bg",t},nn=(t,e)=>{t.width=window.innerWidth,t.height=window.innerHeight},on=t=>{t.classList.add("r6o-annotatable");const e=en(),n=e.getContext("2d");document.body.appendChild(e);const o=(r,l,f,h)=>requestAnimationFrame(()=>{const{width:u,height:g}=e;n.clearRect(-.5,-.5,u+1,g+1),h&&h.clear();const{top:v,left:p}=l;[...r].sort((w,d)=>{const{annotation:{target:{created:c}}}=w,{annotation:{target:{created:b}}}=d;return c.getTime()-b.getTime()}).forEach(w=>{var x;const d=f?typeof f=="function"?f(w.annotation,w.state):f:(x=w.state)!=null&&x.selected?at:q,c=h&&h.paint(w,l)||d,b=w.rects.map(({x:S,y:C,width:M,height:B})=>({x:S+p,y:C+v,width:M,height:B}));if(n.fillStyle=c.fill,n.globalAlpha=c.fillOpacity||1,b.forEach(({x:S,y:C,width:M,height:B})=>n.fillRect(S,C,M,B)),c.underlineColor){n.globalAlpha=1,n.strokeStyle=c.underlineColor,n.lineWidth=c.underlineThickness??1;const S=c.underlineOffset??0;b.forEach(({x:C,y:M,width:B,height:T})=>{n.beginPath(),n.moveTo(C,M+T+S),n.lineTo(C+B,M+T+S),n.stroke()})}})}),i=bt(()=>{nn(e)});return window.addEventListener("resize",i),{destroy:()=>{e.remove(),window.removeEventListener("resize",i)},setVisible:r=>{console.log("setVisible not implemented on Canvas renderer")},redraw:o}},se=(t,e,n)=>Rt(t,e,n,on(t));var sn={grad:.9,turn:360,rad:360/(2*Math.PI)},G=function(t){return typeof t=="string"?t.length>0:typeof t=="number"},V=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},re=function(t){return(t=isFinite(t)?t%360:0)>0?t:t+360},ae=function(t){return{r:$(t.r,0,255),g:$(t.g,0,255),b:$(t.b,0,255),a:$(t.a)}},Mt=function(t){return{r:V(t.r),g:V(t.g),b:V(t.b),a:V(t.a,3)}},rn=/^#([0-9a-f]{3,8})$/i,Et=function(t){var e=t.toString(16);return e.length<2?"0"+e:e},ce=function(t){var e=t.r,n=t.g,o=t.b,i=t.a,s=Math.max(e,n,o),a=s-Math.min(e,n,o),r=a?s===e?(n-o)/a:s===n?2+(o-e)/a:4+(e-n)/a:0;return{h:60*(r<0?r+6:r),s:s?a/s*100:0,v:s/255*100,a:i}},le=function(t){var e=t.h,n=t.s,o=t.v,i=t.a;e=e/360*6,n/=100,o/=100;var s=Math.floor(e),a=o*(1-n),r=o*(1-(e-s)*n),l=o*(1-(1-e+s)*n),f=s%6;return{r:255*[o,r,a,a,l,o][f],g:255*[l,o,o,r,a,a][f],b:255*[a,a,l,o,o,r][f],a:i}},de=function(t){return{h:re(t.h),s:$(t.s,0,100),l:$(t.l,0,100),a:$(t.a)}},ue=function(t){return{h:V(t.h),s:V(t.s),l:V(t.l),a:V(t.a,3)}},fe=function(t){return le((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},ct=function(t){return{h:(e=ce(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},an=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*,\s*([+-]?\d*\.?\d+)%\s*,\s*([+-]?\d*\.?\d+)%\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,cn=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,ln=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,dn=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,he={string:[[function(t){var e=rn.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?V(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?V(parseInt(t.substr(6,2),16)/255,2):1}:null:null},"hex"],[function(t){var e=ln.exec(t)||dn.exec(t);return e?e[2]!==e[4]||e[4]!==e[6]?null:ae({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=an.exec(t)||cn.exec(t);if(!e)return null;var n,o,i=de({h:(n=e[1],o=e[2],o===void 0&&(o="deg"),Number(n)*(sn[o]||1)),s:Number(e[3]),l:Number(e[4]),a:e[5]===void 0?1:Number(e[5])/(e[6]?100:1)});return fe(i)},"hsl"]],object:[[function(t){var e=t.r,n=t.g,o=t.b,i=t.a,s=i===void 0?1:i;return G(e)&&G(n)&&G(o)?ae({r:Number(e),g:Number(n),b:Number(o),a:Number(s)}):null},"rgb"],[function(t){var e=t.h,n=t.s,o=t.l,i=t.a,s=i===void 0?1:i;if(!G(e)||!G(n)||!G(o))return null;var a=de({h:Number(e),s:Number(n),l:Number(o),a:Number(s)});return fe(a)},"hsl"],[function(t){var e=t.h,n=t.s,o=t.v,i=t.a,s=i===void 0?1:i;if(!G(e)||!G(n)||!G(o))return null;var a=function(r){return{h:re(r.h),s:$(r.s,0,100),v:$(r.v,0,100),a:$(r.a)}}({h:Number(e),s:Number(n),v:Number(o),a:Number(s)});return le(a)},"hsv"]]},ge=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]},un=function(t){return typeof t=="string"?ge(t.trim(),he.string):typeof t=="object"&&t!==null?ge(t,he.object):[null,void 0]},Bt=function(t,e){var n=ct(t);return{h:n.h,s:$(n.s+100*e,0,100),l:n.l,a:n.a}},kt=function(t){return(299*t.r+587*t.g+114*t.b)/1e3/255},pe=function(t,e){var n=ct(t);return{h:n.h,s:n.s,l:$(n.l+100*e,0,100),a:n.a}},me=function(){function t(e){this.parsed=un(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 V(kt(this.rgba),2)},t.prototype.isDark=function(){return kt(this.rgba)<.5},t.prototype.isLight=function(){return kt(this.rgba)>=.5},t.prototype.toHex=function(){return e=Mt(this.rgba),n=e.r,o=e.g,i=e.b,a=(s=e.a)<1?Et(V(255*s)):"","#"+Et(n)+Et(o)+Et(i)+a;var e,n,o,i,s,a},t.prototype.toRgb=function(){return Mt(this.rgba)},t.prototype.toRgbString=function(){return e=Mt(this.rgba),n=e.r,o=e.g,i=e.b,(s=e.a)<1?"rgba("+n+", "+o+", "+i+", "+s+")":"rgb("+n+", "+o+", "+i+")";var e,n,o,i,s},t.prototype.toHsl=function(){return ue(ct(this.rgba))},t.prototype.toHslString=function(){return e=ue(ct(this.rgba)),n=e.h,o=e.s,i=e.l,(s=e.a)<1?"hsla("+n+", "+o+"%, "+i+"%, "+s+")":"hsl("+n+", "+o+"%, "+i+"%)";var e,n,o,i,s},t.prototype.toHsv=function(){return e=ce(this.rgba),{h:V(e.h),s:V(e.s),v:V(e.v),a:V(e.a,3)};var e},t.prototype.invert=function(){return F({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),F(Bt(this.rgba,e))},t.prototype.desaturate=function(e){return e===void 0&&(e=.1),F(Bt(this.rgba,-e))},t.prototype.grayscale=function(){return F(Bt(this.rgba,-1))},t.prototype.lighten=function(e){return e===void 0&&(e=.1),F(pe(this.rgba,e))},t.prototype.darken=function(e){return e===void 0&&(e=.1),F(pe(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"?F({r:(n=this.rgba).r,g:n.g,b:n.b,a:e}):V(this.rgba.a,3);var n},t.prototype.hue=function(e){var n=ct(this.rgba);return typeof e=="number"?F({h:e,s:n.s,l:n.l,a:n.a}):V(n.h)},t.prototype.isEqual=function(e){return this.toHex()===F(e).toHex()},t}(),F=function(t){return t instanceof me?t:new me(t)};const fn=t=>[`background-color:${F((t==null?void 0:t.fill)||q.fill).alpha((t==null?void 0:t.fillOpacity)===void 0?q.fillOpacity:t.fillOpacity).toHex()}`,t!=null&&t.underlineThickness?"text-decoration:underline":void 0,t!=null&&t.underlineColor?`text-decoration-color:${t.underlineColor}`:void 0,t!=null&&t.underlineOffset?`text-underline-offset:${t.underlineOffset}px`:void 0,t!=null&&t.underlineThickness?`text-decoration-thickness:${t.underlineThickness}px`:void 0].filter(Boolean).join(";"),ye=()=>{const t=document.createElement("style");document.getElementsByTagName("head")[0].appendChild(t);let e=new Set;return{destroy:()=>{CSS.highlights.clear(),t.remove()},setVisible:s=>{console.log("setVisible not implemented on CSS Custom Highlights renderer")},redraw:(s,a,r,l)=>{l&&l.clear();const f=new Set(s.map(u=>u.annotation.id));Array.from(e).filter(u=>!f.has(u));const h=s.map(u=>{var p;const g=r?typeof r=="function"?r(u.annotation,u.state):r:(p=u.state)!=null&&p.selected?at:q,v=l&&l.paint(u,a)||g;return`::highlight(_${u.annotation.id}) { ${fn(v)} }`});t.innerHTML=h.join(`
2
- `),CSS.highlights.clear(),s.forEach(({annotation:u})=>{const g=u.target.selector.map(p=>p.range),v=new Highlight(...g);CSS.highlights.set(`_${u.id}`,v)}),e=f}}},be=(t,e,n)=>Rt(t,e,n,ye());var Ae=Object.prototype.hasOwnProperty;function It(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--&&It(t[o],e[o]););return o===-1}if(!n||typeof t=="object"){o=0;for(n in t)if(Ae.call(t,n)&&++o&&!Ae.call(e,n)||!(n in e)||!It(t[n],e[n]))return!1;return Object.keys(e).length===o}}return t!==t&&e!==e}const hn=(t,e)=>{const n=(s,a)=>s.x<=a.x+a.width&&s.x+s.width>=a.x&&s.y<=a.y+a.height&&s.y+s.height>=a.y,o=s=>s.rects.reduce((a,r)=>a+r.width,0),i=e.filter(({rects:s})=>s.some(a=>n(t,a)));return i.sort((s,a)=>o(a)-o(s)),i.findIndex(s=>s.rects.includes(t))},gn=t=>{t.classList.add("r6o-annotatable");const e=document.createElement("div");e.className="r6o-span-highlight-layer",t.insertBefore(e,t.firstChild);let n=[];return{destroy:()=>{e.remove()},redraw:(a,r,l,f,h)=>{const g=!(It(n,a)&&h);if(!f&&!g)return;g&&(e.innerHTML=""),[...a].sort((p,m)=>{const{annotation:{target:{created:w}}}=p,{annotation:{target:{created:d}}}=m;return w&&d?w.getTime()-d.getTime():0}).forEach(p=>{p.rects.map(m=>{const w=hn(m,a),d=ie(p,r,l,f,w);if(g){const c=document.createElement("span");c.className="r6o-annotation",c.dataset.annotation=p.annotation.id,c.style.left=`${m.x}px`,c.style.top=`${m.y}px`,c.style.width=`${m.width}px`,c.style.height=`${m.height}px`,c.style.backgroundColor=F((d==null?void 0:d.fill)||q.fill).alpha((d==null?void 0:d.fillOpacity)===void 0?q.fillOpacity:d.fillOpacity).toHex(),d.underlineStyle&&(c.style.borderStyle=d.underlineStyle),d.underlineColor&&(c.style.borderColor=d.underlineColor),d.underlineThickness&&(c.style.borderBottomWidth=`${d.underlineThickness}px`),d.underlineOffset&&(c.style.paddingBottom=`${d.underlineOffset}px`),e.appendChild(c)}})}),n=a},setVisible:a=>{a?e.classList.remove("hidden"):e.classList.add("hidden")}}},we=(t,e,n)=>Rt(t,e,n,gn(t)),Y=[];for(let t=0;t<256;++t)Y.push((t+256).toString(16).slice(1));function pn(t,e=0){return(Y[t[e+0]]+Y[t[e+1]]+Y[t[e+2]]+Y[t[e+3]]+"-"+Y[t[e+4]]+Y[t[e+5]]+"-"+Y[t[e+6]]+Y[t[e+7]]+"-"+Y[t[e+8]]+Y[t[e+9]]+"-"+Y[t[e+10]]+Y[t[e+11]]+Y[t[e+12]]+Y[t[e+13]]+Y[t[e+14]]+Y[t[e+15]]).toLowerCase()}let _t;const mn=new Uint8Array(16);function yn(){if(!_t){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");_t=crypto.getRandomValues.bind(crypto)}return _t(mn)}const Ee={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function ve(t,e,n){var i;if(Ee.randomUUID&&!t)return Ee.randomUUID();t=t||{};const o=t.random??((i=t.rng)==null?void 0:i.call(t))??yn();if(o.length<16)throw new Error("Random bytes length must be >= 16");return o[6]=o[6]&15|64,o[8]=o[8]&63|128,pn(o)}var Se=Object.prototype.hasOwnProperty;function tt(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--&&tt(t[o],e[o]););return o===-1}if(!n||typeof t=="object"){o=0;for(n in t)if(Se.call(t,n)&&++o&&!Se.call(e,n)||!(n in e)||!tt(t[n],e[n]))return!1;return Object.keys(e).length===o}}return t!==t&&e!==e}function Nt(){}function bn(t,e){return t!=t?e==e:t!==e||t&&typeof t=="object"||typeof t=="function"}const nt=[];function Ut(t,e=Nt){let n;const o=new Set;function i(r){if(bn(t,r)&&(t=r,n)){const l=!nt.length;for(const f of o)f[1](),nt.push(f,t);if(l){for(let f=0;f<nt.length;f+=2)nt[f][0](nt[f+1]);nt.length=0}}}function s(r){i(r(t))}function a(r,l=Nt){const f=[r,l];return o.add(f),o.size===1&&(n=e(i,s)||Nt),r(t),()=>{o.delete(f),o.size===0&&n&&(n(),n=null)}}return{set:i,update:s,subscribe:a}}const An=t=>{const{subscribe:e,set:n}=Ut();let o;return e(i=>o=i),t.observe(({changes:i})=>{if(o){(i.deleted||[]).some(a=>a.id===o)&&n(void 0);const s=(i.updated||[]).find(({oldValue:a})=>a.id===o);s&&n(s.newValue.id)}}),{get current(){return o},subscribe:e,set:n}};var xe=(t=>(t.EDIT="EDIT",t.SELECT="SELECT",t.NONE="NONE",t))(xe||{});const vt={selected:[]},wn=(t,e,n)=>{const{subscribe:o,set:i}=Ut(vt);let s=e,a=vt;o(p=>a=p);const r=()=>{tt(a,vt)||i(vt)},l=()=>{var p;return((p=a.selected)==null?void 0:p.length)===0},f=p=>{if(l())return!1;const m=typeof p=="string"?p:p.id;return a.selected.some(w=>w.id===m)},h=(p,m)=>{let w;if(Array.isArray(p)){if(w=p.map(c=>t.getAnnotation(c)).filter(Boolean),w.length<p.length){console.warn("Invalid selection: "+p.filter(c=>!w.some(b=>b.id===c)));return}}else{const c=t.getAnnotation(p);if(!c){console.warn("Invalid selection: "+p);return}w=[c]}const d=w.reduce((c,b)=>{const x=Ce(b,s);return x==="EDIT"?[...c,{id:b.id,editable:!0}]:x==="SELECT"?[...c,{id:b.id}]:c},[]);i({selected:d,event:m})},u=(p,m)=>{const w=Array.isArray(p)?p:[p],d=w.map(c=>t.getAnnotation(c)).filter(c=>!!c);i({selected:d.map(c=>{const b=m===void 0?Ce(c,s)==="EDIT":m;return{id:c.id,editable:b}})}),d.length!==w.length&&console.warn("Invalid selection",p)},g=p=>{if(l())return!1;const{selected:m}=a;m.some(({id:w})=>p.includes(w))&&i({selected:m.filter(({id:w})=>!p.includes(w))})},v=p=>{s=p,u(a.selected.map(({id:m})=>m))};return t.observe(({changes:p})=>g((p.deleted||[]).map(m=>m.id))),{get event(){return a?a.event:null},get selected(){return a?[...a.selected]:null},get userSelectAction(){return s},clear:r,isEmpty:l,isSelected:f,setSelected:u,setUserSelectAction:v,subscribe:o,userSelect:h}},Ce=(t,e,n)=>typeof e=="function"?e(t):e||"EDIT",K=[];for(let t=0;t<256;++t)K.push((t+256).toString(16).slice(1));function En(t,e=0){return(K[t[e+0]]+K[t[e+1]]+K[t[e+2]]+K[t[e+3]]+"-"+K[t[e+4]]+K[t[e+5]]+"-"+K[t[e+6]]+K[t[e+7]]+"-"+K[t[e+8]]+K[t[e+9]]+"-"+K[t[e+10]]+K[t[e+11]]+K[t[e+12]]+K[t[e+13]]+K[t[e+14]]+K[t[e+15]]).toLowerCase()}let Dt;const vn=new Uint8Array(16);function Sn(){if(!Dt){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");Dt=crypto.getRandomValues.bind(crypto)}return Dt(vn)}const xn=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),Le={randomUUID:xn};function Te(t,e,n){var o;if(Le.randomUUID&&!t)return Le.randomUUID();t=t||{};const i=t.random??((o=t.rng)==null?void 0:o.call(t))??Sn();if(i.length<16)throw new Error("Random bytes length must be >= 16");return i[6]=i[6]&15|64,i[8]=i[8]&63|128,En(i)}const Vt=t=>{const e=n=>{const o={...n};return n.created&&typeof n.created=="string"&&(o.created=new Date(n.created)),n.updated&&typeof n.updated=="string"&&(o.updated=new Date(n.updated)),o};return{...t,bodies:(t.bodies||[]).map(e),target:e(t.target)}},Cn=(t,e,n,o)=>({id:Te(),annotation:typeof t=="string"?t:t.id,created:n||new Date,creator:o,...e}),Ln=(t,e)=>{const n=new Set(t.bodies.map(o=>o.id));return e.bodies.filter(o=>!n.has(o.id))},Tn=(t,e)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},On=(t,e)=>e.bodies.map(n=>{const o=t.bodies.find(i=>i.id===n.id);return{newBody:n,oldBody:o&&!tt(o,n)?o:void 0}}).filter(({oldBody:n})=>n).map(({oldBody:n,newBody:o})=>({oldBody:n,newBody:o})),Rn=(t,e)=>!tt(t.target,e.target),Oe=(t,e)=>{const n=Ln(t,e),o=Tn(t,e),i=On(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:Rn(t,e)?{oldTarget:t.target,newTarget:e.target}:void 0}};var k=(t=>(t.LOCAL="LOCAL",t.REMOTE="REMOTE",t.SILENT="SILENT",t))(k||{});const Mn=(t,e)=>{var n,o;const{changes:i,origin:s}=e;if(!(t.options.origin?t.options.origin===s:s!=="SILENT"))return!1;if(t.options.ignore){const{ignore:a}=t.options,r=l=>l&&l.length>0;if(!(r(i.created)||r(i.deleted))){const l=(n=i.updated)==null?void 0:n.some(h=>r(h.bodiesCreated)||r(h.bodiesDeleted)||r(h.bodiesUpdated)),f=(o=i.updated)==null?void 0:o.some(h=>h.targetUpdated);if(a==="BODY_ONLY"&&l&&!f||a==="TARGET_ONLY"&&f&&!l)return!1}}if(t.options.annotations){const a=new Set([...(i.created||[]).map(r=>r.id),...(i.deleted||[]).map(r=>r.id),...(i.updated||[]).map(({oldValue:r})=>r.id)]);return!!(Array.isArray(t.options.annotations)?t.options.annotations:[t.options.annotations]).find(r=>a.has(r))}else return!0},Bn=(t,e)=>{const n=new Set((t.created||[]).map(u=>u.id)),o=new Set((t.updated||[]).map(({newValue:u})=>u.id)),i=new Set((e.created||[]).map(u=>u.id)),s=new Set((e.deleted||[]).map(u=>u.id)),a=new Set((e.updated||[]).map(({oldValue:u})=>u.id)),r=new Set((e.updated||[]).filter(({oldValue:u})=>n.has(u.id)||o.has(u.id)).map(({oldValue:u})=>u.id)),l=[...(t.created||[]).filter(u=>!s.has(u.id)).map(u=>a.has(u.id)?e.updated.find(({oldValue:g})=>g.id===u.id).newValue:u),...e.created||[]],f=[...(t.deleted||[]).filter(u=>!i.has(u.id)),...(e.deleted||[]).filter(u=>!n.has(u.id))],h=[...(t.updated||[]).filter(({newValue:u})=>!s.has(u.id)).map(u=>{const{oldValue:g,newValue:v}=u;if(a.has(v.id)){const p=e.updated.find(m=>m.oldValue.id===v.id).newValue;return Oe(g,p)}else return u}),...(e.updated||[]).filter(({oldValue:u})=>!r.has(u.id))];return{created:l,deleted:f,updated:h}},St=t=>{const e=t.id===void 0?Te():t.id;return{...t,id:e,bodies:t.bodies===void 0?[]:t.bodies.map(n=>({...n,annotation:e})),target:{...t.target,annotation:e}}},kn=t=>t.id!==void 0,In=()=>{const t=new Map,e=new Map,n=[],o=(y,E={})=>{n.push({onChange:y,options:E})},i=y=>{const E=n.findIndex(A=>A.onChange==y);E>-1&&n.splice(E,1)},s=(y,E)=>{const A={origin:y,changes:{created:E.created||[],updated:E.updated||[],deleted:E.deleted||[]},state:[...t.values()]};n.forEach(L=>{Mn(L,A)&&L.onChange(A)})},a=(y,E=k.LOCAL)=>{if(y.id&&t.get(y.id))throw Error(`Cannot add annotation ${y.id} - exists already`);{const A=St(y);t.set(A.id,A),A.bodies.forEach(L=>e.set(L.id,A.id)),s(E,{created:[A]})}},r=(y,E)=>{const A=St(typeof y=="string"?E:y),L=typeof y=="string"?y:y.id,I=L&&t.get(L);if(I){const R=Oe(I,A);return L===A.id?t.set(L,A):(t.delete(L),t.set(A.id,A)),I.bodies.forEach(N=>e.delete(N.id)),A.bodies.forEach(N=>e.set(N.id,A.id)),R}else console.warn(`Cannot update annotation ${L} - does not exist`)},l=(y,E=k.LOCAL,A=k.LOCAL)=>{const L=kn(E)?A:E,I=r(y,E);I&&s(L,{updated:[I]})},f=(y,E=k.LOCAL)=>{t.get(y.id)?l(y,E):a(y,E)},h=(y,E=k.LOCAL)=>{const A=y.reduce((L,I)=>{const R=r(I);return R?[...L,R]:L},[]);A.length>0&&s(E,{updated:A})},u=(y,E=k.LOCAL)=>{const A=y.map(St),{toAdd:L,toUpdate:I}=A.reduce((N,H)=>t.get(H.id)?{...N,toUpdate:[...N.toUpdate,H]}:{...N,toAdd:[...N.toAdd,H]},{toAdd:[],toUpdate:[]}),R=I.map(N=>r(N,E)).filter(Boolean);L.forEach(N=>{t.set(N.id,N),N.bodies.forEach(H=>e.set(H.id,N.id))}),s(E,{created:L,updated:R})},g=(y,E=k.LOCAL)=>{const A=t.get(y.annotation);if(A){const L={...A,bodies:[...A.bodies,y]};t.set(A.id,L),e.set(y.id,L.id),s(E,{updated:[{oldValue:A,newValue:L,bodiesCreated:[y]}]})}else console.warn(`Attempt to add body to missing annotation: ${y.annotation}`)},v=()=>[...t.values()],p=(y=k.LOCAL)=>{const E=[...t.values()];t.clear(),e.clear(),s(y,{deleted:E})},m=(y,E=!0,A=k.LOCAL)=>{const L=y.map(St);if(E){const I=[...t.values()];t.clear(),e.clear(),L.forEach(R=>{t.set(R.id,R),R.bodies.forEach(N=>e.set(N.id,R.id))}),s(A,{created:L,deleted:I})}else{const I=y.reduce((R,N)=>{const H=N.id&&t.get(N.id);return H?[...R,H]:R},[]);if(I.length>0)throw Error(`Bulk insert would overwrite the following annotations: ${I.map(R=>R.id).join(", ")}`);L.forEach(R=>{t.set(R.id,R),R.bodies.forEach(N=>e.set(N.id,R.id))}),s(A,{created:L})}},w=y=>{const E=typeof y=="string"?y:y.id,A=t.get(E);if(A)return t.delete(E),A.bodies.forEach(L=>e.delete(L.id)),A;console.warn(`Attempt to delete missing annotation: ${E}`)},d=(y,E=k.LOCAL)=>{const A=w(y);A&&s(E,{deleted:[A]})},c=(y,E=k.LOCAL)=>{const A=y.reduce((L,I)=>{const R=w(I);return R?[...L,R]:L},[]);A.length>0&&s(E,{deleted:A})},b=y=>{const E=t.get(y.annotation);if(E){const A=E.bodies.find(L=>L.id===y.id);if(A){e.delete(A.id);const L={...E,bodies:E.bodies.filter(I=>I.id!==y.id)};return t.set(E.id,L),{oldValue:E,newValue:L,bodiesDeleted:[A]}}else console.warn(`Attempt to delete missing body ${y.id} from annotation ${y.annotation}`)}else console.warn(`Attempt to delete body from missing annotation ${y.annotation}`)},x=(y,E=k.LOCAL)=>{const A=b(y);A&&s(E,{updated:[A]})},S=(y,E=k.LOCAL)=>{const A=y.map(L=>b(L)).filter(Boolean);A.length>0&&s(E,{updated:A})},C=y=>{const E=t.get(y);return E?{...E}:void 0},M=y=>{const E=e.get(y);if(E){const A=C(E).bodies.find(L=>L.id===y);if(A)return A;console.error(`Store integrity error: body ${y} in index, but not in annotation`)}else console.warn(`Attempt to retrieve missing body: ${y}`)},B=(y,E)=>{if(y.annotation!==E.annotation)throw"Annotation integrity violation: annotation ID must be the same when updating bodies";const A=t.get(y.annotation);if(A){const L=A.bodies.find(R=>R.id===y.id),I={...A,bodies:A.bodies.map(R=>R.id===L.id?E:R)};return t.set(A.id,I),L.id!==E.id&&(e.delete(L.id),e.set(E.id,I.id)),{oldValue:A,newValue:I,bodiesUpdated:[{oldBody:L,newBody:E}]}}else console.warn(`Attempt to add body to missing annotation ${y.annotation}`)},T=(y,E,A=k.LOCAL)=>{const L=B(y,E);L&&s(A,{updated:[L]})},_=(y,E=k.LOCAL)=>{const A=y.map(L=>B({id:L.id,annotation:L.annotation},L)).filter(Boolean);s(E,{updated:A})},P=y=>{const E=t.get(y.annotation);if(E){const A={...E,target:{...E.target,...y}};return t.set(E.id,A),{oldValue:E,newValue:A,targetUpdated:{oldTarget:E.target,newTarget:y}}}else console.warn(`Attempt to update target on missing annotation: ${y.annotation}`)};return{addAnnotation:a,addBody:g,all:v,bulkAddAnnotations:m,bulkDeleteAnnotations:c,bulkDeleteBodies:S,bulkUpdateAnnotations:h,bulkUpdateBodies:_,bulkUpdateTargets:(y,E=k.LOCAL)=>{const A=y.map(L=>P(L)).filter(Boolean);A.length>0&&s(E,{updated:A})},bulkUpsertAnnotations:u,clear:p,deleteAnnotation:d,deleteBody:x,getAnnotation:C,getBody:M,observe:o,unobserve:i,updateAnnotation:l,updateBody:T,updateTarget:(y,E=k.LOCAL)=>{const A=P(y);A&&s(E,{updated:[A]})},upsertAnnotation:f}};let _n=()=>({emit(t,...e){for(let n=this.events[t]||[],o=0,i=n.length;o<i;o++)n[o](...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 Nn=250,Un=(t,e)=>{const n=_n(),o=[];let i=-1,s=!1,a=0;const r=p=>{if(!s){const{changes:m}=p,w=performance.now();if(w-a>Nn)o.splice(i+1),o.push(m),i=o.length-1;else{const d=o.length-1;o[d]=Bn(o[d],m)}a=w}s=!1};t.observe(r,{origin:k.LOCAL});const l=p=>p&&p.length>0&&t.bulkDeleteAnnotations(p),f=p=>p&&p.length>0&&t.bulkAddAnnotations(p,!1),h=p=>p&&p.length>0&&t.bulkUpdateAnnotations(p.map(({oldValue:m})=>m)),u=p=>p&&p.length>0&&t.bulkUpdateAnnotations(p.map(({newValue:m})=>m)),g=p=>p&&p.length>0&&t.bulkAddAnnotations(p,!1),v=p=>p&&p.length>0&&t.bulkDeleteAnnotations(p);return{canRedo:()=>o.length-1>i,canUndo:()=>i>-1,destroy:()=>t.unobserve(r),getHistory:()=>({changes:[...o],pointer:i}),on:(p,m)=>n.on(p,m),redo:()=>{if(o.length-1>i){s=!0;const{created:p,updated:m,deleted:w}=o[i+1];f(p),u(m),v(w),n.emit("redo",o[i+1]),i+=1}},undo:()=>{if(i>-1){s=!0;const{created:p,updated:m,deleted:w}=o[i];l(p),h(m),g(w),n.emit("undo",o[i]),i-=1}}}},Dn=()=>{const{subscribe:t,set:e}=Ut([]);return{subscribe:t,set:e}},Vn=(t,e,n,o)=>{const{hover:i,selection:s,store:a,viewport:r}=t,l=new Map;let f=[],h;const u=(m,w)=>{l.has(m)?l.get(m).push(w):l.set(m,[w])},g=(m,w)=>{const d=l.get(m);if(d){const c=d.indexOf(w);c!==-1&&d.splice(c,1)}},v=(m,w,d)=>{l.has(m)&&setTimeout(()=>{l.get(m).forEach(c=>{if(n){const b=Array.isArray(w)?w.map(S=>n.serialize(S)):n.serialize(w),x=d?d instanceof PointerEvent?d:n.serialize(d):void 0;c(b,x)}else c(w,d)})},1)};s.subscribe(({selected:m})=>{if(!(f.length===0&&m.length===0)){if(f.length===0&&m.length>0)f=m.map(({id:w})=>a.getAnnotation(w));else if(f.length>0&&m.length===0)f.forEach(w=>{const d=a.getAnnotation(w.id);d&&!tt(d,w)&&v("updateAnnotation",d,w)}),f=[];else{const w=new Set(f.map(c=>c.id)),d=new Set(m.map(({id:c})=>c));f.filter(c=>!d.has(c.id)).forEach(c=>{const b=a.getAnnotation(c.id);b&&!tt(b,c)&&v("updateAnnotation",b,c)}),f=[...f.filter(c=>d.has(c.id)),...m.filter(({id:c})=>!w.has(c)).map(({id:c})=>a.getAnnotation(c))]}v("selectionChanged",f)}}),i.subscribe(m=>{!h&&m?v("mouseEnterAnnotation",a.getAnnotation(m)):h&&!m?v("mouseLeaveAnnotation",a.getAnnotation(h)):h&&m&&(v("mouseLeaveAnnotation",a.getAnnotation(h)),v("mouseEnterAnnotation",a.getAnnotation(m))),h=m}),r==null||r.subscribe(m=>v("viewportIntersect",m.map(w=>a.getAnnotation(w)))),a.observe(m=>{const{created:w,deleted:d}=m.changes;(w||[]).forEach(c=>v("createAnnotation",c)),(d||[]).forEach(c=>v("deleteAnnotation",c)),(m.changes.updated||[]).filter(c=>[...c.bodiesCreated||[],...c.bodiesDeleted||[],...c.bodiesUpdated||[]].length>0).forEach(({oldValue:c,newValue:b})=>{const x=f.find(S=>S.id===c.id)||c;f=f.map(S=>S.id===c.id?b:S),v("updateAnnotation",b,x)})},{origin:k.LOCAL}),a.observe(m=>{if(f){const w=new Set(f.map(c=>c.id)),d=(m.changes.updated||[]).filter(({newValue:c})=>w.has(c.id)).map(({newValue:c})=>c);d.length>0&&(f=f.map(c=>d.find(x=>x.id===c.id)||c))}},{origin:k.REMOTE});const p=m=>w=>{const{updated:d}=w;m?(d||[]).forEach(c=>v("updateAnnotation",c.oldValue,c.newValue)):(d||[]).forEach(c=>v("updateAnnotation",c.newValue,c.oldValue))};return e.on("undo",p(!0)),e.on("redo",p(!1)),{on:u,off:g,emit:v}},Yn=t=>e=>e.reduce((n,o)=>{const{parsed:i,error:s}=t.parse(o);return s?{parsed:n.parsed,failed:[...n.failed,o]}:i?{parsed:[...n.parsed,i],failed:n.failed}:{...n}},{parsed:[],failed:[]}),Kn=(t,e,n)=>{const{store:o,selection:i}=t,s=d=>{if(n){const{parsed:c,error:b}=n.parse(d);c?o.addAnnotation(c,k.REMOTE):console.error(b)}else o.addAnnotation(Vt(d),k.REMOTE)},a=()=>i.clear(),r=()=>o.clear(),l=d=>{const c=o.getAnnotation(d);return n&&c?n.serialize(c):c},f=()=>n?o.all().map(n.serialize):o.all(),h=()=>{var d;const c=(((d=i.selected)==null?void 0:d.map(b=>b.id))||[]).map(b=>o.getAnnotation(b)).filter(Boolean);return n?c.map(n.serialize):c},u=(d,c=!0)=>fetch(d).then(b=>b.json()).then(b=>(v(b,c),b)),g=d=>{if(typeof d=="string"){const c=o.getAnnotation(d);if(o.deleteAnnotation(d),c)return n?n.serialize(c):c}else{const c=n?n.parse(d).parsed:d;if(c)return o.deleteAnnotation(c),d}},v=(d,c=!0)=>{if(n){const b=n.parseAll||Yn(n),{parsed:x,failed:S}=b(d);S.length>0&&console.warn(`Discarded ${S.length} invalid annotations`,S),o.bulkAddAnnotations(x,c,k.REMOTE)}else o.bulkAddAnnotations(d.map(Vt),c,k.REMOTE)},p=(d,c)=>{d?i.setSelected(d,c):i.clear()},m=d=>{i.clear(),i.setUserSelectAction(d)},w=d=>{if(n){const c=n.parse(d).parsed,b=n.serialize(o.getAnnotation(c.id));return o.updateAnnotation(c),b}else{const c=o.getAnnotation(d.id);return o.updateAnnotation(Vt(d)),c}};return{addAnnotation:s,cancelSelected:a,canRedo:e.canRedo,canUndo:e.canUndo,clearAnnotations:r,getAnnotationById:l,getAnnotations:f,getHistory:e.getHistory,getSelected:h,loadAnnotations:u,redo:e.redo,removeAnnotation:g,setAnnotations:v,setSelected:p,setUserSelectAction:m,undo:e.undo,updateAnnotation:w}},Xn="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let Pn=t=>crypto.getRandomValues(new Uint8Array(t)),Hn=(t,e,n)=>{let o=(2<<Math.log2(t.length-1))-1,i=-~(1.6*o*e/t.length);return(s=e)=>{let a="";for(;;){let r=n(i),l=i|0;for(;l--;)if(a+=t[r[l]&o]||"",a.length>=s)return a}}},$n=(t,e=21)=>Hn(t,e|0,Pn),jn=(t=21)=>{let e="",n=crypto.getRandomValues(new Uint8Array(t|=0));for(;t--;)e+=Xn[n[t]&63];return e};const Fn=()=>({isGuest:!0,id:$n("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",20)()}),zn=t=>{const e=JSON.stringify(t);let n=0;for(let o=0,i=e.length;o<i;o++){let s=e.charCodeAt(o);n=(n<<5)-n+s,n|=0}return`${n}`},Re=t=>t?typeof t=="object"?{...t}:t:void 0,Wn=(t,e)=>(Array.isArray(t)?t:[t]).map(n=>{const{id:o,type:i,purpose:s,value:a,created:r,modified:l,creator:f,...h}=n;return{id:o||`temp-${zn(n)}`,annotation:e,type:i,purpose:s,value:a,creator:Re(f),created:r?new Date(r):void 0,updated:l?new Date(l):void 0,...h}}),qn=t=>t.map(e=>{var n;const{annotation:o,created:i,updated:s,...a}=e,r={...a,created:i==null?void 0:i.toISOString(),modified:s==null?void 0:s.toISOString()};return(n=r.id)!=null&&n.startsWith("temp-")&&delete r.id,r});jn();const Gn=(t,e)=>({parse:n=>Me(n),serialize:n=>Be(n,t,e)}),Qn=t=>t.quote!==void 0&&t.start!==void 0&&t.end!==void 0,Jn=t=>{const{id:e,creator:n,created:o,modified:i,target:s}=t,a=Array.isArray(s)?s:[s];if(a.length===0)return{error:Error(`No targets found for annotation: ${t.id}`)};const r={creator:Re(n),created:o?new Date(o):void 0,updated:i?new Date(i):void 0,annotation:e,selector:[],styleClass:"styleClass"in a[0]?a[0].styleClass:void 0};for(const l of a){const h=(Array.isArray(l.selector)?l.selector:[l.selector]).reduce((u,g)=>{switch(g.type){case"TextQuoteSelector":u.quote=g.exact;break;case"TextPositionSelector":u.start=g.start,u.end=g.end;break}return u},{});if(Qn(h))r.selector.push({...h,id:l.id,scope:l.scope});else{const u=[h.start?void 0:"TextPositionSelector",h.quote?void 0:"TextQuoteSelector"].filter(Boolean);return{error:Error(`Missing selector types: ${u.join(" and ")} for annotation: ${t.id}`)}}}return{parsed:r}},Me=t=>{const e=t.id||ve(),{creator:n,created:o,modified:i,body:s,...a}=t,r=Wn(s,e),l=Jn(t);return"error"in l?{error:l.error}:{parsed:{...a,id:e,bodies:r,target:l.parsed}}},Be=(t,e,n)=>{const{bodies:o,target:i,...s}=t,{selector:a,creator:r,created:l,updated:f,...h}=i,u=a.map(g=>{const{id:v,quote:p,start:m,end:w,range:d}=g,c={type:"TextQuoteSelector",exact:p};if(n){const{prefix:x,suffix:S}=Gt(d,n);c.prefix=x,c.suffix=S}const b={type:"TextPositionSelector",start:m,end:w};return{...h,id:v,scope:"scope"in g?g.scope:void 0,source:e,selector:[c,b]}});return{...s,"@context":"http://www.w3.org/ns/anno.jsonld",id:t.id,type:"Annotation",body:qn(t.bodies),creator:r,created:l==null?void 0:l.toISOString(),modified:f==null?void 0:f.toISOString(),target:u}};function ke(t,e,n=0,o=t.length-1,i=Zn){for(;o>n;){if(o-n>600){const l=o-n+1,f=e-n+1,h=Math.log(l),u=.5*Math.exp(2*h/3),g=.5*Math.sqrt(h*u*(l-u)/l)*(f-l/2<0?-1:1),v=Math.max(n,Math.floor(e-f*u/l+g)),p=Math.min(o,Math.floor(e+(l-f)*u/l+g));ke(t,e,v,p,i)}const s=t[e];let a=n,r=o;for(lt(t,n,e),i(t[o],s)>0&&lt(t,n,o);a<r;){for(lt(t,a,r),a++,r--;i(t[a],s)<0;)a++;for(;i(t[r],s)>0;)r--}i(t[n],s)===0?lt(t,n,r):(r++,lt(t,r,o)),r<=e&&(n=r+1),e<=r&&(o=r-1)}}function lt(t,e,n){const o=t[e];t[e]=t[n],t[n]=o}function Zn(t,e){return t<e?-1:t>e?1:0}class to{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(!Ct(e,n))return o;const i=this.toBBox,s=[];for(;n;){for(let a=0;a<n.children.length;a++){const r=n.children[a],l=n.leaf?i(r):r;Ct(e,l)&&(n.leaf?o.push(r):Kt(e,l)?this._all(r,o):s.push(r))}n=s.pop()}return o}collides(e){let n=this.data;if(!Ct(e,n))return!1;const o=[];for(;n;){for(let i=0;i<n.children.length;i++){const s=n.children[i],a=n.leaf?this.toBBox(s):s;if(Ct(e,a)){if(n.leaf||Kt(e,a))return!0;o.push(s)}}n=o.pop()}return!1}load(e){if(!(e&&e.length))return this;if(e.length<this._minEntries){for(let o=0;o<e.length;o++)this.insert(e[o]);return this}let n=this._build(e.slice(),0,e.length-1,0);if(!this.data.children.length)this.data=n;else if(this.data.height===n.height)this._splitRoot(this.data,n);else{if(this.data.height<n.height){const o=this.data;this.data=n,n=o}this._insert(n,this.data.height-n.height-1,!0)}return this}insert(e){return e&&this._insert(e,this.data.height-1),this}clear(){return this.data=it([]),this}remove(e,n){if(!e)return this;let o=this.data;const i=this.toBBox(e),s=[],a=[];let r,l,f;for(;o||s.length;){if(o||(o=s.pop(),l=s[s.length-1],r=a.pop(),f=!0),o.leaf){const h=eo(e,o.children,n);if(h!==-1)return o.children.splice(h,1),s.push(o),this._condense(s),this}!f&&!o.leaf&&Kt(o,i)?(s.push(o),a.push(r),r=0,l=o,o=o.children[0]):l?(r++,o=l.children[r],f=!1):o=null}return this}toBBox(e){return e}compareMinX(e,n){return e.minX-n.minX}compareMinY(e,n){return e.minY-n.minY}toJSON(){return this.data}fromJSON(e){return this.data=e,this}_all(e,n){const o=[];for(;e;)e.leaf?n.push(...e.children):o.push(...e.children),e=o.pop();return n}_build(e,n,o,i){const s=o-n+1;let a=this._maxEntries,r;if(s<=a)return r=it(e.slice(n,o+1)),ot(r,this.toBBox),r;i||(i=Math.ceil(Math.log(s)/Math.log(a)),a=Math.ceil(s/Math.pow(a,i-1))),r=it([]),r.leaf=!1,r.height=i;const l=Math.ceil(s/a),f=l*Math.ceil(Math.sqrt(a));Ie(e,n,o,f,this.compareMinX);for(let h=n;h<=o;h+=f){const u=Math.min(h+f-1,o);Ie(e,h,u,l,this.compareMinY);for(let g=h;g<=u;g+=l){const v=Math.min(g+l-1,u);r.children.push(this._build(e,g,v,i-1))}}return ot(r,this.toBBox),r}_chooseSubtree(e,n,o,i){for(;i.push(n),!(n.leaf||i.length-1===o);){let s=1/0,a=1/0,r;for(let l=0;l<n.children.length;l++){const f=n.children[l],h=Yt(f),u=io(e,f)-h;u<a?(a=u,s=h<s?h:s,r=f):u===a&&h<s&&(s=h,r=f)}n=r||n.children[0]}return n}_insert(e,n,o){const i=o?e:this.toBBox(e),s=[],a=this._chooseSubtree(i,this.data,n,s);for(a.children.push(e),ut(a,i);n>=0&&s[n].children.length>this._maxEntries;)this._split(s,n),n--;this._adjustParentBBoxes(i,s,n)}_split(e,n){const o=e[n],i=o.children.length,s=this._minEntries;this._chooseSplitAxis(o,s,i);const a=this._chooseSplitIndex(o,s,i),r=it(o.children.splice(a,o.children.length-a));r.height=o.height,r.leaf=o.leaf,ot(o,this.toBBox),ot(r,this.toBBox),n?e[n-1].children.push(r):this._splitRoot(o,r)}_splitRoot(e,n){this.data=it([e,n]),this.data.height=e.height+1,this.data.leaf=!1,ot(this.data,this.toBBox)}_chooseSplitIndex(e,n,o){let i,s=1/0,a=1/0;for(let r=n;r<=o-n;r++){const l=dt(e,0,r,this.toBBox),f=dt(e,r,o,this.toBBox),h=so(l,f),u=Yt(l)+Yt(f);h<s?(s=h,i=r,a=u<a?u:a):h===s&&u<a&&(a=u,i=r)}return i||o-n}_chooseSplitAxis(e,n,o){const i=e.leaf?this.compareMinX:no,s=e.leaf?this.compareMinY:oo,a=this._allDistMargin(e,n,o,i),r=this._allDistMargin(e,n,o,s);a<r&&e.children.sort(i)}_allDistMargin(e,n,o,i){e.children.sort(i);const s=this.toBBox,a=dt(e,0,n,s),r=dt(e,o-n,o,s);let l=xt(a)+xt(r);for(let f=n;f<o-n;f++){const h=e.children[f];ut(a,e.leaf?s(h):h),l+=xt(a)}for(let f=o-n-1;f>=n;f--){const h=e.children[f];ut(r,e.leaf?s(h):h),l+=xt(r)}return l}_adjustParentBBoxes(e,n,o){for(let i=o;i>=0;i--)ut(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():ot(e[n],this.toBBox)}}function eo(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 ot(t,e){dt(t,0,t.children.length,e,t)}function dt(t,e,n,o,i){i||(i=it(null)),i.minX=1/0,i.minY=1/0,i.maxX=-1/0,i.maxY=-1/0;for(let s=e;s<n;s++){const a=t.children[s];ut(i,t.leaf?o(a):a)}return i}function ut(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 no(t,e){return t.minX-e.minX}function oo(t,e){return t.minY-e.minY}function Yt(t){return(t.maxX-t.minX)*(t.maxY-t.minY)}function xt(t){return t.maxX-t.minX+(t.maxY-t.minY)}function io(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 so(t,e){const n=Math.max(t.minX,e.minX),o=Math.max(t.minY,e.minY),i=Math.min(t.maxX,e.maxX),s=Math.min(t.maxY,e.maxY);return Math.max(0,i-n)*Math.max(0,s-o)}function Kt(t,e){return t.minX<=e.minX&&t.minY<=e.minY&&e.maxX<=t.maxX&&e.maxY<=t.maxY}function Ct(t,e){return e.minX<=t.maxX&&e.minY<=t.maxY&&e.maxX>=t.minX&&e.maxY>=t.minY}function it(t){return{children:t,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function Ie(t,e,n,o,i){const s=[e,n];for(;s.length;){if(n=s.pop(),e=s.pop(),n-e<=o)continue;const a=e+Math.ceil((n-e)/o/2)*o;ke(t,a,e,n,i),s.push(e,a,a,n)}}const ro=(t,e)=>{const n=new to,o=new Map,i=(d,c)=>{const b=d.selector.flatMap(S=>{const C=z([S])?S.range:Ot(S,e).range;return Array.from(C.getClientRects())}),x=Zt(b).map(({left:S,top:C,right:M,bottom:B})=>new DOMRect(S-c.left,C-c.top,M-S,B-C));return x.map(S=>{const{x:C,y:M,width:B,height:T}=S;return{minX:C,minY:M,maxX:C+B,maxY:M+T,annotation:{id:d.annotation,rects:x}}})},s=()=>[...o.values()],a=()=>{n.clear(),o.clear()},r=d=>{const c=i(d,e.getBoundingClientRect());c.length!==0&&(c.forEach(b=>n.insert(b)),o.set(d.annotation,c))},l=d=>{const c=o.get(d.annotation);c&&(c.forEach(b=>n.remove(b)),o.delete(d.annotation))},f=d=>{l(d),r(d)},h=(d,c=!0)=>{c&&a();const b=e.getBoundingClientRect(),x=d.map(C=>({target:C,rects:i(C,b)}));x.forEach(({target:C,rects:M})=>{M.length>0&&o.set(C.annotation,M)});const S=x.flatMap(({rects:C})=>C);n.load(S)},u=(d,c,b=!1)=>{const x=n.search({minX:d,minY:c,maxX:d,maxY:c}),S=C=>C.annotation.rects.reduce((M,B)=>M+B.width*B.height,0);return x.length>0?(x.sort((C,M)=>S(C)-S(M)),b?x.map(C=>C.annotation.id):[x[0].annotation.id]):[]},g=d=>{const c=v(d);if(c.length===0)return;let b=c[0].left,x=c[0].top,S=c[0].right,C=c[0].bottom;for(let M=1;M<c.length;M++){const B=c[M];b=Math.min(b,B.left),x=Math.min(x,B.top),S=Math.max(S,B.right),C=Math.max(C,B.bottom)}return new DOMRect(b,x,S-b,C-x)},v=d=>{const c=o.get(d);return c?c[0].annotation.rects:[]};return{all:s,clear:a,getAt:u,getAnnotationBounds:g,getAnnotationRects:v,getIntersecting:(d,c,b,x)=>{const S=n.search({minX:d,minY:c,maxX:b,maxY:x}),C=new Set(S.map(M=>M.annotation.id));return Array.from(C).map(M=>({annotation:t.getAnnotation(M),rects:v(M)})).filter(M=>!!M.annotation)},insert:r,recalculate:()=>h(t.all().map(d=>d.target),!0),remove:l,set:h,size:()=>n.all().length,update:f}},_e=(t,e)=>{const n=In(),o=ro(n,t),i=wn(n);i.setUserSelectAction(e);const s=An(n),a=Dn(),r=(d,c=k.LOCAL)=>{const b=wt(d,t),x=z(b.target.selector);return x&&n.addAnnotation(b,c),x},l=(d,c=!0,b=k.LOCAL)=>{const x=d.map(C=>wt(C,t)),S=x.filter(C=>!z(C.target.selector));return n.bulkAddAnnotations(x,c,b),S},f=(d,c=k.LOCAL)=>{const b=d.map(S=>wt(S,t)),x=b.filter(S=>!z(S.target.selector));return b.forEach(S=>{n.getAnnotation(S.id)?n.updateAnnotation(S,c):n.addAnnotation(S,c)}),x},h=(d,c=k.LOCAL)=>{const b=rt(d,t);n.updateTarget(b,c)},u=(d,c=k.LOCAL)=>{const b=d.map(x=>rt(x,t));n.bulkUpdateTargets(b,c)};function g(d,c,b,x){const S=b||!!x,C=o.getAt(d,c,S).map(B=>n.getAnnotation(B)),M=x?C.filter(x):C;if(M.length!==0)return b?M:M[0]}const v=d=>{if(o.getAnnotationRects(d).length!==0)return o.getAnnotationBounds(d)},p=(d,c,b,x)=>o.getIntersecting(d,c,b,x),m=d=>o.getAnnotationRects(d),w=()=>o.recalculate();return n.observe(({changes:d})=>{const c=(d.deleted||[]).filter(S=>z(S.target.selector)),b=(d.created||[]).filter(S=>z(S.target.selector)),x=(d.updated||[]).filter(S=>z(S.newValue.target.selector));(c==null?void 0:c.length)>0&&c.forEach(S=>o.remove(S.target)),b.length>0&&o.set(b.map(S=>S.target),!1),(x==null?void 0:x.length)>0&&x.forEach(({newValue:S})=>o.update(S.target))}),{store:{...n,addAnnotation:r,bulkAddAnnotations:l,bulkUpdateTargets:u,bulkUpsertAnnotations:f,getAnnotationBounds:v,getAnnotationRects:m,getIntersecting:p,getAt:g,recalculatePositions:w,updateTarget:h},selection:i,hover:s,viewport:a}},ao=()=>{const t=document.createElement("canvas");t.width=2*window.innerWidth,t.height=2*window.innerHeight,t.className="r6o-presence-layer";const e=t.getContext("2d");return e.scale(2,2),e.translate(.5,.5),t},Ne=(t,e={})=>{const n=ao(),o=n.getContext("2d");document.body.appendChild(n);const i=new Map,s=h=>Array.from(i.entries()).filter(([u,g])=>g.presenceKey===h.presenceKey).map(([u,g])=>u);return t.on("selectionChange",(h,u)=>{s(h).forEach(v=>i.delete(v)),u&&u.forEach(v=>i.set(v,h))}),{clear:()=>{const{width:h,height:u}=n;o.clearRect(-.5,-.5,h+1,u+1)},destroy:()=>{n.remove()},paint:(h,u,g)=>{e.font&&(o.font=e.font);const v=i.get(h.annotation.id);if(v){const{height:p}=h.rects[0],m=h.rects[0].x+u.left,w=h.rects[0].y+u.top;o.fillStyle=v.appearance.color,o.fillRect(m-2,w-2.5,2,p+5);const d=o.measureText(v.appearance.label),c=d.width+6,b=d.actualBoundingBoxAscent+d.actualBoundingBoxDescent+8,x=d.fontBoundingBoxAscent?8:6.5;return o.fillRect(m-2,w-2.5-b,c,b),o.fillStyle="#fff",o.fillText(v.appearance.label,m+1,w-x),{fill:v.appearance.color,fillOpacity:g?.45:.18}}},reset:()=>{n.width=2*window.innerWidth,n.height=2*window.innerHeight;const h=n.getContext("2d");h.scale(2,2),h.translate(.5,.5)}}},Xt=typeof navigator<"u"?navigator.userAgent.toLowerCase().indexOf("firefox")>0:!1;function Pt(t,e,n,o){t.addEventListener?t.addEventListener(e,n,o):t.attachEvent&&t.attachEvent("on".concat(e),n)}function ft(t,e,n,o){t.removeEventListener?t.removeEventListener(e,n,o):t.detachEvent&&t.detachEvent("on".concat(e),n)}function Ue(t,e){const n=e.slice(0,e.length-1);for(let o=0;o<n.length;o++)n[o]=t[n[o].toLowerCase()];return n}function De(t){typeof t!="string"&&(t=""),t=t.replace(/\s/g,"");const e=t.split(",");let n=e.lastIndexOf("");for(;n>=0;)e[n-1]+=",",e.splice(n,1),n=e.lastIndexOf("");return e}function co(t,e){const n=t.length>=e.length?t:e,o=t.length>=e.length?e:t;let i=!0;for(let s=0;s<n.length;s++)o.indexOf(n[s])===-1&&(i=!1);return i}const ht={backspace:8,"⌫":8,tab:9,clear:12,enter:13,"↩":13,return:13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,delete:46,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,capslock:20,num_0:96,num_1:97,num_2:98,num_3:99,num_4:100,num_5:101,num_6:102,num_7:103,num_8:104,num_9:105,num_multiply:106,num_add:107,num_enter:108,num_subtract:109,num_decimal:110,num_divide:111,"⇪":20,",":188,".":190,"/":191,"`":192,"-":Xt?173:189,"=":Xt?61:187,";":Xt?59:186,"'":222,"[":219,"]":221,"\\":220},W={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":91,cmd:91,command:91},Lt={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey",shiftKey:16,ctrlKey:17,altKey:18,metaKey:91},X={16:!1,18:!1,17:!1,91:!1},D={};for(let t=1;t<20;t++)ht["f".concat(t)]=111+t;let U=[],gt=null,Ve="all";const Q=new Map,pt=t=>ht[t.toLowerCase()]||W[t.toLowerCase()]||t.toUpperCase().charCodeAt(0),lo=t=>Object.keys(ht).find(e=>ht[e]===t),uo=t=>Object.keys(W).find(e=>W[e]===t);function Ye(t){Ve=t||"all"}function mt(){return Ve||"all"}function fo(){return U.slice(0)}function ho(){return U.map(t=>lo(t)||uo(t)||String.fromCharCode(t))}function go(){const t=[];return Object.keys(D).forEach(e=>{D[e].forEach(n=>{let{key:o,scope:i,mods:s,shortcut:a}=n;t.push({scope:i,shortcut:a,mods:s,keys:o.split("+").map(r=>pt(r))})})}),t}function po(t){const e=t.target||t.srcElement,{tagName:n}=e;let o=!0;const i=n==="INPUT"&&!["checkbox","radio","range","button","file","reset","submit","color"].includes(e.type);return(e.isContentEditable||(i||n==="TEXTAREA"||n==="SELECT")&&!e.readOnly)&&(o=!1),o}function mo(t){return typeof t=="string"&&(t=pt(t)),U.indexOf(t)!==-1}function yo(t,e){let n,o;t||(t=mt());for(const i in D)if(Object.prototype.hasOwnProperty.call(D,i))for(n=D[i],o=0;o<n.length;)n[o].scope===t?n.splice(o,1).forEach(a=>{let{element:r}=a;return Ht(r)}):o++;mt()===t&&Ye(e||"all")}function bo(t){let e=t.keyCode||t.which||t.charCode;const n=U.indexOf(e);if(n>=0&&U.splice(n,1),t.key&&t.key.toLowerCase()==="meta"&&U.splice(0,U.length),(e===93||e===224)&&(e=91),e in X){X[e]=!1;for(const o in W)W[o]===e&&(j[o]=!1)}}function Ke(t){if(typeof t>"u")Object.keys(D).forEach(i=>{Array.isArray(D[i])&&D[i].forEach(s=>Tt(s)),delete D[i]}),Ht(null);else if(Array.isArray(t))t.forEach(i=>{i.key&&Tt(i)});else if(typeof t=="object")t.key&&Tt(t);else if(typeof t=="string"){for(var e=arguments.length,n=new Array(e>1?e-1:0),o=1;o<e;o++)n[o-1]=arguments[o];let[i,s]=n;typeof i=="function"&&(s=i,i=""),Tt({key:t,scope:i,method:s,splitKey:"+"})}}const Tt=t=>{let{key:e,scope:n,method:o,splitKey:i="+"}=t;De(e).forEach(a=>{const r=a.split(i),l=r.length,f=r[l-1],h=f==="*"?"*":pt(f);if(!D[h])return;n||(n=mt());const u=l>1?Ue(W,r):[],g=[];D[h]=D[h].filter(v=>{const m=(o?v.method===o:!0)&&v.scope===n&&co(v.mods,u);return m&&g.push(v.element),!m}),g.forEach(v=>Ht(v))})};function Xe(t,e,n,o){if(e.element!==o)return;let i;if(e.scope===n||e.scope==="all"){i=e.mods.length>0;for(const s in X)Object.prototype.hasOwnProperty.call(X,s)&&(!X[s]&&e.mods.indexOf(+s)>-1||X[s]&&e.mods.indexOf(+s)===-1)&&(i=!1);(e.mods.length===0&&!X[16]&&!X[18]&&!X[17]&&!X[91]||i||e.shortcut==="*")&&(e.keys=[],e.keys=e.keys.concat(U),e.method(t,e)===!1&&(t.preventDefault?t.preventDefault():t.returnValue=!1,t.stopPropagation&&t.stopPropagation(),t.cancelBubble&&(t.cancelBubble=!0)))}}function Pe(t,e){const n=D["*"];let o=t.keyCode||t.which||t.charCode;if(!j.filter.call(this,t))return;if((o===93||o===224)&&(o=91),U.indexOf(o)===-1&&o!==229&&U.push(o),["metaKey","ctrlKey","altKey","shiftKey"].forEach(r=>{const l=Lt[r];t[r]&&U.indexOf(l)===-1?U.push(l):!t[r]&&U.indexOf(l)>-1?U.splice(U.indexOf(l),1):r==="metaKey"&&t[r]&&(U=U.filter(f=>f in Lt||f===o))}),o in X){X[o]=!0;for(const r in W)W[r]===o&&(j[r]=!0);if(!n)return}for(const r in X)Object.prototype.hasOwnProperty.call(X,r)&&(X[r]=t[Lt[r]]);t.getModifierState&&!(t.altKey&&!t.ctrlKey)&&t.getModifierState("AltGraph")&&(U.indexOf(17)===-1&&U.push(17),U.indexOf(18)===-1&&U.push(18),X[17]=!0,X[18]=!0);const i=mt();if(n)for(let r=0;r<n.length;r++)n[r].scope===i&&(t.type==="keydown"&&n[r].keydown||t.type==="keyup"&&n[r].keyup)&&Xe(t,n[r],i,e);if(!(o in D))return;const s=D[o],a=s.length;for(let r=0;r<a;r++)if((t.type==="keydown"&&s[r].keydown||t.type==="keyup"&&s[r].keyup)&&s[r].key){const l=s[r],{splitKey:f}=l,h=l.key.split(f),u=[];for(let g=0;g<h.length;g++)u.push(pt(h[g]));u.sort().join("")===U.sort().join("")&&Xe(t,l,i,e)}}function j(t,e,n){U=[];const o=De(t);let i=[],s="all",a=document,r=0,l=!1,f=!0,h="+",u=!1,g=!1;for(n===void 0&&typeof e=="function"&&(n=e),Object.prototype.toString.call(e)==="[object Object]"&&(e.scope&&(s=e.scope),e.element&&(a=e.element),e.keyup&&(l=e.keyup),e.keydown!==void 0&&(f=e.keydown),e.capture!==void 0&&(u=e.capture),typeof e.splitKey=="string"&&(h=e.splitKey),e.single===!0&&(g=!0)),typeof e=="string"&&(s=e),g&&Ke(t,s);r<o.length;r++)t=o[r].split(h),i=[],t.length>1&&(i=Ue(W,t)),t=t[t.length-1],t=t==="*"?"*":pt(t),t in D||(D[t]=[]),D[t].push({keyup:l,keydown:f,scope:s,mods:i,shortcut:o[r],method:n,key:o[r],splitKey:h,element:a});if(typeof a<"u"&&window){if(!Q.has(a)){const v=function(){let m=arguments.length>0&&arguments[0]!==void 0?arguments[0]:window.event;return Pe(m,a)},p=function(){let m=arguments.length>0&&arguments[0]!==void 0?arguments[0]:window.event;Pe(m,a),bo(m)};Q.set(a,{keydownListener:v,keyupListenr:p,capture:u}),Pt(a,"keydown",v,u),Pt(a,"keyup",p,u)}if(!gt){const v=()=>{U=[]};gt={listener:v,capture:u},Pt(window,"focus",v,u)}}}function Ao(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:"all";Object.keys(D).forEach(n=>{D[n].filter(i=>i.scope===e&&i.shortcut===t).forEach(i=>{i&&i.method&&i.method()})})}function Ht(t){const e=Object.values(D).flat();if(e.findIndex(o=>{let{element:i}=o;return i===t})<0){const{keydownListener:o,keyupListenr:i,capture:s}=Q.get(t)||{};o&&i&&(ft(t,"keyup",i,s),ft(t,"keydown",o,s),Q.delete(t))}if((e.length<=0||Q.size<=0)&&(Object.keys(Q).forEach(i=>{const{keydownListener:s,keyupListenr:a,capture:r}=Q.get(i)||{};s&&a&&(ft(i,"keyup",a,r),ft(i,"keydown",s,r),Q.delete(i))}),Q.clear(),Object.keys(D).forEach(i=>delete D[i]),gt)){const{listener:i,capture:s}=gt;ft(window,"focus",i,s),gt=null}}const $t={getPressedKeyString:ho,setScope:Ye,getScope:mt,deleteScope:yo,getPressedKeyCodes:fo,getAllKeyCodes:go,isPressed:mo,filter:po,trigger:Ao,unbind:Ke,keyMap:ht,modifier:W,modifierMap:Lt};for(const t in $t)Object.prototype.hasOwnProperty.call($t,t)&&(j[t]=$t[t]);if(typeof window<"u"){const t=window.hotkeys;j.noConflict=e=>(e&&window.hotkeys===j&&(window.hotkeys=t),j),window.hotkeys=j}const He=300,$e=["up","down","left","right"],je=zt?"⌘+a":"ctrl+a",wo=[...$e.map(t=>`shift+${t}`),je],Fe=(t,e,n)=>{let o;const{annotatingEnabled:i,offsetReferenceSelector:s,selectionMode:a}=n,r=T=>o=T;let l;const f=T=>l=T,{store:h,selection:u}=e;let g,v,p;const m=T=>{v!==!1&&(g=et(T.target)?void 0:{annotation:ve(),selector:[],creator:o,created:new Date})},w=bt(T=>{const _=document.getSelection();if(!(_!=null&&_.anchorNode))return;if(et(_.anchorNode)){g=void 0;return}const P=T.timeStamp-((p==null?void 0:p.timeStamp)||T.timeStamp);if((p==null?void 0:p.type)==="pointerdown"&&(P<1e3&&!g||_.isCollapsed&&P<He)&&m(p||T),!g)return;if(_.isCollapsed){h.getAnnotation(g.annotation)&&(u.clear(),h.deleteAnnotation(g.annotation));return}const y=_.getRangeAt(0),E=ee(y,t);if(Jt(E))return;const A=qt(E.cloneRange());(A.length!==g.selector.length||A.some((I,R)=>{var N;return I.toString()!==((N=g.selector[R])==null?void 0:N.quote)}))&&(g={...g,selector:A.map(I=>te(I,t,s)),updated:new Date},h.getAnnotation(g.annotation)?h.updateTarget(g,k.LOCAL):u.clear())}),d=T=>{et(T.target)||(p=yt(T),v=p.button===0)},c=T=>{if(et(T.target)||!v)return;const _=()=>{const{x:y,y:E}=t.getBoundingClientRect(),A=T.target instanceof Node&&t.contains(T.target)&&h.getAt(T.clientX-y,T.clientY-E,a==="all",l);if(A){const{selected:L}=u,I=new Set(L.map(H=>H.id)),R=Array.isArray(A)?A.map(H=>H.id):[A.id];(I.size!==R.length||!R.every(H=>I.has(H)))&&u.userSelect(R,T)}else u.clear()},P=T.timeStamp-p.timeStamp;setTimeout(()=>{const y=document.getSelection();y!=null&&y.isCollapsed&&P<He?(g=void 0,_()):g&&g.selector.length>0&&(M(),u.userSelect(g.annotation,yt(T)))})},b=T=>{const _=document.getSelection();_!=null&&_.isCollapsed||((!g||g.selector.length===0)&&w(T),M(),u.userSelect(g.annotation,yt(T)))},x=T=>{T.key==="Shift"&&g&&(document.getSelection().isCollapsed||(M(),u.userSelect(g.annotation,st(T))))},S=T=>{const _=()=>setTimeout(()=>{(g==null?void 0:g.selector.length)>0&&(u.clear(),h.addAnnotation({id:g.annotation,bodies:[],target:g}),u.userSelect(g.annotation,st(T))),document.removeEventListener("selectionchange",_)},100);document.addEventListener("selectionchange",_),m(T)};j(wo.join(","),{element:t,keydown:!0,keyup:!1},T=>{T.repeat||(p=st(T))}),j(je,{keydown:!0,keyup:!1},T=>{p=st(T),S(T)});const C=T=>{T.repeat||T.target!==t&&T.target!==document.body||(g=void 0,u.clear())};j($e.join(","),{keydown:!0,keyup:!1},C);const M=()=>{const T=h.getAnnotation(g.annotation);if(!T){h.addAnnotation({id:g.annotation,bodies:[],target:g});return}const{target:{updated:_}}=T,{updated:P}=g;(!_||!P||_<P)&&h.updateTarget(g)};return t.addEventListener("pointerdown",d),document.addEventListener("pointerup",c),document.addEventListener("contextmenu",b),i&&(t.addEventListener("keyup",x),t.addEventListener("selectstart",m),document.addEventListener("selectionchange",w)),{destroy:()=>{t.removeEventListener("pointerdown",d),document.removeEventListener("pointerup",c),document.removeEventListener("contextmenu",b),t.removeEventListener("keyup",x),t.removeEventListener("selectstart",m),document.removeEventListener("selectionchange",w),j.unbind()},setFilter:f,setUser:r}},ze=(t,e)=>({...t,annotatingEnabled:t.annotatingEnabled??e.annotatingEnabled,user:t.user||e.user}),We="SPANS",Eo=(t,e={})=>{Ft(t),Wt(t);const n=ze(e,{annotatingEnabled:!0,user:Fn()}),o=_e(t,n.userSelectAction),{selection:i,viewport:s}=o,a=o.store,r=Un(a),l=Vn(o,r,n.adapter);let f=n.user;const h=n.renderer==="CSS_HIGHLIGHTS"?CSS.highlights?"CSS_HIGHLIGHTS":We:n.renderer||We,u=h==="SPANS"?we(t,o,s):h==="CSS_HIGHLIGHTS"?be(t,o,s):h==="CANVAS"?se(t,o,s):void 0;if(!u)throw`Unknown renderer implementation: ${h}`;console.debug(`Using ${h} renderer`),n.style&&u.setStyle(n.style);const g=Fe(t,o,n);return g.setUser(f),{...Kn(o,r,n.adapter),destroy:()=>{u.destroy(),g.destroy(),r.destroy()},element:t,getUser:()=>f,setFilter:C=>{u.setFilter(C),g.setFilter(C)},setStyle:C=>u.setStyle(C),setUser:C=>{f=C,g.setUser(C)},setSelected:C=>{C?i.setSelected(C):i.clear()},setPresenceProvider:C=>{C&&(u.setPainter(Ne(C,n.presence)),C.on("selectionChange",()=>u.redraw()))},setVisible:C=>u.setVisible(C),on:l.on,off:l.off,scrollIntoView:oe(t,a),state:o}};O.DEFAULT_SELECTED_STYLE=at,O.DEFAULT_STYLE=q,O.NOT_ANNOTATABLE_CLASS=J,O.NOT_ANNOTATABLE_SELECTOR=Z,O.Origin=k,O.SelectionHandler=Fe,O.UserSelectAction=xe,O.W3CTextFormat=Gn,O.cancelSingleClickEvents=Ft,O.cloneKeyboardEvent=st,O.clonePointerEvent=yt,O.createBody=Cn,O.createCanvasRenderer=se,O.createHighlightsRenderer=be,O.createPresencePainter=Ne,O.createRenderer=ye,O.createSpansRenderer=we,O.createTextAnnotator=Eo,O.createTextAnnotatorState=_e,O.debounce=bt,O.fillDefaults=ze,O.getQuoteContext=Gt,O.getRangeAnnotatableContents=At,O.isMac=zt,O.isNotAnnotatable=et,O.isRangeAnnotatable=jt,O.isRevived=z,O.isWhitespaceOrEmpty=Jt,O.mergeClientRects=Zt,O.paint=ie,O.parseW3CTextAnnotation=Me,O.programmaticallyFocusable=Wt,O.rangeToSelector=te,O.reviveAnnotation=wt,O.reviveSelector=Ot,O.reviveTarget=rt,O.scrollIntoView=oe,O.serializeW3CTextAnnotation=Be,O.splitAnnotatableRanges=qt,O.toDomRectList=Je,O.trimRangeToContainer=ee,O.whitespaceOrEmptyRegex=Qt,Object.defineProperty(O,Symbol.toStringTag,{value:"Module"})});
1
+ (function(O,J){typeof exports=="object"&&typeof module<"u"?J(exports):typeof define=="function"&&define.amd?define(["exports"],J):(O=typeof globalThis<"u"?globalThis:O||self,J(O.RecogitoJS={}))})(this,function(O){"use strict";const J="not-annotatable",Z=`.${J}`,et=t=>{var n;return!!(t instanceof HTMLElement?t.closest(Z):(n=t.parentElement)==null?void 0:n.closest(Z))},jt=t=>{const e=t.commonAncestorContainer;return!et(e)},zt=t=>t.addEventListener("click",e=>{!e.target.closest(Z)&&!e.target.closest("a")&&e.preventDefault()}),bt=t=>({...t,type:t.type,x:t.x,y:t.y,clientX:t.clientX,clientY:t.clientY,offsetX:t.offsetX,offsetY:t.offsetY,screenX:t.screenX,screenY:t.screenY,isPrimary:t.isPrimary,altKey:t.altKey,ctrlKey:t.ctrlKey,metaKey:t.metaKey,shiftKey:t.shiftKey,button:t.button,buttons:t.buttons,currentTarget:t.currentTarget,target:t.target,defaultPrevented:t.defaultPrevented,detail:t.detail,eventPhase:t.eventPhase,pointerId:t.pointerId,pointerType:t.pointerType,timeStamp:t.timeStamp}),st=t=>({...t,type:t.type,key:t.key,code:t.code,location:t.location,repeat:t.repeat,altKey:t.altKey,ctrlKey:t.ctrlKey,metaKey:t.metaKey,shiftKey:t.shiftKey,currentTarget:t.currentTarget,target:t.target,defaultPrevented:t.defaultPrevented,detail:t.detail,timeStamp:t.timeStamp}),Ft=typeof navigator<"u"&&/mac/i.test(navigator.userAgentData?navigator.userAgentData.platform:navigator.platform),Wt=t=>{!t.hasAttribute("tabindex")&&t.tabIndex<0&&t.setAttribute("tabindex","-1"),t.classList.add("no-focus-outline")},At=(t,e=10)=>{let n;return(...o)=>{clearTimeout(n),n=setTimeout(()=>t.apply(void 0,o),e)}},qe=function*(t){const e=document.createNodeIterator(t.commonAncestorContainer,NodeFilter.SHOW_ELEMENT,o=>o instanceof HTMLElement&&o.classList.contains(J)&&!o.parentElement.closest(Z)&&t.intersectsNode(o)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP);let n;for(;n=e.nextNode();)n instanceof HTMLElement&&(yield n)},qt=t=>{if(!jt(t))return[];const e=[];let n=null;for(const o of qe(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]},wt=t=>{const e=t.cloneContents();return e.querySelectorAll(Z).forEach(n=>n.remove()),e},Gt=(t,e,n=10,o)=>{const i=o?t.startContainer.parentElement.closest(o):e,s=document.createRange();s.setStart(i,0),s.setEnd(t.startContainer,t.startOffset);const a=wt(s).textContent,r=document.createRange();r.setStart(t.endContainer,t.endOffset),i===document.body?r.setEnd(i,i.childNodes.length):r.setEndAfter(i);const l=wt(r).textContent;return{prefix:a.substring(a.length-n),suffix:l.substring(0,n)}},W=t=>t.every(e=>e.range instanceof Range&&!e.range.collapsed),Qt=/^\s*$/,Jt=t=>Qt.test(t.toString()),Ge=(t,e)=>{const n=s=>Math.round(s*10)/10,o={top:n(t.top),bottom:n(t.bottom),left:n(t.left),right:n(t.right)},i={top:n(e.top),bottom:n(e.bottom),left:n(e.left),right:n(e.right)};if(Math.abs(o.top-i.top)<.5&&Math.abs(o.bottom-i.bottom)<.5){if(Math.abs(o.left-i.right)<.5||Math.abs(o.right-i.left)<.5)return"inline-adjacent";if(o.left>=i.left&&o.right<=i.right)return"inline-is-contained";if(o.left<=i.left&&o.right>=i.right)return"inline-contains"}else if(o.top<=i.top&&o.bottom>=i.bottom){if(o.left<=i.left&&o.right>=i.right)return"block-contains"}else if(o.top>=i.top&&o.bottom<=i.bottom&&o.left>=i.left&&o.right<=i.right)return"block-is-contained"},Qe=(t,e)=>{const n=Math.min(t.left,e.left),o=Math.max(t.right,e.right),i=Math.min(t.top,e.top),s=Math.max(t.bottom,e.bottom);return new DOMRect(n,i,o-n,s-i)},Zt=t=>t.reduce((e,n)=>{if(n.width===0||n.height===0)return e;let o=[...e],i=!1;for(const s of e){const a=Ge(n,s);if(a==="inline-adjacent"){o=o.map(r=>r===s?Qe(n,s):r),i=!0;break}else if(a==="inline-contains"){o=o.map(r=>r===s?n:r),i=!0;break}else if(a==="inline-is-contained"){i=!0;break}else if(a==="block-contains"||a==="block-is-contained"){n.width<s.width&&(o=o.map(r=>r===s?n:r)),i=!0;break}}return i?o:[...o,n]},[]),Je=t=>({length:t.length,item:e=>t[e],[Symbol.iterator]:function*(){for(let e=0;e<this.length;e++)yield this.item(e)}}),te=(t,e,n)=>{const o=document.createRange(),i=n?t.startContainer.parentElement.closest(n):e;o.setStart(i,0),o.setEnd(t.startContainer,t.startOffset);const s=wt(o).textContent,a=t.toString(),r=s.length||0,l=r+a.length;return n?{quote:a,start:r,end:l,range:t,offsetReference:i}:{quote:a,start:r,end:l,range:t}},Ot=(t,e)=>{var h,u;const{start:n,end:o}=t,i=t.offsetReference||e,s=document.createNodeIterator(e,NodeFilter.SHOW_TEXT,g=>{var E;return(E=g.parentElement)!=null&&E.closest(Z)?NodeFilter.FILTER_SKIP:NodeFilter.FILTER_ACCEPT});let a=0;const r=document.createRange();let l=s.nextNode();l===null&&console.error("Could not revive annotation target. Content missing.");let f=!i;for(;l!==null;){if(f||(f=typeof(i==null?void 0:i.contains)=="function"?i.contains(l):!1),f){const g=((h=l.textContent)==null?void 0:h.length)||0;if(a+g>n){r.setStart(l,n-a);break}a+=g}l=s.nextNode()}for(;l!==null;){const g=((u=l.textContent)==null?void 0:u.length)||0;if(a+g>=o){r.setEnd(l,o-a);break}a+=g,l=s.nextNode()}return{...t,range:r}},rt=(t,e)=>W(t.selector)?t:{...t,selector:t.selector.map(n=>n.range instanceof Range&&!n.range.collapsed?n:Ot(n,e))},vt=(t,e)=>W(t.target.selector)?t:{...t,target:rt(t.target,e)},ee=(t,e)=>{const n=t.cloneRange();return e.contains(n.startContainer)||n.setStart(e,0),e.contains(n.endContainer)||n.setEnd(e,e.childNodes.length),n},ne=t=>{if(t===null)return document.scrollingElement;const{overflowY:e}=window.getComputedStyle(t);return e!=="visible"&&e!=="hidden"&&t.scrollHeight>t.clientHeight?t:ne(t.parentElement)},oe=(t,e)=>n=>{const o=typeof n=="string"?n:n.id,i=h=>{const u=s.getBoundingClientRect(),g=s.clientHeight,E=s.clientWidth,p=h.selector[0].range.getBoundingClientRect(),{width:m,height:w}=e.getAnnotationBounds(o),d=p.top-u.top,c=p.left-u.left,y=s.parentElement?s.scrollTop:0,x=s.parentElement?s.scrollLeft:0,S=d+y-(g-w)/2,C=c+x-(E-m)/2;s.scroll({top:S,left:C,behavior:"smooth"})},s=ne(t);if(!s)return console.warn(`The scroll parent is missing for the annotation: ${o}`,{container:t}),!1;const a=e.getAnnotation(o);if(!a)return console.warn(`The annotation is missing in the store: ${o}`),!1;const{range:r}=a.target.selector[0];if(r&&!r.collapsed)return i(a.target),!0;const l=rt(a.target,t),{range:f}=l.selector[0];return f&&!f.collapsed?(i(l),!0):!1},q={fill:"rgb(0, 128, 255)",fillOpacity:.18},at={fill:"rgb(0, 128, 255)",fillOpacity:.45},ie=(t,e,n,o,i)=>{var a,r;const s=n?typeof n=="function"?n(t.annotation,t.state,i)||((a=t.state)!=null&&a.selected?at:q):n:(r=t.state)!=null&&r.selected?at:q;return o&&o.paint(t,e)||s},Ze=t=>{const{top:e,left:n}=t.getBoundingClientRect(),{innerWidth:o,innerHeight:i}=window,s=-n,a=-e,r=o-n,l=i-e;return{top:e,left:n,minX:s,minY:a,maxX:r,maxY:l}},tn=t=>{let e=new Set;return o=>{const i=o.map(s=>s.id);(e.size!==i.length||i.some(s=>!e.has(s)))&&t.set(i),e=new Set(i)}},Rt=(t,e,n,o)=>{const{store:i,selection:s,hover:a}=e;let r,l,f;const h=tn(n),u=B=>{const{x:T,y:_}=t.getBoundingClientRect(),P=i.getAt(B.clientX-T,B.clientY-_,!1,l);P?a.current!==P.id&&(t.classList.add("hovered"),a.set(P.id)):a.current&&(t.classList.remove("hovered"),a.set(null))};t.addEventListener("pointermove",u);const g=(B=!1)=>{f&&f.clear();const T=Ze(t),{minX:_,minY:P,maxX:b,maxY:v}=T,A=l?i.getIntersecting(_,P,b,v).filter(({annotation:R})=>l(R)):i.getIntersecting(_,P,b,v),L=s.selected.map(({id:R})=>R),I=A.map(({annotation:R,rects:N})=>{const $=L.includes(R.id),Eo=R.id===a.current;return{annotation:R,rects:N,state:{selected:$,hover:Eo}}});o.redraw(I,T,r,f,B),setTimeout(()=>h(A.map(({annotation:R})=>R)),1)},E=B=>{f=B,g()},p=B=>{r=B,g()},m=B=>{l=B,g(!1)},w=()=>g();i.observe(w);const d=s.subscribe(()=>g()),c=()=>g(!0);document.addEventListener("scroll",c,{capture:!0,passive:!0});const y=At(()=>{i.recalculatePositions(),f&&f.reset(),g()});window.addEventListener("resize",y);const x=new ResizeObserver(y);x.observe(t);const S={attributes:!0,childList:!0,subtree:!0},C=new MutationObserver(B=>{B.every(_=>_.target===t||t.contains(_.target))||g(!0)});return C.observe(document.body,S),{destroy:()=>{t.removeEventListener("pointermove",u),o.destroy(),i.unobserve(w),d(),document.removeEventListener("scroll",c),window.removeEventListener("resize",y),x.disconnect(),C.disconnect()},redraw:g,setStyle:p,setFilter:m,setPainter:E,setVisible:o.setVisible}},en=()=>{const t=document.createElement("canvas");return t.width=window.innerWidth,t.height=window.innerHeight,t.className="r6o-canvas-highlight-layer bg",t},nn=(t,e)=>{t.width=window.innerWidth,t.height=window.innerHeight},on=t=>{t.classList.add("r6o-annotatable");const e=en(),n=e.getContext("2d");document.body.appendChild(e);const o=(r,l,f,h)=>requestAnimationFrame(()=>{const{width:u,height:g}=e;n.clearRect(-.5,-.5,u+1,g+1),h&&h.clear();const{top:E,left:p}=l;[...r].sort((w,d)=>{const{annotation:{target:{created:c}}}=w,{annotation:{target:{created:y}}}=d;return c.getTime()-y.getTime()}).forEach(w=>{var x;const d=f?typeof f=="function"?f(w.annotation,w.state):f:(x=w.state)!=null&&x.selected?at:q,c=h&&h.paint(w,l)||d,y=w.rects.map(({x:S,y:C,width:M,height:B})=>({x:S+p,y:C+E,width:M,height:B}));if(n.fillStyle=c.fill,n.globalAlpha=c.fillOpacity||1,y.forEach(({x:S,y:C,width:M,height:B})=>n.fillRect(S,C,M,B)),c.underlineColor){n.globalAlpha=1,n.strokeStyle=c.underlineColor,n.lineWidth=c.underlineThickness??1;const S=c.underlineOffset??0;y.forEach(({x:C,y:M,width:B,height:T})=>{n.beginPath(),n.moveTo(C,M+T+S),n.lineTo(C+B,M+T+S),n.stroke()})}})}),i=At(()=>{nn(e)});return window.addEventListener("resize",i),{destroy:()=>{e.remove(),window.removeEventListener("resize",i)},setVisible:r=>{console.log("setVisible not implemented on Canvas renderer")},redraw:o}},se=(t,e,n)=>Rt(t,e,n,on(t));var sn={grad:.9,turn:360,rad:360/(2*Math.PI)},G=function(t){return typeof t=="string"?t.length>0:typeof t=="number"},V=function(t,e,n){return e===void 0&&(e=0),n===void 0&&(n=Math.pow(10,e)),Math.round(n*t)/n+0},H=function(t,e,n){return e===void 0&&(e=0),n===void 0&&(n=1),t>n?n:t>e?t:e},re=function(t){return(t=isFinite(t)?t%360:0)>0?t:t+360},ae=function(t){return{r:H(t.r,0,255),g:H(t.g,0,255),b:H(t.b,0,255),a:H(t.a)}},Mt=function(t){return{r:V(t.r),g:V(t.g),b:V(t.b),a:V(t.a,3)}},rn=/^#([0-9a-f]{3,8})$/i,Et=function(t){var e=t.toString(16);return e.length<2?"0"+e:e},ce=function(t){var e=t.r,n=t.g,o=t.b,i=t.a,s=Math.max(e,n,o),a=s-Math.min(e,n,o),r=a?s===e?(n-o)/a:s===n?2+(o-e)/a:4+(e-n)/a:0;return{h:60*(r<0?r+6:r),s:s?a/s*100:0,v:s/255*100,a:i}},le=function(t){var e=t.h,n=t.s,o=t.v,i=t.a;e=e/360*6,n/=100,o/=100;var s=Math.floor(e),a=o*(1-n),r=o*(1-(e-s)*n),l=o*(1-(1-e+s)*n),f=s%6;return{r:255*[o,r,a,a,l,o][f],g:255*[l,o,o,r,a,a][f],b:255*[a,a,l,o,o,r][f],a:i}},de=function(t){return{h:re(t.h),s:H(t.s,0,100),l:H(t.l,0,100),a:H(t.a)}},ue=function(t){return{h:V(t.h),s:V(t.s),l:V(t.l),a:V(t.a,3)}},fe=function(t){return le((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},ct=function(t){return{h:(e=ce(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},an=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*,\s*([+-]?\d*\.?\d+)%\s*,\s*([+-]?\d*\.?\d+)%\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,cn=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,ln=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,dn=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,he={string:[[function(t){var e=rn.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?V(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?V(parseInt(t.substr(6,2),16)/255,2):1}:null:null},"hex"],[function(t){var e=ln.exec(t)||dn.exec(t);return e?e[2]!==e[4]||e[4]!==e[6]?null:ae({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=an.exec(t)||cn.exec(t);if(!e)return null;var n,o,i=de({h:(n=e[1],o=e[2],o===void 0&&(o="deg"),Number(n)*(sn[o]||1)),s:Number(e[3]),l:Number(e[4]),a:e[5]===void 0?1:Number(e[5])/(e[6]?100:1)});return fe(i)},"hsl"]],object:[[function(t){var e=t.r,n=t.g,o=t.b,i=t.a,s=i===void 0?1:i;return G(e)&&G(n)&&G(o)?ae({r:Number(e),g:Number(n),b:Number(o),a:Number(s)}):null},"rgb"],[function(t){var e=t.h,n=t.s,o=t.l,i=t.a,s=i===void 0?1:i;if(!G(e)||!G(n)||!G(o))return null;var a=de({h:Number(e),s:Number(n),l:Number(o),a:Number(s)});return fe(a)},"hsl"],[function(t){var e=t.h,n=t.s,o=t.v,i=t.a,s=i===void 0?1:i;if(!G(e)||!G(n)||!G(o))return null;var a=function(r){return{h:re(r.h),s:H(r.s,0,100),v:H(r.v,0,100),a:H(r.a)}}({h:Number(e),s:Number(n),v:Number(o),a:Number(s)});return le(a)},"hsv"]]},ge=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]},un=function(t){return typeof t=="string"?ge(t.trim(),he.string):typeof t=="object"&&t!==null?ge(t,he.object):[null,void 0]},Bt=function(t,e){var n=ct(t);return{h:n.h,s:H(n.s+100*e,0,100),l:n.l,a:n.a}},kt=function(t){return(299*t.r+587*t.g+114*t.b)/1e3/255},pe=function(t,e){var n=ct(t);return{h:n.h,s:n.s,l:H(n.l+100*e,0,100),a:n.a}},me=function(){function t(e){this.parsed=un(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 V(kt(this.rgba),2)},t.prototype.isDark=function(){return kt(this.rgba)<.5},t.prototype.isLight=function(){return kt(this.rgba)>=.5},t.prototype.toHex=function(){return e=Mt(this.rgba),n=e.r,o=e.g,i=e.b,a=(s=e.a)<1?Et(V(255*s)):"","#"+Et(n)+Et(o)+Et(i)+a;var e,n,o,i,s,a},t.prototype.toRgb=function(){return Mt(this.rgba)},t.prototype.toRgbString=function(){return e=Mt(this.rgba),n=e.r,o=e.g,i=e.b,(s=e.a)<1?"rgba("+n+", "+o+", "+i+", "+s+")":"rgb("+n+", "+o+", "+i+")";var e,n,o,i,s},t.prototype.toHsl=function(){return ue(ct(this.rgba))},t.prototype.toHslString=function(){return e=ue(ct(this.rgba)),n=e.h,o=e.s,i=e.l,(s=e.a)<1?"hsla("+n+", "+o+"%, "+i+"%, "+s+")":"hsl("+n+", "+o+"%, "+i+"%)";var e,n,o,i,s},t.prototype.toHsv=function(){return e=ce(this.rgba),{h:V(e.h),s:V(e.s),v:V(e.v),a:V(e.a,3)};var e},t.prototype.invert=function(){return z({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),z(Bt(this.rgba,e))},t.prototype.desaturate=function(e){return e===void 0&&(e=.1),z(Bt(this.rgba,-e))},t.prototype.grayscale=function(){return z(Bt(this.rgba,-1))},t.prototype.lighten=function(e){return e===void 0&&(e=.1),z(pe(this.rgba,e))},t.prototype.darken=function(e){return e===void 0&&(e=.1),z(pe(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"?z({r:(n=this.rgba).r,g:n.g,b:n.b,a:e}):V(this.rgba.a,3);var n},t.prototype.hue=function(e){var n=ct(this.rgba);return typeof e=="number"?z({h:e,s:n.s,l:n.l,a:n.a}):V(n.h)},t.prototype.isEqual=function(e){return this.toHex()===z(e).toHex()},t}(),z=function(t){return t instanceof me?t:new me(t)};const fn=t=>[`background-color:${z((t==null?void 0:t.fill)||q.fill).alpha((t==null?void 0:t.fillOpacity)===void 0?q.fillOpacity:t.fillOpacity).toHex()}`,t!=null&&t.underlineThickness?"text-decoration:underline":void 0,t!=null&&t.underlineColor?`text-decoration-color:${t.underlineColor}`:void 0,t!=null&&t.underlineOffset?`text-underline-offset:${t.underlineOffset}px`:void 0,t!=null&&t.underlineThickness?`text-decoration-thickness:${t.underlineThickness}px`:void 0].filter(Boolean).join(";"),ye=()=>{const t=document.createElement("style");document.getElementsByTagName("head")[0].appendChild(t);let e=new Set;return{destroy:()=>{CSS.highlights.clear(),t.remove()},setVisible:s=>{console.log("setVisible not implemented on CSS Custom Highlights renderer")},redraw:(s,a,r,l)=>{l&&l.clear();const f=new Set(s.map(u=>u.annotation.id));Array.from(e).filter(u=>!f.has(u));const h=s.map(u=>{var p;const g=r?typeof r=="function"?r(u.annotation,u.state):r:(p=u.state)!=null&&p.selected?at:q,E=l&&l.paint(u,a)||g;return`::highlight(_${u.annotation.id}) { ${fn(E)} }`});t.innerHTML=h.join(`
2
+ `),CSS.highlights.clear(),s.forEach(({annotation:u})=>{const g=u.target.selector.map(p=>p.range),E=new Highlight(...g);CSS.highlights.set(`_${u.id}`,E)}),e=f}}},be=(t,e,n)=>Rt(t,e,n,ye());var Ae=Object.prototype.hasOwnProperty;function It(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--&&It(t[o],e[o]););return o===-1}if(!n||typeof t=="object"){o=0;for(n in t)if(Ae.call(t,n)&&++o&&!Ae.call(e,n)||!(n in e)||!It(t[n],e[n]))return!1;return Object.keys(e).length===o}}return t!==t&&e!==e}const hn=(t,e)=>{const n=(s,a)=>s.x<=a.x+a.width&&s.x+s.width>=a.x&&s.y<=a.y+a.height&&s.y+s.height>=a.y,o=s=>s.rects.reduce((a,r)=>a+r.width,0),i=e.filter(({rects:s})=>s.some(a=>n(t,a)));return i.sort((s,a)=>o(a)-o(s)),i.findIndex(s=>s.rects.includes(t))},gn=t=>{t.classList.add("r6o-annotatable");const e=document.createElement("div");e.className="r6o-span-highlight-layer",t.insertBefore(e,t.firstChild);let n=[];return{destroy:()=>{e.remove()},redraw:(a,r,l,f,h)=>{const g=!(It(n,a)&&h);if(!f&&!g)return;g&&(e.innerHTML=""),[...a].sort((p,m)=>{const{annotation:{target:{created:w}}}=p,{annotation:{target:{created:d}}}=m;return w&&d?w.getTime()-d.getTime():0}).forEach(p=>{p.rects.map(m=>{const w=hn(m,a),d=ie(p,r,l,f,w);if(g){const c=document.createElement("span");c.className="r6o-annotation",c.dataset.annotation=p.annotation.id,c.style.left=`${m.x}px`,c.style.top=`${m.y}px`,c.style.width=`${m.width}px`,c.style.height=`${m.height}px`,c.style.backgroundColor=z((d==null?void 0:d.fill)||q.fill).alpha((d==null?void 0:d.fillOpacity)===void 0?q.fillOpacity:d.fillOpacity).toHex(),d.underlineStyle&&(c.style.borderStyle=d.underlineStyle),d.underlineColor&&(c.style.borderColor=d.underlineColor),d.underlineThickness&&(c.style.borderBottomWidth=`${d.underlineThickness}px`),d.underlineOffset&&(c.style.paddingBottom=`${d.underlineOffset}px`),e.appendChild(c)}})}),n=a},setVisible:a=>{a?e.classList.remove("hidden"):e.classList.add("hidden")}}},we=(t,e,n)=>Rt(t,e,n,gn(t)),Y=[];for(let t=0;t<256;++t)Y.push((t+256).toString(16).slice(1));function pn(t,e=0){return(Y[t[e+0]]+Y[t[e+1]]+Y[t[e+2]]+Y[t[e+3]]+"-"+Y[t[e+4]]+Y[t[e+5]]+"-"+Y[t[e+6]]+Y[t[e+7]]+"-"+Y[t[e+8]]+Y[t[e+9]]+"-"+Y[t[e+10]]+Y[t[e+11]]+Y[t[e+12]]+Y[t[e+13]]+Y[t[e+14]]+Y[t[e+15]]).toLowerCase()}let _t;const mn=new Uint8Array(16);function yn(){if(!_t){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");_t=crypto.getRandomValues.bind(crypto)}return _t(mn)}const ve={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function Ee(t,e,n){var i;if(ve.randomUUID&&!t)return ve.randomUUID();t=t||{};const o=t.random??((i=t.rng)==null?void 0:i.call(t))??yn();if(o.length<16)throw new Error("Random bytes length must be >= 16");return o[6]=o[6]&15|64,o[8]=o[8]&63|128,pn(o)}var Se=Object.prototype.hasOwnProperty;function tt(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--&&tt(t[o],e[o]););return o===-1}if(!n||typeof t=="object"){o=0;for(n in t)if(Se.call(t,n)&&++o&&!Se.call(e,n)||!(n in e)||!tt(t[n],e[n]))return!1;return Object.keys(e).length===o}}return t!==t&&e!==e}function Nt(){}function bn(t,e){return t!=t?e==e:t!==e||t&&typeof t=="object"||typeof t=="function"}const nt=[];function Ut(t,e=Nt){let n;const o=new Set;function i(r){if(bn(t,r)&&(t=r,n)){const l=!nt.length;for(const f of o)f[1](),nt.push(f,t);if(l){for(let f=0;f<nt.length;f+=2)nt[f][0](nt[f+1]);nt.length=0}}}function s(r){i(r(t))}function a(r,l=Nt){const f=[r,l];return o.add(f),o.size===1&&(n=e(i,s)||Nt),r(t),()=>{o.delete(f),o.size===0&&n&&(n(),n=null)}}return{set:i,update:s,subscribe:a}}const An=t=>{const{subscribe:e,set:n}=Ut();let o;return e(i=>o=i),t.observe(({changes:i})=>{if(o){(i.deleted||[]).some(a=>a.id===o)&&n(void 0);const s=(i.updated||[]).find(({oldValue:a})=>a.id===o);s&&n(s.newValue.id)}}),{get current(){return o},subscribe:e,set:n}};var xe=(t=>(t.EDIT="EDIT",t.SELECT="SELECT",t.NONE="NONE",t))(xe||{});const St={selected:[]},wn=(t,e,n)=>{const{subscribe:o,set:i}=Ut(St);let s=e,a=St;o(p=>a=p);const r=()=>{tt(a,St)||i(St)},l=()=>{var p;return((p=a.selected)==null?void 0:p.length)===0},f=p=>{if(l())return!1;const m=typeof p=="string"?p:p.id;return a.selected.some(w=>w.id===m)},h=(p,m)=>{let w;if(Array.isArray(p)){if(w=p.map(c=>t.getAnnotation(c)).filter(Boolean),w.length<p.length){console.warn("Invalid selection: "+p.filter(c=>!w.some(y=>y.id===c)));return}}else{const c=t.getAnnotation(p);if(!c){console.warn("Invalid selection: "+p);return}w=[c]}const d=w.reduce((c,y)=>{const x=Ce(y,s,n);return x==="EDIT"?[...c,{id:y.id,editable:!0}]:x==="SELECT"?[...c,{id:y.id}]:c},[]);i({selected:d,event:m})},u=(p,m)=>{const w=Array.isArray(p)?p:[p],d=w.map(c=>t.getAnnotation(c)).filter(c=>!!c);i({selected:d.map(c=>{const y=m===void 0?Ce(c,s,n)==="EDIT":m;return{id:c.id,editable:y}})}),d.length!==w.length&&console.warn("Invalid selection",p)},g=p=>{if(l())return!1;const{selected:m}=a;m.some(({id:w})=>p.includes(w))&&i({selected:m.filter(({id:w})=>!p.includes(w))})},E=p=>{s=p,u(a.selected.map(({id:m})=>m))};return t.observe(({changes:p})=>g((p.deleted||[]).map(m=>m.id))),{get event(){return a?a.event:null},get selected(){return a?[...a.selected]:null},get userSelectAction(){return s},clear:r,isEmpty:l,isSelected:f,setSelected:u,setUserSelectAction:E,subscribe:o,userSelect:h}},Ce=(t,e,n)=>{const o=n?n.serialize(t):t;return typeof e=="function"?e(o):e||"EDIT"},K=[];for(let t=0;t<256;++t)K.push((t+256).toString(16).slice(1));function vn(t,e=0){return(K[t[e+0]]+K[t[e+1]]+K[t[e+2]]+K[t[e+3]]+"-"+K[t[e+4]]+K[t[e+5]]+"-"+K[t[e+6]]+K[t[e+7]]+"-"+K[t[e+8]]+K[t[e+9]]+"-"+K[t[e+10]]+K[t[e+11]]+K[t[e+12]]+K[t[e+13]]+K[t[e+14]]+K[t[e+15]]).toLowerCase()}let Dt;const En=new Uint8Array(16);function Sn(){if(!Dt){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");Dt=crypto.getRandomValues.bind(crypto)}return Dt(En)}const xn=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),Le={randomUUID:xn};function Te(t,e,n){var o;if(Le.randomUUID&&!t)return Le.randomUUID();t=t||{};const i=t.random??((o=t.rng)==null?void 0:o.call(t))??Sn();if(i.length<16)throw new Error("Random bytes length must be >= 16");return i[6]=i[6]&15|64,i[8]=i[8]&63|128,vn(i)}const Vt=t=>{const e=n=>{const o={...n};return n.created&&typeof n.created=="string"&&(o.created=new Date(n.created)),n.updated&&typeof n.updated=="string"&&(o.updated=new Date(n.updated)),o};return{...t,bodies:(t.bodies||[]).map(e),target:e(t.target)}},Cn=(t,e,n,o)=>({id:Te(),annotation:typeof t=="string"?t:t.id,created:n||new Date,creator:o,...e}),Ln=(t,e)=>{const n=new Set(t.bodies.map(o=>o.id));return e.bodies.filter(o=>!n.has(o.id))},Tn=(t,e)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},On=(t,e)=>e.bodies.map(n=>{const o=t.bodies.find(i=>i.id===n.id);return{newBody:n,oldBody:o&&!tt(o,n)?o:void 0}}).filter(({oldBody:n})=>n).map(({oldBody:n,newBody:o})=>({oldBody:n,newBody:o})),Rn=(t,e)=>!tt(t.target,e.target),Oe=(t,e)=>{const n=Ln(t,e),o=Tn(t,e),i=On(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:Rn(t,e)?{oldTarget:t.target,newTarget:e.target}:void 0}};var k=(t=>(t.LOCAL="LOCAL",t.REMOTE="REMOTE",t.SILENT="SILENT",t))(k||{});const Mn=(t,e)=>{var n,o;const{changes:i,origin:s}=e;if(!(t.options.origin?t.options.origin===s:s!=="SILENT"))return!1;if(t.options.ignore){const{ignore:a}=t.options,r=l=>l&&l.length>0;if(!(r(i.created)||r(i.deleted))){const l=(n=i.updated)==null?void 0:n.some(h=>r(h.bodiesCreated)||r(h.bodiesDeleted)||r(h.bodiesUpdated)),f=(o=i.updated)==null?void 0:o.some(h=>h.targetUpdated);if(a==="BODY_ONLY"&&l&&!f||a==="TARGET_ONLY"&&f&&!l)return!1}}if(t.options.annotations){const a=new Set([...(i.created||[]).map(r=>r.id),...(i.deleted||[]).map(r=>r.id),...(i.updated||[]).map(({oldValue:r})=>r.id)]);return!!(Array.isArray(t.options.annotations)?t.options.annotations:[t.options.annotations]).find(r=>a.has(r))}else return!0},Bn=(t,e)=>{const n=new Set((t.created||[]).map(u=>u.id)),o=new Set((t.updated||[]).map(({newValue:u})=>u.id)),i=new Set((e.created||[]).map(u=>u.id)),s=new Set((e.deleted||[]).map(u=>u.id)),a=new Set((e.updated||[]).map(({oldValue:u})=>u.id)),r=new Set((e.updated||[]).filter(({oldValue:u})=>n.has(u.id)||o.has(u.id)).map(({oldValue:u})=>u.id)),l=[...(t.created||[]).filter(u=>!s.has(u.id)).map(u=>a.has(u.id)?e.updated.find(({oldValue:g})=>g.id===u.id).newValue:u),...e.created||[]],f=[...(t.deleted||[]).filter(u=>!i.has(u.id)),...(e.deleted||[]).filter(u=>!n.has(u.id))],h=[...(t.updated||[]).filter(({newValue:u})=>!s.has(u.id)).map(u=>{const{oldValue:g,newValue:E}=u;if(a.has(E.id)){const p=e.updated.find(m=>m.oldValue.id===E.id).newValue;return Oe(g,p)}else return u}),...(e.updated||[]).filter(({oldValue:u})=>!r.has(u.id))];return{created:l,deleted:f,updated:h}},xt=t=>{const e=t.id===void 0?Te():t.id;return{...t,id:e,bodies:t.bodies===void 0?[]:t.bodies.map(n=>({...n,annotation:e})),target:{...t.target,annotation:e}}},kn=t=>t.id!==void 0,In=()=>{const t=new Map,e=new Map,n=[],o=(b,v={})=>{n.push({onChange:b,options:v})},i=b=>{const v=n.findIndex(A=>A.onChange==b);v>-1&&n.splice(v,1)},s=(b,v)=>{const A={origin:b,changes:{created:v.created||[],updated:v.updated||[],deleted:v.deleted||[]},state:[...t.values()]};n.forEach(L=>{Mn(L,A)&&L.onChange(A)})},a=(b,v=k.LOCAL)=>{if(b.id&&t.get(b.id))throw Error(`Cannot add annotation ${b.id} - exists already`);{const A=xt(b);t.set(A.id,A),A.bodies.forEach(L=>e.set(L.id,A.id)),s(v,{created:[A]})}},r=(b,v)=>{const A=xt(typeof b=="string"?v:b),L=typeof b=="string"?b:b.id,I=L&&t.get(L);if(I){const R=Oe(I,A);return L===A.id?t.set(L,A):(t.delete(L),t.set(A.id,A)),I.bodies.forEach(N=>e.delete(N.id)),A.bodies.forEach(N=>e.set(N.id,A.id)),R}else console.warn(`Cannot update annotation ${L} - does not exist`)},l=(b,v=k.LOCAL,A=k.LOCAL)=>{const L=kn(v)?A:v,I=r(b,v);I&&s(L,{updated:[I]})},f=(b,v=k.LOCAL)=>{t.get(b.id)?l(b,v):a(b,v)},h=(b,v=k.LOCAL)=>{const A=b.reduce((L,I)=>{const R=r(I);return R?[...L,R]:L},[]);A.length>0&&s(v,{updated:A})},u=(b,v=k.LOCAL)=>{const A=b.map(xt),{toAdd:L,toUpdate:I}=A.reduce((N,$)=>t.get($.id)?{...N,toUpdate:[...N.toUpdate,$]}:{...N,toAdd:[...N.toAdd,$]},{toAdd:[],toUpdate:[]}),R=I.map(N=>r(N,v)).filter(Boolean);L.forEach(N=>{t.set(N.id,N),N.bodies.forEach($=>e.set($.id,N.id))}),s(v,{created:L,updated:R})},g=(b,v=k.LOCAL)=>{const A=t.get(b.annotation);if(A){const L={...A,bodies:[...A.bodies,b]};t.set(A.id,L),e.set(b.id,L.id),s(v,{updated:[{oldValue:A,newValue:L,bodiesCreated:[b]}]})}else console.warn(`Attempt to add body to missing annotation: ${b.annotation}`)},E=()=>[...t.values()],p=(b=k.LOCAL)=>{const v=[...t.values()];t.clear(),e.clear(),s(b,{deleted:v})},m=(b,v=!0,A=k.LOCAL)=>{const L=b.map(xt);if(v){const I=[...t.values()];t.clear(),e.clear(),L.forEach(R=>{t.set(R.id,R),R.bodies.forEach(N=>e.set(N.id,R.id))}),s(A,{created:L,deleted:I})}else{const I=b.reduce((R,N)=>{const $=N.id&&t.get(N.id);return $?[...R,$]:R},[]);if(I.length>0)throw Error(`Bulk insert would overwrite the following annotations: ${I.map(R=>R.id).join(", ")}`);L.forEach(R=>{t.set(R.id,R),R.bodies.forEach(N=>e.set(N.id,R.id))}),s(A,{created:L})}},w=b=>{const v=typeof b=="string"?b:b.id,A=t.get(v);if(A)return t.delete(v),A.bodies.forEach(L=>e.delete(L.id)),A;console.warn(`Attempt to delete missing annotation: ${v}`)},d=(b,v=k.LOCAL)=>{const A=w(b);A&&s(v,{deleted:[A]})},c=(b,v=k.LOCAL)=>{const A=b.reduce((L,I)=>{const R=w(I);return R?[...L,R]:L},[]);A.length>0&&s(v,{deleted:A})},y=b=>{const v=t.get(b.annotation);if(v){const A=v.bodies.find(L=>L.id===b.id);if(A){e.delete(A.id);const L={...v,bodies:v.bodies.filter(I=>I.id!==b.id)};return t.set(v.id,L),{oldValue:v,newValue:L,bodiesDeleted:[A]}}else console.warn(`Attempt to delete missing body ${b.id} from annotation ${b.annotation}`)}else console.warn(`Attempt to delete body from missing annotation ${b.annotation}`)},x=(b,v=k.LOCAL)=>{const A=y(b);A&&s(v,{updated:[A]})},S=(b,v=k.LOCAL)=>{const A=b.map(L=>y(L)).filter(Boolean);A.length>0&&s(v,{updated:A})},C=b=>{const v=t.get(b);return v?{...v}:void 0},M=b=>{const v=e.get(b);if(v){const A=C(v).bodies.find(L=>L.id===b);if(A)return A;console.error(`Store integrity error: body ${b} in index, but not in annotation`)}else console.warn(`Attempt to retrieve missing body: ${b}`)},B=(b,v)=>{if(b.annotation!==v.annotation)throw"Annotation integrity violation: annotation ID must be the same when updating bodies";const A=t.get(b.annotation);if(A){const L=A.bodies.find(R=>R.id===b.id),I={...A,bodies:A.bodies.map(R=>R.id===L.id?v:R)};return t.set(A.id,I),L.id!==v.id&&(e.delete(L.id),e.set(v.id,I.id)),{oldValue:A,newValue:I,bodiesUpdated:[{oldBody:L,newBody:v}]}}else console.warn(`Attempt to add body to missing annotation ${b.annotation}`)},T=(b,v,A=k.LOCAL)=>{const L=B(b,v);L&&s(A,{updated:[L]})},_=(b,v=k.LOCAL)=>{const A=b.map(L=>B({id:L.id,annotation:L.annotation},L)).filter(Boolean);s(v,{updated:A})},P=b=>{const v=t.get(b.annotation);if(v){const A={...v,target:{...v.target,...b}};return t.set(v.id,A),{oldValue:v,newValue:A,targetUpdated:{oldTarget:v.target,newTarget:b}}}else console.warn(`Attempt to update target on missing annotation: ${b.annotation}`)};return{addAnnotation:a,addBody:g,all:E,bulkAddAnnotations:m,bulkDeleteAnnotations:c,bulkDeleteBodies:S,bulkUpdateAnnotations:h,bulkUpdateBodies:_,bulkUpdateTargets:(b,v=k.LOCAL)=>{const A=b.map(L=>P(L)).filter(Boolean);A.length>0&&s(v,{updated:A})},bulkUpsertAnnotations:u,clear:p,deleteAnnotation:d,deleteBody:x,getAnnotation:C,getBody:M,observe:o,unobserve:i,updateAnnotation:l,updateBody:T,updateTarget:(b,v=k.LOCAL)=>{const A=P(b);A&&s(v,{updated:[A]})},upsertAnnotation:f}};let _n=()=>({emit(t,...e){for(let n=this.events[t]||[],o=0,i=n.length;o<i;o++)n[o](...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 Nn=250,Un=(t,e)=>{const n=_n(),o=[];let i=-1,s=!1,a=0;const r=p=>{if(!s){const{changes:m}=p,w=performance.now();if(w-a>Nn)o.splice(i+1),o.push(m),i=o.length-1;else{const d=o.length-1;o[d]=Bn(o[d],m)}a=w}s=!1};t.observe(r,{origin:k.LOCAL});const l=p=>p&&p.length>0&&t.bulkDeleteAnnotations(p),f=p=>p&&p.length>0&&t.bulkAddAnnotations(p,!1),h=p=>p&&p.length>0&&t.bulkUpdateAnnotations(p.map(({oldValue:m})=>m)),u=p=>p&&p.length>0&&t.bulkUpdateAnnotations(p.map(({newValue:m})=>m)),g=p=>p&&p.length>0&&t.bulkAddAnnotations(p,!1),E=p=>p&&p.length>0&&t.bulkDeleteAnnotations(p);return{canRedo:()=>o.length-1>i,canUndo:()=>i>-1,destroy:()=>t.unobserve(r),getHistory:()=>({changes:[...o],pointer:i}),on:(p,m)=>n.on(p,m),redo:()=>{if(o.length-1>i){s=!0;const{created:p,updated:m,deleted:w}=o[i+1];f(p),u(m),E(w),n.emit("redo",o[i+1]),i+=1}},undo:()=>{if(i>-1){s=!0;const{created:p,updated:m,deleted:w}=o[i];l(p),h(m),g(w),n.emit("undo",o[i]),i-=1}}}},Dn=()=>{const{subscribe:t,set:e}=Ut([]);return{subscribe:t,set:e}},Vn=(t,e,n,o)=>{const{hover:i,selection:s,store:a,viewport:r}=t,l=new Map;let f=[],h;const u=(m,w)=>{l.has(m)?l.get(m).push(w):l.set(m,[w])},g=(m,w)=>{const d=l.get(m);if(d){const c=d.indexOf(w);c!==-1&&d.splice(c,1)}},E=(m,w,d)=>{l.has(m)&&setTimeout(()=>{l.get(m).forEach(c=>{if(n){const y=Array.isArray(w)?w.map(S=>n.serialize(S)):n.serialize(w),x=d?d instanceof PointerEvent?d:n.serialize(d):void 0;c(y,x)}else c(w,d)})},1)};s.subscribe(({selected:m})=>{if(!(f.length===0&&m.length===0)){if(f.length===0&&m.length>0)f=m.map(({id:w})=>a.getAnnotation(w));else if(f.length>0&&m.length===0)f.forEach(w=>{const d=a.getAnnotation(w.id);d&&!tt(d,w)&&E("updateAnnotation",d,w)}),f=[];else{const w=new Set(f.map(c=>c.id)),d=new Set(m.map(({id:c})=>c));f.filter(c=>!d.has(c.id)).forEach(c=>{const y=a.getAnnotation(c.id);y&&!tt(y,c)&&E("updateAnnotation",y,c)}),f=[...f.filter(c=>d.has(c.id)),...m.filter(({id:c})=>!w.has(c)).map(({id:c})=>a.getAnnotation(c))]}E("selectionChanged",f)}}),i.subscribe(m=>{!h&&m?E("mouseEnterAnnotation",a.getAnnotation(m)):h&&!m?E("mouseLeaveAnnotation",a.getAnnotation(h)):h&&m&&(E("mouseLeaveAnnotation",a.getAnnotation(h)),E("mouseEnterAnnotation",a.getAnnotation(m))),h=m}),r==null||r.subscribe(m=>E("viewportIntersect",m.map(w=>a.getAnnotation(w)))),a.observe(m=>{const{created:w,deleted:d}=m.changes;(w||[]).forEach(c=>E("createAnnotation",c)),(d||[]).forEach(c=>E("deleteAnnotation",c)),(m.changes.updated||[]).filter(c=>[...c.bodiesCreated||[],...c.bodiesDeleted||[],...c.bodiesUpdated||[]].length>0).forEach(({oldValue:c,newValue:y})=>{const x=f.find(S=>S.id===c.id)||c;f=f.map(S=>S.id===c.id?y:S),E("updateAnnotation",y,x)})},{origin:k.LOCAL}),a.observe(m=>{if(f){const w=new Set(f.map(c=>c.id)),d=(m.changes.updated||[]).filter(({newValue:c})=>w.has(c.id)).map(({newValue:c})=>c);d.length>0&&(f=f.map(c=>d.find(x=>x.id===c.id)||c))}},{origin:k.REMOTE});const p=m=>w=>{const{updated:d}=w;m?(d||[]).forEach(c=>E("updateAnnotation",c.oldValue,c.newValue)):(d||[]).forEach(c=>E("updateAnnotation",c.newValue,c.oldValue))};return e.on("undo",p(!0)),e.on("redo",p(!1)),{on:u,off:g,emit:E}},Yn=t=>e=>e.reduce((n,o)=>{const{parsed:i,error:s}=t.parse(o);return s?{parsed:n.parsed,failed:[...n.failed,o]}:i?{parsed:[...n.parsed,i],failed:n.failed}:{...n}},{parsed:[],failed:[]}),Kn=(t,e,n)=>{const{store:o,selection:i}=t,s=d=>{if(n){const{parsed:c,error:y}=n.parse(d);c?o.addAnnotation(c,k.REMOTE):console.error(y)}else o.addAnnotation(Vt(d),k.REMOTE)},a=()=>i.clear(),r=()=>o.clear(),l=d=>{const c=o.getAnnotation(d);return n&&c?n.serialize(c):c},f=()=>n?o.all().map(n.serialize):o.all(),h=()=>{var d;const c=(((d=i.selected)==null?void 0:d.map(y=>y.id))||[]).map(y=>o.getAnnotation(y)).filter(Boolean);return n?c.map(n.serialize):c},u=(d,c=!0)=>fetch(d).then(y=>y.json()).then(y=>(E(y,c),y)),g=d=>{if(typeof d=="string"){const c=o.getAnnotation(d);if(o.deleteAnnotation(d),c)return n?n.serialize(c):c}else{const c=n?n.parse(d).parsed:d;if(c)return o.deleteAnnotation(c),d}},E=(d,c=!0)=>{if(n){const y=n.parseAll||Yn(n),{parsed:x,failed:S}=y(d);S.length>0&&console.warn(`Discarded ${S.length} invalid annotations`,S),o.bulkAddAnnotations(x,c,k.REMOTE)}else o.bulkAddAnnotations(d.map(Vt),c,k.REMOTE)},p=(d,c)=>{d?i.setSelected(d,c):i.clear()},m=d=>{i.clear(),i.setUserSelectAction(d)},w=d=>{if(n){const c=n.parse(d).parsed,y=n.serialize(o.getAnnotation(c.id));return o.updateAnnotation(c),y}else{const c=o.getAnnotation(d.id);return o.updateAnnotation(Vt(d)),c}};return{addAnnotation:s,cancelSelected:a,canRedo:e.canRedo,canUndo:e.canUndo,clearAnnotations:r,getAnnotationById:l,getAnnotations:f,getHistory:e.getHistory,getSelected:h,loadAnnotations:u,redo:e.redo,removeAnnotation:g,setAnnotations:E,setSelected:p,setUserSelectAction:m,undo:e.undo,updateAnnotation:w}},Xn="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let Pn=t=>crypto.getRandomValues(new Uint8Array(t)),$n=(t,e,n)=>{let o=(2<<Math.log2(t.length-1))-1,i=-~(1.6*o*e/t.length);return(s=e)=>{let a="";for(;;){let r=n(i),l=i|0;for(;l--;)if(a+=t[r[l]&o]||"",a.length>=s)return a}}},Hn=(t,e=21)=>$n(t,e|0,Pn),jn=(t=21)=>{let e="",n=crypto.getRandomValues(new Uint8Array(t|=0));for(;t--;)e+=Xn[n[t]&63];return e};const zn=()=>({isGuest:!0,id:Hn("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",20)()}),Fn=t=>{const e=JSON.stringify(t);let n=0;for(let o=0,i=e.length;o<i;o++){let s=e.charCodeAt(o);n=(n<<5)-n+s,n|=0}return`${n}`},Re=t=>t?typeof t=="object"?{...t}:t:void 0,Wn=(t,e)=>(Array.isArray(t)?t:[t]).map(n=>{const{id:o,type:i,purpose:s,value:a,created:r,modified:l,creator:f,...h}=n;return{id:o||`temp-${Fn(n)}`,annotation:e,type:i,purpose:s,value:a,creator:Re(f),created:r?new Date(r):void 0,updated:l?new Date(l):void 0,...h}}),qn=t=>t.map(e=>{var n;const{annotation:o,created:i,updated:s,...a}=e,r={...a,created:i==null?void 0:i.toISOString(),modified:s==null?void 0:s.toISOString()};return(n=r.id)!=null&&n.startsWith("temp-")&&delete r.id,r});jn();const Gn=(t,e)=>({parse:n=>Me(n),serialize:n=>Be(n,t,e)}),Qn=t=>t.quote!==void 0&&t.start!==void 0&&t.end!==void 0,Jn=t=>{const{id:e,creator:n,created:o,modified:i,target:s}=t,a=Array.isArray(s)?s:[s];if(a.length===0)return{error:Error(`No targets found for annotation: ${t.id}`)};const r={creator:Re(n),created:o?new Date(o):void 0,updated:i?new Date(i):void 0,annotation:e,selector:[],styleClass:"styleClass"in a[0]?a[0].styleClass:void 0};for(const l of a){const h=(Array.isArray(l.selector)?l.selector:[l.selector]).reduce((u,g)=>{switch(g.type){case"TextQuoteSelector":u.quote=g.exact;break;case"TextPositionSelector":u.start=g.start,u.end=g.end;break}return u},{});if(Qn(h))r.selector.push({...h,id:l.id,scope:l.scope});else{const u=[h.start?void 0:"TextPositionSelector",h.quote?void 0:"TextQuoteSelector"].filter(Boolean);return{error:Error(`Missing selector types: ${u.join(" and ")} for annotation: ${t.id}`)}}}return{parsed:r}},Me=t=>{const e=t.id||Ee(),{creator:n,created:o,modified:i,body:s,...a}=t,r=Wn(s,e),l=Jn(t);return"error"in l?{error:l.error}:{parsed:{...a,id:e,bodies:r,target:l.parsed}}},Be=(t,e,n)=>{const{bodies:o,target:i,...s}=t,{selector:a,creator:r,created:l,updated:f,...h}=i,u=a.map(g=>{const{id:E,quote:p,start:m,end:w,range:d}=g,c={type:"TextQuoteSelector",exact:p};if(n){const{prefix:x,suffix:S}=Gt(d,n);c.prefix=x,c.suffix=S}const y={type:"TextPositionSelector",start:m,end:w};return{...h,id:E,scope:"scope"in g?g.scope:void 0,source:e,selector:[c,y]}});return{...s,"@context":"http://www.w3.org/ns/anno.jsonld",id:t.id,type:"Annotation",body:qn(t.bodies),creator:r,created:l==null?void 0:l.toISOString(),modified:f==null?void 0:f.toISOString(),target:u}};function ke(t,e,n=0,o=t.length-1,i=Zn){for(;o>n;){if(o-n>600){const l=o-n+1,f=e-n+1,h=Math.log(l),u=.5*Math.exp(2*h/3),g=.5*Math.sqrt(h*u*(l-u)/l)*(f-l/2<0?-1:1),E=Math.max(n,Math.floor(e-f*u/l+g)),p=Math.min(o,Math.floor(e+(l-f)*u/l+g));ke(t,e,E,p,i)}const s=t[e];let a=n,r=o;for(lt(t,n,e),i(t[o],s)>0&&lt(t,n,o);a<r;){for(lt(t,a,r),a++,r--;i(t[a],s)<0;)a++;for(;i(t[r],s)>0;)r--}i(t[n],s)===0?lt(t,n,r):(r++,lt(t,r,o)),r<=e&&(n=r+1),e<=r&&(o=r-1)}}function lt(t,e,n){const o=t[e];t[e]=t[n],t[n]=o}function Zn(t,e){return t<e?-1:t>e?1:0}class to{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(!Lt(e,n))return o;const i=this.toBBox,s=[];for(;n;){for(let a=0;a<n.children.length;a++){const r=n.children[a],l=n.leaf?i(r):r;Lt(e,l)&&(n.leaf?o.push(r):Kt(e,l)?this._all(r,o):s.push(r))}n=s.pop()}return o}collides(e){let n=this.data;if(!Lt(e,n))return!1;const o=[];for(;n;){for(let i=0;i<n.children.length;i++){const s=n.children[i],a=n.leaf?this.toBBox(s):s;if(Lt(e,a)){if(n.leaf||Kt(e,a))return!0;o.push(s)}}n=o.pop()}return!1}load(e){if(!(e&&e.length))return this;if(e.length<this._minEntries){for(let o=0;o<e.length;o++)this.insert(e[o]);return this}let n=this._build(e.slice(),0,e.length-1,0);if(!this.data.children.length)this.data=n;else if(this.data.height===n.height)this._splitRoot(this.data,n);else{if(this.data.height<n.height){const o=this.data;this.data=n,n=o}this._insert(n,this.data.height-n.height-1,!0)}return this}insert(e){return e&&this._insert(e,this.data.height-1),this}clear(){return this.data=it([]),this}remove(e,n){if(!e)return this;let o=this.data;const i=this.toBBox(e),s=[],a=[];let r,l,f;for(;o||s.length;){if(o||(o=s.pop(),l=s[s.length-1],r=a.pop(),f=!0),o.leaf){const h=eo(e,o.children,n);if(h!==-1)return o.children.splice(h,1),s.push(o),this._condense(s),this}!f&&!o.leaf&&Kt(o,i)?(s.push(o),a.push(r),r=0,l=o,o=o.children[0]):l?(r++,o=l.children[r],f=!1):o=null}return this}toBBox(e){return e}compareMinX(e,n){return e.minX-n.minX}compareMinY(e,n){return e.minY-n.minY}toJSON(){return this.data}fromJSON(e){return this.data=e,this}_all(e,n){const o=[];for(;e;)e.leaf?n.push(...e.children):o.push(...e.children),e=o.pop();return n}_build(e,n,o,i){const s=o-n+1;let a=this._maxEntries,r;if(s<=a)return r=it(e.slice(n,o+1)),ot(r,this.toBBox),r;i||(i=Math.ceil(Math.log(s)/Math.log(a)),a=Math.ceil(s/Math.pow(a,i-1))),r=it([]),r.leaf=!1,r.height=i;const l=Math.ceil(s/a),f=l*Math.ceil(Math.sqrt(a));Ie(e,n,o,f,this.compareMinX);for(let h=n;h<=o;h+=f){const u=Math.min(h+f-1,o);Ie(e,h,u,l,this.compareMinY);for(let g=h;g<=u;g+=l){const E=Math.min(g+l-1,u);r.children.push(this._build(e,g,E,i-1))}}return ot(r,this.toBBox),r}_chooseSubtree(e,n,o,i){for(;i.push(n),!(n.leaf||i.length-1===o);){let s=1/0,a=1/0,r;for(let l=0;l<n.children.length;l++){const f=n.children[l],h=Yt(f),u=io(e,f)-h;u<a?(a=u,s=h<s?h:s,r=f):u===a&&h<s&&(s=h,r=f)}n=r||n.children[0]}return n}_insert(e,n,o){const i=o?e:this.toBBox(e),s=[],a=this._chooseSubtree(i,this.data,n,s);for(a.children.push(e),ut(a,i);n>=0&&s[n].children.length>this._maxEntries;)this._split(s,n),n--;this._adjustParentBBoxes(i,s,n)}_split(e,n){const o=e[n],i=o.children.length,s=this._minEntries;this._chooseSplitAxis(o,s,i);const a=this._chooseSplitIndex(o,s,i),r=it(o.children.splice(a,o.children.length-a));r.height=o.height,r.leaf=o.leaf,ot(o,this.toBBox),ot(r,this.toBBox),n?e[n-1].children.push(r):this._splitRoot(o,r)}_splitRoot(e,n){this.data=it([e,n]),this.data.height=e.height+1,this.data.leaf=!1,ot(this.data,this.toBBox)}_chooseSplitIndex(e,n,o){let i,s=1/0,a=1/0;for(let r=n;r<=o-n;r++){const l=dt(e,0,r,this.toBBox),f=dt(e,r,o,this.toBBox),h=so(l,f),u=Yt(l)+Yt(f);h<s?(s=h,i=r,a=u<a?u:a):h===s&&u<a&&(a=u,i=r)}return i||o-n}_chooseSplitAxis(e,n,o){const i=e.leaf?this.compareMinX:no,s=e.leaf?this.compareMinY:oo,a=this._allDistMargin(e,n,o,i),r=this._allDistMargin(e,n,o,s);a<r&&e.children.sort(i)}_allDistMargin(e,n,o,i){e.children.sort(i);const s=this.toBBox,a=dt(e,0,n,s),r=dt(e,o-n,o,s);let l=Ct(a)+Ct(r);for(let f=n;f<o-n;f++){const h=e.children[f];ut(a,e.leaf?s(h):h),l+=Ct(a)}for(let f=o-n-1;f>=n;f--){const h=e.children[f];ut(r,e.leaf?s(h):h),l+=Ct(r)}return l}_adjustParentBBoxes(e,n,o){for(let i=o;i>=0;i--)ut(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():ot(e[n],this.toBBox)}}function eo(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 ot(t,e){dt(t,0,t.children.length,e,t)}function dt(t,e,n,o,i){i||(i=it(null)),i.minX=1/0,i.minY=1/0,i.maxX=-1/0,i.maxY=-1/0;for(let s=e;s<n;s++){const a=t.children[s];ut(i,t.leaf?o(a):a)}return i}function ut(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 no(t,e){return t.minX-e.minX}function oo(t,e){return t.minY-e.minY}function Yt(t){return(t.maxX-t.minX)*(t.maxY-t.minY)}function Ct(t){return t.maxX-t.minX+(t.maxY-t.minY)}function io(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 so(t,e){const n=Math.max(t.minX,e.minX),o=Math.max(t.minY,e.minY),i=Math.min(t.maxX,e.maxX),s=Math.min(t.maxY,e.maxY);return Math.max(0,i-n)*Math.max(0,s-o)}function Kt(t,e){return t.minX<=e.minX&&t.minY<=e.minY&&e.maxX<=t.maxX&&e.maxY<=t.maxY}function Lt(t,e){return e.minX<=t.maxX&&e.minY<=t.maxY&&e.maxX>=t.minX&&e.maxY>=t.minY}function it(t){return{children:t,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function Ie(t,e,n,o,i){const s=[e,n];for(;s.length;){if(n=s.pop(),e=s.pop(),n-e<=o)continue;const a=e+Math.ceil((n-e)/o/2)*o;ke(t,a,e,n,i),s.push(e,a,a,n)}}const ro=(t,e)=>{const n=new to,o=new Map,i=(d,c)=>{const y=d.selector.flatMap(S=>{const C=W([S])?S.range:Ot(S,e).range;return Array.from(C.getClientRects())}),x=Zt(y).map(({left:S,top:C,right:M,bottom:B})=>new DOMRect(S-c.left,C-c.top,M-S,B-C));return x.map(S=>{const{x:C,y:M,width:B,height:T}=S;return{minX:C,minY:M,maxX:C+B,maxY:M+T,annotation:{id:d.annotation,rects:x}}})},s=()=>[...o.values()],a=()=>{n.clear(),o.clear()},r=d=>{const c=i(d,e.getBoundingClientRect());c.length!==0&&(c.forEach(y=>n.insert(y)),o.set(d.annotation,c))},l=d=>{const c=o.get(d.annotation);c&&(c.forEach(y=>n.remove(y)),o.delete(d.annotation))},f=d=>{l(d),r(d)},h=(d,c=!0)=>{c&&a();const y=e.getBoundingClientRect(),x=d.map(C=>({target:C,rects:i(C,y)}));x.forEach(({target:C,rects:M})=>{M.length>0&&o.set(C.annotation,M)});const S=x.flatMap(({rects:C})=>C);n.load(S)},u=(d,c,y=!1)=>{const x=n.search({minX:d,minY:c,maxX:d,maxY:c}),S=C=>C.annotation.rects.reduce((M,B)=>M+B.width*B.height,0);return x.length>0?(x.sort((C,M)=>S(C)-S(M)),y?x.map(C=>C.annotation.id):[x[0].annotation.id]):[]},g=d=>{const c=E(d);if(c.length===0)return;let y=c[0].left,x=c[0].top,S=c[0].right,C=c[0].bottom;for(let M=1;M<c.length;M++){const B=c[M];y=Math.min(y,B.left),x=Math.min(x,B.top),S=Math.max(S,B.right),C=Math.max(C,B.bottom)}return new DOMRect(y,x,S-y,C-x)},E=d=>{const c=o.get(d);return c?c[0].annotation.rects:[]};return{all:s,clear:a,getAt:u,getAnnotationBounds:g,getAnnotationRects:E,getIntersecting:(d,c,y,x)=>{const S=n.search({minX:d,minY:c,maxX:y,maxY:x}),C=new Set(S.map(M=>M.annotation.id));return Array.from(C).map(M=>({annotation:t.getAnnotation(M),rects:E(M)})).filter(M=>!!M.annotation)},insert:r,recalculate:()=>h(t.all().map(d=>d.target),!0),remove:l,set:h,size:()=>n.all().length,update:f}},_e=(t,e)=>{const n=In(),o=ro(n,t),i=wn(n,e.userSelectAction,e.adapter),s=An(n),a=Dn(),r=(d,c=k.LOCAL)=>{const y=vt(d,t),x=W(y.target.selector);return x&&n.addAnnotation(y,c),x},l=(d,c=!0,y=k.LOCAL)=>{const x=d.map(C=>vt(C,t)),S=x.filter(C=>!W(C.target.selector));return n.bulkAddAnnotations(x,c,y),S},f=(d,c=k.LOCAL)=>{const y=d.map(S=>vt(S,t)),x=y.filter(S=>!W(S.target.selector));return y.forEach(S=>{n.getAnnotation(S.id)?n.updateAnnotation(S,c):n.addAnnotation(S,c)}),x},h=(d,c=k.LOCAL)=>{const y=rt(d,t);n.updateTarget(y,c)},u=(d,c=k.LOCAL)=>{const y=d.map(x=>rt(x,t));n.bulkUpdateTargets(y,c)};function g(d,c,y,x){const S=y||!!x,C=o.getAt(d,c,S).map(B=>n.getAnnotation(B)),M=x?C.filter(x):C;if(M.length!==0)return y?M:M[0]}const E=d=>{if(o.getAnnotationRects(d).length!==0)return o.getAnnotationBounds(d)},p=(d,c,y,x)=>o.getIntersecting(d,c,y,x),m=d=>o.getAnnotationRects(d),w=()=>o.recalculate();return n.observe(({changes:d})=>{const c=(d.deleted||[]).filter(S=>W(S.target.selector)),y=(d.created||[]).filter(S=>W(S.target.selector)),x=(d.updated||[]).filter(S=>W(S.newValue.target.selector));(c==null?void 0:c.length)>0&&c.forEach(S=>o.remove(S.target)),y.length>0&&o.set(y.map(S=>S.target),!1),(x==null?void 0:x.length)>0&&x.forEach(({newValue:S})=>o.update(S.target))}),{store:{...n,addAnnotation:r,bulkAddAnnotations:l,bulkUpdateTargets:u,bulkUpsertAnnotations:f,getAnnotationBounds:E,getAnnotationRects:m,getIntersecting:p,getAt:g,recalculatePositions:w,updateTarget:h},selection:i,hover:s,viewport:a}},ao=()=>{const t=document.createElement("canvas");t.width=2*window.innerWidth,t.height=2*window.innerHeight,t.className="r6o-presence-layer";const e=t.getContext("2d");return e.scale(2,2),e.translate(.5,.5),t},Ne=(t,e={})=>{const n=ao(),o=n.getContext("2d");document.body.appendChild(n);const i=new Map,s=h=>Array.from(i.entries()).filter(([u,g])=>g.presenceKey===h.presenceKey).map(([u,g])=>u);return t.on("selectionChange",(h,u)=>{s(h).forEach(E=>i.delete(E)),u&&u.forEach(E=>i.set(E,h))}),{clear:()=>{const{width:h,height:u}=n;o.clearRect(-.5,-.5,h+1,u+1)},destroy:()=>{n.remove()},paint:(h,u,g)=>{e.font&&(o.font=e.font);const E=i.get(h.annotation.id);if(E){const{height:p}=h.rects[0],m=h.rects[0].x+u.left,w=h.rects[0].y+u.top;o.fillStyle=E.appearance.color,o.fillRect(m-2,w-2.5,2,p+5);const d=o.measureText(E.appearance.label),c=d.width+6,y=d.actualBoundingBoxAscent+d.actualBoundingBoxDescent+8,x=d.fontBoundingBoxAscent?8:6.5;return o.fillRect(m-2,w-2.5-y,c,y),o.fillStyle="#fff",o.fillText(E.appearance.label,m+1,w-x),{fill:E.appearance.color,fillOpacity:g?.45:.18}}},reset:()=>{n.width=2*window.innerWidth,n.height=2*window.innerHeight;const h=n.getContext("2d");h.scale(2,2),h.translate(.5,.5)}}},Xt=typeof navigator<"u"?navigator.userAgent.toLowerCase().indexOf("firefox")>0:!1;function Pt(t,e,n,o){t.addEventListener?t.addEventListener(e,n,o):t.attachEvent&&t.attachEvent("on".concat(e),n)}function ft(t,e,n,o){t.removeEventListener?t.removeEventListener(e,n,o):t.detachEvent&&t.detachEvent("on".concat(e),n)}function Ue(t,e){const n=e.slice(0,e.length-1);for(let o=0;o<n.length;o++)n[o]=t[n[o].toLowerCase()];return n}function De(t){typeof t!="string"&&(t=""),t=t.replace(/\s/g,"");const e=t.split(",");let n=e.lastIndexOf("");for(;n>=0;)e[n-1]+=",",e.splice(n,1),n=e.lastIndexOf("");return e}function co(t,e){const n=t.length>=e.length?t:e,o=t.length>=e.length?e:t;let i=!0;for(let s=0;s<n.length;s++)o.indexOf(n[s])===-1&&(i=!1);return i}const ht={backspace:8,"⌫":8,tab:9,clear:12,enter:13,"↩":13,return:13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,delete:46,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,capslock:20,num_0:96,num_1:97,num_2:98,num_3:99,num_4:100,num_5:101,num_6:102,num_7:103,num_8:104,num_9:105,num_multiply:106,num_add:107,num_enter:108,num_subtract:109,num_decimal:110,num_divide:111,"⇪":20,",":188,".":190,"/":191,"`":192,"-":Xt?173:189,"=":Xt?61:187,";":Xt?59:186,"'":222,"[":219,"]":221,"\\":220},F={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":91,cmd:91,command:91},gt={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey",shiftKey:16,ctrlKey:17,altKey:18,metaKey:91},X={16:!1,18:!1,17:!1,91:!1},D={};for(let t=1;t<20;t++)ht["f".concat(t)]=111+t;let U=[],pt=null,Ve="all";const Q=new Map,mt=t=>ht[t.toLowerCase()]||F[t.toLowerCase()]||t.toUpperCase().charCodeAt(0),lo=t=>Object.keys(ht).find(e=>ht[e]===t),uo=t=>Object.keys(F).find(e=>F[e]===t);function Ye(t){Ve=t||"all"}function yt(){return Ve||"all"}function fo(){return U.slice(0)}function ho(){return U.map(t=>lo(t)||uo(t)||String.fromCharCode(t))}function go(){const t=[];return Object.keys(D).forEach(e=>{D[e].forEach(n=>{let{key:o,scope:i,mods:s,shortcut:a}=n;t.push({scope:i,shortcut:a,mods:s,keys:o.split("+").map(r=>mt(r))})})}),t}function po(t){const e=t.target||t.srcElement,{tagName:n}=e;let o=!0;const i=n==="INPUT"&&!["checkbox","radio","range","button","file","reset","submit","color"].includes(e.type);return(e.isContentEditable||(i||n==="TEXTAREA"||n==="SELECT")&&!e.readOnly)&&(o=!1),o}function mo(t){return typeof t=="string"&&(t=mt(t)),U.indexOf(t)!==-1}function yo(t,e){let n,o;t||(t=yt());for(const i in D)if(Object.prototype.hasOwnProperty.call(D,i))for(n=D[i],o=0;o<n.length;)n[o].scope===t?n.splice(o,1).forEach(a=>{let{element:r}=a;return $t(r)}):o++;yt()===t&&Ye(e||"all")}function bo(t){let e=t.keyCode||t.which||t.charCode;const n=U.indexOf(e);if(n>=0&&U.splice(n,1),t.key&&t.key.toLowerCase()==="meta"&&U.splice(0,U.length),(e===93||e===224)&&(e=91),e in X){X[e]=!1;for(const o in F)F[o]===e&&(j[o]=!1)}}function Ke(t){if(typeof t>"u")Object.keys(D).forEach(i=>{Array.isArray(D[i])&&D[i].forEach(s=>Tt(s)),delete D[i]}),$t(null);else if(Array.isArray(t))t.forEach(i=>{i.key&&Tt(i)});else if(typeof t=="object")t.key&&Tt(t);else if(typeof t=="string"){for(var e=arguments.length,n=new Array(e>1?e-1:0),o=1;o<e;o++)n[o-1]=arguments[o];let[i,s]=n;typeof i=="function"&&(s=i,i=""),Tt({key:t,scope:i,method:s,splitKey:"+"})}}const Tt=t=>{let{key:e,scope:n,method:o,splitKey:i="+"}=t;De(e).forEach(a=>{const r=a.split(i),l=r.length,f=r[l-1],h=f==="*"?"*":mt(f);if(!D[h])return;n||(n=yt());const u=l>1?Ue(F,r):[],g=[];D[h]=D[h].filter(E=>{const m=(o?E.method===o:!0)&&E.scope===n&&co(E.mods,u);return m&&g.push(E.element),!m}),g.forEach(E=>$t(E))})};function Xe(t,e,n,o){if(e.element!==o)return;let i;if(e.scope===n||e.scope==="all"){i=e.mods.length>0;for(const s in X)Object.prototype.hasOwnProperty.call(X,s)&&(!X[s]&&e.mods.indexOf(+s)>-1||X[s]&&e.mods.indexOf(+s)===-1)&&(i=!1);(e.mods.length===0&&!X[16]&&!X[18]&&!X[17]&&!X[91]||i||e.shortcut==="*")&&(e.keys=[],e.keys=e.keys.concat(U),e.method(t,e)===!1&&(t.preventDefault?t.preventDefault():t.returnValue=!1,t.stopPropagation&&t.stopPropagation(),t.cancelBubble&&(t.cancelBubble=!0)))}}function Pe(t,e){const n=D["*"];let o=t.keyCode||t.which||t.charCode;if(!j.filter.call(this,t))return;if((o===93||o===224)&&(o=91),U.indexOf(o)===-1&&o!==229&&U.push(o),["metaKey","ctrlKey","altKey","shiftKey"].forEach(r=>{const l=gt[r];t[r]&&U.indexOf(l)===-1?U.push(l):!t[r]&&U.indexOf(l)>-1?U.splice(U.indexOf(l),1):r==="metaKey"&&t[r]&&(U=U.filter(f=>f in gt||f===o))}),o in X){X[o]=!0;for(const r in F)if(Object.prototype.hasOwnProperty.call(F,r)){const l=gt[F[r]];j[r]=t[l]}if(!n)return}for(const r in X)Object.prototype.hasOwnProperty.call(X,r)&&(X[r]=t[gt[r]]);t.getModifierState&&!(t.altKey&&!t.ctrlKey)&&t.getModifierState("AltGraph")&&(U.indexOf(17)===-1&&U.push(17),U.indexOf(18)===-1&&U.push(18),X[17]=!0,X[18]=!0);const i=yt();if(n)for(let r=0;r<n.length;r++)n[r].scope===i&&(t.type==="keydown"&&n[r].keydown||t.type==="keyup"&&n[r].keyup)&&Xe(t,n[r],i,e);if(!(o in D))return;const s=D[o],a=s.length;for(let r=0;r<a;r++)if((t.type==="keydown"&&s[r].keydown||t.type==="keyup"&&s[r].keyup)&&s[r].key){const l=s[r],{splitKey:f}=l,h=l.key.split(f),u=[];for(let g=0;g<h.length;g++)u.push(mt(h[g]));u.sort().join("")===U.sort().join("")&&Xe(t,l,i,e)}}function j(t,e,n){U=[];const o=De(t);let i=[],s="all",a=document,r=0,l=!1,f=!0,h="+",u=!1,g=!1;for(n===void 0&&typeof e=="function"&&(n=e),Object.prototype.toString.call(e)==="[object Object]"&&(e.scope&&(s=e.scope),e.element&&(a=e.element),e.keyup&&(l=e.keyup),e.keydown!==void 0&&(f=e.keydown),e.capture!==void 0&&(u=e.capture),typeof e.splitKey=="string"&&(h=e.splitKey),e.single===!0&&(g=!0)),typeof e=="string"&&(s=e),g&&Ke(t,s);r<o.length;r++)t=o[r].split(h),i=[],t.length>1&&(i=Ue(F,t)),t=t[t.length-1],t=t==="*"?"*":mt(t),t in D||(D[t]=[]),D[t].push({keyup:l,keydown:f,scope:s,mods:i,shortcut:o[r],method:n,key:o[r],splitKey:h,element:a});if(typeof a<"u"&&window){if(!Q.has(a)){const E=function(){let m=arguments.length>0&&arguments[0]!==void 0?arguments[0]:window.event;return Pe(m,a)},p=function(){let m=arguments.length>0&&arguments[0]!==void 0?arguments[0]:window.event;Pe(m,a),bo(m)};Q.set(a,{keydownListener:E,keyupListenr:p,capture:u}),Pt(a,"keydown",E,u),Pt(a,"keyup",p,u)}if(!pt){const E=()=>{U=[]};pt={listener:E,capture:u},Pt(window,"focus",E,u)}}}function Ao(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:"all";Object.keys(D).forEach(n=>{D[n].filter(i=>i.scope===e&&i.shortcut===t).forEach(i=>{i&&i.method&&i.method()})})}function $t(t){const e=Object.values(D).flat();if(e.findIndex(o=>{let{element:i}=o;return i===t})<0){const{keydownListener:o,keyupListenr:i,capture:s}=Q.get(t)||{};o&&i&&(ft(t,"keyup",i,s),ft(t,"keydown",o,s),Q.delete(t))}if((e.length<=0||Q.size<=0)&&(Object.keys(Q).forEach(i=>{const{keydownListener:s,keyupListenr:a,capture:r}=Q.get(i)||{};s&&a&&(ft(i,"keyup",a,r),ft(i,"keydown",s,r),Q.delete(i))}),Q.clear(),Object.keys(D).forEach(i=>delete D[i]),pt)){const{listener:i,capture:s}=pt;ft(window,"focus",i,s),pt=null}}const Ht={getPressedKeyString:ho,setScope:Ye,getScope:yt,deleteScope:yo,getPressedKeyCodes:fo,getAllKeyCodes:go,isPressed:mo,filter:po,trigger:Ao,unbind:Ke,keyMap:ht,modifier:F,modifierMap:gt};for(const t in Ht)Object.prototype.hasOwnProperty.call(Ht,t)&&(j[t]=Ht[t]);if(typeof window<"u"){const t=window.hotkeys;j.noConflict=e=>(e&&window.hotkeys===j&&(window.hotkeys=t),j),window.hotkeys=j}const $e=300,He=["up","down","left","right"],je=Ft?"⌘+a":"ctrl+a",wo=[...He.map(t=>`shift+${t}`),je],ze=(t,e,n)=>{let o;const{annotatingEnabled:i,offsetReferenceSelector:s,selectionMode:a}=n,r=T=>o=T;let l;const f=T=>l=T,{store:h,selection:u}=e;let g,E,p;const m=T=>{E!==!1&&(g=et(T.target)?void 0:{annotation:Ee(),selector:[],creator:o,created:new Date})},w=At(T=>{const _=document.getSelection();if(!(_!=null&&_.anchorNode))return;if(et(_.anchorNode)){g=void 0;return}const P=T.timeStamp-((p==null?void 0:p.timeStamp)||T.timeStamp);if((p==null?void 0:p.type)==="pointerdown"&&(P<1e3&&!g||_.isCollapsed&&P<$e)&&m(p||T),!g)return;if(_.isCollapsed){h.getAnnotation(g.annotation)&&(u.clear(),h.deleteAnnotation(g.annotation));return}const b=_.getRangeAt(0),v=ee(b,t);if(Jt(v))return;const A=qt(v.cloneRange());(A.length!==g.selector.length||A.some((I,R)=>{var N;return I.toString()!==((N=g.selector[R])==null?void 0:N.quote)}))&&(g={...g,selector:A.map(I=>te(I,t,s)),updated:new Date},h.getAnnotation(g.annotation)?h.updateTarget(g,k.LOCAL):u.clear())}),d=T=>{et(T.target)||(p=bt(T),E=p.button===0)},c=T=>{if(et(T.target)||!E)return;const _=()=>{const{x:b,y:v}=t.getBoundingClientRect(),A=T.target instanceof Node&&t.contains(T.target)&&h.getAt(T.clientX-b,T.clientY-v,a==="all",l);if(A){const{selected:L}=u,I=new Set(L.map($=>$.id)),R=Array.isArray(A)?A.map($=>$.id):[A.id];(I.size!==R.length||!R.every($=>I.has($)))&&u.userSelect(R,T)}else u.clear()},P=T.timeStamp-p.timeStamp;setTimeout(()=>{const b=document.getSelection();b!=null&&b.isCollapsed&&P<$e?(g=void 0,_()):g&&g.selector.length>0&&(M(),u.userSelect(g.annotation,bt(T)))})},y=T=>{const _=document.getSelection();_!=null&&_.isCollapsed||((!g||g.selector.length===0)&&w(T),g&&(M(),u.userSelect(g.annotation,bt(T))))},x=T=>{T.key==="Shift"&&g&&(document.getSelection().isCollapsed||(M(),u.userSelect(g.annotation,st(T))))},S=T=>{const _=()=>setTimeout(()=>{(g==null?void 0:g.selector.length)>0&&(u.clear(),h.addAnnotation({id:g.annotation,bodies:[],target:g}),u.userSelect(g.annotation,st(T))),document.removeEventListener("selectionchange",_)},100);document.addEventListener("selectionchange",_),m(T)};j(wo.join(","),{element:t,keydown:!0,keyup:!1},T=>{T.repeat||(p=st(T))}),j(je,{keydown:!0,keyup:!1},T=>{p=st(T),S(T)});const C=T=>{T.repeat||T.target!==t&&T.target!==document.body||(g=void 0,u.clear())};j(He.join(","),{keydown:!0,keyup:!1},C);const M=()=>{const T=h.getAnnotation(g.annotation);if(!T){h.addAnnotation({id:g.annotation,bodies:[],target:g});return}const{target:{updated:_}}=T,{updated:P}=g;(!_||!P||_<P)&&h.updateTarget(g)};return t.addEventListener("pointerdown",d),document.addEventListener("pointerup",c),document.addEventListener("contextmenu",y),i&&(t.addEventListener("keyup",x),t.addEventListener("selectstart",m),document.addEventListener("selectionchange",w)),{destroy:()=>{t.removeEventListener("pointerdown",d),document.removeEventListener("pointerup",c),document.removeEventListener("contextmenu",y),t.removeEventListener("keyup",x),t.removeEventListener("selectstart",m),document.removeEventListener("selectionchange",w),j.unbind()},setFilter:f,setUser:r}},Fe=(t,e)=>({...t,annotatingEnabled:t.annotatingEnabled??e.annotatingEnabled,user:t.user||e.user}),We="SPANS",vo=(t,e={})=>{zt(t),Wt(t);const n=Fe(e,{annotatingEnabled:!0,user:zn()}),o=_e(t,n),{selection:i,viewport:s}=o,a=o.store,r=Un(a),l=Vn(o,r,n.adapter);let f=n.user;const h=n.renderer==="CSS_HIGHLIGHTS"?CSS.highlights?"CSS_HIGHLIGHTS":We:n.renderer||We,u=h==="SPANS"?we(t,o,s):h==="CSS_HIGHLIGHTS"?be(t,o,s):h==="CANVAS"?se(t,o,s):void 0;if(!u)throw`Unknown renderer implementation: ${h}`;console.debug(`Using ${h} renderer`),n.style&&u.setStyle(n.style);const g=ze(t,o,n);return g.setUser(f),{...Kn(o,r,n.adapter),destroy:()=>{u.destroy(),g.destroy(),r.destroy()},element:t,getUser:()=>f,setFilter:C=>{u.setFilter(C),g.setFilter(C)},setStyle:C=>u.setStyle(C),setUser:C=>{f=C,g.setUser(C)},setSelected:C=>{C?i.setSelected(C):i.clear()},setPresenceProvider:C=>{C&&(u.setPainter(Ne(C,n.presence)),C.on("selectionChange",()=>u.redraw()))},setVisible:C=>u.setVisible(C),on:l.on,off:l.off,scrollIntoView:oe(t,a),state:o}};O.DEFAULT_SELECTED_STYLE=at,O.DEFAULT_STYLE=q,O.NOT_ANNOTATABLE_CLASS=J,O.NOT_ANNOTATABLE_SELECTOR=Z,O.Origin=k,O.SelectionHandler=ze,O.UserSelectAction=xe,O.W3CTextFormat=Gn,O.cancelSingleClickEvents=zt,O.cloneKeyboardEvent=st,O.clonePointerEvent=bt,O.createBody=Cn,O.createCanvasRenderer=se,O.createHighlightsRenderer=be,O.createPresencePainter=Ne,O.createRenderer=ye,O.createSpansRenderer=we,O.createTextAnnotator=vo,O.createTextAnnotatorState=_e,O.debounce=At,O.fillDefaults=Fe,O.getQuoteContext=Gt,O.getRangeAnnotatableContents=wt,O.isMac=Ft,O.isNotAnnotatable=et,O.isRangeAnnotatable=jt,O.isRevived=W,O.isWhitespaceOrEmpty=Jt,O.mergeClientRects=Zt,O.paint=ie,O.parseW3CTextAnnotation=Me,O.programmaticallyFocusable=Wt,O.rangeToSelector=te,O.reviveAnnotation=vt,O.reviveSelector=Ot,O.reviveTarget=rt,O.scrollIntoView=oe,O.serializeW3CTextAnnotation=Be,O.splitAnnotatableRanges=qt,O.toDomRectList=Je,O.trimRangeToContainer=ee,O.whitespaceOrEmptyRegex=Qt,Object.defineProperty(O,Symbol.toStringTag,{value:"Module"})});
3
3
  //# sourceMappingURL=text-annotator.umd.js.map