add pref storage for space settings like grid

This commit is contained in:
Stewart Allen 2022-01-11 12:29:15 -05:00
commit cd7e3dde5a
3 changed files with 29 additions and 14 deletions

View file

@ -280,7 +280,13 @@ let tool = {
// persisted preference map
let prefs = {
map: {},
map: {
info: {},
space: {
grid: true,
dark: false
},
},
put(key, val) {
prefs.map[key] = val;
@ -288,14 +294,15 @@ let prefs = {
},
// persist to data store
update() {
save() {
mesh.db.admin.put("prefs", prefs.map);
},
// reload from data store
restore() {
mesh.db.admin.get("prefs").then(data => {
if (data) prefs.map = data;
load() {
return mesh.db.admin.get("prefs").then(data => {
// handle/ignore incompatible older prefs
if (data) Object.assign(prefs.map, data);
});
}
};
@ -328,11 +335,15 @@ let api = mesh.api = {
},
grid(state = {toggle:true}) {
let { platform } = space;
let { map, save } = prefs;
if (state.toggle) {
space.platform.showGrid(!space.platform.isGridVisible());
platform.showGrid(!platform.isGridVisible());
} else {
space.platform.showGrid(state);
platform.showGrid(state);
}
map.space.grid = platform.isGridVisible();
save();
},
wireframe(state = {toggle:true}, opt = {opacity:0.15}) {
@ -502,6 +513,6 @@ broker.wrapObject(model, 'model');
broker.wrapObject(group, 'group');
// optimize db writes by merging updates
prefs.update = util.deferWrap(prefs.update);
prefs.save = util.deferWrap(prefs.save);
})();

View file

@ -350,19 +350,20 @@ function ui_build() {
...h_ara,
...h_msh
]);
let prefs = api.prefs.map.info;
// map buttons to show/hide selection info
function toggle(label, h, s, d) {
s.onclick = () => {
s.style.display = 'none';
d.style.display = 'flex';
api.prefs.put(label, 1);
api.prefs.save( prefs[label] = 'show' ); // <- :)
};
h.onclick = () => {
d.style.display = 'none';
s.style.display = 'flex';
api.prefs.put(label, 2);
api.prefs.save( prefs[label] = 'hide' );
};
if (api.prefs.map[label] === 1) {
if (prefs[label] === 'show') {
s.onclick();
} else {
h.onclick();