Merge pull request #349 from Xyphyn/infinite-scroll

feat: add infinite scroll
This commit is contained in:
Xylight authored and GitHub committed 2024-07-09 16:22:28 -07:00
commit 49a0e983df
19 files changed
+561 -110

No files matched your search

+2 -1
View File
@@ -84,7 +84,7 @@
<!-- svelte-ignore a11y-click-events-have-key-events -->
<Material
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'
? 'py-4 list-type'
: view == 'list'
@@ -95,6 +95,7 @@
on:click={(e) => {
onClick(e)
}}
style={$$props.style ?? ''}
>
<PostMeta
community={post.community}
@@ -1,6 +1,6 @@
<script lang="ts">
import Link, { parseURL } from '$lib/components/input/Link.svelte'
import { Material } from 'mono-svelte'
import { Button, Material } from 'mono-svelte'
import { Icon, Link as LinkIcon } from 'svelte-hero-icons'
import { optimizeImageURL } from './helpers'
@@ -64,12 +64,15 @@
{/if}
</Material>
{:else}
<Link
<Button
href={url}
target="_blank"
class="text-slate-700 dark:text-zinc-300 inline-flex items-center gap-1 text-sm"
class="text-slate-700 dark:text-zinc-300 inline-flex items-center gap-1 text-xs overflow-hidden w-max max-w-full"
size="sm"
color="ghost"
rounding="pill"
>
<Icon src={LinkIcon} size="14" mini slot="icon" class="flex-shrink-0" />
<Icon src={LinkIcon} size="14" mini slot="prefix" class="flex-shrink-0" />
{#if richURL}
<div class="flex max-w-full overflow-hidden">
{richURL.hostname}
@@ -80,7 +83,7 @@
{:else}
{url}
{/if}
</Link>
</Button>
{#if thumbnail_url && !compact}
<a href={url} target="_blank">
<img
@@ -253,6 +253,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>
@@ -7,6 +7,11 @@
import { ArchiveBox, Icon, Minus, Plus } from 'svelte-hero-icons'
import { expoOut } from 'svelte/easing'
import { fly, slide } from 'svelte/transition'
import { browser } from '$app/environment'
import { afterUpdate, onMount, tick, type SvelteComponent } from 'svelte'
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
import { afterNavigate, beforeNavigate } from '$app/navigation'
import { _posts } from '../../../../../routes/+page.svelte'
type PostViewWithCrossposts = PostView & {
withCrossposts: true
@@ -98,16 +103,16 @@
</Placeholder>
</div>
{:else}
{#each combinedPosts as post, index (post.post.id)}
{#each combinedPosts as post, index}
{#if !($userSettings.hidePosts.deleted && post.post.deleted) && !($userSettings.hidePosts.removed && post.post.removed)}
<li
in:fly|global={{
y: -8,
duration: index < 4 ? 500 : 0,
duration: 500,
opacity: 0,
delay: index < 4 ? index * 100 : 0,
delay: 100 + index * 20,
}}
class="relative"
class="relative post-container"
>
<Post
hideCommunity={community}
@@ -116,12 +121,9 @@
? 'compact'
: $userSettings.view}
{post}
class="transition-all duration-250 {post.withCrossposts &&
viewPost != post.post.id
? ''
: ''}"
class="transition-all duration-250"
on:hide={() => {
combinedPosts = combinedPosts.toSpliced(index, 1)
posts = posts.toSpliced(index, 1)
}}
>
<svelte:fragment slot="badges">
@@ -175,4 +177,5 @@
{/if}
{/each}
{/if}
<slot />
</ul>
@@ -0,0 +1,222 @@
<script lang="ts">
import Post from '$lib/components/lemmy/post/Post.svelte'
import Placeholder from '$lib/components/ui/Placeholder.svelte'
import { userSettings } from '$lib/settings.js'
import type { PostView } from 'lemmy-js-client'
import { Badge, Button } from 'mono-svelte'
import { ArchiveBox, Icon, Minus, Plus } from 'svelte-hero-icons'
import { expoOut } from 'svelte/easing'
import { fly, slide } from 'svelte/transition'
import { browser } from '$app/environment'
import { afterUpdate, onMount, tick, type SvelteComponent } from 'svelte'
import { createWindowVirtualizer } from '@tanstack/svelte-virtual'
import { afterNavigate, beforeNavigate } from '$app/navigation'
import { _posts } from '../../../../../routes/+page.svelte'
type PostViewWithCrossposts = PostView & {
withCrossposts: true
crossposts: PostView[]
}
type PostViewWithoutCrossposts = PostView & { withCrossposts?: false }
export let posts: PostView[]
export let community: boolean = false
const addCrosspostProperty = (
post: PostView,
crossposts: PostView[]
): PostViewWithCrossposts => ({
...post,
crossposts: crossposts,
withCrossposts: true,
})
const combineCrossposts = (
posts: PostView[]
): (PostViewWithCrossposts | PostViewWithoutCrossposts)[] => {
const urlMap = new Map<
string,
PostViewWithCrossposts | PostViewWithoutCrossposts
>()
const results: (PostViewWithCrossposts | PostViewWithoutCrossposts)[] = []
const seenUrls = new Set<string>()
posts?.forEach((post) => {
if (
!post ||
($userSettings.hidePosts.deleted && post.post.deleted) ||
($userSettings.hidePosts.removed && post.post.removed)
)
return
if (!post?.post?.url) {
results.push(post)
return
}
let existing = urlMap.get(post.post.url)
if (existing) {
existing.withCrossposts = true
if (existing.withCrossposts) {
existing.crossposts = [...(existing.crossposts || []), post]
}
urlMap.set(post.post.url, existing)
} else {
urlMap.set(post.post.url, post)
results.push(post)
}
seenUrls.add(post.post.url)
})
return results
}
// $: combinedPosts = combineCrossposts(posts)
let viewPost: number = -1
const virtualizer = createWindowVirtualizer({
count: posts.length,
estimateSize: () => 150,
})
$: if (posts.length)
$virtualizer.setOptions({
count: posts.length,
})
let virtualItemEls: HTMLElement[] = []
$: items = $virtualizer.getVirtualItems()
$: {
if (virtualItemEls.length)
virtualItemEls.forEach((el) => $virtualizer.measureElement(el))
}
afterNavigate(() => {
$virtualizer.scrollToIndex($_posts?.lastSeen)
})
</script>
<ul
class="flex flex-col list-none {$userSettings.view == 'card'
? 'gap-3 md:gap-4'
: 'divide-y'} divide-slate-200 dark:divide-zinc-800"
style={$userSettings.leftAlign
? `--template-areas:
'media meta'
'media title'
'media body'
'embed embed'
'actions actions'; --template-columns: auto 1fr;`
: ``}
>
{#if posts?.length == 0}
<div class="h-full grid place-items-center">
<Placeholder
icon={ArchiveBox}
title="No posts"
description="There are no posts that match this filter."
>
<Button href="/communities">
<Icon src={Plus} size="16" mini slot="prefix" />
<span>Follow some communities</span>
</Button>
</Placeholder>
</div>
{:else}
<!-- {#each combinedPosts as post, index} -->
<!-- {#if !($userSettings.hidePosts.deleted && post.post.deleted) && !($userSettings.hidePosts.removed && post.post.removed)} -->
<div
style="position:relative; height: {browser
? `${$virtualizer.getTotalSize()}px`
: '100%'}; width: 100%;"
>
<div
style="position: absolute; top: 0; left: 0; width: 100%; transform: translateY({items[0]
? items[0].start
: 0}px);"
class="divide-y divide-slate-200 dark:divide-zinc-800"
id="feed"
>
{#each items as row, index (row.index)}
<li
in:fly|global={{
y: -8,
duration: 500,
opacity: 0,
delay: 100 + index * 20,
}}
bind:this={virtualItemEls[index]}
data-index={row.index}
class="relative post-container"
>
<Post
hideCommunity={community}
view={(posts[row.index].post.featured_community ||
posts[row.index].post.featured_local) &&
$userSettings.posts.compactFeatured
? 'compact'
: $userSettings.view}
post={posts[row.index]}
class="transition-all duration-250"
on:hide={() => {
posts = posts.toSpliced(row.index, 1)
}}
>
<svelte:fragment slot="badges">
<!-- {#if posts[index].withCrossposts}
<button
on:click={() => {
if (viewPost == posts[index].post.id) viewPost = -1
else viewPost = posts[index].post.id
}}
>
<Badge
class="z-10 backdrop-blur-xl hover:brightness-110 cursor-pointer transition-all"
color="gray-subtle"
>
{#if viewPost == posts[index].post.id}
<Icon mini src={Minus} size="14" />
{:else}
<Icon mini src={Plus} size="14" />
{/if}
{posts[index].crossposts.length} crosspost{posts[index]
.crossposts.length == 1
? ''
: 's'}
</Badge>
</button>{/if} -->
</svelte:fragment>
</Post>
<!-- {#if posts[index].withCrossposts && viewPost == posts[index].post.id}
<div
transition:slide|global={{
axis: 'y',
duration: 500,
easing: expoOut,
}}
>
<span
class="text-sm flex flex-row gap-2 items-center"
class:my-4={$userSettings.view == 'card'}
>
Crossposts <hr class="w-full dark:border-zinc-800" />
{posts[index].crossposts.length}
</span>
{#each posts[index].crossposts as crosspost, index}
<div class="w-full transition-all mb-4">
<Post post={crosspost} view={$userSettings.view} />
</div>
{/each}
</div>
{/if} -->
</li>
{/each}
</div>
</div>
<!-- {/if} -->
<!-- {/each} -->
{/if}
<slot />
</ul>