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
+273
-134
No files matched your search
@@ -12,7 +12,6 @@
|
|||||||
"@sveltejs/kit": "^2.17.1",
|
"@sveltejs/kit": "^2.17.1",
|
||||||
"@sveltejs/vite-plugin-svelte": "^4.0.0",
|
"@sveltejs/vite-plugin-svelte": "^4.0.0",
|
||||||
"@tailwindcss/container-queries": "^0.1.1",
|
"@tailwindcss/container-queries": "^0.1.1",
|
||||||
"@tanstack/svelte-virtual": "^3.8.1",
|
|
||||||
"@types/nprogress": "^0.2.0",
|
"@types/nprogress": "^0.2.0",
|
||||||
"@types/ua-parser-js": "^0.7.39",
|
"@types/ua-parser-js": "^0.7.39",
|
||||||
"autoprefixer": "^10.4.16",
|
"autoprefixer": "^10.4.16",
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { run } from 'svelte/legacy'
|
|
||||||
|
|
||||||
import type { Post } from 'lemmy-js-client'
|
import type { Post } from 'lemmy-js-client'
|
||||||
import type { CommentNodeI } from './comments.svelte'
|
import type { CommentNodeI } from './comments.svelte'
|
||||||
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
|
|
||||||
import Comments from './Comments.svelte'
|
import Comments from './Comments.svelte'
|
||||||
import { browser } from '$app/environment'
|
|
||||||
import { onMount } from 'svelte'
|
import { onMount } from 'svelte'
|
||||||
|
import VirtualList from '$lib/components/render/VirtualList.svelte'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
nodes: CommentNodeI[]
|
nodes: CommentNodeI[]
|
||||||
@@ -16,31 +13,7 @@
|
|||||||
|
|
||||||
let { nodes, post, scrollTo }: Props = $props()
|
let { nodes, post, scrollTo }: Props = $props()
|
||||||
|
|
||||||
let virtualListEl = $state<HTMLElement>()
|
let offsetEl = $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()
|
|
||||||
})
|
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
if (scrollTo) {
|
if (scrollTo) {
|
||||||
@@ -52,33 +25,21 @@
|
|||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div bind:this={offsetEl}>
|
||||||
style="position:relative; height: {browser
|
<VirtualList
|
||||||
? `${$virtualizer.getTotalSize()}px`
|
class="divide-y divide-slate-200 dark:divide-zinc-800 w-full"
|
||||||
: '100%'}; width: 100%;"
|
overscan={500}
|
||||||
bind:this={virtualListEl}
|
items={nodes}
|
||||||
>
|
initialOffset={offsetEl?.offsetTop}
|
||||||
<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"
|
|
||||||
>
|
|
||||||
{#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
|
<Comments
|
||||||
isParent={true}
|
isParent={true}
|
||||||
bind:nodes={
|
bind:nodes={() => [nodes[index]], (v) => (nodes[index] = v[0])}
|
||||||
() => [nodes[row.index]], (v) => (nodes[row.index] = v[0])
|
|
||||||
}
|
|
||||||
{post}
|
{post}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/snippet}
|
||||||
</div>
|
</VirtualList>
|
||||||
</div>
|
</div>
|
||||||
@@ -1,8 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { run } from 'svelte/legacy'
|
|
||||||
|
|
||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { afterNavigate } from '$app/navigation'
|
|
||||||
import Post from '$lib/components/lemmy/post/Post.svelte'
|
import Post from '$lib/components/lemmy/post/Post.svelte'
|
||||||
import EndPlaceholder from '$lib/components/ui/EndPlaceholder.svelte'
|
import EndPlaceholder from '$lib/components/ui/EndPlaceholder.svelte'
|
||||||
import Placeholder from '$lib/components/ui/Placeholder.svelte'
|
import Placeholder from '$lib/components/ui/Placeholder.svelte'
|
||||||
@@ -14,10 +11,9 @@
|
|||||||
} from '$lib/lemmy/postfeed.svelte'
|
} from '$lib/lemmy/postfeed.svelte'
|
||||||
import { settings } from '$lib/settings.svelte.js'
|
import { settings } from '$lib/settings.svelte.js'
|
||||||
import { t } from '$lib/translations'
|
import { t } from '$lib/translations'
|
||||||
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
|
|
||||||
import type { PostView } from 'lemmy-js-client'
|
import type { PostView } from 'lemmy-js-client'
|
||||||
import { Button } from 'mono-svelte'
|
import { Button } from 'mono-svelte'
|
||||||
import { onMount } from 'svelte'
|
import { onMount, tick, untrack } from 'svelte'
|
||||||
import {
|
import {
|
||||||
ArchiveBox,
|
ArchiveBox,
|
||||||
ChevronDoubleUp,
|
ChevronDoubleUp,
|
||||||
@@ -26,13 +22,7 @@
|
|||||||
Plus,
|
Plus,
|
||||||
} from 'svelte-hero-icons'
|
} from 'svelte-hero-icons'
|
||||||
import InfiniteScroll from 'svelte-infinite-scroll'
|
import InfiniteScroll from 'svelte-infinite-scroll'
|
||||||
|
import VirtualList from '$lib/components/render/VirtualList.svelte'
|
||||||
let virtualItemEls: HTMLElement[] = $state([])
|
|
||||||
let virtualListEl: HTMLElement | undefined = $state(undefined)
|
|
||||||
|
|
||||||
afterNavigate(() => {
|
|
||||||
$virtualizer.scrollToIndex(postFeeds.value[feedId]?.lastSeen ?? 0)
|
|
||||||
})
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
posts: PostView[]
|
posts: PostView[]
|
||||||
@@ -50,14 +40,10 @@
|
|||||||
children,
|
children,
|
||||||
}: Props = $props()
|
}: Props = $props()
|
||||||
|
|
||||||
const virtualizer = createWindowVirtualizer({
|
let listEl = $state<HTMLUListElement>()
|
||||||
count: posts.length,
|
let listComp = $state<{
|
||||||
estimateSize: () => 150,
|
scrollToIndex: (index: number, window?: boolean) => void
|
||||||
overscan: 5,
|
}>()
|
||||||
measureElement: (element, entry, instance) => {
|
|
||||||
return element.scrollHeight
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
let error: any = $state(undefined)
|
let error: any = $state(undefined)
|
||||||
let loading = $state(false)
|
let loading = $state(false)
|
||||||
@@ -92,7 +78,7 @@
|
|||||||
hasMore = newPosts.posts.length != 0
|
hasMore = newPosts.posts.length != 0
|
||||||
|
|
||||||
feedData.cursor.next = newPosts.next_page
|
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
|
postFeeds.value[feedId].data = feedData
|
||||||
|
|
||||||
@@ -120,63 +106,50 @@
|
|||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
const observer = new IntersectionObserver(callback, {
|
const observer = new IntersectionObserver(callback, {
|
||||||
root: null,
|
|
||||||
rootMargin: '0px',
|
|
||||||
threshold: 0.5,
|
threshold: 0.5,
|
||||||
})
|
})
|
||||||
|
|
||||||
const elements = document.querySelectorAll('.post-container')
|
const observePost = (node: Node) => {
|
||||||
elements.forEach((el) => observer.observe(el))
|
|
||||||
|
|
||||||
const postContainer = document.querySelector('#feed')
|
|
||||||
if (postContainer) {
|
|
||||||
const mutationObserver = new MutationObserver((mutations) => {
|
|
||||||
mutations.forEach((mutation) => {
|
|
||||||
if (mutation.type === 'childList') {
|
|
||||||
mutation.addedNodes.forEach((node) => {
|
|
||||||
if (
|
if (
|
||||||
node instanceof HTMLElement &&
|
node instanceof HTMLElement &&
|
||||||
node.classList.contains('post-container')
|
node.classList.contains('post-container')
|
||||||
) {
|
)
|
||||||
observer.observe(node)
|
observer.observe(node)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
mutation.removedNodes.forEach((node) => {
|
const unobservePost = (node: Node) => {
|
||||||
if (
|
if (
|
||||||
node instanceof HTMLElement &&
|
node instanceof HTMLElement &&
|
||||||
node.classList.contains('post-container')
|
node.classList.contains('post-container')
|
||||||
) {
|
)
|
||||||
observer.unobserve(node)
|
observer.unobserve(node)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
mutationObserver.observe(postContainer, {
|
document.querySelectorAll('.post-container').forEach(observePost)
|
||||||
childList: true,
|
|
||||||
subtree: true,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
let items = $derived($virtualizer.getVirtualItems())
|
const feed = document.getElementById('feed')
|
||||||
|
if (!feed) return
|
||||||
|
|
||||||
$effect(() => {
|
new MutationObserver((mutations) => {
|
||||||
if (virtualItemEls.length)
|
mutations.forEach(({ addedNodes, removedNodes }) => {
|
||||||
virtualItemEls.forEach($virtualizer.measureElement)
|
addedNodes.forEach(observePost)
|
||||||
|
removedNodes.forEach(unobservePost)
|
||||||
|
})
|
||||||
|
}).observe(feed, { childList: true, subtree: false })
|
||||||
})
|
})
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if (posts.length && virtualListEl)
|
if (listComp) {
|
||||||
$virtualizer.setOptions({
|
untrack(() => {
|
||||||
scrollMargin: virtualListEl?.offsetTop,
|
if (postFeeds.value[feedId].lastSeen != 0) {
|
||||||
count: posts.length,
|
listComp?.scrollToIndex(postFeeds.value[feedId].lastSeen, true)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<ul class="flex flex-col list-none">
|
<ul class="flex flex-col list-none" bind:this={listEl}>
|
||||||
{#key posts}
|
{#key posts}
|
||||||
{#if posts?.length == 0}
|
{#if posts?.length == 0}
|
||||||
<div class="h-full grid place-items-center">
|
<div class="h-full grid place-items-center">
|
||||||
@@ -194,45 +167,39 @@
|
|||||||
</Placeholder>
|
</Placeholder>
|
||||||
</div>
|
</div>
|
||||||
{:else}
|
{:else}
|
||||||
<div
|
<VirtualList
|
||||||
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"
|
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}
|
||||||
>
|
>
|
||||||
{#each items as row, index (posts[row.index]?.post.id)}
|
{#snippet item(_, row, __)}
|
||||||
<li
|
<li
|
||||||
bind:this={virtualItemEls[index]}
|
data-index={row}
|
||||||
data-index={row.index}
|
style={row < 7 ? `--anim-delay: ${row * 100}ms` : ''}
|
||||||
style={row.index < 7 ? `--anim-delay: ${index * 100}ms` : ''}
|
class="relative post-container {row < 7
|
||||||
class="relative post-container {row.index < 7
|
|
||||||
? 'pop-in opacity-0'
|
? 'pop-in opacity-0'
|
||||||
: ''} -mx-4 sm:-mx-6 px-4 sm:px-6"
|
: ''} -mx-4 sm:-mx-6 px-4 sm:px-6"
|
||||||
>
|
>
|
||||||
<Post
|
<Post
|
||||||
bind:post={posts[row.index]}
|
bind:post={posts[row]}
|
||||||
hideCommunity={community}
|
hideCommunity={community}
|
||||||
view={(posts[row.index]?.post.featured_community ||
|
view={(posts[row]?.post.featured_community ||
|
||||||
posts[row.index]?.post.featured_local) &&
|
posts[row]?.post.featured_local) &&
|
||||||
settings.posts.compactFeatured
|
settings.posts.compactFeatured
|
||||||
? 'compact'
|
? 'compact'
|
||||||
: settings.view}
|
: settings.view}
|
||||||
class="transition-all duration-250"
|
class="transition-all duration-250"
|
||||||
onhide={() => {
|
onhide={() => {
|
||||||
posts = posts.toSpliced(row.index, 1)
|
posts = posts.toSpliced(row, 1)
|
||||||
}}
|
}}
|
||||||
></Post>
|
></Post>
|
||||||
</li>
|
</li>
|
||||||
{/each}
|
{/snippet}
|
||||||
</div>
|
</VirtualList>
|
||||||
</div>
|
|
||||||
{/if}
|
{/if}
|
||||||
{/key}
|
{/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>
|
||||||
@@ -283,7 +283,8 @@
|
|||||||
"title": "No comments",
|
"title": "No comments",
|
||||||
"description": "Start the conversation!"
|
"description": "Start the conversation!"
|
||||||
},
|
},
|
||||||
"commented": "Your comment was posted."
|
"commented": "Your comment was posted.",
|
||||||
|
"loadComments": "Load comments"
|
||||||
},
|
},
|
||||||
"inbox": {
|
"inbox": {
|
||||||
"title": "Inbox",
|
"title": "Inbox",
|
||||||
|
|||||||
@@ -49,6 +49,9 @@ export async function postFeed(args: {
|
|||||||
url: args.url,
|
url: args.url,
|
||||||
lastSeen: 0,
|
lastSeen: 0,
|
||||||
instance: instance.data,
|
instance: instance.data,
|
||||||
|
clientData: {
|
||||||
|
itemHeights: [],
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -96,6 +99,9 @@ export interface PostFeed {
|
|||||||
lastSeen: number
|
lastSeen: number
|
||||||
instance: string
|
instance: string
|
||||||
id: PostFeedID
|
id: PostFeedID
|
||||||
|
clientData: {
|
||||||
|
itemHeights: (number | null)[]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export let postFeeds = new PostFeedState()
|
export let postFeeds = new PostFeedState()
|
||||||
@@ -374,6 +374,13 @@
|
|||||||
{$t('routes.post.commentCount')}
|
{$t('routes.post.commentCount')}
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
{#if !page.url.searchParams.get('noVirtualize')}
|
||||||
|
<noscript>
|
||||||
|
<Button href="?noVirtualize=true" class="block">
|
||||||
|
{$t('routes.post.loadComments')}
|
||||||
|
</Button>
|
||||||
|
</noscript>
|
||||||
|
{/if}
|
||||||
{#await data.comments.value}
|
{#await data.comments.value}
|
||||||
<div class="flex flex-col gap-4">
|
<div class="flex flex-col gap-4">
|
||||||
{#each new Array(10) as empty, index}
|
{#each new Array(10) as empty, index}
|
||||||
@@ -400,6 +407,7 @@
|
|||||||
focus={data.thread.value.focus}
|
focus={data.thread.value.focus}
|
||||||
onupdate={reloadComments}
|
onupdate={reloadComments}
|
||||||
bind:sort={data.commentSort.value}
|
bind:sort={data.commentSort.value}
|
||||||
|
virtualize={!page.url.searchParams.get('noVirtualize')}
|
||||||
/>
|
/>
|
||||||
{#if comments.comments.length == 0}
|
{#if comments.comments.length == 0}
|
||||||
<Placeholder
|
<Placeholder
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
import { profile } from '$lib/auth.svelte'
|
import { profile } from '$lib/auth.svelte'
|
||||||
import CommentForm from '$lib/components/lemmy/comment/CommentForm.svelte'
|
import CommentForm from '$lib/components/lemmy/comment/CommentForm.svelte'
|
||||||
import CommentListVirtualizer from '$lib/components/lemmy/comment/CommentListVirtualizer.svelte'
|
import CommentListVirtualizer from '$lib/components/lemmy/comment/CommentListVirtualizer.svelte'
|
||||||
|
import Comments from '$lib/components/lemmy/comment/Comments.svelte'
|
||||||
import { buildCommentsTree } from '$lib/components/lemmy/comment/comments.svelte'
|
import { buildCommentsTree } from '$lib/components/lemmy/comment/comments.svelte'
|
||||||
import EndPlaceholder from '$lib/components/ui/EndPlaceholder.svelte'
|
import EndPlaceholder from '$lib/components/ui/EndPlaceholder.svelte'
|
||||||
import { instance } from '$lib/instance.svelte'
|
import { instance } from '$lib/instance.svelte'
|
||||||
@@ -32,6 +33,7 @@
|
|||||||
sort?: CommentSortType
|
sort?: CommentSortType
|
||||||
onupdate?: () => void
|
onupdate?: () => void
|
||||||
focus?: string
|
focus?: string
|
||||||
|
virtualize?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
let {
|
let {
|
||||||
@@ -40,6 +42,7 @@
|
|||||||
sort = $bindable(),
|
sort = $bindable(),
|
||||||
onupdate,
|
onupdate,
|
||||||
focus,
|
focus,
|
||||||
|
virtualize = true,
|
||||||
}: Props = $props()
|
}: Props = $props()
|
||||||
let comments = $state(passedComments)
|
let comments = $state(passedComments)
|
||||||
|
|
||||||
@@ -128,8 +131,16 @@
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
<CommentListVirtualizer
|
{#if virtualize}
|
||||||
|
<CommentListVirtualizer
|
||||||
post={post.post_view.post}
|
post={post.post_view.post}
|
||||||
nodes={tree}
|
nodes={tree}
|
||||||
scrollTo={focus}
|
scrollTo={focus}
|
||||||
/>
|
/>
|
||||||
|
{:else}
|
||||||
|
<div class="divide-y divide-slate-200 dark:divide-zinc-800">
|
||||||
|
<div class="-mx-4 sm:-mx-6 px-4 sm:px-6">
|
||||||
|
<Comments isParent={true} bind:nodes={tree} post={post.post_view.post} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
Reference in new issue
Block a user