@recogito/text-annotator 3.0.0-rc.8 → 3.0.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 (59) hide show
  1. package/dist/src/SelectionHandler.d.ts +8 -6
  2. package/dist/src/TextAnnotator.d.ts +10 -6
  3. package/dist/src/TextAnnotatorOptions.d.ts +14 -7
  4. package/dist/src/api/scrollIntoView.d.ts +3 -3
  5. package/dist/src/highlight/Highlight.d.ts +5 -0
  6. package/dist/src/highlight/HighlightPainter.d.ts +10 -19
  7. package/dist/src/highlight/HighlightStyle.d.ts +11 -0
  8. package/dist/src/highlight/baseRenderer.d.ts +20 -0
  9. package/dist/src/highlight/canvas/canvasRenderer.d.ts +4 -0
  10. package/dist/src/highlight/canvas/index.d.ts +1 -0
  11. package/dist/src/highlight/highlights/highlightsRenderer.d.ts +6 -0
  12. package/dist/src/highlight/highlights/index.d.ts +1 -0
  13. package/dist/src/highlight/index.d.ts +5 -1
  14. package/dist/src/highlight/span/index.d.ts +1 -0
  15. package/dist/src/highlight/span/spansRenderer.d.ts +4 -0
  16. package/dist/src/highlight/viewport.d.ts +12 -0
  17. package/dist/src/index.d.ts +6 -3
  18. package/dist/src/model/{TextAnnotation.d.ts → core/TextAnnotation.d.ts} +3 -2
  19. package/dist/src/model/core/index.d.ts +1 -0
  20. package/dist/src/model/index.d.ts +2 -1
  21. package/dist/src/model/w3c/W3CTextAnnotation.d.ts +34 -0
  22. package/dist/src/model/w3c/W3CTextFormatAdapter.d.ts +12 -0
  23. package/dist/src/model/w3c/index.d.ts +2 -0
  24. package/dist/src/presence/PresencePainter.d.ts +4 -4
  25. package/dist/src/state/TextAnnotationStore.d.ts +15 -11
  26. package/dist/src/state/TextAnnotatorState.d.ts +9 -10
  27. package/dist/src/state/index.d.ts +0 -1
  28. package/dist/src/state/spatialTree.d.ts +10 -8
  29. package/dist/src/utils/cancelSingleClickEvents.d.ts +6 -0
  30. package/dist/src/utils/cloneEvents.d.ts +11 -0
  31. package/dist/src/utils/debounce.d.ts +1 -0
  32. package/dist/src/utils/device.d.ts +1 -0
  33. package/dist/src/utils/getQuoteContext.d.ts +4 -0
  34. package/dist/src/utils/index.d.ts +15 -2
  35. package/dist/src/utils/isNotAnnotatable.d.ts +4 -0
  36. package/dist/src/utils/isRevived.d.ts +2 -0
  37. package/dist/src/utils/isWhitespaceOrEmpty.d.ts +2 -0
  38. package/dist/src/utils/mergeClientRects.d.ts +1 -0
  39. package/dist/src/utils/programmaticallyFocusable.d.ts +6 -0
  40. package/dist/src/utils/rangeToSelector.d.ts +2 -0
  41. package/dist/src/utils/reviveAnnotation.d.ts +2 -0
  42. package/dist/src/utils/reviveSelector.d.ts +11 -0
  43. package/dist/src/utils/reviveTarget.d.ts +2 -0
  44. package/dist/src/utils/splitAnnotatableRanges.d.ts +5 -0
  45. package/dist/src/utils/trimRangeToContainer.d.ts +1 -0
  46. package/dist/test/model/w3c/W3CTextFormatAdapter.test.d.ts +1 -0
  47. package/dist/test/model/w3c/fixtures.d.ts +3 -0
  48. package/dist/text-annotator.css +1 -1
  49. package/dist/text-annotator.es.js +2003 -938
  50. package/dist/text-annotator.es.js.map +1 -1
  51. package/dist/text-annotator.umd.js +2 -1
  52. package/dist/text-annotator.umd.js.map +1 -1
  53. package/package.json +18 -13
  54. package/dist/src/highlight/highlightLayer.d.ts +0 -10
  55. package/dist/src/highlight/trackViewport.d.ts +0 -3
  56. package/dist/src/state/reviveTarget.d.ts +0 -9
  57. package/dist/src/utils/getClientRectsPonyfill.d.ts +0 -1
  58. package/dist/src/utils/getContext.d.ts +0 -4
  59. package/dist/src/utils/trimRange.d.ts +0 -1
@@ -1,2 +1,3 @@
1
- (function(X,N){typeof exports=="object"&&typeof module<"u"?N(exports):typeof define=="function"&&define.amd?define(["exports"],N):(X=typeof globalThis<"u"?globalThis:X||self,N(X.RecogitoJS={}))})(this,function(X){"use strict";const N={fill:"rgb(0, 128, 255",fillOpacity:.18},dt={fill:"rgb(0, 128, 255)",fillOpacity:.45},j={paint:(e,t,n,o,i,s)=>{const a=s?typeof s=="function"?s(e,i):s:i?dt:N;n.fillStyle=a.fill,n.globalAlpha=a.fillOpacity||1,t.forEach(({x:r,y:d,width:u,height:b})=>n.fillRect(r,d-2.5,u,b+5))}},ut=e=>{let t=new Set;return o=>{const i=o.map(s=>s.id);(t.size!==i.length||i.some(s=>!t.has(s)))&&e.set(i),t=new Set(i)}},ht=(e,t=10)=>{let n;return(...o)=>{clearTimeout(n),n=setTimeout(()=>e.apply(void 0,o),t)}},tt=(e,t)=>{const n=document.createElement("canvas");if(n.width=t?2*window.innerWidth:window.innerWidth,n.height=t?2*window.innerHeight:window.innerHeight,n.className=e,t){const o=n.getContext("2d");o.scale(2,2),o.translate(.5,.5)}return n},et=(e,t)=>{if(e.width=t?2*window.innerWidth:window.innerWidth,e.height=t?2*window.innerHeight:window.innerHeight,t){const n=e.getContext("2d");n.scale(2,2),n.translate(.5,.5)}},nt=(e,t,n)=>{const{store:o,selection:i,hover:s}=t;let a,r,d=j;const u=ut(n);e.classList.add("r6o-annotatable");const b=tt("r6o-highlight-layer bg"),h=tt("r6o-highlight-layer fg",!0),v=b.getContext("2d"),m=h.getContext("2d");e.insertBefore(b,e.firstChild),e.appendChild(h),e.addEventListener("pointermove",E=>{const{x:T,y:L}=e.getBoundingClientRect(),f=o.getAt(E.clientX-T,E.clientY-L);f&&(!r||r(f))?s.current!==f.id&&(e.classList.add("hovered"),s.set(f.id)):s.current&&(e.classList.remove("hovered"),s.set(null))});const x=()=>{const{top:E,left:T}=e.getBoundingClientRect(),{innerWidth:L,innerHeight:f}=window,w=-T,A=-E,S=L-T,B=f-E;return{top:E,left:T,minX:w,minY:A,maxX:S,maxY:B}},M=()=>c();document.addEventListener("scroll",M,{capture:!0,passive:!0});const y=ht(()=>{et(b),et(h,!0),o.recalculatePositions(),c()});window.addEventListener("resize",y),new ResizeObserver(y).observe(e);const c=()=>requestAnimationFrame(()=>{const{top:E,left:T,minX:L,minY:f,maxX:w,maxY:A}=x(),S=r?o.getIntersectingRects(L,f,w,A).filter(({annotation:Y})=>r(Y)):o.getIntersectingRects(L,f,w,A),{width:B,height:R}=h,U=new Set(i.selected.map(({id:Y})=>Y));m.clearRect(-.5,-.5,B+1,R+1),v.clearRect(-.5,-.5,B+1,R+1),S.forEach(({annotation:Y,rects:ae})=>{const re=ae.map(({x:ce,y:de,width:ue,height:he})=>({x:ce+T,y:de+E,width:ue,height:he})),le=U.has(Y.id);d.paint(Y,re,v,m,le,a)}),setTimeout(()=>u(S.map(({annotation:Y})=>Y)),1)});return o.observe(()=>c()),i.subscribe(()=>c()),{redraw:c,setDrawingStyle:E=>{a=E,c()},setFilter:E=>{r=E,c()},setPainter:E=>d=E}},I=(e,t)=>{const{start:n,end:o}=e.selector,i=e.selector.offsetReference?e.selector.offsetReference:t;if(!i)return e;const s=document.createNodeIterator(t,NodeFilter.SHOW_TEXT);let a=0,r=document.createRange(),d=s.nextNode();d===null&&console.error("Could not revive annotation target. Content missing.");let u=!i;for(;d!==null;){const b=d.textContent.length;if(!u&&i&&(u=i.contains(d)),u){if(a+b>n){r.setStart(d,n-a);break}a+=b}d=s.nextNode()}for(;d!==null;){const b=d.textContent.length;if(a+b>o){r.setEnd(d,o-a);break}a+=b,d=s.nextNode()}return{...e,selector:{...e.selector,range:r}}};var ot=Object.prototype.hasOwnProperty;function D(e,t){var n,o;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((o=e.length)===t.length)for(;o--&&D(e[o],t[o]););return o===-1}if(!n||typeof e=="object"){o=0;for(n in e)if(ot.call(e,n)&&++o&&!ot.call(t,n)||!(n in t)||!D(e[n],t[n]))return!1;return Object.keys(t).length===o}}return e!==e&&t!==t}function G(){}function ft(e,t){return e!=e?t==t:e!==t||e&&typeof e=="object"||typeof e=="function"}const V=[];function J(e,t=G){let n;const o=new Set;function i(r){if(ft(e,r)&&(e=r,n)){const d=!V.length;for(const u of o)u[1](),V.push(u,e);if(d){for(let u=0;u<V.length;u+=2)V[u][0](V[u+1]);V.length=0}}}function s(r){i(r(e))}function a(r,d=G){const u=[r,d];return o.add(u),o.size===1&&(n=t(i,s)||G),r(e),()=>{o.delete(u),o.size===0&&n&&(n(),n=null)}}return{set:i,update:s,subscribe:a}}const gt=e=>{const{subscribe:t,set:n}=J();let o;return t(i=>o=i),e.observe(({changes:i})=>{if(o){(i.deleted||[]).some(a=>a.id===o)&&n(void 0);const s=(i.updated||[]).find(({oldValue:a})=>a.id===o);s&&n(s.newValue.id)}}),{get current(){return o},subscribe:t,set:n}},K={selected:[]},pt=(e,t="EDIT")=>{const{subscribe:n,set:o}=J(K);let i=K;n(h=>i=h);const s=()=>o(K),a=()=>{var h;return((h=i.selected)==null?void 0:h.length)===0},r=h=>{if(i.selected.length===0)return!1;const v=typeof h=="string"?h:h.id;return i.selected.some(m=>m.id===v)},d=(h,v)=>{const m=e.getAnnotation(h);if(m){const x=mt(m,t);o(x==="EDIT"?{selected:[{id:h,editable:!0}],pointerEvent:v}:x==="SELECT"?{selected:[{id:h}],pointerEvent:v}:{selected:[],pointerEvent:v})}else console.warn("Invalid selection: "+h)},u=(h,v=!0)=>{const m=Array.isArray(h)?h:[h],x=m.map(M=>e.getAnnotation(M)).filter(Boolean);o({selected:x.map(({id:M})=>({id:M,editable:v}))}),x.length!==m.length&&console.warn("Invalid selection",h)},b=h=>{if(i.selected.length===0)return!1;const{selected:v}=i;v.filter(({id:m})=>h.includes(m)).length>0&&o({selected:v.filter(({id:m})=>!h.includes(m))})};return e.observe(({changes:h})=>b((h.deleted||[]).map(v=>v.id))),{clear:s,clickSelect:d,get selected(){return i?[...i.selected]:null},get pointerEvent(){return i?i.pointerEvent:null},isEmpty:a,isSelected:r,setSelected:u,subscribe:n}},mt=(e,t)=>typeof t=="function"?t(e)||"EDIT":t||"EDIT",bt=[];for(let e=0;e<256;++e)bt.push((e+256).toString(16).slice(1));typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto);const At=(e,t)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},wt=(e,t)=>{const n=new Set(t.bodies.map(o=>o.id));return e.bodies.filter(o=>!n.has(o.id))},xt=(e,t)=>t.bodies.map(n=>{const o=e.bodies.find(i=>i.id===n.id);return{newBody:n,oldBody:o&&!D(o,n)?o:void 0}}).filter(({oldBody:n})=>n).map(({oldBody:n,newBody:o})=>({oldBody:n,newBody:o})),yt=(e,t)=>!D(e.target,t.target),it=(e,t)=>{const n=At(e,t),o=wt(e,t),i=xt(e,t);return{oldValue:e,newValue:t,bodiesCreated:n.length>0?n:void 0,bodiesDeleted:o.length>0?o:void 0,bodiesUpdated:i.length>0?i:void 0,targetUpdated:yt(e,t)?{oldTarget:e.target,newTarget:t.target}:void 0}};var C=(e=>(e.LOCAL="LOCAL",e.REMOTE="REMOTE",e))(C||{});const Et=(e,t)=>{var n,o;const{changes:i,origin:s}=t;if(!(!e.options.origin||e.options.origin===s))return!1;if(e.options.ignore){const{ignore:a}=e.options,r=d=>d&&d.length>0;if(!(r(i.created)||r(i.deleted))){const d=(n=i.updated)==null?void 0:n.some(b=>r(b.bodiesCreated)||r(b.bodiesDeleted)||r(b.bodiesUpdated)),u=(o=i.updated)==null?void 0:o.some(b=>b.targetUpdated);if(a==="BODY_ONLY"&&d&&!u||a==="TARGET_ONLY"&&u&&!d)return!1}}if(e.options.annotations){const a=new Set([...(i.created||[]).map(r=>r.id),...(i.deleted||[]).map(r=>r.id),...(i.updated||[]).map(({oldValue:r})=>r.id)]);return!!(Array.isArray(e.options.annotations)?e.options.annotations:[e.options.annotations]).find(r=>a.has(r))}else return!0},vt=(e,t)=>{const n=new Set((e.created||[]).map(h=>h.id)),o=new Set((e.updated||[]).map(({newValue:h})=>h.id)),i=new Set((t.created||[]).map(h=>h.id)),s=new Set((t.deleted||[]).map(h=>h.id)),a=new Set((t.updated||[]).map(({oldValue:h})=>h.id)),r=new Set((t.updated||[]).filter(({oldValue:h})=>n.has(h.id)||o.has(h.id)).map(({oldValue:h})=>h.id)),d=[...(e.created||[]).filter(h=>!s.has(h.id)).map(h=>a.has(h.id)?t.updated.find(({oldValue:v})=>v.id===h.id).newValue:h),...t.created||[]],u=[...(e.deleted||[]).filter(h=>!i.has(h.id)),...(t.deleted||[]).filter(h=>!n.has(h.id))],b=[...(e.updated||[]).filter(({newValue:h})=>!s.has(h.id)).map(h=>{const{oldValue:v,newValue:m}=h;if(a.has(m.id)){const x=t.updated.find(M=>M.oldValue.id===m.id).newValue;return it(v,x)}else return h}),...(t.updated||[]).filter(({oldValue:h})=>!r.has(h.id))];return{created:d,deleted:u,updated:b}},Tt=e=>e.id!==void 0,St=()=>{const e=new Map,t=new Map,n=[],o=(f,w={})=>n.push({onChange:f,options:w}),i=f=>{const w=n.findIndex(A=>A.onChange==f);w>-1&&n.splice(w,1)},s=(f,w)=>{const A={origin:f,changes:{created:w.created||[],updated:w.updated||[],deleted:w.deleted||[]},state:[...e.values()]};n.forEach(S=>{Et(S,A)&&S.onChange(A)})},a=(f,w=C.LOCAL)=>{if(e.get(f.id))throw Error(`Cannot add annotation ${f.id} - exists already`);e.set(f.id,f),f.bodies.forEach(A=>t.set(A.id,f.id)),s(w,{created:[f]})},r=(f,w)=>{const A=typeof f=="string"?w:f,S=typeof f=="string"?f:f.id,B=e.get(S);if(B){const R=it(B,A);return S===A.id?e.set(S,A):(e.delete(S),e.set(A.id,A)),B.bodies.forEach(U=>t.delete(U.id)),A.bodies.forEach(U=>t.set(U.id,A.id)),R}else console.warn(`Cannot update annotation ${S} - does not exist`)},d=(f,w=C.LOCAL,A=C.LOCAL)=>{const S=Tt(w)?A:w,B=r(f,w);B&&s(S,{updated:[B]})},u=(f,w=C.LOCAL)=>{const A=f.reduce((S,B)=>{const R=r(B);return R?[...S,R]:S},[]);A.length>0&&s(w,{updated:A})},b=(f,w=C.LOCAL)=>{const A=e.get(f.annotation);if(A){const S={...A,bodies:[...A.bodies,f]};e.set(A.id,S),t.set(f.id,S.id),s(w,{updated:[{oldValue:A,newValue:S,bodiesCreated:[f]}]})}else console.warn(`Attempt to add body to missing annotation: ${f.annotation}`)},h=()=>[...e.values()],v=(f=C.LOCAL)=>{const w=[...e.values()];e.clear(),t.clear(),s(f,{deleted:w})},m=(f,w=!0,A=C.LOCAL)=>{if(w){const S=[...e.values()];e.clear(),t.clear(),f.forEach(B=>{e.set(B.id,B),B.bodies.forEach(R=>t.set(R.id,B.id))}),s(A,{created:f,deleted:S})}else{const S=f.reduce((B,R)=>{const U=e.get(R.id);return U?[...B,U]:B},[]);if(S.length>0)throw Error(`Bulk insert would overwrite the following annotations: ${S.map(B=>B.id).join(", ")}`);f.forEach(B=>{e.set(B.id,B),B.bodies.forEach(R=>t.set(R.id,B.id))}),s(A,{created:f})}},x=f=>{const w=typeof f=="string"?f:f.id,A=e.get(w);if(A)return e.delete(w),A.bodies.forEach(S=>t.delete(S.id)),A;console.warn(`Attempt to delete missing annotation: ${w}`)},M=(f,w=C.LOCAL)=>{const A=x(f);A&&s(w,{deleted:[A]})},y=(f,w=C.LOCAL)=>{const A=f.reduce((S,B)=>{const R=x(B);return R?[...S,R]:S},[]);A.length>0&&s(w,{deleted:A})},l=(f,w=C.LOCAL)=>{const A=e.get(f.annotation);if(A){const S=A.bodies.find(B=>B.id===f.id);if(S){t.delete(S.id);const B={...A,bodies:A.bodies.filter(R=>R.id!==f.id)};e.set(A.id,B),s(w,{updated:[{oldValue:A,newValue:B,bodiesDeleted:[S]}]})}else console.warn(`Attempt to delete missing body ${f.id} from annotation ${f.annotation}`)}else console.warn(`Attempt to delete body from missing annotation ${f.annotation}`)},c=f=>{const w=e.get(f);return w?{...w}:void 0},g=f=>{const w=t.get(f);if(w){const A=c(w).bodies.find(S=>S.id===f);if(A)return A;console.error(`Store integrity error: body ${f} in index, but not in annotation`)}else console.warn(`Attempt to retrieve missing body: ${f}`)},p=(f,w)=>{if(f.annotation!==w.annotation)throw"Annotation integrity violation: annotation ID must be the same when updating bodies";const A=e.get(f.annotation);if(A){const S=A.bodies.find(R=>R.id===f.id),B={...A,bodies:A.bodies.map(R=>R.id===S.id?w:R)};return e.set(A.id,B),S.id!==w.id&&(t.delete(S.id),t.set(w.id,B.id)),{oldValue:A,newValue:B,bodiesUpdated:[{oldBody:S,newBody:w}]}}else console.warn(`Attempt to add body to missing annotation ${f.annotation}`)},E=(f,w,A=C.LOCAL)=>{const S=p(f,w);S&&s(A,{updated:[S]})},T=(f,w=C.LOCAL)=>{const A=f.map(S=>p({id:S.id,annotation:S.annotation},S)).filter(Boolean);s(w,{updated:A})},L=f=>{const w=e.get(f.annotation);if(w){const A={...w,target:{...w.target,...f}};return e.set(w.id,A),{oldValue:w,newValue:A,targetUpdated:{oldTarget:w.target,newTarget:f}}}else console.warn(`Attempt to update target on missing annotation: ${f.annotation}`)};return{addAnnotation:a,addBody:b,all:h,bulkAddAnnotation:m,bulkDeleteAnnotation:y,bulkUpdateAnnotation:u,bulkUpdateBodies:T,bulkUpdateTargets:(f,w=C.LOCAL)=>{const A=f.map(S=>L(S)).filter(Boolean);A.length>0&&s(w,{updated:A})},clear:v,deleteAnnotation:M,deleteBody:l,getAnnotation:c,getBody:g,observe:o,unobserve:i,updateAnnotation:d,updateBody:E,updateTarget:(f,w=C.LOCAL)=>{const A=L(f);A&&s(w,{updated:[A]})}}};let Bt=()=>({emit(e,...t){for(let n=0,o=this.events[e]||[],i=o.length;n<i;n++)o[n](...t)},events:{},on(e,t){var n;return((n=this.events)[e]||(n[e]=[])).push(t),()=>{var o;this.events[e]=(o=this.events[e])==null?void 0:o.filter(i=>t!==i)}}});const Mt=250,Lt=e=>{const t=Bt(),n=[];let o=-1,i=!1,s=0;const a=m=>{if(!i){const{changes:x}=m,M=performance.now();if(M-s>Mt)n.splice(o+1),n.push(x),o=n.length-1;else{const y=n.length-1;n[y]=vt(n[y],x)}s=M}i=!1};e.observe(a,{origin:C.LOCAL});const r=m=>m&&m.length>0&&e.bulkDeleteAnnotation(m),d=m=>m&&m.length>0&&e.bulkAddAnnotation(m,!1),u=m=>m&&m.length>0&&e.bulkUpdateAnnotation(m.map(({oldValue:x})=>x)),b=m=>m&&m.length>0&&e.bulkUpdateAnnotation(m.map(({newValue:x})=>x)),h=m=>m&&m.length>0&&e.bulkAddAnnotation(m,!1),v=m=>m&&m.length>0&&e.bulkDeleteAnnotation(m);return{canRedo:()=>n.length-1>o,canUndo:()=>o>-1,destroy:()=>e.unobserve(a),on:(m,x)=>t.on(m,x),redo:()=>{if(n.length-1>o){i=!0;const{created:m,updated:x,deleted:M}=n[o+1];d(m),b(x),v(M),t.emit("redo",n[o+1]),o+=1}},undo:()=>{if(o>-1){i=!0;const{created:m,updated:x,deleted:M}=n[o];r(m),u(x),h(M),t.emit("undo",n[o]),o-=1}}}},Ct=()=>{const{subscribe:e,set:t}=J([]);return{subscribe:e,set:t}},Rt=(e,t,n,o)=>{const{store:i,selection:s,hover:a,viewport:r}=e,d=new Map;let u=[],b,h;const v=(l,c)=>{d.has(l)?d.get(l).push(c):d.set(l,[c])},m=(l,c)=>{const g=d.get(l);g&&g.indexOf(c)>0&&g.splice(g.indexOf(c),1)},x=(l,c,g)=>{d.has(l)&&setTimeout(()=>{d.get(l).forEach(p=>{if(n){const E=Array.isArray(c)?c.map(L=>n.serialize(L)):n.serialize(c),T=g?g instanceof PointerEvent?g:n.serialize(g):void 0;p(E,T)}else p(c,g)})},1)},M=()=>{const{selected:l}=s,c=(l||[]).map(({id:g})=>i.getAnnotation(g));c.forEach(g=>{const p=u.find(E=>E.id===g.id);(!p||!D(p,g))&&x("updateAnnotation",g,p)}),u=u.map(g=>c.find(({id:E})=>E===g.id)||g)};s.subscribe(({selected:l})=>{if(!(u.length===0&&l.length===0)){if(u.length===0&&l.length>0)u=l.map(({id:c})=>i.getAnnotation(c));else if(u.length>0&&l.length===0)u.forEach(c=>{const g=i.getAnnotation(c.id);g&&!D(g,c)&&x("updateAnnotation",g,c)}),u=[];else{const c=new Set(u.map(p=>p.id)),g=new Set(l.map(({id:p})=>p));u.filter(p=>!g.has(p.id)).forEach(p=>{const E=i.getAnnotation(p.id);E&&!D(E,p)&&x("updateAnnotation",E,p)}),u=[...u.filter(p=>g.has(p.id)),...l.filter(({id:p})=>!c.has(p)).map(({id:p})=>i.getAnnotation(p))]}x("selectionChanged",u)}}),a.subscribe(l=>{!b&&l?x("mouseEnterAnnotation",i.getAnnotation(l)):b&&!l?x("mouseLeaveAnnotation",i.getAnnotation(b)):b&&l&&(x("mouseLeaveAnnotation",i.getAnnotation(b)),x("mouseEnterAnnotation",i.getAnnotation(l))),b=l}),r==null||r.subscribe(l=>x("viewportIntersect",l.map(c=>i.getAnnotation(c)))),i.observe(l=>{o&&(h&&clearTimeout(h),h=setTimeout(M,1e3));const{created:c,deleted:g}=l.changes;(c||[]).forEach(p=>x("createAnnotation",p)),(g||[]).forEach(p=>x("deleteAnnotation",p)),(l.changes.updated||[]).filter(p=>[...p.bodiesCreated||[],...p.bodiesDeleted||[],...p.bodiesUpdated||[]].length>0).forEach(({oldValue:p,newValue:E})=>{const T=u.find(L=>L.id===p.id)||p;u=u.map(L=>L.id===p.id?E:L),x("updateAnnotation",E,T)})},{origin:C.LOCAL}),i.observe(l=>{if(u){const c=new Set(u.map(p=>p.id)),g=(l.changes.updated||[]).filter(({newValue:p})=>c.has(p.id)).map(({newValue:p})=>p);g.length>0&&(u=u.map(p=>g.find(T=>T.id===p.id)||p))}},{origin:C.REMOTE});const y=l=>c=>{const{created:g,deleted:p,updated:E}=c;(g||[]).forEach(T=>x("createAnnotation",T)),(p||[]).forEach(T=>x("deleteAnnotation",T)),l?(E||[]).forEach(T=>x("updateAnnotation",T.oldValue,T.newValue)):(E||[]).forEach(T=>x("updateAnnotation",T.newValue,T.oldValue))};return t.on("undo",y(!0)),t.on("redo",y(!1)),{on:v,off:m,emit:x}},Ot=e=>t=>t.reduce((n,o)=>{const{parsed:i,error:s}=e.parse(o);return s?{parsed:n.parsed,failed:[...n.failed,o]}:i?{parsed:[...n.parsed,i],failed:n.failed}:{...n}},{parsed:[],failed:[]}),Xt=(e,t,n)=>{const{store:o,selection:i}=e,s=y=>{if(n){const{parsed:l,error:c}=n.parse(y);l?o.addAnnotation(l,C.REMOTE):console.error(c)}else o.addAnnotation(y,C.REMOTE)},a=()=>i.clear(),r=()=>o.clear(),d=y=>{const l=o.getAnnotation(y);return n&&l?n.serialize(l):l},u=()=>n?o.all().map(n.serialize):o.all(),b=()=>{var y;const l=(((y=i.selected)==null?void 0:y.map(c=>c.id))||[]).map(c=>o.getAnnotation(c)).filter(Boolean);return n?l.map(n.serialize):l},h=y=>fetch(y).then(l=>l.json()).then(l=>(m(l),l)),v=y=>{if(typeof y=="string"){const l=o.getAnnotation(y);if(o.deleteAnnotation(y),l)return n?n.serialize(l):l}else{const l=n?n.parse(y).parsed:y;if(l)return o.deleteAnnotation(l),y}},m=y=>{if(n){const{parsed:l,failed:c}=Ot(n)(y);c.length>0&&console.warn(`Discarded ${c.length} invalid annotations`,c),o.bulkAddAnnotation(l,!0,C.REMOTE)}else o.bulkAddAnnotation(y,!0,C.REMOTE)},x=y=>{y?i.setSelected(y):i.clear()},M=y=>{if(n){const l=n.parse(y).parsed,c=n.serialize(o.getAnnotation(l.id));return o.updateAnnotation(l),c}else{const l=o.getAnnotation(y.id);return o.updateAnnotation(y),l}};return{addAnnotation:s,cancelSelected:a,canRedo:t.canRedo,canUndo:t.canUndo,clearAnnotations:r,getAnnotationById:d,getAnnotations:u,getSelected:b,loadAnnotations:h,redo:t.redo,removeAnnotation:v,setAnnotations:m,setSelected:x,undo:t.undo,updateAnnotation:M}};let Ut=e=>crypto.getRandomValues(new Uint8Array(e)),Yt=(e,t,n)=>{let o=(2<<Math.log(e.length-1)/Math.LN2)-1,i=-~(1.6*o*t/e.length);return(s=t)=>{let a="";for(;;){let r=n(i),d=i;for(;d--;)if(a+=e[r[d]&o]||"",a.length===s)return a}}},Dt=(e,t=21)=>Yt(e,t,Ut),It=(e=21)=>crypto.getRandomValues(new Uint8Array(e)).reduce((t,n)=>(n&=63,n<36?t+=n.toString(36):n<62?t+=(n-26).toString(36).toUpperCase():n>62?t+="-":t+="_",t),"");const Vt=()=>({isGuest:!0,id:Dt("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",20)()});It();function _t(e,t,n,o,i){st(e,t,n||0,o||e.length-1,i||kt)}function st(e,t,n,o,i){for(;o>n;){if(o-n>600){var s=o-n+1,a=t-n+1,r=Math.log(s),d=.5*Math.exp(2*r/3),u=.5*Math.sqrt(r*d*(s-d)/s)*(a-s/2<0?-1:1),b=Math.max(n,Math.floor(t-a*d/s+u)),h=Math.min(o,Math.floor(t+(s-a)*d/s+u));st(e,t,b,h,i)}var v=e[t],m=n,x=o;for(P(e,n,t),i(e[o],v)>0&&P(e,n,o);m<x;){for(P(e,m,x),m++,x--;i(e[m],v)<0;)m++;for(;i(e[x],v)>0;)x--}i(e[n],v)===0?P(e,n,x):(x++,P(e,x,o)),x<=t&&(n=x+1),t<=x&&(o=x-1)}}function P(e,t,n){var o=e[t];e[t]=e[n],e[n]=o}function kt(e,t){return e<t?-1:e>t?1:0}class Nt{constructor(t=9){this._maxEntries=Math.max(4,t),this._minEntries=Math.max(2,Math.ceil(this._maxEntries*.4)),this.clear()}all(){return this._all(this.data,[])}search(t){let n=this.data;const o=[];if(!$(t,n))return o;const i=this.toBBox,s=[];for(;n;){for(let a=0;a<n.children.length;a++){const r=n.children[a],d=n.leaf?i(r):r;$(t,d)&&(n.leaf?o.push(r):Q(t,d)?this._all(r,o):s.push(r))}n=s.pop()}return o}collides(t){let n=this.data;if(!$(t,n))return!1;const o=[];for(;n;){for(let i=0;i<n.children.length;i++){const s=n.children[i],a=n.leaf?this.toBBox(s):s;if($(t,a)){if(n.leaf||Q(t,a))return!0;o.push(s)}}n=o.pop()}return!1}load(t){if(!(t&&t.length))return this;if(t.length<this._minEntries){for(let o=0;o<t.length;o++)this.insert(t[o]);return this}let n=this._build(t.slice(),0,t.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(t){return t&&this._insert(t,this.data.height-1),this}clear(){return this.data=k([]),this}remove(t,n){if(!t)return this;let o=this.data;const i=this.toBBox(t),s=[],a=[];let r,d,u;for(;o||s.length;){if(o||(o=s.pop(),d=s[s.length-1],r=a.pop(),u=!0),o.leaf){const b=Pt(t,o.children,n);if(b!==-1)return o.children.splice(b,1),s.push(o),this._condense(s),this}!u&&!o.leaf&&Q(o,i)?(s.push(o),a.push(r),r=0,d=o,o=o.children[0]):d?(r++,o=d.children[r],u=!1):o=null}return this}toBBox(t){return t}compareMinX(t,n){return t.minX-n.minX}compareMinY(t,n){return t.minY-n.minY}toJSON(){return this.data}fromJSON(t){return this.data=t,this}_all(t,n){const o=[];for(;t;)t.leaf?n.push(...t.children):o.push(...t.children),t=o.pop();return n}_build(t,n,o,i){const s=o-n+1;let a=this._maxEntries,r;if(s<=a)return r=k(t.slice(n,o+1)),_(r,this.toBBox),r;i||(i=Math.ceil(Math.log(s)/Math.log(a)),a=Math.ceil(s/Math.pow(a,i-1))),r=k([]),r.leaf=!1,r.height=i;const d=Math.ceil(s/a),u=d*Math.ceil(Math.sqrt(a));at(t,n,o,u,this.compareMinX);for(let b=n;b<=o;b+=u){const h=Math.min(b+u-1,o);at(t,b,h,d,this.compareMinY);for(let v=b;v<=h;v+=d){const m=Math.min(v+d-1,h);r.children.push(this._build(t,v,m,i-1))}}return _(r,this.toBBox),r}_chooseSubtree(t,n,o,i){for(;i.push(n),!(n.leaf||i.length-1===o);){let s=1/0,a=1/0,r;for(let d=0;d<n.children.length;d++){const u=n.children[d],b=Z(u),h=Ft(t,u)-b;h<a?(a=h,s=b<s?b:s,r=u):h===a&&b<s&&(s=b,r=u)}n=r||n.children[0]}return n}_insert(t,n,o){const i=o?t:this.toBBox(t),s=[],a=this._chooseSubtree(i,this.data,n,s);for(a.children.push(t),z(a,i);n>=0&&s[n].children.length>this._maxEntries;)this._split(s,n),n--;this._adjustParentBBoxes(i,s,n)}_split(t,n){const o=t[n],i=o.children.length,s=this._minEntries;this._chooseSplitAxis(o,s,i);const a=this._chooseSplitIndex(o,s,i),r=k(o.children.splice(a,o.children.length-a));r.height=o.height,r.leaf=o.leaf,_(o,this.toBBox),_(r,this.toBBox),n?t[n-1].children.push(r):this._splitRoot(o,r)}_splitRoot(t,n){this.data=k([t,n]),this.data.height=t.height+1,this.data.leaf=!1,_(this.data,this.toBBox)}_chooseSplitIndex(t,n,o){let i,s=1/0,a=1/0;for(let r=n;r<=o-n;r++){const d=H(t,0,r,this.toBBox),u=H(t,r,o,this.toBBox),b=$t(d,u),h=Z(d)+Z(u);b<s?(s=b,i=r,a=h<a?h:a):b===s&&h<a&&(a=h,i=r)}return i||o-n}_chooseSplitAxis(t,n,o){const i=t.leaf?this.compareMinX:Ht,s=t.leaf?this.compareMinY:zt,a=this._allDistMargin(t,n,o,i),r=this._allDistMargin(t,n,o,s);a<r&&t.children.sort(i)}_allDistMargin(t,n,o,i){t.children.sort(i);const s=this.toBBox,a=H(t,0,n,s),r=H(t,o-n,o,s);let d=F(a)+F(r);for(let u=n;u<o-n;u++){const b=t.children[u];z(a,t.leaf?s(b):b),d+=F(a)}for(let u=o-n-1;u>=n;u--){const b=t.children[u];z(r,t.leaf?s(b):b),d+=F(r)}return d}_adjustParentBBoxes(t,n,o){for(let i=o;i>=0;i--)z(n[i],t)}_condense(t){for(let n=t.length-1,o;n>=0;n--)t[n].children.length===0?n>0?(o=t[n-1].children,o.splice(o.indexOf(t[n]),1)):this.clear():_(t[n],this.toBBox)}}function Pt(e,t,n){if(!n)return t.indexOf(e);for(let o=0;o<t.length;o++)if(n(e,t[o]))return o;return-1}function _(e,t){H(e,0,e.children.length,t,e)}function H(e,t,n,o,i){i||(i=k(null)),i.minX=1/0,i.minY=1/0,i.maxX=-1/0,i.maxY=-1/0;for(let s=t;s<n;s++){const a=e.children[s];z(i,e.leaf?o(a):a)}return i}function z(e,t){return e.minX=Math.min(e.minX,t.minX),e.minY=Math.min(e.minY,t.minY),e.maxX=Math.max(e.maxX,t.maxX),e.maxY=Math.max(e.maxY,t.maxY),e}function Ht(e,t){return e.minX-t.minX}function zt(e,t){return e.minY-t.minY}function Z(e){return(e.maxX-e.minX)*(e.maxY-e.minY)}function F(e){return e.maxX-e.minX+(e.maxY-e.minY)}function Ft(e,t){return(Math.max(t.maxX,e.maxX)-Math.min(t.minX,e.minX))*(Math.max(t.maxY,e.maxY)-Math.min(t.minY,e.minY))}function $t(e,t){const n=Math.max(e.minX,t.minX),o=Math.max(e.minY,t.minY),i=Math.min(e.maxX,t.maxX),s=Math.min(e.maxY,t.maxY);return Math.max(0,i-n)*Math.max(0,s-o)}function Q(e,t){return e.minX<=t.minX&&e.minY<=t.minY&&t.maxX<=e.maxX&&t.maxY<=e.maxY}function $(e,t){return t.minX<=e.maxX&&t.minY<=e.maxY&&t.maxX>=e.minX&&t.maxY>=e.minY}function k(e){return{children:e,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function at(e,t,n,o,i){const s=[t,n];for(;s.length;){if(n=s.pop(),t=s.pop(),n-t<=o)continue;const a=t+Math.ceil((n-t)/o/2)*o;_t(e,a,t,n,i),s.push(t,a,a,n)}}const Wt=(e,t)=>{const n=s=>Math.round(s*10)/10,o={top:n(e.top),bottom:n(e.bottom),left:n(e.left),right:n(e.right)},i={top:n(t.top),bottom:n(t.bottom),left:n(t.left),right:n(t.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"},qt=(e,t)=>{const n=Math.min(e.left,t.left),o=Math.max(e.right,t.right),i=Math.min(e.top,t.top),s=Math.max(e.bottom,t.bottom);return new DOMRect(n,i,o-n,s-i)},jt=e=>e.reduce((t,n)=>{if(n.width===0||n.height===0)return t;let o=[...t],i=!1;for(const s of t){const a=Wt(n,s);if(a==="inline-adjacent"){o=o.map(r=>r===s?qt(n,s):r),i=!0;break}else if(a==="inline-contains"){o=o.map(r=>r===s?n:r),i=!0;break}else if(a==="inline-is-contained"){i=!0;break}else if(a==="block-contains"||a==="block-is-contained"){n.width<s.width&&(o=o.map(r=>r===s?n:r)),i=!0;break}}return i?o:[...o,n]},[]),Gt=e=>{const{startContainer:t,endContainer:n}=e;if(t.nodeType===Node.TEXT_NODE&&n.nodeType===Node.TEXT_NODE)return e;if(t.nodeType!==Node.TEXT_NODE){const i=t.nextSibling||t.parentNode,s=i.nodeType===Node.TEXT_NODE?i:Array.from(i.childNodes).filter(a=>a.nodeType===Node.TEXT_NODE).shift();e.setEnd(s,0)}if(n.nodeType!==Node.TEXT_NODE){const i=n.previousSibling||n.parentNode,s=i.nodeType===Node.TEXT_NODE?i:Array.from(i.childNodes).filter(a=>a.nodeType===Node.TEXT_NODE).pop();e.setEnd(s,s.textContent.length)}return e},Jt=(e,t)=>{const n=new Nt,o=new Map,i=l=>{const c=t.getBoundingClientRect(),p=l.selector.range instanceof Range&&!l.selector.range.collapsed&&l.selector.range.startContainer.nodeType===Node.TEXT_NODE&&l.selector.range.endContainer.nodeType===Node.TEXT_NODE?l:I(l,t),E=Array.from(p.selector.range.getClientRects()),T=jt(E);return T.map(L=>{const{x:f,y:w,width:A,height:S}=L;return{minX:f-c.x,minY:w-c.y,maxX:f-c.x+A,maxY:w-c.y+S,annotation:{id:l.annotation,rects:T}}})},s=()=>[...o.values()],a=()=>{n.clear(),o.clear()},r=l=>{const c=i(l);c.forEach(g=>n.insert(g)),o.set(l.annotation,c)},d=l=>{o.get(l.annotation).forEach(g=>n.remove(g)),o.delete(l.annotation)},u=l=>{d(l),r(l)},b=(l,c=!0)=>{c&&a();const g=l.map(E=>({target:E,rects:i(E)}));g.forEach(({target:E,rects:T})=>o.set(E.annotation,T));const p=g.reduce((E,{rects:T})=>[...E,...T],[]);n.load(p)},h=(l,c)=>{const g=n.search({minX:l,minY:c,maxX:l,maxY:c}),p=E=>E.annotation.rects.reduce((T,L)=>T+L.width*L.height,0);if(g.length>0)return g.sort((E,T)=>p(E)-p(T)),g[0].annotation.id},v=l=>{const c=m(l);if(c.length===0)return;let g=c[0].left,p=c[0].top,E=c[0].right,T=c[0].bottom;for(let L=1;L<c.length;L++){const f=c[L];g=Math.min(g,f.left),p=Math.min(p,f.top),E=Math.max(E,f.right),T=Math.max(T,f.bottom)}return new DOMRect(g,p,E-g,T-p)},m=l=>{const c=o.get(l);return c?c[0].annotation.rects:[]};return{all:s,clear:a,getAt:h,getBoundsForAnnotation:v,getDOMRectsForAnnotation:m,getIntersectingRects:(l,c,g,p)=>n.search({minX:l,minY:c,maxX:g,maxY:p}),insert:r,recalculate:()=>b(e.all().map(l=>l.target),!0),remove:d,set:b,size:()=>n.all().length,update:u}},W=e=>{const{range:t}=e.target.selector;return t instanceof Range&&!t.collapsed},rt=(e,t)=>{const n=St(),o=Jt(n,e),i=pt(n,t),s=gt(n),a=Ct(),r=(y,l=C.LOCAL)=>{const c=y.target.selector.range instanceof Range?y:{...y,target:I(y.target,e)},{range:g}=c.target.selector,p=g&&!g.collapsed;return p&&n.addAnnotation(c,l),p},d=(y,l=!0,c=C.LOCAL)=>{const g=y.map(E=>W(E)?E:{...E,target:I(E.target,e)});if(g.some(E=>E.target.selector.range.collapsed)){const E=g.filter(T=>T.target.selector.range.collapsed);return console.warn("Could not revive all targets"),console.warn(E),n.bulkAddAnnotation(g,l,c),E}else return n.bulkAddAnnotation(g,l,c),[]},u=(y,l=C.LOCAL)=>{const c=y.selector.range instanceof Range?y:I(y,e);n.updateTarget(c,l)},b=(y,l=C.LOCAL)=>{const c=y.map(g=>g.selector.range instanceof Range?g:I(g,e));n.bulkUpdateTargets(c,l)},h=(y,l)=>{const c=o.getAt(y,l);return c?n.getAnnotation(c):void 0},v=(y,l,c,g)=>{const p=o.getIntersectingRects(y,l,c,g);return Array.from(new Set(p.map(T=>T.annotation.id))).map(T=>n.getAnnotation(T)).filter(T=>T)},m=(y,l,c,g=5)=>{const p=o.getDOMRectsForAnnotation(y);if(p.length>0){if(l&&c){const E=p.find(({top:T,right:L,bottom:f,left:w})=>l>=w-g&&l<=L+g&&c>=T-g&&c<=f+g);if(E)return E}return o.getBoundsForAnnotation(y)}},x=(y,l,c,g)=>{const E=o.getIntersectingRects(y,l,c,g).reduce((T,L)=>((T[L.annotation.id]=T[L.annotation.id]||[]).push(L),T),{});return Object.entries(E).map(([T,L])=>({annotation:n.getAnnotation(T),rects:L.map(({minX:f,minY:w,maxX:A,maxY:S})=>({x:f,y:w,width:A-f,height:S-w}))}))},M=()=>o.recalculate();return n.observe(({changes:y})=>{const l=(y.created||[]).filter(W),c=(y.deleted||[]).filter(W),g=(y.updated||[]).filter(p=>W(p.newValue));l.length>0&&o.set(l.map(p=>p.target),!1),(c==null?void 0:c.length)>0&&c.forEach(p=>o.remove(p.target)),(g==null?void 0:g.length)>0&&g.forEach(({newValue:p})=>o.update(p.target))}),{store:{...n,addAnnotation:r,bulkAddAnnotation:d,bulkUpdateTargets:b,getAnnotationBounds:m,getAt:h,getIntersecting:v,getIntersectingRects:x,recalculatePositions:M,updateTarget:u},selection:i,hover:s,viewport:a}},Kt=(e,t={})=>{const n=new Map,o=s=>Array.from(n.entries()).filter(([a,r])=>r.presenceKey===s.presenceKey).map(([a,r])=>a);return e.on("selectionChange",(s,a)=>{o(s).forEach(d=>n.delete(d)),a&&a.forEach(d=>n.set(d,s))}),{paint:(s,a,r,d,u,b)=>{t.font&&(d.font=t.font);const h=n.get(s.id);if(h){const{x:v,y:m,height:x}=a[0];d.fillStyle=h.appearance.color,d.fillRect(v-2,m-2.5,2,x+5);const M=d.measureText(h.appearance.label),y=M.width+6,l=M.actualBoundingBoxAscent+M.actualBoundingBoxDescent+8,c=M.fontBoundingBoxAscent?8:6.5;d.fillRect(v-2,m-2.5-l,y,l),d.fillStyle="#fff",d.fillText(h.appearance.label,v+1,m-c),r.fillStyle=h.appearance.color,r.globalAlpha=u?.45:.18,a.forEach(({x:g,y:p,width:E,height:T})=>r.fillRect(g,p-2.5,E,T+5))}else j.paint(s,a,r,d,u,b)}}},lt=e=>{if(e===null)return document.scrollingElement;const{overflowY:t}=window.getComputedStyle(e);return t!=="visible"&&t!=="hidden"&&e.scrollHeight>e.clientHeight?e:lt(e.parentElement)},Zt=(e,t)=>n=>{const o=lt(e);if(o){const i=t.getAnnotation(n.id),{range:s}=i.target.selector;if(!s||s.collapsed)return!1;const a=o.getBoundingClientRect(),r=o.clientHeight,d=o.clientWidth,u=i.target.selector.range.getBoundingClientRect(),{width:b,height:h}=t.getAnnotationBounds(n.id),v=u.top-a.top,m=u.left-a.left,x=o.parentElement?o.scrollTop:0,M=o.parentElement?o.scrollLeft:0,y=v+x-(r-h)/2,l=m+M-(d-b)/2;return o.scroll({top:y,left:l,behavior:"smooth"}),!0}};let q;const Qt=new Uint8Array(16);function te(){if(!q&&(q=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!q))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return q(Qt)}const O=[];for(let e=0;e<256;++e)O.push((e+256).toString(16).slice(1));function ee(e,t=0){return O[e[t+0]]+O[e[t+1]]+O[e[t+2]]+O[e[t+3]]+"-"+O[e[t+4]]+O[e[t+5]]+"-"+O[e[t+6]]+O[e[t+7]]+"-"+O[e[t+8]]+O[e[t+9]]+"-"+O[e[t+10]]+O[e[t+11]]+O[e[t+12]]+O[e[t+13]]+O[e[t+14]]+O[e[t+15]]}const ct={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function ne(e,t,n){if(ct.randomUUID&&!t&&!e)return ct.randomUUID();e=e||{};const o=e.random||(e.rng||te)();if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,t){n=n||0;for(let i=0;i<16;++i)t[n+i]=o[i];return t}return ee(o)}const oe=(e,t,n)=>{const o=document.createRange(),i=n?e.startContainer.parentElement.closest(n):t;o.setStart(i,0),o.setEnd(e.startContainer,e.startOffset);const s=e.toString(),a=o.toString().length,r=a+s.length;return n?{quote:s,start:a,end:r,range:e,offsetReference:i}:{quote:s,start:a,end:r,range:e}},ie=(e,t,n)=>{const{store:o,selection:i}=t;let s,a=null;const r=v=>s=v;let d=!1,u;e.addEventListener("selectstart",v=>{if(!d)return;!v.target.parentElement.closest(".not-annotatable")?a={annotation:ne(),selector:void 0,creator:s,created:new Date}:(a=null,u=void 0)});let b;document.addEventListener("selectionchange",v=>{b&&clearTimeout(b),b=setTimeout(()=>h(),50)});const h=()=>{var m,x;const v=document.getSelection();if(!v.isCollapsed&&d&&a){const M=Array.from(Array(v.rangeCount).keys()).map(c=>v.getRangeAt(c));Gt(M[0]).toString()!==((x=(m=a.selector)==null?void 0:m.range)==null?void 0:x.toString())&&(a={...a,selector:oe(M[0],e,n)},o.getAnnotation(a.annotation)?o.updateTarget(a,C.LOCAL):(o.addAnnotation({id:a.annotation,bodies:[],target:a}),i.clickSelect(a.annotation,u)))}};return e.addEventListener("pointerdown",v=>{u=v,d=v.button===0}),document.addEventListener("pointerup",v=>{var x;u=v,!((x=v.target.parentElement)!=null&&x.closest(".not-annotatable")||!d)&&setTimeout(()=>{if(a!=null&&a.selector)o.updateTarget(a,C.LOCAL),i.clickSelect(a.annotation,v),a=null,u=void 0;else{const{x:M,y}=e.getBoundingClientRect(),l=o.getAt(v.clientX-M,v.clientY-y);if(l){const{selected:c}=i;(c.length!==1||c[0].id!==l.id)&&(i.clickSelect(l.id,v),u=void 0)}else i.isEmpty()||i.clear()}},50)}),{setUser:r}},se=(e,t={})=>{const n=rt(e,t.pointerAction),{selection:o,viewport:i}=n,s=n.store,a=Lt(s),r=Rt(n,a,t.adapter);let d=Vt();const u=nt(e,n,i);t.style&&u.setDrawingStyle(t.style);const b=ie(e,n,t.offsetReferenceSelector);return b.setUser(d),{...Xt(n,a,t.adapter),destroy:()=>{throw"Not implemented yet"},element:e,getUser:()=>d,setFilter:g=>u.setFilter(g),setStyle:g=>u.setDrawingStyle(g),setUser:g=>{d=g,b.setUser(g)},setSelected:g=>{g?o.setSelected(g):o.clear()},setPresenceProvider:g=>{g&&(u.setPainter(Kt(g,t.presence)),g.on("selectionChange",()=>u.redraw()))},on:r.on,off:r.off,scrollIntoView:Zt(e,s),state:n}};X.Origin=C,X.createHighlightLayer=nt,X.createTextAnnotator=se,X.createTextAnnotatorState=rt,X.defaultPainter=j,X.reviveTarget=I,Object.defineProperty(X,Symbol.toStringTag,{value:"Module"})});
1
+ (function(O,J){typeof exports=="object"&&typeof module<"u"?J(exports):typeof define=="function"&&define.amd?define(["exports"],J):(O=typeof globalThis<"u"?globalThis:O||self,J(O.RecogitoJS={}))})(this,function(O){"use strict";const J="not-annotatable",Z=`.${J}`,et=t=>{var n;return!!(t instanceof HTMLElement?t.closest(Z):(n=t.parentElement)==null?void 0:n.closest(Z))},jt=t=>{const e=t.commonAncestorContainer;return!et(e)},Ft=t=>t.addEventListener("click",e=>{!e.target.closest(Z)&&!e.target.closest("a")&&e.preventDefault()}),yt=t=>({...t,type:t.type,x:t.x,y:t.y,clientX:t.clientX,clientY:t.clientY,offsetX:t.offsetX,offsetY:t.offsetY,screenX:t.screenX,screenY:t.screenY,isPrimary:t.isPrimary,altKey:t.altKey,ctrlKey:t.ctrlKey,metaKey:t.metaKey,shiftKey:t.shiftKey,button:t.button,buttons:t.buttons,currentTarget:t.currentTarget,target:t.target,defaultPrevented:t.defaultPrevented,detail:t.detail,eventPhase:t.eventPhase,pointerId:t.pointerId,pointerType:t.pointerType,timeStamp:t.timeStamp}),st=t=>({...t,type:t.type,key:t.key,code:t.code,location:t.location,repeat:t.repeat,altKey:t.altKey,ctrlKey:t.ctrlKey,metaKey:t.metaKey,shiftKey:t.shiftKey,currentTarget:t.currentTarget,target:t.target,defaultPrevented:t.defaultPrevented,detail:t.detail,timeStamp:t.timeStamp}),zt=/mac/i.test(navigator.userAgentData?navigator.userAgentData.platform:navigator.platform),Wt=t=>{!t.hasAttribute("tabindex")&&t.tabIndex<0&&t.setAttribute("tabindex","-1"),t.classList.add("no-focus-outline")},bt=(t,e=10)=>{let n;return(...o)=>{clearTimeout(n),n=setTimeout(()=>t.apply(void 0,o),e)}},qe=function*(t){const e=document.createNodeIterator(t.commonAncestorContainer,NodeFilter.SHOW_ELEMENT,o=>o instanceof HTMLElement&&o.classList.contains(J)&&!o.parentElement.closest(Z)&&t.intersectsNode(o)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP);let n;for(;n=e.nextNode();)n instanceof HTMLElement&&(yield n)},qt=t=>{if(!jt(t))return[];const e=[];let n=null;for(const o of qe(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]},At=t=>{const e=t.cloneContents();return e.querySelectorAll(Z).forEach(n=>n.remove()),e},Gt=(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 a=At(s).textContent,r=document.createRange();r.setStart(t.endContainer,t.endOffset),i===document.body?r.setEnd(i,i.childNodes.length):r.setEndAfter(i);const l=At(r).textContent;return{prefix:a.substring(a.length-n),suffix:l.substring(0,n)}},z=t=>t.every(e=>e.range instanceof Range&&!e.range.collapsed),Qt=/^\s*$/,Jt=t=>Qt.test(t.toString()),Ge=(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"},Qe=(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)},Zt=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 a=Ge(n,s);if(a==="inline-adjacent"){o=o.map(r=>r===s?Qe(n,s):r),i=!0;break}else if(a==="inline-contains"){o=o.map(r=>r===s?n:r),i=!0;break}else if(a==="inline-is-contained"){i=!0;break}else if(a==="block-contains"||a==="block-is-contained"){n.width<s.width&&(o=o.map(r=>r===s?n:r)),i=!0;break}}return i?o:[...o,n]},[]),Je=t=>({length:t.length,item:e=>t[e],[Symbol.iterator]:function*(){for(let e=0;e<this.length;e++)yield this.item(e)}}),te=(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=At(o).textContent,a=t.toString(),r=s.length||0,l=r+a.length;return n?{quote:a,start:r,end:l,range:t,offsetReference:i}:{quote:a,start:r,end:l,range:t}},Ot=(t,e)=>{var h,u;const{start:n,end:o}=t,i=t.offsetReference||e,s=document.createNodeIterator(e,NodeFilter.SHOW_TEXT,g=>{var v;return(v=g.parentElement)!=null&&v.closest(Z)?NodeFilter.FILTER_SKIP:NodeFilter.FILTER_ACCEPT});let a=0;const r=document.createRange();let l=s.nextNode();l===null&&console.error("Could not revive annotation target. Content missing.");let f=!i;for(;l!==null;){if(f||(f=typeof(i==null?void 0:i.contains)=="function"?i.contains(l):!1),f){const g=((h=l.textContent)==null?void 0:h.length)||0;if(a+g>n){r.setStart(l,n-a);break}a+=g}l=s.nextNode()}for(;l!==null;){const g=((u=l.textContent)==null?void 0:u.length)||0;if(a+g>=o){r.setEnd(l,o-a);break}a+=g,l=s.nextNode()}return{...t,range:r}},rt=(t,e)=>z(t.selector)?t:{...t,selector:t.selector.map(n=>n.range instanceof Range&&!n.range.collapsed?n:Ot(n,e))},wt=(t,e)=>z(t.target.selector)?t:{...t,target:rt(t.target,e)},ee=(t,e)=>{const n=t.cloneRange();return e.contains(n.startContainer)||n.setStart(e,0),e.contains(n.endContainer)||n.setEnd(e,e.childNodes.length),n},ne=t=>{if(t===null)return document.scrollingElement;const{overflowY:e}=window.getComputedStyle(t);return e!=="visible"&&e!=="hidden"&&t.scrollHeight>t.clientHeight?t:ne(t.parentElement)},oe=(t,e)=>n=>{const o=typeof n=="string"?n:n.id,i=a=>{const r=s.getBoundingClientRect(),l=s.clientHeight,f=s.clientWidth,h=a.selector[0].range.getBoundingClientRect(),{width:u,height:g}=e.getAnnotationBounds(o),v=h.top-r.top,p=h.left-r.left,m=s.parentElement?s.scrollTop:0,w=s.parentElement?s.scrollLeft:0,d=v+m-(l-g)/2,c=p+w-(f-u)/2;s.scroll({top:d,left:c,behavior:"smooth"})},s=ne(t);if(s){const a=e.getAnnotation(o),{range:r}=a.target.selector[0];if(r&&!r.collapsed)return i(a.target),!0;{const l=rt(a.target,t),{range:f}=l.selector[0];if(f&&!f.collapsed)return i(l),!0}}return!1},q={fill:"rgb(0, 128, 255)",fillOpacity:.18},at={fill:"rgb(0, 128, 255)",fillOpacity:.45},ie=(t,e,n,o,i)=>{var a,r;const s=n?typeof n=="function"?n(t.annotation,t.state,i)||((a=t.state)!=null&&a.selected?at:q):n:(r=t.state)!=null&&r.selected?at:q;return o&&o.paint(t,e)||s},Ze=t=>{const{top:e,left:n}=t.getBoundingClientRect(),{innerWidth:o,innerHeight:i}=window,s=-n,a=-e,r=o-n,l=i-e;return{top:e,left:n,minX:s,minY:a,maxX:r,maxY:l}},tn=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)}},Rt=(t,e,n,o)=>{const{store:i,selection:s,hover:a}=e;let r,l,f;const h=tn(n),u=B=>{const{x:T,y:_}=t.getBoundingClientRect(),P=i.getAt(B.clientX-T,B.clientY-_,!1,l);P?a.current!==P.id&&(t.classList.add("hovered"),a.set(P.id)):a.current&&(t.classList.remove("hovered"),a.set(null))};t.addEventListener("pointermove",u);const g=(B=!1)=>{f&&f.clear();const T=Ze(t),{minX:_,minY:P,maxX:y,maxY:E}=T,A=l?i.getIntersecting(_,P,y,E).filter(({annotation:R})=>l(R)):i.getIntersecting(_,P,y,E),L=s.selected.map(({id:R})=>R),I=A.map(({annotation:R,rects:N})=>{const H=L.includes(R.id),vo=R.id===a.current;return{annotation:R,rects:N,state:{selected:H,hover:vo}}});o.redraw(I,T,r,f,B),setTimeout(()=>h(A.map(({annotation:R})=>R)),1)},v=B=>{f=B,g()},p=B=>{r=B,g()},m=B=>{l=B,g(!1)},w=()=>g();i.observe(w);const d=s.subscribe(()=>g()),c=()=>g(!0);document.addEventListener("scroll",c,{capture:!0,passive:!0});const b=bt(()=>{i.recalculatePositions(),f&&f.reset(),g()});window.addEventListener("resize",b);const x=new ResizeObserver(b);x.observe(t);const S={attributes:!0,childList:!0,subtree:!0},C=new MutationObserver(B=>{B.every(_=>_.target===t||t.contains(_.target))||g(!0)});return C.observe(document.body,S),{destroy:()=>{t.removeEventListener("pointermove",u),o.destroy(),i.unobserve(w),d(),document.removeEventListener("scroll",c),window.removeEventListener("resize",b),x.disconnect(),C.disconnect()},redraw:g,setStyle:p,setFilter:m,setPainter:v,setVisible:o.setVisible}},en=()=>{const t=document.createElement("canvas");return t.width=window.innerWidth,t.height=window.innerHeight,t.className="r6o-canvas-highlight-layer bg",t},nn=(t,e)=>{t.width=window.innerWidth,t.height=window.innerHeight},on=t=>{t.classList.add("r6o-annotatable");const e=en(),n=e.getContext("2d");document.body.appendChild(e);const o=(r,l,f,h)=>requestAnimationFrame(()=>{const{width:u,height:g}=e;n.clearRect(-.5,-.5,u+1,g+1),h&&h.clear();const{top:v,left:p}=l;[...r].sort((w,d)=>{const{annotation:{target:{created:c}}}=w,{annotation:{target:{created:b}}}=d;return c.getTime()-b.getTime()}).forEach(w=>{var x;const d=f?typeof f=="function"?f(w.annotation,w.state):f:(x=w.state)!=null&&x.selected?at:q,c=h&&h.paint(w,l)||d,b=w.rects.map(({x:S,y:C,width:M,height:B})=>({x:S+p,y:C+v,width:M,height:B}));if(n.fillStyle=c.fill,n.globalAlpha=c.fillOpacity||1,b.forEach(({x:S,y:C,width:M,height:B})=>n.fillRect(S,C,M,B)),c.underlineColor){n.globalAlpha=1,n.strokeStyle=c.underlineColor,n.lineWidth=c.underlineThickness??1;const S=c.underlineOffset??0;b.forEach(({x:C,y:M,width:B,height:T})=>{n.beginPath(),n.moveTo(C,M+T+S),n.lineTo(C+B,M+T+S),n.stroke()})}})}),i=bt(()=>{nn(e)});return window.addEventListener("resize",i),{destroy:()=>{e.remove(),window.removeEventListener("resize",i)},setVisible:r=>{console.log("setVisible not implemented on Canvas renderer")},redraw:o}},se=(t,e,n)=>Rt(t,e,n,on(t));var sn={grad:.9,turn:360,rad:360/(2*Math.PI)},G=function(t){return typeof t=="string"?t.length>0:typeof t=="number"},V=function(t,e,n){return e===void 0&&(e=0),n===void 0&&(n=Math.pow(10,e)),Math.round(n*t)/n+0},$=function(t,e,n){return e===void 0&&(e=0),n===void 0&&(n=1),t>n?n:t>e?t:e},re=function(t){return(t=isFinite(t)?t%360:0)>0?t:t+360},ae=function(t){return{r:$(t.r,0,255),g:$(t.g,0,255),b:$(t.b,0,255),a:$(t.a)}},Mt=function(t){return{r:V(t.r),g:V(t.g),b:V(t.b),a:V(t.a,3)}},rn=/^#([0-9a-f]{3,8})$/i,Et=function(t){var e=t.toString(16);return e.length<2?"0"+e:e},ce=function(t){var e=t.r,n=t.g,o=t.b,i=t.a,s=Math.max(e,n,o),a=s-Math.min(e,n,o),r=a?s===e?(n-o)/a:s===n?2+(o-e)/a:4+(e-n)/a:0;return{h:60*(r<0?r+6:r),s:s?a/s*100:0,v:s/255*100,a:i}},le=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),a=o*(1-n),r=o*(1-(e-s)*n),l=o*(1-(1-e+s)*n),f=s%6;return{r:255*[o,r,a,a,l,o][f],g:255*[l,o,o,r,a,a][f],b:255*[a,a,l,o,o,r][f],a:i}},de=function(t){return{h:re(t.h),s:$(t.s,0,100),l:$(t.l,0,100),a:$(t.a)}},ue=function(t){return{h:V(t.h),s:V(t.s),l:V(t.l),a:V(t.a,3)}},fe=function(t){return le((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},ct=function(t){return{h:(e=ce(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},an=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*,\s*([+-]?\d*\.?\d+)%\s*,\s*([+-]?\d*\.?\d+)%\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,cn=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,ln=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,dn=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,he={string:[[function(t){var e=rn.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?V(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?V(parseInt(t.substr(6,2),16)/255,2):1}:null:null},"hex"],[function(t){var e=ln.exec(t)||dn.exec(t);return e?e[2]!==e[4]||e[4]!==e[6]?null:ae({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=an.exec(t)||cn.exec(t);if(!e)return null;var n,o,i=de({h:(n=e[1],o=e[2],o===void 0&&(o="deg"),Number(n)*(sn[o]||1)),s:Number(e[3]),l:Number(e[4]),a:e[5]===void 0?1:Number(e[5])/(e[6]?100:1)});return fe(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 G(e)&&G(n)&&G(o)?ae({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(!G(e)||!G(n)||!G(o))return null;var a=de({h:Number(e),s:Number(n),l:Number(o),a:Number(s)});return fe(a)},"hsl"],[function(t){var e=t.h,n=t.s,o=t.v,i=t.a,s=i===void 0?1:i;if(!G(e)||!G(n)||!G(o))return null;var a=function(r){return{h:re(r.h),s:$(r.s,0,100),v:$(r.v,0,100),a:$(r.a)}}({h:Number(e),s:Number(n),v:Number(o),a:Number(s)});return le(a)},"hsv"]]},ge=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]},un=function(t){return typeof t=="string"?ge(t.trim(),he.string):typeof t=="object"&&t!==null?ge(t,he.object):[null,void 0]},Bt=function(t,e){var n=ct(t);return{h:n.h,s:$(n.s+100*e,0,100),l:n.l,a:n.a}},kt=function(t){return(299*t.r+587*t.g+114*t.b)/1e3/255},pe=function(t,e){var n=ct(t);return{h:n.h,s:n.s,l:$(n.l+100*e,0,100),a:n.a}},me=function(){function t(e){this.parsed=un(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 V(kt(this.rgba),2)},t.prototype.isDark=function(){return kt(this.rgba)<.5},t.prototype.isLight=function(){return kt(this.rgba)>=.5},t.prototype.toHex=function(){return e=Mt(this.rgba),n=e.r,o=e.g,i=e.b,a=(s=e.a)<1?Et(V(255*s)):"","#"+Et(n)+Et(o)+Et(i)+a;var e,n,o,i,s,a},t.prototype.toRgb=function(){return Mt(this.rgba)},t.prototype.toRgbString=function(){return e=Mt(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 ue(ct(this.rgba))},t.prototype.toHslString=function(){return e=ue(ct(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=ce(this.rgba),{h:V(e.h),s:V(e.s),v:V(e.v),a:V(e.a,3)};var e},t.prototype.invert=function(){return F({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),F(Bt(this.rgba,e))},t.prototype.desaturate=function(e){return e===void 0&&(e=.1),F(Bt(this.rgba,-e))},t.prototype.grayscale=function(){return F(Bt(this.rgba,-1))},t.prototype.lighten=function(e){return e===void 0&&(e=.1),F(pe(this.rgba,e))},t.prototype.darken=function(e){return e===void 0&&(e=.1),F(pe(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"?F({r:(n=this.rgba).r,g:n.g,b:n.b,a:e}):V(this.rgba.a,3);var n},t.prototype.hue=function(e){var n=ct(this.rgba);return typeof e=="number"?F({h:e,s:n.s,l:n.l,a:n.a}):V(n.h)},t.prototype.isEqual=function(e){return this.toHex()===F(e).toHex()},t}(),F=function(t){return t instanceof me?t:new me(t)};const fn=t=>[`background-color:${F((t==null?void 0:t.fill)||q.fill).alpha((t==null?void 0:t.fillOpacity)===void 0?q.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(";"),ye=()=>{const t=document.createElement("style");document.getElementsByTagName("head")[0].appendChild(t);let e=new Set;return{destroy:()=>{CSS.highlights.clear(),t.remove()},setVisible:s=>{console.log("setVisible not implemented on CSS Custom Highlights renderer")},redraw:(s,a,r,l)=>{l&&l.clear();const f=new Set(s.map(u=>u.annotation.id));Array.from(e).filter(u=>!f.has(u));const h=s.map(u=>{var p;const g=r?typeof r=="function"?r(u.annotation,u.state):r:(p=u.state)!=null&&p.selected?at:q,v=l&&l.paint(u,a)||g;return`::highlight(_${u.annotation.id}) { ${fn(v)} }`});t.innerHTML=h.join(`
2
+ `),CSS.highlights.clear(),s.forEach(({annotation:u})=>{const g=u.target.selector.map(p=>p.range),v=new Highlight(...g);CSS.highlights.set(`_${u.id}`,v)}),e=f}}},be=(t,e,n)=>Rt(t,e,n,ye());var Ae=Object.prototype.hasOwnProperty;function It(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--&&It(t[o],e[o]););return o===-1}if(!n||typeof t=="object"){o=0;for(n in t)if(Ae.call(t,n)&&++o&&!Ae.call(e,n)||!(n in e)||!It(t[n],e[n]))return!1;return Object.keys(e).length===o}}return t!==t&&e!==e}const hn=(t,e)=>{const n=(s,a)=>s.x<=a.x+a.width&&s.x+s.width>=a.x&&s.y<=a.y+a.height&&s.y+s.height>=a.y,o=s=>s.rects.reduce((a,r)=>a+r.width,0),i=e.filter(({rects:s})=>s.some(a=>n(t,a)));return i.sort((s,a)=>o(a)-o(s)),i.findIndex(s=>s.rects.includes(t))},gn=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,r,l,f,h)=>{const g=!(It(n,a)&&h);if(!f&&!g)return;g&&(e.innerHTML=""),[...a].sort((p,m)=>{const{annotation:{target:{created:w}}}=p,{annotation:{target:{created:d}}}=m;return w&&d?w.getTime()-d.getTime():0}).forEach(p=>{p.rects.map(m=>{const w=hn(m,a),d=ie(p,r,l,f,w);if(g){const c=document.createElement("span");c.className="r6o-annotation",c.dataset.annotation=p.annotation.id,c.style.left=`${m.x}px`,c.style.top=`${m.y}px`,c.style.width=`${m.width}px`,c.style.height=`${m.height}px`,c.style.backgroundColor=F((d==null?void 0:d.fill)||q.fill).alpha((d==null?void 0:d.fillOpacity)===void 0?q.fillOpacity:d.fillOpacity).toHex(),d.underlineStyle&&(c.style.borderStyle=d.underlineStyle),d.underlineColor&&(c.style.borderColor=d.underlineColor),d.underlineThickness&&(c.style.borderBottomWidth=`${d.underlineThickness}px`),d.underlineOffset&&(c.style.paddingBottom=`${d.underlineOffset}px`),e.appendChild(c)}})}),n=a},setVisible:a=>{a?e.classList.remove("hidden"):e.classList.add("hidden")}}},we=(t,e,n)=>Rt(t,e,n,gn(t)),Y=[];for(let t=0;t<256;++t)Y.push((t+256).toString(16).slice(1));function pn(t,e=0){return(Y[t[e+0]]+Y[t[e+1]]+Y[t[e+2]]+Y[t[e+3]]+"-"+Y[t[e+4]]+Y[t[e+5]]+"-"+Y[t[e+6]]+Y[t[e+7]]+"-"+Y[t[e+8]]+Y[t[e+9]]+"-"+Y[t[e+10]]+Y[t[e+11]]+Y[t[e+12]]+Y[t[e+13]]+Y[t[e+14]]+Y[t[e+15]]).toLowerCase()}let _t;const mn=new Uint8Array(16);function yn(){if(!_t){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");_t=crypto.getRandomValues.bind(crypto)}return _t(mn)}const Ee={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function ve(t,e,n){var i;if(Ee.randomUUID&&!t)return Ee.randomUUID();t=t||{};const o=t.random??((i=t.rng)==null?void 0:i.call(t))??yn();if(o.length<16)throw new Error("Random bytes length must be >= 16");return o[6]=o[6]&15|64,o[8]=o[8]&63|128,pn(o)}var Se=Object.prototype.hasOwnProperty;function tt(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--&&tt(t[o],e[o]););return o===-1}if(!n||typeof t=="object"){o=0;for(n in t)if(Se.call(t,n)&&++o&&!Se.call(e,n)||!(n in e)||!tt(t[n],e[n]))return!1;return Object.keys(e).length===o}}return t!==t&&e!==e}function Nt(){}function bn(t,e){return t!=t?e==e:t!==e||t&&typeof t=="object"||typeof t=="function"}const nt=[];function Ut(t,e=Nt){let n;const o=new Set;function i(r){if(bn(t,r)&&(t=r,n)){const l=!nt.length;for(const f of o)f[1](),nt.push(f,t);if(l){for(let f=0;f<nt.length;f+=2)nt[f][0](nt[f+1]);nt.length=0}}}function s(r){i(r(t))}function a(r,l=Nt){const f=[r,l];return o.add(f),o.size===1&&(n=e(i,s)||Nt),r(t),()=>{o.delete(f),o.size===0&&n&&(n(),n=null)}}return{set:i,update:s,subscribe:a}}const An=t=>{const{subscribe:e,set:n}=Ut();let o;return e(i=>o=i),t.observe(({changes:i})=>{if(o){(i.deleted||[]).some(a=>a.id===o)&&n(void 0);const s=(i.updated||[]).find(({oldValue:a})=>a.id===o);s&&n(s.newValue.id)}}),{get current(){return o},subscribe:e,set:n}};var xe=(t=>(t.EDIT="EDIT",t.SELECT="SELECT",t.NONE="NONE",t))(xe||{});const vt={selected:[]},wn=(t,e,n)=>{const{subscribe:o,set:i}=Ut(vt);let s=e,a=vt;o(p=>a=p);const r=()=>{tt(a,vt)||i(vt)},l=()=>{var p;return((p=a.selected)==null?void 0:p.length)===0},f=p=>{if(l())return!1;const m=typeof p=="string"?p:p.id;return a.selected.some(w=>w.id===m)},h=(p,m)=>{let w;if(Array.isArray(p)){if(w=p.map(c=>t.getAnnotation(c)).filter(Boolean),w.length<p.length){console.warn("Invalid selection: "+p.filter(c=>!w.some(b=>b.id===c)));return}}else{const c=t.getAnnotation(p);if(!c){console.warn("Invalid selection: "+p);return}w=[c]}const d=w.reduce((c,b)=>{const x=Ce(b,s);return x==="EDIT"?[...c,{id:b.id,editable:!0}]:x==="SELECT"?[...c,{id:b.id}]:c},[]);i({selected:d,event:m})},u=(p,m)=>{const w=Array.isArray(p)?p:[p],d=w.map(c=>t.getAnnotation(c)).filter(c=>!!c);i({selected:d.map(c=>{const b=m===void 0?Ce(c,s)==="EDIT":m;return{id:c.id,editable:b}})}),d.length!==w.length&&console.warn("Invalid selection",p)},g=p=>{if(l())return!1;const{selected:m}=a;m.some(({id:w})=>p.includes(w))&&i({selected:m.filter(({id:w})=>!p.includes(w))})},v=p=>{s=p,u(a.selected.map(({id:m})=>m))};return t.observe(({changes:p})=>g((p.deleted||[]).map(m=>m.id))),{get event(){return a?a.event:null},get selected(){return a?[...a.selected]:null},get userSelectAction(){return s},clear:r,isEmpty:l,isSelected:f,setSelected:u,setUserSelectAction:v,subscribe:o,userSelect:h}},Ce=(t,e,n)=>typeof e=="function"?e(t):e||"EDIT",K=[];for(let t=0;t<256;++t)K.push((t+256).toString(16).slice(1));function En(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()}let Dt;const vn=new Uint8Array(16);function Sn(){if(!Dt){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");Dt=crypto.getRandomValues.bind(crypto)}return Dt(vn)}const xn=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),Le={randomUUID:xn};function Te(t,e,n){var o;if(Le.randomUUID&&!t)return Le.randomUUID();t=t||{};const i=t.random??((o=t.rng)==null?void 0:o.call(t))??Sn();if(i.length<16)throw new Error("Random bytes length must be >= 16");return i[6]=i[6]&15|64,i[8]=i[8]&63|128,En(i)}const Vt=t=>{const e=n=>{const o={...n};return n.created&&typeof n.created=="string"&&(o.created=new Date(n.created)),n.updated&&typeof n.updated=="string"&&(o.updated=new Date(n.updated)),o};return{...t,bodies:(t.bodies||[]).map(e),target:e(t.target)}},Cn=(t,e,n,o)=>({id:Te(),annotation:typeof t=="string"?t:t.id,created:n||new Date,creator:o,...e}),Ln=(t,e)=>{const n=new Set(t.bodies.map(o=>o.id));return e.bodies.filter(o=>!n.has(o.id))},Tn=(t,e)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},On=(t,e)=>e.bodies.map(n=>{const o=t.bodies.find(i=>i.id===n.id);return{newBody:n,oldBody:o&&!tt(o,n)?o:void 0}}).filter(({oldBody:n})=>n).map(({oldBody:n,newBody:o})=>({oldBody:n,newBody:o})),Rn=(t,e)=>!tt(t.target,e.target),Oe=(t,e)=>{const n=Ln(t,e),o=Tn(t,e),i=On(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:Rn(t,e)?{oldTarget:t.target,newTarget:e.target}:void 0}};var k=(t=>(t.LOCAL="LOCAL",t.REMOTE="REMOTE",t.SILENT="SILENT",t))(k||{});const Mn=(t,e)=>{var n,o;const{changes:i,origin:s}=e;if(!(t.options.origin?t.options.origin===s:s!=="SILENT"))return!1;if(t.options.ignore){const{ignore:a}=t.options,r=l=>l&&l.length>0;if(!(r(i.created)||r(i.deleted))){const l=(n=i.updated)==null?void 0:n.some(h=>r(h.bodiesCreated)||r(h.bodiesDeleted)||r(h.bodiesUpdated)),f=(o=i.updated)==null?void 0:o.some(h=>h.targetUpdated);if(a==="BODY_ONLY"&&l&&!f||a==="TARGET_ONLY"&&f&&!l)return!1}}if(t.options.annotations){const a=new Set([...(i.created||[]).map(r=>r.id),...(i.deleted||[]).map(r=>r.id),...(i.updated||[]).map(({oldValue:r})=>r.id)]);return!!(Array.isArray(t.options.annotations)?t.options.annotations:[t.options.annotations]).find(r=>a.has(r))}else return!0},Bn=(t,e)=>{const n=new Set((t.created||[]).map(u=>u.id)),o=new Set((t.updated||[]).map(({newValue:u})=>u.id)),i=new Set((e.created||[]).map(u=>u.id)),s=new Set((e.deleted||[]).map(u=>u.id)),a=new Set((e.updated||[]).map(({oldValue:u})=>u.id)),r=new Set((e.updated||[]).filter(({oldValue:u})=>n.has(u.id)||o.has(u.id)).map(({oldValue:u})=>u.id)),l=[...(t.created||[]).filter(u=>!s.has(u.id)).map(u=>a.has(u.id)?e.updated.find(({oldValue:g})=>g.id===u.id).newValue:u),...e.created||[]],f=[...(t.deleted||[]).filter(u=>!i.has(u.id)),...(e.deleted||[]).filter(u=>!n.has(u.id))],h=[...(t.updated||[]).filter(({newValue:u})=>!s.has(u.id)).map(u=>{const{oldValue:g,newValue:v}=u;if(a.has(v.id)){const p=e.updated.find(m=>m.oldValue.id===v.id).newValue;return Oe(g,p)}else return u}),...(e.updated||[]).filter(({oldValue:u})=>!r.has(u.id))];return{created:l,deleted:f,updated:h}},St=t=>{const e=t.id===void 0?Te():t.id;return{...t,id:e,bodies:t.bodies===void 0?[]:t.bodies.map(n=>({...n,annotation:e})),target:{...t.target,annotation:e}}},kn=t=>t.id!==void 0,In=()=>{const t=new Map,e=new Map,n=[],o=(y,E={})=>{n.push({onChange:y,options:E})},i=y=>{const E=n.findIndex(A=>A.onChange==y);E>-1&&n.splice(E,1)},s=(y,E)=>{const A={origin:y,changes:{created:E.created||[],updated:E.updated||[],deleted:E.deleted||[]},state:[...t.values()]};n.forEach(L=>{Mn(L,A)&&L.onChange(A)})},a=(y,E=k.LOCAL)=>{if(y.id&&t.get(y.id))throw Error(`Cannot add annotation ${y.id} - exists already`);{const A=St(y);t.set(A.id,A),A.bodies.forEach(L=>e.set(L.id,A.id)),s(E,{created:[A]})}},r=(y,E)=>{const A=St(typeof y=="string"?E:y),L=typeof y=="string"?y:y.id,I=L&&t.get(L);if(I){const R=Oe(I,A);return L===A.id?t.set(L,A):(t.delete(L),t.set(A.id,A)),I.bodies.forEach(N=>e.delete(N.id)),A.bodies.forEach(N=>e.set(N.id,A.id)),R}else console.warn(`Cannot update annotation ${L} - does not exist`)},l=(y,E=k.LOCAL,A=k.LOCAL)=>{const L=kn(E)?A:E,I=r(y,E);I&&s(L,{updated:[I]})},f=(y,E=k.LOCAL)=>{t.get(y.id)?l(y,E):a(y,E)},h=(y,E=k.LOCAL)=>{const A=y.reduce((L,I)=>{const R=r(I);return R?[...L,R]:L},[]);A.length>0&&s(E,{updated:A})},u=(y,E=k.LOCAL)=>{const A=y.map(St),{toAdd:L,toUpdate:I}=A.reduce((N,H)=>t.get(H.id)?{...N,toUpdate:[...N.toUpdate,H]}:{...N,toAdd:[...N.toAdd,H]},{toAdd:[],toUpdate:[]}),R=I.map(N=>r(N,E)).filter(Boolean);L.forEach(N=>{t.set(N.id,N),N.bodies.forEach(H=>e.set(H.id,N.id))}),s(E,{created:L,updated:R})},g=(y,E=k.LOCAL)=>{const A=t.get(y.annotation);if(A){const L={...A,bodies:[...A.bodies,y]};t.set(A.id,L),e.set(y.id,L.id),s(E,{updated:[{oldValue:A,newValue:L,bodiesCreated:[y]}]})}else console.warn(`Attempt to add body to missing annotation: ${y.annotation}`)},v=()=>[...t.values()],p=(y=k.LOCAL)=>{const E=[...t.values()];t.clear(),e.clear(),s(y,{deleted:E})},m=(y,E=!0,A=k.LOCAL)=>{const L=y.map(St);if(E){const I=[...t.values()];t.clear(),e.clear(),L.forEach(R=>{t.set(R.id,R),R.bodies.forEach(N=>e.set(N.id,R.id))}),s(A,{created:L,deleted:I})}else{const I=y.reduce((R,N)=>{const H=N.id&&t.get(N.id);return H?[...R,H]:R},[]);if(I.length>0)throw Error(`Bulk insert would overwrite the following annotations: ${I.map(R=>R.id).join(", ")}`);L.forEach(R=>{t.set(R.id,R),R.bodies.forEach(N=>e.set(N.id,R.id))}),s(A,{created:L})}},w=y=>{const E=typeof y=="string"?y:y.id,A=t.get(E);if(A)return t.delete(E),A.bodies.forEach(L=>e.delete(L.id)),A;console.warn(`Attempt to delete missing annotation: ${E}`)},d=(y,E=k.LOCAL)=>{const A=w(y);A&&s(E,{deleted:[A]})},c=(y,E=k.LOCAL)=>{const A=y.reduce((L,I)=>{const R=w(I);return R?[...L,R]:L},[]);A.length>0&&s(E,{deleted:A})},b=y=>{const E=t.get(y.annotation);if(E){const A=E.bodies.find(L=>L.id===y.id);if(A){e.delete(A.id);const L={...E,bodies:E.bodies.filter(I=>I.id!==y.id)};return t.set(E.id,L),{oldValue:E,newValue:L,bodiesDeleted:[A]}}else console.warn(`Attempt to delete missing body ${y.id} from annotation ${y.annotation}`)}else console.warn(`Attempt to delete body from missing annotation ${y.annotation}`)},x=(y,E=k.LOCAL)=>{const A=b(y);A&&s(E,{updated:[A]})},S=(y,E=k.LOCAL)=>{const A=y.map(L=>b(L)).filter(Boolean);A.length>0&&s(E,{updated:A})},C=y=>{const E=t.get(y);return E?{...E}:void 0},M=y=>{const E=e.get(y);if(E){const A=C(E).bodies.find(L=>L.id===y);if(A)return A;console.error(`Store integrity error: body ${y} in index, but not in annotation`)}else console.warn(`Attempt to retrieve missing body: ${y}`)},B=(y,E)=>{if(y.annotation!==E.annotation)throw"Annotation integrity violation: annotation ID must be the same when updating bodies";const A=t.get(y.annotation);if(A){const L=A.bodies.find(R=>R.id===y.id),I={...A,bodies:A.bodies.map(R=>R.id===L.id?E:R)};return t.set(A.id,I),L.id!==E.id&&(e.delete(L.id),e.set(E.id,I.id)),{oldValue:A,newValue:I,bodiesUpdated:[{oldBody:L,newBody:E}]}}else console.warn(`Attempt to add body to missing annotation ${y.annotation}`)},T=(y,E,A=k.LOCAL)=>{const L=B(y,E);L&&s(A,{updated:[L]})},_=(y,E=k.LOCAL)=>{const A=y.map(L=>B({id:L.id,annotation:L.annotation},L)).filter(Boolean);s(E,{updated:A})},P=y=>{const E=t.get(y.annotation);if(E){const A={...E,target:{...E.target,...y}};return t.set(E.id,A),{oldValue:E,newValue:A,targetUpdated:{oldTarget:E.target,newTarget:y}}}else console.warn(`Attempt to update target on missing annotation: ${y.annotation}`)};return{addAnnotation:a,addBody:g,all:v,bulkAddAnnotations:m,bulkDeleteAnnotations:c,bulkDeleteBodies:S,bulkUpdateAnnotations:h,bulkUpdateBodies:_,bulkUpdateTargets:(y,E=k.LOCAL)=>{const A=y.map(L=>P(L)).filter(Boolean);A.length>0&&s(E,{updated:A})},bulkUpsertAnnotations:u,clear:p,deleteAnnotation:d,deleteBody:x,getAnnotation:C,getBody:M,observe:o,unobserve:i,updateAnnotation:l,updateBody:T,updateTarget:(y,E=k.LOCAL)=>{const A=P(y);A&&s(E,{updated:[A]})},upsertAnnotation:f}};let _n=()=>({emit(t,...e){for(let n=this.events[t]||[],o=0,i=n.length;o<i;o++)n[o](...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,Un=(t,e)=>{const n=_n(),o=[];let i=-1,s=!1,a=0;const r=p=>{if(!s){const{changes:m}=p,w=performance.now();if(w-a>Nn)o.splice(i+1),o.push(m),i=o.length-1;else{const d=o.length-1;o[d]=Bn(o[d],m)}a=w}s=!1};t.observe(r,{origin:k.LOCAL});const l=p=>p&&p.length>0&&t.bulkDeleteAnnotations(p),f=p=>p&&p.length>0&&t.bulkAddAnnotations(p,!1),h=p=>p&&p.length>0&&t.bulkUpdateAnnotations(p.map(({oldValue:m})=>m)),u=p=>p&&p.length>0&&t.bulkUpdateAnnotations(p.map(({newValue:m})=>m)),g=p=>p&&p.length>0&&t.bulkAddAnnotations(p,!1),v=p=>p&&p.length>0&&t.bulkDeleteAnnotations(p);return{canRedo:()=>o.length-1>i,canUndo:()=>i>-1,destroy:()=>t.unobserve(r),getHistory:()=>({changes:[...o],pointer:i}),on:(p,m)=>n.on(p,m),redo:()=>{if(o.length-1>i){s=!0;const{created:p,updated:m,deleted:w}=o[i+1];f(p),u(m),v(w),n.emit("redo",o[i+1]),i+=1}},undo:()=>{if(i>-1){s=!0;const{created:p,updated:m,deleted:w}=o[i];l(p),h(m),g(w),n.emit("undo",o[i]),i-=1}}}},Dn=()=>{const{subscribe:t,set:e}=Ut([]);return{subscribe:t,set:e}},Vn=(t,e,n,o)=>{const{hover:i,selection:s,store:a,viewport:r}=t,l=new Map;let f=[],h;const u=(m,w)=>{l.has(m)?l.get(m).push(w):l.set(m,[w])},g=(m,w)=>{const d=l.get(m);if(d){const c=d.indexOf(w);c!==-1&&d.splice(c,1)}},v=(m,w,d)=>{l.has(m)&&setTimeout(()=>{l.get(m).forEach(c=>{if(n){const b=Array.isArray(w)?w.map(S=>n.serialize(S)):n.serialize(w),x=d?d instanceof PointerEvent?d:n.serialize(d):void 0;c(b,x)}else c(w,d)})},1)};s.subscribe(({selected:m})=>{if(!(f.length===0&&m.length===0)){if(f.length===0&&m.length>0)f=m.map(({id:w})=>a.getAnnotation(w));else if(f.length>0&&m.length===0)f.forEach(w=>{const d=a.getAnnotation(w.id);d&&!tt(d,w)&&v("updateAnnotation",d,w)}),f=[];else{const w=new Set(f.map(c=>c.id)),d=new Set(m.map(({id:c})=>c));f.filter(c=>!d.has(c.id)).forEach(c=>{const b=a.getAnnotation(c.id);b&&!tt(b,c)&&v("updateAnnotation",b,c)}),f=[...f.filter(c=>d.has(c.id)),...m.filter(({id:c})=>!w.has(c)).map(({id:c})=>a.getAnnotation(c))]}v("selectionChanged",f)}}),i.subscribe(m=>{!h&&m?v("mouseEnterAnnotation",a.getAnnotation(m)):h&&!m?v("mouseLeaveAnnotation",a.getAnnotation(h)):h&&m&&(v("mouseLeaveAnnotation",a.getAnnotation(h)),v("mouseEnterAnnotation",a.getAnnotation(m))),h=m}),r==null||r.subscribe(m=>v("viewportIntersect",m.map(w=>a.getAnnotation(w)))),a.observe(m=>{const{created:w,deleted:d}=m.changes;(w||[]).forEach(c=>v("createAnnotation",c)),(d||[]).forEach(c=>v("deleteAnnotation",c)),(m.changes.updated||[]).filter(c=>[...c.bodiesCreated||[],...c.bodiesDeleted||[],...c.bodiesUpdated||[]].length>0).forEach(({oldValue:c,newValue:b})=>{const x=f.find(S=>S.id===c.id)||c;f=f.map(S=>S.id===c.id?b:S),v("updateAnnotation",b,x)})},{origin:k.LOCAL}),a.observe(m=>{if(f){const w=new Set(f.map(c=>c.id)),d=(m.changes.updated||[]).filter(({newValue:c})=>w.has(c.id)).map(({newValue:c})=>c);d.length>0&&(f=f.map(c=>d.find(x=>x.id===c.id)||c))}},{origin:k.REMOTE});const p=m=>w=>{const{updated:d}=w;m?(d||[]).forEach(c=>v("updateAnnotation",c.oldValue,c.newValue)):(d||[]).forEach(c=>v("updateAnnotation",c.newValue,c.oldValue))};return e.on("undo",p(!0)),e.on("redo",p(!1)),{on:u,off:g,emit:v}},Yn=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:[]}),Kn=(t,e,n)=>{const{store:o,selection:i}=t,s=d=>{if(n){const{parsed:c,error:b}=n.parse(d);c?o.addAnnotation(c,k.REMOTE):console.error(b)}else o.addAnnotation(Vt(d),k.REMOTE)},a=()=>i.clear(),r=()=>o.clear(),l=d=>{const c=o.getAnnotation(d);return n&&c?n.serialize(c):c},f=()=>n?o.all().map(n.serialize):o.all(),h=()=>{var d;const c=(((d=i.selected)==null?void 0:d.map(b=>b.id))||[]).map(b=>o.getAnnotation(b)).filter(Boolean);return n?c.map(n.serialize):c},u=(d,c=!0)=>fetch(d).then(b=>b.json()).then(b=>(v(b,c),b)),g=d=>{if(typeof d=="string"){const c=o.getAnnotation(d);if(o.deleteAnnotation(d),c)return n?n.serialize(c):c}else{const c=n?n.parse(d).parsed:d;if(c)return o.deleteAnnotation(c),d}},v=(d,c=!0)=>{if(n){const b=n.parseAll||Yn(n),{parsed:x,failed:S}=b(d);S.length>0&&console.warn(`Discarded ${S.length} invalid annotations`,S),o.bulkAddAnnotations(x,c,k.REMOTE)}else o.bulkAddAnnotations(d.map(Vt),c,k.REMOTE)},p=(d,c)=>{d?i.setSelected(d,c):i.clear()},m=d=>{i.clear(),i.setUserSelectAction(d)},w=d=>{if(n){const c=n.parse(d).parsed,b=n.serialize(o.getAnnotation(c.id));return o.updateAnnotation(c),b}else{const c=o.getAnnotation(d.id);return o.updateAnnotation(Vt(d)),c}};return{addAnnotation:s,cancelSelected:a,canRedo:e.canRedo,canUndo:e.canUndo,clearAnnotations:r,getAnnotationById:l,getAnnotations:f,getHistory:e.getHistory,getSelected:h,loadAnnotations:u,redo:e.redo,removeAnnotation:g,setAnnotations:v,setSelected:p,setUserSelectAction:m,undo:e.undo,updateAnnotation:w}},Xn="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let Pn=t=>crypto.getRandomValues(new Uint8Array(t)),Hn=(t,e,n)=>{let o=(2<<Math.log2(t.length-1))-1,i=-~(1.6*o*e/t.length);return(s=e)=>{let a="";for(;;){let r=n(i),l=i|0;for(;l--;)if(a+=t[r[l]&o]||"",a.length>=s)return a}}},$n=(t,e=21)=>Hn(t,e|0,Pn),jn=(t=21)=>{let e="",n=crypto.getRandomValues(new Uint8Array(t|=0));for(;t--;)e+=Xn[n[t]&63];return e};const Fn=()=>({isGuest:!0,id:$n("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",20)()}),zn=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}`},Re=t=>t?typeof t=="object"?{...t}:t:void 0,Wn=(t,e)=>(Array.isArray(t)?t:[t]).map(n=>{const{id:o,type:i,purpose:s,value:a,created:r,modified:l,creator:f,...h}=n;return{id:o||`temp-${zn(n)}`,annotation:e,type:i,purpose:s,value:a,creator:Re(f),created:r?new Date(r):void 0,updated:l?new Date(l):void 0,...h}}),qn=t=>t.map(e=>{var n;const{annotation:o,created:i,updated:s,...a}=e,r={...a,created:i==null?void 0:i.toISOString(),modified:s==null?void 0:s.toISOString()};return(n=r.id)!=null&&n.startsWith("temp-")&&delete r.id,r});jn();const Gn=(t,e)=>({parse:n=>Me(n),serialize:n=>Be(n,t,e)}),Qn=t=>t.quote!==void 0&&t.start!==void 0&&t.end!==void 0,Jn=t=>{const{id:e,creator:n,created:o,modified:i,target:s}=t,a=Array.isArray(s)?s:[s];if(a.length===0)return{error:Error(`No targets found for annotation: ${t.id}`)};const r={creator:Re(n),created:o?new Date(o):void 0,updated:i?new Date(i):void 0,annotation:e,selector:[],styleClass:"styleClass"in a[0]?a[0].styleClass:void 0};for(const l of a){const h=(Array.isArray(l.selector)?l.selector:[l.selector]).reduce((u,g)=>{switch(g.type){case"TextQuoteSelector":u.quote=g.exact;break;case"TextPositionSelector":u.start=g.start,u.end=g.end;break}return u},{});if(Qn(h))r.selector.push({...h,id:l.id,scope:l.scope});else{const u=[h.start?void 0:"TextPositionSelector",h.quote?void 0:"TextQuoteSelector"].filter(Boolean);return{error:Error(`Missing selector types: ${u.join(" and ")} for annotation: ${t.id}`)}}}return{parsed:r}},Me=t=>{const e=t.id||ve(),{creator:n,created:o,modified:i,body:s,...a}=t,r=Wn(s,e),l=Jn(t);return"error"in l?{error:l.error}:{parsed:{...a,id:e,bodies:r,target:l.parsed}}},Be=(t,e,n)=>{const{bodies:o,target:i,...s}=t,{selector:a,creator:r,created:l,updated:f,...h}=i,u=a.map(g=>{const{id:v,quote:p,start:m,end:w,range:d}=g,{prefix:c,suffix:b}=Gt(d,n),x=[{type:"TextQuoteSelector",exact:p,prefix:c,suffix:b},{type:"TextPositionSelector",start:m,end:w}];return{...h,id:v,scope:"scope"in g?g.scope:void 0,source:e,selector:x}});return{...s,"@context":"http://www.w3.org/ns/anno.jsonld",id:t.id,type:"Annotation",body:qn(t.bodies),creator:r,created:l==null?void 0:l.toISOString(),modified:f==null?void 0:f.toISOString(),target:u}};function ke(t,e,n=0,o=t.length-1,i=Zn){for(;o>n;){if(o-n>600){const l=o-n+1,f=e-n+1,h=Math.log(l),u=.5*Math.exp(2*h/3),g=.5*Math.sqrt(h*u*(l-u)/l)*(f-l/2<0?-1:1),v=Math.max(n,Math.floor(e-f*u/l+g)),p=Math.min(o,Math.floor(e+(l-f)*u/l+g));ke(t,e,v,p,i)}const s=t[e];let a=n,r=o;for(lt(t,n,e),i(t[o],s)>0&&lt(t,n,o);a<r;){for(lt(t,a,r),a++,r--;i(t[a],s)<0;)a++;for(;i(t[r],s)>0;)r--}i(t[n],s)===0?lt(t,n,r):(r++,lt(t,r,o)),r<=e&&(n=r+1),e<=r&&(o=r-1)}}function lt(t,e,n){const o=t[e];t[e]=t[n],t[n]=o}function Zn(t,e){return t<e?-1:t>e?1:0}class to{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,s=[];for(;n;){for(let a=0;a<n.children.length;a++){const r=n.children[a],l=n.leaf?i(r):r;Ct(e,l)&&(n.leaf?o.push(r):Kt(e,l)?this._all(r,o):s.push(r))}n=s.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 s=n.children[i],a=n.leaf?this.toBBox(s):s;if(Ct(e,a)){if(n.leaf||Kt(e,a))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=it([]),this}remove(e,n){if(!e)return this;let o=this.data;const i=this.toBBox(e),s=[],a=[];let r,l,f;for(;o||s.length;){if(o||(o=s.pop(),l=s[s.length-1],r=a.pop(),f=!0),o.leaf){const h=eo(e,o.children,n);if(h!==-1)return o.children.splice(h,1),s.push(o),this._condense(s),this}!f&&!o.leaf&&Kt(o,i)?(s.push(o),a.push(r),r=0,l=o,o=o.children[0]):l?(r++,o=l.children[r],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 a=this._maxEntries,r;if(s<=a)return r=it(e.slice(n,o+1)),ot(r,this.toBBox),r;i||(i=Math.ceil(Math.log(s)/Math.log(a)),a=Math.ceil(s/Math.pow(a,i-1))),r=it([]),r.leaf=!1,r.height=i;const l=Math.ceil(s/a),f=l*Math.ceil(Math.sqrt(a));Ie(e,n,o,f,this.compareMinX);for(let h=n;h<=o;h+=f){const u=Math.min(h+f-1,o);Ie(e,h,u,l,this.compareMinY);for(let g=h;g<=u;g+=l){const v=Math.min(g+l-1,u);r.children.push(this._build(e,g,v,i-1))}}return ot(r,this.toBBox),r}_chooseSubtree(e,n,o,i){for(;i.push(n),!(n.leaf||i.length-1===o);){let s=1/0,a=1/0,r;for(let l=0;l<n.children.length;l++){const f=n.children[l],h=Yt(f),u=io(e,f)-h;u<a?(a=u,s=h<s?h:s,r=f):u===a&&h<s&&(s=h,r=f)}n=r||n.children[0]}return n}_insert(e,n,o){const i=o?e:this.toBBox(e),s=[],a=this._chooseSubtree(i,this.data,n,s);for(a.children.push(e),ut(a,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 a=this._chooseSplitIndex(o,s,i),r=it(o.children.splice(a,o.children.length-a));r.height=o.height,r.leaf=o.leaf,ot(o,this.toBBox),ot(r,this.toBBox),n?e[n-1].children.push(r):this._splitRoot(o,r)}_splitRoot(e,n){this.data=it([e,n]),this.data.height=e.height+1,this.data.leaf=!1,ot(this.data,this.toBBox)}_chooseSplitIndex(e,n,o){let i,s=1/0,a=1/0;for(let r=n;r<=o-n;r++){const l=dt(e,0,r,this.toBBox),f=dt(e,r,o,this.toBBox),h=so(l,f),u=Yt(l)+Yt(f);h<s?(s=h,i=r,a=u<a?u:a):h===s&&u<a&&(a=u,i=r)}return i||o-n}_chooseSplitAxis(e,n,o){const i=e.leaf?this.compareMinX:no,s=e.leaf?this.compareMinY:oo,a=this._allDistMargin(e,n,o,i),r=this._allDistMargin(e,n,o,s);a<r&&e.children.sort(i)}_allDistMargin(e,n,o,i){e.children.sort(i);const s=this.toBBox,a=dt(e,0,n,s),r=dt(e,o-n,o,s);let l=xt(a)+xt(r);for(let f=n;f<o-n;f++){const h=e.children[f];ut(a,e.leaf?s(h):h),l+=xt(a)}for(let f=o-n-1;f>=n;f--){const h=e.children[f];ut(r,e.leaf?s(h):h),l+=xt(r)}return l}_adjustParentBBoxes(e,n,o){for(let i=o;i>=0;i--)ut(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():ot(e[n],this.toBBox)}}function eo(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 ot(t,e){dt(t,0,t.children.length,e,t)}function dt(t,e,n,o,i){i||(i=it(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 a=t.children[s];ut(i,t.leaf?o(a):a)}return i}function ut(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 no(t,e){return t.minX-e.minX}function oo(t,e){return t.minY-e.minY}function Yt(t){return(t.maxX-t.minX)*(t.maxY-t.minY)}function xt(t){return t.maxX-t.minX+(t.maxY-t.minY)}function io(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 so(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 Kt(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 it(t){return{children:t,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function Ie(t,e,n,o,i){const s=[e,n];for(;s.length;){if(n=s.pop(),e=s.pop(),n-e<=o)continue;const a=e+Math.ceil((n-e)/o/2)*o;ke(t,a,e,n,i),s.push(e,a,a,n)}}const ro=(t,e)=>{const n=new to,o=new Map,i=(d,c)=>{const b=d.selector.flatMap(S=>{const C=z([S])?S.range:Ot(S,e).range;return Array.from(C.getClientRects())}),x=Zt(b).map(({left:S,top:C,right:M,bottom:B})=>new DOMRect(S-c.left,C-c.top,M-S,B-C));return x.map(S=>{const{x:C,y:M,width:B,height:T}=S;return{minX:C,minY:M,maxX:C+B,maxY:M+T,annotation:{id:d.annotation,rects:x}}})},s=()=>[...o.values()],a=()=>{n.clear(),o.clear()},r=d=>{const c=i(d,e.getBoundingClientRect());c.length!==0&&(c.forEach(b=>n.insert(b)),o.set(d.annotation,c))},l=d=>{const c=o.get(d.annotation);c&&(c.forEach(b=>n.remove(b)),o.delete(d.annotation))},f=d=>{l(d),r(d)},h=(d,c=!0)=>{c&&a();const b=e.getBoundingClientRect(),x=d.map(C=>({target:C,rects:i(C,b)}));x.forEach(({target:C,rects:M})=>{M.length>0&&o.set(C.annotation,M)});const S=x.flatMap(({rects:C})=>C);n.load(S)},u=(d,c,b=!1)=>{const x=n.search({minX:d,minY:c,maxX:d,maxY:c}),S=C=>C.annotation.rects.reduce((M,B)=>M+B.width*B.height,0);return x.length>0?(x.sort((C,M)=>S(C)-S(M)),b?x.map(C=>C.annotation.id):[x[0].annotation.id]):[]},g=d=>{const c=v(d);if(c.length===0)return;let b=c[0].left,x=c[0].top,S=c[0].right,C=c[0].bottom;for(let M=1;M<c.length;M++){const B=c[M];b=Math.min(b,B.left),x=Math.min(x,B.top),S=Math.max(S,B.right),C=Math.max(C,B.bottom)}return new DOMRect(b,x,S-b,C-x)},v=d=>{const c=o.get(d);return c?c[0].annotation.rects:[]};return{all:s,clear:a,getAt:u,getAnnotationBounds:g,getAnnotationRects:v,getIntersecting:(d,c,b,x)=>{const S=n.search({minX:d,minY:c,maxX:b,maxY:x}),C=new Set(S.map(M=>M.annotation.id));return Array.from(C).map(M=>({annotation:t.getAnnotation(M),rects:v(M)})).filter(M=>!!M.annotation)},insert:r,recalculate:()=>h(t.all().map(d=>d.target),!0),remove:l,set:h,size:()=>n.all().length,update:f}},_e=(t,e)=>{const n=In(),o=ro(n,t),i=wn(n);i.setUserSelectAction(e);const s=An(n),a=Dn(),r=(d,c=k.LOCAL)=>{const b=wt(d,t),x=z(b.target.selector);return x&&n.addAnnotation(b,c),x},l=(d,c=!0,b=k.LOCAL)=>{const x=d.map(C=>wt(C,t)),S=x.filter(C=>!z(C.target.selector));return n.bulkAddAnnotations(x,c,b),S},f=(d,c=k.LOCAL)=>{const b=d.map(S=>wt(S,t)),x=b.filter(S=>!z(S.target.selector));return b.forEach(S=>{n.getAnnotation(S.id)?n.updateAnnotation(S,c):n.addAnnotation(S,c)}),x},h=(d,c=k.LOCAL)=>{const b=rt(d,t);n.updateTarget(b,c)},u=(d,c=k.LOCAL)=>{const b=d.map(x=>rt(x,t));n.bulkUpdateTargets(b,c)};function g(d,c,b,x){const S=b||!!x,C=o.getAt(d,c,S).map(B=>n.getAnnotation(B)),M=x?C.filter(x):C;if(M.length!==0)return b?M:M[0]}const v=d=>{if(o.getAnnotationRects(d).length!==0)return o.getAnnotationBounds(d)},p=(d,c,b,x)=>o.getIntersecting(d,c,b,x),m=d=>o.getAnnotationRects(d),w=()=>o.recalculate();return n.observe(({changes:d})=>{const c=(d.deleted||[]).filter(S=>z(S.target.selector)),b=(d.created||[]).filter(S=>z(S.target.selector)),x=(d.updated||[]).filter(S=>z(S.newValue.target.selector));(c==null?void 0:c.length)>0&&c.forEach(S=>o.remove(S.target)),b.length>0&&o.set(b.map(S=>S.target),!1),(x==null?void 0:x.length)>0&&x.forEach(({newValue:S})=>o.update(S.target))}),{store:{...n,addAnnotation:r,bulkAddAnnotations:l,bulkUpdateTargets:u,bulkUpsertAnnotations:f,getAnnotationBounds:v,getAnnotationRects:m,getIntersecting:p,getAt:g,recalculatePositions:w,updateTarget:h},selection:i,hover:s,viewport:a}},ao=()=>{const t=document.createElement("canvas");t.width=2*window.innerWidth,t.height=2*window.innerHeight,t.className="r6o-presence-layer";const e=t.getContext("2d");return e.scale(2,2),e.translate(.5,.5),t},Ne=(t,e={})=>{const n=ao(),o=n.getContext("2d");document.body.appendChild(n);const i=new Map,s=h=>Array.from(i.entries()).filter(([u,g])=>g.presenceKey===h.presenceKey).map(([u,g])=>u);return t.on("selectionChange",(h,u)=>{s(h).forEach(v=>i.delete(v)),u&&u.forEach(v=>i.set(v,h))}),{clear:()=>{const{width:h,height:u}=n;o.clearRect(-.5,-.5,h+1,u+1)},destroy:()=>{n.remove()},paint:(h,u,g)=>{e.font&&(o.font=e.font);const v=i.get(h.annotation.id);if(v){const{height:p}=h.rects[0],m=h.rects[0].x+u.left,w=h.rects[0].y+u.top;o.fillStyle=v.appearance.color,o.fillRect(m-2,w-2.5,2,p+5);const d=o.measureText(v.appearance.label),c=d.width+6,b=d.actualBoundingBoxAscent+d.actualBoundingBoxDescent+8,x=d.fontBoundingBoxAscent?8:6.5;return o.fillRect(m-2,w-2.5-b,c,b),o.fillStyle="#fff",o.fillText(v.appearance.label,m+1,w-x),{fill:v.appearance.color,fillOpacity:g?.45:.18}}},reset:()=>{n.width=2*window.innerWidth,n.height=2*window.innerHeight;const h=n.getContext("2d");h.scale(2,2),h.translate(.5,.5)}}},Xt=typeof navigator<"u"?navigator.userAgent.toLowerCase().indexOf("firefox")>0:!1;function Pt(t,e,n,o){t.addEventListener?t.addEventListener(e,n,o):t.attachEvent&&t.attachEvent("on".concat(e),n)}function ft(t,e,n,o){t.removeEventListener?t.removeEventListener(e,n,o):t.detachEvent&&t.detachEvent("on".concat(e),n)}function Ue(t,e){const n=e.slice(0,e.length-1);for(let o=0;o<n.length;o++)n[o]=t[n[o].toLowerCase()];return n}function De(t){typeof t!="string"&&(t=""),t=t.replace(/\s/g,"");const e=t.split(",");let n=e.lastIndexOf("");for(;n>=0;)e[n-1]+=",",e.splice(n,1),n=e.lastIndexOf("");return e}function co(t,e){const n=t.length>=e.length?t:e,o=t.length>=e.length?e:t;let i=!0;for(let s=0;s<n.length;s++)o.indexOf(n[s])===-1&&(i=!1);return i}const ht={backspace:8,"⌫":8,tab:9,clear:12,enter:13,"↩":13,return:13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,delete:46,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,capslock:20,num_0:96,num_1:97,num_2:98,num_3:99,num_4:100,num_5:101,num_6:102,num_7:103,num_8:104,num_9:105,num_multiply:106,num_add:107,num_enter:108,num_subtract:109,num_decimal:110,num_divide:111,"⇪":20,",":188,".":190,"/":191,"`":192,"-":Xt?173:189,"=":Xt?61:187,";":Xt?59:186,"'":222,"[":219,"]":221,"\\":220},W={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":91,cmd:91,command:91},Lt={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey",shiftKey:16,ctrlKey:17,altKey:18,metaKey:91},X={16:!1,18:!1,17:!1,91:!1},D={};for(let t=1;t<20;t++)ht["f".concat(t)]=111+t;let U=[],gt=null,Ve="all";const Q=new Map,pt=t=>ht[t.toLowerCase()]||W[t.toLowerCase()]||t.toUpperCase().charCodeAt(0),lo=t=>Object.keys(ht).find(e=>ht[e]===t),uo=t=>Object.keys(W).find(e=>W[e]===t);function Ye(t){Ve=t||"all"}function mt(){return Ve||"all"}function fo(){return U.slice(0)}function ho(){return U.map(t=>lo(t)||uo(t)||String.fromCharCode(t))}function go(){const t=[];return Object.keys(D).forEach(e=>{D[e].forEach(n=>{let{key:o,scope:i,mods:s,shortcut:a}=n;t.push({scope:i,shortcut:a,mods:s,keys:o.split("+").map(r=>pt(r))})})}),t}function po(t){const e=t.target||t.srcElement,{tagName:n}=e;let o=!0;const i=n==="INPUT"&&!["checkbox","radio","range","button","file","reset","submit","color"].includes(e.type);return(e.isContentEditable||(i||n==="TEXTAREA"||n==="SELECT")&&!e.readOnly)&&(o=!1),o}function mo(t){return typeof t=="string"&&(t=pt(t)),U.indexOf(t)!==-1}function yo(t,e){let n,o;t||(t=mt());for(const i in D)if(Object.prototype.hasOwnProperty.call(D,i))for(n=D[i],o=0;o<n.length;)n[o].scope===t?n.splice(o,1).forEach(a=>{let{element:r}=a;return Ht(r)}):o++;mt()===t&&Ye(e||"all")}function bo(t){let e=t.keyCode||t.which||t.charCode;const n=U.indexOf(e);if(n>=0&&U.splice(n,1),t.key&&t.key.toLowerCase()==="meta"&&U.splice(0,U.length),(e===93||e===224)&&(e=91),e in X){X[e]=!1;for(const o in W)W[o]===e&&(j[o]=!1)}}function Ke(t){if(typeof t>"u")Object.keys(D).forEach(i=>{Array.isArray(D[i])&&D[i].forEach(s=>Tt(s)),delete D[i]}),Ht(null);else if(Array.isArray(t))t.forEach(i=>{i.key&&Tt(i)});else if(typeof t=="object")t.key&&Tt(t);else if(typeof t=="string"){for(var e=arguments.length,n=new Array(e>1?e-1:0),o=1;o<e;o++)n[o-1]=arguments[o];let[i,s]=n;typeof i=="function"&&(s=i,i=""),Tt({key:t,scope:i,method:s,splitKey:"+"})}}const Tt=t=>{let{key:e,scope:n,method:o,splitKey:i="+"}=t;De(e).forEach(a=>{const r=a.split(i),l=r.length,f=r[l-1],h=f==="*"?"*":pt(f);if(!D[h])return;n||(n=mt());const u=l>1?Ue(W,r):[],g=[];D[h]=D[h].filter(v=>{const m=(o?v.method===o:!0)&&v.scope===n&&co(v.mods,u);return m&&g.push(v.element),!m}),g.forEach(v=>Ht(v))})};function Xe(t,e,n,o){if(e.element!==o)return;let i;if(e.scope===n||e.scope==="all"){i=e.mods.length>0;for(const s in X)Object.prototype.hasOwnProperty.call(X,s)&&(!X[s]&&e.mods.indexOf(+s)>-1||X[s]&&e.mods.indexOf(+s)===-1)&&(i=!1);(e.mods.length===0&&!X[16]&&!X[18]&&!X[17]&&!X[91]||i||e.shortcut==="*")&&(e.keys=[],e.keys=e.keys.concat(U),e.method(t,e)===!1&&(t.preventDefault?t.preventDefault():t.returnValue=!1,t.stopPropagation&&t.stopPropagation(),t.cancelBubble&&(t.cancelBubble=!0)))}}function Pe(t,e){const n=D["*"];let o=t.keyCode||t.which||t.charCode;if(!j.filter.call(this,t))return;if((o===93||o===224)&&(o=91),U.indexOf(o)===-1&&o!==229&&U.push(o),["metaKey","ctrlKey","altKey","shiftKey"].forEach(r=>{const l=Lt[r];t[r]&&U.indexOf(l)===-1?U.push(l):!t[r]&&U.indexOf(l)>-1?U.splice(U.indexOf(l),1):r==="metaKey"&&t[r]&&(U=U.filter(f=>f in Lt||f===o))}),o in X){X[o]=!0;for(const r in W)W[r]===o&&(j[r]=!0);if(!n)return}for(const r in X)Object.prototype.hasOwnProperty.call(X,r)&&(X[r]=t[Lt[r]]);t.getModifierState&&!(t.altKey&&!t.ctrlKey)&&t.getModifierState("AltGraph")&&(U.indexOf(17)===-1&&U.push(17),U.indexOf(18)===-1&&U.push(18),X[17]=!0,X[18]=!0);const i=mt();if(n)for(let r=0;r<n.length;r++)n[r].scope===i&&(t.type==="keydown"&&n[r].keydown||t.type==="keyup"&&n[r].keyup)&&Xe(t,n[r],i,e);if(!(o in D))return;const s=D[o],a=s.length;for(let r=0;r<a;r++)if((t.type==="keydown"&&s[r].keydown||t.type==="keyup"&&s[r].keyup)&&s[r].key){const l=s[r],{splitKey:f}=l,h=l.key.split(f),u=[];for(let g=0;g<h.length;g++)u.push(pt(h[g]));u.sort().join("")===U.sort().join("")&&Xe(t,l,i,e)}}function j(t,e,n){U=[];const o=De(t);let i=[],s="all",a=document,r=0,l=!1,f=!0,h="+",u=!1,g=!1;for(n===void 0&&typeof e=="function"&&(n=e),Object.prototype.toString.call(e)==="[object Object]"&&(e.scope&&(s=e.scope),e.element&&(a=e.element),e.keyup&&(l=e.keyup),e.keydown!==void 0&&(f=e.keydown),e.capture!==void 0&&(u=e.capture),typeof e.splitKey=="string"&&(h=e.splitKey),e.single===!0&&(g=!0)),typeof e=="string"&&(s=e),g&&Ke(t,s);r<o.length;r++)t=o[r].split(h),i=[],t.length>1&&(i=Ue(W,t)),t=t[t.length-1],t=t==="*"?"*":pt(t),t in D||(D[t]=[]),D[t].push({keyup:l,keydown:f,scope:s,mods:i,shortcut:o[r],method:n,key:o[r],splitKey:h,element:a});if(typeof a<"u"&&window){if(!Q.has(a)){const v=function(){let m=arguments.length>0&&arguments[0]!==void 0?arguments[0]:window.event;return Pe(m,a)},p=function(){let m=arguments.length>0&&arguments[0]!==void 0?arguments[0]:window.event;Pe(m,a),bo(m)};Q.set(a,{keydownListener:v,keyupListenr:p,capture:u}),Pt(a,"keydown",v,u),Pt(a,"keyup",p,u)}if(!gt){const v=()=>{U=[]};gt={listener:v,capture:u},Pt(window,"focus",v,u)}}}function Ao(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:"all";Object.keys(D).forEach(n=>{D[n].filter(i=>i.scope===e&&i.shortcut===t).forEach(i=>{i&&i.method&&i.method()})})}function Ht(t){const e=Object.values(D).flat();if(e.findIndex(o=>{let{element:i}=o;return i===t})<0){const{keydownListener:o,keyupListenr:i,capture:s}=Q.get(t)||{};o&&i&&(ft(t,"keyup",i,s),ft(t,"keydown",o,s),Q.delete(t))}if((e.length<=0||Q.size<=0)&&(Object.keys(Q).forEach(i=>{const{keydownListener:s,keyupListenr:a,capture:r}=Q.get(i)||{};s&&a&&(ft(i,"keyup",a,r),ft(i,"keydown",s,r),Q.delete(i))}),Q.clear(),Object.keys(D).forEach(i=>delete D[i]),gt)){const{listener:i,capture:s}=gt;ft(window,"focus",i,s),gt=null}}const $t={getPressedKeyString:ho,setScope:Ye,getScope:mt,deleteScope:yo,getPressedKeyCodes:fo,getAllKeyCodes:go,isPressed:mo,filter:po,trigger:Ao,unbind:Ke,keyMap:ht,modifier:W,modifierMap:Lt};for(const t in $t)Object.prototype.hasOwnProperty.call($t,t)&&(j[t]=$t[t]);if(typeof window<"u"){const t=window.hotkeys;j.noConflict=e=>(e&&window.hotkeys===j&&(window.hotkeys=t),j),window.hotkeys=j}const He=300,$e=["up","down","left","right"],je=zt?"⌘+a":"ctrl+a",wo=[...$e.map(t=>`shift+${t}`),je],Fe=(t,e,n)=>{let o;const{annotatingEnabled:i,offsetReferenceSelector:s,selectionMode:a}=n,r=T=>o=T;let l;const f=T=>l=T,{store:h,selection:u}=e;let g,v,p;const m=T=>{v!==!1&&(g=et(T.target)?void 0:{annotation:ve(),selector:[],creator:o,created:new Date})},w=bt(T=>{const _=document.getSelection();if(!(_!=null&&_.anchorNode))return;if(et(_.anchorNode)){g=void 0;return}const P=T.timeStamp-((p==null?void 0:p.timeStamp)||T.timeStamp);if((p==null?void 0:p.type)==="pointerdown"&&(P<1e3&&!g||_.isCollapsed&&P<He)&&m(p||T),!g)return;if(_.isCollapsed){h.getAnnotation(g.annotation)&&(u.clear(),h.deleteAnnotation(g.annotation));return}const y=_.getRangeAt(0),E=ee(y,t);if(Jt(E))return;const A=qt(E.cloneRange());(A.length!==g.selector.length||A.some((I,R)=>{var N;return I.toString()!==((N=g.selector[R])==null?void 0:N.quote)}))&&(g={...g,selector:A.map(I=>te(I,t,s)),updated:new Date},h.getAnnotation(g.annotation)?h.updateTarget(g,k.LOCAL):u.clear())}),d=T=>{et(T.target)||(p=yt(T),v=p.button===0)},c=T=>{if(et(T.target)||!v)return;const _=()=>{const{x:y,y:E}=t.getBoundingClientRect(),A=T.target instanceof Node&&t.contains(T.target)&&h.getAt(T.clientX-y,T.clientY-E,a==="all",l);if(A){const{selected:L}=u,I=new Set(L.map(H=>H.id)),R=Array.isArray(A)?A.map(H=>H.id):[A.id];(I.size!==R.length||!R.every(H=>I.has(H)))&&u.userSelect(R,T)}else u.clear()},P=T.timeStamp-p.timeStamp;setTimeout(()=>{const y=document.getSelection();y!=null&&y.isCollapsed&&P<He?(g=void 0,_()):g&&g.selector.length>0&&(M(),u.userSelect(g.annotation,yt(T)))})},b=T=>{const _=document.getSelection();_!=null&&_.isCollapsed||((!g||g.selector.length===0)&&w(T),M(),u.userSelect(g.annotation,yt(T)))},x=T=>{T.key==="Shift"&&g&&(document.getSelection().isCollapsed||(M(),u.userSelect(g.annotation,st(T))))},S=T=>{const _=()=>setTimeout(()=>{(g==null?void 0:g.selector.length)>0&&(u.clear(),h.addAnnotation({id:g.annotation,bodies:[],target:g}),u.userSelect(g.annotation,st(T))),document.removeEventListener("selectionchange",_)},100);document.addEventListener("selectionchange",_),m(T)};j(wo.join(","),{element:t,keydown:!0,keyup:!1},T=>{T.repeat||(p=st(T))}),j(je,{keydown:!0,keyup:!1},T=>{p=st(T),S(T)});const C=T=>{T.repeat||T.target!==t&&T.target!==document.body||(g=void 0,u.clear())};j($e.join(","),{keydown:!0,keyup:!1},C);const M=()=>{const T=h.getAnnotation(g.annotation);if(!T){h.addAnnotation({id:g.annotation,bodies:[],target:g});return}const{target:{updated:_}}=T,{updated:P}=g;(!_||!P||_<P)&&h.updateTarget(g)};return t.addEventListener("pointerdown",d),document.addEventListener("pointerup",c),document.addEventListener("contextmenu",b),i&&(t.addEventListener("keyup",x),t.addEventListener("selectstart",m),document.addEventListener("selectionchange",w)),{destroy:()=>{t.removeEventListener("pointerdown",d),document.removeEventListener("pointerup",c),document.removeEventListener("contextmenu",b),t.removeEventListener("keyup",x),t.removeEventListener("selectstart",m),document.removeEventListener("selectionchange",w),j.unbind()},setFilter:f,setUser:r}},ze=(t,e)=>({...t,annotatingEnabled:t.annotatingEnabled??e.annotatingEnabled,user:t.user||e.user}),We="SPANS",Eo=(t,e={})=>{Ft(t),Wt(t);const n=ze(e,{annotatingEnabled:!0,user:Fn()}),o=_e(t,n.userSelectAction),{selection:i,viewport:s}=o,a=o.store,r=Un(a),l=Vn(o,r,n.adapter);let f=n.user;const h=n.renderer==="CSS_HIGHLIGHTS"?CSS.highlights?"CSS_HIGHLIGHTS":We:n.renderer||We,u=h==="SPANS"?we(t,o,s):h==="CSS_HIGHLIGHTS"?be(t,o,s):h==="CANVAS"?se(t,o,s):void 0;if(!u)throw`Unknown renderer implementation: ${h}`;console.debug(`Using ${h} renderer`),n.style&&u.setStyle(n.style);const g=Fe(t,o,n);return g.setUser(f),{...Kn(o,r,n.adapter),destroy:()=>{u.destroy(),g.destroy(),r.destroy()},element:t,getUser:()=>f,setFilter:C=>{u.setFilter(C),g.setFilter(C)},setStyle:C=>u.setStyle(C),setUser:C=>{f=C,g.setUser(C)},setSelected:C=>{C?i.setSelected(C):i.clear()},setPresenceProvider:C=>{C&&(u.setPainter(Ne(C,n.presence)),C.on("selectionChange",()=>u.redraw()))},setVisible:C=>u.setVisible(C),on:l.on,off:l.off,scrollIntoView:oe(t,a),state:o}};O.DEFAULT_SELECTED_STYLE=at,O.DEFAULT_STYLE=q,O.NOT_ANNOTATABLE_CLASS=J,O.NOT_ANNOTATABLE_SELECTOR=Z,O.Origin=k,O.SelectionHandler=Fe,O.UserSelectAction=xe,O.W3CTextFormat=Gn,O.cancelSingleClickEvents=Ft,O.cloneKeyboardEvent=st,O.clonePointerEvent=yt,O.createBody=Cn,O.createCanvasRenderer=se,O.createHighlightsRenderer=be,O.createPresencePainter=Ne,O.createRenderer=ye,O.createSpansRenderer=we,O.createTextAnnotator=Eo,O.createTextAnnotatorState=_e,O.debounce=bt,O.fillDefaults=ze,O.getQuoteContext=Gt,O.getRangeAnnotatableContents=At,O.isMac=zt,O.isNotAnnotatable=et,O.isRangeAnnotatable=jt,O.isRevived=z,O.isWhitespaceOrEmpty=Jt,O.mergeClientRects=Zt,O.paint=ie,O.parseW3CTextAnnotation=Me,O.programmaticallyFocusable=Wt,O.rangeToSelector=te,O.reviveAnnotation=wt,O.reviveSelector=Ot,O.reviveTarget=rt,O.scrollIntoView=oe,O.serializeW3CTextAnnotation=Be,O.splitAnnotatableRanges=qt,O.toDomRectList=Je,O.trimRangeToContainer=ee,O.whitespaceOrEmptyRegex=Qt,Object.defineProperty(O,Symbol.toStringTag,{value:"Module"})});
2
3
  //# sourceMappingURL=text-annotator.umd.js.map