Skip to content

Commit 4f0811b

Browse files
committed
Pasting image directly from clipboard
1 parent 23b6c39 commit 4f0811b

2 files changed

Lines changed: 44 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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import useShowCommands from '../../hooks/useShowCommands';
3434
import useSearchMentionUser from '../../hooks/useSearchMentionUser';
3535
import formatSelection from '../../lib/formatSelection';
3636
import { parseEmoji } from '../../lib/emoji';
37+
import useDropBox from '../../hooks/useDropBox';
3738

3839
const ChatInput = ({ scrollToBottom }) => {
3940
const { styleOverrides, classNames } = useComponentOverrides('ChatInput');
@@ -141,6 +142,8 @@ const ChatInput = ({ scrollToBottom }) => {
141142
setShowMembersList
142143
);
143144

145+
const { handlePaste } = useDropBox();
146+
144147
useEffect(() => {
145148
RCInstance.auth.onAuthChange((user) => {
146149
if (user) {
@@ -417,6 +420,40 @@ const ChatInput = ({ scrollToBottom }) => {
417420
}
418421
};
419422

423+
const handlePasting = (event) => {
424+
const { clipboardData } = event;
425+
426+
if (!clipboardData) {
427+
return;
428+
}
429+
430+
const items = Array.from(clipboardData.items);
431+
if (
432+
items.some(({ kind, type }) => kind === 'string' && type === 'text/plain')
433+
) {
434+
return;
435+
}
436+
437+
const files = items
438+
.filter(
439+
(item) => item.kind === 'file' && item.type.indexOf('image/') !== -1
440+
)
441+
.map((item) => {
442+
const fileItem = item.getAsFile();
443+
444+
if (!fileItem) {
445+
return;
446+
}
447+
return fileItem;
448+
})
449+
.filter((file) => !!file);
450+
451+
if (files.length) {
452+
event.preventDefault();
453+
handlePaste(files[0]);
454+
}
455+
};
456+
420457
const onKeyDown = (e) => {
421458
switch (true) {
422459
case e.ctrlKey && e.code === 'KeyI': {
@@ -608,6 +645,7 @@ const ChatInput = ({ scrollToBottom }) => {
608645
}}
609646
onFocus={handleFocus}
610647
onKeyDown={onKeyDown}
648+
onPaste={handlePasting}
611649
ref={messageRef}
612650
/>
613651

0 commit comments

Comments
 (0)