Skip to content

Commit

Permalink
chat input: enable ctrl/cmd+v paste images
Browse files Browse the repository at this point in the history
  • Loading branch information
roienatan committed Oct 10, 2023
1 parent b920331 commit b627773
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/pages/common/components/ChatComponent/ChatComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export default function ChatComponent({
const currentFilesPreview = useSelector(selectFilesPreview());
const chatContentRef = useRef<ChatContentRef>(null);
const chatWrapperId = useMemo(() => `chat-wrapper-${uuidv4()}`, []);
const chatInputWrapperRef = useRef<HTMLElement>(null);

const [message, setMessage] = useState<TextEditorValue>(
parseStringToTextEditorValue(),
Expand Down Expand Up @@ -323,7 +324,14 @@ export default function ChatComponent({
);

const uploadFiles = (event: ChangeEvent<HTMLInputElement>) => {
const newFilesPreview = Array.from(event.target.files || []).map((file) => {
let clipboardfiles: FileList | undefined;
if ((event as any).clipboardData.files.length) {
clipboardfiles = (event as any).clipboardData.files;
}

const newFilesPreview = Array.from(
event.target.files || clipboardfiles || [],
).map((file) => {
return {
info: file,
src: URL.createObjectURL(file),
Expand Down Expand Up @@ -583,6 +591,20 @@ export default function ChatComponent({
return null;
}

useEffect(() => {
const handlePaste = (event) => {
if (event.clipboardData.files.length) {
uploadFiles(event);
}
};

chatInputWrapperRef.current?.addEventListener("paste", handlePaste);

return () => {
chatInputWrapperRef.current?.removeEventListener("paste", handlePaste);
};
}, []);

return (
<>
<ButtonIcon
Expand Down Expand Up @@ -674,6 +696,7 @@ export default function ChatComponent({
<MessageReply users={users} />
<ChatFilePreview />
<div
ref={chatInputWrapperRef as any}
className={classNames(styles.chatInputWrapper, {
[styles.chatInputWrapperMultiLine]: isMultiLineInput,
})}
Expand Down

0 comments on commit b627773

Please sign in to comment.