diff --git a/src/vs/workbench/contrib/webview/browser/webview.contribution.ts b/src/vs/workbench/contrib/webview/browser/webview.contribution.ts index 32f91cd0ec9..6a60aba8625 100644 --- a/src/vs/workbench/contrib/webview/browser/webview.contribution.ts +++ b/src/vs/workbench/contrib/webview/browser/webview.contribution.ts @@ -18,7 +18,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 { CopyWebviewEditorCommand, CutWebviewEditorCommand, HideWebViewEditorFindCommand, PasteWebviewEditorCommand, RedoWebviewEditorCommand, ReloadWebviewAction, SelectAllWebviewEditorCommand, ShowWebViewEditorFindWidgetCommand, UndoWebviewEditorCommand } from '../browser/webviewCommands'; +import { HideWebViewEditorFindCommand, RedoWebviewEditorCommand, ReloadWebviewAction, SelectAllWebviewEditorCommand, ShowWebViewEditorFindWidgetCommand, UndoWebviewEditorCommand } from '../browser/webviewCommands'; import { WebviewEditor } from '../browser/webviewEditor'; import { WebviewEditorInput } from '../browser/webviewEditorInput'; import { IWebviewEditorService, WebviewEditorService } from '../browser/webviewEditorService'; @@ -72,34 +72,6 @@ export function registerWebViewCommands(editorId: string): void { // These commands are only needed on MacOS where we have to disable the menu bar commands if (isMacintosh) { - (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)), diff --git a/src/vs/workbench/contrib/webview/browser/webviewCommands.ts b/src/vs/workbench/contrib/webview/browser/webviewCommands.ts index dbf38d80b79..0273e0b7313 100644 --- a/src/vs/workbench/contrib/webview/browser/webviewCommands.ts +++ b/src/vs/workbench/contrib/webview/browser/webviewCommands.ts @@ -43,39 +43,6 @@ 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'; diff --git a/src/vs/workbench/contrib/webview/browser/webviewEditor.ts b/src/vs/workbench/contrib/webview/browser/webviewEditor.ts index 7c5730e4457..037eec0e325 100644 --- a/src/vs/workbench/contrib/webview/browser/webviewEditor.ts +++ b/src/vs/workbench/contrib/webview/browser/webviewEditor.ts @@ -146,18 +146,6 @@ export class WebviewEditor extends BaseEditor { this.withWebview(webview => webview.selectAll()); } - public copy(): void { - this.withWebview(webview => webview.copy()); - } - - public paste(): void { - this.withWebview(webview => webview.paste()); - } - - public cut(): void { - this.withWebview(webview => webview.cut()); - } - public undo(): void { this.withWebview(webview => webview.undo()); } @@ -166,7 +154,7 @@ export class WebviewEditor extends BaseEditor { this.withWebview(webview => webview.redo()); } - private withWebview(f: (element: Webview) => void): void { + public withWebview(f: (element: Webview) => void): void { if (this._webview) { f(this._webview); } diff --git a/src/vs/workbench/contrib/webview/common/webview.ts b/src/vs/workbench/contrib/webview/common/webview.ts index ca427d11f57..0ef2fe6c70c 100644 --- a/src/vs/workbench/contrib/webview/common/webview.ts +++ b/src/vs/workbench/contrib/webview/common/webview.ts @@ -75,9 +75,6 @@ export interface Webview extends IDisposable { reload(): void; selectAll(): void; - copy(): void; - paste(): void; - cut(): void; undo(): void; redo(): 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 cd912708989..0a65e8a9267 100644 --- a/src/vs/workbench/contrib/webview/electron-browser/webview.contribution.ts +++ b/src/vs/workbench/contrib/webview/electron-browser/webview.contribution.ts @@ -3,13 +3,19 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ +import { KeyCode, KeyMod } from 'vs/base/common/keyCodes'; +import { isMacintosh } from 'vs/base/common/platform'; 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 { registerSingleton } from 'vs/platform/instantiation/common/extensions'; +import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry'; 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 { WebviewService } from 'vs/workbench/contrib/webview/electron-browser/webviewService'; -import { OpenWebviewDeveloperToolsAction } from 'vs/workbench/contrib/webview/electron-browser/webviewCommands'; registerSingleton(IWebviewService, WebviewService, true); @@ -19,3 +25,39 @@ actionRegistry.registerWorkbenchAction( new SyncActionDescriptor(OpenWebviewDeveloperToolsAction, OpenWebviewDeveloperToolsAction.ID, OpenWebviewDeveloperToolsAction.LABEL), 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 */); + + // These commands are only needed on MacOS where we have to disable the menu bar commands + if (isMacintosh) { + (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(); + } +} + +registerWebViewCommands(WebviewEditor.ID); \ No newline at end of file diff --git a/src/vs/workbench/contrib/webview/electron-browser/webviewCommands.ts b/src/vs/workbench/contrib/webview/electron-browser/webviewCommands.ts index e239b767855..97fc3ac7501 100644 --- a/src/vs/workbench/contrib/webview/electron-browser/webviewCommands.ts +++ b/src/vs/workbench/contrib/webview/electron-browser/webviewCommands.ts @@ -5,6 +5,10 @@ import { Action } from 'vs/base/common/actions'; import * as nls from 'vs/nls'; +import { Command, ServicesAccessor } from 'vs/editor/browser/editorExtensions'; +import { WebviewEditor } from 'vs/workbench/contrib/webview/browser/webviewEditor'; +import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; +import { WebviewElement } from 'vs/workbench/contrib/webview/electron-browser/webviewElement'; export class OpenWebviewDeveloperToolsAction extends Action { static readonly ID = 'workbench.action.webview.openDeveloperTools'; @@ -27,3 +31,44 @@ export class OpenWebviewDeveloperToolsAction extends Action { return Promise.resolve(true); } } + +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()); + } +} + +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()); + } +} + +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()); + } +} + +function getActiveWebviewEditor(accessor: ServicesAccessor): WebviewEditor | undefined { + const editorService = accessor.get(IEditorService); + const activeControl = editorService.activeControl as WebviewEditor; + return activeControl.isWebviewEditor ? activeControl : undefined; +} + +function withActiveWebviewBasedWebview(accessor: ServicesAccessor, f: (webview: WebviewElement) => void): void { + const webViewEditor = getActiveWebviewEditor(accessor); + if (webViewEditor) { + webViewEditor.withWebview(webview => { + if (webview instanceof WebviewElement) { + f(webview); + } + }); + } +} \ No newline at end of file