import Language from '~/src/constants/language' ; import { Module , MutationTree , ActionTree } from 'vuex' ; import { RootState } from '@/store' ; import { Language as LanguageSet } from '~/src/types/language' ; import { BaseConfig } from '~/src/types/preference' ; import { MyWindow } from '~/src/types/global' ; const win = ( window as any ) as MyWindow ; export type LanguageState = { language : LanguageSet ; } ; const state = { language : { language : Language . en . key , spellchecker : { enabled : true , languages : [ Language . en . key ] } } } ; export const MUTATION_TYPES = { UPDATE_LANGUAGE : 'updateLanguage' , CHANGE_LANGUAGE : 'changeLanguage' , TOGGLE_SPELLCHECKER : 'toggleSpellchecker' , UPDATE_SPELLCHECKER_LANGUAGES : 'updateSpellcheckerLanguages' } ; const mutations = { [ MUTATION_TYPES . UPDATE_LANGUAGE ] : ( state , conf ) => { state . language = conf ; } , [ MUTATION_TYPES . CHANGE_LANGUAGE ] : ( state , key ) => { state . language . language = key ; } , [ MUTATION_TYPES . TOGGLE_SPELLCHECKER ] : ( state , enabled ) => { state . language . spellchecker . enabled = enabled ; } , [ MUTATION_TYPES . UPDATE_SPELLCHECKER_LANGUAGES ] : ( state , languages ) => { state . language . spellchecker . languages = languages ; } } ; const actions = { loadLanguage : async ( { commit } ) : Promise < string > => { const conf = await win . ipcRenderer . invoke ( 'get-preferences' ) ; commit ( MUTATION_TYPES . UPDATE_LANGUAGE , conf . language as LanguageSet ) ; return conf . language . language ; } , changeLanguage : async ( { commit } , key ) : Promise < string > => { const value = await win . ipcRenderer . invoke ( 'change-language' , key ) ; commit ( MUTATION_TYPES . CHANGE_LANGUAGE , value ) ; return value ; } , toggleSpellchecker : async ( { commit } , enabled ) => { const value = await win . ipcRenderer . invoke ( 'toggle-spellchecker' , enabled ) ; commit ( MUTATION_TYPES . TOGGLE_SPELLCHECKER , value ) ; return value ; } , updateSpellcheckerLanguages : async ( { commit } , languages ) => { const langs = await win . ipcRenderer . invoke ( 'update-spellchecker-languages' , languages ) ; commit ( MUTATION_TYPES . UPDATE_SPELLCHECKER_LANGUAGES , langs ) ; return langs ; } } ; export default { namespaced : true , state : state , mutations : mutations , actions : actions } as Module < LanguageState , RootState > ;