wujingjing
2025-03-03 1075860848d14e3d6d1506b91d9c9039433bf4cc
src/components/chat/components/playBar/hook/useUploadFile.ts
@@ -1,9 +1,11 @@
import { useEventListener, useFileDialog } from '@vueuse/core';
import { ref, type Ref } from 'vue';
import { type Ref } from 'vue';
import type { Attach } from './useAttach';
import { convertFileSize } from '/@/utils/file';
export type UseUploadFileOptions = {
   pastTarget: Ref<HTMLElement | null>;
   attachFileList: Ref<Attach<UploadFile>[]>;
};
export type FileType = 'doc' | 'docx' | 'pdf' | 'md' | 'xls' | 'xlsx' | 'png' | 'jpg' | 'jpeg' | 'gif' | 'json' | 'txt' | 'csv';
@@ -93,10 +95,8 @@
};
// const supportFileType = ['doc', 'docx', 'md', 'xls', 'xlsx', 'png', 'jpg', 'jpeg', 'gif', 'json', 'pdf'];
const supportFileType = ['csv', 'txt'];
const acceptFiles = [].join(',')
export const useUploadFile = (options: UseUploadFileOptions) => {
   const attachFileList = ref<UploadFile[]>([]);
   const { attachFileList } = options;
   const parseFiles = (files: FileList) => {
      const filterFiles: UploadFile[] = [];
      for (const file of files) {
@@ -126,7 +126,24 @@
            filterFiles.push(uploadFile);
         }
      }
      attachFileList.value.push(...filterFiles);
      attachFileList.value.push(
         ...filterFiles.map(
            (item) =>
               ({
                  get title() {
                     return item.name;
                  },
                  type: 'file',
                  model: item,
                  get icon() {
                     return item.icon;
                  },
                  get iconClass() {
                     return item.iconClass;
                  },
               } as Attach<UploadFile>)
         )
      );
   };
   /**
@@ -140,17 +157,14 @@
      parseFiles(files);
   };
   const clearFileList = () => {
      attachFileList.value = [];
   };
   const {
      files,
      open: openFileDialog,
      reset: resetOpenFileDialog,
      onChange: onPickFileChange,
   } = useFileDialog({
      accept:
         'text/csv,text/plain', // Only accept csv and txt files
      accept: 'text/csv,text/plain', // Only accept csv and txt files
      reset: true,
   });
@@ -161,21 +175,17 @@
      if (!files) return;
      parseFiles(files);
   });
   const deleteIndexFile = (index) => {
      const file = attachFileList.value[index];
      // Revoke object URL for image files
   const deleteUploadFile = (file: UploadFile) => {
      if (file.previewUrl) {
         URL.revokeObjectURL(file.previewUrl);
      }
      attachFileList.value.splice(index, 1);
   };
   useEventListener(options.pastTarget, 'paste', pasteUpload);
   return {
      attachFileList,
      clearFileList,
      deleteIndexFile,
      deleteUploadFile,
      openFileClick,
   };
};