'use strict';

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const Menu = require('menu');
const dialog = require('dialog');
const fs = require('fs');
const ipcMain = require('electron').ipcMain;

let mainWindow;

const mainMenu = Menu.buildFromTemplate([{}, {
  label: 'File',
  submenu: [
  {
    label: 'New File',
    click: function() {
      mainWindow.webContents.send('new-file');
    }
  },
  {
    label: 'Open File',
    click: openFileHandler
  },
  {
    label: 'Save As',
    click: saveFileHandler
  },
  {
    type: 'separator'
  },
  {
    label: 'Quit',
    accelerator: 'Command+Q',
    click: function() {
      app.quit();
    }
  }]
}]);

function openFileHandler () {
  dialog.showOpenDialog({ filters: [{ name: 'markdown', extensions: ['md'] }]}, function (fileNames) {
    if (fileNames === undefined) return;
    var fileName = fileNames[0];
    fs.readFile(fileName, 'utf-8', function (err, data) {
      mainWindow.webContents.send('load-file', data);
    });
  });
}

function saveFileHandler() {
  mainWindow.webContents.send('get-editor-content');

  ipcMain.once('editor-content', function(event, arg) {
    let fileName = dialog.showSaveDialog();

    if (fileName !== undefined) {
      fs.writeFile(fileName, arg);
    }
  });
}

function createMainWindow() {
  mainWindow = new BrowserWindow({width: 800, height: 600, title: 'MdEditor'});
  mainWindow.loadURL('file://' + __dirname + '/app/index.html');
  mainWindow.maximize();

  Menu.setApplicationMenu(mainMenu);

  mainWindow.on('closed', function() {
    mainWindow = null;
  });
}

app.on('ready', createMainWindow);

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', function () {
  if (mainWindow === null) {
    createMainWindow();
  }
});

@@ -269,16 +269,30 @@
 nWindow;
+%0Alet fileName;
 %0A%0Aconst 
@@ -404,73 +404,82 @@
 ck: 
-function() %7B
+newFileHandler%0A  %7D,
 %0A  
+%7B%0A
     
-mainWindow.webContents.send('new-f
+label: 'Open F
 ile'
-);
+,
 %0A    
-%7D
+click: openFileHandler
 %0A  %7D
@@ -492,32 +492,27 @@
     label: '
-Open Fil
+Sav
 e',%0A    clic
@@ -506,36 +506,36 @@
 ve',%0A    click: 
-open
+save
 FileHandler%0A  %7D,
@@ -568,32 +568,34 @@
 %0A    click: save
+As
 FileHandler%0A  %7D,
@@ -694,54 +694,139 @@
 ck: 
-function() %7B%0A      app.quit(
+quitProgramHandler%0A  %7D%5D%0A%7D%5D);%0A%0Afunction newFileHandler() %7B%0A  mainWindow.webContents.send('new-file'
 );%0A  
- 
- %7D%0A  %7D%5D%0A%7D%5D);
+disableSaveMenuPosition();%0A%7D
 %0A%0Afu
@@ -1005,12 +1005,8 @@
     
-var 
 file
@@ -1138,16 +1138,48 @@
  data);%0A
+      enableSaveMenuPosition();%0A
     %7D);%0A
@@ -1196,24 +1196,26 @@
 unction save
+As
 FileHandler(
@@ -1332,20 +1332,16 @@
 ) %7B%0A    
-let 
 fileName
@@ -1446,14 +1446,462 @@
     
-%7D%0A  %7D)
+  enableSaveMenuPosition();%0A    %7D%0A  %7D);%0A%7D%0A%0Afunction saveFileHandler() %7B%0A  mainWindow.webContents.send('get-editor-content');%0A%0A  ipcMain.once('editor-content', function(event, arg) %7B%0A    fs.writeFile(fileName, arg);%0A  %7D);%0A%7D%0A%0Afunction quitProgramHandler() %7B%0A  app.quit();%0A%7D%0A%0Afunction disableSaveMenuPosition() %7B%0A  mainMenu.items%5B1%5D.submenu.items%5B2%5D.enabled = false;%0A%7D%0A%0Afunction enableSaveMenuPosition() %7B%0A  mainMenu.items%5B1%5D.submenu.items%5B2%5D.enabled = true
 ;%0A%7D%0A
@@ -2102,16 +2102,45 @@
 ize();%0A%0A
+  disableSaveMenuPosition();%0A
   Menu.s

add 'Save' item to menu