mirror of
https://github.com/marktext/marktext.git
synced 2025-05-04 04:21:38 +08:00
Reset modification indicator after successfully saved changes (#117)
This commit is contained in:
parent
b16c23b5ee
commit
a924b52096
3
.github/CHANGELOG.md
vendored
3
.github/CHANGELOG.md
vendored
@ -9,12 +9,13 @@
|
|||||||
**:beetle:Bug fix**
|
**:beetle:Bug fix**
|
||||||
|
|
||||||
- fix: update outdated preferences on startup #100
|
- fix: update outdated preferences on startup #100
|
||||||
|
- fix: reset modification indicator after successfully saved changes
|
||||||
|
|
||||||
### 0.9.25
|
### 0.9.25
|
||||||
|
|
||||||
**:cactus:Feature**
|
**:cactus:Feature**
|
||||||
|
|
||||||
- display and inline math surport #36
|
- display and inline math support #36
|
||||||
- Image path auto complement #96
|
- Image path auto complement #96
|
||||||
- Feature: Toggle loose list item in paragraph menu #103
|
- Feature: Toggle loose list item in paragraph menu #103
|
||||||
- Add loose and tight list compatibility #74
|
- Add loose and tight list compatibility #74
|
||||||
|
@ -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) {
|
if (pathname) {
|
||||||
pathname = pathname.endsWith(extension) ? pathname : `${pathname}${extension}`
|
pathname = !extension || pathname.endsWith(extension) ? pathname : `${pathname}${extension}`
|
||||||
const filename = path.basename(pathname)
|
|
||||||
fs.writeFile(pathname, content, 'utf-8', err => {
|
fs.writeFile(pathname, content, 'utf-8', err => {
|
||||||
if (err) log(err)
|
if (err) log(err)
|
||||||
// not export
|
if (callback) callback(err, pathname)
|
||||||
if (extension === '.md' && e) e.sender.send('AGANI::set-pathname', { pathname, filename })
|
|
||||||
})
|
})
|
||||||
// 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 => {
|
const forceClose = win => {
|
||||||
if (!win) return
|
if (!win) return
|
||||||
if (windows.has(win.id)) {
|
if (windows.has(win.id)) {
|
||||||
@ -49,6 +57,7 @@ const forceClose = win => {
|
|||||||
app.quit()
|
app.quit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle the response from render process.
|
// handle the response from render process.
|
||||||
const handleResponseForExport = (e, { type, content, filename, pathname }) => {
|
const handleResponseForExport = (e, { type, content, filename, pathname }) => {
|
||||||
const win = BrowserWindow.fromWebContents(e.sender)
|
const win = BrowserWindow.fromWebContents(e.sender)
|
||||||
@ -63,24 +72,22 @@ const handleResponseForExport = (e, { type, content, filename, pathname }) => {
|
|||||||
if (!content && type === 'pdf') {
|
if (!content && type === 'pdf') {
|
||||||
win.webContents.printToPDF({ printBackground: true }, (err, data) => {
|
win.webContents.printToPDF({ printBackground: true }, (err, data) => {
|
||||||
if (err) log(err)
|
if (err) log(err)
|
||||||
writeFile(filePath, data, extension, win, e)
|
writeFile(filePath, data, extension, e)
|
||||||
})
|
})
|
||||||
} else {
|
} 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)
|
const win = BrowserWindow.fromWebContents(e.sender)
|
||||||
if (pathname) {
|
if (pathname) {
|
||||||
fs.writeFile(pathname, markdown, 'utf-8', err => {
|
writeMarkdownFile(pathname, markdown, '', win, e, quitAfterSave)
|
||||||
if (err) log(err)
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
const filePath = dialog.showSaveDialog(win, {
|
const filePath = dialog.showSaveDialog(win, {
|
||||||
defaultPath: getPath('documents') + '/Untitled.md'
|
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, {
|
let filePath = dialog.showSaveDialog(win, {
|
||||||
defaultPath: pathname || getPath('documents') + '/Untitled.md'
|
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 }) => {
|
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
|
break
|
||||||
case 0:
|
case 0:
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
handleResponseForSave(e, { pathname, markdown })
|
handleResponseForSave(e, { pathname, markdown, quitAfterSave: true })
|
||||||
})
|
})
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -96,6 +96,7 @@
|
|||||||
dispatch('LISTEN_FOR_ABOUT_DIALOG')
|
dispatch('LISTEN_FOR_ABOUT_DIALOG')
|
||||||
dispatch('LISTEN_FOR_RENAME')
|
dispatch('LISTEN_FOR_RENAME')
|
||||||
dispatch('LISTEN_FOR_IMAGE_PATH')
|
dispatch('LISTEN_FOR_IMAGE_PATH')
|
||||||
|
dispatch('LISTEN_FOR_FILE_SAVED_SUCCESSFULLY')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -191,7 +191,7 @@
|
|||||||
})
|
})
|
||||||
|
|
||||||
this.editor.on('change', (markdown, wordCount, cursor) => {
|
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 => {
|
this.editor.on('selectionChange', changes => {
|
||||||
|
@ -219,17 +219,16 @@ const actions = {
|
|||||||
ipcRenderer.send('AGANI::response-export', { type, content, filename, pathname })
|
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
|
const { pathname, autoSave, markdown: oldMarkdown } = state
|
||||||
commit('SET_MARKDOWN', markdown)
|
commit('SET_MARKDOWN', markdown)
|
||||||
// set word count
|
// set word count
|
||||||
if (wordCount) commit('SET_WORD_COUNT', wordCount)
|
if (wordCount) commit('SET_WORD_COUNT', wordCount)
|
||||||
// set cursor
|
// set cursor
|
||||||
if (cursor) commit('SET_CURSOR', 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 (markdown !== oldMarkdown) {
|
||||||
if (pathname && autoSave) {
|
if (pathname && autoSave) {
|
||||||
commit('SET_SAVE_STATUS', true)
|
|
||||||
ipcRenderer.send('AGANI::response-file-save', { pathname, markdown })
|
ipcRenderer.send('AGANI::response-file-save', { pathname, markdown })
|
||||||
} else {
|
} else {
|
||||||
commit('SET_SAVE_STATUS', false)
|
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) {
|
SELECTION_CHANGE ({ commit }, changes) {
|
||||||
const { start, end } = changes
|
const { start, end } = changes
|
||||||
if (start.key === end.key && start.block.text) {
|
if (start.key === end.key && start.block.text) {
|
||||||
@ -312,7 +317,6 @@ const actions = {
|
|||||||
const { isSaved, markdown, pathname, filename } = state
|
const { isSaved, markdown, pathname, filename } = state
|
||||||
if (!isSaved && /[^\n]/.test(markdown)) {
|
if (!isSaved && /[^\n]/.test(markdown)) {
|
||||||
ipcRenderer.send('AGANI::response-close-confirm', { filename, pathname, markdown })
|
ipcRenderer.send('AGANI::response-close-confirm', { filename, pathname, markdown })
|
||||||
commit('SET_SAVE_STATUS', true)
|
|
||||||
} else {
|
} else {
|
||||||
ipcRenderer.send('AGANI::close-window')
|
ipcRenderer.send('AGANI::close-window')
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user