apply method

  1. @override
Delta apply(
  1. Delta document,
  2. int index,
  3. int length
)
override

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

Implementation

@override
Delta apply(Delta document, int index, int length) {
  final iter = DeltaIterator(document);
  iter.skip(index);
  final target = iter.next(1);
  if (target.data != '\n') return null;
  iter.skip(length - 1);
  final result = Delta()
    ..retain(index)
    ..delete(length);

  // Look for next line-break to apply the attributes
  while (iter.hasNext) {
    final op = iter.next();
    final lf = op.data.indexOf('\n');
    if (lf == -1) {
      result..retain(op.length);
      continue;
    }
    var attributes = _unsetAttributes(op.attributes);
    if (target.isNotPlain) {
      attributes ??= <String, dynamic>{};
      attributes.addAll(target.attributes);
    }
    result..retain(lf)..retain(1, attributes);
    break;
  }
  return result;
}