aboutsummaryrefslogtreecommitdiff
path: root/Map
diff options
context:
space:
mode:
Diffstat (limited to 'Map')
-rw-r--r--Map/Data/MapDocument.swift36
-rw-r--r--Map/Localizable.xcstrings14
-rw-r--r--Map/MapApp.swift2
-rw-r--r--Map/Presentation/Commands/MapCommands.swift43
-rw-r--r--Map/Presentation/MapEditor.swift2
5 files changed, 83 insertions, 14 deletions
diff --git a/Map/Data/MapDocument.swift b/Map/Data/MapDocument.swift
index 3c6d352..2377d5a 100644
--- a/Map/Data/MapDocument.swift
+++ b/Map/Data/MapDocument.swift
@@ -25,19 +25,31 @@ struct MapDocument: FileDocument {
var text: String
init(
- text: String = """
- Anchor (60, 5) [x]
- Node A (78, 23)
- Node B (40, 30) [square]
- Node C (65, 70)
-
- Anchor -> Node A
- Anchor -> Node B
- Node A -> Node C
- Node B -> Node C
- """
+ text: String? = nil
) {
- self.text = text
+ if let text = text {
+ self.text = text
+ } else {
+ // Try to get default template, fallback to built-in content
+ if let templatesData = UserDefaults.standard.data(forKey: "mapTemplates"),
+ let templates = try? JSONDecoder().decode([Template].self, from: templatesData),
+ let defaultTemplate = templates.first(where: { $0.isDefault })
+ {
+ self.text = defaultTemplate.content
+ } else {
+ self.text = """
+ Anchor (60, 5) [x]
+ Node A (78, 23)
+ Node B (40, 30) [square]
+ Node C (65, 70)
+
+ Anchor -> Node A
+ Anchor -> Node B
+ Node A -> Node C
+ Node B -> Node C
+ """
+ }
+ }
}
static var readableContentTypes: [UTType] { [.wmap] }
diff --git a/Map/Localizable.xcstrings b/Map/Localizable.xcstrings
index 22f8e6b..4e6112d 100644
--- a/Map/Localizable.xcstrings
+++ b/Map/Localizable.xcstrings
@@ -22,6 +22,17 @@
"Check for Updates…" : {
},
+ "commands.file.new_from_template" : {
+ "extractionState" : "manual",
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "New From Template"
+ }
+ }
+ }
+ },
"Done" : {
},
@@ -108,6 +119,9 @@
"Name" : {
},
+ "No templates available" : {
+
+ },
"preferences.stages.explanation" : {
"extractionState" : "manual",
"localizations" : {
diff --git a/Map/MapApp.swift b/Map/MapApp.swift
index 08edcb4..54eb9be 100644
--- a/Map/MapApp.swift
+++ b/Map/MapApp.swift
@@ -22,7 +22,7 @@ struct MapApp: App {
startingUpdater: true, updaterDelegate: nil, userDriverDelegate: nil)
var body: some Scene {
- DocumentGroup(newDocument: MapDocument()) { file in
+ DocumentGroup(newDocument: MapDocument(text: nil)) { file in
MapEditor(document: file.$document, url: file.fileURL)
.focusedSceneValue(\.document, file.$document)
.focusedSceneValue(\.fileURL, file.fileURL)
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)
+ }
+ }
}
diff --git a/Map/Presentation/MapEditor.swift b/Map/Presentation/MapEditor.swift
index 5005a73..fe10427 100644
--- a/Map/Presentation/MapEditor.swift
+++ b/Map/Presentation/MapEditor.swift
@@ -182,5 +182,5 @@ struct MapEditor: View {
}
#Preview {
- MapEditor(document: .constant(MapDocument()), url: URL(filePath: "test.wmap")!)
+ MapEditor(document: .constant(MapDocument(text: nil)), url: URL(filePath: "test.wmap")!)
}