import 'dart:convert'; /// An object which can be embedded into a Quill document. /// /// See also: /// /// * [BlockEmbed] which represents a block embed. class Embeddable { const Embeddable(this.type, this.data); /// The type of this object. final String type; /// The data payload of this object. final dynamic data; Map toJson() { return {type: data}; } static Embeddable fromJson(Map json) { final m = Map.from(json); assert(m.length == 1, 'Embeddable map must only have one key'); return Embeddable(m.keys.first, m.values.first); } } /// There are two built-in embed types supported by Quill documents, however /// the document model itself does not make any assumptions about the types /// of embedded objects and allows users to define their own types. class BlockEmbed extends Embeddable { const BlockEmbed(String type, String data) : super(type, data); static const String imageType = 'image'; static BlockEmbed image(String imageUrl) => BlockEmbed(imageType, imageUrl); static const String videoType = 'video'; static BlockEmbed video(String videoUrl) => BlockEmbed(videoType, videoUrl); static const String customType = 'custom'; static BlockEmbed custom(CustomBlockEmbed customBlock) => BlockEmbed(customType, customBlock.toJsonString()); } class CustomBlockEmbed extends BlockEmbed { const CustomBlockEmbed(String type, String data) : super(type, data); String toJsonString() => jsonEncode(toJson()); static CustomBlockEmbed fromJsonString(String data) { final embeddable = Embeddable.fromJson(jsonDecode(data)); return CustomBlockEmbed(embeddable.type, embeddable.data); } }