Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick "[SuperEditor] Avoid restoring selection upon re-focus when selected nodes were deleted (Resolves #1074) (#1103)" to stable #1104

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class EditorSelectionAndFocusPolicy extends StatefulWidget {
const EditorSelectionAndFocusPolicy({
Key? key,
required this.focusNode,
required this.document,
required this.selection,
required this.isDocumentLayoutAvailable,
required this.getDocumentLayout,
Expand All @@ -31,6 +32,9 @@ class EditorSelectionAndFocusPolicy extends StatefulWidget {
/// When focus is lost, this widget may clear the editor's selection.
final FocusNode focusNode;

/// The editor's [Document].
final Document document;

/// The document editor's current selection.
final ValueNotifier<DocumentSelection?> selection;

Expand Down Expand Up @@ -109,6 +113,12 @@ class _EditorSelectionAndFocusPolicyState extends State<EditorSelectionAndFocusP
// Ensure the editor has a selection when focused.
if (!_wasFocused && widget.focusNode.hasFocus) {
if (widget.restorePreviousSelectionOnGainFocus && _previousSelection != null) {
if (widget.document.getNodeById(_previousSelection!.base.nodeId) == null ||
widget.document.getNodeById(_previousSelection!.extent.nodeId) == null) {
editorPoliciesLog.info(
"[${widget.runtimeType}] - not restoring previous editor selection because one of the selected nodes was deleted");
return;
}
// Restore the previous selection.
editorPoliciesLog
.info("[${widget.runtimeType}] - restoring previous editor selection because the editor re-gained focus");
Expand Down
1 change: 1 addition & 0 deletions super_editor/lib/src/default_editor/super_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ class SuperEditorState extends State<SuperEditor> {
focusNode: _focusNode,
child: EditorSelectionAndFocusPolicy(
focusNode: _focusNode,
document: widget.editor.document,
selection: _composer.selectionNotifier,
isDocumentLayoutAvailable: () => _docLayoutKey.currentContext != null,
getDocumentLayout: () => editContext.documentLayout,
Expand Down
55 changes: 55 additions & 0 deletions super_editor/test/super_editor/supereditor_selection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,61 @@ Second Paragraph
expect(_caretFinder(), findsOneWidget);
});

testWidgetsOnAllPlatforms("doesn't restore previous selection upon re-focusing when selected node was deleted",
(tester) async {
final focusNode = FocusNode();

await tester
.createDocument()
.withLongTextContent()
.withInputSource(TextInputSource.ime)
.withFocusNode(focusNode)
.withCustomWidgetTreeBuilder(
(superEditor) => MaterialApp(
home: Scaffold(
body: Column(
children: [
const TextField(),
Expanded(child: superEditor),
],
),
),
),
)
.pump();

final doc = SuperEditorInspector.findDocument()! as MutableDocument;

// Place caret at the beginning of the text.
await tester.placeCaretInParagraph('1', 0);
expect(
SuperEditorInspector.findDocumentSelection(),
const DocumentSelection.collapsed(
position: DocumentPosition(
nodeId: '1',
nodePosition: TextNodePosition(offset: 0),
),
),
);

// Focus the textfield.
await tester.tap(find.byType(TextField));
await tester.pumpAndSettle();

// Ensure selection was cleared.
expect(SuperEditorInspector.findDocumentSelection(), isNull);

// Delete the selected node.
doc.deleteNodeAt(0);

// Focus the editor.
focusNode.requestFocus();
await tester.pumpAndSettle();

// Ensure no selection was restored.
expect(SuperEditorInspector.findDocumentSelection(), isNull);
});

testWidgetsOnAllPlatforms('retains composer initial selection upon first editor focus', (tester) async {
final focusNode = FocusNode();

Expand Down