Sources/Orgstar/BacklinksPane.swift

dd764a1db9c6495c09fa4fc20856a781d84441e4
orgstar/Sources/Orgstar/BacklinksPane.swift

84 lines · 3546 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    @Environment(\.orgTheme) private var theme
16    @Environment(\.colorScheme) private var colorScheme
17
18    /// The heading at the caret.
19    private var heading: OutlineEntry? { session.outline.last { $0.start <= commands.selection.location } }
20
21    private struct Key: Equatable {
22        var path: String?
23        var heading: Int?
24        var title: String?
25        var index: Int
26    }
27
28    var body: some View {
29        List {
30            if let heading {
31                Section("Links to \(heading.title.isEmpty ? "(untitled)" : heading.title)") { rows(toHeading) }
32            }
33            Section("Links to this file") { rows(toFile) }
34        }
35        .themedListBackground(theme.color("sidebar-background", in: colorScheme))
36        .task(id: Key(path: session.path, heading: heading?.start, title: heading?.title, index: workspace.indexVersion)) { await refresh() }
37    }
38
39    @ViewBuilder
40    private func rows(_ locations: [HeadingLocation]) -> some View {
41        if locations.isEmpty {
42            Text("None").foregroundStyle(.secondary)
43        }
44        ForEach(locations, id: \.self) { location in
45            Button {
46                open(location)
47            } label: {
48                VStack(alignment: .leading, spacing: 2) {
49                    Text(location.title.isEmpty ? "(untitled)" : location.title).lineLimit(1)
50                    if let snippet = location.snippet {
51                        Text(snippet.attributed).font(.callout).foregroundStyle(.secondary).lineLimit(3)
52                    }
53                    Text((location.path as NSString).lastPathComponent).font(.caption).foregroundStyle(.tertiary)
54                }
55            }
56            .buttonStyle(.plain)
57        }
58    }
59
60    private func refresh() async {
61        guard let path = session.path else {
62            toHeading = []
63            toFile = []
64            return
65        }
66        let overlay = workspace.overlay(session.openDocuments)
67        let found = await Self.backlinks(in: workspace, path: path, tree: session.document?.tree, heading: heading?.start, overlay: overlay)
68        guard !Task.isCancelled else { return }
69        (toFile, toHeading) = found
70    }
71
72    @concurrent
73    private nonisolated static func backlinks(
74        in workspace: WorkspaceModel, path: String, tree: OrgTree?, heading: Int?, overlay: [String: FileRecord]
75    ) async -> (toFile: [HeadingLocation], toHeading: [HeadingLocation]) {
76        let model = tree.map { DocumentModel(tree: $0) }
77        // The file's own ID, as an org-roam file node.
78        let toFile = workspace.backlinks(to: Backlinks.Target(path: path, id: model?.property("ID", of: nil)?.value), overlay: overlay)
79        guard let heading, let info = model?.headings.first(where: { $0.headingRange.lowerBound == heading }) else { return (toFile, []) }
80        let customID = info.properties.last { $0.key.uppercased() == "CUSTOM_ID" }?.value
81        let target = Backlinks.Target(path: path, title: info.title, id: info.id, customID: customID)
82        return (toFile, workspace.backlinks(to: target, excluding: heading, overlay: overlay))
83    }
84}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form