Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persisting Settings Values #279

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { Events } from './events';
import { initFileHandler } from './file-handler';
import { initMaterials } from './material';
import { Persistence } from './persistence';
import { Scene } from './scene';
import { getSceneConfig } from './scene-config';
import { registerSelectionEvents } from './selection';
Expand Down Expand Up @@ -258,6 +259,8 @@
}
});
}

new Persistence(events);

Check warning on line 263 in src/main.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Do not use 'new' for side effects
};

export { main };
46 changes: 46 additions & 0 deletions src/persistence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Events } from './events';

const PERSISTED_EVENTS = [
'camera.setSplatSize',
'camera.setOverlay',
'camera.setMode',
'camera.setBound',
'camera.setFov',
'view.setBands',
'view.setOutlineSelection',
'grid.setVisible'
];

const STORAGEKEY_EVENTS = 'supersplat.events';

class Persistence {
events: Events;
settings: {[key: string]: any[]} = {};

constructor(events: Events) {
this.events = events;
this.loadSettings();

PERSISTED_EVENTS.forEach(evt => this.register(evt));
}

loadSettings() {

const storedString = localStorage.getItem(STORAGEKEY_EVENTS);
if (storedString) {
this.settings = JSON.parse(storedString);
Object.entries(this.settings).forEach(([eventName, args]) => {
this.events.fire(eventName, ...args);
});
}
}

register(eventName: string) {
this.events.on(eventName, (...args: any[]) => {
this.settings[eventName] = Array.from(args).filter(arg => arg);
localStorage.setItem(STORAGEKEY_EVENTS, JSON.stringify(this.settings));
});
}
}

export { Persistence };
Loading