From 6e71ec23a111e71bc7fd6f696fcf806215c3cf90 Mon Sep 17 00:00:00 2001
From: "Lucas.Xu" <lucas.xu@appflowy.io>
Date: Mon, 8 Aug 2022 18:24:11 +0800
Subject: [PATCH] feat: delete slash when using the popuplist and pressing
 enter key

---
 .../lib/render/rich_text/flowy_rich_text.dart   |  2 +-
 .../slash_handler.dart                          | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/frontend/app_flowy/packages/flowy_editor/lib/render/rich_text/flowy_rich_text.dart b/frontend/app_flowy/packages/flowy_editor/lib/render/rich_text/flowy_rich_text.dart
index dbf4e5f63c..366942ee6c 100644
--- a/frontend/app_flowy/packages/flowy_editor/lib/render/rich_text/flowy_rich_text.dart
+++ b/frontend/app_flowy/packages/flowy_editor/lib/render/rich_text/flowy_rich_text.dart
@@ -35,7 +35,7 @@ class FlowyRichText extends StatefulWidget {
     this.cursorHeight,
     this.cursorWidth = 2.0,
     this.textSpanDecorator,
-    this.placeholderText = ' ',
+    this.placeholderText = 'Type \'/\' for commands',
     this.placeholderTextSpanDecorator,
     required this.textNode,
     required this.editorState,
diff --git a/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/slash_handler.dart b/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/slash_handler.dart
index f824b87234..5c476cd139 100644
--- a/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/slash_handler.dart
+++ b/frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/slash_handler.dart
@@ -211,6 +211,7 @@ class _PopupListWidgetState extends State<PopupListWidget> {
 
     if (event.logicalKey == LogicalKeyboardKey.enter) {
       if (0 <= selectedIndex && selectedIndex < widget.items.length) {
+        _deleteSlash();
         widget.items[selectedIndex].handler(widget.editorState);
         return KeyEventResult.handled;
       }
@@ -239,6 +240,22 @@ class _PopupListWidgetState extends State<PopupListWidget> {
     }
     return KeyEventResult.ignored;
   }
+
+  void _deleteSlash() {
+    final selection =
+        widget.editorState.service.selectionService.currentSelection.value;
+    final nodes =
+        widget.editorState.service.selectionService.currentSelectedNodes;
+    if (selection != null && nodes.length == 1) {
+      TransactionBuilder(widget.editorState)
+        ..deleteText(
+          nodes.first as TextNode,
+          selection.start.offset - 1,
+          1,
+        )
+        ..commit();
+    }
+  }
 }
 
 class _PopupListItemWidget extends StatelessWidget {