Skip to content

Commit 7cdbdd9

Browse files
committed
Make attribute registry ordered
1 parent 900d0f2 commit 7cdbdd9

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

lib/models/documents/attribute.dart

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:collection';
2+
13
import 'package:quiver/core.dart';
24

35
enum AttributeScope {
@@ -14,7 +16,7 @@ class Attribute<T> {
1416
final AttributeScope scope;
1517
final T value;
1618

17-
static final Map<String, Attribute> _registry = {
19+
static final Map<String, Attribute> _registry = LinkedHashMap.of({
1820
Attribute.bold.key: Attribute.bold,
1921
Attribute.italic.key: Attribute.italic,
2022
Attribute.underline.key: Attribute.underline,
@@ -26,16 +28,16 @@ class Attribute<T> {
2628
Attribute.background.key: Attribute.background,
2729
Attribute.placeholder.key: Attribute.placeholder,
2830
Attribute.header.key: Attribute.header,
29-
Attribute.indent.key: Attribute.indent,
3031
Attribute.align.key: Attribute.align,
3132
Attribute.list.key: Attribute.list,
3233
Attribute.codeBlock.key: Attribute.codeBlock,
3334
Attribute.blockQuote.key: Attribute.blockQuote,
35+
Attribute.indent.key: Attribute.indent,
3436
Attribute.width.key: Attribute.width,
3537
Attribute.height.key: Attribute.height,
3638
Attribute.style.key: Attribute.style,
3739
Attribute.token.key: Attribute.token,
38-
};
40+
});
3941

4042
static final BoldAttribute bold = BoldAttribute();
4143

@@ -88,22 +90,22 @@ class Attribute<T> {
8890
Attribute.placeholder.key,
8991
};
9092

91-
static final Set<String> blockKeys = {
93+
static final Set<String> blockKeys = LinkedHashSet.of({
9294
Attribute.header.key,
93-
Attribute.indent.key,
9495
Attribute.align.key,
9596
Attribute.list.key,
9697
Attribute.codeBlock.key,
9798
Attribute.blockQuote.key,
98-
};
99+
Attribute.indent.key,
100+
});
99101

100-
static final Set<String> blockKeysExceptHeader = {
102+
static final Set<String> blockKeysExceptHeader = LinkedHashSet.of({
101103
Attribute.list.key,
102-
Attribute.indent.key,
103104
Attribute.align.key,
104105
Attribute.codeBlock.key,
105106
Attribute.blockQuote.key,
106-
};
107+
Attribute.indent.key,
108+
});
107109

108110
static Attribute<int?> get h1 => HeaderAttribute(level: 1);
109111

@@ -172,6 +174,18 @@ class Attribute<T> {
172174
return attribute;
173175
}
174176

177+
static int getRegistryOrder(Attribute attribute) {
178+
var order = 0;
179+
for (final attr in _registry.values) {
180+
if (attr.key == attribute.key) {
181+
break;
182+
}
183+
order++;
184+
}
185+
186+
return order;
187+
}
188+
175189
static Attribute clone(Attribute origin, dynamic value) {
176190
return Attribute(origin.key, origin.scope, value);
177191
}

lib/models/documents/style.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class Style {
3030

3131
Iterable<String> get keys => _attributes.keys;
3232

33-
Iterable<Attribute> get values => _attributes.values;
33+
Iterable<Attribute> get values => _attributes.values.sorted(
34+
(a, b) => Attribute.getRegistryOrder(a) - Attribute.getRegistryOrder(b));
3435

3536
Map<String, Attribute> get attributes => _attributes;
3637

0 commit comments

Comments
 (0)