Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

community(composer): paste-to-upload image via clipboard

Gener801e8798a5b8
diff --git a/src/web/src/components/community/composer.test.ts b/src/web/src/components/community/composer.test.ts
index 7996a3a87..2ba1ea12f 100644
--- a/src/web/src/components/community/composer.test.ts
+++ b/src/web/src/components/community/composer.test.ts
@@ -1,7 +1,17 @@
import { describe, it, expect } from "vitest"
-import { pendingFilesToSendAttachments, popoverStyle } from "./composer"
+import { clipboardFiles, pendingFilesToSendAttachments, popoverStyle } from "./composer"
import type { PendingFile } from "@/hooks/use-file-attachments"
+// Minimal DataTransferItemList stand-in: each entry declares its `kind` and the
+// File its `getAsFile()` yields (null models the browser returning nothing for
+// a "file" item). Indexed access + `.length` is all `clipboardFiles` touches.
+function itemList(entries: Array<{ kind: string; file: File | null }>): DataTransferItemList {
+ const items = entries.map((e) => ({ kind: e.kind, getAsFile: () => e.file })) as unknown as DataTransferItem[]
+ const list = { length: items.length } as unknown as DataTransferItemList
+ items.forEach((it, i) => { (list as unknown as Record<number, DataTransferItem>)[i] = it })
+ return list
+}
+
function rectAt(top: number, opts: { left?: number; height?: number } = {}): DOMRect {
const height = opts.height ?? 16
const left = opts.left ?? 40
@@ -60,6 +70,44 @@ describe("pendingFilesToSendAttachments", () => {
})
})
+// `clipboardFiles` is the pure filter behind `editorProps.handlePaste` — it
+// picks the pasted File(s) out of the clipboard so an image paste routes to
+// `addPendingFiles` (same as picker/drop). A text-only paste yields [] so the
+// handler returns false and the paste falls through to `clipboardTextParser`.
+describe("clipboardFiles", () => {
+ const png = new File(["x"], "photo.png", { type: "image/png" })
+ const jpg = new File(["y"], "shot.jpg", { type: "image/jpeg" })
+
+ it("returns [] when there's no clipboard items list", () => {
+ expect(clipboardFiles(undefined)).toEqual([])
+ })
+
+ it("returns [] for a text-only paste (no file items → falls through to text parser)", () => {
+ expect(clipboardFiles(itemList([{ kind: "string", file: null }]))).toEqual([])
+ })
+
+ it("collects a single pasted image file", () => {
+ expect(clipboardFiles(itemList([{ kind: "file", file: png }]))).toEqual([png])
+ })
+
+ it("collects multiple pasted files in order, ignoring interleaved string items", () => {
+ const list = itemList([
+ { kind: "string", file: null },
+ { kind: "file", file: png },
+ { kind: "file", file: jpg },
+ ])
+ expect(clipboardFiles(list)).toEqual([png, jpg])
+ })
+
+ it("skips a file item whose getAsFile() returns null", () => {
+ const list = itemList([
+ { kind: "file", file: null },
+ { kind: "file", file: png },
+ ])
+ expect(clipboardFiles(list)).toEqual([png])
+ })
+})
+
describe("popoverStyle", () => {
const VW = 1024
const VH = 768
diff --git a/src/web/src/components/community/composer.tsx b/src/web/src/components/community/composer.tsx
index 0e94c4b16..d2d1adb2d 100644
--- a/src/web/src/components/community/composer.tsx
+++ b/src/web/src/components/community/composer.tsx
@@ -48,6 +48,23 @@ export function pendingFilesToSendAttachments(pendingFiles: PendingFile[]): Send
return pendingFiles.map((pf) => ({ file: pf.file, width: pf.width, height: pf.height }))
}
+// Pure extraction of the paste → File[] collection used by `handlePaste`:
+// keep only clipboard items whose `kind === "file"` (a pasted image, or any
+// dragged-in-then-copied file) and unwrap each via `getAsFile()`, dropping the
+// occasional null the API returns. Extracted (like `pendingFilesToSendAttachments`)
+// so the filtering is unit-testable without mounting the tiptap editor.
+export function clipboardFiles(items: DataTransferItemList | undefined): File[] {
+ if (!items) return []
+ const files: File[] = []
+ for (let i = 0; i < items.length; i++) {
+ if (items[i].kind === "file") {
+ const f = items[i].getAsFile()
+ if (f) files.push(f)
+ }
+ }
+ return files
+}
+
type ComposerMode = "chat" | "forumPostBody"
export type ComposerHandle = {
@@ -138,6 +155,7 @@ export const Composer = forwardRef<ComposerHandle, ComposerProps>(function Compo
const {
pendingFiles,
setPendingFiles,
+ addPendingFiles,
fileInputRef,
handleFileSelect,
removePendingFile,
@@ -309,6 +327,21 @@ export const Composer = forwardRef<ComposerHandle, ComposerProps>(function Compo
}
return false
},
+ // Paste an image (or any file) from the clipboard → same pipeline as the
+ // file picker / drag-drop: `addPendingFiles` (25MB + MIME + thumbnail).
+ // Consume the event (`preventDefault` + `return true`) once files are
+ // found so ProseMirror doesn't ALSO paste the image as text/HTML. Text-
+ // only pastes return false and fall through to `clipboardTextParser`
+ // below, which preserves both newline levels.
+ handlePaste: (_view, event) => {
+ const files = clipboardFiles(event.clipboardData?.items)
+ if (files.length > 0) {
+ event.preventDefault()
+ addPendingFiles(files)
+ return true
+ }
+ return false
+ },
// Preserve BOTH newline levels on paste: a blank line (`\n\n`) becomes a
// paragraph break, a single `\n` becomes a hard line break — instead of
// ProseMirror's default, which collapses any run of newlines to one