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);
  final before = iter.skip(index);
  final after = iter.next();
  if (isEdgeLineSplit(before, after)) return null;

  // This is not an edge line split, meaning that the cursor is somewhere in
  // the middle of the lines' text. Which in turn means there is no embeds
  // around the cursor and it is safe to assume we have only text.
  final textAfter = after.data as String;

  final result = Delta()..retain(index);
  if (textAfter.contains('\n')) {
    // It is not allowed to combine line and inline styles in insert
    // operation containing newline together with other characters.
    // The only scenario we get such operation is when the text is plain.
    assert(after.isPlain);
    // No attributes to apply so we simply create a new line.
    result..insert('\n');
    return result;
  }
  // Continue looking for a newline.
  final nextNewline = _findNextNewline(iter);
  final attributes = nextNewline.op?.attributes;

  return result..insert('\n', attributes);
}