Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ React.render(<Upload />, container);
|customRequest | function | null | provide an override for the default xhr behavior for additional customization|
|withCredentials | boolean | false | ajax upload with cookie send |
|openFileDialogOnClick | boolean | true | useful for drag only upload as it does not trigger on enter key or click event |
|allowPasteUpload | boolean | false | support paste upload |
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个命名不符合 antd 的 name standard,用 pastable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Select 有 allowClear 🐶


#### onError arguments

Expand Down
8 changes: 8 additions & 0 deletions docs/demo/paste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: paste
nav:
title: Demo
path: /demo
---

<code src="../examples/paste.tsx"/></code>
8 changes: 8 additions & 0 deletions docs/demo/pasteDirectory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: pasteDirectory
nav:
title: Demo
path: /demo
---

<code src="../examples/pasteDirectory.tsx"/></code>
43 changes: 43 additions & 0 deletions docs/examples/paste.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint no-console:0 */
import React from 'react';
import Upload from 'rc-upload';

const props = {
action: '/upload.do',
type: 'drag',
accept: '.png',
beforeUpload(file) {
console.log('beforeUpload', file.name);
},
onStart: file => {
console.log('onStart', file.name);
},
onSuccess(file) {
console.log('onSuccess', file);
},
onProgress(step, file) {
console.log('onProgress', Math.round(step.percent), file.name);
},
onError(err) {
console.log('onError', err);
},
style: { display: 'inline-block', width: 200, height: 200, background: '#eee' },
};

const Test = () => {
return (
<div
style={{
margin: 100,
}}
>
<div>
<Upload {...props}>
<a>开始上传</a>
</Upload>
</div>
</div>
);
};

export default Test;
44 changes: 44 additions & 0 deletions docs/examples/pasteDirectory.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint no-console:0 */
import React from 'react';
import Upload from 'rc-upload';

const props = {
action: '/upload.do',
type: 'drag',
accept: '.png',
directory: true,
beforeUpload(file) {
console.log('beforeUpload', file.name);
},
onStart: file => {
console.log('onStart', file.name);
},
onSuccess(file) {
console.log('onSuccess', file);
},
onProgress(step, file) {
console.log('onProgress', Math.round(step.percent), file.name);
},
onError(err) {
console.log('onError', err);
},
style: { display: 'inline-block', width: 200, height: 200, background: '#eee' },
};

const Test = () => {
return (
<div
style={{
margin: 100,
}}
>
<div>
<Upload {...props}>
<a>开始上传</a>
</Upload>
</div>
</div>
);
};

export default Test;
52 changes: 38 additions & 14 deletions src/AjaxUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,41 +66,65 @@ class AjaxUploader extends Component<UploadProps> {
}
};

onFileDrop = async (e: React.DragEvent<HTMLDivElement>) => {
const { multiple } = this.props;

onFileDropOrPaste = async (e: React.DragEvent<HTMLDivElement> | ClipboardEvent) => {
e.preventDefault();

if (e.type === 'dragover') {
return;
}

if (this.props.directory) {
const files = await traverseFileTree(
Array.prototype.slice.call(e.dataTransfer.items),
(_file: RcFile) => attrAccept(_file, this.props.accept),
const { multiple, accept, directory } = this.props;
let items: DataTransferItem[] = [];
let files: File[] = [];

if (e.type === 'drop') {
const dataTransfer = (e as React.DragEvent<HTMLDivElement>).dataTransfer;
items = [...(dataTransfer.items || [])];
files = [...(dataTransfer.files || [])];
} else if (e.type === 'paste') {
const clipboardData = (e as ClipboardEvent).clipboardData;
items = [...(clipboardData.items || [])];
files = [...(clipboardData.files || [])];
}

if (directory) {
files = await traverseFileTree(Array.prototype.slice.call(items), (_file: RcFile) =>
attrAccept(_file, this.props.accept),
);
this.uploadFiles(files);
} else {
let files = [...e.dataTransfer.files].filter((file: RcFile) =>
attrAccept(file, this.props.accept),
);
let acceptFiles = [...files].filter((file: RcFile) => attrAccept(file, accept));

if (multiple === false) {
files = files.slice(0, 1);
acceptFiles = files.slice(0, 1);
}

this.uploadFiles(files);
this.uploadFiles(acceptFiles);
}
};

onPrePaste(e: ClipboardEvent) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onPrePaste(e: ClipboardEvent) {} 改成 onPrePaste = (e: ClipboardEvent) => {}

这样你下面就不需要 this.onPrePaste.bind

const { allowPasteUpload } = this.props;

if (allowPasteUpload) {
this.onFileDropOrPaste(e);
}
}

componentDidMount() {
this._isMounted = true;

const { allowPasteUpload } = this.props;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

动态切换就不行了,componentDidUpdate 也需要确定


if (allowPasteUpload) {
document.addEventListener('paste', this.onPrePaste.bind(this));
}
}

componentWillUnmount() {
this._isMounted = false;
this.abort();
document.removeEventListener('paste', this.onPrePaste.bind(this));
}

uploadFiles = (files: File[]) => {
Expand Down Expand Up @@ -299,8 +323,8 @@ class AjaxUploader extends Component<UploadProps> {
onKeyDown: openFileDialogOnClick ? this.onKeyDown : () => {},
onMouseEnter,
onMouseLeave,
onDrop: this.onFileDrop,
onDragOver: this.onFileDrop,
onDrop: this.onFileDropOrPaste,
onDragOver: this.onFileDropOrPaste,
tabIndex: hasControlInside ? undefined : '0',
};
return (
Expand Down
1 change: 1 addition & 0 deletions src/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface UploadProps
input?: React.CSSProperties;
};
hasControlInside?: boolean;
allowPasteUpload?: boolean;
}

export interface UploadProgressEvent extends Partial<ProgressEvent> {
Expand Down
Loading