@recogito/text-annotator 3.0.0-rc.3 → 3.0.0-rc.5
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.
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(X,z){typeof exports=="object"&&typeof module<"u"?z(exports):typeof define=="function"&&define.amd?define(["exports"],z):(X=typeof globalThis<"u"?globalThis:X||self,z(X.RecogitoJS={}))})(this,function(X){"use strict";const z={fill:"rgb(0, 128, 255",fillOpacity:.18},gt={fill:"rgb(0, 128, 255)",fillOpacity:.45},G={paint:(e,t,n,o,i,s)=>{const r=s?typeof s=="function"?s(e,i):s:i?gt:z;n.fillStyle=r.fill,n.globalAlpha=r.fillOpacity||1,t.forEach(({x:c,y:u,width:d,height:m})=>n.fillRect(c,u-2.5,d,m+5))}},pt=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)}},Ae="",mt=(e,t=10)=>{let n;return(...o)=>{clearTimeout(n),n=setTimeout(()=>e.apply(void 0,o),t)}},et=(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},nt=(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)}},ot=(e,t,n)=>{const{store:o,selection:i,hover:s}=t;let r,c,u=G;const d=pt(n);e.classList.add("r6o-annotatable");const m=et("r6o-highlight-layer bg"),f=et("r6o-highlight-layer fg",!0),x=m.getContext("2d"),C=f.getContext("2d");e.insertBefore(m,e.firstChild),e.appendChild(f),e.addEventListener("pointermove",g=>{const{x:E,y:R}=e.getBoundingClientRect(),M=o.getAt(g.clientX-E,g.clientY-R);M&&(!c||c(M))?s.current!==M.id&&(e.classList.add("hovered"),s.set(M.id)):s.current&&(e.classList.remove("hovered"),s.set(null))});const v=()=>{const{top:g,left:E}=e.getBoundingClientRect(),{innerWidth:R,innerHeight:M}=window,U=-E,p=-g,w=R-E,y=M-g;return{top:g,left:E,minX:U,minY:p,maxX:w,maxY:y}},B=()=>h();document.addEventListener("scroll",B,{capture:!0,passive:!0});const b=mt(()=>{nt(m),nt(f,!0),o.recalculatePositions(),h()});window.addEventListener("resize",b),new ResizeObserver(b).observe(e);const h=()=>requestAnimationFrame(()=>{const{top:g,left:E,minX:R,minY:M,maxX:U,maxY:p}=v(),w=c?o.getIntersectingRects(R,M,U,p).filter(({annotation:O})=>c(O)):o.getIntersectingRects(R,M,U,p),{width:y,height:T}=f,S=new Set(i.selected.map(({id:O})=>O));C.clearRect(-.5,-.5,y+1,T+1),x.clearRect(-.5,-.5,y+1,T+1),w.forEach(({annotation:O,rects:Y})=>{const he=Y.map(({x:fe,y:ge,width:pe,height:me})=>({x:fe+E,y:ge+g,width:pe,height:me})),ue=S.has(O.id);u.paint(O,he,x,C,ue,r)}),setTimeout(()=>d(w.map(({annotation:O})=>O)),1)});return o.observe(()=>h()),i.subscribe(()=>h()),{redraw:h,setDrawingStyle:g=>{r=g,h()},setFilter:g=>{c=g,h()},setPainter:g=>u=g}},_=(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 r=0,c=document.createRange(),u=s.nextNode();u===null&&console.error("Could not revive annotation target. Content missing.");let d=!i;for(;u!==null;){const m=u.textContent.length;if(!d&&i&&(d=i.contains(u)),d){if(r+m>n){c.setStart(u,n-r);break}r+=m}u=s.nextNode()}for(;u!==null;){const m=u.textContent.length;if(r+m>o){c.setEnd(u,o-r);break}r+=m,u=s.nextNode()}return{...e,selector:{...e.selector,range:c}}};var it=Object.prototype.hasOwnProperty;function D(e,t){var n,o;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((o=e.length)===t.length)for(;o--&&D(e[o],t[o]););return o===-1}if(!n||typeof e=="object"){o=0;for(n in e)if(it.call(e,n)&&++o&&!it.call(t,n)||!(n in t)||!D(e[n],t[n]))return!1;return Object.keys(t).length===o}}return e!==e&&t!==t}function J(){}function At(e,t){return e!=e?t==t:e!==t||e&&typeof e=="object"||typeof e=="function"}const N=[];function K(e,t=J){let n;const o=new Set;function i(c){if(At(e,c)&&(e=c,n)){const u=!N.length;for(const d of o)d[1](),N.push(d,e);if(u){for(let d=0;d<N.length;d+=2)N[d][0](N[d+1]);N.length=0}}}function s(c){i(c(e))}function r(c,u=J){const d=[c,u];return o.add(d),o.size===1&&(n=t(i,s)||J),c(e),()=>{o.delete(d),o.size===0&&n&&(n(),n=null)}}return{set:i,update:s,subscribe:r}}const xt=e=>{const{subscribe:t,set:n}=K(null);let o=null;return t(i=>o=i),e.observe(({changes:i})=>{if(o){i.deleted.some(c=>c.id===o)&&n(null);const r=i.updated.find(({oldValue:c})=>c.id===o);r&&n(r.newValue.id)}}),{get current(){return o},subscribe:t,set:n}},Q={selected:[]},wt=(e,t="EDIT")=>{const{subscribe:n,set:o}=K(Q);let i=Q;n(f=>i=f);const s=()=>o(Q),r=()=>{var f;return((f=i.selected)==null?void 0:f.length)===0},c=f=>{if(i.selected.length===0)return!1;const x=typeof f=="string"?f:f.id;return i.selected.some(C=>C.id===x)},u=(f,x)=>{const C=e.getAnnotation(f);if(C){const v=yt(C,t);o(v==="EDIT"?{selected:[{id:f,editable:!0}],pointerEvent:x}:v==="SELECT"?{selected:[{id:f}],pointerEvent:x}:{selected:[],pointerEvent:x})}else console.warn("Invalid selection: "+f)},d=(f,x=!0)=>{const C=Array.isArray(f)?f:[f],v=C.map(B=>e.getAnnotation(B)).filter(B=>B);o({selected:v.map(({id:B})=>({id:B,editable:x}))}),v.length!==C.length&&console.warn("Invalid selection",f)},m=f=>{if(i.selected.length===0)return!1;const{selected:x}=i;x.filter(({id:v})=>f.includes(v)).length>0&&o({selected:x.filter(({id:v})=>!f.includes(v))})};return e.observe(({changes:f})=>m(f.deleted.map(x=>x.id))),{clear:s,clickSelect:u,get selected(){return i?[...i.selected]:null},get pointerEvent(){return i?i.pointerEvent:null},isEmpty:r,isSelected:c,setSelected:d,subscribe:n}},yt=(e,t)=>typeof t=="function"?t(e)||"EDIT":t||"EDIT";let F;const bt=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(bt)}const I=[];for(let e=0;e<256;++e)I.push((e+256).toString(16).slice(1));function vt(e,t=0){return I[e[t+0]]+I[e[t+1]]+I[e[t+2]]+I[e[t+3]]+"-"+I[e[t+4]]+I[e[t+5]]+"-"+I[e[t+6]]+I[e[t+7]]+"-"+I[e[t+8]]+I[e[t+9]]+"-"+I[e[t+10]]+I[e[t+11]]+I[e[t+12]]+I[e[t+13]]+I[e[t+14]]+I[e[t+15]]}const st={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function Tt(e,t,n){if(st.randomUUID&&!t&&!e)return st.randomUUID();e=e||{};const o=e.random||(e.rng||Et)();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 vt(o)}const Ct=(e,t)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},St=(e,t)=>{const n=new Set(t.bodies.map(o=>o.id));return e.bodies.filter(o=>!n.has(o.id))},Bt=(e,t)=>t.bodies.map(n=>{const o=e.bodies.find(i=>i.id===n.id);return{newBody:n,oldBody:o&&!D(o,n)?o:void 0}}).filter(({oldBody:n})=>n),Rt=(e,t)=>!D(e.target,t.target),rt=(e,t)=>{const n=Ct(e,t),o=St(e,t),i=Bt(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:Rt(e,t)?{oldTarget:e.target,newTarget:t.target}:void 0}};var L=(e=>(e.LOCAL="LOCAL",e.REMOTE="REMOTE",e))(L||{});const Lt=(e,t)=>{var s,r;const{changes:n,origin:o}=t;if(!(!e.options.origin||e.options.origin===o))return!1;if(e.options.ignore){const{ignore:c}=e.options,u=m=>(m==null?void 0:m.length)>0;if(!(u(n.created)||u(n.deleted))){const m=(s=n.updated)==null?void 0:s.some(x=>u(x.bodiesCreated)||u(x.bodiesDeleted)||u(x.bodiesUpdated)),f=(r=n.updated)==null?void 0:r.some(x=>x.targetUpdated);if(c==="BODY_ONLY"&&m&&!f||c==="TARGET_ONLY"&&f&&!m)return!1}}if(e.options.annotations){const c=new Set([...n.created.map(d=>d.id),...n.deleted.map(d=>d.id),...n.updated.map(({oldValue:d})=>d.id)]);return!!(Array.isArray(e.options.annotations)?e.options.annotations:[e.options.annotations]).find(d=>c.has(d))}else return!0},Mt=(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)),r=new Set((t.updated||[]).map(({oldValue:f})=>f.id)),c=new Set((t.updated||[]).filter(({oldValue:f})=>n.has(f.id)||o.has(f.id)).map(({oldValue:f})=>f.id)),u=[...(e.created||[]).filter(f=>!s.has(f.id)).map(f=>r.has(f.id)?t.updated.find(({oldValue:x})=>x.id===f.id).newValue:f),...t.created||[]],d=[...(e.deleted||[]).filter(f=>!i.has(f.id)),...(t.deleted||[]).filter(f=>!n.has(f.id))],m=[...(e.updated||[]).filter(({newValue:f})=>!s.has(f.id)).map(f=>{const{oldValue:x,newValue:C}=f;if(r.has(C.id)){const v=t.updated.find(B=>B.oldValue.id===C.id).newValue;return rt(x,v)}else return f}),...(t.updated||[]).filter(({oldValue:f})=>!c.has(f.id))];return{created:u,deleted:d,updated:m}},Ot=e=>e.id!==void 0,Xt=()=>{const e=new Map,t=new Map,n=[],o=(p,w={})=>n.push({onChange:p,options:w}),i=p=>{const w=n.findIndex(y=>y.onChange==p);w>-1&&n.splice(w,1)},s=(p,w)=>{const y={origin:p,changes:{created:w.created||[],updated:w.updated||[],deleted:w.deleted||[]},state:[...e.values()]};n.forEach(T=>{Lt(T,y)&&T.onChange(y)})},r=(p,w=L.LOCAL)=>{if(e.get(p.id))throw Error(`Cannot add annotation ${p.id} - exists already`);e.set(p.id,p),p.bodies.forEach(T=>t.set(T.id,p.id)),s(w,{created:[p]})},c=(p,w)=>{const y=typeof p=="string"?w:p,T=typeof p=="string"?p:p.id,S=e.get(T);if(S){const O=rt(S,y);return T===y.id?e.set(T,y):(e.delete(T),e.set(y.id,y)),S.bodies.forEach(Y=>t.delete(Y.id)),y.bodies.forEach(Y=>t.set(Y.id,y.id)),O}else console.warn(`Cannot update annotation ${T} - does not exist`)},u=(p,w=L.LOCAL,y=L.LOCAL)=>{const T=Ot(w)?y:w,S=c(p,w);S&&s(T,{updated:[S]})},d=(p,w=L.LOCAL)=>{const y=p.reduce((T,S)=>{const O=c(S);return O?[...T,O]:T},[]);y.length>0&&s(w,{updated:y})},m=(p,w=L.LOCAL)=>{const y=e.get(p.annotation);if(y){const T={...y,bodies:[...y.bodies,p]};e.set(y.id,T),t.set(p.id,T.id),s(w,{updated:[{oldValue:y,newValue:T,bodiesCreated:[p]}]})}else console.warn(`Attempt to add body to missing annotation: ${p.annotation}`)},f=()=>[...e.values()],x=(p=L.LOCAL)=>{const w=[...e.values()];e.clear(),t.clear(),s(p,{deleted:w})},C=(p,w=!0,y=L.LOCAL)=>{if(w){const T=[...e.values()];e.clear(),t.clear(),p.forEach(S=>{e.set(S.id,S),S.bodies.forEach(O=>t.set(O.id,S.id))}),s(y,{created:p,deleted:T})}else{const T=p.reduce((S,O)=>{const Y=e.get(O.id);return Y?[...S,Y]:S},[]);if(T.length>0)throw Error(`Bulk insert would overwrite the following annotations: ${T.map(S=>S.id).join(", ")}`);p.forEach(S=>{e.set(S.id,S),S.bodies.forEach(O=>t.set(O.id,S.id))}),s(y,{created:p})}},v=p=>{const w=typeof p=="string"?p:p.id,y=e.get(w);if(y)return e.delete(w),y.bodies.forEach(T=>t.delete(T.id)),y;console.warn(`Attempt to delete missing annotation: ${w}`)},B=(p,w=L.LOCAL)=>{const y=v(p);y&&s(w,{deleted:[y]})},b=(p,w=L.LOCAL)=>{const y=p.reduce((T,S)=>{const O=v(S);return O?[...T,O]:T},[]);y.length>0&&s(w,{deleted:y})},l=(p,w=L.LOCAL)=>{const y=e.get(p.annotation);if(y){const T=y.bodies.find(S=>S.id===p.id);if(T){t.delete(T.id);const S={...y,bodies:y.bodies.filter(Y=>Y.id!==p.id)};e.set(y.id,S),s(w,{updated:[{oldValue:y,newValue:S,bodiesDeleted:[T]}]})}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}`)},h=p=>{const w=e.get(p);return w?{...w}:void 0},a=p=>{const w=t.get(p);if(w){const T=h(w).bodies.find(S=>S.id===p);if(T)return T;console.error(`Store integrity error: body ${p} in index, but not in annotation`)}else console.warn(`Attempt to retrieve missing body: ${p}`)},A=(p,w)=>{if(p.annotation!==w.annotation)throw"Annotation integrity violation: annotation ID must be the same when updating bodies";const y=e.get(p.annotation);if(y){const T=y.bodies.find(O=>O.id===p.id),S={...y,bodies:y.bodies.map(O=>O.id===T.id?w:O)};return e.set(y.id,S),T.id!==w.id&&(t.delete(T.id),t.set(w.id,S.id)),{oldValue:y,newValue:S,bodiesUpdated:[{oldBody:T,newBody:w}]}}else console.warn(`Attempt to add body to missing annotation ${p.annotation}`)},g=(p,w,y=L.LOCAL)=>{const T=A(p,w);s(y,{updated:[T]})},E=(p,w=L.LOCAL)=>{const y=p.map(T=>A({id:T.id,annotation:T.annotation},T));s(w,{updated:y})},R=p=>{const w=e.get(p.annotation);if(w){const y={...w,target:{...w.target,...p}};return e.set(w.id,y),{oldValue:w,newValue:y,targetUpdated:{oldTarget:w.target,newTarget:p}}}else console.warn(`Attempt to update target on missing annotation: ${p.annotation}`)};return{addAnnotation:r,addBody:m,all:f,bulkAddAnnotation:C,bulkDeleteAnnotation:b,bulkUpdateAnnotation:d,bulkUpdateBodies:E,bulkUpdateTargets:(p,w=L.LOCAL)=>{const y=p.map(R).filter(T=>T);y.length>0&&s(w,{updated:y})},clear:x,deleteAnnotation:B,deleteBody:l,getAnnotation:h,getBody:a,observe:o,unobserve:i,updateAnnotation:u,updateBody:g,updateTarget:(p,w=L.LOCAL)=>{const y=R(p);y&&s(w,{updated:[y]})}}};let It=()=>({emit(e,...t){let n=this.events[e]||[];for(let o=0,i=n.length;o<i;o++)n[o](...t)},events:{},on(e,t){var n;return(n=this.events[e])!=null&&n.push(t)||(this.events[e]=[t]),()=>{var o;this.events[e]=(o=this.events[e])==null?void 0:o.filter(i=>t!==i)}}});const Ut=250,Yt=e=>{const t=It(),n=[];let o=-1,i=!1,s=0;const r=a=>{if(!i){const{changes:A}=a,g=performance.now();if(g-s>Ut)n.splice(o+1),n.push(A),o=n.length-1;else{const E=n.length-1;n[E]=Mt(n[E],A)}s=g}i=!1};e.observe(r,{origin:L.LOCAL});const c=a=>(a==null?void 0:a.length)>0&&e.bulkDeleteAnnotation(a),u=a=>(a==null?void 0:a.length)>0&&e.bulkAddAnnotation(a,!1),d=a=>(a==null?void 0:a.length)>0&&e.bulkUpdateAnnotation(a.map(({oldValue:A})=>A)),m=a=>(a==null?void 0:a.length)>0&&e.bulkUpdateAnnotation(a.map(({newValue:A})=>A)),f=a=>(a==null?void 0:a.length)>0&&e.bulkAddAnnotation(a,!1),x=a=>(a==null?void 0:a.length)>0&&e.bulkDeleteAnnotation(a);return{canRedo:()=>n.length-1>o,canUndo:()=>o>-1,destroy:()=>e.unobserve(r),on:(a,A)=>t.on(a,A),redo:()=>{if(n.length-1>o){i=!0;const{created:a,updated:A,deleted:g}=n[o+1];u(a),m(A),x(g),t.emit("redo",n[o+1]),o+=1}},undo:()=>{if(o>-1){i=!0;const{created:a,updated:A,deleted:g}=n[o];c(a),d(A),f(g),t.emit("undo",n[o]),o-=1}}}},Dt=()=>{const{subscribe:e,set:t}=K([]);return{subscribe:e,set:t}},_t=(e,t,n,o)=>{const{store:i,selection:s,hover:r,viewport:c}=e,u=new Map;let d=[],m,f;const x=(l,h)=>{u.has(l)?u.get(l).push(h):u.set(l,[h])},C=(l,h)=>{const a=u.get(l);a&&a.indexOf(h)>0&&a.splice(a.indexOf(h),1)},v=(l,h,a)=>{u.has(l)&&setTimeout(()=>{u.get(l).forEach(A=>{if(n){const g=Array.isArray(h)?h.map(R=>n.serialize(R)):n.serialize(h),E=a?a instanceof PointerEvent?a:n.serialize(a):void 0;A(g,E)}else A(h,a)})},1)},B=()=>{const{selected:l}=s,h=l.map(({id:a})=>i.getAnnotation(a));h.forEach(a=>{const A=d.find(g=>g.id===a.id);(!A||!D(A,a))&&v("updateAnnotation",a,A)}),d=d.map(a=>{const A=h.find(({id:g})=>g===a.id);return A||a})};s.subscribe(({selected:l})=>{if(!(d.length===0&&l.length===0)){if(d.length===0&&l.length>0)d=l.map(({id:h})=>i.getAnnotation(h));else if(d.length>0&&l.length===0)d.forEach(h=>{const a=i.getAnnotation(h.id);a&&!D(a,h)&&v("updateAnnotation",a,h)}),d=[];else{const h=new Set(d.map(g=>g.id)),a=new Set(l.map(({id:g})=>g));d.filter(g=>!a.has(g.id)).forEach(g=>{const E=i.getAnnotation(g.id);E&&!D(E,g)&&v("updateAnnotation",E,g)}),d=[...d.filter(g=>a.has(g.id)),...l.filter(({id:g})=>!h.has(g)).map(({id:g})=>i.getAnnotation(g))]}v("selectionChanged",d)}}),r.subscribe(l=>{!m&&l?v("mouseEnterAnnotation",i.getAnnotation(l)):m&&!l?v("mouseLeaveAnnotation",i.getAnnotation(m)):m&&l&&(v("mouseLeaveAnnotation",i.getAnnotation(m)),v("mouseEnterAnnotation",i.getAnnotation(l))),m=l}),c==null||c.subscribe(l=>v("viewportIntersect",l.map(i.getAnnotation))),i.observe(l=>{o&&(f&&clearTimeout(f),f=setTimeout(B,1e3));const{created:h,deleted:a}=l.changes;h.forEach(g=>v("createAnnotation",g)),a.forEach(g=>v("deleteAnnotation",g)),l.changes.updated.filter(g=>[...g.bodiesCreated||[],...g.bodiesDeleted||[],...g.bodiesUpdated||[]].length>0).forEach(({oldValue:g,newValue:E})=>{const R=d.find(M=>M.id===g.id)||g;d=d.map(M=>M.id===g.id?E:M),v("updateAnnotation",E,R)})},{origin:L.LOCAL}),i.observe(l=>{if(d){const h=new Set(d.map(A=>A.id)),a=l.changes.updated.filter(({newValue:A})=>h.has(A.id)).map(({newValue:A})=>A);a.length>0&&(d=d.map(A=>{const g=a.find(E=>E.id===A.id);return g||A}))}},{origin:L.REMOTE});const b=l=>h=>{const{created:a,deleted:A,updated:g}=h;a.forEach(E=>v("createAnnotation",E)),A.forEach(E=>v("deleteAnnotation",E)),l?g.forEach(E=>v("updateAnnotation",E.oldValue,E.newValue)):g.forEach(E=>v("updateAnnotation",E.newValue,E.oldValue))};return t.on("undo",b(!0)),t.on("redo",b(!1)),{on:x,off:C,emit:v}},Nt=e=>t=>t.map(n=>e.serialize(n)),at=e=>t=>t.reduce((n,o)=>{const{parsed:i,error:s}=e.parse(o);return s?{parsed:n.parsed,failed:[...n.failed,o]}:{parsed:[...n.parsed,i],failed:n.failed}},{parsed:[],failed:[]}),ct=(e,t,n)=>{const{store:o,selection:i}=e,s=b=>{if(n){const{parsed:l,error:h}=n.parse(b);l?o.addAnnotation(l,L.REMOTE):console.error(h)}else o.addAnnotation(b,L.REMOTE)},r=()=>i.clear(),c=()=>o.clear(),u=b=>{const l=o.getAnnotation(b);return n&&l?n.serialize(l):l},d=()=>n?o.all().map(n.serialize):o.all(),m=()=>{var h;const l=(((h=i.selected)==null?void 0:h.map(a=>a.id))||[]).map(a=>o.getAnnotation(a));return n?l.map(n.serialize):l},f=b=>fetch(b).then(l=>l.json()).then(l=>(C(l),l)),x=b=>{if(typeof b=="string"){const l=o.getAnnotation(b);return o.deleteAnnotation(b),n?n.serialize(l):l}else{const l=n?n.parse(b).parsed:b;return o.deleteAnnotation(l),b}},C=b=>{if(n){const{parsed:l,failed:h}=at(n)(b);h.length>0&&console.warn(`Discarded ${h.length} invalid annotations`,h),o.bulkAddAnnotation(l,!0,L.REMOTE)}else o.bulkAddAnnotation(b,!0,L.REMOTE)},v=b=>{b?i.setSelected(b):i.clear()},B=b=>{if(n){const l=n.parse(b).parsed,h=n.serialize(o.getAnnotation(l.id));return o.updateAnnotation(l),h}else{const l=o.getAnnotation(b.id);return o.updateAnnotation(b),l}};return{addAnnotation:s,cancelSelected:r,canRedo:t.canRedo,canUndo:t.canUndo,clearAnnotations:c,getAnnotationById:u,getAnnotations:d,getSelected:m,loadAnnotations:f,redo:t.redo,removeAnnotation:x,setAnnotations:C,setSelected:v,undo:t.undo,updateAnnotation:B}};let kt=e=>crypto.getRandomValues(new Uint8Array(e)),Vt=(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 r="";for(;;){let c=n(i),u=i;for(;u--;)if(r+=e[c[u]&o]||"",r.length===s)return r}}},zt=(e,t=21)=>Vt(e,t,kt),Pt=(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 lt=()=>({isGuest:!0,id:zt("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",20)()}),Ht=e=>{const t=JSON.stringify(e);let n=0;for(let o=0,i=t.length;o<i;o++){let s=t.charCodeAt(o);n=(n<<5)-n+s,n|=0}return`${n}`},Wt=(e,t)=>(Array.isArray(e)?e:[e]).map(n=>{const{id:o,type:i,purpose:s,value:r,created:c,creator:u,...d}=n;return{id:o||`temp-${Ht(n)}`,annotation:t,type:i,purpose:s,value:r,created:c,creator:u?typeof u=="object"?{...u}:u:void 0,...d}}),Ft=e=>e.map(t=>{var o;const n={...t};return delete n.annotation,(o=n.id)!=null&&o.startsWith("temp-")&&delete n.id,n});Pt();function $t(e,t,n,o,i){dt(e,t,n||0,o||e.length-1,i||qt)}function dt(e,t,n,o,i){for(;o>n;){if(o-n>600){var s=o-n+1,r=t-n+1,c=Math.log(s),u=.5*Math.exp(2*c/3),d=.5*Math.sqrt(c*u*(s-u)/s)*(r-s/2<0?-1:1),m=Math.max(n,Math.floor(t-r*u/s+d)),f=Math.min(o,Math.floor(t+(s-r)*u/s+d));dt(e,t,m,f,i)}var x=e[t],C=n,v=o;for(P(e,n,t),i(e[o],x)>0&&P(e,n,o);C<v;){for(P(e,C,v),C++,v--;i(e[C],x)<0;)C++;for(;i(e[v],x)>0;)v--}i(e[n],x)===0?P(e,n,v):(v++,P(e,v,o)),v<=t&&(n=v+1),t<=v&&(o=v-1)}}function P(e,t,n){var o=e[t];e[t]=e[n],e[n]=o}function qt(e,t){return e<t?-1:e>t?1:0}class jt{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(!q(t,n))return o;const i=this.toBBox,s=[];for(;n;){for(let r=0;r<n.children.length;r++){const c=n.children[r],u=n.leaf?i(c):c;q(t,u)&&(n.leaf?o.push(c):tt(t,u)?this._all(c,o):s.push(c))}n=s.pop()}return o}collides(t){let n=this.data;if(!q(t,n))return!1;const o=[];for(;n;){for(let i=0;i<n.children.length;i++){const s=n.children[i],r=n.leaf?this.toBBox(s):s;if(q(t,r)){if(n.leaf||tt(t,r))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=V([]),this}remove(t,n){if(!t)return this;let o=this.data;const i=this.toBBox(t),s=[],r=[];let c,u,d;for(;o||s.length;){if(o||(o=s.pop(),u=s[s.length-1],c=r.pop(),d=!0),o.leaf){const m=Gt(t,o.children,n);if(m!==-1)return o.children.splice(m,1),s.push(o),this._condense(s),this}!d&&!o.leaf&&tt(o,i)?(s.push(o),r.push(c),c=0,u=o,o=o.children[0]):u?(c++,o=u.children[c],d=!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 r=this._maxEntries,c;if(s<=r)return c=V(t.slice(n,o+1)),k(c,this.toBBox),c;i||(i=Math.ceil(Math.log(s)/Math.log(r)),r=Math.ceil(s/Math.pow(r,i-1))),c=V([]),c.leaf=!1,c.height=i;const u=Math.ceil(s/r),d=u*Math.ceil(Math.sqrt(r));ht(t,n,o,d,this.compareMinX);for(let m=n;m<=o;m+=d){const f=Math.min(m+d-1,o);ht(t,m,f,u,this.compareMinY);for(let x=m;x<=f;x+=u){const C=Math.min(x+u-1,f);c.children.push(this._build(t,x,C,i-1))}}return k(c,this.toBBox),c}_chooseSubtree(t,n,o,i){for(;i.push(n),!(n.leaf||i.length-1===o);){let s=1/0,r=1/0,c;for(let u=0;u<n.children.length;u++){const d=n.children[u],m=Z(d),f=Qt(t,d)-m;f<r?(r=f,s=m<s?m:s,c=d):f===r&&m<s&&(s=m,c=d)}n=c||n.children[0]}return n}_insert(t,n,o){const i=o?t:this.toBBox(t),s=[],r=this._chooseSubtree(i,this.data,n,s);for(r.children.push(t),W(r,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 r=this._chooseSplitIndex(o,s,i),c=V(o.children.splice(r,o.children.length-r));c.height=o.height,c.leaf=o.leaf,k(o,this.toBBox),k(c,this.toBBox),n?t[n-1].children.push(c):this._splitRoot(o,c)}_splitRoot(t,n){this.data=V([t,n]),this.data.height=t.height+1,this.data.leaf=!1,k(this.data,this.toBBox)}_chooseSplitIndex(t,n,o){let i,s=1/0,r=1/0;for(let c=n;c<=o-n;c++){const u=H(t,0,c,this.toBBox),d=H(t,c,o,this.toBBox),m=Zt(u,d),f=Z(u)+Z(d);m<s?(s=m,i=c,r=f<r?f:r):m===s&&f<r&&(r=f,i=c)}return i||o-n}_chooseSplitAxis(t,n,o){const i=t.leaf?this.compareMinX:Jt,s=t.leaf?this.compareMinY:Kt,r=this._allDistMargin(t,n,o,i),c=this._allDistMargin(t,n,o,s);r<c&&t.children.sort(i)}_allDistMargin(t,n,o,i){t.children.sort(i);const s=this.toBBox,r=H(t,0,n,s),c=H(t,o-n,o,s);let u=$(r)+$(c);for(let d=n;d<o-n;d++){const m=t.children[d];W(r,t.leaf?s(m):m),u+=$(r)}for(let d=o-n-1;d>=n;d--){const m=t.children[d];W(c,t.leaf?s(m):m),u+=$(c)}return u}_adjustParentBBoxes(t,n,o){for(let i=o;i>=0;i--)W(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():k(t[n],this.toBBox)}}function Gt(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 k(e,t){H(e,0,e.children.length,t,e)}function H(e,t,n,o,i){i||(i=V(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 r=e.children[s];W(i,e.leaf?o(r):r)}return i}function W(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 Jt(e,t){return e.minX-t.minX}function Kt(e,t){return e.minY-t.minY}function Z(e){return(e.maxX-e.minX)*(e.maxY-e.minY)}function $(e){return e.maxX-e.minX+(e.maxY-e.minY)}function Qt(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 Zt(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 tt(e,t){return e.minX<=t.minX&&e.minY<=t.minY&&t.maxX<=e.maxX&&t.maxY<=e.maxY}function q(e,t){return t.minX<=e.maxX&&t.minY<=e.maxY&&t.maxX>=e.minX&&t.maxY>=e.minY}function V(e){return{children:e,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function ht(e,t,n,o,i){const s=[t,n];for(;s.length;){if(n=s.pop(),t=s.pop(),n-t<=o)continue;const r=t+Math.ceil((n-t)/o/2)*o;$t(e,r,t,n,i),s.push(t,r,r,n)}}const te=(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"},ee=(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)},ne=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 r=te(n,s);if(r==="inline-adjacent"){o=o.map(c=>c===s?ee(n,s):c),i=!0;break}else if(r==="inline-contains"){o=o.map(c=>c===s?n:c),i=!0;break}else if(r==="inline-is-contained"){i=!0;break}else if(r==="block-contains"||r==="block-is-contained"){n.width<s.width&&(o=o.map(c=>c===s?n:c)),i=!0;break}}return i?o:[...o,n]},[]),oe=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(r=>r.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(r=>r.nodeType===Node.TEXT_NODE).pop();e.setEnd(s,s.textContent.length)}return e},ie=(e,t)=>{const n=new jt,o=new Map,i=l=>{const h=t.getBoundingClientRect(),A=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:_(l,t),g=Array.from(A.selector.range.getClientRects()),E=ne(g);return E.map(R=>{const{x:M,y:U,width:p,height:w}=R;return{minX:M-h.x,minY:U-h.y,maxX:M-h.x+p,maxY:U-h.y+w,annotation:{id:l.annotation,rects:E}}})},s=()=>[...o.values()],r=()=>{n.clear(),o.clear()},c=l=>{const h=i(l);h.forEach(a=>n.insert(a)),o.set(l.annotation,h)},u=l=>{o.get(l.annotation).forEach(a=>n.remove(a)),o.delete(l.annotation)},d=l=>{u(l),c(l)},m=(l,h=!0)=>{h&&r();const a=l.map(g=>({target:g,rects:i(g)}));a.forEach(({target:g,rects:E})=>o.set(g.annotation,E));const A=a.reduce((g,{rects:E})=>[...g,...E],[]);n.load(A)},f=(l,h)=>{const a=n.search({minX:l,minY:h,maxX:l,maxY:h}),A=g=>g.annotation.rects.reduce((E,R)=>E+R.width*R.height,0);if(a.length>0)return a.sort((g,E)=>A(g)-A(E)),a[0].annotation.id},x=l=>{const h=C(l);if(h.length===0)return;let a=h[0].left,A=h[0].top,g=h[0].right,E=h[0].bottom;for(let R=1;R<h.length;R++){const M=h[R];a=Math.min(a,M.left),A=Math.min(A,M.top),g=Math.max(g,M.right),E=Math.max(E,M.bottom)}return new DOMRect(a,A,g-a,E-A)},C=l=>{const h=o.get(l);return h?h[0].annotation.rects:[]};return{all:s,clear:r,getAt:f,getBoundsForAnnotation:x,getDOMRectsForAnnotation:C,getIntersectingRects:(l,h,a,A)=>n.search({minX:l,minY:h,maxX:a,maxY:A}),insert:c,recalculate:()=>m(e.all().map(l=>l.target),!0),remove:u,set:m,size:()=>n.all().length,update:d}},j=e=>{const{range:t}=e.target.selector;return t instanceof Range&&!t.collapsed},ut=(e,t)=>{const n=Xt(),o=ie(n,e),i=wt(n,t),s=xt(n),r=Dt(),c=(b,l=L.LOCAL)=>{const h=b.target.selector.range instanceof Range?b:{...b,target:_(b.target,e)},{range:a}=h.target.selector,A=a&&!a.collapsed;return A&&n.addAnnotation(h,l),A},u=(b,l=!0,h=L.LOCAL)=>{const a=b.map(g=>j(g)?g:{...g,target:_(g.target,e)});if(a.some(g=>g.target.selector.range.collapsed)){const g=a.filter(E=>E.target.selector.range.collapsed);return console.warn("Could not revive all targets"),console.warn(g),n.bulkAddAnnotation(a,l,h),g}else return n.bulkAddAnnotation(a,l,h),[]},d=(b,l=L.LOCAL)=>{const h=b.selector.range instanceof Range?b:_(b,e);n.updateTarget(h,l)},m=(b,l=L.LOCAL)=>{const h=b.map(a=>a.selector.range instanceof Range?a:_(a,e));n.bulkUpdateTargets(h,l)},f=(b,l)=>{const h=o.getAt(b,l);return h?n.getAnnotation(h):void 0},x=(b,l,h,a)=>{const A=o.getIntersectingRects(b,l,h,a);return Array.from(new Set(A.map(E=>E.annotation.id))).map(E=>n.getAnnotation(E)).filter(E=>E)},C=(b,l,h,a=5)=>{const A=o.getDOMRectsForAnnotation(b);if(A.length>0){if(l&&h){const g=A.find(({top:E,right:R,bottom:M,left:U})=>l>=U-a&&l<=R+a&&h>=E-a&&h<=M+a);if(g)return g}return o.getBoundsForAnnotation(b)}},v=(b,l,h,a)=>{const g=o.getIntersectingRects(b,l,h,a).reduce((E,R)=>((E[R.annotation.id]=E[R.annotation.id]||[]).push(R),E),{});return Object.entries(g).map(([E,R])=>({annotation:n.getAnnotation(E),rects:R.map(({minX:M,minY:U,maxX:p,maxY:w})=>({x:M,y:U,width:p-M,height:w-U}))}))},B=()=>o.recalculate();return n.observe(({changes:b})=>{const l=(b.created||[]).filter(j),h=(b.deleted||[]).filter(j),a=(b.updated||[]).filter(A=>j(A.newValue));l.length>0&&o.set(l.map(A=>A.target),!1),(h==null?void 0:h.length)>0&&h.forEach(A=>o.remove(A.target)),(a==null?void 0:a.length)>0&&a.forEach(({newValue:A})=>o.update(A.target))}),{store:{...n,addAnnotation:c,bulkAddAnnotation:u,bulkUpdateTargets:m,getAnnotationBounds:C,getAt:f,getIntersecting:x,getIntersectingRects:v,recalculatePositions:B,updateTarget:d},selection:i,hover:s,viewport:r}},se=(e,t={})=>{const n=new Map,o=s=>Array.from(n.entries()).filter(([r,c])=>c.presenceKey===s.presenceKey).map(([r,c])=>r);return e.on("selectionChange",(s,r)=>{o(s).forEach(u=>n.delete(u)),r&&r.forEach(u=>n.set(u,s))}),{paint:(s,r,c,u,d,m)=>{t.font&&(u.font=t.font);const f=n.get(s.id);if(f){const{x,y:C,height:v}=r[0];u.fillStyle=f.appearance.color,u.fillRect(x-2,C-2.5,2,v+5);const B=u.measureText(f.appearance.label),b=B.width+6,l=B.actualBoundingBoxAscent+B.actualBoundingBoxDescent+8,h=B.fontBoundingBoxAscent?8:6.5;u.fillRect(x-2,C-2.5-l,b,l),u.fillStyle="#fff",u.fillText(f.appearance.label,x+1,C-h),c.fillStyle=f.appearance.color,c.globalAlpha=d?.45:.18,r.forEach(({x:a,y:A,width:g,height:E})=>c.fillRect(a,A-2.5,g,E+5))}else G.paint(s,r,c,u,d,m)}}},ft=e=>e===null?null:e.scrollHeight>e.clientHeight?e:ft(e.parentElement),re=(e,t)=>n=>{const o=ft(e);if(o){const i=t.getAnnotation(n.id),{range:s}=i.target.selector;if(!s||s.collapsed)return!1;const r=o.getBoundingClientRect(),c=o.clientHeight,u=o.clientWidth,d=i.target.selector.range.getBoundingClientRect(),{width:m,height:f}=t.getAnnotationBounds(n.id),x=d.top-r.top,C=d.left-r.left,v=o.parentElement?o.scrollTop:0,B=o.parentElement?o.scrollLeft:0,b=x+v-(c-f)/2,l=C+B-(u-m)/2;return o.scroll({top:b,left:l,behavior:"smooth"}),!0}},ae=(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(),r=o.toString().length,c=r+s.length;return n?{quote:s,start:r,end:c,range:e,offsetReference:i}:{quote:s,start:r,end:c,range:e}},ce=(e,t,n)=>{const{store:o,selection:i}=t;let s,r=null;const c=x=>s=x;let u=!1,d;e.addEventListener("selectstart",x=>{if(!u)return;!x.target.parentElement.closest(".not-annotatable")?r={annotation:Tt(),selector:void 0,creator:s,created:new Date}:(r=null,d=void 0)});let m;document.addEventListener("selectionchange",x=>{m&&clearTimeout(m),m=setTimeout(()=>f(),50)});const f=()=>{var C,v;const x=document.getSelection();if(!x.isCollapsed&&u&&r){const B=Array.from(Array(x.rangeCount).keys()).map(h=>x.getRangeAt(h));oe(B[0]).toString()!==((v=(C=r.selector)==null?void 0:C.range)==null?void 0:v.toString())&&(r={...r,selector:ae(B[0],e,n)},o.getAnnotation(r.annotation)?o.updateTarget(r,L.LOCAL):(o.addAnnotation({id:r.annotation,bodies:[],target:r}),i.clickSelect(r.annotation,d)))}};return e.addEventListener("pointerdown",x=>{d=x,u=x.button===0}),document.addEventListener("pointerup",x=>{var v;d=x,!((v=x.target.parentElement)!=null&&v.closest(".not-annotatable")||!u)&&setTimeout(()=>{if(r!=null&&r.selector)o.updateTarget(r,L.LOCAL),i.clickSelect(r.annotation,x),r=null,d=void 0;else{const{x:B,y:b}=e.getBoundingClientRect(),l=o.getAt(x.clientX-B,x.clientY-b);if(l){const{selected:h}=i;(h.length!==1||h[0].id!==l.id)&&(i.clickSelect(l.id,x),d=void 0)}else i.isEmpty()||i.clear()}},50)}),{setUser:c}},we="",le=(e,t={})=>{const n=ut(e,t.pointerAction),{selection:o,viewport:i}=n,s=n.store,r=Yt(s),c=_t(n,r,t.adapter);let u=lt();const d=ot(e,n,i);t.style&&d.setDrawingStyle(t.style);const m=ce(e,n,t.offsetReferenceSelector);return m.setUser(u),{...ct(n,r,t.adapter),destroy:()=>{throw"Not implemented yet"},element:e,getUser:()=>u,setFilter:a=>d.setFilter(a),setStyle:a=>d.setDrawingStyle(a),setUser:a=>{u=a,m.setUser(a)},setSelected:a=>{a?o.setSelected(a):o.clear()},setPresenceProvider:a=>{a&&(d.setPainter(se(a,t.presence)),a.on("selectionChange",()=>d.redraw()))},on:c.on,off:c.off,scrollIntoView:re(e,s),state:n}},de=L;X.Origin=de,X.createAnonymousGuest=lt,X.createBaseAnnotator=ct,X.createHighlightLayer=ot,X.createTextAnnotator=le,X.createTextAnnotatorState=ut,X.defaultPainter=G,X.parseAll=at,X.parseW3CBodies=Wt,X.reviveTarget=_,X.serializeAll=Nt,X.serializeW3CBodies=Ft,Object.defineProperty(X,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(X,N){typeof exports=="object"&&typeof module<"u"?N(exports):typeof define=="function"&&define.amd?define(["exports"],N):(X=typeof globalThis<"u"?globalThis:X||self,N(X.RecogitoJS={}))})(this,function(X){"use strict";const N={fill:"rgb(0, 128, 255",fillOpacity:.18},dt={fill:"rgb(0, 128, 255)",fillOpacity:.45},j={paint:(e,t,n,o,i,s)=>{const a=s?typeof s=="function"?s(e,i):s:i?dt:N;n.fillStyle=a.fill,n.globalAlpha=a.fillOpacity||1,t.forEach(({x:r,y:d,width:u,height:b})=>n.fillRect(r,d-2.5,u,b+5))}},ut=e=>{let t=new Set;return o=>{const i=o.map(s=>s.id);(t.size!==i.length||i.some(s=>!t.has(s)))&&e.set(i),t=new Set(i)}},fe="",ht=(e,t=10)=>{let n;return(...o)=>{clearTimeout(n),n=setTimeout(()=>e.apply(void 0,o),t)}},tt=(e,t)=>{const n=document.createElement("canvas");if(n.width=t?2*window.innerWidth:window.innerWidth,n.height=t?2*window.innerHeight:window.innerHeight,n.className=e,t){const o=n.getContext("2d");o.scale(2,2),o.translate(.5,.5)}return n},et=(e,t)=>{if(e.width=t?2*window.innerWidth:window.innerWidth,e.height=t?2*window.innerHeight:window.innerHeight,t){const n=e.getContext("2d");n.scale(2,2),n.translate(.5,.5)}},nt=(e,t,n)=>{const{store:o,selection:i,hover:s}=t;let a,r,d=j;const u=ut(n);e.classList.add("r6o-annotatable");const b=tt("r6o-highlight-layer bg"),h=tt("r6o-highlight-layer fg",!0),E=b.getContext("2d"),m=h.getContext("2d");e.insertBefore(b,e.firstChild),e.appendChild(h),e.addEventListener("pointermove",w=>{const{x:T,y:M}=e.getBoundingClientRect(),f=o.getAt(w.clientX-T,w.clientY-M);f&&(!r||r(f))?s.current!==f.id&&(e.classList.add("hovered"),s.set(f.id)):s.current&&(e.classList.remove("hovered"),s.set(null))});const v=()=>{const{top:w,left:T}=e.getBoundingClientRect(),{innerWidth:M,innerHeight:f}=window,x=-T,A=-w,S=M-T,B=f-w;return{top:w,left:T,minX:x,minY:A,maxX:S,maxY:B}},L=()=>c();document.addEventListener("scroll",L,{capture:!0,passive:!0});const y=ht(()=>{et(b),et(h,!0),o.recalculatePositions(),c()});window.addEventListener("resize",y),new ResizeObserver(y).observe(e);const c=()=>requestAnimationFrame(()=>{const{top:w,left:T,minX:M,minY:f,maxX:x,maxY:A}=v(),S=r?o.getIntersectingRects(M,f,x,A).filter(({annotation:Y})=>r(Y)):o.getIntersectingRects(M,f,x,A),{width:B,height:R}=h,U=new Set(i.selected.map(({id:Y})=>Y));m.clearRect(-.5,-.5,B+1,R+1),E.clearRect(-.5,-.5,B+1,R+1),S.forEach(({annotation:Y,rects:ae})=>{const re=ae.map(({x:ce,y:de,width:ue,height:he})=>({x:ce+T,y:de+w,width:ue,height:he})),le=U.has(Y.id);d.paint(Y,re,E,m,le,a)}),setTimeout(()=>u(S.map(({annotation:Y})=>Y)),1)});return o.observe(()=>c()),i.subscribe(()=>c()),{redraw:c,setDrawingStyle:w=>{a=w,c()},setFilter:w=>{r=w,c()},setPainter:w=>d=w}},I=(e,t)=>{const{start:n,end:o}=e.selector,i=e.selector.offsetReference?e.selector.offsetReference:t;if(!i)return e;const s=document.createNodeIterator(t,NodeFilter.SHOW_TEXT);let a=0,r=document.createRange(),d=s.nextNode();d===null&&console.error("Could not revive annotation target. Content missing.");let u=!i;for(;d!==null;){const b=d.textContent.length;if(!u&&i&&(u=i.contains(d)),u){if(a+b>n){r.setStart(d,n-a);break}a+=b}d=s.nextNode()}for(;d!==null;){const b=d.textContent.length;if(a+b>o){r.setEnd(d,o-a);break}a+=b,d=s.nextNode()}return{...e,selector:{...e.selector,range:r}}};var ot=Object.prototype.hasOwnProperty;function D(e,t){var n,o;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((o=e.length)===t.length)for(;o--&&D(e[o],t[o]););return o===-1}if(!n||typeof e=="object"){o=0;for(n in e)if(ot.call(e,n)&&++o&&!ot.call(t,n)||!(n in t)||!D(e[n],t[n]))return!1;return Object.keys(t).length===o}}return e!==e&&t!==t}function J(){}function ft(e,t){return e!=e?t==t:e!==t||e&&typeof e=="object"||typeof e=="function"}const V=[];function K(e,t=J){let n;const o=new Set;function i(r){if(ft(e,r)&&(e=r,n)){const d=!V.length;for(const u of o)u[1](),V.push(u,e);if(d){for(let u=0;u<V.length;u+=2)V[u][0](V[u+1]);V.length=0}}}function s(r){i(r(e))}function a(r,d=J){const u=[r,d];return o.add(u),o.size===1&&(n=t(i)||J),r(e),()=>{o.delete(u),o.size===0&&n&&(n(),n=null)}}return{set:i,update:s,subscribe:a}}const gt=e=>{const{subscribe:t,set:n}=K(null);let o=null;return t(i=>o=i),e.observe(({changes:i})=>{if(o){i.deleted.some(a=>a.id===o)&&n(null);const s=i.updated.find(({oldValue:a})=>a.id===o);s&&n(s.newValue.id)}}),{get current(){return o},subscribe:t,set:n}},G={selected:[]},pt=(e,t="EDIT")=>{const{subscribe:n,set:o}=K(G);let i=G;n(h=>i=h);const s=()=>o(G),a=()=>{var h;return((h=i.selected)==null?void 0:h.length)===0},r=h=>{if(i.selected.length===0)return!1;const E=typeof h=="string"?h:h.id;return i.selected.some(m=>m.id===E)},d=(h,E)=>{const m=e.getAnnotation(h);if(m){const v=mt(m,t);o(v==="EDIT"?{selected:[{id:h,editable:!0}],pointerEvent:E}:v==="SELECT"?{selected:[{id:h}],pointerEvent:E}:{selected:[],pointerEvent:E})}else console.warn("Invalid selection: "+h)},u=(h,E=!0)=>{const m=Array.isArray(h)?h:[h],v=m.map(L=>e.getAnnotation(L)).filter(L=>L);o({selected:v.map(({id:L})=>({id:L,editable:E}))}),v.length!==m.length&&console.warn("Invalid selection",h)},b=h=>{if(i.selected.length===0)return!1;const{selected:E}=i;E.filter(({id:m})=>h.includes(m)).length>0&&o({selected:E.filter(({id:m})=>!h.includes(m))})};return e.observe(({changes:h})=>b(h.deleted.map(E=>E.id))),{clear:s,clickSelect:d,get selected(){return i?[...i.selected]:null},get pointerEvent(){return i?i.pointerEvent:null},isEmpty:a,isSelected:r,setSelected:u,subscribe:n}},mt=(e,t)=>typeof t=="function"?t(e)||"EDIT":t||"EDIT",bt=[];for(let e=0;e<256;++e)bt.push((e+256).toString(16).slice(1));typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto);const At=(e,t)=>{const n=new Set(e.bodies.map(o=>o.id));return t.bodies.filter(o=>!n.has(o.id))},xt=(e,t)=>{const n=new Set(t.bodies.map(o=>o.id));return e.bodies.filter(o=>!n.has(o.id))},vt=(e,t)=>t.bodies.map(n=>{const o=e.bodies.find(i=>i.id===n.id);return{newBody:n,oldBody:o&&!D(o,n)?o:void 0}}).filter(({oldBody:n})=>n),yt=(e,t)=>!D(e.target,t.target),it=(e,t)=>{const n=At(e,t),o=xt(e,t),i=vt(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 wt=(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==null?void 0:d.length)>0;if(!(r(i.created)||r(i.deleted))){const d=(n=i.updated)==null?void 0:n.some(b=>r(b.bodiesCreated)||r(b.bodiesDeleted)||r(b.bodiesUpdated)),u=(o=i.updated)==null?void 0:o.some(b=>b.targetUpdated);if(a==="BODY_ONLY"&&d&&!u||a==="TARGET_ONLY"&&u&&!d)return!1}}if(e.options.annotations){const a=new Set([...i.created.map(r=>r.id),...i.deleted.map(r=>r.id),...i.updated.map(({oldValue:r})=>r.id)]);return!!(Array.isArray(e.options.annotations)?e.options.annotations:[e.options.annotations]).find(r=>a.has(r))}else return!0},Et=(e,t)=>{const n=new Set((e.created||[]).map(h=>h.id)),o=new Set((e.updated||[]).map(({newValue:h})=>h.id)),i=new Set((t.created||[]).map(h=>h.id)),s=new Set((t.deleted||[]).map(h=>h.id)),a=new Set((t.updated||[]).map(({oldValue:h})=>h.id)),r=new Set((t.updated||[]).filter(({oldValue:h})=>n.has(h.id)||o.has(h.id)).map(({oldValue:h})=>h.id)),d=[...(e.created||[]).filter(h=>!s.has(h.id)).map(h=>a.has(h.id)?t.updated.find(({oldValue:E})=>E.id===h.id).newValue:h),...t.created||[]],u=[...(e.deleted||[]).filter(h=>!i.has(h.id)),...(t.deleted||[]).filter(h=>!n.has(h.id))],b=[...(e.updated||[]).filter(({newValue:h})=>!s.has(h.id)).map(h=>{const{oldValue:E,newValue:m}=h;if(a.has(m.id)){const v=t.updated.find(L=>L.oldValue.id===m.id).newValue;return it(E,v)}else return h}),...(t.updated||[]).filter(({oldValue:h})=>!r.has(h.id))];return{created:d,deleted:u,updated:b}},Tt=e=>e.id!==void 0,St=()=>{const e=new Map,t=new Map,n=[],o=(f,x={})=>n.push({onChange:f,options:x}),i=f=>{const x=n.findIndex(A=>A.onChange==f);x>-1&&n.splice(x,1)},s=(f,x)=>{const A={origin:f,changes:{created:x.created||[],updated:x.updated||[],deleted:x.deleted||[]},state:[...e.values()]};n.forEach(S=>{wt(S,A)&&S.onChange(A)})},a=(f,x=C.LOCAL)=>{if(e.get(f.id))throw Error(`Cannot add annotation ${f.id} - exists already`);e.set(f.id,f),f.bodies.forEach(A=>t.set(A.id,f.id)),s(x,{created:[f]})},r=(f,x)=>{const A=typeof f=="string"?x:f,S=typeof f=="string"?f:f.id,B=e.get(S);if(B){const R=it(B,A);return S===A.id?e.set(S,A):(e.delete(S),e.set(A.id,A)),B.bodies.forEach(U=>t.delete(U.id)),A.bodies.forEach(U=>t.set(U.id,A.id)),R}else console.warn(`Cannot update annotation ${S} - does not exist`)},d=(f,x=C.LOCAL,A=C.LOCAL)=>{const S=Tt(x)?A:x,B=r(f,x);B&&s(S,{updated:[B]})},u=(f,x=C.LOCAL)=>{const A=f.reduce((S,B)=>{const R=r(B);return R?[...S,R]:S},[]);A.length>0&&s(x,{updated:A})},b=(f,x=C.LOCAL)=>{const A=e.get(f.annotation);if(A){const S={...A,bodies:[...A.bodies,f]};e.set(A.id,S),t.set(f.id,S.id),s(x,{updated:[{oldValue:A,newValue:S,bodiesCreated:[f]}]})}else console.warn(`Attempt to add body to missing annotation: ${f.annotation}`)},h=()=>[...e.values()],E=(f=C.LOCAL)=>{const x=[...e.values()];e.clear(),t.clear(),s(f,{deleted:x})},m=(f,x=!0,A=C.LOCAL)=>{if(x){const S=[...e.values()];e.clear(),t.clear(),f.forEach(B=>{e.set(B.id,B),B.bodies.forEach(R=>t.set(R.id,B.id))}),s(A,{created:f,deleted:S})}else{const S=f.reduce((B,R)=>{const U=e.get(R.id);return U?[...B,U]:B},[]);if(S.length>0)throw Error(`Bulk insert would overwrite the following annotations: ${S.map(B=>B.id).join(", ")}`);f.forEach(B=>{e.set(B.id,B),B.bodies.forEach(R=>t.set(R.id,B.id))}),s(A,{created:f})}},v=f=>{const x=typeof f=="string"?f:f.id,A=e.get(x);if(A)return e.delete(x),A.bodies.forEach(S=>t.delete(S.id)),A;console.warn(`Attempt to delete missing annotation: ${x}`)},L=(f,x=C.LOCAL)=>{const A=v(f);A&&s(x,{deleted:[A]})},y=(f,x=C.LOCAL)=>{const A=f.reduce((S,B)=>{const R=v(B);return R?[...S,R]:S},[]);A.length>0&&s(x,{deleted:A})},l=(f,x=C.LOCAL)=>{const A=e.get(f.annotation);if(A){const S=A.bodies.find(B=>B.id===f.id);if(S){t.delete(S.id);const B={...A,bodies:A.bodies.filter(R=>R.id!==f.id)};e.set(A.id,B),s(x,{updated:[{oldValue:A,newValue:B,bodiesDeleted:[S]}]})}else console.warn(`Attempt to delete missing body ${f.id} from annotation ${f.annotation}`)}else console.warn(`Attempt to delete body from missing annotation ${f.annotation}`)},c=f=>{const x=e.get(f);return x?{...x}:void 0},g=f=>{const x=t.get(f);if(x){const A=c(x).bodies.find(S=>S.id===f);if(A)return A;console.error(`Store integrity error: body ${f} in index, but not in annotation`)}else console.warn(`Attempt to retrieve missing body: ${f}`)},p=(f,x)=>{if(f.annotation!==x.annotation)throw"Annotation integrity violation: annotation ID must be the same when updating bodies";const A=e.get(f.annotation);if(A){const S=A.bodies.find(R=>R.id===f.id),B={...A,bodies:A.bodies.map(R=>R.id===S.id?x:R)};return e.set(A.id,B),S.id!==x.id&&(t.delete(S.id),t.set(x.id,B.id)),{oldValue:A,newValue:B,bodiesUpdated:[{oldBody:S,newBody:x}]}}else console.warn(`Attempt to add body to missing annotation ${f.annotation}`)},w=(f,x,A=C.LOCAL)=>{const S=p(f,x);s(A,{updated:[S]})},T=(f,x=C.LOCAL)=>{const A=f.map(S=>p({id:S.id,annotation:S.annotation},S));s(x,{updated:A})},M=f=>{const x=e.get(f.annotation);if(x){const A={...x,target:{...x.target,...f}};return e.set(x.id,A),{oldValue:x,newValue:A,targetUpdated:{oldTarget:x.target,newTarget:f}}}else console.warn(`Attempt to update target on missing annotation: ${f.annotation}`)};return{addAnnotation:a,addBody:b,all:h,bulkAddAnnotation:m,bulkDeleteAnnotation:y,bulkUpdateAnnotation:u,bulkUpdateBodies:T,bulkUpdateTargets:(f,x=C.LOCAL)=>{const A=f.map(M).filter(S=>S);A.length>0&&s(x,{updated:A})},clear:E,deleteAnnotation:L,deleteBody:l,getAnnotation:c,getBody:g,observe:o,unobserve:i,updateAnnotation:d,updateBody:w,updateTarget:(f,x=C.LOCAL)=>{const A=M(f);A&&s(x,{updated:[A]})}}};let Bt=()=>({emit(e,...t){let n=this.events[e]||[];for(let o=0,i=n.length;o<i;o++)n[o](...t)},events:{},on(e,t){var n;return(n=this.events[e])!=null&&n.push(t)||(this.events[e]=[t]),()=>{var o;this.events[e]=(o=this.events[e])==null?void 0:o.filter(i=>t!==i)}}});const Lt=250,Mt=e=>{const t=Bt(),n=[];let o=-1,i=!1,s=0;const a=m=>{if(!i){const{changes:v}=m,L=performance.now();if(L-s>Lt)n.splice(o+1),n.push(v),o=n.length-1;else{const y=n.length-1;n[y]=Et(n[y],v)}s=L}i=!1};e.observe(a,{origin:C.LOCAL});const r=m=>(m==null?void 0:m.length)>0&&e.bulkDeleteAnnotation(m),d=m=>(m==null?void 0:m.length)>0&&e.bulkAddAnnotation(m,!1),u=m=>(m==null?void 0:m.length)>0&&e.bulkUpdateAnnotation(m.map(({oldValue:v})=>v)),b=m=>(m==null?void 0:m.length)>0&&e.bulkUpdateAnnotation(m.map(({newValue:v})=>v)),h=m=>(m==null?void 0:m.length)>0&&e.bulkAddAnnotation(m,!1),E=m=>(m==null?void 0:m.length)>0&&e.bulkDeleteAnnotation(m);return{canRedo:()=>n.length-1>o,canUndo:()=>o>-1,destroy:()=>e.unobserve(a),on:(m,v)=>t.on(m,v),redo:()=>{if(n.length-1>o){i=!0;const{created:m,updated:v,deleted:L}=n[o+1];d(m),b(v),E(L),t.emit("redo",n[o+1]),o+=1}},undo:()=>{if(o>-1){i=!0;const{created:m,updated:v,deleted:L}=n[o];r(m),u(v),h(L),t.emit("undo",n[o]),o-=1}}}},Ct=()=>{const{subscribe:e,set:t}=K([]);return{subscribe:e,set:t}},Rt=(e,t,n,o)=>{const{store:i,selection:s,hover:a,viewport:r}=e,d=new Map;let u=[],b,h;const E=(l,c)=>{d.has(l)?d.get(l).push(c):d.set(l,[c])},m=(l,c)=>{const g=d.get(l);g&&g.indexOf(c)>0&&g.splice(g.indexOf(c),1)},v=(l,c,g)=>{d.has(l)&&setTimeout(()=>{d.get(l).forEach(p=>{if(n){const w=Array.isArray(c)?c.map(M=>n.serialize(M)):n.serialize(c),T=g?g instanceof PointerEvent?g:n.serialize(g):void 0;p(w,T)}else p(c,g)})},1)},L=()=>{const{selected:l}=s,c=l.map(({id:g})=>i.getAnnotation(g));c.forEach(g=>{const p=u.find(w=>w.id===g.id);(!p||!D(p,g))&&v("updateAnnotation",g,p)}),u=u.map(g=>c.find(({id:w})=>w===g.id)||g)};s.subscribe(({selected:l})=>{if(!(u.length===0&&l.length===0)){if(u.length===0&&l.length>0)u=l.map(({id:c})=>i.getAnnotation(c));else if(u.length>0&&l.length===0)u.forEach(c=>{const g=i.getAnnotation(c.id);g&&!D(g,c)&&v("updateAnnotation",g,c)}),u=[];else{const c=new Set(u.map(p=>p.id)),g=new Set(l.map(({id:p})=>p));u.filter(p=>!g.has(p.id)).forEach(p=>{const w=i.getAnnotation(p.id);w&&!D(w,p)&&v("updateAnnotation",w,p)}),u=[...u.filter(p=>g.has(p.id)),...l.filter(({id:p})=>!c.has(p)).map(({id:p})=>i.getAnnotation(p))]}v("selectionChanged",u)}}),a.subscribe(l=>{!b&&l?v("mouseEnterAnnotation",i.getAnnotation(l)):b&&!l?v("mouseLeaveAnnotation",i.getAnnotation(b)):b&&l&&(v("mouseLeaveAnnotation",i.getAnnotation(b)),v("mouseEnterAnnotation",i.getAnnotation(l))),b=l}),r==null||r.subscribe(l=>v("viewportIntersect",l.map(i.getAnnotation))),i.observe(l=>{o&&(h&&clearTimeout(h),h=setTimeout(L,1e3));const{created:c,deleted:g}=l.changes;c.forEach(p=>v("createAnnotation",p)),g.forEach(p=>v("deleteAnnotation",p)),l.changes.updated.filter(p=>[...p.bodiesCreated||[],...p.bodiesDeleted||[],...p.bodiesUpdated||[]].length>0).forEach(({oldValue:p,newValue:w})=>{const T=u.find(M=>M.id===p.id)||p;u=u.map(M=>M.id===p.id?w:M),v("updateAnnotation",w,T)})},{origin:C.LOCAL}),i.observe(l=>{if(u){const c=new Set(u.map(p=>p.id)),g=l.changes.updated.filter(({newValue:p})=>c.has(p.id)).map(({newValue:p})=>p);g.length>0&&(u=u.map(p=>g.find(T=>T.id===p.id)||p))}},{origin:C.REMOTE});const y=l=>c=>{const{created:g,deleted:p,updated:w}=c;g.forEach(T=>v("createAnnotation",T)),p.forEach(T=>v("deleteAnnotation",T)),l?w.forEach(T=>v("updateAnnotation",T.oldValue,T.newValue)):w.forEach(T=>v("updateAnnotation",T.newValue,T.oldValue))};return t.on("undo",y(!0)),t.on("redo",y(!1)),{on:E,off:m,emit:v}},Ot=e=>t=>t.reduce((n,o)=>{const{parsed:i,error:s}=e.parse(o);return s?{parsed:n.parsed,failed:[...n.failed,o]}:{parsed:[...n.parsed,i],failed:n.failed}},{parsed:[],failed:[]}),Xt=(e,t,n)=>{const{store:o,selection:i}=e,s=y=>{if(n){const{parsed:l,error:c}=n.parse(y);l?o.addAnnotation(l,C.REMOTE):console.error(c)}else o.addAnnotation(y,C.REMOTE)},a=()=>i.clear(),r=()=>o.clear(),d=y=>{const l=o.getAnnotation(y);return n&&l?n.serialize(l):l},u=()=>n?o.all().map(n.serialize):o.all(),b=()=>{var y;const l=(((y=i.selected)==null?void 0:y.map(c=>c.id))||[]).map(c=>o.getAnnotation(c));return n?l.map(n.serialize):l},h=y=>fetch(y).then(l=>l.json()).then(l=>(m(l),l)),E=y=>{if(typeof y=="string"){const l=o.getAnnotation(y);return o.deleteAnnotation(y),n?n.serialize(l):l}else{const l=n?n.parse(y).parsed:y;return o.deleteAnnotation(l),y}},m=y=>{if(n){const{parsed:l,failed:c}=Ot(n)(y);c.length>0&&console.warn(`Discarded ${c.length} invalid annotations`,c),o.bulkAddAnnotation(l,!0,C.REMOTE)}else o.bulkAddAnnotation(y,!0,C.REMOTE)},v=y=>{y?i.setSelected(y):i.clear()},L=y=>{if(n){const l=n.parse(y).parsed,c=n.serialize(o.getAnnotation(l.id));return o.updateAnnotation(l),c}else{const l=o.getAnnotation(y.id);return o.updateAnnotation(y),l}};return{addAnnotation:s,cancelSelected:a,canRedo:t.canRedo,canUndo:t.canUndo,clearAnnotations:r,getAnnotationById:d,getAnnotations:u,getSelected:b,loadAnnotations:h,redo:t.redo,removeAnnotation:E,setAnnotations:m,setSelected:v,undo:t.undo,updateAnnotation:L}};let Ut=e=>crypto.getRandomValues(new Uint8Array(e)),Yt=(e,t,n)=>{let o=(2<<Math.log(e.length-1)/Math.LN2)-1,i=-~(1.6*o*t/e.length);return(s=t)=>{let a="";for(;;){let r=n(i),d=i;for(;d--;)if(a+=e[r[d]&o]||"",a.length===s)return a}}},Dt=(e,t=21)=>Yt(e,t,Ut),It=(e=21)=>crypto.getRandomValues(new Uint8Array(e)).reduce((t,n)=>(n&=63,n<36?t+=n.toString(36):n<62?t+=(n-26).toString(36).toUpperCase():n>62?t+="-":t+="_",t),"");const Vt=()=>({isGuest:!0,id:Dt("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",20)()});It();function _t(e,t,n,o,i){st(e,t,n||0,o||e.length-1,i||kt)}function st(e,t,n,o,i){for(;o>n;){if(o-n>600){var s=o-n+1,a=t-n+1,r=Math.log(s),d=.5*Math.exp(2*r/3),u=.5*Math.sqrt(r*d*(s-d)/s)*(a-s/2<0?-1:1),b=Math.max(n,Math.floor(t-a*d/s+u)),h=Math.min(o,Math.floor(t+(s-a)*d/s+u));st(e,t,b,h,i)}var E=e[t],m=n,v=o;for(P(e,n,t),i(e[o],E)>0&&P(e,n,o);m<v;){for(P(e,m,v),m++,v--;i(e[m],E)<0;)m++;for(;i(e[v],E)>0;)v--}i(e[n],E)===0?P(e,n,v):(v++,P(e,v,o)),v<=t&&(n=v+1),t<=v&&(o=v-1)}}function P(e,t,n){var o=e[t];e[t]=e[n],e[n]=o}function kt(e,t){return e<t?-1:e>t?1:0}class Nt{constructor(t=9){this._maxEntries=Math.max(4,t),this._minEntries=Math.max(2,Math.ceil(this._maxEntries*.4)),this.clear()}all(){return this._all(this.data,[])}search(t){let n=this.data;const o=[];if(!$(t,n))return o;const i=this.toBBox,s=[];for(;n;){for(let a=0;a<n.children.length;a++){const r=n.children[a],d=n.leaf?i(r):r;$(t,d)&&(n.leaf?o.push(r):Q(t,d)?this._all(r,o):s.push(r))}n=s.pop()}return o}collides(t){let n=this.data;if(!$(t,n))return!1;const o=[];for(;n;){for(let i=0;i<n.children.length;i++){const s=n.children[i],a=n.leaf?this.toBBox(s):s;if($(t,a)){if(n.leaf||Q(t,a))return!0;o.push(s)}}n=o.pop()}return!1}load(t){if(!(t&&t.length))return this;if(t.length<this._minEntries){for(let o=0;o<t.length;o++)this.insert(t[o]);return this}let n=this._build(t.slice(),0,t.length-1,0);if(!this.data.children.length)this.data=n;else if(this.data.height===n.height)this._splitRoot(this.data,n);else{if(this.data.height<n.height){const o=this.data;this.data=n,n=o}this._insert(n,this.data.height-n.height-1,!0)}return this}insert(t){return t&&this._insert(t,this.data.height-1),this}clear(){return this.data=k([]),this}remove(t,n){if(!t)return this;let o=this.data;const i=this.toBBox(t),s=[],a=[];let r,d,u;for(;o||s.length;){if(o||(o=s.pop(),d=s[s.length-1],r=a.pop(),u=!0),o.leaf){const b=Pt(t,o.children,n);if(b!==-1)return o.children.splice(b,1),s.push(o),this._condense(s),this}!u&&!o.leaf&&Q(o,i)?(s.push(o),a.push(r),r=0,d=o,o=o.children[0]):d?(r++,o=d.children[r],u=!1):o=null}return this}toBBox(t){return t}compareMinX(t,n){return t.minX-n.minX}compareMinY(t,n){return t.minY-n.minY}toJSON(){return this.data}fromJSON(t){return this.data=t,this}_all(t,n){const o=[];for(;t;)t.leaf?n.push(...t.children):o.push(...t.children),t=o.pop();return n}_build(t,n,o,i){const s=o-n+1;let a=this._maxEntries,r;if(s<=a)return r=k(t.slice(n,o+1)),_(r,this.toBBox),r;i||(i=Math.ceil(Math.log(s)/Math.log(a)),a=Math.ceil(s/Math.pow(a,i-1))),r=k([]),r.leaf=!1,r.height=i;const d=Math.ceil(s/a),u=d*Math.ceil(Math.sqrt(a));at(t,n,o,u,this.compareMinX);for(let b=n;b<=o;b+=u){const h=Math.min(b+u-1,o);at(t,b,h,d,this.compareMinY);for(let E=b;E<=h;E+=d){const m=Math.min(E+d-1,h);r.children.push(this._build(t,E,m,i-1))}}return _(r,this.toBBox),r}_chooseSubtree(t,n,o,i){for(;i.push(n),!(n.leaf||i.length-1===o);){let s=1/0,a=1/0,r;for(let d=0;d<n.children.length;d++){const u=n.children[d],b=Z(u),h=Ft(t,u)-b;h<a?(a=h,s=b<s?b:s,r=u):h===a&&b<s&&(s=b,r=u)}n=r||n.children[0]}return n}_insert(t,n,o){const i=o?t:this.toBBox(t),s=[],a=this._chooseSubtree(i,this.data,n,s);for(a.children.push(t),z(a,i);n>=0&&s[n].children.length>this._maxEntries;)this._split(s,n),n--;this._adjustParentBBoxes(i,s,n)}_split(t,n){const o=t[n],i=o.children.length,s=this._minEntries;this._chooseSplitAxis(o,s,i);const a=this._chooseSplitIndex(o,s,i),r=k(o.children.splice(a,o.children.length-a));r.height=o.height,r.leaf=o.leaf,_(o,this.toBBox),_(r,this.toBBox),n?t[n-1].children.push(r):this._splitRoot(o,r)}_splitRoot(t,n){this.data=k([t,n]),this.data.height=t.height+1,this.data.leaf=!1,_(this.data,this.toBBox)}_chooseSplitIndex(t,n,o){let i,s=1/0,a=1/0;for(let r=n;r<=o-n;r++){const d=H(t,0,r,this.toBBox),u=H(t,r,o,this.toBBox),b=$t(d,u),h=Z(d)+Z(u);b<s?(s=b,i=r,a=h<a?h:a):b===s&&h<a&&(a=h,i=r)}return i||o-n}_chooseSplitAxis(t,n,o){const i=t.leaf?this.compareMinX:Ht,s=t.leaf?this.compareMinY:zt,a=this._allDistMargin(t,n,o,i),r=this._allDistMargin(t,n,o,s);a<r&&t.children.sort(i)}_allDistMargin(t,n,o,i){t.children.sort(i);const s=this.toBBox,a=H(t,0,n,s),r=H(t,o-n,o,s);let d=F(a)+F(r);for(let u=n;u<o-n;u++){const b=t.children[u];z(a,t.leaf?s(b):b),d+=F(a)}for(let u=o-n-1;u>=n;u--){const b=t.children[u];z(r,t.leaf?s(b):b),d+=F(r)}return d}_adjustParentBBoxes(t,n,o){for(let i=o;i>=0;i--)z(n[i],t)}_condense(t){for(let n=t.length-1,o;n>=0;n--)t[n].children.length===0?n>0?(o=t[n-1].children,o.splice(o.indexOf(t[n]),1)):this.clear():_(t[n],this.toBBox)}}function Pt(e,t,n){if(!n)return t.indexOf(e);for(let o=0;o<t.length;o++)if(n(e,t[o]))return o;return-1}function _(e,t){H(e,0,e.children.length,t,e)}function H(e,t,n,o,i){i||(i=k(null)),i.minX=1/0,i.minY=1/0,i.maxX=-1/0,i.maxY=-1/0;for(let s=t;s<n;s++){const a=e.children[s];z(i,e.leaf?o(a):a)}return i}function z(e,t){return e.minX=Math.min(e.minX,t.minX),e.minY=Math.min(e.minY,t.minY),e.maxX=Math.max(e.maxX,t.maxX),e.maxY=Math.max(e.maxY,t.maxY),e}function Ht(e,t){return e.minX-t.minX}function zt(e,t){return e.minY-t.minY}function Z(e){return(e.maxX-e.minX)*(e.maxY-e.minY)}function F(e){return e.maxX-e.minX+(e.maxY-e.minY)}function Ft(e,t){return(Math.max(t.maxX,e.maxX)-Math.min(t.minX,e.minX))*(Math.max(t.maxY,e.maxY)-Math.min(t.minY,e.minY))}function $t(e,t){const n=Math.max(e.minX,t.minX),o=Math.max(e.minY,t.minY),i=Math.min(e.maxX,t.maxX),s=Math.min(e.maxY,t.maxY);return Math.max(0,i-n)*Math.max(0,s-o)}function Q(e,t){return e.minX<=t.minX&&e.minY<=t.minY&&t.maxX<=e.maxX&&t.maxY<=e.maxY}function $(e,t){return t.minX<=e.maxX&&t.minY<=e.maxY&&t.maxX>=e.minX&&t.maxY>=e.minY}function k(e){return{children:e,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function at(e,t,n,o,i){const s=[t,n];for(;s.length;){if(n=s.pop(),t=s.pop(),n-t<=o)continue;const a=t+Math.ceil((n-t)/o/2)*o;_t(e,a,t,n,i),s.push(t,a,a,n)}}const Wt=(e,t)=>{const n=s=>Math.round(s*10)/10,o={top:n(e.top),bottom:n(e.bottom),left:n(e.left),right:n(e.right)},i={top:n(t.top),bottom:n(t.bottom),left:n(t.left),right:n(t.right)};if(Math.abs(o.top-i.top)<.5&&Math.abs(o.bottom-i.bottom)<.5){if(Math.abs(o.left-i.right)<.5||Math.abs(o.right-i.left)<.5)return"inline-adjacent";if(o.left>=i.left&&o.right<=i.right)return"inline-is-contained";if(o.left<=i.left&&o.right>=i.right)return"inline-contains"}else if(o.top<=i.top&&o.bottom>=i.bottom){if(o.left<=i.left&&o.right>=i.right)return"block-contains"}else if(o.top>=i.top&&o.bottom<=i.bottom&&o.left>=i.left&&o.right<=i.right)return"block-is-contained"},qt=(e,t)=>{const n=Math.min(e.left,t.left),o=Math.max(e.right,t.right),i=Math.min(e.top,t.top),s=Math.max(e.bottom,t.bottom);return new DOMRect(n,i,o-n,s-i)},jt=e=>e.reduce((t,n)=>{if(n.width===0||n.height===0)return t;let o=[...t],i=!1;for(const s of t){const a=Wt(n,s);if(a==="inline-adjacent"){o=o.map(r=>r===s?qt(n,s):r),i=!0;break}else if(a==="inline-contains"){o=o.map(r=>r===s?n:r),i=!0;break}else if(a==="inline-is-contained"){i=!0;break}else if(a==="block-contains"||a==="block-is-contained"){n.width<s.width&&(o=o.map(r=>r===s?n:r)),i=!0;break}}return i?o:[...o,n]},[]),Jt=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},Kt=(e,t)=>{const n=new Nt,o=new Map,i=l=>{const c=t.getBoundingClientRect(),p=l.selector.range instanceof Range&&!l.selector.range.collapsed&&l.selector.range.startContainer.nodeType===Node.TEXT_NODE&&l.selector.range.endContainer.nodeType===Node.TEXT_NODE?l:I(l,t),w=Array.from(p.selector.range.getClientRects()),T=jt(w);return T.map(M=>{const{x:f,y:x,width:A,height:S}=M;return{minX:f-c.x,minY:x-c.y,maxX:f-c.x+A,maxY:x-c.y+S,annotation:{id:l.annotation,rects:T}}})},s=()=>[...o.values()],a=()=>{n.clear(),o.clear()},r=l=>{const c=i(l);c.forEach(g=>n.insert(g)),o.set(l.annotation,c)},d=l=>{o.get(l.annotation).forEach(g=>n.remove(g)),o.delete(l.annotation)},u=l=>{d(l),r(l)},b=(l,c=!0)=>{c&&a();const g=l.map(w=>({target:w,rects:i(w)}));g.forEach(({target:w,rects:T})=>o.set(w.annotation,T));const p=g.reduce((w,{rects:T})=>[...w,...T],[]);n.load(p)},h=(l,c)=>{const g=n.search({minX:l,minY:c,maxX:l,maxY:c}),p=w=>w.annotation.rects.reduce((T,M)=>T+M.width*M.height,0);if(g.length>0)return g.sort((w,T)=>p(w)-p(T)),g[0].annotation.id},E=l=>{const c=m(l);if(c.length===0)return;let g=c[0].left,p=c[0].top,w=c[0].right,T=c[0].bottom;for(let M=1;M<c.length;M++){const f=c[M];g=Math.min(g,f.left),p=Math.min(p,f.top),w=Math.max(w,f.right),T=Math.max(T,f.bottom)}return new DOMRect(g,p,w-g,T-p)},m=l=>{const c=o.get(l);return c?c[0].annotation.rects:[]};return{all:s,clear:a,getAt:h,getBoundsForAnnotation:E,getDOMRectsForAnnotation:m,getIntersectingRects:(l,c,g,p)=>n.search({minX:l,minY:c,maxX:g,maxY:p}),insert:r,recalculate:()=>b(e.all().map(l=>l.target),!0),remove:d,set:b,size:()=>n.all().length,update:u}},W=e=>{const{range:t}=e.target.selector;return t instanceof Range&&!t.collapsed},rt=(e,t)=>{const n=St(),o=Kt(n,e),i=pt(n,t),s=gt(n),a=Ct(),r=(y,l=C.LOCAL)=>{const c=y.target.selector.range instanceof Range?y:{...y,target:I(y.target,e)},{range:g}=c.target.selector,p=g&&!g.collapsed;return p&&n.addAnnotation(c,l),p},d=(y,l=!0,c=C.LOCAL)=>{const g=y.map(w=>W(w)?w:{...w,target:I(w.target,e)});if(g.some(w=>w.target.selector.range.collapsed)){const w=g.filter(T=>T.target.selector.range.collapsed);return console.warn("Could not revive all targets"),console.warn(w),n.bulkAddAnnotation(g,l,c),w}else return n.bulkAddAnnotation(g,l,c),[]},u=(y,l=C.LOCAL)=>{const c=y.selector.range instanceof Range?y:I(y,e);n.updateTarget(c,l)},b=(y,l=C.LOCAL)=>{const c=y.map(g=>g.selector.range instanceof Range?g:I(g,e));n.bulkUpdateTargets(c,l)},h=(y,l)=>{const c=o.getAt(y,l);return c?n.getAnnotation(c):void 0},E=(y,l,c,g)=>{const p=o.getIntersectingRects(y,l,c,g);return Array.from(new Set(p.map(T=>T.annotation.id))).map(T=>n.getAnnotation(T)).filter(T=>T)},m=(y,l,c,g=5)=>{const p=o.getDOMRectsForAnnotation(y);if(p.length>0){if(l&&c){const w=p.find(({top:T,right:M,bottom:f,left:x})=>l>=x-g&&l<=M+g&&c>=T-g&&c<=f+g);if(w)return w}return o.getBoundsForAnnotation(y)}},v=(y,l,c,g)=>{const w=o.getIntersectingRects(y,l,c,g).reduce((T,M)=>((T[M.annotation.id]=T[M.annotation.id]||[]).push(M),T),{});return Object.entries(w).map(([T,M])=>({annotation:n.getAnnotation(T),rects:M.map(({minX:f,minY:x,maxX:A,maxY:S})=>({x:f,y:x,width:A-f,height:S-x}))}))},L=()=>o.recalculate();return n.observe(({changes:y})=>{const l=(y.created||[]).filter(W),c=(y.deleted||[]).filter(W),g=(y.updated||[]).filter(p=>W(p.newValue));l.length>0&&o.set(l.map(p=>p.target),!1),(c==null?void 0:c.length)>0&&c.forEach(p=>o.remove(p.target)),(g==null?void 0:g.length)>0&&g.forEach(({newValue:p})=>o.update(p.target))}),{store:{...n,addAnnotation:r,bulkAddAnnotation:d,bulkUpdateTargets:b,getAnnotationBounds:m,getAt:h,getIntersecting:E,getIntersectingRects:v,recalculatePositions:L,updateTarget:u},selection:i,hover:s,viewport:a}},Gt=(e,t={})=>{const n=new Map,o=s=>Array.from(n.entries()).filter(([a,r])=>r.presenceKey===s.presenceKey).map(([a,r])=>a);return e.on("selectionChange",(s,a)=>{o(s).forEach(d=>n.delete(d)),a&&a.forEach(d=>n.set(d,s))}),{paint:(s,a,r,d,u,b)=>{t.font&&(d.font=t.font);const h=n.get(s.id);if(h){const{x:E,y:m,height:v}=a[0];d.fillStyle=h.appearance.color,d.fillRect(E-2,m-2.5,2,v+5);const L=d.measureText(h.appearance.label),y=L.width+6,l=L.actualBoundingBoxAscent+L.actualBoundingBoxDescent+8,c=L.fontBoundingBoxAscent?8:6.5;d.fillRect(E-2,m-2.5-l,y,l),d.fillStyle="#fff",d.fillText(h.appearance.label,E+1,m-c),r.fillStyle=h.appearance.color,r.globalAlpha=u?.45:.18,a.forEach(({x:g,y:p,width:w,height:T})=>r.fillRect(g,p-2.5,w,T+5))}else j.paint(s,a,r,d,u,b)}}},lt=e=>e===null?null:e.scrollHeight>e.clientHeight?e:lt(e.parentElement),Zt=(e,t)=>n=>{const o=lt(e);if(o){const i=t.getAnnotation(n.id),{range:s}=i.target.selector;if(!s||s.collapsed)return!1;const a=o.getBoundingClientRect(),r=o.clientHeight,d=o.clientWidth,u=i.target.selector.range.getBoundingClientRect(),{width:b,height:h}=t.getAnnotationBounds(n.id),E=u.top-a.top,m=u.left-a.left,v=o.parentElement?o.scrollTop:0,L=o.parentElement?o.scrollLeft:0,y=E+v-(r-h)/2,l=m+L-(d-b)/2;return o.scroll({top:y,left:l,behavior:"smooth"}),!0}};let q;const Qt=new Uint8Array(16);function te(){if(!q&&(q=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!q))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return q(Qt)}const O=[];for(let e=0;e<256;++e)O.push((e+256).toString(16).slice(1));function ee(e,t=0){return O[e[t+0]]+O[e[t+1]]+O[e[t+2]]+O[e[t+3]]+"-"+O[e[t+4]]+O[e[t+5]]+"-"+O[e[t+6]]+O[e[t+7]]+"-"+O[e[t+8]]+O[e[t+9]]+"-"+O[e[t+10]]+O[e[t+11]]+O[e[t+12]]+O[e[t+13]]+O[e[t+14]]+O[e[t+15]]}const ct={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function ne(e,t,n){if(ct.randomUUID&&!t&&!e)return ct.randomUUID();e=e||{};const o=e.random||(e.rng||te)();if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,t){n=n||0;for(let i=0;i<16;++i)t[n+i]=o[i];return t}return ee(o)}const oe=(e,t,n)=>{const o=document.createRange(),i=n?e.startContainer.parentElement.closest(n):t;o.setStart(i,0),o.setEnd(e.startContainer,e.startOffset);const s=e.toString(),a=o.toString().length,r=a+s.length;return n?{quote:s,start:a,end:r,range:e,offsetReference:i}:{quote:s,start:a,end:r,range:e}},ie=(e,t,n)=>{const{store:o,selection:i}=t;let s,a=null;const r=E=>s=E;let d=!1,u;e.addEventListener("selectstart",E=>{if(!d)return;!E.target.parentElement.closest(".not-annotatable")?a={annotation:ne(),selector:void 0,creator:s,created:new Date}:(a=null,u=void 0)});let b;document.addEventListener("selectionchange",E=>{b&&clearTimeout(b),b=setTimeout(()=>h(),50)});const h=()=>{var m,v;const E=document.getSelection();if(!E.isCollapsed&&d&&a){const L=Array.from(Array(E.rangeCount).keys()).map(c=>E.getRangeAt(c));Jt(L[0]).toString()!==((v=(m=a.selector)==null?void 0:m.range)==null?void 0:v.toString())&&(a={...a,selector:oe(L[0],e,n)},o.getAnnotation(a.annotation)?o.updateTarget(a,C.LOCAL):(o.addAnnotation({id:a.annotation,bodies:[],target:a}),i.clickSelect(a.annotation,u)))}};return e.addEventListener("pointerdown",E=>{u=E,d=E.button===0}),document.addEventListener("pointerup",E=>{var v;u=E,!((v=E.target.parentElement)!=null&&v.closest(".not-annotatable")||!d)&&setTimeout(()=>{if(a!=null&&a.selector)o.updateTarget(a,C.LOCAL),i.clickSelect(a.annotation,E),a=null,u=void 0;else{const{x:L,y}=e.getBoundingClientRect(),l=o.getAt(E.clientX-L,E.clientY-y);if(l){const{selected:c}=i;(c.length!==1||c[0].id!==l.id)&&(i.clickSelect(l.id,E),u=void 0)}else i.isEmpty()||i.clear()}},50)}),{setUser:r}},pe="",se=(e,t={})=>{const n=rt(e,t.pointerAction),{selection:o,viewport:i}=n,s=n.store,a=Mt(s),r=Rt(n,a,t.adapter);let d=Vt();const u=nt(e,n,i);t.style&&u.setDrawingStyle(t.style);const b=ie(e,n,t.offsetReferenceSelector);return b.setUser(d),{...Xt(n,a,t.adapter),destroy:()=>{throw"Not implemented yet"},element:e,getUser:()=>d,setFilter:g=>u.setFilter(g),setStyle:g=>u.setDrawingStyle(g),setUser:g=>{d=g,b.setUser(g)},setSelected:g=>{g?o.setSelected(g):o.clear()},setPresenceProvider:g=>{g&&(u.setPainter(Gt(g,t.presence)),g.on("selectionChange",()=>u.redraw()))},on:r.on,off:r.off,scrollIntoView:Zt(e,s),state:n}};X.Origin=C,X.createHighlightLayer=nt,X.createTextAnnotator=se,X.createTextAnnotatorState=rt,X.defaultPainter=j,X.reviveTarget=I,Object.defineProperty(X,Symbol.toStringTag,{value:"Module"})});
|
|
2
2
|
//# sourceMappingURL=text-annotator.umd.js.map
|