compose method

void compose(
  1. Delta delta,
  2. ChangeSource changeSource
)

Composes change Delta into this document.

Use this method with caution as it does not apply heuristic rules to the change.

It is callers responsibility to ensure that the change conforms to the document model semantics and can be composed with the current state of this document.

In case the change is invalid, behavior of this method is unspecified.

Implementation

void compose(Delta delta, ChangeSource changeSource) {
  assert(!documentChangeObserver.isClosed);
  delta.trim();
  assert(delta.isNotEmpty);

  var offset = 0;
  delta = _transform(delta);
  final originalDelta = toDelta();
  for (final op in delta.toList()) {
    final style =
        op.attributes != null ? Style.fromJson(op.attributes) : null;

    if (op.isInsert) {
      // Must normalize data before inserting into the document, makes sure
      // that any embedded objects are converted into EmbeddableObject type.
      _root.insert(offset, _normalize(op.data), style);
    } else if (op.isDelete) {
      _root.delete(offset, op.length);
    } else if (op.attributes != null) {
      _root.retain(offset, op.length, style);
    }

    if (!op.isDelete) {
      offset += op.length!;
    }
  }
  try {
    _delta = _delta.compose(delta);
  } catch (e) {
    throw StateError('_delta compose failed');
  }
  assert(_delta == _root.toDelta(), 'Compose failed');
  final change = DocChange(originalDelta, delta, changeSource);
  documentChangeObserver.add(change);
  history.handleDocChange(change);
}