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