Repository: marktext/marktext
File: src/renderer/store/editor.js (JavaScript)

Code before:
    state.filename = filename
  },
  SET_PATHNAME (state, pathname) {
    window.__dirname = path.dirname(pathname)
    state.pathname = pathname
  },
  SET_SAVE_STATUS (state, status) {
    state.isSaved = status
  },
  SET_MARKDOWN (state, markdown) {
    state.markdown = markdown
  },
  SET_WORD_COUNT (state, wordCount) {
    state.wordCount = wordCount
  },
  SET_CURSOR (state, cursor) {
    state.cursor = cursor
  },
  SET_USER_PREFERENCE (state, preference) {
    Object.keys(preference).forEach(key => {
      if (typeof preference[key] !== 'undefined' && typeof state[key] !== 'undefined') {
        state[key] = preference[key]
      }
    })

    const { preferLooseListItem } = state
    bus.$emit('update-prefer-loose-list-item', preferLooseListItem)
  }
}

const actions = {
  CHANGE_FONT ({ commit }, { type, value }) {
    commit('SET_USER_PREFERENCE', { [type]: value })
    // save to preference.md
    ipcRenderer.send('AGANI::set-user-preference', { [type]: value })
  },

  ASK_FOR_USER_PREFERENCE ({ commit, state }) {
    ipcRenderer.send('AGANI::ask-for-user-preference')
    ipcRenderer.on('AGANI::user-preference', (e, preference) => {
      const { autoSave } = preference

      commit('SET_USER_PREFERENCE', preference)

      // handle autoSave
      if (autoSave) {
        const { pathname, markdown } = state

        if (autoSave && pathname) {
          commit('SET_SAVE_STATUS', true)
          ipcRenderer.send('AGANI::response-file-save', { pathname, markdown })

Review comment (suggestion):
I think maybe you can put this `event emit` in `action: ASK_FOR_USER_PREFERENCE `.

Another thing, You need judge weather `preferLooseListItem` is changed before emit `update-prefer-loose-list-item`.

codes maybe like this:

```javascript
const { preferLooseListItem } = preferences
if (preferLooseListItem) {
  // bus.$emit('update-prefer-loose-list-item', preferLooseListItem)
}
```

Code after:
    state.filename = filename
  },
  SET_PATHNAME (state, pathname) {
    window.__dirname = path.dirname(pathname)
    state.pathname = pathname
  },
  SET_SAVE_STATUS (state, status) {
    state.isSaved = status
  },
  SET_MARKDOWN (state, markdown) {
    state.markdown = markdown
  },
  SET_WORD_COUNT (state, wordCount) {
    state.wordCount = wordCount
  },
  SET_CURSOR (state, cursor) {
    state.cursor = cursor
  },
  SET_USER_PREFERENCE (state, preference) {
    Object.keys(preference).forEach(key => {
      if (typeof preference[key] !== 'undefined' && typeof state[key] !== 'undefined') {
        state[key] = preference[key]
      }
    })
  }
}

const actions = {
  ASK_FOR_INSERT_IMAGE ({ commit }, type) {
    ipcRenderer.send('AGANI::ask-for-insert-image', type)
  },

  ASK_FOR_IMAGE_AUTO_PATH ({ commit, state }, src) {
    const { pathname } = state
    if (pathname) {
      ipcRenderer.send('AGANI::ask-for-image-auto-path', { pathname, src })
    }
  },

  LISTEN_FOR_IMAGE_PATH ({ commit }) {
    ipcRenderer.on('AGANI::image-auto-path', (e, files) => {
      bus.$emit('image-auto-path', files)
    })
  },

  CHANGE_FONT ({ commit }, { type, value }) {
    commit('SET_USER_PREFERENCE', { [type]: value })
    // save to preference.md
    ipcRenderer.send('AGANI::set-user-preference', { [type]: value })
  },