diff --git a/lib/models/documents/attribute.dart b/lib/models/documents/attribute.dart index cb580103..3159ee20 100644 --- a/lib/models/documents/attribute.dart +++ b/lib/models/documents/attribute.dart @@ -7,9 +7,50 @@ enum AttributeScope { class Attribute { final String key; final AttributeScope scope; - final T value; + T value; Attribute(this.key, this.scope, this.value); + + static final Map _registry = { + Attribute.bold.key: Attribute.bold, + Attribute.italic.key: Attribute.italic, + Attribute.underline.key: Attribute.underline, + Attribute.strikeThrough.key: Attribute.strikeThrough, + Attribute.link.key: Attribute.link, + Attribute.headerAttribute.key: Attribute.headerAttribute, + Attribute.listAttribute.key: Attribute.listAttribute, + Attribute.codeBlockAttribute.key: Attribute.codeBlockAttribute, + Attribute.blockQuoteAttribute.key: Attribute.blockQuoteAttribute, + }; + + static BoldAttribute bold = BoldAttribute(); + + static ItalicAttribute italic = ItalicAttribute(); + + static UnderlineAttribute underline = UnderlineAttribute(); + + static StrikeThroughAttribute strikeThrough = StrikeThroughAttribute(); + + static LinkAttribute link = LinkAttribute(); + + static HeaderAttribute headerAttribute = HeaderAttribute(1); + + static ListAttribute listAttribute = ListAttribute('ordered'); + + static CodeBlockAttribute codeBlockAttribute = CodeBlockAttribute(); + + static BlockQuoteAttribute blockQuoteAttribute = BlockQuoteAttribute(); + + static Attribute fromKeyValue(String key, dynamic value) { + if (!_registry.containsKey(key)) { + throw ArgumentError.value(key, 'key "$key" not found.'); + } + Attribute attribute = _registry[key]; + if (value != null) { + attribute.value = value; + } + return attribute; + } } class BoldAttribute extends Attribute { diff --git a/lib/models/documents/style.dart b/lib/models/documents/style.dart new file mode 100644 index 00000000..43025310 --- /dev/null +++ b/lib/models/documents/style.dart @@ -0,0 +1,22 @@ +import 'package:flutter_quill/models/documents/attribute.dart'; + +/* Collection of style attributes */ +class Style { + final Map _attributes; + + Style.attr(this._attributes); + + Style() : _attributes = {}; + + static Style fromJson(Map attributes) { + if (attributes == null) { + return Style(); + } + + Map result = attributes.map((String key, dynamic value) { + var attr = Attribute.fromKeyValue(key, value); + return MapEntry(key, attr); + }); + return Style.attr(result); + } +}