From ba229da0f224ec6be839246ca7016668d6c88041 Mon Sep 17 00:00:00 2001
From: Amit Karamchandani Batra <amitkmbt@gmail.com>
Date: Mon, 30 Oct 2023 17:43:27 +0100
Subject: [PATCH 1/2] feat: #18 add an option for allowing to open an existing
 quick note when creating a new one

---
 main.js | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)

diff --git a/main.js b/main.js
index 7f7c461..072392b 100644
--- a/main.js
+++ b/main.js
@@ -15,6 +15,10 @@ const LOG_PREFIX = "obsidian-tray",
   LOG_TRAY_ICON = "creating tray icon",
   LOG_REGISTER_HOTKEY = "registering hotkey",
   LOG_UNREGISTER_HOTKEY = "unregistering hotkey",
+  LOG_QUICK_NOTE_LOCATION = "quick note location",
+  LOG_QUICK_NOTE_EXISTS = "quick note already exists",
+  LOG_QUICK_NOTE_CREATED = "quick note created",
+  LOG_QUICK_NOTE_OPENED = "quick note opened",
   ACTION_QUICK_NOTE = "Quick Note",
   ACTION_SHOW = "Show Vault",
   ACTION_HIDE = "Hide Vault",
@@ -173,10 +177,31 @@ const addQuickNote = () => {
       leaf = plugin.app.workspace.getLeaf(),
       root = plugin.app.fileManager.getNewFileParent(""),
       openMode = { active: true, state: { mode: "source" } };
-    plugin.app.fileManager
-      .createNewMarkdownFile(root, name)
-      .then((file) => leaf.openFile(file, openMode));
-    showWindows();
+
+    log(LOG_QUICK_NOTE_LOCATION + ": " + name);
+    const fileExists = plugin.app.vault.adapter.exists(`${name}.md`);
+
+    fileExists.then((isFile) => {
+      // Check if the file already exists
+      if (isFile === false) {
+        // If it doesn't, create it
+        log(LOG_QUICK_NOTE_CREATED + ": " + name);
+        plugin.app.fileManager
+          .createNewMarkdownFile(root, name)
+          .then((file) => {
+            leaf.openFile(file, openMode);
+            log(LOG_QUICK_NOTE_OPENED + ": " + name);
+          });
+      } else {
+        // If it does, open it
+        log(LOG_QUICK_NOTE_EXISTS + ": " + name);
+        const file = plugin.app.vault.getAbstractFileByPath(`${name}.md`);
+        leaf.openFile(file, openMode);
+        log(LOG_QUICK_NOTE_OPENED + ": " + name);
+      }
+
+      showWindows();
+    });
   },
   replaceVaultName = (str) => {
     return str.replace(/{{vault}}/g, plugin.app.vault.getName());

From d7a6765a661626d1aa6d8ec6abe62d5b4ee6bed3 Mon Sep 17 00:00:00 2001
From: Amit Karamchandani Batra <amitkmbt@gmail.com>
Date: Sun, 11 Feb 2024 00:40:44 +0100
Subject: [PATCH 2/2] feat: #18 add functionality for configuring separate
 directories and note filename formats for quick notes and daily notes, with
 independent hotkey settings

---
 main.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 88 insertions(+), 6 deletions(-)

diff --git a/main.js b/main.js
index 072392b..d608dba 100644
--- a/main.js
+++ b/main.js
@@ -20,6 +20,11 @@ const LOG_PREFIX = "obsidian-tray",
   LOG_QUICK_NOTE_CREATED = "quick note created",
   LOG_QUICK_NOTE_OPENED = "quick note opened",
   ACTION_QUICK_NOTE = "Quick Note",
+  LOG_DAILY_NOTE_LOCATION = "daily note location",
+  LOG_DAILY_NOTE_EXISTS = "daily note already exists",
+  LOG_DAILY_NOTE_CREATED = "daily note created",
+  LOG_DAILY_NOTE_OPENED = "daily note opened",
+  ACTION_DAILY_NOTE = "Daily Note",
   ACTION_SHOW = "Show Vault",
   ACTION_HIDE = "Hide Vault",
   ACTION_RELAUNCH = "Relaunch Obsidian",
@@ -182,9 +187,7 @@ const addQuickNote = () => {
     const fileExists = plugin.app.vault.adapter.exists(`${name}.md`);
 
     fileExists.then((isFile) => {
-      // Check if the file already exists
-      if (isFile === false) {
-        // If it doesn't, create it
+      if (isFile === false || plugin.settings.quickNoteOpensExistingNote === false) {
         log(LOG_QUICK_NOTE_CREATED + ": " + name);
         plugin.app.fileManager
           .createNewMarkdownFile(root, name)
@@ -193,7 +196,6 @@ const addQuickNote = () => {
             log(LOG_QUICK_NOTE_OPENED + ": " + name);
           });
       } else {
-        // If it does, open it
         log(LOG_QUICK_NOTE_EXISTS + ": " + name);
         const file = plugin.app.vault.getAbstractFileByPath(`${name}.md`);
         leaf.openFile(file, openMode);
@@ -203,6 +205,44 @@ const addQuickNote = () => {
       showWindows();
     });
   },
+  addDailyNote = () => {
+    const { dailyNoteLocation, dailyNoteDateFormat } = plugin.settings,
+      pattern = dailyNoteDateFormat || DEFAULT_DATE_FORMAT,
+      date = obsidian.moment().format(pattern),
+      name = obsidian
+        .normalizePath(`${dailyNoteLocation ?? ""}/${date}`)
+        .replace(/\*|"|\\|<|>|:|\||\?/g, "-"),
+      // manually create and open file instead of depending
+      // on createAndOpenMarkdownFile to force file creation
+      // relative to the root instead of the active file
+      // (in case user has default location for new notes
+      // set to "same folder as current file")
+      leaf = plugin.app.workspace.getLeaf(),
+      root = plugin.app.fileManager.getNewFileParent(""),
+      openMode = { active: true, state: { mode: "source" } };
+
+    log(LOG_DAILY_NOTE_LOCATION + ": " + name);
+    const fileExists = plugin.app.vault.adapter.exists(`${name}.md`);
+
+    fileExists.then((isFile) => {
+      if (isFile === false) {
+        log(LOG_DAILY_NOTE_CREATED + ": " + name);
+        plugin.app.fileManager
+          .createNewMarkdownFile(root, name)
+          .then((file) => {
+            leaf.openFile(file, openMode);
+            log(LOG_DAILY_NOTE_OPENED + ": " + name);
+          });
+      } else {
+        log(LOG_DAILY_NOTE_EXISTS + ": " + name);
+        const file = plugin.app.vault.getAbstractFileByPath(`${name}.md`);
+        leaf.openFile(file, openMode);
+        log(LOG_DAILY_NOTE_OPENED + ": " + name);
+      }
+
+      showWindows();
+    });
+  },
   replaceVaultName = (str) => {
     return str.replace(/{{vault}}/g, plugin.app.vault.getName());
   },
@@ -224,6 +264,12 @@ const addQuickNote = () => {
           accelerator: plugin.settings.quickNoteHotkey,
           click: addQuickNote,
         },
+        {
+          type: "normal",
+          label: ACTION_DAILY_NOTE,
+          accelerator: plugin.settings.dailyNoteHotkey,
+          click: addDailyNote,
+        },
         {
           type: "normal",
           label: ACTION_SHOW,
@@ -255,13 +301,16 @@ const addQuickNote = () => {
 const registerHotkeys = () => {
     log(LOG_REGISTER_HOTKEY);
     try {
-      const { toggleWindowFocusHotkey, quickNoteHotkey } = plugin.settings;
+      const { toggleWindowFocusHotkey, quickNoteHotkey, dailyNoteHotkey } = plugin.settings;
       if (toggleWindowFocusHotkey) {
         globalShortcut.register(toggleWindowFocusHotkey, toggleWindows);
       }
       if (quickNoteHotkey) {
         globalShortcut.register(quickNoteHotkey, addQuickNote);
       }
+      if (dailyNoteHotkey) {
+        globalShortcut.register(dailyNoteHotkey, addDailyNote);
+      }
     } catch {}
   },
   unregisterHotkeys = () => {
@@ -359,6 +408,30 @@ const OPTIONS = [
     onBeforeChange: unregisterHotkeys,
     onChange: registerHotkeys,
   },
+  "Daily notes",
+  {
+    key: "dailyNoteLocation",
+    desc: "New daily notes will be placed in this folder.",
+    type: "text",
+    placeholder: "Example: notes/daily",
+  },
+  {
+    key: "dailyNoteDateFormat",
+    desc: `
+      New daily notes will use a filename of this pattern. ${MOMENT_FORMAT}
+      <br>Preview: <b class="u-pop" data-preview></b>
+    `,
+    type: "moment",
+    default: DEFAULT_DATE_FORMAT,
+  },
+  {
+    key: "dailyNoteHotkey",
+    desc: ACCELERATOR_FORMAT,
+    type: "hotkey",
+    default: "CmdOrCtrl+Shift+D",
+    onBeforeChange: unregisterHotkeys,
+    onChange: registerHotkeys,
+  },
   "Quick notes",
   {
     key: "quickNoteLocation",
@@ -379,10 +452,19 @@ const OPTIONS = [
     key: "quickNoteHotkey",
     desc: ACCELERATOR_FORMAT,
     type: "hotkey",
-    default: "CmdOrCtrl+Shift+Q",
+    default: "CmdOrCtrl+Shift+D",
     onBeforeChange: unregisterHotkeys,
     onChange: registerHotkeys,
   },
+  {
+    key: "quickNoteOpensExistingNote",
+    desc: `
+      If a quick note already exists for the current date, open it instead of
+      creating a new note.
+    `,
+    type: "toggle",
+    default: true,
+  },
 ];
 
 const keyToLabel = (key) =>