Skip to content

Commit 7d3983b

Browse files
committed
feat(#3151): 为 taro 版本的 uploader 增加粘贴上传的演示。
1 parent 1de630c commit 7d3983b

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

src/packages/uploader/demo.taro.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Demo11 from './demos/taro/demo11'
1818
import Demo12 from './demos/taro/demo12'
1919
import Demo13 from './demos/taro/demo13'
2020
import Demo14 from './demos/taro/demo14'
21+
import Demo15 from './demos/taro/demo15'
2122

2223
const UploaderDemo = () => {
2324
const [translated] = useTranslate({
@@ -35,6 +36,7 @@ const UploaderDemo = () => {
3536
manualExecution: '选中文件后,通过按钮手动执行上传',
3637
disabled: '禁用状态',
3738
customDeleteIcon: '自定义删除icon',
39+
enablePasteUpload: '启用粘贴上传',
3840
},
3941
'zh-TW': {
4042
basic: '基础用法',
@@ -50,6 +52,7 @@ const UploaderDemo = () => {
5052
manualExecution: '選取檔後,通過按鈕手動執行上傳',
5153
disabled: '禁用狀態',
5254
customDeleteIcon: '自定義刪除icon',
55+
enablePasteUpload: '啟用粘貼上傳',
5356
},
5457
'en-US': {
5558
basic: 'Basic usage',
@@ -67,6 +70,7 @@ const UploaderDemo = () => {
6770
'After selecting Chinese, manually perform the upload via the button',
6871
disabled: 'Disabled state',
6972
customDeleteIcon: 'Custom DeleteIcon',
73+
enablePasteUpload: 'Enable paste upload',
7074
},
7175
})
7276

@@ -102,6 +106,8 @@ const UploaderDemo = () => {
102106
<Demo13 />
103107
<h2>{translated.customDeleteIcon}</h2>
104108
<Demo14 />
109+
<h2>{translated.enablePasteUpload}</h2>
110+
<Demo15 />
105111
</div>
106112
</>
107113
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react'
2+
import { Uploader } from '@nutui/nutui-react-taro'
3+
4+
const Demo15 = () => {
5+
return (
6+
<>
7+
<Uploader
8+
url="https://my-json-server.typicode.com/linrufeng/demo/posts"
9+
enablePasteUpload
10+
uploadLabel="点击上传或粘贴图片"
11+
/>
12+
</>
13+
)
14+
}
15+
export default Demo15

src/packages/uploader/uploader.taro.tsx

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import React, {
55
PropsWithChildren,
66
useRef,
77
useEffect,
8+
useCallback,
89
} from 'react'
910
import classNames from 'classnames'
1011
import Taro, {
@@ -119,6 +120,7 @@ export interface UploaderProps extends BasicComponent {
119120
beforeXhrUpload?: (xhr: XMLHttpRequest, options: any) => void
120121
beforeDelete?: (file: FileItem, files: FileItem[]) => boolean
121122
onFileItemClick?: (file: FileItem, index: number) => void
123+
enablePasteUpload?: boolean
122124
}
123125

124126
const defaultProps = {
@@ -152,6 +154,7 @@ const defaultProps = {
152154
beforeDelete: (file: FileItem, files: FileItem[]) => {
153155
return true
154156
},
157+
enablePasteUpload: false,
155158
} as UploaderProps
156159

157160
const InternalUploader: ForwardRefRenderFunction<
@@ -202,6 +205,7 @@ const InternalUploader: ForwardRefRenderFunction<
202205
beforeUpload,
203206
beforeXhrUpload,
204207
beforeDelete,
208+
enablePasteUpload,
205209
...restProps
206210
} = { ...defaultProps, ...props }
207211
const [fileList, setFileList] = usePropsValue({
@@ -439,7 +443,7 @@ const InternalUploader: ForwardRefRenderFunction<
439443
setFileList([...fileList, ...results])
440444
}
441445

442-
reader.readAsDataURL(file as unknown as Blob)
446+
reader.readAsDataURL(file.originalFileObj ?? (file as unknown as Blob))
443447
} else {
444448
executeUpload(fileItem, index)
445449
results.push(fileItem)
@@ -520,6 +524,62 @@ const InternalUploader: ForwardRefRenderFunction<
520524
onFileItemClick?.(file, index)
521525
}
522526

527+
const handlePaste = useCallback(
528+
(event: ClipboardEvent) => {
529+
if (!enablePasteUpload || disabled) return
530+
531+
const clipboardData = event.clipboardData
532+
if (!clipboardData) return
533+
534+
const files: TFileType[] = []
535+
536+
if (clipboardData?.items && clipboardData.items.length) {
537+
for (let i = 0; i < clipboardData.items.length; i++) {
538+
const item = clipboardData.items[i]
539+
if (item.kind === 'file' && item.type.startsWith('image/')) {
540+
const file = item.getAsFile()
541+
if (file) {
542+
files.push({
543+
originalFileObj: file,
544+
size: file.size,
545+
path: '',
546+
tempFilePath: '',
547+
type: file.type,
548+
fileType: file.type,
549+
})
550+
}
551+
}
552+
}
553+
} else if (clipboardData?.files && clipboardData.files.length) {
554+
for (let i = 0; i < clipboardData.files.length; i++) {
555+
const file = clipboardData.files[i]
556+
if (file.type.startsWith('image/')) {
557+
files.push(file)
558+
}
559+
}
560+
}
561+
562+
if (files.length) {
563+
readFile(files)
564+
}
565+
},
566+
[enablePasteUpload, disabled, beforeUpload, filterFiles, readFile, onChange]
567+
)
568+
569+
useEffect(() => {
570+
fileListRef.current = fileList
571+
572+
if (enablePasteUpload) {
573+
document.addEventListener('paste', handlePaste)
574+
}
575+
576+
return () => {
577+
if (enablePasteUpload) {
578+
document.removeEventListener('paste', handlePaste)
579+
}
580+
}
581+
}, [fileList, enablePasteUpload, handlePaste])
582+
523583
return (
524584
<div className={classes} {...restProps}>
525585
{(children || previewType === 'list') && (

0 commit comments

Comments
 (0)