Ciao,
Sto cercando di aggiungere una nuova icona all’intestazione (per un selettore di tema chiaro/scuro) e mi sono ispirato a un altro commento qui, ma non funziona esattamente come previsto. L’icona scompare quando scorro verso il basso in una pagina di un post; potete aiutarmi a farla rimanere visibile per tutto il tempo?
Grazie mille ![]()
(Ho provato ad aggiungere la classe “keep”, ma è comunque scomparsa)
<script type="text/discourse-plugin" version="0.8">
var h = require('virtual-dom').h;
var ajax = require('discourse/lib/ajax').ajax;
var themeSelector = require('discourse/lib/theme-selector');
var light = 94;
var dark = 98;
const { iconNode } = require("discourse-common/lib/icon-library");
let icon = iconNode('adjust');
api.createWidget("header-theme-selector", {
tagName: "span.header-theme-selector",
buildKey: attrs => `header-theme-selector`,
click(event){
let $target = $(event.target);
let id = light;
let user = api.getCurrentUser();
if(user){
console.log(themeSelector.currentThemeId());
if (themeSelector.currentThemeId() == light) {
id = dark;
}
user.findDetails().then(user => {
seq = user.get("user_option.theme_key_seq");
this.setTheme(id, seq);
});
}else{
this.setTheme(id);
};
return true;
},
setTheme(themeId, seq = 0){
themeSelector.setLocalTheme([themeId], seq);
window.location.reload();
this.scheduleRerender();
},
html(attrs, state){
return [h('div', {attributes: {"class": "switcher keep"}}, icon)];
}
});
api.decorateWidget('header-buttons:after', (helper)=>{
const showExtraInfo = helper.attrs.topic;
if(!showExtraInfo) {
return [helper.widget.attach('header-theme-selector')];
}
});
</script>