mirror of
https://github.com/dragonwocky/obsidian-tray.git
synced 2025-04-10 14:29:03 +00:00
feat: #18 add functionality for configuring separate directories and note filename formats for quick notes and daily notes, with independent hotkey settings
This commit is contained in:
parent
ba229da0f2
commit
d7a6765a66
94
main.js
94
main.js
@ -20,6 +20,11 @@ const LOG_PREFIX = "obsidian-tray",
|
|||||||
LOG_QUICK_NOTE_CREATED = "quick note created",
|
LOG_QUICK_NOTE_CREATED = "quick note created",
|
||||||
LOG_QUICK_NOTE_OPENED = "quick note opened",
|
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",
|
||||||
@ -182,9 +187,7 @@ const addQuickNote = () => {
|
|||||||
const fileExists = plugin.app.vault.adapter.exists(`${name}.md`);
|
const fileExists = plugin.app.vault.adapter.exists(`${name}.md`);
|
||||||
|
|
||||||
fileExists.then((isFile) => {
|
fileExists.then((isFile) => {
|
||||||
// Check if the file already exists
|
if (isFile === false || plugin.settings.quickNoteOpensExistingNote === false) {
|
||||||
if (isFile === false) {
|
|
||||||
// If it doesn't, create it
|
|
||||||
log(LOG_QUICK_NOTE_CREATED + ": " + name);
|
log(LOG_QUICK_NOTE_CREATED + ": " + name);
|
||||||
plugin.app.fileManager
|
plugin.app.fileManager
|
||||||
.createNewMarkdownFile(root, name)
|
.createNewMarkdownFile(root, name)
|
||||||
@ -193,7 +196,6 @@ const addQuickNote = () => {
|
|||||||
log(LOG_QUICK_NOTE_OPENED + ": " + name);
|
log(LOG_QUICK_NOTE_OPENED + ": " + name);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// If it does, open it
|
|
||||||
log(LOG_QUICK_NOTE_EXISTS + ": " + name);
|
log(LOG_QUICK_NOTE_EXISTS + ": " + name);
|
||||||
const file = plugin.app.vault.getAbstractFileByPath(`${name}.md`);
|
const file = plugin.app.vault.getAbstractFileByPath(`${name}.md`);
|
||||||
leaf.openFile(file, openMode);
|
leaf.openFile(file, openMode);
|
||||||
@ -203,6 +205,44 @@ const addQuickNote = () => {
|
|||||||
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());
|
||||||
},
|
},
|
||||||
@ -224,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,
|
||||||
@ -255,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 = () => {
|
||||||
@ -359,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",
|
||||||
@ -379,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) =>
|
||||||
|
Loading…
Reference in New Issue
Block a user