aboutsummaryrefslogtreecommitdiff
path: root/Sources/NorgUI/Views/NorgView.swift
diff options
context:
space:
mode:
authorRuben Beltran del Rio <jj@r.bdr.sh>2026-06-15 14:35:36 +0200
committerRuben Beltran del Rio <jj@r.bdr.sh>2026-06-16 14:47:30 +0200
commit657aa83a5ca24dc35572c040df64a0abb85e8612 (patch)
treefe0b6fd425d8cad9dc6a28ada4ad8600e42b367e /Sources/NorgUI/Views/NorgView.swift
Initial extraction from Norganize
Diffstat (limited to 'Sources/NorgUI/Views/NorgView.swift')
-rw-r--r--Sources/NorgUI/Views/NorgView.swift43
1 files changed, 43 insertions, 0 deletions
diff --git a/Sources/NorgUI/Views/NorgView.swift b/Sources/NorgUI/Views/NorgView.swift
new file mode 100644
index 0000000..6f3deac
--- /dev/null
+++ b/Sources/NorgUI/Views/NorgView.swift
@@ -0,0 +1,43 @@
+import NorgKit
+import SwiftUI
+
+public struct NorgView: View {
+ let document: NorgDocument
+ let collapsible: Bool
+ let onSetStatus: (Int, TaskStatus) -> Void
+ let onOpenLink: ((InlineLink) -> Void)?
+
+ public init(
+ document: NorgDocument,
+ collapsible: Bool = false,
+ onSetStatus: @escaping (Int, TaskStatus) -> Void = { _, _ in },
+ onOpenLink: ((InlineLink) -> Void)? = nil
+ ) {
+ self.document = document
+ self.collapsible = collapsible
+ self.onSetStatus = onSetStatus
+ self.onOpenLink = onOpenLink
+ }
+
+ public var body: some View {
+ content
+ .environment(\.openURL, OpenURLAction(handler: handleURL))
+ }
+
+ @ViewBuilder private var content: some View {
+ if collapsible {
+ TreeNodesView(nodes: document.tree(), onSetStatus: onSetStatus)
+ } else {
+ FlatBlocksView(blocks: document.blocks, onSetStatus: onSetStatus)
+ }
+ }
+
+ private func handleURL(_ url: URL) -> OpenURLAction.Result {
+ if let onOpenLink {
+ let link = NorgLinkURL.decode(url) ?? InlineLink(kind: .link, target: url.absoluteString)
+ onOpenLink(link)
+ return .handled
+ }
+ return NorgLinkURL.decode(url) == nil ? .systemAction : .discarded
+ }
+}