Skip to content

Commit

Permalink
feat: listen document paste event
Browse files Browse the repository at this point in the history
  • Loading branch information
madocto committed Jan 20, 2024
1 parent acde511 commit bc718b0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/AjaxUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class AjaxUploader extends Component<UploadProps> {

private fileInput: HTMLInputElement;

private isMouseEnter: boolean;

private _isMounted: boolean;

onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -66,9 +68,7 @@ class AjaxUploader extends Component<UploadProps> {
}
};

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

if (e.type === 'dragover') {
Expand All @@ -84,7 +84,7 @@ class AjaxUploader extends Component<UploadProps> {
items = [...(dataTransfer.items || [])];
files = [...(dataTransfer.files || [])];
} else if (e.type === 'paste') {
const clipboardData = (e as React.ClipboardEvent<HTMLDivElement>).clipboardData;
const clipboardData = (e as ClipboardEvent).clipboardData;
items = [...(clipboardData.items || [])];
files = [...(clipboardData.files || [])];
}
Expand All @@ -104,13 +104,21 @@ class AjaxUploader extends Component<UploadProps> {
}
};

onPrePaste(e: ClipboardEvent) {
if (this.isMouseEnter) {
this.onFileDropOrPaste(e);
}
}

componentDidMount() {
this._isMounted = true;
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 @@ -271,6 +279,18 @@ class AjaxUploader extends Component<UploadProps> {
this.fileInput = node;
};

handleMouseEnter = (e: React.MouseEvent<HTMLDivElement>) => {
this.isMouseEnter = true;

this.props.onMouseEnter?.(e);
};

handleMouseLeave = (e: React.MouseEvent<HTMLDivElement>) => {
this.isMouseEnter = false;

this.props.onMouseLeave?.(e);
};

render() {
const {
component: Tag,
Expand All @@ -287,8 +307,6 @@ class AjaxUploader extends Component<UploadProps> {
children,
directory,
openFileDialogOnClick,
onMouseEnter,
onMouseLeave,
hasControlInside,
...otherProps
} = this.props;
Expand All @@ -306,11 +324,10 @@ class AjaxUploader extends Component<UploadProps> {
: {
onClick: openFileDialogOnClick ? this.onClick : () => {},
onKeyDown: openFileDialogOnClick ? this.onKeyDown : () => {},
onMouseEnter,
onMouseLeave,
onMouseEnter: this.handleMouseEnter,
onMouseLeave: this.handleMouseLeave,
onDrop: this.onFileDropOrPaste,
onDragOver: this.onFileDropOrPaste,
onPaste: this.onFileDropOrPaste,
tabIndex: hasControlInside ? undefined : '0',
};
return (
Expand Down
4 changes: 4 additions & 0 deletions tests/uploader.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ describe('uploader', () => {
});

it('paste to upload', done => {
const rcUpload = uploader.container.querySelector('.rc-upload')!;
const input = uploader.container.querySelector('input')!;

const files = [
Expand All @@ -329,6 +330,7 @@ describe('uploader', () => {
done(err);
};

fireEvent.mouseEnter(rcUpload);
fireEvent.paste(input, {
clipboardData: { files },
});
Expand Down Expand Up @@ -363,6 +365,7 @@ describe('uploader', () => {

it('paste files with multiple false', done => {
const { container } = render(<Upload {...props} multiple={false} />);
const rcUpload = container.querySelector('.rc-upload')!;
const input = container.querySelector('input')!;
const files = [
new File([''], 'success.png', { type: 'image/png' }),
Expand Down Expand Up @@ -395,6 +398,7 @@ describe('uploader', () => {
value: files,
});

fireEvent.mouseEnter(rcUpload);
fireEvent.paste(input, { clipboardData: { files } });

setTimeout(() => {
Expand Down

0 comments on commit bc718b0

Please sign in to comment.