PERF: attempts to resort to compute markdown in less cases (#15762)

This commit is contained in:
Joffrey JAFFEUX 2022-02-01 15:28:50 +01:00 committed by GitHub
parent 3f694e4ab5
commit c0e2a01d9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -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 (

View File

@ -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);
}
}