@recogito/text-annotator 3.0.0-rc.2 → 3.0.0-rc.3
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(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(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"})});
|
|
2
2
|
//# sourceMappingURL=text-annotator.umd.js.map
|