impr: rewrite image upload handling

This commit is contained in:
James Xylight committed 2025-08-23 11:36:05 -07:00
1 parent 935650ff07
commit 7fa3b60bd4
11 files changed
+135 -221

No files matched your search

@@ -1,17 +1,15 @@
<script lang="ts"> <script lang="ts">
import { profile } from '$lib/auth.svelte.js' import { profile } from '$lib/auth.svelte.js'
import ProgressBar from '$lib/components/ui/ProgressBar.svelte' import ProgressBar from '$lib/components/ui/ProgressBar.svelte'
import { errorMessage } from '$lib/lemmy/error'
import { t } from '$lib/i18n/translations' import { t } from '$lib/i18n/translations'
import { errorMessage } from '$lib/lemmy/error'
import { uploadImage } from '$lib/util.svelte.js' import { uploadImage } from '$lib/util.svelte.js'
import { toast } from 'mono-svelte' import { Button, toast } from 'mono-svelte'
import { Button, Modal } from 'mono-svelte'
import { DocumentPlus, Icon } from 'svelte-hero-icons' import { DocumentPlus, Icon } from 'svelte-hero-icons'
import { expoOut } from 'svelte/easing' import { expoOut } from 'svelte/easing'
import { slide } from 'svelte/transition' import { slide } from 'svelte/transition'
interface Props { interface Props {
open: boolean
image?: FileList | null image?: FileList | null
preview?: boolean preview?: boolean
multiple?: boolean multiple?: boolean
@@ -19,7 +17,6 @@
} }
let { let {
open = $bindable(),
image = $bindable(null), image = $bindable(null),
preview = true, preview = true,
multiple = true, multiple = true,
@@ -57,7 +54,6 @@
onupload?.(uploaded) onupload?.(uploaded)
progress = 1 progress = 1
open = false
} catch (err) { } catch (err) {
toast({ toast({
content: errorMessage(err), content: errorMessage(err),
@@ -69,10 +65,6 @@
} }
</script> </script>
<Modal bind:open>
{#snippet customTitle()}
<div class="flex items-center gap-2">Upload image</div>
{/snippet}
{#if progress != 1} {#if progress != 1}
<div transition:slide={{ duration: 500, easing: expoOut }} class="w-full"> <div transition:slide={{ duration: 500, easing: expoOut }} class="w-full">
<ProgressBar {progress} /> <ProgressBar {progress} />
@@ -134,4 +126,3 @@
Upload Upload
</Button> </Button>
</form> </form>
</Modal>
@@ -0,0 +1,69 @@
<script lang="ts">
import { Button, Modal, TextInput } from 'mono-svelte'
import Switch from '../input/Switch.svelte'
import { t } from '$lib/i18n/translations'
import ImageAttachForm from './ImageAttachForm.svelte'
let {
open = $bindable(),
imageUrl: passedImageUrl = $bindable(),
}: { open: boolean; imageUrl?: string } = $props()
let imageUrl = $derived(passedImageUrl)
let customUrl = $state(false)
</script>
<Modal bind:open title={$t('form.post.uploadImage')}>
<div class="flex justify-between gap-1 flex-wrap">
<Switch
options={[false, true]}
optionNames={[$t('common.attach'), $t('content.url')]}
bind:selected={customUrl}
/>
<Button
onclick={() => {
passedImageUrl = undefined
open = false
}}
size="xs"
rounding="xl"
>
{$t('common.remove')}
</Button>
</div>
{#if customUrl}
<form
onsubmit={e => {
e.preventDefault()
if (!imageUrl) throw new Error('missing imageurl')
if (URL.canParse != undefined) {
if (!URL.canParse(imageUrl)) throw new Error('invalid URL')
}
passedImageUrl = imageUrl
open = false
}}
class="contents"
>
<TextInput
label={$t('content.url')}
bind:value={imageUrl}
pattern="http(s)?:\/\/(.*).(png|jpg|gif|avif|webp|jpeg|jxl|svg|bmp)"
/>
<Button submit color="primary" size="lg">
{$t('form.submit')}
</Button>
</form>
{:else}
<ImageAttachForm
multiple={false}
onupload={uploaded => {
open = false
passedImageUrl = uploaded[0]
}}
/>
{/if}
</Modal>
+12 -14
View File
@@ -1,11 +1,18 @@
<script lang="ts"> <script lang="ts">
import { t } from '$lib/i18n/translations' import { t } from '$lib/i18n/translations'
import { DocumentPlus, Icon, Plus } from 'svelte-hero-icons'
import ImageUploadModal from '../lemmy/modal/ImageUploadModal.svelte'
import { Label } from 'mono-svelte' import { Label } from 'mono-svelte'
import { DocumentPlus, Icon, Plus } from 'svelte-hero-icons'
import ImageInputModal from './ImageInputModal.svelte'
let { imageUrl = $bindable(), label }: { imageUrl?: string; label?: string } = let {
$props() imageUrl: passedImageUrl = $bindable(),
label,
}: {
imageUrl?: string
label?: string
} = $props()
let imageUrl = $derived(passedImageUrl)
let open = $state(false) let open = $state(false)
</script> </script>
@@ -43,15 +50,6 @@
{$t('common.attach')} {$t('common.attach')}
</p> </p>
{/if} {/if}
{#if open} <ImageInputModal bind:open bind:imageUrl={passedImageUrl} />
<ImageUploadModal
bind:open
multiple={false}
onupload={uploaded => {
open = false
imageUrl = uploaded[0]
}}
/>
{/if}
</button> </button>
</div> </div>
@@ -251,14 +251,10 @@
</script> </script>
{#if uploadingImage} {#if uploadingImage}
{#await import('$lib/components/lemmy/modal/ImageUploadModal.svelte') then { default: UploadModal }} {#await import('$lib/components/form/ImageInputModal.svelte') then { default: UploadModal }}
<UploadModal <UploadModal
bind:open={uploadingImage} bind:open={uploadingImage}
multiple={false} bind:imageUrl={() => '', v => (data.url = v)}
onupload={e => {
if (e) data.url = e[0]
uploadingImage = false
}}
/> />
{/await} {/await}
{/if} {/if}
@@ -2,7 +2,7 @@
import MultiSelect from '$lib/components/input/Switch.svelte' import MultiSelect from '$lib/components/input/Switch.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte' import Markdown from '$lib/components/markdown/Markdown.svelte'
import { t } from '$lib/i18n/translations' import { t } from '$lib/i18n/translations'
import { Button, Label, TextArea } from 'mono-svelte' import { Button, Label, Modal, TextArea } from 'mono-svelte'
import type { TextAreaProps } from 'mono-svelte/forms/TextArea.svelte' import type { TextAreaProps } from 'mono-svelte/forms/TextArea.svelte'
import { tick } from 'svelte' import { tick } from 'svelte'
import { import {
@@ -17,8 +17,8 @@
Photo, Photo,
Strikethrough, Strikethrough,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import ImageUploadModal from '../lemmy/modal/ImageUploadModal.svelte'
import type { ClassValue } from 'svelte/elements' import type { ClassValue } from 'svelte/elements'
import ImageAttachForm from '../form/ImageAttachForm.svelte'
let textArea: HTMLTextAreaElement | undefined = $state() let textArea: HTMLTextAreaElement | undefined = $state()
@@ -122,8 +122,8 @@
</script> </script>
{#if uploadingImage && images} {#if uploadingImage && images}
<ImageUploadModal <Modal title={$t('form.post.uploadImage')} bind:open={uploadingImage}>
bind:open={uploadingImage} <ImageAttachForm
bind:image bind:image
onupload={e => { onupload={e => {
e.forEach(i => { e.forEach(i => {
@@ -131,6 +131,7 @@
}) })
}} }}
/> />
</Modal>
{/if} {/if}
<div> <div>
+2 -1
View File
@@ -254,7 +254,8 @@
"warning": "This is permanent. Are you sure you want to do this?" "warning": "This is permanent. Are you sure you want to do this?"
}, },
"overview": "Overview", "overview": "Overview",
"submissions": "Submissions" "submissions": "Submissions",
"edit": "Edit"
}, },
"translators": "Translators", "translators": "Translators",
"accounts": "Accounts", "accounts": "Accounts",
+5 -3
View File
@@ -77,6 +77,8 @@
<Portal> <Portal>
{#if open} {#if open}
<!--svelte-ignore a11y_interactive_supports_focus-->
<!--svelte-ignore a11y_click_events_have_key_events-->
<div <div
role="dialog" role="dialog"
class="overflow-hidden fixed top-0 left-0 w-screen h-screen z-100 class="overflow-hidden fixed top-0 left-0 w-screen h-screen z-100
@@ -97,9 +99,9 @@ bg-white/50 dark:bg-black/50 box-border p-4"
duration: 300, duration: 300,
}} }}
class="w-full border border-slate-200 border-b-slate-300 class="w-full border border-slate-200 border-b-slate-300
dark:border-zinc-900 dark:border-t-zinc-800 dark:border-b-zinc-900 dark:border-zinc-900
rounded-2xl max-w-lg box-border mx-auto overscroll-contain shadow-lg overflow-auto rounded-3xl max-w-lg box-border mx-auto overscroll-contain shadow-lg overflow-auto
p-5 flex flex-col gap-2 dark:bg-zinc-925 p-5 flex flex-col gap-2 dark:bg-zinc-950
bg-white relative {clazz}" bg-white relative {clazz}"
> >
{#if dismissable} {#if dismissable}
+18 -75
View File
@@ -1,6 +1,5 @@
<script lang="ts"> <script lang="ts">
import { profile } from '$lib/auth.svelte.js' import { profile } from '$lib/auth.svelte.js'
import ImageUploadModal from '$lib/components/lemmy/modal/ImageUploadModal.svelte'
import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte' import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte'
import Header from '$lib/components/ui/layout/pages/Header.svelte' import Header from '$lib/components/ui/layout/pages/Header.svelte'
import SectionTitle from '$lib/components/ui/SectionTitle.svelte' import SectionTitle from '$lib/components/ui/SectionTitle.svelte'
@@ -23,6 +22,7 @@
import { DocumentPlus, Icon, Plus } from 'svelte-hero-icons' import { DocumentPlus, Icon, Plus } from 'svelte-hero-icons'
import type { PageData } from './$types.js' import type { PageData } from './$types.js'
import { errorMessage } from '$lib/lemmy/error.js' import { errorMessage } from '$lib/lemmy/error.js'
import ImageInputUpload from '$lib/components/form/ImageInputUpload.svelte'
interface Props { interface Props {
data: PageData data: PageData
@@ -108,66 +108,18 @@
} }
label={$t('routes.legal.title')} label={$t('routes.legal.title')}
/> />
<div class="flex flex-col gap-1"> <ImageInputUpload
<Label>{$t('routes.admin.config.icon')}</Label> label={$t('routes.admin.config.icon')}
<button bind:imageUrl={formData.icon}
type="button"
onclick={() => (uploading.icon = !uploading.icon)}
class="flex flex-col gap-4 bg-white dark:bg-black border border-slate-300 dark:border-zinc-800 p-4 w-full h-32 rounded-xl"
>
{#if formData.icon}
<img src={formData.icon} alt="" class="rounded-md mx-auto h-full" />
{:else}
<Icon
src={DocumentPlus}
size="48"
class="text-slate-500 dark:text-zinc-500 mx-auto my-auto"
/> />
{/if} <ImageInputUpload
</button> label={$t('routes.admin.config.banner')}
{#if uploading.icon} bind:imageUrl={formData.banner}
<ImageUploadModal
bind:open={uploading.icon}
multiple={false}
onupload={uploaded => {
uploading.icon = false
formData.icon = uploaded[0]
}}
/> />
{/if} <Switch bind:checked={formData.enable_downvotes}>
</div>
<div class="flex flex-col gap-1">
<Label>{$t('routes.admin.config.banner')}</Label>
<button
type="button"
onclick={() => (uploading.banner = !uploading.banner)}
class="flex flex-col gap-4 bg-white dark:bg-black border border-slate-300 dark:border-zinc-800 p-4 w-full h-32 rounded-xl"
>
{#if formData.banner}
<img src={formData.banner} alt="" class="rounded-md mx-auto h-full" />
{:else}
<Icon
src={DocumentPlus}
size="48"
class="text-slate-500 dark:text-zinc-500 mx-auto my-auto"
/>
{/if}
</button>
{#if uploading.banner}
<ImageUploadModal
bind:open={uploading.banner}
multiple={false}
onupload={uploaded => {
uploading.banner = false
formData.banner = uploaded[0]
}}
/>
{/if}
</div>
<Switch bind:checked={formData.enable_downvotes} defaultValue={true}>
{$t('routes.admin.config.downvotesEnabled')} {$t('routes.admin.config.downvotesEnabled')}
</Switch> </Switch>
<Switch bind:checked={formData.enable_nsfw} defaultValue={true}> <Switch bind:checked={formData.enable_nsfw}>
{$t('routes.admin.config.nsfwEnabled')} {$t('routes.admin.config.nsfwEnabled')}
</Switch> </Switch>
<Select <Select
@@ -192,25 +144,16 @@
bind:value={formData.application_question} bind:value={formData.application_question}
/> />
{/if} {/if}
<Switch <Switch bind:checked={formData.community_creation_admin_only}>
bind:checked={formData.community_creation_admin_only}
defaultValue={true}
>
{$t('routes.admin.config.adminCommunityOnly')} {$t('routes.admin.config.adminCommunityOnly')}
</Switch> </Switch>
<Switch <Switch bind:checked={formData.require_email_verification}>
bind:checked={formData.require_email_verification}
defaultValue={true}
>
{$t('routes.admin.config.requireVerifyEmail')} {$t('routes.admin.config.requireVerifyEmail')}
</Switch> </Switch>
<Switch <Switch bind:checked={formData.application_email_admins}>
bind:checked={formData.application_email_admins}
defaultValue={true}
>
{$t('routes.admin.config.emailAdminsOnApplication')} {$t('routes.admin.config.emailAdminsOnApplication')}
</Switch> </Switch>
<Switch bind:checked={formData.reports_email_admins} defaultValue={true}> <Switch bind:checked={formData.reports_email_admins}>
{$t('routes.admin.config.emailAdminsOnReport')} {$t('routes.admin.config.emailAdminsOnReport')}
</Switch> </Switch>
<Select <Select
@@ -223,10 +166,10 @@
{$t('routes.admin.config.listingType.local')} {$t('routes.admin.config.listingType.local')}
</Option> </Option>
</Select> </Select>
<Switch bind:checked={formData.private_instance} defaultValue={true}> <Switch bind:checked={formData.private_instance}>
{$t('routes.admin.config.private')} {$t('routes.admin.config.private')}
</Switch> </Switch>
<Switch bind:checked={formData.hide_modlog_mod_names} defaultValue={true}> <Switch bind:checked={formData.hide_modlog_mod_names}>
{$t('routes.admin.config.hideModlogModNames')} {$t('routes.admin.config.hideModlogModNames')}
</Switch> </Switch>
<TextInput <TextInput
@@ -234,13 +177,13 @@
label={$t('routes.admin.config.slurFilter')} label={$t('routes.admin.config.slurFilter')}
placeholder="(word1|word2)" placeholder="(word1|word2)"
/> />
<Switch bind:checked={formData.federation_enabled} defaultValue={true}> <Switch bind:checked={formData.federation_enabled}>
{$t('routes.admin.config.federation')} {$t('routes.admin.config.federation')}
</Switch> </Switch>
<Switch bind:checked={formData.federation_debug} defaultValue={false}> <Switch bind:checked={formData.federation_debug}>
{$t('routes.admin.config.federationDebug')} {$t('routes.admin.config.federationDebug')}
</Switch> </Switch>
<Switch bind:checked={formData.captcha_enabled} defaultValue={false}> <Switch bind:checked={formData.captcha_enabled}>
{$t('routes.admin.config.captcha.enabled')} {$t('routes.admin.config.captcha.enabled')}
</Switch> </Switch>
@@ -1,6 +1,4 @@
<script lang="ts"> <script lang="ts">
import { preventDefault } from 'svelte/legacy'
import { profile } from '$lib/auth.svelte.js' import { profile } from '$lib/auth.svelte.js'
import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte' import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte'
import Header from '$lib/components/ui/layout/pages/Header.svelte' import Header from '$lib/components/ui/layout/pages/Header.svelte'
@@ -22,6 +20,7 @@
} from 'mono-svelte' } from 'mono-svelte'
import { Icon, Plus } from 'svelte-hero-icons' import { Icon, Plus } from 'svelte-hero-icons'
import type { PageData } from './$types' import type { PageData } from './$types'
import ImageInputUpload from '$lib/components/form/ImageInputUpload.svelte'
interface Props { interface Props {
inline?: boolean inline?: boolean
@@ -37,35 +36,14 @@
discussion_languages: data.my_user?.discussion_languages, discussion_languages: data.my_user?.discussion_languages,
}) })
let profileImage: FileList | undefined = $state()
let bannerImage: FileList | undefined = $state()
async function save() { async function save() {
if (!formData || !profile.current?.jwt) return if (!formData || !profile.current?.jwt) return
loading = true loading = true
try { try {
let pfp = profileImage
? await uploadImage(
profileImage[0],
profile.current.instance,
profile.current.jwt,
)
: undefined
let banner = bannerImage
? await uploadImage(
bannerImage[0],
profile.current.instance,
profile.current.jwt,
)
: undefined
await getClient().saveUserSettings({ await getClient().saveUserSettings({
...formData, ...formData,
avatar: pfp,
banner: banner,
}) })
toast({ toast({
@@ -84,9 +62,15 @@
let loading = $state(false) let loading = $state(false)
</script> </script>
<form class="flex flex-col gap-4 h-full" onsubmit={preventDefault(save)}> <form
class="flex flex-col gap-4 h-full"
onsubmit={e => {
e.preventDefault()
save()
}}
>
{#if !inline} {#if !inline}
<Header pageHeader>{$t('routes.profile.settings')}</Header> <Header pageHeader>{$t('routes.profile.edit')}</Header>
{/if} {/if}
{@render children?.()} {@render children?.()}
{#if data.my_user?.local_user_view?.local_user && formData} {#if data.my_user?.local_user_view?.local_user && formData}
@@ -103,8 +87,14 @@
previewButton previewButton
/> />
<div class="flex gap-2 items-center *:flex-1"> <div class="flex gap-2 items-center *:flex-1">
<ImageInput label={$t('form.profile.avatar')} bind:files={profileImage} /> <ImageInputUpload
<ImageInput label={$t('form.profile.banner')} bind:files={bannerImage} /> label={$t('form.profile.avatar')}
bind:imageUrl={formData.avatar}
/>
<ImageInputUpload
label={$t('form.profile.banner')}
bind:imageUrl={formData.banner}
/>
</div> </div>
<TextInput <TextInput
label={$t('form.profile.matrix')} label={$t('form.profile.matrix')}
@@ -114,18 +104,12 @@
<Switch bind:checked={formData.show_nsfw}> <Switch bind:checked={formData.show_nsfw}>
{$t('form.profile.showNSFW')} {$t('form.profile.showNSFW')}
</Switch> </Switch>
<Switch bind:checked={formData.show_scores}>
{$t('form.profile.scores')}
</Switch>
<Switch bind:checked={formData.bot_account}> <Switch bind:checked={formData.bot_account}>
{$t('form.profile.bot')} {$t('form.profile.bot')}
</Switch> </Switch>
<Switch bind:checked={formData.show_bot_accounts}> <Switch bind:checked={formData.show_bot_accounts}>
{$t('form.profile.showBots')} {$t('form.profile.showBots')}
</Switch> </Switch>
<Switch bind:checked={formData.show_read_posts}>
{$t('form.profile.showRead')}
</Switch>
<div class="space-y-1"> <div class="space-y-1">
<SectionTitle id="languages"> <SectionTitle id="languages">
{$t('form.profile.languages.title')} {$t('form.profile.languages.title')}
+2 -3
View File
@@ -27,11 +27,10 @@
<div class="flex flex-col gap-4 h-full z-0"> <div class="flex flex-col gap-4 h-full z-0">
<div class="flex flex-row justify-between"> <div class="flex flex-row justify-between">
<Tabs <Tabs
class="overflow-auto"
routes={[ routes={[
{ {
href: '/profile', href: '/profile',
name: $t('routes.profile.overview'), name: $t('profile.profile'),
}, },
{ {
href: '/profile/user', href: '/profile/user',
@@ -40,7 +39,7 @@
{ {
href: '/profile/settings', href: '/profile/settings',
name: $t('routes.profile.settings'), name: $t('routes.profile.edit'),
}, },
{ {
+1 -71
View File
@@ -1,78 +1,8 @@
<script lang="ts"> <script lang="ts">
import ItemList from '$lib/components/lemmy/generic/ItemList.svelte'
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
import EntityHeader from '$lib/components/ui/EntityHeader.svelte'
import Expandable from '$lib/components/ui/Expandable.svelte'
import Header from '$lib/components/ui/layout/pages/Header.svelte' import Header from '$lib/components/ui/layout/pages/Header.svelte'
import { publishedToDate } from '$lib/components/util/date.js'
import { formatRelativeDate } from '$lib/components/util/RelativeDate.svelte'
import { t } from '$lib/i18n/translations.js' import { t } from '$lib/i18n/translations.js'
import { communityLink } from '$lib/lemmy/generic.js'
import { settings } from '$lib/settings.svelte.js'
let { data } = $props()
let my_user = $derived(data.my_user!.local_user_view)
</script> </script>
<Header pageHeader> <Header pageHeader>
{$t('routes.profile.overview')} {$t('profile.profile')}
</Header> </Header>
<EntityHeader
avatar={my_user.person.avatar}
name={my_user.person.display_name || my_user.person.name}
banner={my_user.person.banner}
bio={my_user.person.bio}
stats={[
{
name: $t('content.posts'),
value: my_user.counts.post_count.toString(),
},
{
name: $t('content.comments'),
value: my_user.counts.comment_count.toString(),
},
{
name: $t('stats.joined'),
value: formatRelativeDate(publishedToDate(my_user.person.published), {
style: 'short',
}).toString(),
format: false,
},
]}
>
{#snippet nameDetail()}
<span class="text-sm flex gap-0 items-center w-max">
@
<UserLink
showInstance
user={my_user.person}
displayName={false}
class="font-normal"
/>
</span>
{/snippet}
{#if data.moderates && data.moderates.length > 0}
<Expandable class="">
{#snippet title()}
{$t('routes.profile.moderates')}
<hr class="flex-1 w-full border-slate-200 dark:border-zinc-800 mx-3" />
{/snippet}
<ItemList
items={data.moderates.map(m => ({
id: m.community.id,
name: m.community.title,
url: communityLink(m.community),
avatar: m.community.icon,
instance: new URL(m.community.actor_id).hostname,
}))}
/>
</Expandable>
{/if}
</EntityHeader>
{#if settings.debugInfo}
<pre>
{JSON.stringify(my_user, undefined, 4)}
</pre>
{/if}