dartlangeditorflutterflutter-appsflutter-examplesflutter-packageflutter-widgetquillquill-deltaquilljsreactquillrich-textrich-text-editorwysiwygwysiwyg-editor
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.8 KiB
59 lines
1.8 KiB
1 year ago
|
import 'dart:convert' show jsonDecode, jsonEncode;
|
||
3 years ago
|
|
||
3 years ago
|
/// 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<String, dynamic> toJson() {
|
||
3 years ago
|
return {type: data};
|
||
3 years ago
|
}
|
||
|
|
||
|
static Embeddable fromJson(Map<String, dynamic> json) {
|
||
|
final m = Map<String, dynamic>.from(json);
|
||
|
assert(m.length == 1, 'Embeddable map must only have one key');
|
||
|
|
||
3 years ago
|
return Embeddable(m.keys.first, m.values.first);
|
||
3 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/// 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 {
|
||
1 year ago
|
const BlockEmbed(super.type, String super.data);
|
||
3 years ago
|
|
||
|
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);
|
||
3 years ago
|
|
||
3 years ago
|
static const String formulaType = 'formula';
|
||
|
static BlockEmbed formula(String formula) => BlockEmbed(formulaType, formula);
|
||
|
|
||
3 years ago
|
static const String customType = 'custom';
|
||
|
static BlockEmbed custom(CustomBlockEmbed customBlock) =>
|
||
|
BlockEmbed(customType, customBlock.toJsonString());
|
||
|
}
|
||
|
|
||
|
class CustomBlockEmbed extends BlockEmbed {
|
||
1 year ago
|
const CustomBlockEmbed(super.type, super.data);
|
||
3 years ago
|
|
||
|
String toJsonString() => jsonEncode(toJson());
|
||
|
|
||
|
static CustomBlockEmbed fromJsonString(String data) {
|
||
|
final embeddable = Embeddable.fromJson(jsonDecode(data));
|
||
|
return CustomBlockEmbed(embeddable.type, embeddable.data);
|
||
|
}
|
||
3 years ago
|
}
|