From c2ae5436643c538a974f8dccc7a93f319c70f0d3 Mon Sep 17 00:00:00 2001 From: Alan Mantoux Date: Mon, 4 Dec 2023 11:28:07 +0100 Subject: [PATCH] Fix tests & make selection optional is AutoFormatResult --- .../fleather/lib/src/widgets/autoformats.dart | 16 +++++++--------- .../fleather/lib/src/widgets/controller.dart | 4 +++- .../fleather/test/widgets/autoformats_test.dart | 8 ++++---- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/fleather/lib/src/widgets/autoformats.dart b/packages/fleather/lib/src/widgets/autoformats.dart index b9272c30..f688bbcd 100644 --- a/packages/fleather/lib/src/widgets/autoformats.dart +++ b/packages/fleather/lib/src/widgets/autoformats.dart @@ -72,7 +72,7 @@ class AutoFormats { /// Remove auto format from [document] and de-activate current suggestion /// It will throw if [_activeSuggestion] is null. - TextSelection undoActive(ParchmentDocument document) { + TextSelection? undoActive(ParchmentDocument document) { final undoSelection = _activeSuggestion!.undoSelection; document.compose(_activeSuggestion!.undo, ChangeSource.local); _activeSuggestion = null; @@ -88,22 +88,24 @@ class AutoFormats { /// An auto format result class AutoFormatResult { AutoFormatResult({ - required this.selection, + this.selection, required this.change, - required this.undoSelection, + this.undoSelection, required this.undo, required this.undoPositionCandidate, required this.keepTriggerCharacter, }); /// The selection after applying the auto format - final TextSelection selection; + /// Optional + final TextSelection? selection; /// The change that was applied final Delta change; /// The selection after undoing the formatting - final TextSelection undoSelection; + /// Optional + final TextSelection? undoSelection; /// The changes to undo the formatting final Delta undo; @@ -162,10 +164,8 @@ class _AutoFormatLinks extends AutoFormat { final undo = change.invert(documentDelta); document.compose(change, ChangeSource.local); return AutoFormatResult( - selection: TextSelection.collapsed(offset: position + 1), change: change, undo: undo, - undoSelection: TextSelection.collapsed(offset: position), keepTriggerCharacter: keepTriggerCharacter, undoPositionCandidate: position); } on FormatException { @@ -391,9 +391,7 @@ class _AutoTextDirection extends AutoFormat { final undo = change.invert(documentDelta); document.compose(change, ChangeSource.local); return AutoFormatResult( - selection: TextSelection.collapsed(offset: position + data.length), change: change, - undoSelection: TextSelection.collapsed(offset: position), undo: undo, keepTriggerCharacter: keepTriggerCharacter, undoPositionCandidate: position); diff --git a/packages/fleather/lib/src/widgets/controller.dart b/packages/fleather/lib/src/widgets/controller.dart index 056e818a..318d2bc3 100644 --- a/packages/fleather/lib/src/widgets/controller.dart +++ b/packages/fleather/lib/src/widgets/controller.dart @@ -187,7 +187,9 @@ class FleatherController extends ChangeNotifier { // Undo if deleting 1 character after retain of autoformat if (position == _autoFormats.undoPosition) { final undoSelection = _autoFormats.undoActive(document); - _updateSelectionSilent(undoSelection, source: ChangeSource.local); + if (undoSelection != null) { + _updateSelectionSilent(undoSelection, source: ChangeSource.local); + } return keepTriggerCharacter; } } diff --git a/packages/fleather/test/widgets/autoformats_test.dart b/packages/fleather/test/widgets/autoformats_test.dart index d06413ec..98cda842 100644 --- a/packages/fleather/test/widgets/autoformats_test.dart +++ b/packages/fleather/test/widgets/autoformats_test.dart @@ -15,7 +15,7 @@ void main() { {'insert': 'Some long text and a https://fleather-editor.github.io\n'} ]); final selection = autoformats.run(document, 54, ' '); - expect(selection, const TextSelection.collapsed(offset: 55)); + expect(selection, isNull); final attributes = document.toDelta().toList()[1].attributes; expect(attributes, isNotNull); expect(attributes!.containsKey(ParchmentAttribute.link.key), isTrue); @@ -28,7 +28,7 @@ void main() { {'insert': 'Some long text and a www.github.com\n'} ]); final selection = autoformats.run(document, 35, ' '); - expect(selection, const TextSelection.collapsed(offset: 36)); + expect(selection, isNull); final attributes = document.toDelta().toList()[1].attributes; expect(attributes, isNotNull); expect(attributes!.containsKey(ParchmentAttribute.link.key), isTrue); @@ -53,7 +53,7 @@ void main() { ]); autoformats.run(document, 54, ' '); final undoSelection = autoformats.undoActive(document); - expect(undoSelection, const TextSelection.collapsed(offset: 54)); + expect(undoSelection, isNull); expect(document.toDelta().length, 1); expect(document.toDelta().first.isInsert, isTrue); expect(document.toDelta().first.data, text); @@ -147,7 +147,7 @@ void main() { {'insert': 'some ltr text\nש\n'} ]); final selection = autoformats.run(document, 14, 'ש'); - expect(selection, const TextSelection.collapsed(offset: 15)); + expect(selection, isNull); final attributes = document.toDelta().toList()[1].attributes; expect(attributes, isNotNull); expect(attributes!.containsKey(ParchmentAttribute.direction.key), isTrue);