|
let e=new WeakMap,t=new WeakMap;export class FocusStartingPoint extends EventTarget{constructor(n=globalThis){super();let o,{document:s,history:i}=n,{caretPositionFromPoint:r,body:a}=s,l=(n,o)=>{c===n&&d===o||(e.set(this,n),t.set(this,o),this.dispatchEvent(Object.assign(new Event('change'),{oldNode:c,newNode:n,oldOffset:d,newOffset:o})),c=n,d=o)},c=s.activeElement,d=0,f=i.length;e.set(this,c),t.set(this,d),r||(r=(e,t)=>{let n=s.caretRangeFromPoint(e,t),i=n.startContainer,r=n.startOffset,a=n.selectNode(i)||n.getBoundingClientRect();return(1===i.nodeType||e<a.left||e>a.right||t<a.top||t>a.bottom)&&(i=o,r=0),{offsetNode:i,offset:r}}),n.addEventListener('focusin',(()=>{l(s.activeElement,0)}),!0),n.addEventListener('hashchange',(()=>{f!==(f=i.length)&&l(s.querySelector(':target')||a,0)}),!0),n.addEventListener('pointerdown',(e=>{o=s.elementFromPoint(e.clientX,e.clientY);let{offsetNode:t,offset:n}=r.call(s,e.clientX,e.clientY);t.contains(o)?l(o,0):l(t,n)}),!0)}get node(){return e.get(this)}get offset(){return t.get(this)}} |