UNPKG

yanzhen-design-vue

Version:

168 lines (167 loc) 4.91 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const vue = require("vue"); const lodashEs = require("lodash-es"); const utils = require("@yanzhen-libs/utils"); const utilsVue = require("@yanzhen-libs/utils-vue"); const useFileListConfig = require("./hooks/use-file-list-config.cjs"); const usePreviewConfig = require("./hooks/use-preview-config.cjs"); const useUploadApis = require("./hooks/use-upload-apis.cjs"); const useUploadImagePreview = require("./use-upload-image-preview.cjs"); function useUploadFilePreview(props, emit) { const _ref = vue.ref(null); const { getFileListInfo, downloadFile, previewFile } = useUploadApis.provideUploadApi(props); const loading = vue.ref(false); const { fileListRef, fileUrls, fileListMap, isImageUrl } = useFileListConfig.useFileListConfig(props, emit); const { previewConfig } = usePreviewConfig.usePreviewConfig(props); const { imageListRef, handleOpenPreviewImage } = useUploadImagePreview.useUploadImagePreview(props, emit); const useDrawer = vue.computed(() => { switch (true) { case props.drawerConfig === true: case !utils.isEmptyObject(props.drawerConfig): return true; } return false; }); const propsRef = vue.computed(() => { return lodashEs.omit(props, "drawerConfig"); }); const visible = utilsVue.useModelValue(props, "visible", emit); const drawerConfigRef = vue.computed(() => { const drawerConfig = utils.isEmptyObject(props.drawerConfig) ? {} : props.drawerConfig; const { onAfterOpenChange, onClose } = drawerConfig; const config = { visible: vue.unref(visible), onAfterOpenChange(open) { try { if (lodashEs.isFunction(onAfterOpenChange)) { onAfterOpenChange(open); } } catch { } if (open) { handlePreviewFileList().then((r) => r); } }, onClose(e) { if (lodashEs.isFunction(onClose)) { onClose(e); } handleClosePreviewFile(e); } }; return utils.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 (vue.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 = vue.reactive(ctx); async function handlePreviewFileList() { var _a; if (vue.unref(loading)) return; const fileIdList = vue.unref(fileUrls); if (utils.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 (!utils.isEmptyArray(fileDetailList)) { for (const file of fileDetailList) { if (file.fileId) { const fileInfo = fileListMap[file.fileId]; fileInfo.originInfo = file; } } } } } catch { } await vue.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 }; } exports.useUploadFilePreview = useUploadFilePreview;