mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: node traverser
This commit is contained in:
@ -0,0 +1,42 @@
|
|||||||
|
import 'package:flowy_editor/document/node.dart';
|
||||||
|
|
||||||
|
import './state_tree.dart';
|
||||||
|
import './node.dart';
|
||||||
|
|
||||||
|
/// [NodeTraverser] is used to traverse the nodes in visual order.
|
||||||
|
class NodeTraverser {
|
||||||
|
final StateTree stateTree;
|
||||||
|
Node? currentNode;
|
||||||
|
|
||||||
|
NodeTraverser(this.stateTree, Node beginNode) : currentNode = beginNode;
|
||||||
|
|
||||||
|
Node? next() {
|
||||||
|
final node = currentNode;
|
||||||
|
if (node == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.children.isNotEmpty) {
|
||||||
|
currentNode = _findLeadingChild(node);
|
||||||
|
} else if (node.next != null) {
|
||||||
|
currentNode = node.next!;
|
||||||
|
} else {
|
||||||
|
final parent = node.parent!;
|
||||||
|
final nextOfParent = parent.next;
|
||||||
|
if (nextOfParent == null) {
|
||||||
|
currentNode = null;
|
||||||
|
} else {
|
||||||
|
currentNode = _findLeadingChild(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node _findLeadingChild(Node node) {
|
||||||
|
while (node.children.isNotEmpty) {
|
||||||
|
node = node.children.first;
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user