Export and import state actions implemented
parent
463706be5e
commit
b51580fe6a
|
|
@ -105,13 +105,7 @@ state.core = (function () {
|
||||||
function loadUI() {
|
function loadUI() {
|
||||||
let resetBtn = document.createElement("button");
|
let resetBtn = document.createElement("button");
|
||||||
resetBtn.innerHTML = "*️⃣";
|
resetBtn.innerHTML = "*️⃣";
|
||||||
resetBtn.addEventListener('click', function () {
|
resetBtn.addEventListener('click', actions.resetAll);
|
||||||
let confirmed = confirm('Reset all state values?');
|
|
||||||
if (confirmed) {
|
|
||||||
store.clearAll();
|
|
||||||
alert('All state values deleted!');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
let quickSettings = gradioApp().getElementById("quicksettings");
|
let quickSettings = gradioApp().getElementById("quicksettings");
|
||||||
resetBtn.className = quickSettings.querySelector('button').className;
|
resetBtn.className = quickSettings.querySelector('button').className;
|
||||||
quickSettings.appendChild(resetBtn);
|
quickSettings.appendChild(resetBtn);
|
||||||
|
|
@ -268,8 +262,43 @@ state.core = (function () {
|
||||||
'marginTop': '20px',
|
'marginTop': '20px',
|
||||||
'marginBottom': '10px'
|
'marginBottom': '10px'
|
||||||
});
|
});
|
||||||
|
buttonsContainer.appendChild(state.utils.html.create('hr'));
|
||||||
|
buttonsContainer.appendChild(state.utils.html.create('div', { innerHTML: 'Actions' }, { marginBottom: '10px' }));
|
||||||
|
buttonsContainer.appendChild(state.utils.html.createButton('Reset All', actions.resetAll));
|
||||||
|
buttonsContainer.appendChild(state.utils.html.createButton('Export State', actions.exportState));
|
||||||
|
buttonsContainer.appendChild(state.utils.html.createButton('Import State', actions.importState));
|
||||||
|
buttonsContainer.appendChild(state.utils.html.create('input', {
|
||||||
|
id: 'state-import-file', type: 'file', accept: 'application/json'
|
||||||
|
}));
|
||||||
page.appendChild(buttonsContainer);
|
page.appendChild(buttonsContainer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let actions = {
|
||||||
|
resetAll: function () {
|
||||||
|
let confirmed = confirm('Reset all state values?');
|
||||||
|
if (confirmed) {
|
||||||
|
store.clearAll();
|
||||||
|
alert('All state values deleted!');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
exportState: function () {
|
||||||
|
state.utils.saveFile('sd-webui-state', store.getAll());
|
||||||
|
},
|
||||||
|
importState: function () {
|
||||||
|
const fileInput = gradioApp().getElementById('state-import-file');
|
||||||
|
if (! fileInput.files || ! fileInput.files[0]) {
|
||||||
|
alert('Please select a JSON file!');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const file = fileInput.files[0];
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = function (event) {
|
||||||
|
store.load(JSON.parse(event.target.result));
|
||||||
|
window.location.reload();
|
||||||
|
};
|
||||||
|
reader.readAsText(file);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return { init };
|
return { init };
|
||||||
}());
|
}());
|
||||||
|
|
|
||||||
|
|
@ -5,19 +5,23 @@ state.Store = function Store (prefix) {
|
||||||
this.prefix = state.constants.LS_PREFIX + (prefix ? prefix + '-' : '');
|
this.prefix = state.constants.LS_PREFIX + (prefix ? prefix + '-' : '');
|
||||||
}
|
}
|
||||||
|
|
||||||
state.Store.prototype.set = function(key, value) {
|
state.Store.prototype.set = function (key, value) {
|
||||||
localStorage.setItem(this.prefix + key, value);
|
if (key.startsWith(this.prefix)) {
|
||||||
|
localStorage.setItem(key, value);
|
||||||
|
} else {
|
||||||
|
localStorage.setItem(this.prefix + key, value);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
state.Store.prototype.get = function(key) {
|
state.Store.prototype.get = function (key) {
|
||||||
return localStorage.getItem(this.prefix + key);
|
return localStorage.getItem(this.prefix + key);
|
||||||
};
|
};
|
||||||
|
|
||||||
state.Store.prototype.remove = function(key) {
|
state.Store.prototype.remove = function (key) {
|
||||||
localStorage.removeItem(this.prefix + key);
|
localStorage.removeItem(this.prefix + key);
|
||||||
};
|
};
|
||||||
|
|
||||||
state.Store.prototype.clear = function() {
|
state.Store.prototype.clear = function () {
|
||||||
localStorage.clear();
|
localStorage.clear();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -29,3 +33,24 @@ state.Store.prototype.clearAll = function () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
state.Store.prototype.getAll = function () {
|
||||||
|
let result = {};
|
||||||
|
let keys = Object.keys(localStorage);
|
||||||
|
for (let i = 0; i < keys.length; i++) {
|
||||||
|
if (keys[i].startsWith(state.constants.LS_PREFIX)) {
|
||||||
|
result[keys[i]] = localStorage[keys[i]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
state.Store.prototype.load = function (json) {
|
||||||
|
this.clearAll();
|
||||||
|
let keys = Object.keys(json);
|
||||||
|
for (let i = 0; i < keys.length; i++) {
|
||||||
|
if (keys[i].startsWith(state.constants.LS_PREFIX)) {
|
||||||
|
this.set(keys[i], json[keys[i]]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -143,6 +143,17 @@ state.utils = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
saveFile: function saveJSON(fileName ,data) {
|
||||||
|
const json = JSON.stringify(data, null, 4);
|
||||||
|
const blob = new Blob([json], {type: 'application/json'});
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
const link = document.createElement('a');
|
||||||
|
link.href = url;
|
||||||
|
link.download = fileName + '.json';
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
link.parentNode.removeChild(link);
|
||||||
|
},
|
||||||
debounce: function debounce(func, delay) {
|
debounce: function debounce(func, delay) {
|
||||||
let lastCallTime = 0;
|
let lastCallTime = 0;
|
||||||
return function() {
|
return function() {
|
||||||
|
|
@ -176,6 +187,20 @@ state.utils.html = {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
create: function create(type, props, style) {
|
||||||
|
const element = document.createElement(type);
|
||||||
|
if (props) {
|
||||||
|
for (let key in props) {
|
||||||
|
if (props.hasOwnProperty(key)) {
|
||||||
|
element[key] = props[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (style) {
|
||||||
|
this.setStyle(element, style);
|
||||||
|
}
|
||||||
|
return element;
|
||||||
|
},
|
||||||
createButton: function createButton(text, onclick) {
|
createButton: function createButton(text, onclick) {
|
||||||
const btn = document.createElement('button');
|
const btn = document.createElement('button');
|
||||||
btn.innerHTML = text;
|
btn.innerHTML = text;
|
||||||
|
|
|
||||||
12
style.css
12
style.css
|
|
@ -1,4 +1,5 @@
|
||||||
#settings_state_buttons button {
|
#settings_state_buttons button,
|
||||||
|
#settings_state_buttons input[type="file"] {
|
||||||
color: white;
|
color: white;
|
||||||
background: rgb(249, 115, 22);
|
background: rgb(249, 115, 22);
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
|
|
@ -8,3 +9,12 @@
|
||||||
margin-left: 5px;
|
margin-left: 5px;
|
||||||
margin-right: 5px;
|
margin-right: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#settings_state_buttons input[type="file"] {
|
||||||
|
padding: 3px 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#settings_state hr {
|
||||||
|
margin-top: 32px;
|
||||||
|
margin-bottom: 32px;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue