diff options
Diffstat (limited to 'Map/Presentation/Commands/MapCommands.swift')
| -rw-r--r-- | Map/Presentation/Commands/MapCommands.swift | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Map/Presentation/Commands/MapCommands.swift b/Map/Presentation/Commands/MapCommands.swift index 35250e2..e7ee8f8 100644 --- a/Map/Presentation/Commands/MapCommands.swift +++ b/Map/Presentation/Commands/MapCommands.swift @@ -18,15 +18,42 @@ struct MapCommands: Commands { @AppStorage("viewStyle") var viewStyle: ViewStyle = .horizontal @AppStorage("zoom") var zoom = 1.0 + @AppStorage("mapTemplates") private var templatesData: Data = Data() @FocusedBinding(\.document) var document: MapDocument? @FocusedValue(\.fileURL) var url: URL? @FocusedBinding(\.selectedEvolution) var selectedEvolution: StageType? @FocusedBinding(\.isSearching) var isSearching + private var templates: [Template] { + if let decoded = try? JSONDecoder().decode([Template].self, from: templatesData) { + return decoded + } + return [] + } + + private var defaultTemplate: Template? { + templates.first { $0.isDefault } + } + var body: some Commands { // File + CommandGroup(after: CommandGroupPlacement.newItem) { + Menu(String(localized: "commands.file.new_from_template")) { + if templates.isEmpty { + Text("No templates available") + .disabled(true) + } else { + ForEach(templates) { template in + Button(template.name) { + createNewDocument(from: template) + } + } + } + } + } + CommandGroup(after: CommandGroupPlacement.saveItem) { Divider() Button("Export...") { @@ -107,4 +134,20 @@ struct MapCommands: Commands { Button("Map Help") { NSWorkspace.shared.open(URL(string: "https://map.tranquil.systems")!) } } } + + @MainActor + private func createNewDocument(from template: Template) { + // Create a temporary file with the template content + let temporaryDirectory = FileManager.default.temporaryDirectory + let temporaryFile = temporaryDirectory.appendingPathComponent("Template-\(template.name).wmap") + + do { + try template.content.write(to: temporaryFile, atomically: true, encoding: .utf8) + NSWorkspace.shared.open(temporaryFile) + } catch { + // Fallback: just trigger the new document command + NSApplication.shared.sendAction( + #selector(NSDocumentController.newDocument(_:)), to: nil, from: nil) + } + } } |