Adopting EditorAction2

This commit is contained in:
Alex Dima 2016-08-04 09:00:53 +02:00
parent bd891bda79
commit 093290327a
3 changed files with 127 additions and 128 deletions

View file

@ -195,15 +195,14 @@ class EditorContributionRegistry {
public registerEditorAction2(action:EditorAction2) {
if (action.menuOpts) {
let {menu, kbExpr, group, order} = action.menuOpts;
MenuRegistry.appendMenuItem(menu || MenuId.EditorContext, {
MenuRegistry.appendMenuItem(action.menuOpts.menu || MenuId.EditorContext, {
command: {
id: action.id,
title: action.label
},
when: kbExpr,
group,
order
when: action.menuOpts.kbExpr,
group: action.menuOpts.group,
order: action.menuOpts.order
});
}
@ -215,7 +214,7 @@ class EditorContributionRegistry {
let commandDesc: ICommandDescriptor = {
id: action.id,
handler: triggerEditorActionGlobal.bind(null, action.id),
handler: action.kbOpts.commandHandler || triggerEditorActionGlobal.bind(null, action.id),
weight: KeybindingsRegistry.WEIGHT.editorContrib(),
when: action.kbOpts.kbExpr,
primary: action.kbOpts.primary,
@ -340,6 +339,7 @@ function createCommandHandler(commandId: string, handler: IEditorCommandHandler)
export interface IEditorActionKeybindingOptions2 extends IKeybindings {
kbExpr?: KbExpr;
commandHandler?: ICommandHandler;
}
export abstract class EditorAction2 {

View file

@ -10,7 +10,6 @@ import * as nls from 'vs/nls';
import {KeyCode, KeyMod} from 'vs/base/common/keyCodes';
import {Disposable} from 'vs/base/common/lifecycle';
import * as strings from 'vs/base/common/strings';
import {TPromise} from 'vs/base/common/winjs.base';
import {clearNode} from 'vs/base/browser/dom';
import {renderHtml} from 'vs/base/browser/htmlContentRenderer';
import {StyleMutator} from 'vs/base/browser/styleMutator';
@ -19,15 +18,12 @@ import {ServicesAccessor} from 'vs/platform/instantiation/common/instantiation';
import {IKeybindingContextKey, IKeybindingService} from 'vs/platform/keybinding/common/keybinding';
import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegistry';
import {GlobalScreenReaderNVDA} from 'vs/editor/common/config/commonEditorConfig';
import {EditorAction} from 'vs/editor/common/editorAction';
import {Behaviour} from 'vs/editor/common/editorActionEnablement';
import {ICommonCodeEditor, IEditorActionDescriptorData, IEditorContribution, SHOW_ACCESSIBILITY_HELP_ACTION_ID} from 'vs/editor/common/editorCommon';
import {CommonEditorRegistry, ContextKey, EditorActionDescriptor} from 'vs/editor/common/editorCommonExtensions';
import {ICommonCodeEditor, IEditorContribution, SHOW_ACCESSIBILITY_HELP_ACTION_ID} from 'vs/editor/common/editorCommon';
import {CommonEditorRegistry, EditorKbExpr, EditorAction2} from 'vs/editor/common/editorCommonExtensions';
import {ICodeEditor, IOverlayWidget, IOverlayWidgetPosition} from 'vs/editor/browser/editorBrowser';
import {EditorBrowserRegistry} from 'vs/editor/browser/editorBrowserExtensions';
import {ToggleTabFocusModeAction} from 'vs/editor/contrib/toggleTabFocusMode/common/toggleTabFocusMode';
const NLS_SHOW_ACCESSIBILITY_HELP_ACTION_LABEL = nls.localize('ShowAccessibilityHelpAction',"Show Accessibility Help");
const CONTEXT_ACCESSIBILITY_WIDGET_VISIBLE = 'accessibilityHelpWidgetVisible';
const TOGGLE_EXPERIMENTAL_SCREEN_READER_SUPPORT_COMMAND_ID = 'toggleExperimentalScreenReaderSupport';
@ -193,24 +189,30 @@ class AccessibilityHelpWidget extends Widget implements IOverlayWidget {
}
}
class ShowAccessibilityHelpAction extends EditorAction {
class ShowAccessibilityHelpAction extends EditorAction2 {
constructor(descriptor:IEditorActionDescriptorData, editor:ICommonCodeEditor) {
super(descriptor, editor, Behaviour.WidgetFocus);
constructor() {
super(
SHOW_ACCESSIBILITY_HELP_ACTION_ID,
nls.localize('ShowAccessibilityHelpAction',"Show Accessibility Help"),
'Show Accessibility Help',
false
);
this.kbOpts = {
kbExpr: EditorKbExpr.Focus,
primary: KeyMod.Alt | KeyCode.F1
};
}
public run(): TPromise<boolean> {
let controller = AccessibilityHelpController.get(this.editor);
public run(accessor:ServicesAccessor, editor:ICommonCodeEditor): void {
let controller = AccessibilityHelpController.get(editor);
controller.show();
return TPromise.as(true);
}
}
EditorBrowserRegistry.registerEditorContribution(AccessibilityHelpController);
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(ShowAccessibilityHelpAction, SHOW_ACCESSIBILITY_HELP_ACTION_ID, NLS_SHOW_ACCESSIBILITY_HELP_ACTION_LABEL, {
context: ContextKey.EditorFocus,
primary: KeyMod.Alt | KeyCode.F1
}, 'Show Accessibility Help'));
CommonEditorRegistry.registerEditorAction2(new ShowAccessibilityHelpAction());
CommonEditorRegistry.registerEditorCommand('closeAccessibilityHelp', CommonEditorRegistry.commandWeight(100), { primary: KeyCode.Escape, secondary: [KeyMod.Shift | KeyCode.Escape] }, false, CONTEXT_ACCESSIBILITY_WIDGET_VISIBLE, (ctx, editor, args) => {
AccessibilityHelpController.get(editor).hide();
});

View file

@ -8,168 +8,165 @@
import 'vs/css!./clipboard';
import * as nls from 'vs/nls';
import {KeyCode, KeyMod} from 'vs/base/common/keyCodes';
import {dispose, IDisposable} from 'vs/base/common/lifecycle';
import {TPromise} from 'vs/base/common/winjs.base';
import * as browser from 'vs/base/browser/browser';
import {ServicesAccessor} from 'vs/platform/instantiation/common/instantiation';
import {IKeybindings, KbExpr} from 'vs/platform/keybinding/common/keybinding';
import {KbExpr} from 'vs/platform/keybinding/common/keybinding';
import {findFocusedEditor} from 'vs/editor/common/config/config';
import {EditorAction} from 'vs/editor/common/editorAction';
import {Behaviour} from 'vs/editor/common/editorActionEnablement';
import * as editorCommon from 'vs/editor/common/editorCommon';
import {CommonEditorRegistry, ContextKey, EditorActionDescriptor} from 'vs/editor/common/editorCommonExtensions';
import {MenuId, MenuRegistry} from 'vs/platform/actions/common/actions';
import {EditorKbExpr, EditorAction2, CommonEditorRegistry} from 'vs/editor/common/editorCommonExtensions';
import {MenuId} from 'vs/platform/actions/common/actions';
class ClipboardWritingAction extends EditorAction {
const CLIPBOARD_CONTEXT_MENU_GROUP = '9_cutcopypaste';
private toUnhook:IDisposable[];
abstract class ClipboardWritingAction extends EditorAction2 {
constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor, condition:Behaviour) {
super(descriptor, editor, condition);
this.toUnhook = [];
this.toUnhook.push(this.editor.onDidChangeCursorSelection((e:editorCommon.ICursorSelectionChangedEvent) => {
this.resetEnablementState();
}));
constructor(id:string, label:string, alias:string, needsWritableEditor:boolean) {
super(id, label, alias, needsWritableEditor);
}
public dispose(): void {
this.toUnhook = dispose(this.toUnhook);
super.dispose();
}
public enabled(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): boolean {
if (!super.enabled(accessor, editor)) {
return false;
}
public getEnablementState(): boolean {
if (browser.enableEmptySelectionClipboard) {
return true;
} else {
return !this.editor.getSelection().isEmpty();
return !editor.getSelection().isEmpty();
}
}
}
function editorCursorIsInEditableRange(editor:editorCommon.ICommonCodeEditor): boolean {
var model = editor.getModel();
let model = editor.getModel();
if (!model) {
return false;
}
var hasEditableRange = model.hasEditableRange();
let hasEditableRange = model.hasEditableRange();
if (!hasEditableRange) {
return true;
}
var editableRange = model.getEditableRange();
var editorPosition = editor.getPosition();
let editableRange = model.getEditableRange();
let editorPosition = editor.getPosition();
return editableRange.containsPosition(editorPosition);
}
class ExecCommandCutAction extends ClipboardWritingAction {
constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
super(descriptor, editor, Behaviour.Writeable | Behaviour.WidgetFocus | Behaviour.UpdateOnCursorPositionChange);
constructor() {
super(
'editor.action.clipboardCutAction',
nls.localize('actions.clipboard.cutLabel', "Cut"),
'Cut',
true
);
this.kbOpts = {
commandHandler: execCommandToHandler.bind(null, this.id, 'cut'),
kbExpr: KbExpr.and(EditorKbExpr.Focus, EditorKbExpr.Writable),
primary: KeyMod.CtrlCmd | KeyCode.KEY_X,
win: { primary: KeyMod.CtrlCmd | KeyCode.KEY_X, secondary: [KeyMod.Shift | KeyCode.Delete] }
};
this.menuOpts = {
kbExpr: EditorKbExpr.Writable,
menu: MenuId.EditorContext,
group: CLIPBOARD_CONTEXT_MENU_GROUP,
order: 1
};
}
public getEnablementState(): boolean {
return super.getEnablementState() && editorCursorIsInEditableRange(this.editor);
public enabled(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): boolean {
if (!super.enabled(accessor, editor)) {
return false;
}
return editorCursorIsInEditableRange(editor);
}
public run(): TPromise<boolean> {
this.editor.focus();
public run(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): void {
editor.focus();
document.execCommand('cut');
return TPromise.as(true);
}
}
class ExecCommandCopyAction extends ClipboardWritingAction {
constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
super(descriptor, editor, Behaviour.WidgetFocus);
constructor() {
super(
'editor.action.clipboardCopyAction',
nls.localize('actions.clipboard.copyLabel', "Copy"),
'Copy',
false
);
this.kbOpts = {
commandHandler: execCommandToHandler.bind(null, this.id, 'copy'),
kbExpr: EditorKbExpr.Focus,
primary: KeyMod.CtrlCmd | KeyCode.KEY_C,
win: { primary: KeyMod.CtrlCmd | KeyCode.KEY_C, secondary: [KeyMod.CtrlCmd | KeyCode.Insert] }
};
this.menuOpts = {
kbExpr: null,
menu: MenuId.EditorContext,
group: CLIPBOARD_CONTEXT_MENU_GROUP,
order: 2
};
}
public run(): TPromise<boolean> {
this.editor.focus();
public run(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): void {
editor.focus();
document.execCommand('copy');
return TPromise.as(true);
}
}
class ExecCommandPasteAction extends EditorAction {
class ExecCommandPasteAction extends EditorAction2 {
constructor(descriptor:editorCommon.IEditorActionDescriptorData, editor:editorCommon.ICommonCodeEditor) {
super(descriptor, editor, Behaviour.Writeable | Behaviour.WidgetFocus | Behaviour.UpdateOnCursorPositionChange);
constructor() {
super(
'editor.action.clipboardPasteAction',
nls.localize('actions.clipboard.pasteLabel', "Paste"),
'Paste',
true
);
this.kbOpts = {
commandHandler: execCommandToHandler.bind(null, this.id, 'paste'),
kbExpr: KbExpr.and(EditorKbExpr.Focus, EditorKbExpr.Writable),
primary: KeyMod.CtrlCmd | KeyCode.KEY_V,
win: { primary: KeyMod.CtrlCmd | KeyCode.KEY_V, secondary: [KeyMod.Shift | KeyCode.Insert] }
};
this.menuOpts = {
kbExpr: EditorKbExpr.Writable,
menu: MenuId.EditorContext,
group: CLIPBOARD_CONTEXT_MENU_GROUP,
order: 3
};
}
public getEnablementState(): boolean {
return editorCursorIsInEditableRange(this.editor);
public enabled(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): boolean {
if (!super.enabled(accessor, editor)) {
return false;
}
return editorCursorIsInEditableRange(editor);
}
public run(): TPromise<boolean> {
this.editor.focus();
public run(accessor:ServicesAccessor, editor:editorCommon.ICommonCodeEditor): void {
editor.focus();
document.execCommand('paste');
return null;
}
}
interface IClipboardCommand extends IKeybindings {
ctor: editorCommon.IEditorActionContributionCtor;
id: string;
label: string;
execCommand: string;
kbExpr: KbExpr;
if (browser.supportsExecCommand('cut')) {
CommonEditorRegistry.registerEditorAction2(new ExecCommandCutAction());
}
function registerClipboardAction(desc: IClipboardCommand, alias: string, weight: number) {
if (!browser.supportsExecCommand(desc.execCommand)) {
return;
}
CommonEditorRegistry.registerEditorAction(new EditorActionDescriptor(desc.ctor, desc.id, desc.label, {
handler: execCommandToHandler.bind(null, desc.id, desc.execCommand),
context: ContextKey.None,
primary: desc.primary,
secondary: desc.secondary,
win: desc.win,
linux: desc.linux,
mac: desc.mac,
kbExpr: KbExpr.has(editorCommon.KEYBINDING_CONTEXT_EDITOR_TEXT_FOCUS)
}, alias));
MenuRegistry.appendMenuItem(MenuId.EditorContext, {
command: {
id: desc.id,
title: desc.label
},
group: `9_cutcopypaste`,
order: weight,
when: desc.kbExpr
});
if (browser.supportsExecCommand('copy')) {
CommonEditorRegistry.registerEditorAction2(new ExecCommandCopyAction());
}
if (browser.supportsExecCommand('paste')) {
CommonEditorRegistry.registerEditorAction2(new ExecCommandPasteAction());
}
registerClipboardAction({
ctor: ExecCommandCutAction,
id: 'editor.action.clipboardCutAction',
label: nls.localize('actions.clipboard.cutLabel', "Cut"),
execCommand: 'cut',
primary: KeyMod.CtrlCmd | KeyCode.KEY_X,
win: { primary: KeyMod.CtrlCmd | KeyCode.KEY_X, secondary: [KeyMod.Shift | KeyCode.Delete] },
kbExpr: KbExpr.and(KbExpr.has(editorCommon.KEYBINDING_CONTEXT_EDITOR_FOCUS), KbExpr.not(editorCommon.KEYBINDING_CONTEXT_EDITOR_READONLY))
}, 'Cut', 1);
registerClipboardAction({
ctor: ExecCommandCopyAction,
id: 'editor.action.clipboardCopyAction',
label: nls.localize('actions.clipboard.copyLabel', "Copy"),
execCommand: 'copy',
primary: KeyMod.CtrlCmd | KeyCode.KEY_C,
win: { primary: KeyMod.CtrlCmd | KeyCode.KEY_C, secondary: [KeyMod.CtrlCmd | KeyCode.Insert] },
kbExpr: KbExpr.has(editorCommon.KEYBINDING_CONTEXT_EDITOR_FOCUS)
}, 'Copy', 2);
registerClipboardAction({
ctor: ExecCommandPasteAction,
id: 'editor.action.clipboardPasteAction',
label: nls.localize('actions.clipboard.pasteLabel', "Paste"),
execCommand: 'paste',
primary: KeyMod.CtrlCmd | KeyCode.KEY_V,
win: { primary: KeyMod.CtrlCmd | KeyCode.KEY_V, secondary: [KeyMod.Shift | KeyCode.Insert] },
kbExpr: KbExpr.and(KbExpr.has(editorCommon.KEYBINDING_CONTEXT_EDITOR_FOCUS), KbExpr.not(editorCommon.KEYBINDING_CONTEXT_EDITOR_READONLY))
}, 'Paste', 3);
function execCommandToHandler(actionId: string, browserCommand: string, accessor: ServicesAccessor, args: any): void {
let focusedEditor = findFocusedEditor(actionId, accessor, false);