-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlantUMLDocument.swift
More file actions
109 lines (85 loc) · 2.85 KB
/
Copy pathPlantUMLDocument.swift
File metadata and controls
109 lines (85 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// PlantUMLDocument.swift
// PlantUML
//
// Created by Bartolomeo Sorrentino on 01/08/22.
//
import SwiftUI
import UniformTypeIdentifiers
extension UTType {
static var umldiagram: UTType {
UTType(importedAs: "org.bsc.plantuml-text")
}
}
fileprivate struct Content {
let text: String
let drawing: Data?
private enum CodingKeys: String, CodingKey {
case text
case drawing // = "image" // Optional name change for clarity (optional)
}
}
// Encodable Exension
extension Content : Encodable {
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
// guard let textUTF8 = text.data(using: .utf8) else {
// throw Errors.documentEncodeError("Cannot convert text in utf8")
// }
try container.encode(text, forKey: .text)
if let drawing {
try container.encode(drawing.base64EncodedString(), forKey: .drawing)
}
// try container.encodeIfPresent(drawing, forKey: .drawing)
}
}
// Decodable Extension
extension Content : Decodable {
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
text = try container.decode(String.self, forKey: .text)
if let base64String = try container.decodeIfPresent(String.self, forKey: .drawing) {
if let data = Data(base64Encoded: base64String) {
drawing = data
} else {
throw Errors.documentDecodeError("Invalid base64 string")
}
} else {
drawing = nil
}
}
}
struct PlantUMLDocument: FileDocument {
static var readableContentTypes: [UTType] { [.umldiagram] }
var text: String
var drawing: Data?
var isNew:Bool {
text.isEmpty
}
init( text:String = "") {
self.text = text
}
init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents else {
throw Errors.documentDecodeError("error getting regularFileContents ")
}
text = ""
do {
let decoder = JSONDecoder()
let content = try decoder.decode(Content.self, from: data)
text = content.text
drawing = content.drawing
}
catch { // Backward compatibility
if let string = String(data: data, encoding: .utf8) { // Backward compatibility
text = string
}
}
}
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
let encoder = JSONEncoder()
let content = Content(text: text, drawing: drawing)
let data = try encoder.encode(content)
return .init(regularFileWithContents: data)
}
}