Sources/Orgstar/TemplateSheet.swift

52d620b11ecb28c3cd78962df6e07dbbe747bda1
orgstar/Sources/Orgstar/TemplateSheet.swift

72 lines · 2979 bytes

 1import OrgApp
 2import OrgCore
 3import SwiftUI
 4
 5/// New File from Template…: a template from `snippets.toml`, a folder and a name. The file is
 6/// created as New File creates one and opens with the template expanded in it.
 7struct TemplateSheet: View {
 8    let workspace: WorkspaceModel
 9    /// The folder of the file shown, which the folder menu starts on.
10    let folder: String?
11    let open: (String) -> Void
12    @Environment(\.dismiss) private var dismiss
13    @State private var template = ""
14    @State private var chosenFolder = ""
15    @State private var name = ""
16    @State private var failure: String?
17
18    var body: some View {
19        let templates = SnippetLibrary.shared.templates
20        let folders = SnippetLibrary.folders(in: workspace)
21        VStack(alignment: .leading, spacing: 12) {
22            Text("New File from Template").font(.headline)
23            if templates.isEmpty {
24                Text("No templates yet. A snippet in snippets.toml with file = true is one.")
25                    .foregroundStyle(.secondary)
26            } else if folders.isEmpty {
27                Text("Add a folder first.").foregroundStyle(.secondary)
28            } else {
29                Form {
30                    Picker("Template", selection: $template) {
31                        ForEach(templates, id: \.trigger) { Text($0.name).tag($0.trigger) }
32                    }
33                    Picker("Folder", selection: $chosenFolder) {
34                        ForEach(folders, id: \.path) { Text($0.title).tag($0.path) }
35                    }
36                    TextField("Name", text: $name)
37                }
38                Text("A name without an extension gets .org. Use folder/name to make folders.")
39                    .font(.caption).foregroundStyle(.secondary)
40            }
41            if let failure {
42                Text(failure).font(.caption).foregroundStyle(.red)
43            }
44            HStack {
45                Spacer()
46                Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
47                Button("Create") { create() }
48                    .keyboardShortcut(.defaultAction)
49                    .disabled(templates.isEmpty || folders.isEmpty || name.trimmingCharacters(in: .whitespaces).isEmpty)
50            }
51        }
52        .padding(20)
53        .frame(width: 440)
54        .onAppear {
55            template = templates.first?.trigger ?? ""
56            chosenFolder = folders.first { $0.path == folder }?.path ?? folders.first?.path ?? ""
57        }
58    }
59
60    private func create() {
61        guard let chosen = SnippetLibrary.shared.templates.first(where: { $0.trigger == template }) else { return }
62        Task {
63            do {
64                let path = try await SnippetLibrary.shared.createFile(named: name, in: chosenFolder, from: chosen, workspace: workspace)
65                dismiss()
66                open(path)
67            } catch {
68                failure = error.localizedDescription
69            }
70        }
71    }
72}
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