Skip to content

Commit 4b1468c

Browse files
authored
Web: support pasting images to PublishDialog
1 parent 00fe639 commit 4b1468c

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

web/src/components/Messaging.jsx

+16-9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ const Messaging = (props) => {
2626
setAttachFile(null);
2727
};
2828

29+
const getPastedImage = (ev) => {
30+
const { items } = ev.clipboardData;
31+
for (let i = 0; i < items.length; i += 1) {
32+
if (items[i].type.indexOf("image") !== -1) {
33+
return items[i].getAsFile();
34+
}
35+
}
36+
return null;
37+
};
38+
2939
return (
3040
<>
3141
{subscription && (
@@ -35,6 +45,7 @@ const Messaging = (props) => {
3545
onMessageChange={setMessage}
3646
onFilePasted={setAttachFile}
3747
onOpenDialogClick={handleOpenDialogClick}
48+
getPastedImage={getPastedImage}
3849
/>
3950
)}
4051
<PublishDialog
@@ -44,6 +55,7 @@ const Messaging = (props) => {
4455
topic={subscription?.topic ?? ""}
4556
message={message}
4657
attachFile={attachFile}
58+
getPastedImage={getPastedImage}
4759
onClose={handleDialogClose}
4860
onDragEnter={() => props.onDialogOpenModeChange((prev) => prev || PublishDialog.OPEN_MODE_DRAG)} // Only update if not already open
4961
onResetOpenMode={() => props.onDialogOpenModeChange(PublishDialog.OPEN_MODE_DEFAULT)}
@@ -67,15 +79,10 @@ const MessageBar = (props) => {
6779
};
6880

6981
const handlePaste = (ev) => {
70-
const clipboardData = ev.clipboardData || window.clipboardData;
71-
const { items } = clipboardData;
72-
for (let i = 0; i < items.length; i += 1) {
73-
if (items[i].type.indexOf("image") !== -1) {
74-
const blob = items[i].getAsFile();
75-
props.onFilePasted(blob);
76-
props.onOpenDialogClick();
77-
break;
78-
}
82+
const blob = props.getPastedImage(ev);
83+
if (blob) {
84+
props.onFilePasted(blob);
85+
props.onOpenDialogClick();
7986
}
8087
};
8188

web/src/components/PublishDialog.jsx

+8
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ const PublishDialog = (props) => {
241241
}
242242
}, [props.attachFile]);
243243

244+
const handlePaste = (ev) => {
245+
const blob = props.getPastedImage(ev);
246+
if (blob) {
247+
updateAttachFile(blob);
248+
}
249+
};
250+
244251
const handleAttachFileChanged = async (ev) => {
245252
await updateAttachFile(ev.target.files[0]);
246253
};
@@ -363,6 +370,7 @@ const PublishDialog = (props) => {
363370
inputProps={{
364371
"aria-label": t("publish_dialog_message_label"),
365372
}}
373+
onPaste={handlePaste}
366374
/>
367375
<FormControlLabel
368376
label={t("publish_dialog_checkbox_markdown")}

0 commit comments

Comments
 (0)