-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathRemoteBlockEditorSettings.swift
85 lines (75 loc) · 3.52 KB
/
RemoteBlockEditorSettings.swift
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
import Foundation
public class RemoteBlockEditorSettings: Codable {
enum CodingKeys: String, CodingKey {
case isFSETheme = "__unstableIsBlockBasedTheme"
case galleryWithImageBlocks = "__unstableGalleryWithImageBlocks"
case quoteBlockV2 = "__experimentalEnableQuoteBlockV2"
case listBlockV2 = "__experimentalEnableListBlockV2"
case rawStyles = "__experimentalStyles"
case rawFeatures = "__experimentalFeatures"
case gutenbergVersion = "gutenbergVersion"
case colors
case gradients
}
public let isFSETheme: Bool
public let galleryWithImageBlocks: Bool
public let quoteBlockV2: Bool
public let listBlockV2: Bool
public let rawStyles: String?
public let rawFeatures: String?
public let gutenbergVersion: String?
public let colors: [[String: String]]?
public let gradients: [[String: String]]?
public lazy var checksum: String = {
return ChecksumUtil.checksum(from: self)
}()
private static func parseToString(_ container: KeyedDecodingContainer<CodingKeys>, _ key: CodingKeys) -> String? {
// Swift cuurently doesn't support type conversions from Dictionaries to strings while decoding. So we need to
// parse the reponse then convert it to a string.
guard
let json = try? container.decode([String: Any].self, forKey: key),
let data = try? JSONSerialization.data(withJSONObject: json, options: [.sortedKeys])
else {
return nil
}
return String(data: data, encoding: .utf8)
}
required public init(from decoder: Decoder) throws {
let map = try decoder.container(keyedBy: CodingKeys.self)
self.isFSETheme = (try? map.decode(Bool.self, forKey: .isFSETheme)) ?? false
self.galleryWithImageBlocks = (try? map.decode(Bool.self, forKey: .galleryWithImageBlocks)) ?? false
self.quoteBlockV2 = (try? map.decode(Bool.self, forKey: .quoteBlockV2)) ?? false
self.listBlockV2 = (try? map.decode(Bool.self, forKey: .listBlockV2)) ?? false
self.rawStyles = RemoteBlockEditorSettings.parseToString(map, .rawStyles)
self.rawFeatures = RemoteBlockEditorSettings.parseToString(map, .rawFeatures)
self.gutenbergVersion = (try? map.decode(String.self, forKey: .gutenbergVersion)) ?? ""
self.colors = try? map.decode([[String: String]].self, forKey: .colors)
self.gradients = try? map.decode([[String: String]].self, forKey: .gradients)
}
}
// MARK: EditorTheme
public class RemoteEditorTheme: Codable {
enum CodingKeys: String, CodingKey {
case themeSupport = "theme_supports"
}
public let themeSupport: RemoteEditorThemeSupport?
public lazy var checksum: String = {
return ChecksumUtil.checksum(from: themeSupport)
}()
}
public struct RemoteEditorThemeSupport: Codable {
enum CodingKeys: String, CodingKey {
case colors = "editor-color-palette"
case gradients = "editor-gradient-presets"
case blockTemplates = "block-templates"
}
public let colors: [[String: String]]?
public let gradients: [[String: String]]?
public let blockTemplates: Bool
public init(from decoder: Decoder) throws {
let map = try decoder.container(keyedBy: CodingKeys.self)
self.colors = try? map.decode([[String: String]].self, forKey: .colors)
self.gradients = try? map.decode([[String: String]].self, forKey: .gradients)
self.blockTemplates = (try? map.decode(Bool.self, forKey: .blockTemplates)) ?? false
}
}