mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: find node with path or index in state tree
This commit is contained in:
parent
76999c6a46
commit
47436bf6e2
@ -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,
|
||||
|
@ -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;
|
||||
// }
|
||||
}
|
||||
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user