fix: scroll position breaking
This commit is contained in:
1 parent
d936f8f035
commit
0b13cbc429
6 files changed
+85
-7
No files matched your search
@@ -59,7 +59,7 @@
|
|||||||
<!-- svelte-ignore a11y-click-events-have-key-events -->
|
<!-- svelte-ignore a11y-click-events-have-key-events -->
|
||||||
<Material
|
<Material
|
||||||
color={view != 'card' ? 'none' : 'distinct'}
|
color={view != 'card' ? 'none' : 'distinct'}
|
||||||
class="relative max-w-full min-w-0 w-full group cursor-pointer
|
class="post relative max-w-full min-w-0 w-full group cursor-pointer
|
||||||
{view != 'card' ? 'bg-transparent !border-0' : 'p-5'} {view == 'compact'
|
{view != 'card' ? 'bg-transparent !border-0' : 'p-5'} {view == 'compact'
|
||||||
? 'py-4 list-type'
|
? 'py-4 list-type'
|
||||||
: view == 'list'
|
: view == 'list'
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
import { afterUpdate, onMount, tick, type SvelteComponent } from 'svelte'
|
import { afterUpdate, onMount, tick, type SvelteComponent } from 'svelte'
|
||||||
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
|
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
|
||||||
import { afterNavigate, beforeNavigate } from '$app/navigation'
|
import { afterNavigate, beforeNavigate } from '$app/navigation'
|
||||||
|
import { _posts } from '../../../../routes/+page.svelte'
|
||||||
|
|
||||||
type PostViewWithCrossposts = PostView & {
|
type PostViewWithCrossposts = PostView & {
|
||||||
withCrossposts: true
|
withCrossposts: true
|
||||||
@@ -91,6 +92,10 @@
|
|||||||
if (virtualItemEls.length)
|
if (virtualItemEls.length)
|
||||||
virtualItemEls.forEach((el) => $virtualizer.measureElement(el))
|
virtualItemEls.forEach((el) => $virtualizer.measureElement(el))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
afterNavigate(() => {
|
||||||
|
$virtualizer.scrollToIndex($_posts.lastSeen)
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<ul
|
<ul
|
||||||
@@ -130,6 +135,7 @@
|
|||||||
? items[0].start
|
? items[0].start
|
||||||
: 0}px);"
|
: 0}px);"
|
||||||
class="divide-y divide-slate-200 dark:divide-zinc-800"
|
class="divide-y divide-slate-200 dark:divide-zinc-800"
|
||||||
|
id="feed"
|
||||||
>
|
>
|
||||||
{#each items as row, index (row.index)}
|
{#each items as row, index (row.index)}
|
||||||
<li
|
<li
|
||||||
@@ -141,7 +147,7 @@
|
|||||||
}}
|
}}
|
||||||
bind:this={virtualItemEls[index]}
|
bind:this={virtualItemEls[index]}
|
||||||
data-index={row.index}
|
data-index={row.index}
|
||||||
class="relative"
|
class="relative post-container"
|
||||||
>
|
>
|
||||||
<Post
|
<Post
|
||||||
hideCommunity={community}
|
hideCommunity={community}
|
||||||
|
|||||||
@@ -40,11 +40,9 @@
|
|||||||
if (browser) {
|
if (browser) {
|
||||||
if ($navigating) {
|
if ($navigating) {
|
||||||
nProgress.start()
|
nProgress.start()
|
||||||
console.log(`saving scroll at ${window.scrollY}`)
|
|
||||||
}
|
}
|
||||||
if (!$navigating) {
|
if (!$navigating) {
|
||||||
nProgress.done()
|
nProgress.done()
|
||||||
console.log(`scrolling to ${window.scrollY}`)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+57
-1
@@ -2,6 +2,7 @@
|
|||||||
export const _posts = writable<{
|
export const _posts = writable<{
|
||||||
data: GetPostsResponse
|
data: GetPostsResponse
|
||||||
params: URLSearchParams
|
params: URLSearchParams
|
||||||
|
lastSeen: number
|
||||||
}>(undefined)
|
}>(undefined)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -29,6 +30,9 @@
|
|||||||
import { writable } from 'svelte/store'
|
import { writable } from 'svelte/store'
|
||||||
import type { GetPostsResponse } from 'lemmy-js-client'
|
import type { GetPostsResponse } from 'lemmy-js-client'
|
||||||
import { userSettings } from '$lib/settings.js'
|
import { userSettings } from '$lib/settings.js'
|
||||||
|
import { afterNavigate } from '$app/navigation'
|
||||||
|
import { browser } from '$app/environment'
|
||||||
|
import { onMount } from 'svelte'
|
||||||
|
|
||||||
export let data
|
export let data
|
||||||
|
|
||||||
@@ -61,7 +65,11 @@
|
|||||||
data.cursor.next = newPosts.next_page
|
data.cursor.next = newPosts.next_page
|
||||||
data.posts.posts = [...data.posts.posts, ...newPosts.posts]
|
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
|
loading = false
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -69,6 +77,54 @@
|
|||||||
loading = false
|
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>
|
</script>
|
||||||
|
|
||||||
<div class="flex flex-col gap-4 max-w-full w-full min-w-0">
|
<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()) &&
|
!(cached && url.searchParams.toString() == cached.params.toString()) &&
|
||||||
browser
|
browser
|
||||||
)
|
)
|
||||||
_posts.set({ data: posts, params: url.searchParams })
|
_posts.set({ data: posts, params: url.searchParams, lastSeen: 0 })
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -42,6 +42,7 @@
|
|||||||
import Expandable from '$lib/components/ui/Expandable.svelte'
|
import Expandable from '$lib/components/ui/Expandable.svelte'
|
||||||
import { Popover } from 'mono-svelte'
|
import { Popover } from 'mono-svelte'
|
||||||
import { t } from '$lib/translations.js'
|
import { t } from '$lib/translations.js'
|
||||||
|
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
|
||||||
|
|
||||||
export let data
|
export let data
|
||||||
|
|
||||||
@@ -110,7 +111,24 @@
|
|||||||
$: remoteView =
|
$: remoteView =
|
||||||
$page.params.instance?.toLowerCase() != $instance.toLowerCase()
|
$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>
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
|
|||||||
Reference in new issue
Block a user