Update PreserveBlockStyleOnInsertRule to apply all block styles

pull/162/head
Xin Yao 4 years ago
parent df602cfa9b
commit 80d918ef9c
  1. 10
      lib/models/documents/style.dart
  2. 17
      lib/models/rules/insert.dart

@ -61,6 +61,16 @@ class Style {
return null; return null;
} }
Map<String, Attribute> getBlocksExceptHeader() {
final m = <String, Attribute>{};
attributes.forEach((key, value) {
if (Attribute.blockKeysExceptHeader.contains(key)) {
m[key] = value;
}
});
return m;
}
Style merge(Attribute attribute) { Style merge(Attribute attribute) {
final merged = Map<String, Attribute>.from(_attributes); final merged = Map<String, Attribute>.from(_attributes);
if (attribute.value == null) { if (attribute.value == null) {

@ -62,28 +62,32 @@ class PreserveBlockStyleOnInsertRule extends InsertRule {
Delta? applyRule(Delta document, int index, Delta? applyRule(Delta document, int index,
{int? len, Object? data, Attribute? attribute}) { {int? len, Object? data, Attribute? attribute}) {
if (data is! String || !data.contains('\n')) { if (data is! String || !data.contains('\n')) {
// Only interested in text containing at least one newline character.
return null; return null;
} }
final itr = DeltaIterator(document)..skip(index); final itr = DeltaIterator(document)..skip(index);
// Look for the next newline.
final nextNewLine = _getNextNewLine(itr); final nextNewLine = _getNextNewLine(itr);
final lineStyle = final lineStyle =
Style.fromJson(nextNewLine.item1?.attributes ?? <String, dynamic>{}); Style.fromJson(nextNewLine.item1?.attributes ?? <String, dynamic>{});
final attribute = lineStyle.getBlockExceptHeader(); final blockStyle = lineStyle.getBlocksExceptHeader();
if (attribute == null) { // Are we currently in a block? If not then ignore.
if (blockStyle.isEmpty) {
return null; return null;
} }
final blockStyle = <String, dynamic>{attribute.key: attribute.value};
Map<String, dynamic>? resetStyle; Map<String, dynamic>? resetStyle;
// If current line had heading style applied to it we'll need to move this
// style to the newly inserted line before it and reset style of the
// original line.
if (lineStyle.containsKey(Attribute.header.key)) { if (lineStyle.containsKey(Attribute.header.key)) {
resetStyle = Attribute.header.toJson(); resetStyle = Attribute.header.toJson();
} }
// Go over each inserted line and ensure block style is applied.
final lines = data.split('\n'); final lines = data.split('\n');
final delta = Delta()..retain(index + (len ?? 0)); final delta = Delta()..retain(index + (len ?? 0));
for (var i = 0; i < lines.length; i++) { for (var i = 0; i < lines.length; i++) {
@ -92,12 +96,15 @@ class PreserveBlockStyleOnInsertRule extends InsertRule {
delta.insert(line); delta.insert(line);
} }
if (i == 0) { if (i == 0) {
// The first line should inherit the lineStyle entirely.
delta.insert('\n', lineStyle.toJson()); delta.insert('\n', lineStyle.toJson());
} else if (i < lines.length - 1) { } else if (i < lines.length - 1) {
// we don't want to insert a newline after the last chunk of text, so -1
delta.insert('\n', blockStyle); delta.insert('\n', blockStyle);
} }
} }
// Reset style of the original newline character if needed.
if (resetStyle != null) { if (resetStyle != null) {
delta delta
..retain(nextNewLine.item2!) ..retain(nextNewLine.item2!)

Loading…
Cancel
Save