2023-04-28 19:48:34 +08:00
|
|
|
import 'package:appflowy_editor/appflowy_editor.dart';
|
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
|
|
|
|
|
|
import '../util/util.dart';
|
|
|
|
|
|
|
|
|
|
void main() async {
|
|
|
|
|
group('allSatisfyInSelection - node', () {
|
|
|
|
|
const welcome = 'Welcome ';
|
|
|
|
|
const toAppFlowy = 'to AppFlowy';
|
|
|
|
|
const editor = ' Editor 🔥!';
|
|
|
|
|
|
|
|
|
|
// Welcome <b>|to AppFlowy</b> Editor 🔥!
|
|
|
|
|
test('the selection is collapsed', () async {
|
|
|
|
|
final document = Document.blank().addParagraph(
|
|
|
|
|
builder: (index) => Delta()
|
|
|
|
|
..insert(welcome)
|
|
|
|
|
..insert(
|
|
|
|
|
toAppFlowy,
|
|
|
|
|
attributes: {
|
|
|
|
|
'bold': true,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
..insert(editor),
|
|
|
|
|
);
|
|
|
|
|
final editorState = EditorState(document: document);
|
|
|
|
|
|
|
|
|
|
// Welcome |to AppFlowy| Editor 🔥!
|
2023-08-07 14:46:09 +02:00
|
|
|
final selection = Selection.collapsed(
|
|
|
|
|
Position(path: [0], offset: welcome.length),
|
2023-04-28 19:48:34 +08:00
|
|
|
);
|
|
|
|
|
editorState.selection = selection;
|
|
|
|
|
final node = editorState.getNodeAtPath([0]);
|
|
|
|
|
final result = node!.allSatisfyInSelection(selection, (delta) {
|
|
|
|
|
final textInserts = delta.whereType<TextInsert>();
|
2025-12-23 15:12:57 +08:00
|
|
|
|
2023-04-28 19:48:34 +08:00
|
|
|
return textInserts
|
|
|
|
|
.every((element) => element.attributes?['bold'] == true);
|
|
|
|
|
});
|
|
|
|
|
expect(result, false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Welcome |<b>to AppFlowy</b>| Editor 🔥!
|
|
|
|
|
test('the selection is single and not collapsed - 1', () async {
|
|
|
|
|
final document = Document.blank().addParagraph(
|
|
|
|
|
builder: (index) => Delta()
|
|
|
|
|
..insert(welcome)
|
|
|
|
|
..insert(
|
|
|
|
|
toAppFlowy,
|
|
|
|
|
attributes: {
|
|
|
|
|
'bold': true,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
..insert(editor),
|
|
|
|
|
);
|
|
|
|
|
final editorState = EditorState(document: document);
|
|
|
|
|
|
|
|
|
|
// Welcome |to AppFlowy| Editor 🔥!
|
|
|
|
|
final selection = Selection.single(
|
|
|
|
|
path: [0],
|
|
|
|
|
startOffset: welcome.length,
|
|
|
|
|
endOffset: welcome.length + toAppFlowy.length,
|
|
|
|
|
);
|
|
|
|
|
editorState.selection = selection;
|
|
|
|
|
final node = editorState.getNodeAtPath([0]);
|
|
|
|
|
final result = node!.allSatisfyInSelection(selection, (delta) {
|
|
|
|
|
final textInserts = delta.whereType<TextInsert>();
|
2025-12-23 15:12:57 +08:00
|
|
|
|
2023-04-28 19:48:34 +08:00
|
|
|
return textInserts
|
|
|
|
|
.every((element) => element.attributes?['bold'] == true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result, true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// |Welcome <b>to AppFlowy</b>| Editor 🔥!
|
|
|
|
|
test('the selection is single and not collapsed - 2', () async {
|
|
|
|
|
final document = Document.blank().addParagraph(
|
|
|
|
|
builder: (index) => Delta()
|
|
|
|
|
..insert(welcome)
|
|
|
|
|
..insert(
|
|
|
|
|
toAppFlowy,
|
|
|
|
|
attributes: {
|
|
|
|
|
'bold': true,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
..insert(editor),
|
|
|
|
|
);
|
|
|
|
|
final editorState = EditorState(document: document);
|
|
|
|
|
|
|
|
|
|
// Welcome |to AppFlowy| Editor 🔥!
|
|
|
|
|
final selection = Selection.single(
|
|
|
|
|
path: [0],
|
|
|
|
|
startOffset: 0,
|
|
|
|
|
endOffset: welcome.length + toAppFlowy.length,
|
|
|
|
|
);
|
|
|
|
|
editorState.selection = selection;
|
|
|
|
|
final node = editorState.getNodeAtPath([0]);
|
|
|
|
|
final result = node!.allSatisfyInSelection(selection, (delta) {
|
|
|
|
|
final textInserts = delta.whereType<TextInsert>();
|
2025-12-23 15:12:57 +08:00
|
|
|
|
2023-04-28 19:48:34 +08:00
|
|
|
return textInserts
|
|
|
|
|
.every((element) => element.attributes?['bold'] == true);
|
|
|
|
|
});
|
|
|
|
|
expect(result, false);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
group('allSatisfyInSelection - nodes', () {
|
|
|
|
|
const welcome = 'Welcome ';
|
|
|
|
|
const toAppFlowy = 'to AppFlowy';
|
|
|
|
|
const editor = ' Editor 🔥!';
|
|
|
|
|
|
|
|
|
|
// Welcome <b>|to AppFlowy Editor 🔥!</b>
|
|
|
|
|
// <b>Welcome to AppFlowy|</b> Editor 🔥!
|
|
|
|
|
test('the selection is not collapsed and not single - 1', () async {
|
|
|
|
|
final document = Document.blank().addParagraph(
|
|
|
|
|
builder: (index) => Delta()
|
|
|
|
|
..insert(welcome)
|
|
|
|
|
..insert(
|
|
|
|
|
toAppFlowy + editor,
|
|
|
|
|
attributes: {
|
|
|
|
|
'bold': true,
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
)..addParagraph(
|
|
|
|
|
builder: (index) => Delta()
|
|
|
|
|
..insert(
|
|
|
|
|
welcome + toAppFlowy,
|
|
|
|
|
attributes: {
|
|
|
|
|
'bold': true,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
..insert(
|
|
|
|
|
editor,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
final editorState = EditorState(document: document);
|
|
|
|
|
|
|
|
|
|
// Welcome <b>|to AppFlowy Editor 🔥!</b>
|
|
|
|
|
// <b>Welcome to AppFlowy|</b> Editor 🔥!
|
|
|
|
|
final selection = Selection(
|
|
|
|
|
start: Position(path: [0], offset: welcome.length),
|
|
|
|
|
end: Position(path: [1], offset: welcome.length + toAppFlowy.length),
|
|
|
|
|
);
|
|
|
|
|
editorState.selection = selection;
|
|
|
|
|
final nodes = editorState.getNodesInSelection(selection);
|
|
|
|
|
final result = nodes.allSatisfyInSelection(selection, (delta) {
|
|
|
|
|
final textInserts = delta.whereType<TextInsert>();
|
2025-12-23 15:12:57 +08:00
|
|
|
|
2023-04-28 19:48:34 +08:00
|
|
|
return textInserts
|
|
|
|
|
.every((element) => element.attributes?['bold'] == true);
|
|
|
|
|
});
|
|
|
|
|
expect(result, true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// |Welcome <b>to AppFlowy Editor 🔥!</b>
|
|
|
|
|
// <b>Welcome to AppFlowy</b> Editor 🔥!|
|
|
|
|
|
test('the selection is not collapsed and not single - 2', () async {
|
|
|
|
|
final document = Document.blank().addParagraph(
|
|
|
|
|
builder: (index) => Delta()
|
|
|
|
|
..insert(welcome)
|
|
|
|
|
..insert(
|
|
|
|
|
toAppFlowy + editor,
|
|
|
|
|
attributes: {
|
|
|
|
|
'bold': true,
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
)..addParagraph(
|
|
|
|
|
builder: (index) => Delta()
|
|
|
|
|
..insert(
|
|
|
|
|
welcome + toAppFlowy,
|
|
|
|
|
attributes: {
|
|
|
|
|
'bold': true,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
..insert(
|
|
|
|
|
editor,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
final editorState = EditorState(document: document);
|
|
|
|
|
|
|
|
|
|
// |Welcome <b>to AppFlowy Editor 🔥!</b>
|
|
|
|
|
// <b>Welcome to AppFlowy</b> Editor 🔥!|
|
|
|
|
|
final selection = Selection(
|
2026-07-27 15:37:27 +08:00
|
|
|
start: Position(path: [0]),
|
2023-04-28 19:48:34 +08:00
|
|
|
end: Position(
|
|
|
|
|
path: [1],
|
|
|
|
|
offset: welcome.length + toAppFlowy.length + editor.length,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
editorState.selection = selection;
|
|
|
|
|
final nodes = editorState.getNodesInSelection(selection);
|
|
|
|
|
final result = nodes.allSatisfyInSelection(selection, (delta) {
|
|
|
|
|
final textInserts = delta.whereType<TextInsert>();
|
2025-12-23 15:12:57 +08:00
|
|
|
|
2023-04-28 19:48:34 +08:00
|
|
|
return textInserts
|
|
|
|
|
.every((element) => element.attributes?['bold'] == true);
|
|
|
|
|
});
|
|
|
|
|
expect(result, false);
|
|
|
|
|
});
|
2024-11-25 09:41:38 +08:00
|
|
|
|
|
|
|
|
test('2 non-empty nodes with 1 empty node', () {
|
|
|
|
|
final document = Document.blank()
|
|
|
|
|
..addParagraph(
|
|
|
|
|
builder: (index) => Delta()
|
|
|
|
|
..insert(
|
|
|
|
|
'Hello',
|
|
|
|
|
attributes: {AppFlowyRichTextKeys.bold: true},
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
..addParagraph(
|
|
|
|
|
builder: (index) => Delta(),
|
|
|
|
|
)
|
|
|
|
|
..addParagraph(
|
|
|
|
|
builder: (index) => Delta()
|
|
|
|
|
..insert(
|
|
|
|
|
'World',
|
|
|
|
|
attributes: {
|
|
|
|
|
AppFlowyRichTextKeys.bold: true,
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
final editorState = EditorState(document: document);
|
|
|
|
|
final selection = Selection(
|
2026-07-27 15:37:27 +08:00
|
|
|
start: Position(path: [0]),
|
2024-11-25 09:41:38 +08:00
|
|
|
end: Position(path: [2], offset: 5),
|
|
|
|
|
);
|
|
|
|
|
final nodes = editorState.getNodesInSelection(selection);
|
|
|
|
|
final isHighlight = nodes.allSatisfyInSelection(
|
|
|
|
|
selection,
|
|
|
|
|
(delta) =>
|
|
|
|
|
delta.isNotEmpty &&
|
|
|
|
|
delta.everyAttributes(
|
|
|
|
|
(attr) => attr[AppFlowyRichTextKeys.bold] == true,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
expect(isHighlight, true);
|
|
|
|
|
});
|
2023-04-28 19:48:34 +08:00
|
|
|
});
|
|
|
|
|
}
|