Pārlūkot izejas kodu

feat: find node with path or index in state tree

Lucas.Xu 2 gadi atpakaļ
vecāks
revīzija
47436bf6e2

+ 18 - 0
frontend/app_flowy/packages/flowy_editor/lib/document/node.dart

@@ -1,5 +1,7 @@
 import 'dart:collection';
 import 'dart:collection';
 
 
+import 'package:flowy_editor/document/path.dart';
+
 class Node extends LinkedListEntry<Node> {
 class Node extends LinkedListEntry<Node> {
   Node? parent;
   Node? parent;
   final String type;
   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() {
   Map<String, Object> toJson() {
     var map = <String, Object>{
     var map = <String, Object>{
       'type': type,
       'type': type,

+ 5 - 0
frontend/app_flowy/packages/flowy_editor/lib/document/state_tree.dart

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

+ 5 - 0
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.type, 'root');
     expect(stateTree.root.toJson(), data['document']);
     expect(stateTree.root.toJson(), data['document']);
     expect(stateTree.root.children.last.type, 'video');
     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);
   });
   });
 }
 }