Skip to content

Commit

Permalink
refactor: Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon committed May 17, 2023
1 parent c8673e2 commit 6bbab71
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 11 deletions.
8 changes: 5 additions & 3 deletions Mail/Views/Thread/WebViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class WebViewModel: NSObject, ObservableObject {
}

private func loadScripts(configuration: WKWebViewConfiguration) {
var scripts = ["javaScriptBridge", "fixEmailStyle", "heightHandler"]
var scripts = ["javaScriptBridge", "fixEmailStyle", "sizeHandler"]
#if DEBUG
scripts.insert("captureLog", at: 0)
#endif
Expand Down Expand Up @@ -127,9 +127,11 @@ extension WebViewModel: WKScriptMessageHandler {
}

private func updateWebViewHeight(_ message: WKScriptMessage) {
guard let data = message.body as? [String: CGFloat] else { return }
guard let data = message.body as? [String: CGFloat], let height = data["height"] else { return }

if let height = data["height"], abs(webViewHeight - height) > 5 {
// On some messages, the size infinitely increases by 1px ?
// Having a threshold avoids this problem
if Int(abs(webViewHeight - height)) > Constants.sizeChangeThreshold {
contentLoading = false
webViewHeight = height
}
Expand Down
1 change: 1 addition & 0 deletions MailCore/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public enum Constants {
try! NSRegularExpression(pattern: ">\\s*<|>?\\s+<?")
}()

public static let sizeChangeThreshold = 3
public static let viewportContent = "width=device-width, initial-scale=1.0"
public static let divWrapperId = "kmail-message-content"
public static let styleCSS = {
Expand Down
6 changes: 2 additions & 4 deletions MailCore/Utils/MessageWebViewUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ public enum MessageWebViewUtils {
resources += "<style>\(fixDisplayCSS)</style>"
}

if target == .editor {
if let editorCSS = Bundle.main.loadCSS(filename: "editor") {
resources += "<style>\(editorCSS)</style>"
}
if target == .editor, let editorCSS = Bundle.main.loadCSS(filename: "editor") {
resources += "<style>\(editorCSS)</style>"
}

return resources
Expand Down
4 changes: 0 additions & 4 deletions MailResources/js/captureLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* Sends a message to the WKScriptMessageHandler
* @param message Message content
*/
function captureLog(message) {
window.webkit.messageHandlers.log.postMessage(message);
}
Expand Down
File renamed without changes.

0 comments on commit 6bbab71

Please sign in to comment.