Skip to content

Commit

Permalink
refactor: Clean JavaScript code
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon committed Jun 30, 2023
1 parent 6adfbb9 commit 99a7a6f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
8 changes: 6 additions & 2 deletions Mail/Views/Thread/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class WebViewController: UIViewController {
.debounce(for: .seconds(0.3), scheduler: DispatchQueue.main)
.sink { newWidth in
Task {
_ = try await self.model.webView.evaluateJavaScript("normalizeMessageWidth(\(newWidth), '\(self.messageUid ?? "")')")
try await self.normalizeMessageWidth(webViewWidth: CGFloat(newWidth))
}
}
}
Expand All @@ -65,6 +65,10 @@ final class WebViewController: UIViewController {
}
#endif
}

private func normalizeMessageWidth(webViewWidth width: CGFloat) async throws {
_ = try await model.webView.evaluateJavaScript("normalizeMessageWidth(\(width), '\(messageUid ?? "")')")
}
}

extension WebViewController: WKNavigationDelegate {
Expand All @@ -82,7 +86,7 @@ extension WebViewController: WKNavigationDelegate {
guard readyState == "complete" else { return }

_ = try await webView.evaluateJavaScript("removeAllProperties()")
_ = try await webView.evaluateJavaScript("normalizeMessageWidth(\(webView.frame.width), '\(messageUid ?? "")')")
try await normalizeMessageWidth(webViewWidth: webView.frame.width)
}
}

Expand Down
33 changes: 24 additions & 9 deletions MailResources/js/mungeEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const PREFERENCES = {
normalizeMessageWidths: true,
mungeImages: true,
mungeTables: true,
minimumEffectiveRatio: 0.7
minimumEffectiveRatio: 0.7,
undoPreviousChanges: true
};

let actionsLog = {};
Expand Down Expand Up @@ -55,15 +56,16 @@ function normalizeElementWidths(elements, webViewWidth, messageUid) {
const documentWidth = document.body.offsetWidth;
logInfo(`Starts to normalize elements. Document width: ${documentWidth}. WebView width: ${webViewWidth}.`);

let currentActionsLog = actionsLog[messageUid];
if (currentActionsLog != undefined && currentActionsLog.length > 0) {
logInfo('We need to undo previous changes.');
undoActions(currentActionsLog);
}

for (const element of elements) {
logInfo(`Current element: ${elementDebugName(element)}.`);

// If the script has already been run, we can undo the changes we've made and start again from scratch
let currentActionsLog = getActionsLog(element, messageUid);
if (PREFERENCES.undoPreviousChanges && currentActionsLog.length > 0) {
logInfo('We need to undo changes from a previous run.');
undoActions(currentActionsLog);
}

// Reset any existing normalization
const originalZoom = element.style.zoom;
if (originalZoom) {
Expand Down Expand Up @@ -106,8 +108,7 @@ function transformContent(element, documentWidth, elementWidth, messageUid) {
let newWidth = elementWidth;
let isTransformationDone = false;
/** Format of entries : { function: fn, object: object, arguments: [list of arguments] } */
actionsLog[messageUid] = [];
let currentActionsLog = actionsLog[messageUid];
let currentActionsLog = getActionsLog(element, messageUid);

// Try munging all divs or textareas with inline styles where the width
// is wider than `documentWidth`, and change it to be a max-width.
Expand Down Expand Up @@ -319,6 +320,20 @@ function shouldMungeTable(table) {
return table.hasAttribute('width') || table.style.width;
}

/**
* Get the actionsLog associated with the element to be modified
* @param element Element to be modified
* @param messageUid MessageUid associated to the element
* @returns {string} Id of the actionsLog
*/
function getActionsLog(element, messageUid) {
const id= `${element.id}${messageUid}`;
if (actionsLog[id] === undefined) {
actionsLog[id] = [];
}
return actionsLog[id];
}

// Logger

function logInfo(text) {
Expand Down

0 comments on commit 99a7a6f

Please sign in to comment.