yanzhen-design-vue
Version:
168 lines (167 loc) • 4.71 kB
JavaScript
import { ref, computed, unref, reactive, nextTick } from "vue";
import { omit, isFunction } from "lodash-es";
import { isEmptyObject, mergeWith, isEmptyArray } from "@yanzhen-libs/utils";
import { useModelValue } from "@yanzhen-libs/utils-vue";
import { useFileListConfig } from "./hooks/use-file-list-config.mjs";
import { usePreviewConfig } from "./hooks/use-preview-config.mjs";
import { provideUploadApi } from "./hooks/use-upload-apis.mjs";
import { useUploadImagePreview } from "./use-upload-image-preview.mjs";
function useUploadFilePreview(props, emit) {
const _ref = ref(null);
const { getFileListInfo, downloadFile, previewFile } = provideUploadApi(props);
const loading = ref(false);
const { fileListRef, fileUrls, fileListMap, isImageUrl } = useFileListConfig(props, emit);
const { previewConfig } = usePreviewConfig(props);
const { imageListRef, handleOpenPreviewImage } = useUploadImagePreview(props, emit);
const useDrawer = computed(() => {
switch (true) {
case props.drawerConfig === true:
case !isEmptyObject(props.drawerConfig):
return true;
}
return false;
});
const propsRef = computed(() => {
return omit(props, "drawerConfig");
});
const visible = useModelValue(props, "visible", emit);
const drawerConfigRef = computed(() => {
const drawerConfig = isEmptyObject(props.drawerConfig) ? {} : props.drawerConfig;
const { onAfterOpenChange, onClose } = drawerConfig;
const config = {
visible: unref(visible),
onAfterOpenChange(open) {
try {
if (isFunction(onAfterOpenChange)) {
onAfterOpenChange(open);
}
} catch {
}
if (open) {
handlePreviewFileList().then((r) => r);
}
},
onClose(e) {
if (isFunction(onClose)) {
onClose(e);
}
handleClosePreviewFile(e);
}
};
return mergeWith(
{
title: props.title,
width: props.width
},
drawerConfig,
config
);
});
async function openPreviewFile(file) {
loading.value = true;
const result = await previewFile({
data: file
});
loading.value = false;
if ((result == null ? void 0 : result.code) !== 0) {
emit == null ? void 0 : emit("preview", file);
}
}
function handlePreviewFile(file) {
if (unref(previewConfig).preview) {
if (file && isImageUrl(file)) {
handleOpenPreviewImage(file);
return;
}
try {
if (file == null ? void 0 : file.url) {
openPreviewFile(file).then((r) => r);
return;
}
} catch {
}
loading.value = false;
}
emit == null ? void 0 : emit("preview", file);
}
function handleClosePreviewFile(e) {
visible.value = false;
emit == null ? void 0 : emit("close", e);
}
const ctx = {
open: handleOpenPreviewFile,
close: handleClosePreviewFile,
previewFile: handlePreviewFile
};
const exposeCtx = reactive(ctx);
async function handlePreviewFileList() {
var _a;
if (unref(loading)) return;
const fileIdList = unref(fileUrls);
if (isEmptyArray(fileIdList)) return;
loading.value = true;
try {
const result = await getFileListInfo({
data: {
fileId: "",
fileIdList
}
});
if ((result == null ? void 0 : result.code) === 0) {
const fileDetailList = (_a = result == null ? void 0 : result.data) == null ? void 0 : _a.fileDetailList;
if (!isEmptyArray(fileDetailList)) {
for (const file of fileDetailList) {
if (file.fileId) {
const fileInfo = fileListMap[file.fileId];
fileInfo.originInfo = file;
}
}
}
}
} catch {
}
await nextTick();
loading.value = false;
}
function handleOpenPreviewFile(file) {
visible.value = true;
if (file && isImageUrl(file)) {
setTimeout(() => {
handlePreviewFile(file);
}, 500);
}
}
async function handleDownloadFile(file) {
try {
loading.value = true;
const result = await downloadFile({
data: file
});
loading.value = false;
if ((result == null ? void 0 : result.code) === 0) {
return;
}
} catch {
}
loading.value = false;
emit == null ? void 0 : emit("download", file);
}
return {
_ref,
useDrawer,
propsRef,
visible,
drawerConfigRef,
fileListRef,
imageListRef,
exposeCtx,
handlePreviewFile,
handleDownloadFile,
handleOpenPreviewFile,
handleClosePreviewFile,
loading
};
}
export {
useUploadFilePreview
};