From 5c12b11777710342b8550bfe736d3c5851187799 Mon Sep 17 00:00:00 2001 From: X Code Date: Sat, 18 Dec 2021 17:38:43 -0800 Subject: [PATCH] Refactor out _getRemovedBlocks method --- lib/src/models/rules/format.dart | 39 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/src/models/rules/format.dart b/lib/src/models/rules/format.dart index 1a2f2e5a..3c23b15f 100644 --- a/lib/src/models/rules/format.dart +++ b/lib/src/models/rules/format.dart @@ -43,16 +43,7 @@ class ResolveLineFormatRule extends FormatRule { final tmp = Delta(); var offset = 0; - // Enforce Block Format exclusivity by rule - final removedBlocks = Attribute.exclusiveBlockKeys.contains(attribute.key) - ? op.attributes?.keys - .where((key) => - Attribute.exclusiveBlockKeys.contains(key) && - attribute.key != key && - attribute.value != null) - .map((key) => MapEntry(key, null)) ?? - [] - : >[]; + final removedBlocks = _getRemovedBlocks(attribute, op); for (var lineBreak = text.indexOf('\n'); lineBreak >= 0; @@ -74,16 +65,8 @@ class ResolveLineFormatRule extends FormatRule { delta.retain(op.length!); continue; } - // Enforce Block Format exclusivity by rule - final removedBlocks = Attribute.exclusiveBlockKeys.contains(attribute.key) - ? op.attributes?.keys - .where((key) => - Attribute.exclusiveBlockKeys.contains(key) && - attribute.key != key && - attribute.value != null) - .map((key) => MapEntry(key, null)) ?? - [] - : >[]; + + final removedBlocks = _getRemovedBlocks(attribute, op); delta ..retain(lineBreak) ..retain(1, attribute.toJson()..addEntries(removedBlocks)); @@ -91,6 +74,22 @@ class ResolveLineFormatRule extends FormatRule { } return delta; } + + Iterable> _getRemovedBlocks( + Attribute attribute, Operation op) { + // Enforce Block Format exclusivity by rule + if (!Attribute.exclusiveBlockKeys.contains(attribute.key)) { + return >[]; + } + + return op.attributes?.keys + .where((key) => + Attribute.exclusiveBlockKeys.contains(key) && + attribute.key != key && + attribute.value != null) + .map((key) => MapEntry(key, null)) ?? + []; + } } /// Allows updating link format with collapsed selection.