nodeMap method

Map<String, Object?> nodeMap(
  1. SemanticNode n, {
  2. bool fold = true,
  3. int? maxDepth,
  4. int depth = 0,
})

One node as a map, folding runs among its children.

Implementation

Map<String, Object?> nodeMap(SemanticNode n, {bool fold = true, int? maxDepth, int depth = 0}) {
  final own = n.toJson()..remove('children');
  if (n.children.isEmpty) return own;
  if (maxDepth != null && depth >= maxDepth) {
    return {...own, 'childCount': n.children.length};
  }
  final kids = <Object?>[];
  var i = 0;
  while (i < n.children.length) {
    final run = fold ? detectFoldRun(n.children, i, threshold: foldThreshold) : null;
    if (run == null) {
      kids.add(nodeMap(n.children[i], fold: fold, maxDepth: maxDepth, depth: depth + 1));
      i++;
      continue;
    }
    final items = n.children.sublist(run.start, run.end);
    kids.add(nodeMap(items.first, fold: fold, maxDepth: maxDepth, depth: depth + 1));
    final firstId = items.first.glintId;
    final base = firstId == null ? null : glintIdBase(firstId);
    kids.add({
      'folded': {
        'count': items.length - 1,
        if (base != null) 'base': base,
        'items': [
          for (final item in items.sublist(1))
            {
              if (item.glintId != null) 'glintId': item.glintId,
              if (foldItemLabel(item) != null) 'label': foldItemLabel(item),
            }
        ],
      }
    });
    i = run.end;
  }
  return {...own, 'children': kids};
}