Skip to content

Commit

Permalink
Render a Dialog when a new file is opened and current file is modified
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian Popescu committed Dec 22, 2018
1 parent 6ef6409 commit 159dac2
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 31 deletions.
22 changes: 12 additions & 10 deletions app/scripts/functions.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -26,34 +26,34 @@ var changeTheme = (opt) => {
document.getElementById('darkThemeRadio').checked = true;
config.set('darkMode', true);
}
}
};

var showToolBar = () => {
if(document.getElementById("toolbarArea").style.display == "block"){
document.getElementById("angleToolBar").className = "";
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) => {
Expand All @@ -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;
}
};
98 changes: 77 additions & 21 deletions app/scripts/ipc_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,50 @@ var saveAs = () => {
options.defaultPath = data.filename;
}
dialog.showSaveDialog(options, (fileName) => {
if (fileName === undefined){
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}, (error) => {
if (error) alert(error);
});

var 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;
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', () => {
let saveFile = () => {
storage.get('markdown-savefile', (error, data) => {
if (error) {
saveAs();
return;
}
if ('filename' in data) {
var fileName = data.filename;
if (fileName === undefined){
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}, (error) => { if(error) alert(error); });

var 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;
Expand All @@ -63,7 +58,68 @@ ipc.on('file-save', () => {
saveAs();
}
});
});
};

const resetFile = () => {
storage.set('markdown-savefile', {}, (error) => { if (error) alert(error); });
this.setClean();
this.currentFile = '';
updateWindowTitle();
cm.getDoc().setValue('');
};

const newFile = () => {
if(!isClean()) {
const options = {
title: 'You made some changes',
type: 'question',
message: 'Do you want to save the file?',
buttons: ['Save', 'Don\'t Save', 'Cancel']
};
const buttonIndex = dialog.showMessageBox(options);
if(buttonIndex === 0) { // If Save is pressed
storage.get('markdown-savefile', (error, data) => {
if (error) {
saveAs();
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}, (error) => {
if (error) alert(error);
});

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}`)
}
});
} else { // if filename not in data
saveAs();
}

resetFile();
});
} else if(buttonIndex === 1) { // if Don't save is pressed
resetFile()
}
} else { // if file is clean
resetFile()
}
};

ipc.on('file-new', newFile);

// Handling file saving through IPCRenderer
ipc.on('file-save', saveFile);

ipc.on('file-save-as', saveAs);

Expand All @@ -89,7 +145,7 @@ ipc.on('file-open', () => {
// 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);
});
Expand Down

0 comments on commit 159dac2

Please sign in to comment.