@recogito/text-annotator 3.0.0-rc.24 → 3.0.0-rc.26

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