Alla fine della giornata ho bisogno di un pulsante “nuovo argomento” nel corpo dell’elenco degli argomenti nel tema centrale di Discourse. Ogni ingegnere si lamenta del fatto che non gli piace il pulsante “avvia nuova conversazione” posizionato solo in fondo all’elenco degli argomenti all’interno di ciascuna categoria. Non vogliono usare la scelta del menu “nuovo argomento” in alto perché ciò non avvia l’argomento nella categoria in cui si trovano attualmente.
Ho eseguito questo tramite llama 3.1 405B e gpt4 tramite ask.discourse.com, ma non riesco a ottenere una versione di JavaScript che compili. Ricevo continuamente un errore di compilazione: SyntaxError: Private field must be used in an enclosing class.
Ricevo anche un errore nel registro dei widget, ma ciò è dovuto al fatto che il widget personalizzato non viene mai creato a causa dell’errore di compilazione JS.
Sto usando il tema Discourse Central.
Dato questo blocco Header:
"<script type=\"text/discourse-plugin\" version=\"0.8.18\">
api.createWidget('custom-new-topic-button', {
tagName: 'div.custom-new-topic-button',
buildKey: () => `custom-new-topic-button`,
html() {
return [
this.attach('button', {
className: 'btn btn-primary',
action: 'createNewTopic',
contents: 'New Topic'
})
];
},
click() {
const composerController = this.container.lookup("controller:composer");
const currentCategory = this.container.lookup("controller:navigation/category").get("model.id");
composerController.open({
action: require("discourse/models/composer").default.CREATE_TOPIC,
draftKey: require("discourse/models/composer").default.DRAFT,
categoryId: currentCategory,
});
return false;
},
});
api.decorateWidget('topic-list:before', (helper) => {
if (api.getCurrentUser()) {
helper.appendChild(helper.createWidget('custom-new-topic-button'));
}
});
</script>"
con questo blocco Head:
"<script>
{{#if currentUser}}
<div>class="topic-list-body-new-topic">
{{custom-new-topic-button}}
</div>
{{/if}}
</script>"
e questo CSS:
".topic-list-body::before {
content: "";
display: block;
position: relative; /* Important to allow absolute positioning within */
}
.topic-list-body-new-topic {
position: absolute;
top: 0;
left: 0;
padding: 10px;
background: #f2f3f5;
border-bottom: 1px solid #ccc;
}
.custom-new-topic-button .btn.btn-primary {
background-color: #007bff;
border-color: #007bff;
color: #fff;
}"
Questo era il mio codice originale, la raccomandazione più recente è:
<script type="text/discourse-plugin" version="0.8.18">
api.createWidget('custom-new-topic-button', {
tagName: 'div.custom-new-topic-button',
buildKey() {
return 'custom-new-topic-button';
},
defaultState() {
return {};
},
html() {
return [
this.attach('button', {
className: 'btn btn-primary',
action: 'createNewTopic',
contents: 'New Topic'
})
];
},
createNewTopic() {
const composerController = this.container.lookup("controller:composer");
const currentCategory = this.container.lookup("controller:navigation/category").get("model.id");
const Composer = require("discourse/models/composer").default;
composerController.open({
action: Composer.CREATE_TOPIC,
draftKey: Composer.DRAFT,
categoryId: currentCategory,
});
return false;
},
});
api.decorateWidget('topic-list:before', (helper) => {
if (api.getCurrentUser()) {
helper.appendChild(helper.createWidget('custom-new-topic-button'));
}
});
</script>

