applyRule method

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

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

Implementation

@override
Delta? applyRule(Delta document, int index,
    {int? len, Object? data, Attribute? attribute}) {
  if (data is! String || data != '\n') {
    return null;
  }

  final itr = DeltaIterator(document);
  final before = itr.skip(index);
  if (before == null ||
      before.data is! String ||
      (before.data as String).endsWith('\n')) {
    return null;
  }
  final after = itr.next();
  if (after.data is! String || (after.data as String).startsWith('\n')) {
    return null;
  }

  final text = after.data as String;

  final delta = Delta()..retain(index + (len ?? 0));
  if (text.contains('\n')) {
    assert(after.isPlain);
    delta.insert('\n');
    return delta;
  }
  final nextNewLine = _getNextNewLine(itr);
  final attributes = nextNewLine.item1?.attributes;

  return delta..insert('\n', attributes);
}