aboutsummaryrefslogtreecommitdiff
path: root/Sources/NorgUI/Views/TreeNodesView.swift
blob: a5091381478f0eb6a98f6e09972f9dced4815758 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import NorgKit
import SwiftUI

struct TreeNodesView: View {
  let nodes: [NorgNode]
  let onSetStatus: (Int, TaskStatus) -> Void

  var body: some View {
    let siblingOrdinals = ordinals(forSiblings: nodes)
    VStack(alignment: .leading, spacing: 10) {
      ForEach(Array(nodes.enumerated()), id: \.offset) { index, node in
        NodeView(node: node, ordinal: siblingOrdinals[index], onSetStatus: onSetStatus)
          .id(node.block.line)
          .frame(maxWidth: .infinity, alignment: .leading)
      }
    }
  }
}

private struct NodeView: View {
  let node: NorgNode
  let ordinal: Int?
  let onSetStatus: (Int, TaskStatus) -> Void

  @State private var expanded = true

  var body: some View {
    if !node.children.isEmpty {
      DisclosureGroup(isExpanded: $expanded) {
        TreeNodesView(nodes: node.children, onSetStatus: onSetStatus)
      } label: {
        label
      }
    } else if let body = node.block.body, !body.isEmpty {
      DisclosureGroup(isExpanded: $expanded) {
        FlatBlocksView(blocks: body, onSetStatus: onSetStatus)
      } label: {
        label
      }
    } else {
      label
    }
  }

  private var label: some View {
    BlockView(block: node.block, ordinal: ordinal, onSetStatus: onSetStatus)
  }
}