diff --git a/src/features/terminal/drop.test.ts b/src/features/terminal/drop.test.ts index ab6a226c8..ccd3501fc 100644 --- a/src/features/terminal/drop.test.ts +++ b/src/features/terminal/drop.test.ts @@ -3,6 +3,7 @@ import { commands } from "@/platform/ipc"; import { usePlatform } from "@/platform/platformInfo"; import { isRemoteWindow } from "@/platform/remote"; import { handleFileDrop } from "@/workbench/fileDrop"; +import { dragSession, FILES_DRAG } from "@/ui"; import { droppedPathsText, installTerminalDrop } from "./drop"; vi.mock("@/platform/remote", () => ({ isRemoteWindow: vi.fn(() => false) })); @@ -109,4 +110,23 @@ describe("terminal drop", () => { remove(); } }); + + it("pastes paths dragged from the Project view, also in a remote window", () => { + vi.mocked(isRemoteWindow).mockReturnValue(true); + const host = document.createElement("div"); + const child = document.createElement("span"); + host.append(child); + document.body.replaceChildren(host); + document.elementFromPoint = (() => child) as typeof document.elementFromPoint; + const paste = vi.fn(); + const remove = installTerminalDrop(host, paste); + try { + const drag = dragSession({ type: FILES_DRAG, payload: "/p/src/a b.ts\n/p/docs/" }); + expect(drag.move(1, 1)).toBe(true); + expect(drag.drop(1, 1)).toBe(true); + expect(paste.mock.calls).toEqual([["'/p/src/a b.ts' /p/docs"]]); + } finally { + remove(); + } + }); }); diff --git a/src/features/terminal/drop.ts b/src/features/terminal/drop.ts index d49746c10..a18d9eb20 100644 --- a/src/features/terminal/drop.ts +++ b/src/features/terminal/drop.ts @@ -1,13 +1,14 @@ /** - * Files dropped from the OS onto a terminal: the path is pasted at the prompt - * (shell-quoted when needed), separated by spaces, instead of the window-wide - * drop (open in the editor, "Open project in" for a folder) or the browser / - * shell falling through to a system dialog. Images are treated the same way: - * showing the path is the expected behaviour. + * Files dropped from the OS or dragged from the Project view onto a terminal: the + * path is pasted at the prompt (shell-quoted when needed), separated by spaces, + * instead of the window-wide drop (open in the editor, "Open project in" for a + * folder) or the browser / shell falling through to a system dialog. Images are + * treated the same way: showing the path is the expected behaviour. */ import { formatArgs } from "@/platform/commandLine"; import { usePlatform } from "@/platform/platformInfo"; import { isRemoteWindow } from "@/platform/remote"; +import { FILES_DRAG, registerDropTarget } from "@/ui"; import { registerFileDropHandler } from "@/workbench/fileDrop"; /** Shell-quoted paths for the prompt; the user presses Enter to execute. */ @@ -17,13 +18,31 @@ export function droppedPathsText(paths: readonly string[]): string { return formatArgs(paths); } -/** Registers the terminal's drop handler; the disposer unregisters it. */ +/** Registers the terminal's drop handlers; the disposer unregisters them. */ export function installTerminalDrop(host: HTMLElement, paste: (text: string) => void): () => void { - return registerFileDropHandler((paths, x, y) => { + const removeOsDrop = registerFileDropHandler((paths, x, y) => { + // A remote terminal can't reach this machine's files. if (!paths.length || isRemoteWindow()) return false; const el = document.elementFromPoint(x, y); if (!el || !host.contains(el)) return false; paste(droppedPathsText(paths)); return true; }); + // Project view paths are the project's own, so they also hold in a remote terminal. + const removeProjectDrop = registerDropTarget(host, { + accepts: (type) => type === FILES_DRAG, + over: () => {}, + leave: () => {}, + drop: (data) => { + const paths = data.payload + .split("\n") + .filter(Boolean) + .map((p) => (p.length > 1 && p.endsWith("/") ? p.slice(0, -1) : p)); + if (paths.length) paste(droppedPathsText(paths)); + }, + }); + return () => { + removeOsDrop(); + removeProjectDrop(); + }; }