-
Notifications
You must be signed in to change notification settings - Fork 138
/
useUploadControl.ts
56 lines (44 loc) 路 1.41 KB
/
useUploadControl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* @license
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/IDuxFE/idux/blob/main/LICENSE
*/
import type { UploadRequest } from './useRequest'
import type { UploadFile } from '../types'
import { type ComputedRef, watch } from 'vue'
import { useState } from '@idux/cdk/utils'
import { getTargetFile } from '../utils/files'
export function useUploadControl(fileList: ComputedRef<UploadFile[]>, uploadRequest: UploadRequest): void {
const { startUpload, abort } = uploadRequest
const [proxyedFileList, setProxyedFileList] = useState<UploadFile[]>([...fileList.value])
watch(
fileList,
_fileList => {
setProxyedFileList([..._fileList])
},
{
deep: true,
},
)
watch(
[proxyedFileList, () => proxyedFileList.value.length],
([currentFileList, currentLength], [preFileList, preLength]) => {
if (currentLength === preLength && currentLength !== 1 && preLength !== 1) {
return
}
currentFileList.forEach(file => {
const preFile = getTargetFile(file, preFileList)
if (!preFile && file.status === 'selected') {
startUpload(file)
}
})
preFileList.forEach(file => {
const currentFile = getTargetFile(file, currentFileList)
if (!currentFile && file.status === 'uploading') {
abort(file)
}
})
},
)
}