Skip to content

Commit

Permalink
Small fixes (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann authored Jun 24, 2021
1 parent 5e8847f commit ca2956b
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 31 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.8",
"version": "0.2.9",
"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.8",
"version": "0.2.9",
"description": "Quickly add new pages or content to your vault.",
"main": "main.js",
"scripts": {
Expand Down
30 changes: 21 additions & 9 deletions src/engine/CaptureChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/engine/MacroChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {
}

async run(): Promise<void> {
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);
Expand Down
4 changes: 0 additions & 4 deletions src/engine/SingleTemplateEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 3 additions & 5 deletions src/engine/TemplateEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -18,7 +18,7 @@ export abstract class TemplateEngine extends QuickAddEngine {
this.formatter = new CompleteFormatter(app, plugin, choiceFormatter);
}

public abstract run(): Promise<void> | Promise<string>;
public abstract run(): Promise<void> | Promise<string> | Promise<{file: TFile, content: string}>;

protected async getOrCreateFolder(folders: string[]): Promise<string> {
let folderPath: string;
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion src/formatters/completeFormatter.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
4 changes: 2 additions & 2 deletions src/gui/ChoiceBuilder/captureChoiceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export class CaptureChoiceBuilder extends ChoiceBuilder {
}

this.addTaskSetting();
this.addPrependSetting();

if (!this.choice.captureToActiveFile) {
this.addPrependSetting();
this.addAppendLinkSetting();
this.addInsertAfterSetting();
}
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/gui/choiceList/ChoiceView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/types/choices/ICaptureChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion src/utility.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"0.2.8": "0.12.4"
"0.2.9": "0.12.4"
}

0 comments on commit ca2956b

Please sign in to comment.