Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

message-input paste images

Pedro Ladeira2e286e91f603
diff --git a/apps/web/ui/shared/message-input.tsx b/apps/web/ui/shared/message-input.tsx
index 5d61304f76..2905b11307 100644
--- a/apps/web/ui/shared/message-input.tsx
+++ b/apps/web/ui/shared/message-input.tsx
@@ -226,6 +226,16 @@ export function MessageInput({
onChange={(editor) => setTypedMessage((editor as any).getMarkdown())}
editorProps={{
handleDOMEvents: {
+ paste: (_view, event) => {
+ if (!canAddFiles) return false;
+
+ const files = pastedAttachmentFiles(event.clipboardData);
+ if (files.length === 0) return false;
+
+ event.preventDefault();
+ handleFiles(files);
+ return true;
+ },
keydown: (_view, e) => {
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
e.preventDefault();
@@ -351,6 +361,33 @@ export function MessageInput({
);
}
+function pastedAttachmentFiles(data: DataTransfer | null) {
+ if (!data) return [];
+
+ // Read files before text. Some browsers clear the file list after getData.
+ const files = Array.from(data.files);
+ if (files.length === 0) {
+ for (const item of Array.from(data.items)) {
+ if (item.kind !== "file") continue;
+ const file = item.getAsFile();
+ if (file) files.push(file);
+ }
+ }
+ if (files.length === 0) return [];
+
+ // Screenshot HTML is an empty image wrapper. Text pastes stay text.
+ const html = data.getData("text/html");
+ const pastedText = (
+ html.trim()
+ ? new DOMParser().parseFromString(html, "text/html").body.textContent
+ : data.getData("text/plain")
+ )
+ ?.replace(/\u00a0/g, " ")
+ .trim();
+
+ return pastedText ? [] : files;
+}
+
function getUnsupportedFileTypeMessage(allowedFileTypes: readonly string[]) {
if (allowedFileTypes.length === 0) {
return "File type not supported.";