我有一个主题组件,用于不在搜索中显示已静音的标签和类别,但我禁用它然后重新启用它来测试其他东西,现在它不起作用,包括 console.log。我无法让 api.decoratePluginOutlet 做任何事情,即使是在基于 plugin-api.js 示例的新干净主题组件中。
A. 是否有什么技巧可以使 decoratePluginOutlet 生效,以及 B. 是否有更好的方法来仅将一个类添加到搜索结果列表项?我现在使用 decoratePluginOutlet 来获取元素并遍历层级来查找然后添加类别,但这感觉很混乱。完整的代码如下。
<script type="text/discourse-plugin" version="0.8">
const discourseComputed = require("discourse-common/utils/decorators").default;
let currentUser = api.getCurrentUser();
api.decoratePluginOutlet(
"full-page-search-category",
(elem, args) => {
console.log(currentUser)
let searchCategory = elem.parentElement.parentElement
let fpsTopic = searchCategory.parentElement.parentElement
let category_id = searchCategory.children[0].getAttribute('href').split("/").slice(-1);
let search_children = searchCategory.children
let searchArray = Array.from(search_children)
let tags = searchArray.filter(item => item.classList.contains("discourse-tags"))
if (tags[0]) {
let tagList = Array.from(tags[0].children).map(x => x.innerText)
for (let index = 0; index < tagList.length; ++index) {
fpsTopic.classList.add("tagged_" + tagList[index])
}
console.log()
}
fpsTopic.classList.add("category_" + category_id)
if (currentUser) {
const muted_categories = currentUser.muted_category_ids
const muted_tags = currentUser.muted_tags
if (muted_categories) {
if (muted_categories.includes(parseInt(category_id))) {
const children = fpsTopic.childNodes;
const newNode = document.createElement("a");
const textNode = document.createTextNode("This result is from a category you have muted.");
newNode.classList.add("block-notice")
newNode.appendChild(textNode);
newNode.onclick = function() { showComment(this); };
fpsTopic.insertBefore(newNode, fpsTopic.children[0]);
fpsTopic.classList.add("blocker-blocked");
newNode.classList.remove("blocker-blocked");
}
}
if (muted_tags && tags[0]) {
let tagList = Array.from(tags[0].children).map(x => x.innerText)
if (muted_tags.some(v => tagList.includes(v))) {
const children = fpsTopic.childNodes;
const newNode = document.createElement("a");
const textNode = document.createTextNode("This result is from a tag you have muted.");
newNode.classList.add("block-notice")
newNode.appendChild(textNode);
newNode.onclick = function() { showComment(this); };
fpsTopic.insertBefore(newNode, fpsTopic.children[0]);
fpsTopic.classList.add("blocker-blocked");
newNode.classList.remove("blocker-blocked");
}
}
}
}
);
function showComment(elem) {
const cooked = elem.parentNode;
elem.remove();
cooked.classList.remove("blocker-blocked");
}
</script>