mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-18 16:08:55 +00:00
fix(terminal): paste paths of files dragged from the Project view (#3511)
The terminal took file paths only from OS drops (Finder / Explorer). A file or folder dragged from the Project view onto the terminal did nothing. The terminal is now a drop target for Project view drags too and pastes the paths at the prompt, shell-quoted, without executing; it also works in a remote window, where the paths are the remote project's. Fixes #3503
This commit is contained in:
@@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user