mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 13:03:45 +08:00
PERF: reduces rendering time of local-dates (#13931)
- prefers insertAdjacentHTML over innerHTML as it's much faster in this case (about 5x) - memoizes tz.guess() - memoizes list of timezones - inlines template - applies main element class in one pass All in all for a very edge case of about 80 dates it should be faster of about 15/20ms.
This commit is contained in:
parent
1da0aa838f
commit
d23c0c06c3
|
@ -2,15 +2,6 @@ import LocalDateBuilder from "../lib/local-date-builder";
|
||||||
import showModal from "discourse/lib/show-modal";
|
import showModal from "discourse/lib/show-modal";
|
||||||
import { withPluginApi } from "discourse/lib/plugin-api";
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
||||||
|
|
||||||
const DATE_TEMPLATE = `
|
|
||||||
<span>
|
|
||||||
<svg class="fa d-icon d-icon-globe-americas svg-icon" xmlns="http://www.w3.org/2000/svg">
|
|
||||||
<use xlink:href="#globe-americas"></use>
|
|
||||||
</svg>
|
|
||||||
<span class="relative-time"></span>
|
|
||||||
</span>
|
|
||||||
`;
|
|
||||||
|
|
||||||
function initializeDiscourseLocalDates(api) {
|
function initializeDiscourseLocalDates(api) {
|
||||||
api.decorateCooked(
|
api.decorateCooked(
|
||||||
($elem) => $(".discourse-local-date", $elem).applyLocalDates(),
|
($elem) => $(".discourse-local-date", $elem).applyLocalDates(),
|
||||||
|
@ -45,6 +36,8 @@ export default {
|
||||||
initialize(container) {
|
initialize(container) {
|
||||||
const siteSettings = container.lookup("site-settings:main");
|
const siteSettings = container.lookup("site-settings:main");
|
||||||
if (siteSettings.discourse_local_dates_enabled) {
|
if (siteSettings.discourse_local_dates_enabled) {
|
||||||
|
const currentUserTZ = moment.tz.guess();
|
||||||
|
|
||||||
$.fn.applyLocalDates = function () {
|
$.fn.applyLocalDates = function () {
|
||||||
return this.each(function () {
|
return this.each(function () {
|
||||||
const opts = {};
|
const opts = {};
|
||||||
|
@ -67,7 +60,7 @@ export default {
|
||||||
|
|
||||||
const localDateBuilder = new LocalDateBuilder(
|
const localDateBuilder = new LocalDateBuilder(
|
||||||
opts,
|
opts,
|
||||||
moment.tz.guess()
|
currentUserTZ
|
||||||
).build();
|
).build();
|
||||||
|
|
||||||
const htmlPreviews = localDateBuilder.previews.map((preview) => {
|
const htmlPreviews = localDateBuilder.previews.map((preview) => {
|
||||||
|
@ -96,15 +89,24 @@ export default {
|
||||||
previewsNode.appendChild(htmlPreview)
|
previewsNode.appendChild(htmlPreview)
|
||||||
);
|
);
|
||||||
|
|
||||||
this.innerHTML = DATE_TEMPLATE;
|
this.innerText = "";
|
||||||
|
this.insertAdjacentHTML(
|
||||||
|
"beforeend",
|
||||||
|
`
|
||||||
|
<svg class="fa d-icon d-icon-globe-americas svg-icon" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<use xlink:href="#globe-americas"></use>
|
||||||
|
</svg>
|
||||||
|
<span class="relative-time">${localDateBuilder.formated}</span>
|
||||||
|
`
|
||||||
|
);
|
||||||
this.setAttribute("aria-label", localDateBuilder.textPreview);
|
this.setAttribute("aria-label", localDateBuilder.textPreview);
|
||||||
this.dataset.htmlTooltip = previewsNode.outerHTML;
|
this.dataset.htmlTooltip = previewsNode.outerHTML;
|
||||||
this.classList.add("cooked-date");
|
|
||||||
|
const classes = ["cooked-date"];
|
||||||
if (localDateBuilder.pastEvent) {
|
if (localDateBuilder.pastEvent) {
|
||||||
this.classList.add("past");
|
classes.push("past");
|
||||||
}
|
}
|
||||||
const relativeTime = this.querySelector(".relative-time");
|
this.classList.add(...classes);
|
||||||
relativeTime.innerText = localDateBuilder.formated;
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import { parseBBCodeTag } from "pretty-text/engines/discourse-markdown/bbcode-block";
|
import { parseBBCodeTag } from "pretty-text/engines/discourse-markdown/bbcode-block";
|
||||||
|
|
||||||
|
const timezoneNames = moment.tz.names();
|
||||||
|
|
||||||
function addLocalDate(buffer, matches, state) {
|
function addLocalDate(buffer, matches, state) {
|
||||||
let token;
|
let token;
|
||||||
|
|
||||||
|
@ -77,7 +79,7 @@ function addLocalDate(buffer, matches, state) {
|
||||||
|
|
||||||
if (
|
if (
|
||||||
config.displayedTimezone &&
|
config.displayedTimezone &&
|
||||||
moment.tz.names().includes(config.displayedTimezone)
|
timezoneNames.includes(config.displayedTimezone)
|
||||||
) {
|
) {
|
||||||
token.attrs.push([
|
token.attrs.push([
|
||||||
"data-displayed-timezone",
|
"data-displayed-timezone",
|
||||||
|
@ -87,7 +89,7 @@ function addLocalDate(buffer, matches, state) {
|
||||||
|
|
||||||
if (config.timezones) {
|
if (config.timezones) {
|
||||||
const timezones = config.timezones.split("|").filter((timezone) => {
|
const timezones = config.timezones.split("|").filter((timezone) => {
|
||||||
return moment.tz.names().includes(timezone);
|
return timezoneNames.includes(timezone);
|
||||||
});
|
});
|
||||||
|
|
||||||
token.attrs.push([
|
token.attrs.push([
|
||||||
|
@ -96,7 +98,7 @@ function addLocalDate(buffer, matches, state) {
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.timezone && moment.tz.names().includes(config.timezone)) {
|
if (config.timezone && timezoneNames.includes(config.timezone)) {
|
||||||
token.attrs.push([
|
token.attrs.push([
|
||||||
"data-timezone",
|
"data-timezone",
|
||||||
state.md.utils.escapeHtml(config.timezone),
|
state.md.utils.escapeHtml(config.timezone),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user