검색 결과에서 음소거된 태그와 카테고리를 표시하지 않도록 하는 테마 컴포넌트가 있습니다. 그런데 다른 것을 테스트하려고 이 컴포넌트를 비활성화했다가 다시 활성화했는데, 이제 동작하지 않습니다. console.log조차 출력되지 않습니다. plugin-api.js의 예시를 기반으로 새로 만든 깨끗한 테마 컴포넌트에서도 api.decoratePluginOutlet이 전혀 동작하지 않습니다.
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>