diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 4150c458..4a558acf 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -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 diff --git a/src/main/actions/file.js b/src/main/actions/file.js index 2b2e5144..6554922d 100644 --- a/src/main/actions/file.js +++ b/src/main/actions/file.js @@ -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 } diff --git a/src/renderer/app.vue b/src/renderer/app.vue index b1f6e062..550291b1 100644 --- a/src/renderer/app.vue +++ b/src/renderer/app.vue @@ -96,6 +96,7 @@ dispatch('LISTEN_FOR_ABOUT_DIALOG') dispatch('LISTEN_FOR_RENAME') dispatch('LISTEN_FOR_IMAGE_PATH') + dispatch('LISTEN_FOR_FILE_SAVED_SUCCESSFULLY') } } diff --git a/src/renderer/components/editor.vue b/src/renderer/components/editor.vue index d4728ec1..9613c94a 100644 --- a/src/renderer/components/editor.vue +++ b/src/renderer/components/editor.vue @@ -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 => { diff --git a/src/renderer/store/editor.js b/src/renderer/store/editor.js index 9c463fdf..55a643bb 100644 --- a/src/renderer/store/editor.js +++ b/src/renderer/store/editor.js @@ -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') }