From ca2956bad60d19ddbfb4f1058bd8a0fa47c8371e Mon Sep 17 00:00:00 2001 From: Christian Bager Bach Houmann Date: Thu, 24 Jun 2021 07:46:48 +0100 Subject: [PATCH] Small fixes (#30) --- manifest.json | 2 +- package.json | 2 +- src/engine/CaptureChoiceEngine.ts | 30 +++++++++++++------ src/engine/MacroChoiceEngine.ts | 2 +- src/engine/SingleTemplateEngine.ts | 4 --- src/engine/TemplateEngine.ts | 8 ++--- src/formatters/completeFormatter.ts | 1 - src/gui/ChoiceBuilder/captureChoiceBuilder.ts | 4 +-- src/gui/choiceList/ChoiceView.svelte | 10 +++++-- src/main.ts | 6 ++-- src/types/choices/ICaptureChoice.ts | 1 + src/utility.ts | 10 ++++++- versions.json | 2 +- 13 files changed, 51 insertions(+), 31 deletions(-) diff --git a/manifest.json b/manifest.json index a98dd64..5d91cd7 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "quickadd", "name": "QuickAdd", - "version": "0.2.8", + "version": "0.2.9", "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 6c0941d..62e7c9b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "quickadd", - "version": "0.2.8", + "version": "0.2.9", "description": "Quickly add new pages or content to your vault.", "main": "main.js", "scripts": { diff --git a/src/engine/CaptureChoiceEngine.ts b/src/engine/CaptureChoiceEngine.ts index de2f4f9..eaf9cd8 100644 --- a/src/engine/CaptureChoiceEngine.ts +++ b/src/engine/CaptureChoiceEngine.ts @@ -2,8 +2,8 @@ import type ICaptureChoice from "../types/choices/ICaptureChoice"; import type {App, TFile} from "obsidian"; import {log} from "../logger/logManager"; import {CaptureChoiceFormatter} from "../formatters/captureChoiceFormatter"; -import {appendToCurrentLine} from "../utility"; -import {MARKDOWN_FILE_EXTENSION_REGEX, VALUE_SYNTAX} from "../constants"; +import {appendToCurrentLine, replaceTemplaterTemplatesInCreatedFile} from "../utility"; +import {VALUE_SYNTAX} from "../constants"; import type QuickAdd from "../main"; import {QuickAddChoiceEngine} from "./QuickAddChoiceEngine"; import {SingleTemplateEngine} from "./SingleTemplateEngine"; @@ -51,14 +51,13 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine { new SingleTemplateEngine(this.app, this.plugin, this.choice.createFileIfItDoesntExist.template, this.choiceExecutor); const fileContent: string = await singleTemplateEngine.run(); - file = await this.createFileWithInput(filePath, fileContent); - if (!file) { - log.logError(`could not create '${filePath}.'`); - return; - } + const file: TFile = await this.createFileWithInput(filePath, fileContent); + await replaceTemplaterTemplatesInCreatedFile(this.app, file); - const newFileContent: string = await this.formatter.formatContentWithFile(content, this.choice, fileContent, file); + const updatedFileContent: string = await this.app.vault.cachedRead(file); + const newFileContent: string = await this.formatter.formatContentWithFile(content, this.choice, updatedFileContent, file); await this.app.vault.modify(file, newFileContent); + } else { const formattedContent = await this.formatter.formatContent(content, this.choice); if (!formattedContent) return; @@ -106,6 +105,19 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine { } if (!content) return; - appendToCurrentLine(content, this.app); + + if (this.choice.prepend) { + const activeFile: TFile = this.app.workspace.getActiveFile(); + if (!activeFile) { + log.logError("Cannot capture to active file - no active file.") + } + + const fileContent: string = await this.app.vault.cachedRead(activeFile); + const newFileContent: string = `${fileContent}${content}` + + await this.app.vault.modify(activeFile, newFileContent); + } else { + appendToCurrentLine(content, this.app); + } } } \ No newline at end of file diff --git a/src/engine/MacroChoiceEngine.ts b/src/engine/MacroChoiceEngine.ts index 5fa8aea..a9d8c61 100644 --- a/src/engine/MacroChoiceEngine.ts +++ b/src/engine/MacroChoiceEngine.ts @@ -39,7 +39,7 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine { } async run(): Promise { - const macroId: string = this.choice.macroId ?? this.choice.macro.id; + const macroId: string = this.choice.macroId ?? this.choice?.macro?.id; const macro: IMacro = this.macros.find(m => m.id === macroId); await this.executeCommands(macro.commands); diff --git a/src/engine/SingleTemplateEngine.ts b/src/engine/SingleTemplateEngine.ts index edaf44e..e00273a 100644 --- a/src/engine/SingleTemplateEngine.ts +++ b/src/engine/SingleTemplateEngine.ts @@ -15,10 +15,6 @@ export class SingleTemplateEngine extends TemplateEngine { templateContent = await this.formatter.formatFileContent(templateContent); - if (this.templater) { - templateContent = this.templater.templater.parser.parseTemplates(templateContent); - } - return templateContent; } } \ No newline at end of file diff --git a/src/engine/TemplateEngine.ts b/src/engine/TemplateEngine.ts index f829937..f63e4a9 100644 --- a/src/engine/TemplateEngine.ts +++ b/src/engine/TemplateEngine.ts @@ -2,7 +2,7 @@ import {QuickAddEngine} from "./QuickAddEngine"; import {CompleteFormatter} from "../formatters/completeFormatter"; import {App, TAbstractFile, TFile} from "obsidian"; import type QuickAdd from "../main"; -import {getTemplater} from "../utility"; +import {getTemplater, replaceTemplaterTemplatesInCreatedFile} from "../utility"; import GenericSuggester from "../gui/GenericSuggester/genericSuggester"; import {FILE_NUMBER_REGEX} from "../constants"; import {log} from "../logger/logManager"; @@ -18,7 +18,7 @@ export abstract class TemplateEngine extends QuickAddEngine { this.formatter = new CompleteFormatter(app, plugin, choiceFormatter); } - public abstract run(): Promise | Promise; + public abstract run(): Promise | Promise | Promise<{file: TFile, content: string}>; protected async getOrCreateFolder(folders: string[]): Promise { let folderPath: string; @@ -71,9 +71,7 @@ export abstract class TemplateEngine extends QuickAddEngine { const formattedTemplateContent: string = await this.formatter.formatFileContent(templateContent); const createdFile: TFile = await this.app.vault.create(filePath, formattedTemplateContent); - if (this.templater && !this.templater?.settings["trigger_on_file_creation"]) { - await this.templater.templater.overwrite_file_templates(createdFile); - } + await replaceTemplaterTemplatesInCreatedFile(this.app, createdFile); return createdFile; } diff --git a/src/formatters/completeFormatter.ts b/src/formatters/completeFormatter.ts index 56b9c4e..7024363 100644 --- a/src/formatters/completeFormatter.ts +++ b/src/formatters/completeFormatter.ts @@ -1,6 +1,5 @@ import {Formatter} from "./formatter"; import type {App, TFile} from "obsidian"; -import {MARKDOWN_FILE_EXTENSION_REGEX} from "../constants"; import {getNaturalLanguageDates} from "../utility"; import GenericInputPrompt from "../gui/GenericInputPrompt/genericInputPrompt"; import GenericSuggester from "../gui/GenericSuggester/genericSuggester"; diff --git a/src/gui/ChoiceBuilder/captureChoiceBuilder.ts b/src/gui/ChoiceBuilder/captureChoiceBuilder.ts index 60366ed..0d5406a 100644 --- a/src/gui/ChoiceBuilder/captureChoiceBuilder.ts +++ b/src/gui/ChoiceBuilder/captureChoiceBuilder.ts @@ -32,9 +32,9 @@ export class CaptureChoiceBuilder extends ChoiceBuilder { } this.addTaskSetting(); + this.addPrependSetting(); if (!this.choice.captureToActiveFile) { - this.addPrependSetting(); this.addAppendLinkSetting(); this.addInsertAfterSetting(); } @@ -88,7 +88,7 @@ export class CaptureChoiceBuilder extends ChoiceBuilder { private addPrependSetting() { const prependSetting: Setting = new Setting(this.contentEl); prependSetting.setName("Write to bottom of file") - .setDesc("Put value at the bottom of the file - otherwise at the top.") + .setDesc(`Put value at the bottom of the file - otherwise at the ${this.choice?.captureToActiveFile ? "active cursor location" : "top"}.`) .addToggle(toggle => { toggle.setValue(this.choice.prepend); toggle.onChange(value => this.choice.prepend = value); diff --git a/src/gui/choiceList/ChoiceView.svelte b/src/gui/choiceList/ChoiceView.svelte index c99b9ad..7d05b90 100644 --- a/src/gui/choiceList/ChoiceView.svelte +++ b/src/gui/choiceList/ChoiceView.svelte @@ -97,11 +97,15 @@ } function updateChoiceHelper(oldChoice: IChoice, newChoice: IChoice) { - if (oldChoice.id === newChoice.id) - return newChoice; + if (oldChoice.id === newChoice.id) { + oldChoice = {...oldChoice, ...newChoice}; + return oldChoice; + } if (oldChoice.type === ChoiceType.Multi) { - (oldChoice as IMultiChoice).choices.map(c => updateChoiceHelper(c, newChoice)) + const multiChoice = (oldChoice as IMultiChoice); + const multiChoiceChoices = multiChoice.choices.map(c => updateChoiceHelper(c, newChoice)) + return {...multiChoice, choices: multiChoiceChoices} as IChoice; } return oldChoice; diff --git a/src/main.ts b/src/main.ts index af09b53..465e594 100644 --- a/src/main.ts +++ b/src/main.ts @@ -92,8 +92,10 @@ export default class QuickAdd extends Plugin { private async convertMacroChoicesMacroToId() { function convertMacroChoiceMacroToIdHelper(choice: IChoice): IChoice { if (choice.type === ChoiceType.Multi) { - (choice as IMultiChoice).choices.map(convertMacroChoiceMacroToIdHelper); - return choice; + let multiChoice = (choice as IMultiChoice); + const multiChoices = multiChoice.choices.map(convertMacroChoiceMacroToIdHelper); + multiChoice = {...multiChoice, choices: multiChoices}; + return multiChoice; } if (choice.type !== ChoiceType.Macro) return choice; diff --git a/src/types/choices/ICaptureChoice.ts b/src/types/choices/ICaptureChoice.ts index 0f58f6f..a7e6d83 100644 --- a/src/types/choices/ICaptureChoice.ts +++ b/src/types/choices/ICaptureChoice.ts @@ -5,6 +5,7 @@ export default interface ICaptureChoice extends IChoice { captureToActiveFile: boolean; createFileIfItDoesntExist: {enabled: boolean, createWithTemplate: boolean, template: string}; format: { enabled: boolean, format: string }; + /** Capture to bottom of file (after current file content). */ prepend: boolean; appendLink: boolean; task: boolean; diff --git a/src/utility.ts b/src/utility.ts index 027b993..07821f6 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -1,4 +1,4 @@ -import type {App} from "obsidian"; +import type {App, TFile} from "obsidian"; import {MarkdownView, TFolder} from "obsidian"; import {log} from "./logger/logManager"; @@ -7,6 +7,14 @@ export function getTemplater(app: App) { return app.plugins.plugins["templater-obsidian"] } +export async function replaceTemplaterTemplatesInCreatedFile(app: App, file: TFile) { + const templater = getTemplater(app); + + if (templater && !templater?.settings["trigger_on_file_creation"]) { + await templater.templater.overwrite_file_templates(file); + } +} + export function getTemplatesFolderPath(app: App): string { let path: string = ""; // @ts-ignore diff --git a/versions.json b/versions.json index 6248e47..d8130ac 100644 --- a/versions.json +++ b/versions.json @@ -1,3 +1,3 @@ { - "0.2.8": "0.12.4" + "0.2.9": "0.12.4" }