PERF: Cache PrettyText instance for rendering composer preview (#9987)
Previously we were building pretty-text from scratch on every keypress
This commit is contained in:
parent
4ce618e55b
commit
833b5d89e0
|
@ -9,7 +9,7 @@ import discourseComputed, {
|
||||||
} from "discourse-common/utils/decorators";
|
} from "discourse-common/utils/decorators";
|
||||||
import { categoryHashtagTriggerRule } from "discourse/lib/category-hashtags";
|
import { categoryHashtagTriggerRule } from "discourse/lib/category-hashtags";
|
||||||
import { search as searchCategoryTag } from "discourse/lib/category-tag-search";
|
import { search as searchCategoryTag } from "discourse/lib/category-tag-search";
|
||||||
import { cookAsync } from "discourse/lib/text";
|
import { generateCookFunction } from "discourse/lib/text";
|
||||||
import { getRegister } from "discourse-common/lib/get-owner";
|
import { getRegister } from "discourse-common/lib/get-owner";
|
||||||
import { findRawTemplate } from "discourse-common/lib/raw-templates";
|
import { findRawTemplate } from "discourse-common/lib/raw-templates";
|
||||||
import { siteDir } from "discourse/lib/text-direction";
|
import { siteDir } from "discourse/lib/text-direction";
|
||||||
|
@ -344,15 +344,26 @@ export default Component.extend({
|
||||||
return toolbar;
|
return toolbar;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
cachedCookAsync(text) {
|
||||||
|
if (this._cachedCookFunction) {
|
||||||
|
return Promise.resolve(this._cachedCookFunction(text));
|
||||||
|
}
|
||||||
|
|
||||||
|
const markdownOptions = this.markdownOptions || {};
|
||||||
|
return generateCookFunction(markdownOptions).then(cook => {
|
||||||
|
this._cachedCookFunction = cook;
|
||||||
|
return cook(text);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
_updatePreview() {
|
_updatePreview() {
|
||||||
if (this._state !== "inDOM") {
|
if (this._state !== "inDOM") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const value = this.value;
|
const value = this.value;
|
||||||
const markdownOptions = this.markdownOptions || {};
|
|
||||||
|
|
||||||
cookAsync(value, markdownOptions).then(cooked => {
|
this.cachedCookAsync(value).then(cooked => {
|
||||||
if (this.isDestroyed) {
|
if (this.isDestroyed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,15 @@ export function cookAsync(text, options) {
|
||||||
return loadMarkdownIt().then(() => cook(text, options));
|
return loadMarkdownIt().then(() => cook(text, options));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Warm up pretty text with a set of options and return a function
|
||||||
|
// which can be used to cook without rebuilding prettytext every time
|
||||||
|
export function generateCookFunction(options) {
|
||||||
|
return loadMarkdownIt().then(() => {
|
||||||
|
const prettyText = createPrettyText(options);
|
||||||
|
return text => prettyText.cook(text);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function sanitize(text, options) {
|
export function sanitize(text, options) {
|
||||||
return textSanitize(text, new WhiteLister(options));
|
return textSanitize(text, new WhiteLister(options));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue