@soomo/text-annotator 3.0.0-staging.0

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