Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.

Commit 22022ea

Browse files
authored
Merge pull request #1246 from Ansimorph/dragndrop
Add drag & drop path insertion
2 parents 60a23ee + 6c0bcae commit 22022ea

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

src/Interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,5 @@ export interface TerminalLikeDevice {
5757
}
5858

5959
export type UserEvent = KeyboardEvent | ClipboardEvent;
60+
61+
export type MouseEvent = DragEvent;

src/views/Main.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {handleUserEvent} from "./keyevents/Keybindings";
2+
import {handleMouseEvent} from "./mouseevents/MouseEvents";
23

34
process.env.PATH = "/usr/local/bin:" + process.env.PATH;
45
process.env.NODE_ENV = process.env.NODE_ENV || "production";
@@ -12,7 +13,7 @@ import * as React from "react";
1213
import {ApplicationComponent} from "./ApplicationComponent";
1314
import {loadAllPlugins} from "../PluginManager";
1415
import {loadEnvironment} from "../shell/Environment";
15-
import {UserEvent} from "../Interfaces";
16+
import {UserEvent, MouseEvent} from "../Interfaces";
1617
import {remote} from "electron";
1718
import {buildMenuTemplate} from "./menu/Menu";
1819

@@ -27,15 +28,6 @@ document.addEventListener(
2728
false,
2829
);
2930

30-
document.addEventListener(
31-
"drop",
32-
function(event) {
33-
event.preventDefault();
34-
return false;
35-
},
36-
false,
37-
);
38-
3931
async function main() {
4032
// Should be required before mounting Application.
4133
require("../monaco/PromptTheme");
@@ -58,8 +50,14 @@ async function main() {
5850
event,
5951
);
6052

53+
const mouseEventHandler = (event: MouseEvent) => handleMouseEvent(
54+
application,
55+
event,
56+
);
57+
6158
document.body.addEventListener("keydown", userEventHandler, true);
6259
document.body.addEventListener("paste", userEventHandler, true);
60+
document.body.addEventListener("drop", mouseEventHandler, true);
6361

6462
require("../plugins/JobFinishedNotifications");
6563
require("../plugins/UpdateLastPresentWorkingDirectory");

src/views/PromptComponent.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ export class PromptComponent extends React.Component<Props, State> {
204204
this.focus();
205205
}
206206

207+
insertValueInPlace(value: string): void {
208+
this.editor.trigger("keyboard", "type", {text: value});
209+
this.focus();
210+
}
211+
207212
private async execute(): Promise<void> {
208213
let promptText = this.editor.getValue();
209214
this.prompt.setValue(promptText);

src/views/mouseevents/MouseEvents.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {ApplicationComponent} from "../ApplicationComponent";
2+
import {MouseEvent} from "../../Interfaces";
3+
import * as fs from "fs";
4+
import {userFriendlyPath, escapeFilePath, normalizeDirectory} from "../../utils/Common";
5+
import {Status} from "../../Enums";
6+
7+
function isDirectory(path: string): boolean {
8+
return fs.lstatSync(path).isDirectory();
9+
}
10+
11+
export function handleMouseEvent(application: ApplicationComponent, event: MouseEvent) {
12+
const sessionComponent = application.focusedTabComponent.focusedSessionComponent;
13+
if (!sessionComponent) {
14+
return;
15+
}
16+
17+
const isJobRunning = sessionComponent.status === Status.InProgress;
18+
const promptComponent = sessionComponent.promptComponent;
19+
20+
if (event instanceof DragEvent) {
21+
const path = event.dataTransfer.files[0].path;
22+
let formattedPath = userFriendlyPath(escapeFilePath(path));
23+
24+
if (isDirectory(path)) {
25+
formattedPath = normalizeDirectory(formattedPath);
26+
}
27+
28+
if (!isJobRunning) {
29+
promptComponent.insertValueInPlace(formattedPath);
30+
}
31+
32+
event.preventDefault();
33+
return;
34+
}
35+
}

0 commit comments

Comments
 (0)