recalculateListIndices function

void recalculateListIndices(
  1. Root root
)

Recalculates the indices of all lists in the document. Updates indexList for each ListItem based on hierarchical position.

Implementation

void recalculateListIndices(Root root) {
  void recalculateList(FluentList list, List<int> parentIndices) {
    for (var i = 0; i < list.items.length; i++) {
      final item = list.items[i];
      // Build the new indexList: parent + current position (1-based)
      final newIndexList = [...parentIndices, i + 1];
      item.indexList = newIndexList;

      // Check if the item has sublists in its children
      for (final child in item.children) {
        if (child is FluentList) {
          recalculateList(child, newIndexList);
        }
      }
    }
  }

  // Start from the root level
  for (final node in root.nodes) {
    if (node is FluentList) {
      recalculateList(node, []);
    }
  }
}