diff --git a/lib/src/editor/command/text_commands.dart b/lib/src/editor/command/text_commands.dart index a0cb434f7..28da80c50 100644 --- a/lib/src/editor/command/text_commands.dart +++ b/lib/src/editor/command/text_commands.dart @@ -190,21 +190,25 @@ extension TextTransforms on EditorState { final nodes = getNodesInSelection(selection); if (selection.isCollapsed) { - // get the attributes from the previous one character. - selection = selection.copyWith( - start: selection.start.copyWith( - offset: max( - selection.startIndex - 1, - 0, + if (toggledStyle.containsKey(key)) { + toggledStyle[key] = !toggledStyle[key]!; + } else { + // get the attributes from the previous one character. + selection = selection.copyWith( + start: selection.start.copyWith( + offset: max( + selection.startIndex - 1, + 0, + ), ), - ), - ); - final toggled = nodes.allSatisfyInSelection(selection, (delta) { - return delta.everyAttributes( - (attributes) => attributes[key] == true, ); - }); - toggledStyle[key] = !toggled; + final toggled = nodes.allSatisfyInSelection(selection, (delta) { + return delta.everyAttributes( + (attributes) => attributes[key] == true, + ); + }); + toggledStyle[key] = !toggled; + } } else { final isHighlight = nodes.allSatisfyInSelection(selection, (delta) { return delta.everyAttributes( diff --git a/test/new/command/text_commands_test.dart b/test/new/command/text_commands_test.dart index ed032a9d5..d616d3f3e 100644 --- a/test/new/command/text_commands_test.dart +++ b/test/new/command/text_commands_test.dart @@ -377,5 +377,50 @@ void main() async { expect(editor.editorState.toggledStyle, isEmpty); await editor.dispose(); }); + + testWidgets('toggle twice to reset the toggled style', (tester) async { + const text = ''; + final editor = tester.editor..addParagraph(initialText: text); + + await editor.startTesting(); + await editor.updateSelection( + Selection.single(path: [0], startOffset: text.length), + ); + + // toggle bold, italic, underline + final keys = [ + LogicalKeyboardKey.keyB, + LogicalKeyboardKey.keyI, + LogicalKeyboardKey.keyU, + ]; + for (final key in keys) { + await editor.pressKey( + key: key, + isControlPressed: !Platform.isMacOS, + isMetaPressed: Platform.isMacOS, + ); + } + + // reset + for (final key in keys) { + await editor.pressKey( + key: key, + isControlPressed: !Platform.isMacOS, + isMetaPressed: Platform.isMacOS, + ); + } + + await editor.ime.insertText('Hello'); + final delta1 = editor.nodeAtPath([0])!.delta!; + expect(delta1.toJson(), [ + { + "insert": "Hello", + "attributes": {"bold": false, "italic": false, "underline": false} + } + ]); + + expect(editor.editorState.toggledStyle, isEmpty); + await editor.dispose(); + }); }); }