Skip to content

Commit 80bb710

Browse files
liujupingJackLian
authored andcommitted
feat(command): update default commands
1 parent ed7befb commit 80bb710

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

packages/engine/src/inner-plugins/default-command.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ const sampleNodeSchema: IPublicTypePropType = {
1010
value: [
1111
{
1212
name: 'id',
13-
propType: {
14-
type: 'string',
15-
isRequired: true,
16-
},
13+
propType: 'string',
1714
},
1815
{
1916
name: 'componentName',
@@ -277,10 +274,12 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
277274
handler: (param: {
278275
parentNodeId: string;
279276
nodeSchema: IPublicTypeNodeSchema;
277+
index: number;
280278
}) => {
281279
const {
282280
parentNodeId,
283281
nodeSchema,
282+
index,
284283
} = param;
285284
const { project } = ctx;
286285
const parentNode = project.currentDocument?.getNodeById(parentNodeId);
@@ -296,7 +295,11 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
296295
throw new Error('Invalid node.');
297296
}
298297

299-
project.currentDocument?.insertNode(parentNode, nodeSchema);
298+
if (index < 0 || index > (parentNode.children?.size || 0)) {
299+
throw new Error(`Invalid index '${index}'.`);
300+
}
301+
302+
project.currentDocument?.insertNode(parentNode, nodeSchema, index);
300303
},
301304
parameters: [
302305
{
@@ -309,6 +312,11 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
309312
propType: nodeSchemaPropType,
310313
description: 'The node to be added.',
311314
},
315+
{
316+
name: 'index',
317+
propType: 'number',
318+
description: 'The index of the node to be added.',
319+
},
312320
],
313321
});
314322

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

337+
if (!nodeId) {
338+
throw new Error('Invalid node id.');
339+
}
340+
341+
if (!targetNodeId) {
342+
throw new Error('Invalid target node id.');
343+
}
344+
329345
const node = project.currentDocument?.getNodeById(nodeId);
330346
const targetNode = project.currentDocument?.getNodeById(targetNodeId);
331347
if (!node) {
@@ -350,12 +366,18 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
350366
parameters: [
351367
{
352368
name: 'nodeId',
353-
propType: 'string',
369+
propType: {
370+
type: 'string',
371+
isRequired: true,
372+
},
354373
description: 'The id of the node to be moved.',
355374
},
356375
{
357376
name: 'targetNodeId',
358-
propType: 'string',
377+
propType: {
378+
type: 'string',
379+
isRequired: true,
380+
},
359381
description: 'The id of the target node.',
360382
},
361383
{
@@ -393,8 +415,8 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
393415
});
394416

395417
command.registerCommand({
396-
name: 'replace',
397-
description: 'Replace a node with another node.',
418+
name: 'update',
419+
description: 'Update a node.',
398420
handler(param: {
399421
nodeId: string;
400422
nodeSchema: IPublicTypeNodeSchema;
@@ -419,12 +441,12 @@ export const nodeCommand = (ctx: IPublicModelPluginContext) => {
419441
{
420442
name: 'nodeId',
421443
propType: 'string',
422-
description: 'The id of the node to be replaced.',
444+
description: 'The id of the node to be updated.',
423445
},
424446
{
425447
name: 'nodeSchema',
426448
propType: nodeSchemaPropType,
427-
description: 'The node to replace.',
449+
description: 'The node to be updated.',
428450
},
429451
],
430452
});

packages/types/src/shell/api/command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ export interface IPublicApiCommand {
1515
/**
1616
* 通过名称和给定参数执行一个命令,会校验参数是否符合命令定义
1717
*/
18-
executeCommand(name: string, args: IPublicTypeCommandHandlerArgs): void;
18+
executeCommand(name: string, args?: IPublicTypeCommandHandlerArgs): void;
1919

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

2525
/**
2626
* 列出所有已注册的命令

0 commit comments

Comments
 (0)