migrate: use svelte 5 event system

This commit is contained in:
James Xylight committed 2025-02-03 19:57:00 -08:00
1 parent dd9abe5345
commit 9025f5db18
18 files changed
+68 -58

No files matched your search

@@ -237,7 +237,7 @@
<CommentActions <CommentActions
comment={node.comment_view} comment={node.comment_view}
bind:replying bind:replying
on:edit={() => (editing = true)} onedit={() => (editing = true)}
disabled={node.comment_view.banned_from_community || disabled={node.comment_view.banned_from_community ||
node.comment_view.post.locked} node.comment_view.post.locked}
/> />
@@ -32,18 +32,15 @@
comment: CommentView comment: CommentView
replying?: boolean replying?: boolean
disabled?: boolean disabled?: boolean
onedit?: (comment: CommentView) => void
} }
let { let {
comment = $bindable(), comment = $bindable(),
replying = $bindable(false), replying = $bindable(false),
disabled = false, disabled = false,
onedit,
}: Props = $props() }: Props = $props()
const dispatcher = createEventDispatcher<{ edit: CommentView }>()
let reply = ''
let translating = $state(false)
</script> </script>
<div <div
@@ -97,7 +94,7 @@
</MenuButton> </MenuButton>
{#if $profile?.jwt} {#if $profile?.jwt}
{#if comment.creator.id == $profile.user?.local_user_view.person.id} {#if comment.creator.id == $profile.user?.local_user_view.person.id}
<MenuButton onclick={() => dispatcher('edit', comment)}> <MenuButton onclick={() => onedit?.(comment)}>
<Icon src={PencilSquare} mini size="16" /> <Icon src={PencilSquare} mini size="16" />
<span>{$t('post.actions.more.edit')}</span> <span>{$t('post.actions.more.edit')}</span>
</MenuButton> </MenuButton>
@@ -16,7 +16,7 @@
let { nodes, post, scrollTo }: Props = $props() let { nodes, post, scrollTo }: Props = $props()
let virtualListEl: HTMLElement | undefined = $state(undefined) let virtualListEl = $state<HTMLElement>()
let virtualizer = $derived( let virtualizer = $derived(
createWindowVirtualizer({ createWindowVirtualizer({
@@ -27,7 +27,6 @@ export function buildCommentsTree(
baseDepth: number = 0, baseDepth: number = 0,
filter: (c: CommentView) => boolean = (c) => true, filter: (c: CommentView) => boolean = (c) => true,
): CommentNodeI[] { ): CommentNodeI[] {
const start = performance.now()
const map = new Map<number, CommentNodeI>() const map = new Map<number, CommentNodeI>()
let min_depth = Number.MAX_VALUE let min_depth = Number.MAX_VALUE
@@ -69,7 +68,6 @@ export function buildCommentsTree(
} }
} }
console.log(`Built tree in ${performance.now() - start}ms`)
return tree return tree
} }
@@ -34,7 +34,7 @@
<Select <Select
{...rest} {...rest}
bind:value={selected} bind:value={selected}
on:change={() => { onchange={() => {
if (navigate) searchParam($page.url, 'type', selected, 'page', 'cursor') if (navigate) searchParam($page.url, 'type', selected, 'page', 'cursor')
}} }}
> >
@@ -49,7 +49,7 @@
{...rest} {...rest}
class={selected?.startsWith('Top') ? 'rounded-r-none' : ''} class={selected?.startsWith('Top') ? 'rounded-r-none' : ''}
bind:value={sort} bind:value={sort}
on:change={(e) => { onchange={() => {
setSelected() setSelected()
if (navigate) searchParam($page.url, 'sort', selected, 'page', 'cursor') if (navigate) searchParam($page.url, 'sort', selected, 'page', 'cursor')
}} }}
@@ -93,7 +93,7 @@
<Select <Select
class="border-l-0 rounded-l-none" class="border-l-0 rounded-l-none"
bind:value={selected} bind:value={selected}
on:change={(e) => { onchange={() => {
sort = 'TopAll' sort = 'TopAll'
if (navigate) if (navigate)
searchParam($page.url, 'sort', selected, 'page', 'cursor') searchParam($page.url, 'sort', selected, 'page', 'cursor')
+3 -1
View File
@@ -62,6 +62,7 @@
style?: string style?: string
class?: string class?: string
badges?: import('svelte').Snippet badges?: import('svelte').Snippet
onhide?: (hide: boolean) => void
} }
let { let {
@@ -72,6 +73,7 @@
style = '', style = '',
class: clazz = '', class: clazz = '',
badges, badges,
onhide,
}: Props = $props() }: Props = $props()
let tags = $derived(parseTags(post.post.name)) let tags = $derived(parseTags(post.post.name))
@@ -186,7 +188,7 @@
/> />
{/if} {/if}
{#if actions} {#if actions}
<PostActions on:hide bind:post style="grid-area: actions;" {view} /> <PostActions {onhide} bind:post style="grid-area: actions;" {view} />
{:else if view == 'compact'} {:else if view == 'compact'}
<div class="flex flex-row items-center gap-2 text-sm"> <div class="flex flex-row items-center gap-2 text-sm">
<Badge> <Badge>
@@ -66,13 +66,9 @@
formatRelativeDate, formatRelativeDate,
} from '$lib/components/util/RelativeDate.svelte' } from '$lib/components/util/RelativeDate.svelte'
const dispatcher = createEventDispatcher<{ edit: PostView; hide: boolean }>()
let editing = $state(false) let editing = $state(false)
let saving = $state(false) let saving = $state(false)
let translating = $state(false)
let localShare = $state(false) let localShare = $state(false)
interface Props { interface Props {
@@ -80,6 +76,8 @@
view?: View view?: View
debug?: boolean debug?: boolean
style?: string style?: string
onedit?: (post: PostView) => void
onhide?: (hide: boolean) => void
} }
let { let {
@@ -87,6 +85,8 @@
view = 'cozy', view = 'cozy',
debug = $bindable(false), debug = $bindable(false),
style = '', style = '',
onedit,
onhide,
}: Props = $props() }: Props = $props()
let buttonHeight = $derived(view == 'compact' ? 'h-7' : 'h-8') let buttonHeight = $derived(view == 'compact' ? 'h-7' : 'h-8')
let buttonSquare = $derived(view == 'compact' ? 'w-7 h-7' : 'w-8 h-8') let buttonSquare = $derived(view == 'compact' ? 'w-7 h-7' : 'w-8 h-8')
@@ -105,10 +105,10 @@
<PostForm <PostForm
edit edit
editingPost={post.post} editingPost={post.post}
on:submit={(e) => { onsubmit={(e) => {
editing = false editing = false
post = e.detail post = e
dispatcher('edit', e.detail) onedit?.(e)
}} }}
> >
{#snippet formtitle()} {#snippet formtitle()}
@@ -431,9 +431,7 @@
$profile?.jwt, $profile?.jwt,
) )
post.hidden = hidden post.hidden = hidden
if (hidden) { if (hidden) onhide?.(hidden)
dispatcher('hide', hidden)
}
}} }}
color="danger-subtle" color="danger-subtle"
> >
@@ -38,7 +38,6 @@
} from '$lib/lemmy/postfeed' } from '$lib/lemmy/postfeed'
import { t } from '$lib/translations' import { t } from '$lib/translations'
import InfiniteScroll from 'svelte-infinite-scroll' import InfiniteScroll from 'svelte-infinite-scroll'
import type { Readable } from 'svelte/motion'
import EndPlaceholder from '$lib/components/ui/EndPlaceholder.svelte' import EndPlaceholder from '$lib/components/ui/EndPlaceholder.svelte'
let virtualItemEls: HTMLElement[] = $state([]) let virtualItemEls: HTMLElement[] = $state([])
@@ -252,7 +251,7 @@
: settings.view} : settings.view}
{post} {post}
class="transition-all duration-250" class="transition-all duration-250"
on:hide={() => { onhide={() => {
posts = posts.toSpliced(row.index, 1) posts = posts.toSpliced(row.index, 1)
}} }}
></Post> ></Post>
@@ -56,6 +56,7 @@
language_id?: number | string language_id?: number | string
} }
formtitle?: import('svelte').Snippet formtitle?: import('svelte').Snippet
onsubmit?: (post: PostView) => void
} }
let { let {
@@ -75,10 +76,12 @@
language_id: undefined, language_id: undefined,
}), }),
formtitle, formtitle,
onsubmit,
}: Props = $props() }: Props = $props()
// weird select menu language handling // weird select menu language handling
// @ts-ignore // @ts-ignore
run(() => { $effect(() => {
if (data.language_id === '') data.language_id = undefined if (data.language_id === '') data.language_id = undefined
}) })
@@ -87,8 +90,6 @@
let communities: Community[] = $state([]) let communities: Community[] = $state([])
const dispatcher = createEventDispatcher<{ submit: PostView }>()
onMount(async () => { onMount(async () => {
if (editingPost) { if (editingPost) {
data.url = editingPost.url ?? '' data.url = editingPost.url ?? ''
@@ -166,7 +167,7 @@
console.log(`Edited post ${post?.post_view.post.id}`) console.log(`Edited post ${post?.post_view.post.id}`)
dispatcher('submit', post.post_view) onsubmit?.(post.post_view)
} else { } else {
const post = await client().createPost({ const post = await client().createPost({
community_id: data.community!.id, community_id: data.community!.id,
@@ -184,7 +185,7 @@
console.log(`Uploaded post ${post?.post_view.post.id}`) console.log(`Uploaded post ${post?.post_view.post.id}`)
saveDraft = false saveDraft = false
dispatcher('submit', post.post_view) onsubmit?.(post.post_view)
} }
} catch (err) { } catch (err) {
pushError({ message: errorMessage(err as any), scope: 'post-form' }) pushError({ message: errorMessage(err as any), scope: 'post-form' })
@@ -19,9 +19,6 @@
import Expandable from '$lib/components/ui/Expandable.svelte' import Expandable from '$lib/components/ui/Expandable.svelte'
import EndPlaceholder from '../EndPlaceholder.svelte' import EndPlaceholder from '../EndPlaceholder.svelte'
import { t } from '$lib/translations' import { t } from '$lib/translations'
import { iconOfLink } from '../navbar/link'
import { goto } from '$app/navigation'
import { page } from '$app/stores'
interface Props { interface Props {
style?: string style?: string
+2
View File
@@ -144,7 +144,9 @@ export async function markAsRead(
if (item.type == 'post') { if (item.type == 'post') {
getClient().markPostAsRead({ getClient().markPostAsRead({
// @ts-ignore
post_id: item.id, post_id: item.id,
post_ids: [item.id],
read: read, read: read,
}) })
return read return read
+8 -9
View File
@@ -22,6 +22,7 @@
Icon, Icon,
type IconSource, type IconSource,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import type { HTMLSelectAttributes } from 'svelte/elements'
let open = $state(false) let open = $state(false)
let element: HTMLSelectElement | undefined = $state() let element: HTMLSelectElement | undefined = $state()
@@ -39,13 +40,7 @@
options: [], options: [],
}) })
const dispatcher = createEventDispatcher<{ interface Props extends Omit<HTMLSelectAttributes, 'size'> {
change: any
contextmenu: any
input: any
}>()
interface Props {
value?: T | string | undefined value?: T | string | undefined
placeholder?: string | undefined placeholder?: string | undefined
label?: string | undefined label?: string | undefined
@@ -56,6 +51,8 @@
customLabel?: import('svelte').Snippet customLabel?: import('svelte').Snippet
children?: import('svelte').Snippet children?: import('svelte').Snippet
customOption?: import('svelte').Snippet<[any]> customOption?: import('svelte').Snippet<[any]>
oncontextmenu?: () => void
onchange?: () => void
} }
let { let {
@@ -69,6 +66,8 @@
customLabel, customLabel,
children, children,
customOption, customOption,
oncontextmenu,
onchange,
...rest ...rest
}: Props = $props() }: Props = $props()
</script> </script>
@@ -117,10 +116,10 @@
selected: option.value == value, selected: option.value == value,
})}{:else} })}{:else}
<MenuButton <MenuButton
on:contextmenu={() => dispatcher('contextmenu')} on:contextmenu={() => oncontextmenu?.()}
onclick={() => { onclick={() => {
value = option.value value = option.value
dispatcher('change') onchange?.()
}} }}
size="custom" size="custom"
disabled={option.disabled} disabled={option.disabled}
+1 -1
View File
@@ -48,7 +48,7 @@
url: undefined, url: undefined,
}} }}
passedCommunity={community} passedCommunity={community}
on:submit={(e) => goto(`/post/${e.detail.post.id}`)} onsubmit={(e) => goto(`/post/${e.post.id}`)}
> >
{#snippet formtitle()} {#snippet formtitle()}
<Header class="text-3xl font-bold" pageHeader> <Header class="text-3xl font-bold" pageHeader>
+1 -1
View File
@@ -48,7 +48,7 @@
<div class="flex flex-row flex-wrap gap-2"> <div class="flex flex-row flex-wrap gap-2">
<Select <Select
bind:value={data.type} bind:value={data.type}
on:change={(e) => searchParam($page.url, 'type', data.type, 'page')} onchange={() => searchParam($page.url, 'type', data.type, 'page')}
class="w-48" class="w-48"
> >
{#snippet customLabel()} {#snippet customLabel()}
@@ -1,7 +1,10 @@
<script lang="ts"> <script lang="ts">
import { run } from 'svelte/legacy' import { run } from 'svelte/legacy'
import { buildCommentsTree } from '$lib/components/lemmy/comment/comments.svelte.js' import {
buildCommentsTree,
type CommentNodeI,
} from '$lib/components/lemmy/comment/comments.svelte.js'
import { isImage } from '$lib/ui/image.js' import { isImage } from '$lib/ui/image.js'
import { client, getClient } from '$lib/lemmy.js' import { client, getClient } from '$lib/lemmy.js'
import CommentForm from '$lib/components/lemmy/comment/CommentForm.svelte' import CommentForm from '$lib/components/lemmy/comment/CommentForm.svelte'
@@ -58,6 +61,11 @@
let { data = $bindable() } = $props() let { data = $bindable() } = $props()
let comments = $state(data.comments)
$effect(() => {
comments = data.comments
})
let type = $derived(mediaType(data.post.post_view.post.url, 'cozy')) let type = $derived(mediaType(data.post.post_view.post.url, 'cozy'))
const updateActions = () => { const updateActions = () => {
@@ -164,7 +172,7 @@
async function reloadComments() { async function reloadComments() {
loading = true loading = true
data.comments = await getClient().getComments({ comments = getClient().getComments({
page: 1, page: 1,
limit: 25, limit: 25,
type_: 'All', type_: 'All',
@@ -182,6 +190,11 @@
let remoteView = $derived( let remoteView = $derived(
$page.params.instance?.toLowerCase() != $instance.toLowerCase(), $page.params.instance?.toLowerCase() != $instance.toLowerCase(),
) )
function createCommentsState(tree: CommentNodeI[]) {
const state = $state(tree)
return state
}
</script> </script>
<svelte:head> <svelte:head>
@@ -308,7 +321,7 @@
<div class="w-full relative"> <div class="w-full relative">
<PostActions <PostActions
bind:post={data.post.post_view} bind:post={data.post.post_view}
on:edit={() => onedit={() =>
toast({ toast({
content: 'The post was edited successfully.', content: 'The post was edited successfully.',
type: 'success', type: 'success',
@@ -375,7 +388,7 @@
{$t('routes.post.commentCount')} {$t('routes.post.commentCount')}
</div> </div>
</header> </header>
{#await data.comments} {#await comments}
<div class="flex flex-col gap-4"> <div class="flex flex-col gap-4">
{#each new Array(10) as empty, index} {#each new Array(10) as empty, index}
<div <div
@@ -383,7 +396,7 @@
duration: 500, duration: 500,
easing: expoOut, easing: expoOut,
y: 8, y: 8,
delay: index * 25, delay: index * 50,
}} }}
class="animate-pulse flex flex-col gap-2 w-full class="animate-pulse flex flex-col gap-2 w-full
*:bg-slate-100 *:dark:bg-zinc-800 *:rounded-md" *:bg-slate-100 *:dark:bg-zinc-800 *:rounded-md"
@@ -408,7 +421,7 @@
<Select <Select
size="md" size="md"
bind:value={commentSort} bind:value={commentSort}
on:change={reloadComments} onchange={reloadComments}
> >
<Option icon={Fire} value="Hot">{$t('filter.sort.hot')}</Option> <Option icon={Fire} value="Hot">{$t('filter.sort.hot')}</Option>
<Option icon={Trophy} value="Top"> <Option icon={Trophy} value="Top">
@@ -459,7 +472,7 @@
{#if commenting || !$profile.jwt} {#if commenting || !$profile.jwt}
<div class="gap-2 flex items-center"> <div class="gap-2 flex items-center">
<Select size="md" bind:value={commentSort} on:change={reloadComments}> <Select size="md" bind:value={commentSort} onchange={reloadComments}>
<Option icon={Fire} value="Hot">{$t('filter.sort.hot')}</Option> <Option icon={Fire} value="Hot">{$t('filter.sort.hot')}</Option>
<Option icon={Trophy} value="Top"> <Option icon={Trophy} value="Top">
{$t('filter.sort.top.label')} {$t('filter.sort.top.label')}
@@ -479,9 +492,8 @@
</Button> </Button>
</div> </div>
{/if} {/if}
<CommentListVirtualizer {#if comments}
post={data.post.post_view.post} {@const tree = buildCommentsTree(
nodes={buildCommentsTree(
comments.comments, comments.comments,
undefined, undefined,
(c) => (c) =>
@@ -490,8 +502,13 @@
(settings.hidePosts.removed && c.comment.removed) (settings.hidePosts.removed && c.comment.removed)
), ),
)} )}
<CommentListVirtualizer
post={data.post.post_view.post}
nodes={createCommentsState(tree)}
scrollTo={data.thread.focus} scrollTo={data.thread.focus}
/> />
{/if}
{#if comments.comments.length == 0} {#if comments.comments.length == 0}
<Placeholder <Placeholder
icon={ChatBubbleLeftRight} icon={ChatBubbleLeftRight}
+1 -1
View File
@@ -44,7 +44,7 @@
<div class="flex items-center"> <div class="flex items-center">
<Select <Select
bind:value={type} bind:value={type}
on:change={() => searchParam($page.url, 'type', type, 'page')} onchange={() => searchParam($page.url, 'type', type, 'page')}
> >
{#snippet customLabel()} {#snippet customLabel()}
<div class="flex items-center gap-0.5"> <div class="flex items-center gap-0.5">
+2 -2
View File
@@ -296,7 +296,7 @@
<div class="flex flex-row gap-4 flex-wrap"> <div class="flex flex-row gap-4 flex-wrap">
<Select <Select
bind:value={data.type} bind:value={data.type}
on:change={() => searchParam($page.url, 'type', data.type, 'page')} onchange={() => searchParam($page.url, 'type', data.type, 'page')}
> >
{#snippet customLabel()} {#snippet customLabel()}
<span class="flex items-center gap-1"> <span class="flex items-center gap-1">
@@ -310,7 +310,7 @@
</Select> </Select>
<Select <Select
bind:value={data.sort} bind:value={data.sort}
on:change={() => searchParam($page.url, 'sort', data.sort, 'page')} onchange={() => searchParam($page.url, 'sort', data.sort, 'page')}
> >
{#snippet customLabel()} {#snippet customLabel()}
<span class="flex items-center gap-1"> <span class="flex items-center gap-1">