[管理员通知] 主题“_Base”包含需要更新的代码。(ID:discourse.script-tag-discourse-plugin)(了解更多)
我在一个论坛上,通过将我的 JS 脚本移动到新的 JS 自定义选项卡,就解决了这个消息。
在我的另一个论坛上,它没有那么容易。
这是一个旧的覆盖,只在接受标签的类别中显示撰写器的标签字段:
<script type="text/discourse-plugin" version="1.4.0">
// show tag chooser in the composer only when necessary
const discourseComputed = require("discourse-common/utils/decorators").default;
const EmberObject = require("@ember/object").default;
function toggleTagChooser(display = "hide") {
if (display == "show") {
document.getElementsByClassName("mini-tag-chooser")[0].style.display = "block";
document.getElementsByClassName("reply-area")[0].classList.add("with-tags");
return;
}
// Verify the existence of the tag choser
let tagChoser = document.getElementsByClassName("mini-tag-chooser");
if(tagChoser.length > 0) {
tagChoser[0].style.display = "none";
document.getElementsByClassName("reply-area")[0].classList.remove("with-tags");
}
return;
}
api.modifyClass('controller:composer', {
pluginId: "toggleTagChoser",
@discourseComputed("model.category", "model.tags", "lastValidatedAt")
tagValidation(category, tags, lastValidatedAt) {
// custom code to toggle the tag choser
toggleTagChooser("hide");
if(category != null) {
if (
category.allow_global_tags == true ||
category.allowed_tag_groups.length > 0 ||
category.allowed_tags.length > 0
) {
toggleTagChooser("show");
}
}
// end of the custom code
const tagsArray = tags || [];
if (this.site.can_tag_topics && !this.currentUser.staff && category) {
// category.minimumRequiredTags incorporates both minimum_required_tags, and required_tag_groups
if (category.minimumRequiredTags > tagsArray.length) {
return EmberObject.create({
failed: true,
reason: I18n.t("composer.error.tags_missing", {
count: category.minimumRequiredTags,
}),
lastShownAt: lastValidatedAt,
});
}
}
}
});
</script>
这个脚本很大程度上基于 meta 上发布的一个类似(且很旧,2019 年或更早)的修改,但我再也找不到原始帖子了。
总之,有两个问题:
-
如何正确地将其移动到 JS 选项卡?有可能吗,还是我应该创建一个主题组件?
-
有没有更优雅的方法来隐藏/显示撰写器的标签选择器,仅在某些类别中显示?我的论坛只在一个类别中使用标签(一个广告类别,带有
#search和#offer标签)