@@ -2,7 +2,7 @@ import 'dart:collection';
2
2
3
3
import 'package:appflowy_editor/appflowy_editor.dart' ;
4
4
import 'package:flutter/material.dart' ;
5
- import 'package:nanoid/nanoid .dart' ;
5
+ import 'package:nanoid/non_secure .dart' ;
6
6
7
7
abstract class NodeExternalValues {
8
8
const NodeExternalValues ();
@@ -39,8 +39,8 @@ final class Node extends ChangeNotifier with LinkedListEntry<Node> {
39
39
),
40
40
), // unlink the given children to avoid the error of "node has already a parent"
41
41
_attributes = attributes,
42
- id = id ?? nanoid (10 ) {
43
- for (final child in this . children) {
42
+ id = id ?? nanoid (6 ) {
43
+ for (final child in children) {
44
44
child.parent = this ;
45
45
}
46
46
}
@@ -80,7 +80,12 @@ final class Node extends ChangeNotifier with LinkedListEntry<Node> {
80
80
81
81
/// The children of the node.
82
82
final LinkedList <Node > _children;
83
- Iterable <Node > get children => _children.toList (growable: false );
83
+ List <Node > get children {
84
+ _cacheChildren ?? = _children.toList (growable: false );
85
+ return _cacheChildren! ;
86
+ }
87
+
88
+ List <Node >? _cacheChildren;
84
89
85
90
/// The attributes of the node.
86
91
Attributes _attributes;
@@ -141,7 +146,9 @@ final class Node extends ChangeNotifier with LinkedListEntry<Node> {
141
146
142
147
entry.parent = this ;
143
148
144
- if (children.isEmpty) {
149
+ _cacheChildren = null ;
150
+
151
+ if (_children.isEmpty) {
145
152
_children.add (entry);
146
153
notifyListeners ();
147
154
return ;
@@ -164,6 +171,8 @@ final class Node extends ChangeNotifier with LinkedListEntry<Node> {
164
171
entry.parent = parent;
165
172
super .insertAfter (entry);
166
173
174
+ parent? ._cacheChildren = null ;
175
+
167
176
// Notifies the new node.
168
177
parent? .notifyListeners ();
169
178
}
@@ -173,6 +182,8 @@ final class Node extends ChangeNotifier with LinkedListEntry<Node> {
173
182
entry.parent = parent;
174
183
super .insertBefore (entry);
175
184
185
+ parent? ._cacheChildren = null ;
186
+
176
187
// Notifies the new node.
177
188
parent? .notifyListeners ();
178
189
}
@@ -185,6 +196,8 @@ final class Node extends ChangeNotifier with LinkedListEntry<Node> {
185
196
Log .editor.debug ('delete Node $this from path $path ' );
186
197
super .unlink ();
187
198
199
+ parent? ._cacheChildren = null ;
200
+
188
201
parent? .notifyListeners ();
189
202
parent = null ;
190
203
}
@@ -230,12 +243,12 @@ final class Node extends ChangeNotifier with LinkedListEntry<Node> {
230
243
}) {
231
244
final node = Node (
232
245
type: type ?? this .type,
233
- id: nanoid (10 ),
246
+ id: nanoid (6 ),
234
247
attributes: attributes ?? {...this .attributes},
235
248
children: children ?? [],
236
249
);
237
- if (children == null && this .children .isNotEmpty) {
238
- for (final child in this .children ) {
250
+ if (children == null && _children .isNotEmpty) {
251
+ for (final child in _children ) {
239
252
node._children.add (
240
253
child.copyWith ()..parent = node,
241
254
);
@@ -246,11 +259,12 @@ final class Node extends ChangeNotifier with LinkedListEntry<Node> {
246
259
}
247
260
248
261
Path _computePath ([Path previous = const []]) {
262
+ final parent = this .parent;
249
263
if (parent == null ) {
250
264
return previous;
251
265
}
252
- final index = parent! .children. toList () .indexOf (this );
253
- return parent! ._computePath ([index, ...previous]);
266
+ final index = parent.children.indexOf (this );
267
+ return parent._computePath ([index, ...previous]);
254
268
}
255
269
}
256
270
0 commit comments