misc: change preloading strategy

This commit is contained in:
Xylight committed 2024-07-03 12:11:28 -07:00
1 parent 322874dafa
commit 92e6c9ef9f
5 files changed
+127 -4

No files matched your search

@@ -16,6 +16,7 @@
class="items-center flex flex-row gap-2 hover:underline max-w-full min-w-0 {$$props.class ||
''}"
href="/c/{community.name}@{new URL(community.actor_id).hostname}"
data-sveltekit-preload-data="tap"
>
{#if avatar}
<Avatar
@@ -180,6 +180,7 @@
class:text-slate-600={$userSettings.markReadPosts && read}
class:dark:text-zinc-400={$userSettings.markReadPosts && read}
style="grid-area: title;"
data-sveltekit-preload-data="tap"
>
<Markdown source={title} inline noStyle class="leading-[1.3]"></Markdown>
</a>
@@ -19,6 +19,7 @@
class="items-center flex flex-row gap-1 hover:underline max-w-full min-w-0 {$$props.class ??
''}"
href="/u/{user.name}@{new URL(user.actor_id).hostname}"
data-sveltekit-preload-data="tap"
>
{#if avatar}
<Avatar
+102
View File
@@ -0,0 +1,102 @@
<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>