appflowy-editor/test/new/util/document_util.dart

76 lines
1.5 KiB
Dart
Raw Permalink Normal View History

2023-04-21 21:03:35 +08:00
import 'package:appflowy_editor/appflowy_editor.dart';
2023-04-26 14:57:18 +08:00
import 'typedef_util.dart';
2023-04-21 21:03:35 +08:00
extension DocumentExtension on Document {
2023-04-26 14:57:18 +08:00
Document addParagraphs(
2023-04-21 21:03:35 +08:00
int count, {
2023-04-28 10:54:21 +08:00
TextBuilder? builder,
String? initialText,
NodeDecorator? decorator,
2023-05-01 15:13:55 +08:00
}) {
return addNodes(
count,
'paragraph',
builder: builder,
initialText: initialText,
decorator: decorator,
);
}
Document addParagraph({
TextBuilder? builder,
String? initialText,
NodeDecorator? decorator,
}) {
return addParagraphs(
1,
builder: builder,
initialText: initialText,
decorator: decorator,
);
}
Document addNodes(
int count,
String type, {
TextBuilder? builder,
String? initialText,
NodeDecorator? decorator,
2023-04-21 21:03:35 +08:00
}) {
final builder0 = builder ??
(index) => Delta()
..insert(initialText ?? '🔥 $index. Welcome to AppFlowy Editor!');
final decorator0 = decorator ?? (index, node) {};
2023-04-26 14:57:18 +08:00
final children = List.generate(count, (index) {
2023-05-01 15:13:55 +08:00
final node = Node(type: type);
2023-04-26 14:57:18 +08:00
decorator0(index, node);
node.updateAttributes({
'delta': builder0(index).toJson(),
2023-04-26 14:57:18 +08:00
});
2023-04-26 14:57:18 +08:00
return node;
});
2023-04-21 21:03:35 +08:00
return this
..insert(
[root.children.length],
2023-04-26 14:57:18 +08:00
children,
2023-04-21 21:03:35 +08:00
);
}
2023-04-28 10:54:21 +08:00
2023-05-01 15:13:55 +08:00
Document addNode(
String type, {
2023-04-28 10:54:21 +08:00
TextBuilder? builder,
String? initialText,
NodeDecorator? decorator,
}) {
2023-05-01 15:13:55 +08:00
return addNodes(
2023-04-28 10:54:21 +08:00
1,
2023-05-01 15:13:55 +08:00
type,
2023-04-28 10:54:21 +08:00
builder: builder,
initialText: initialText,
decorator: decorator,
);
}
2023-04-21 21:03:35 +08:00
}