From 74723b86355b697c647916b319ee6e85eb2e3b43 Mon Sep 17 00:00:00 2001 From: Cristian Popescu Date: Thu, 20 Dec 2018 16:44:29 +0200 Subject: [PATCH] Render a Dialog when a new file is opened and current file is modified --- app/scripts/functions.js | 22 +++--- app/scripts/ipc_renderer.js | 137 ++++++++++++++++++++++++++++-------- 2 files changed, 119 insertions(+), 40 deletions(-) diff --git a/app/scripts/functions.js b/app/scripts/functions.js index 786bac6..3ce6e62 100644 --- a/app/scripts/functions.js +++ b/app/scripts/functions.js @@ -1,6 +1,6 @@ var clkPref = (opt) => { currentValue = opt.value; - if ( currentValue=='preview' || opt===false ) { + if (currentValue === 'preview' || opt === false) { document.getElementById("htmlPreview").style.display = "none"; document.getElementById("markdown").style.display = "block"; document.getElementById('previewRadio').checked = true; @@ -11,11 +11,11 @@ var clkPref = (opt) => { document.getElementById('htmlRadio').checked = true; config.set('isHtml', true); } -} +}; var changeTheme = (opt) => { currentValueTheme = opt.value; - if ( currentValueTheme=='light' || opt===false) { + if (currentValueTheme === 'light' || opt === false) { cm.setOption("theme", "default"); document.getElementById("previewPanel").className = "col-md-6 full-height"; document.getElementById('lightThemeRadio').checked = true; @@ -26,7 +26,7 @@ var changeTheme = (opt) => { document.getElementById('darkThemeRadio').checked = true; config.set('darkMode', true); } -} +}; var showToolBar = () => { if(document.getElementById("toolbarArea").style.display == "block"){ @@ -34,26 +34,26 @@ var showToolBar = () => { document.getElementById("angleToolBar").className = "fa fa-angle-double-right"; document.getElementById("toolbarArea").style.display = "none"; document.getElementById("editArea").style.paddingTop = "24px"; - }else{ + } else { document.getElementById("angleToolBar").className = ""; document.getElementById("angleToolBar").className = "fa fa-angle-double-down"; document.getElementById("toolbarArea").style.display = "block"; document.getElementById("editArea").style.paddingTop = "53px"; } -} +}; // Generations and clean state of CodeMirror var getGeneration = () => { return this.cm.doc.changeGeneration(); -} +}; var setClean = () => { this.latestGeneration = this.getGeneration(); -} +}; var isClean = () => { return this.cm.doc.isClean(this.latestGeneration); -} +}; // Update window title on various events var updateWindowTitle = (path) => { @@ -68,11 +68,13 @@ var updateWindowTitle = (path) => { parsedPath = parsePath(path); dir = parsedPath.dirname || process.cwd(); title = parsedPath.basename + " - " + dir + " - " + appName; + console.log(title) } else { + console.log('new file'); title = "New document - " + appName; } if (!this.isClean()) { title = saveSymbol + title; } document.title = title; -} +}; diff --git a/app/scripts/ipc_renderer.js b/app/scripts/ipc_renderer.js index 79e1d22..204ff09 100644 --- a/app/scripts/ipc_renderer.js +++ b/app/scripts/ipc_renderer.js @@ -1,23 +1,27 @@ -// Handling file saving through IPCRenderer -var saveAs = () => { + +const alertIfError = (error) => { + if(error) alert(error); +}; + +const saveAs = () => { storage.get('markdown-savefile', (error, data) => { options = {}; if ('filename' in data) { options.defaultPath = data.filename; } dialog.showSaveDialog(options, (fileName) => { - if (fileName === undefined){ - console.log("You didn't save the file"); + if (fileName === undefined) { + console.log('You didn\'t save the file'); return; } - storage.set('markdown-savefile', {'filename' : fileName}, (error) => { if (error) alert(error); }); + storage.set('markdown-savefile', {'filename': fileName}, alertIfError); - var mdValue = cm.getValue(); + const mdValue = cm.getValue(); // fileName is a string that contains the path and filename created in the save file dialog. fs.writeFile(fileName, mdValue, (err) => { - if(err){ - alert("An error ocurred creating the file "+ err.message) + if (err) { + alert(`An error occurred creating the file ${err.message}`) } }); this.setClean(); @@ -25,36 +29,29 @@ var saveAs = () => { this.updateWindowTitle(fileName); }); }); -} - -ipc.on('file-new', () => { - storage.set('markdown-savefile', {}, (error) => { if (error) alert(error); }); - currentFile = ''; - cm.getDoc().setValue(""); -}); +}; -// Handling file saving through IPCRenderer -ipc.on('file-save', () => { +const saveFile = () => { storage.get('markdown-savefile', (error, data) => { if (error) { saveAs(); return; } if ('filename' in data) { - var fileName = data.filename; - if (fileName === undefined){ - console.log("You didn't save the file"); + const fileName = data.filename; + if (fileName === undefined) { + console.log('You didn\'t save the file'); return; } - storage.set('markdown-savefile', {'filename' : fileName}, (error) => { if (error) alert(error); }); + storage.set('markdown-savefile', {'filename': fileName}, alertIfError); - var mdValue = cm.getValue(); + const mdValue = cm.getValue(); // fileName is a string that contains the path and filename created in the save file dialog. fs.writeFile(fileName, mdValue, (err) => { - if(err){ - alert("An error ocurred creating the file "+ err.message) - } + if (err) { + alert(`An error occurred creating the file ${err.message}`) + } }); this.setClean(); this.currentFile = fileName; @@ -63,8 +60,88 @@ ipc.on('file-save', () => { saveAs(); } }); -}); +}; + +const resetFile = () => { + storage.set('markdown-savefile', {}, alertIfError); + setClean(); + this.currentFile = ''; + updateWindowTitle(); + cm.getDoc().setValue(''); +}; + +const saveAsAndReset = () => { + dialog.showSaveDialog({}, (fileName) => { + if (fileName === undefined) { + console.log('You didn\'t save the file'); + return; + } + + storage.set('markdown-savefile', {'filename': fileName}, alertIfError); + + const mdValue = cm.getValue(); + // fileName is a string that contains the path and filename created in the save file dialog. + fs.writeFile(fileName, mdValue, (err) => { + if (err) { + alert(`An error occurred creating the file ${err.message}`); + } + }); + resetFile(); + }); +}; + +const newFile = () => { + if (!isClean()) { // File is modified + const options = { + title: 'You made some changes', + type: 'question', + message: 'Do you want to save the file?', + buttons: ['Save', 'Don\'t Save', 'Cancel'] + }; + dialog.showMessageBox(options, (buttonIndex) => { + if (buttonIndex === 0) { // If Save is pressed + storage.get('markdown-savefile', (error, data) => { + if (error) { + saveAsAndReset(); + return; + } + if ('filename' in data) { + const fileName = data.filename; + if (fileName === undefined) { + console.log('You didn\'t save the file'); + return; + } + + storage.set('markdown-savefile', {'filename': fileName}, alertIfError); + + const mdValue = cm.getValue(); + // fileName is a string that contains the path and filename created in the save file dialog. + fs.writeFile(fileName, mdValue, (err) => { + if (err) { + alert(`An error occurred creating the file ${err.message}`); + } + }); + resetFile(); + } else { // if filename not in data show the save file dialog + saveAsAndReset(); + } + }); + } else if (buttonIndex === 1) { // if Don't save is pressed + resetFile(); + } + }); + } else { // if file is clean + resetFile(); + } +}; + +// Handling new file creation through IPCRenderer +ipc.on('file-new', newFile); +// Handling file saving through IPCRenderer +ipc.on('file-save', saveFile); + +// Handling file saving through IPCRenderer ipc.on('file-save-as', saveAs); // Handling file opening through IPCRenderer @@ -72,7 +149,7 @@ ipc.on('file-open', () => { storage.get('markdown-savefile', (error, data) => { if (error) alert(error); - var options = {'properties' : ['openFile'], 'filters' : [{name: 'Markdown', 'extensions':['md']}]}; + const options = {'properties' : ['openFile'], 'filters' : [{name: 'Markdown', 'extensions':['md']}]}; if ('filename' in data) { options.defaultPath = data.filename; } @@ -83,13 +160,13 @@ ipc.on('file-open', () => { return; } - storage.set('markdown-savefile', {'filename' : fileName[0]}, (error) => { if (error) alert(error); }); + storage.set('markdown-savefile', {'filename' : fileName[0]}, alertIfError); - var mdValue = cm.getValue(); + const mdValue = cm.getValue(); // fileName is a string that contains the path and filename created in the save file dialog. fs.readFile(fileName[0], 'utf-8', (err, data) => { if(err){ - alert("An error ocurred while opening the file "+ err.message) + alert(`An error ocurred while opening the file ${err.message}`) } cm.getDoc().setValue(data); });