From c0e2a01d9f7c3d32951d2c1678798f1884142747 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Tue, 1 Feb 2022 15:28:50 +0100 Subject: [PATCH] PERF: attempts to resort to compute markdown in less cases (#15762) --- .../javascripts/discourse/app/components/quote-button.js | 9 +++++++-- app/assets/javascripts/discourse/app/lib/utilities.js | 6 +++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse/app/components/quote-button.js b/app/assets/javascripts/discourse/app/components/quote-button.js index b4002f8604e..a8a4de73de5 100644 --- a/app/assets/javascripts/discourse/app/components/quote-button.js +++ b/app/assets/javascripts/discourse/app/components/quote-button.js @@ -5,6 +5,7 @@ import { popupAjaxError } from "discourse/lib/ajax-error"; import { postUrl, selectedElement, + selectedRange, selectedText, setCaretPosition, translateModKey, @@ -164,10 +165,14 @@ export default Component.extend(KeyEnterEscape, { const cooked = $selectedElement.find(".cooked")[0] || $selectedElement.closest(".cooked")[0]; - const postBody = toMarkdown(cooked.innerHTML); + // computing markdown takes a lot of time on long posts + // this code attempts to compute it only when we can't fast track let opts = { - full: _selectedText === postBody, + full: + selectedRange().startOffset > 0 + ? false + : _selectedText === toMarkdown(cooked.innerHTML), }; for ( diff --git a/app/assets/javascripts/discourse/app/lib/utilities.js b/app/assets/javascripts/discourse/app/lib/utilities.js index dff0909873a..ddb80fafb2a 100644 --- a/app/assets/javascripts/discourse/app/lib/utilities.js +++ b/app/assets/javascripts/discourse/app/lib/utilities.js @@ -207,9 +207,13 @@ export function selectedText() { } export function selectedElement() { + return selectedRange()?.commonAncestorContainer; +} + +export function selectedRange() { const selection = window.getSelection(); if (selection.rangeCount > 0) { - return selection.getRangeAt(0).commonAncestorContainer; + return selection.getRangeAt(0); } }