aboutsummaryrefslogtreecommitdiff
path: root/Sources/NorgUI/Views/FlatBlocksView.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Sources/NorgUI/Views/FlatBlocksView.swift')
-rw-r--r--Sources/NorgUI/Views/FlatBlocksView.swift42
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
+ }
+ }
+}