Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
DEV: Switch to new addComposerToolbarPopupMenuOption plugin API (#113)
Browse files Browse the repository at this point in the history
Why this change?

`api.addToolbarPopupMenuOptionsCallback` has been deprecated in discourse/discourse@913fd3a
  • Loading branch information
tgxworld authored Oct 23, 2023
1 parent 09b4e50 commit 49f86ba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
1 change: 1 addition & 0 deletions .discourse-compatibility
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
< 3.2.0.beta2: 09b4e50441d4e30d2e3aec14f862fa2cede644e1
3.1.999: 65989714af08eda44196cca3a0afe85c9e9443f9
2.9.0.beta3: c65972911f7ab3323dfe3de4a727a9cb2961b57b
32 changes: 11 additions & 21 deletions assets/javascripts/initializers/spoiler-alert.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { withPluginApi } from "discourse/lib/plugin-api";
import ComposerController from "discourse/controllers/composer";
import applySpoiler from "discourse/plugins/discourse-spoiler-alert/lib/apply-spoiler";
import {
addBlockDecorateCallback,
addTagDecorateCallback,
} from "discourse/lib/to-markdown";
import applySpoiler from "discourse/plugins/discourse-spoiler-alert/lib/apply-spoiler";

function spoil(element) {
element.querySelectorAll(".spoiler").forEach((spoiler) => {
Expand All @@ -17,24 +16,14 @@ function spoil(element) {
export function initializeSpoiler(api) {
api.decorateCookedElement(spoil, { id: "spoiler-alert" });

api.addToolbarPopupMenuOptionsCallback(() => {
return {
action: "insertSpoiler",
icon: "magic",
label: "spoiler.title",
};
});

ComposerController.reopen({
actions: {
insertSpoiler() {
this.get("toolbarEvent").applySurround(
"[spoiler]",
"[/spoiler]",
"spoiler_text",
{ multiline: false, useBlockMode: true }
);
},
api.addComposerToolbarPopupMenuOption({
icon: "magic",
label: "spoiler.title",
action: (toolbarEvent) => {
toolbarEvent.applySurround("[spoiler]", "[/spoiler]", "spoiler_text", {
multiline: false,
useBlockMode: true,
});
},
});

Expand All @@ -61,8 +50,9 @@ export default {

initialize(container) {
const siteSettings = container.lookup("site-settings:main");

if (siteSettings.spoiler_enabled) {
withPluginApi("1.3.0", initializeSpoiler);
withPluginApi("1.15.0", initializeSpoiler);
}
},
};
20 changes: 8 additions & 12 deletions test/javascripts/acceptance/spoiler-button-test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { click, fillIn, visit } from "@ember/test-helpers";
import { test } from "qunit";
import {
acceptance,
exists,
query,
} from "discourse/tests/helpers/qunit-helpers";
import { test } from "qunit";
import { click, fillIn, visit } from "@ember/test-helpers";
import I18n from "I18n";
import selectKit from "discourse/tests/helpers/select-kit-helper";
import { clearPopupMenuOptionsCallback } from "discourse/controllers/composer";
import I18n from "discourse-i18n";

acceptance("Spoiler Button", function (needs) {
needs.user();
needs.settings({ spoiler_enabled: true });
needs.hooks.beforeEach(() => {
clearPopupMenuOptionsCallback();
});

test("spoiler button", async (assert) => {
const popUpMenu = selectKit(".toolbar-popup-menu-options");
Expand All @@ -28,7 +24,7 @@ acceptance("Spoiler Button", function (needs) {
await categoryChooser.expand();
await categoryChooser.selectRowByValue(2);
await popUpMenu.expand();
await popUpMenu.selectRowByValue("insertSpoiler");
await popUpMenu.selectRowByName(I18n.t("spoiler.title"));

assert.strictEqual(
query(".d-editor-input").value,
Expand All @@ -54,7 +50,7 @@ acceptance("Spoiler Button", function (needs) {
textarea.selectionEnd = textarea.value.length;

await popUpMenu.expand();
await popUpMenu.selectRowByValue("insertSpoiler");
await popUpMenu.selectRowByName(I18n.t("spoiler.title"));

assert.strictEqual(
query(".d-editor-input").value,
Expand All @@ -79,7 +75,7 @@ acceptance("Spoiler Button", function (needs) {
textarea.selectionEnd = 21;

await popUpMenu.expand();
await popUpMenu.selectRowByValue("insertSpoiler");
await popUpMenu.selectRowByName(I18n.t("spoiler.title"));

assert.strictEqual(
query(".d-editor-input").value,
Expand All @@ -104,7 +100,7 @@ acceptance("Spoiler Button", function (needs) {
textarea.selectionEnd = 21;

await popUpMenu.expand();
await popUpMenu.selectRowByValue("insertSpoiler");
await popUpMenu.selectRowByName(I18n.t("spoiler.title"));

assert.strictEqual(
query(".d-editor-input").value,
Expand All @@ -130,7 +126,7 @@ acceptance("Spoiler Button", function (needs) {
textarea.selectionEnd = 22;

await popUpMenu.expand();
await popUpMenu.selectRowByValue("insertSpoiler");
await popUpMenu.selectRowByName(I18n.t("spoiler.title"));

assert.strictEqual(
query(".d-editor-input").value,
Expand Down

0 comments on commit 49f86ba

Please sign in to comment.