applyRule method

  1. @override
Delta? applyRule(
  1. Delta document,
  2. int index, {
  3. int? len,
  4. Object? data,
  5. Attribute? attribute,
})
override

Implementation

@override
Delta? applyRule(Delta document, int index,
    {int? len, Object? data, Attribute? attribute}) {
  final itr = DeltaIterator(document);

  var op = itr.skip(index);
  int? indexDelta = 0, lengthDelta = 0, remain = len;
  var embedFound = op != null && op.data is! String;
  final hasLineBreakBefore =
      !embedFound && (op == null || (op.data as String).endsWith('\n'));
  if (embedFound) {
    var candidate = itr.next(1);
    if (remain != null) {
      remain--;
      if (candidate.data == '\n') {
        indexDelta++;
        lengthDelta--;

        candidate = itr.next(1);
        remain--;
        if (candidate.data == '\n') {
          lengthDelta++;
        }
      }
    }
  }

  op = itr.skip(remain!);
  if (op != null &&
      (op.data is String ? op.data as String? : '')!.endsWith('\n')) {
    final candidate = itr.next(1);
    if (candidate.data is! String && !hasLineBreakBefore) {
      embedFound = true;
      lengthDelta--;
    }
  }

  if (!embedFound) {
    return null;
  }

  return Delta()
    ..retain(index + indexDelta)
    ..delete(len! + lengthDelta);
}