apply method

  1. @override
Delta? apply(
  1. Delta document,
  2. int index,
  3. String text
)
override

Applies heuristic rule to an insert operation on a document and returns resulting Delta.

Implementation

@override
Delta? apply(Delta document, int index, String text) {
  if (text != '\n') return null;

  final iter = DeltaIterator(document);
  final previous = iter.skip(index);
  final target = iter.next();
  final isInBlock = target != null &&
      target.isNotPlain &&
      target.attributes!.containsKey(NotusAttribute.block.key);
  if (isEmptyLine(previous, target!) && isInBlock) {
    // We reset block style even if this line is not the last one in it's
    // block which effectively splits the block into two.
    // TODO: For code blocks this should not split the block but allow inserting as many lines as needed.
    var attributes;
    if (target.attributes != null) {
      attributes = target.attributes;
    } else {
      attributes = <String, dynamic>{};
    }
    attributes.addAll(NotusAttribute.block.unset.toJson());
    return Delta()..retain(index)..retain(1, attributes);
  }
  return null;
}