feat: handle number list on delete event

This commit is contained in:
Vincent Chan 2022-09-13 19:36:22 +08:00
parent cfdd891991
commit ee0f3d3227
2 changed files with 43 additions and 23 deletions

View File

@ -10,7 +10,6 @@ import 'package:appflowy_editor/src/document/text_delta.dart';
import 'package:appflowy_editor/src/editor_state.dart';
import 'package:appflowy_editor/src/operation/operation.dart';
import 'package:appflowy_editor/src/operation/transaction.dart';
import 'package:logging/logging.dart';
/// A [TransactionBuilder] is used to build the transaction from the state.
/// It will save a snapshot of the cursor selection state automatically.

View File

@ -181,16 +181,16 @@ KeyEventResult _handleDelete(EditorState editorState, RawKeyEvent event) {
final transactionBuilder = TransactionBuilder(editorState);
if (textNodes.length == 1) {
final textNode = textNodes.first;
// The cursor is at the end of the line,
// merge next line into this line.
if (selection.start.offset >= textNode.delta.length) {
final nextNode = textNode.next;
if (nextNode == null) {
return KeyEventResult.ignored;
return _mergeNextLineIntoThisLine(
editorState,
textNode,
transactionBuilder,
selection,
);
}
if (nextNode is TextNode) {
transactionBuilder.mergeText(textNode, nextNode);
}
transactionBuilder.deleteNode(nextNode);
} else {
final index = textNode.delta.nextRunePosition(selection.start.offset);
if (selection.isCollapsed) {
transactionBuilder.deleteText(
@ -205,7 +205,6 @@ KeyEventResult _handleDelete(EditorState editorState, RawKeyEvent event) {
selection.end.offset - selection.start.offset,
);
}
}
transactionBuilder.commit();
} else {
final startPosition = selection.start;
@ -222,6 +221,28 @@ KeyEventResult _handleDelete(EditorState editorState, RawKeyEvent event) {
return KeyEventResult.handled;
}
KeyEventResult _mergeNextLineIntoThisLine(
EditorState editorState,
TextNode textNode,
TransactionBuilder transactionBuilder,
Selection selection) {
final nextNode = textNode.next;
if (nextNode == null) {
return KeyEventResult.ignored;
}
if (nextNode is TextNode) {
transactionBuilder.mergeText(textNode, nextNode);
}
transactionBuilder.deleteNode(nextNode);
transactionBuilder.commit();
if (textNode.subtype == StyleKey.numberList) {
makeFollowingNodesIncremental(editorState, textNode.path, selection);
}
return KeyEventResult.handled;
}
void _deleteTextNodes(TransactionBuilder transactionBuilder,
List<TextNode> textNodes, Selection selection) {
final first = textNodes.first;