From 62c2ec629e29101849ae34d4f8fc3ed699fc6429 Mon Sep 17 00:00:00 2001 From: Alan Mantoux Date: Mon, 4 Dec 2023 12:29:33 +0100 Subject: [PATCH] Cleanup --- .../fleather/lib/src/widgets/autoformats.dart | 52 ++++++++----------- .../parchment/lib/src/heuristics/utils.dart | 34 ------------ 2 files changed, 23 insertions(+), 63 deletions(-) diff --git a/packages/fleather/lib/src/widgets/autoformats.dart b/packages/fleather/lib/src/widgets/autoformats.dart index f688bbcd..469c119e 100644 --- a/packages/fleather/lib/src/widgets/autoformats.dart +++ b/packages/fleather/lib/src/widgets/autoformats.dart @@ -46,8 +46,6 @@ class AutoFormats { int get undoPosition => _activeSuggestion!.undoPositionCandidate; - Delta get activeSuggestionChange => _activeSuggestion!.change; - bool get activeSuggestionKeepTriggerCharacter => _activeSuggestion!.keepTriggerCharacter; @@ -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 ?? {}; - - // 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 ?? {}; + + // 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); } } diff --git a/packages/parchment/lib/src/heuristics/utils.dart b/packages/parchment/lib/src/heuristics/utils.dart index a7429a6f..4ff78013 100644 --- a/packages/parchment/lib/src/heuristics/utils.dart +++ b/packages/parchment/lib/src/heuristics/utils.dart @@ -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 skipToLineAt(DeltaIterator iter, int length) { - if (length == 0) { - return List.empty(growable: false); - } - - final prefix = []; - - 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) {