fix: scroll position breaking
This commit is contained in:
1 parent
d936f8f035
commit
0b13cbc429
6 files changed
+85
-7
No files matched your search
@@ -40,11 +40,9 @@
|
||||
if (browser) {
|
||||
if ($navigating) {
|
||||
nProgress.start()
|
||||
console.log(`saving scroll at ${window.scrollY}`)
|
||||
}
|
||||
if (!$navigating) {
|
||||
nProgress.done()
|
||||
console.log(`scrolling to ${window.scrollY}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+57
-1
@@ -2,6 +2,7 @@
|
||||
export const _posts = writable<{
|
||||
data: GetPostsResponse
|
||||
params: URLSearchParams
|
||||
lastSeen: number
|
||||
}>(undefined)
|
||||
</script>
|
||||
|
||||
@@ -29,6 +30,9 @@
|
||||
import { writable } from 'svelte/store'
|
||||
import type { GetPostsResponse } from 'lemmy-js-client'
|
||||
import { userSettings } from '$lib/settings.js'
|
||||
import { afterNavigate } from '$app/navigation'
|
||||
import { browser } from '$app/environment'
|
||||
import { onMount } from 'svelte'
|
||||
|
||||
export let data
|
||||
|
||||
@@ -61,7 +65,11 @@
|
||||
data.cursor.next = newPosts.next_page
|
||||
data.posts.posts = [...data.posts.posts, ...newPosts.posts]
|
||||
|
||||
_posts.update((ps) => ({ data: data.posts, params: ps.params }))
|
||||
_posts.update((ps) => ({
|
||||
data: data.posts,
|
||||
params: ps.params,
|
||||
lastSeen: ps.lastSeen,
|
||||
}))
|
||||
|
||||
loading = false
|
||||
} catch (e) {
|
||||
@@ -69,6 +77,54 @@
|
||||
loading = false
|
||||
}
|
||||
}
|
||||
|
||||
const callback: IntersectionObserverCallback = (entries, observer) => {
|
||||
entries.forEach((entry) => {
|
||||
if (!entry.isIntersecting) return
|
||||
|
||||
const element = entry.target as HTMLElement
|
||||
const id = element.getAttribute('data-index')
|
||||
|
||||
if (!id) return
|
||||
|
||||
$_posts.lastSeen = Number(id)
|
||||
observer.unobserve(element)
|
||||
})
|
||||
}
|
||||
|
||||
const observer = new IntersectionObserver(callback, {
|
||||
root: null,
|
||||
rootMargin: '0px',
|
||||
threshold: 0.5,
|
||||
})
|
||||
|
||||
onMount(() => {
|
||||
const elements = document.querySelectorAll('.post-container')
|
||||
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 (
|
||||
node instanceof HTMLElement &&
|
||||
node.classList.contains('post-container')
|
||||
) {
|
||||
observer.observe(node)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
mutationObserver.observe(postContainer, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
})
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col gap-4 max-w-full w-full min-w-0">
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ export async function load({ url, fetch }) {
|
||||
!(cached && url.searchParams.toString() == cached.params.toString()) &&
|
||||
browser
|
||||
)
|
||||
_posts.set({ data: posts, params: url.searchParams })
|
||||
_posts.set({ data: posts, params: url.searchParams, lastSeen: 0 })
|
||||
|
||||
try {
|
||||
return {
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
import Expandable from '$lib/components/ui/Expandable.svelte'
|
||||
import { Popover } from 'mono-svelte'
|
||||
import { t } from '$lib/translations.js'
|
||||
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
|
||||
|
||||
export let data
|
||||
|
||||
@@ -110,7 +111,24 @@
|
||||
$: remoteView =
|
||||
$page.params.instance?.toLowerCase() != $instance.toLowerCase()
|
||||
|
||||
const test = 'wow'
|
||||
const virtualizer = createWindowVirtualizer({
|
||||
count: 0,
|
||||
estimateSize: () => 50,
|
||||
})
|
||||
|
||||
onMount(async () => {
|
||||
const comments = await data.comments
|
||||
|
||||
$virtualizer.setOptions({ count: comments.comments.length })
|
||||
})
|
||||
|
||||
let virtualItemEls: HTMLElement[] = []
|
||||
|
||||
$: items = $virtualizer.getVirtualItems()
|
||||
$: {
|
||||
if (virtualItemEls.length)
|
||||
virtualItemEls.forEach((el) => $virtualizer.measureElement(el))
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
|
||||
Reference in new issue
Block a user