-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathCommandManager.js
More file actions
386 lines (341 loc) · 12.8 KB
/
CommandManager.js
File metadata and controls
386 lines (341 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
// @INCLUDE_IN_API_DOCS
/**
* Manages global application commands that can be called from menu items, key bindings, or subparts
* of the application.
*
* This module dispatches these event(s):
* - commandRegistered -- when a new command is registered
* - beforeExecuteCommand -- before dispatching a command
*/
define(function (require, exports, module) {
const EventDispatcher = require("utils/EventDispatcher");
/**
* Event triggered before command executes.
* @constant {string}
*/
const EVENT_BEFORE_EXECUTE_COMMAND = "beforeExecuteCommand";
/**
* Keyboard shortcut trigger.
* @constant {string}
*/
const SOURCE_KEYBOARD_SHORTCUT = "keyboardShortcut";
/**
* UI menu click trigger.
* @constant {string}
*/
const SOURCE_UI_MENU_CLICK = "uiMenuClick";
/**
* Other trigger types.
* @constant {string}
*/
const SOURCE_OTHER = "otherExecAction";
/**
* Map of all registered global commands
* @type {Object} CommandMap
* @property {Object.<string, Command>} commands - A map of command IDs to Command objects.
* @private
*/
let _commands = {};
/**
* Temporary copy of commands map for restoring after testing
* TODO (issue #1039): implement separate require contexts for unit tests
* @type {Object} CommandMap
* @property {Object.<string, Command>} commands - A map of command IDs to Command objects.
* @private
*/
let _commandsOriginal = {};
/**
* Events:
* - enabledStateChange
* - checkedStateChange
* - keyBindingAdded
* - keyBindingRemoved
*
* @constructor
* @param {string} name - text that will be displayed in the UI to represent command
* @param {string} id
* @param {function} commandFn - the function that is called when the command is executed.
*
* TODO: where should this be triggered, The Command or Exports?
* @param [options]
*/
function Command(name, id, commandFn, options= {}) {
this._name = name;
this._id = id;
this._commandFn = commandFn;
this._checked = undefined;
this._enabled = true;
this._options = options;
}
EventDispatcher.makeEventDispatcher(Command.prototype);
/**
* Get command id
*
* @return {string}
*/
Command.prototype.getID = function () {
return this._id;
};
/**
* Executes the command. Additional arguments are passed to the executing function
*
* @return {$.Promise} a jQuery promise that will be resolved when the command completes.
*/
Command.prototype.execute = function () {
if (!this._enabled) {
return (new $.Deferred()).reject().promise();
}
let args = arguments;
if(this._options.eventSource) {
// This command has been registered with the optional source details set we have to ensure the event
// argument is inserted first. The event source may be already injected by the executor,
// if not we have to do it now.
if(!args.length || !(typeof args[0] === 'object' && args[0].eventSource)) {
const event = {eventSource: SOURCE_OTHER}; // default we don't know the source
args = [event].concat(args);
}
}
let result = this._commandFn.apply(this, args);
if (!result) {
// If command does not return a promise, assume that it handled the
// command and return a resolved promise
return (new $.Deferred()).resolve().promise();
}
return result;
};
/**
* Is command enabled?
*
* @return {boolean}
*/
Command.prototype.getEnabled = function () {
return this._enabled;
};
/**
* get the command options
*
* @return {object}
*/
Command.prototype.getOptions = function () {
return this._options || {};
};
/**
* Sets enabled state of Command and dispatches "enabledStateChange"
* when the enabled state changes.
*
* @param {boolean} enabled
*/
Command.prototype.setEnabled = function (enabled) {
var changed = this._enabled !== enabled;
this._enabled = enabled;
if (changed) {
this.trigger("enabledStateChange");
}
};
/**
* Sets enabled state of Command and dispatches "checkedStateChange"
* when the enabled state changes.
*
* @param {boolean} checked
*/
Command.prototype.setChecked = function (checked) {
var changed = this._checked !== checked;
this._checked = checked;
if (changed) {
this.trigger("checkedStateChange");
}
};
/**
* Is command checked?
*
* @return {boolean}
*/
Command.prototype.getChecked = function () {
return this._checked;
};
/**
* Sets the name of the Command and dispatches "nameChange" so that
* UI that reflects the command name can update.
*
* Note, a Command name can appear in either HTML or native UI
* so HTML tags should not be used. To add a Unicode character,
* use \uXXXX instead of an HTML entity.
*
* @param {string} name
* @param {string} htmlName If set, this will be displayed in ui menus instead of the name given.
* Eg. "Phoenix menu<i class='fa fa-car' style='margin-left: 4px;'></i>"
*/
Command.prototype.setName = function (name, htmlName) {
let changed = this._name !== name;
this._name = name;
if (this._options.htmlName !== htmlName) {
changed = true;
this._options.htmlName = htmlName;
}
if (changed) {
this.trigger("nameChange");
}
};
/**
* Get command name
*
* @return {string}
*/
Command.prototype.getName = function () {
return this._name;
};
/**
* Registers a global command.
*
* @param {string} name - text that will be displayed in the UI to represent command
* @param {string} id - unique identifier for command.
* Core commands in Brackets use a simple command title as an id, for example "open.file".
* Extensions should use the following format: "author.myextension.mycommandname".
* For example, "lschmitt.csswizard.format.css".
* @param {function(...)} commandFn - the function to call when the command is executed. Any arguments passed to
* execute() (after the id) are passed as arguments to the function. If the function is asynchronous,
* it must return a jQuery promise that is resolved when the command completes. Otherwise, the
* CommandManager will assume it is synchronous, and return a promise that is already resolved.
* @param {Object} [options]
* @param {boolean} options.eventSource If set to true, the commandFn will be called with the first argument `event`
* with details about the source(invoker) as event.eventSource(one of the `CommandManager.SOURCE_*`) and
* event.sourceType(Eg. Ctrl-K) parameter.
* @param {string} options.htmlName If set, this will be displayed in ui menus instead of the name given.
* Eg. "Phoenix menu<i class='fa fa-car' style='margin-left: 4px;'></i>"
* @return {?Command}
*/
function register(name, id, commandFn, options={}) {
if (_commands[id]) {
console.log("Attempting to register an already-registered command: " + id);
return null;
}
if (!name || !id || !commandFn) {
console.error("Attempting to register a command with a missing name, id, or command function:" + name + " " + id);
return null;
}
var command = new Command(name, id, commandFn, options);
_commands[id] = command;
exports.trigger("commandRegistered", command);
return command;
}
/**
* Registers a global internal only command.
*
* @param {string} id - unique identifier for command.
* Core commands in Brackets use a simple command title as an id, for example "app.abort_quit".
* Extensions should use the following format: "author.myextension.mycommandname".
* For example, "lschmitt.csswizard.format.css".
* @param {function(...)} commandFn - the function to call when the command is executed. Any arguments passed to
* execute() (after the id) are passed as arguments to the function. If the function is asynchronous,
* it must return a jQuery promise that is resolved when the command completes. Otherwise, the
* CommandManager will assume it is synchronous, and return a promise that is already resolved.
* @return {?Command}
*/
function registerInternal(id, commandFn) {
if (_commands[id]) {
console.log("Attempting to register an already-registered command: " + id);
return null;
}
if (!id || !commandFn) {
console.error("Attempting to register an internal command with a missing id, or command function: " + id);
return null;
}
var command = new Command(null, id, commandFn);
_commands[id] = command;
exports.trigger("commandRegistered", command);
return command;
}
/**
* Clear all commands for unit testing, but first make copy of commands so that
* they can be restored afterward
* @private
*/
function _testReset() {
_commandsOriginal = _commands;
_commands = {};
}
/**
* Restore original commands after test and release copy
* @private
*/
function _testRestore() {
_commands = _commandsOriginal;
_commandsOriginal = {};
}
/**
* Retrieves a Command object by id
* @param {string} id
* @return {Command}
*/
function get(id) {
return _commands[id];
}
/**
* Returns the ids of all registered commands
* @return {Array.<string>}
*/
function getAll() {
return Object.keys(_commands);
}
/**
* Looks up and runs a global command. Additional arguments are passed to the command.
*
* @param {string} id The ID of the command to run.
* @return {$.Promise} a jQuery promise that will be resolved when the command completes.
*/
function execute(id) {
var command = _commands[id];
if (command) {
try {
exports.trigger(EVENT_BEFORE_EXECUTE_COMMAND, id);
} catch (err) {
console.error(err);
}
let args = Array.prototype.slice.call(arguments, 1); // remove id
if(command._options.eventSource) {
// This command has been registered with the optional source details set we have to ensure the event
// argument is inserted first. The event source may be already injected by the executor,
// if not we have to do it now.
if(!args.length || !(typeof args[0] === 'object' && args[0].eventSource)) {
const event = {eventSource: SOURCE_OTHER}; // default we don't know the source
args = [event].concat(args);
}
}
return command.execute.apply(command, args);
}
return (new $.Deferred()).reject().promise();
}
EventDispatcher.makeEventDispatcher(exports);
// Define public API
exports.register = register;
exports.registerInternal = registerInternal;
exports.execute = execute;
exports.get = get;
exports.getAll = getAll;
exports._testReset = _testReset;
exports._testRestore = _testRestore;
exports.EVENT_BEFORE_EXECUTE_COMMAND = EVENT_BEFORE_EXECUTE_COMMAND;
exports.SOURCE_KEYBOARD_SHORTCUT = SOURCE_KEYBOARD_SHORTCUT;
exports.SOURCE_UI_MENU_CLICK = SOURCE_UI_MENU_CLICK;
exports.SOURCE_OTHER = SOURCE_OTHER;
});