]> git.r.bdr.sh - rbdr/map/blob - Map/Data/MapDocument.swift
9340684e428a8d2e5d6d77b9bca5d04e0bd87017
[rbdr/map] / Map / Data / MapDocument.swift
1 import SwiftUI
2 import UniformTypeIdentifiers
3
4 extension UTType {
5 static var exampleText: UTType {
6 UTType(importedAs: "systems.tranquil.map.wmap")
7 }
8 }
9
10 struct MapDocument: FileDocument {
11 var text: String
12
13 init(text: String = "Hello, world!") {
14 self.text = text
15 }
16
17 static var readableContentTypes: [UTType] { [.exampleText] }
18
19 init(configuration: ReadConfiguration) throws {
20 guard let data = configuration.file.regularFileContents,
21 let string = String(data: data, encoding: .utf8)
22 else {
23 throw CocoaError(.fileReadCorruptFile)
24 }
25 text = string
26 }
27
28 func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
29 let data = text.data(using: .utf8)!
30 return .init(regularFileWithContents: data)
31 }
32
33 @MainActor
34 func exportAsImage(withEvolution selectedEvolution: StageType) -> NSImage? {
35 let renderView = MapRenderView(
36 document: .constant(self),
37 evolution: .constant(selectedEvolution))
38 let renderer = ImageRenderer(content: renderView)
39
40 return renderer.nsImage
41 }
42 }