insert method

Delta insert(
  1. int index,
  2. Object? data, {
  3. int replaceLength = 0,
})

Inserts data in this document at specified index.

The data parameter can be either a String or an instance of Embeddable.

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 insert(int index, Object? data, {int replaceLength = 0}) {
  assert(index >= 0);
  assert(data is String || data is Embeddable);
  if (data is Embeddable) {
    data = data.toJson();
  } else if ((data as String).isEmpty) {
    return Delta();
  }

  final delta = _rules.apply(RuleType.insert, this, index,
      data: data, len: replaceLength);
  compose(delta, ChangeSource.local);
  return delta;
}