@@ -42,10 +42,10 @@ export interface ICommandKeybindingsOptions extends IKeybindings {
4242 kbExpr ?: ContextKeyExpr | null ;
4343 weight : number ;
4444}
45- export interface ICommandMenubarOptions {
45+ export interface ICommandMenuOptions {
4646 menuId : MenuId ;
47- group : string ;
48- order : number ;
47+ group ? : string ;
48+ order ? : number ;
4949 when ?: ContextKeyExpr ;
5050 title : string ;
5151}
@@ -54,36 +54,29 @@ export interface ICommandOptions {
5454 precondition : ContextKeyExpr | undefined ;
5555 kbOpts ?: ICommandKeybindingsOptions ;
5656 description ?: ICommandHandlerDescription ;
57- menubarOpts ?: ICommandMenubarOptions ;
57+ menuOpts ?: ICommandMenuOptions | ICommandMenuOptions [ ] ;
5858}
5959export abstract class Command {
6060 public readonly id : string ;
6161 public readonly precondition : ContextKeyExpr | undefined ;
6262 private readonly _kbOpts : ICommandKeybindingsOptions | undefined ;
63- private readonly _menubarOpts : ICommandMenubarOptions | undefined ;
63+ private readonly _menuOpts : ICommandMenuOptions | ICommandMenuOptions [ ] | undefined ;
6464 private readonly _description : ICommandHandlerDescription | undefined ;
6565
6666 constructor ( opts : ICommandOptions ) {
6767 this . id = opts . id ;
6868 this . precondition = opts . precondition ;
6969 this . _kbOpts = opts . kbOpts ;
70- this . _menubarOpts = opts . menubarOpts ;
70+ this . _menuOpts = opts . menuOpts ;
7171 this . _description = opts . description ;
7272 }
7373
7474 public register ( ) : void {
7575
76- if ( this . _menubarOpts ) {
77- MenuRegistry . appendMenuItem ( this . _menubarOpts . menuId , {
78- group : this . _menubarOpts . group ,
79- command : {
80- id : this . id ,
81- title : this . _menubarOpts . title ,
82- // precondition: this.precondition
83- } ,
84- when : this . _menubarOpts . when ,
85- order : this . _menubarOpts . order
86- } ) ;
76+ if ( Array . isArray ( this . _menuOpts ) ) {
77+ this . _menuOpts . forEach ( this . _registerMenuItem , this ) ;
78+ } else if ( this . _menuOpts ) {
79+ this . _registerMenuItem ( this . _menuOpts ) ;
8780 }
8881
8982 if ( this . _kbOpts ) {
@@ -119,6 +112,19 @@ export abstract class Command {
119112 }
120113 }
121114
115+ private _registerMenuItem ( item : ICommandMenuOptions ) : void {
116+ MenuRegistry . appendMenuItem ( item . menuId , {
117+ group : item . group ,
118+ command : {
119+ id : this . id ,
120+ title : item . title ,
121+ // precondition: this.precondition
122+ } ,
123+ when : item . when ,
124+ order : item . order
125+ } ) ;
126+ }
127+
122128 public abstract runCommand ( accessor : ServicesAccessor , args : any ) : void | Promise < void > ;
123129}
124130
@@ -184,44 +190,70 @@ export abstract class EditorCommand extends Command {
184190
185191//#region EditorAction
186192
187- export interface IEditorCommandMenuOptions {
193+ export interface IEditorActionContextMenuOptions {
188194 group : string ;
189195 order : number ;
190196 when ?: ContextKeyExpr ;
191197}
192- export interface IActionOptions extends ICommandOptions {
198+ export interface IActionOptions {
199+ id : string ;
193200 label : string ;
194201 alias : string ;
195- menuOpts ?: IEditorCommandMenuOptions ;
202+ description ?: ICommandHandlerDescription ;
203+ precondition : ContextKeyExpr | undefined ;
204+ kbOpts ?: ICommandKeybindingsOptions ;
205+ contextMenuOpts ?: IEditorActionContextMenuOptions ;
206+ menuOpts ?: Partial < ICommandMenuOptions > | Partial < ICommandMenuOptions [ ] > ;
196207}
208+
197209export abstract class EditorAction extends EditorCommand {
198210
211+ private static convertOptions ( opts : IActionOptions ) : ICommandOptions {
212+
213+ function patch ( menu : Partial < ICommandMenuOptions > ) : ICommandMenuOptions {
214+ if ( ! menu . title ) {
215+ menu . title = opts . label ;
216+ }
217+ if ( ! menu . menuId ) {
218+ menu . menuId = MenuId . EditorContext ;
219+ }
220+ if ( ! menu . when ) {
221+ menu . when = opts . precondition ;
222+ }
223+ return < ICommandMenuOptions > menu ;
224+ }
225+
226+ let menuOpts : ICommandMenuOptions [ ] ;
227+ if ( Array . isArray ( opts . menuOpts ) ) {
228+ menuOpts = opts . menuOpts . map ( m => patch ( m ! ) ) ;
229+ } else if ( opts . menuOpts ) {
230+ menuOpts = [ patch ( opts . menuOpts ) ] ;
231+ } else {
232+ menuOpts = [ ] ;
233+ }
234+
235+ if ( opts . contextMenuOpts ) {
236+ const contextMenuItem = {
237+ title : opts . label ,
238+ when : ContextKeyExpr . and ( opts . precondition , opts . contextMenuOpts . when ) ,
239+ menuId : MenuId . EditorContext ,
240+ group : opts . contextMenuOpts . group ,
241+ order : opts . contextMenuOpts . order
242+ } ;
243+ menuOpts . push ( contextMenuItem ) ;
244+ }
245+
246+ opts . menuOpts = menuOpts ;
247+ return < ICommandOptions > opts ;
248+ }
249+
199250 public readonly label : string ;
200251 public readonly alias : string ;
201- private readonly menuOpts : IEditorCommandMenuOptions | undefined ;
202252
203253 constructor ( opts : IActionOptions ) {
204- super ( opts ) ;
254+ super ( EditorAction . convertOptions ( opts ) ) ;
205255 this . label = opts . label ;
206256 this . alias = opts . alias ;
207- this . menuOpts = opts . menuOpts ;
208- }
209-
210- public register ( ) : void {
211-
212- if ( this . menuOpts ) {
213- MenuRegistry . appendMenuItem ( MenuId . EditorContext , {
214- command : {
215- id : this . id ,
216- title : this . label
217- } ,
218- when : ContextKeyExpr . and ( this . precondition , this . menuOpts . when ) ,
219- group : this . menuOpts . group ,
220- order : this . menuOpts . order
221- } ) ;
222- }
223-
224- super . register ( ) ;
225257 }
226258
227259 public runEditorCommand ( accessor : ServicesAccessor , editor : ICodeEditor , args : any ) : void | Promise < void > {
0 commit comments