feat: make following lines incremental

This commit is contained in:
Vincent Chan 2022-09-06 16:45:25 +08:00
parent 565617d1f0
commit 69f04d0958
3 changed files with 53 additions and 13 deletions
frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers

@ -4,6 +4,7 @@ import 'package:flutter/services.dart';
import 'package:appflowy_editor/src/extensions/path_extensions.dart';
import 'package:appflowy_editor/src/render/rich_text/rich_text_style.dart';
import './number_list_helper.dart';
/// Handle some cases where enter is pressed and shift is not pressed.
///
@ -101,8 +102,9 @@ ShortcutEventHandler enterWithoutShiftInTextNodesHandler =
// split the node into two nodes with style
Attributes attributes = _attributesFromPreviousLine(textNode);
final nextPath = textNode.path.next;
final afterSelection = Selection.collapsed(
Position(path: textNode.path.next, offset: 0),
Position(path: nextPath, offset: 0),
);
TransactionBuilder(editorState)
@ -124,7 +126,7 @@ ShortcutEventHandler enterWithoutShiftInTextNodesHandler =
// If the new type of a text node is number list,
// the numbers of the following nodes should be incremental.
if (textNode.subtype == StyleKey.numberList) {
_makeFollowingNodeIncremental(editorState, textNode);
makeFollowingNodesIncremental(editorState, nextPath, afterSelection);
}
return KeyEventResult.handled;
@ -156,8 +158,3 @@ Attributes _nextNumberAttributesFromPreviousLine(
copy[StyleKey.number] = prevNum == null ? 1 : prevNum + 1;
return copy;
}
void _makeFollowingNodeIncremental(EditorState editorState, TextNode textNode) {
debugPrint("following nodes");
TransactionBuilder(editorState).commit();
}

@ -0,0 +1,37 @@
import 'package:appflowy_editor/src/document/selection.dart';
import 'package:appflowy_editor/src/editor_state.dart';
import 'package:appflowy_editor/src/render/rich_text/rich_text_style.dart';
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) {
final insertNode = editorState.document.nodeAtPath(insertPath);
if (insertNode == null) {
return;
}
final int beginNum = insertNode.attributes[StyleKey.number] as int;
int numPtr = beginNum + 1;
var ptr = insertNode.next;
final builder = TransactionBuilder(editorState);
while (ptr != null) {
if (ptr.subtype != StyleKey.numberList) {
break;
}
final currentNum = ptr.attributes[StyleKey.number] as int;
if (currentNum != numPtr) {
Attributes updateAttributes = {};
updateAttributes[StyleKey.number] = numPtr;
builder.updateNode(ptr, updateAttributes);
}
ptr = ptr.next;
numPtr++;
}
builder.afterSelection = afterSelection;
builder.commit();
}

@ -8,6 +8,7 @@ import 'package:appflowy_editor/src/document/selection.dart';
import 'package:appflowy_editor/src/editor_state.dart';
import 'package:appflowy_editor/src/operation/transaction_builder.dart';
import 'package:appflowy_editor/src/render/rich_text/rich_text_style.dart';
import './number_list_helper.dart';
@visibleForTesting
List<String> get checkboxListSymbols => _checkboxListSymbols;
@ -76,17 +77,22 @@ KeyEventResult _toNumberList(EditorState editorState, TextNode textNode,
return KeyEventResult.ignored;
}
final afterSelection = Selection.collapsed(Position(
path: textNode.path,
offset: 0,
));
final insertPath = textNode.path;
TransactionBuilder(editorState)
..deleteText(textNode, 0, matchText.length)
..updateNode(textNode,
{StyleKey.subtype: StyleKey.numberList, StyleKey.number: numValue})
..afterSelection = Selection.collapsed(
Position(
path: textNode.path,
offset: 0,
),
)
..afterSelection = afterSelection
..commit();
makeFollowingNodesIncremental(editorState, insertPath, afterSelection);
return KeyEventResult.handled;
}