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/app/initializers/codeblock-buttons.js
David Taylor 9534f13256
DEV: Replace site-settings:main with service:site-settings (#17734)
This will allow consumers to inject it using `siteSettings: service()` in preparation for the removal of implicit injections in Ember 4.0. `site-settings:main` is still available and will print a deprecation notice.
2022-08-01 09:43:33 +01:00

56 lines
1.3 KiB
JavaScript

import { withPluginApi } from "discourse/lib/plugin-api";
import { schedule } from "@ember/runloop";
import CodeblockButtons from "discourse/lib/codeblock-buttons";
let _codeblockButtons = [];
export default {
name: "codeblock-buttons",
initialize(container) {
const siteSettings = container.lookup("service:site-settings");
withPluginApi("0.8.7", (api) => {
function _cleanUp() {
_codeblockButtons.forEach((cb) => cb.cleanup());
_codeblockButtons.length = 0;
}
function _attachCommands(postElement, helper) {
if (!helper) {
return;
}
if (!siteSettings.show_copy_button_on_codeblocks) {
return;
}
const post = helper.getModel();
const cb = new CodeblockButtons({
showFullscreen: true,
showCopy: true,
});
cb.attachToPost(post, postElement);
_codeblockButtons.push(cb);
}
api.decorateCookedElement(
(postElement, helper) => {
// must be done after render so we can check the scroll width
// of the code blocks
schedule("afterRender", () => {
_attachCommands(postElement, helper);
});
},
{
onlyStream: true,
id: "codeblock-buttons",
}
);
api.cleanupStream(_cleanUp);
});
},
};