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 newline to apply the attributes
  while (iter.hasNext) {
    final op = iter.next();
    final opText = op.data is String ? (op.data as String?)! : '';
    final lf = opText.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;
}