mirror of
https://gitee.com/dapppp/ruoyi-plus-vben5.git
synced 2026-03-22 12:58:59 +08:00
refactor(upload): 优化图片预览逻辑,移除未使用的标题状态
重构图片预览功能,移除未使用的previewTitle状态,拆分handleCancel为handleOpenChange和handleAfterOpenChange以更精确控制预览状态
This commit is contained in:
@@ -44,12 +44,9 @@ export function useImagePreview() {
|
|||||||
const previewVisible = ref(false);
|
const previewVisible = ref(false);
|
||||||
// 预览的图片 url/base64
|
// 预览的图片 url/base64
|
||||||
const previewImage = ref('');
|
const previewImage = ref('');
|
||||||
// 预览的图片名称
|
|
||||||
const previewTitle = ref('');
|
|
||||||
|
|
||||||
function handleCancel() {
|
function handleOpenChange(isOpen: boolean) {
|
||||||
previewVisible.value = false;
|
previewVisible.value = isOpen;
|
||||||
previewTitle.value = '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handlePreview(file: UploadFile) {
|
async function handlePreview(file: UploadFile) {
|
||||||
@@ -64,16 +61,21 @@ export function useImagePreview() {
|
|||||||
const url = file.url ?? '';
|
const url = file.url ?? '';
|
||||||
previewImage.value = url || file.preview || '';
|
previewImage.value = url || file.preview || '';
|
||||||
previewVisible.value = true;
|
previewVisible.value = true;
|
||||||
previewTitle.value =
|
}
|
||||||
file.name || url.slice(Math.max(0, url.lastIndexOf('/') + 1));
|
|
||||||
|
function handleAfterOpenChange(open: boolean) {
|
||||||
|
if (!open) {
|
||||||
|
previewVisible.value = false;
|
||||||
|
previewImage.value = '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
previewVisible,
|
previewVisible,
|
||||||
previewImage,
|
previewImage,
|
||||||
previewTitle,
|
handleOpenChange,
|
||||||
handleCancel,
|
|
||||||
handlePreview,
|
handlePreview,
|
||||||
|
handleAfterOpenChange,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import type { BaseUploadProps, UploadEmits } from './props';
|
|||||||
import { $t, I18nT } from '@vben/locales';
|
import { $t, I18nT } from '@vben/locales';
|
||||||
|
|
||||||
import { PlusOutlined, UploadOutlined } from '@ant-design/icons-vue';
|
import { PlusOutlined, UploadOutlined } from '@ant-design/icons-vue';
|
||||||
import { Image, ImagePreviewGroup, Upload } from 'antdv-next';
|
import { Image, Upload } from 'antdv-next';
|
||||||
import { isFunction } from 'lodash-es';
|
import { isFunction } from 'lodash-es';
|
||||||
|
|
||||||
import { uploadApi } from '#/api';
|
import { uploadApi } from '#/api';
|
||||||
@@ -65,8 +65,13 @@ const {
|
|||||||
customRequest,
|
customRequest,
|
||||||
} = useUpload(props, emit, ossIdList, 'image');
|
} = useUpload(props, emit, ossIdList, 'image');
|
||||||
|
|
||||||
const { previewVisible, previewImage, handleCancel, handlePreview } =
|
const {
|
||||||
useImagePreview();
|
previewVisible,
|
||||||
|
previewImage,
|
||||||
|
handleOpenChange,
|
||||||
|
handlePreview,
|
||||||
|
handleAfterOpenChange,
|
||||||
|
} = useImagePreview();
|
||||||
|
|
||||||
function currentPreview(file: UploadFile) {
|
function currentPreview(file: UploadFile) {
|
||||||
// 有自定义预览逻辑走自定义
|
// 有自定义预览逻辑走自定义
|
||||||
@@ -141,14 +146,16 @@ function currentPreview(file: UploadFile) {
|
|||||||
</I18nT>
|
</I18nT>
|
||||||
</slot>
|
</slot>
|
||||||
|
|
||||||
<ImagePreviewGroup
|
<Image
|
||||||
|
v-if="previewImage"
|
||||||
|
class="hidden"
|
||||||
|
:src="previewImage"
|
||||||
:preview="{
|
:preview="{
|
||||||
visible: previewVisible,
|
open: previewVisible,
|
||||||
onVisibleChange: handleCancel,
|
onOpenChange: handleOpenChange,
|
||||||
|
afterOpenChange: handleAfterOpenChange,
|
||||||
}"
|
}"
|
||||||
>
|
/>
|
||||||
<Image class="hidden" :src="previewImage" />
|
|
||||||
</ImagePreviewGroup>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user