applyRule method

  1. @override
Delta? applyRule(
  1. Document 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(
  Document document,
  int index, {
  int? len,
  Object? data,
  Attribute? attribute,
}) {
  if (data is! String || data != '\n') {
    return null;
  }
  final itr = DeltaIterator(document.toDelta());
  final before = itr.skip(index);
  if (before == null) {
    return null;
  }
  if (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 delta = Delta()..retain(index + (len ?? 0));
  if (after.data is String && (after.data as String).contains('\n')) {
    assert(after.isPlain);
    delta.insert('\n');
    return delta;
  }
  final nextNewLine = _getNextNewLine(itr);
  final attributes = nextNewLine.operation?.attributes;

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