4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
6
import 'vs/css!./standalone-tokens' ;
7
- import { IDisposable } from 'vs/base/common/lifecycle' ;
7
+ import { Disposable , DisposableStore , IDisposable } from 'vs/base/common/lifecycle' ;
8
8
import { splitLines } from 'vs/base/common/strings' ;
9
9
import { URI } from 'vs/base/common/uri' ;
10
10
import { FontMeasurements } from 'vs/editor/browser/config/fontMeasurements' ;
@@ -23,12 +23,16 @@ import { IModelService } from 'vs/editor/common/services/model';
23
23
import { createWebWorker as actualCreateWebWorker , IWebWorkerOptions , MonacoWebWorker } from 'vs/editor/browser/services/webWorker' ;
24
24
import * as standaloneEnums from 'vs/editor/common/standalone/standaloneEnums' ;
25
25
import { Colorizer , IColorizerElementOptions , IColorizerOptions } from 'vs/editor/standalone/browser/colorizer' ;
26
- import { createTextModel , IStandaloneCodeEditor , IStandaloneDiffEditor , IStandaloneDiffEditorConstructionOptions , IStandaloneEditorConstructionOptions , StandaloneDiffEditor , StandaloneEditor } from 'vs/editor/standalone/browser/standaloneCodeEditor' ;
27
- import { IEditorOverrideServices , StandaloneServices } from 'vs/editor/standalone/browser/standaloneServices' ;
26
+ import { createTextModel , IActionDescriptor , IStandaloneCodeEditor , IStandaloneDiffEditor , IStandaloneDiffEditorConstructionOptions , IStandaloneEditorConstructionOptions , StandaloneDiffEditor , StandaloneEditor } from 'vs/editor/standalone/browser/standaloneCodeEditor' ;
27
+ import { IEditorOverrideServices , StandaloneKeybindingService , StandaloneServices } from 'vs/editor/standalone/browser/standaloneServices' ;
28
28
import { StandaloneThemeService } from 'vs/editor/standalone/browser/standaloneThemeService' ;
29
29
import { IStandaloneThemeData , IStandaloneThemeService } from 'vs/editor/standalone/common/standaloneTheme' ;
30
- import { CommandsRegistry } from 'vs/platform/commands/common/commands' ;
30
+ import { CommandsRegistry , ICommandHandler } from 'vs/platform/commands/common/commands' ;
31
31
import { IMarker , IMarkerData , IMarkerService } from 'vs/platform/markers/common/markers' ;
32
+ import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding' ;
33
+ import { EditorCommand , ServicesAccessor } from 'vs/editor/browser/editorExtensions' ;
34
+ import { IMenuItem , MenuRegistry , MenuId } from 'vs/platform/actions/common/actions' ;
35
+ import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey' ;
32
36
33
37
/**
34
38
* Create a new editor under `domElement`.
@@ -99,6 +103,119 @@ export function createDiffNavigator(diffEditor: IStandaloneDiffEditor, opts?: ID
99
103
return new DiffNavigator ( diffEditor , opts ) ;
100
104
}
101
105
106
+ /**
107
+ * Description of a command contribution
108
+ */
109
+ export interface ICommandDescriptor {
110
+ /**
111
+ * An unique identifier of the contributed command.
112
+ */
113
+ id : string ;
114
+ /**
115
+ * Callback that will be executed when the command is triggered.
116
+ */
117
+ run : ICommandHandler ;
118
+ }
119
+
120
+ /**
121
+ * Add a command.
122
+ */
123
+ export function addCommand ( descriptor : ICommandDescriptor ) : IDisposable {
124
+ if ( ( typeof descriptor . id !== 'string' ) || ( typeof descriptor . run !== 'function' ) ) {
125
+ throw new Error ( 'Invalid command descriptor, `id` and `run` are required properties!' ) ;
126
+ }
127
+ return CommandsRegistry . registerCommand ( descriptor . id , descriptor . run ) ;
128
+ }
129
+
130
+ /**
131
+ * Add an action to all editors.
132
+ */
133
+ export function addEditorAction ( descriptor : IActionDescriptor ) : IDisposable {
134
+ if ( ( typeof descriptor . id !== 'string' ) || ( typeof descriptor . label !== 'string' ) || ( typeof descriptor . run !== 'function' ) ) {
135
+ throw new Error ( 'Invalid action descriptor, `id`, `label` and `run` are required properties!' ) ;
136
+ }
137
+
138
+ const precondition = ContextKeyExpr . deserialize ( descriptor . precondition ) ;
139
+ const run = ( accessor : ServicesAccessor , ...args : any [ ] ) : void | Promise < void > => {
140
+ return EditorCommand . runEditorCommand ( accessor , args , precondition , ( accessor , editor , args ) => Promise . resolve ( descriptor . run ( editor , ...args ) ) ) ;
141
+ } ;
142
+
143
+ const toDispose = new DisposableStore ( ) ;
144
+
145
+ // Register the command
146
+ toDispose . add ( CommandsRegistry . registerCommand ( descriptor . id , run ) ) ;
147
+
148
+ // Register the context menu item
149
+ if ( descriptor . contextMenuGroupId ) {
150
+ const menuItem : IMenuItem = {
151
+ command : {
152
+ id : descriptor . id ,
153
+ title : descriptor . label
154
+ } ,
155
+ when : precondition ,
156
+ group : descriptor . contextMenuGroupId ,
157
+ order : descriptor . contextMenuOrder || 0
158
+ } ;
159
+ toDispose . add ( MenuRegistry . appendMenuItem ( MenuId . EditorContext , menuItem ) ) ;
160
+ }
161
+
162
+ // Register the keybindings
163
+ if ( Array . isArray ( descriptor . keybindings ) ) {
164
+ const keybindingService = StandaloneServices . get ( IKeybindingService ) ;
165
+ if ( ! ( keybindingService instanceof StandaloneKeybindingService ) ) {
166
+ console . warn ( 'Cannot add keybinding because the editor is configured with an unrecognized KeybindingService' ) ;
167
+ } else {
168
+ const keybindingsWhen = ContextKeyExpr . and ( precondition , ContextKeyExpr . deserialize ( descriptor . keybindingContext ) ) ;
169
+ toDispose . add ( keybindingService . addDynamicKeybindings ( descriptor . keybindings . map ( ( keybinding ) => {
170
+ return {
171
+ keybinding,
172
+ command : descriptor . id ,
173
+ when : keybindingsWhen
174
+ } ;
175
+ } ) ) ) ;
176
+ }
177
+ }
178
+
179
+ return toDispose ;
180
+ }
181
+
182
+ /**
183
+ * A keybinding rule.
184
+ */
185
+ export interface IKeybindingRule {
186
+ keybinding : number ;
187
+ command ?: string | null ;
188
+ commandArgs ?: any ;
189
+ when ?: string | null ;
190
+ }
191
+
192
+ /**
193
+ * Add a keybinding rule.
194
+ */
195
+ export function addKeybindingRule ( rule : IKeybindingRule ) : IDisposable {
196
+ return addKeybindingRules ( [ rule ] ) ;
197
+ }
198
+
199
+ /**
200
+ * Add keybinding rules.
201
+ */
202
+ export function addKeybindingRules ( rules : IKeybindingRule [ ] ) : IDisposable {
203
+ const keybindingService = StandaloneServices . get ( IKeybindingService ) ;
204
+ if ( ! ( keybindingService instanceof StandaloneKeybindingService ) ) {
205
+ console . warn ( 'Cannot add keybinding because the editor is configured with an unrecognized KeybindingService' ) ;
206
+ return Disposable . None ;
207
+ }
208
+
209
+ return keybindingService . addDynamicKeybindings ( rules . map ( ( rule ) => {
210
+ return {
211
+ keybinding : rule . keybinding ,
212
+ command : rule . command ,
213
+ commandArgs : rule . commandArgs ,
214
+ when : ContextKeyExpr . deserialize ( rule . when ) ,
215
+ } ;
216
+ } ) ) ;
217
+ }
218
+
102
219
/**
103
220
* Create a new editor model.
104
221
* You can specify the language that should be set for this model or let the language be inferred from the `uri`.
@@ -325,6 +442,11 @@ export function createMonacoEditorAPI(): typeof monaco.editor {
325
442
createDiffEditor : < any > createDiffEditor ,
326
443
createDiffNavigator : < any > createDiffNavigator ,
327
444
445
+ addCommand : < any > addCommand ,
446
+ addEditorAction : < any > addEditorAction ,
447
+ addKeybindingRule : < any > addKeybindingRule ,
448
+ addKeybindingRules : < any > addKeybindingRules ,
449
+
328
450
createModel : < any > createModel ,
329
451
setModelLanguage : < any > setModelLanguage ,
330
452
setModelMarkers : < any > setModelMarkers ,
0 commit comments