Skip to content

Commit 7ec9ee2

Browse files
committed
SpenEmbed configuration
1 parent 61a029f commit 7ec9ee2

4 files changed

Lines changed: 68 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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,36 @@ 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+
91+
/// How the placeholder aligns vertically with the text.
92+
///
93+
/// See [ui.PlaceholderAlignment] for details on each mode.
94+
final PlaceholderAlignment placeholderAlignment;
95+
96+
/// The [TextBaseline] to align against when using [ui.PlaceholderAlignment.baseline],
97+
/// [ui.PlaceholderAlignment.aboveBaseline], and [ui.PlaceholderAlignment.belowBaseline].
98+
///
99+
/// This is ignored when using other alignment modes.
100+
final TextBaseline? textBaseline;
101+
102+
/// The [TextStyle] to apply to this span.
103+
///
104+
/// The [style] is also applied to any child spans when this is an instance
105+
/// of [TextSpan].
106+
///
107+
/// A [TextStyle] may be provided with the [style] property, but only the
108+
/// decoration, foreground, background, and spacing options will be used.
109+
final TextStyle? textStyle;
110+
}
111+
82112
/// Default implementation of a builder function for embeddable objects in
83113
/// Fleather.
84114
///
@@ -255,6 +285,13 @@ class FleatherEditor extends StatefulWidget {
255285
/// Defaults to [defaultFleatherEmbedBuilder].
256286
final FleatherEmbedBuilder embedBuilder;
257287

288+
/// Available configuration for [SpanEmbed]s.
289+
/// If no configuration of found for a [SpanEmbed], builder will fallback to
290+
/// [embedBuilder].
291+
///
292+
/// Defaults to `{}`
293+
final Map<String, FleatherSpanEmbedConfiguration> spanEmbedConfigurations;
294+
258295
/// Configuration that details how spell check should be performed.
259296
///
260297
/// Specifies the [SpellCheckService] used to spell check text input and the
@@ -331,6 +368,7 @@ class FleatherEditor extends StatefulWidget {
331368
this.clipboardStatus,
332369
this.contextMenuBuilder = defaultContextMenuBuilder,
333370
this.embedBuilder = defaultFleatherEmbedBuilder,
371+
this.spanEmbedConfigurations = const {},
334372
this.linkActionPickerDelegate = defaultLinkActionPickerDelegate,
335373
this.textSelectionControls});
336374

@@ -505,6 +543,7 @@ class _FleatherEditorState extends State<FleatherEditor>
505543
scrollPhysics: widget.scrollPhysics,
506544
onLaunchUrl: widget.onLaunchUrl,
507545
embedBuilder: widget.embedBuilder,
546+
spanEmbedConfigurations: widget.spanEmbedConfigurations,
508547
spellCheckConfiguration: widget.spellCheckConfiguration,
509548
linkActionPickerDelegate: widget.linkActionPickerDelegate,
510549
clipboardManager: widget.clipboardManager,
@@ -618,6 +657,7 @@ class RawEditor extends StatefulWidget {
618657
this.contextMenuBuilder = defaultContextMenuBuilder,
619658
this.spellCheckConfiguration,
620659
this.embedBuilder = defaultFleatherEmbedBuilder,
660+
this.spanEmbedConfigurations = const {},
621661
this.linkActionPickerDelegate = defaultLinkActionPickerDelegate,
622662
}) : assert(maxHeight == null || maxHeight > 0),
623663
assert(minHeight == null || minHeight >= 0),
@@ -800,6 +840,8 @@ class RawEditor extends StatefulWidget {
800840
/// Defaults to [defaultFleatherEmbedBuilder].
801841
final FleatherEmbedBuilder embedBuilder;
802842

843+
final Map<String, FleatherSpanEmbedConfiguration> spanEmbedConfigurations;
844+
803845
final LinkActionPickerDelegate linkActionPickerDelegate;
804846

805847
final ClipboardManager clipboardManager;
@@ -1843,6 +1885,7 @@ class RawEditorState extends EditorState
18431885
readOnly: widget.readOnly,
18441886
controller: widget.controller,
18451887
embedBuilder: widget.embedBuilder,
1888+
spanEmbedConfigurations: widget.spanEmbedConfigurations,
18461889
linkActionPicker: _linkActionPicker,
18471890
onLaunchUrl: widget.onLaunchUrl,
18481891
textWidthBasis: widget.textWidthBasis,
@@ -1870,6 +1913,7 @@ class RawEditorState extends EditorState
18701913
? const EdgeInsets.all(16.0)
18711914
: null,
18721915
embedBuilder: widget.embedBuilder,
1916+
spanEmbedConfigurations: widget.spanEmbedConfigurations,
18731917
linkActionPicker: _linkActionPicker,
18741918
onLaunchUrl: widget.onLaunchUrl,
18751919
),

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ class FleatherField extends StatefulWidget {
172172
/// Defaults to [defaultFleatherEmbedBuilder].
173173
final FleatherEmbedBuilder embedBuilder;
174174

175+
/// Available configuration for [SpanEmbed]s.
176+
/// If no configuration of found for a [SpanEmbed], builder will fallback to
177+
/// [embedBuilder].
178+
///
179+
/// Defaults to `{}`
180+
final Map<String, FleatherSpanEmbedConfiguration> spanEmbedConfigurations;
181+
175182
/// Builds the text selection toolbar when requested by the user.
176183
///
177184
/// Defaults to [defaultContextMenuBuilder].
@@ -212,6 +219,7 @@ class FleatherField extends StatefulWidget {
212219
this.contextMenuBuilder = defaultContextMenuBuilder,
213220
this.spellCheckConfiguration,
214221
this.embedBuilder = defaultFleatherEmbedBuilder,
222+
this.spanEmbedConfigurations = const {},
215223
this.clipboardManager = const PlainTextClipboardManager(),
216224
});
217225

@@ -281,6 +289,7 @@ class _FleatherFieldState extends State<FleatherField> {
281289
scrollPhysics: widget.scrollPhysics,
282290
onLaunchUrl: widget.onLaunchUrl,
283291
embedBuilder: widget.embedBuilder,
292+
spanEmbedConfigurations: widget.spanEmbedConfigurations,
284293
spellCheckConfiguration: widget.spellCheckConfiguration,
285294
contextMenuBuilder: widget.contextMenuBuilder,
286295
clipboardManager: widget.clipboardManager,

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)