2023-10-24 03:50:43 +08:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
|
|
import {
|
|
|
|
addBlockDecorateCallback,
|
|
|
|
addTagDecorateCallback,
|
|
|
|
} from "discourse/lib/to-markdown";
|
2023-10-24 05:14:58 +08:00
|
|
|
import applySpoiler from "discourse/plugins/spoiler-alert/lib/apply-spoiler";
|
2023-10-24 03:50:43 +08:00
|
|
|
|
|
|
|
function spoil(element) {
|
|
|
|
element.querySelectorAll(".spoiler").forEach((spoiler) => {
|
|
|
|
spoiler.classList.remove("spoiler");
|
|
|
|
spoiler.classList.add("spoiled");
|
|
|
|
applySpoiler(spoiler);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initializeSpoiler(api) {
|
|
|
|
api.decorateCookedElement(spoil, { id: "spoiler-alert" });
|
|
|
|
|
|
|
|
api.addComposerToolbarPopupMenuOption({
|
|
|
|
icon: "magic",
|
|
|
|
label: "spoiler.title",
|
|
|
|
action: (toolbarEvent) => {
|
|
|
|
toolbarEvent.applySurround("[spoiler]", "[/spoiler]", "spoiler_text", {
|
|
|
|
multiline: false,
|
|
|
|
useBlockMode: true,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
addTagDecorateCallback(function () {
|
2024-05-29 00:07:42 +08:00
|
|
|
const { attributes } = this.element;
|
|
|
|
|
|
|
|
if (/\bspoiled\b/.test(attributes.class)) {
|
2023-10-24 03:50:43 +08:00
|
|
|
this.prefix = "[spoiler]";
|
|
|
|
this.suffix = "[/spoiler]";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
addBlockDecorateCallback(function (text) {
|
|
|
|
const { name, attributes } = this.element;
|
|
|
|
|
2024-05-29 00:07:42 +08:00
|
|
|
if (name === "div" && /\bspoiled\b/.test(attributes.class)) {
|
|
|
|
this.prefix = "[spoiler]\n";
|
|
|
|
this.suffix = "\n[/spoiler]";
|
2023-10-24 03:50:43 +08:00
|
|
|
return text.trim();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: "spoiler-alert",
|
|
|
|
|
|
|
|
initialize(container) {
|
2024-01-24 22:59:44 +08:00
|
|
|
const siteSettings = container.lookup("service:site-settings");
|
2023-10-24 03:50:43 +08:00
|
|
|
|
|
|
|
if (siteSettings.spoiler_enabled) {
|
|
|
|
withPluginApi("1.15.0", initializeSpoiler);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|