Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}})",
Expand Down
35 changes: 33 additions & 2 deletions src/frontend/terminal/Terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -373,6 +374,17 @@ const TerminalInner = forwardRef<TerminalHandle, SSHTerminalProps>(
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<string | null>(null);
Expand Down Expand Up @@ -3866,6 +3878,7 @@ const TerminalInner = forwardRef<TerminalHandle, SSHTerminalProps>(
async function handleImageUpload(
file: File,
source: TerminalImageUploadSource,
inputPrefix = "",
) {
if (file.type && !file.type.startsWith("image/")) {
toast.error(t("terminal.imageChooseFile"));
Expand Down Expand Up @@ -3895,7 +3908,7 @@ const TerminalInner = forwardRef<TerminalHandle, SSHTerminalProps>(
webSocketRef.current?.send(
JSON.stringify({
type: "input",
data: quoteTerminalImagePath(shellPath),
data: inputPrefix + quoteTerminalImagePath(shellPath),
}),
);
toast.success(t("terminal.imageUploaded", { path: shellPath }));
Expand All @@ -3915,9 +3928,17 @@ const TerminalInner = forwardRef<TerminalHandle, SSHTerminalProps>(
}
}

// 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() {
Expand Down Expand Up @@ -4090,6 +4111,7 @@ const TerminalInner = forwardRef<TerminalHandle, SSHTerminalProps>(
{toolbarOnTop && toolbar}
<div
className="relative min-h-0 w-full flex-1"
{...imageDropHandlers}
style={{
backgroundColor: backgroundImage ? "transparent" : backgroundColor,
...(backgroundSrc && {
Expand Down Expand Up @@ -4126,6 +4148,15 @@ const TerminalInner = forwardRef<TerminalHandle, SSHTerminalProps>(
}}
/>

{isImageDragOver && (
<div className="pointer-events-none absolute inset-0 z-[125] flex items-center justify-center border-2 border-dashed border-accent-brand bg-background/70">
<div className="flex items-center gap-2 text-sm text-foreground">
<ImagePlus className="size-4 text-accent-brand" />
{t("terminal.imageDropHint")}
</div>
</div>
)}

<ZmodemBar
state={zmodemState}
onFiles={(files) => zmodemRef.current?.sendFiles(files)}
Expand Down
65 changes: 65 additions & 0 deletions src/frontend/terminal/terminal-image-drop.ts
Original file line number Diff line number Diff line change
@@ -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();
},
};
}
93 changes: 93 additions & 0 deletions tests/frontend/terminal/terminal-image-drop.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});