In this example, we will show you how to use the CreativeEditor SDK’s CreativeEngine to modify the hierarchy of blocks through the block
API.
Manipulate the hierarchy of blocks
public func getParent(_ id: DesignBlockID) throws -> DesignBlockID?
Query a block’s parent.
id:
: The block to query.- Returns: The parent’s handle or
nil
if the block has no parent.
public func getChildren(_ id: DesignBlockID) throws -> [DesignBlockID]
Get all children of the given block. Children are sorted in their rendering order: Last child is rendered in front of other children.
id:
: The block to query.- Returns: A list of block ids.
public func insertChild(into parent: DesignBlockID, child: DesignBlockID, at index: Int) throws
Insert a new or existing child at a certain position in the parent’s children. Required scope: “editor/add”
parent
: The block whose children should be updated.child
: The child to insert. Can be an existing child ofparent
.index
: The index to insert or move to.
public func appendChild(to parent: DesignBlockID, child: DesignBlockID) throws
Appends a new or existing child to a block’s children. Required scope: “editor/add”
parent
: The block whose children should be updated.child
: The child to insert. Can be an existing child ofparent
.
When adding a block to a new parent, it is automatically removed from its previous parent.
Full Code
Here’s the full code:
try engine.block.insertChild(into: page, child: block, at: 0)let parent = try engine.block.getParent(block)let childIds = try engine.block.getChildren(block)try engine.block.appendChild(to: parent!, child: block)