mergeConsecutiveLists function
Merges consecutive lists with the same listType in the document. This ensures that if two bullet lists are adjacent, they become one list. This should be called after operations that create or modify lists.
Implementation
void mergeConsecutiveLists(Root root) {
void mergeInContainer(FNode container) {
final children = childrenOf(container);
final listsToMerge = <FluentList>[];
// Find consecutive lists with the same type
for (var i = 0; i < children.length; i++) {
final child = children[i];
if (child is FluentList) {
// Only merge if it has the same type as the previous list
if (listsToMerge.isEmpty || child.listType == listsToMerge.first.listType) {
listsToMerge.add(child);
} else {
// Different type: merge accumulated lists and start new group
if (listsToMerge.length > 1) {
_mergeLists(root, listsToMerge);
}
listsToMerge.clear();
listsToMerge.add(child);
}
} else {
// Non-list node: merge any accumulated lists and reset
if (listsToMerge.length > 1) {
_mergeLists(root, listsToMerge);
}
listsToMerge.clear();
}
}
// Merge any remaining lists at the end
if (listsToMerge.length > 1) {
_mergeLists(root, listsToMerge);
}
}
// Process root level
mergeInContainer(root);
// Process nested lists (sublists in ListItem children)
void processSublists(FNode node) {
if (node is ListItem) {
for (final child in node.children) {
if (child is FluentList) {
mergeInContainer(child);
processSublists(child);
}
}
} else {
for (final child in childrenOf(node)) {
processSublists(child);
}
}
}
processSublists(root);
}