feat: browseable without open dialog
This commit is contained in:
parent
6454265eb4
commit
a4777c4c46
1 changed files with 17 additions and 12 deletions
|
|
@ -1,13 +1,17 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { ref } from 'vue';
|
|
||||||
import AppBox from './app/AppBox.vue';
|
import AppBox from './app/AppBox.vue';
|
||||||
|
|
||||||
|
defineExpose({ browse });
|
||||||
defineProps<{
|
defineProps<{
|
||||||
clearButtonDisabled?: boolean;
|
clearButtonDisabled?: boolean;
|
||||||
clearButton?: boolean;
|
clearButton?: boolean;
|
||||||
hiddenFooter?: boolean;
|
hiddenFooter?: boolean;
|
||||||
defaultUrl?: string;
|
defaultUrl?: string;
|
||||||
}>();
|
}>();
|
||||||
|
defineEmits<{
|
||||||
|
(e: 'save', file: File | null, url: string | null): void;
|
||||||
|
}>();
|
||||||
|
|
||||||
const imageUrl = defineModel<string>('imageUrl', {
|
const imageUrl = defineModel<string>('imageUrl', {
|
||||||
required: false,
|
required: false,
|
||||||
default: '',
|
default: '',
|
||||||
|
|
@ -23,17 +27,24 @@ const dialogState = defineModel<boolean>('dialogState', {
|
||||||
const file = defineModel<File | null>('file', {
|
const file = defineModel<File | null>('file', {
|
||||||
required: true,
|
required: true,
|
||||||
});
|
});
|
||||||
defineEmits<{
|
|
||||||
(e: 'save', file: File | null, url: string | null): void;
|
|
||||||
}>();
|
|
||||||
|
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
const inputFile = ref<HTMLInputElement>();
|
const inputFile = (() => {
|
||||||
|
const _element = document.createElement('input');
|
||||||
|
_element.type = 'file';
|
||||||
|
_element.accept = 'image/*';
|
||||||
|
_element.addEventListener('change', change);
|
||||||
|
return _element;
|
||||||
|
})();
|
||||||
|
|
||||||
reader.addEventListener('load', () => {
|
reader.addEventListener('load', () => {
|
||||||
if (typeof reader.result === 'string') imageUrl.value = reader.result;
|
if (typeof reader.result === 'string') imageUrl.value = reader.result;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function browse() {
|
||||||
|
inputFile?.click();
|
||||||
|
}
|
||||||
|
|
||||||
function change(e: Event) {
|
function change(e: Event) {
|
||||||
const _element = e.target as HTMLInputElement | null;
|
const _element = e.target as HTMLInputElement | null;
|
||||||
const _file = _element?.files?.[0];
|
const _file = _element?.files?.[0];
|
||||||
|
|
@ -45,6 +56,7 @@ function change(e: Event) {
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<template>
|
<template>
|
||||||
|
<input type="file" accept="image/*" ref="inputFile" @change="change" hidden />
|
||||||
<q-dialog v-model="dialogState">
|
<q-dialog v-model="dialogState">
|
||||||
<AppBox class="image-dialog-content">
|
<AppBox class="image-dialog-content">
|
||||||
<div class="row items-center q-py-sm q-px-md bordered-b">
|
<div class="row items-center q-py-sm q-px-md bordered-b">
|
||||||
|
|
@ -62,13 +74,6 @@ function change(e: Event) {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="image-dialog-body">
|
<div class="image-dialog-body">
|
||||||
<input
|
|
||||||
type="file"
|
|
||||||
accept="image/*"
|
|
||||||
ref="inputFile"
|
|
||||||
@change="change"
|
|
||||||
hidden
|
|
||||||
/>
|
|
||||||
<img
|
<img
|
||||||
:src="imageUrl || fallbackUrl"
|
:src="imageUrl || fallbackUrl"
|
||||||
v-if="imageUrl || fallbackUrl"
|
v-if="imageUrl || fallbackUrl"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue