From 4c5f72826c0aee09cd8a0bade2b5ac8f990efdfc Mon Sep 17 00:00:00 2001 From: Xin Yao Date: Thu, 22 Apr 2021 15:14:54 -0700 Subject: [PATCH] Indent attribute is consider block but may have null value --- lib/models/documents/attribute.dart | 32 +++++++++++++++++++++-------- lib/models/documents/style.dart | 3 ++- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/models/documents/attribute.dart b/lib/models/documents/attribute.dart index 09564e4e..1b9043b9 100644 --- a/lib/models/documents/attribute.dart +++ b/lib/models/documents/attribute.dart @@ -1,3 +1,5 @@ +import 'dart:collection'; + import 'package:quiver/core.dart'; enum AttributeScope { @@ -14,7 +16,7 @@ class Attribute { final AttributeScope scope; final T value; - static final Map _registry = { + static final Map _registry = LinkedHashMap.of({ Attribute.bold.key: Attribute.bold, Attribute.italic.key: Attribute.italic, Attribute.underline.key: Attribute.underline, @@ -26,16 +28,16 @@ class Attribute { Attribute.background.key: Attribute.background, Attribute.placeholder.key: Attribute.placeholder, Attribute.header.key: Attribute.header, - Attribute.indent.key: Attribute.indent, Attribute.align.key: Attribute.align, Attribute.list.key: Attribute.list, Attribute.codeBlock.key: Attribute.codeBlock, Attribute.blockQuote.key: Attribute.blockQuote, + Attribute.indent.key: Attribute.indent, Attribute.width.key: Attribute.width, Attribute.height.key: Attribute.height, Attribute.style.key: Attribute.style, Attribute.token.key: Attribute.token, - }; + }); static final BoldAttribute bold = BoldAttribute(); @@ -88,22 +90,22 @@ class Attribute { Attribute.placeholder.key, }; - static final Set blockKeys = { + static final Set blockKeys = LinkedHashSet.of({ Attribute.header.key, - Attribute.indent.key, Attribute.align.key, Attribute.list.key, Attribute.codeBlock.key, Attribute.blockQuote.key, - }; + Attribute.indent.key, + }); - static final Set blockKeysExceptHeader = { + static final Set blockKeysExceptHeader = LinkedHashSet.of({ Attribute.list.key, - Attribute.indent.key, Attribute.align.key, Attribute.codeBlock.key, Attribute.blockQuote.key, - }; + Attribute.indent.key, + }); static Attribute get h1 => HeaderAttribute(level: 1); @@ -172,6 +174,18 @@ class Attribute { return attribute; } + static int getRegistryOrder(Attribute attribute) { + var order = 0; + for (final attr in _registry.values) { + if (attr.key == attribute.key) { + break; + } + order++; + } + + return order; + } + static Attribute clone(Attribute origin, dynamic value) { return Attribute(origin.key, origin.scope, value); } diff --git a/lib/models/documents/style.dart b/lib/models/documents/style.dart index c805280d..7f3a39ac 100644 --- a/lib/models/documents/style.dart +++ b/lib/models/documents/style.dart @@ -30,7 +30,8 @@ class Style { Iterable get keys => _attributes.keys; - Iterable get values => _attributes.values; + Iterable get values => _attributes.values.sorted( + (a, b) => Attribute.getRegistryOrder(a) - Attribute.getRegistryOrder(b)); Map get attributes => _attributes;