v2: replace @tanstack/svelte-virtual with custom virtualizer (#516)
* impr: use custom virtualizer for post feeds * fix: use resizeobserver to update heights dynamically * fix: add overcsan and initialOffset for items * misc: update package.json (remove tanstack) * misc: use binary search to improve performance * fix: use translate for cheap gpu composite time * fix: reduce recalculate calls * feat: re-add list restore * fix: use relative instead of translations * misc: add ssr support to virtualizer * fix: onload scroll broken
This commit is contained in:
1 parent
af02883314
commit
472aa00e65
8 files changed
+303
-164
No files matched your search
@@ -1,12 +1,9 @@
|
||||
<script lang="ts">
|
||||
import { run } from 'svelte/legacy'
|
||||
|
||||
import type { Post } from 'lemmy-js-client'
|
||||
import type { CommentNodeI } from './comments.svelte'
|
||||
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
|
||||
import Comments from './Comments.svelte'
|
||||
import { browser } from '$app/environment'
|
||||
import { onMount } from 'svelte'
|
||||
import VirtualList from '$lib/components/render/VirtualList.svelte'
|
||||
|
||||
interface Props {
|
||||
nodes: CommentNodeI[]
|
||||
@@ -16,31 +13,7 @@
|
||||
|
||||
let { nodes, post, scrollTo }: Props = $props()
|
||||
|
||||
let virtualListEl = $state<HTMLElement>()
|
||||
|
||||
let virtualizer = $derived(
|
||||
createWindowVirtualizer({
|
||||
count: nodes.length,
|
||||
estimateSize: () => 30,
|
||||
scrollMargin: virtualListEl?.offsetTop,
|
||||
initialRect: {
|
||||
height: browser ? 1 : 1500,
|
||||
width: 99999,
|
||||
},
|
||||
overscan: 5,
|
||||
}),
|
||||
)
|
||||
|
||||
let virtualItemEls: HTMLElement[] = $state([])
|
||||
|
||||
let items = $derived($virtualizer.getVirtualItems())
|
||||
|
||||
const updateItems = () =>
|
||||
virtualItemEls.forEach((el) => $virtualizer.measureElement(el))
|
||||
|
||||
$effect(() => {
|
||||
if (virtualItemEls) updateItems()
|
||||
})
|
||||
let offsetEl = $state<HTMLElement>()
|
||||
|
||||
onMount(() => {
|
||||
if (scrollTo) {
|
||||
@@ -52,33 +25,21 @@
|
||||
})
|
||||
</script>
|
||||
|
||||
<div
|
||||
style="position:relative; height: {browser
|
||||
? `${$virtualizer.getTotalSize()}px`
|
||||
: '100%'}; width: 100%;"
|
||||
bind:this={virtualListEl}
|
||||
>
|
||||
<div
|
||||
style="position: absolute; top: 0; left: 0; width: 100%; transform: translateY({items?.[0]
|
||||
? items?.[0]?.start - $virtualizer.options.scrollMargin
|
||||
: 0}px);"
|
||||
class="divide-y divide-slate-200 dark:divide-zinc-900"
|
||||
id="feed"
|
||||
<div bind:this={offsetEl}>
|
||||
<VirtualList
|
||||
class="divide-y divide-slate-200 dark:divide-zinc-800 w-full"
|
||||
overscan={500}
|
||||
items={nodes}
|
||||
initialOffset={offsetEl?.offsetTop}
|
||||
>
|
||||
{#each items as row, index (row.index)}
|
||||
<div
|
||||
bind:this={virtualItemEls[index]}
|
||||
data-index={row.index}
|
||||
class="-mx-4 sm:-mx-6 px-4 sm:px-6"
|
||||
>
|
||||
{#snippet item(item, index)}
|
||||
<div class="-mx-4 sm:-mx-6 px-4 sm:px-6">
|
||||
<Comments
|
||||
isParent={true}
|
||||
bind:nodes={
|
||||
() => [nodes[row.index]], (v) => (nodes[row.index] = v[0])
|
||||
}
|
||||
bind:nodes={() => [nodes[index]], (v) => (nodes[index] = v[0])}
|
||||
{post}
|
||||
/>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/snippet}
|
||||
</VirtualList>
|
||||
</div>
|
||||
@@ -1,8 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { run } from 'svelte/legacy'
|
||||
|
||||
import { browser } from '$app/environment'
|
||||
import { afterNavigate } from '$app/navigation'
|
||||
import Post from '$lib/components/lemmy/post/Post.svelte'
|
||||
import EndPlaceholder from '$lib/components/ui/EndPlaceholder.svelte'
|
||||
import Placeholder from '$lib/components/ui/Placeholder.svelte'
|
||||
@@ -14,10 +11,9 @@
|
||||
} from '$lib/lemmy/postfeed.svelte'
|
||||
import { settings } from '$lib/settings.svelte.js'
|
||||
import { t } from '$lib/translations'
|
||||
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
|
||||
import type { PostView } from 'lemmy-js-client'
|
||||
import { Button } from 'mono-svelte'
|
||||
import { onMount } from 'svelte'
|
||||
import { onMount, tick, untrack } from 'svelte'
|
||||
import {
|
||||
ArchiveBox,
|
||||
ChevronDoubleUp,
|
||||
@@ -26,13 +22,7 @@
|
||||
Plus,
|
||||
} from 'svelte-hero-icons'
|
||||
import InfiniteScroll from 'svelte-infinite-scroll'
|
||||
|
||||
let virtualItemEls: HTMLElement[] = $state([])
|
||||
let virtualListEl: HTMLElement | undefined = $state(undefined)
|
||||
|
||||
afterNavigate(() => {
|
||||
$virtualizer.scrollToIndex(postFeeds.value[feedId]?.lastSeen ?? 0)
|
||||
})
|
||||
import VirtualList from '$lib/components/render/VirtualList.svelte'
|
||||
|
||||
interface Props {
|
||||
posts: PostView[]
|
||||
@@ -50,14 +40,10 @@
|
||||
children,
|
||||
}: Props = $props()
|
||||
|
||||
const virtualizer = createWindowVirtualizer({
|
||||
count: posts.length,
|
||||
estimateSize: () => 150,
|
||||
overscan: 5,
|
||||
measureElement: (element, entry, instance) => {
|
||||
return element.scrollHeight
|
||||
},
|
||||
})
|
||||
let listEl = $state<HTMLUListElement>()
|
||||
let listComp = $state<{
|
||||
scrollToIndex: (index: number, window?: boolean) => void
|
||||
}>()
|
||||
|
||||
let error: any = $state(undefined)
|
||||
let loading = $state(false)
|
||||
@@ -92,7 +78,7 @@
|
||||
hasMore = newPosts.posts.length != 0
|
||||
|
||||
feedData.cursor.next = newPosts.next_page
|
||||
feedData.posts.posts = [...feedData.posts.posts, ...newPosts.posts]
|
||||
feedData.posts.posts.push(...newPosts.posts)
|
||||
|
||||
postFeeds.value[feedId].data = feedData
|
||||
|
||||
@@ -120,63 +106,50 @@
|
||||
|
||||
onMount(() => {
|
||||
const observer = new IntersectionObserver(callback, {
|
||||
root: null,
|
||||
rootMargin: '0px',
|
||||
threshold: 0.5,
|
||||
})
|
||||
|
||||
const elements = document.querySelectorAll('.post-container')
|
||||
elements.forEach((el) => observer.observe(el))
|
||||
const observePost = (node: Node) => {
|
||||
if (
|
||||
node instanceof HTMLElement &&
|
||||
node.classList.contains('post-container')
|
||||
)
|
||||
observer.observe(node)
|
||||
}
|
||||
|
||||
const postContainer = document.querySelector('#feed')
|
||||
if (postContainer) {
|
||||
const mutationObserver = new MutationObserver((mutations) => {
|
||||
mutations.forEach((mutation) => {
|
||||
if (mutation.type === 'childList') {
|
||||
mutation.addedNodes.forEach((node) => {
|
||||
if (
|
||||
node instanceof HTMLElement &&
|
||||
node.classList.contains('post-container')
|
||||
) {
|
||||
observer.observe(node)
|
||||
}
|
||||
})
|
||||
mutation.removedNodes.forEach((node) => {
|
||||
if (
|
||||
node instanceof HTMLElement &&
|
||||
node.classList.contains('post-container')
|
||||
) {
|
||||
observer.unobserve(node)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
const unobservePost = (node: Node) => {
|
||||
if (
|
||||
node instanceof HTMLElement &&
|
||||
node.classList.contains('post-container')
|
||||
)
|
||||
observer.unobserve(node)
|
||||
}
|
||||
|
||||
document.querySelectorAll('.post-container').forEach(observePost)
|
||||
|
||||
const feed = document.getElementById('feed')
|
||||
if (!feed) return
|
||||
|
||||
new MutationObserver((mutations) => {
|
||||
mutations.forEach(({ addedNodes, removedNodes }) => {
|
||||
addedNodes.forEach(observePost)
|
||||
removedNodes.forEach(unobservePost)
|
||||
})
|
||||
}).observe(feed, { childList: true, subtree: false })
|
||||
})
|
||||
|
||||
mutationObserver.observe(postContainer, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
$effect(() => {
|
||||
if (listComp) {
|
||||
untrack(() => {
|
||||
if (postFeeds.value[feedId].lastSeen != 0) {
|
||||
listComp?.scrollToIndex(postFeeds.value[feedId].lastSeen, true)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
let items = $derived($virtualizer.getVirtualItems())
|
||||
|
||||
$effect(() => {
|
||||
if (virtualItemEls.length)
|
||||
virtualItemEls.forEach($virtualizer.measureElement)
|
||||
})
|
||||
|
||||
$effect(() => {
|
||||
if (posts.length && virtualListEl)
|
||||
$virtualizer.setOptions({
|
||||
scrollMargin: virtualListEl?.offsetTop,
|
||||
count: posts.length,
|
||||
})
|
||||
})
|
||||
</script>
|
||||
|
||||
<ul class="flex flex-col list-none">
|
||||
<ul class="flex flex-col list-none" bind:this={listEl}>
|
||||
{#key posts}
|
||||
{#if posts?.length == 0}
|
||||
<div class="h-full grid place-items-center">
|
||||
@@ -194,45 +167,39 @@
|
||||
</Placeholder>
|
||||
</div>
|
||||
{:else}
|
||||
<div
|
||||
style="position:relative; height: {browser
|
||||
? `${$virtualizer?.getTotalSize()}px`
|
||||
: '100%'}; width: 100%;"
|
||||
bind:this={virtualListEl}
|
||||
<VirtualList
|
||||
id="feed"
|
||||
class="divide-y divide-slate-200 dark:divide-zinc-800"
|
||||
items={posts}
|
||||
initialOffset={listEl?.offsetTop}
|
||||
overscan={500}
|
||||
bind:restore={postFeeds.value[feedId].clientData}
|
||||
bind:this={listComp}
|
||||
>
|
||||
<div
|
||||
style="position: absolute; top: 0; left: 0; width: 100%; transform: translateY({items?.[0]
|
||||
? items?.[0]?.start - $virtualizer.options.scrollMargin
|
||||
: 0}px);"
|
||||
class="divide-y divide-slate-200 dark:divide-zinc-800"
|
||||
id="feed"
|
||||
>
|
||||
{#each items as row, index (posts[row.index]?.post.id)}
|
||||
<li
|
||||
bind:this={virtualItemEls[index]}
|
||||
data-index={row.index}
|
||||
style={row.index < 7 ? `--anim-delay: ${index * 100}ms` : ''}
|
||||
class="relative post-container {row.index < 7
|
||||
? 'pop-in opacity-0'
|
||||
: ''} -mx-4 sm:-mx-6 px-4 sm:px-6"
|
||||
>
|
||||
<Post
|
||||
bind:post={posts[row.index]}
|
||||
hideCommunity={community}
|
||||
view={(posts[row.index]?.post.featured_community ||
|
||||
posts[row.index]?.post.featured_local) &&
|
||||
settings.posts.compactFeatured
|
||||
? 'compact'
|
||||
: settings.view}
|
||||
class="transition-all duration-250"
|
||||
onhide={() => {
|
||||
posts = posts.toSpliced(row.index, 1)
|
||||
}}
|
||||
></Post>
|
||||
</li>
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
{#snippet item(_, row, __)}
|
||||
<li
|
||||
data-index={row}
|
||||
style={row < 7 ? `--anim-delay: ${row * 100}ms` : ''}
|
||||
class="relative post-container {row < 7
|
||||
? 'pop-in opacity-0'
|
||||
: ''} -mx-4 sm:-mx-6 px-4 sm:px-6"
|
||||
>
|
||||
<Post
|
||||
bind:post={posts[row]}
|
||||
hideCommunity={community}
|
||||
view={(posts[row]?.post.featured_community ||
|
||||
posts[row]?.post.featured_local) &&
|
||||
settings.posts.compactFeatured
|
||||
? 'compact'
|
||||
: settings.view}
|
||||
class="transition-all duration-250"
|
||||
onhide={() => {
|
||||
posts = posts.toSpliced(row, 1)
|
||||
}}
|
||||
></Post>
|
||||
</li>
|
||||
{/snippet}
|
||||
</VirtualList>
|
||||
{/if}
|
||||
{/key}
|
||||
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
<script lang="ts" generics="T">
|
||||
import { browser } from '$app/environment'
|
||||
import { debounce } from 'mono-svelte/util/time'
|
||||
import { onDestroy, tick, untrack, type Snippet } from 'svelte'
|
||||
import type { HTMLAttributes } from 'svelte/elements'
|
||||
import { innerHeight } from 'svelte/reactivity/window'
|
||||
|
||||
interface Props extends HTMLAttributes<HTMLDivElement> {
|
||||
items: T[]
|
||||
estimatedHeight?: number
|
||||
overscan?: number
|
||||
item: Snippet<[T, number, Function]>
|
||||
restore?: {
|
||||
itemHeights: (number | null)[]
|
||||
}
|
||||
initialOffset?: number
|
||||
}
|
||||
|
||||
let {
|
||||
items,
|
||||
estimatedHeight = 100,
|
||||
overscan = 5,
|
||||
item: itemSnippet,
|
||||
initialOffset = 0,
|
||||
restore = $bindable(),
|
||||
...rest
|
||||
}: Props = $props()
|
||||
|
||||
export function scrollToIndex(index: number, useWindow: boolean = false) {
|
||||
scrollPosition = cumulativeItemHeights[index] - initialOffset || 0
|
||||
if (useWindow && browser)
|
||||
window.scrollTo({ behavior: 'instant', top: scrollPosition })
|
||||
}
|
||||
|
||||
onDestroy(() => {
|
||||
restore = {
|
||||
itemHeights: itemHeights,
|
||||
}
|
||||
})
|
||||
|
||||
let virtualListEl = $state<HTMLElement>()
|
||||
|
||||
let itemHeights = $state<(number | null)[]>([
|
||||
...(restore?.itemHeights ?? Array(items.length).fill(null)),
|
||||
])
|
||||
let cumulativeItemHeights = $derived.by(() => {
|
||||
let cumulation = new Array(itemHeights.length)
|
||||
let sum = 0
|
||||
|
||||
for (let i = 0; i < itemHeights.length; i++) {
|
||||
const height = itemHeights[i] || estimatedHeight
|
||||
sum += height
|
||||
cumulation[i] = sum
|
||||
}
|
||||
|
||||
return cumulation
|
||||
})
|
||||
|
||||
let scrollPosition = $state(0)
|
||||
let viewportHeight = $state(0)
|
||||
let visibleItems = $state<{ index: number; offset: number }[]>([])
|
||||
|
||||
$effect.pre(() => {
|
||||
if (virtualListEl) {
|
||||
untrack(() => {
|
||||
visibleItems = updateVisibleItems()
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
$effect.pre(() => {
|
||||
if (items.length > itemHeights.length) {
|
||||
const missing = items.length - itemHeights.length
|
||||
itemHeights = [...itemHeights, ...Array(missing).fill(null)]
|
||||
}
|
||||
})
|
||||
|
||||
function findFirstVisibleIndex(
|
||||
scrollTop: number,
|
||||
cumulativeHeights: number[],
|
||||
): number {
|
||||
let low = 0
|
||||
let high = cumulativeHeights.length - 1
|
||||
let mid = 0
|
||||
|
||||
while (low <= high) {
|
||||
mid = Math.floor((low + high) / 2)
|
||||
|
||||
if (cumulativeHeights[mid] <= scrollTop) low = mid + 1
|
||||
else high = mid - 1
|
||||
}
|
||||
|
||||
return low
|
||||
}
|
||||
|
||||
function updateVisibleItems() {
|
||||
if (!virtualListEl) return []
|
||||
|
||||
viewportHeight = innerHeight?.current ?? 1000
|
||||
const scrollTop = scrollPosition - initialOffset
|
||||
|
||||
let newVisibleItems: { index: number; offset: number }[] = []
|
||||
|
||||
const firstIndex = findFirstVisibleIndex(scrollTop, cumulativeItemHeights)
|
||||
|
||||
let i = firstIndex
|
||||
let offset = i == 0 ? 0 : cumulativeItemHeights[i - 1]
|
||||
|
||||
while (i < items.length && offset < scrollTop + viewportHeight + overscan) {
|
||||
newVisibleItems.push({ index: i, offset: offset })
|
||||
const height = itemHeights[i] || estimatedHeight
|
||||
offset += height
|
||||
i++
|
||||
}
|
||||
|
||||
return newVisibleItems ?? []
|
||||
}
|
||||
|
||||
function resizeObserver(node: HTMLElement) {
|
||||
const observer = new ResizeObserver((entries) => {
|
||||
for (const entry of entries) {
|
||||
const indexAttr = node.getAttribute('data-index')
|
||||
if (indexAttr === null) continue
|
||||
const index = Number(indexAttr)
|
||||
if (isNaN(index)) continue
|
||||
|
||||
const newHeight = entry.contentRect.height
|
||||
if (itemHeights[index] !== newHeight) {
|
||||
itemHeights[index] = newHeight
|
||||
visibleItems = updateVisibleItems()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
observer.observe(node)
|
||||
|
||||
onDestroy(() => {
|
||||
observer.disconnect()
|
||||
})
|
||||
|
||||
return {
|
||||
destroy() {
|
||||
observer.disconnect()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
let oldScroll = $state(0)
|
||||
$effect(() => {
|
||||
if (Math.abs(scrollPosition - oldScroll) > estimatedHeight) {
|
||||
visibleItems = updateVisibleItems()
|
||||
oldScroll = scrollPosition
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<svelte:window bind:scrollY={scrollPosition} />
|
||||
|
||||
<div
|
||||
bind:this={virtualListEl}
|
||||
style="position: relative; height: {cumulativeItemHeights[
|
||||
visibleItems?.[visibleItems.length - 1]?.index
|
||||
]}px;"
|
||||
{...rest}
|
||||
id="feed"
|
||||
>
|
||||
<div
|
||||
style="height: {cumulativeItemHeights[visibleItems?.[0]?.index - 1] ||
|
||||
0}px; border: 0 !important;"
|
||||
></div>
|
||||
{#each visibleItems as item (item.index)}
|
||||
<div
|
||||
data-index={item.index}
|
||||
class="post-container fix-divide"
|
||||
use:resizeObserver
|
||||
>
|
||||
{@render itemSnippet(items[item.index], item.index, () => {})}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.fix-divide:nth-child(2) {
|
||||
border-top: 0;
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user