Skip to content

Commit ed80f13

Browse files
committed
fix: cursor is correctly positioned after pasted text when pasting simple text (without newlines) in the middle of a paragraph
1 parent de422f3 commit ed80f13

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

super_editor/lib/src/default_editor/common_editor_operations.dart

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ class CommonEditorOperations {
100100
}) {
101101
DocumentPosition? position;
102102
if (findNearestPosition) {
103-
position = documentLayoutResolver().getDocumentPositionNearestToOffset(documentOffset);
103+
position = documentLayoutResolver()
104+
.getDocumentPositionNearestToOffset(documentOffset);
104105
} else {
105106
position = documentLayoutResolver().getDocumentPositionAtOffset(documentOffset);
106107
}
@@ -2510,17 +2511,31 @@ class PasteEditorCommand extends EditCommand {
25102511
]);
25112512
}
25122513

2513-
// Place the caret at the end of the pasted content.
2514-
final pastedNode = document.getNodeById(previousNode.id)!;
2515-
// ^ re-query the node where we pasted content because nodes are immutable.
2516-
2514+
late DocumentPosition documentPositionAfterPaste;
2515+
if (parsedContent.length > 1) {
2516+
// Place the caret at the end of the pasted content.
2517+
final pastedNode = document.getNodeById(previousNode.id)!;
2518+
// ^ re-query the node where we pasted content because nodes are immutable.
2519+
documentPositionAfterPaste = DocumentPosition(
2520+
nodeId: pastedNode.id,
2521+
nodePosition: pastedNode.endPosition,
2522+
);
2523+
} else {
2524+
// The user only pasted content without any newlines in it. Place the
2525+
// caret in the existing node at the end of the pasted text. This is
2526+
// guaranteed to be a TextNode.
2527+
documentPositionAfterPaste = DocumentPosition(
2528+
nodeId: _pastePosition.nodeId,
2529+
nodePosition: TextNodePosition(
2530+
offset:
2531+
pasteTextOffset + (_parsedContent!.first as TextNode).text.length,
2532+
),
2533+
);
2534+
}
25172535
executor.executeCommand(
25182536
ChangeSelectionCommand(
25192537
DocumentSelection.collapsed(
2520-
position: DocumentPosition(
2521-
nodeId: pastedNode.id,
2522-
nodePosition: pastedNode.endPosition,
2523-
),
2538+
position: documentPositionAfterPaste,
25242539
),
25252540
SelectionChangeType.insertContent,
25262541
SelectionReason.userInteraction,

0 commit comments

Comments
 (0)