From c6db5f1defd2f2c162d9ed26c59f95a56944c530 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Fri, 3 Feb 2023 11:17:16 +0100 Subject: [PATCH] Move blockquote switcher to HTML parser #902 --- dev/Common/Html.js | 28 ++++++++++++++++++++++++++-- dev/Model/Message.js | 26 -------------------------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/dev/Common/Html.js b/dev/Common/Html.js index 196ffae3b..225bf751a 100644 --- a/dev/Common/Html.js +++ b/dev/Common/Html.js @@ -1,4 +1,4 @@ -import { createElement } from 'Common/Globals'; +import { doc, createElement } from 'Common/Globals'; import { forEachObjectEntry, pInt } from 'Common/Utils'; import { SettingsUserStore } from 'Stores/User/Settings'; @@ -14,6 +14,24 @@ const "'": ''' }, + hcont = Element.fromHTML('
'), + getRealHeight = el => { + hcont.innerHTML = el.outerHTML; + const result = hcont.clientHeight; + hcont.innerHTML = ''; + return result; + }, + blockquoteSwitcher = () => { + tpl.content.querySelectorAll('blockquote').forEach(node => { + let h = node.clientHeight || getRealHeight(node); + if (0 === h || 100 < h) { + const el = Element.fromHTML('
•••
'); + node.replaceWith(el); + el.append(node); + } + }); + }, + replaceWithChildren = node => node.replaceWith(...[...node.childNodes]), url = /(^|\s|\n|\/?>)(https?:\/\/[-A-Z0-9+&#/%?=()~_|!:,.;]*[-A-Z0-9+&#/%=~()_|])/gi, @@ -79,6 +97,8 @@ const return url; }; +doc.body.append(hcont); + export const /** @@ -435,6 +455,8 @@ export const } }); + blockquoteSwitcher(); + // return tpl.content.firstChild; result.html = tpl.innerHTML.trim(); return result; @@ -588,7 +610,7 @@ export const aText = aNextText; } while (bDo); - return aText.join('\n') + tpl.innerHTML = aText.join('\n') // .replace(/~~~\/blockquote~~~\n~~~blockquote~~~/g, '\n') .replace(/&/g, '&') .replace(/>/g, '>') @@ -602,6 +624,8 @@ export const .replace(/~~~blockquote~~~\s*/g, '
') .replace(/\s*~~~\/blockquote~~~/g, '
') .replace(/\n/g, '
'); + blockquoteSwitcher(); + return tpl.innerHTML.trim(); }, WYSIWYGS = ko.observableArray(); diff --git a/dev/Model/Message.js b/dev/Model/Message.js index 177a8029f..038ffb45d 100644 --- a/dev/Model/Message.js +++ b/dev/Model/Message.js @@ -25,14 +25,6 @@ import { LanguageStore } from 'Stores/Language'; import Remote from 'Remote/User/Fetch'; const - hcont = Element.fromHTML('
'), - getRealHeight = el => { - hcont.innerHTML = el.outerHTML; - const result = hcont.clientHeight; - hcont.innerHTML = ''; - return result; - }, - toggleTag = (message, keyword) => { const lower = keyword.toLowerCase(), flags = message.flags, @@ -60,8 +52,6 @@ const unic[email.email] || localEmails.has(email.email) || localEmails.set(email.email, email) ); -doc.body.append(hcont); - export class MessageModel extends AbstractModel { constructor() { super(); @@ -307,7 +297,6 @@ export class MessageModel extends AbstractModel { } this.isHtml(true); - this.initView(); return true; } } @@ -327,25 +316,10 @@ export class MessageModel extends AbstractModel { ); this.isHtml(false); this.hasImages(false); - this.initView(); return true; } } - initView() { - // init BlockquoteSwitcher - this.body.querySelectorAll('blockquote').forEach(node => { - if (node.textContent.trim()) { - let h = node.clientHeight || getRealHeight(node); - if (0 === h || 100 < h) { - const el = Element.fromHTML('
•••
'); - node.replaceWith(el); - el.append(node); - } - } - }); - } - viewPopupMessage(print) { const timeStampInUTC = this.dateTimeStampInUTC() || 0, ccLine = this.cc.toString(),