Skip to content

Commit 462a0e4

Browse files
authored
Merge pull request #949 from LucasXu0/fix/948
test: add more test cases to toolbar_service
2 parents 93f4d43 + 25a43c2 commit 462a0e4

File tree

2 files changed

+81
-9
lines changed

2 files changed

+81
-9
lines changed

frontend/app_flowy/packages/appflowy_editor/lib/src/extensions/text_node_extensions.dart

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,34 @@ extension TextNodesExtension on List<TextNode> {
162162
final node = this[i];
163163
final Selection newSelection;
164164
if (i == 0 && pathEquals(node.path, selection.start.path)) {
165-
newSelection = selection.copyWith(
166-
end: Position(path: node.path, offset: node.toRawString().length),
167-
);
165+
if (selection.isBackward) {
166+
newSelection = selection.copyWith(
167+
end: Position(path: node.path, offset: node.toRawString().length),
168+
);
169+
} else {
170+
newSelection = selection.copyWith(
171+
end: Position(path: node.path, offset: 0),
172+
);
173+
}
168174
} else if (i == length - 1 &&
169175
pathEquals(node.path, selection.end.path)) {
170-
newSelection = selection.copyWith(
171-
start: Position(path: node.path, offset: 0),
172-
);
176+
if (selection.isBackward) {
177+
newSelection = selection.copyWith(
178+
start: Position(path: node.path, offset: 0),
179+
);
180+
} else {
181+
newSelection = selection.copyWith(
182+
start:
183+
Position(path: node.path, offset: node.toRawString().length),
184+
);
185+
}
173186
} else {
174187
newSelection = Selection(
175188
start: Position(path: node.path, offset: 0),
176189
end: Position(path: node.path, offset: node.toRawString().length),
177190
);
178191
}
179-
if (!node.allSatisfyInSelection(newSelection, styleKey, (value) {
180-
return test(value);
181-
})) {
192+
if (!node.allSatisfyInSelection(newSelection, styleKey, test)) {
182193
return false;
183194
}
184195
}

frontend/app_flowy/packages/appflowy_editor/test/service/toolbar_service_test.dart

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,67 @@ void main() async {
154154
itemWidget = _itemWidgetForId(tester, 'appflowy.toolbar.bulleted_list');
155155
expect(itemWidget.isHighlight, true);
156156
});
157+
158+
testWidgets('Test toolbar service in multi text selection', (tester) async {
159+
const text = 'Welcome to Appflowy 😁';
160+
161+
/// [h1][bold] Welcome to Appflowy 😁
162+
/// [EmptyLine]
163+
/// Welcome to Appflowy 😁
164+
final editor = tester.editor
165+
..insertTextNode(
166+
null,
167+
attributes: {
168+
StyleKey.subtype: StyleKey.heading,
169+
StyleKey.heading: StyleKey.h1,
170+
},
171+
delta: Delta([
172+
TextInsert(text, {
173+
StyleKey.bold: true,
174+
})
175+
]),
176+
)
177+
..insertTextNode(null)
178+
..insertTextNode(text);
179+
await editor.startTesting();
180+
181+
await editor.updateSelection(
182+
Selection.single(path: [2], startOffset: text.length, endOffset: 0),
183+
);
184+
expect(find.byType(ToolbarWidget), findsOneWidget);
185+
expect(
186+
_itemWidgetForId(tester, 'appflowy.toolbar.h1').isHighlight,
187+
false,
188+
);
189+
expect(
190+
_itemWidgetForId(tester, 'appflowy.toolbar.bold').isHighlight,
191+
false,
192+
);
193+
194+
await editor.updateSelection(
195+
Selection(
196+
start: Position(path: [2], offset: text.length),
197+
end: Position(path: [1], offset: 0),
198+
),
199+
);
200+
expect(find.byType(ToolbarWidget), findsOneWidget);
201+
expect(
202+
_itemWidgetForId(tester, 'appflowy.toolbar.bold').isHighlight,
203+
false,
204+
);
205+
206+
await editor.updateSelection(
207+
Selection(
208+
start: Position(path: [2], offset: text.length),
209+
end: Position(path: [0], offset: 0),
210+
),
211+
);
212+
expect(find.byType(ToolbarWidget), findsOneWidget);
213+
expect(
214+
_itemWidgetForId(tester, 'appflowy.toolbar.bold').isHighlight,
215+
false,
216+
);
217+
});
157218
});
158219
}
159220

0 commit comments

Comments
 (0)