Skip to content

Commit

Permalink
Add additionalFormats to fallback and make formats public
Browse files Browse the repository at this point in the history
Co-Authored-By: cotw-fabier <[email protected]>
  • Loading branch information
Amir-P and cotw-fabier committed Aug 25, 2024
1 parent e60be6c commit 5699e31
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions packages/fleather/lib/src/widgets/autoformats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<AutoFormat>? additionalFormats]) {
return AutoFormats(autoFormats: [
const _AutoFormatLinks(),
const _MarkdownInlineShortcuts(),
const _MarkdownLineShortcuts(),
const _AutoTextDirection(),
const AutoFormatLinks(),
const MarkdownInlineShortcuts(),
const MarkdownLineShortcuts(),
const AutoTextDirection(),
...?additionalFormats,
]);
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -166,15 +169,15 @@ class _AutoFormatLinks extends AutoFormat {
}

// Replaces certain Markdown shortcuts with actual inline styles.
class _MarkdownInlineShortcuts extends AutoFormat {
class MarkdownInlineShortcuts extends AutoFormat {
static final rules = <String, ParchmentAttribute>{
'**': ParchmentAttribute.bold,
'*': ParchmentAttribute.italic,
'`': ParchmentAttribute.inlineCode,
'~~': ParchmentAttribute.strikethrough,
};

const _MarkdownInlineShortcuts();
const MarkdownInlineShortcuts();

@override
AutoFormatResult? apply(
Expand Down Expand Up @@ -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 = <String, ParchmentAttribute>{
'-': ParchmentAttribute.block.bulletList,
'*': ParchmentAttribute.block.bulletList,
Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 5699e31

Please sign in to comment.