refs #850 Replace App with typescript

This commit is contained in:
AkiraFukushima 2019-04-03 23:46:01 +09:00
parent 234499581a
commit 35ddf9e32b
2 changed files with 165 additions and 123 deletions

View File

@ -1,123 +0,0 @@
import { ipcRenderer } from 'electron'
import router from '@/router'
import { LightTheme, DarkTheme, SolarizedLightTheme, SolarizedDarkTheme, KimbieDarkTheme } from '@/utils/theme'
import DisplayStyle from '~/src/constants/displayStyle'
import Theme from '~/src/constants/theme'
import TimeFormat from '~/src/constants/timeFormat'
import Language from '~/src/constants/language'
import DefaultFonts from '@/utils/fonts'
const App = {
namespaced: true,
state: {
theme: LightTheme,
fontSize: 14,
displayNameStyle: DisplayStyle.DisplayNameAndUsername.value,
notify: {
reply: true,
reblog: true,
favourite: true,
follow: true
},
timeFormat: TimeFormat.Absolute.value,
language: Language.en.key,
defaultFonts: DefaultFonts,
ignoreCW: false,
ignoreNFSW: false,
hideAllAttachments: false
},
mutations: {
updateTheme (state, themeColorList) {
state.theme = themeColorList
},
updateFontSize (state, value) {
state.fontSize = value
},
updateDisplayNameStyle (state, value) {
state.displayNameStyle = value
},
updateNotify (state, notify) {
state.notify = notify
},
updateTimeFormat (state, format) {
state.timeFormat = format
},
updateLanguage (state, key) {
state.language = key
},
addFont (state, font) {
const list = [font].concat(DefaultFonts)
state.defaultFonts = Array.from(new Set(list))
},
updateIgnoreCW (state, cw) {
state.ignoreCW = cw
},
updateIgnoreNFSW (state, nfsw) {
state.ignoreNFSW = nfsw
},
updateHideAllAttachments (state, hideAllAttachments) {
state.hideAllAttachments = hideAllAttachments
}
},
actions: {
watchShortcutsEvents () {
ipcRenderer.on('open-preferences', () => {
router.push('/preferences/general')
})
},
removeShortcutsEvents () {
ipcRenderer.removeAllListeners('open-preferences')
},
loadPreferences ({ commit, dispatch }) {
return new Promise((resolve, reject) => {
ipcRenderer.send('get-preferences')
ipcRenderer.once('error-get-preferences', (event, err) => {
ipcRenderer.removeAllListeners('response-get-preferences')
reject(err)
})
ipcRenderer.once('response-get-preferences', (event, conf) => {
ipcRenderer.removeAllListeners('error-get-preferences')
dispatch('updateTheme', conf.appearance)
commit('updateDisplayNameStyle', conf.appearance.displayNameStyle)
commit('updateFontSize', conf.appearance.fontSize)
commit('updateNotify', conf.notification.notify)
commit('updateTimeFormat', conf.appearance.timeFormat)
commit('updateLanguage', conf.language.language)
commit('addFont', conf.appearance.font)
commit('updateIgnoreCW', conf.general.timeline.cw)
commit('updateIgnoreNFSW', conf.general.timeline.nfsw)
commit('updateHideAllAttachments', conf.general.timeline.hideAllAttachments)
resolve(conf)
})
})
},
updateTheme ({ commit }, appearance) {
const themeKey = appearance.theme
switch (themeKey) {
case Theme.Light.key:
commit('updateTheme', LightTheme)
break
case Theme.Dark.key:
commit('updateTheme', DarkTheme)
break
case Theme.SolarizedLight.key:
commit('updateTheme', SolarizedLightTheme)
break
case Theme.SolarizedDark.key:
commit('updateTheme', SolarizedDarkTheme)
break
case Theme.KimbieDark.key:
commit('updateTheme', KimbieDarkTheme)
break
case Theme.Custom.key:
commit('updateTheme', appearance.customThemeColor)
break
default:
commit('updateTheme', LightTheme)
break
}
}
}
}
export default App

165
src/renderer/store/App.ts Normal file
View File

@ -0,0 +1,165 @@
import { ipcRenderer } from 'electron'
import { MutationTree, ActionTree, Module } from 'vuex'
import router from '@/router'
import { LightTheme, DarkTheme, SolarizedLightTheme, SolarizedDarkTheme, KimbieDarkTheme } from '@/utils/theme'
import DisplayStyle from '~/src/constants/displayStyle'
import Theme from '~/src/constants/theme'
import TimeFormat from '~/src/constants/timeFormat'
import Language from '~/src/constants/language'
import DefaultFonts from '@/utils/fonts'
export interface Notify {
reply: boolean,
reblog: boolean,
favourite: boolean,
follow: boolean
}
export interface AppState {
theme: any, // TODO: type
fontSize: number,
displayNameStyle: number,
notify: Notify,
timeFormat: number,
language: string,
defaultFonts: Array<string>,
ignoreCW: boolean,
ignoreNFSW: boolean,
hideAllAttachments: boolean
}
const state = (): AppState => ({
theme: LightTheme,
fontSize: 14,
displayNameStyle: DisplayStyle.DisplayNameAndUsername.value,
notify: {
reply: true,
reblog: true,
favourite: true,
follow: true
},
timeFormat: TimeFormat.Absolute.value,
language: Language.en.key,
defaultFonts: DefaultFonts,
ignoreCW: false,
ignoreNFSW: false,
hideAllAttachments: false
})
const MUTATION_TYPES = {
UPDATE_THEME: 'updateTheme',
UPDATE_FONT_SIZE: 'updateFontSize',
UPDATE_DISPLAY_NAME_STYLE: 'updateDisplayNameStyle',
UPDATE_NOTIFY: 'updateNotify',
UPDATE_TIME_FORMAT: 'updateTimeFormat',
UPDATE_LANGUAGE: 'updateLanguage',
ADD_FONT: 'addFont',
UPDATE_IGNORE_CW: 'updateIgnoreCW',
UPDATE_IGNORE_NFSW: 'updateIgnoreNFSW',
UPDATE_HIDE_ALL_ATTACHMENTS: 'updateHideAllAttachments'
}
const mutations: MutationTree<AppState> = {
[MUTATION_TYPES.UPDATE_THEME]: (state: AppState, themeColorList: any) => {
state.theme = themeColorList
},
[MUTATION_TYPES.UPDATE_FONT_SIZE]: (state: AppState, value: number) => {
state.fontSize = value
},
[MUTATION_TYPES.UPDATE_DISPLAY_NAME_STYLE]: (state: AppState, value: number) => {
state.displayNameStyle = value
},
[MUTATION_TYPES.UPDATE_NOTIFY]: (state: AppState, notify: Notify) => {
state.notify = notify
},
[MUTATION_TYPES.UPDATE_TIME_FORMAT]: (state: AppState, format: number) => {
state.timeFormat = format
},
[MUTATION_TYPES.UPDATE_LANGUAGE]: (state: AppState, key: string) => {
state.language = key
},
[MUTATION_TYPES.ADD_FONT]: (state: AppState, font: string) => {
const list = [font].concat(DefaultFonts)
state.defaultFonts = Array.from(new Set(list))
},
[MUTATION_TYPES.UPDATE_IGNORE_CW]: (state: AppState, cw: boolean) => {
state.ignoreCW = cw
},
[MUTATION_TYPES.UPDATE_IGNORE_NFSW]: (state: AppState, nfsw: boolean) => {
state.ignoreNFSW = nfsw
},
[MUTATION_TYPES.UPDATE_HIDE_ALL_ATTACHMENTS]: (state: AppState, hideAllAttachments: boolean) => {
state.hideAllAttachments = hideAllAttachments
}
}
// TODO: use type of rootState
const actions: ActionTree<AppState, any> = {
watchShortcutsEvents: () => {
ipcRenderer.on('open-preferences', () => {
router.push('/preferences/general')
})
},
removeShortcutsEvents: () => {
ipcRenderer.removeAllListeners('open-preferences')
},
loadPreferences: ({ commit, dispatch }) => {
return new Promise((resolve, reject) => {
ipcRenderer.send('get-preferences')
ipcRenderer.once('error-get-preferences', (_, err) => {
ipcRenderer.removeAllListeners('response-get-preferences')
reject(err)
})
ipcRenderer.once('response-get-preferences', (_, conf: any) => {
ipcRenderer.removeAllListeners('error-get-preferences')
dispatch('updateTheme', conf.appearance as any)
commit(MUTATION_TYPES.UPDATE_DISPLAY_NAME_STYLE, conf.appearance.displayNameStyle as number)
commit(MUTATION_TYPES.UPDATE_FONT_SIZE, conf.appearance.fontSize as number)
commit(MUTATION_TYPES.UPDATE_NOTIFY, conf.notification.notify as Notify)
commit(MUTATION_TYPES.UPDATE_TIME_FORMAT, conf.appearance.timeFormat as number)
commit(MUTATION_TYPES.UPDATE_LANGUAGE, conf.language.language as string)
commit(MUTATION_TYPES.ADD_FONT, conf.appearance.font as string)
commit(MUTATION_TYPES.UPDATE_IGNORE_CW, conf.general.timeline.cw as boolean)
commit(MUTATION_TYPES.UPDATE_IGNORE_NFSW, conf.general.timeline.nfsw as boolean)
commit(MUTATION_TYPES.UPDATE_HIDE_ALL_ATTACHMENTS, conf.general.timeline.hideAllAttachments as boolean)
resolve(conf)
})
})
},
updateTheme: ({ commit }, appearance: any) => {
const themeKey = appearance.theme
switch (themeKey) {
case Theme.Light.key:
commit(MUTATION_TYPES.UPDATE_THEME, LightTheme)
break
case Theme.Dark.key:
commit(MUTATION_TYPES.UPDATE_THEME, DarkTheme)
break
case Theme.SolarizedLight.key:
commit(MUTATION_TYPES.UPDATE_THEME, SolarizedLightTheme)
break
case Theme.SolarizedDark.key:
commit(MUTATION_TYPES.UPDATE_THEME, SolarizedDarkTheme)
break
case Theme.KimbieDark.key:
commit(MUTATION_TYPES.UPDATE_THEME, KimbieDarkTheme)
break
case Theme.Custom.key:
commit(MUTATION_TYPES.UPDATE_THEME, appearance.customThemeColor)
break
default:
commit(MUTATION_TYPES.UPDATE_THEME, LightTheme)
break
}
}
}
// TODO: use type of rootState
const App: Module<AppState, any> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default App