Skip to content

Commit 35dc611

Browse files
committed
Account for nodes with Auto Layout applied
1 parent 78699d8 commit 35dc611

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

packages/utilities/src/node/sort-nodes/sort-nodes-by-canonical-order.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function sortNodesByCanonicalOrder<Node extends SceneNode>(
1919
if (areSiblingNodes(siblingNodes) === false) {
2020
throw new Error('Nodes in `siblingNodes` do not have the same parent')
2121
}
22-
return siblingNodes
22+
const result = siblingNodes
2323
.slice()
2424
.map(function (node: Node): { index: number; node: Node } {
2525
return {
@@ -33,4 +33,8 @@ export function sortNodesByCanonicalOrder<Node extends SceneNode>(
3333
.map(function ({ node }: { node: Node }): Node {
3434
return node
3535
})
36+
if ('layoutMode' in parentNode && parentNode.layoutMode !== 'NONE') {
37+
return result
38+
}
39+
return result.reverse()
3640
}

packages/utilities/src/node/sort-nodes/update-nodes-sort-order.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@ export function updateNodesSortOrder(siblingNodes: Array<SceneNode>): boolean {
1717
throw new Error('Nodes in `siblingNodes` do not have the same parent')
1818
}
1919
const siblingNodesCopy = siblingNodes.slice()
20-
const ids = parentNode.children.map(function ({ id }: SceneNode) {
20+
if ('layoutMode' in parentNode && parentNode.layoutMode !== 'NONE') {
21+
siblingNodesCopy.reverse()
22+
}
23+
const idsBefore = parentNode.children.map(function ({ id }: SceneNode) {
2124
return id
2225
})
23-
const insertIndex = computeInsertIndex(siblingNodesCopy, ids)
26+
const insertIndex = computeInsertIndex(siblingNodesCopy, idsBefore)
2427
for (const node of siblingNodesCopy) {
2528
parentNode.insertChild(insertIndex, node)
2629
}
2730
const idsAfter = parentNode.children.map(function ({ id }: SceneNode) {
2831
return id
2932
})
30-
return compareStringArrays(ids, idsAfter) === false
33+
return compareStringArrays(idsBefore, idsAfter) === false
3134
}
3235

3336
function computeInsertIndex(

0 commit comments

Comments
 (0)