From 2810097b956cebcfc9778e0ba60e18396cd52344 Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Wed, 7 Sep 2022 14:21:16 +0800 Subject: [PATCH] feat: backspace --- .../backspace_handler.dart | 11 +++++++++++ .../enter_without_shift_in_text_node_handler.dart | 4 +++- .../number_list_helper.dart | 5 +++-- 3 files changed, 17 insertions(+), 3 deletions(-) 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 ee3f80f197..a8337543e7 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,4 +1,5 @@ import 'package:appflowy_editor/src/render/rich_text/rich_text_style.dart'; +import 'package:appflowy_editor/src/service/internal_key_event_handlers/number_list_helper.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -29,6 +30,7 @@ KeyEventResult _handleBackspace(EditorState editorState, RawKeyEvent event) { nodes.where((node) => node is! TextNode).toList(growable: false); final transactionBuilder = TransactionBuilder(editorState); + List? cancelNumberListPath; if (nonTextNodes.isNotEmpty) { transactionBuilder.deleteNodes(nonTextNodes); @@ -40,6 +42,9 @@ KeyEventResult _handleBackspace(EditorState editorState, RawKeyEvent event) { if (index < 0 && selection.isCollapsed) { // 1. style if (textNode.subtype != null) { + if (textNode.subtype == StyleKey.numberList) { + cancelNumberListPath = textNode.path; + } transactionBuilder ..updateNode(textNode, { StyleKey.subtype: null, @@ -100,6 +105,12 @@ KeyEventResult _handleBackspace(EditorState editorState, RawKeyEvent event) { transactionBuilder.commit(); } + if (cancelNumberListPath != null) { + makeFollowingNodesIncremental( + editorState, cancelNumberListPath, Selection.collapsed(selection.start), + beginNum: 0); + } + return KeyEventResult.handled; } diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler.dart index 15a1222942..0a8ee552cb 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/enter_without_shift_in_text_node_handler.dart @@ -69,7 +69,9 @@ ShortcutEventHandler enterWithoutShiftInTextNodesHandler = // If selection is collapsed and position.start.offset == 0, // insert a empty text node before. - if (selection.isCollapsed && selection.start.offset == 0) { + if (selection.isCollapsed && + selection.start.offset == 0 && + textNode.subtype != StyleKey.numberList) { if (textNode.toRawString().isEmpty && textNode.subtype != null) { final afterSelection = Selection.collapsed( Position(path: textNode.path, offset: 0), diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/number_list_helper.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/number_list_helper.dart index 7c5d846a36..4e726fc86e 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/number_list_helper.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/number_list_helper.dart @@ -5,12 +5,13 @@ import 'package:appflowy_editor/src/operation/transaction_builder.dart'; import 'package:appflowy_editor/src/document/attributes.dart'; void makeFollowingNodesIncremental( - EditorState editorState, List insertPath, Selection afterSelection) { + EditorState editorState, List insertPath, Selection afterSelection, + {int? beginNum}) { final insertNode = editorState.document.nodeAtPath(insertPath); if (insertNode == null) { return; } - final int beginNum = insertNode.attributes[StyleKey.number] as int; + beginNum ??= insertNode.attributes[StyleKey.number] as int; int numPtr = beginNum + 1; var ptr = insertNode.next;