Skip to content

Commit

Permalink
Add webview specific copy/paste, undo/redo commands
Browse files Browse the repository at this point in the history
Fixes #65452

Adds copy/paste, undo/redo commands that are specific to the webview. This is very similar to the approach we already use to implement 'select all'
  • Loading branch information
mjbvz committed Jan 28, 2019
1 parent 70f4460 commit 61efc1d
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,44 @@ export abstract class BaseWebviewEditor extends BaseEditor {
}

public reload() {
if (this._webview) {
this._webview.reload();
}
this.withWebviewElement(webview => webview.reload());
}

public layout(dimension: Dimension): void {
if (this._webview) {
this._webview.layout();
}
this.withWebviewElement(webview => webview.layout());
}

public focus(): void {
if (this._webview) {
this._webview.focus();
}
this.withWebviewElement(webview => webview.focus());
}

public selectAll(): void {
this.withWebviewElement(webview => webview.selectAll());
}

public copy(): void {
this.withWebviewElement(webview => webview.copy());
}

public paste(): void {
this.withWebviewElement(webview => webview.paste());
}

public cut(): void {
this.withWebviewElement(webview => webview.cut());
}

public undo(): void {
this.withWebviewElement(webview => webview.undo());
}

public redo(): void {
this.withWebviewElement(webview => webview.redo());
}

private withWebviewElement(f: (element: WebviewElement) => void): void {
if (this._webview) {
this._webview.selectAll();
f(this._webview);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Extensions as ActionExtensions, IWorkbenchActionRegistry } from 'vs/wor
import { Extensions as EditorInputExtensions, IEditorInputFactoryRegistry } from 'vs/workbench/common/editor';
import { WebviewEditorInputFactory } from 'vs/workbench/parts/webview/electron-browser/webviewEditorInputFactory';
import { KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_VISIBLE } from './baseWebviewEditor';
import { HideWebViewEditorFindCommand, OpenWebviewDeveloperToolsAction, ReloadWebviewAction, ShowWebViewEditorFindWidgetCommand, SelectAllWebviewEditorCommand } from './webviewCommands';
import { HideWebViewEditorFindCommand, OpenWebviewDeveloperToolsAction, ReloadWebviewAction, ShowWebViewEditorFindWidgetCommand, SelectAllWebviewEditorCommand, CopyWebviewEditorCommand, PasteWebviewEditorCommand, CutWebviewEditorCommand, UndoWebviewEditorCommand, RedoWebviewEditorCommand } from './webviewCommands';
import { WebviewEditor } from './webviewEditor';
import { WebviewEditorInput } from './webviewEditorInput';
import { IWebviewEditorService, WebviewEditorService } from './webviewEditorService';
Expand Down Expand Up @@ -52,7 +52,7 @@ export function registerWebViewCommands(editorId: string): void {
});
showNextFindWidgetCommand.register();

const hideCommand = new HideWebViewEditorFindCommand({
(new HideWebViewEditorFindCommand({
id: HideWebViewEditorFindCommand.ID,
precondition: ContextKeyExpr.and(
contextKeyExpr,
Expand All @@ -61,18 +61,64 @@ export function registerWebViewCommands(editorId: string): void {
primary: KeyCode.Escape,
weight: KeybindingWeight.EditorContrib
}
});
hideCommand.register();
})).register();

const selectAllCommand = new SelectAllWebviewEditorCommand({
(new SelectAllWebviewEditorCommand({
id: SelectAllWebviewEditorCommand.ID,
precondition: ContextKeyExpr.and(contextKeyExpr, ContextKeyExpr.not(InputFocusedContextKey)),
kbOpts: {
primary: KeyMod.CtrlCmd | KeyCode.KEY_A,
weight: KeybindingWeight.EditorContrib
}
});
selectAllCommand.register();
})).register();

(new CopyWebviewEditorCommand({
id: CopyWebviewEditorCommand.ID,
precondition: ContextKeyExpr.and(contextKeyExpr, ContextKeyExpr.not(InputFocusedContextKey)),
kbOpts: {
primary: KeyMod.CtrlCmd | KeyCode.KEY_C,
weight: KeybindingWeight.EditorContrib
}
})).register();

(new PasteWebviewEditorCommand({
id: PasteWebviewEditorCommand.ID,
precondition: ContextKeyExpr.and(contextKeyExpr, ContextKeyExpr.not(InputFocusedContextKey)),
kbOpts: {
primary: KeyMod.CtrlCmd | KeyCode.KEY_V,
weight: KeybindingWeight.EditorContrib
}
})).register();


(new CutWebviewEditorCommand({
id: CutWebviewEditorCommand.ID,
precondition: ContextKeyExpr.and(contextKeyExpr, ContextKeyExpr.not(InputFocusedContextKey)),
kbOpts: {
primary: KeyMod.CtrlCmd | KeyCode.KEY_X,
weight: KeybindingWeight.EditorContrib
}
})).register();

(new UndoWebviewEditorCommand({
id: UndoWebviewEditorCommand.ID,
precondition: ContextKeyExpr.and(contextKeyExpr, ContextKeyExpr.not(InputFocusedContextKey)),
kbOpts: {
primary: KeyMod.CtrlCmd | KeyCode.KEY_Z,
weight: KeybindingWeight.EditorContrib
}
})).register();

(new RedoWebviewEditorCommand({
id: RedoWebviewEditorCommand.ID,
precondition: ContextKeyExpr.and(contextKeyExpr, ContextKeyExpr.not(InputFocusedContextKey)),
kbOpts: {
primary: KeyMod.CtrlCmd | KeyCode.KEY_Y,
secondary: [KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_Z],
mac: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_Z },
weight: KeybindingWeight.EditorContrib
}
})).register();
}

registerWebViewCommands(WebviewEditor.ID);
Expand Down
55 changes: 55 additions & 0 deletions src/vs/workbench/parts/webview/electron-browser/webviewCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,61 @@ export class SelectAllWebviewEditorCommand extends Command {
}
}

export class CopyWebviewEditorCommand extends Command {
public static readonly ID = 'editor.action.webvieweditor.copy';

public runCommand(accessor: ServicesAccessor, args: any): void {
const webViewEditor = getActiveWebviewEditor(accessor);
if (webViewEditor) {
webViewEditor.copy();
}
}
}

export class PasteWebviewEditorCommand extends Command {
public static readonly ID = 'editor.action.webvieweditor.paste';

public runCommand(accessor: ServicesAccessor, args: any): void {
const webViewEditor = getActiveWebviewEditor(accessor);
if (webViewEditor) {
webViewEditor.paste();
}
}
}

export class CutWebviewEditorCommand extends Command {
public static readonly ID = 'editor.action.webvieweditor.cut';

public runCommand(accessor: ServicesAccessor, args: any): void {
const webViewEditor = getActiveWebviewEditor(accessor);
if (webViewEditor) {
webViewEditor.cut();
}
}
}

export class UndoWebviewEditorCommand extends Command {
public static readonly ID = 'editor.action.webvieweditor.undo';

public runCommand(accessor: ServicesAccessor, args: any): void {
const webViewEditor = getActiveWebviewEditor(accessor);
if (webViewEditor) {
webViewEditor.undo();
}
}
}

export class RedoWebviewEditorCommand extends Command {
public static readonly ID = 'editor.action.webvieweditor.redo';

public runCommand(accessor: ServicesAccessor, args: any): void {
const webViewEditor = getActiveWebviewEditor(accessor);
if (webViewEditor) {
webViewEditor.redo();
}
}
}

export class OpenWebviewDeveloperToolsAction extends Action {
static readonly ID = 'workbench.action.webview.openDeveloperTools';
static readonly LABEL = nls.localize('openToolsLabel', "Open Webview Developer Tools");
Expand Down
20 changes: 20 additions & 0 deletions src/vs/workbench/parts/webview/electron-browser/webviewElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,26 @@ export class WebviewElement extends Disposable {
public selectAll() {
this._webview.selectAll();
}

public copy() {
this._webview.copy();
}

public paste() {
this._webview.paste();
}

public cut() {
this._webview.cut();
}

public undo() {
this._webview.undo();
}

public redo() {
this._webview.redo();
}
}


Expand Down

0 comments on commit 61efc1d

Please sign in to comment.