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