Destination Folder Option for Each Note

This commit is contained in:
Ozan Tellioglu 2023-03-12 12:18:44 +01:00
parent 9909b3b019
commit 02552d216b
2 changed files with 49 additions and 17 deletions

View file

@ -3,6 +3,7 @@ import { Modal, TFolder, Notice } from 'obsidian';
import { createNewMarkdownFile } from './util/utils';
import { stripIndents } from 'common-tags';
import dayjs from 'dayjs';
import { FolderSuggest } from 'settings/suggestor';
export class CreateNoteModal extends Modal {
plugin: OZCalendarPlugin;
@ -18,40 +19,54 @@ export class CreateNoteModal extends Modal {
let { contentEl } = this;
let thisModal = this;
const headerEl = contentEl.createEl('div', { text: 'Create Note: Provide Name' });
const headerEl = contentEl.createEl('div', { text: 'Create Note' });
headerEl.addClass('modal-title');
let inputCss = 'width: 100%; height: 2.5em;';
// Input El
const inputEl = contentEl.createEl('input');
inputEl.style.cssText = 'width: 100%; height: 2.5em; margin-bottom: 15px;';
contentEl.createEl('p', { text: 'File Name:' });
const fileNameInputEl = contentEl.createEl('input');
fileNameInputEl.style.cssText = inputCss;
let defFileNamePref = this.plugin.settings.defaultFileNamePrefix;
if (defFileNamePref !== '' && dayjs(new Date(), defFileNamePref, true).isValid()) {
inputEl.value = dayjs().format(defFileNamePref) + ' ';
fileNameInputEl.value = dayjs().format(defFileNamePref) + ' ';
}
inputEl.focus();
fileNameInputEl.focus();
// Folder Select
let folderInputEl: HTMLInputElement = null;
if (this.plugin.settings.showDestinationFolderDuringCreate) {
contentEl.createEl('p', { text: 'Destination Folder:' });
folderInputEl = contentEl.createEl('input');
new FolderSuggest(folderInputEl);
folderInputEl.value = this.plugin.settings.defaultFolder;
folderInputEl.style.cssText = inputCss + '; margin-bottom: 15px;';
}
// Create - Cancel Buttons
const createButton = contentEl.createEl('button', { text: 'Create Note' });
const cancelButton = contentEl.createEl('button', { text: 'Cancel' });
cancelButton.style.cssText = 'float: right;';
cancelButton.addEventListener('click', () => {
thisModal.close();
});
let defaultNewFileText = stripIndents`
---
${this.plugin.settings.yamlKey}: ${dayjs().format(this.plugin.settings.dateFormat)}
---
`;
const onClickCreateButton = async () => {
let newFileName = inputEl.value;
let newFileName = fileNameInputEl.value;
if (newFileName !== '') {
let defFolderSrc = this.plugin.settings.defaultFolder;
let defFolderSrc = folderInputEl ? folderInputEl.value : this.plugin.settings.defaultFolder;
let defFolder = this.app.vault.getAbstractFileByPath(defFolderSrc);
if (defFolder && defFolder instanceof TFolder) {
// Default Text Preparation for File
let defaultNewFileText = stripIndents`
---
${this.plugin.settings.yamlKey}: ${dayjs().format(this.plugin.settings.dateFormat)}
---
`;
// Create the MD File and close the modal
await createNewMarkdownFile(this.plugin, defFolder, newFileName, defaultNewFileText);
thisModal.close();
}
@ -61,7 +76,7 @@ export class CreateNoteModal extends Modal {
};
createButton.addEventListener('click', onClickCreateButton);
inputEl.addEventListener('keydown', async (e) => {
fileNameInputEl.addEventListener('keydown', async (e) => {
if (e.key === 'Enter') await onClickCreateButton();
});
}

View file

@ -9,6 +9,7 @@ export interface OZCalendarPluginSettings {
defaultFolder: string;
defaultFileNamePrefix: string;
fixedCalendar: boolean;
showDestinationFolderDuringCreate: boolean;
}
export const DEFAULT_SETTINGS: OZCalendarPluginSettings = {
@ -18,6 +19,7 @@ export const DEFAULT_SETTINGS: OZCalendarPluginSettings = {
defaultFolder: '/',
defaultFileNamePrefix: 'YYYY-MM-DD',
fixedCalendar: true,
showDestinationFolderDuringCreate: true,
};
export class OZCalendarPluginSettingsTab extends PluginSettingTab {
@ -123,8 +125,6 @@ export class OZCalendarPluginSettingsTab extends PluginSettingTab {
this.plugin.settings.defaultFolder = new_folder;
this.plugin.saveSettings();
});
// @ts-ignore
cb.containerEl.addClass('templater_search');
});
new Setting(containerEl)
@ -139,6 +139,23 @@ export class OZCalendarPluginSettingsTab extends PluginSettingTab {
});
});
new Setting(containerEl)
.setName('Show Destination Folder During File Creation')
.setDesc(
`
Disable this if you dont want to see the destination folder selection during
the file creation process. The value is always going to be defaulted to the
selected Default Folder above. You can change the destination folder for each
folder separately but the default value will always stay same.
`
)
.addToggle((toggle) => {
toggle.setValue(this.plugin.settings.showDestinationFolderDuringCreate).onChange((newValue) => {
this.plugin.settings.showDestinationFolderDuringCreate = newValue;
this.plugin.saveSettings();
});
});
containerEl.createEl('h2', { text: 'Style Settings' });
containerEl.createEl('p', {