Skip to content

Commit

Permalink
feat(command): update default commands
Browse files Browse the repository at this point in the history
  • Loading branch information
liujuping authored and JackLian committed Jan 30, 2024
1 parent ed7befb commit 80bb710
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
44 changes: 33 additions & 11 deletions packages/engine/src/inner-plugins/default-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ const sampleNodeSchema: IPublicTypePropType = {
value: [
{
name: 'id',
propType: {
type: 'string',
isRequired: true,
},
propType: 'string',
},
{
name: 'componentName',
Expand Down Expand Up @@ -277,10 +274,12 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
handler: (param: {
parentNodeId: string;
nodeSchema: IPublicTypeNodeSchema;
index: number;
}) => {
const {
parentNodeId,
nodeSchema,
index,
} = param;
const { project } = ctx;
const parentNode = project.currentDocument?.getNodeById(parentNodeId);
Expand All @@ -296,7 +295,11 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
throw new Error('Invalid node.');
}

project.currentDocument?.insertNode(parentNode, nodeSchema);
if (index < 0 || index > (parentNode.children?.size || 0)) {
throw new Error(`Invalid index '${index}'.`);
}

project.currentDocument?.insertNode(parentNode, nodeSchema, index);
},
parameters: [
{
Expand All @@ -309,6 +312,11 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
propType: nodeSchemaPropType,
description: 'The node to be added.',
},
{
name: 'index',
propType: 'number',
description: 'The index of the node to be added.',
},
],
});

Expand All @@ -326,6 +334,14 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
index = 0,
} = param;

if (!nodeId) {
throw new Error('Invalid node id.');
}

if (!targetNodeId) {
throw new Error('Invalid target node id.');
}

const node = project.currentDocument?.getNodeById(nodeId);
const targetNode = project.currentDocument?.getNodeById(targetNodeId);
if (!node) {
Expand All @@ -350,12 +366,18 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
parameters: [
{
name: 'nodeId',
propType: 'string',
propType: {
type: 'string',
isRequired: true,
},
description: 'The id of the node to be moved.',
},
{
name: 'targetNodeId',
propType: 'string',
propType: {
type: 'string',
isRequired: true,
},
description: 'The id of the target node.',
},
{
Expand Down Expand Up @@ -393,8 +415,8 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
});

command.registerCommand({
name: 'replace',
description: 'Replace a node with another node.',
name: 'update',
description: 'Update a node.',
handler(param: {
nodeId: string;
nodeSchema: IPublicTypeNodeSchema;
Expand All @@ -419,12 +441,12 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
{
name: 'nodeId',
propType: 'string',
description: 'The id of the node to be replaced.',
description: 'The id of the node to be updated.',
},
{
name: 'nodeSchema',
propType: nodeSchemaPropType,
description: 'The node to replace.',
description: 'The node to be updated.',
},
],
});
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/shell/api/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export interface IPublicApiCommand {
/**
* 通过名称和给定参数执行一个命令,会校验参数是否符合命令定义
*/
executeCommand(name: string, args: IPublicTypeCommandHandlerArgs): void;
executeCommand(name: string, args?: IPublicTypeCommandHandlerArgs): void;

/**
* 批量执行命令,执行完所有命令后再进行一次重绘,历史记录中只会记录一次
*/
batchExecuteCommand(commands: { name: string; args: IPublicTypeCommandHandlerArgs }[]): void;
batchExecuteCommand(commands: { name: string; args?: IPublicTypeCommandHandlerArgs }[]): void;

/**
* 列出所有已注册的命令
Expand Down

0 comments on commit 80bb710

Please sign in to comment.