@@ -26,18 +26,13 @@ public struct EditorConfiguration {
26
26
public let namespaceExcludedPaths : [ String ]
27
27
/// Authorization header
28
28
public let authHeader : String
29
- /// Global variables to be made available to the editor
30
- public let webViewGlobals : [ WebViewGlobal ]
31
29
/// Raw block editor settings from the WordPress REST API
32
- public let editorSettings : EditorSettings
30
+ public let editorSettings : String
33
31
/// Locale used for translations
34
32
public let locale : String
35
33
/// Endpoint for loading editor assets, used when enabling `shouldUsePlugins`
36
34
public var editorAssetsEndpoint : URL ?
37
35
38
- // Cookies
39
- public let cookies : [ HTTPCookie ]
40
-
41
36
/// Deliberately non-public – consumers should use `EditorConfigurationBuilder` to construct a configuration
42
37
init (
43
38
title: String ,
@@ -52,11 +47,9 @@ public struct EditorConfiguration {
52
47
siteApiNamespace: [ String ] ,
53
48
namespaceExcludedPaths: [ String ] ,
54
49
authHeader: String ,
55
- webViewGlobals: [ WebViewGlobal ] ,
56
- editorSettings: EditorSettings ,
50
+ editorSettings: String ,
57
51
locale: String ,
58
52
editorAssetsEndpoint: URL ? = nil ,
59
- cookies: [ HTTPCookie ] = [ ]
60
53
) {
61
54
self . title = title
62
55
self . content = content
@@ -70,11 +63,9 @@ public struct EditorConfiguration {
70
63
self . siteApiNamespace = siteApiNamespace
71
64
self . namespaceExcludedPaths = namespaceExcludedPaths
72
65
self . authHeader = authHeader
73
- self . webViewGlobals = webViewGlobals
74
66
self . editorSettings = editorSettings
75
67
self . locale = locale
76
68
self . editorAssetsEndpoint = editorAssetsEndpoint
77
- self . cookies = cookies
78
69
}
79
70
80
71
public func toBuilder( ) -> EditorConfigurationBuilder {
@@ -91,7 +82,6 @@ public struct EditorConfiguration {
91
82
siteApiNamespace: siteApiNamespace,
92
83
namespaceExcludedPaths: namespaceExcludedPaths,
93
84
authHeader: authHeader,
94
- webViewGlobals: webViewGlobals,
95
85
editorSettings: editorSettings,
96
86
locale: locale,
97
87
editorAssetsEndpoint: editorAssetsEndpoint
@@ -106,12 +96,6 @@ public struct EditorConfiguration {
106
96
content. addingPercentEncoding ( withAllowedCharacters: . alphanumerics) !
107
97
}
108
98
109
- var editorSettingsJSON : String {
110
- // `editorSettings` values are always `encodable` so this should never fail
111
- let jsonData = try ! JSONSerialization . data ( withJSONObject: editorSettings, options: [ ] )
112
- return String ( data: jsonData, encoding: . utf8) ?? " undefined "
113
- }
114
-
115
99
public static let `default` = EditorConfigurationBuilder ( ) . build ( )
116
100
}
117
101
@@ -128,8 +112,7 @@ public struct EditorConfigurationBuilder {
128
112
private var siteApiNamespace : [ String ]
129
113
private var namespaceExcludedPaths : [ String ]
130
114
private var authHeader : String
131
- private var webViewGlobals : [ WebViewGlobal ]
132
- private var editorSettings : EditorSettings
115
+ private var editorSettings : String
133
116
private var locale : String
134
117
private var editorAssetsEndpoint : URL ?
135
118
@@ -146,8 +129,7 @@ public struct EditorConfigurationBuilder {
146
129
siteApiNamespace: [ String ] = [ ] ,
147
130
namespaceExcludedPaths: [ String ] = [ ] ,
148
131
authHeader: String = " " ,
149
- webViewGlobals: [ WebViewGlobal ] = [ ] ,
150
- editorSettings: EditorSettings = [ : ] ,
132
+ editorSettings: String = " undefined " ,
151
133
locale: String = " en " ,
152
134
editorAssetsEndpoint: URL ? = nil
153
135
) {
@@ -163,7 +145,6 @@ public struct EditorConfigurationBuilder {
163
145
self . siteApiNamespace = siteApiNamespace
164
146
self . namespaceExcludedPaths = namespaceExcludedPaths
165
147
self . authHeader = authHeader
166
- self . webViewGlobals = webViewGlobals
167
148
self . editorSettings = editorSettings
168
149
self . locale = locale
169
150
self . editorAssetsEndpoint = editorAssetsEndpoint
@@ -241,13 +222,7 @@ public struct EditorConfigurationBuilder {
241
222
return copy
242
223
}
243
224
244
- public func setWebViewGlobals( _ webViewGlobals: [ WebViewGlobal ] ) -> EditorConfigurationBuilder {
245
- var copy = self
246
- copy. webViewGlobals = webViewGlobals
247
- return copy
248
- }
249
-
250
- public func setEditorSettings( _ editorSettings: EditorSettings ) -> EditorConfigurationBuilder {
225
+ public func setEditorSettings( _ editorSettings: String ) -> EditorConfigurationBuilder {
251
226
var copy = self
252
227
copy. editorSettings = editorSettings
253
228
return copy
@@ -265,6 +240,28 @@ public struct EditorConfigurationBuilder {
265
240
return copy
266
241
}
267
242
243
+ /// Simplify conditionally applying a configuration change
244
+ ///
245
+ /// Sample Code:
246
+ /// ```swift
247
+ /// // Before
248
+ /// let configurationBuilder = EditorConfigurationBuilder()
249
+ /// if let postID = post.id {
250
+ /// configurationBuilder = configurationBuilder.setPostID(postID)
251
+ /// }
252
+ ///
253
+ /// // After
254
+ /// let configurationBuilder = EditorConfigurationBuilder()
255
+ /// .apply(post.id, { $0.setPostID($1) } )
256
+ /// ```
257
+ public func apply< T> ( _ value: T ? , _ closure: ( EditorConfigurationBuilder , T ) -> EditorConfigurationBuilder ) -> Self {
258
+ guard let value else {
259
+ return self
260
+ }
261
+
262
+ return closure ( self , value)
263
+ }
264
+
268
265
public func build( ) -> EditorConfiguration {
269
266
EditorConfiguration (
270
267
title: title,
@@ -279,69 +276,13 @@ public struct EditorConfigurationBuilder {
279
276
siteApiNamespace: siteApiNamespace,
280
277
namespaceExcludedPaths: namespaceExcludedPaths,
281
278
authHeader: authHeader,
282
- webViewGlobals: webViewGlobals,
283
279
editorSettings: editorSettings,
284
280
locale: locale,
285
281
editorAssetsEndpoint: editorAssetsEndpoint
286
282
)
287
283
}
288
284
}
289
285
290
- public struct WebViewGlobal : Equatable {
291
- let name : String
292
- let value : WebViewGlobalValue
293
-
294
- public init ( name: String , value: WebViewGlobalValue ) throws {
295
- // Validate name is a valid JavaScript identifier
296
- guard Self . isValidJavaScriptIdentifier ( name) else {
297
- throw WebViewGlobalError . invalidIdentifier ( name)
298
- }
299
- self . name = name
300
- self . value = value
301
- }
302
-
303
- private static func isValidJavaScriptIdentifier( _ name: String ) -> Bool {
304
- // Add validation logic for JavaScript identifiers
305
- return name. range ( of: " ^[a-zA-Z_$][a-zA-Z0-9_$]*$ " , options: . regularExpression) != nil
306
- }
307
- }
308
-
309
- public enum WebViewGlobalError : Error {
310
- case invalidIdentifier( String )
311
- }
312
-
313
- public enum WebViewGlobalValue : Equatable {
314
- case string( String )
315
- case number( Double )
316
- case boolean( Bool )
317
- case object( [ String : WebViewGlobalValue ] )
318
- case array( [ WebViewGlobalValue ] )
319
- case null
320
-
321
- func toJavaScript( ) -> String {
322
- switch self {
323
- case . string( let str) :
324
- return " \" \( str. escaped) \" "
325
- case . number( let num) :
326
- return " \( num) "
327
- case . boolean( let bool) :
328
- return " \( bool) "
329
- case . object( let dict) :
330
- let sortedKeys = dict. keys. sorted ( )
331
- var pairs : [ String ] = [ ]
332
- for key in sortedKeys {
333
- let value = dict [ key] !
334
- pairs. append ( " \" \( key. escaped) \" : \( value. toJavaScript ( ) ) " )
335
- }
336
- return " { \( pairs. joined ( separator: " , " ) ) } "
337
- case . array( let array) :
338
- return " [ \( array. map { $0. toJavaScript ( ) } . joined ( separator: " , " ) ) ] "
339
- case . null:
340
- return " null "
341
- }
342
- }
343
- }
344
-
345
286
public typealias EditorSettings = [ String : Encodable ]
346
287
347
288
// String escaping extension
0 commit comments