parseDelta method
Parses a Quill Delta into a structured document.
returnNoSealedCopies
indicates if will need to return a deep copy of the elements to avoid return a Paragraphs that cannot add more elementsignoreAllNewLines
indicates that all the new lines with no block-level target to apply will be ignored
Implementation
Document? parseDelta({
required fq.Delta delta,
bool returnNoSealedCopies = false,
bool ignoreAllNewLines = false,
}) {
if (delta.isEmpty) return null;
_document.clean();
final List<fq.Operation> denormalizedOperations =
delta.denormalize().operations;
// sometimes, we can find only new lines at the start of the Delta, then to avoid remove them, we
// will need to add a verification
bool startParagraphNewLineChecking = false;
final it = denormalizedOperations.iterator;
int index = 0;
while (it.moveNext()) {
final fq.Operation? previousOperation =
index == 0 ? null : denormalizedOperations.elementAtOrNull(index - 1);
final fq.Operation operation = it.current;
final fq.Operation? nextOp =
denormalizedOperations.elementAtOrNull(index + 1);
_checkOperation(index, operation);
if (nextOp != null) _checkOperation(index, nextOp);
if (ignoreAllNewLines &&
operation.data == '\n' &&
operation.attributes == null) {
continue;
}
if (!startParagraphNewLineChecking) {
startParagraphNewLineChecking = operation.data != '\n';
}
if (operation.data == '\n' &&
!startParagraphNewLineChecking &&
!ignoreAllNewLines) {
_document
.insert(Paragraph.newLine(blockAttributes: operation.attributes));
index++;
continue;
}
final bool isParagraphBreak =
previousOperation?.data != '\n' && operation.data == '\n';
final bool isBlankLine =
previousOperation?.data == '\n' && operation.data == '\n';
final bool hasNextOp = nextOp != null;
final bool isLastInsertion = isParagraphBreak && !hasNextOp;
// updates here
index++;
if (operation.data is! String) {
_applyEmbed(operation: operation);
} else if (operation.data == '\n') {
_applyNewLine(
operation: operation,
isBlankLine: isBlankLine,
ignoreAllNewLines: ignoreAllNewLines,
isParagraphBreak: isParagraphBreak,
isLastInsertion: isLastInsertion,
);
} else {
_applyText(operation, hasNextOp);
}
}
if (mergerBuilder.enabled) {
final List<Paragraph> paragraphs = <Paragraph>[..._document.paragraphs];
_document.clean();
final Iterable<Paragraph> newParagraphs = mergerBuilder.buildAccumulation(
paragraphs,
);
_document.paragraphs.addAll(newParagraphs);
}
if (returnNoSealedCopies) {
return Document(
paragraphs: _document.paragraphs
.map(
(pr) => pr.clone,
)
.toList(),
);
}
return _document;
}