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