Compare commits

...

5 Commits

Author SHA1 Message Date
Amit Karamchandani Batra
4708a21ca7
Merge d7a6765a66 into ac353bb5a6 2025-10-20 14:52:07 +02:00
Tom
ac353bb5a6
Merge pull request #66 from achilleas-k/fix/accelerator-docs-url
fix: update documentation URL for Electron accelerator format
2025-10-19 20:06:06 +11:00
Achilleas Koutsou
5e7ee6ca73 fix: update documentation URL for Electron accelerator format
The current URL is outdated (404).  Accelerator documentation is now
in the keyboard shortcuts section of the tutorial.
2025-10-16 11:08:33 +02:00
Amit Karamchandani Batra
d7a6765a66 feat: #18 add functionality for configuring separate directories and note filename formats for quick notes and daily notes, with independent hotkey settings 2024-02-11 00:40:44 +01:00
Amit Karamchandani Batra
ba229da0f2 feat: #18 add an option for allowing to open an existing quick note when creating a new one 2023-10-30 17:46:06 +01:00
2 changed files with 129 additions and 22 deletions

View File

@ -9,7 +9,7 @@ toggle window visibility and create quick notes from anywhere in your operating
### Window management ### Window management
| Option | Description | Default | | Option | Description | Default |
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------ | | -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| ------------------------------ |
| Launch on startup | Open Obsidian automatically whenever you log into your computer. | Disabled | | Launch on startup | Open Obsidian automatically whenever you log into your computer. | Disabled |
| Hide on launch | Minimises Obsidian automatically whenever the app is launched. If the "Run in background" option is enabled, windows will be hidden to the system tray/menubar instead of minimised to the taskbar/dock. | Disabled | | Hide on launch | Minimises Obsidian automatically whenever the app is launched. If the "Run in background" option is enabled, windows will be hidden to the system tray/menubar instead of minimised to the taskbar/dock. | Disabled |
| Run in background | Hide the app and continue to run it in the background instead of quitting it when pressing the window close button or toggle focus hotkey. | Disabled | | Run in background | Hide the app and continue to run it in the background instead of quitting it when pressing the window close button or toggle focus hotkey. | Disabled |
@ -17,7 +17,7 @@ toggle window visibility and create quick notes from anywhere in your operating
| Create tray icon | Add an icon to your system tray/menubar to bring hidden Obsidian windows back into focus on click or force a full quit/relaunch of the app through the right-click menu. | Enabled | | Create tray icon | Add an icon to your system tray/menubar to bring hidden Obsidian windows back into focus on click or force a full quit/relaunch of the app through the right-click menu. | Enabled |
| Tray icon image | Set the image used by the tray/menubar icon. Recommended size: 16x16 | ![](obsidian.png) | | Tray icon image | Set the image used by the tray/menubar icon. Recommended size: 16x16 | ![](obsidian.png) |
| Tray icon tooltip | Set a title to identify the tray/menubar icon by. The `{{vault}}` placeholder will be replaced by the vault name. | `{{vault}} \| Obsidian` | | Tray icon tooltip | Set a title to identify the tray/menubar icon by. The `{{vault}}` placeholder will be replaced by the vault name. | `{{vault}} \| Obsidian` |
| Toggle window focus hotkey | This hotkey is registered globally and will be detected even if Obsidian does not have keyboard focus. Format: [Electron accelerator](https://www.electronjs.org/docs/latest/api/accelerator) | <kbd>CmdOrCtrl+Shift+Tab</kbd> | | Toggle window focus hotkey | This hotkey is registered globally and will be detected even if Obsidian does not have keyboard focus. Format: [Electron accelerator](https://www.electronjs.org/docs/latest/tutorial/keyboard-shortcuts#accelerators) | <kbd>CmdOrCtrl+Shift+Tab</kbd> |
The `Relaunch Obsidian` and `Close Vault` actions can be triggered from the tray/menubar context menu, The `Relaunch Obsidian` and `Close Vault` actions can be triggered from the tray/menubar context menu,
or with the in-app command palette (search for "Tray: Relaunch Obsidian" or "Tray: Close Vault"). or with the in-app command palette (search for "Tray: Relaunch Obsidian" or "Tray: Close Vault").
@ -26,10 +26,10 @@ Hotkeys can be assigned to the commands via Obsidian's built-in hotkey manager.
### Quick notes ### Quick notes
| Option | Description | Default | | Option | Description | Default |
| ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- | | ---------------------- | -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| ---------------------------- |
| Quick note location | New quick notes will be placed in this folder. | | | Quick note location | New quick notes will be placed in this folder. | |
| Quick note date format | New quick notes will use a filename of this pattern. Format: [Moment.js format string](https://momentjs.com/docs/#/displaying/format/) | `YYYY-MM-DD` | | Quick note date format | New quick notes will use a filename of this pattern. Format: [Moment.js format string](https://momentjs.com/docs/#/displaying/format/) | `YYYY-MM-DD` |
| Quick note hotkey | This hotkey is registered globally and will be detected even if Obsidian does not have keyboard focus. Format: [Electron accelerator](https://www.electronjs.org/docs/latest/api/accelerator) | <kbd>CmdOrCtrl+Shift+Q</kbd> | | Quick note hotkey | This hotkey is registered globally and will be detected even if Obsidian does not have keyboard focus. Format: [Electron accelerator](https://www.electronjs.org/docs/latest/tutorial/keyboard-shortcuts#accelerators) | <kbd>CmdOrCtrl+Shift+Q</kbd> |
## Installation ## Installation

115
main.js
View File

@ -15,7 +15,16 @@ const LOG_PREFIX = "obsidian-tray",
LOG_TRAY_ICON = "creating tray icon", LOG_TRAY_ICON = "creating tray icon",
LOG_REGISTER_HOTKEY = "registering hotkey", LOG_REGISTER_HOTKEY = "registering hotkey",
LOG_UNREGISTER_HOTKEY = "unregistering 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_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_SHOW = "Show Vault",
ACTION_HIDE = "Hide Vault", ACTION_HIDE = "Hide Vault",
ACTION_RELAUNCH = "Relaunch Obsidian", ACTION_RELAUNCH = "Relaunch Obsidian",
@ -24,7 +33,7 @@ const LOG_PREFIX = "obsidian-tray",
ACCELERATOR_FORMAT = ` ACCELERATOR_FORMAT = `
This hotkey is registered globally and will be detected even if Obsidian does This hotkey is registered globally and will be detected even if Obsidian does
not have keyboard focus. Format: not have keyboard focus. Format:
<a href="https://www.electronjs.org/docs/latest/api/accelerator" target="_blank" rel="noopener"> <a href="https://www.electronjs.org/docs/latest/tutorial/keyboard-shortcuts#accelerators" target="_blank" rel="noopener">
Electron accelerator</a> Electron accelerator</a>
`, `,
MOMENT_FORMAT = ` MOMENT_FORMAT = `
@ -173,10 +182,66 @@ const addQuickNote = () => {
leaf = plugin.app.workspace.getLeaf(), leaf = plugin.app.workspace.getLeaf(),
root = plugin.app.fileManager.getNewFileParent(""), root = plugin.app.fileManager.getNewFileParent(""),
openMode = { active: true, state: { mode: "source" } }; openMode = { active: true, state: { mode: "source" } };
log(LOG_QUICK_NOTE_LOCATION + ": " + name);
const fileExists = plugin.app.vault.adapter.exists(`${name}.md`);
fileExists.then((isFile) => {
if (isFile === false || plugin.settings.quickNoteOpensExistingNote === false) {
log(LOG_QUICK_NOTE_CREATED + ": " + name);
plugin.app.fileManager plugin.app.fileManager
.createNewMarkdownFile(root, name) .createNewMarkdownFile(root, name)
.then((file) => leaf.openFile(file, openMode)); .then((file) => {
leaf.openFile(file, openMode);
log(LOG_QUICK_NOTE_OPENED + ": " + name);
});
} else {
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(); 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) => { replaceVaultName = (str) => {
return str.replace(/{{vault}}/g, plugin.app.vault.getName()); return str.replace(/{{vault}}/g, plugin.app.vault.getName());
@ -199,6 +264,12 @@ const addQuickNote = () => {
accelerator: plugin.settings.quickNoteHotkey, accelerator: plugin.settings.quickNoteHotkey,
click: addQuickNote, click: addQuickNote,
}, },
{
type: "normal",
label: ACTION_DAILY_NOTE,
accelerator: plugin.settings.dailyNoteHotkey,
click: addDailyNote,
},
{ {
type: "normal", type: "normal",
label: ACTION_SHOW, label: ACTION_SHOW,
@ -230,13 +301,16 @@ const addQuickNote = () => {
const registerHotkeys = () => { const registerHotkeys = () => {
log(LOG_REGISTER_HOTKEY); log(LOG_REGISTER_HOTKEY);
try { try {
const { toggleWindowFocusHotkey, quickNoteHotkey } = plugin.settings; const { toggleWindowFocusHotkey, quickNoteHotkey, dailyNoteHotkey } = plugin.settings;
if (toggleWindowFocusHotkey) { if (toggleWindowFocusHotkey) {
globalShortcut.register(toggleWindowFocusHotkey, toggleWindows); globalShortcut.register(toggleWindowFocusHotkey, toggleWindows);
} }
if (quickNoteHotkey) { if (quickNoteHotkey) {
globalShortcut.register(quickNoteHotkey, addQuickNote); globalShortcut.register(quickNoteHotkey, addQuickNote);
} }
if (dailyNoteHotkey) {
globalShortcut.register(dailyNoteHotkey, addDailyNote);
}
} catch {} } catch {}
}, },
unregisterHotkeys = () => { unregisterHotkeys = () => {
@ -334,6 +408,30 @@ const OPTIONS = [
onBeforeChange: unregisterHotkeys, onBeforeChange: unregisterHotkeys,
onChange: registerHotkeys, 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", "Quick notes",
{ {
key: "quickNoteLocation", key: "quickNoteLocation",
@ -354,10 +452,19 @@ const OPTIONS = [
key: "quickNoteHotkey", key: "quickNoteHotkey",
desc: ACCELERATOR_FORMAT, desc: ACCELERATOR_FORMAT,
type: "hotkey", type: "hotkey",
default: "CmdOrCtrl+Shift+Q", default: "CmdOrCtrl+Shift+D",
onBeforeChange: unregisterHotkeys, onBeforeChange: unregisterHotkeys,
onChange: registerHotkeys, 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) => const keyToLabel = (key) =>