UNPKG

@alilc/lowcode-editor-core

Version:

Core Api for Ali lowCode engine

85 lines (84 loc) 2.99 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.Command = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _lowcodeTypes = require("@alilc/lowcode-types"); var _lowcodeUtils = require("@alilc/lowcode-utils"); var Command = exports.Command = /*#__PURE__*/function () { function Command() { this.commands = new Map(); this.commandErrors = []; } var _proto = Command.prototype; _proto.registerCommand = function registerCommand(command, options) { if (!(options !== null && options !== void 0 && options.commandScope)) { throw new Error('plugin meta.commandScope is required.'); } var name = options.commandScope + ":" + command.name; if (this.commands.has(name)) { throw new Error("Command '" + command.name + "' is already registered."); } this.commands.set(name, (0, _extends2["default"])({}, command, { name: name })); }; _proto.unregisterCommand = function unregisterCommand(name) { if (!this.commands.has(name)) { throw new Error("Command '" + name + "' is not registered."); } this.commands["delete"](name); }; _proto.executeCommand = function executeCommand(name, args) { var _command$parameters; var command = this.commands.get(name); if (!command) { throw new Error("Command '" + name + "' is not registered."); } (_command$parameters = command.parameters) === null || _command$parameters === void 0 ? void 0 : _command$parameters.forEach(function (d) { if (!(0, _lowcodeUtils.checkPropTypes)(args[d.name], d.name, d.propType, 'command')) { throw new Error("Command '" + name + "' arguments " + d.name + " is invalid."); } }); try { command.handler(args); } catch (error) { if (this.commandErrors && this.commandErrors.length) { this.commandErrors.forEach(function (callback) { return callback(name, error); }); } else { throw error; } } }; _proto.batchExecuteCommand = function batchExecuteCommand(commands, pluginContext) { var _this = this; if (!commands || !commands.length) { return; } pluginContext.common.utils.executeTransaction(function () { commands.forEach(function (command) { return _this.executeCommand(command.name, command.args); }); }, _lowcodeTypes.IPublicEnumTransitionType.REPAINT); }; _proto.listCommands = function listCommands() { return Array.from(this.commands.values()).map(function (d) { var result = { name: d.name }; if (d.description) { result.description = d.description; } if (d.parameters) { result.parameters = d.parameters; } return result; }); }; _proto.onCommandError = function onCommandError(callback) { this.commandErrors.push(callback); }; return Command; }();