perf: use a virtual list
This commit is contained in:
1 parent
25b1ab8d8e
commit
58b6c12a8b
5 files changed
+82
-138
No files matched your search
+12
-12
@@ -1,17 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "photon-lemmy",
|
"name": "photon-lemmy",
|
||||||
"version": "1.30.4",
|
"version": "1.30.4",
|
||||||
"private": true,
|
|
||||||
"scripts": {
|
|
||||||
"dev": "vite dev",
|
|
||||||
"build": "vite build",
|
|
||||||
"test": "vitest",
|
|
||||||
"preview": "vite preview",
|
|
||||||
"start": "node build/index.js",
|
|
||||||
"bunstart": "bun build/index.js",
|
|
||||||
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
||||||
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@catdadcode/svelte-adapter-bun": "^0.5.7",
|
"@catdadcode/svelte-adapter-bun": "^0.5.7",
|
||||||
"@dicebear/core": "^8.0.1",
|
"@dicebear/core": "^8.0.1",
|
||||||
@@ -22,6 +11,7 @@
|
|||||||
"@sveltejs/kit": "latest",
|
"@sveltejs/kit": "latest",
|
||||||
"@sveltejs/vite-plugin-svelte": "^3.0.0",
|
"@sveltejs/vite-plugin-svelte": "^3.0.0",
|
||||||
"@tailwindcss/container-queries": "^0.1.1",
|
"@tailwindcss/container-queries": "^0.1.1",
|
||||||
|
"@tanstack/svelte-virtual": "^3.8.1",
|
||||||
"@types/markdown-it": "^12.2.3",
|
"@types/markdown-it": "^12.2.3",
|
||||||
"@types/markdown-it-container": "^2.0.6",
|
"@types/markdown-it-container": "^2.0.6",
|
||||||
"@types/nprogress": "^0.2.0",
|
"@types/nprogress": "^0.2.0",
|
||||||
@@ -41,7 +31,6 @@
|
|||||||
"svelte-check": "^3.0.1",
|
"svelte-check": "^3.0.1",
|
||||||
"svelte-hero-icons": "^5.0.0",
|
"svelte-hero-icons": "^5.0.0",
|
||||||
"svelte-infinite-scroll": "^2.0.1",
|
"svelte-infinite-scroll": "^2.0.1",
|
||||||
"svelte-tiny-virtual-list": "^2.0.5",
|
|
||||||
"sveltekit-i18n": "^2.4.2",
|
"sveltekit-i18n": "^2.4.2",
|
||||||
"tailwindcss": "^3.3.3",
|
"tailwindcss": "^3.3.3",
|
||||||
"tslib": "^2.4.1",
|
"tslib": "^2.4.1",
|
||||||
@@ -51,5 +40,16 @@
|
|||||||
"vitest": "^1.0.0",
|
"vitest": "^1.0.0",
|
||||||
"workbox-window": "^7.0.0"
|
"workbox-window": "^7.0.0"
|
||||||
},
|
},
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite dev",
|
||||||
|
"build": "vite build",
|
||||||
|
"test": "vitest",
|
||||||
|
"preview": "vite preview",
|
||||||
|
"start": "node build/index.js",
|
||||||
|
"bunstart": "bun build/index.js",
|
||||||
|
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
||||||
|
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
|
||||||
|
},
|
||||||
"type": "module"
|
"type": "module"
|
||||||
}
|
}
|
||||||
@@ -70,6 +70,7 @@
|
|||||||
on:click={(e) => {
|
on:click={(e) => {
|
||||||
onClick(e)
|
onClick(e)
|
||||||
}}
|
}}
|
||||||
|
style={$$props.style ?? ''}
|
||||||
>
|
>
|
||||||
<PostMeta
|
<PostMeta
|
||||||
community={hideCommunity ? undefined : post.community}
|
community={hideCommunity ? undefined : post.community}
|
||||||
@@ -108,7 +109,7 @@
|
|||||||
<PostMediaCompact
|
<PostMediaCompact
|
||||||
{view}
|
{view}
|
||||||
bind:post={post.post}
|
bind:post={post.post}
|
||||||
bind:type={type}
|
bind:type
|
||||||
class="{$userSettings.leftAlign
|
class="{$userSettings.leftAlign
|
||||||
? 'mr-2'
|
? 'mr-2'
|
||||||
: 'ml-2'} flex-shrink no-list-margin"
|
: 'ml-2'} flex-shrink no-list-margin"
|
||||||
|
|||||||
@@ -7,6 +7,9 @@
|
|||||||
import { ArchiveBox, Icon, Minus, Plus } from 'svelte-hero-icons'
|
import { ArchiveBox, Icon, Minus, Plus } from 'svelte-hero-icons'
|
||||||
import { expoOut } from 'svelte/easing'
|
import { expoOut } from 'svelte/easing'
|
||||||
import { fly, slide } from 'svelte/transition'
|
import { fly, slide } from 'svelte/transition'
|
||||||
|
import { browser } from '$app/environment'
|
||||||
|
import { afterUpdate, type SvelteComponent } from 'svelte'
|
||||||
|
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
|
||||||
|
|
||||||
type PostViewWithCrossposts = PostView & {
|
type PostViewWithCrossposts = PostView & {
|
||||||
withCrossposts: true
|
withCrossposts: true
|
||||||
@@ -69,6 +72,36 @@
|
|||||||
$: combinedPosts = combineCrossposts(posts)
|
$: combinedPosts = combineCrossposts(posts)
|
||||||
|
|
||||||
let viewPost: number = -1
|
let viewPost: number = -1
|
||||||
|
|
||||||
|
let list: any
|
||||||
|
|
||||||
|
function calculateHeight(index: number): number {
|
||||||
|
if (browser) {
|
||||||
|
const el = document.querySelector(`[data-index="${index}"]`)
|
||||||
|
return el?.clientHeight ?? 150
|
||||||
|
} else return 150
|
||||||
|
|
||||||
|
return 150
|
||||||
|
}
|
||||||
|
|
||||||
|
afterUpdate(() => {
|
||||||
|
setTimeout(() => {
|
||||||
|
list?.recomputeSizes?.(0)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
const virtualizer = createWindowVirtualizer({
|
||||||
|
count: posts.length,
|
||||||
|
estimateSize: () => 150,
|
||||||
|
})
|
||||||
|
|
||||||
|
let virtualItemEls: HTMLElement[] = []
|
||||||
|
|
||||||
|
$: items = $virtualizer.getVirtualItems()
|
||||||
|
$: {
|
||||||
|
if (virtualItemEls.length)
|
||||||
|
virtualItemEls.forEach((el) => $virtualizer.measureElement(el))
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<ul
|
<ul
|
||||||
@@ -98,58 +131,67 @@
|
|||||||
</Placeholder>
|
</Placeholder>
|
||||||
</div>
|
</div>
|
||||||
{:else}
|
{:else}
|
||||||
{#each combinedPosts as post, index}
|
<!-- {#each combinedPosts as post, index} -->
|
||||||
{#if !($userSettings.hidePosts.deleted && post.post.deleted) && !($userSettings.hidePosts.removed && post.post.removed)}
|
<!-- {#if !($userSettings.hidePosts.deleted && post.post.deleted) && !($userSettings.hidePosts.removed && post.post.removed)} -->
|
||||||
|
<div
|
||||||
|
style="position:relative; height: {$virtualizer.getTotalSize()}px; width: 100%;"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
style="position: absolute; top: 0; left: 0; width: 100%; transform: translateY({items[0]
|
||||||
|
? items[0].start
|
||||||
|
: 0}px);"
|
||||||
|
>
|
||||||
|
{#each items as row, index (row.index)}
|
||||||
<li
|
<li
|
||||||
in:fly|global={{
|
in:fly|global={{
|
||||||
y: -8,
|
y: -8,
|
||||||
duration: index < 4 ? 500 : 0,
|
duration: 500,
|
||||||
opacity: 0,
|
opacity: 0,
|
||||||
delay: index < 4 ? index * 100 : 0,
|
delay: 200,
|
||||||
}}
|
}}
|
||||||
|
bind:this={virtualItemEls[index]}
|
||||||
|
data-index={row.index}
|
||||||
class="relative"
|
class="relative"
|
||||||
>
|
>
|
||||||
<Post
|
<Post
|
||||||
hideCommunity={community}
|
hideCommunity={community}
|
||||||
view={(post.post.featured_community || post.post.featured_local) &&
|
view={(posts[row.index].post.featured_community ||
|
||||||
|
posts[row.index].post.featured_local) &&
|
||||||
$userSettings.posts.compactFeatured
|
$userSettings.posts.compactFeatured
|
||||||
? 'compact'
|
? 'compact'
|
||||||
: $userSettings.view}
|
: $userSettings.view}
|
||||||
{post}
|
post={posts[row.index]}
|
||||||
class="transition-all duration-250 {post.withCrossposts &&
|
class="transition-all duration-250"
|
||||||
viewPost != post.post.id
|
|
||||||
? ''
|
|
||||||
: ''}"
|
|
||||||
on:hide={() => {
|
on:hide={() => {
|
||||||
combinedPosts = combinedPosts.toSpliced(index, 1)
|
posts = posts.toSpliced(row.index, 1)
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<svelte:fragment slot="badges">
|
<svelte:fragment slot="badges">
|
||||||
{#if post.withCrossposts}
|
<!-- {#if posts[index].withCrossposts}
|
||||||
<button
|
<button
|
||||||
on:click={() => {
|
on:click={() => {
|
||||||
if (viewPost == post.post.id) viewPost = -1
|
if (viewPost == posts[index].post.id) viewPost = -1
|
||||||
else viewPost = post.post.id
|
else viewPost = posts[index].post.id
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Badge
|
<Badge
|
||||||
class="z-10 backdrop-blur-xl hover:brightness-110 cursor-pointer transition-all"
|
class="z-10 backdrop-blur-xl hover:brightness-110 cursor-pointer transition-all"
|
||||||
color="gray-subtle"
|
color="gray-subtle"
|
||||||
>
|
>
|
||||||
{#if viewPost == post.post.id}
|
{#if viewPost == posts[index].post.id}
|
||||||
<Icon mini src={Minus} size="14" />
|
<Icon mini src={Minus} size="14" />
|
||||||
{:else}
|
{:else}
|
||||||
<Icon mini src={Plus} size="14" />
|
<Icon mini src={Plus} size="14" />
|
||||||
{/if}
|
{/if}
|
||||||
{post.crossposts.length} crosspost{post.crossposts.length ==
|
{posts[index].crossposts.length} crosspost{posts[index]
|
||||||
1
|
.crossposts.length == 1
|
||||||
? ''
|
? ''
|
||||||
: 's'}
|
: 's'}
|
||||||
</Badge>
|
</Badge>
|
||||||
</button>{/if}
|
</button>{/if} -->
|
||||||
</svelte:fragment>
|
</svelte:fragment>
|
||||||
</Post>
|
</Post>
|
||||||
{#if post.withCrossposts && viewPost == post.post.id}
|
<!-- {#if posts[index].withCrossposts && viewPost == posts[index].post.id}
|
||||||
<div
|
<div
|
||||||
transition:slide|global={{
|
transition:slide|global={{
|
||||||
axis: 'y',
|
axis: 'y',
|
||||||
@@ -162,18 +204,21 @@
|
|||||||
class:my-4={$userSettings.view == 'card'}
|
class:my-4={$userSettings.view == 'card'}
|
||||||
>
|
>
|
||||||
Crossposts <hr class="w-full dark:border-zinc-800" />
|
Crossposts <hr class="w-full dark:border-zinc-800" />
|
||||||
{post.crossposts.length}
|
{posts[index].crossposts.length}
|
||||||
</span>
|
</span>
|
||||||
{#each post.crossposts as crosspost, index}
|
{#each posts[index].crossposts as crosspost, index}
|
||||||
<div class="w-full transition-all mb-4">
|
<div class="w-full transition-all mb-4">
|
||||||
<Post post={crosspost} view={$userSettings.view} />
|
<Post post={crosspost} view={$userSettings.view} />
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if} -->
|
||||||
</li>
|
</li>
|
||||||
{/if}
|
|
||||||
{/each}
|
{/each}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<!-- {/if} -->
|
||||||
|
<!-- {/each} -->
|
||||||
{/if}
|
{/if}
|
||||||
<slot />
|
<slot />
|
||||||
</ul>
|
</ul>
|
||||||
@@ -1,102 +0,0 @@
|
|||||||
<script lang="ts">
|
|
||||||
import { onMount, afterUpdate } from 'svelte'
|
|
||||||
import type { SvelteComponent } from 'svelte'
|
|
||||||
|
|
||||||
interface VisibleItem<T> {
|
|
||||||
item: T
|
|
||||||
index: number
|
|
||||||
top: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export let items: any[] = []
|
|
||||||
|
|
||||||
let containerHeight = 0
|
|
||||||
let scrollTop = 0
|
|
||||||
let itemHeights: number[] = []
|
|
||||||
let visibleItems: VisibleItem<any>[] = []
|
|
||||||
let totalHeight = 0
|
|
||||||
let container: HTMLDivElement
|
|
||||||
|
|
||||||
$: {
|
|
||||||
let currentOffset = 0
|
|
||||||
let startIndex = 0
|
|
||||||
let endIndex = items.length - 1
|
|
||||||
|
|
||||||
// Find the start index
|
|
||||||
for (let i = 0; i < items.length; i++) {
|
|
||||||
if (currentOffset + itemHeights[i] > scrollTop) {
|
|
||||||
startIndex = Math.max(0, i)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
currentOffset += itemHeights[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find the end index
|
|
||||||
currentOffset = 0
|
|
||||||
for (let i = 0; i < items.length; i++) {
|
|
||||||
if (currentOffset > scrollTop + containerHeight) {
|
|
||||||
endIndex = Math.min(items.length - 1, i)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
currentOffset += itemHeights[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
visibleItems = items.slice(startIndex, endIndex + 1).map((item, index) => ({
|
|
||||||
item,
|
|
||||||
index: startIndex + index,
|
|
||||||
top: itemHeights.slice(0, startIndex + index).reduce((a, b) => a + b, 0),
|
|
||||||
}))
|
|
||||||
|
|
||||||
console.log(visibleItems.length == items.length)
|
|
||||||
|
|
||||||
totalHeight = itemHeights.reduce((a, b) => a + b, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleScroll(event: Event): void {
|
|
||||||
const target = event.target as HTMLDivElement
|
|
||||||
scrollTop = target.scrollTop
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateItemHeight(index: number, height: number): void {
|
|
||||||
if (itemHeights[index] !== height) {
|
|
||||||
itemHeights[index] = height
|
|
||||||
itemHeights = itemHeights
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
onMount(() => {
|
|
||||||
containerHeight = container.clientHeight
|
|
||||||
// Force an update
|
|
||||||
items = items
|
|
||||||
})
|
|
||||||
|
|
||||||
afterUpdate(() => {
|
|
||||||
// Measure item heights after render
|
|
||||||
visibleItems.forEach(({ index }) => {
|
|
||||||
const element = container.querySelector(
|
|
||||||
`[data-index="${index}"]`
|
|
||||||
) as HTMLDivElement
|
|
||||||
if (element) {
|
|
||||||
updateItemHeight(index, element.offsetHeight)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<svelte:window on:scroll={handleScroll} />
|
|
||||||
|
|
||||||
<div
|
|
||||||
bind:this={container}
|
|
||||||
style="height: 100%; width: 100%; overflow-x: hidden; overflow-y: scroll;"
|
|
||||||
>
|
|
||||||
<div style="height: {totalHeight}px; position: relative;">
|
|
||||||
{#each visibleItems as { item, index, top }}
|
|
||||||
<div
|
|
||||||
data-index={index}
|
|
||||||
style="position: absolute; top: {top}px; width: 100%;"
|
|
||||||
>
|
|
||||||
<slot {item} {index} />
|
|
||||||
</div>
|
|
||||||
{/each}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
Reference in new issue
Block a user