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;

  final iter = DeltaIterator(document);
  final previous = iter.skip(index);
  final target = iter.next();
  final cursorBeforeEmbed = target.data is! String;
  final cursorAfterEmbed = previous != null && previous.data is! String;

  if (cursorBeforeEmbed || cursorAfterEmbed) {
    final delta = Delta()..retain(index);
    if (cursorBeforeEmbed && !text.endsWith('\n')) {
      return delta..insert(text)..insert('\n');
    }
    if (cursorAfterEmbed && !text.startsWith('\n')) {
      return delta..insert('\n')..insert(text);
    }
    return delta..insert(text);
  }
  return null;
}