Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions packages/react/src/hooks/useDropBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ const useDropBox = () => {
setData(e.dataTransfer.files[0]);
};

const handlePaste = (file) => {
toggle();
setData(file);
};

return {
data,
handleDrag,
handleDragDrop,
handlePaste,
};
};

Expand Down
37 changes: 37 additions & 0 deletions packages/react/src/views/ChatInput/ChatInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import useSearchMentionUser from '../../hooks/useSearchMentionUser';
import useSearchEmoji from '../../hooks/useSearchEmoji';
import formatSelection from '../../lib/formatSelection';
import { parseEmoji } from '../../lib/emoji';
import useDropBox from '../../hooks/useDropBox';

const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => {
const { styleOverrides, classNames } = useComponentOverrides('ChatInput');
Expand Down Expand Up @@ -150,6 +151,7 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => {
setShowMembersList
);

const { handlePaste } = useDropBox();
const searchEmoji = useSearchEmoji(
startReadEmoji,
setStartReadEmoji,
Expand Down Expand Up @@ -463,6 +465,40 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => {
}
};

const handlePasting = (event) => {
const { clipboardData } = event;

if (!clipboardData) {
return;
}

const items = Array.from(clipboardData.items);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks good, isn't there a well known and lightweight library to handle this, as we may miss some cases

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can take it as part of separate PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure will look into it as a part of another PR.

Thank you for taking your time to review the PR :)

if (
items.some(({ kind, type }) => kind === 'string' && type === 'text/plain')
) {
return;
}

const files = items
.filter(
(item) => item.kind === 'file' && item.type.indexOf('image/') !== -1
)
.map((item) => {
const fileItem = item.getAsFile();

if (!fileItem) {
return;
}
return fileItem;
})
.filter((file) => !!file);

if (files.length) {
event.preventDefault();
handlePaste(files[0]);
}
};

const onKeyDown = (e) => {
switch (true) {
case e.ctrlKey && e.code === 'KeyI': {
Expand Down Expand Up @@ -666,6 +702,7 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => {
}}
onFocus={handleFocus}
onKeyDown={onKeyDown}
onPaste={handlePasting}
ref={messageRef}
/>

Expand Down
Loading