Terminal pasting
This commit is contained in:
@ -2,7 +2,7 @@ import { Event } from "@coder/events";
|
||||
import { field, logger, time, Time } from "@coder/logger";
|
||||
import { InitData, ISharedProcessData } from "@coder/protocol";
|
||||
import { retry } from "./retry";
|
||||
import { Upload } from "./upload";
|
||||
import { upload } from "./upload";
|
||||
import { client } from "./fill/client";
|
||||
import { clipboard } from "./fill/clipboard";
|
||||
import { INotificationService, NotificationService, IProgressService, ProgressService } from "./fill/notification";
|
||||
@ -21,7 +21,8 @@ export abstract class Client {
|
||||
public readonly retry = retry;
|
||||
public readonly clipboard = clipboard;
|
||||
public readonly uriFactory: IURIFactory;
|
||||
public readonly upload = new Upload(new NotificationService(), new ProgressService());
|
||||
public readonly upload = upload;
|
||||
|
||||
private start: Time | undefined;
|
||||
private readonly progressElement: HTMLElement | undefined;
|
||||
private tasks: string[] = [];
|
||||
|
@ -1,3 +1,6 @@
|
||||
export * from "./client";
|
||||
export * from "./fill/uri";
|
||||
export * from "./fill/clipboard";
|
||||
export * from "./fill/notification";
|
||||
export * from "./fill/uri";
|
||||
export * from "./retry";
|
||||
export * from "./upload";
|
||||
|
@ -4,7 +4,7 @@ import { promisify } from "util";
|
||||
import { logger, Logger } from "@coder/logger";
|
||||
import { escapePath } from "@coder/protocol";
|
||||
import { IURI } from "./fill/uri";
|
||||
import { INotificationService, IProgressService, IProgress, Severity } from "./fill/notification";
|
||||
import { NotificationService, INotificationService, ProgressService, IProgressService, IProgress, Severity } from "./fill/notification";
|
||||
|
||||
/**
|
||||
* Represents an uploadable directory, so we can query for existing files once.
|
||||
@ -355,3 +355,6 @@ export class Upload {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Global instance.
|
||||
export const upload = new Upload(new NotificationService(), new ProgressService());
|
||||
|
@ -4,6 +4,7 @@ import "./fill/storageDatabase";
|
||||
import "./fill/windowsService";
|
||||
import "./fill/environmentService";
|
||||
import "./fill/vscodeTextmate";
|
||||
import { PasteAction } from "./fill/paste";
|
||||
import "./fill/dom";
|
||||
import "./vscode.scss";
|
||||
import { Client as IDEClient, IURI, IURIFactory, IProgress, INotificationHandle } from "@coder/ide";
|
||||
@ -19,8 +20,6 @@ import { IEditorGroup } from "vs/workbench/services/group/common/editorGroupsSer
|
||||
import { IWindowsService } from "vs/platform/windows/common/windows";
|
||||
import { ServiceCollection } from "vs/platform/instantiation/common/serviceCollection";
|
||||
import { RawContextKey, IContextKeyService } from "vs/platform/contextkey/common/contextkey";
|
||||
import { Action } from "vs/base/common/actions";
|
||||
import * as nls from "vs/nls";
|
||||
|
||||
export class Client extends IDEClient {
|
||||
|
||||
@ -78,27 +77,8 @@ export class Client extends IDEClient {
|
||||
/**
|
||||
* Create a paste action for use in text inputs.
|
||||
*/
|
||||
public get pasteAction(): Action {
|
||||
const getLabel = (enabled: boolean): string => {
|
||||
return enabled
|
||||
? nls.localize("paste", "Paste")
|
||||
: nls.localize("pasteWithKeybind", "Paste (must use keybind)");
|
||||
};
|
||||
|
||||
const pasteAction = new Action(
|
||||
"editor.action.clipboardPasteAction",
|
||||
getLabel(this.clipboard.isEnabled),
|
||||
undefined,
|
||||
this.clipboard.isEnabled,
|
||||
async (): Promise<boolean> => this.clipboard.paste(),
|
||||
);
|
||||
|
||||
this.clipboard.onPermissionChange((enabled) => {
|
||||
pasteAction.label = getLabel(enabled);
|
||||
pasteAction.enabled = enabled;
|
||||
});
|
||||
|
||||
return pasteAction;
|
||||
public get pasteAction(): PasteAction {
|
||||
return new PasteAction();
|
||||
}
|
||||
|
||||
public get serviceCollection(): ServiceCollection {
|
||||
|
86
packages/vscode/src/fill/paste.ts
Normal file
86
packages/vscode/src/fill/paste.ts
Normal file
@ -0,0 +1,86 @@
|
||||
import * as nls from "vs/nls";
|
||||
import { Action } from "vs/base/common/actions";
|
||||
import { TERMINAL_COMMAND_ID } from "vs/workbench/parts/terminal/common/terminalCommands";
|
||||
import { ITerminalService } from "vs/workbench/parts/terminal/common/terminal";
|
||||
import * as actions from "vs/workbench/parts/terminal/electron-browser/terminalActions";
|
||||
import * as instance from "vs/workbench/parts/terminal/electron-browser/terminalInstance";
|
||||
import { clipboard } from "@coder/ide";
|
||||
|
||||
const getLabel = (key: string, enabled: boolean): string => {
|
||||
return enabled
|
||||
? nls.localize(key, "Paste")
|
||||
: nls.localize(`${key}WithKeybind`, "Paste (must use keybind)");
|
||||
};
|
||||
|
||||
export class PasteAction extends Action {
|
||||
|
||||
private static readonly KEY = "paste";
|
||||
|
||||
public constructor() {
|
||||
super(
|
||||
"editor.action.clipboardPasteAction",
|
||||
getLabel(PasteAction.KEY, clipboard.isEnabled),
|
||||
undefined,
|
||||
clipboard.isEnabled,
|
||||
async (): Promise<boolean> => clipboard.paste(),
|
||||
);
|
||||
|
||||
clipboard.onPermissionChange((enabled) => {
|
||||
this.label = getLabel(PasteAction.KEY, enabled);
|
||||
this.enabled = enabled;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class TerminalPasteAction extends Action {
|
||||
|
||||
private static readonly KEY = "workbench.action.terminal.paste";
|
||||
|
||||
public static readonly ID = TERMINAL_COMMAND_ID.PASTE;
|
||||
public static readonly LABEL = nls.localize("workbench.action.terminal.paste", "Paste into Active Terminal");
|
||||
public static readonly SHORT_LABEL = getLabel(TerminalPasteAction.KEY, clipboard.isEnabled);
|
||||
|
||||
public constructor(
|
||||
id: string, label: string,
|
||||
@ITerminalService private terminalService: ITerminalService,
|
||||
) {
|
||||
super(id, label);
|
||||
clipboard.onPermissionChange((enabled) => {
|
||||
this._setLabel(getLabel(TerminalPasteAction.KEY, enabled));
|
||||
});
|
||||
this._setLabel(getLabel(TerminalPasteAction.KEY, clipboard.isEnabled));
|
||||
}
|
||||
|
||||
public run(): Promise<void> {
|
||||
const instance = this.terminalService.getActiveOrCreateInstance();
|
||||
if (instance) {
|
||||
// tslint:disable-next-line no-any it will return a promise (see below)
|
||||
return (instance as any).paste();
|
||||
}
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class TerminalInstance extends instance.TerminalInstance {
|
||||
|
||||
public async paste(): Promise<void> {
|
||||
this.focus();
|
||||
if (clipboard.isEnabled) {
|
||||
const text = await clipboard.readText();
|
||||
this.sendText(text, false);
|
||||
} else {
|
||||
document.execCommand("paste");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const actionsTarget = actions as typeof actions;
|
||||
// @ts-ignore TODO: don't ignore it.
|
||||
actionsTarget.TerminalPasteAction = TerminalPasteAction;
|
||||
|
||||
const instanceTarget = instance as typeof instance;
|
||||
instanceTarget.TerminalInstance = TerminalInstance;
|
Reference in New Issue
Block a user