react-anchorlist 0.3.6 → 0.3.8
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/hooks/useVirtualEngine.d.ts.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +49 -47
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useVirtualEngine.d.ts","sourceRoot":"","sources":["../../src/hooks/useVirtualEngine.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAkB,sBAAsB,EAAe,MAAM,UAAU,CAAA;AAEnF;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,EAAE;IAC3C,KAAK,EAAE,CAAC,EAAE,CAAA;IACV,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM,CAAA;IACnD,iBAAiB,EAAE,MAAM,CAAA;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,gBAAgB,EAAE,KAAK,GAAG,QAAQ,CAAA;CACnC,GAAG,sBAAsB,CAAC,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"useVirtualEngine.d.ts","sourceRoot":"","sources":["../../src/hooks/useVirtualEngine.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAkB,sBAAsB,EAAe,MAAM,UAAU,CAAA;AAEnF;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,EAAE;IAC3C,KAAK,EAAE,CAAC,EAAE,CAAA;IACV,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM,CAAA;IACnD,iBAAiB,EAAE,MAAM,CAAA;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,gBAAgB,EAAE,KAAK,GAAG,QAAQ,CAAA;CACnC,GAAG,sBAAsB,CAAC,CAAC,CAAC,CAyX5B"}
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const q=require("react/jsx-runtime"),r=require("react");function he(i){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(i){for(const t in i)if(t!=="default"){const s=Object.getOwnPropertyDescriptor(i,t);Object.defineProperty(e,t,s.get?s:{enumerable:!0,get:()=>i[t]})}}return e.default=i,Object.freeze(e)}const te=he(r);class de{constructor(e,t){this.defaultSize=t,this.sizes=e>0?Array(e).fill(t):[],this.offsets=e>0?Array(e).fill(0):[],e>0&&this._recalcFrom(0)}_recalcFrom(e){for(let t=e;t<this.sizes.length;t++)this.offsets[t]=t===0?0:(this.offsets[t-1]??0)+(this.sizes[t-1]??this.defaultSize)}getOffset(e){return this.offsets[e]??0}getSize(e){return this.sizes[e]??this.defaultSize}setSize(e,t){return this.sizes[e]===t?!1:(this.sizes[e]=t,this._recalcFrom(e+1),!0)}prepend(e){const t=Array(e).fill(this.defaultSize);this.sizes=[...t,...this.sizes],this.offsets=Array(this.sizes.length).fill(0),this._recalcFrom(0)}append(e){const t=this.sizes.length;for(let s=0;s<e;s++)this.sizes.push(this.defaultSize),this.offsets.push(0);this._recalcFrom(t)}resize(e){const t=this.sizes.length;e>t?this.append(e-t):e<t&&(this.sizes=this.sizes.slice(0,e),this.offsets=this.offsets.slice(0,e))}totalSize(){if(this.sizes.length===0)return 0;const e=this.sizes.length-1;return(this.offsets[e]??0)+(this.sizes[e]??this.defaultSize)}get count(){return this.sizes.length}getOffsets(){return this.offsets}getSizes(){return this.sizes}}class me{constructor(){this.cache=new Map}get(e){return this.cache.get(e)}set(e,t){this.cache.set(e,t)}has(e){return this.cache.has(e)}delete(e){this.cache.delete(e)}clear(){this.cache.clear()}applyToOffsetMap(e,t){for(const[s,l]of this.cache){const o=t.get(s);o!==void 0&&e.setSize(o,l)}}}function oe(i,e){if(i.length===0)return 0;let t=0,s=i.length-1;for(;t<s;){const l=t+s>>1;(i[l]??0)<e?t=l+1:s=l}return Math.max(0,t>0&&(i[t]??0)>e?t-1:t)}function pe(i,e,t){if(i.length===0)return 0;let s=0,l=i.length-1,o=0;for(;s<=l;){const n=s+l>>1;(i[n]??0)<t?(o=n,s=n+1):l=n-1}return o}function ge(i){const{firstVisible:e,lastVisible:t,itemCount:s,overscan:l}=i;return s===0?{start:0,end:-1}:{start:Math.max(0,e-l),end:Math.min(s-1,t+l)}}function Re(i,e,t,s){const l=s==null?void 0:s.reconcile,o=r.useRef(null);return r.useCallback((n,y)=>{const u=i.current,d=e.current;if(!u||!d)return;o.current!==null&&(cancelAnimationFrame(o.current),o.current=null);let R,h;if(typeof n=="object"&&n!==null?(R=n.index,h={align:n.align,behavior:n.behavior,offset:n.offset}):(R=n,h=y),!Number.isFinite(R))return;const p=Math.max(0,Math.min(Math.floor(R),d.count-1));d.getOffset(p),d.getSize(p);const I=(h==null?void 0:h.align)??"start",k=(h==null?void 0:h.behavior)??"auto",E=(h==null?void 0:h.offset)??0,a=()=>{var P;const H=e.current,f=i.current;if(!H||!f)return 0;const S=((P=t==null?void 0:t.current)==null?void 0:P.offsetTop)??0,v=H.getOffset(p),C=H.getSize(p);return I==="start"?S+v+E:I==="center"?S+v-f.clientHeight/2+C/2+E:S+v-f.clientHeight+C+E},w=a();((H,f)=>{typeof u.scrollTo=="function"?u.scrollTo({top:H,behavior:f}):u.scrollTop=H})(Math.max(0,w),k);const F=performance.now();let T=0;const j=12,L=300,D=1,K=()=>{o.current=null;const H=i.current;if(!H)return;const f=Math.max(0,a()),S=Math.abs(H.scrollTop-f),v=performance.now()-F>=L,C=T>=j;S<=D||v||C||(typeof H.scrollTo=="function"?H.scrollTo({top:f,behavior:"auto"}):H.scrollTop=f,T+=1,o.current=requestAnimationFrame(K))};o.current=requestAnimationFrame(K)},[i,e,t,l])}function ce(i){var se;const{items:e,getKey:t,estimatedItemSize:s,overscan:l,initialAlignment:o}=i,n=r.useRef(null),y=r.useRef(null),u=r.useRef(null),d=r.useRef(new me),R=r.useRef([]),h=r.useRef(new Map),p=r.useRef(e.length),I=r.useRef(null),k=r.useRef(null),E=r.useRef(!1),a=r.useRef(0),w=r.useRef(0),g=r.useRef(null),F=r.useRef(!1),T=r.useRef(null),j=r.useRef(0),L=r.useRef(0),D=r.useRef(0),K=r.useRef(null),H=r.useRef(!1),f=r.useRef(!1),[,S]=r.useState(0),v=r.useCallback(()=>S(c=>c+1),[]);u.current||(u.current=new de(e.length,s));const C=e.length>0?t(e[0],0):null,P=e.length>0?t(e[e.length-1],e.length-1):null;if(e.length!==p.current||C!==I.current||P!==k.current){const c=u.current,m=e.map((O,W)=>t(O,W)),z=R.current,x=z.length,M=m.length;if(M===0)c.resize(0);else if(x===0)c.resize(M);else if(M>x){const O=M-x;z.length>0&&O>=0&&m[O]===z[0]?c.prepend(O):c.resize(M)}else M<x,c.resize(M);const A=new Map;m.forEach((O,W)=>A.set(O,W)),h.current=A,d.current.applyToOffsetMap(c,A),R.current=m,p.current=e.length,I.current=C,k.current=P}const b=r.useCallback(()=>{K.current===null&&(K.current=requestAnimationFrame(()=>{K.current=null;const c=n.current;if(!c)return;const m=j.current;Math.abs(m)<.01||(j.current=0,L.current+=m,D.current+=m,c.scrollTop+=m,a.current=c.scrollTop,H.current=!0,v())}))},[v]);r.useEffect(()=>{const c=n.current;if(!c)return;const m=()=>{g.current===null&&(g.current=requestAnimationFrame(()=>{g.current=null,a.current=c.scrollTop,w.current=c.clientHeight,H.current&&(H.current=!1,L.current=0,D.current=0),v()}))};return c.addEventListener("scroll",m,{passive:!0}),()=>{c.removeEventListener("scroll",m),g.current!==null&&(cancelAnimationFrame(g.current),g.current=null),K.current!==null&&(cancelAnimationFrame(K.current),K.current=null)}},[v]),r.useEffect(()=>{const c=n.current;if(!c)return;w.current=c.clientHeight,a.current=c.scrollTop;const m=new ResizeObserver(([z])=>{z&&(w.current=z.contentRect.height,v())});return m.observe(c),()=>m.disconnect()},[v]),r.useLayoutEffect(()=>{if(E.current||e.length===0)return;const c=n.current;if(c){if(o==="bottom"){c.scrollTop=c.scrollHeight,a.current=c.scrollTop,w.current=c.clientHeight,F.current=!0,T.current!==null&&cancelAnimationFrame(T.current);const m=performance.now();let z=0,x=c.scrollHeight;const M=()=>{if(!F.current){T.current=null;return}const A=c.scrollHeight,O=Math.abs(A-x);x=A,O<1?z+=1:z=0,c.scrollTop=c.scrollHeight,a.current=c.scrollTop;const W=performance.now()-m;if(z>=3||W>=500){F.current=!1,T.current=null;return}T.current=requestAnimationFrame(M)};T.current=requestAnimationFrame(M)}E.current=!0}},[o,e.length]),r.useEffect(()=>{e.length===0&&(E.current=!1,F.current=!1,T.current!==null&&(cancelAnimationFrame(T.current),T.current=null))},[e.length]);const B=r.useCallback((c,m)=>{var X;const z=u.current;if(!z||d.current.get(c)===m)return;d.current.set(c,m);const M=h.current.get(c);if(M===void 0)return;const A=z.getSize(M),O=m-A,W=n.current;W&&!F.current&&!f.current&&O!==0&&z.getOffset(M)+A+(((X=y.current)==null?void 0:X.offsetTop)??0)<W.scrollTop&&(j.current+=O,b()),z.setSize(M,m)&&v()},[b,v]),V=r.useCallback((c,m="auto")=>{var z;(z=n.current)==null||z.scrollTo({top:c,behavior:m})},[]),Y=Re(n,u,y,{reconcile:!0}),N=r.useCallback(()=>{var Z;const c=n.current,m=u.current;if(!c||!m||m.count===0)return null;const z=((Z=y.current)==null?void 0:Z.offsetTop)??0,x=Math.max(0,c.scrollTop-z),M=m.getOffsets(),A=oe(M,x),O=R.current[A]??null,W=[];for(let X=A;X<Math.min(m.count,A+6);X++){const ee=R.current[X]??null;ee!==null&&W.push({key:ee,offsetWithinItem:x-m.getOffset(X)})}return{key:O,offsetWithinItem:x-m.getOffset(A),candidates:W,scrollTop:c.scrollTop,scrollHeight:c.scrollHeight}},[]),$=r.useCallback((c,m)=>{var A;const z=u.current;if(!z)return null;const x=h.current.get(c);return x===void 0?null:(((A=y.current)==null?void 0:A.offsetTop)??0)+z.getOffset(x)+m},[]),_=u.current,ue=_?_.totalSize():0,J=n.current,G=(J==null?void 0:J.scrollTop)??a.current,re=(J==null?void 0:J.clientHeight)??w.current,ae=((se=y.current)==null?void 0:se.offsetTop)??0,ne=Math.max(0,G-ae),Q=[];if(_&&_.count>0&&re>0){const c=_.getOffsets(),m=_.getSizes(),z=oe(c,ne),x=pe(c,m,ne+re),M=ge({firstVisible:z,lastVisible:x,itemCount:_.count,overscan:l});for(let A=M.start;A<=M.end&&A<e.length;A++)Q.push({key:R.current[A]??t(e[A],A),index:A,start:_.getOffset(A),size:_.getSize(A),data:e[A]})}else if(_&&_.count>0){const c=Math.min(e.length,l*2+1),m=o==="bottom"?Math.max(0,e.length-c):0,z=m+c-1;for(let x=m;x<=z;x++)Q.push({key:R.current[x]??t(e[x],x),index:x,start:_.getOffset(x),size:_.getSize(x),data:e[x]})}const fe=J?J.scrollHeight-J.scrollTop-J.clientHeight:1/0;return{scrollerRef:n,innerRef:y,virtualItems:Q,totalSize:ue,measureItem:B,scrollToIndex:Y,scrollToOffset:V,captureAnchorSnapshot:N,resolveAnchorTop:$,isAtTop:G<=1,isAtBottom:fe<=1,scrollTop:G,jumpSuppressedRef:f}}function ve(i){var l;const{snapshot:e,currentScrollHeight:t,resolveAnchorTop:s}=i;if(e.key!==null){const o=s(e.key,e.offsetWithinItem);if(o!==null)return o}if((l=e.candidates)!=null&&l.length)for(const o of e.candidates){const n=s(o.key,o.offsetWithinItem);if(n!==null)return n}return e.scrollTop+(t-e.scrollHeight)}function ye(i){const{scrollerRef:e,itemCount:t,captureAnchorSnapshot:s,resolveAnchorTop:l,onRestored:o}=i,n=r.useRef(null),y=r.useRef(!1),u=r.useRef({first:null,second:null,timeout:null}),d=r.useCallback(()=>{const{first:h,second:p,timeout:I}=u.current;h&&cancelAnimationFrame(h),p&&cancelAnimationFrame(p),I&&clearTimeout(I),u.current={first:null,second:null,timeout:null}},[]),R=r.useCallback(()=>{const h=e.current;if(!h)return;const p=s();n.current=p??{key:null,offsetWithinItem:0,scrollTop:h.scrollTop,scrollHeight:h.scrollHeight},y.current=!0},[e,s]);return r.useLayoutEffect(()=>{if(!y.current)return;const h=e.current,p=n.current;if(!h||!p)return;y.current=!1;const I=()=>{const k=ve({snapshot:p,currentScrollHeight:h.scrollHeight,resolveAnchorTop:l});Number.isFinite(k)&&Math.abs(h.scrollTop-k)>1&&(h.scrollTop=k)};return d(),I(),o==null||o(),u.current.first=requestAnimationFrame(()=>{u.current.first=null,I(),u.current.second=requestAnimationFrame(()=>{u.current.second=null,I()})}),u.current.timeout=setTimeout(()=>{u.current.timeout=null,I()},90),()=>d()},[t,e,l,d,o]),{prepareAnchor:R}}function Te(i){const{previous:e,distanceFromBottom:t,threshold:s,hysteresis:l}=i;if(!l)return t<=s;const o=Math.max(0,l.enter),n=Math.max(o,l.leave);return e?t<=n:t<=o}function Se(i,e){const t=typeof e=="number"?e:e.threshold??200,s=typeof e=="number"?void 0:e.hysteresis,[l,o]=r.useState(!0),n=r.useRef(null),y=r.useRef(!0);return r.useEffect(()=>{const u=i.current;if(!u)return;const d=()=>{const h=u.scrollHeight-u.scrollTop-u.clientHeight,p=Te({previous:y.current,distanceFromBottom:h,threshold:t,hysteresis:s});y.current=p,o(p)},R=()=>{n.current!==null&&cancelAnimationFrame(n.current),n.current=requestAnimationFrame(d)};return u.addEventListener("scroll",R,{passive:!0}),d(),()=>{u.removeEventListener("scroll",R),n.current!==null&&cancelAnimationFrame(n.current)}},[i,t,s==null?void 0:s.enter,s==null?void 0:s.leave]),l}function be(i){const{itemCount:e,firstKey:t,lastKey:s,isAtBottom:l,scrollToIndex:o,mode:n}=i,y=r.useRef(e),u=r.useRef(t),d=r.useRef(s);r.useLayoutEffect(()=>{if(!n){y.current=e,u.current=t,d.current=s;return}const R=y.current,h=u.current,p=d.current;e>R&&t===h&&s!==p&&l&&e>0&&o(e-1,{align:"end",behavior:n==="smooth"?"smooth":"auto"}),y.current=e,u.current=t,d.current=s},[e,t,s,l,o,n])}function le(i){const{items:e,getKey:t,estimatedItemSize:s=80,overscan:l=20,atBottomThreshold:o=200,atBottomHysteresis:n,followOutput:y="auto",initialAlignment:u="bottom",scrollModifier:d=null,onStartReached:R,onEndReached:h,startReachedThreshold:p=300,endReachedThreshold:I=300,scrollToMessageKey:k,onScrollToMessageComplete:E}=i,a=ce({items:e,getKey:t,estimatedItemSize:s,overscan:l,initialAlignment:u}),w=Se(a.scrollerRef,{threshold:o,hysteresis:n??{enter:80,leave:160}}),[,g]=r.useState(0),F=r.useRef(null),T=r.useCallback(()=>{g(b=>b+1),F.current&&clearTimeout(F.current),F.current=setTimeout(()=>{a.jumpSuppressedRef.current=!1,F.current=null},200)},[a.jumpSuppressedRef]),{prepareAnchor:j}=ye({scrollerRef:a.scrollerRef,itemCount:e.length,captureAnchorSnapshot:a.captureAnchorSnapshot,resolveAnchorTop:a.resolveAnchorTop,onRestored:T}),L=r.useCallback(()=>{a.jumpSuppressedRef.current=!0,j()},[j,a.jumpSuppressedRef]),D=e.length>0?t(e[0],0):null,K=e.length>0?t(e[e.length-1],e.length-1):null;be({itemCount:e.length,firstKey:D,lastKey:K,isAtBottom:w,scrollToIndex:a.scrollToIndex,mode:y??!1});const H=r.useCallback((b="auto")=>{e.length!==0&&a.scrollToIndex(e.length-1,{align:"end",behavior:b})},[e.length,a]),f=r.useCallback((b,B)=>{const V=e.findIndex((Y,N)=>t(Y,N)===b);V!==-1&&a.scrollToIndex(V,B)},[e,t,a]),S=r.useRef(null);r.useLayoutEffect(()=>{if(d&&S.current!==d.id){if(S.current=d.id,d.type==="prepend"){L();return}if(d.type==="append"){if(d.ifAtBottomOnly&&!w)return;H(d.behavior??"auto");return}if(d.type==="items-change"){w&&H("auto");return}f(d.key,{align:d.align??"center",behavior:d.behavior??"auto"})}},[d,w,L,H,f]);const v=r.useRef(!1),C=r.useRef(u==="top");r.useEffect(()=>{const b=a.scrollerRef.current;if(!b||!R)return;const B=()=>{const V=b.scrollTop;C.current||(b.scrollHeight<=b.clientHeight+p||V>p)&&(C.current=!0),C.current&&V<=p&&!v.current&&(v.current=!0,Promise.resolve(R()).finally(()=>{v.current=!1}))};return b.addEventListener("scroll",B,{passive:!0}),B(),()=>b.removeEventListener("scroll",B)},[a.scrollerRef,R,p,u]);const P=r.useRef(!1);r.useEffect(()=>{const b=a.scrollerRef.current;if(!b||!h)return;const B=()=>{b.scrollHeight-b.scrollTop-b.clientHeight<=I&&!P.current&&(P.current=!0,Promise.resolve(h()).finally(()=>{P.current=!1}))};return b.addEventListener("scroll",B,{passive:!0}),()=>b.removeEventListener("scroll",B)},[a.scrollerRef,h,I]);const U=r.useRef(null);return r.useEffect(()=>{if(!k||U.current===k)return;const b=e.findIndex((B,V)=>t(B,V)===k);b!==-1&&(U.current=k,a.scrollToIndex(b,{align:"center",behavior:"auto"}),E==null||E())},[k,e,t,a,E]),{scrollerRef:a.scrollerRef,innerRef:a.innerRef,virtualItems:a.virtualItems,totalSize:a.totalSize,measureItem:a.measureItem,scrollToIndex:a.scrollToIndex,scrollToBottom:H,scrollToKey:f,isAtBottom:w,prepareAnchor:L}}function ie({virtualItem:i,measureItem:e,children:t}){const s=r.useRef(null),l=r.useRef(!1);return r.useEffect(()=>{const o=s.current;if(!o)return;l.current=!1;const n=new ResizeObserver(([y])=>{y&&(l.current=!0,e(i.key,y.contentRect.height))});return n.observe(o),()=>n.disconnect()},[i.key,e]),q.jsx("div",{ref:s,style:{position:"absolute",top:0,transform:`translateY(${i.start}px)`,width:"100%",minHeight:l.current?void 0:i.size},children:t})}function Ae(i,e){const{data:t,itemContent:s,computeItemKey:l,estimatedItemSize:o=80,overscan:n=20,followOutput:y="auto",atBottomThreshold:u=200,atBottomHysteresis:d,initialAlignment:R="bottom",scrollModifier:h,onStartReached:p,onEndReached:I,startReachedThreshold:k=300,endReachedThreshold:E=300,scrollToMessageKey:a,onScrollToMessageComplete:w,onAtBottomChange:g,components:F={},className:T,style:j}=i,{scrollerRef:L,innerRef:D,virtualItems:K,totalSize:H,measureItem:f,scrollToIndex:S,scrollToBottom:v,scrollToKey:C,isAtBottom:P,prepareAnchor:U}=le({items:t,getKey:(N,$)=>l($,N),estimatedItemSize:o,overscan:n,atBottomThreshold:u,atBottomHysteresis:d,followOutput:y,initialAlignment:R,scrollModifier:h,onStartReached:p,onEndReached:I,startReachedThreshold:k,endReachedThreshold:E,scrollToMessageKey:a,onScrollToMessageComplete:w}),b=te.useRef(P);te.useEffect(()=>{b.current!==P&&(b.current=P,g==null||g(P))},[P,g]),r.useImperativeHandle(e,()=>({scrollToBottom:v,scrollToIndex:S,scrollToKey:C,getScrollTop:()=>{var N;return((N=L.current)==null?void 0:N.scrollTop)??0},isAtBottom:()=>P,prepareAnchor:U}),[v,S,C,L,P,U]);const{Header:B,Footer:V,EmptyPlaceholder:Y}=F;return t.length===0&&Y?q.jsx(Y,{}):q.jsxs("div",{ref:L,className:T,style:{overflow:"auto",height:"100%",position:"relative",overflowAnchor:"none",overscrollBehaviorY:"contain",...j},children:[B&&q.jsx(B,{}),q.jsx("div",{ref:D,style:{height:H,position:"relative",width:"100%"},children:K.map(N=>q.jsx(ie,{virtualItem:N,measureItem:f,children:s(N.index,N.data)},N.key))}),V&&q.jsx(V,{})]})}const ze=r.forwardRef(Ae);function xe({data:i,itemContent:e,computeItemKey:t,estimatedItemSize:s=60,overscan:l=20,onEndReached:o,endReachedThreshold:n=300,components:y={},className:u,style:d}){const{scrollerRef:R,innerRef:h,virtualItems:p,totalSize:I,measureItem:k}=ce({items:i,getKey:(g,F)=>t(F,g),estimatedItemSize:s,overscan:l,initialAlignment:"top"});te.useEffect(()=>{const g=R.current;if(!g||!o)return;let F=!1;const T=()=>{g.scrollHeight-g.scrollTop-g.clientHeight<=n&&!F&&(F=!0,Promise.resolve(o()).finally(()=>{F=!1}))};return g.addEventListener("scroll",T,{passive:!0}),()=>g.removeEventListener("scroll",T)},[R,o,n]);const{Header:E,Footer:a,EmptyPlaceholder:w}=y;return i.length===0&&w?q.jsx(w,{}):q.jsxs("div",{ref:R,className:u,style:{overflow:"auto",height:"100%",position:"relative",overflowAnchor:"none",overscrollBehaviorY:"contain",...d},children:[E&&q.jsx(E,{}),q.jsx("div",{ref:h,style:{height:I,position:"relative",width:"100%"},children:p.map(g=>q.jsx(ie,{virtualItem:g,measureItem:k,children:e(g.index,g.data)},g.key))}),a&&q.jsx(a,{})]})}function Ie(i){const{fetcher:e,initialPage:t=1,direction:s="append",getKey:l,onPageLoaded:o,onError:n}=i,[y,u]=r.useState([]),[d,R]=r.useState(t),[h,p]=r.useState(!0),[I,k]=r.useState(!1),[E,a]=r.useState(!1),[w,g]=r.useState(!1),F=r.useRef(new Set),T=r.useRef(!1),j=r.useCallback(f=>l?f.filter(S=>{const v=l(S);return F.current.has(v)?!1:(F.current.add(v),!0)}):f,[l]),L=r.useCallback(async()=>{if(!(T.current||!h)){T.current=!0,g(!0);try{const f=d+1,S=await e(f),v=j(S.data);u(C=>s==="prepend"?[...v,...C]:[...C,...v]),R(f),p(S.hasNextPage),k(S.hasPrevPage),o==null||o(f,v)}catch(f){n==null||n(f instanceof Error?f:new Error(String(f)))}finally{g(!1),T.current=!1}}},[d,h,e,j,s,o,n]),D=r.useCallback(async()=>{if(!(T.current||!I)){T.current=!0,g(!0);try{const f=d-1,S=await e(f),v=j(S.data);u(C=>[...v,...C]),R(f),k(S.hasPrevPage),p(S.hasNextPage),o==null||o(f,v)}catch(f){n==null||n(f instanceof Error?f:new Error(String(f)))}finally{g(!1),T.current=!1}}},[d,I,e,j,o,n]),K=r.useCallback(async()=>{if(!T.current){T.current=!0,a(!0);try{const f=await e(t),S=f.data;if(l){const v=new Set(S.map(l));S.forEach(C=>F.current.add(l(C))),u(C=>{const P=C.filter(U=>!v.has(l(U)));return s==="prepend"?[...S,...P]:[...P,...S]})}else u(S);R(t),p(f.hasNextPage),k(f.hasPrevPage),o==null||o(t,S)}catch(f){n==null||n(f instanceof Error?f:new Error(String(f)))}finally{a(!1),T.current=!1}}},[e,t,l,s,o,n]),H=r.useCallback(()=>{u([]),R(t),p(!0),k(!1),a(!1),g(!1),F.current.clear(),T.current=!1},[t]);return{items:y,loadNextPage:L,loadPrevPage:D,hasNextPage:h,hasPrevPage:I,loading:E,loadingMore:w,refresh:K,reset:H,currentPage:d}}exports.ChatVirtualList=ze;exports.VirtualList=xe;exports.useChatVirtualizer=le;exports.usePagination=Ie;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const q=require("react/jsx-runtime"),r=require("react");function he(i){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(i){for(const t in i)if(t!=="default"){const s=Object.getOwnPropertyDescriptor(i,t);Object.defineProperty(e,t,s.get?s:{enumerable:!0,get:()=>i[t]})}}return e.default=i,Object.freeze(e)}const te=he(r);class de{constructor(e,t){this.defaultSize=t,this.sizes=e>0?Array(e).fill(t):[],this.offsets=e>0?Array(e).fill(0):[],e>0&&this._recalcFrom(0)}_recalcFrom(e){for(let t=e;t<this.sizes.length;t++)this.offsets[t]=t===0?0:(this.offsets[t-1]??0)+(this.sizes[t-1]??this.defaultSize)}getOffset(e){return this.offsets[e]??0}getSize(e){return this.sizes[e]??this.defaultSize}setSize(e,t){return this.sizes[e]===t?!1:(this.sizes[e]=t,this._recalcFrom(e+1),!0)}prepend(e){const t=Array(e).fill(this.defaultSize);this.sizes=[...t,...this.sizes],this.offsets=Array(this.sizes.length).fill(0),this._recalcFrom(0)}append(e){const t=this.sizes.length;for(let s=0;s<e;s++)this.sizes.push(this.defaultSize),this.offsets.push(0);this._recalcFrom(t)}resize(e){const t=this.sizes.length;e>t?this.append(e-t):e<t&&(this.sizes=this.sizes.slice(0,e),this.offsets=this.offsets.slice(0,e))}totalSize(){if(this.sizes.length===0)return 0;const e=this.sizes.length-1;return(this.offsets[e]??0)+(this.sizes[e]??this.defaultSize)}get count(){return this.sizes.length}getOffsets(){return this.offsets}getSizes(){return this.sizes}}class me{constructor(){this.cache=new Map}get(e){return this.cache.get(e)}set(e,t){this.cache.set(e,t)}has(e){return this.cache.has(e)}delete(e){this.cache.delete(e)}clear(){this.cache.clear()}applyToOffsetMap(e,t){for(const[s,l]of this.cache){const o=t.get(s);o!==void 0&&e.setSize(o,l)}}}function oe(i,e){if(i.length===0)return 0;let t=0,s=i.length-1;for(;t<s;){const l=t+s>>1;(i[l]??0)<e?t=l+1:s=l}return Math.max(0,t>0&&(i[t]??0)>e?t-1:t)}function pe(i,e,t){if(i.length===0)return 0;let s=0,l=i.length-1,o=0;for(;s<=l;){const n=s+l>>1;(i[n]??0)<t?(o=n,s=n+1):l=n-1}return o}function ge(i){const{firstVisible:e,lastVisible:t,itemCount:s,overscan:l}=i;return s===0?{start:0,end:-1}:{start:Math.max(0,e-l),end:Math.min(s-1,t+l)}}function Re(i,e,t,s){const l=s==null?void 0:s.reconcile,o=r.useRef(null);return r.useCallback((n,y)=>{const u=i.current,d=e.current;if(!u||!d)return;o.current!==null&&(cancelAnimationFrame(o.current),o.current=null);let R,h;if(typeof n=="object"&&n!==null?(R=n.index,h={align:n.align,behavior:n.behavior,offset:n.offset}):(R=n,h=y),!Number.isFinite(R))return;const p=Math.max(0,Math.min(Math.floor(R),d.count-1));d.getOffset(p),d.getSize(p);const I=(h==null?void 0:h.align)??"start",k=(h==null?void 0:h.behavior)??"auto",E=(h==null?void 0:h.offset)??0,a=()=>{var P;const H=e.current,f=i.current;if(!H||!f)return 0;const S=((P=t==null?void 0:t.current)==null?void 0:P.offsetTop)??0,v=H.getOffset(p),C=H.getSize(p);return I==="start"?S+v+E:I==="center"?S+v-f.clientHeight/2+C/2+E:S+v-f.clientHeight+C+E},w=a();((H,f)=>{typeof u.scrollTo=="function"?u.scrollTo({top:H,behavior:f}):u.scrollTop=H})(Math.max(0,w),k);const F=performance.now();let T=0;const j=12,L=300,D=1,K=()=>{o.current=null;const H=i.current;if(!H)return;const f=Math.max(0,a()),S=Math.abs(H.scrollTop-f),v=performance.now()-F>=L,C=T>=j;S<=D||v||C||(typeof H.scrollTo=="function"?H.scrollTo({top:f,behavior:"auto"}):H.scrollTop=f,T+=1,o.current=requestAnimationFrame(K))};o.current=requestAnimationFrame(K)},[i,e,t,l])}function ce(i){var se;const{items:e,getKey:t,estimatedItemSize:s,overscan:l,initialAlignment:o}=i,n=r.useRef(null),y=r.useRef(null),u=r.useRef(null),d=r.useRef(new me),R=r.useRef([]),h=r.useRef(new Map),p=r.useRef(e.length),I=r.useRef(null),k=r.useRef(null),E=r.useRef(!1),a=r.useRef(0),w=r.useRef(0),g=r.useRef(null),F=r.useRef(!1),T=r.useRef(null),j=r.useRef(0),L=r.useRef(0),D=r.useRef(0),K=r.useRef(null),H=r.useRef(!1),f=r.useRef(!1),[,S]=r.useState(0),v=r.useCallback(()=>S(c=>c+1),[]);u.current||(u.current=new de(e.length,s));const C=e.length>0?t(e[0],0):null,P=e.length>0?t(e[e.length-1],e.length-1):null;if(e.length!==p.current||C!==I.current||P!==k.current){const c=u.current,m=e.map((O,W)=>t(O,W)),x=R.current,b=x.length,M=m.length;if(M===0)c.resize(0);else if(b===0)c.resize(M);else if(M>b){const O=M-b;x.length>0&&O>=0&&m[O]===x[0]?c.prepend(O):c.resize(M)}else M<b,c.resize(M);const z=new Map;m.forEach((O,W)=>z.set(O,W)),h.current=z,d.current.applyToOffsetMap(c,z),R.current=m,p.current=e.length,I.current=C,k.current=P}const A=r.useCallback(()=>{K.current===null&&(K.current=requestAnimationFrame(()=>{K.current=null;const c=n.current;if(!c)return;const m=j.current;Math.abs(m)<.01||(j.current=0,L.current+=m,D.current+=m,c.scrollTop+=m,a.current=c.scrollTop,H.current=!0,v())}))},[v]);r.useEffect(()=>{const c=n.current;if(!c)return;const m=()=>{g.current===null&&(g.current=requestAnimationFrame(()=>{g.current=null,a.current=c.scrollTop,w.current=c.clientHeight,H.current&&(H.current=!1,L.current=0,D.current=0),v()}))};return c.addEventListener("scroll",m,{passive:!0}),()=>{c.removeEventListener("scroll",m),g.current!==null&&(cancelAnimationFrame(g.current),g.current=null),K.current!==null&&(cancelAnimationFrame(K.current),K.current=null)}},[v]),r.useEffect(()=>{const c=n.current;if(!c)return;w.current=c.clientHeight,a.current=c.scrollTop;const m=new ResizeObserver(([x])=>{x&&(w.current=x.contentRect.height,v())});return m.observe(c),()=>m.disconnect()},[v]),r.useLayoutEffect(()=>{if(E.current||e.length===0)return;const c=n.current;if(c){if(o==="bottom"){c.scrollTop=c.scrollHeight,a.current=c.scrollTop,w.current=c.clientHeight,F.current=!0,T.current!==null&&cancelAnimationFrame(T.current);const m=performance.now();let x=0,b=c.scrollHeight;const M=()=>{if(!F.current){T.current=null;return}const z=c.scrollHeight,O=Math.abs(z-b);b=z,O<1?x+=1:x=0,c.scrollTop=c.scrollHeight,a.current=c.scrollTop;const W=performance.now()-m;if(x>=3||W>=500){F.current=!1,T.current=null;return}T.current=requestAnimationFrame(M)};T.current=requestAnimationFrame(M)}E.current=!0}},[o,e.length]),r.useEffect(()=>{e.length===0&&(E.current=!1,F.current=!1,T.current!==null&&(cancelAnimationFrame(T.current),T.current=null))},[e.length]);const B=r.useCallback((c,m)=>{var X;const x=u.current;if(!x)return;const b=d.current.get(c);if(b===m)return;d.current.set(c,m);const M=h.current.get(c);if(M===void 0)return;const z=x.getSize(M),O=m-z,W=n.current;W&&b!==void 0&&!F.current&&!f.current&&O!==0&&x.getOffset(M)+z+(((X=y.current)==null?void 0:X.offsetTop)??0)<W.scrollTop&&(j.current+=O,A()),x.setSize(M,m)&&v()},[A,v]),V=r.useCallback((c,m="auto")=>{var x;(x=n.current)==null||x.scrollTo({top:c,behavior:m})},[]),Y=Re(n,u,y,{reconcile:!0}),N=r.useCallback(()=>{var Z;const c=n.current,m=u.current;if(!c||!m||m.count===0)return null;const x=((Z=y.current)==null?void 0:Z.offsetTop)??0,b=Math.max(0,c.scrollTop-x),M=m.getOffsets(),z=oe(M,b),O=R.current[z]??null,W=[];for(let X=z;X<Math.min(m.count,z+6);X++){const ee=R.current[X]??null;ee!==null&&W.push({key:ee,offsetWithinItem:b-m.getOffset(X)})}return{key:O,offsetWithinItem:b-m.getOffset(z),candidates:W,scrollTop:c.scrollTop,scrollHeight:c.scrollHeight}},[]),$=r.useCallback((c,m)=>{var z;const x=u.current;if(!x)return null;const b=h.current.get(c);return b===void 0?null:(((z=y.current)==null?void 0:z.offsetTop)??0)+x.getOffset(b)+m},[]),_=u.current,ue=_?_.totalSize():0,J=n.current,G=(J==null?void 0:J.scrollTop)??a.current,re=(J==null?void 0:J.clientHeight)??w.current,ae=((se=y.current)==null?void 0:se.offsetTop)??0,ne=Math.max(0,G-ae),Q=[];if(_&&_.count>0&&re>0){const c=_.getOffsets(),m=_.getSizes(),x=oe(c,ne),b=pe(c,m,ne+re),M=ge({firstVisible:x,lastVisible:b,itemCount:_.count,overscan:l});for(let z=M.start;z<=M.end&&z<e.length;z++)Q.push({key:R.current[z]??t(e[z],z),index:z,start:_.getOffset(z),size:_.getSize(z),data:e[z]})}else if(_&&_.count>0){const c=Math.min(e.length,l*2+1),m=o==="bottom"?Math.max(0,e.length-c):0,x=m+c-1;for(let b=m;b<=x;b++)Q.push({key:R.current[b]??t(e[b],b),index:b,start:_.getOffset(b),size:_.getSize(b),data:e[b]})}const fe=J?J.scrollHeight-J.scrollTop-J.clientHeight:1/0;return{scrollerRef:n,innerRef:y,virtualItems:Q,totalSize:ue,measureItem:B,scrollToIndex:Y,scrollToOffset:V,captureAnchorSnapshot:N,resolveAnchorTop:$,isAtTop:G<=1,isAtBottom:fe<=1,scrollTop:G,jumpSuppressedRef:f}}function ve(i){var l;const{snapshot:e,currentScrollHeight:t,resolveAnchorTop:s}=i;if(e.key!==null){const o=s(e.key,e.offsetWithinItem);if(o!==null)return o}if((l=e.candidates)!=null&&l.length)for(const o of e.candidates){const n=s(o.key,o.offsetWithinItem);if(n!==null)return n}return e.scrollTop+(t-e.scrollHeight)}function ye(i){const{scrollerRef:e,itemCount:t,captureAnchorSnapshot:s,resolveAnchorTop:l,onRestored:o}=i,n=r.useRef(null),y=r.useRef(!1),u=r.useRef({first:null,second:null,timeout:null}),d=r.useCallback(()=>{const{first:h,second:p,timeout:I}=u.current;h&&cancelAnimationFrame(h),p&&cancelAnimationFrame(p),I&&clearTimeout(I),u.current={first:null,second:null,timeout:null}},[]),R=r.useCallback(()=>{const h=e.current;if(!h)return;const p=s();n.current=p??{key:null,offsetWithinItem:0,scrollTop:h.scrollTop,scrollHeight:h.scrollHeight},y.current=!0},[e,s]);return r.useLayoutEffect(()=>{if(!y.current)return;const h=e.current,p=n.current;if(!h||!p)return;y.current=!1;const I=()=>{const k=ve({snapshot:p,currentScrollHeight:h.scrollHeight,resolveAnchorTop:l});Number.isFinite(k)&&Math.abs(h.scrollTop-k)>1&&(h.scrollTop=k)};return d(),I(),o==null||o(),u.current.first=requestAnimationFrame(()=>{u.current.first=null,I(),u.current.second=requestAnimationFrame(()=>{u.current.second=null,I()})}),u.current.timeout=setTimeout(()=>{u.current.timeout=null,I()},90),()=>d()},[t,e,l,d,o]),{prepareAnchor:R}}function Te(i){const{previous:e,distanceFromBottom:t,threshold:s,hysteresis:l}=i;if(!l)return t<=s;const o=Math.max(0,l.enter),n=Math.max(o,l.leave);return e?t<=n:t<=o}function Se(i,e){const t=typeof e=="number"?e:e.threshold??200,s=typeof e=="number"?void 0:e.hysteresis,[l,o]=r.useState(!0),n=r.useRef(null),y=r.useRef(!0);return r.useEffect(()=>{const u=i.current;if(!u)return;const d=()=>{const h=u.scrollHeight-u.scrollTop-u.clientHeight,p=Te({previous:y.current,distanceFromBottom:h,threshold:t,hysteresis:s});y.current=p,o(p)},R=()=>{n.current!==null&&cancelAnimationFrame(n.current),n.current=requestAnimationFrame(d)};return u.addEventListener("scroll",R,{passive:!0}),d(),()=>{u.removeEventListener("scroll",R),n.current!==null&&cancelAnimationFrame(n.current)}},[i,t,s==null?void 0:s.enter,s==null?void 0:s.leave]),l}function be(i){const{itemCount:e,firstKey:t,lastKey:s,isAtBottom:l,scrollToIndex:o,mode:n}=i,y=r.useRef(e),u=r.useRef(t),d=r.useRef(s);r.useLayoutEffect(()=>{if(!n){y.current=e,u.current=t,d.current=s;return}const R=y.current,h=u.current,p=d.current;e>R&&t===h&&s!==p&&l&&e>0&&o(e-1,{align:"end",behavior:n==="smooth"?"smooth":"auto"}),y.current=e,u.current=t,d.current=s},[e,t,s,l,o,n])}function le(i){const{items:e,getKey:t,estimatedItemSize:s=80,overscan:l=20,atBottomThreshold:o=200,atBottomHysteresis:n,followOutput:y="auto",initialAlignment:u="bottom",scrollModifier:d=null,onStartReached:R,onEndReached:h,startReachedThreshold:p=300,endReachedThreshold:I=300,scrollToMessageKey:k,onScrollToMessageComplete:E}=i,a=ce({items:e,getKey:t,estimatedItemSize:s,overscan:l,initialAlignment:u}),w=Se(a.scrollerRef,{threshold:o,hysteresis:n??{enter:80,leave:160}}),[,g]=r.useState(0),F=r.useRef(null),T=r.useCallback(()=>{g(A=>A+1),F.current&&clearTimeout(F.current),F.current=setTimeout(()=>{a.jumpSuppressedRef.current=!1,F.current=null},200)},[a.jumpSuppressedRef]),{prepareAnchor:j}=ye({scrollerRef:a.scrollerRef,itemCount:e.length,captureAnchorSnapshot:a.captureAnchorSnapshot,resolveAnchorTop:a.resolveAnchorTop,onRestored:T}),L=r.useCallback(()=>{a.jumpSuppressedRef.current=!0,j()},[j,a.jumpSuppressedRef]),D=e.length>0?t(e[0],0):null,K=e.length>0?t(e[e.length-1],e.length-1):null;be({itemCount:e.length,firstKey:D,lastKey:K,isAtBottom:w,scrollToIndex:a.scrollToIndex,mode:y??!1});const H=r.useCallback((A="auto")=>{e.length!==0&&a.scrollToIndex(e.length-1,{align:"end",behavior:A})},[e.length,a]),f=r.useCallback((A,B)=>{const V=e.findIndex((Y,N)=>t(Y,N)===A);V!==-1&&a.scrollToIndex(V,B)},[e,t,a]),S=r.useRef(null);r.useLayoutEffect(()=>{if(d&&S.current!==d.id){if(S.current=d.id,d.type==="prepend"){L();return}if(d.type==="append"){if(d.ifAtBottomOnly&&!w)return;H(d.behavior??"auto");return}if(d.type==="items-change"){w&&H("auto");return}f(d.key,{align:d.align??"center",behavior:d.behavior??"auto"})}},[d,w,L,H,f]);const v=r.useRef(!1),C=r.useRef(u==="top");r.useEffect(()=>{const A=a.scrollerRef.current;if(!A||!R)return;const B=()=>{const V=A.scrollTop;C.current||(A.scrollHeight<=A.clientHeight+p||V>p)&&(C.current=!0),C.current&&V<=p&&!v.current&&(v.current=!0,Promise.resolve(R()).finally(()=>{v.current=!1}))};return A.addEventListener("scroll",B,{passive:!0}),B(),()=>A.removeEventListener("scroll",B)},[a.scrollerRef,R,p,u]);const P=r.useRef(!1);r.useEffect(()=>{const A=a.scrollerRef.current;if(!A||!h)return;const B=()=>{A.scrollHeight-A.scrollTop-A.clientHeight<=I&&!P.current&&(P.current=!0,Promise.resolve(h()).finally(()=>{P.current=!1}))};return A.addEventListener("scroll",B,{passive:!0}),()=>A.removeEventListener("scroll",B)},[a.scrollerRef,h,I]);const U=r.useRef(null);return r.useEffect(()=>{if(!k||U.current===k)return;const A=e.findIndex((B,V)=>t(B,V)===k);A!==-1&&(U.current=k,a.scrollToIndex(A,{align:"center",behavior:"auto"}),E==null||E())},[k,e,t,a,E]),{scrollerRef:a.scrollerRef,innerRef:a.innerRef,virtualItems:a.virtualItems,totalSize:a.totalSize,measureItem:a.measureItem,scrollToIndex:a.scrollToIndex,scrollToBottom:H,scrollToKey:f,isAtBottom:w,prepareAnchor:L}}function ie({virtualItem:i,measureItem:e,children:t}){const s=r.useRef(null),l=r.useRef(!1);return r.useEffect(()=>{const o=s.current;if(!o)return;l.current=!1;const n=new ResizeObserver(([y])=>{y&&(l.current=!0,e(i.key,y.contentRect.height))});return n.observe(o),()=>n.disconnect()},[i.key,e]),q.jsx("div",{ref:s,style:{position:"absolute",top:0,transform:`translateY(${i.start}px)`,width:"100%",minHeight:l.current?void 0:i.size},children:t})}function Ae(i,e){const{data:t,itemContent:s,computeItemKey:l,estimatedItemSize:o=80,overscan:n=20,followOutput:y="auto",atBottomThreshold:u=200,atBottomHysteresis:d,initialAlignment:R="bottom",scrollModifier:h,onStartReached:p,onEndReached:I,startReachedThreshold:k=300,endReachedThreshold:E=300,scrollToMessageKey:a,onScrollToMessageComplete:w,onAtBottomChange:g,components:F={},className:T,style:j}=i,{scrollerRef:L,innerRef:D,virtualItems:K,totalSize:H,measureItem:f,scrollToIndex:S,scrollToBottom:v,scrollToKey:C,isAtBottom:P,prepareAnchor:U}=le({items:t,getKey:(N,$)=>l($,N),estimatedItemSize:o,overscan:n,atBottomThreshold:u,atBottomHysteresis:d,followOutput:y,initialAlignment:R,scrollModifier:h,onStartReached:p,onEndReached:I,startReachedThreshold:k,endReachedThreshold:E,scrollToMessageKey:a,onScrollToMessageComplete:w}),A=te.useRef(P);te.useEffect(()=>{A.current!==P&&(A.current=P,g==null||g(P))},[P,g]),r.useImperativeHandle(e,()=>({scrollToBottom:v,scrollToIndex:S,scrollToKey:C,getScrollTop:()=>{var N;return((N=L.current)==null?void 0:N.scrollTop)??0},isAtBottom:()=>P,prepareAnchor:U}),[v,S,C,L,P,U]);const{Header:B,Footer:V,EmptyPlaceholder:Y}=F;return t.length===0&&Y?q.jsx(Y,{}):q.jsxs("div",{ref:L,className:T,style:{overflow:"auto",height:"100%",position:"relative",overflowAnchor:"none",overscrollBehaviorY:"contain",...j},children:[B&&q.jsx(B,{}),q.jsx("div",{ref:D,style:{height:H,position:"relative",width:"100%"},children:K.map(N=>q.jsx(ie,{virtualItem:N,measureItem:f,children:s(N.index,N.data)},N.key))}),V&&q.jsx(V,{})]})}const ze=r.forwardRef(Ae);function xe({data:i,itemContent:e,computeItemKey:t,estimatedItemSize:s=60,overscan:l=20,onEndReached:o,endReachedThreshold:n=300,components:y={},className:u,style:d}){const{scrollerRef:R,innerRef:h,virtualItems:p,totalSize:I,measureItem:k}=ce({items:i,getKey:(g,F)=>t(F,g),estimatedItemSize:s,overscan:l,initialAlignment:"top"});te.useEffect(()=>{const g=R.current;if(!g||!o)return;let F=!1;const T=()=>{g.scrollHeight-g.scrollTop-g.clientHeight<=n&&!F&&(F=!0,Promise.resolve(o()).finally(()=>{F=!1}))};return g.addEventListener("scroll",T,{passive:!0}),()=>g.removeEventListener("scroll",T)},[R,o,n]);const{Header:E,Footer:a,EmptyPlaceholder:w}=y;return i.length===0&&w?q.jsx(w,{}):q.jsxs("div",{ref:R,className:u,style:{overflow:"auto",height:"100%",position:"relative",overflowAnchor:"none",overscrollBehaviorY:"contain",...d},children:[E&&q.jsx(E,{}),q.jsx("div",{ref:h,style:{height:I,position:"relative",width:"100%"},children:p.map(g=>q.jsx(ie,{virtualItem:g,measureItem:k,children:e(g.index,g.data)},g.key))}),a&&q.jsx(a,{})]})}function Ie(i){const{fetcher:e,initialPage:t=1,direction:s="append",getKey:l,onPageLoaded:o,onError:n}=i,[y,u]=r.useState([]),[d,R]=r.useState(t),[h,p]=r.useState(!0),[I,k]=r.useState(!1),[E,a]=r.useState(!1),[w,g]=r.useState(!1),F=r.useRef(new Set),T=r.useRef(!1),j=r.useCallback(f=>l?f.filter(S=>{const v=l(S);return F.current.has(v)?!1:(F.current.add(v),!0)}):f,[l]),L=r.useCallback(async()=>{if(!(T.current||!h)){T.current=!0,g(!0);try{const f=d+1,S=await e(f),v=j(S.data);u(C=>s==="prepend"?[...v,...C]:[...C,...v]),R(f),p(S.hasNextPage),k(S.hasPrevPage),o==null||o(f,v)}catch(f){n==null||n(f instanceof Error?f:new Error(String(f)))}finally{g(!1),T.current=!1}}},[d,h,e,j,s,o,n]),D=r.useCallback(async()=>{if(!(T.current||!I)){T.current=!0,g(!0);try{const f=d-1,S=await e(f),v=j(S.data);u(C=>[...v,...C]),R(f),k(S.hasPrevPage),p(S.hasNextPage),o==null||o(f,v)}catch(f){n==null||n(f instanceof Error?f:new Error(String(f)))}finally{g(!1),T.current=!1}}},[d,I,e,j,o,n]),K=r.useCallback(async()=>{if(!T.current){T.current=!0,a(!0);try{const f=await e(t),S=f.data;if(l){const v=new Set(S.map(l));S.forEach(C=>F.current.add(l(C))),u(C=>{const P=C.filter(U=>!v.has(l(U)));return s==="prepend"?[...S,...P]:[...P,...S]})}else u(S);R(t),p(f.hasNextPage),k(f.hasPrevPage),o==null||o(t,S)}catch(f){n==null||n(f instanceof Error?f:new Error(String(f)))}finally{a(!1),T.current=!1}}},[e,t,l,s,o,n]),H=r.useCallback(()=>{u([]),R(t),p(!0),k(!1),a(!1),g(!1),F.current.clear(),T.current=!1},[t]);return{items:y,loadNextPage:L,loadPrevPage:D,hasNextPage:h,hasPrevPage:I,loading:E,loadingMore:w,refresh:K,reset:H,currentPage:d}}exports.ChatVirtualList=ze;exports.VirtualList=xe;exports.useChatVirtualizer=le;exports.usePagination=Ie;
|
package/dist/index.js
CHANGED
|
@@ -100,7 +100,7 @@ function Ae(a) {
|
|
|
100
100
|
end: Math.min(s - 1, t + c)
|
|
101
101
|
};
|
|
102
102
|
}
|
|
103
|
-
function
|
|
103
|
+
function Re(a, e, t, s) {
|
|
104
104
|
const c = s == null ? void 0 : s.reconcile, n = m(null);
|
|
105
105
|
return k(
|
|
106
106
|
(r, y) => {
|
|
@@ -145,17 +145,17 @@ function fe(a) {
|
|
|
145
145
|
l.current || (l.current = new Te(e.length, s));
|
|
146
146
|
const w = e.length > 0 ? t(e[0], 0) : null, B = e.length > 0 ? t(e[e.length - 1], e.length - 1) : null;
|
|
147
147
|
if (e.length !== p.current || w !== F.current || B !== M.current) {
|
|
148
|
-
const o = l.current, d = e.map((V, W) => t(V, W)),
|
|
148
|
+
const o = l.current, d = e.map((V, W) => t(V, W)), I = v.current, R = I.length, P = d.length;
|
|
149
149
|
if (P === 0)
|
|
150
150
|
o.resize(0);
|
|
151
|
-
else if (
|
|
151
|
+
else if (R === 0)
|
|
152
152
|
o.resize(P);
|
|
153
|
-
else if (P >
|
|
154
|
-
const V = P -
|
|
155
|
-
|
|
156
|
-
} else P <
|
|
157
|
-
const
|
|
158
|
-
d.forEach((V, W) =>
|
|
153
|
+
else if (P > R) {
|
|
154
|
+
const V = P - R;
|
|
155
|
+
I.length > 0 && V >= 0 && d[V] === I[0] ? o.prepend(V) : o.resize(P);
|
|
156
|
+
} else P < R, o.resize(P);
|
|
157
|
+
const x = /* @__PURE__ */ new Map();
|
|
158
|
+
d.forEach((V, W) => x.set(V, W)), f.current = x, h.current.applyToOffsetMap(o, x), v.current = d, p.current = e.length, F.current = w, M.current = B;
|
|
159
159
|
}
|
|
160
160
|
const z = k(() => {
|
|
161
161
|
L.current === null && (L.current = requestAnimationFrame(() => {
|
|
@@ -181,8 +181,8 @@ function fe(a) {
|
|
|
181
181
|
const o = r.current;
|
|
182
182
|
if (!o) return;
|
|
183
183
|
E.current = o.clientHeight, i.current = o.scrollTop;
|
|
184
|
-
const d = new ResizeObserver(([
|
|
185
|
-
|
|
184
|
+
const d = new ResizeObserver(([I]) => {
|
|
185
|
+
I && (E.current = I.contentRect.height, T());
|
|
186
186
|
});
|
|
187
187
|
return d.observe(o), () => d.disconnect();
|
|
188
188
|
}, [T]), Z(() => {
|
|
@@ -192,16 +192,16 @@ function fe(a) {
|
|
|
192
192
|
if (n === "bottom") {
|
|
193
193
|
o.scrollTop = o.scrollHeight, i.current = o.scrollTop, E.current = o.clientHeight, b.current = !0, S.current !== null && cancelAnimationFrame(S.current);
|
|
194
194
|
const d = performance.now();
|
|
195
|
-
let
|
|
195
|
+
let I = 0, R = o.scrollHeight;
|
|
196
196
|
const P = () => {
|
|
197
197
|
if (!b.current) {
|
|
198
198
|
S.current = null;
|
|
199
199
|
return;
|
|
200
200
|
}
|
|
201
|
-
const
|
|
202
|
-
|
|
201
|
+
const x = o.scrollHeight, V = Math.abs(x - R);
|
|
202
|
+
R = x, V < 1 ? I += 1 : I = 0, o.scrollTop = o.scrollHeight, i.current = o.scrollTop;
|
|
203
203
|
const W = performance.now() - d;
|
|
204
|
-
if (
|
|
204
|
+
if (I >= 3 || W >= 500) {
|
|
205
205
|
b.current = !1, S.current = null;
|
|
206
206
|
return;
|
|
207
207
|
}
|
|
@@ -216,62 +216,74 @@ function fe(a) {
|
|
|
216
216
|
}, [e.length]);
|
|
217
217
|
const O = k((o, d) => {
|
|
218
218
|
var Q;
|
|
219
|
-
const
|
|
220
|
-
if (!
|
|
219
|
+
const I = l.current;
|
|
220
|
+
if (!I) return;
|
|
221
|
+
const R = h.current.get(o);
|
|
222
|
+
if (R === d) return;
|
|
221
223
|
h.current.set(o, d);
|
|
222
224
|
const P = f.current.get(o);
|
|
223
225
|
if (P === void 0) return;
|
|
224
|
-
const
|
|
225
|
-
W && !b.current && !u.current && V !== 0 &&
|
|
226
|
+
const x = I.getSize(P), V = d - x, W = r.current;
|
|
227
|
+
W && R !== void 0 && !b.current && !u.current && V !== 0 && I.getOffset(P) + x + (((Q = y.current) == null ? void 0 : Q.offsetTop) ?? 0) < W.scrollTop && (C.current += V, z()), I.setSize(P, d) && T();
|
|
226
228
|
}, [z, T]), N = k(
|
|
227
229
|
(o, d = "auto") => {
|
|
228
|
-
var
|
|
229
|
-
(
|
|
230
|
+
var I;
|
|
231
|
+
(I = r.current) == null || I.scrollTo({ top: o, behavior: d });
|
|
230
232
|
},
|
|
231
233
|
[]
|
|
232
|
-
), G =
|
|
234
|
+
), G = Re(r, l, y, {
|
|
233
235
|
reconcile: !0
|
|
234
236
|
}), q = k(() => {
|
|
235
237
|
var ne;
|
|
236
238
|
const o = r.current, d = l.current;
|
|
237
239
|
if (!o || !d || d.count === 0) return null;
|
|
238
|
-
const
|
|
239
|
-
for (let Q =
|
|
240
|
+
const I = ((ne = y.current) == null ? void 0 : ne.offsetTop) ?? 0, R = Math.max(0, o.scrollTop - I), P = d.getOffsets(), x = ue(P, R), V = v.current[x] ?? null, W = [];
|
|
241
|
+
for (let Q = x; Q < Math.min(d.count, x + 6); Q++) {
|
|
240
242
|
const se = v.current[Q] ?? null;
|
|
241
243
|
se !== null && W.push({
|
|
242
244
|
key: se,
|
|
243
|
-
offsetWithinItem:
|
|
245
|
+
offsetWithinItem: R - d.getOffset(Q)
|
|
244
246
|
});
|
|
245
247
|
}
|
|
246
248
|
return {
|
|
247
249
|
key: V,
|
|
248
|
-
offsetWithinItem:
|
|
250
|
+
offsetWithinItem: R - d.getOffset(x),
|
|
249
251
|
candidates: W,
|
|
250
252
|
scrollTop: o.scrollTop,
|
|
251
253
|
scrollHeight: o.scrollHeight
|
|
252
254
|
};
|
|
253
255
|
}, []), ee = k(
|
|
254
256
|
(o, d) => {
|
|
255
|
-
var
|
|
256
|
-
const
|
|
257
|
-
if (!
|
|
258
|
-
const
|
|
259
|
-
return
|
|
257
|
+
var x;
|
|
258
|
+
const I = l.current;
|
|
259
|
+
if (!I) return null;
|
|
260
|
+
const R = f.current.get(o);
|
|
261
|
+
return R === void 0 ? null : (((x = y.current) == null ? void 0 : x.offsetTop) ?? 0) + I.getOffset(R) + d;
|
|
260
262
|
},
|
|
261
263
|
[]
|
|
262
264
|
), _ = l.current, me = _ ? _.totalSize() : 0, Y = r.current, te = (Y == null ? void 0 : Y.scrollTop) ?? i.current, ce = (Y == null ? void 0 : Y.clientHeight) ?? E.current, de = ((ie = y.current) == null ? void 0 : ie.offsetTop) ?? 0, le = Math.max(0, te - de), re = [];
|
|
263
265
|
if (_ && _.count > 0 && ce > 0) {
|
|
264
|
-
const o = _.getOffsets(), d = _.getSizes(),
|
|
266
|
+
const o = _.getOffsets(), d = _.getSizes(), I = ue(o, le), R = Se(
|
|
265
267
|
o,
|
|
266
268
|
d,
|
|
267
269
|
le + ce
|
|
268
270
|
), P = Ae({
|
|
269
|
-
firstVisible:
|
|
270
|
-
lastVisible:
|
|
271
|
+
firstVisible: I,
|
|
272
|
+
lastVisible: R,
|
|
271
273
|
itemCount: _.count,
|
|
272
274
|
overscan: c
|
|
273
275
|
});
|
|
274
|
-
for (let
|
|
276
|
+
for (let x = P.start; x <= P.end && x < e.length; x++)
|
|
277
|
+
re.push({
|
|
278
|
+
key: v.current[x] ?? t(e[x], x),
|
|
279
|
+
index: x,
|
|
280
|
+
start: _.getOffset(x),
|
|
281
|
+
size: _.getSize(x),
|
|
282
|
+
data: e[x]
|
|
283
|
+
});
|
|
284
|
+
} else if (_ && _.count > 0) {
|
|
285
|
+
const o = Math.min(e.length, c * 2 + 1), d = n === "bottom" ? Math.max(0, e.length - o) : 0, I = d + o - 1;
|
|
286
|
+
for (let R = d; R <= I; R++)
|
|
275
287
|
re.push({
|
|
276
288
|
key: v.current[R] ?? t(e[R], R),
|
|
277
289
|
index: R,
|
|
@@ -279,16 +291,6 @@ function fe(a) {
|
|
|
279
291
|
size: _.getSize(R),
|
|
280
292
|
data: e[R]
|
|
281
293
|
});
|
|
282
|
-
} else if (_ && _.count > 0) {
|
|
283
|
-
const o = Math.min(e.length, c * 2 + 1), d = n === "bottom" ? Math.max(0, e.length - o) : 0, x = d + o - 1;
|
|
284
|
-
for (let I = d; I <= x; I++)
|
|
285
|
-
re.push({
|
|
286
|
-
key: v.current[I] ?? t(e[I], I),
|
|
287
|
-
index: I,
|
|
288
|
-
start: _.getOffset(I),
|
|
289
|
-
size: _.getSize(I),
|
|
290
|
-
data: e[I]
|
|
291
|
-
});
|
|
292
294
|
}
|
|
293
295
|
const pe = Y ? Y.scrollHeight - Y.scrollTop - Y.clientHeight : 1 / 0;
|
|
294
296
|
return {
|
|
@@ -307,7 +309,7 @@ function fe(a) {
|
|
|
307
309
|
jumpSuppressedRef: u
|
|
308
310
|
};
|
|
309
311
|
}
|
|
310
|
-
function
|
|
312
|
+
function ze(a) {
|
|
311
313
|
var c;
|
|
312
314
|
const { snapshot: e, currentScrollHeight: t, resolveAnchorTop: s } = a;
|
|
313
315
|
if (e.key !== null) {
|
|
@@ -352,7 +354,7 @@ function xe(a) {
|
|
|
352
354
|
if (!f || !p) return;
|
|
353
355
|
y.current = !1;
|
|
354
356
|
const F = () => {
|
|
355
|
-
const M =
|
|
357
|
+
const M = ze({
|
|
356
358
|
snapshot: p,
|
|
357
359
|
currentScrollHeight: f.scrollHeight,
|
|
358
360
|
resolveAnchorTop: c
|