]> BookStack Code Mirror - bookstack/blob - resources/js/markdown/settings.js
6dd1422108a0e30e81fcf132f33ab2766dbafa59
[bookstack] / resources / js / markdown / settings.js
1 import {kebabToCamel} from "../services/text";
2
3
4 export class Settings {
5
6     constructor(initialSettings) {
7         this.settingMap = {};
8         this.changeListeners = {};
9         this.merge(initialSettings);
10     }
11
12     set(key, value) {
13         key = this.normaliseKey(key);
14         this.settingMap[key] = value;
15         for (const listener of (this.changeListeners[key] || [])) {
16             listener(value);
17         }
18     }
19
20     get(key) {
21         return this.settingMap[this.normaliseKey(key)] || null;
22     }
23
24     merge(settings) {
25         for (const [key, value] of Object.entries(settings)) {
26             this.set(key, value);
27         }
28     }
29
30     onChange(key, callback) {
31         key = this.normaliseKey(key);
32         const listeners = this.changeListeners[this.normaliseKey(key)] || [];
33         listeners.push(callback);
34         this.changeListeners[this.normaliseKey(key)] = listeners;
35     }
36
37     normaliseKey(key) {
38         return kebabToCamel(key.replace('md-', ''));
39     }
40 }