Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
amantoux committed Dec 4, 2023
1 parent f72a82a commit 04e1551
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 71 deletions.
52 changes: 23 additions & 29 deletions packages/fleather/lib/src/widgets/autoformats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class AutoFormats {

int get undoPosition => _activeSuggestion!.undoPositionCandidate;

Delta get activeSuggestionChange => _activeSuggestion!.change;

bool get activeSuggestionKeepTriggerCharacter =>
_activeSuggestion!.keepTriggerCharacter;

Expand Down Expand Up @@ -144,33 +142,29 @@ class _AutoFormatLinks extends AutoFormat {
// Split text of previous operation in lines and words and take the last
// word to test.
final candidate = previousText.split('\n').last.split(' ').last;
try {
final match = _urlRegex.firstMatch(candidate);
if (match == null) return null;

final attributes = previous.attributes ?? <String, dynamic>{};

// Do nothing if already formatted as link.
if (attributes.containsKey(ParchmentAttribute.link.key)) return null;

String url = candidate;
if (!url.startsWith('http')) url = 'https://$url';
attributes
.addAll(ParchmentAttribute.link.fromString(url.toString()).toJson());

final change = Delta()
..retain(position - candidate.length)
..retain(candidate.length, attributes);
final undo = change.invert(documentDelta);
document.compose(change, ChangeSource.local);
return AutoFormatResult(
change: change,
undo: undo,
keepTriggerCharacter: keepTriggerCharacter,
undoPositionCandidate: position);
} on FormatException {
return null; // Our candidate is not a link.
}
final match = _urlRegex.firstMatch(candidate);
if (match == null) return null;

final attributes = previous.attributes ?? <String, dynamic>{};

// Do nothing if already formatted as link.
if (attributes.containsKey(ParchmentAttribute.link.key)) return null;

String url = candidate;
if (!url.startsWith('http')) url = 'https://$url';
attributes
.addAll(ParchmentAttribute.link.fromString(url.toString()).toJson());

final change = Delta()
..retain(position - candidate.length)
..retain(candidate.length, attributes);
final undo = change.invert(documentDelta);
document.compose(change, ChangeSource.local);
return AutoFormatResult(
change: change,
undo: undo,
keepTriggerCharacter: keepTriggerCharacter,
undoPositionCandidate: position);
}
}

Expand Down
8 changes: 0 additions & 8 deletions packages/fleather/lib/src/widgets/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,6 @@ class FleatherController extends ChangeNotifier {
if (delta.length <= 2 && delta.last.isInsert) {
return true;
}
// special case for AutoTextDirectionRule
if (delta.length <= 3 && delta.last.isRetain) {
return delta.last.attributes != null &&
delta.last.attributes!
.containsKey(ParchmentAttribute.direction.key) &&
delta.last.attributes!
.containsKey(ParchmentAttribute.alignment.key);
}
}
return false;
}
Expand Down
34 changes: 0 additions & 34 deletions packages/parchment/lib/src/heuristics/utils.dart
Original file line number Diff line number Diff line change
@@ -1,38 +1,4 @@
import 'dart:math' as math;

import 'package:parchment/src/document/embeds.dart';
import 'package:quill_delta/quill_delta.dart';

/// Skips to the beginning of line containing position at specified [length]
/// and returns contents of the line skipped so far.
List<Operation> skipToLineAt(DeltaIterator iter, int length) {
if (length == 0) {
return List.empty(growable: false);
}

final prefix = <Operation>[];

var skipped = 0;
while (skipped < length && iter.hasNext) {
final opLength = iter.peekLength();
final skip = math.min(length - skipped, opLength);
final op = iter.next(skip);
if (op.data is! String) {
prefix.add(op);
} else {
var text = op.data as String;
var pos = text.lastIndexOf('\n');
if (pos == -1) {
prefix.add(op);
} else {
prefix.clear();
prefix.add(Operation.insert(text.substring(pos + 1), op.attributes));
}
}
skipped += op.length;
}
return prefix;
}

bool isBlockEmbed(Object data) {
if (data is EmbeddableObject) {
Expand Down

0 comments on commit 04e1551

Please sign in to comment.