Browse Source

fix: # doesn't work #937

Lucas.Xu 2 years ago
parent
commit
8afa48ca16

+ 0 - 1
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<HeadingTextNodeWidget>
     with Selectable, DefaultSelectable {
   @override

+ 4 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/rich_text_style.dart

@@ -79,7 +79,10 @@ Map<String, double> 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;

+ 3 - 1
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(

+ 32 - 0
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<void> _deleteFirstImage(WidgetTester tester, bool isBackward) async {