-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Recover content on WebView refresh via pull model #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f4fddbe
feat: requestLatestContent retrieves content from host
dcalhoun 2968727
feat: iOS returns latest content across bridge
dcalhoun 3de1f60
feat: Android returns latest content across bridge
dcalhoun 45470c3
feat: Retrieve content from native host during initialization
dcalhoun 1b77ea2
fix: Prevent sending `Any?` as `Sendable`
dcalhoun 9d605ba
task: Log errors when retrieving and parsing post content
dcalhoun ce72b19
task: Catch Android exceptions while parsing post content
dcalhoun d37ea3d
test: Remove misguided platform preference assertion
dcalhoun f7b7eb5
docs: Update inline draft post comment
dcalhoun 8045ae0
fix: Apply consistent post status fallback
dcalhoun 4df4950
fix: Switch default fallback post status to `draft`
dcalhoun 996d8ef
feat: Configure iOS post status and type
dcalhoun 1084f24
feat: Configure Android post status and type
dcalhoun 1090293
fix: Expose Android post type and status in GBKit global
dcalhoun 8d6faf8
refactor: Rename iOS `postStatus` field to mirror Android
dcalhoun fa4ac86
refactor: Align Android `postStatus` default with iOS
dcalhoun 5c33db0
refactor: Rename iOS bridge methods
dcalhoun ba98f11
feat: GutenbergKit handles `webViewWebContentProcessDidTerminate`
dcalhoun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -170,6 +170,10 @@ public final class EditorViewController: UIViewController, GutenbergEditorContro | |
| // Set-up communications with the editor. | ||
| config.userContentController.add(controller, name: "editorDelegate") | ||
|
|
||
| // Register async message handler for content recovery requests. | ||
| // This allows JavaScript to request the latest persisted content from the native host. | ||
| config.userContentController.addScriptMessageHandler(controller, contentWorld: .page, name: "requestLatestContent") | ||
|
|
||
| // This is important so they user can't select anything but text across blocks. | ||
| config.selectionGranularity = .character | ||
|
|
||
|
|
@@ -659,6 +663,14 @@ public final class EditorViewController: UIViewController, GutenbergEditorContro | |
| } | ||
| } | ||
|
|
||
| fileprivate func controllerDidRequestLatestContent(_ controller: GutenbergEditorController) -> (title: String, content: String)? { | ||
| return delegate?.editorDidRequestLatestContent(self) | ||
| } | ||
|
|
||
| fileprivate func controllerWebContentProcessDidTerminate(_ controller: GutenbergEditorController) { | ||
| webView.reload() | ||
| } | ||
|
|
||
| // MARK: - Loading Complete: Editor Ready | ||
|
|
||
| /// Called when the editor JavaScript emits the `onEditorLoaded` message. | ||
|
|
@@ -714,10 +726,12 @@ public final class EditorViewController: UIViewController, GutenbergEditorContro | |
| @MainActor | ||
| private protocol GutenbergEditorControllerDelegate: AnyObject { | ||
| func controller(_ controller: GutenbergEditorController, didReceiveMessage message: EditorJSMessage) | ||
| func controllerDidRequestLatestContent(_ controller: GutenbergEditorController) -> (title: String, content: String)? | ||
| func controllerWebContentProcessDidTerminate(_ controller: GutenbergEditorController) | ||
| } | ||
|
|
||
| /// Hiding the conformances, and breaking retain cycles. | ||
| private final class GutenbergEditorController: NSObject, WKNavigationDelegate, WKScriptMessageHandler { | ||
| private final class GutenbergEditorController: NSObject, WKNavigationDelegate, WKScriptMessageHandler, WKScriptMessageHandlerWithReply { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL WKScriptMessageHandlerWithReply – its' really nice. |
||
| weak var delegate: GutenbergEditorControllerDelegate? | ||
| let configuration: EditorConfiguration | ||
| private let editorURL: URL? | ||
|
|
@@ -728,6 +742,27 @@ private final class GutenbergEditorController: NSObject, WKNavigationDelegate, W | |
| super.init() | ||
| } | ||
|
|
||
| // MARK: - WKScriptMessageHandlerWithReply | ||
|
|
||
| func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) async -> (Any?, String?) { | ||
| guard message.name == "requestLatestContent" else { | ||
| return (nil, "Unknown message handler: \(message.name)") | ||
| } | ||
|
|
||
| let content = await MainActor.run { | ||
| delegate?.controllerDidRequestLatestContent(self) | ||
| } | ||
|
|
||
| guard let content else { | ||
dcalhoun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return (nil, nil) // No content available - not an error | ||
| } | ||
|
|
||
| return ([ | ||
| "title": content.title, | ||
| "content": content.content | ||
| ] as [String: String], nil) | ||
| } | ||
|
|
||
| // MARK: - WKNavigationDelegate | ||
|
|
||
| func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { | ||
|
|
@@ -756,6 +791,13 @@ private final class GutenbergEditorController: NSObject, WKNavigationDelegate, W | |
| return .allow | ||
| } | ||
|
|
||
| func webViewWebContentProcessDidTerminate(_ webView: WKWebView) { | ||
| NSLog("webViewWebContentProcessDidTerminate: reloading editor") | ||
| MainActor.assumeIsolated { | ||
| delegate?.controllerWebContentProcessDidTerminate(self) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - WKScriptMessageHandler | ||
|
|
||
| func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.