diff --git a/src/main/actions/file.js b/src/main/actions/file.js index b819f973..b774f761 100644 --- a/src/main/actions/file.js +++ b/src/main/actions/file.js @@ -3,10 +3,10 @@ import fs from 'fs' // import chokidar from 'chokidar' import path from 'path' -import { app, dialog, ipcMain, BrowserWindow } from 'electron' +import { app, BrowserWindow, dialog, ipcMain } from 'electron' import createWindow, { windows } from '../createWindow' -import { EXTENSIONS, EXTENSION_HASN } from '../config' -import { getPath, log, isMarkdownFile } from '../utils' +import { EXTENSION_HASN, EXTENSIONS } from '../config' +import { getPath, isMarkdownFile, log } from '../utils' import userPreference from '../preference' const watchAndReload = (pathname, win) => { // when i build, and failed. @@ -134,6 +134,44 @@ ipcMain.on('AGANI::window::drop', (e, fileList) => { } }) +ipcMain.on('AGANI::response-file-move-to', (e, { pathname }) => { + const win = BrowserWindow.fromWebContents(e.sender) + if (pathname !== '') { + let newPath = dialog.showSaveDialog(win, { + buttonLabel: 'Move or rename', + nameFieldLabel: 'Filename:', + defaultPath: pathname + }) + if (newPath === undefined) return + if (!fs.existsSync(newPath)) { + fs.renameSync(pathname, newPath) + e.sender.send('AGANI::set-pathname', { pathname: newPath, filename: path.basename(newPath) }) + } else { + dialog.showMessageBox(win, { + type: 'warning', + buttons: ['Replace', 'Cancel'], + defaultId: 1, + message: `The file ${pathname} already exists. Do you want to replace it?`, + cancelId: 1, + noLink: true + }, index => { + if (index === 0) { + fs.renameSync(pathname, newPath) + e.sender.send('AGANI::set-pathname', { pathname: newPath, filename: path.basename(newPath) }) + } + }) + } + } else { + dialog.showMessageBox(win, { + type: 'info', + buttons: ['OK'], + message: `Please save the file before moving it!`, + cancelId: 0, + noLink: true + }) + } +}) + export const exportFile = (win, type) => { win.webContents.send('AGANI::export', { type }) } @@ -144,7 +182,7 @@ export const print = win => { export const open = win => { const filename = dialog.showOpenDialog(win, { - properties: [ 'openFile' ], + properties: ['openFile'], filters: [{ name: 'text', extensions: EXTENSIONS @@ -178,3 +216,7 @@ export const autoSave = (menuItem, browserWindow) => { }) .catch(log) } + +export const moveTo = win => { + win.webContents.send('AGANI::ask-file-move-to') +} diff --git a/src/main/menus/file.js b/src/main/menus/file.js index 61fe2040..693cc8a2 100755 --- a/src/main/menus/file.js +++ b/src/main/menus/file.js @@ -50,6 +50,13 @@ export default { } }, { type: 'separator' + }, { + label: 'Move To...', + click (menuItem, browserWindow) { + actions.moveTo(browserWindow) + } + }, { + type: 'separator' }, { label: 'Export Styled HTML', click (menuItem, browserWindow) { diff --git a/src/renderer/app.vue b/src/renderer/app.vue index 6f48d744..815c538f 100644 --- a/src/renderer/app.vue +++ b/src/renderer/app.vue @@ -79,6 +79,7 @@ dispatch('ASK_FOR_MODE') dispatch('LISTEN_FOR_CLOSE') dispatch('LISTEN_FOR_SAVE_AS') + dispatch('LISTEN_FOR_MOVE_TO') dispatch('LINTEN_WIN_STATUS') dispatch('LISTEN_FOR_SAVE') dispatch('GET_FILENAME') diff --git a/src/renderer/store/editor.js b/src/renderer/store/editor.js index c5b6033d..bd3d9169 100644 --- a/src/renderer/store/editor.js +++ b/src/renderer/store/editor.js @@ -135,6 +135,13 @@ const actions = { }) }, + LISTEN_FOR_MOVE_TO ({ commit, state }) { + ipcRenderer.on('AGANI::ask-file-move-to', () => { + const { pathname } = state + ipcRenderer.send('AGANI::response-file-move-to', { pathname }) + }) + }, + GET_FILENAME ({ commit, state }) { ipcRenderer.on('AGANI::set-pathname', (e, { pathname, filename }) => { commit('SET_FILENAME', filename)