Reset modification indicator after successfully saved changes (#117)

This commit is contained in:
Felix Häusler 2018-04-08 12:04:42 +02:00 committed by 冉四夕
parent b16c23b5ee
commit a924b52096
5 changed files with 34 additions and 21 deletions

View File

@ -9,12 +9,13 @@
**:beetle:Bug fix**
- fix: update outdated preferences on startup #100
- fix: reset modification indicator after successfully saved changes
### 0.9.25
**:cactus:Feature**
- display and inline math surport #36
- display and inline math support #36
- Image path auto complement #96
- Feature: Toggle loose list item in paragraph menu #103
- Add loose and tight list compatibility #74

View File

@ -26,19 +26,27 @@ const watchAndReload = (pathname, win) => { // when i build, and failed.
// })
}
const writeFile = (pathname, content, extension, win, e) => {
const writeFile = (pathname, content, extension, e, callback = null) => {
if (pathname) {
pathname = pathname.endsWith(extension) ? pathname : `${pathname}${extension}`
const filename = path.basename(pathname)
pathname = !extension || pathname.endsWith(extension) ? pathname : `${pathname}${extension}`
fs.writeFile(pathname, content, 'utf-8', err => {
if (err) log(err)
// not export
if (extension === '.md' && e) e.sender.send('AGANI::set-pathname', { pathname, filename })
if (callback) callback(err, pathname)
})
// watchAndReload(pathname, win)
} else {
log('[ERROR] Cannot save file without path.')
}
}
const writeMarkdownFile = (pathname, content, extension, win, e, quitAfterSave = false) => {
writeFile(pathname, content, extension, e, (err, filePath) => {
if (!err) e.sender.send('AGANI::file-saved-successfully')
const filename = path.basename(filePath)
if (e && filePath) e.sender.send('AGANI::set-pathname', { pathname: filePath, filename })
if (!err && quitAfterSave) forceClose(win)
})
}
const forceClose = win => {
if (!win) return
if (windows.has(win.id)) {
@ -49,6 +57,7 @@ const forceClose = win => {
app.quit()
}
}
// handle the response from render process.
const handleResponseForExport = (e, { type, content, filename, pathname }) => {
const win = BrowserWindow.fromWebContents(e.sender)
@ -63,24 +72,22 @@ const handleResponseForExport = (e, { type, content, filename, pathname }) => {
if (!content && type === 'pdf') {
win.webContents.printToPDF({ printBackground: true }, (err, data) => {
if (err) log(err)
writeFile(filePath, data, extension, win, e)
writeFile(filePath, data, extension, e)
})
} else {
writeFile(filePath, content, extension, win, e)
writeFile(filePath, content, extension, e)
}
}
const handleResponseForSave = (e, { markdown, pathname }) => {
const handleResponseForSave = (e, { markdown, pathname, quitAfterSave = false }) => {
const win = BrowserWindow.fromWebContents(e.sender)
if (pathname) {
fs.writeFile(pathname, markdown, 'utf-8', err => {
if (err) log(err)
})
writeMarkdownFile(pathname, markdown, '', win, e, quitAfterSave)
} else {
const filePath = dialog.showSaveDialog(win, {
defaultPath: getPath('documents') + '/Untitled.md'
})
writeFile(filePath, markdown, '.md', win, e)
writeMarkdownFile(filePath, markdown, '.md', win, e, quitAfterSave)
}
}
@ -89,7 +96,7 @@ ipcMain.on('AGANI::response-file-save-as', (e, { markdown, pathname }) => {
let filePath = dialog.showSaveDialog(win, {
defaultPath: pathname || getPath('documents') + '/Untitled.md'
})
writeFile(filePath, markdown, '.md', win, e)
writeMarkdownFile(filePath, markdown, '.md', win, e)
})
ipcMain.on('AGANI::response-close-confirm', (e, { filename, pathname, markdown }) => {
@ -109,7 +116,7 @@ ipcMain.on('AGANI::response-close-confirm', (e, { filename, pathname, markdown }
break
case 0:
setTimeout(() => {
handleResponseForSave(e, { pathname, markdown })
handleResponseForSave(e, { pathname, markdown, quitAfterSave: true })
})
break
}

View File

@ -96,6 +96,7 @@
dispatch('LISTEN_FOR_ABOUT_DIALOG')
dispatch('LISTEN_FOR_RENAME')
dispatch('LISTEN_FOR_IMAGE_PATH')
dispatch('LISTEN_FOR_FILE_SAVED_SUCCESSFULLY')
}
}
</script>

View File

@ -191,7 +191,7 @@
})
this.editor.on('change', (markdown, wordCount, cursor) => {
this.$store.dispatch('SAVE_FILE', { markdown, wordCount, cursor })
this.$store.dispatch('LISTEN_FOR_CONTENT_CHANGE', { markdown, wordCount, cursor })
})
this.editor.on('selectionChange', changes => {

View File

@ -219,17 +219,16 @@ const actions = {
ipcRenderer.send('AGANI::response-export', { type, content, filename, pathname })
},
SAVE_FILE ({ commit, state }, { markdown, wordCount, cursor }) {
LISTEN_FOR_CONTENT_CHANGE ({ commit, state }, { markdown, wordCount, cursor }) {
const { pathname, autoSave, markdown: oldMarkdown } = state
commit('SET_MARKDOWN', markdown)
// set word count
if (wordCount) commit('SET_WORD_COUNT', wordCount)
// set cursor
if (cursor) commit('SET_CURSOR', cursor)
// save to file only when the markdown changed!
// change save status/save to file only when the markdown changed!
if (markdown !== oldMarkdown) {
if (pathname && autoSave) {
commit('SET_SAVE_STATUS', true)
ipcRenderer.send('AGANI::response-file-save', { pathname, markdown })
} else {
commit('SET_SAVE_STATUS', false)
@ -237,6 +236,12 @@ const actions = {
}
},
LISTEN_FOR_FILE_SAVED_SUCCESSFULLY ({ commit }) {
ipcRenderer.on('AGANI::file-saved-successfully', e => {
commit('SET_SAVE_STATUS', true)
})
},
SELECTION_CHANGE ({ commit }, changes) {
const { start, end } = changes
if (start.key === end.key && start.block.text) {
@ -312,7 +317,6 @@ const actions = {
const { isSaved, markdown, pathname, filename } = state
if (!isSaved && /[^\n]/.test(markdown)) {
ipcRenderer.send('AGANI::response-close-confirm', { filename, pathname, markdown })
commit('SET_SAVE_STATUS', true)
} else {
ipcRenderer.send('AGANI::close-window')
}