mergeConsecutiveLists function

void mergeConsecutiveLists(
  1. Root root
)

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>[];

    for (var i = 0; i < children.length; i++) {
      final child = children[i];
      if (child is FluentList) {
        if (listsToMerge.isEmpty || child.listType == listsToMerge.first.listType) {
          listsToMerge.add(child);
        } else {
          if (listsToMerge.length > 1) {
            _mergeLists(root, listsToMerge);
          }
          listsToMerge.clear();
          listsToMerge.add(child);
        }
      } else {
        if (listsToMerge.length > 1) {
          _mergeLists(root, listsToMerge);
        }
        listsToMerge.clear();
      }
    }

    if (listsToMerge.length > 1) {
      _mergeLists(root, listsToMerge);
    }
  }

  mergeInContainer(root);

  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);
}