BlfKXJ2P.js
16.1 KB
import{g as E,y as I,bd as T,J as W,bz as z,s as k}from"./BtEn9SvP.js";function x(o,r,e){let t=e.initialDeps??[],s,n=!0;function l(){var i,a,u;let c;e.key&&((i=e.debug)!=null&&i.call(e))&&(c=Date.now());const d=o();if(!(d.length!==t.length||d.some((f,p)=>t[p]!==f)))return s;t=d;let h;if(e.key&&((a=e.debug)!=null&&a.call(e))&&(h=Date.now()),s=r(...d),e.key&&((u=e.debug)!=null&&u.call(e))){const f=Math.round((Date.now()-c)*100)/100,p=Math.round((Date.now()-h)*100)/100,m=p/16,v=(g,M)=>{for(g=String(g);g.length<M;)g=" "+g;return g};console.info(`%c⏱ ${v(p,5)} /${v(f,5)} ms`,`
font-size: .6rem;
font-weight: bold;
color: hsl(${Math.max(0,Math.min(120-120*m,120))}deg 100% 31%);`,e?.key)}return e?.onChange&&!(n&&e.skipInitialOnChange)&&e.onChange(s),n=!1,s}return l.updateDeps=i=>{t=i},l}function C(o,r){if(o===void 0)throw new Error("Unexpected undefined");return o}const D=(o,r)=>Math.abs(o-r)<1.01,L=(o,r,e)=>{let t;return function(...s){o.clearTimeout(t),t=o.setTimeout(()=>r.apply(this,s),e)}},y=o=>{const{offsetWidth:r,offsetHeight:e}=o;return{width:r,height:e}},j=o=>o,P=o=>{const r=Math.max(o.startIndex-o.overscan,0),e=Math.min(o.endIndex+o.overscan,o.count-1),t=[];for(let s=r;s<=e;s++)t.push(s);return t},B=(o,r)=>{const e=o.scrollElement;if(!e)return;const t=o.targetWindow;if(!t)return;const s=l=>{const{width:i,height:a}=l;r({width:Math.round(i),height:Math.round(a)})};if(s(y(e)),!t.ResizeObserver)return()=>{};const n=new t.ResizeObserver(l=>{const i=()=>{const a=l[0];if(a?.borderBoxSize){const u=a.borderBoxSize[0];if(u){s({width:u.inlineSize,height:u.blockSize});return}}s(y(e))};o.options.useAnimationFrameWithResizeObserver?requestAnimationFrame(i):i()});return n.observe(e,{box:"border-box"}),()=>{n.unobserve(e)}},A={passive:!0},R=typeof window>"u"?!0:"onscrollend"in window,N=(o,r)=>{const e=o.scrollElement;if(!e)return;const t=o.targetWindow;if(!t)return;let s=0;const n=o.options.useScrollendEvent&&R?()=>{}:L(t,()=>{r(s,!1)},o.options.isScrollingResetDelay),l=c=>()=>{const{horizontal:d,isRtl:b}=o.options;s=d?e.scrollLeft*(b&&-1||1):e.scrollTop,n(),r(s,c)},i=l(!0),a=l(!1);e.addEventListener("scroll",i,A);const u=o.options.useScrollendEvent&&R;return u&&e.addEventListener("scrollend",a,A),()=>{e.removeEventListener("scroll",i),u&&e.removeEventListener("scrollend",a)}},V=(o,r,e)=>{if(r?.borderBoxSize){const t=r.borderBoxSize[0];if(t)return Math.round(t[e.options.horizontal?"inlineSize":"blockSize"])}return o[e.options.horizontal?"offsetWidth":"offsetHeight"]},H=(o,{adjustments:r=0,behavior:e},t)=>{var s,n;const l=o+r;(n=(s=t.scrollElement)==null?void 0:s.scrollTo)==null||n.call(s,{[t.options.horizontal?"left":"top"]:l,behavior:e})};class U{constructor(r){this.unsubs=[],this.scrollElement=null,this.targetWindow=null,this.isScrolling=!1,this.scrollState=null,this.measurementsCache=[],this.itemSizeCache=new Map,this.laneAssignments=new Map,this.pendingMeasuredCacheIndexes=[],this.prevLanes=void 0,this.lanesChangedFlag=!1,this.lanesSettling=!1,this.scrollRect=null,this.scrollOffset=null,this.scrollDirection=null,this.scrollAdjustments=0,this.elementsCache=new Map,this.now=()=>{var e,t,s;return((s=(t=(e=this.targetWindow)==null?void 0:e.performance)==null?void 0:t.now)==null?void 0:s.call(t))??Date.now()},this.observer=(()=>{let e=null;const t=()=>e||(!this.targetWindow||!this.targetWindow.ResizeObserver?null:e=new this.targetWindow.ResizeObserver(s=>{s.forEach(n=>{const l=()=>{const i=n.target,a=this.indexFromElement(i);if(!i.isConnected){this.observer.unobserve(i);return}this.shouldMeasureDuringScroll(a)&&this.resizeItem(a,this.options.measureElement(i,n,this))};this.options.useAnimationFrameWithResizeObserver?requestAnimationFrame(l):l()})}));return{disconnect:()=>{var s;(s=t())==null||s.disconnect(),e=null},observe:s=>{var n;return(n=t())==null?void 0:n.observe(s,{box:"border-box"})},unobserve:s=>{var n;return(n=t())==null?void 0:n.unobserve(s)}}})(),this.range=null,this.setOptions=e=>{Object.entries(e).forEach(([t,s])=>{typeof s>"u"&&delete e[t]}),this.options={debug:!1,initialOffset:0,overscan:1,paddingStart:0,paddingEnd:0,scrollPaddingStart:0,scrollPaddingEnd:0,horizontal:!1,getItemKey:j,rangeExtractor:P,onChange:()=>{},measureElement:V,initialRect:{width:0,height:0},scrollMargin:0,gap:0,indexAttribute:"data-index",initialMeasurementsCache:[],lanes:1,isScrollingResetDelay:150,enabled:!0,isRtl:!1,useScrollendEvent:!1,useAnimationFrameWithResizeObserver:!1,laneAssignmentMode:"estimate",...e}},this.notify=e=>{var t,s;(s=(t=this.options).onChange)==null||s.call(t,this,e)},this.maybeNotify=x(()=>(this.calculateRange(),[this.isScrolling,this.range?this.range.startIndex:null,this.range?this.range.endIndex:null]),e=>{this.notify(e)},{key:!1,debug:()=>this.options.debug,initialDeps:[this.isScrolling,this.range?this.range.startIndex:null,this.range?this.range.endIndex:null]}),this.cleanup=()=>{this.unsubs.filter(Boolean).forEach(e=>e()),this.unsubs=[],this.observer.disconnect(),this.rafId!=null&&this.targetWindow&&(this.targetWindow.cancelAnimationFrame(this.rafId),this.rafId=null),this.scrollState=null,this.scrollElement=null,this.targetWindow=null},this._didMount=()=>()=>{this.cleanup()},this._willUpdate=()=>{var e;const t=this.options.enabled?this.options.getScrollElement():null;if(this.scrollElement!==t){if(this.cleanup(),!t){this.maybeNotify();return}this.scrollElement=t,this.scrollElement&&"ownerDocument"in this.scrollElement?this.targetWindow=this.scrollElement.ownerDocument.defaultView:this.targetWindow=((e=this.scrollElement)==null?void 0:e.window)??null,this.elementsCache.forEach(s=>{this.observer.observe(s)}),this.unsubs.push(this.options.observeElementRect(this,s=>{this.scrollRect=s,this.maybeNotify()})),this.unsubs.push(this.options.observeElementOffset(this,(s,n)=>{this.scrollAdjustments=0,this.scrollDirection=n?this.getScrollOffset()<s?"forward":"backward":null,this.scrollOffset=s,this.isScrolling=n,this.scrollState&&this.scheduleScrollReconcile(),this.maybeNotify()})),this._scrollToOffset(this.getScrollOffset(),{adjustments:void 0,behavior:void 0})}},this.rafId=null,this.getSize=()=>this.options.enabled?(this.scrollRect=this.scrollRect??this.options.initialRect,this.scrollRect[this.options.horizontal?"width":"height"]):(this.scrollRect=null,0),this.getScrollOffset=()=>this.options.enabled?(this.scrollOffset=this.scrollOffset??(typeof this.options.initialOffset=="function"?this.options.initialOffset():this.options.initialOffset),this.scrollOffset):(this.scrollOffset=null,0),this.getFurthestMeasurement=(e,t)=>{const s=new Map,n=new Map;for(let l=t-1;l>=0;l--){const i=e[l];if(s.has(i.lane))continue;const a=n.get(i.lane);if(a==null||i.end>a.end?n.set(i.lane,i):i.end<a.end&&s.set(i.lane,!0),s.size===this.options.lanes)break}return n.size===this.options.lanes?Array.from(n.values()).sort((l,i)=>l.end===i.end?l.index-i.index:l.end-i.end)[0]:void 0},this.getMeasurementOptions=x(()=>[this.options.count,this.options.paddingStart,this.options.scrollMargin,this.options.getItemKey,this.options.enabled,this.options.lanes,this.options.laneAssignmentMode],(e,t,s,n,l,i,a)=>(this.prevLanes!==void 0&&this.prevLanes!==i&&(this.lanesChangedFlag=!0),this.prevLanes=i,this.pendingMeasuredCacheIndexes=[],{count:e,paddingStart:t,scrollMargin:s,getItemKey:n,enabled:l,lanes:i,laneAssignmentMode:a}),{key:!1}),this.getMeasurements=x(()=>[this.getMeasurementOptions(),this.itemSizeCache],({count:e,paddingStart:t,scrollMargin:s,getItemKey:n,enabled:l,lanes:i,laneAssignmentMode:a},u)=>{if(!l)return this.measurementsCache=[],this.itemSizeCache.clear(),this.laneAssignments.clear(),[];if(this.laneAssignments.size>e)for(const h of this.laneAssignments.keys())h>=e&&this.laneAssignments.delete(h);this.lanesChangedFlag&&(this.lanesChangedFlag=!1,this.lanesSettling=!0,this.measurementsCache=[],this.itemSizeCache.clear(),this.laneAssignments.clear(),this.pendingMeasuredCacheIndexes=[]),this.measurementsCache.length===0&&!this.lanesSettling&&(this.measurementsCache=this.options.initialMeasurementsCache,this.measurementsCache.forEach(h=>{this.itemSizeCache.set(h.key,h.size)}));const c=this.lanesSettling?0:this.pendingMeasuredCacheIndexes.length>0?Math.min(...this.pendingMeasuredCacheIndexes):0;this.pendingMeasuredCacheIndexes=[],this.lanesSettling&&this.measurementsCache.length===e&&(this.lanesSettling=!1);const d=this.measurementsCache.slice(0,c),b=new Array(i).fill(void 0);for(let h=0;h<c;h++){const f=d[h];f&&(b[f.lane]=h)}for(let h=c;h<e;h++){const f=n(h),p=this.laneAssignments.get(h);let m,v;const g=a==="estimate"||u.has(f);if(p!==void 0&&this.options.lanes>1){m=p;const S=b[m],O=S!==void 0?d[S]:void 0;v=O?O.end+this.options.gap:t+s}else{const S=this.options.lanes===1?d[h-1]:this.getFurthestMeasurement(d,h);v=S?S.end+this.options.gap:t+s,m=S?S.lane:h%this.options.lanes,this.options.lanes>1&&g&&this.laneAssignments.set(h,m)}const M=u.get(f),w=typeof M=="number"?M:this.options.estimateSize(h),_=v+w;d[h]={index:h,start:v,size:w,end:_,key:f,lane:m},b[m]=h}return this.measurementsCache=d,d},{key:!1,debug:()=>this.options.debug}),this.calculateRange=x(()=>[this.getMeasurements(),this.getSize(),this.getScrollOffset(),this.options.lanes],(e,t,s,n)=>this.range=e.length>0&&t>0?$({measurements:e,outerSize:t,scrollOffset:s,lanes:n}):null,{key:!1,debug:()=>this.options.debug}),this.getVirtualIndexes=x(()=>{let e=null,t=null;const s=this.calculateRange();return s&&(e=s.startIndex,t=s.endIndex),this.maybeNotify.updateDeps([this.isScrolling,e,t]),[this.options.rangeExtractor,this.options.overscan,this.options.count,e,t]},(e,t,s,n,l)=>n===null||l===null?[]:e({startIndex:n,endIndex:l,overscan:t,count:s}),{key:!1,debug:()=>this.options.debug}),this.indexFromElement=e=>{const t=this.options.indexAttribute,s=e.getAttribute(t);return s?parseInt(s,10):(console.warn(`Missing attribute name '${t}={index}' on measured element.`),-1)},this.shouldMeasureDuringScroll=e=>{var t;if(!this.scrollState||this.scrollState.behavior!=="smooth")return!0;const s=this.scrollState.index??((t=this.getVirtualItemForOffset(this.scrollState.lastTargetOffset))==null?void 0:t.index);if(s!==void 0&&this.range){const n=Math.max(this.options.overscan,Math.ceil((this.range.endIndex-this.range.startIndex)/2)),l=Math.max(0,s-n),i=Math.min(this.options.count-1,s+n);return e>=l&&e<=i}return!0},this.measureElement=e=>{if(!e){this.elementsCache.forEach((l,i)=>{l.isConnected||(this.observer.unobserve(l),this.elementsCache.delete(i))});return}const t=this.indexFromElement(e),s=this.options.getItemKey(t),n=this.elementsCache.get(s);n!==e&&(n&&this.observer.unobserve(n),this.observer.observe(e),this.elementsCache.set(s,e)),(!this.isScrolling||this.scrollState)&&this.shouldMeasureDuringScroll(t)&&this.resizeItem(t,this.options.measureElement(e,void 0,this))},this.resizeItem=(e,t)=>{var s;const n=this.measurementsCache[e];if(!n)return;const l=this.itemSizeCache.get(n.key)??n.size,i=t-l;i!==0&&(((s=this.scrollState)==null?void 0:s.behavior)!=="smooth"&&(this.shouldAdjustScrollPositionOnItemSizeChange!==void 0?this.shouldAdjustScrollPositionOnItemSizeChange(n,i,this):n.start<this.getScrollOffset()+this.scrollAdjustments)&&this._scrollToOffset(this.getScrollOffset(),{adjustments:this.scrollAdjustments+=i,behavior:void 0}),this.pendingMeasuredCacheIndexes.push(n.index),this.itemSizeCache=new Map(this.itemSizeCache.set(n.key,t)),this.notify(!1))},this.getVirtualItems=x(()=>[this.getVirtualIndexes(),this.getMeasurements()],(e,t)=>{const s=[];for(let n=0,l=e.length;n<l;n++){const i=e[n],a=t[i];s.push(a)}return s},{key:!1,debug:()=>this.options.debug}),this.getVirtualItemForOffset=e=>{const t=this.getMeasurements();if(t.length!==0)return C(t[F(0,t.length-1,s=>C(t[s]).start,e)])},this.getMaxScrollOffset=()=>{if(!this.scrollElement)return 0;if("scrollHeight"in this.scrollElement)return this.options.horizontal?this.scrollElement.scrollWidth-this.scrollElement.clientWidth:this.scrollElement.scrollHeight-this.scrollElement.clientHeight;{const e=this.scrollElement.document.documentElement;return this.options.horizontal?e.scrollWidth-this.scrollElement.innerWidth:e.scrollHeight-this.scrollElement.innerHeight}},this.getOffsetForAlignment=(e,t,s=0)=>{if(!this.scrollElement)return 0;const n=this.getSize(),l=this.getScrollOffset();t==="auto"&&(t=e>=l+n?"end":"start"),t==="center"?e+=(s-n)/2:t==="end"&&(e-=n);const i=this.getMaxScrollOffset();return Math.max(Math.min(i,e),0)},this.getOffsetForIndex=(e,t="auto")=>{e=Math.max(0,Math.min(e,this.options.count-1));const s=this.getSize(),n=this.getScrollOffset(),l=this.measurementsCache[e];if(!l)return;if(t==="auto")if(l.end>=n+s-this.options.scrollPaddingEnd)t="end";else if(l.start<=n+this.options.scrollPaddingStart)t="start";else return[n,t];if(t==="end"&&e===this.options.count-1)return[this.getMaxScrollOffset(),t];const i=t==="end"?l.end+this.options.scrollPaddingEnd:l.start-this.options.scrollPaddingStart;return[this.getOffsetForAlignment(i,t,l.size),t]},this.scrollToOffset=(e,{align:t="start",behavior:s="auto"}={})=>{const n=this.getOffsetForAlignment(e,t),l=this.now();this.scrollState={index:null,align:t,behavior:s,startedAt:l,lastTargetOffset:n,stableFrames:0},this._scrollToOffset(n,{adjustments:void 0,behavior:s}),this.scheduleScrollReconcile()},this.scrollToIndex=(e,{align:t="auto",behavior:s="auto"}={})=>{e=Math.max(0,Math.min(e,this.options.count-1));const n=this.getOffsetForIndex(e,t);if(!n)return;const[l,i]=n,a=this.now();this.scrollState={index:e,align:i,behavior:s,startedAt:a,lastTargetOffset:l,stableFrames:0},this._scrollToOffset(l,{adjustments:void 0,behavior:s}),this.scheduleScrollReconcile()},this.scrollBy=(e,{behavior:t="auto"}={})=>{const s=this.getScrollOffset()+e,n=this.now();this.scrollState={index:null,align:"start",behavior:t,startedAt:n,lastTargetOffset:s,stableFrames:0},this._scrollToOffset(s,{adjustments:void 0,behavior:t}),this.scheduleScrollReconcile()},this.getTotalSize=()=>{var e;const t=this.getMeasurements();let s;if(t.length===0)s=this.options.paddingStart;else if(this.options.lanes===1)s=((e=t[t.length-1])==null?void 0:e.end)??0;else{const n=Array(this.options.lanes).fill(null);let l=t.length-1;for(;l>=0&&n.some(i=>i===null);){const i=t[l];n[i.lane]===null&&(n[i.lane]=i.end),l--}s=Math.max(...n.filter(i=>i!==null))}return Math.max(s-this.options.scrollMargin+this.options.paddingEnd,0)},this._scrollToOffset=(e,{adjustments:t,behavior:s})=>{this.options.scrollToFn(e,{behavior:s,adjustments:t},this)},this.measure=()=>{this.itemSizeCache=new Map,this.laneAssignments=new Map,this.notify(!1)},this.setOptions(r)}scheduleScrollReconcile(){if(!this.targetWindow){this.scrollState=null;return}this.rafId==null&&(this.rafId=this.targetWindow.requestAnimationFrame(()=>{this.rafId=null,this.reconcileScroll()}))}reconcileScroll(){if(!this.scrollState||!this.scrollElement)return;if(this.now()-this.scrollState.startedAt>5e3){this.scrollState=null;return}const t=this.scrollState.index!=null?this.getOffsetForIndex(this.scrollState.index,this.scrollState.align):void 0,s=t?t[0]:this.scrollState.lastTargetOffset,n=1,l=s!==this.scrollState.lastTargetOffset;if(!l&&D(s,this.getScrollOffset())){if(this.scrollState.stableFrames++,this.scrollState.stableFrames>=n){this.scrollState=null;return}}else this.scrollState.stableFrames=0,l&&(this.scrollState.lastTargetOffset=s,this.scrollState.behavior="auto",this._scrollToOffset(s,{adjustments:void 0,behavior:"auto"}));this.scheduleScrollReconcile()}}const F=(o,r,e,t)=>{for(;o<=r;){const s=(o+r)/2|0,n=e(s);if(n<t)o=s+1;else if(n>t)r=s-1;else return s}return o>0?o-1:0};function $({measurements:o,outerSize:r,scrollOffset:e,lanes:t}){const s=o.length-1,n=a=>o[a].start;if(o.length<=t)return{startIndex:0,endIndex:s};let l=F(0,s,n,e),i=l;if(t===1)for(;i<s&&o[i].end<e+r;)i++;else if(t>1){const a=Array(t).fill(0);for(;i<s&&a.some(c=>c<e+r);){const c=o[i];a[c.lane]=c.end,i++}const u=Array(t).fill(e+r);for(;l>=0&&u.some(c=>c>=e);){const c=o[l];u[c.lane]=c.start,l--}l=Math.max(0,l-l%t),i=Math.min(s,i+(t-1-i%t))}return{startIndex:l,endIndex:i}}function q(o){const r=new U(E(o)),e=W(r),t=r._didMount();return I(()=>E(o).getScrollElement(),s=>{s&&r._willUpdate()},{immediate:!0}),I(()=>E(o),s=>{r.setOptions({...s,onChange:(n,l)=>{var i;z(e),(i=s.onChange)==null||i.call(s,n,l)}}),r._willUpdate(),z(e)},{immediate:!0}),T(t),e}function X(o){return q(k(()=>({observeElementRect:B,observeElementOffset:N,scrollToFn:H,...E(o)})))}export{X as u};