apply method

  1. @override
Delta? apply(
  1. Delta document,
  2. int index,
  3. int length,
  4. NotusAttribute attribute,
)
override

Applies heuristic rule to a retain (format) operation on a document and returns resulting Delta.

Implementation

@override
Delta? apply(
    Delta document, int index, int length, NotusAttribute attribute) {
  if (attribute.scope != NotusAttributeScope.line) return null;

  var result = Delta()..retain(index);
  final iter = DeltaIterator(document);
  iter.skip(index);

  // Apply line styles to all newline characters within range of this
  // retain operation.
  var current = 0;
  while (current < length && iter.hasNext) {
    final op = iter.next(length - current);
    final opText = op.data is String ? (op.data as String?)! : '';
    if (opText.contains('\n')) {
      final delta = _applyAttribute(op.data as String, attribute);
      result = result.concat(delta);
    } else {
      result.retain(op.length);
    }
    current += op.length;
  }
  // And include extra newline after retain
  while (iter.hasNext) {
    final op = iter.next();
    final opText = op.data is String ? (op.data as String?)! : '';
    final lf = opText.indexOf('\n');
    if (lf == -1) {
      result..retain(op.length);
      continue;
    }
    result..retain(lf)..retain(1, attribute.toJson());
    break;
  }
  return result;
}