浏览代码

fix(uploader): before-delete support Promise #1672

richard1015 3 年之前
父节点
当前提交
f792112b38

+ 1 - 1
src/packages/__VUE/uploader/doc.en-US.md

@@ -389,7 +389,7 @@ export default {
 | timeout           | timeout, in milliseconds                                                                                                                         | Number丨String                    | 1000 * 30        |
 | before-upload     | Hook before reading the file, return false to stop reading the file, can return Promise                                                          | Function                          | null             |
 | before-xhr-upload`v3.2.1` | Customize the method when uploading XHR                                                                                                                                                                          | Function(xhr,option)                          | null             |
-| before-delete     | Hook before delete the file, return false to stop reading the file, can return Promise                                                           | Function(file): boolean 丨Promise | -                |
+| before-delete     | Hook before delete the file, return false to stop reading the file, can return Promise                                                           | Function(file,fileList): boolean 丨Promise | -                |
 
 
 

+ 1 - 1
src/packages/__VUE/uploader/doc.md

@@ -389,7 +389,7 @@ export default {
 | timeout                   | 超时时间,单位为毫秒                                                                                                                                                                   | Number丨String                    | 1000 * 30        |
 | before-upload             | 上传前的函数需要返回一个`Promise`对象                                                                                                                                                  | Function                          | null             |
 | before-xhr-upload`v3.2.1` | 执行 XHR 上传时,自定义方式                                                                                                                                                            | Function(xhr,option)             | null             |
-| before-delete             | 除文件时的回调,返回值为 false 时不移除。支持返回一个 `Promise` 对象,`Promise` 对象 resolve(false) 或 reject 时不移除                                                                 | Function(file): boolean 丨Promise | -                |
+| before-delete             | 除文件时的回调,返回值为 false 时不移除。支持返回一个 `Promise` 对象,`Promise` 对象 resolve(false) 或 reject 时不移除                                                                 | Function(file,fileList): boolean 丨Promise | -                |
 
 
 

+ 1 - 1
src/packages/__VUE/uploader/doc.taro.md

@@ -252,7 +252,7 @@ setup() {
 | timeout                   | 超时时间,单位为毫秒                                                                                                   | Number丨String                    | 1000 * 30                 |
 | before-upload             | 上传前的函数需要返回一个`Promise`对象                                                                                  | Function                          | null                      |
 | before-xhr-upload`v3.2.1` | 执行 Taro.uploadFile 上传时,自定义方式                                                                                | Function(Taro.uploadFile,option) | null                      |
-| before-delete             | 除文件时的回调,返回值为 false 时不移除。支持返回一个 `Promise` 对象,`Promise` 对象 resolve(false) 或 reject 时不移除 | Function(file): boolean 丨Promise | -                         |
+| before-delete             | 除文件时的回调,返回值为 false 时不移除。支持返回一个 `Promise` 对象,`Promise` 对象 resolve(false) 或 reject 时不移除 | Function(file,fileList): boolean 丨Promise | -                         |
 
 
 

+ 22 - 7
src/packages/__VUE/uploader/index.taro.vue

@@ -68,6 +68,7 @@ import { Uploader, UploadOptions } from './uploader';
 import { FileItem } from './type';
 const { componentName, create, translate } = createComponent('uploader');
 import Taro from '@tarojs/taro';
+import { isPromise } from '@/packages/utils/util';
 export default create({
   props: {
     name: { type: String, default: 'file' },
@@ -283,17 +284,31 @@ export default create({
       }
       return files;
     };
+
+    const deleted = (file: import('./type').FileItem, index: number) => {
+      fileList.splice(index, 1);
+      emit('delete', {
+        file,
+        fileList,
+        index
+      });
+    };
+
     const onDelete = (file: import('./type').FileItem, index: number) => {
       clearUploadQueue(index);
-      if (props.beforeDelete(file, fileList)) {
-        fileList.splice(index, 1);
-        emit('delete', {
-          file,
-          fileList,
-          index
+      let fn = props.beforeDelete(file, fileList);
+      if (isPromise(fn)) {
+        fn.then((res) => {
+          if (res) {
+            deleted(file, index);
+          }
+        }).catch((error) => {
+          console.log(error, '用户阻止了删除!');
         });
+      } else if (fn) {
+        deleted(file, index);
       } else {
-        // console.log('用户阻止了删除!');
+        console.log('用户阻止了删除!');
       }
     };
 

+ 22 - 7
src/packages/__VUE/uploader/index.vue

@@ -106,6 +106,7 @@ import { computed, reactive } from 'vue';
 import { createComponent } from '@/packages/utils/create';
 import { Uploader, UploadOptions } from './uploader';
 import { FileItem } from './type';
+import { isPromise } from '@/packages/utils/util';
 const { componentName, create, translate } = createComponent('uploader');
 export default create({
   props: {
@@ -300,17 +301,31 @@ export default create({
       }
       return files;
     };
+
+    const deleted = (file: import('./type').FileItem, index: number) => {
+      fileList.splice(index, 1);
+      emit('delete', {
+        file,
+        fileList,
+        index
+      });
+    };
+
     const onDelete = (file: import('./type').FileItem, index: number) => {
       clearUploadQueue(index);
-      if (props.beforeDelete(file, fileList)) {
-        fileList.splice(index, 1);
-        emit('delete', {
-          file,
-          fileList,
-          index
+      let fn = props.beforeDelete(file, fileList);
+      if (isPromise(fn)) {
+        fn.then((res) => {
+          if (res) {
+            deleted(file, index);
+          }
+        }).catch((error) => {
+          console.log(error, '用户阻止了删除!');
         });
+      } else if (fn) {
+        deleted(file, index);
       } else {
-        // console.log('用户阻止了删除!');
+        console.log('用户阻止了删除!');
       }
     };