1 line
3.0 KiB
JavaScript
1 line
3.0 KiB
JavaScript
import{r as e}from"./rolldown-runtime-hePW80VL.js";import{B as t,W as n,c as r,l as i,r as a}from"./button-BhAQo6Sy.js";var o=e(n(),1),s=o.createContext({register:()=>{},unregister:()=>{},subscribeMapChange:()=>()=>{},nextIndexRef:{current:0}});function c(){return o.useContext(s)}function l(e={}){let{guess:t,label:n,metadata:i,textRef:a,index:s}=e,{register:l,unregister:u,subscribeMapChange:d,nextIndexRef:f}=c(),p=o.useRef(-1),[m,h]=o.useState(s==null&&t?()=>{if(p.current===-1){let e=f.current;f.current+=1,p.current=e}return p.current}:-1),g=s??m,_=o.useRef(null),v=o.useCallback(e=>{let t=_.current;t&&u(t),_.current=e,e&&l(e,{metadata:i??null,index:s??null,label:n,textRef:a})},[s,l,u,i,n,a]);return r(()=>{if(s==null)return d(e=>{let t=_.current?e.get(_.current)?.index:null;t!=null&&h(t)})},[s,d]),{ref:v,index:g}}var u=a();function d(e){let{children:n,elementsRef:a,labelsRef:c,onMapChange:l}=e,d=i(l),[,v]=o.useState(!1),y=t(p).current,b=t(f).current,x=o.useRef(0),S=o.useRef(!0),C=o.useRef([]),w=o.useRef(null),T=i(()=>{S.current||(S.current=!0,v(e=>!e))}),E=i((e,t)=>{b.set(e,t),T()}),D=i(e=>{b.delete(e),T()}),O=i(e=>{let t=new Map;return a.current.length=0,c&&(c.current.length=0),e.forEach(e=>{t.set(e.element,{...e.registration.metadata??{},index:e.index}),a.current[e.index]=e.element,c&&(c.current[e.index]=e.registration.label===void 0?e.registration.textRef?.current?.textContent??e.element.textContent:e.registration.label)}),x.current=a.current.length,t});function k(e){if(w.current?.disconnect(),w.current=null,typeof MutationObserver!=`function`||e.length<2)return;let t=new MutationObserver(n=>{if(!g(n))return;let r=null;for(let n of e)if(n.isConnected){if(r&&_(r,n)>0){t.disconnect(),T();return}r=n}});w.current=t;let n=new Set;for(let t=1;t<e.length;t+=1){let r=h(e[t-1],e[t]);r&&n.add(r)}n.forEach(e=>t.observe(e,{childList:!0}))}let A=i(()=>{let[e,t]=m(b),n=O(e);k(t),C.current=e,S.current=!1,y.forEach(e=>e(n)),d(n)});r(()=>(S.current||O(C.current),()=>{a.current=[],c&&(c.current=[])}),[a,c,O]),r(()=>{S.current&&A()}),r(()=>()=>{w.current?.disconnect(),S.current=!0},[]);let j=i(e=>(y.add(e),()=>{y.delete(e)})),M=o.useMemo(()=>({register:E,unregister:D,subscribeMapChange:j,nextIndexRef:x}),[E,D,j,x]);return(0,u.jsx)(s.Provider,{value:M,children:n})}function f(){return new Map}function p(){return new Set}function m(e){let t=new Set,n=[],r=[];e.forEach((e,i)=>{if(!i.isConnected)return;let a=e.index,o={index:a??-1,element:i,registration:e};a===null?r.push(o):a>=0&&(t.add(a),n.push(o))});let i=0;return r.sort((e,t)=>_(e.element,t.element)),r.forEach(e=>{for(;t.has(i);)i+=1;e.index=i,n.push(e),i+=1}),t.size>0&&n.sort((e,t)=>e.index-t.index),[n,r.map(e=>e.element)]}function h(e,t){let n=e.parentElement;for(;n&&!n.contains(t);)n=n.parentElement;return n}function g(e){for(let t of e)for(let e=0;e<t.removedNodes.length;e+=1)if(t.removedNodes[e].isConnected)return!0;return!1}function _(e,t){return e.compareDocumentPosition(t)&Node.DOCUMENT_POSITION_FOLLOWING?-1:1}export{l as n,d as t}; |