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.inline) return null;

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

  // Apply inline styles to all non-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?)! : '';
    var lf = opText.indexOf('\n');
    if (lf != -1) {
      var pos = 0;
      while (lf != -1) {
        result..retain(lf - pos, attribute.toJson())..retain(1);
        pos = lf + 1;
        lf = opText.indexOf('\n', pos);
      }
      if (pos < op.length) result.retain(op.length - pos, attribute.toJson());
    } else {
      result.retain(op.length, attribute.toJson());
    }
    current += op.length;
  }

  return result;
}