@@ -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 } ) ;
0 commit comments