@alilc/lowcode-plugin-command
Version:
> TODO: description
372 lines (371 loc) • 12.4 kB
JavaScript
"use strict";
exports.__esModule = true;
exports.nodeSchemaPropType = exports.nodeCommand = void 0;
var _lowcodeUtils = require("@alilc/lowcode-utils");
var sampleNodeSchema = {
type: 'shape',
value: [{
name: 'id',
propType: 'string'
}, {
name: 'componentName',
propType: {
type: 'string',
isRequired: true
}
}, {
name: 'props',
propType: 'object'
}, {
name: 'condition',
propType: 'any'
}, {
name: 'loop',
propType: 'any'
}, {
name: 'loopArgs',
propType: 'any'
}, {
name: 'children',
propType: 'any'
}]
};
var nodeSchemaPropType = exports.nodeSchemaPropType = {
type: 'shape',
value: [sampleNodeSchema.value[0], sampleNodeSchema.value[1], {
name: 'props',
propType: {
type: 'objectOf',
value: {
type: 'oneOfType',
// 不会强制校验,更多作为提示
value: ['any', {
type: 'shape',
value: [{
name: 'type',
propType: {
type: 'oneOf',
value: ['JSExpression']
}
}, {
name: 'value',
propType: 'string'
}]
}, {
type: 'shape',
value: [{
name: 'type',
propType: {
type: 'oneOf',
value: ['JSFunction']
}
}, {
name: 'value',
propType: 'string'
}]
}, {
type: 'shape',
value: [{
name: 'type',
propType: {
type: 'oneOf',
value: ['JSSlot']
}
}, {
name: 'value',
propType: {
type: 'oneOfType',
value: [sampleNodeSchema, {
type: 'arrayOf',
value: sampleNodeSchema
}]
}
}]
}]
}
}
}, {
name: 'condition',
propType: {
type: 'oneOfType',
value: ['bool', {
type: 'shape',
value: [{
name: 'type',
propType: {
type: 'oneOf',
value: ['JSExpression']
}
}, {
name: 'value',
propType: 'string'
}]
}]
}
}, {
name: 'loop',
propType: {
type: 'oneOfType',
value: ['array', {
type: 'shape',
value: [{
name: 'type',
propType: {
type: 'oneOf',
value: ['JSExpression']
}
}, {
name: 'value',
propType: 'string'
}]
}]
}
}, {
name: 'loopArgs',
propType: {
type: 'oneOfType',
value: [{
type: 'arrayOf',
value: {
type: 'oneOfType',
value: ['any', {
type: 'shape',
value: [{
name: 'type',
propType: {
type: 'oneOf',
value: ['JSExpression']
}
}, {
name: 'value',
propType: 'string'
}]
}]
}
}, {
type: 'shape',
value: [{
name: 'type',
propType: {
type: 'oneOf',
value: ['JSExpression']
}
}, {
name: 'value',
propType: 'string'
}]
}]
}
}, {
name: 'children',
propType: {
type: 'arrayOf',
value: sampleNodeSchema
}
}]
};
var nodeCommand = exports.nodeCommand = function nodeCommand(ctx) {
var command = ctx.command,
project = ctx.project;
return {
init: function init() {
command.registerCommand({
name: 'add',
description: 'Add a node to the canvas.',
handler: function handler(param) {
var _project$currentDocum, _parentNode$children, _project$currentDocum2;
var parentNodeId = param.parentNodeId,
nodeSchema = param.nodeSchema,
index = param.index;
var project = ctx.project;
var parentNode = (_project$currentDocum = project.currentDocument) === null || _project$currentDocum === void 0 ? void 0 : _project$currentDocum.getNodeById(parentNodeId);
if (!parentNode) {
throw new Error("Can not find node '" + parentNodeId + "'.");
}
if (!parentNode.isContainerNode) {
throw new Error("Node '" + parentNodeId + "' is not a container node.");
}
if (!(0, _lowcodeUtils.isNodeSchema)(nodeSchema)) {
throw new Error('Invalid node.');
}
if (index < 0 || index > (((_parentNode$children = parentNode.children) === null || _parentNode$children === void 0 ? void 0 : _parentNode$children.size) || 0)) {
throw new Error("Invalid index '" + index + "'.");
}
(_project$currentDocum2 = project.currentDocument) === null || _project$currentDocum2 === void 0 ? void 0 : _project$currentDocum2.insertNode(parentNode, nodeSchema, index);
},
parameters: [{
name: 'parentNodeId',
propType: 'string',
description: 'The id of the parent node.'
}, {
name: 'nodeSchema',
propType: nodeSchemaPropType,
description: 'The node to be added.'
}, {
name: 'index',
propType: 'number',
description: 'The index of the node to be added.'
}]
});
command.registerCommand({
name: 'move',
description: 'Move a node to another node.',
handler: function handler(param) {
var _project$currentDocum3, _project$currentDocum4, _targetNode$children, _project$currentDocum5, _project$currentDocum6;
var nodeId = param.nodeId,
targetNodeId = param.targetNodeId,
_param$index = param.index,
index = _param$index === void 0 ? 0 : _param$index;
if (!nodeId) {
throw new Error('Invalid node id.');
}
if (!targetNodeId) {
throw new Error('Invalid target node id.');
}
var node = (_project$currentDocum3 = project.currentDocument) === null || _project$currentDocum3 === void 0 ? void 0 : _project$currentDocum3.getNodeById(nodeId);
var targetNode = (_project$currentDocum4 = project.currentDocument) === null || _project$currentDocum4 === void 0 ? void 0 : _project$currentDocum4.getNodeById(targetNodeId);
if (!node) {
throw new Error("Can not find node '" + nodeId + "'.");
}
if (!targetNode) {
throw new Error("Can not find node '" + targetNodeId + "'.");
}
if (!targetNode.isContainerNode) {
throw new Error("Node '" + targetNodeId + "' is not a container node.");
}
if (index < 0 || index > (((_targetNode$children = targetNode.children) === null || _targetNode$children === void 0 ? void 0 : _targetNode$children.size) || 0)) {
throw new Error("Invalid index '" + index + "'.");
}
(_project$currentDocum5 = project.currentDocument) === null || _project$currentDocum5 === void 0 ? void 0 : _project$currentDocum5.removeNode(node);
(_project$currentDocum6 = project.currentDocument) === null || _project$currentDocum6 === void 0 ? void 0 : _project$currentDocum6.insertNode(targetNode, node, index);
},
parameters: [{
name: 'nodeId',
propType: {
type: 'string',
isRequired: true
},
description: 'The id of the node to be moved.'
}, {
name: 'targetNodeId',
propType: {
type: 'string',
isRequired: true
},
description: 'The id of the target node.'
}, {
name: 'index',
propType: 'number',
description: 'The index of the node to be moved.'
}]
});
command.registerCommand({
name: 'remove',
description: 'Remove a node from the canvas.',
handler: function handler(param) {
var _project$currentDocum7, _project$currentDocum8;
var nodeId = param.nodeId;
var node = (_project$currentDocum7 = project.currentDocument) === null || _project$currentDocum7 === void 0 ? void 0 : _project$currentDocum7.getNodeById(nodeId);
if (!node) {
throw new Error("Can not find node '" + nodeId + "'.");
}
(_project$currentDocum8 = project.currentDocument) === null || _project$currentDocum8 === void 0 ? void 0 : _project$currentDocum8.removeNode(node);
},
parameters: [{
name: 'nodeId',
propType: 'string',
description: 'The id of the node to be removed.'
}]
});
command.registerCommand({
name: 'update',
description: 'Update a node.',
handler: function handler(param) {
var _project$currentDocum9;
var nodeId = param.nodeId,
nodeSchema = param.nodeSchema;
var node = (_project$currentDocum9 = project.currentDocument) === null || _project$currentDocum9 === void 0 ? void 0 : _project$currentDocum9.getNodeById(nodeId);
if (!node) {
throw new Error("Can not find node '" + nodeId + "'.");
}
if (!(0, _lowcodeUtils.isNodeSchema)(nodeSchema)) {
throw new Error('Invalid node.');
}
node.importSchema(nodeSchema);
},
parameters: [{
name: 'nodeId',
propType: 'string',
description: 'The id of the node to be updated.'
}, {
name: 'nodeSchema',
propType: nodeSchemaPropType,
description: 'The node to be updated.'
}]
});
command.registerCommand({
name: 'updateProps',
description: 'Update the properties of a node.',
handler: function handler(param) {
var _project$currentDocum10;
var nodeId = param.nodeId,
props = param.props;
var node = (_project$currentDocum10 = project.currentDocument) === null || _project$currentDocum10 === void 0 ? void 0 : _project$currentDocum10.getNodeById(nodeId);
if (!node) {
throw new Error("Can not find node '" + nodeId + "'.");
}
Object.keys(props).forEach(function (key) {
node.setPropValue(key, props[key]);
});
},
parameters: [{
name: 'nodeId',
propType: 'string',
description: 'The id of the node to be updated.'
}, {
name: 'props',
propType: 'object',
description: 'The properties to be updated.'
}]
});
command.registerCommand({
name: 'removeProps',
description: 'Remove the properties of a node.',
handler: function handler(param) {
var _project$currentDocum11;
var nodeId = param.nodeId,
propNames = param.propNames;
var node = (_project$currentDocum11 = project.currentDocument) === null || _project$currentDocum11 === void 0 ? void 0 : _project$currentDocum11.getNodeById(nodeId);
if (!node) {
throw new Error("Can not find node '" + nodeId + "'.");
}
propNames.forEach(function (key) {
var _node$props, _node$props$getProp;
(_node$props = node.props) === null || _node$props === void 0 ? void 0 : (_node$props$getProp = _node$props.getProp(key)) === null || _node$props$getProp === void 0 ? void 0 : _node$props$getProp.remove();
});
},
parameters: [{
name: 'nodeId',
propType: 'string',
description: 'The id of the node to be updated.'
}, {
name: 'propNames',
propType: 'array',
description: 'The properties to be removed.'
}]
});
},
destroy: function destroy() {
command.unregisterCommand('node:add');
command.unregisterCommand('node:move');
command.unregisterCommand('node:remove');
command.unregisterCommand('node:update');
command.unregisterCommand('node:updateProps');
command.unregisterCommand('node:removeProps');
}
};
};
nodeCommand.pluginName = '___node_command___';
nodeCommand.meta = {
commandScope: 'node'
};