@recogito/text-annotator 3.0.0-rc.20 → 3.0.0-rc.22

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