@stoplight/command
Advanced tools
Comparing version 0.0.11-2 to 0.0.11-3
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var mobx_1 = require("mobx"); | ||
var lib_1 = require("@stoplight/common/lib"); | ||
var event_1 = require("@stoplight/common/lib/event"); | ||
var lib_2 = require("@stoplight/ioc/lib"); | ||
var types_1 = require("./types"); | ||
var keyboardJS; | ||
var Command = /** @class */ (function () { | ||
function Command(command) { | ||
const tslib_1 = require("tslib"); | ||
const mobx_1 = require("mobx"); | ||
const lib_1 = require("@stoplight/common/lib"); | ||
const event_1 = require("@stoplight/common/lib/event"); | ||
const lib_2 = require("@stoplight/ioc/lib"); | ||
const types_1 = require("./types"); | ||
let keyboardJS; | ||
class Command { | ||
constructor(command) { | ||
this._enabled = false; | ||
@@ -18,34 +18,18 @@ this._executing = false; | ||
} | ||
Object.defineProperty(Command.prototype, "enabled", { | ||
get: function () { | ||
return this._enabled; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Command.prototype, "executing", { | ||
get: function () { | ||
return this._executing; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Command.prototype, "visible", { | ||
get: function () { | ||
return this._visible; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Command.prototype, "onDidUpdate", { | ||
get: function () { | ||
return this._didUpdateEmitter.event; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Command.prototype.update = function (change) { | ||
get enabled() { | ||
return this._enabled; | ||
} | ||
get executing() { | ||
return this._executing; | ||
} | ||
get visible() { | ||
return this._visible; | ||
} | ||
get onDidUpdate() { | ||
return this._didUpdateEmitter.event; | ||
} | ||
update(change) { | ||
if (!change) | ||
return; | ||
var enabled = change.enabled, executing = change.executing, visible = change.visible; | ||
const { enabled, executing, visible } = change; | ||
if (enabled === this.enabled && executing === this.executing && visible === this.visible) | ||
@@ -60,38 +44,37 @@ return; | ||
this._didUpdateEmitter.fire(this); | ||
}; | ||
tslib_1.__decorate([ | ||
mobx_1.observable, | ||
tslib_1.__metadata("design:type", Boolean) | ||
], Command.prototype, "_enabled", void 0); | ||
tslib_1.__decorate([ | ||
mobx_1.observable, | ||
tslib_1.__metadata("design:type", Boolean) | ||
], Command.prototype, "_executing", void 0); | ||
tslib_1.__decorate([ | ||
mobx_1.observable, | ||
tslib_1.__metadata("design:type", Boolean) | ||
], Command.prototype, "_visible", void 0); | ||
tslib_1.__decorate([ | ||
mobx_1.computed, | ||
tslib_1.__metadata("design:type", Object), | ||
tslib_1.__metadata("design:paramtypes", []) | ||
], Command.prototype, "enabled", null); | ||
tslib_1.__decorate([ | ||
mobx_1.computed, | ||
tslib_1.__metadata("design:type", Object), | ||
tslib_1.__metadata("design:paramtypes", []) | ||
], Command.prototype, "executing", null); | ||
tslib_1.__decorate([ | ||
mobx_1.computed, | ||
tslib_1.__metadata("design:type", Object), | ||
tslib_1.__metadata("design:paramtypes", []) | ||
], Command.prototype, "visible", null); | ||
tslib_1.__decorate([ | ||
mobx_1.action, | ||
tslib_1.__metadata("design:type", Function), | ||
tslib_1.__metadata("design:paramtypes", [Object]), | ||
tslib_1.__metadata("design:returntype", void 0) | ||
], Command.prototype, "update", null); | ||
return Command; | ||
}()); | ||
} | ||
} | ||
tslib_1.__decorate([ | ||
mobx_1.observable, | ||
tslib_1.__metadata("design:type", Boolean) | ||
], Command.prototype, "_enabled", void 0); | ||
tslib_1.__decorate([ | ||
mobx_1.observable, | ||
tslib_1.__metadata("design:type", Boolean) | ||
], Command.prototype, "_executing", void 0); | ||
tslib_1.__decorate([ | ||
mobx_1.observable, | ||
tslib_1.__metadata("design:type", Boolean) | ||
], Command.prototype, "_visible", void 0); | ||
tslib_1.__decorate([ | ||
mobx_1.computed, | ||
tslib_1.__metadata("design:type", Object), | ||
tslib_1.__metadata("design:paramtypes", []) | ||
], Command.prototype, "enabled", null); | ||
tslib_1.__decorate([ | ||
mobx_1.computed, | ||
tslib_1.__metadata("design:type", Object), | ||
tslib_1.__metadata("design:paramtypes", []) | ||
], Command.prototype, "executing", null); | ||
tslib_1.__decorate([ | ||
mobx_1.computed, | ||
tslib_1.__metadata("design:type", Object), | ||
tslib_1.__metadata("design:paramtypes", []) | ||
], Command.prototype, "visible", null); | ||
tslib_1.__decorate([ | ||
mobx_1.action, | ||
tslib_1.__metadata("design:type", Function), | ||
tslib_1.__metadata("design:paramtypes", [Object]), | ||
tslib_1.__metadata("design:returntype", void 0) | ||
], Command.prototype, "update", null); | ||
exports.Command = Command; | ||
@@ -101,4 +84,4 @@ /** | ||
*/ | ||
var CommandRegistry = /** @class */ (function () { | ||
function CommandRegistry() { | ||
let CommandRegistry = class CommandRegistry { | ||
constructor() { | ||
this._commands = {}; | ||
@@ -112,36 +95,27 @@ this._handlers = {}; | ||
} | ||
Object.defineProperty(CommandRegistry.prototype, "commands", { | ||
/** | ||
* Get all registered commands. | ||
*/ | ||
get: function () { | ||
var commands = []; | ||
for (var _i = 0, _a = this.commandIds; _i < _a.length; _i++) { | ||
var id = _a[_i]; | ||
var cmd = this.getCommand(id); | ||
if (cmd) { | ||
commands.push(cmd); | ||
} | ||
/** | ||
* Get all registered commands. | ||
*/ | ||
get commands() { | ||
const commands = []; | ||
for (const id of this.commandIds) { | ||
const cmd = this.getCommand(id); | ||
if (cmd) { | ||
commands.push(cmd); | ||
} | ||
return commands; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(CommandRegistry.prototype, "commandIds", { | ||
/** | ||
* Get all registered commands identifiers. | ||
*/ | ||
get: function () { | ||
return Object.keys(this._commands); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
} | ||
return commands; | ||
} | ||
/** | ||
* Get all registered commands identifiers. | ||
*/ | ||
get commandIds() { | ||
return Object.keys(this._commands); | ||
} | ||
/** | ||
* Get a command for the given command identifier. | ||
*/ | ||
CommandRegistry.prototype.getCommand = function (id) { | ||
getCommand(id) { | ||
return this._commands[id]; | ||
}; | ||
} | ||
/** | ||
@@ -152,5 +126,5 @@ * Register the given command and handler if present. | ||
*/ | ||
CommandRegistry.prototype.registerCommand = function (command, handler) { | ||
registerCommand(command, handler) { | ||
if (handler) { | ||
var toDispose = new lib_1.disposable.Collection(); | ||
const toDispose = new lib_1.disposable.Collection(); | ||
toDispose.push(this._registerCommand(command)); | ||
@@ -161,9 +135,8 @@ toDispose.push(this.registerHandler(command.id, handler)); | ||
return this._registerCommand(command); | ||
}; | ||
} | ||
/** | ||
* Register the given handler for the given command identifier. | ||
*/ | ||
CommandRegistry.prototype.registerHandler = function (commandId, handler) { | ||
var _this = this; | ||
var handlers = this._handlers[commandId]; | ||
registerHandler(commandId, handler) { | ||
let handlers = this._handlers[commandId]; | ||
if (!handlers) { | ||
@@ -174,34 +147,33 @@ this._handlers[commandId] = handlers = []; | ||
this.refreshCommandState(commandId); | ||
return lib_1.disposable.create(function () { | ||
var idx = handlers.indexOf(handler); | ||
return lib_1.disposable.create(() => { | ||
const idx = handlers.indexOf(handler); | ||
if (idx >= 0) { | ||
handlers.splice(idx, 1); | ||
} | ||
_this.refreshCommandState(commandId); | ||
this.refreshCommandState(commandId); | ||
}); | ||
}; | ||
} | ||
/** | ||
* Register the given shortcut for the given command identifier. | ||
*/ | ||
CommandRegistry.prototype.registerShortcut = function (shortcut, options) { | ||
var _this = this; | ||
registerShortcut(shortcut, options) { | ||
if (!keyboardJS) | ||
return lib_1.disposable.NOOP; | ||
var commandId = options.commandId, execute = options.execute; | ||
const { commandId, execute } = options; | ||
if (commandId) { | ||
if (!this._commands[commandId]) { | ||
throw Error("Cannot bind '" + shortcut + "' to unregistered command with ID '" + commandId + "'."); | ||
throw Error(`Cannot bind '${shortcut}' to unregistered command with ID '${commandId}'.`); | ||
} | ||
} | ||
else if (!execute) { | ||
throw Error("Cannot bind '" + shortcut + "' shortcut. commandId or execute function must be provided."); | ||
throw Error(`Cannot bind '${shortcut}' shortcut. commandId or execute function must be provided.`); | ||
} | ||
if (this._shortcuts[shortcut]) { | ||
throw Error("The '" + shortcut + "' has already been registered."); | ||
throw Error(`The '${shortcut}' has already been registered.`); | ||
} | ||
this._shortcuts[shortcut] = keyboardJS.bind(shortcut, function (e) { | ||
this._shortcuts[shortcut] = keyboardJS.bind(shortcut, (e) => { | ||
e.preventDefault(); | ||
if (commandId) { | ||
if (_this.isEnabled(commandId)) { | ||
_this.executeCommand(commandId); | ||
if (this.isEnabled(commandId)) { | ||
this.executeCommand(commandId); | ||
} | ||
@@ -213,40 +185,28 @@ } | ||
}); | ||
return lib_1.disposable.create(function () { | ||
var sh = _this._shortcuts[shortcut]; | ||
return lib_1.disposable.create(() => { | ||
const sh = this._shortcuts[shortcut]; | ||
if (sh) { | ||
keyboardJS.unbind(shortcut, sh); | ||
delete _this._shortcuts[shortcut]; | ||
delete this._shortcuts[shortcut]; | ||
} | ||
}); | ||
}; | ||
} | ||
/** | ||
* Test whether there is an active handler for the given command. | ||
*/ | ||
CommandRegistry.prototype.isEnabled = function (commandId) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return this.getActiveHandler.apply(this, [commandId].concat(args)) !== undefined; | ||
}; | ||
isEnabled(commandId, ...args) { | ||
return this.getActiveHandler(commandId, ...args) !== undefined; | ||
} | ||
/** | ||
* Test whether there is a visible handler for the given command. | ||
*/ | ||
CommandRegistry.prototype.isVisible = function (commandId) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return this.getVisibleHandler.apply(this, [commandId].concat(args)) !== undefined; | ||
}; | ||
isVisible(commandId, ...args) { | ||
return this.getVisibleHandler(commandId, ...args) !== undefined; | ||
} | ||
/** | ||
* Test whether there is a handler that is currently executing for the given command. | ||
*/ | ||
CommandRegistry.prototype.isExecuting = function (commandId) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return this.getExecutingHandler.apply(this, [commandId].concat(args)) !== undefined; | ||
}; | ||
isExecuting(commandId, ...args) { | ||
return this.getExecutingHandler(commandId, ...args) !== undefined; | ||
} | ||
/** | ||
@@ -257,26 +217,18 @@ * Execute the active handler for the given command and arguments. | ||
*/ | ||
CommandRegistry.prototype.executeCommand = function (commandId) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
var handler = this.getActiveHandler.apply(this, [commandId].concat(args)); | ||
executeCommand(commandId, ...args) { | ||
const handler = this.getActiveHandler(commandId, ...args); | ||
if (handler) { | ||
return Promise.resolve(handler.execute.apply(handler, args)); | ||
return Promise.resolve(handler.execute(...args)); | ||
} | ||
return Promise.reject("The command '" + commandId + "' cannot be executed. There are no active\n handlers available for the command."); | ||
}; | ||
return Promise.reject(`The command '${commandId}' cannot be executed. There are no active | ||
handlers available for the command.`); | ||
} | ||
/** | ||
* Get a visible handler for the given command or `undefined`. | ||
*/ | ||
CommandRegistry.prototype.getVisibleHandler = function (commandId) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
var handlers = this._handlers[commandId]; | ||
getVisibleHandler(commandId, ...args) { | ||
const handlers = this._handlers[commandId]; | ||
if (handlers) { | ||
for (var _a = 0, handlers_1 = handlers; _a < handlers_1.length; _a++) { | ||
var handler = handlers_1[_a]; | ||
if (!handler.isVisible || handler.isVisible.apply(handler, args)) { | ||
for (const handler of handlers) { | ||
if (!handler.isVisible || handler.isVisible(...args)) { | ||
return handler; | ||
@@ -286,16 +238,11 @@ } | ||
} | ||
}; | ||
} | ||
/** | ||
* Get a handler that is currently executing for the given command or `undefined`. | ||
*/ | ||
CommandRegistry.prototype.getExecutingHandler = function (commandId) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
var handlers = this._handlers[commandId]; | ||
getExecutingHandler(commandId, ...args) { | ||
const handlers = this._handlers[commandId]; | ||
if (handlers) { | ||
for (var _a = 0, handlers_2 = handlers; _a < handlers_2.length; _a++) { | ||
var handler = handlers_2[_a]; | ||
if (handler.isExecuting && handler.isExecuting.apply(handler, args)) { | ||
for (const handler of handlers) { | ||
if (handler.isExecuting && handler.isExecuting(...args)) { | ||
return handler; | ||
@@ -305,16 +252,11 @@ } | ||
} | ||
}; | ||
} | ||
/** | ||
* Get an active handler for the given command or `undefined`. | ||
*/ | ||
CommandRegistry.prototype.getActiveHandler = function (commandId) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
var handlers = this._handlers[commandId]; | ||
getActiveHandler(commandId, ...args) { | ||
const handlers = this._handlers[commandId]; | ||
if (handlers) { | ||
for (var _a = 0, handlers_3 = handlers; _a < handlers_3.length; _a++) { | ||
var handler = handlers_3[_a]; | ||
if (!handler.isEnabled || handler.isEnabled.apply(handler, args)) { | ||
for (const handler of handlers) { | ||
if (!handler.isEnabled || handler.isEnabled(...args)) { | ||
return handler; | ||
@@ -324,12 +266,8 @@ } | ||
} | ||
}; | ||
} | ||
/** | ||
* Re-compute and set command props such as enabled. | ||
*/ | ||
CommandRegistry.prototype.refreshCommandState = function (commandId) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
var command = this.getCommand(commandId); | ||
refreshCommandState(commandId, ...args) { | ||
const command = this.getCommand(commandId); | ||
if (!command) | ||
@@ -342,27 +280,25 @@ return; | ||
}); | ||
}; | ||
CommandRegistry.prototype._registerCommand = function (command) { | ||
var _this = this; | ||
} | ||
_registerCommand(command) { | ||
if (this._commands[command.id]) { | ||
throw Error("A command with ID '" + command.id + "' is already registered."); | ||
throw Error(`A command with ID '${command.id}' is already registered.`); | ||
} | ||
this._commands[command.id] = new Command(command); | ||
var shortcut; | ||
let shortcut; | ||
if (command.shortcut) { | ||
shortcut = this.registerShortcut(command.shortcut, { commandId: command.id }); | ||
} | ||
return lib_1.disposable.create(function () { | ||
return lib_1.disposable.create(() => { | ||
if (shortcut) { | ||
shortcut.dispose(); | ||
} | ||
delete _this._commands[command.id]; | ||
delete this._commands[command.id]; | ||
}); | ||
}; | ||
CommandRegistry = tslib_1.__decorate([ | ||
lib_2.provideSingleton(types_1.SYMBOLS.CommandRegistry), | ||
tslib_1.__metadata("design:paramtypes", []) | ||
], CommandRegistry); | ||
return CommandRegistry; | ||
}()); | ||
} | ||
}; | ||
CommandRegistry = tslib_1.__decorate([ | ||
lib_2.provideSingleton(types_1.SYMBOLS.CommandRegistry), | ||
tslib_1.__metadata("design:paramtypes", []) | ||
], CommandRegistry); | ||
exports.CommandRegistry = CommandRegistry; | ||
//# sourceMappingURL=command.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./command"), exports); | ||
var types = require("./types"); | ||
const types = require("./types"); | ||
exports.types = types; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@stoplight/command", | ||
"version": "0.0.11-2", | ||
"version": "0.0.11-3", | ||
"description": "Stoplight command registry implementation.", | ||
@@ -24,4 +24,4 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@stoplight/common": "^0.0.11-2", | ||
"@stoplight/ioc": "^0.0.11-2", | ||
"@stoplight/common": "^0.0.11-3", | ||
"@stoplight/ioc": "^0.0.11-3", | ||
"keyboardjs": "2.4.x", | ||
@@ -28,0 +28,0 @@ "tslib": "1.x.x" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23885
457
Updated@stoplight/common@^0.0.11-3
Updated@stoplight/ioc@^0.0.11-3