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

67 lines
1.8 KiB
Dart
Raw Permalink Normal View History

import 'package:appflowy_editor/appflowy_editor.dart';
2023-04-21 21:03:35 +08:00
2023-04-26 14:57:18 +08:00
import 'typedef_util.dart';
2023-04-21 21:03:35 +08:00
extension NodeExtension on Node {
2023-04-26 14:57:18 +08:00
void addParagraphs(
int count, {
2023-04-28 10:54:21 +08:00
TextBuilder? builder,
String? initialText,
2023-04-26 14:57:18 +08:00
NodeDecorator? decorator,
}) {
final builder0 = builder ??
(index) => Delta()
..insert(initialText ?? '🔥 $index. Welcome to AppFlowy Editor!');
2023-04-26 14:57:18 +08:00
final decorator0 = decorator ?? (index, node) {};
final nodes = List.generate(
count,
2023-04-26 14:57:18 +08:00
(index) {
final node = Node(type: 'paragraph');
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;
},
);
nodes.forEach(insert);
}
2023-04-28 10:54:21 +08:00
void addParagraph({
TextBuilder? builder,
String? initialText,
NodeDecorator? decorator,
}) {
addParagraphs(
1,
builder: builder,
initialText: initialText,
decorator: decorator,
);
}
2023-05-03 12:01:34 +08:00
bool everyAttributeValue(
Selection selection,
String key,
bool Function(dynamic value) test,
) {
return allSatisfyInSelection(
selection,
(delta) => delta.whereType<TextInsert>().every(
(element) => test(element.attributes?[key]),
),
);
}
bool allBold(Selection selection) =>
everyAttributeValue(selection, 'bold', (value) => value == true);
bool allItalic(Selection selection) =>
everyAttributeValue(selection, 'italic', (value) => value == true);
bool allCode(Selection selection) =>
everyAttributeValue(selection, 'code', (value) => value == true);
bool allStrikethrough(Selection selection) =>
everyAttributeValue(selection, 'strikethrough', (value) => value == true);
bool allUnderline(Selection selection) =>
everyAttributeValue(selection, 'underline', (value) => value == true);
}