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) {
  final iter = DeltaIterator(document);
  final previous = iter.skip(index);
  final target = iter.next();
  final beforeEmbed = target.data == EmbedNode.kPlainTextPlaceholder;
  final afterEmbed = previous?.data == EmbedNode.kPlainTextPlaceholder;
  if (beforeEmbed || afterEmbed) {
    final delta = Delta()..retain(index);
    if (beforeEmbed && !text.endsWith('\n')) {
      return delta..insert(text)..insert('\n');
    }
    if (afterEmbed && !text.startsWith('\n')) {
      return delta..insert('\n')..insert(text);
    }
    return delta..insert(text);
  }
  return null;
}