applyRule method

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

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 (attribute!.scope != AttributeScope.inline) {
    return null;
  }

  final delta = Delta()..retain(index);
  final itr = DeltaIterator(document.toDelta())..skip(index);

  Operation op;
  for (var cur = 0; cur < len! && itr.hasNext; cur += op.length!) {
    op = itr.next(len - cur);
    final text = op.data is String ? (op.data as String?)! : '';
    var lineBreak = text.indexOf('\n');
    if (lineBreak < 0) {
      delta.retain(op.length!, attribute.toJson());
      continue;
    }
    var pos = 0;
    while (lineBreak >= 0) {
      delta
        ..retain(lineBreak - pos, attribute.toJson())
        ..retain(1);
      pos = lineBreak + 1;
      lineBreak = text.indexOf('\n', pos);
    }
    if (pos < op.length!) {
      delta.retain(op.length! - pos, attribute.toJson());
    }
  }

  return delta;
}