@soomo/text-annotator 3.2.0-staging.8 → 3.3.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.
- package/dist/src/TextAnnotatorOptions.d.ts +13 -0
- package/dist/src/utils/isNotAnnotatable.d.ts +2 -2
- package/dist/src/utils/splitAnnotatableRanges.d.ts +1 -1
- package/dist/text-annotator.es.js +537 -528
- package/dist/text-annotator.es.js.map +1 -1
- package/dist/text-annotator.umd.js +2 -2
- package/dist/text-annotator.umd.js.map +1 -1
- package/package.json +9 -9
|
@@ -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,_){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"})}));
|
|
3
3
|
//# sourceMappingURL=text-annotator.umd.js.map
|