diff options
| author | Ruben Beltran del Rio <jj@r.bdr.sh> | 2026-06-15 14:35:36 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <jj@r.bdr.sh> | 2026-06-16 14:47:30 +0200 |
| commit | 657aa83a5ca24dc35572c040df64a0abb85e8612 (patch) | |
| tree | fe0b6fd425d8cad9dc6a28ada4ad8600e42b367e /Sources/NorgUI/Views/FlatBlocksView.swift | |
Initial extraction from Norganize
Diffstat (limited to 'Sources/NorgUI/Views/FlatBlocksView.swift')
| -rw-r--r-- | Sources/NorgUI/Views/FlatBlocksView.swift | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/Sources/NorgUI/Views/FlatBlocksView.swift b/Sources/NorgUI/Views/FlatBlocksView.swift new file mode 100644 index 0000000..185d08b --- /dev/null +++ b/Sources/NorgUI/Views/FlatBlocksView.swift @@ -0,0 +1,42 @@ +import NorgKit +import SwiftUI + +struct FlatBlocksView: View { + let blocks: [NorgBlock] + let onSetStatus: (Int, TaskStatus) -> Void + var indent: CGFloat = 0 + + @Environment(\.norgTheme) private var theme + + var body: some View { + let ordinals = orderedOrdinals(blocks) + VStack(alignment: .leading, spacing: 10) { + ForEach(Array(blocks.enumerated()), id: \.offset) { index, block in + row(block, ordinal: ordinals[index]) + } + } + } + + @ViewBuilder + private func row(_ block: NorgBlock, ordinal: Int?) -> some View { + BlockView(block: block, ordinal: ordinal, onSetStatus: onSetStatus) + .padding(.leading, indent + levelIndent(block)) + .id(block.line) + .frame(maxWidth: .infinity, alignment: .leading) + + if let body = block.body, !body.isEmpty { + FlatBlocksView(blocks: body, onSetStatus: onSetStatus, indent: indent + theme.indentWidth) + } + } + + private func levelIndent(_ block: NorgBlock) -> CGFloat { + switch block { + case .unorderedListItem(let level, _, _, _), + .orderedListItem(let level, _, _, _), + .quote(let level, _, _, _): + return CGFloat(max(0, level - 1)) * theme.indentWidth + default: + return 0 + } + } +} |