组合视图中不要装饰已烹饪的物品?

我正在开发一个主题组件,用于阻止包含用户在其自定义用户字段中指定的关键字的帖子。

我已经实现了阻止/显示功能,但它也会在键入时装饰撰写器视图的预览窗格。有没有办法阻止 api.decorateCookedElement 在撰写器预览上触发?下面是我目前为止的工作:

<script type="text/discourse-plugin" version="0.8">
let currentUser = api.getCurrentUser();

if (currentUser) {
    api.container.lookup('store:main').find('user', currentUser.username).then((user) => {
        let blocklist = user.user_fields[1];

        const blocked = blocklist.split(",").map(function(item) {
            return item.trim();
        });
        
        api.decorateCookedElement(
            elem => {
                let textContent = elem.textContent;
                let result = "";
                if (blocked.find(text => textContent.includes(text))) {
                    const found = blocked.filter(text => textContent.includes(text));
                    if (found.length >= 2) {
                        const last = found.pop();
                        result = found.join(', ') + ' and ' + last;
                    } else {
                        result = found.join(', ');
                    }
                    const children = elem.childNodes;
                    const newNode = document.createElement("a");
                    const textNode = document.createTextNode("Blocked for containing " + result + ".");
                    newNode.classList.add("block-notice")
                    newNode.appendChild(textNode);
                    newNode.onclick = function() { showComment(this); };
                    
                    elem.insertBefore(newNode, elem.children[0]);
                    for (let index = 0; index < children.length; ++index) {
                        children[index].classList.add("blocker-blocked")
                    }
                    newNode.classList.remove("blocker-blocked");
                }
            },
            {"id": "blocker-blocked"}
        );
        
    });
}

function showComment(elem) {
    const cooked = elem.parentNode;
    elem.remove();
    const children = cooked.childNodes;
    for (let index = 0; index < children.length; ++index) {
        children[index].classList.remove("blocker-blocked")
    }
}
</script>

你好,

添加 onlyStream 就可以做到。这是来自另一个主题的一个例子。:slightly_smiling_face:

3 个赞

好的,谢谢链接!

1 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.