fix: cancel number list style after enter in empty line

This commit is contained in:
Vincent Chan
2022-09-13 11:26:52 +08:00
parent 4402f4dec9
commit 06bd6064ac
2 changed files with 37 additions and 19 deletions

View File

@ -76,9 +76,7 @@ ShortcutEventHandler enterWithoutShiftInTextNodesHandler =
// If selection is collapsed and position.start.offset == 0, // If selection is collapsed and position.start.offset == 0,
// insert a empty text node before. // insert a empty text node before.
if (selection.isCollapsed && if (selection.isCollapsed && selection.start.offset == 0) {
selection.start.offset == 0 &&
textNode.subtype != StyleKey.numberList) {
if (textNode.toRawString().isEmpty && textNode.subtype != null) { if (textNode.toRawString().isEmpty && textNode.subtype != null) {
final afterSelection = Selection.collapsed( final afterSelection = Selection.collapsed(
Position(path: textNode.path, offset: 0), Position(path: textNode.path, offset: 0),
@ -92,17 +90,44 @@ ShortcutEventHandler enterWithoutShiftInTextNodesHandler =
)) ))
..afterSelection = afterSelection ..afterSelection = afterSelection
..commit(); ..commit();
final nextNode = textNode.next;
if (nextNode is TextNode && nextNode.subtype == StyleKey.numberList) {
makeFollowingNodesIncremental(
editorState, textNode.path, afterSelection,
beginNum: 0);
}
} else { } else {
final subtype = textNode.subtype;
final afterSelection = Selection.collapsed( final afterSelection = Selection.collapsed(
Position(path: textNode.path.next, offset: 0), Position(path: textNode.path.next, offset: 0),
); );
TransactionBuilder(editorState)
..insertNode( if (subtype == StyleKey.numberList) {
textNode.path, final prevNumber = textNode.attributes[StyleKey.number] as int;
TextNode.empty(), final newNode = TextNode.empty();
) newNode.attributes[StyleKey.subtype] = StyleKey.numberList;
..afterSelection = afterSelection newNode.attributes[StyleKey.number] = prevNumber;
..commit(); final insertPath = textNode.path;
TransactionBuilder(editorState)
..insertNode(
insertPath,
newNode,
)
..afterSelection = afterSelection
..commit();
makeFollowingNodesIncremental(editorState, insertPath, afterSelection,
beginNum: prevNumber);
} else {
TransactionBuilder(editorState)
..insertNode(
textNode.path,
TextNode.empty(),
)
..afterSelection = afterSelection
..commit();
}
} }
return KeyEventResult.handled; return KeyEventResult.handled;
} }

View File

@ -176,15 +176,8 @@ Future<void> _testStyleNeedToBeCopy(WidgetTester tester, String style) async {
await editor.pressLogicKey( await editor.pressLogicKey(
LogicalKeyboardKey.enter, LogicalKeyboardKey.enter,
); );
if (style == StyleKey.numberList) { expect(editor.documentSelection, Selection.single(path: [4], startOffset: 0));
expect( expect(editor.nodeAtPath([4])?.subtype, null);
editor.documentSelection, Selection.single(path: [5], startOffset: 0));
expect(editor.nodeAtPath([4])?.subtype, StyleKey.numberList);
} else {
expect(
editor.documentSelection, Selection.single(path: [4], startOffset: 0));
expect(editor.nodeAtPath([4])?.subtype, null);
}
} }
Future<void> _testMultipleSelection( Future<void> _testMultipleSelection(