parseDelta method

Document? parseDelta(
  1. Delta delta
)

Parses a Quill Delta into a structured document.

Returns the parsed Document instance, or null if the delta is empty.

Implementation

Document? parseDelta(fq.Delta delta) {
  if (delta.isEmpty) return null;
  _document.clean();
  _isNumberedListActive = false;
  final List<fq.Operation> denormalizedOperations =
      delta.fullDenormalizer().operations;
  bool wasPreviousNewLine = false;
  bool ignoreNewLine = false;
  for (int index = 0; index < denormalizedOperations.length; index++) {
    ignoreNewLine = true;
    final fq.Operation operation = denormalizedOperations.elementAt(index);
    final nextOp = denormalizedOperations.elementAtOrNull(index + 1);

    // Verify first if the current if a line with just a new line before the last one that is the definitive block attribute
    // _It also ensure to validate if the current operation is just a simple new line_
    //
    // An example of this could be
    // { "insert": "\n", { "header": 1 } }, { "insert": "\n", { "header": 1 } }, { "insert": "Text block breaker" }
    // When verify that the next operation has not the same attrs, then will ignore that new line since
    // that one is the definitive (it was the unique insert with the block attribute, but
    // denormalizer makes this to do more easy store on it)
    if (nextOp != null &&
            mapEquality(operation.attributes, nextOp.attributes) ||
        operation.data == '\n' && operation.attributes == null) {
      ignoreNewLine = false;
    }

    _parseOperation(operation, wasPreviousNewLine, ignoreNewLine);
    wasPreviousNewLine = operation.data == '\n';
  }

  return _document.ensureCorrectFormat();
}