Skip to content

Commit

Permalink
Change behavior to match hyperlink auto format standard
Browse files Browse the repository at this point in the history
Google Doc & MS Word both keep space after canceling auto format
  • Loading branch information
amantoux committed Dec 10, 2023
1 parent 41dbfbd commit 06ef0a5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 39 deletions.
38 changes: 2 additions & 36 deletions packages/fleather/lib/src/widgets/autoformats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ import 'package:quill_delta/quill_delta.dart';
abstract class AutoFormat {
const AutoFormat();

/// Indicates whether the character triggering the auto format is kept in
/// document
///
/// E.g: for link detections, [space] is kept whereas for Markdown block
/// shortcuts, the [space] is not added to document, it only serves to
/// trigger the block formatting
bool get keepTriggerCharacter;

/// Upon insertion of a trigger character, run format detection and apply
/// formatting to document
///
Expand Down Expand Up @@ -53,11 +45,6 @@ class AutoFormats {
/// The position at which the active suggestion can be deactivated
int get undoPosition => _activeSuggestion!.undoPositionCandidate;

/// `true` if the active suggestion [AutoFormat] keeps trigger character in
/// document; `false` otherwise
bool get activeSuggestionKeepTriggerCharacter =>
_activeSuggestion!.keepTriggerCharacter;

/// `true` if there is an active suggestion; `false` otherwise
bool get hasActiveSuggestion => _activeSuggestion != null;

Expand Down Expand Up @@ -104,7 +91,6 @@ class AutoFormatResult {
this.undoSelection,
required this.undo,
required this.undoPositionCandidate,
required this.keepTriggerCharacter,
});

/// *Optional* [TextSelection] after applying the auto format.
Expand All @@ -125,9 +111,6 @@ class AutoFormatResult {

/// The position at which an auto format can be canceled
final int undoPositionCandidate;

/// Whether the trigger character will be removed from the document after undoing the auto format
final bool keepTriggerCharacter;
}

class _AutoFormatLinks extends AutoFormat {
Expand All @@ -136,9 +119,6 @@ class _AutoFormatLinks extends AutoFormat {

const _AutoFormatLinks();

@override
final bool keepTriggerCharacter = false;

@override
AutoFormatResult? apply(
ParchmentDocument document, int position, String data) {
Expand Down Expand Up @@ -175,10 +155,7 @@ class _AutoFormatLinks extends AutoFormat {
final undo = change.invert(documentDelta);
document.compose(change, ChangeSource.local);
return AutoFormatResult(
change: change,
undo: undo,
keepTriggerCharacter: keepTriggerCharacter,
undoPositionCandidate: position);
change: change, undo: undo, undoPositionCandidate: position);
}
}

Expand All @@ -199,9 +176,6 @@ class _MarkdownShortCuts extends AutoFormat {

const _MarkdownShortCuts();

@override
final bool keepTriggerCharacter = false;

String? _getLinePrefix(DeltaIterator iter, int index) {
final prefixOps = skipToLineAt(iter, index);
if (prefixOps.any((element) => element.data is! String)) return null;
Expand Down Expand Up @@ -314,7 +288,6 @@ class _MarkdownShortCuts extends AutoFormat {
undoSelection:
TextSelection.collapsed(offset: position + data.length),
undo: undo,
keepTriggerCharacter: keepTriggerCharacter,
undoPositionCandidate: position - prefix.length - 1);
}
}
Expand Down Expand Up @@ -342,7 +315,6 @@ class _MarkdownShortCuts extends AutoFormat {
// current position is after prefix, so need to add 1 for space
undoSelection: TextSelection.collapsed(offset: position + 1),
undo: undo,
keepTriggerCharacter: keepTriggerCharacter,
undoPositionCandidate: position - prefix.length - 1);
}
}
Expand All @@ -354,9 +326,6 @@ class _AutoTextDirection extends AutoFormat {

final _isRTL = intl.Bidi.startsWithRtl;

@override
final bool keepTriggerCharacter = true;

bool _isAfterEmptyLine(Operation? previous) {
final data = previous?.data;
return data == null || (data is String ? data.endsWith('\n') : false);
Expand Down Expand Up @@ -400,9 +369,6 @@ class _AutoTextDirection extends AutoFormat {
final undo = change.invert(documentDelta);
document.compose(change, ChangeSource.local);
return AutoFormatResult(
change: change,
undo: undo,
keepTriggerCharacter: keepTriggerCharacter,
undoPositionCandidate: position);
change: change, undo: undo, undoPositionCandidate: position);
}
}
4 changes: 1 addition & 3 deletions packages/fleather/lib/src/widgets/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ class FleatherController extends ChangeNotifier {
ParchmentDocument document, int position, int length, Object data) {
if (!_autoFormats.hasActiveSuggestion) return true;

final keepTriggerCharacter =
_autoFormats.activeSuggestionKeepTriggerCharacter;
final isDeletionOfOneChar = data is String && data.isEmpty && length == 1;
if (isDeletionOfOneChar) {
// Undo if deleting 1 character after retain of auto-format
Expand All @@ -183,7 +181,7 @@ class FleatherController extends ChangeNotifier {
if (undoSelection != null) {
_updateSelectionSilent(undoSelection, source: ChangeSource.local);
}
return keepTriggerCharacter;
return false;
}
}
// Cancel active nevertheless
Expand Down

0 comments on commit 06ef0a5

Please sign in to comment.