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 line-break characters within range of this
  // retain operation.
  var current = 0;
  while (current < length && iter.hasNext) {
    final op = iter.next(length - current);
    if (op!.data.contains('\n')) {
      final delta = _applyAttribute(op.data, attribute);
      result = result.concat(delta);
    } else {
      result.retain(op.length!);
    }
    current += op.length!;
  }
  // And include extra line-break after retain
  while (iter.hasNext) {
    final op = iter.next();
    final lf = op!.data.indexOf('\n');
    if (lf == -1) {
      result..retain(op.length!);
      continue;
    }
    result..retain(lf)..retain(1, attribute.toJson());
    break;
  }
  return result;
}