Skip to content

Commit

Permalink
fix: resolve "Bad state: no element" when in-table paragraph deleted (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sun-jiao authored Sep 20, 2023
1 parent fa207e5 commit e37b44f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,33 @@ CommandShortcutEventHandler _backspaceInCollapsedSelection = (editorState) {
),
);
} else {
// merge with the previous node contains delta.
final previousNodeWithDelta =
node.previousNodeWhere((element) => element.delta != null);
if (previousNodeWithDelta != null) {
assert(previousNodeWithDelta.delta != null);
Node? tableParent =
node.findParent((element) => element.type == TableBlockKeys.type);
Node? prevTableParent;
final prev = node.previousNodeWhere((element) {
prevTableParent = element
.findParent((element) => element.type == TableBlockKeys.type);
// break if only one is in a table or they're in different tables
return tableParent != prevTableParent ||
// merge with the previous node contains delta.
element.delta != null;
});
// table nodes should be deleted using the table menu
// in-table paragraphs should only be deleted inside the table
if (prev != null && tableParent == prevTableParent) {
assert(prev.delta != null);
transaction
..mergeText(previousNodeWithDelta, node)
..mergeText(prev, node)
..insertNodes(
// insert children to previous node
previousNodeWithDelta.path.next,
prev.path.next,
node.children.toList(),
)
..deleteNode(node)
..afterSelection = Selection.collapsed(
Position(
path: previousNodeWithDelta.path,
offset: previousNodeWithDelta.delta!.length,
path: prev.path,
offset: prev.delta!.length,
),
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,21 @@ CommandShortcutEventHandler _deleteInCollapsedSelection = (editorState) {

final transaction = editorState.transaction;

// merge the next node with delta
if (position.offset == delta.length) {
final next = node.findDownward((element) => element.delta != null);
if (next != null) {
Node? tableParent =
node.findParent((element) => element.type == TableBlockKeys.type);
Node? nextTableParent;
final next = node.findDownward((element) {
nextTableParent =
element.findParent((element) => element.type == TableBlockKeys.type);
// break if only one is in a table or they're in different tables
return tableParent != nextTableParent ||
// merge the next node with delta
element.delta != null;
});
// table nodes should be deleted using the table menu
// in-table paragraphs should only be deleted inside the table
if (next != null && tableParent == nextTableParent) {
if (next.children.isNotEmpty) {
final path = node.path + [node.children.length];
transaction.insertNodes(path, next.children);
Expand Down
8 changes: 8 additions & 0 deletions lib/src/extensions/node_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ extension NodeExtensions on Node {
}
return false;
}

Node? findParent(bool Function(Node element) test) {
if (test(this)) {
return this;
}
final parent = this.parent;
return parent?.findParent(test);
}
}

extension NodesExtensions<T extends Node> on List<T> {
Expand Down

0 comments on commit e37b44f

Please sign in to comment.