replace method

Delta replace(
  1. int index,
  2. int length,
  3. Object data
)

Replaces length of characters starting at index with data.

This method applies heuristic rules before modifying this document and produces a change event with its source set to ChangeSource.local.

Returns an instance of Delta actually composed into this document.

Implementation

Delta replace(int index, int length, Object data) {
  assert(data is String || data is EmbeddableObject);

  final dataIsNotEmpty = (data is String) ? data.isNotEmpty : true;

  assert(index >= 0 && (dataIsNotEmpty || length > 0),
      'With index $index, length $length and text "$data"');

  var delta = Delta();

  // We have to insert before applying delete rules
  // Otherwise delete would be operating on stale document snapshot.
  if (dataIsNotEmpty) {
    delta = insert(index + length, data);
  }

  if (length > 0) {
    final deleteDelta = delete(index, length);
    delta = delta.compose(deleteDelta);
  }

  return delta;
}