Added `unknownEmbedBuilder` to QuillEditor (#1073)

* Added `unknownEmbedBuilder` to QuillEditor

* `unknownEmbedBuilder` constructor parameter
pull/1076/head
Alex Isaienko 2 years ago committed by GitHub
parent 61a4c6cc35
commit 0e5556424d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      lib/src/widgets/editor.dart

@ -173,6 +173,7 @@ class QuillEditor extends StatefulWidget {
this.onSingleLongTapMoveUpdate, this.onSingleLongTapMoveUpdate,
this.onSingleLongTapEnd, this.onSingleLongTapEnd,
this.embedBuilders, this.embedBuilders,
this.unknownEmbedBuilder,
this.linkActionPickerDelegate = defaultLinkActionPickerDelegate, this.linkActionPickerDelegate = defaultLinkActionPickerDelegate,
this.customStyleBuilder, this.customStyleBuilder,
this.locale, this.locale,
@ -359,6 +360,7 @@ class QuillEditor extends StatefulWidget {
onSingleLongTapEnd; onSingleLongTapEnd;
final Iterable<EmbedBuilder>? embedBuilders; final Iterable<EmbedBuilder>? embedBuilders;
final EmbedsBuilder? unknownEmbedBuilder;
final CustomStyleBuilder? customStyleBuilder; final CustomStyleBuilder? customStyleBuilder;
/// The locale to use for the editor toolbar, defaults to system locale /// The locale to use for the editor toolbar, defaults to system locale
@ -491,7 +493,13 @@ class QuillEditorState extends State<QuillEditor>
node, node,
readOnly, readOnly,
) => ) =>
_buildCustomBlockEmbed(node, context, controller, readOnly), _buildCustomBlockEmbed(
node,
context,
controller,
readOnly,
widget.unknownEmbedBuilder,
),
linkActionPickerDelegate: widget.linkActionPickerDelegate, linkActionPickerDelegate: widget.linkActionPickerDelegate,
customStyleBuilder: widget.customStyleBuilder, customStyleBuilder: widget.customStyleBuilder,
floatingCursorDisabled: widget.floatingCursorDisabled, floatingCursorDisabled: widget.floatingCursorDisabled,
@ -525,18 +533,22 @@ class QuillEditorState extends State<QuillEditor>
return editor; return editor;
} }
Widget _buildCustomBlockEmbed(Embed node, BuildContext context, Widget _buildCustomBlockEmbed(
QuillController controller, bool readOnly) { Embed node,
BuildContext context,
QuillController controller,
bool readOnly,
EmbedsBuilder? unknownEmbedBuilder,
) {
final builders = widget.embedBuilders; final builders = widget.embedBuilders;
if (builders != null) {
var _node = node; var _node = node;
// Creates correct node for custom embed // Creates correct node for custom embed
if (node.value.type == BlockEmbed.customType) { if (node.value.type == BlockEmbed.customType) {
_node = Embed(CustomBlockEmbed.fromJsonString(node.value.data)); _node = Embed(CustomBlockEmbed.fromJsonString(node.value.data));
} }
if (builders != null) {
for (final builder in builders) { for (final builder in builders) {
if (builder.key == _node.value.type) { if (builder.key == _node.value.type) {
return builder.build(context, controller, _node, readOnly); return builder.build(context, controller, _node, readOnly);
@ -544,10 +556,15 @@ class QuillEditorState extends State<QuillEditor>
} }
} }
if (unknownEmbedBuilder != null) {
return unknownEmbedBuilder(context, controller, _node, readOnly);
}
throw UnimplementedError( throw UnimplementedError(
'Embeddable type "${node.value.type}" is not supported by supplied ' 'Embeddable type "${node.value.type}" is not supported by supplied '
'embed builders. You must pass your own builder function to ' 'embed builders. You must pass your own builder function to '
'embedBuilders property of QuillEditor or QuillField widgets.', 'embedBuilders property of QuillEditor or QuillField widgets or '
'specify an unknownEmbedBuilder.',
); );
} }

Loading…
Cancel
Save