perf: virtualize comment lists
This commit is contained in:
1 parent
17ad851ca5
commit
345d4a7366
2 files changed
+54
-2
No files matched your search
@@ -0,0 +1,52 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import type { Post } from 'lemmy-js-client'
|
||||||
|
import type { CommentNodeI } from './comments'
|
||||||
|
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
|
||||||
|
import Comments from './Comments.svelte'
|
||||||
|
import { browser } from '$app/environment'
|
||||||
|
|
||||||
|
export let nodes: CommentNodeI[]
|
||||||
|
export let post: Post
|
||||||
|
|
||||||
|
let virtualListEl: HTMLElement | undefined = undefined
|
||||||
|
|
||||||
|
$: virtualizer = createWindowVirtualizer({
|
||||||
|
count: nodes.length,
|
||||||
|
estimateSize: () => 30,
|
||||||
|
scrollMargin: virtualListEl?.offsetTop,
|
||||||
|
initialRect: {
|
||||||
|
height: 1500,
|
||||||
|
width: 99999,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
let virtualItemEls: HTMLElement[] = []
|
||||||
|
|
||||||
|
$: items = $virtualizer.getVirtualItems()
|
||||||
|
|
||||||
|
$: {
|
||||||
|
if (virtualItemEls.length)
|
||||||
|
virtualItemEls.forEach((el) => $virtualizer.measureElement(el))
|
||||||
|
}
|
||||||
|
</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-800"
|
||||||
|
id="feed"
|
||||||
|
>
|
||||||
|
{#each items as row, index (row.index)}
|
||||||
|
<div bind:this={virtualItemEls[index]} data-index={row.index}>
|
||||||
|
<Comments isParent={true} nodes={[nodes[row.index]]} {post} />
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -55,6 +55,7 @@
|
|||||||
import { resumables } from '$lib/lemmy/item.js'
|
import { resumables } from '$lib/lemmy/item.js'
|
||||||
import { contentPadding } from '$lib/components/ui/layout/Shell.svelte'
|
import { contentPadding } from '$lib/components/ui/layout/Shell.svelte'
|
||||||
import Placeholder from '$lib/components/ui/Placeholder.svelte'
|
import Placeholder from '$lib/components/ui/Placeholder.svelte'
|
||||||
|
import CommentListVirtualizer from '$lib/components/lemmy/comment/CommentListVirtualizer.svelte'
|
||||||
|
|
||||||
export let data
|
export let data
|
||||||
|
|
||||||
@@ -412,7 +413,7 @@
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
<Comments
|
<CommentListVirtualizer
|
||||||
post={post.post_view.post}
|
post={post.post_view.post}
|
||||||
nodes={buildCommentsTree(
|
nodes={buildCommentsTree(
|
||||||
comments.comments,
|
comments.comments,
|
||||||
@@ -423,7 +424,6 @@
|
|||||||
($userSettings.hidePosts.removed && c.comment.removed)
|
($userSettings.hidePosts.removed && c.comment.removed)
|
||||||
)
|
)
|
||||||
)}
|
)}
|
||||||
isParent={true}
|
|
||||||
/>
|
/>
|
||||||
{#if comments.comments.length == 0}
|
{#if comments.comments.length == 0}
|
||||||
<Placeholder
|
<Placeholder
|
||||||
|
|||||||
Reference in new issue
Block a user