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

**URL:** https://meta.discourse.org/t/dont-decorate-cooked-items-in-composer-view/254415
**Category:** Development
**Created:** [2023年二月8日 03:07 UTC](https://meta.discourse.org/t/dont-decorate-cooked-items-in-composer-view/254415 "2023-02-08T03:07:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![asc](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/asc/32/288324_2.png) [@asc](https://meta.discourse.org/u/asc)
#### Post date: [2023年二月8日 03:07 UTC](https://meta.discourse.org/t/dont-decorate-cooked-items-in-composer-view/254415/1 "2023-02-08T03:07:39Z")

</div>

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

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

```plaintext
<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>

```

---

<div class="post-metadata">

### Author: ![Don](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/don/32/228726_2.png) [@Don](https://meta.discourse.org/u/Don)
#### Post date: [2023年二月8日 06:07 UTC](https://meta.discourse.org/t/dont-decorate-cooked-items-in-composer-view/254415/2 "2023-02-08T06:07:47Z")

</div>

你好，

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

> [@Johani](#):
>
> 你可以传递给该方法的选项之一是 `onlyStream`
> 
> 它有什么作用？它告诉该方法，你只希望这段代码在帖子在主题内渲染时触发。让我们添加那个选项。

---

<div class="post-metadata">

### Author: ![asc](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/asc/32/288324_2.png) [@asc](https://meta.discourse.org/u/asc)
#### Post date: [2023年二月8日 14:37 UTC](https://meta.discourse.org/t/dont-decorate-cooked-items-in-composer-view/254415/4 "2023-02-08T14:37:19Z")

</div>

好的，谢谢链接！

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2023年三月10日 14:38 UTC](https://meta.discourse.org/t/dont-decorate-cooked-items-in-composer-view/254415/5 "2023-03-10T14:38:01Z")

</div>

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