1 export class Settings {
3 constructor(settingInputs) {
9 this.changeListeners = {};
10 this.loadFromLocalStorage();
11 this.applyToInputs(settingInputs);
12 this.listenToInputChanges(settingInputs);
15 applyToInputs(inputs) {
16 for (const input of inputs) {
17 const name = input.getAttribute('name').replace('md-', '');
18 input.checked = this.settingMap[name];
22 listenToInputChanges(inputs) {
23 for (const input of inputs) {
24 input.addEventListener('change', () => {
25 const name = input.getAttribute('name').replace('md-', '');
26 this.set(name, input.checked);
31 loadFromLocalStorage() {
32 const lsValString = window.localStorage.getItem('md-editor-settings');
37 const lsVals = JSON.parse(lsValString);
38 for (const [key, value] of Object.entries(lsVals)) {
39 if (value !== null && this.settingMap[key] !== undefined) {
40 this.settingMap[key] = value;
46 this.settingMap[key] = value;
47 window.localStorage.setItem('md-editor-settings', JSON.stringify(this.settingMap));
48 for (const listener of (this.changeListeners[key] || [])) {
54 return this.settingMap[key] || null;
57 onChange(key, callback) {
58 const listeners = this.changeListeners[key] || [];
59 listeners.push(callback);
60 this.changeListeners[key] = listeners;