add method

void add(
  1. Operation operation, {
  2. bool transform = true,
})

Adds an operation to the transaction. This method will merge operations if they are both TextEdits.

Also, this method will transform the path of the operations to avoid conflicts.

Implementation

void add(Operation operation, {bool transform = true}) {
  Operation? op = operation;
  final Operation? last = _operations.isEmpty ? null : _operations.last;
  if (last != null) {
    if (op is UpdateTextOperation &&
        last is UpdateTextOperation &&
        op.path.equals(last.path)) {
      final newOp = UpdateTextOperation(
        op.path,
        last.delta.compose(op.delta),
        op.inverted.compose(last.inverted),
      );
      operations[_operations.length - 1] = newOp;
      return;
    }
  }
  if (transform) {
    for (var i = 0; i < _operations.length; i++) {
      if (op == null) {
        continue;
      }
      op = transformOperation(_operations[i], op);
    }
  }
  if (op is UpdateTextOperation && op.delta.isEmpty) {
    return;
  }
  if (op == null) {
    return;
  }
  _operations.add(op);
}