Skip to content

Commit

Permalink
Add option to be prompted for which folder a note should be created in
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Jun 21, 2021
1 parent f203905 commit 0276337
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 53 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
14 changes: 12 additions & 2 deletions src/engine/TemplateChoiceEngine.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -17,7 +17,17 @@ export class TemplateChoiceEngine extends TemplateEngine {

public async run(): Promise<void> {
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;

Expand Down
103 changes: 59 additions & 44 deletions src/gui/ChoiceBuilder/templateChoiceBuilder.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/types/choices/ITemplateChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/types/choices/TemplateChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/utility.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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);
}
7 changes: 7 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"0.2.3": "0.12.4"
"0.2.4": "0.12.4"
}

0 comments on commit 0276337

Please sign in to comment.