feat: find node with path or index in state tree

This commit is contained in:
Lucas.Xu 2022-07-11 18:38:08 +08:00
parent 76999c6a46
commit 47436bf6e2
3 changed files with 28 additions and 0 deletions

View File

@ -1,5 +1,7 @@
import 'dart:collection';
import 'package:flowy_editor/document/path.dart';
class Node extends LinkedListEntry<Node> {
Node? parent;
final String type;
@ -40,6 +42,22 @@ class Node extends LinkedListEntry<Node> {
);
}
Node? childAtIndex(int index) {
if (children.length <= index) {
return null;
}
return children.elementAt(index);
}
Node? childAtPath(Path path) {
if (path.isEmpty) {
return this;
}
return childAtIndex(path.first)?.childAtPath(path.sublist(1));
}
Map<String, Object> toJson() {
var map = <String, Object>{
'type': type,

View File

@ -12,4 +12,9 @@ class StateTree {
final root = Node.fromJson(document);
return StateTree(root: root);
}
// bool insert(Path path, Node node) {
// final insertedNode = root
// return false;
// }
}

View File

@ -14,5 +14,10 @@ void main() {
expect(stateTree.root.type, 'root');
expect(stateTree.root.toJson(), data['document']);
expect(stateTree.root.children.last.type, 'video');
final checkBoxNode = stateTree.root.childAtPath([1, 0]);
expect(checkBoxNode != null, true);
final textType = checkBoxNode!.attributes['text-type'];
expect(textType != null, true);
});
}