Skip to content
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

WebKit Reader Comments (Groundwork) #24022

Open
wants to merge 26 commits into
base: trunk
Choose a base branch
from
Open

Conversation

kean
Copy link
Contributor

@kean kean commented Jan 28, 2025

This is the initial PR that enabled WebKit-based rendering for comments in the "Comments" screen.

The app already had WebCommentContentRenderer. It was used only in a subset of screens, for example, for a single comment displayed right under the article. This PR introduces a few performance and other improvements that made it possible to use in the lists.

This PR also introduces a new WordPressReader module. I moved WebCommentContentRenderer and ReaderDisplaySettings to this new module and added Xcode previews to make it easy to iterate on it.

The feature is not complete, so it's currently hidden behind a feature flag, so it should be safe to merge now and continue iterating in trunk. I'm planning more performance optimizations, better styles for some Gutenberg blocks, and better handling for URLs.

Screenshot 2025-01-30 at 4 14 41 PM Screenshot 2025-01-30 at 4 14 35 PM

Future Plans

The main issues is that rendering in WKWebView is inherently asynchronous and depending on how fast you scroll and how slow your device is, you might end up seeing empty cells until the comment loads. I'm planning to work this around by adding prefetching to this screen. It will ensure that in the vast majority of cases, you are never going to see the empty cells. I will also add loading state (skeletons?) for empty cells.

I also haven't measured the impact on the battery life yet. It might be worth adding a simple UITextView-based implementation for plain text cells. It will also free the resources for other WKWebView instances that do need WebKit to render the content.

Testing

  • Enable the new "Render Comments using WebKit" feature flag
  • Verify that Gutenberg comments are displayed (well) and the cell heights are computed correctly

Future

Regression Notes

  1. Potential unintended areas of impact

  2. What I did to test those areas of impact (or what existing automated tests I relied on)

  3. What automated tests I added (or what prevented me from doing so)

PR submission checklist:

  • I have completed the Regression Notes.
  • I have considered adding unit tests for my changes.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

Testing checklist:

  • WordPress.com sites and self-hosted Jetpack sites.
  • Portrait and landscape orientations.
  • Light and dark modes.
  • Fonts: Larger, smaller and bold text.
  • High contrast.
  • VoiceOver.
  • Languages with large words or with letters/accents not frequently used in English.
  • Right-to-left languages. (Even if translation isn’t complete, formatting should still respect the right-to-left layout)
  • iPhone and iPad.
  • Multi-tasking: Split view and Slide over. (iPad)

@kean kean added the Reader label Jan 28, 2025
@kean kean added this to the 25.8 milestone Jan 28, 2025
@dangermattic
Copy link
Collaborator

dangermattic commented Jan 28, 2025

2 Warnings
⚠️ Modules/Package.swift was changed without updating its corresponding Package.resolved. Please resolve the Swift packages as appropriate to your project setup (e.g. in Xcode or by running swift package resolve).
⚠️ This PR is larger than 500 lines of changes. Please consider splitting it into smaller PRs for easier and faster reviews.

Generated by 🚫 Danger

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Jan 28, 2025

Jetpack Alpha📲 You can test the changes from this Pull Request in Jetpack Alpha by scanning the QR code below to install the corresponding build.
App NameJetpack Alpha Jetpack Alpha
ConfigurationRelease-Alpha
Build Numberpr24022-075261c
Version25.7
Bundle IDcom.jetpack.alpha
Commit075261c
App Center Buildjetpack-installable-builds #10461
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Jan 28, 2025

WordPress Alpha📲 You can test the changes from this Pull Request in WordPress Alpha by scanning the QR code below to install the corresponding build.
App NameWordPress Alpha WordPress Alpha
ConfigurationRelease-Alpha
Build Numberpr24022-075261c
Version25.7
Bundle IDorg.wordpress.alpha
Commit075261c
App Center BuildWPiOS - One-Offs #11428
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

@kean kean force-pushed the task/better-comments-rendering branch from 292a91d to 108380e Compare January 30, 2025 21:13
}

public func render(comment: String) -> UIView {
guard self.comment != comment else {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was part of the existing CommentContentRenderer protocol, and I don't want to change it too much just yet.

public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// Wait until the HTML document finished loading.
// This also waits for all of resources within the HTML (images, video thumbnail images) to be fully loaded.
webView.evaluateJavaScript("document.readyState") { complete, _ in
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the existing code. I just moved it. Not sure if it works the way it says it does.

// remove empty HTML elements from the `content`, as the content often contains empty paragraph elements which adds unnecessary padding/margin.
// `rawContent` does not have this problem, but it's not used because `rawContent` gets rid of links (<a> tags) for mentions.
let comment = comment
.replacingOccurrences(of: Self.emptyElementRegexPattern, with: "", options: [.regularExpression])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, I want to preprocess this in the background and store in Core Data in this way.


// MARK: - ReaderDisplaySetting (CSS)

extension ReaderDisplaySettings {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an opportunity to reuse this with ReaderWebView. I'm going to look into it.

import WordPressShared

struct ReaderDisplaySetting: Codable, Equatable {
public struct ReaderDisplaySettings: Codable, Equatable, Hashable, Sendable {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed it because I kept typing ReaderDisplaySettings.

return renderer
}

func getCachedContentHeight(for commentID: TaggedManagedObjectID<Comment>) -> CGFloat? {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll probably change the keys to use comment's content (String). I'm not sure yet because comparing long strings isn't ideal.

}

func getRenderer(for comment: Comment) -> WebCommentContentRenderer {
if let renderer = renderers.object(forKey: comment) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's a good idea to reuse web view across different cells this way, but it helps with cell reload. UITableView does little to nothing to ensure the same cells are used for the same comments on reload.

@kean kean force-pushed the task/better-comments-rendering branch from 6c43b39 to 075261c Compare January 30, 2025 21:38
@kean kean changed the title WebKit Reader Comments WebKit Reader Comments (Ground Work) Jan 30, 2025
@kean kean marked this pull request as ready for review January 30, 2025 21:38
@kean kean changed the title WebKit Reader Comments (Ground Work) WebKit Reader Comments (Groundwork) Jan 30, 2025
@kean kean requested a review from crazytonyli January 30, 2025 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants