diff --git a/lib/models/documents/attribute.dart b/lib/models/documents/attribute.dart index 4fae59b0..79ad5174 100644 --- a/lib/models/documents/attribute.dart +++ b/lib/models/documents/attribute.dart @@ -66,23 +66,23 @@ class Attribute { } class BoldAttribute extends Attribute { - BoldAttribute() : super('bold', AttributeScope.INLINE, null); + BoldAttribute() : super('bold', AttributeScope.INLINE, true); } class ItalicAttribute extends Attribute { - ItalicAttribute() : super('italic', AttributeScope.INLINE, null); + ItalicAttribute() : super('italic', AttributeScope.INLINE, true); } class UnderlineAttribute extends Attribute { - UnderlineAttribute() : super('underline', AttributeScope.INLINE, null); + UnderlineAttribute() : super('underline', AttributeScope.INLINE, true); } class StrikeThroughAttribute extends Attribute { - StrikeThroughAttribute() : super('strike', AttributeScope.INLINE, null); + StrikeThroughAttribute() : super('strike', AttributeScope.INLINE, true); } class LinkAttribute extends Attribute { - LinkAttribute() : super('link', AttributeScope.INLINE, null); + LinkAttribute() : super('link', AttributeScope.INLINE, ''); } class HeaderAttribute extends Attribute { @@ -109,9 +109,9 @@ class ListAttribute extends Attribute { } class CodeBlockAttribute extends Attribute { - CodeBlockAttribute() : super('code-block', AttributeScope.BLOCK, null); + CodeBlockAttribute() : super('code-block', AttributeScope.BLOCK, true); } class BlockQuoteAttribute extends Attribute { - BlockQuoteAttribute() : super('blockquote', AttributeScope.BLOCK, null); + BlockQuoteAttribute() : super('blockquote', AttributeScope.BLOCK, true); } diff --git a/lib/models/documents/leaf.dart b/lib/models/documents/leaf.dart new file mode 100644 index 00000000..a785e6b2 --- /dev/null +++ b/lib/models/documents/leaf.dart @@ -0,0 +1,6 @@ +import 'node.dart'; + +/* A leaf node in document tree */ +abstract class Leaf extends Node implements StyledNode { + +} diff --git a/lib/models/documents/node.dart b/lib/models/documents/node.dart new file mode 100644 index 00000000..a6cb7e39 --- /dev/null +++ b/lib/models/documents/node.dart @@ -0,0 +1,41 @@ +import 'dart:collection'; + +import 'package:flutter_quill/models/documents/style.dart'; + +import 'attribute.dart'; + +/* node in a document tree */ +abstract class Node extends LinkedListEntry {} + +abstract class StyledNode implements Node { + Style get style => _style; + Style _style = Style(); + + void applyAttribute(Attribute attribute) { + _style = _style.merge(attribute); + } + + void applyStyle(Style value) { + if (value == null) { + throw ArgumentError('null value'); + } + _style = _style.mergeAll(value); + } + + void clearStyle() { + _style = Style(); + } +} + +abstract class Container extends Node { + Container _parent; + + Container get parent => _parent; + + bool get isFirst => list.first == this; + + bool get isLast => list.last == this; +} + +/* Root node of document tree */ +class Root extends Container> {} diff --git a/lib/models/documents/style.dart b/lib/models/documents/style.dart index 6843ed50..d5e6944d 100644 --- a/lib/models/documents/style.dart +++ b/lib/models/documents/style.dart @@ -33,4 +33,22 @@ class Style { Attribute get single => _attributes.values.single; bool containsKey(String key) => _attributes.containsKey(key); + + Style merge(Attribute attribute) { + Map merged = Map.from(_attributes); + if (attribute.value == null) { + merged.remove(attribute.key); + } else { + merged[attribute.key] = attribute; + } + return Style.attr(merged); + } + + Style mergeAll(Style other) { + Style result = Style.attr(_attributes); + for (Attribute attribute in other.values) { + result = result.merge(attribute); + } + return result; + } }