Sources/Orgstar/EditorView.swift

decaf5db8c55edebdb66cae18bbb466a4989ecc8
orgstar/Sources/Orgstar/EditorView.swift

49 lines · 1785 bytes

 1import AppKit
 2import OrgApp
 3import OrgEditorAppKit
 4import SwiftUI
 5
 6/// The read-only TextKit 2 editor for the session's file. A new file gets a new editor; reveal
 7/// requests and changes on disk go to the existing one, so folds survive a reload.
 8struct EditorView: NSViewRepresentable {
 9    let session: DocumentSession
10
11    final class Coordinator {
12        var editor: OrgEditor?
13        var path: String?
14        var revealID = 0
15        var diskID = 0
16    }
17
18    func makeCoordinator() -> Coordinator { Coordinator() }
19
20    func makeNSView(context: Context) -> NSView {
21        NSView()
22    }
23
24    func updateNSView(_ container: NSView, context: Context) {
25        let coordinator = context.coordinator
26        if coordinator.path != session.path {
27            coordinator.path = session.path
28            container.subviews.forEach { $0.removeFromSuperview() }
29            coordinator.editor = nil
30            if let document = session.document {
31                let editor = OrgEditor(document: document, frame: container.bounds, editable: false)
32                let scroll = editor.makeScrollView()
33                scroll.frame = container.bounds
34                scroll.autoresizingMask = [.width, .height]
35                container.addSubview(scroll)
36                container.window?.makeFirstResponder(editor.textView)
37                coordinator.editor = editor
38            }
39        }
40        if let request = session.revealRequest, request.id != coordinator.revealID {
41            coordinator.revealID = request.id
42            coordinator.editor?.reveal(request.offset)
43        }
44        if let disk = session.diskBytes, disk.id != coordinator.diskID {
45            coordinator.diskID = disk.id
46            coordinator.editor?.diskChanged(to: disk.bytes)
47        }
48    }
49}
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