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? parent; final String type; @@ -40,6 +42,22 @@ class Node extends LinkedListEntry { ); } + 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 toJson() { var map = { '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); }); }