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