Skip to content

Commit e37b44f

Browse files
authored
fix: resolve "Bad state: no element" when in-table paragraph deleted (#471)
1 parent fa207e5 commit e37b44f

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/backspace_command.dart

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,33 @@ CommandShortcutEventHandler _backspaceInCollapsedSelection = (editorState) {
6767
),
6868
);
6969
} else {
70-
// merge with the previous node contains delta.
71-
final previousNodeWithDelta =
72-
node.previousNodeWhere((element) => element.delta != null);
73-
if (previousNodeWithDelta != null) {
74-
assert(previousNodeWithDelta.delta != null);
70+
Node? tableParent =
71+
node.findParent((element) => element.type == TableBlockKeys.type);
72+
Node? prevTableParent;
73+
final prev = node.previousNodeWhere((element) {
74+
prevTableParent = element
75+
.findParent((element) => element.type == TableBlockKeys.type);
76+
// break if only one is in a table or they're in different tables
77+
return tableParent != prevTableParent ||
78+
// merge with the previous node contains delta.
79+
element.delta != null;
80+
});
81+
// table nodes should be deleted using the table menu
82+
// in-table paragraphs should only be deleted inside the table
83+
if (prev != null && tableParent == prevTableParent) {
84+
assert(prev.delta != null);
7585
transaction
76-
..mergeText(previousNodeWithDelta, node)
86+
..mergeText(prev, node)
7787
..insertNodes(
7888
// insert children to previous node
79-
previousNodeWithDelta.path.next,
89+
prev.path.next,
8090
node.children.toList(),
8191
)
8292
..deleteNode(node)
8393
..afterSelection = Selection.collapsed(
8494
Position(
85-
path: previousNodeWithDelta.path,
86-
offset: previousNodeWithDelta.delta!.length,
95+
path: prev.path,
96+
offset: prev.delta!.length,
8797
),
8898
);
8999
} else {

lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/delete_command.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,21 @@ CommandShortcutEventHandler _deleteInCollapsedSelection = (editorState) {
4444

4545
final transaction = editorState.transaction;
4646

47-
// merge the next node with delta
4847
if (position.offset == delta.length) {
49-
final next = node.findDownward((element) => element.delta != null);
50-
if (next != null) {
48+
Node? tableParent =
49+
node.findParent((element) => element.type == TableBlockKeys.type);
50+
Node? nextTableParent;
51+
final next = node.findDownward((element) {
52+
nextTableParent =
53+
element.findParent((element) => element.type == TableBlockKeys.type);
54+
// break if only one is in a table or they're in different tables
55+
return tableParent != nextTableParent ||
56+
// merge the next node with delta
57+
element.delta != null;
58+
});
59+
// table nodes should be deleted using the table menu
60+
// in-table paragraphs should only be deleted inside the table
61+
if (next != null && tableParent == nextTableParent) {
5162
if (next.children.isNotEmpty) {
5263
final path = node.path + [node.children.length];
5364
transaction.insertNodes(path, next.children);

lib/src/extensions/node_extensions.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ extension NodeExtensions on Node {
132132
}
133133
return false;
134134
}
135+
136+
Node? findParent(bool Function(Node element) test) {
137+
if (test(this)) {
138+
return this;
139+
}
140+
final parent = this.parent;
141+
return parent?.findParent(test);
142+
}
135143
}
136144

137145
extension NodesExtensions<T extends Node> on List<T> {

0 commit comments

Comments
 (0)