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