]> BookStack Code Mirror - bookstack/blob - resources/js/markdown/settings.js
Merge pull request #5626 from BookStackApp/rubentalstra-development
[bookstack] / resources / js / markdown / settings.js
1 export class Settings {
2
3     constructor(settingInputs) {
4         this.settingMap = {
5             scrollSync: true,
6             showPreview: true,
7             editorWidth: 50,
8         };
9         this.changeListeners = {};
10         this.loadFromLocalStorage();
11         this.applyToInputs(settingInputs);
12         this.listenToInputChanges(settingInputs);
13     }
14
15     applyToInputs(inputs) {
16         for (const input of inputs) {
17             const name = input.getAttribute('name').replace('md-', '');
18             input.checked = this.settingMap[name];
19         }
20     }
21
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);
27             });
28         }
29     }
30
31     loadFromLocalStorage() {
32         const lsValString = window.localStorage.getItem('md-editor-settings');
33         if (!lsValString) {
34             return;
35         }
36
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;
41             }
42         }
43     }
44
45     set(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] || [])) {
49             listener(value);
50         }
51     }
52
53     get(key) {
54         return this.settingMap[key] || null;
55     }
56
57     onChange(key, callback) {
58         const listeners = this.changeListeners[key] || [];
59         listeners.push(callback);
60         this.changeListeners[key] = listeners;
61     }
62
63 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.