apply method

  1. @override
Delta? apply(
  1. Delta document,
  2. int index,
  3. String text
)
override

Applies heuristic rule to an insert operation on a document and returns resulting Delta.

Implementation

@override
Delta? apply(Delta document, int index, String text) {
  if (text != '\n') return null;

  final iter = DeltaIterator(document);
  final before = iter.skip(index);
  final after = iter.next();
  if (isEdgeLineSplit(before, after!)) return null;
  final result = Delta()..retain(index);
  if (after.data.contains('\n')) {
    // It is not allowed to combine line and inline styles in insert
    // operation containing line-break together with other characters.
    // The only scenario we get such operation is when the text is plain.
    assert(after.isPlain);
    // No attributes to apply so we simply create a new line.
    result.insert('\n');
    return result;
  }
  // Continue looking for line-break.
  Map<String, dynamic>? attributes;
  while (iter.hasNext) {
    final op = iter.next();
    final lf = op!.data.indexOf('\n');
    if (lf >= 0) {
      attributes = op.attributes;
      break;
    }
  }
  result.insert('\n', attributes);
  return result;
}