From 8afa48ca1620edae27683df6b2a5f8d319f2d16d Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Tue, 30 Aug 2022 17:22:33 +0800 Subject: [PATCH] fix: # doesn't work #937 --- .../src/render/rich_text/heading_text.dart | 1 - .../src/render/rich_text/rich_text_style.dart | 5 ++- .../backspace_handler.dart | 4 ++- .../backspace_handler_test.dart | 32 +++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/heading_text.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/heading_text.dart index fff25dd2d5..93defaae8e 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/heading_text.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/heading_text.dart @@ -38,7 +38,6 @@ class HeadingTextNodeWidget extends StatefulWidget { } // customize - class _HeadingTextNodeWidgetState extends State with Selectable, DefaultSelectable { @override diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/rich_text_style.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/rich_text_style.dart index 93d088d66f..2cd03fe389 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/rich_text_style.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/rich_text_style.dart @@ -79,7 +79,10 @@ Map headingToFontSize = { extension NodeAttributesExtensions on Attributes { String? get heading { - if (containsKey(StyleKey.heading) && this[StyleKey.heading] is String) { + if (containsKey(StyleKey.subtype) && + containsKey(StyleKey.heading) && + this[StyleKey.subtype] == StyleKey.heading && + this[StyleKey.heading] is String) { return this[StyleKey.heading]; } return null; diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/backspace_handler.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/backspace_handler.dart index 0eeaf654de..675ee5b446 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/backspace_handler.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/backspace_handler.dart @@ -1,3 +1,4 @@ +import 'package:appflowy_editor/src/render/rich_text/rich_text_style.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -29,7 +30,8 @@ KeyEventResult _handleBackspace(EditorState editorState, RawKeyEvent event) { if (textNode.subtype != null) { transactionBuilder ..updateNode(textNode, { - 'subtype': null, + StyleKey.subtype: null, + textNode.subtype!: null, }) ..afterSelection = Selection.collapsed( Position( diff --git a/frontend/app_flowy/packages/appflowy_editor/test/service/internal_key_event_handlers/backspace_handler_test.dart b/frontend/app_flowy/packages/appflowy_editor/test/service/internal_key_event_handlers/backspace_handler_test.dart index 1976ec3250..da5d22a786 100644 --- a/frontend/app_flowy/packages/appflowy_editor/test/service/internal_key_event_handlers/backspace_handler_test.dart +++ b/frontend/app_flowy/packages/appflowy_editor/test/service/internal_key_event_handlers/backspace_handler_test.dart @@ -234,6 +234,38 @@ void main() async { (tester) async { await _deleteLastImage(tester, false); }); + + testWidgets('Removes the style of heading text and revert', (tester) async { + const text = 'Welcome to Appflowy 😁'; + final editor = tester.editor..insertTextNode(text); + await editor.startTesting(); + + await editor.updateSelection( + Selection.single(path: [0], startOffset: 0), + ); + + final textNode = editor.nodeAtPath([0]) as TextNode; + + await editor.insertText(textNode, '#', 0); + await editor.pressLogicKey(LogicalKeyboardKey.space); + expect( + (editor.nodeAtPath([0]) as TextNode).attributes.heading, + StyleKey.h1, + ); + + await editor.pressLogicKey(LogicalKeyboardKey.backspace); + expect( + textNode.attributes.heading, + null, + ); + + await editor.insertText(textNode, '#', 0); + await editor.pressLogicKey(LogicalKeyboardKey.space); + expect( + (editor.nodeAtPath([0]) as TextNode).attributes.heading, + StyleKey.h1, + ); + }); } Future _deleteFirstImage(WidgetTester tester, bool isBackward) async {