Skip to content

Commit ca2956b

Browse files
authored
Small fixes (#30)
1 parent 5e8847f commit ca2956b

File tree

13 files changed

+51
-31
lines changed

13 files changed

+51
-31
lines changed

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "quickadd",
33
"name": "QuickAdd",
4-
"version": "0.2.8",
4+
"version": "0.2.9",
55
"minAppVersion": "0.12.00",
66
"description": "Quickly add new pages or content to your vault.",
77
"author": "Christian B. B. Houmann",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "quickadd",
3-
"version": "0.2.8",
3+
"version": "0.2.9",
44
"description": "Quickly add new pages or content to your vault.",
55
"main": "main.js",
66
"scripts": {

src/engine/CaptureChoiceEngine.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type ICaptureChoice from "../types/choices/ICaptureChoice";
22
import type {App, TFile} from "obsidian";
33
import {log} from "../logger/logManager";
44
import {CaptureChoiceFormatter} from "../formatters/captureChoiceFormatter";
5-
import {appendToCurrentLine} from "../utility";
6-
import {MARKDOWN_FILE_EXTENSION_REGEX, VALUE_SYNTAX} from "../constants";
5+
import {appendToCurrentLine, replaceTemplaterTemplatesInCreatedFile} from "../utility";
6+
import {VALUE_SYNTAX} from "../constants";
77
import type QuickAdd from "../main";
88
import {QuickAddChoiceEngine} from "./QuickAddChoiceEngine";
99
import {SingleTemplateEngine} from "./SingleTemplateEngine";
@@ -51,14 +51,13 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
5151
new SingleTemplateEngine(this.app, this.plugin, this.choice.createFileIfItDoesntExist.template, this.choiceExecutor);
5252

5353
const fileContent: string = await singleTemplateEngine.run();
54-
file = await this.createFileWithInput(filePath, fileContent);
55-
if (!file) {
56-
log.logError(`could not create '${filePath}.'`);
57-
return;
58-
}
54+
const file: TFile = await this.createFileWithInput(filePath, fileContent);
55+
await replaceTemplaterTemplatesInCreatedFile(this.app, file);
5956

60-
const newFileContent: string = await this.formatter.formatContentWithFile(content, this.choice, fileContent, file);
57+
const updatedFileContent: string = await this.app.vault.cachedRead(file);
58+
const newFileContent: string = await this.formatter.formatContentWithFile(content, this.choice, updatedFileContent, file);
6159
await this.app.vault.modify(file, newFileContent);
60+
6261
} else {
6362
const formattedContent = await this.formatter.formatContent(content, this.choice);
6463
if (!formattedContent) return;
@@ -106,6 +105,19 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
106105
}
107106

108107
if (!content) return;
109-
appendToCurrentLine(content, this.app);
108+
109+
if (this.choice.prepend) {
110+
const activeFile: TFile = this.app.workspace.getActiveFile();
111+
if (!activeFile) {
112+
log.logError("Cannot capture to active file - no active file.")
113+
}
114+
115+
const fileContent: string = await this.app.vault.cachedRead(activeFile);
116+
const newFileContent: string = `${fileContent}${content}`
117+
118+
await this.app.vault.modify(activeFile, newFileContent);
119+
} else {
120+
appendToCurrentLine(content, this.app);
121+
}
110122
}
111123
}

src/engine/MacroChoiceEngine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {
3939
}
4040

4141
async run(): Promise<void> {
42-
const macroId: string = this.choice.macroId ?? this.choice.macro.id;
42+
const macroId: string = this.choice.macroId ?? this.choice?.macro?.id;
4343
const macro: IMacro = this.macros.find(m => m.id === macroId);
4444

4545
await this.executeCommands(macro.commands);

src/engine/SingleTemplateEngine.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ export class SingleTemplateEngine extends TemplateEngine {
1515

1616
templateContent = await this.formatter.formatFileContent(templateContent);
1717

18-
if (this.templater) {
19-
templateContent = this.templater.templater.parser.parseTemplates(templateContent);
20-
}
21-
2218
return templateContent;
2319
}
2420
}

src/engine/TemplateEngine.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {QuickAddEngine} from "./QuickAddEngine";
22
import {CompleteFormatter} from "../formatters/completeFormatter";
33
import {App, TAbstractFile, TFile} from "obsidian";
44
import type QuickAdd from "../main";
5-
import {getTemplater} from "../utility";
5+
import {getTemplater, replaceTemplaterTemplatesInCreatedFile} from "../utility";
66
import GenericSuggester from "../gui/GenericSuggester/genericSuggester";
77
import {FILE_NUMBER_REGEX} from "../constants";
88
import {log} from "../logger/logManager";
@@ -18,7 +18,7 @@ export abstract class TemplateEngine extends QuickAddEngine {
1818
this.formatter = new CompleteFormatter(app, plugin, choiceFormatter);
1919
}
2020

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

2323
protected async getOrCreateFolder(folders: string[]): Promise<string> {
2424
let folderPath: string;
@@ -71,9 +71,7 @@ export abstract class TemplateEngine extends QuickAddEngine {
7171
const formattedTemplateContent: string = await this.formatter.formatFileContent(templateContent);
7272
const createdFile: TFile = await this.app.vault.create(filePath, formattedTemplateContent);
7373

74-
if (this.templater && !this.templater?.settings["trigger_on_file_creation"]) {
75-
await this.templater.templater.overwrite_file_templates(createdFile);
76-
}
74+
await replaceTemplaterTemplatesInCreatedFile(this.app, createdFile);
7775

7876
return createdFile;
7977
}

src/formatters/completeFormatter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {Formatter} from "./formatter";
22
import type {App, TFile} from "obsidian";
3-
import {MARKDOWN_FILE_EXTENSION_REGEX} from "../constants";
43
import {getNaturalLanguageDates} from "../utility";
54
import GenericInputPrompt from "../gui/GenericInputPrompt/genericInputPrompt";
65
import GenericSuggester from "../gui/GenericSuggester/genericSuggester";

src/gui/ChoiceBuilder/captureChoiceBuilder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ export class CaptureChoiceBuilder extends ChoiceBuilder {
3232
}
3333

3434
this.addTaskSetting();
35+
this.addPrependSetting();
3536

3637
if (!this.choice.captureToActiveFile) {
37-
this.addPrependSetting();
3838
this.addAppendLinkSetting();
3939
this.addInsertAfterSetting();
4040
}
@@ -88,7 +88,7 @@ export class CaptureChoiceBuilder extends ChoiceBuilder {
8888
private addPrependSetting() {
8989
const prependSetting: Setting = new Setting(this.contentEl);
9090
prependSetting.setName("Write to bottom of file")
91-
.setDesc("Put value at the bottom of the file - otherwise at the top.")
91+
.setDesc(`Put value at the bottom of the file - otherwise at the ${this.choice?.captureToActiveFile ? "active cursor location" : "top"}.`)
9292
.addToggle(toggle => {
9393
toggle.setValue(this.choice.prepend);
9494
toggle.onChange(value => this.choice.prepend = value);

src/gui/choiceList/ChoiceView.svelte

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,15 @@
9797
}
9898
9999
function updateChoiceHelper(oldChoice: IChoice, newChoice: IChoice) {
100-
if (oldChoice.id === newChoice.id)
101-
return newChoice;
100+
if (oldChoice.id === newChoice.id) {
101+
oldChoice = {...oldChoice, ...newChoice};
102+
return oldChoice;
103+
}
102104
103105
if (oldChoice.type === ChoiceType.Multi) {
104-
(oldChoice as IMultiChoice).choices.map(c => updateChoiceHelper(c, newChoice))
106+
const multiChoice = (oldChoice as IMultiChoice);
107+
const multiChoiceChoices = multiChoice.choices.map(c => updateChoiceHelper(c, newChoice))
108+
return {...multiChoice, choices: multiChoiceChoices} as IChoice;
105109
}
106110
107111
return oldChoice;

src/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ export default class QuickAdd extends Plugin {
9292
private async convertMacroChoicesMacroToId() {
9393
function convertMacroChoiceMacroToIdHelper(choice: IChoice): IChoice {
9494
if (choice.type === ChoiceType.Multi) {
95-
(choice as IMultiChoice).choices.map(convertMacroChoiceMacroToIdHelper);
96-
return choice;
95+
let multiChoice = (choice as IMultiChoice);
96+
const multiChoices = multiChoice.choices.map(convertMacroChoiceMacroToIdHelper);
97+
multiChoice = {...multiChoice, choices: multiChoices};
98+
return multiChoice;
9799
}
98100

99101
if (choice.type !== ChoiceType.Macro) return choice;

0 commit comments

Comments
 (0)