diff --git a/Mail/Views/Thread/WebView.swift b/Mail/Views/Thread/WebView.swift index 5af7b89e1..a9d4a6d18 100644 --- a/Mail/Views/Thread/WebView.swift +++ b/Mail/Views/Thread/WebView.swift @@ -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)) } } } @@ -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 { @@ -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) } } diff --git a/MailResources/js/mungeEmail.js b/MailResources/js/mungeEmail.js index 3ca4eb139..abf6ed507 100644 --- a/MailResources/js/mungeEmail.js +++ b/MailResources/js/mungeEmail.js @@ -21,7 +21,8 @@ const PREFERENCES = { normalizeMessageWidths: true, mungeImages: true, mungeTables: true, - minimumEffectiveRatio: 0.7 + minimumEffectiveRatio: 0.7, + undoPreviousChanges: true }; let actionsLog = {}; @@ -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) { @@ -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. @@ -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) {