Hello Guys, like the title says, I can’t get my head around this, I have about 30 or more checkboxes in a plugin, I am trying to save the state of all checkboxes when a user chooses.
Right now I am using this code, but it does not work. When I reload or close photoshop, the previous state gets lost.
You could save the settings to a JSON file which you read on plugin load.
Check this repository for a basic example. Look at /lib/settings.js in particular.
The demo is saving different data (persistent tokens), but the principal is the same.
If you, like me, are learning javascript to be able to develop UXP plugins, I advise you to do a search on the internet, there is a lot of free content.
Here a working example based on your question!
html:
restoreCheckboxes ();
document.querySelector("#salvar").addEventListener("click", async evt => {
savefunction()
})
function savefunction(){
var checkboxes = document.querySelectorAll('.checks');
var checked = {};
for (var i = 0; i < checkboxes.length; i ++) {
if (checkboxes[i].checked) {
checked[checkboxes[i].id] = checkboxes[i].value;
}
}
localStorage.setItem('checked_boxes', JSON.stringify(checked));
console.log(checked);
}
function restoreCheckboxes () {
var checkbox = document.querySelectorAll('.checks');
var checkboxStates = localStorage.getItem('checked_boxes');
if (checkboxStates) {
checkboxStates = JSON.parse(checkboxStates); // <-- parse string to object
for (i = 0; i < checkbox.length; i ++) {
if (checkboxStates.hasOwnProperty(checkbox[i].id)) {
checkbox[i].checked = true;
}
}
}
}
Hi, localStorage should work fine, just make sure to use the checked property in a <sp-checkbox> (not the value).
Also, be aware that if you store the boolean true in localStorage, you’re gonna get the "true" string back—so you’ll need to take that into account and do something like: