Skip to content
Open
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
38 changes: 38 additions & 0 deletions packages/react/src/views/ChatInput/ChatInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import useShowCommands from '../../hooks/useShowCommands';
import useSearchMentionUser from '../../hooks/useSearchMentionUser';
import formatSelection from '../../lib/formatSelection';
import { parseEmoji } from '../../lib/emoji';
import useDropBox from '../../hooks/useDropBox';

const ChatInput = ({ scrollToBottom }) => {
const { styleOverrides, classNames } = useComponentOverrides('ChatInput');
Expand Down Expand Up @@ -141,6 +142,8 @@ const ChatInput = ({ scrollToBottom }) => {
setShowMembersList
);

const { handlePaste } = useDropBox();

useEffect(() => {
RCInstance.auth.onAuthChange((user) => {
if (user) {
Expand Down Expand Up @@ -417,6 +420,40 @@ const ChatInput = ({ scrollToBottom }) => {
}
};

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

if (!clipboardData) {
return;
}

const items = Array.from(clipboardData.items);
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 @@ -608,6 +645,7 @@ const ChatInput = ({ scrollToBottom }) => {
}}
onFocus={handleFocus}
onKeyDown={onKeyDown}
onPaste={handlePasting}
ref={messageRef}
/>

Expand Down