From 657aa83a5ca24dc35572c040df64a0abb85e8612 Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Mon, 15 Jun 2026 14:35:36 +0200 Subject: Initial extraction from Norganize --- Sources/NorgUI/Views/FlatBlocksView.swift | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Sources/NorgUI/Views/FlatBlocksView.swift (limited to 'Sources/NorgUI/Views/FlatBlocksView.swift') 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 + } + } +} -- cgit