Skip to content

Commit 6b14983

Browse files
authored
fix: #1997 Exception trhown after inserting '/' slash in middle of text (#1998)
1 parent 9a4cde0 commit 6b14983

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

frontend/appflowy_flutter/packages/appflowy_editor/lib/src/core/transform/transaction.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,19 @@ extension TextTransaction on Transaction {
264264
if (index != 0 && attributes == null) {
265265
newAttributes =
266266
textNode.delta.slice(max(index - 1, 0), index).first.attributes;
267-
if (newAttributes != null) {
268-
newAttributes = {...newAttributes}; // make a copy
269-
} else {
270-
newAttributes =
271-
textNode.delta.slice(index, index + length).first.attributes;
267+
if (newAttributes == null) {
268+
final slicedDelta = textNode.delta.slice(index, index + length);
269+
if (slicedDelta.isNotEmpty) {
270+
newAttributes = slicedDelta.first.attributes;
271+
}
272272
}
273273
}
274274
updateText(
275275
textNode,
276276
Delta()
277277
..retain(index)
278278
..delete(length)
279-
..insert(text, attributes: newAttributes),
279+
..insert(text, attributes: {...newAttributes ?? {}}),
280280
);
281281
afterSelection = Selection.collapsed(
282282
Position(

frontend/appflowy_flutter/packages/appflowy_editor/test/service/internal_key_event_handlers/slash_handler_test.dart

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ void main() async {
1010
});
1111

1212
group('slash_handler.dart', () {
13-
testWidgets('Presses / to trigger selection menu', (tester) async {
13+
testWidgets('Presses / to trigger selection menu in 0 index',
14+
(tester) async {
1415
const text = 'Welcome to Appflowy 😁';
1516
const lines = 3;
1617
final editor = tester.editor;
@@ -41,5 +42,38 @@ void main() async {
4142
findsNothing,
4243
);
4344
});
45+
46+
testWidgets('Presses / to trigger selection menu in not 0 index',
47+
(tester) async {
48+
const text = 'Welcome to Appflowy 😁';
49+
const lines = 3;
50+
final editor = tester.editor;
51+
for (var i = 0; i < lines; i++) {
52+
editor.insertTextNode(text);
53+
}
54+
await editor.startTesting();
55+
await editor.updateSelection(Selection.single(path: [1], startOffset: 5));
56+
await editor.pressLogicKey(LogicalKeyboardKey.slash);
57+
58+
await tester.pumpAndSettle(const Duration(milliseconds: 1000));
59+
60+
expect(
61+
find.byType(SelectionMenuWidget, skipOffstage: false),
62+
findsOneWidget,
63+
);
64+
65+
for (final item in defaultSelectionMenuItems) {
66+
expect(find.text(item.name), findsOneWidget);
67+
}
68+
69+
await editor.updateSelection(Selection.single(path: [1], startOffset: 0));
70+
71+
await tester.pumpAndSettle(const Duration(milliseconds: 200));
72+
73+
expect(
74+
find.byType(SelectionMenuItemWidget, skipOffstage: false),
75+
findsNothing,
76+
);
77+
});
4478
});
4579
}

0 commit comments

Comments
 (0)