From 5699e31c0ba27c966e13d6943c3b7b28e5d27ec4 Mon Sep 17 00:00:00 2001 From: Amir Panahandeh Date: Sun, 25 Aug 2024 22:07:45 +0330 Subject: [PATCH 1/5] Add additionalFormats to fallback and make formats public Co-Authored-By: cotw-fabier <112340360+cotw-fabier@users.noreply.github.com> --- .../fleather/lib/src/widgets/autoformats.dart | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/fleather/lib/src/widgets/autoformats.dart b/packages/fleather/lib/src/widgets/autoformats.dart index a811dea5..cb46cd12 100644 --- a/packages/fleather/lib/src/widgets/autoformats.dart +++ b/packages/fleather/lib/src/widgets/autoformats.dart @@ -28,12 +28,15 @@ class AutoFormats { : _autoFormats = autoFormats; /// Default set of auto formats. - factory AutoFormats.fallback() { + /// + /// Use [additionalFormats] to add your autoformats to the default set. + factory AutoFormats.fallback([List? additionalFormats]) { return AutoFormats(autoFormats: [ - const _AutoFormatLinks(), - const _MarkdownInlineShortcuts(), - const _MarkdownLineShortcuts(), - const _AutoTextDirection(), + const AutoFormatLinks(), + const MarkdownInlineShortcuts(), + const MarkdownLineShortcuts(), + const AutoTextDirection(), + ...?additionalFormats, ]); } @@ -119,11 +122,11 @@ class AutoFormatResult { final int undoPositionCandidate; } -class _AutoFormatLinks extends AutoFormat { +class AutoFormatLinks extends AutoFormat { static final _urlRegex = RegExp(r'^(.?)((?:https?://|www\.)[^\s/$.?#].[^\s]*)'); - const _AutoFormatLinks(); + const AutoFormatLinks(); @override AutoFormatResult? apply( @@ -166,7 +169,7 @@ class _AutoFormatLinks extends AutoFormat { } // Replaces certain Markdown shortcuts with actual inline styles. -class _MarkdownInlineShortcuts extends AutoFormat { +class MarkdownInlineShortcuts extends AutoFormat { static final rules = { '**': ParchmentAttribute.bold, '*': ParchmentAttribute.italic, @@ -174,7 +177,7 @@ class _MarkdownInlineShortcuts extends AutoFormat { '~~': ParchmentAttribute.strikethrough, }; - const _MarkdownInlineShortcuts(); + const MarkdownInlineShortcuts(); @override AutoFormatResult? apply( @@ -222,7 +225,7 @@ class _MarkdownInlineShortcuts extends AutoFormat { } // Replaces certain Markdown shortcuts with actual line or block styles. -class _MarkdownLineShortcuts extends AutoFormat { +class MarkdownLineShortcuts extends AutoFormat { static final rules = { '-': ParchmentAttribute.block.bulletList, '*': ParchmentAttribute.block.bulletList, @@ -236,7 +239,7 @@ class _MarkdownLineShortcuts extends AutoFormat { '###': ParchmentAttribute.h3, }; - const _MarkdownLineShortcuts(); + const MarkdownLineShortcuts(); String? _getLinePrefix(DeltaIterator iter, int index) { final prefixOps = skipToLineAt(iter, index); @@ -383,8 +386,8 @@ class _MarkdownLineShortcuts extends AutoFormat { // Infers text direction from the input when happens in the beginning of a line. // This rule also removes alignment and sets it based on inferred direction. -class _AutoTextDirection extends AutoFormat { - const _AutoTextDirection(); +class AutoTextDirection extends AutoFormat { + const AutoTextDirection(); final _isRTL = intl.Bidi.startsWithRtl; From 6b99ce7fb9b9bf698a135b094979a59734c1c95b Mon Sep 17 00:00:00 2001 From: Amir Panahandeh Date: Sun, 25 Aug 2024 22:09:49 +0330 Subject: [PATCH 2/5] Format --- packages/fleather/lib/src/widgets/autoformats.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fleather/lib/src/widgets/autoformats.dart b/packages/fleather/lib/src/widgets/autoformats.dart index cb46cd12..f03df9eb 100644 --- a/packages/fleather/lib/src/widgets/autoformats.dart +++ b/packages/fleather/lib/src/widgets/autoformats.dart @@ -28,7 +28,7 @@ class AutoFormats { : _autoFormats = autoFormats; /// Default set of auto formats. - /// + /// /// Use [additionalFormats] to add your autoformats to the default set. factory AutoFormats.fallback([List? additionalFormats]) { return AutoFormats(autoFormats: [ From 84911e36c4f7381a0b8d57c6e88f4f640e9c824f Mon Sep 17 00:00:00 2001 From: Amir Panahandeh Date: Sun, 25 Aug 2024 22:37:33 +0330 Subject: [PATCH 3/5] Add test --- .../test/widgets/autoformats_test.dart | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/fleather/test/widgets/autoformats_test.dart b/packages/fleather/test/widgets/autoformats_test.dart index 17c233bf..d0d60182 100644 --- a/packages/fleather/test/widgets/autoformats_test.dart +++ b/packages/fleather/test/widgets/autoformats_test.dart @@ -1,12 +1,30 @@ import 'package:fleather/fleather.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:mocktail/mocktail.dart'; + +class MockAutoFormat extends Mock implements AutoFormat {} + +class MockAutoFormatResult extends Mock implements AutoFormatResult {} void main() { late AutoFormats autoformats; setUp(() { autoformats = AutoFormats.fallback(); + registerFallbackValue(ParchmentDocument()); + }); + + test('Can use custom formats with fallbacks', () { + final customAutoformat = MockAutoFormat(); + final document = ParchmentDocument(); + final position = 5; + final data = 'Test'; + when(() => customAutoformat.apply(any(), any(), any())) + .thenReturn(MockAutoFormatResult()); + final formats = AutoFormats.fallback([customAutoformat]); + formats.run(document, position, data); + verify(() => customAutoformat.apply(document, position, data)); }); group('Link detection', () { From 584e87d683d431a60e7f8f4ad14afcd966388206 Mon Sep 17 00:00:00 2001 From: Amir Panahandeh Date: Sun, 25 Aug 2024 22:39:10 +0330 Subject: [PATCH 4/5] Update autoformats_test.dart --- packages/fleather/test/widgets/autoformats_test.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/fleather/test/widgets/autoformats_test.dart b/packages/fleather/test/widgets/autoformats_test.dart index d0d60182..5a49b44e 100644 --- a/packages/fleather/test/widgets/autoformats_test.dart +++ b/packages/fleather/test/widgets/autoformats_test.dart @@ -18,8 +18,8 @@ void main() { test('Can use custom formats with fallbacks', () { final customAutoformat = MockAutoFormat(); final document = ParchmentDocument(); - final position = 5; - final data = 'Test'; + const position = 5; + const data = 'Test'; when(() => customAutoformat.apply(any(), any(), any())) .thenReturn(MockAutoFormatResult()); final formats = AutoFormats.fallback([customAutoformat]); From cb8fa8a8e63efd7d3883bb4c567461cd5fa83822 Mon Sep 17 00:00:00 2001 From: Amir Panahandeh Date: Sun, 25 Aug 2024 23:03:30 +0330 Subject: [PATCH 5/5] Update autoformats_test.dart --- .../test/widgets/autoformats_test.dart | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/packages/fleather/test/widgets/autoformats_test.dart b/packages/fleather/test/widgets/autoformats_test.dart index 5a49b44e..b1220508 100644 --- a/packages/fleather/test/widgets/autoformats_test.dart +++ b/packages/fleather/test/widgets/autoformats_test.dart @@ -3,10 +3,6 @@ import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; -class MockAutoFormat extends Mock implements AutoFormat {} - -class MockAutoFormatResult extends Mock implements AutoFormatResult {} - void main() { late AutoFormats autoformats; @@ -16,15 +12,10 @@ void main() { }); test('Can use custom formats with fallbacks', () { - final customAutoformat = MockAutoFormat(); final document = ParchmentDocument(); - const position = 5; - const data = 'Test'; - when(() => customAutoformat.apply(any(), any(), any())) - .thenReturn(MockAutoFormatResult()); - final formats = AutoFormats.fallback([customAutoformat]); - formats.run(document, position, data); - verify(() => customAutoformat.apply(document, position, data)); + final formats = AutoFormats.fallback([FakeAutoFormat('.')]); + expect(formats.run(document, 0, '.'), isTrue); + expect(document.toDelta(), Delta()..insert('Fake\n')); }); group('Link detection', () { @@ -283,3 +274,28 @@ void main() { }); }); } + +class FakeAutoFormat extends AutoFormat { + final String trigger; + + FakeAutoFormat(this.trigger); + + @override + AutoFormatResult? apply( + ParchmentDocument document, int position, String data) { + if (data == trigger) { + final change = Delta() + ..retain(position) + ..insert('Fake'); + document.compose(change, ChangeSource.local); + return AutoFormatResult( + change: change, + undo: Delta() + ..retain(position) + ..delete(4), + undoPositionCandidate: position, + ); + } + return null; + } +}