Skip to content

Commit

Permalink
fix: unable to clear the style by toggling twice (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 authored Oct 11, 2023
1 parent 105eb14 commit 194fe2f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/src/editor/command/text_commands.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
45 changes: 45 additions & 0 deletions test/new/command/text_commands_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
}

0 comments on commit 194fe2f

Please sign in to comment.