fix: could not remove text style when pressing enter in empty text node

This commit is contained in:
Lucas.Xu 2022-08-09 19:03:55 +08:00
parent 4223324689
commit 1391d202a9
3 changed files with 34 additions and 19 deletions

View File

@ -137,17 +137,16 @@ class TransactionBuilder {
replaceText(TextNode node, int index, int length, String content,
[Attributes? attributes]) {
var newAttributes = attributes;
if (attributes == null) {
final ops = node.delta.slice(index, index + length).operations;
if (ops.isNotEmpty) {
newAttributes = ops.first.attributes;
}
}
textEdit(
node,
() => Delta().retain(index).delete(length).insert(
content,
attributes ??
node.delta
.slice(index, index + length)
.operations
.first
.attributes,
),
() => Delta().retain(index).delete(length).insert(content, newAttributes),
);
afterSelection = Selection.collapsed(
Position(

View File

@ -51,6 +51,7 @@ class StyleKey {
];
static List<String> globalStyleKeys = [
StyleKey.subtype,
StyleKey.heading,
StyleKey.checkbox,
StyleKey.bulletedList,

View File

@ -67,16 +67,31 @@ FlowyKeyEventHandler enterWithoutShiftInTextNodesHandler =
// If selection is collapsed and position.start.offset == 0,
// insert a empty text node before.
if (selection.isCollapsed && selection.start.offset == 0) {
final afterSelection = Selection.collapsed(
Position(path: textNode.path.next, offset: 0),
);
TransactionBuilder(editorState)
..insertNode(
textNode.path,
TextNode.empty(),
)
..afterSelection = afterSelection
..commit();
if (textNode.toRawString().isEmpty) {
final afterSelection = Selection.collapsed(
Position(path: textNode.path, offset: 0),
);
TransactionBuilder(editorState)
..updateNode(
textNode,
Attributes.fromIterable(
StyleKey.globalStyleKeys,
value: (_) => null,
))
..afterSelection = afterSelection
..commit();
} else {
final afterSelection = Selection.collapsed(
Position(path: textNode.path.next, offset: 0),
);
TransactionBuilder(editorState)
..insertNode(
textNode.path,
TextNode.empty(),
)
..afterSelection = afterSelection
..commit();
}
return KeyEventResult.handled;
}