@recogito/text-annotator 3.0.0-rc.21 → 3.0.0-rc.23

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