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