@recogito/text-annotator 3.0.0-rc.10 → 3.0.0-rc.12

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