Skip to content

Commit c1343c9

Browse files
committed
Add editorSettingsEndpoint
1 parent 6626ae7 commit c1343c9

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

ios/Sources/GutenbergKit/Sources/EditorConfiguration.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public struct EditorConfiguration: Sendable {
3232
public let locale: String
3333
/// Enables the native inserter UI in the editor
3434
public let isNativeInserterEnabled: Bool
35+
/// Endpoint for loading editor settings
36+
public let editorSettingsEndpoint: URL?
3537
/// Endpoint for loading editor assets, used when enabling `shouldUsePlugins`
3638
public let editorAssetsEndpoint: URL?
3739
/// Logs emitted at or above this level will be printed to the debug console
@@ -56,6 +58,7 @@ public struct EditorConfiguration: Sendable {
5658
editorSettings: String,
5759
locale: String,
5860
isNativeInserterEnabled: Bool,
61+
editorSettingsEndpoint: URL?,
5962
editorAssetsEndpoint: URL?,
6063
logLevel: EditorLogLevel,
6164
enableNetworkLogging: Bool = false
@@ -75,6 +78,7 @@ public struct EditorConfiguration: Sendable {
7578
self.editorSettings = editorSettings
7679
self.locale = locale
7780
self.isNativeInserterEnabled = isNativeInserterEnabled
81+
self.editorSettingsEndpoint = editorSettingsEndpoint
7882
self.editorAssetsEndpoint = editorAssetsEndpoint
7983
self.logLevel = logLevel
8084
self.enableNetworkLogging = enableNetworkLogging
@@ -97,6 +101,7 @@ public struct EditorConfiguration: Sendable {
97101
editorSettings: editorSettings,
98102
locale: locale,
99103
isNativeInserterEnabled: isNativeInserterEnabled,
104+
editorSettingsEndpoint: editorSettingsEndpoint,
100105
editorAssetsEndpoint: editorAssetsEndpoint,
101106
logLevel: logLevel,
102107
enableNetworkLogging: enableNetworkLogging
@@ -130,6 +135,7 @@ public struct EditorConfigurationBuilder {
130135
private var editorSettings: String
131136
private var locale: String
132137
private var isNativeInserterEnabled: Bool
138+
private var editorSettingsEndpoint: URL?
133139
private var editorAssetsEndpoint: URL?
134140
private var logLevel: EditorLogLevel
135141
private var enableNetworkLogging: Bool
@@ -150,6 +156,7 @@ public struct EditorConfigurationBuilder {
150156
editorSettings: String = "undefined",
151157
locale: String = "en",
152158
isNativeInserterEnabled: Bool = false,
159+
editorSettingsEndpoint: URL? = nil,
153160
editorAssetsEndpoint: URL? = nil,
154161
logLevel: EditorLogLevel = .error,
155162
enableNetworkLogging: Bool = false
@@ -169,6 +176,7 @@ public struct EditorConfigurationBuilder {
169176
self.editorSettings = editorSettings
170177
self.locale = locale
171178
self.isNativeInserterEnabled = isNativeInserterEnabled
179+
self.editorSettingsEndpoint = editorSettingsEndpoint
172180
self.editorAssetsEndpoint = editorAssetsEndpoint
173181
self.logLevel = logLevel
174182
self.enableNetworkLogging = enableNetworkLogging
@@ -264,6 +272,12 @@ public struct EditorConfigurationBuilder {
264272
return copy
265273
}
266274

275+
public func setEditorSettingsEndpoint(_ editorSettingsEndpoint: URL?) -> EditorConfigurationBuilder {
276+
var copy = self
277+
copy.editorSettingsEndpoint = editorSettingsEndpoint
278+
return copy
279+
}
280+
267281
public func setEditorAssetsEndpoint(_ editorAssetsEndpoint: URL?) -> EditorConfigurationBuilder {
268282
var copy = self
269283
copy.editorAssetsEndpoint = editorAssetsEndpoint
@@ -321,6 +335,7 @@ public struct EditorConfigurationBuilder {
321335
editorSettings: editorSettings,
322336
locale: locale,
323337
isNativeInserterEnabled: isNativeInserterEnabled,
338+
editorSettingsEndpoint: editorSettingsEndpoint,
324339
editorAssetsEndpoint: editorAssetsEndpoint,
325340
logLevel: logLevel,
326341
enableNetworkLogging: enableNetworkLogging

ios/Sources/GutenbergKit/Sources/Service/EditorService.swift

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public actor EditorService {
153153

154154
// Fetch settings and manifest in parallel
155155
async let settingsFuture = Result {
156-
try await fetchEditorSettings(baseURL: baseURL, authHeader: configuration.authHeader)
156+
try await fetchEditorSettings(configuration: configuration)
157157
}
158158
async let manifestFuture = Result {
159159
try await fetchManifestData(configuration: configuration)
@@ -203,8 +203,9 @@ public actor EditorService {
203203
///
204204
/// - Returns: Raw settings data from the API
205205
@discardableResult
206-
private func fetchEditorSettings(baseURL: URL, authHeader: String) async throws -> Data {
207-
let data = try await fetchData(for: baseURL.appendingPathComponent("/wp-block-editor/v1/settings"), authHeader: authHeader)
206+
private func fetchEditorSettings(configuration: EditorConfiguration) async throws -> Data {
207+
let endpoint = try configuration.editorSettingsEndpointURL()
208+
let data = try await fetchData(for: endpoint, authHeader: configuration.authHeader)
208209
do {
209210
FileManager.default.createDirectoryIfNeeded(at: storeURL)
210211
try data.write(to: editorSettingsFileURL)
@@ -492,18 +493,33 @@ private extension FileManager {
492493
}
493494

494495
private extension EditorConfiguration {
495-
/// Returns the endpoint URL for fetching the editor assets manifest
496-
func editorAssetsManifestEndpoint() throws -> URL {
497-
if let customEndpoint = editorAssetsEndpoint {
496+
/// Returns the endpoint URL for fetching the editor settings
497+
func editorSettingsEndpointURL() throws -> URL {
498+
if let customEndpoint = editorSettingsEndpoint {
498499
return customEndpoint
499500
}
500501
// Fall back to constructing endpoint from siteApiRoot
501502
guard let baseURL = URL(string: siteApiRoot) else {
502503
throw URLError(.badURL)
503504
}
505+
return baseURL.appendingPathComponent("/wp-block-editor/v1/settings")
506+
}
507+
508+
/// Returns the endpoint URL for fetching the editor assets manifest
509+
func editorAssetsManifestEndpoint() throws -> URL {
510+
let endpoint: URL
511+
if let customEndpoint = editorAssetsEndpoint {
512+
endpoint = customEndpoint
513+
} else {
514+
// Fall back to constructing endpoint from siteApiRoot
515+
guard let baseURL = URL(string: siteApiRoot) else {
516+
throw URLError(.badURL)
517+
}
518+
endpoint = baseURL.appendingPathComponent("/wpcom/v2/editor-assets")
519+
}
520+
521+
// Always add query parameters
504522
let excludeParam = URLQueryItem(name: "exclude", value: "core,gutenberg")
505-
return baseURL
506-
.appendingPathComponent("/wpcom/v2/editor-assets")
507-
.appending(queryItems: [excludeParam])
523+
return endpoint.appending(queryItems: [excludeParam])
508524
}
509525
}

0 commit comments

Comments
 (0)