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.block) {
    return null;
  }

  // Apply line styles to all newline characters within range of this
  // retain operation.
  var result = 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 opText = op.data is String ? op.data as String : '';
    if (!opText.contains('\n')) {
      result.retain(op.length!);
      continue;
    }

    final delta = _applyAttribute(opText, op, attribute);
    result = result.concat(delta);
  }
  // And include extra newline after retain
  while (itr.hasNext) {
    op = itr.next();
    final opText = op.data is String ? op.data as String : '';
    final lf = opText.indexOf('\n');
    if (lf < 0) {
      result.retain(op.length!);
      continue;
    }

    final delta = _applyAttribute(opText, op, attribute, firstOnly: true);
    result = result.concat(delta);
    break;
  }
  return result;
}