apply method

  1. @override
Delta? apply(
  1. Delta document,
  2. int index,
  3. int length,
  4. NotusAttribute attribute,
)
override

Applies heuristic rule to a retain (format) operation on a document and returns resulting Delta.

Implementation

@override
Delta? apply(Delta document, int index, int length, NotusAttribute attribute) {
  // We are only interested in embed attributes
  if (attribute is! EmbedAttribute) return null;
  EmbedAttribute embed = attribute;

  if (length == 1 && embed.isUnset) {
    // Remove the embed.
    return Delta()
      ..retain(index)
      ..delete(length);
  } else {
    // If length is 0 we treat it as an insert at specified [index].
    // If length is non-zero we treat it as a replace of selected range
    // with the embed.
    assert(!embed.isUnset);
    return _insertEmbed(document, index, length, embed);
  }
}