apply method

  1. @override
Delta? apply(
  1. Delta document,
  2. int index,
  3. Object data
)
override

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

Implementation

@override
Delta? apply(Delta document, int index, Object data) {
  if (data is! String) return null;

  final text = data;
  if (text != '\n') return null;

  final iter = DeltaIterator(document);
  iter.skip(index);
  final target = iter.next();

  // We have an embed right after us, ignore (embeds handled by a different rule).
  if (target.data is! String) return null;

  final targetText = target.data as String;

  if (targetText.startsWith('\n')) {
    Map<String, dynamic>? resetStyle;
    if (target.attributes != null &&
        target.attributes!.containsKey(NotusAttribute.heading.key)) {
      resetStyle = NotusAttribute.heading.unset.toJson();
    }
    return Delta()
      ..retain(index)
      ..insert('\n', target.attributes)
      ..retain(1, resetStyle)
      ..trim();
  }
  return null;
}