What is the proper way of reordering the newly built elements?


#1

This might be an easy thing to do, but I need to make sure that I am taking the right approach. When we create a new element through the API, how can we get the z-order of a specific element and place that newly built element right at the top of it?


#2

Are you referring to elements on the canvas or within a dialog?


#3

Hi Kerri! On the canvas.
To be more clear, for example, you create the element A through the API. Now there is an element on the artboard (element B) that you want the element A to be placed right at the top of it.


#4

After importing the ‘commands’ package you can use its methods.
For example:

// import ‘commands’ package
const commands = require(“commands”);

// select B element
selection.items = [B];

// apply method
commands.sendToBack();


#5

Yes, but doesn’t it send the element B all the way back? Which I do not intend to do.
Note that I do not intend to reorder the existing elements, but to place the newly built element right at the top of an existing one. Also, there might be many elements on the page, and sending the element B toBack will cause more ordering complications.


#6

@peterflynn / @stevekwak Can you assist with the above?


#7

What you want is parent.addChildAfter(newNode, nodeToPutItAbove)


#8

Wow, exactly what I wanted, thank you!