Skip to content

Commit

Permalink
fix: miss the nested node when copying (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 authored Aug 22, 2023
1 parent 5e78abc commit aaef934
Showing 1 changed file with 55 additions and 15 deletions.
70 changes: 55 additions & 15 deletions lib/src/editor_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,25 +290,65 @@ class EditorState {
if (selection == null || selection.isCollapsed) {
return res;
}
final nodes = getNodesInSelection(selection);
final nodes = getNodesInSelection(selection).map((e) => e.copyWith());
for (final node in nodes) {
if (node.level > 1) {
continue;
}
final delta = node.delta;
if (delta == null) {
if (res.any((element) => element.path.isParentOf(node.path))) {
continue;
}
final startIndex = node == nodes.first ? selection.startIndex : 0;
final endIndex = node == nodes.last ? selection.endIndex : delta.length;
res.add(
node.copyWith(
attributes: {
...node.attributes,
blockComponentDelta: delta.slice(startIndex, endIndex).toJson()
res.add(node);
}

if (res.isNotEmpty) {
var delta = res.first.delta;
if (delta != null) {
res.first.updateAttributes(
{
...res.first.attributes,
blockComponentDelta: delta
.slice(
selection.startIndex,
delta.length,
)
.toJson()
},
),
);
);
}

var node = res.last;
while (node.children.isNotEmpty) {
node = node.children.last;
}
delta = node.delta;
if (delta != null) {
if (node.parent != null) {
node.insertBefore(
node.copyWith(
attributes: {
...node.attributes,
blockComponentDelta: delta
.slice(
0,
selection.endIndex,
)
.toJson()
},
),
);
node.unlink();
} else {
node.updateAttributes(
{
...node.attributes,
blockComponentDelta: delta
.slice(
0,
selection.endIndex,
)
.toJson()
},
);
}
}
}

return res;
Expand Down

0 comments on commit aaef934

Please sign in to comment.