feat: backspace

This commit is contained in:
Vincent Chan 2022-09-07 14:21:16 +08:00
parent 69f04d0958
commit 2810097b95
3 changed files with 17 additions and 3 deletions

View File

@ -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<int>? 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;
}

View File

@ -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),

View File

@ -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<int> insertPath, Selection afterSelection) {
EditorState editorState, List<int> 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;