jws-frontend/src/components/ImageUploadDialog.vue

231 lines
5.4 KiB
Vue
Raw Normal View History

2024-08-01 14:20:13 +07:00
<script lang="ts" setup>
import AppBox from './app/AppBox.vue';
2024-08-28 13:17:29 +07:00
import { CancelButton, ClearButton, SaveButton } from './button';
2024-08-01 14:20:13 +07:00
2024-08-02 11:36:48 +07:00
defineExpose({ browse });
2024-08-02 09:18:11 +07:00
defineProps<{
changeDisabled?: boolean;
2024-08-02 09:28:09 +07:00
clearButtonDisabled?: boolean;
clearButton?: boolean;
2024-08-02 09:18:11 +07:00
hiddenFooter?: boolean;
2024-08-02 09:28:09 +07:00
defaultUrl?: string;
2024-08-02 09:18:11 +07:00
}>();
2024-08-08 09:45:59 +07:00
const emit = defineEmits<{
2024-08-02 11:36:48 +07:00
(e: 'save', file: File | null, url: string | null): void;
}>();
2024-08-01 14:20:13 +07:00
const imageUrl = defineModel<string>('imageUrl', {
required: false,
default: '',
});
const fallbackUrl = defineModel<string>('fallbackUrl', {
required: false,
default: '',
});
const dialogState = defineModel<boolean>('dialogState', {
required: false,
default: true,
});
2024-08-01 14:29:00 +07:00
const file = defineModel<File | null>('file', {
required: true,
});
2024-08-01 14:20:13 +07:00
const reader = new FileReader();
2024-08-02 11:36:48 +07:00
const inputFile = (() => {
const _element = document.createElement('input');
_element.type = 'file';
_element.accept = 'image/*';
_element.addEventListener('change', change);
return _element;
})();
2024-08-01 14:20:13 +07:00
reader.addEventListener('load', () => {
if (typeof reader.result === 'string') imageUrl.value = reader.result;
});
2024-08-02 11:36:48 +07:00
function browse() {
inputFile?.click();
}
2024-08-01 14:20:13 +07:00
function change(e: Event) {
2024-08-01 14:29:00 +07:00
const _element = e.target as HTMLInputElement | null;
const _file = _element?.files?.[0];
2024-08-01 14:20:13 +07:00
2024-08-01 14:29:00 +07:00
if (_file) {
file.value = _file;
reader.readAsDataURL(_file);
2024-08-08 17:09:36 +07:00
if (!dialogState.value) {
emit('save', _file, imageUrl.value);
}
2024-08-01 14:29:00 +07:00
}
2024-08-01 14:20:13 +07:00
}
async function downloadImage(url: string) {
const res = await fetch(url);
const blob = await res.blob();
let extension = '';
if (blob.type === 'image/jpeg') extension = '.jpg';
2024-08-22 13:51:05 +07:00
else if (blob.type === 'image/png') extension = '.png';
else return;
let a = document.createElement('a');
a.download = `download${extension}`;
a.href = window.URL.createObjectURL(blob);
a.click();
a.remove();
}
2024-08-01 14:20:13 +07:00
</script>
<template>
<q-dialog v-model="dialogState">
2024-08-01 17:18:04 +07:00
<AppBox class="image-dialog-content">
2024-08-01 14:20:13 +07:00
<div class="row items-center q-py-sm q-px-md bordered-b">
<div style="flex: 1"><slot name="title" /></div>
<div>
<q-btn
round
flat
icon="mdi-close"
padding="xs"
class="close-btn"
:class="{ dark: $q.dark.isActive }"
v-close-popup
/>
</div>
</div>
2024-08-01 17:18:04 +07:00
<div class="image-dialog-body">
2024-08-01 14:20:13 +07:00
<img
2024-08-15 11:12:47 +07:00
:src="imageUrl || fallbackUrl"
v-if="imageUrl || fallbackUrl"
2024-08-01 14:20:13 +07:00
class="image-container"
style="object-fit: contain"
@error="imageUrl = ''"
2024-08-01 14:20:13 +07:00
/>
<div class="image-container" v-if="!imageUrl && !fallbackUrl">
<slot name="error"></slot>
2024-08-01 14:20:13 +07:00
</div>
<div style="position: fixed; padding: var(--size-2)">
<q-btn
2024-08-27 17:32:17 +07:00
class="upload-image-btn q-mr-md"
icon="mdi-camera-outline"
2024-08-01 14:20:13 +07:00
size="md"
unelevated
round
v-if="!changeDisabled"
2024-08-01 14:20:13 +07:00
@click="inputFile?.click()"
style="color: hsla(var(--stone-0-hsl) / 0.7)"
></q-btn>
<q-btn
2024-08-27 17:32:17 +07:00
class="upload-image-btn"
icon="mdi-download-outline"
size="md"
unelevated
round
@click="downloadImage(imageUrl)"
style="color: hsla(var(--stone-0-hsl) / 0.7)"
2024-08-01 14:20:13 +07:00
></q-btn>
</div>
</div>
2024-08-02 09:18:11 +07:00
<div
class="row items-center justify-end q-py-sm q-px-md bordered-t"
v-if="!hiddenFooter"
>
2024-08-28 13:17:29 +07:00
<ClearButton
outlined
2024-08-02 09:28:09 +07:00
@click="
inputFile && (inputFile.value = ''),
(imageUrl = defaultUrl || fallbackUrl || ''),
(file = null)
"
class="q-px-md q-mr-auto"
2024-08-28 13:17:29 +07:00
:disabled="
2024-08-02 09:28:09 +07:00
clearButtonDisabled ||
imageUrl === fallbackUrl ||
imageUrl === defaultUrl ||
!imageUrl
"
v-if="clearButton"
/>
<CancelButton
outlined
class="q-px-md q-mr-sm"
2024-08-15 11:02:01 +07:00
@click="
inputFile && (inputFile.value = ''),
(imageUrl = defaultUrl || fallbackUrl || ''),
(file = null)
"
2024-08-01 14:20:13 +07:00
v-close-popup
/>
<SaveButton
outlined
2024-08-02 03:52:56 +00:00
@click="$emit('save', inputFile?.files?.[0] || null, imageUrl)"
2024-08-01 14:20:13 +07:00
/>
</div>
</AppBox>
</q-dialog>
</template>
<style scoped>
2024-08-01 17:18:04 +07:00
.image-dialog-content {
2024-08-01 14:20:13 +07:00
padding: 0;
border-radius: var(--radius-2);
flex-wrap: nowrap;
width: 100%;
max-width: 80%;
}
@media (min-width: 768px) {
2024-08-01 17:18:04 +07:00
.image-dialog-content {
2024-08-01 14:20:13 +07:00
max-width: 60%;
}
}
2024-08-01 17:18:04 +07:00
.image-dialog-body {
2024-08-01 14:20:13 +07:00
overflow-y: auto;
background-color: var(--surface-2) !important;
position: relative;
display: flex;
flex-direction: column;
flex: 1;
2024-08-01 17:18:04 +07:00
height: calc(100vh - 200px);
2024-08-01 14:20:13 +07:00
}
.upload-image-btn {
transition: 0.3s background-color ease-in-out;
background-color: hsla(0 0% 0% / 0.2);
backdrop-filter: blur(1px);
&:not(:hover) {
color: hsla(0 0% 40% / 1);
background-color: hsla(0 0% 0% / 0.2);
}
}
.image-container {
width: 100%;
2024-08-01 17:18:04 +07:00
height: calc(100vh - 200px);
2024-08-01 14:20:13 +07:00
min-height: 480px;
2024-08-02 09:31:49 +07:00
& > :deep(*) {
2024-08-01 14:20:13 +07:00
height: 100%;
width: 100%;
}
}
.image-container > :deep(*:not(:first-child)) {
display: none;
}
2024-08-02 03:52:56 +00:00
.close-btn {
color: hsl(var(--negative-bg));
background-color: hsla(var(--negative-bg) / 0.1);
&.dark {
background-color: transparent;
border: 1px solid hsl(var(--negative-bg));
}
}
2024-08-01 14:20:13 +07:00
</style>