Sources/OrgstarMobile/TemplateScreen.swift

fc3ddfcd5858fab2b88399e3df1ce76f3213a7c9
orgstar/Sources/OrgstarMobile/TemplateScreen.swift

96 lines · 3696 bytes

 1#if os(iOS)
 2import OrgApp
 3import OrgCore
 4import SwiftUI
 5
 6/// A folder New File from Template… creates its file in.
 7struct TemplateFolder: Identifiable {
 8    let path: String
 9    var id: String { path }
10}
11
12/// New File from Template…: a template from `snippets.toml` and a name. The file is created in
13/// the folder as New File creates one, and opens in the editor with the template expanded.
14struct TemplateScreen: View {
15    let folder: String
16    let workspace: WorkspaceModel
17    let created: (String) -> Void
18    @State private var template = ""
19    @State private var name = ""
20    @State private var failure: String?
21    @Environment(\.dismiss) private var dismiss
22
23    var body: some View {
24        let templates = SnippetLibrary.shared.templates
25        NavigationStack {
26            Form {
27                if templates.isEmpty {
28                    Text("No templates yet. A snippet in snippets.toml with file = true is one.")
29                        .foregroundStyle(.secondary)
30                } else {
31                    Picker("Template", selection: $template) {
32                        ForEach(templates, id: \.trigger) { Text($0.name).tag($0.trigger) }
33                    }
34                    Section {
35                        TextField("Name", text: $name)
36                            .autocorrectionDisabled()
37                            .textInputAutocapitalization(.never)
38                    } footer: {
39                        Text("A name without an extension gets .org.")
40                    }
41                }
42                if let failure {
43                    Text(failure).foregroundStyle(.red)
44                }
45            }
46            .navigationTitle((folder as NSString).lastPathComponent)
47            .navigationBarTitleDisplayMode(.inline)
48            .toolbar {
49                ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() } }
50                ToolbarItem(placement: .confirmationAction) {
51                    Button("Create") { create() }
52                        .disabled(templates.isEmpty || name.trimmingCharacters(in: .whitespaces).isEmpty)
53                }
54            }
55            .onAppear { if template.isEmpty { template = templates.first?.trigger ?? "" } }
56        }
57    }
58
59    private func create() {
60        guard let chosen = SnippetLibrary.shared.templates.first(where: { $0.trigger == template }) else { return }
61        Task {
62            do {
63                let path = try await SnippetLibrary.shared.createFile(named: name, in: folder, from: chosen, workspace: workspace)
64                dismiss()
65                created(path)
66            } catch {
67                failure = error.localizedDescription
68            }
69        }
70    }
71}
72
73/// The template sheet for `folder`, and the editor the new file opens in.
74private struct NewFromTemplate: ViewModifier {
75    @Binding var folder: TemplateFolder?
76    let workspace: WorkspaceModel
77    let session: DocumentSession
78    @State private var editing: EditorRoute?
79
80    func body(content: Content) -> some View {
81        content
82            .sheet(item: $folder) { folder in
83                TemplateScreen(folder: folder.path, workspace: workspace) { editing = EditorRoute(path: $0, offset: nil) }
84            }
85            .navigationDestination(item: $editing) { route in
86                EditorSplitScreen(path: route.path, offset: nil, workspace: workspace, session: session)
87            }
88    }
89}
90
91extension View {
92    func newFromTemplate(_ folder: Binding<TemplateFolder?>, workspace: WorkspaceModel, session: DocumentSession) -> some View {
93        modifier(NewFromTemplate(folder: folder, workspace: workspace, session: session))
94    }
95}
96#endif
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