This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/initializers/post-decorations.js.es6
Robin Ward c322cccd53 FIX: Memory Leaks when decorating posts (#7749)
* Remove long-deprecated method

* FIX: Memory Leaks when decorating posts

Previously we'd keep creating mixins dynamically when decorating the
same class.

This code changes the API to recommend an `id` parameter for each
decorator which will avoid leaks. All plugins should be updated to
include this parameter, although if they don't in the meantime it'll
just mean a warning in the console (and a continued leak.)
2019-06-11 17:21:23 +02:00

41 lines
1.2 KiB
JavaScript

import highlightSyntax from "discourse/lib/highlight-syntax";
import lightbox from "discourse/lib/lightbox";
import { setupLazyLoading } from "discourse/lib/lazy-load-images";
import { setTextDirections } from "discourse/lib/text-direction";
import { withPluginApi } from "discourse/lib/plugin-api";
export default {
name: "post-decorations",
initialize(container) {
withPluginApi("0.1", api => {
const siteSettings = container.lookup("site-settings:main");
api.decorateCooked(highlightSyntax, {
id: "discourse-syntax-highlighting"
});
api.decorateCooked(lightbox, { id: "discourse-lightbox" });
if (siteSettings.support_mixed_text_direction) {
api.decorateCooked(setTextDirections, {
id: "discourse-text-direction"
});
}
setupLazyLoading(api);
api.decorateCooked(
$elem => {
const players = $("audio", $elem);
if (players.length) {
players.on("play", () => {
const postId = parseInt($elem.closest("article").data("post-id"));
if (postId) {
api.preventCloak(postId);
}
});
}
},
{ id: "discourse-audio" }
);
});
}
};