From 5caddbe2626ffb8d2b876d67d8dbec3ece2fbded Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Fri, 28 Jun 2019 15:08:38 -0700 Subject: [PATCH] Move undo/redo and select all to be electron specific webview elements as well --- .../webview/browser/webview.contribution.ts | 38 +-------------- .../webview/browser/webviewCommands.ts | 33 ------------- .../contrib/webview/browser/webviewEditor.ts | 14 +----- .../contrib/webview/browser/webviewElement.ts | 19 +------- .../contrib/webview/common/webview.ts | 4 -- .../electron-browser/webview.contribution.ts | 47 +++++++++++++++---- .../electron-browser/webviewCommands.ts | 30 ++++++++++-- 7 files changed, 69 insertions(+), 116 deletions(-) diff --git a/src/vs/workbench/contrib/webview/browser/webview.contribution.ts b/src/vs/workbench/contrib/webview/browser/webview.contribution.ts index 6a60aba8625..40dac14b615 100644 --- a/src/vs/workbench/contrib/webview/browser/webview.contribution.ts +++ b/src/vs/workbench/contrib/webview/browser/webview.contribution.ts @@ -4,11 +4,9 @@ *--------------------------------------------------------------------------------------------*/ import { KeyCode, KeyMod } from 'vs/base/common/keyCodes'; -import { isMacintosh } from 'vs/base/common/platform'; import { localize } from 'vs/nls'; import { SyncActionDescriptor } from 'vs/platform/actions/common/actions'; import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; -import { InputFocusedContextKey } from 'vs/platform/contextkey/common/contextkeys'; import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors'; import { registerSingleton } from 'vs/platform/instantiation/common/extensions'; import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry'; @@ -18,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/contrib/webview/browser/webviewEditorInputFactory'; import { KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_VISIBLE, webviewDeveloperCategory } from 'vs/workbench/contrib/webview/common/webview'; -import { HideWebViewEditorFindCommand, RedoWebviewEditorCommand, ReloadWebviewAction, SelectAllWebviewEditorCommand, ShowWebViewEditorFindWidgetCommand, UndoWebviewEditorCommand } from '../browser/webviewCommands'; +import { HideWebViewEditorFindCommand, ReloadWebviewAction, ShowWebViewEditorFindWidgetCommand } from '../browser/webviewCommands'; import { WebviewEditor } from '../browser/webviewEditor'; import { WebviewEditorInput } from '../browser/webviewEditorInput'; import { IWebviewEditorService, WebviewEditorService } from '../browser/webviewEditorService'; @@ -37,7 +35,7 @@ registerSingleton(IWebviewEditorService, WebviewEditorService, true); const actionRegistry = Registry.as(ActionExtensions.WorkbenchActions); -export function registerWebViewCommands(editorId: string): void { +function registerWebViewCommands(editorId: string): void { const contextKeyExpr = ContextKeyExpr.and(ContextKeyExpr.equals('activeEditor', editorId), ContextKeyExpr.not('editorFocus') /* https://github.com/Microsoft/vscode/issues/58668 */); const showNextFindWidgetCommand = new ShowWebViewEditorFindWidgetCommand({ @@ -60,38 +58,6 @@ export function registerWebViewCommands(editorId: string): void { weight: KeybindingWeight.EditorContrib } })).register(); - - (new SelectAllWebviewEditorCommand({ - id: SelectAllWebviewEditorCommand.ID, - precondition: ContextKeyExpr.and(contextKeyExpr, ContextKeyExpr.not(InputFocusedContextKey)), - kbOpts: { - primary: KeyMod.CtrlCmd | KeyCode.KEY_A, - weight: KeybindingWeight.EditorContrib - } - })).register(); - - // These commands are only needed on MacOS where we have to disable the menu bar commands - if (isMacintosh) { - (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); diff --git a/src/vs/workbench/contrib/webview/browser/webviewCommands.ts b/src/vs/workbench/contrib/webview/browser/webviewCommands.ts index 0273e0b7313..a5888e3e668 100644 --- a/src/vs/workbench/contrib/webview/browser/webviewCommands.ts +++ b/src/vs/workbench/contrib/webview/browser/webviewCommands.ts @@ -32,39 +32,6 @@ export class HideWebViewEditorFindCommand extends Command { } } -export class SelectAllWebviewEditorCommand extends Command { - public static readonly ID = 'editor.action.webvieweditor.selectAll'; - - public runCommand(accessor: ServicesAccessor, args: any): void { - const webViewEditor = getActiveWebviewEditor(accessor); - if (webViewEditor) { - webViewEditor.selectAll(); - } - } -} - -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 ReloadWebviewAction extends Action { static readonly ID = 'workbench.action.webview.reloadWebviewAction'; static readonly LABEL = nls.localize('refreshWebviewLabel', "Reload Webviews"); diff --git a/src/vs/workbench/contrib/webview/browser/webviewEditor.ts b/src/vs/workbench/contrib/webview/browser/webviewEditor.ts index 037eec0e325..9c4bb48b1df 100644 --- a/src/vs/workbench/contrib/webview/browser/webviewEditor.ts +++ b/src/vs/workbench/contrib/webview/browser/webviewEditor.ts @@ -17,7 +17,7 @@ import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace import { BaseEditor } from 'vs/workbench/browser/parts/editor/baseEditor'; import { EditorOptions } from 'vs/workbench/common/editor'; import { WebviewEditorInput } from 'vs/workbench/contrib/webview/browser/webviewEditorInput'; -import { IWebviewService, Webview, KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_VISIBLE } from 'vs/workbench/contrib/webview/common/webview'; +import { IWebviewService, KEYBINDING_CONTEXT_WEBVIEW_FIND_WIDGET_VISIBLE, Webview } from 'vs/workbench/contrib/webview/common/webview'; import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService'; import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; @@ -142,18 +142,6 @@ export class WebviewEditor extends BaseEditor { this.withWebview(webview => webview.focus()); } - public selectAll(): void { - this.withWebview(webview => webview.selectAll()); - } - - public undo(): void { - this.withWebview(webview => webview.undo()); - } - - public redo(): void { - this.withWebview(webview => webview.redo()); - } - public withWebview(f: (element: Webview) => void): void { if (this._webview) { f(this._webview); diff --git a/src/vs/workbench/contrib/webview/browser/webviewElement.ts b/src/vs/workbench/contrib/webview/browser/webviewElement.ts index 0c57649c8b6..ed7e22ab84d 100644 --- a/src/vs/workbench/contrib/webview/browser/webviewElement.ts +++ b/src/vs/workbench/contrib/webview/browser/webviewElement.ts @@ -250,27 +250,10 @@ export class IFrameWebview extends Disposable implements Webview { this.doUpdateContent(); } - selectAll(): void { - throw new Error('Method not implemented.'); - } - copy(): void { - throw new Error('Method not implemented.'); - } - paste(): void { - throw new Error('Method not implemented.'); - } - cut(): void { - throw new Error('Method not implemented.'); - } - undo(): void { - throw new Error('Method not implemented.'); - } - redo(): void { - throw new Error('Method not implemented.'); - } showFind(): void { throw new Error('Method not implemented.'); } + hideFind(): void { throw new Error('Method not implemented.'); } diff --git a/src/vs/workbench/contrib/webview/common/webview.ts b/src/vs/workbench/contrib/webview/common/webview.ts index 0ef2fe6c70c..bf459a3c694 100644 --- a/src/vs/workbench/contrib/webview/common/webview.ts +++ b/src/vs/workbench/contrib/webview/common/webview.ts @@ -72,11 +72,7 @@ export interface Webview extends IDisposable { layout(): void; mountTo(parent: HTMLElement): void; focus(): void; - reload(): void; - selectAll(): void; - undo(): void; - redo(): void; showFind(): void; hideFind(): void; diff --git a/src/vs/workbench/contrib/webview/electron-browser/webview.contribution.ts b/src/vs/workbench/contrib/webview/electron-browser/webview.contribution.ts index 0a65e8a9267..13b7185842a 100644 --- a/src/vs/workbench/contrib/webview/electron-browser/webview.contribution.ts +++ b/src/vs/workbench/contrib/webview/electron-browser/webview.contribution.ts @@ -14,7 +14,7 @@ import { Registry } from 'vs/platform/registry/common/platform'; import { Extensions as ActionExtensions, IWorkbenchActionRegistry } from 'vs/workbench/common/actions'; import { WebviewEditor } from 'vs/workbench/contrib/webview/browser/webviewEditor'; import { IWebviewService, webviewDeveloperCategory } from 'vs/workbench/contrib/webview/common/webview'; -import { CopyWebviewEditorCommand, CutWebviewEditorCommand, OpenWebviewDeveloperToolsAction, PasteWebviewEditorCommand } from 'vs/workbench/contrib/webview/electron-browser/webviewCommands'; +import * as webviewCommands from 'vs/workbench/contrib/webview/electron-browser/webviewCommands'; import { WebviewService } from 'vs/workbench/contrib/webview/electron-browser/webviewService'; registerSingleton(IWebviewService, WebviewService, true); @@ -22,17 +22,26 @@ registerSingleton(IWebviewService, WebviewService, true); const actionRegistry = Registry.as(ActionExtensions.WorkbenchActions); actionRegistry.registerWorkbenchAction( - new SyncActionDescriptor(OpenWebviewDeveloperToolsAction, OpenWebviewDeveloperToolsAction.ID, OpenWebviewDeveloperToolsAction.LABEL), - OpenWebviewDeveloperToolsAction.ALIAS, + new SyncActionDescriptor(webviewCommands.OpenWebviewDeveloperToolsAction, webviewCommands.OpenWebviewDeveloperToolsAction.ID, webviewCommands.OpenWebviewDeveloperToolsAction.LABEL), + webviewCommands.OpenWebviewDeveloperToolsAction.ALIAS, webviewDeveloperCategory); function registerWebViewCommands(editorId: string): void { const contextKeyExpr = ContextKeyExpr.and(ContextKeyExpr.equals('activeEditor', editorId), ContextKeyExpr.not('editorFocus') /* https://github.com/Microsoft/vscode/issues/58668 */); + (new webviewCommands.SelectAllWebviewEditorCommand({ + id: webviewCommands.SelectAllWebviewEditorCommand.ID, + precondition: ContextKeyExpr.and(contextKeyExpr, ContextKeyExpr.not(InputFocusedContextKey)), + kbOpts: { + primary: KeyMod.CtrlCmd | KeyCode.KEY_A, + weight: KeybindingWeight.EditorContrib + } + })).register(); + // These commands are only needed on MacOS where we have to disable the menu bar commands if (isMacintosh) { - (new CopyWebviewEditorCommand({ - id: CopyWebviewEditorCommand.ID, + (new webviewCommands.CopyWebviewEditorCommand({ + id: webviewCommands.CopyWebviewEditorCommand.ID, precondition: ContextKeyExpr.and(contextKeyExpr, ContextKeyExpr.not(InputFocusedContextKey)), kbOpts: { primary: KeyMod.CtrlCmd | KeyCode.KEY_C, @@ -40,8 +49,8 @@ function registerWebViewCommands(editorId: string): void { } })).register(); - (new PasteWebviewEditorCommand({ - id: PasteWebviewEditorCommand.ID, + (new webviewCommands.PasteWebviewEditorCommand({ + id: webviewCommands.PasteWebviewEditorCommand.ID, precondition: ContextKeyExpr.and(contextKeyExpr, ContextKeyExpr.not(InputFocusedContextKey)), kbOpts: { primary: KeyMod.CtrlCmd | KeyCode.KEY_V, @@ -49,14 +58,34 @@ function registerWebViewCommands(editorId: string): void { } })).register(); - (new CutWebviewEditorCommand({ - id: CutWebviewEditorCommand.ID, + (new webviewCommands.CutWebviewEditorCommand({ + id: webviewCommands.CutWebviewEditorCommand.ID, precondition: ContextKeyExpr.and(contextKeyExpr, ContextKeyExpr.not(InputFocusedContextKey)), kbOpts: { primary: KeyMod.CtrlCmd | KeyCode.KEY_X, weight: KeybindingWeight.EditorContrib } })).register(); + + (new webviewCommands.UndoWebviewEditorCommand({ + id: webviewCommands.UndoWebviewEditorCommand.ID, + precondition: ContextKeyExpr.and(contextKeyExpr, ContextKeyExpr.not(InputFocusedContextKey)), + kbOpts: { + primary: KeyMod.CtrlCmd | KeyCode.KEY_Z, + weight: KeybindingWeight.EditorContrib + } + })).register(); + + (new webviewCommands.RedoWebviewEditorCommand({ + id: webviewCommands.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(); } } diff --git a/src/vs/workbench/contrib/webview/electron-browser/webviewCommands.ts b/src/vs/workbench/contrib/webview/electron-browser/webviewCommands.ts index 97fc3ac7501..15f320ce9f1 100644 --- a/src/vs/workbench/contrib/webview/electron-browser/webviewCommands.ts +++ b/src/vs/workbench/contrib/webview/electron-browser/webviewCommands.ts @@ -32,11 +32,19 @@ export class OpenWebviewDeveloperToolsAction extends Action { } } +export class SelectAllWebviewEditorCommand extends Command { + public static readonly ID = 'editor.action.webvieweditor.selectAll'; + + public runCommand(accessor: ServicesAccessor, args: any): void { + withActiveWebviewBasedWebview(accessor, webview => webview.selectAll()); + } +} + export class CopyWebviewEditorCommand extends Command { public static readonly ID = 'editor.action.webvieweditor.copy'; public runCommand(accessor: ServicesAccessor, _args: any): void { - return withActiveWebviewBasedWebview(accessor, webview => webview.copy()); + withActiveWebviewBasedWebview(accessor, webview => webview.copy()); } } @@ -44,7 +52,7 @@ export class PasteWebviewEditorCommand extends Command { public static readonly ID = 'editor.action.webvieweditor.paste'; public runCommand(accessor: ServicesAccessor, _args: any): void { - return withActiveWebviewBasedWebview(accessor, webview => webview.paste()); + withActiveWebviewBasedWebview(accessor, webview => webview.paste()); } } @@ -52,7 +60,23 @@ export class CutWebviewEditorCommand extends Command { public static readonly ID = 'editor.action.webvieweditor.cut'; public runCommand(accessor: ServicesAccessor, _args: any): void { - return withActiveWebviewBasedWebview(accessor, webview => webview.cut()); + withActiveWebviewBasedWebview(accessor, webview => webview.cut()); + } +} + +export class UndoWebviewEditorCommand extends Command { + public static readonly ID = 'editor.action.webvieweditor.undo'; + + public runCommand(accessor: ServicesAccessor, args: any): void { + withActiveWebviewBasedWebview(accessor, webview => webview.undo()); + } +} + +export class RedoWebviewEditorCommand extends Command { + public static readonly ID = 'editor.action.webvieweditor.redo'; + + public runCommand(accessor: ServicesAccessor, args: any): void { + withActiveWebviewBasedWebview(accessor, webview => webview.redo()); } }