migrate: switch to svelte 5 events system for inputs

This commit is contained in:
James Xylight committed 2025-02-05 19:11:25 -08:00
1 parent 6f442eceab
commit 43a857ed66
11 files changed
+99 -98

No files matched your search

@@ -112,7 +112,7 @@
value={newComment}
actions={false}
preview={true}
on:confirm={save}
onconfirm={save}
/>
<Button
submit
@@ -252,18 +252,18 @@
label={$t('comment.reply')}
{postId}
parentId={node.comment_view.comment.id}
on:comment={(e) => {
oncomment={(e) => {
node.children = [
{
children: [],
comment_view: e.detail.comment_view,
comment_view: e.comment_view,
depth: node.depth + 1,
},
...node.children,
]
replying = false
}}
on:cancel={() => (replying = false)}
oncancel={() => (replying = false)}
/>
</div>
</div>
@@ -12,12 +12,6 @@
import { errorMessage } from '$lib/lemmy/error'
import { Icon, XMark } from 'svelte-hero-icons'
const dispatch = createEventDispatcher<{
comment: CommentResponse
confirm: string
cancel: boolean
}>()
interface Props {
postId: number
parentId?: number | undefined
@@ -28,6 +22,9 @@
value?: string
actions?: boolean
preview?: boolean
oncomment?: (comment: CommentResponse) => void
onconfirm?: (value: string) => void
oncancel?: (cancel: boolean) => void
[key: string]: any
}
@@ -41,6 +38,9 @@
value = $bindable(''),
actions = true,
preview: previewAction = true,
oncancel,
oncomment,
onconfirm,
...rest
}: Props = $props()
@@ -58,7 +58,7 @@
post_id: postId,
parent_id: parentId,
})
dispatch('comment', response)
oncomment?.(response)
value = ''
} catch (err) {
@@ -92,13 +92,12 @@
: (placeholder ?? placeholders.get('comment'))}
bind:value
disabled={locked || loading || banned}
on:confirm={() => {
onconfirm={() => {
if (actions) {
submit()
dispatch('confirm', value)
onconfirm?.(value)
}
}}
on:focus
previewButton={previewAction}
>
<div class="flex-1"></div>
@@ -106,19 +105,26 @@
<Button
size="custom"
title={$t('common.cancel')}
onclick={() => dispatch('cancel', true)}
onclick={() => oncancel?.(true)}
color="tertiary"
class="w-8 h-8"
rounding="xl"
rounding="pill"
>
<Icon src={XMark} size="16" micro />
<Icon
src={XMark}
size="20"
micro
class="text-slate-600 dark:text-zinc-400"
/>
</Button>
<Button
onclick={submit}
color="primary"
rounding="pill"
size="sm"
class="py-2 px-4"
{loading}
disabled={locked || loading || banned}
size="lg"
>
{$t('form.submit')}
</Button>
@@ -143,7 +143,7 @@
required
label={$t('form.name')}
bind:value={formData.name}
on:input={() => {
oninput={() => {
formData.name = formData.name.toLowerCase().replaceAll(' ', '_')
}}
disabled={edit != undefined}
@@ -32,9 +32,7 @@
</script>
<ul
class="flex flex-col list-none {settings.view == 'card'
? 'gap-3 md:gap-4'
: 'divide-y'} divide-slate-200 dark:divide-zinc-800"
class="flex flex-col list-none divide-y divide-slate-200 dark:divide-zinc-800"
>
{#if posts?.length == 0}
<div class="h-full grid place-items-center">
@@ -63,7 +61,7 @@
: settings.view}
{post}
class="transition-all duration-250"
on:hide={() => {
onhide={() => {
posts = posts.toSpliced(index, 1)
}}
>
@@ -100,10 +98,7 @@
easing: expoOut,
}}
>
<span
class="text-sm flex flex-row gap-2 items-center"
class:my-4={settings.view == 'card'}
>
<span class="text-sm flex flex-row gap-2 items-center">
Crossposts <hr class="w-full dark:border-zinc-800" />
{post.crossposts.length}
</span>
@@ -198,9 +198,7 @@
<!-- <svelte:window on:keydown={handleKeydown} /> -->
<ul
class="flex flex-col list-none {settings.view == 'card'
? 'gap-3 md:gap-4'
: 'divide-y'} divide-slate-200 dark:divide-zinc-800"
class="flex flex-col list-none divide-y divide-slate-200 dark:divide-zinc-800"
>
{#if posts?.length == 0}
<div class="h-full grid place-items-center">
@@ -23,8 +23,8 @@
Strikethrough,
} from 'svelte-hero-icons'
import ImageUploadModal from '../lemmy/modal/ImageUploadModal.svelte'
const dispatcher = createEventDispatcher<{ confirm: string }>()
import type { HTMLTextareaAttributes } from 'svelte/elements'
import type { TextAreaProps } from 'mono-svelte/forms/TextArea.svelte'
let textArea: HTMLTextAreaElement | undefined = $state()
@@ -69,9 +69,9 @@
KeyH: () => wrapSelection('\n# ', ''),
KeyK: () => wrapSelection('[](', ')'),
Enter: (e: any) => {
dispatcher('confirm', value)
onconfirm?.(value)
const newEvent = new Event('submit', { cancelable: true })
e.target.form.dispatchEvent(newEvent)
e?.target?.form?.dispatchEvent(newEvent)
},
}
@@ -83,7 +83,7 @@
}
}
interface Props {
interface Props extends TextAreaProps {
images?: boolean
value?: string
label?: string | undefined
@@ -96,7 +96,7 @@
class?: string
customLabel?: import('svelte').Snippet
children?: import('svelte').Snippet
[key: string]: any
onconfirm?: (value: string) => void
}
let {
@@ -112,10 +112,11 @@
class: clazz = '',
customLabel,
children,
onconfirm,
...rest
}: Props = $props()
run(() => {
$effect(() => {
if (!previewing && value) adjustHeight()
})
</script>
@@ -264,7 +265,7 @@ overflow-hidden transition-colors {clazz}"
class="bg-inherit z-0 border-0 rounded-none !ring-0 focus:!ring-transparent !transition-none resize-none"
bind:value
bind:element={textArea}
on:keydown={(e) => {
onkeydown={(e) => {
if (disabled) return
if (e.ctrlKey || e.metaKey) {
// @ts-ignore
@@ -275,9 +276,8 @@ overflow-hidden transition-colors {clazz}"
}
}
}}
on:input={adjustHeight}
on:focus
on:paste={(e) => {
oninput={adjustHeight}
onpaste={(e) => {
if (!e.clipboardData?.files) return
const files = Array.from(e.clipboardData.files)
if (files[0]?.type.startsWith('image/')) {
+25 -27
View File
@@ -6,19 +6,8 @@
md: 'p-4',
lg: 'p-5',
}
</script>
<script lang="ts">
import { createBubbler } from 'svelte/legacy'
const bubble = createBubbler()
import Label from '../forms/Label.svelte'
import { generateID } from '../forms/helper.js'
const borderClass = `
border border-slate-200 dark:border-zinc-800
`
interface Props {
interface Props extends HTMLTextareaAttributes {
label?: string | undefined
value?: string
placeholder?: string
@@ -28,13 +17,25 @@
id?: string
rows?: number
element?: HTMLTextAreaElement | undefined
class?: string
class?: ClassValue
customLabel?: import('svelte').Snippet
suffix?: import('svelte').Snippet
children?: import('svelte').Snippet
[key: string]: any
}
export type { Props as TextAreaProps }
</script>
<script lang="ts">
import type { ClassValue, HTMLTextareaAttributes } from 'svelte/elements'
import Label from '../forms/Label.svelte'
import { generateID } from '../forms/helper.js'
const borderClass = `
border border-slate-200 dark:border-zinc-800
`
let {
label = undefined,
value = $bindable(''),
@@ -74,22 +75,19 @@
{disabled}
{rows}
bind:value
oninput={bubble('input')}
onchange={bubble('change')}
onkeydown={bubble('keydown')}
onfocus={bubble('focus')}
onpaste={bubble('paste')}
bind:this={element}
{...rest}
class="{sizeClass[size]} {borderClass} focus:border-slate-800
focus:dark:border-zinc-200 bg-white dark:bg-zinc-950
focus:outline-none focus:ring-2 ring-slate-800/50 rounded-xl
dark:ring-zinc-200/50 transition-all text-sm w-full disabled:bg-slate-100
class={[
sizeClass[size],
borderClass,
`focus:border-slate-800 focus:dark:border-zinc-200 bg-white dark:bg-zinc-950
focus:outline-none focus:ring-2 ring-slate-800/50 rounded-xl dark:ring-zinc-200/50
transition-all text-sm w-full disabled:bg-slate-100
disabled:cursor-not-allowed disabled:dark:bg-zinc-900 invalid:!border-red-500
peer invalid:text-red-500 z-10
{clazz || ''}"
class:rounded-b-none={suffix}
class:border-b-0={suffix}
peer invalid:text-red-500 z-10`,
suffix && 'rounded-b-none border-b-0',
clazz,
]}
></textarea>
{#if suffix}
<div
+27 -20
View File
@@ -15,16 +15,18 @@
</script>
<script lang="ts">
import { createBubbler } from 'svelte/legacy'
import type {
HTMLInputAttributes,
HTMLInputTypeAttribute,
} from 'svelte/elements'
const bubble = createBubbler()
import Label from '../forms/Label.svelte'
import { generateID } from '../forms/helper.js'
const borderClass = `
border border-slate-200 border-b-slate-300 dark:border-zinc-900 dark:border-t-zinc-800
`
interface Props {
interface Props extends Omit<HTMLInputAttributes, 'size' | 'prefix'> {
label?: string | undefined
value?: string
placeholder?: string
@@ -40,7 +42,6 @@
prefix?: import('svelte').Snippet
suffix?: import('svelte').Snippet
children?: import('svelte').Snippet
[key: string]: any
}
let {
@@ -78,11 +79,15 @@
</Label>
{/if}
<div
class="{shadowClass[shadow]} border {borderClass}
focus-within:border-primary-900 focus-within:dark:border-primary-100 focus-within:ring-2
class={[
shadowClass[shadow],
borderClass,
`border focus-within:border-primary-900 focus-within:dark:border-primary-100 focus-within:ring-2
ring-slate-300 dark:ring-zinc-700
transition-colors
rounded-lg flex flex-row items-center text-sm {clazz}"
rounded-lg flex flex-row items-center text-sm`,
clazz,
]}
>
{#if prefix}
<div
@@ -101,25 +106,27 @@
{disabled}
bind:value
bind:this={element}
oninput={bubble('input')}
onchange={bubble('change')}
onfocus={bubble('focus')}
{...rest}
class="{sizeClass[size]} bg-white dark:bg-zinc-950
class={[
sizeClass[size],
`bg-white dark:bg-zinc-950
focus:outline-none rounded-lg text-sm w-full disabled:bg-slate-100
disabled:cursor-not-allowed disabled:dark:bg-zinc-950 invalid:!border-red-500
peer invalid:text-red-500 z-10
{clazz || ''}"
class:rounded-l-none={prefix}
class:border-l-0={prefix && inlineAffixes}
class:rounded-r-none={suffix}
class:border-r-0={suffix && inlineAffixes}
peer invalid:text-red-500 z-10`,
prefix && 'rounded-l-none',
prefix && inlineAffixes && 'border-l-0',
suffix && 'rounded-r-none',
suffix && inlineAffixes && 'border-r-0',
clazz,
]}
/>
{#if suffix}
<div
class="rounded-lg rounded-l-none text-slate-600 dark:text-zinc-400 {inlineAffixes
? 'bg-white dark:bg-zinc-950 pl-0'
: ''} {sizeClass[size]}"
class={[
'rounded-lg rounded-l-none text-slate-600 dark:text-zinc-400',
inlineAffixes && 'bg-white dark:bg-zinc-950 pl-0',
sizeClass[size],
]}
>
{@render suffix?.()}
</div>
+2 -2
View File
@@ -60,13 +60,13 @@
{#if input}{@render input()}{:else}
<TextInput
bind:value={query}
on:input={(e) => {
oninput={(e) => {
searching = true
openMenu = true
dispatcher('input', e)
debounceFunc()
}}
on:focus={(e) => {
onfocus={(e) => {
searching = true
openMenu = true
dispatcher('input', e)
@@ -433,11 +433,8 @@
{:else}
<CommentForm
postId={data.post.post_view.post.id}
on:comment={(comment) =>
(comments.comments = [
comment.detail.comment_view,
...comments.comments,
])}
oncomment={(comment) =>
(comments.comments = [comment.comment_view, ...comments.comments])}
locked={(data.post.post_view.post.locked &&
!(
$profile?.user?.local_user_view.local_user.admin ||
@@ -447,12 +444,12 @@
)) ||
page.params.instance.toLowerCase() != $instance.toLowerCase()}
banned={data.post.community_view.banned_from_community}
on:focus={() => (commenting = true)}
onfocus={() => (commenting = true)}
tools={commenting}
preview={commenting}
placeholder={commenting ? undefined : $t('routes.post.addComment')}
rows={commenting ? 7 : 1}
on:cancel={() => (commenting = false)}
oncancel={() => (commenting = false)}
/>
{/if}
{/if}
+1 -1
View File
@@ -133,7 +133,7 @@
label={$t('form.signup.chooseInstance')}
required
{placeholder}
on:input={() => {
oninput={() => {
selectedInstance = selectedInstance.toLowerCase().replaceAll(' ', '')
}}
/>