Skip to content

Commit 6ce39be

Browse files
Support caching remote assets (#148)
* Use Xcode 16.3 on CI * Create `WKURLSchemeHandler` instance to cache js & css files * Update the Demo app to show remote editors * Support caching scheme-less resource links * Add a note about how the "Remote Editor" is launched in the demo * Support caching JS source map files * Be extra clear what to change in the demo app's configuration * Fix editor assets endpoint value * Fix npm lint issues * Update testing notes * Do not cache `wpcom/v2/editor-assets` API responses * Only cache assets if server responses with 2xx status code * fix: Reinstate Android remote editor asset fetch Utilize JS fetch until we implement fetching and caching editor assets for Android. * build: Align SwiftSoup version with WP-iOS WP-iOS relies upon an exact version of 2.7.5. This caused a conflict: ``` Failed to resolve dependencies Dependencies could not be resolved because root depends on 'swiftsoup' 2.7.5 and 'gutenbergkit' depends on 'swiftsoup' 2.8.8..<3.0.0. ``` * Add `EditorConfiguration` parameter to the `warmup` function --------- Co-authored-by: David Calhoun <[email protected]>
1 parent ce3658a commit 6ce39be

File tree

15 files changed

+636
-40
lines changed

15 files changed

+636
-40
lines changed

.xcode-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
15.4
1+
16.3-v2

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SIMULATOR_DESTINATION := OS=17.5,name=iPhone 15 Plus
1+
SIMULATOR_DESTINATION := OS=18.4,name=iPhone 16 Plus
22

33
define XCODEBUILD_CMD
44
@set -o pipefail && \

Package.resolved

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ let package = Package(
99
products: [
1010
.library(name: "GutenbergKit", targets: ["GutenbergKit"])
1111
],
12+
dependencies: [
13+
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.7.5"),
14+
],
1215
targets: [
1316
.target(
1417
name: "GutenbergKit",
15-
dependencies: [],
18+
dependencies: ["SwiftSoup"],
1619
path: "ios/Sources/GutenbergKit",
1720
exclude: [],
1821
resources: [.copy("Gutenberg")]
@@ -21,7 +24,10 @@ let package = Package(
2124
name: "GutenbergKitTests",
2225
dependencies: ["GutenbergKit"],
2326
path: "ios/Tests",
24-
exclude: []
25-
)
27+
exclude: [],
28+
resources: [
29+
.copy("GutenbergKitTests/Resources/manifest-test-case-1.json")
30+
]
31+
),
2632
]
2733
)

ios/Demo-iOS/Gutenberg.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ios/Demo-iOS/Sources/ContentView.swift

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,84 @@ import GutenbergKit
44
let editorURL: URL? = ProcessInfo.processInfo.environment["GUTENBERG_EDITOR_URL"].flatMap(URL.init)
55

66
struct ContentView: View {
7+
8+
let remoteEditorConfigurations: [EditorConfiguration] = [.template]
9+
710
var body: some View {
8-
NavigationView {
9-
EditorView(editorURL: editorURL)
11+
List {
12+
Section {
13+
NavigationLink {
14+
EditorView(configuration: .default)
15+
} label: {
16+
Text("Bundled Editor")
17+
}
18+
}
19+
20+
Section {
21+
ForEach(remoteEditorConfigurations, id: \.siteURL) { configuration in
22+
NavigationLink {
23+
EditorView(configuration: configuration)
24+
} label: {
25+
Text(URL(string: configuration.siteURL)?.host ?? configuration.siteURL)
26+
}
27+
}
28+
29+
if remoteEditorConfigurations.isEmpty {
30+
Text("Add `EditorConfiguration` instances to the `remoteEditorConfigurations` array to launch remote editors here.")
31+
}
32+
} header: {
33+
Text("Remote Editors")
34+
} footer: {
35+
if ProcessInfo.processInfo.environment["GUTENBERG_EDITOR_REMOTE_URL"] != nil {
36+
Text("Note: The editor is backed by the dev server created by `make dev-server-remote`.")
37+
} else {
38+
Text("Note: The editor is backed by the compiled web app created by `make build`.")
39+
}
40+
}
1041
}
42+
.toolbar {
43+
ToolbarItem(placement: .primaryAction) {
44+
Button {
45+
Task {
46+
NSLog("Start to fetch assets")
47+
for configuration in remoteEditorConfigurations {
48+
let library = EditorAssetsLibrary(configuration: configuration)
49+
do {
50+
try await library.fetchAssets()
51+
} catch {
52+
NSLog("Failed to fetch assets for \(configuration.siteURL): \(error)")
53+
}
54+
}
55+
NSLog("Done fetching assets")
56+
}
57+
} label: {
58+
Image(systemName: "arrow.clockwise")
59+
}
60+
61+
}
62+
}
63+
}
64+
}
65+
66+
private extension EditorConfiguration {
67+
68+
static var template: Self {
69+
var configuration = EditorConfiguration.default
70+
71+
#warning("1. Update the property values below")
72+
#warning("2. Install the Jetpack plugin to the site")
73+
configuration.siteURL = "https://modify-me.com"
74+
configuration.authHeader = "Insert the Authorization header value here"
75+
76+
// DO NOT CHANGE the properties below
77+
configuration.siteApiRoot = "\(configuration.siteURL)/wp-json/"
78+
configuration.editorAssetsEndpoint = URL(string: configuration.siteApiRoot)!.appendingPathComponent("wpcom/v2/editor-assets")
79+
// The `plugins: true` is necessary for the editor to use 'remote.html'
80+
configuration.plugins = true
81+
82+
return configuration
1183
}
84+
1285
}
1386

1487
#Preview {

ios/Demo-iOS/Sources/EditorView.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import SwiftUI
22
import GutenbergKit
33

44
struct EditorView: View {
5-
var editorURL: URL?
5+
var configuration: EditorConfiguration
66

77
var body: some View {
8-
_EditorView(editorURL: editorURL)
8+
_EditorView(configuration: configuration)
99
.toolbar {
1010
ToolbarItemGroup(placement: .topBarLeading) {
1111
Button(action: {}, label: {
@@ -69,11 +69,11 @@ struct EditorView: View {
6969
}
7070

7171
private struct _EditorView: UIViewControllerRepresentable {
72-
var editorURL: URL?
72+
let configuration: EditorConfiguration
7373

7474
func makeUIViewController(context: Context) -> EditorViewController {
75-
let viewController = EditorViewController()
76-
viewController.editorURL = editorURL
75+
let viewController = EditorViewController(configuration: configuration)
76+
7777
if #available(iOS 16.4, *) {
7878
viewController.webView.isInspectable = true
7979
}
@@ -88,6 +88,6 @@ private struct _EditorView: UIViewControllerRepresentable {
8888

8989
#Preview {
9090
NavigationStack {
91-
EditorView()
91+
EditorView(configuration: .default)
9292
}
9393
}

ios/Demo-iOS/Sources/GutenbergApp.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import SwiftUI
44
struct GutenbergApp: App {
55
var body: some Scene {
66
WindowGroup {
7-
ContentView()
7+
NavigationStack {
8+
ContentView()
9+
}
810
}
911
}
1012
}

0 commit comments

Comments
 (0)