2022-01-18 09:18:21 -05:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
2023-11-19 16:43:47 -05:00
|
|
|
import I18n from "I18n";
|
2022-01-18 09:18:21 -05:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: "disco-toc-composer",
|
|
|
|
|
|
|
|
initialize() {
|
2023-10-16 03:23:04 -04:00
|
|
|
withPluginApi("1.0.0", (api) => {
|
2022-01-18 09:18:21 -05:00
|
|
|
const currentUser = api.getCurrentUser();
|
|
|
|
if (!currentUser) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const minimumTL = settings.minimum_trust_level_to_create_TOC;
|
|
|
|
|
2024-03-26 09:48:08 -04:00
|
|
|
if (currentUser.trust_level >= minimumTL || currentUser.staff) {
|
2022-01-18 09:18:21 -05:00
|
|
|
if (!I18n.translations[I18n.currentLocale()].js.composer) {
|
|
|
|
I18n.translations[I18n.currentLocale()].js.composer = {};
|
|
|
|
}
|
|
|
|
I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = " ";
|
|
|
|
|
2023-10-22 20:09:06 -04:00
|
|
|
api.addComposerToolbarPopupMenuOption({
|
|
|
|
action: (toolbarEvent) => {
|
|
|
|
toolbarEvent.applySurround(
|
|
|
|
`<div data-theme-toc="true">`,
|
|
|
|
`</div>`,
|
|
|
|
"contains_dtoc"
|
|
|
|
);
|
|
|
|
},
|
|
|
|
icon: "align-left",
|
|
|
|
label: themePrefix("insert_table_of_contents"),
|
|
|
|
condition: (composer) => {
|
2024-08-07 03:40:11 -04:00
|
|
|
return (
|
|
|
|
settings.enable_TOC_for_replies || composer.model.topicFirstPost
|
|
|
|
);
|
2022-01-18 09:18:21 -05:00
|
|
|
},
|
2023-10-16 03:23:04 -04:00
|
|
|
});
|
2024-08-07 03:40:11 -04:00
|
|
|
|
|
|
|
if (settings.enable_TOC_for_replies) {
|
|
|
|
document.body.classList.add("toc-for-replies-enabled");
|
|
|
|
}
|
2022-01-18 09:18:21 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|