diff --git a/manifest.json b/manifest.json index 494dd63..64d4142 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "quickadd", "name": "QuickAdd", - "version": "0.2.3", + "version": "0.2.4", "minAppVersion": "0.12.00", "description": "Quickly add new pages or content to your vault.", "author": "Christian B. B. Houmann", diff --git a/package.json b/package.json index 02d9b46..a83a427 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "quickadd", - "version": "0.2.3", + "version": "0.2.4", "description": "Quickly add new pages or content to your vault.", "main": "main.js", "scripts": { diff --git a/src/engine/TemplateChoiceEngine.ts b/src/engine/TemplateChoiceEngine.ts index bb15800..19f8c26 100644 --- a/src/engine/TemplateChoiceEngine.ts +++ b/src/engine/TemplateChoiceEngine.ts @@ -1,6 +1,6 @@ import type ITemplateChoice from "../types/choices/ITemplateChoice"; import type {App, TFile} from "obsidian"; -import {appendToCurrentLine} from "../utility"; +import {appendToCurrentLine, getAllFolders} from "../utility"; import {MARKDOWN_FILE_EXTENSION_REGEX} from "../constants"; import {log} from "../logger/logManager"; import type QuickAdd from "../main"; @@ -17,7 +17,17 @@ export class TemplateChoiceEngine extends TemplateEngine { public async run(): Promise { try { - const folderPath = await this.getOrCreateFolder(this.choice.folder.folders); + let folderPath: string = ""; + + if (this.choice.folder.enabled) { + let folders: string[] = this.choice.folder.folders; + + if (this.choice.folder?.chooseWhenCreatingNote) { + folders = await getAllFolders(this.app); + } + + folderPath = await this.getOrCreateFolder(folders); + } let filePath; diff --git a/src/gui/ChoiceBuilder/templateChoiceBuilder.ts b/src/gui/ChoiceBuilder/templateChoiceBuilder.ts index f28f042..9c9df7b 100644 --- a/src/gui/ChoiceBuilder/templateChoiceBuilder.ts +++ b/src/gui/ChoiceBuilder/templateChoiceBuilder.ts @@ -1,5 +1,5 @@ import {ChoiceBuilder} from "./choiceBuilder"; -import {App, ButtonComponent, Setting, TextComponent, TFolder} from "obsidian"; +import {App, ButtonComponent, Setting, TextComponent, TFolder, ToggleComponent} from "obsidian"; import type ITemplateChoice from "../../types/choices/ITemplateChoice"; import {FormatSyntaxSuggester} from "../formatSyntaxSuggester"; import {FILE_NAME_FORMAT_SYNTAX} from "../../constants"; @@ -8,7 +8,7 @@ import FolderList from "./FolderList.svelte"; import {FileNameDisplayFormatter} from "../../formatters/fileNameDisplayFormatter"; import {ExclusiveSuggester} from "../exclusiveSuggester"; import {log} from "../../logger/logManager"; -import {getTemplatePaths} from "../../utility"; +import {getAllFolders, getTemplatePaths} from "../../utility"; import {GenericTextSuggester} from "../genericTextSuggester"; export class TemplateChoiceBuilder extends ChoiceBuilder { @@ -88,60 +88,75 @@ export class TemplateChoiceBuilder extends ChoiceBuilder { .setDesc("Create the file in the specified folder. If multiple folders are specified, you will be prompted for which folder to create the file in.") .addToggle(toggle => { toggle.setValue(this.choice.folder.enabled); - toggle.onChange(value => this.choice.folder.enabled = value); + toggle.onChange(value => { + this.choice.folder.enabled = value; + this.reload(); + }); }); - const folderList: HTMLDivElement = this.contentEl.createDiv('folderList'); + if (this.choice.folder.enabled) { + const chooseFolderWhenCreatingNoteContainer: HTMLDivElement = this.contentEl.createDiv('chooseFolderWhenCreatingNoteContainer'); + chooseFolderWhenCreatingNoteContainer.createEl('span', {text: "Choose folder when creating then note"}); + const chooseFolderWhenCreatingNote: ToggleComponent = new ToggleComponent(chooseFolderWhenCreatingNoteContainer); + chooseFolderWhenCreatingNote.setValue(this.choice.folder?.chooseWhenCreatingNote) + .onChange(value => { + this.choice.folder.chooseWhenCreatingNote = value; + this.reload(); + }); - const folderListEl = new FolderList({ - target: folderList, - props: { - folders: this.choice.folder.folders, - deleteFolder: (folder: string) => { - this.choice.folder.folders = this.choice.folder.folders.filter(f => f !== folder); - folderListEl.updateFolders(this.choice.folder.folders); - suggester.updateCurrentItems(this.choice.folder.folders); - } - } - }); + if (!this.choice.folder?.chooseWhenCreatingNote) { + const folderSelectionContainer: HTMLDivElement = this.contentEl.createDiv('folderSelectionContainer'); + const folderList: HTMLDivElement = folderSelectionContainer.createDiv('folderList'); + + const folderListEl = new FolderList({ + target: folderList, + props: { + folders: this.choice.folder.folders, + deleteFolder: (folder: string) => { + this.choice.folder.folders = this.choice.folder.folders.filter(f => f !== folder); + folderListEl.updateFolders(this.choice.folder.folders); + suggester.updateCurrentItems(this.choice.folder.folders); + } + } + }); - this.svelteElements.push(folderListEl); + this.svelteElements.push(folderListEl); - const inputContainer = this.contentEl.createDiv('folderInputContainer'); - const folderInput = new TextComponent(inputContainer); - folderInput.inputEl.style.width = "100%"; - folderInput.setPlaceholder("Folder path"); - const folders: string[] = this.app.vault.getAllLoadedFiles() - .filter(f => f instanceof TFolder) - .map(folder => folder.path); + const inputContainer = folderSelectionContainer.createDiv('folderInputContainer'); + const folderInput = new TextComponent(inputContainer); + folderInput.inputEl.style.width = "100%"; + folderInput.setPlaceholder("Folder path"); + const allFolders: string[] = getAllFolders(this.app); - const suggester = new ExclusiveSuggester(this.app, folderInput.inputEl, folders, this.choice.folder.folders); + const suggester = new ExclusiveSuggester(this.app, folderInput.inputEl, allFolders, this.choice.folder.folders); - const addFolder = () => { - const input = folderInput.inputEl.value.trim(); + const addFolder = () => { + const input = folderInput.inputEl.value.trim(); - if (this.choice.folder.folders.some(folder => folder === input)) { - log.logWarning("cannot add same folder twice."); - return; - } + if (this.choice.folder.folders.some(folder => folder === input)) { + log.logWarning("cannot add same folder twice."); + return; + } - this.choice.folder.folders.push(input); - folderListEl.updateFolders(this.choice.folder.folders); - folderInput.inputEl.value = ""; + this.choice.folder.folders.push(input); + folderListEl.updateFolders(this.choice.folder.folders); + folderInput.inputEl.value = ""; - suggester.updateCurrentItems(this.choice.folder.folders); - } + suggester.updateCurrentItems(this.choice.folder.folders); + } - folderInput.inputEl.addEventListener('keypress', (e: KeyboardEvent) => { - if (e.key === 'Enter') { - addFolder(); - } - }); + folderInput.inputEl.addEventListener('keypress', (e: KeyboardEvent) => { + if (e.key === 'Enter') { + addFolder(); + } + }); - const addButton: ButtonComponent = new ButtonComponent(inputContainer); - addButton.setCta().setButtonText("Add").onClick(evt => { - addFolder(); - }); + const addButton: ButtonComponent = new ButtonComponent(inputContainer); + addButton.setCta().setButtonText("Add").onClick(evt => { + addFolder(); + }); + } + } } private addAppendLinkSetting(): void { diff --git a/src/types/choices/ITemplateChoice.ts b/src/types/choices/ITemplateChoice.ts index 4878d69..8c1b403 100644 --- a/src/types/choices/ITemplateChoice.ts +++ b/src/types/choices/ITemplateChoice.ts @@ -3,7 +3,7 @@ import type {NewTabDirection} from "../newTabDirection"; export default interface ITemplateChoice extends IChoice { templatePath: string; - folder: { enabled: boolean, folders: string[] } + folder: { enabled: boolean, folders: string[], chooseWhenCreatingNote: boolean } fileNameFormat: { enabled: boolean, format: string }; appendLink: boolean; incrementFileName: boolean; diff --git a/src/types/choices/TemplateChoice.ts b/src/types/choices/TemplateChoice.ts index 6e84816..0ac1ca0 100644 --- a/src/types/choices/TemplateChoice.ts +++ b/src/types/choices/TemplateChoice.ts @@ -6,7 +6,7 @@ import {NewTabDirection} from "../newTabDirection"; export class TemplateChoice extends Choice implements ITemplateChoice { appendLink: boolean; fileNameFormat: { enabled: boolean; format: string }; - folder: { enabled: boolean; folders: string[] }; + folder: { enabled: boolean; folders: string[], chooseWhenCreatingNote: boolean }; incrementFileName: boolean; openFileInNewTab: { enabled: boolean; direction: NewTabDirection }; openFile: boolean; @@ -17,7 +17,7 @@ export class TemplateChoice extends Choice implements ITemplateChoice { this.templatePath = ""; this.fileNameFormat = {enabled: false, format: ""}; - this.folder = {enabled: false, folders: []}; + this.folder = {enabled: false, folders: [], chooseWhenCreatingNote: false}; this.openFileInNewTab = {enabled: false, direction: NewTabDirection.vertical}; this.appendLink = false; this.incrementFileName = false; diff --git a/src/utility.ts b/src/utility.ts index 046ce4d..b8cea84 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -1,5 +1,5 @@ import type {App} from "obsidian"; -import {MarkdownView} from "obsidian"; +import {MarkdownView, TFolder} from "obsidian"; import {log} from "./logger/logManager"; export function getTemplater(app: App) { @@ -83,4 +83,10 @@ export function deleteObsidianCommand(app: App, commandId: string) { // @ts-ignore delete app.commands.editorCommands[commandId]; } +} + +export function getAllFolders(app: App): string[] { + return app.vault.getAllLoadedFiles() + .filter(f => f instanceof TFolder) + .map(folder => folder.path); } \ No newline at end of file diff --git a/styles.css b/styles.css index 46a48c1..d92a426 100644 --- a/styles.css +++ b/styles.css @@ -103,4 +103,11 @@ display: flex; align-content: center; justify-content: center; +} + +.chooseFolderWhenCreatingNoteContainer { + display: flex; + align-content: center; + justify-content: space-between; + margin-bottom: 10px; } \ No newline at end of file diff --git a/versions.json b/versions.json index 553de29..f605876 100644 --- a/versions.json +++ b/versions.json @@ -1,3 +1,3 @@ { - "0.2.3": "0.12.4" + "0.2.4": "0.12.4" }