tags) for mentions.
+ let sanitizedContent = comment.content
+ .trimmingCharacters(in: .whitespacesAndNewlines)
+ .replacingOccurrences(of: String.emptyElementRegexPattern, with: String(), options: [.regularExpression])
+
+ webView.loadHTMLString(.init(format: templateFormat, sanitizedContent), baseURL: nil)
// TODO: Configure component visibility
}
@@ -227,4 +235,7 @@ private extension String {
+ "%1$d is a placeholder for the number of Likes.")
static let pluralLikesFormat = NSLocalizedString("%1$d Likes", comment: "Plural button title to Like a comment. "
+ "%1$d is a placeholder for the number of Likes.")
+
+ // pattern that detects empty HTML elements (including HTML comments within).
+ static let emptyElementRegexPattern = "<[a-z]+>()+<\\/[a-z]+>"
}
From c123b05cf2e3749348f81adf45c45681f42cdfa7 Mon Sep 17 00:00:00 2001
From: David Christiandy <1299411+dvdchr@users.noreply.github.com>
Date: Tue, 7 Sep 2021 17:31:57 +0700
Subject: [PATCH 10/29] Recalculate cell height on orientation change.
Also improve styling for and .
---
.../CommentContentTableViewCell.swift | 23 +++++++++++-
WordPress/richCommentTemplate.html | 37 +++++++++++++++++--
2 files changed, 54 insertions(+), 6 deletions(-)
diff --git a/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift b/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
index e18c5f3fa1a7..7435e6afc06d 100644
--- a/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
+++ b/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
@@ -42,7 +42,7 @@ class CommentContentTableViewCell: UITableViewCell, NibReusable {
@IBOutlet private weak var likeButton: UIButton!
/// Cache the HTML template format. We only need read the template once.
- private static var htmlTemplateFormat: String? = {
+ private static let htmlTemplateFormat: String? = {
guard let templatePath = Bundle.main.path(forResource: "richCommentTemplate", ofType: "html"),
let templateString = try? String(contentsOfFile: templatePath) else {
return nil
@@ -51,6 +51,9 @@ class CommentContentTableViewCell: UITableViewCell, NibReusable {
return templateString
}()
+ /// Caches the HTML content, to be reused when the orientation changed.
+ private var htmlContentCache: String? = nil
+
// MARK: Lifecycle
override func awakeFromNib() {
@@ -60,6 +63,7 @@ class CommentContentTableViewCell: UITableViewCell, NibReusable {
override func prepareForReuse() {
onContentLoaded = nil
+ htmlContentCache = nil
}
// MARK: Public Methods
@@ -70,6 +74,7 @@ class CommentContentTableViewCell: UITableViewCell, NibReusable {
/// - comment: The `Comment` object to display.
/// - onContentLoaded: Callback to be called once the content has been loaded. Provides the new content height as parameter.
func configure(with comment: Comment, onContentLoaded: ((CGFloat) -> Void)?) {
+ print(">>>> CONFIGURE WITH COMMENT CALLED FOR CELL: \(self)")
nameLabel?.setText(comment.authorForDisplay())
dateLabel?.setText(comment.dateForDisplay()?.toMediumString() ?? String())
@@ -93,11 +98,25 @@ class CommentContentTableViewCell: UITableViewCell, NibReusable {
let sanitizedContent = comment.content
.trimmingCharacters(in: .whitespacesAndNewlines)
.replacingOccurrences(of: String.emptyElementRegexPattern, with: String(), options: [.regularExpression])
+ let htmlString = String(format: templateFormat, sanitizedContent)
- webView.loadHTMLString(.init(format: templateFormat, sanitizedContent), baseURL: nil)
+ webView.loadHTMLString(htmlString, baseURL: nil)
+ self.htmlContentCache = htmlString
// TODO: Configure component visibility
}
+
+ override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
+ // when screen orientation changed, reload the HTML content to ensure that the content height accounts for the new width.
+ guard let cachedContent = htmlContentCache,
+ traitCollection.verticalSizeClass != previousTraitCollection?.verticalSizeClass
+ || traitCollection.horizontalSizeClass != previousTraitCollection?.horizontalSizeClass else {
+ return
+ }
+
+ webViewHeightConstraint.constant = 1
+ webView.loadHTMLString(cachedContent, baseURL: nil)
+ }
}
// MARK: - WKNavigationDelegate
diff --git a/WordPress/richCommentTemplate.html b/WordPress/richCommentTemplate.html
index 4179d72999f4..e4723c06f514 100644
--- a/WordPress/richCommentTemplate.html
+++ b/WordPress/richCommentTemplate.html
@@ -64,8 +64,16 @@
/** IMAGES **/
.wp-block-image > img {
- -webkit-border-radius: 3pt;
+ display: block;
+ margin: 0 auto; /* align center */
max-width: 100%;
+ -webkit-border-radius: 3pt;
+ }
+
+ figcaption {
+ font: -apple-system-caption1;
+ color: -apple-system-secondary-label;
+ text-align: center;
}
/* set custom emoji images to be match current font size, and align it to the text baseline. */
@@ -77,6 +85,7 @@
/** BLOCKQUOTES **/
blockquote {
+ font-size: 1rem;
margin: auto 0;
padding: 10pt;
background-color: -apple-system-secondary-background;
@@ -88,16 +97,36 @@
padding-top: 0;
}
+ blockquote cite {
+ display: block;
+ font: -apple-system-footnote;
+ text-align: right;
+ }
+
+ blockquote cite::before {
+ content: "— "
+ }
+
/** CODE SNIPPETS **/
- code {
+ code,
+ pre {
font-family: var(--monospace-font);
font-size: 0.85rem;
- padding: 1px 3px;
- -webkit-border-radius: 3px;
+ -webkit-border-radius: 3pt;
background-color: -apple-system-secondary-background;
}
+ code {
+ padding: 1px 3px;
+ }
+
+ pre {
+ padding: 10pt 5pt 10pt;
+ white-space: pre-wrap;
+ word-wrap: break-word;
+ }
+
/* TABLES */
table {
From 6fcbc09b0e54cb042c8a4e7674b89f69016a2d8e Mon Sep 17 00:00:00 2001
From: David Christiandy <1299411+dvdchr@users.noreply.github.com>
Date: Tue, 7 Sep 2021 18:32:41 +0700
Subject: [PATCH 11/29] Fix white flash for web view in dark mode
---
.../ViewRelated/Comments/CommentContentTableViewCell.swift | 1 +
1 file changed, 1 insertion(+)
diff --git a/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift b/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
index 7435e6afc06d..883f0c5aaf22 100644
--- a/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
+++ b/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
@@ -177,6 +177,7 @@ private extension CommentContentTableViewCell {
webView.scrollView.isScrollEnabled = false
webView.scrollView.contentInset = .zero
webView.backgroundColor = .clear
+ webView.isOpaque = false // gets rid of the white flash upon content load in dark mode.
replyButton?.tintColor = Style.buttonTintColor
replyButton?.titleLabel?.font = Style.reactionButtonFont
From b5f0f22a39e3ca6e15c0b85806b7825a64ead753 Mon Sep 17 00:00:00 2001
From: David Christiandy <1299411+dvdchr@users.noreply.github.com>
Date: Tue, 7 Sep 2021 18:38:35 +0700
Subject: [PATCH 12/29] Allow overflowing table to scroll horizontally
---
.../ViewRelated/Comments/CommentContentTableViewCell.swift | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift b/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
index 883f0c5aaf22..4a3b41efc320 100644
--- a/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
+++ b/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
@@ -174,8 +174,8 @@ private extension CommentContentTableViewCell {
webView.navigationDelegate = self
webView.scrollView.bounces = false
- webView.scrollView.isScrollEnabled = false
webView.scrollView.contentInset = .zero
+ webView.scrollView.showsVerticalScrollIndicator = false
webView.backgroundColor = .clear
webView.isOpaque = false // gets rid of the white flash upon content load in dark mode.
From 08a4db684d2b86a63b732e3cdbe4480d0ac2c5de Mon Sep 17 00:00:00 2001
From: David Christiandy <1299411+dvdchr@users.noreply.github.com>
Date: Tue, 7 Sep 2021 19:07:48 +0700
Subject: [PATCH 13/29] Remove print statement
---
.../ViewRelated/Comments/CommentContentTableViewCell.swift | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift b/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
index 4a3b41efc320..e3ed694baf50 100644
--- a/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
+++ b/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
@@ -74,7 +74,6 @@ class CommentContentTableViewCell: UITableViewCell, NibReusable {
/// - comment: The `Comment` object to display.
/// - onContentLoaded: Callback to be called once the content has been loaded. Provides the new content height as parameter.
func configure(with comment: Comment, onContentLoaded: ((CGFloat) -> Void)?) {
- print(">>>> CONFIGURE WITH COMMENT CALLED FOR CELL: \(self)")
nameLabel?.setText(comment.authorForDisplay())
dateLabel?.setText(comment.dateForDisplay()?.toMediumString() ?? String())
@@ -99,9 +98,9 @@ class CommentContentTableViewCell: UITableViewCell, NibReusable {
.trimmingCharacters(in: .whitespacesAndNewlines)
.replacingOccurrences(of: String.emptyElementRegexPattern, with: String(), options: [.regularExpression])
let htmlString = String(format: templateFormat, sanitizedContent)
+ self.htmlContentCache = htmlString
webView.loadHTMLString(htmlString, baseURL: nil)
- self.htmlContentCache = htmlString
// TODO: Configure component visibility
}
@@ -131,7 +130,7 @@ extension CommentContentTableViewCell: WKNavigationDelegate {
}
// To capture the content height, the methods to use is either `document.body.scrollHeight` or `document.documentElement.scrollHeight`.
- // However, `document.body` does not capture margins on tag, so we'll use `document.documentElement` instead.
+ // `document.body` does not capture margins on tag, so we'll use `document.documentElement` instead.
webView.evaluateJavaScript("document.documentElement.scrollHeight") { height, _ in
guard let height = height as? CGFloat else {
return
From 7416de707298c7b0347daf43924b84d5c3d29140 Mon Sep 17 00:00:00 2001
From: David Christiandy <1299411+dvdchr@users.noreply.github.com>
Date: Tue, 7 Sep 2021 22:18:16 +0700
Subject: [PATCH 14/29] Improve styling for image and code snippets
---
.../Comments/CommentContentTableViewCell.swift | 2 +-
WordPress/richCommentTemplate.html | 18 ++++++++++++------
2 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift b/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
index e3ed694baf50..dbe9c3dc727a 100644
--- a/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
+++ b/WordPress/Classes/ViewRelated/Comments/CommentContentTableViewCell.swift
@@ -98,8 +98,8 @@ class CommentContentTableViewCell: UITableViewCell, NibReusable {
.trimmingCharacters(in: .whitespacesAndNewlines)
.replacingOccurrences(of: String.emptyElementRegexPattern, with: String(), options: [.regularExpression])
let htmlString = String(format: templateFormat, sanitizedContent)
- self.htmlContentCache = htmlString
+ self.htmlContentCache = htmlString
webView.loadHTMLString(htmlString, baseURL: nil)
// TODO: Configure component visibility
diff --git a/WordPress/richCommentTemplate.html b/WordPress/richCommentTemplate.html
index e4723c06f514..c71be41d9e66 100644
--- a/WordPress/richCommentTemplate.html
+++ b/WordPress/richCommentTemplate.html
@@ -58,15 +58,20 @@
text-decoration: none;
background-color: var(--mention-background-color);
padding: 0 3px;
- -webkit-border-radius: 3px;
+ -webkit-border-radius: 3pt;
}
/** IMAGES **/
- .wp-block-image > img {
+ .wp-block-image {
+ background-color: -apple-system-blue;
+ }
+
+ .wp-block-image img {
display: block;
margin: 0 auto; /* align center */
- max-width: 100%;
+ width: auto;
+ max-width: 100vw;
-webkit-border-radius: 3pt;
}
@@ -104,7 +109,7 @@
}
blockquote cite::before {
- content: "— "
+ content: "— ";
}
/** CODE SNIPPETS **/
@@ -118,11 +123,12 @@
}
code {
- padding: 1px 3px;
+ padding: 0 3pt;
}
pre {
- padding: 10pt 5pt 10pt;
+ padding: 10pt;
+ padding-right: 5pt;
white-space: pre-wrap;
word-wrap: break-word;
}
From a14b931afcfb21356aac7ffc6a72b38f81e5c705 Mon Sep 17 00:00:00 2001
From: David Christiandy <1299411+dvdchr@users.noreply.github.com>
Date: Tue, 7 Sep 2021 22:30:15 +0700
Subject: [PATCH 15/29] Override hardcoded block styles
---
WordPress/richCommentTemplate.html | 24 ++++++++++++++++++------
1 file changed, 18 insertions(+), 6 deletions(-)
diff --git a/WordPress/richCommentTemplate.html b/WordPress/richCommentTemplate.html
index c71be41d9e66..504f93ab5a23 100644
--- a/WordPress/richCommentTemplate.html
+++ b/WordPress/richCommentTemplate.html
@@ -43,6 +43,10 @@
margin-bottom: 0;
}
+ p {
+ word-wrap: break-word;
+ }
+
/* remove the default left/right margin for figures, as some elements (tables, images) are often embedded in