@recogito/text-annotator 3.0.0-rc.31 → 3.0.0-rc.33

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