From 1391d202a971216537a5b690f0223b3455d7ac76 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Tue, 9 Aug 2022 19:03:55 +0800 Subject: [PATCH] fix: could not remove text style when pressing enter in empty text node --- .../lib/operation/transaction_builder.dart | 17 +++++---- .../lib/render/rich_text/rich_text_style.dart | 1 + ...er_without_shift_in_text_node_handler.dart | 35 +++++++++++++------ 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction_builder.dart b/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction_builder.dart index 55ca336bfb..2898f7113a 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction_builder.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/operation/transaction_builder.dart @@ -137,17 +137,16 @@ class TransactionBuilder { replaceText(TextNode node, int index, int length, String content, [Attributes? attributes]) { + var newAttributes = attributes; + if (attributes == null) { + final ops = node.delta.slice(index, index + length).operations; + if (ops.isNotEmpty) { + newAttributes = ops.first.attributes; + } + } textEdit( node, - () => Delta().retain(index).delete(length).insert( - content, - attributes ?? - node.delta - .slice(index, index + length) - .operations - .first - .attributes, - ), + () => Delta().retain(index).delete(length).insert(content, newAttributes), ); afterSelection = Selection.collapsed( Position( diff --git a/frontend/app_flowy/packages/flowy_editor/lib/render/rich_text/rich_text_style.dart b/frontend/app_flowy/packages/flowy_editor/lib/render/rich_text/rich_text_style.dart index e39aed9aa2..c44fd8dac1 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/render/rich_text/rich_text_style.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/render/rich_text/rich_text_style.dart @@ -51,6 +51,7 @@ class StyleKey { ]; static List globalStyleKeys = [ + StyleKey.subtype, StyleKey.heading, StyleKey.checkbox, StyleKey.bulletedList, diff --git a/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler.dart b/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler.dart index d2d3b14450..3dd0eef2df 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler.dart @@ -67,16 +67,31 @@ FlowyKeyEventHandler enterWithoutShiftInTextNodesHandler = // If selection is collapsed and position.start.offset == 0, // insert a empty text node before. if (selection.isCollapsed && selection.start.offset == 0) { - final afterSelection = Selection.collapsed( - Position(path: textNode.path.next, offset: 0), - ); - TransactionBuilder(editorState) - ..insertNode( - textNode.path, - TextNode.empty(), - ) - ..afterSelection = afterSelection - ..commit(); + if (textNode.toRawString().isEmpty) { + final afterSelection = Selection.collapsed( + Position(path: textNode.path, offset: 0), + ); + TransactionBuilder(editorState) + ..updateNode( + textNode, + Attributes.fromIterable( + StyleKey.globalStyleKeys, + value: (_) => null, + )) + ..afterSelection = afterSelection + ..commit(); + } else { + final afterSelection = Selection.collapsed( + Position(path: textNode.path.next, offset: 0), + ); + TransactionBuilder(editorState) + ..insertNode( + textNode.path, + TextNode.empty(), + ) + ..afterSelection = afterSelection + ..commit(); + } return KeyEventResult.handled; }