blob: 6f3deacbf4c1106baefb77dc3a84adfacde243f7 (
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
|
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
}
}
|