2022-12-26 09:50:11 +01:00
|
|
|
<script lang="ts" setup>
|
2023-01-01 21:47:46 +01:00
|
|
|
import { fileOpen } from 'browser-fs-access'
|
|
|
|
import type { FileWithHandle } from 'browser-fs-access'
|
|
|
|
|
2022-12-26 09:50:11 +01:00
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
/** The image src before change */
|
|
|
|
original?: string
|
|
|
|
/** Allowed file types */
|
|
|
|
allowedFileTypes?: string[]
|
|
|
|
/** Allowed file size */
|
|
|
|
allowedFileSize?: number
|
|
|
|
|
|
|
|
imgClass?: string
|
|
|
|
|
|
|
|
loading?: boolean
|
|
|
|
}>(), {
|
|
|
|
allowedFileTypes: () => ['image/jpeg', 'image/png'],
|
|
|
|
allowedFileSize: 1024 * 1024 * 5, // 5 MB
|
|
|
|
})
|
2023-01-01 21:47:46 +01:00
|
|
|
const emit = defineEmits<{
|
|
|
|
(event: 'pick', value: FileWithHandle): void
|
2022-12-26 09:50:11 +01:00
|
|
|
(event: 'error', code: number, message: string): void
|
|
|
|
}>()
|
|
|
|
|
2023-01-06 16:46:36 +01:00
|
|
|
const { modelValue: file } = defineModel<{
|
|
|
|
modelValue: FileWithHandle | null
|
|
|
|
}>()
|
2022-12-26 09:50:11 +01:00
|
|
|
|
|
|
|
const { t } = useI18n()
|
|
|
|
|
2023-01-01 21:47:46 +01:00
|
|
|
const defaultImage = computed(() => props.original || '')
|
|
|
|
/** Preview of selected images */
|
|
|
|
const previewImage = ref('')
|
|
|
|
/** The current images on display */
|
|
|
|
const imageSrc = computed<string>(() => previewImage.value || defaultImage.value)
|
2022-12-26 09:50:11 +01:00
|
|
|
|
2023-03-30 21:01:24 +02:00
|
|
|
async function pickImage() {
|
2023-02-06 10:34:50 +01:00
|
|
|
if (process.server)
|
|
|
|
return
|
2023-01-01 21:47:46 +01:00
|
|
|
const image = await fileOpen({
|
|
|
|
description: 'Image',
|
|
|
|
mimeTypes: props.allowedFileTypes,
|
|
|
|
})
|
2022-12-26 09:50:11 +01:00
|
|
|
|
2023-01-01 21:47:46 +01:00
|
|
|
if (!props.allowedFileTypes.includes(image.type)) {
|
|
|
|
emit('error', 1, t('error.unsupported_file_format'))
|
|
|
|
return
|
2022-12-26 09:50:11 +01:00
|
|
|
}
|
|
|
|
else if (image.size > props.allowedFileSize) {
|
2023-01-01 21:47:46 +01:00
|
|
|
emit('error', 2, t('error.file_size_cannot_exceed_n_mb', [5]))
|
|
|
|
return
|
2022-12-26 09:50:11 +01:00
|
|
|
}
|
|
|
|
|
2023-01-01 21:47:46 +01:00
|
|
|
file.value = image
|
|
|
|
emit('pick', file.value)
|
|
|
|
}
|
2022-12-26 09:50:11 +01:00
|
|
|
|
2023-01-01 21:47:46 +01:00
|
|
|
watch(file, (image, _, onCleanup) => {
|
2022-12-26 09:50:11 +01:00
|
|
|
let expired = false
|
|
|
|
onCleanup(() => expired = true)
|
|
|
|
|
2023-01-01 21:47:46 +01:00
|
|
|
if (!image) {
|
2022-12-26 09:50:11 +01:00
|
|
|
previewImage.value = ''
|
2023-01-01 21:47:46 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
const reader = new FileReader()
|
|
|
|
reader.readAsDataURL(image)
|
|
|
|
reader.onload = (evt) => {
|
|
|
|
if (expired)
|
|
|
|
return
|
|
|
|
previewImage.value = evt.target?.result as string
|
2022-12-26 09:50:11 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<label
|
|
|
|
class="bg-slate-500/10 focus-within:(outline outline-primary)"
|
|
|
|
relative
|
|
|
|
flex justify-center items-center
|
|
|
|
cursor-pointer
|
|
|
|
of-hidden
|
2023-01-01 21:47:46 +01:00
|
|
|
@click="pickImage"
|
2022-12-26 09:50:11 +01:00
|
|
|
>
|
|
|
|
<img
|
|
|
|
v-if="imageSrc"
|
|
|
|
:src="imageSrc"
|
|
|
|
:class="imgClass || ''"
|
|
|
|
object-cover
|
|
|
|
w-full
|
|
|
|
h-full
|
|
|
|
>
|
2023-01-13 17:00:32 +01:00
|
|
|
<span absolute bg="black/50" text-white rounded-full text-xl w12 h12 flex justify-center items-center hover="bg-black/40 text-primary">
|
|
|
|
<span block i-ri:upload-line />
|
|
|
|
</span>
|
2022-12-26 09:50:11 +01:00
|
|
|
|
2023-01-13 17:00:32 +01:00
|
|
|
<span
|
2022-12-26 09:50:11 +01:00
|
|
|
v-if="loading"
|
|
|
|
absolute inset-0
|
|
|
|
bg="black/30" text-white
|
|
|
|
flex justify-center items-center
|
|
|
|
>
|
2023-01-13 17:00:32 +01:00
|
|
|
<span class="animate-spin animate-duration-[2.5s] preserve-3d">
|
|
|
|
<span block i-ri:loader-4-line text-4xl />
|
|
|
|
</span>
|
|
|
|
</span>
|
2022-12-26 09:50:11 +01:00
|
|
|
</label>
|
|
|
|
</template>
|