impr: rewrite image upload handling
This commit is contained in:
11 files changed
+200
-286
No files matched your search
+58
-67
@@ -1,17 +1,15 @@
|
||||
<script lang="ts">
|
||||
import { profile } from '$lib/auth.svelte.js'
|
||||
import ProgressBar from '$lib/components/ui/ProgressBar.svelte'
|
||||
import { errorMessage } from '$lib/lemmy/error'
|
||||
import { t } from '$lib/i18n/translations'
|
||||
import { errorMessage } from '$lib/lemmy/error'
|
||||
import { uploadImage } from '$lib/util.svelte.js'
|
||||
import { toast } from 'mono-svelte'
|
||||
import { Button, Modal } from 'mono-svelte'
|
||||
import { Button, toast } from 'mono-svelte'
|
||||
import { DocumentPlus, Icon } from 'svelte-hero-icons'
|
||||
import { expoOut } from 'svelte/easing'
|
||||
import { slide } from 'svelte/transition'
|
||||
|
||||
interface Props {
|
||||
open: boolean
|
||||
image?: FileList | null
|
||||
preview?: boolean
|
||||
multiple?: boolean
|
||||
@@ -19,7 +17,6 @@
|
||||
}
|
||||
|
||||
let {
|
||||
open = $bindable(),
|
||||
image = $bindable(null),
|
||||
preview = true,
|
||||
multiple = true,
|
||||
@@ -57,7 +54,6 @@
|
||||
|
||||
onupload?.(uploaded)
|
||||
progress = 1
|
||||
open = false
|
||||
} catch (err) {
|
||||
toast({
|
||||
content: errorMessage(err),
|
||||
@@ -69,69 +65,64 @@
|
||||
}
|
||||
</script>
|
||||
|
||||
<Modal bind:open>
|
||||
{#snippet customTitle()}
|
||||
<div class="flex items-center gap-2">Upload image</div>
|
||||
{/snippet}
|
||||
{#if progress != 1}
|
||||
<div transition:slide={{ duration: 500, easing: expoOut }} class="w-full">
|
||||
<ProgressBar {progress} />
|
||||
</div>
|
||||
{/if}
|
||||
<form
|
||||
class="flex flex-col gap-4"
|
||||
onsubmit={e => {
|
||||
e.preventDefault()
|
||||
upload()
|
||||
}}
|
||||
>
|
||||
<label
|
||||
for="image-input"
|
||||
class="p-4 border-2 border-dashed rounded-xl cursor-pointer transition-colors
|
||||
{#if progress != 1}
|
||||
<div transition:slide={{ duration: 500, easing: expoOut }} class="w-full">
|
||||
<ProgressBar {progress} />
|
||||
</div>
|
||||
{/if}
|
||||
<form
|
||||
class="flex flex-col gap-4"
|
||||
onsubmit={e => {
|
||||
e.preventDefault()
|
||||
upload()
|
||||
}}
|
||||
>
|
||||
<label
|
||||
for="image-input"
|
||||
class="p-4 border-2 border-dashed rounded-xl cursor-pointer transition-colors
|
||||
border-slate-300 dark:border-zinc-700 hover:border-primary-900 dark:hover:border-primary-100"
|
||||
>
|
||||
{#if previewURLs}
|
||||
<div
|
||||
class="grid gap-x-2 gap-y-px max-h-64 overflow-auto
|
||||
>
|
||||
{#if previewURLs}
|
||||
<div
|
||||
class="grid gap-x-2 gap-y-px max-h-64 overflow-auto
|
||||
{multiple ? 'grid-cols-3 grid-rows-2' : 'grid-cols-1 grid-rows-1'}"
|
||||
>
|
||||
{#each previewURLs as file}
|
||||
<img
|
||||
src={file}
|
||||
onload={() => URL.revokeObjectURL(file)}
|
||||
alt={file}
|
||||
class="w-full rounded-md h-full object-cover transition-all
|
||||
>
|
||||
{#each previewURLs as file}
|
||||
<img
|
||||
src={file}
|
||||
onload={() => URL.revokeObjectURL(file)}
|
||||
alt={file}
|
||||
class="w-full rounded-md h-full object-cover transition-all
|
||||
border border-slate-200 dark:border-zinc-800 ring-1
|
||||
ring-slate-50 dark:ring-zinc-950 bg-white dark:bg-zinc-950"
|
||||
/>
|
||||
{/each}
|
||||
</div>
|
||||
{:else}
|
||||
<div
|
||||
class="flex flex-col justify-center w-full items-center gap-2
|
||||
/>
|
||||
{/each}
|
||||
</div>
|
||||
{:else}
|
||||
<div
|
||||
class="flex flex-col justify-center w-full items-center gap-2
|
||||
text-slate-600 dark:text-zinc-400"
|
||||
>
|
||||
<Icon src={DocumentPlus} size="32" />
|
||||
<span class="font-medium text-sm">{$t('form.post.selectFile')}</span>
|
||||
</div>
|
||||
{/if}
|
||||
<input
|
||||
{multiple}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
class="hidden"
|
||||
id="image-input"
|
||||
bind:files={image}
|
||||
/>
|
||||
</label>
|
||||
<Button
|
||||
loading={progress != 1}
|
||||
disabled={progress != 1 || image == null}
|
||||
submit
|
||||
color="primary"
|
||||
size="lg"
|
||||
>
|
||||
Upload
|
||||
</Button>
|
||||
</form>
|
||||
</Modal>
|
||||
>
|
||||
<Icon src={DocumentPlus} size="32" />
|
||||
<span class="font-medium text-sm">{$t('form.post.selectFile')}</span>
|
||||
</div>
|
||||
{/if}
|
||||
<input
|
||||
{multiple}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
class="hidden"
|
||||
id="image-input"
|
||||
bind:files={image}
|
||||
/>
|
||||
</label>
|
||||
<Button
|
||||
loading={progress != 1}
|
||||
disabled={progress != 1 || image == null}
|
||||
submit
|
||||
color="primary"
|
||||
size="lg"
|
||||
>
|
||||
Upload
|
||||
</Button>
|
||||
</form>
|
||||
@@ -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>
|
||||
@@ -1,11 +1,18 @@
|
||||
<script lang="ts">
|
||||
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 { DocumentPlus, Icon, Plus } from 'svelte-hero-icons'
|
||||
import ImageInputModal from './ImageInputModal.svelte'
|
||||
|
||||
let { imageUrl = $bindable(), label }: { imageUrl?: string; label?: string } =
|
||||
$props()
|
||||
let {
|
||||
imageUrl: passedImageUrl = $bindable(),
|
||||
label,
|
||||
}: {
|
||||
imageUrl?: string
|
||||
label?: string
|
||||
} = $props()
|
||||
|
||||
let imageUrl = $derived(passedImageUrl)
|
||||
|
||||
let open = $state(false)
|
||||
</script>
|
||||
@@ -43,15 +50,6 @@
|
||||
{$t('common.attach')}
|
||||
</p>
|
||||
{/if}
|
||||
{#if open}
|
||||
<ImageUploadModal
|
||||
bind:open
|
||||
multiple={false}
|
||||
onupload={uploaded => {
|
||||
open = false
|
||||
imageUrl = uploaded[0]
|
||||
}}
|
||||
/>
|
||||
{/if}
|
||||
<ImageInputModal bind:open bind:imageUrl={passedImageUrl} />
|
||||
</button>
|
||||
</div>
|
||||
@@ -251,14 +251,10 @@
|
||||
</script>
|
||||
|
||||
{#if uploadingImage}
|
||||
{#await import('$lib/components/lemmy/modal/ImageUploadModal.svelte') then { default: UploadModal }}
|
||||
{#await import('$lib/components/form/ImageInputModal.svelte') then { default: UploadModal }}
|
||||
<UploadModal
|
||||
bind:open={uploadingImage}
|
||||
multiple={false}
|
||||
onupload={e => {
|
||||
if (e) data.url = e[0]
|
||||
uploadingImage = false
|
||||
}}
|
||||
bind:imageUrl={() => '', v => (data.url = v)}
|
||||
/>
|
||||
{/await}
|
||||
{/if}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import MultiSelect from '$lib/components/input/Switch.svelte'
|
||||
import Markdown from '$lib/components/markdown/Markdown.svelte'
|
||||
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 { tick } from 'svelte'
|
||||
import {
|
||||
@@ -17,8 +17,8 @@
|
||||
Photo,
|
||||
Strikethrough,
|
||||
} from 'svelte-hero-icons'
|
||||
import ImageUploadModal from '../lemmy/modal/ImageUploadModal.svelte'
|
||||
import type { ClassValue } from 'svelte/elements'
|
||||
import ImageAttachForm from '../form/ImageAttachForm.svelte'
|
||||
|
||||
let textArea: HTMLTextAreaElement | undefined = $state()
|
||||
|
||||
@@ -122,15 +122,16 @@
|
||||
</script>
|
||||
|
||||
{#if uploadingImage && images}
|
||||
<ImageUploadModal
|
||||
bind:open={uploadingImage}
|
||||
bind:image
|
||||
onupload={e => {
|
||||
e.forEach(i => {
|
||||
wrapSelection(`\n\n`, '')
|
||||
})
|
||||
}}
|
||||
/>
|
||||
<Modal title={$t('form.post.uploadImage')} bind:open={uploadingImage}>
|
||||
<ImageAttachForm
|
||||
bind:image
|
||||
onupload={e => {
|
||||
e.forEach(i => {
|
||||
wrapSelection(`\n\n`, '')
|
||||
})
|
||||
}}
|
||||
/>
|
||||
</Modal>
|
||||
{/if}
|
||||
|
||||
<div>
|
||||
|
||||
@@ -254,7 +254,8 @@
|
||||
"warning": "This is permanent. Are you sure you want to do this?"
|
||||
},
|
||||
"overview": "Overview",
|
||||
"submissions": "Submissions"
|
||||
"submissions": "Submissions",
|
||||
"edit": "Edit"
|
||||
},
|
||||
"translators": "Translators",
|
||||
"accounts": "Accounts",
|
||||
|
||||
@@ -77,6 +77,8 @@
|
||||
|
||||
<Portal>
|
||||
{#if open}
|
||||
<!--svelte-ignore a11y_interactive_supports_focus-->
|
||||
<!--svelte-ignore a11y_click_events_have_key_events-->
|
||||
<div
|
||||
role="dialog"
|
||||
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,
|
||||
}}
|
||||
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
|
||||
rounded-2xl max-w-lg box-border mx-auto overscroll-contain shadow-lg overflow-auto
|
||||
p-5 flex flex-col gap-2 dark:bg-zinc-925
|
||||
dark:border-zinc-900
|
||||
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-950
|
||||
bg-white relative {clazz}"
|
||||
>
|
||||
{#if dismissable}
|
||||
|
||||
Reference in new issue
Block a user