Skip to content

Commit

Permalink
DEV: Switch theme to use new addComposerToolbarPopupMenuOption api (#…
Browse files Browse the repository at this point in the history
…61)

Why this change?

The new API is better and allows us to avoid modifying the composer
service.
  • Loading branch information
tgxworld committed Oct 6, 2023
1 parent 566d79f commit d59dc7c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
1 change: 1 addition & 0 deletions .discourse-compatibility
@@ -1,3 +1,4 @@
< 3.2.0.beta2-dev: 566d79fa39eacfa299a7e60afe5b8d75020f7f7c
3.1.999: 323bd485b08889360edcae826d6272fd8e77d180
2.7.13: 5b2f5a455e1adf8ce5e8c1cfb7fbc3c388d3d82a
2.6.0.beta3: 68d40fe9f5b625cf465adc31b502a54e16d02cc6
34 changes: 13 additions & 21 deletions javascripts/discourse/initializers/disco-toc-composer.js
Expand Up @@ -5,7 +5,7 @@ export default {
name: "disco-toc-composer",

initialize() {
withPluginApi("1.0.0", (api) => {
withPluginApi("1.14.0", (api) => {
const currentUser = api.getCurrentUser();
if (!currentUser) {
return;
Expand All @@ -19,27 +19,19 @@ export default {
}
I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = " ";

api.modifyClass("controller:composer", {
pluginId: "DiscoTOC",

actions: {
insertDtoc() {
this.get("toolbarEvent").applySurround(
`<div data-theme-toc="true">`,
`</div>`,
"contains_dtoc"
);
},
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) => {
return composer.model.topicFirstPost;
},
});

api.addToolbarPopupMenuOptionsCallback((controller) => {
return {
action: "insertDtoc",
icon: "align-left",
label: themePrefix("insert_table_of_contents"),
condition: controller.get("model.topicFirstPost"),
};
});
}
});
Expand Down
17 changes: 14 additions & 3 deletions test/acceptance/toc-composer-test.js
@@ -1,3 +1,4 @@
import I18n from "I18n";
import {
acceptance,
exists,
Expand All @@ -19,7 +20,10 @@ acceptance("DiscoTOC - Composer", function (needs) {
await click("#create-topic");
const toolbarPopupMenu = selectKit(".toolbar-popup-menu-options");
await toolbarPopupMenu.expand();
await toolbarPopupMenu.selectRowByValue("insertDtoc");

await toolbarPopupMenu.selectRowByName(
I18n.t(themePrefix("insert_table_of_contents"))
);

assert.ok(query(".d-editor-input").value.includes('data-theme-toc="true"'));
});
Expand All @@ -33,7 +37,10 @@ acceptance("DiscoTOC - Composer", function (needs) {

const toolbarPopupMenu = selectKit(".toolbar-popup-menu-options");
await toolbarPopupMenu.expand();
await toolbarPopupMenu.selectRowByValue("insertDtoc");

await toolbarPopupMenu.selectRowByName(
I18n.t(themePrefix("insert_table_of_contents"))
);

assert.ok(query(".d-editor-input").value.includes('data-theme-toc="true"'));
});
Expand All @@ -44,6 +51,10 @@ acceptance("DiscoTOC - Composer", function (needs) {
const toolbarPopupMenu = selectKit(".toolbar-popup-menu-options");
await toolbarPopupMenu.expand();

assert.notOk(toolbarPopupMenu.rowByValue("insertDtoc").exists());
assert.notOk(
toolbarPopupMenu
.rowByName(I18n.t(themePrefix("insert_table_of_contents")))
.exists()
);
});
});

0 comments on commit d59dc7c

Please sign in to comment.