added commands support

This commit is contained in:
unknown 2021-08-08 21:56:34 +05:30
parent 0feb56cb3e
commit b552e2cda8
12 changed files with 682 additions and 34 deletions

View file

@ -22,9 +22,12 @@ const cons = {
LEAVE: 'LEAVE',
CREATE: 'CREATE',
error: {
CREATE: 'CREATE',
CREATE: 'ERROR_CREATE',
},
},
settings: {
TOGGLE_MARKDOWN: 'TOGGLE_MARKDOWN',
},
},
events: {
navigation: {
@ -57,6 +60,9 @@ const cons = {
FILE_UPLOAD_CANCELED: 'FILE_UPLOAD_CANCELED',
ATTACHMENT_CANCELED: 'ATTACHMENT_CANCELED',
},
settings: {
MARKDOWN_TOGGLED: 'MARKDOWN_TOGGLED',
},
},
};

View file

@ -1,15 +1,36 @@
class Settings {
import EventEmitter from 'events';
import appDispatcher from '../dispatcher';
import cons from './cons';
function getSettings() {
const settings = localStorage.getItem('settings');
if (settings === null) return null;
return JSON.parse(settings);
}
function setSettings(key, value) {
let settings = getSettings();
if (settings === null) settings = {};
settings[key] = value;
localStorage.setItem('settings', JSON.stringify(settings));
}
class Settings extends EventEmitter {
constructor() {
super();
this.themes = ['', 'silver-theme', 'dark-theme', 'butter-theme'];
this.themeIndex = this.getThemeIndex();
this.isMarkdown = this.getIsMarkdown();
}
getThemeIndex() {
if (typeof this.themeIndex === 'number') return this.themeIndex;
let settings = localStorage.getItem('settings');
const settings = getSettings();
if (settings === null) return 0;
settings = JSON.parse(settings);
if (typeof settings.themeIndex === 'undefined') return 0;
// eslint-disable-next-line radix
return parseInt(settings.themeIndex);
@ -26,11 +47,33 @@ class Settings {
appBody.classList.remove(themeName);
});
if (this.themes[themeIndex] !== '') appBody.classList.add(this.themes[themeIndex]);
localStorage.setItem('settings', JSON.stringify({ themeIndex }));
setSettings('themeIndex', themeIndex);
this.themeIndex = themeIndex;
}
getIsMarkdown() {
if (typeof this.isMarkdown === 'boolean') return this.isMarkdown;
const settings = getSettings();
if (settings === null) return false;
if (typeof settings.isMarkdown === 'undefined') return false;
return settings.isMarkdown;
}
setter(action) {
const actions = {
[cons.actions.settings.TOGGLE_MARKDOWN]: () => {
this.isMarkdown = !this.isMarkdown;
setSettings('isMarkdown', this.isMarkdown);
this.emit(cons.events.settings.MARKDOWN_TOGGLED, this.isMarkdown);
},
};
actions[action.type]?.();
}
}
const settings = new Settings();
appDispatcher.register(settings.setter.bind(settings));
export default settings;