|
| 1 | +import * as nls from "vs/nls"; |
| 2 | +import { Action } from "vs/base/common/actions"; |
| 3 | +import { TERMINAL_COMMAND_ID } from "vs/workbench/parts/terminal/common/terminalCommands"; |
| 4 | +import { ITerminalService } from "vs/workbench/parts/terminal/common/terminal"; |
| 5 | +import * as actions from "vs/workbench/parts/terminal/electron-browser/terminalActions"; |
| 6 | +import * as instance from "vs/workbench/parts/terminal/electron-browser/terminalInstance"; |
| 7 | +import { clipboard } from "@coder/ide"; |
| 8 | + |
| 9 | +const getLabel = (key: string, enabled: boolean): string => { |
| 10 | + return enabled |
| 11 | + ? nls.localize(key, "Paste") |
| 12 | + : nls.localize(`${key}WithKeybind`, "Paste (must use keybind)"); |
| 13 | +}; |
| 14 | + |
| 15 | +export class PasteAction extends Action { |
| 16 | + |
| 17 | + private static readonly KEY = "paste"; |
| 18 | + |
| 19 | + public constructor() { |
| 20 | + super( |
| 21 | + "editor.action.clipboardPasteAction", |
| 22 | + getLabel(PasteAction.KEY, clipboard.isEnabled), |
| 23 | + undefined, |
| 24 | + clipboard.isEnabled, |
| 25 | + async (): Promise<boolean> => clipboard.paste(), |
| 26 | + ); |
| 27 | + |
| 28 | + clipboard.onPermissionChange((enabled) => { |
| 29 | + this.label = getLabel(PasteAction.KEY, enabled); |
| 30 | + this.enabled = enabled; |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +class TerminalPasteAction extends Action { |
| 37 | + |
| 38 | + private static readonly KEY = "workbench.action.terminal.paste"; |
| 39 | + |
| 40 | + public static readonly ID = TERMINAL_COMMAND_ID.PASTE; |
| 41 | + public static readonly LABEL = nls.localize("workbench.action.terminal.paste", "Paste into Active Terminal"); |
| 42 | + public static readonly SHORT_LABEL = getLabel(TerminalPasteAction.KEY, clipboard.isEnabled); |
| 43 | + |
| 44 | + public constructor( |
| 45 | + id: string, label: string, |
| 46 | + @ITerminalService private terminalService: ITerminalService, |
| 47 | + ) { |
| 48 | + super(id, label); |
| 49 | + clipboard.onPermissionChange((enabled) => { |
| 50 | + this._setLabel(getLabel(TerminalPasteAction.KEY, enabled)); |
| 51 | + }); |
| 52 | + this._setLabel(getLabel(TerminalPasteAction.KEY, clipboard.isEnabled)); |
| 53 | + } |
| 54 | + |
| 55 | + public run(): Promise<void> { |
| 56 | + const instance = this.terminalService.getActiveOrCreateInstance(); |
| 57 | + if (instance) { |
| 58 | + // tslint:disable-next-line no-any it will return a promise (see below) |
| 59 | + return (instance as any).paste(); |
| 60 | + } |
| 61 | + |
| 62 | + return Promise.resolve(); |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +class TerminalInstance extends instance.TerminalInstance { |
| 68 | + |
| 69 | + public async paste(): Promise<void> { |
| 70 | + this.focus(); |
| 71 | + if (clipboard.isEnabled) { |
| 72 | + const text = await clipboard.readText(); |
| 73 | + this.sendText(text, false); |
| 74 | + } else { |
| 75 | + document.execCommand("paste"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | +const actionsTarget = actions as typeof actions; |
| 82 | +// @ts-ignore TODO: don't ignore it. |
| 83 | +actionsTarget.TerminalPasteAction = TerminalPasteAction; |
| 84 | + |
| 85 | +const instanceTarget = instance as typeof instance; |
| 86 | +instanceTarget.TerminalInstance = TerminalInstance; |
0 commit comments