diff --git a/CHANGELOG.md b/CHANGELOG.md index 67af805..658a77d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - A padding setting for the space around the terminal text, per host or as a default +- Drop an image on a terminal to upload it to the host and type its path, like pasting one. Several images are typed in order ### Changed diff --git a/locales/en.json b/locales/en.json index 4c6d28e..dc8148e 100644 --- a/locales/en.json +++ b/locales/en.json @@ -99,6 +99,7 @@ "imageUploaded": "Image uploaded: {{path}}", "imageClipboardUnavailable": "This browser cannot read images from the clipboard", "imageClipboardEmpty": "No image found in the clipboard", + "imageDropHint": "Drop an image to upload it", "localShell": "Local terminal shell", "localBackendUnreachable": "The local backend is not reachable", "processExited": "Process exited ({{code}})", diff --git a/src/frontend/terminal/Terminal.tsx b/src/frontend/terminal/Terminal.tsx index 2e74ad9..877cac4 100644 --- a/src/frontend/terminal/Terminal.tsx +++ b/src/frontend/terminal/Terminal.tsx @@ -64,6 +64,7 @@ import { type TerminalImageUploadSource, } from "./terminal-image-upload"; import { createTerminalNativePasteHandler } from "./terminal-native-paste"; +import { createTerminalImageDropHandlers } from "./terminal-image-drop"; import { TerminalSearchBar } from "./search/TerminalSearchBar.tsx"; import { CommandAutocomplete, @@ -77,7 +78,7 @@ import { type TerminalSlotApi, } from "./terminal-slots"; import { toast } from "sonner"; -import { Save } from "lucide-react"; +import { ImagePlus, Save } from "lucide-react"; import { TerminalToolbar } from "./TerminalToolbar.tsx"; import { BroadcastBar } from "../terminal-tools/broadcast/BroadcastBar"; import { @@ -373,6 +374,17 @@ const TerminalInner = forwardRef( const [isQuickConnectSaved, setIsQuickConnectSaved] = useState(false); const [isImageUploading, setIsImageUploading] = useState(false); const imageUploadRef = useRef(handleImageUpload); + const [isImageDragOver, setIsImageDragOver] = useState(false); + const droppedImagesRef = useRef(handleDroppedImages); + const imageDropHandlers = useMemo( + () => + createTerminalImageDropHandlers({ + uploadImages: (files) => void droppedImagesRef.current(files), + rejectFiles: () => toast.error(t("terminal.imageChooseFile")), + setDragging: setIsImageDragOver, + }), + [t], + ); const [isConnecting, setIsConnecting] = useState(false); const [isFitted, setIsFitted] = useState(false); const [connectionError, setConnectionError] = useState(null); @@ -3866,6 +3878,7 @@ const TerminalInner = forwardRef( async function handleImageUpload( file: File, source: TerminalImageUploadSource, + inputPrefix = "", ) { if (file.type && !file.type.startsWith("image/")) { toast.error(t("terminal.imageChooseFile")); @@ -3895,7 +3908,7 @@ const TerminalInner = forwardRef( webSocketRef.current?.send( JSON.stringify({ type: "input", - data: quoteTerminalImagePath(shellPath), + data: inputPrefix + quoteTerminalImagePath(shellPath), }), ); toast.success(t("terminal.imageUploaded", { path: shellPath })); @@ -3915,9 +3928,17 @@ const TerminalInner = forwardRef( } } + // One at a time, so the paths are typed in drop order with a space between. + async function handleDroppedImages(files: File[]) { + for (const [index, file] of files.entries()) { + await handleImageUpload(file, "file", index > 0 ? " " : ""); + } + } + // Keep the terminal session and upload callback current without rebuilding xterm. useEffect(() => { imageUploadRef.current = handleImageUpload; + droppedImagesRef.current = handleDroppedImages; }); async function handleClipboardImage() { @@ -4090,6 +4111,7 @@ const TerminalInner = forwardRef( {toolbarOnTop && toolbar}
( }} /> + {isImageDragOver && ( +
+
+ + {t("terminal.imageDropHint")} +
+
+ )} + zmodemRef.current?.sendFiles(files)} diff --git a/src/frontend/terminal/terminal-image-drop.ts b/src/frontend/terminal/terminal-image-drop.ts new file mode 100644 index 0000000..3579fa5 --- /dev/null +++ b/src/frontend/terminal/terminal-image-drop.ts @@ -0,0 +1,65 @@ +/** The part of a drag event the drop handlers use, native or React. */ +export interface TerminalDragEvent { + dataTransfer: DataTransfer | null; + preventDefault: () => void; +} + +export interface TerminalImageDropHandlers { + onDragEnter: (event: TerminalDragEvent) => void; + onDragOver: (event: TerminalDragEvent) => void; + onDragLeave: (event: TerminalDragEvent) => void; + onDrop: (event: TerminalDragEvent) => void; +} + +function carriesFiles(event: TerminalDragEvent): boolean { + return Array.from(event.dataTransfer?.types ?? []).includes("Files"); +} + +/** + * Take files dragged onto the terminal. Images go to `uploadImages`, in the + * order they were dropped. A drop with no image calls `rejectFiles`. Either way + * the browser no longer opens the dropped file in place of the page. Drags + * without files, like selected text, are left alone. + * + * `setDragging` says when a file drag is over the terminal. Enter and leave + * also fire for every child the pointer crosses, so they are counted. + */ +export function createTerminalImageDropHandlers(options: { + uploadImages: (files: File[]) => void; + rejectFiles: () => void; + setDragging: (dragging: boolean) => void; +}): TerminalImageDropHandlers { + let depth = 0; + const leave = () => { + depth = 0; + options.setDragging(false); + }; + return { + onDragEnter(event) { + if (!carriesFiles(event)) return; + event.preventDefault(); + depth++; + if (depth === 1) options.setDragging(true); + }, + onDragOver(event) { + if (!carriesFiles(event)) return; + event.preventDefault(); + if (event.dataTransfer) event.dataTransfer.dropEffect = "copy"; + }, + onDragLeave(event) { + if (!carriesFiles(event) || depth === 0) return; + depth--; + if (depth === 0) options.setDragging(false); + }, + onDrop(event) { + if (!carriesFiles(event)) return; + event.preventDefault(); + leave(); + const images = Array.from(event.dataTransfer?.files ?? []).filter( + (file) => file.type.startsWith("image/"), + ); + if (images.length > 0) options.uploadImages(images); + else options.rejectFiles(); + }, + }; +} diff --git a/tests/frontend/terminal/terminal-image-drop.test.ts b/tests/frontend/terminal/terminal-image-drop.test.ts new file mode 100644 index 0000000..0dcc684 --- /dev/null +++ b/tests/frontend/terminal/terminal-image-drop.test.ts @@ -0,0 +1,93 @@ +// @vitest-environment jsdom +import { describe, expect, it, vi } from "vitest"; +import { createTerminalImageDropHandlers } from "../../../src/frontend/terminal/terminal-image-drop"; + +function dragEvent(files: File[] | null, types = files ? ["Files"] : []) { + return { + dataTransfer: { types, files: files ?? [], dropEffect: "none" }, + preventDefault: vi.fn(), + }; +} + +function setup() { + const options = { + uploadImages: vi.fn(), + rejectFiles: vi.fn(), + setDragging: vi.fn(), + }; + return { options, handlers: createTerminalImageDropHandlers(options) }; +} + +const png = new File([new Uint8Array([137, 80, 78, 71])], "capture.png", { + type: "image/png", +}); +const jpeg = new File(["jpeg"], "photo.jpg", { type: "image/jpeg" }); +const pdf = new File(["pdf"], "document.pdf", { type: "application/pdf" }); + +describe("terminal image drop", () => { + it("uploads a dropped image and stops the browser opening it", () => { + const { options, handlers } = setup(); + const event = dragEvent([png]); + handlers.onDrop(event as never); + expect(options.uploadImages).toHaveBeenCalledExactlyOnceWith([png]); + expect(options.rejectFiles).not.toHaveBeenCalled(); + expect(event.preventDefault).toHaveBeenCalledOnce(); + }); + it("uploads every image in drop order and skips other files", () => { + const { options, handlers } = setup(); + handlers.onDrop(dragEvent([jpeg, pdf, png]) as never); + expect(options.uploadImages).toHaveBeenCalledExactlyOnceWith([jpeg, png]); + expect(options.rejectFiles).not.toHaveBeenCalled(); + }); + it("rejects a drop with no image, and still stops the browser opening it", () => { + const { options, handlers } = setup(); + const event = dragEvent([pdf]); + handlers.onDrop(event as never); + expect(options.rejectFiles).toHaveBeenCalledOnce(); + expect(options.uploadImages).not.toHaveBeenCalled(); + expect(event.preventDefault).toHaveBeenCalledOnce(); + }); + it("allows the drop while files are dragged over the terminal", () => { + const { handlers } = setup(); + const event = dragEvent([]); + handlers.onDragOver(event as never); + expect(event.preventDefault).toHaveBeenCalledOnce(); + expect(event.dataTransfer.dropEffect).toBe("copy"); + }); + it("leaves drags without files alone", () => { + const { options, handlers } = setup(); + const text = dragEvent(null, ["text/plain"]); + const missing = { dataTransfer: null, preventDefault: vi.fn() }; + for (const event of [text, missing]) { + handlers.onDragEnter(event as never); + handlers.onDragOver(event as never); + handlers.onDragLeave(event as never); + handlers.onDrop(event as never); + expect(event.preventDefault).not.toHaveBeenCalled(); + } + expect(options.setDragging).not.toHaveBeenCalled(); + expect(options.uploadImages).not.toHaveBeenCalled(); + expect(options.rejectFiles).not.toHaveBeenCalled(); + }); + it("reports dragging once while the pointer crosses child elements", () => { + const { options, handlers } = setup(); + handlers.onDragEnter(dragEvent([]) as never); + handlers.onDragEnter(dragEvent([]) as never); + handlers.onDragLeave(dragEvent([]) as never); + expect(options.setDragging.mock.calls).toEqual([[true]]); + handlers.onDragLeave(dragEvent([]) as never); + expect(options.setDragging.mock.calls).toEqual([[true], [false]]); + handlers.onDragLeave(dragEvent([]) as never); + expect(options.setDragging).toHaveBeenCalledTimes(2); + }); + it("stops reporting dragging after a drop, and starts again on the next drag", () => { + const { options, handlers } = setup(); + handlers.onDragEnter(dragEvent([]) as never); + handlers.onDragEnter(dragEvent([]) as never); + handlers.onDrop(dragEvent([png]) as never); + expect(options.setDragging).toHaveBeenLastCalledWith(false); + handlers.onDragEnter(dragEvent([]) as never); + expect(options.setDragging).toHaveBeenLastCalledWith(true); + expect(options.setDragging).toHaveBeenCalledTimes(3); + }); +});