Skip to content

Commit b98743a

Browse files
authored
Add additionalFormats to fallback and make formats public (#415)
1 parent 97d202f commit b98743a

2 files changed

Lines changed: 50 additions & 13 deletions

File tree

packages/fleather/lib/src/widgets/autoformats.dart

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ class AutoFormats {
2828
: _autoFormats = autoFormats;
2929

3030
/// Default set of auto formats.
31-
factory AutoFormats.fallback() {
31+
///
32+
/// Use [additionalFormats] to add your autoformats to the default set.
33+
factory AutoFormats.fallback([List<AutoFormat>? additionalFormats]) {
3234
return AutoFormats(autoFormats: [
33-
const _AutoFormatLinks(),
34-
const _MarkdownInlineShortcuts(),
35-
const _MarkdownLineShortcuts(),
36-
const _AutoTextDirection(),
35+
const AutoFormatLinks(),
36+
const MarkdownInlineShortcuts(),
37+
const MarkdownLineShortcuts(),
38+
const AutoTextDirection(),
39+
...?additionalFormats,
3740
]);
3841
}
3942

@@ -119,11 +122,11 @@ class AutoFormatResult {
119122
final int undoPositionCandidate;
120123
}
121124

122-
class _AutoFormatLinks extends AutoFormat {
125+
class AutoFormatLinks extends AutoFormat {
123126
static final _urlRegex =
124127
RegExp(r'^(.?)((?:https?://|www\.)[^\s/$.?#].[^\s]*)');
125128

126-
const _AutoFormatLinks();
129+
const AutoFormatLinks();
127130

128131
@override
129132
AutoFormatResult? apply(
@@ -166,15 +169,15 @@ class _AutoFormatLinks extends AutoFormat {
166169
}
167170

168171
// Replaces certain Markdown shortcuts with actual inline styles.
169-
class _MarkdownInlineShortcuts extends AutoFormat {
172+
class MarkdownInlineShortcuts extends AutoFormat {
170173
static final rules = <String, ParchmentAttribute>{
171174
'**': ParchmentAttribute.bold,
172175
'*': ParchmentAttribute.italic,
173176
'`': ParchmentAttribute.inlineCode,
174177
'~~': ParchmentAttribute.strikethrough,
175178
};
176179

177-
const _MarkdownInlineShortcuts();
180+
const MarkdownInlineShortcuts();
178181

179182
@override
180183
AutoFormatResult? apply(
@@ -222,7 +225,7 @@ class _MarkdownInlineShortcuts extends AutoFormat {
222225
}
223226

224227
// Replaces certain Markdown shortcuts with actual line or block styles.
225-
class _MarkdownLineShortcuts extends AutoFormat {
228+
class MarkdownLineShortcuts extends AutoFormat {
226229
static final rules = <String, ParchmentAttribute>{
227230
'-': ParchmentAttribute.block.bulletList,
228231
'*': ParchmentAttribute.block.bulletList,
@@ -236,7 +239,7 @@ class _MarkdownLineShortcuts extends AutoFormat {
236239
'###': ParchmentAttribute.h3,
237240
};
238241

239-
const _MarkdownLineShortcuts();
242+
const MarkdownLineShortcuts();
240243

241244
String? _getLinePrefix(DeltaIterator iter, int index) {
242245
final prefixOps = skipToLineAt(iter, index);
@@ -383,8 +386,8 @@ class _MarkdownLineShortcuts extends AutoFormat {
383386

384387
// Infers text direction from the input when happens in the beginning of a line.
385388
// This rule also removes alignment and sets it based on inferred direction.
386-
class _AutoTextDirection extends AutoFormat {
387-
const _AutoTextDirection();
389+
class AutoTextDirection extends AutoFormat {
390+
const AutoTextDirection();
388391

389392
final _isRTL = intl.Bidi.startsWithRtl;
390393

packages/fleather/test/widgets/autoformats_test.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import 'package:fleather/fleather.dart';
22
import 'package:flutter/rendering.dart';
33
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:mocktail/mocktail.dart';
45

56
void main() {
67
late AutoFormats autoformats;
78

89
setUp(() {
910
autoformats = AutoFormats.fallback();
11+
registerFallbackValue(ParchmentDocument());
12+
});
13+
14+
test('Can use custom formats with fallbacks', () {
15+
final document = ParchmentDocument();
16+
final formats = AutoFormats.fallback([FakeAutoFormat('.')]);
17+
expect(formats.run(document, 0, '.'), isTrue);
18+
expect(document.toDelta(), Delta()..insert('Fake\n'));
1019
});
1120

1221
group('Link detection', () {
@@ -265,3 +274,28 @@ void main() {
265274
});
266275
});
267276
}
277+
278+
class FakeAutoFormat extends AutoFormat {
279+
final String trigger;
280+
281+
FakeAutoFormat(this.trigger);
282+
283+
@override
284+
AutoFormatResult? apply(
285+
ParchmentDocument document, int position, String data) {
286+
if (data == trigger) {
287+
final change = Delta()
288+
..retain(position)
289+
..insert('Fake');
290+
document.compose(change, ChangeSource.local);
291+
return AutoFormatResult(
292+
change: change,
293+
undo: Delta()
294+
..retain(position)
295+
..delete(4),
296+
undoPositionCandidate: position,
297+
);
298+
}
299+
return null;
300+
}
301+
}

0 commit comments

Comments
 (0)