Sto lavorando a un componente tematico per bloccare i post che contengono parole chiave specificate dall’utente in un campo utente personalizzato.
Ho la funzionalità di blocco/visualizzazione funzionante, ma decora anche il riquadro di anteprima della vista del compositore durante la digitazione. Esiste un modo per impedire che api.decorateCookedElement venga eseguito sull’anteprima del compositore? Di seguito è riportato ciò che ho funzionante finora.
<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("Bloccato per contenere " + 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>