revert: switch to old virtual list logic

This commit is contained in:
James Xylight committed 2025-06-26 12:46:58 -07:00
1 parent b98b902c7a
commit 4300b3e719
3 files changed
+47 -67

No files matched your search

@@ -42,8 +42,8 @@
{#if offsetEl} {#if offsetEl}
<VirtualList <VirtualList
class="divide-y divide-slate-200 dark:divide-zinc-800 w-full" class="divide-y divide-slate-200 dark:divide-zinc-800 w-full"
overscan={50} overscan={1000}
estimatedHeight={100} estimatedHeight={300}
items={nodes} items={nodes}
{initialOffset} {initialOffset}
> >
@@ -136,21 +136,6 @@
removedNodes.forEach(unobservePost) removedNodes.forEach(unobservePost)
}) })
}).observe(feed, { childList: true, subtree: false }) }).observe(feed, { childList: true, subtree: false })
const offsetObserver = new ResizeObserver(entries => {
entries.forEach(entry => {
if (listEl) {
initialOffset =
entry.target.getBoundingClientRect().top + window.scrollY
}
})
})
if (listEl) offsetObserver.observe(listEl)
return () => {
observer.disconnect()
offsetObserver.disconnect()
}
}) })
$effect(() => { $effect(() => {
@@ -189,6 +174,7 @@
class="divide-y -mx-4 sm:-mx-6 divide-slate-200 dark:divide-zinc-900" class="divide-y -mx-4 sm:-mx-6 divide-slate-200 dark:divide-zinc-900"
items={posts} items={posts}
{initialOffset} {initialOffset}
overscan={1000}
bind:restore={postFeeds.value[feedId].clientData} bind:restore={postFeeds.value[feedId].clientData}
bind:this={listComp} bind:this={listComp}
> >
+44 -50
View File
@@ -3,7 +3,7 @@
import { debounce } from 'mono-svelte/util/time' import { debounce } from 'mono-svelte/util/time'
import { onDestroy, untrack, type Snippet } from 'svelte' import { onDestroy, untrack, type Snippet } from 'svelte'
import type { HTMLAttributes } from 'svelte/elements' import type { HTMLAttributes } from 'svelte/elements'
import { innerHeight, scrollY } from 'svelte/reactivity/window' import { innerHeight } from 'svelte/reactivity/window'
interface Props extends HTMLAttributes<HTMLDivElement> { interface Props extends HTMLAttributes<HTMLDivElement> {
items: T[] items: T[]
@@ -20,7 +20,7 @@
let { let {
items, items,
estimatedHeight = 100, estimatedHeight = 100,
overscan = 3, overscan = 6,
item: itemSnippet, item: itemSnippet,
initialOffset = 0, initialOffset = 0,
restore = $bindable(), restore = $bindable(),
@@ -29,11 +29,14 @@
}: Props = $props() }: Props = $props()
export function scrollToIndex(index: number, useWindow: boolean = false) { export function scrollToIndex(index: number, useWindow: boolean = false) {
if (useWindow && browser) const targetPx = cumulativeItemHeights[index] - (initialOffset || 0)
window.scrollTo({ if (targetPx < (innerHeight.current ?? 0)) return
behavior: 'instant', scrollY = targetPx
top: cumulativeItemHeights[index] - initialOffset || 0, if (useWindow && browser) {
requestAnimationFrame(() => {
window.scrollTo({ behavior: 'instant', top: scrollY })
}) })
}
} }
onDestroy(() => { onDestroy(() => {
@@ -44,18 +47,11 @@
let virtualListEl = $state<HTMLElement>() let virtualListEl = $state<HTMLElement>()
let itemHeights = $state<(number | null)[]>(Array.from({ length: Math.max(items.length, (restore?.itemHeights ?? []).length) }, (_, i) => (restore?.itemHeights ?? [])[i] || null)) let itemHeights = $state<(number | null)[]>([
...(restore?.itemHeights ?? Array(items.length).fill(null)),
])
$effect(() => { let cumulativeItemHeights = $derived.by<number[]>(() => {
if (items.length !== itemHeights.length) {
const newHeights = Array.from({ length: items.length }, (_, i) =>
itemHeights[i] || null
)
itemHeights = newHeights
}
})
let cumulativeItemHeights = $derived.by(() => {
let cumulation = new Array(itemHeights.length) let cumulation = new Array(itemHeights.length)
let sum = 0 let sum = 0
@@ -68,9 +64,26 @@
return cumulation return cumulation
}) })
let scrollY = $state(0)
let viewportHeight = $state(0) let viewportHeight = $state(0)
let visibleItems = $state<{ index: number; offset: number }[]>([]) let visibleItems = $state<{ index: number; offset: number }[]>([])
// Initial render
$effect.pre(() => {
if (virtualListEl && browser) {
untrack(() => {
visibleItems = updateVisibleItems()
})
}
})
$effect.pre(() => {
if (items.length > itemHeights.length) {
const missing = items.length - itemHeights.length
itemHeights = [...itemHeights, ...Array(missing).fill(null)]
}
})
function findFirstVisibleIndex( function findFirstVisibleIndex(
scrollTop: number, scrollTop: number,
cumulativeHeights: number[], cumulativeHeights: number[],
@@ -93,26 +106,23 @@
if (!virtualListEl) return [] if (!virtualListEl) return []
viewportHeight = innerHeight?.current ?? 1000 viewportHeight = innerHeight?.current ?? 1000
const scrollTop = (scrollY.current ?? 0) - initialOffset const scrollTop = scrollY - initialOffset
let newVisibleItems: { index: number; offset: number }[] = [] let newVisibleItems: { index: number; offset: number }[] = []
const firstIndex = findFirstVisibleIndex(scrollTop, cumulativeItemHeights) const firstIndex = findFirstVisibleIndex(scrollTop, cumulativeItemHeights)
let i = firstIndex let i = firstIndex
let offset = i === 0 ? 0 : cumulativeItemHeights[i - 1] let offset = i == 0 ? 0 : cumulativeItemHeights[i - 1]
while ( while (i < items.length && offset < scrollTop + viewportHeight + overscan) {
i < items.length &&
offset < scrollTop + viewportHeight + overscan * estimatedHeight
) {
newVisibleItems.push({ index: i, offset: offset }) newVisibleItems.push({ index: i, offset: offset })
const height = itemHeights[i] || estimatedHeight const height = itemHeights[i] || estimatedHeight
offset += height offset += height
i++ i++
} }
return newVisibleItems return newVisibleItems ?? []
} }
function resizeObserver(node: HTMLElement) { function resizeObserver(node: HTMLElement) {
@@ -124,7 +134,7 @@
if (isNaN(index)) continue if (isNaN(index)) continue
const newHeight = entry.contentRect.height const newHeight = entry.contentRect.height
if (itemHeights[index] !== newHeight && newHeight !== estimatedHeight) { if (itemHeights[index] !== newHeight) {
itemHeights[index] = newHeight itemHeights[index] = newHeight
visibleItems = updateVisibleItems() visibleItems = updateVisibleItems()
} }
@@ -148,37 +158,21 @@
} }
} }
// Initial render and viewport changes
$effect(() => {
if (virtualListEl && browser) {
untrack(() => {
visibleItems = updateVisibleItems()
})
}
})
// Viewport height changes
$effect(() => {
if (innerHeight?.current && browser) {
untrack(() => {
visibleItems = updateVisibleItems()
})
}
})
// Scroll position changes (only every few px) // Scroll position changes (only every few px)
let oldScroll = $state(0) let oldScroll = $state(0)
$effect(() => { $effect(() => {
const currentScrollY = scrollY.current ?? 0 const currentScrollY = scrollY ?? 0
if (Math.abs(currentScrollY - oldScroll) > estimatedHeight) { if (Math.abs(currentScrollY - oldScroll) > estimatedHeight) {
untrack(() => { untrack(() => {
visibleItems = updateVisibleItems() visibleItems = updateVisibleItems()
oldScroll = currentScrollY oldScroll = currentScrollY
}) })
} }
}) })
</script> </script>
<svelte:window bind:scrollY />
<div <div
bind:this={virtualListEl} bind:this={virtualListEl}
style="position: relative; height: {cumulativeItemHeights[ style="position: relative; height: {cumulativeItemHeights[