Sources/Orgstar/BacklinksPane.swift
cmc One capitalisation rule: title case for titles and buttons, sentence case for settings
7c1313ad9f 18 hours ago114 lines · 4862 bytes
1import OrgApp
2import OrgCore
3import OrgIndex
4import OrgPresentation
5import SwiftUI
6
7/// Headings that link to the heading at the caret, and to the file.
8struct BacklinksPane: View {
9 let workspace: WorkspaceModel
10 let session: DocumentSession
11 let commands: AppCommands
12 let open: (HeadingLocation) -> Void
13 @State private var toHeading: [HeadingLocation] = []
14 @State private var toFile: [HeadingLocation] = []
15 /// Why the index couldn't be read, shown in place of the lists.
16 @State private var failure: String?
17 @Environment(\.orgTheme) private var theme
18 @Environment(\.colorScheme) private var colorScheme
19
20 /// The heading at the caret.
21 private var heading: OutlineEntry? { session.outline.last { $0.start <= commands.selection.location } }
22
23 private struct Key: Equatable {
24 var path: String?
25 var heading: Int?
26 var title: String?
27 var index: Int
28 }
29
30 var body: some View {
31 Group {
32 if failure == nil, toHeading.isEmpty, toFile.isEmpty {
33 // Nothing links here: a single line, leaving the column to the outline.
34 HStack {
35 Text("Links to This File").font(.subheadline.weight(.semibold)).foregroundStyle(.secondary)
36 Spacer()
37 Text("None").foregroundStyle(.secondary)
38 }
39 .padding(.horizontal, 12)
40 .padding(.vertical, 6)
41 .frame(maxWidth: .infinity)
42 .background(theme.color("sidebar-background", in: colorScheme) ?? .clear)
43 } else {
44 List {
45 if let failure {
46 Text(failure).foregroundStyle(.secondary)
47 } else {
48 if let heading {
49 Section("Links to \(heading.title.isEmpty ? "(untitled)" : heading.title)") { rows(toHeading) }
50 }
51 Section("Links to This File") { rows(toFile) }
52 }
53 }
54 .themedListBackground(theme.color("sidebar-background", in: colorScheme))
55 .frame(height: 200)
56 }
57 }
58 .task(id: Key(path: session.path, heading: heading?.start, title: heading?.title, index: workspace.indexVersion)) { await refresh() }
59 }
60
61 @ViewBuilder
62 private func rows(_ locations: [HeadingLocation]) -> some View {
63 if locations.isEmpty {
64 Text("None").foregroundStyle(.secondary)
65 }
66 ForEach(locations, id: \.self) { location in
67 Button {
68 open(location)
69 } label: {
70 VStack(alignment: .leading, spacing: 2) {
71 Text(location.title.isEmpty ? "(untitled)" : location.title).lineLimit(1)
72 if let snippet = location.snippet {
73 Text(snippet.attributed).font(.callout).foregroundStyle(.secondary).lineLimit(3)
74 }
75 Text((location.path as NSString).lastPathComponent).font(.caption).foregroundStyle(.tertiary)
76 }
77 }
78 .buttonStyle(.plain)
79 }
80 }
81
82 private func refresh() async {
83 guard let path = session.path else {
84 toHeading = []
85 toFile = []
86 failure = nil
87 return
88 }
89 let overlay = workspace.overlay(session.openDocuments)
90 do {
91 let found = try await Self.backlinks(in: workspace, path: path, tree: session.document?.tree, heading: heading?.start, overlay: overlay)
92 guard !Task.isCancelled else { return }
93 (toFile, toHeading) = found
94 failure = nil
95 } catch {
96 guard !Task.isCancelled else { return }
97 (toFile, toHeading) = ([], [])
98 failure = error.localizedDescription
99 }
100 }
101
102 @concurrent
103 private nonisolated static func backlinks(
104 in workspace: WorkspaceModel, path: String, tree: OrgTree?, heading: Int?, overlay: [String: FileRecord]
105 ) async throws -> (toFile: [HeadingLocation], toHeading: [HeadingLocation]) {
106 let model = tree.map { DocumentModel(tree: $0) }
107 // The file's own ID, as an org-roam file node.
108 let toFile = try workspace.backlinks(to: Backlinks.Target(path: path, id: model?.property("ID", of: nil)?.value), overlay: overlay)
109 guard let heading, let info = model?.headings.first(where: { $0.headingRange.lowerBound == heading }) else { return (toFile, []) }
110 let customID = info.properties.last { $0.key.uppercased() == "CUSTOM_ID" }?.value
111 let target = Backlinks.Target(path: path, title: info.title, id: info.id, customID: customID)
112 return (toFile, try workspace.backlinks(to: target, excluding: heading, overlay: overlay))
113 }
114}