~dricottone/noticable

3ec618a6239ae35e9fb72e5e5ef0c13a5fc3e44d — Dominic Ricottone 2 years ago 0476f14 dev
Flatten if/else blocks into one-liners
2 files changed, 30 insertions(+), 65 deletions(-)

M main.js
M preload.js
M main.js => main.js +22 -47
@@ 56,11 56,8 @@ function relativeNotePath(filename) {
function promptSave() {
  dialog.showSaveDialog(win, optionsSaveAs)
  .then(r => {
    if (r.canceled) {
      announceFileNotSaved();
    } else {
      preloadSaveFile(relativeNotePath(r.filePath));
    }
    if (r.canceled) announceFileNotSaved();
    else preloadSaveFile(relativeNotePath(r.filePath));
  });
};



@@ 68,16 65,12 @@ function promptSave() {
function rePromptSave() {
  dialog.showMessageBox(win, optionsSaveError)
  .then(r => {
    if (r.response==0) {
      announceFileNotSaved();
    } else {
    if (r.response==0) announceFileNotSaved();
    else {
      dialog.showSaveDialog(win, optionsSaveAs)
      .then(r => {
        if (r.canceled) {
          announceFileNotSaved();
        } else {
          preloadReSaveFile(relativeNotePath(r.filePath));
        }
        if (r.canceled) announceFileNotSaved();
        else preloadReSaveFile(relativeNotePath(r.filePath));
      });
    }
  });


@@ 88,18 81,13 @@ function rePromptSave() {
function promptSaveDiscardableThenNew() {
  dialog.showMessageBox(win, optionsDiscard)
  .then(r => {
    if (r.response==0) {
      announceFileDiscardedForNewFile();
    } else if (r.response==1) {
      preloadTrySaveFileThenNewFile();
    } else {
    if (r.response==0) announceFileDiscardedForNewFile();
    else if (r.response==1) preloadTrySaveFileThenNewFile();
    else {
      dialog.showSaveDialog(win, optionsSaveAs)
      .then(r => {
        if (r.canceled) {
          announceFileDiscardedForNewFile();
        } else {
          preloadSaveFileThenNewFile(relativeNotePath(r.filePath));
        }
        if (r.canceled) announceFileDiscardedForNewFile();
        else preloadSaveFileThenNewFile(relativeNotePath(r.filePath));
      });
    }
  });


@@ 110,18 98,13 @@ function promptSaveDiscardableThenNew() {
function promptSaveDiscardableThenRead(filename) {
  dialog.showMessageBox(win, optionsDiscard)
  .then(r => {
    if (r.response==0) {
      announceFileDiscardedForReadFile(filename);
    } else if (r.response==1) {
      preloadTrySaveFileThenReadFile(filename);
    } else {
    if (r.response==0) announceFileDiscardedForReadFile(filename);
    else if (r.response==1) preloadTrySaveFileThenReadFile(filename);
    else {
      dialog.showSaveDialog(win, optionsSaveAs)
      .then(r => {
        if (r.canceled) {
          announceFileDiscardedForReadFile(filename);
        } else {
          preloadSaveFileThenReadFile(relativeNotePath(r.filePath), filename);
        }
        if (r.canceled) announceFileDiscardedForReadFile(filename);
        else preloadSaveFileThenReadFile(relativeNotePath(r.filePath), filename);
      });
    }
  });


@@ 132,16 115,12 @@ function promptSaveDiscardableThenRead(filename) {
function rePromptSaveDiscardableThenNew() {
  dialog.showMessageBox(win, optionsSaveError)
  .then(r => {
    if (r.response==0) {
      announceFileDiscardedForNewFile();
    } else {
    if (r.response==0) announceFileDiscardedForNewFile();
    else {
      dialog.showSaveDialog(win, optionsSaveAs)
      .then(r => {
        if (r.canceled) {
          announceFileDiscardedForNewFile();
        } else {
          preloadReSaveFileThenNewFile(relativeNotePath(r.filePath));
        }
        if (r.canceled) announceFileDiscardedForNewFile();
        else preloadReSaveFileThenNewFile(relativeNotePath(r.filePath));
      });
    }
  });


@@ 377,14 356,10 @@ function initializeWindow() {
app.on("ready", initializeWindow);

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

app.on("activate", () => {
  if (win === null) {
    initializeWindow();
  }
  if (win === null) initializeWindow();
});


M preload.js => preload.js +8 -18
@@ 36,11 36,8 @@ function saveFile(filename) {
// Otherwise ask main to prompt for a new file name and proceed through
// `saveFile` logic.
function trySaveFile() {
  if (currentFile == "") {
    mainPromptSave();
  } else {
    rendererSendContentForSave();
  }
  if (currentFile == "") mainPromptSave();
  else rendererSendContentForSave();
};

// Cache the new file name and ask renderer to send editor content to be saved


@@ 54,22 51,16 @@ function saveFileThenNewFile(filename) {
// Otherwise ask main to prompt for a new file name and proceed through
// `saveFileThenNewFile` logic.
function trySaveFileThenNewFile() {
  if (currentFile == "") {
    mainPromptSaveDiscardableThenNew();
  } else {
    rendererSendContentForSaveThenNew();
  }
  if (currentFile == "") mainPromptSaveDiscardableThenNew();
  else rendererSendContentForSaveThenNew();
};

// If the file name is cached, save the cached content to it. Otherwise ask
// main to prompt for a new file name and proceed through
// `saveFileThenReadFile` logic.
function trySaveFileThenReadFile(filename) {
  if (currentFile == "") {
    mainPromptSaveDiscardableThenRead(filename);
  } else {
    writeFileThenReadFile(currentFile, currentNote, filename);
  }
  if (currentFile == "") mainPromptSaveDiscardableThenRead(filename);
  else writeFileThenReadFile(currentFile, currentNote, filename);
};

// Read a directory and return a sorted array of all file names.


@@ 81,9 72,8 @@ function readNotesDirectory(directory) {
// Read a file and send the content and title to the renderer.
function readNoteFromFile(filename) {
  fs.readFile(relativeNotePath(filename), "utf8", (err, content) => {
    if (err) {
      announceFileUnreadable();
    } else {
    if (err) announceFileUnreadable();
    else {
      currentFile = filename;
      currentNote = content;
      rendererNewTitle(filename);