Skip to content

Commit b8233cf

Browse files
committed
Sort nodes before position manager resize operations + optimize node & model lookup in position manager. Fixes undoing issues.
1 parent 4bbc33d commit b8233cf

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

lib/src/api/node_processor.dart

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ class NodeProcessor {
249249

250250
/// Sorted nodes is a list of nodes that go from parent -> child using
251251
/// a merge sort with memoization.
252-
static void sortNodes(List<BaseNode> nodes) {
252+
static void sortNodes(List<BaseNode> nodes, {bool reversed = false}) {
253253
// We cache the parent chains for each node to avoid
254254
// re-computing them multiple times as an optimization.
255255
// This is also known as memoization.
@@ -259,15 +259,20 @@ class NodeProcessor {
259259
};
260260

261261
mergeSort(nodes, compare: (a, b) {
262-
if (a.id == kRootNode) return -1;
263-
if (b.id == kRootNode) return 1;
262+
if (a.id == kRootNode) return reversed ? 1 : -1;
263+
if (b.id == kRootNode) return reversed ? -1 : 1;
264264

265265
final BaseNode aParent = getNode(a.parentID);
266266
final BaseNode bParent = getNode(b.parentID);
267267

268268
if (aParent.id == bParent.id) {
269-
return aParent.childrenOrEmpty.indexOf(a.id) -
270-
bParent.childrenOrEmpty.indexOf(b.id);
269+
if (reversed) {
270+
return bParent.childrenOrEmpty.indexOf(b.id) -
271+
aParent.childrenOrEmpty.indexOf(a.id);
272+
} else {
273+
return aParent.childrenOrEmpty.indexOf(a.id) -
274+
bParent.childrenOrEmpty.indexOf(b.id);
275+
}
271276
}
272277

273278
final List<String> aParents = parentChains[a]!;
@@ -277,11 +282,15 @@ class NodeProcessor {
277282
final bool bParentOfA = aParents.contains(b.id);
278283

279284
if (bParentOfA) {
280-
return 1;
285+
return reversed ? -1 : 1;
281286
} else if (aParentOfB) {
282-
return -1;
287+
return reversed ? 1 : -1;
283288
} else {
284-
return aParents.length - bParents.length;
289+
if (reversed) {
290+
return bParents.length - aParents.length;
291+
} else {
292+
return aParents.length - bParents.length;
293+
}
285294
}
286295
});
287296
}

0 commit comments

Comments
 (0)