mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-30 04:23:11 +03:00
Connected md editor settings to logic for functionality
This commit is contained in:
40
resources/js/markdown/settings.js
Normal file
40
resources/js/markdown/settings.js
Normal file
@ -0,0 +1,40 @@
|
||||
import {kebabToCamel} from "../services/text";
|
||||
|
||||
|
||||
export class Settings {
|
||||
|
||||
constructor(initialSettings) {
|
||||
this.settingMap = {};
|
||||
this.changeListeners = {};
|
||||
this.merge(initialSettings);
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
key = this.normaliseKey(key);
|
||||
this.settingMap[key] = value;
|
||||
for (const listener of (this.changeListeners[key] || [])) {
|
||||
listener(value);
|
||||
}
|
||||
}
|
||||
|
||||
get(key) {
|
||||
return this.settingMap[this.normaliseKey(key)] || null;
|
||||
}
|
||||
|
||||
merge(settings) {
|
||||
for (const [key, value] of Object.entries(settings)) {
|
||||
this.set(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
onChange(key, callback) {
|
||||
key = this.normaliseKey(key);
|
||||
const listeners = this.changeListeners[this.normaliseKey(key)] || [];
|
||||
listeners.push(callback);
|
||||
this.changeListeners[this.normaliseKey(key)] = listeners;
|
||||
}
|
||||
|
||||
normaliseKey(key) {
|
||||
return kebabToCamel(key.replace('md-', ''));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user