Skip to content

Commit 6fcf3e5

Browse files
committed
SpenEmbed configuration
1 parent b4c4d7a commit 6fcf3e5

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class EditableTextBlock extends StatelessWidget {
2525
final bool enableInteractiveSelection;
2626
final bool hasFocus;
2727
final FleatherEmbedBuilder embedBuilder;
28+
final Map<String, FleatherSpanEmbedConfiguration> spanEmbedConfigurations;
2829
final LinkActionPicker linkActionPicker;
2930
final ValueChanged<String?>? onLaunchUrl;
3031
final EdgeInsets? contentPadding;
@@ -42,6 +43,7 @@ class EditableTextBlock extends StatelessWidget {
4243
required this.enableInteractiveSelection,
4344
required this.hasFocus,
4445
required this.embedBuilder,
46+
required this.spanEmbedConfigurations,
4547
required this.linkActionPicker,
4648
this.onLaunchUrl,
4749
this.contentPadding,
@@ -84,6 +86,7 @@ class EditableTextBlock extends StatelessWidget {
8486
readOnly: readOnly,
8587
controller: controller,
8688
embedBuilder: embedBuilder,
89+
spanEmbedConfigurations: spanEmbedConfigurations,
8790
linkActionPicker: linkActionPicker,
8891
onLaunchUrl: onLaunchUrl,
8992
textWidthBasis: textWidthBasis,

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ Widget defaultSpellCheckMenuBuilder(
7979
typedef FleatherEmbedBuilder = Widget Function(
8080
BuildContext context, EmbedNode node);
8181

82+
/// Special configuration for [SpanEmbed]s
83+
class FleatherSpanEmbedConfiguration {
84+
const FleatherSpanEmbedConfiguration(this.embedBuilder,
85+
{this.placeholderAlignment = PlaceholderAlignment.bottom,
86+
this.textBaseline,
87+
this.textStyle});
88+
89+
final FleatherEmbedBuilder embedBuilder;
90+
final PlaceholderAlignment placeholderAlignment;
91+
final TextBaseline? textBaseline;
92+
final TextStyle? textStyle;
93+
}
94+
8295
/// Default implementation of a builder function for embeddable objects in
8396
/// Fleather.
8497
///
@@ -255,6 +268,13 @@ class FleatherEditor extends StatefulWidget {
255268
/// Defaults to [defaultFleatherEmbedBuilder].
256269
final FleatherEmbedBuilder embedBuilder;
257270

271+
/// Available configuration for [SpanEmbed]s.
272+
/// If no configuration of found for a [SpanEmbed], builder will fallback to
273+
/// [embedBuilder].
274+
///
275+
/// Defaults to `{}`
276+
final Map<String, FleatherSpanEmbedConfiguration> spanEmbedConfigs;
277+
258278
/// Configuration that details how spell check should be performed.
259279
///
260280
/// Specifies the [SpellCheckService] used to spell check text input and the
@@ -331,6 +351,7 @@ class FleatherEditor extends StatefulWidget {
331351
this.clipboardStatus,
332352
this.contextMenuBuilder = defaultContextMenuBuilder,
333353
this.embedBuilder = defaultFleatherEmbedBuilder,
354+
this.spanEmbedConfigs = const {},
334355
this.linkActionPickerDelegate = defaultLinkActionPickerDelegate,
335356
this.textSelectionControls});
336357

@@ -505,6 +526,7 @@ class _FleatherEditorState extends State<FleatherEditor>
505526
scrollPhysics: widget.scrollPhysics,
506527
onLaunchUrl: widget.onLaunchUrl,
507528
embedBuilder: widget.embedBuilder,
529+
spanEmbedConfigurations: widget.spanEmbedConfigs,
508530
spellCheckConfiguration: widget.spellCheckConfiguration,
509531
linkActionPickerDelegate: widget.linkActionPickerDelegate,
510532
clipboardManager: widget.clipboardManager,
@@ -618,6 +640,7 @@ class RawEditor extends StatefulWidget {
618640
this.contextMenuBuilder = defaultContextMenuBuilder,
619641
this.spellCheckConfiguration,
620642
this.embedBuilder = defaultFleatherEmbedBuilder,
643+
this.spanEmbedConfigurations = const {},
621644
this.linkActionPickerDelegate = defaultLinkActionPickerDelegate,
622645
}) : assert(maxHeight == null || maxHeight > 0),
623646
assert(minHeight == null || minHeight >= 0),
@@ -800,6 +823,8 @@ class RawEditor extends StatefulWidget {
800823
/// Defaults to [defaultFleatherEmbedBuilder].
801824
final FleatherEmbedBuilder embedBuilder;
802825

826+
final Map<String, FleatherSpanEmbedConfiguration> spanEmbedConfigurations;
827+
803828
final LinkActionPickerDelegate linkActionPickerDelegate;
804829

805830
final ClipboardManager clipboardManager;
@@ -1835,6 +1860,7 @@ class RawEditorState extends EditorState
18351860
readOnly: widget.readOnly,
18361861
controller: widget.controller,
18371862
embedBuilder: widget.embedBuilder,
1863+
spanEmbedConfigurations: widget.spanEmbedConfigurations,
18381864
linkActionPicker: _linkActionPicker,
18391865
onLaunchUrl: widget.onLaunchUrl,
18401866
textWidthBasis: widget.textWidthBasis,
@@ -1862,6 +1888,7 @@ class RawEditorState extends EditorState
18621888
? const EdgeInsets.all(16.0)
18631889
: null,
18641890
embedBuilder: widget.embedBuilder,
1891+
spanEmbedConfigurations: widget.spanEmbedConfigurations,
18651892
linkActionPicker: _linkActionPicker,
18661893
onLaunchUrl: widget.onLaunchUrl,
18671894
),

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class TextLine extends StatefulWidget {
2424
final bool readOnly;
2525
final FleatherController controller;
2626
final FleatherEmbedBuilder embedBuilder;
27+
final Map<String, FleatherSpanEmbedConfiguration> spanEmbedConfigurations;
2728
final ValueChanged<String?>? onLaunchUrl;
2829
final LinkActionPicker linkActionPicker;
2930
final TextWidthBasis textWidthBasis;
@@ -34,6 +35,7 @@ class TextLine extends StatefulWidget {
3435
required this.readOnly,
3536
required this.controller,
3637
required this.embedBuilder,
38+
required this.spanEmbedConfigurations,
3739
required this.onLaunchUrl,
3840
required this.linkActionPicker,
3941
required this.textWidthBasis,
@@ -174,6 +176,16 @@ class _TextLineState extends State<TextLine> {
174176

175177
InlineSpan _segmentToTextSpan(Node segment, FleatherThemeData theme) {
176178
if (segment is EmbedNode) {
179+
final spanConfiguration =
180+
widget.spanEmbedConfigurations[segment.value.type];
181+
if (spanConfiguration != null) {
182+
return WidgetSpan(
183+
child: EmbedProxy(
184+
child: spanConfiguration.embedBuilder(context, segment)),
185+
alignment: spanConfiguration.placeholderAlignment,
186+
baseline: spanConfiguration.textBaseline,
187+
style: spanConfiguration.textStyle);
188+
}
177189
return WidgetSpan(
178190
child: EmbedProxy(child: widget.embedBuilder(context, segment)));
179191
}

0 commit comments

Comments
 (0)