Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

feat: find node with path or index in state tree

Lucas.Xu47436bf6e249
diff --git a/frontend/app_flowy/packages/flowy_editor/lib/document/node.dart b/frontend/app_flowy/packages/flowy_editor/lib/document/node.dart
index 095ef87980..deef71b7ec 100644
--- a/frontend/app_flowy/packages/flowy_editor/lib/document/node.dart
+++ b/frontend/app_flowy/packages/flowy_editor/lib/document/node.dart
@@ -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,
diff --git a/frontend/app_flowy/packages/flowy_editor/lib/document/state_tree.dart b/frontend/app_flowy/packages/flowy_editor/lib/document/state_tree.dart
index 3c347753c4..82ee25976c 100644
--- a/frontend/app_flowy/packages/flowy_editor/lib/document/state_tree.dart
+++ b/frontend/app_flowy/packages/flowy_editor/lib/document/state_tree.dart
@@ -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;
+ // }
}
diff --git a/frontend/app_flowy/packages/flowy_editor/test/flowy_editor_test.dart b/frontend/app_flowy/packages/flowy_editor/test/flowy_editor_test.dart
index b1b6dddb39..0c9dcfee0c 100644
--- a/frontend/app_flowy/packages/flowy_editor/test/flowy_editor_test.dart
+++ b/frontend/app_flowy/packages/flowy_editor/test/flowy_editor_test.dart
@@ -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);
});
}