Skip to content

Commit 832d686

Browse files
committed
Use SwiftSoup parser in the demo app
1 parent c0de3ec commit 832d686

File tree

9 files changed

+73
-53
lines changed

9 files changed

+73
-53
lines changed

Package.resolved

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

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ let package = Package(
77
name: "GutenbergKit",
88
platforms: [.iOS(.v15), .macOS(.v14)],
99
products: [
10-
.library(name: "GutenbergKit", targets: ["GutenbergKit"])
10+
.library(name: "GutenbergKit", targets: ["GutenbergKit"]),
11+
.library(name: "AssetManifestParser", targets: ["SwiftSoupAssetManifestParser"])
1112
],
1213
dependencies: [
1314
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.7.5"),

ios/Demo-iOS/Gutenberg.xcodeproj/project.pbxproj

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 56;
6+
objectVersion = 60;
77
objects = {
88

99
/* Begin PBXBuildFile section */
@@ -12,7 +12,8 @@
1212
0CE8E78D2C339B0600B9DC67 /* EditorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CE8E7862C339B0600B9DC67 /* EditorView.swift */; };
1313
0CE8E78E2C339B0600B9DC67 /* GutenbergApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CE8E7872C339B0600B9DC67 /* GutenbergApp.swift */; };
1414
0CE8E78F2C339B0600B9DC67 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0CE8E7892C339B0600B9DC67 /* Preview Assets.xcassets */; };
15-
0CF6E04C2BEFF60E00EDEE8A /* GutenbergKit in Frameworks */ = {isa = PBXBuildFile; productRef = 0CF6E04B2BEFF60E00EDEE8A /* GutenbergKit */; };
15+
24DF8BDF2E723BB8003B3355 /* GutenbergKit in Frameworks */ = {isa = PBXBuildFile; productRef = 24DF8BDE2E723BB8003B3355 /* GutenbergKit */; };
16+
24DF8BE12E723BE5003B3355 /* AssetManifestParser in Frameworks */ = {isa = PBXBuildFile; productRef = 24DF8BE02E723BE5003B3355 /* AssetManifestParser */; };
1617
/* End PBXBuildFile section */
1718

1819
/* Begin PBXFileReference section */
@@ -22,15 +23,15 @@
2223
0CE8E7862C339B0600B9DC67 /* EditorView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditorView.swift; sourceTree = "<group>"; };
2324
0CE8E7872C339B0600B9DC67 /* GutenbergApp.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GutenbergApp.swift; sourceTree = "<group>"; };
2425
0CE8E7892C339B0600B9DC67 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
25-
0CE8E7922C339B1B00B9DC67 /* GutenbergKit */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = GutenbergKit; path = ../..; sourceTree = "<group>"; };
2626
/* End PBXFileReference section */
2727

2828
/* Begin PBXFrameworksBuildPhase section */
2929
0C4F59882BEFF4970028BD96 /* Frameworks */ = {
3030
isa = PBXFrameworksBuildPhase;
3131
buildActionMask = 2147483647;
3232
files = (
33-
0CF6E04C2BEFF60E00EDEE8A /* GutenbergKit in Frameworks */,
33+
24DF8BDF2E723BB8003B3355 /* GutenbergKit in Frameworks */,
34+
24DF8BE12E723BE5003B3355 /* AssetManifestParser in Frameworks */,
3435
);
3536
runOnlyForDeploymentPostprocessing = 0;
3637
};
@@ -40,7 +41,6 @@
4041
0C4F59822BEFF4970028BD96 = {
4142
isa = PBXGroup;
4243
children = (
43-
0CE8E7922C339B1B00B9DC67 /* GutenbergKit */,
4444
0CE8E7882C339B0600B9DC67 /* Sources */,
4545
0C83424D2C339B7F00CAA762 /* Resources */,
4646
0CE8E78A2C339B0600B9DC67 /* PreviewContent */,
@@ -107,7 +107,8 @@
107107
);
108108
name = Gutenberg;
109109
packageProductDependencies = (
110-
0CF6E04B2BEFF60E00EDEE8A /* GutenbergKit */,
110+
24DF8BDE2E723BB8003B3355 /* GutenbergKit */,
111+
24DF8BE02E723BE5003B3355 /* AssetManifestParser */,
111112
);
112113
productName = Gutenberg;
113114
productReference = 0C4F598B2BEFF4970028BD96 /* Gutenberg.app */;
@@ -137,6 +138,9 @@
137138
Base,
138139
);
139140
mainGroup = 0C4F59822BEFF4970028BD96;
141+
packageReferences = (
142+
24DF8BDD2E723BB8003B3355 /* XCLocalSwiftPackageReference "../../../GutenbergKit" */,
143+
);
140144
productRefGroup = 0C4F598C2BEFF4970028BD96 /* Products */;
141145
projectDirPath = "";
142146
projectRoot = "";
@@ -377,11 +381,23 @@
377381
};
378382
/* End XCConfigurationList section */
379383

384+
/* Begin XCLocalSwiftPackageReference section */
385+
24DF8BDD2E723BB8003B3355 /* XCLocalSwiftPackageReference "../../../GutenbergKit" */ = {
386+
isa = XCLocalSwiftPackageReference;
387+
relativePath = ../../../GutenbergKit;
388+
};
389+
/* End XCLocalSwiftPackageReference section */
390+
380391
/* Begin XCSwiftPackageProductDependency section */
381-
0CF6E04B2BEFF60E00EDEE8A /* GutenbergKit */ = {
392+
24DF8BDE2E723BB8003B3355 /* GutenbergKit */ = {
382393
isa = XCSwiftPackageProductDependency;
383394
productName = GutenbergKit;
384395
};
396+
24DF8BE02E723BE5003B3355 /* AssetManifestParser */ = {
397+
isa = XCSwiftPackageProductDependency;
398+
package = 24DF8BDD2E723BB8003B3355 /* XCLocalSwiftPackageReference "../../../GutenbergKit" */;
399+
productName = AssetManifestParser;
400+
};
385401
/* End XCSwiftPackageProductDependency section */
386402
};
387403
rootObject = 0C4F59832BEFF4970028BD96 /* Project object */;

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

Lines changed: 0 additions & 14 deletions
This file was deleted.

ios/Sources/GutenbergKit/Sources/Cache/EditorAssetsLibrary.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public actor EditorAssetsLibrary {
4545
///
4646
/// - SeeAlso: `CachedAssetSchemeHandler`
4747
/// - SeeAlso: `EditorAssetsLibrary.addAsset`
48-
func manifestContentForEditor(using parser: EditorAssetManifestParser) async throws -> Data {
48+
func manifestContentForEditor() async throws -> Data {
4949
// For scheme-less links (i.e. '//stats.wp.com/w.js'), use the scheme in `siteURL`.
5050
guard let siteURLScheme = URL(string: configuration.siteURL)?.scheme else {
5151
throw ManifestError.invalidSiteUrl
@@ -54,19 +54,19 @@ public actor EditorAssetsLibrary {
5454
let data = try await loadManifestContent()
5555
let manifest = try EditorAssetManifest(data: data)
5656

57-
return try JSONEncoder().encode(manifest.applyingUrlScheme(siteURLScheme, using: parser))
57+
return try JSONEncoder().encode(manifest.applyingUrlScheme(siteURLScheme, using: configuration.manifestParser))
5858
}
5959

6060
/// Fetches all assets in the `EditorConfiguration.editorAssetsEndpoint` manifest and stores them on the device.
6161
///
6262
/// - SeeAlso: CachedAssetSchemeHandler
63-
public func fetchAssets(using manifestParser: EditorAssetManifestParser) async throws {
63+
public func fetchAssets() async throws {
6464
// For scheme-less links (i.e. '//stats.wp.com/w.js'), use the scheme in `siteURL`.
6565
let siteURLScheme = URL(string: configuration.siteURL)?.scheme
6666

6767
let data = try await loadManifestContent()
68-
let manifest = try EditorAssetManifest(data: data).applyingUrlScheme(siteURLScheme, using: manifestParser)
69-
let assetUrls = try manifest.getAllAssetUrls(using: manifestParser)
68+
let manifest = try EditorAssetManifest(data: data).applyingUrlScheme(siteURLScheme, using: configuration.manifestParser)
69+
let assetUrls = try manifest.getAllAssetUrls(using: configuration.manifestParser)
7070

7171
for url in assetUrls {
7272
guard url.scheme == "http" || url.scheme == "https" else {

ios/Sources/GutenbergKit/Sources/Cache/EditorAssetsProvider.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import WebKit
33

44
class EditorAssetsProvider: NSObject, WKScriptMessageHandlerWithReply {
55
let library: EditorAssetsLibrary
6-
let manifestParser: EditorAssetManifestParser
76

8-
init(library: EditorAssetsLibrary, manifestParser: EditorAssetManifestParser) {
7+
init(library: EditorAssetsLibrary) {
98
self.library = library
10-
self.manifestParser = manifestParser
119
}
1210

1311
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage, replyHandler: @escaping @MainActor @Sendable (Any?, String?) -> Void) {
@@ -21,7 +19,7 @@ class EditorAssetsProvider: NSObject, WKScriptMessageHandlerWithReply {
2119

2220
Task.detached { [library] in
2321
do {
24-
let data = try await library.manifestContentForEditor(using: self.manifestParser)
22+
let data = try await library.manifestContentForEditor()
2523
let dict = try JSONSerialization.jsonObject(with: data)
2624
await replyHandler(dict, nil)
2725
} catch {

ios/Sources/GutenbergKit/Sources/EditorConfiguration.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,25 @@ public struct EditorConfiguration {
2222
public var locale = "en"
2323
public var editorAssetsEndpoint: URL?
2424

25+
public var manifestParser: EditorAssetManifestParser
26+
27+
@available(iOS 16.0, *)
2528
public init(title: String = "", content: String = "") {
2629
self.title = title
2730
self.content = content
31+
self.manifestParser = DefaultEditorAssetManifestParser()
32+
}
33+
34+
public init(title: String = "", content: String = "", manifestParser: EditorAssetManifestParser) {
35+
self.title = title
36+
self.content = content
37+
self.manifestParser = manifestParser
2838
}
2939

3040
public mutating func updateEditorSettings(_ settings: [String: Any]?) {
3141
self.editorSettings = settings
3242
}
3343

44+
@available(iOS 16.0, *)
3445
public static let `default` = EditorConfiguration()
3546
}

ios/Sources/GutenbergKit/Sources/EditorViewController+AssetManifest.swift

Lines changed: 0 additions & 17 deletions
This file was deleted.

ios/Sources/GutenbergKit/Sources/EditorViewController.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ public final class EditorViewController: UIViewController, GutenbergEditorContro
2727
///
2828
private let isWarmupMode: Bool
2929

30+
@available(iOS 16.0, *)
31+
public convenience init(isWarmupMode: Bool = false) {
32+
self.init(configuration: .default, isWarmupMode: isWarmupMode)
33+
}
34+
3035
/// Initalizes the editor with the initial content (Gutenberg).
31-
public init(configuration: EditorConfiguration = .default, isWarmupMode: Bool = false) {
36+
public init(configuration: EditorConfiguration, isWarmupMode: Bool = false) {
3237
self.configuration = configuration
3338
self.assetsLibrary = EditorAssetsLibrary(configuration: configuration)
3439
self.controller = GutenbergEditorController(configuration: configuration)
@@ -125,7 +130,7 @@ public final class EditorViewController: UIViewController, GutenbergEditorContro
125130
private func loadEditor() {
126131
if configuration.plugins {
127132
webView.configuration.userContentController.addScriptMessageHandler(
128-
EditorAssetsProvider(library: assetsLibrary, manifestParser: self.getAssetManifestParser()),
133+
EditorAssetsProvider(library: assetsLibrary),
129134
contentWorld: .page,
130135
name: "loadFetchedEditorAssets"
131136
)
@@ -356,6 +361,7 @@ public final class EditorViewController: UIViewController, GutenbergEditorContro
356361

357362
/// Calls this at any moment before showing the actual editor. The warmup
358363
/// shaves a couple of hundred milliseconds off the first load.
364+
@available(iOS 16.0, *)
359365
public static func warmup(configuration: EditorConfiguration = .default) {
360366
let editorViewController = EditorViewController(configuration: configuration, isWarmupMode: true)
361367
_ = editorViewController.view // Trigger viewDidLoad

0 commit comments

Comments
 (0)