Create struct for next new line

pull/1128/head
Adil Hanney 2 years ago
parent 007e07357c
commit 3c676a7578
  1. 31
      lib/src/models/rules/insert.dart

@ -1,4 +1,3 @@
import '../../models/documents/document.dart'; import '../../models/documents/document.dart';
import '../documents/attribute.dart'; import '../documents/attribute.dart';
import '../documents/nodes/embeddable.dart'; import '../documents/nodes/embeddable.dart';
@ -55,7 +54,7 @@ class PreserveLineStyleOnSplitRule extends InsertRule {
return delta; return delta;
} }
final nextNewLine = _getNextNewLine(itr); final nextNewLine = _getNextNewLine(itr);
final attributes = nextNewLine.item1?.attributes; final attributes = nextNewLine.operation?.attributes;
return delta..insert('\n', attributes); return delta..insert('\n', attributes);
} }
@ -85,7 +84,8 @@ class PreserveBlockStyleOnInsertRule extends InsertRule {
// Look for the next newline. // 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.operation?.attributes ?? <String, dynamic>{});
final blockStyle = lineStyle.getBlocksExceptHeader(); final blockStyle = lineStyle.getBlocksExceptHeader();
// Are we currently in a block? If not then ignore. // Are we currently in a block? If not then ignore.
@ -125,8 +125,8 @@ class PreserveBlockStyleOnInsertRule extends InsertRule {
// Reset style of the original newline character if needed. // Reset style of the original newline character if needed.
if (resetStyle.isNotEmpty) { if (resetStyle.isNotEmpty) {
delta delta
..retain(nextNewLine.item2!) ..retain(nextNewLine.skipped!)
..retain((nextNewLine.item1!.data as String).indexOf('\n')) ..retain((nextNewLine.operation!.data as String).indexOf('\n'))
..retain(1, resetStyle); ..retain(1, resetStyle);
} }
@ -187,10 +187,10 @@ class AutoExitBlockRule extends InsertRule {
// Keep looking for the next newline character to see if it shares the same // Keep looking for the next newline character to see if it shares the same
// block style as `cur`. // block style as `cur`.
final nextNewLine = _getNextNewLine(itr); final nextNewLine = _getNextNewLine(itr);
if (nextNewLine.item1 != null && if (nextNewLine.operation != null &&
nextNewLine.item1!.attributes != null && nextNewLine.operation!.attributes != null &&
Style.fromJson(nextNewLine.item1!.attributes).getBlockExceptHeader() == Style.fromJson(nextNewLine.operation!.attributes).getBlockExceptHeader()
blockStyle) { == blockStyle) {
// We are not at the end of this block, ignore. // We are not at the end of this block, ignore.
return null; return null;
} }
@ -523,15 +523,22 @@ class CatchAllInsertRule extends InsertRule {
} }
} }
Tuple2<Operation?, int?> _getNextNewLine(DeltaIterator iterator) { _NextNewLine _getNextNewLine(DeltaIterator iterator) {
Operation op; Operation op;
for (var skipped = 0; iterator.hasNext; skipped += op.length!) { for (var skipped = 0; iterator.hasNext; skipped += op.length!) {
op = iterator.next(); op = iterator.next();
final lineBreak = final lineBreak =
(op.data is String ? op.data as String? : '')!.indexOf('\n'); (op.data is String ? op.data as String? : '')!.indexOf('\n');
if (lineBreak >= 0) { if (lineBreak >= 0) {
return Tuple2(op, skipped); return _NextNewLine(op, skipped);
}
} }
return const _NextNewLine(null, null);
} }
return const Tuple2(null, null);
class _NextNewLine {
const _NextNewLine(this.operation, this.skipped);
final Operation? operation;
final int? skipped;
} }

Loading…
Cancel
Save