add method

void add(
  1. TextOperation textOperation
)

Implementation

void add(TextOperation textOperation) {
  if (textOperation.isEmpty) {
    return;
  }
  _plainText = null;

  if (_operations.isNotEmpty) {
    final lastOp = _operations.last;
    if (lastOp is TextDelete && textOperation is TextDelete) {
      lastOp.length += textOperation.length;
      return;
    }
    if (_mapEquals(lastOp.attributes, textOperation.attributes)) {
      if (lastOp is TextInsert && textOperation is TextInsert) {
        lastOp.text += textOperation.text;
        return;
      }
      // if there is an delete before the insert
      // swap the order
      if (lastOp is TextDelete && textOperation is TextInsert) {
        _operations.removeLast();
        _operations.add(textOperation);
        _operations.add(lastOp);
        return;
      }
      if (lastOp is TextRetain && textOperation is TextRetain) {
        lastOp.length += textOperation.length;
        return;
      }
    }
  }

  _operations.add(textOperation);
}