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