feat: add image uploading

This commit is contained in:
Xyphyn committed 2023-08-04 13:57:45 -07:00
1 parent 00348143b8
commit 270855edf1
1 file changed
+56
@@ -1,12 +1,18 @@
<script lang="ts"> <script lang="ts">
import { profile } from '$lib/auth.js'
import Button from '$lib/components/input/Button.svelte' import Button from '$lib/components/input/Button.svelte'
import FileInput from '$lib/components/input/FileInput.svelte'
import TextArea from '$lib/components/input/TextArea.svelte' import TextArea from '$lib/components/input/TextArea.svelte'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { uploadImage } from '$lib/lemmy.js'
import { import {
CodeBracket, CodeBracket,
ExclamationTriangle, ExclamationTriangle,
Icon, Icon,
Link, Link,
ListBullet, ListBullet,
Photo,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
export let images: boolean = true export let images: boolean = true
@@ -40,9 +46,50 @@
textArea.focus() textArea.focus()
textArea.selectionStart = startPos + start.length textArea.selectionStart = startPos + start.length
textArea.selectionEnd = endPos + start.length textArea.selectionEnd = endPos + start.length
value = textArea.value
}
let uploadingImage = false
let loading = false
let image: FileList | null = null
async function upload() {
if (!$profile?.jwt || image == null) return
loading = true
try {
const uploaded = await uploadImage(image[0])
if (!uploaded) throw new Error('Image upload returned undefined')
wrapSelection(`![](${uploaded})`, '')
uploadingImage = false
} catch (err) {
toast({
content: err as any,
type: 'error',
})
}
loading = false
} }
</script> </script>
{#if uploadingImage && images}
<Modal bind:open={uploadingImage}>
<span slot="title">Upload image</span>
<form class="flex flex-col gap-4" on:submit|preventDefault={upload}>
<FileInput image bind:files={image} />
<Button {loading} disabled={loading} submit color="primary" size="lg">
Upload
</Button>
</form>
</Modal>
{/if}
<div <div
class="flex flex-col border border-slate-200 dark:border-zinc-800 rounded-md class="flex flex-col border border-slate-200 dark:border-zinc-800 rounded-md
overflow-hidden" overflow-hidden"
@@ -132,6 +179,15 @@ overflow-hidden"
<sup>1</sup> <sup>1</sup>
</span> </span>
</Button> </Button>
{#if images}
<Button
on:click={() => (uploadingImage = !uploadingImage)}
title="Image"
size="square-md"
>
<Icon src={Photo} size="16" mini />
</Button>
{/if}
</div> </div>
<!--Actual text area--> <!--Actual text area-->
<TextArea <TextArea