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 24, 2018
1 parent 6ef6409 commit 74723b8
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 40 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;
}
};
137 changes: 107 additions & 30 deletions app/scripts/ipc_renderer.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,57 @@
// 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();
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', () => {
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;
Expand All @@ -63,16 +60,96 @@ 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
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;
}
Expand All @@ -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);
});
Expand Down

0 comments on commit 74723b8

Please sign in to comment.