@soomo/text-annotator 3.2.0-staging.8 → 3.2.0-staging.9

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,3 +1,3 @@
1
- (function(h,U){typeof exports=="object"&&typeof module<"u"?U(exports,require("debounce"),require("colord"),require("dequal/lite"),require("uuid"),require("@annotorious/core"),require("rbush"),require("nanoevents"),require("hotkeys-js"),require("poll")):typeof define=="function"&&define.amd?define(["exports","debounce","colord","dequal/lite","uuid","@annotorious/core","rbush","nanoevents","hotkeys-js","poll"],U):(h=typeof globalThis<"u"?globalThis:h||self,U(h.RecogitoJS={},h.debounce,h.Colord,h.DequalLite,h.UUID,h.AnnotoriousCore,h.RBush,h.nanoevents,h.HotkeysJs,h.poll))})(this,function(h,U,nt,Pt,ot,N,_t,Dt,z,Ut){"use strict";const J="not-annotatable",F=`.${J}`,W=t=>{var e;return!!(t instanceof HTMLElement?t.closest(F):(e=t.parentElement)==null?void 0:e.closest(F))},st=t=>{const n=t.commonAncestorContainer;return!W(n)},rt=t=>t.addEventListener("click",n=>{!n.target.closest(F)&&!n.target.closest("a")&&n.preventDefault()}),it=typeof navigator<"u"&&/mac/i.test(navigator.userAgentData?navigator.userAgentData.platform:navigator.platform),at=t=>{!t.hasAttribute("tabindex")&&t.tabIndex<0&&t.setAttribute("tabindex","-1"),t.classList.add("no-focus-outline")},Ft=function*(t){const n=document.createNodeIterator(t.commonAncestorContainer,NodeFilter.SHOW_ELEMENT,o=>o instanceof HTMLElement&&o.classList.contains(J)&&!o.parentElement.closest(F)&&t.intersectsNode(o)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP);let e;for(;e=n.nextNode();)e instanceof HTMLElement&&(yield e)},ct=t=>{if(!st(t))return[];const n=[];let e=null;for(const o of Ft(t)){let s;e?(s=document.createRange(),s.setStartAfter(e),s.setEndBefore(o)):(s=t.cloneRange(),s.setEndBefore(o)),s.collapsed||n.push(s),e=o}if(e){const o=t.cloneRange();o.setStartAfter(e),o.collapsed||n.push(o)}return n.length>0?n:[t]},X=t=>{const n=t.cloneContents();return n.querySelectorAll(F).forEach(e=>e.remove()),n},lt=(t,n,e=10,o)=>{const s=o?t.startContainer.parentElement.closest(o):n,i=document.createRange();i.setStart(s,0),i.setEnd(t.startContainer,t.startOffset);const a=X(i).textContent,c=document.createRange();c.setStart(t.endContainer,t.endOffset),s===document.body?c.setEnd(s,s.childNodes.length):c.setEndAfter(s);const u=X(c).textContent;return{prefix:a.substring(a.length-e),suffix:u.substring(0,e)}},M=t=>t.every(n=>n.range instanceof Range&&!n.range.collapsed),dt=/^\s*$/,ut=t=>dt.test(t.toString()),Kt=(t,n)=>{const e=i=>Math.round(i*10)/10,o={top:e(t.top),bottom:e(t.bottom),left:e(t.left),right:e(t.right)},s={top:e(n.top),bottom:e(n.bottom),left:e(n.left),right:e(n.right)};if(Math.abs(o.top-s.top)<.5&&Math.abs(o.bottom-s.bottom)<.5){if(Math.abs(o.left-s.right)<.5||Math.abs(o.right-s.left)<.5)return"inline-adjacent";if(o.left>=s.left&&o.right<=s.right)return"inline-is-contained";if(o.left<=s.left&&o.right>=s.right)return"inline-contains"}else if(o.top<=s.top&&o.bottom>=s.bottom){if(o.left<=s.left&&o.right>=s.right)return"block-contains"}else if(o.top>=s.top&&o.bottom<=s.bottom&&o.left>=s.left&&o.right<=s.right)return"block-is-contained"},Wt=(t,n)=>{const e=Math.min(t.left,n.left),o=Math.max(t.right,n.right),s=Math.min(t.top,n.top),i=Math.max(t.bottom,n.bottom);return new DOMRect(e,s,o-e,i-s)},ft=t=>t.reduce((n,e)=>{if(e.width===0||e.height===0)return n;let o=[...n],s=!1;for(const i of n){const a=Kt(e,i);if(a==="inline-adjacent"){o=o.map(c=>c===i?Wt(e,i):c),s=!0;break}else if(a==="inline-contains"){o=o.map(c=>c===i?e:c),s=!0;break}else if(a==="inline-is-contained"){s=!0;break}else if(a==="block-contains"||a==="block-is-contained"){e.width<i.width&&(o=o.map(c=>c===i?e:c)),s=!0;break}}return s?o:[...o,e]},[]),$t=t=>({length:t.length,item:n=>t[n],[Symbol.iterator]:function*(){for(let n=0;n<this.length;n++)yield this.item(n)}}),gt=(t,n,e)=>{const o=document.createRange(),s=e?t.startContainer.parentElement.closest(e):n;o.setStart(s,0),o.setEnd(t.startContainer,t.startOffset);const i=X(o).textContent,a=t.toString(),c=i.length||0,u=c+a.length;return e?{quote:a,start:c,end:u,range:t,offsetReference:s}:{quote:a,start:c,end:u,range:t}},Z=(t,n)=>{var b,g;const{start:e,end:o}=t,s=t.offsetReference||n,i=document.createNodeIterator(n,NodeFilter.SHOW_TEXT,r=>{var v;return(v=r.parentElement)!=null&&v.closest(F)?NodeFilter.FILTER_SKIP:NodeFilter.FILTER_ACCEPT});let a=0;const c=document.createRange();let u=i.nextNode();u===null&&console.error("Could not revive annotation target. Content missing.");let C=!s;for(;u!==null;){if(C||(C=typeof(s==null?void 0:s.contains)=="function"?s.contains(u):!1),C){const r=((b=u.textContent)==null?void 0:b.length)||0;if(a+r>e){c.setStart(u,e-a);break}a+=r}u=i.nextNode()}for(;u!==null;){const r=((g=u.textContent)==null?void 0:g.length)||0;if(a+r>=o){c.setEnd(u,o-a);break}a+=r,u=i.nextNode()}return{...t,range:c}},$=(t,n)=>M(t.selector)?t:{...t,selector:t.selector.map(e=>e.range instanceof Range&&!e.range.collapsed?e:Z(e,n))},j=(t,n)=>M(t.target.selector)?t:{...t,target:$(t.target,n)},mt=(t,n)=>{if(t.isEqualNode(n))return!0;for(let e of t.childNodes)if(mt(e,n))return!0;return!1},ht=(t,n)=>{const e=t.cloneContents();return mt(e,n)},pt=(t,n)=>{const e=t.cloneRange(),o=n.contains(e.startContainer),s=n.contains(e.endContainer);return!o&&!s&&!ht(e,n)?(e.collapse(),e):(o||e.setStart(n,0),s||e.setEnd(n,n.childNodes.length),e)},G=t=>({...t,type:t.type,x:t.x,y:t.y,clientX:t.clientX,clientY:t.clientY,offsetX:t.offsetX,offsetY:t.offsetY,screenX:t.screenX,screenY:t.screenY,isPrimary:t.isPrimary,altKey:t.altKey,ctrlKey:t.ctrlKey,metaKey:t.metaKey,shiftKey:t.shiftKey,button:t.button,buttons:t.buttons,currentTarget:t.currentTarget,target:t.target,defaultPrevented:t.defaultPrevented,detail:t.detail,eventPhase:t.eventPhase,pointerId:t.pointerId,pointerType:t.pointerType,timeStamp:t.timeStamp}),V=t=>({...t,type:t.type,key:t.key,code:t.code,location:t.location,repeat:t.repeat,altKey:t.altKey,ctrlKey:t.ctrlKey,metaKey:t.metaKey,shiftKey:t.shiftKey,currentTarget:t.currentTarget,target:t.target,defaultPrevented:t.defaultPrevented,detail:t.detail,timeStamp:t.timeStamp}),yt=(t,n)=>{const{left:e,top:o,right:s,bottom:i}=t;return new DOMRect(e-n.left,o-n.top,s-e,i-o)},Vt=(t,n)=>{const{left:e,top:o,right:s,bottom:i}=t;return new DOMRect(e+n.left,o+n.top,s-e,i-o)},bt=t=>{if(!t)return document.scrollingElement;const{overflowY:n}=window.getComputedStyle(t);return n!=="visible"&&n!=="hidden"&&t.scrollHeight>t.clientHeight?t:bt(t.parentElement)},St=(t,n,e)=>{const o=e.getBoundingClientRect(),s=e.clientHeight,i=e.clientWidth,a=n.selector[0].range.getBoundingClientRect(),{width:c,height:u}=t.getAnnotationBounds(n.annotation),C=a.top-o.top,b=a.left-o.left,g=e.parentElement?e.scrollTop:0,r=e.parentElement?e.scrollLeft:0,v=C+g-(s-u)/2,E=b+r-(i-c)/2;e.scroll({top:v,left:E,behavior:"smooth"})},Ct=(t,n)=>(e,o)=>{const s=typeof e=="string"?e:e.id,i=o?typeof o=="string"?document.getElementById(o):o:bt(t);if(!i)return console.warn(`The scroll parent is missing for the annotation: ${s}`,{container:t}),!1;const a=n.getAnnotation(s);if(!a)return console.warn(`The annotation is missing in the store: ${s}`),!1;const{range:c}=a.target.selector[0];if(c&&!c.collapsed)return St(n,a.target,i),!0;const u=$(a.target,t),{range:C}=u.selector[0];return C&&!C.collapsed?(St(n,u,i),!0):!1},H={fill:"rgb(0, 128, 255)",fillOpacity:.18},q={fill:"rgb(0, 128, 255)",fillOpacity:.45},Et=(t,n,e,o,s)=>{var a,c;const i=e?typeof e=="function"?e(t.annotation,t.state,s)||((a=t.state)!=null&&a.selected?q:H):e:(c=t.state)!=null&&c.selected?q:H;return o&&o.paint(t,n)||i},qt=t=>{const{top:n,left:e}=t.getBoundingClientRect(),{innerWidth:o,innerHeight:s}=window,i=-e,a=-n,c=o-e,u=s-n;return{top:n,left:e,minX:i,minY:a,maxX:c,maxY:u}},Yt=t=>{let n=new Set;return o=>{const s=o.map(i=>i.id);(n.size!==s.length||s.some(i=>!n.has(i)))&&t.set(s),n=new Set(s)}},tt=(t,n,e,o)=>{const{store:s,selection:i,hover:a}=n;let c,u,C;const b=Yt(e),g=S=>{const{x:T,y:O}=t.getBoundingClientRect(),P=s.getAt(S.clientX-T,S.clientY-O,!1,u);P?a.current!==P.id&&(t.classList.add("hovered"),a.set(P.id)):a.current&&(t.classList.remove("hovered"),a.set(null))};t.addEventListener("pointermove",g);const r=(S=!1)=>{C&&C.clear();const T=qt(t),{minX:O,minY:P,maxX:p,maxY:x}=T,k=u?s.getIntersecting(O,P,p,x).filter(({annotation:B})=>u(B)):s.getIntersecting(O,P,p,x),I=i.selected.map(({id:B})=>B),_=k.map(({annotation:B,rects:Q})=>{const D=I.includes(B.id),K=B.id===a.current;return{annotation:B,rects:Q,state:{selected:D,hovered:K}}});o.redraw(_,T,c,C,S),setTimeout(()=>b(k.map(({annotation:B})=>B)),1)},v=S=>{C=S,r()},E=S=>{c=S,r()},L=S=>{u=S,r(!1)},R=()=>r();s.observe(R);const A=i.subscribe(()=>r()),f=()=>r(!0);document.addEventListener("scroll",f,{capture:!0,passive:!0});const l=U(()=>{s.recalculatePositions(),C==null||C.reset(),r()},10);window.addEventListener("resize",l);const m=new ResizeObserver(l);m.observe(t);const d={attributes:!0,childList:!0,subtree:!0},y=new MutationObserver(S=>{S.every(O=>O.target===t||t.contains(O.target))||r(!0)});return y.observe(document.body,d),{destroy:()=>{t.removeEventListener("pointermove",g),o.destroy(),s.unobserve(R),A(),document.removeEventListener("scroll",f),l.clear(),window.removeEventListener("resize",l),m.disconnect(),y.disconnect()},redraw:r,setStyle:E,setFilter:L,setPainter:v,setVisible:o.setVisible}},zt=()=>{const t=document.createElement("canvas");return t.width=window.innerWidth,t.height=window.innerHeight,t.className="r6o-canvas-highlight-layer bg",t},Xt=(t,n)=>{t.width=window.innerWidth,t.height=window.innerHeight},jt=t=>{t.classList.add("r6o-annotatable");const n=zt(),e=n.getContext("2d");document.body.appendChild(n);const o=(c,u,C,b)=>requestAnimationFrame(()=>{const{width:g,height:r}=n;e.clearRect(-.5,-.5,g+1,r+1),b&&b.clear();const{top:v,left:E}=u;[...c].sort((R,A)=>{const{annotation:{target:{created:f}}}=R,{annotation:{target:{created:l}}}=A;return f.getTime()-l.getTime()}).forEach(R=>{var m;const A=C?typeof C=="function"?C(R.annotation,R.state):C:(m=R.state)!=null&&m.selected?q:H,f=b&&b.paint(R,u)||A,l=R.rects.map(({x:d,y,width:w,height:S})=>({x:d+E,y:y+v,width:w,height:S}));if(e.fillStyle=f.fill,e.globalAlpha=f.fillOpacity||1,l.forEach(({x:d,y,width:w,height:S})=>e.fillRect(d,y,w,S)),f.underlineColor){e.globalAlpha=1,e.strokeStyle=f.underlineColor,e.lineWidth=f.underlineThickness??1;const d=f.underlineOffset??0;l.forEach(({x:y,y:w,width:S,height:T})=>{e.beginPath(),e.moveTo(y,w+T+d),e.lineTo(y+S,w+T+d),e.stroke()})}})}),s=U(()=>Xt(n),10);return window.addEventListener("resize",s),{destroy:()=>{n.remove(),s.clear(),window.removeEventListener("resize",s)},setVisible:c=>{console.log("setVisible not implemented on Canvas renderer")},redraw:o}},At=(t,n,e)=>tt(t,n,e,jt(t)),Gt=t=>[`background-color:${nt.colord((t==null?void 0:t.fill)||H.fill).alpha((t==null?void 0:t.fillOpacity)===void 0?H.fillOpacity:t.fillOpacity).toHex()}`,t!=null&&t.underlineThickness?"text-decoration:underline":void 0,t!=null&&t.underlineColor?`text-decoration-color:${t.underlineColor}`:void 0,t!=null&&t.underlineOffset?`text-underline-offset:${t.underlineOffset}px`:void 0,t!=null&&t.underlineThickness?`text-decoration-thickness:${t.underlineThickness}px`:void 0].filter(Boolean).join(";"),vt=()=>{const t=document.createElement("style");document.getElementsByTagName("head")[0].appendChild(t);let n=new Set;return{destroy:()=>{CSS.highlights.clear(),t.remove()},setVisible:i=>{console.log("setVisible not implemented on CSS Custom Highlights renderer")},redraw:(i,a,c,u)=>{u&&u.clear();const C=new Set(i.map(g=>g.annotation.id));Array.from(n).filter(g=>!C.has(g));const b=i.map(g=>{var E;const r=c?typeof c=="function"?c(g.annotation,g.state):c:(E=g.state)!=null&&E.selected?q:H,v=u&&u.paint(g,a)||r;return`::highlight(_${g.annotation.id}) { ${Gt(v)} }`});t.innerHTML=b.join(`
2
- `),CSS.highlights.clear(),i.forEach(({annotation:g})=>{const r=g.target.selector.map(E=>E.range),v=new Highlight(...r);CSS.highlights.set(`_${g.id}`,v)}),n=C}}},wt=(t,n,e)=>tt(t,n,e,vt()),Qt=(t,n)=>{const e=(i,a)=>i.x<=a.x+a.width&&i.x+i.width>=a.x&&i.y<=a.y+a.height&&i.y+i.height>=a.y,o=i=>i.rects.reduce((a,c)=>a+c.width,0),s=n.filter(({rects:i})=>i.some(a=>e(t,a)));return s.sort((i,a)=>o(a)-o(i)),s.findIndex(i=>i.rects.includes(t))},Jt=t=>{t.classList.add("r6o-annotatable");const n=document.createElement("div");n.className="r6o-span-highlight-layer",t.insertBefore(n,t.firstChild);let e=[];return{destroy:()=>{n.remove()},redraw:(a,c,u,C,b)=>{const r=!(Pt.dequal(e,a)&&b);if(!C&&!r)return;r&&(n.innerHTML=""),[...a].sort((E,L)=>{const{annotation:{target:{created:R}}}=E,{annotation:{target:{created:A}}}=L;return R&&A?R.getTime()-A.getTime():0}).forEach(E=>{E.rects.map(L=>{const R=Qt(L,a),A=Et(E,c,u,C,R);if(r){const f=document.createElement("span");f.className="r6o-annotation",f.dataset.annotation=E.annotation.id,f.style.left=`${L.x}px`,f.style.top=`${L.y}px`,f.style.width=`${L.width}px`,f.style.height=`${L.height}px`,f.style.backgroundColor=nt.colord((A==null?void 0:A.fill)||H.fill).alpha((A==null?void 0:A.fillOpacity)===void 0?H.fillOpacity:A.fillOpacity).toHex(),A.underlineStyle&&(f.style.borderStyle=A.underlineStyle),A.underlineColor&&(f.style.borderColor=A.underlineColor),A.underlineThickness&&(f.style.borderBottomWidth=`${A.underlineThickness}px`),A.underlineOffset&&(f.style.paddingBottom=`${A.underlineOffset}px`),n.appendChild(f)}})}),e=a},setVisible:a=>{a?n.classList.remove("hidden"):n.classList.add("hidden")}}},Tt=(t,n,e)=>tt(t,n,e,Jt(t)),Zt=(t,n)=>({parse:e=>Rt(e),serialize:e=>Lt(e,t,n)}),te=t=>t.quote!==void 0&&t.start!==void 0&&t.end!==void 0,ee=t=>{const{id:n,creator:e,created:o,modified:s,target:i}=t,a=Array.isArray(i)?i:[i];if(a.length===0)return{error:Error(`No targets found for annotation: ${t.id}`)};const c={creator:N.parseW3CUser(e),created:o?new Date(o):void 0,updated:s?new Date(s):void 0,annotation:n,selector:[],styleClass:"styleClass"in a[0]?a[0].styleClass:void 0};for(const u of a){const b=(Array.isArray(u.selector)?u.selector:[u.selector]).reduce((g,r)=>{switch(r.type){case"TextQuoteSelector":g.quote=r.exact;break;case"TextPositionSelector":g.start=r.start,g.end=r.end;break}return g},{});if(c.outdated||(c.outdated="outdated"in u?u.outdated:void 0),te(b))c.selector.push({...b,id:u.id,scope:u.scope});else{const g=[b.start?void 0:"TextPositionSelector",b.quote?void 0:"TextQuoteSelector"].filter(Boolean);return{error:Error(`Missing selector types: ${g.join(" and ")} for annotation: ${t.id}`)}}}return{parsed:c}},Rt=t=>{const n=t.id||ot.v4(),{creator:e,created:o,modified:s,body:i,...a}=t,c=N.parseW3CBodies(i,n),u=ee(t);return"error"in u?{error:u.error}:{parsed:{...a,id:n,bodies:c,target:u.parsed}}},Lt=(t,n,e)=>{const{bodies:o,target:s,...i}=t,{selector:a,creator:c,created:u,updated:C,...b}=s,g=a.map(r=>{const{id:v,quote:E,start:L,end:R,range:A}=r,f={type:"TextQuoteSelector",exact:E};if(e){const{prefix:m,suffix:d}=lt(A,e);f.prefix=m,f.suffix=d}const l={type:"TextPositionSelector",start:L,end:R};return{...b,id:v,scope:"scope"in r?r.scope:void 0,source:n,selector:[f,l]}});return{...i,"@context":"http://www.w3.org/ns/anno.jsonld",id:t.id,type:"Annotation",body:N.serializeW3CBodies(t.bodies),creator:c,created:u==null?void 0:u.toISOString(),modified:C==null?void 0:C.toISOString(),target:g}},ne=(t,n)=>{const e=new _t,o=new Map,s=Dt.createNanoEvents(),i=(l,m)=>{const d=l.selector.flatMap(w=>{const S=M([w])?w.range:Z(w,n).range;return Array.from(S.getClientRects())}),y=ft(d).map(w=>yt(w,m));return y.map(w=>{const{x:S,y:T,width:O,height:P}=w;return{minX:S,minY:T,maxX:S+O,maxY:T+P,annotation:{id:l.annotation,rects:y}}})},a=()=>[...o.values()],c=()=>{e.clear(),o.clear()},u=l=>{const m=i(l,n.getBoundingClientRect());m.length!==0&&(m.forEach(d=>e.insert(d)),o.set(l.annotation,m))},C=l=>{const m=o.get(l.annotation);m&&(m.forEach(d=>e.remove(d)),o.delete(l.annotation))},b=l=>{C(l),u(l)},g=(l,m=!0)=>{m&&c();const d=n.getBoundingClientRect(),y=l.map(S=>({target:S,rects:i(S,d)}));y.forEach(({target:S,rects:T})=>{T.length>0&&o.set(S.annotation,T)});const w=y.flatMap(({rects:S})=>S);e.load(w)},r=(l,m,d=!1)=>{const y=e.search({minX:l,minY:m,maxX:l,maxY:m}),w=S=>S.annotation.rects.reduce((T,O)=>T+O.width*O.height,0);return y.length>0?(y.sort((S,T)=>w(S)-w(T)),d?y.map(S=>S.annotation.id):[y[0].annotation.id]):[]},v=l=>{const m=E(l);if(m.length===0)return;let d=m[0].left,y=m[0].top,w=m[0].right,S=m[0].bottom;for(let T=1;T<m.length;T++){const O=m[T];d=Math.min(d,O.left),y=Math.min(y,O.top),w=Math.max(w,O.right),S=Math.max(S,O.bottom)}return new DOMRect(d,y,w-d,S-y)},E=l=>{const m=o.get(l);return m?m[0].annotation.rects:[]};return{all:a,clear:c,getAt:r,getAnnotationBounds:v,getAnnotationRects:E,getIntersecting:(l,m,d,y)=>{const w=e.search({minX:l,minY:m,maxX:d,maxY:y}),S=new Set(w.map(T=>T.annotation.id));return Array.from(S).map(T=>({annotation:t.getAnnotation(T),rects:E(T)})).filter(T=>!!T.annotation)},insert:u,recalculate:()=>{g(t.all().map(l=>l.target),!0),s.emit("recalculate")},remove:C,set:g,size:()=>e.all().length,update:b,on:(l,m)=>s.on(l,m)}},xt=(t,n)=>{const e=N.createStore(),o=ne(e,t),s=N.createSelectionState(e,n.userSelectAction,n.adapter),i=N.createHoverState(e),a=N.createViewportState(),c=(f,l=N.Origin.LOCAL)=>{const m=j(f,t),d=M(m.target.selector);return d&&e.addAnnotation(m,l),d},u=(f,l=!0,m=N.Origin.LOCAL)=>{const d=f.map(w=>j(w,t)),y=d.filter(w=>!M(w.target.selector));return e.bulkAddAnnotations(d,l,m),y},C=(f,l=N.Origin.LOCAL)=>{const m=f.map(y=>j(y,t)),d=m.filter(y=>!M(y.target.selector));return m.forEach(y=>{e.getAnnotation(y.id)?e.updateAnnotation(y,l):e.addAnnotation(y,l)}),d},b=(f,l=N.Origin.LOCAL)=>{const m=$(f,t);e.updateTarget(m,l)},g=(f,l=N.Origin.LOCAL)=>{const m=f.map(d=>$(d,t));e.bulkUpdateTargets(m,l)};function r(f,l,m,d){const y=m||!!d,w=o.getAt(f,l,y).map(T=>e.getAnnotation(T)),S=d?w.filter(d):w;if(S.length!==0)return m?S:S[0]}const v=f=>o.getAnnotationRects(f).length>0?o.getAnnotationBounds(f):void 0,E=(f,l,m,d)=>o.getIntersecting(f,l,m,d),L=f=>o.getAnnotationRects(f),R=()=>o.recalculate(),A=f=>o.on("recalculate",f);return e.observe(({changes:f})=>{const l=(f.deleted||[]).filter(y=>M(y.target.selector)),m=(f.created||[]).filter(y=>M(y.target.selector)),d=(f.updated||[]).filter(y=>M(y.newValue.target.selector));(l==null?void 0:l.length)>0&&l.forEach(y=>o.remove(y.target)),m.length>0&&o.set(m.map(y=>y.target),!1),(d==null?void 0:d.length)>0&&d.forEach(({newValue:y})=>o.update(y.target))}),{store:{...e,addAnnotation:c,bulkAddAnnotations:u,bulkUpdateTargets:g,bulkUpsertAnnotations:C,getAnnotationBounds:v,getAnnotationRects:L,getIntersecting:E,getAt:r,recalculatePositions:R,onRecalculatePositions:A,updateTarget:b},selection:s,hover:i,viewport:a}},oe=()=>{const t=document.createElement("canvas");t.width=2*window.innerWidth,t.height=2*window.innerHeight,t.className="r6o-presence-layer";const n=t.getContext("2d");return n.scale(2,2),n.translate(.5,.5),t},Ot=(t,n={})=>{const e=oe(),o=e.getContext("2d");document.body.appendChild(e);const s=new Map,i=b=>Array.from(s.entries()).filter(([g,r])=>r.presenceKey===b.presenceKey).map(([g,r])=>g);return t.on("selectionChange",(b,g)=>{i(b).forEach(v=>s.delete(v)),g&&g.forEach(v=>s.set(v,b))}),{clear:()=>{const{width:b,height:g}=e;o.clearRect(-.5,-.5,b+1,g+1)},destroy:()=>{e.remove()},paint:(b,g,r)=>{n.font&&(o.font=n.font);const v=s.get(b.annotation.id);if(v){const{height:E}=b.rects[0],L=b.rects[0].x+g.left,R=b.rects[0].y+g.top;o.fillStyle=v.appearance.color,o.fillRect(L-2,R-2.5,2,E+5);const A=o.measureText(v.appearance.label),f=A.width+6,l=A.actualBoundingBoxAscent+A.actualBoundingBoxDescent+8,m=A.fontBoundingBoxAscent?8:6.5;return o.fillRect(L-2,R-2.5-l,f,l),o.fillStyle="#fff",o.fillText(v.appearance.label,L+1,R-m),{fill:v.appearance.color,fillOpacity:r?.45:.18}}},reset:()=>{e.width=2*window.innerWidth,e.height=2*window.innerHeight;const b=e.getContext("2d");b.scale(2,2),b.translate(.5,.5)}}},Nt=300,Bt=["up","down","left","right"],kt=it?"⌘+a":"ctrl+a",se=[...Bt.map(t=>`shift+${t}`),kt],It=(t,n,e)=>{const{store:o,selection:s}=n;let i;const{annotatingEnabled:a,offsetReferenceSelector:c,selectionMode:u}=e,C=p=>i=p;let b;const g=p=>b=p;let r,v,E,L=a;const R=p=>{L=p,f.clear(),p||(r=void 0,v=void 0,E=void 0)},A=p=>{L&&v!==!1&&(r=W(p.target)?void 0:{annotation:ot.v4(),selector:[],creator:i,created:new Date})},f=U(p=>{if(!L)return;const x=document.getSelection();if(!(x!=null&&x.anchorNode))return;if(W(x.anchorNode)){r=void 0;return}const k=p.timeStamp-((E==null?void 0:E.timeStamp)||p.timeStamp);if((E==null?void 0:E.type)==="pointerdown"&&(k<1e3&&!r||x.isCollapsed&&k<Nt)&&A(E||p),!r)return;if(x.isCollapsed){o.getAnnotation(r.annotation)&&(s.clear(),o.deleteAnnotation(r.annotation));return}const I=x.getRangeAt(0),_=pt(I,t);if(ut(_))return;const B=ct(_.cloneRange());(B.length!==r.selector.length||B.some((D,K)=>{var et;return D.toString()!==((et=r.selector[K])==null?void 0:et.quote)}))&&(r={...r,selector:B.map(D=>gt(D,t,c)),updated:new Date},o.getAnnotation(r.annotation)?o.updateTarget(r,N.Origin.LOCAL):s.clear())},10),l=p=>{W(p.target)||(E=G(p),v=E.button===0)},m=async p=>{if(W(p.target)||!v)return;const x=()=>{const{x:I,y:_}=t.getBoundingClientRect(),B=p.target instanceof Node&&t.contains(p.target)&&o.getAt(p.clientX-I,p.clientY-_,u==="all",b);if(B){const{selected:Q}=s,D=new Set(Q.map(Y=>Y.id)),K=Array.isArray(B)?B.map(Y=>Y.id):[B.id];(D.size!==K.length||!K.every(Y=>D.has(Y)))&&s.userSelect(K,p)}else s.clear()};if(p.timeStamp-E.timeStamp<Nt){await d();const I=document.getSelection();if(I!=null&&I.isCollapsed){r=void 0,x();return}}r&&r.selector.length>0&&(O(),s.userSelect(r.annotation,G(p)))},d=async()=>{const p=document.getSelection();let x=!1,k=p==null?void 0:p.isCollapsed;const I=()=>k||x,_=1;return setTimeout(()=>x=!0,50),Ut.poll(()=>k=p==null?void 0:p.isCollapsed,_,I)},y=p=>{const x=document.getSelection();x!=null&&x.isCollapsed||((!r||r.selector.length===0)&&f(p),r&&(O(),s.userSelect(r.annotation,G(p))))},w=p=>{L&&p.key==="Shift"&&r&&(document.getSelection().isCollapsed||(O(),s.userSelect(r.annotation,V(p))))},S=p=>{const x=()=>setTimeout(()=>{(r==null?void 0:r.selector.length)>0&&(s.clear(),o.addAnnotation({id:r.annotation,bodies:[],target:r}),s.userSelect(r.annotation,V(p))),document.removeEventListener("selectionchange",x)},100);document.addEventListener("selectionchange",x),A(p)};z(se.join(","),{element:t,keydown:!0,keyup:!1},p=>{p.repeat||(E=V(p))}),z(kt,{keydown:!0,keyup:!1},p=>{E=V(p),S(p)});const T=p=>{p.repeat||p.target!==t&&p.target!==document.body||(r=void 0,s.clear())};z(Bt.join(","),{keydown:!0,keyup:!1},T);const O=()=>{const p=o.getAnnotation(r.annotation);if(!p){o.addAnnotation({id:r.annotation,bodies:[],target:r});return}const{target:{updated:x}}=p,{updated:k}=r;(!x||!k||x<k)&&o.updateTarget(r)};return document.addEventListener("pointerdown",l),document.addEventListener("pointerup",m),document.addEventListener("contextmenu",y),t.addEventListener("keyup",w),t.addEventListener("selectstart",A),document.addEventListener("selectionchange",f),{destroy:()=>{r=void 0,v=void 0,E=void 0,f.clear(),document.removeEventListener("pointerdown",l),document.removeEventListener("pointerup",m),document.removeEventListener("contextmenu",y),t.removeEventListener("keyup",w),t.removeEventListener("selectstart",A),document.removeEventListener("selectionchange",f),z.unbind()},setFilter:g,setUser:C,setAnnotatingEnabled:R}},Mt=(t,n)=>({...t,annotatingEnabled:t.annotatingEnabled??n.annotatingEnabled,user:t.user||n.user}),Ht="SPANS",re=(t,n={})=>{rt(t),at(t);const e=Mt(n,{annotatingEnabled:!0,user:N.createAnonymousGuest()}),o=xt(t,e),{selection:s,viewport:i}=o,a=o.store,c=N.createUndoStack(a),u=N.createLifecycleObserver(o,c,e.adapter);let C=e.user;const b=e.renderer==="CSS_HIGHLIGHTS"?CSS.highlights?"CSS_HIGHLIGHTS":Ht:e.renderer||Ht,g=b==="SPANS"?Tt(t,o,i):b==="CSS_HIGHLIGHTS"?wt(t,o,i):b==="CANVAS"?At(t,o,i):void 0;if(!g)throw`Unknown renderer implementation: ${b}`;console.debug(`Using ${b} renderer`),e.style&&g.setStyle(e.style);const r=It(t,o,e);r.setUser(C),r.setAnnotatingEnabled(e.annotatingEnabled);const v=N.createBaseAnnotator(o,c,e.adapter),E=()=>C,L=d=>{r.setAnnotatingEnabled(d===void 0?!0:d)},R=d=>{g.setFilter(d),r.setFilter(d)},A=d=>{C=d,r.setUser(d)},f=d=>{d&&(g.setPainter(Ot(d,e.presence)),d.on("selectionChange",()=>g.redraw()))},l=d=>{d?s.setSelected(d):s.clear()};return{...v,destroy:()=>{g.destroy(),r.destroy(),c.destroy()},element:t,getUser:E,setAnnotatingEnabled:L,setFilter:R,setStyle:g.setStyle.bind(g),redraw:g.redraw.bind(g),setUser:A,setSelected:l,setPresenceProvider:f,setVisible:g.setVisible.bind(g),on:u.on,off:u.off,scrollIntoView:Ct(t,a),state:o}};Object.defineProperty(h,"Origin",{enumerable:!0,get:()=>N.Origin}),Object.defineProperty(h,"UserSelectAction",{enumerable:!0,get:()=>N.UserSelectAction}),Object.defineProperty(h,"createBody",{enumerable:!0,get:()=>N.createBody}),h.DEFAULT_SELECTED_STYLE=q,h.DEFAULT_STYLE=H,h.NOT_ANNOTATABLE_CLASS=J,h.NOT_ANNOTATABLE_SELECTOR=F,h.W3CTextFormat=Zt,h.cancelSingleClickEvents=rt,h.cloneKeyboardEvent=V,h.clonePointerEvent=G,h.createCanvasRenderer=At,h.createHighlightsRenderer=wt,h.createPresencePainter=Ot,h.createRenderer=vt,h.createSelectionHandler=It,h.createSpansRenderer=Tt,h.createTextAnnotator=re,h.createTextAnnotatorState=xt,h.fillDefaults=Mt,h.getQuoteContext=lt,h.getRangeAnnotatableContents=X,h.isMac=it,h.isNotAnnotatable=W,h.isRangeAnnotatable=st,h.isRevived=M,h.isWhitespaceOrEmpty=ut,h.mergeClientRects=ft,h.paint=Et,h.parseW3CTextAnnotation=Rt,h.programmaticallyFocusable=at,h.rangeContains=ht,h.rangeToSelector=gt,h.reviveAnnotation=j,h.reviveSelector=Z,h.reviveTarget=$,h.scrollIntoView=Ct,h.serializeW3CTextAnnotation=Lt,h.splitAnnotatableRanges=ct,h.toDomRectList=$t,h.toParentBounds=yt,h.toViewportBounds=Vt,h.trimRangeToContainer=pt,h.whitespaceOrEmptyRegex=dt,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
1
+ (function(p,F){typeof exports=="object"&&typeof module<"u"?F(exports,require("debounce"),require("colord"),require("dequal/lite"),require("uuid"),require("@annotorious/core"),require("rbush"),require("nanoevents"),require("hotkeys-js"),require("poll")):typeof define=="function"&&define.amd?define(["exports","debounce","colord","dequal/lite","uuid","@annotorious/core","rbush","nanoevents","hotkeys-js","poll"],F):(p=typeof globalThis<"u"?globalThis:p||self,F(p.RecogitoJS={},p.debounce,p.Colord,p.DequalLite,p.UUID,p.AnnotoriousCore,p.RBush,p.nanoevents,p.HotkeysJs,p.poll))})(this,function(p,F,ne,Pe,oe,O,_e,De,j,Ue){"use strict";const Z="not-annotatable",K=`.${Z}`,$=e=>{var t;return!!(e instanceof HTMLElement?e.closest(K):(t=e.parentElement)==null?void 0:t.closest(K))},se=e=>{const n=e.commonAncestorContainer;return!$(n)},re=e=>e.addEventListener("click",n=>{!n.target.closest(K)&&!n.target.closest("a")&&n.preventDefault()}),ie=typeof navigator<"u"&&/mac/i.test(navigator.userAgentData?navigator.userAgentData.platform:navigator.platform),ae=e=>{!e.hasAttribute("tabindex")&&e.tabIndex<0&&e.setAttribute("tabindex","-1"),e.classList.add("no-focus-outline")},Fe=function*(e){const n=document.createNodeIterator(e.commonAncestorContainer,NodeFilter.SHOW_ELEMENT,o=>o instanceof HTMLElement&&o.classList.contains(Z)&&!o.parentElement.closest(K)&&e.intersectsNode(o)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP);let t;for(;t=n.nextNode();)t instanceof HTMLElement&&(yield t)},ce=e=>{if(!se(e))return[];const n=[];let t=null;for(const o of Fe(e)){let s;t?(s=document.createRange(),s.setStartAfter(t),s.setEndBefore(o)):(s=e.cloneRange(),s.setEndBefore(o)),s.collapsed||n.push(s),t=o}if(t){const o=e.cloneRange();o.setStartAfter(t),o.collapsed||n.push(o)}return n.length>0?n:[e]},G=e=>{const n=e.cloneContents();return n.querySelectorAll(K).forEach(t=>t.remove()),n},le=(e,n,t=10,o)=>{const s=o?e.startContainer.parentElement.closest(o):n,i=document.createRange();i.setStart(s,0),i.setEnd(e.startContainer,e.startOffset);const a=G(i).textContent,c=document.createRange();c.setStart(e.endContainer,e.endOffset),s===document.body?c.setEnd(s,s.childNodes.length):c.setEndAfter(s);const u=G(c).textContent;return{prefix:a.substring(a.length-t),suffix:u.substring(0,t)}},H=e=>e.every(n=>n.range instanceof Range&&!n.range.collapsed),de=/^\s*$/,ue=e=>de.test(e.toString()),Ke=(e,n)=>{const t=i=>Math.round(i*10)/10,o={top:t(e.top),bottom:t(e.bottom),left:t(e.left),right:t(e.right)},s={top:t(n.top),bottom:t(n.bottom),left:t(n.left),right:t(n.right)};if(Math.abs(o.top-s.top)<.5&&Math.abs(o.bottom-s.bottom)<.5){if(Math.abs(o.left-s.right)<.5||Math.abs(o.right-s.left)<.5)return"inline-adjacent";if(o.left>=s.left&&o.right<=s.right)return"inline-is-contained";if(o.left<=s.left&&o.right>=s.right)return"inline-contains"}else if(o.top<=s.top&&o.bottom>=s.bottom){if(o.left<=s.left&&o.right>=s.right)return"block-contains"}else if(o.top>=s.top&&o.bottom<=s.bottom&&o.left>=s.left&&o.right<=s.right)return"block-is-contained"},We=(e,n)=>{const t=Math.min(e.left,n.left),o=Math.max(e.right,n.right),s=Math.min(e.top,n.top),i=Math.max(e.bottom,n.bottom);return new DOMRect(t,s,o-t,i-s)},fe=e=>e.reduce((n,t)=>{if(t.width===0||t.height===0)return n;let o=[...n],s=!1;for(const i of n){const a=Ke(t,i);if(a==="inline-adjacent"){o=o.map(c=>c===i?We(t,i):c),s=!0;break}else if(a==="inline-contains"){o=o.map(c=>c===i?t:c),s=!0;break}else if(a==="inline-is-contained"){s=!0;break}else if(a==="block-contains"||a==="block-is-contained"){t.width<i.width&&(o=o.map(c=>c===i?t:c)),s=!0;break}}return s?o:[...o,t]},[]),$e=e=>({length:e.length,item:n=>e[n],[Symbol.iterator]:function*(){for(let n=0;n<this.length;n++)yield this.item(n)}}),ge=(e,n,t)=>{const o=document.createRange(),s=t?e.startContainer.parentElement.closest(t):n;o.setStart(s,0),o.setEnd(e.startContainer,e.startOffset);const i=G(o).textContent,a=e.toString(),c=i.length||0,u=c+a.length;return t?{quote:a,start:c,end:u,range:e,offsetReference:s}:{quote:a,start:c,end:u,range:e}},ee=(e,n)=>{var b,g;const{start:t,end:o}=e,s=e.offsetReference||n,i=document.createNodeIterator(n,NodeFilter.SHOW_TEXT,r=>{var w;return(w=r.parentElement)!=null&&w.closest(K)?NodeFilter.FILTER_SKIP:NodeFilter.FILTER_ACCEPT});let a=0;const c=document.createRange();let u=i.nextNode();u===null&&console.error("Could not revive annotation target. Content missing.");let S=!s;for(;u!==null;){if(S||(S=typeof(s==null?void 0:s.contains)=="function"?s.contains(u):!1),S){const r=((b=u.textContent)==null?void 0:b.length)||0;if(a+r>t){c.setStart(u,t-a);break}a+=r}u=i.nextNode()}for(;u!==null;){const r=((g=u.textContent)==null?void 0:g.length)||0;if(a+r>=o){c.setEnd(u,o-a);break}a+=r,u=i.nextNode()}return{...e,range:c}},V=(e,n)=>H(e.selector)?e:{...e,selector:e.selector.map(t=>t.range instanceof Range&&!t.range.collapsed?t:ee(t,n))},Q=(e,n)=>H(e.target.selector)?e:{...e,target:V(e.target,n)},me=(e,n)=>{if(e.isEqualNode(n))return!0;for(let t of e.childNodes)if(me(t,n))return!0;return!1},he=(e,n)=>{const t=e.cloneContents();return me(t,n)},pe=(e,n)=>{const t=e.cloneRange(),o=n.contains(t.startContainer),s=n.contains(t.endContainer);return!o&&!s&&!he(t,n)?(t.collapse(),t):(o||t.setStart(n,0),s||t.setEnd(n,n.childNodes.length),t)},J=e=>({...e,type:e.type,x:e.x,y:e.y,clientX:e.clientX,clientY:e.clientY,offsetX:e.offsetX,offsetY:e.offsetY,screenX:e.screenX,screenY:e.screenY,isPrimary:e.isPrimary,altKey:e.altKey,ctrlKey:e.ctrlKey,metaKey:e.metaKey,shiftKey:e.shiftKey,button:e.button,buttons:e.buttons,currentTarget:e.currentTarget,target:e.target,defaultPrevented:e.defaultPrevented,detail:e.detail,eventPhase:e.eventPhase,pointerId:e.pointerId,pointerType:e.pointerType,timeStamp:e.timeStamp}),q=e=>({...e,type:e.type,key:e.key,code:e.code,location:e.location,repeat:e.repeat,altKey:e.altKey,ctrlKey:e.ctrlKey,metaKey:e.metaKey,shiftKey:e.shiftKey,currentTarget:e.currentTarget,target:e.target,defaultPrevented:e.defaultPrevented,detail:e.detail,timeStamp:e.timeStamp}),ye=(e,n)=>{const{left:t,top:o,right:s,bottom:i}=e;return new DOMRect(t-n.left,o-n.top,s-t,i-o)},Ve=(e,n)=>{const{left:t,top:o,right:s,bottom:i}=e;return new DOMRect(t+n.left,o+n.top,s-t,i-o)},be=e=>{if(!e)return document.scrollingElement;const{overflowY:n}=window.getComputedStyle(e);return n!=="visible"&&n!=="hidden"&&e.scrollHeight>e.clientHeight?e:be(e.parentElement)},Se=(e,n,t)=>{const o=t.getBoundingClientRect(),s=t.clientHeight,i=t.clientWidth,a=n.selector[0].range.getBoundingClientRect(),{width:c,height:u}=e.getAnnotationBounds(n.annotation),S=a.top-o.top,b=a.left-o.left,g=t.parentElement?t.scrollTop:0,r=t.parentElement?t.scrollLeft:0,w=S+g-(s-u)/2,C=b+r-(i-c)/2;t.scroll({top:w,left:C,behavior:"smooth"})},Ce=(e,n)=>(t,o)=>{const s=typeof t=="string"?t:t.id,i=o?typeof o=="string"?document.getElementById(o):o:be(e);if(!i)return console.warn(`The scroll parent is missing for the annotation: ${s}`,{container:e}),!1;const a=n.getAnnotation(s);if(!a)return console.warn(`The annotation is missing in the store: ${s}`),!1;const{range:c}=a.target.selector[0];if(c&&!c.collapsed)return Se(n,a.target,i),!0;const u=V(a.target,e),{range:S}=u.selector[0];return S&&!S.collapsed?(Se(n,u,i),!0):!1},_={fill:"rgb(0, 128, 255)",fillOpacity:.18},Y={fill:"rgb(0, 128, 255)",fillOpacity:.45},Ee=(e,n,t,o,s)=>{var a,c;const i=t?typeof t=="function"?t(e.annotation,e.state,s)||((a=e.state)!=null&&a.selected?Y:_):t:(c=e.state)!=null&&c.selected?Y:_;return o&&o.paint(e,n)||i},qe=e=>{const{top:n,left:t}=e.getBoundingClientRect(),{innerWidth:o,innerHeight:s}=window,i=-t,a=-n,c=o-t,u=s-n;return{top:n,left:t,minX:i,minY:a,maxX:c,maxY:u}},Ye=e=>{let n=new Set;return o=>{const s=o.map(i=>i.id);(n.size!==s.length||s.some(i=>!n.has(i)))&&e.set(s),n=new Set(s)}},te=(e,n,t,o)=>{const{store:s,selection:i,hover:a}=n;let c,u,S;const b=Ye(t),g=A=>{const{x:N,y:P}=e.getBoundingClientRect(),h=s.getAt(A.clientX-N,A.clientY-P,!1,u);h?a.current!==h.id&&(e.classList.add("hovered"),a.set(h.id)):a.current&&(e.classList.remove("hovered"),a.set(null))};e.addEventListener("pointermove",g);const r=(A=!1)=>{S&&S.clear();const N=qe(e),{minX:P,minY:h,maxX:x,maxY:B}=N,k=u?s.getIntersecting(P,h,x,B).filter(({annotation:I})=>u(I)):s.getIntersecting(P,h,x,B),D=i.selected.map(({id:I})=>I),M=k.map(({annotation:I,rects:U})=>{const W=D.includes(I.id),z=I.id===a.current;return{annotation:I,rects:U,state:{selected:W,hovered:z}}});o.redraw(M,N,c,S,A),setTimeout(()=>b(k.map(({annotation:I})=>I)),1)},w=A=>{S=A,r()},C=A=>{c=A,r()},L=A=>{u=A,r(!1)},R=()=>r();s.observe(R);const E=i.subscribe(()=>r()),m=a.subscribe(()=>r()),d=()=>r(!0);document.addEventListener("scroll",d,{capture:!0,passive:!0});const f=F(()=>{s.recalculatePositions(),S==null||S.reset(),r()},10);window.addEventListener("resize",f);const l=new ResizeObserver(f);l.observe(e);const y={attributes:!0,childList:!0,subtree:!0},v=new MutationObserver(A=>{A.every(P=>P.target===e||e.contains(P.target))||r(!0)});return v.observe(document.body,y),{destroy:()=>{e.removeEventListener("pointermove",g),o.destroy(),s.unobserve(R),E(),m(),document.removeEventListener("scroll",d),f.clear(),window.removeEventListener("resize",f),l.disconnect(),v.disconnect()},redraw:r,setStyle:C,setFilter:L,setPainter:w,setVisible:o.setVisible}},ze=()=>{const e=document.createElement("canvas");return e.width=window.innerWidth,e.height=window.innerHeight,e.className="r6o-canvas-highlight-layer bg",e},Xe=(e,n)=>{e.width=window.innerWidth,e.height=window.innerHeight},je=e=>{e.classList.add("r6o-annotatable");const n=ze(),t=n.getContext("2d");document.body.appendChild(n);const o=(c,u,S,b)=>requestAnimationFrame(()=>{const{width:g,height:r}=n;t.clearRect(-.5,-.5,g+1,r+1),b&&b.clear();const{top:w,left:C}=u;[...c].sort((R,E)=>{const{annotation:{target:{created:m}}}=R,{annotation:{target:{created:d}}}=E;return m.getTime()-d.getTime()}).forEach(R=>{var f;const E=S?typeof S=="function"?S(R.annotation,R.state):S:(f=R.state)!=null&&f.selected?Y:_,m=b&&b.paint(R,u)||E,d=R.rects.map(({x:l,y,width:v,height:T})=>({x:l+C,y:y+w,width:v,height:T}));if(t.fillStyle=m.fill,t.globalAlpha=m.fillOpacity||1,d.forEach(({x:l,y,width:v,height:T})=>t.fillRect(l,y,v,T)),m.underlineColor){t.globalAlpha=1,t.strokeStyle=m.underlineColor,t.lineWidth=m.underlineThickness??1;const l=m.underlineOffset??0;d.forEach(({x:y,y:v,width:T,height:A})=>{t.beginPath(),t.moveTo(y,v+A+l),t.lineTo(y+T,v+A+l),t.stroke()})}})}),s=F(()=>Xe(n),10);return window.addEventListener("resize",s),{destroy:()=>{n.remove(),s.clear(),window.removeEventListener("resize",s)},setVisible:c=>{console.log("setVisible not implemented on Canvas renderer")},redraw:o}},Ae=(e,n,t)=>te(e,n,t,je(e)),Ge=e=>[`background-color:${ne.colord((e==null?void 0:e.fill)||_.fill).alpha((e==null?void 0:e.fillOpacity)===void 0?_.fillOpacity:e.fillOpacity).toHex()}`,e!=null&&e.underlineThickness?"text-decoration:underline":void 0,e!=null&&e.underlineColor?`text-decoration-color:${e.underlineColor}`:void 0,e!=null&&e.underlineOffset?`text-underline-offset:${e.underlineOffset}px`:void 0,e!=null&&e.underlineThickness?`text-decoration-thickness:${e.underlineThickness}px`:void 0].filter(Boolean).join(";"),ve=()=>{const e=document.createElement("style");document.getElementsByTagName("head")[0].appendChild(e);let n=new Set;return{destroy:()=>{CSS.highlights.clear(),e.remove()},setVisible:i=>{console.log("setVisible not implemented on CSS Custom Highlights renderer")},redraw:(i,a,c,u)=>{u&&u.clear();const S=new Set(i.map(g=>g.annotation.id));Array.from(n).filter(g=>!S.has(g));const b=i.map(g=>{var C;const r=c?typeof c=="function"?c(g.annotation,g.state):c:(C=g.state)!=null&&C.selected?Y:_,w=u&&u.paint(g,a)||r;return`::highlight(_${g.annotation.id}) { ${Ge(w)} }`});e.innerHTML=b.join(`
2
+ `),CSS.highlights.clear(),i.forEach(({annotation:g})=>{const r=g.target.selector.map(C=>C.range),w=new Highlight(...r);CSS.highlights.set(`_${g.id}`,w)}),n=S}}},we=(e,n,t)=>te(e,n,t,ve()),Qe=(e,n)=>{const t=(i,a)=>i.x<=a.x+a.width&&i.x+i.width>=a.x&&i.y<=a.y+a.height&&i.y+i.height>=a.y,o=i=>i.rects.reduce((a,c)=>a+c.width,0),s=n.filter(({rects:i})=>i.some(a=>t(e,a)));return s.sort((i,a)=>o(a)-o(i)),s.findIndex(i=>i.rects.includes(e))},Je=e=>{e.classList.add("r6o-annotatable");const n=document.createElement("div");n.className="r6o-span-highlight-layer",e.insertBefore(n,e.firstChild);let t=[];return{destroy:()=>{n.remove()},redraw:(a,c,u,S,b)=>{const r=!(Pe.dequal(t,a)&&b);if(!S&&!r)return;r&&(n.innerHTML=""),[...a].sort((C,L)=>{const{annotation:{target:{created:R}}}=C,{annotation:{target:{created:E}}}=L;return R&&E?R.getTime()-E.getTime():0}).forEach(C=>{C.rects.map(L=>{const R=Qe(L,a),E=Ee(C,c,u,S,R);if(r){const m=document.createElement("span");m.className="r6o-annotation",m.dataset.annotation=C.annotation.id,m.style.left=`${L.x}px`,m.style.top=`${L.y}px`,m.style.width=`${L.width}px`,m.style.height=`${L.height}px`,m.style.backgroundColor=ne.colord((E==null?void 0:E.fill)||_.fill).alpha((E==null?void 0:E.fillOpacity)===void 0?_.fillOpacity:E.fillOpacity).toHex(),E.underlineStyle&&(m.style.borderStyle=E.underlineStyle),E.underlineColor&&(m.style.borderColor=E.underlineColor),E.underlineThickness&&(m.style.borderBottomWidth=`${E.underlineThickness}px`),E.underlineOffset&&(m.style.paddingBottom=`${E.underlineOffset}px`),n.appendChild(m)}})}),t=a},setVisible:a=>{a?n.classList.remove("hidden"):n.classList.add("hidden")}}},Te=(e,n,t)=>te(e,n,t,Je(e)),Ze=(e,n)=>({parse:t=>Re(t),serialize:t=>Le(t,e,n)}),et=e=>e.quote!==void 0&&e.start!==void 0&&e.end!==void 0,tt=e=>{const{id:n,creator:t,created:o,modified:s,target:i}=e,a=Array.isArray(i)?i:[i];if(a.length===0)return{error:Error(`No targets found for annotation: ${e.id}`)};const c={creator:O.parseW3CUser(t),created:o?new Date(o):void 0,updated:s?new Date(s):void 0,annotation:n,selector:[],styleClass:"styleClass"in a[0]?a[0].styleClass:void 0};for(const u of a){const b=(Array.isArray(u.selector)?u.selector:[u.selector]).reduce((g,r)=>{switch(r.type){case"TextQuoteSelector":g.quote=r.exact;break;case"TextPositionSelector":g.start=r.start,g.end=r.end;break}return g},{});if(c.outdated||(c.outdated="outdated"in u?u.outdated:void 0),et(b))c.selector.push({...b,id:u.id,scope:u.scope});else{const g=[b.start?void 0:"TextPositionSelector",b.quote?void 0:"TextQuoteSelector"].filter(Boolean);return{error:Error(`Missing selector types: ${g.join(" and ")} for annotation: ${e.id}`)}}}return{parsed:c}},Re=e=>{const n=e.id||oe.v4(),{creator:t,created:o,modified:s,body:i,...a}=e,c=O.parseW3CBodies(i,n),u=tt(e);return"error"in u?{error:u.error}:{parsed:{...a,id:n,bodies:c,target:u.parsed}}},Le=(e,n,t)=>{const{bodies:o,target:s,...i}=e,{selector:a,creator:c,created:u,updated:S,...b}=s,g=a.map(r=>{const{id:w,quote:C,start:L,end:R,range:E}=r,m={type:"TextQuoteSelector",exact:C};if(t){const{prefix:f,suffix:l}=le(E,t);m.prefix=f,m.suffix=l}const d={type:"TextPositionSelector",start:L,end:R};return{...b,id:w,scope:"scope"in r?r.scope:void 0,source:n,selector:[m,d]}});return{...i,"@context":"http://www.w3.org/ns/anno.jsonld",id:e.id,type:"Annotation",body:O.serializeW3CBodies(e.bodies),creator:c,created:u==null?void 0:u.toISOString(),modified:S==null?void 0:S.toISOString(),target:g}},nt=(e,n)=>{const t=new _e,o=new Map,s=De.createNanoEvents(),i=(d,f)=>{const l=d.selector.flatMap(v=>{const T=H([v])?v.range:ee(v,n).range;return Array.from(T.getClientRects())}),y=fe(l).map(v=>ye(v,f));return y.map(v=>{const{x:T,y:A,width:N,height:P}=v;return{minX:T,minY:A,maxX:T+N,maxY:A+P,annotation:{id:d.annotation,rects:y}}})},a=()=>[...o.values()],c=()=>{t.clear(),o.clear()},u=d=>{const f=i(d,n.getBoundingClientRect());f.length!==0&&(f.forEach(l=>t.insert(l)),o.set(d.annotation,f))},S=d=>{const f=o.get(d.annotation);f&&(f.forEach(l=>t.remove(l)),o.delete(d.annotation))},b=d=>{S(d),u(d)},g=(d,f=!0)=>{f&&c();const l=n.getBoundingClientRect(),y=d.map(T=>({target:T,rects:i(T,l)}));y.forEach(({target:T,rects:A})=>{A.length>0&&o.set(T.annotation,A)});const v=y.flatMap(({rects:T})=>T);t.load(v)},r=(d,f,l=!1)=>{const y=t.search({minX:d,minY:f,maxX:d,maxY:f}),v=T=>T.annotation.rects.reduce((A,N)=>A+N.width*N.height,0);return y.length>0?(y.sort((T,A)=>v(T)-v(A)),l?y.map(T=>T.annotation.id):[y[0].annotation.id]):[]},w=d=>{const f=C(d);if(f.length===0)return;let l=f[0].left,y=f[0].top,v=f[0].right,T=f[0].bottom;for(let A=1;A<f.length;A++){const N=f[A];l=Math.min(l,N.left),y=Math.min(y,N.top),v=Math.max(v,N.right),T=Math.max(T,N.bottom)}return new DOMRect(l,y,v-l,T-y)},C=d=>{const f=o.get(d);return f?f[0].annotation.rects:[]};return{all:a,clear:c,getAt:r,getAnnotationBounds:w,getAnnotationRects:C,getIntersecting:(d,f,l,y)=>{const v=t.search({minX:d,minY:f,maxX:l,maxY:y}),T=new Set(v.map(A=>A.annotation.id));return Array.from(T).map(A=>({annotation:e.getAnnotation(A),rects:C(A)})).filter(A=>!!A.annotation)},insert:u,recalculate:()=>{g(e.all().map(d=>d.target),!0),s.emit("recalculate")},remove:S,set:g,size:()=>t.all().length,update:b,on:(d,f)=>s.on(d,f)}},xe=(e,n)=>{const t=O.createStore(),o=nt(t,e),s=O.createSelectionState(t,n.userSelectAction,n.adapter),i=O.createHoverState(t),a=O.createViewportState(),c=(m,d=O.Origin.LOCAL)=>{const f=Q(m,e),l=H(f.target.selector);return l&&t.addAnnotation(f,d),l},u=(m,d=!0,f=O.Origin.LOCAL)=>{const l=m.map(v=>Q(v,e)),y=l.filter(v=>!H(v.target.selector));return t.bulkAddAnnotations(l,d,f),y},S=(m,d=O.Origin.LOCAL)=>{const f=m.map(y=>Q(y,e)),l=f.filter(y=>!H(y.target.selector));return f.forEach(y=>{t.getAnnotation(y.id)?t.updateAnnotation(y,d):t.addAnnotation(y,d)}),l},b=(m,d=O.Origin.LOCAL)=>{const f=V(m,e);t.updateTarget(f,d)},g=(m,d=O.Origin.LOCAL)=>{const f=m.map(l=>V(l,e));t.bulkUpdateTargets(f,d)};function r(m,d,f,l){const y=f||!!l,v=o.getAt(m,d,y).map(A=>t.getAnnotation(A)),T=l?v.filter(l):v;if(T.length!==0)return f?T:T[0]}const w=m=>o.getAnnotationRects(m).length>0?o.getAnnotationBounds(m):void 0,C=(m,d,f,l)=>o.getIntersecting(m,d,f,l),L=m=>o.getAnnotationRects(m),R=()=>o.recalculate(),E=m=>o.on("recalculate",m);return t.observe(({changes:m})=>{const d=(m.deleted||[]).filter(y=>H(y.target.selector)),f=(m.created||[]).filter(y=>H(y.target.selector)),l=(m.updated||[]).filter(y=>H(y.newValue.target.selector));(d==null?void 0:d.length)>0&&d.forEach(y=>o.remove(y.target)),f.length>0&&o.set(f.map(y=>y.target),!1),(l==null?void 0:l.length)>0&&l.forEach(({newValue:y})=>o.update(y.target))}),{store:{...t,addAnnotation:c,bulkAddAnnotations:u,bulkUpdateTargets:g,bulkUpsertAnnotations:S,getAnnotationBounds:w,getAnnotationRects:L,getIntersecting:C,getAt:r,recalculatePositions:R,onRecalculatePositions:E,updateTarget:b},selection:s,hover:i,viewport:a}},ot=()=>{const e=document.createElement("canvas");e.width=2*window.innerWidth,e.height=2*window.innerHeight,e.className="r6o-presence-layer";const n=e.getContext("2d");return n.scale(2,2),n.translate(.5,.5),e},Oe=(e,n={})=>{const t=ot(),o=t.getContext("2d");document.body.appendChild(t);const s=new Map,i=b=>Array.from(s.entries()).filter(([g,r])=>r.presenceKey===b.presenceKey).map(([g,r])=>g);return e.on("selectionChange",(b,g)=>{i(b).forEach(w=>s.delete(w)),g&&g.forEach(w=>s.set(w,b))}),{clear:()=>{const{width:b,height:g}=t;o.clearRect(-.5,-.5,b+1,g+1)},destroy:()=>{t.remove()},paint:(b,g,r)=>{n.font&&(o.font=n.font);const w=s.get(b.annotation.id);if(w){const{height:C}=b.rects[0],L=b.rects[0].x+g.left,R=b.rects[0].y+g.top;o.fillStyle=w.appearance.color,o.fillRect(L-2,R-2.5,2,C+5);const E=o.measureText(w.appearance.label),m=E.width+6,d=E.actualBoundingBoxAscent+E.actualBoundingBoxDescent+8,f=E.fontBoundingBoxAscent?8:6.5;return o.fillRect(L-2,R-2.5-d,m,d),o.fillStyle="#fff",o.fillText(w.appearance.label,L+1,R-f),{fill:w.appearance.color,fillOpacity:r?.45:.18}}},reset:()=>{t.width=2*window.innerWidth,t.height=2*window.innerHeight;const b=t.getContext("2d");b.scale(2,2),b.translate(.5,.5)}}},Ne=300,Be=["up","down","left","right"],ke=ie?"⌘+a":"ctrl+a",st=[...Be.map(e=>`shift+${e}`),ke],Ie=(e,n,t)=>{const{store:o,selection:s}=n;let i;const{annotatingEnabled:a,offsetReferenceSelector:c,selectionMode:u}=t,S=h=>i=h;let b;const g=h=>b=h;let r,w,C,L=a;const R=h=>{L=h,m.clear(),h||(r=void 0,w=void 0,C=void 0)},E=h=>{L&&w!==!1&&(r=$(h.target)?void 0:{annotation:oe.v4(),selector:[],creator:i,created:new Date})},m=F(h=>{if(!L)return;const x=document.getSelection();if(!(x!=null&&x.anchorNode))return;if($(x.anchorNode)){r=void 0;return}const B=h.timeStamp-((C==null?void 0:C.timeStamp)||h.timeStamp);if((C==null?void 0:C.type)==="pointerdown"&&(B<1e3&&!r||x.isCollapsed&&B<Ne)&&E(C||h),!r)return;if(x.isCollapsed){o.getAnnotation(r.annotation)&&(s.clear(),o.deleteAnnotation(r.annotation));return}const k=x.getRangeAt(0),D=pe(k,e);if(ue(D))return;const M=ce(D.cloneRange());(M.length!==r.selector.length||M.some((U,W)=>{var z;return U.toString()!==((z=r.selector[W])==null?void 0:z.quote)}))&&(r={...r,selector:M.map(U=>ge(U,e,c)),updated:new Date},o.getAnnotation(r.annotation)?o.updateTarget(r,O.Origin.LOCAL):s.clear())},10),d=h=>{$(h.target)||(C=J(h),w=C.button===0)},f=async h=>{if($(h.target)||!w)return;const x=()=>{const{x:k,y:D}=e.getBoundingClientRect(),M=h.target instanceof Node&&e.contains(h.target)&&o.getAt(h.clientX-k,h.clientY-D,u==="all",b);if(M){const{selected:I}=s,U=new Set(I.map(X=>X.id)),W=Array.isArray(M)?M.map(X=>X.id):[M.id];(U.size!==W.length||!W.every(X=>U.has(X)))&&s.userSelect(W,h)}else s.clear()};if(h.timeStamp-C.timeStamp<Ne){await l();const k=document.getSelection();if(k!=null&&k.isCollapsed){r=void 0,x();return}}r&&r.selector.length>0&&(N(),s.userSelect(r.annotation,J(h)))},l=async()=>{const h=document.getSelection();let x=!1,B=h==null?void 0:h.isCollapsed;const k=()=>B||x,D=1;return setTimeout(()=>x=!0,50),Ue.poll(()=>B=h==null?void 0:h.isCollapsed,D,k)},y=h=>{const x=document.getSelection();x!=null&&x.isCollapsed||((!r||r.selector.length===0)&&m(h),r&&(N(),s.userSelect(r.annotation,J(h))))},v=h=>{L&&h.key==="Shift"&&r&&(document.getSelection().isCollapsed||(N(),s.userSelect(r.annotation,q(h))))},T=h=>{const x=()=>setTimeout(()=>{(r==null?void 0:r.selector.length)>0&&(s.clear(),o.addAnnotation({id:r.annotation,bodies:[],target:r}),s.userSelect(r.annotation,q(h))),document.removeEventListener("selectionchange",x)},100);document.addEventListener("selectionchange",x),E(h)};j(st.join(","),{element:e,keydown:!0,keyup:!1},h=>{h.repeat||(C=q(h))}),j(ke,{keydown:!0,keyup:!1},h=>{C=q(h),T(h)});const A=h=>{h.repeat||h.target!==e&&h.target!==document.body||(r=void 0,s.clear())};j(Be.join(","),{keydown:!0,keyup:!1},A);const N=()=>{const h=o.getAnnotation(r.annotation);if(!h){o.addAnnotation({id:r.annotation,bodies:[],target:r});return}const{target:{updated:x}}=h,{updated:B}=r;(!x||!B||x<B)&&o.updateTarget(r)};return document.addEventListener("pointerdown",d),document.addEventListener("pointerup",f),document.addEventListener("contextmenu",y),e.addEventListener("keyup",v),e.addEventListener("selectstart",E),document.addEventListener("selectionchange",m),{destroy:()=>{r=void 0,w=void 0,C=void 0,m.clear(),document.removeEventListener("pointerdown",d),document.removeEventListener("pointerup",f),document.removeEventListener("contextmenu",y),e.removeEventListener("keyup",v),e.removeEventListener("selectstart",E),document.removeEventListener("selectionchange",m),j.unbind()},setFilter:g,setUser:S,setAnnotatingEnabled:R}},Me=(e,n)=>({...e,annotatingEnabled:e.annotatingEnabled??n.annotatingEnabled,user:e.user||n.user}),He="SPANS",rt=(e,n={})=>{re(e),ae(e);const t=Me(n,{annotatingEnabled:!0,user:O.createAnonymousGuest()}),o=xe(e,t),{selection:s,viewport:i}=o,a=o.store,c=O.createUndoStack(a),u=O.createLifecycleObserver(o,c,t.adapter);let S=t.user;const b=t.renderer==="CSS_HIGHLIGHTS"?CSS.highlights?"CSS_HIGHLIGHTS":He:t.renderer||He,g=b==="SPANS"?Te(e,o,i):b==="CSS_HIGHLIGHTS"?we(e,o,i):b==="CANVAS"?Ae(e,o,i):void 0;if(!g)throw`Unknown renderer implementation: ${b}`;console.debug(`Using ${b} renderer`),t.style&&g.setStyle(t.style);const r=Ie(e,o,t);r.setUser(S),r.setAnnotatingEnabled(t.annotatingEnabled);const w=O.createBaseAnnotator(o,c,t.adapter),C=()=>S,L=l=>{r.setAnnotatingEnabled(l===void 0?!0:l)},R=l=>{g.setFilter(l),r.setFilter(l)},E=l=>{S=l,r.setUser(l)},m=l=>{l&&(g.setPainter(Oe(l,t.presence)),l.on("selectionChange",()=>g.redraw()))},d=l=>{l?s.setSelected(l):s.clear()};return{...w,destroy:()=>{g.destroy(),r.destroy(),c.destroy()},element:e,getUser:C,setAnnotatingEnabled:L,setFilter:R,setStyle:g.setStyle.bind(g),redraw:g.redraw.bind(g),setUser:E,setSelected:d,setPresenceProvider:m,setVisible:g.setVisible.bind(g),on:u.on,off:u.off,scrollIntoView:Ce(e,a),state:o}};Object.defineProperty(p,"Origin",{enumerable:!0,get:()=>O.Origin}),Object.defineProperty(p,"UserSelectAction",{enumerable:!0,get:()=>O.UserSelectAction}),Object.defineProperty(p,"createBody",{enumerable:!0,get:()=>O.createBody}),p.DEFAULT_SELECTED_STYLE=Y,p.DEFAULT_STYLE=_,p.NOT_ANNOTATABLE_CLASS=Z,p.NOT_ANNOTATABLE_SELECTOR=K,p.W3CTextFormat=Ze,p.cancelSingleClickEvents=re,p.cloneKeyboardEvent=q,p.clonePointerEvent=J,p.createCanvasRenderer=Ae,p.createHighlightsRenderer=we,p.createPresencePainter=Oe,p.createRenderer=ve,p.createSelectionHandler=Ie,p.createSpansRenderer=Te,p.createTextAnnotator=rt,p.createTextAnnotatorState=xe,p.fillDefaults=Me,p.getQuoteContext=le,p.getRangeAnnotatableContents=G,p.isMac=ie,p.isNotAnnotatable=$,p.isRangeAnnotatable=se,p.isRevived=H,p.isWhitespaceOrEmpty=ue,p.mergeClientRects=fe,p.paint=Ee,p.parseW3CTextAnnotation=Re,p.programmaticallyFocusable=ae,p.rangeContains=he,p.rangeToSelector=ge,p.reviveAnnotation=Q,p.reviveSelector=ee,p.reviveTarget=V,p.scrollIntoView=Ce,p.serializeW3CTextAnnotation=Le,p.splitAnnotatableRanges=ce,p.toDomRectList=$e,p.toParentBounds=ye,p.toViewportBounds=Ve,p.trimRangeToContainer=pe,p.whitespaceOrEmptyRegex=de,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
3
3
  //# sourceMappingURL=text-annotator.umd.js.map