Skip to content

Commit 2772218

Browse files
Fixed all tests
1 parent d6937fe commit 2772218

4 files changed

Lines changed: 80 additions & 39 deletions

File tree

super_editor/lib/src/chat/attachments/attachment_list_component.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,21 @@ class _AttachmentListComponentState extends State<AttachmentListComponent> with
352352
throw ArgumentError(
353353
'Invalid extent node position type. Expected _AttachmentListNodePosition but got ${extentNodePosition.runtimeType}');
354354
}
355+
if (baseNodePosition.isEquivalentTo(extentNodePosition)) {
356+
return Rect.zero;
357+
}
358+
359+
final startGap = baseNodePosition.gapIndex < extentNodePosition.gapIndex ? baseNodePosition : extentNodePosition;
360+
final endGap = baseNodePosition.gapIndex > extentNodePosition.gapIndex ? extentNodePosition : baseNodePosition;
361+
362+
var boundingRect = _findLocalRectForAttachment(startGap.gapIndex);
363+
for (int i = startGap.gapIndex + 1; i <= endGap.gapIndex; i += 1) {
364+
if (i >= widget.attachments.length) {
365+
// This is probably the gap after the last attachment. It contributes
366+
// nothing to the selection bounds. Ignore it.
367+
continue;
368+
}
355369

356-
final startGap = min(baseNodePosition.gapIndex, extentNodePosition.gapIndex);
357-
final endGap = max(baseNodePosition.gapIndex, extentNodePosition.gapIndex);
358-
var boundingRect = _findLocalRectForAttachment(startGap);
359-
for (int i = startGap + 1; i < endGap; i += 1) {
360370
final additionalRect = _findLocalRectForAttachment(i);
361371
boundingRect = boundingRect.expandToInclude(additionalRect);
362372
}

super_editor/lib/src/default_editor/multi_node_editing.dart

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import 'package:attributed_text/attributed_text.dart';
44
import 'package:flutter/services.dart';
55
import 'package:super_editor/src/core/document.dart';
66
import 'package:super_editor/src/core/document_composer.dart';
7-
import 'package:super_editor/src/core/editor.dart';
87
import 'package:super_editor/src/core/document_selection.dart';
8+
import 'package:super_editor/src/core/editor.dart';
99
import 'package:super_editor/src/default_editor/box_component.dart';
10-
import 'package:super_editor/src/default_editor/common_editor_operations.dart';
1110
import 'package:super_editor/src/default_editor/selection_upstream_downstream.dart';
1211
import 'package:super_editor/src/default_editor/text.dart';
1312
import 'package:super_editor/src/infrastructure/_logging.dart';
@@ -839,8 +838,6 @@ class DeleteContentCommand extends EditCommand {
839838
return;
840839
}
841840

842-
late final DocumentPosition caretPosition;
843-
844841
final startNode = document.getNode(normalizedRange.start);
845842
if (startNode == null) {
846843
throw Exception('Could not locate start node for DeleteSelectionCommand: ${normalizedRange.start}');
@@ -894,11 +891,6 @@ class DeleteContentCommand extends EditCommand {
894891
),
895892
);
896893
}
897-
898-
caretPosition = DocumentPosition(
899-
nodeId: startNode.id,
900-
nodePosition: normalizedRange.start.nodePosition,
901-
);
902894
}
903895

904896
if (endNode.isDeletable) {
@@ -969,7 +961,22 @@ class DeleteContentCommand extends EditCommand {
969961
)
970962
]);
971963

972-
caretPosition = DocumentPosition(nodeId: emptyParagraphId, nodePosition: const TextNodePosition(offset: 0));
964+
if (updateSelection) {
965+
executor.executeCommand(
966+
ChangeSelectionCommand(
967+
DocumentSelection.collapsed(
968+
position: DocumentPosition(
969+
nodeId: emptyParagraphId,
970+
nodePosition: const TextNodePosition(offset: 0),
971+
),
972+
),
973+
SelectionChangeType.deleteContent,
974+
SelectionReason.userInteraction,
975+
),
976+
);
977+
}
978+
979+
return;
973980
}
974981

975982
// The start/end nodes may have been deleted due to empty content.
@@ -985,18 +992,41 @@ class DeleteContentCommand extends EditCommand {
985992
// Neither of the end nodes are `TextNode`s, so there's nothing
986993
// for us to merge. We're done.
987994
if (updateSelection) {
988-
executor.executeCommand(
989-
ChangeSelectionCommand(
990-
DocumentSelection.collapsed(position: caretPosition),
991-
SelectionChangeType.deleteContent,
992-
SelectionReason.userInteraction,
993-
),
994-
);
995+
if (startNodeAfterDeletion == null || !startNodeAfterDeletion.isDeletable) {
996+
executor.executeCommand(
997+
ChangeSelectionCommand(
998+
DocumentSelection.collapsed(
999+
position: DocumentPosition(
1000+
nodeId: endNodeAfterDeletion!.id,
1001+
nodePosition: endNodeAfterDeletion.beginningPosition,
1002+
),
1003+
),
1004+
SelectionChangeType.deleteContent,
1005+
SelectionReason.userInteraction,
1006+
),
1007+
);
1008+
} else {
1009+
executor.executeCommand(
1010+
ChangeSelectionCommand(
1011+
DocumentSelection.collapsed(
1012+
position: DocumentPosition(
1013+
nodeId: startNodeAfterDeletion.id,
1014+
nodePosition: startNodeAfterDeletion.endPosition,
1015+
),
1016+
),
1017+
SelectionChangeType.deleteContent,
1018+
SelectionReason.userInteraction,
1019+
),
1020+
);
1021+
}
9951022
}
9961023

9971024
return;
9981025
}
9991026

1027+
// Now that the content has been deleted, and we know the starting and ending
1028+
// nodes are both text nodes, add the ending node's text to the starting node.
1029+
// Then delete the ending node.
10001030
_log.log('DeleteSelectionCommand', ' - combining last node text with first node text');
10011031
executor.logChanges([
10021032
DocumentEdit(
@@ -1023,15 +1053,16 @@ class DeleteContentCommand extends EditCommand {
10231053
)
10241054
]);
10251055

1026-
caretPosition = DocumentPosition(
1027-
nodeId: startNodeAfterDeletion.id,
1028-
nodePosition: TextNodePosition(offset: startNodeAfterDeletion.text.length),
1029-
);
1030-
1056+
// (Maybe) update the editor selection after the deletion.
10311057
if (updateSelection) {
10321058
executor.executeCommand(
10331059
ChangeSelectionCommand(
1034-
DocumentSelection.collapsed(position: caretPosition),
1060+
DocumentSelection.collapsed(
1061+
position: DocumentPosition(
1062+
nodeId: startNodeAfterDeletion.id,
1063+
nodePosition: startNodeAfterDeletion.endPosition,
1064+
),
1065+
),
10351066
SelectionChangeType.deleteContent,
10361067
SelectionReason.userInteraction,
10371068
),
@@ -1463,10 +1494,10 @@ class DeleteSelectionCommand extends EditCommand {
14631494
}
14641495
}
14651496

1466-
final newSelectionPosition = CommonEditorOperations.getDocumentPositionAfterExpandedDeletion(
1467-
document: document,
1468-
selection: selection,
1469-
);
1497+
// final newSelectionPosition = CommonEditorOperations.getDocumentPositionAfterExpandedDeletion(
1498+
// document: document,
1499+
// selection: selection,
1500+
// );
14701501

14711502
executor.executeCommand(
14721503
DeleteContentCommand(

super_editor/test/super_editor/components/attachment_list_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void main() {
6767

6868
expect(
6969
SuperEditorInspector.findDocumentSelection(),
70-
_caretAt("2", i),
70+
_caretAt("2", i, TextAffinity.downstream),
7171
);
7272
}
7373
});
@@ -289,11 +289,11 @@ List<Object> _findAttachments() {
289289
return List.from(node.attachments);
290290
}
291291

292-
DocumentSelection _caretAt(String nodeId, int gapIndex) {
292+
DocumentSelection _caretAt(String nodeId, int gapIndex, [TextAffinity affinity = TextAffinity.upstream]) {
293293
return DocumentSelection.collapsed(
294294
position: DocumentPosition(
295295
nodeId: nodeId,
296-
nodePosition: AttachmentListNodePosition(gapIndex),
296+
nodePosition: AttachmentListNodePosition(gapIndex, affinity),
297297
),
298298
);
299299
}

super_editor/test/super_editor/text_entry/ime/ime_serialization_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void main() {
192192
),
193193
extent: DocumentPosition(
194194
nodeId: "1",
195-
nodePosition: AttachmentListNodePosition(2, TextAffinity.downstream),
195+
nodePosition: AttachmentListNodePosition(3, TextAffinity.upstream),
196196
),
197197
),
198198
null,
@@ -250,11 +250,11 @@ void main() {
250250
const DocumentSelection(
251251
base: DocumentPosition(
252252
nodeId: "1",
253-
nodePosition: AttachmentListNodePosition(1, TextAffinity.upstream),
253+
nodePosition: AttachmentListNodePosition(1),
254254
),
255255
extent: DocumentPosition(
256256
nodeId: "3",
257-
nodePosition: AttachmentListNodePosition(1, TextAffinity.downstream),
257+
nodePosition: AttachmentListNodePosition(2),
258258
),
259259
),
260260
null,
@@ -282,11 +282,11 @@ void main() {
282282
const DocumentSelection(
283283
base: DocumentPosition(
284284
nodeId: "1",
285-
nodePosition: AttachmentListNodePosition(1, TextAffinity.upstream),
285+
nodePosition: AttachmentListNodePosition(1),
286286
),
287287
extent: DocumentPosition(
288288
nodeId: "3",
289-
nodePosition: AttachmentListNodePosition(1, TextAffinity.downstream),
289+
nodePosition: AttachmentListNodePosition(2),
290290
),
291291
),
292292
null,

0 commit comments

Comments
 (0)