Skip to content

Commit 31c86d7

Browse files
authored
Pasting image directly from clipboard (#1040)
1 parent d18e390 commit 31c86d7

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

packages/react/src/hooks/useDropBox.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@ const useDropBox = () => {
1515
setData(e.dataTransfer.files[0]);
1616
};
1717

18+
const handlePaste = (file) => {
19+
toggle();
20+
setData(file);
21+
};
22+
1823
return {
1924
data,
2025
handleDrag,
2126
handleDragDrop,
27+
handlePaste,
2228
};
2329
};
2430

packages/react/src/views/ChatInput/ChatInput.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import useSearchMentionUser from '../../hooks/useSearchMentionUser';
3636
import useSearchEmoji from '../../hooks/useSearchEmoji';
3737
import formatSelection from '../../lib/formatSelection';
3838
import { parseEmoji } from '../../lib/emoji';
39+
import useDropBox from '../../hooks/useDropBox';
3940

4041
const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => {
4142
const { styleOverrides, classNames } = useComponentOverrides('ChatInput');
@@ -150,6 +151,7 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => {
150151
setShowMembersList
151152
);
152153

154+
const { handlePaste } = useDropBox();
153155
const searchEmoji = useSearchEmoji(
154156
startReadEmoji,
155157
setStartReadEmoji,
@@ -463,6 +465,40 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => {
463465
}
464466
};
465467

468+
const handlePasting = (event) => {
469+
const { clipboardData } = event;
470+
471+
if (!clipboardData) {
472+
return;
473+
}
474+
475+
const items = Array.from(clipboardData.items);
476+
if (
477+
items.some(({ kind, type }) => kind === 'string' && type === 'text/plain')
478+
) {
479+
return;
480+
}
481+
482+
const files = items
483+
.filter(
484+
(item) => item.kind === 'file' && item.type.indexOf('image/') !== -1
485+
)
486+
.map((item) => {
487+
const fileItem = item.getAsFile();
488+
489+
if (!fileItem) {
490+
return;
491+
}
492+
return fileItem;
493+
})
494+
.filter((file) => !!file);
495+
496+
if (files.length) {
497+
event.preventDefault();
498+
handlePaste(files[0]);
499+
}
500+
};
501+
466502
const onKeyDown = (e) => {
467503
switch (true) {
468504
case e.ctrlKey && e.code === 'KeyI': {
@@ -666,6 +702,7 @@ const ChatInput = ({ scrollToBottom, clearUnreadDividerRef }) => {
666702
}}
667703
onFocus={handleFocus}
668704
onKeyDown={onKeyDown}
705+
onPaste={handlePasting}
669706
ref={messageRef}
670707
/>
671708

0 commit comments

Comments
 (0)