Skip to content

Commit f7dd5de

Browse files
committed
Capture to current page. Fix tasks not automatically creating linebreaks.
1 parent 23b1c02 commit f7dd5de

File tree

10 files changed

+99
-23
lines changed

10 files changed

+99
-23
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.1.5",
4+
"version": "0.1.6",
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.1.5",
3+
"version": "0.1.6",
44
"description": "Quickly add new pages or content to your vault.",
55
"main": "main.js",
66
"scripts": {

src/engine/CaptureChoiceEngine.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,19 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
2020

2121
async run(): Promise<void> {
2222
try {
23+
if (this.choice?.captureToActiveFile) {
24+
await this.captureToActiveFile();
25+
return;
26+
}
27+
2328
const captureTo = this.choice.captureTo;
2429
if (!captureTo) {
2530
log.logError(`Invalid capture to for ${this.choice.name}`);
2631
return;
2732
}
2833

2934
const filePath = await this.getFilePath(captureTo);
30-
let content: string;
31-
32-
if (!this.choice.format.enabled)
33-
content = await GenericInputPrompt.Prompt(this.app, this.choice.name);
34-
else
35-
content = this.choice.format.format;
36-
37-
if (this.choice.task)
38-
content = `- [ ] ${content}`;
35+
let content = await this.getCaptureContent();
3936

4037
if (await this.fileExists(filePath)) {
4138
const file: TFile = await this.getFileByPath(filePath);
@@ -63,9 +60,32 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
6360
}
6461
}
6562

63+
private async getCaptureContent(): Promise<string> {
64+
let content: string;
65+
66+
if (!this.choice.format.enabled)
67+
content = await GenericInputPrompt.Prompt(this.app, this.choice.name);
68+
else
69+
content = this.choice.format.format;
70+
71+
if (this.choice.task)
72+
content = `- [ ] ${content}\n`;
73+
74+
return content;
75+
}
76+
6677
private async getFilePath(captureTo: string) {
6778
const formattedCaptureTo: string = await this.formatter.formatFileName(captureTo, this.choice.name);
6879
return this.formatFilePath("", formattedCaptureTo);
6980
}
7081

82+
private async captureToActiveFile() {
83+
let content: string = await this.getCaptureContent();
84+
85+
if (this.choice.format.enabled) {
86+
content = await this.formatter.formatContent(content, this.choice);
87+
}
88+
89+
appendToCurrentLine(content, this.app);
90+
}
7191
}

src/gui/ChoiceBuilder/captureChoiceBuilder.ts

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {ChoiceBuilder} from "./choiceBuilder";
22
import type ICaptureChoice from "../../types/choices/ICaptureChoice";
33
import type {App} from "obsidian";
4-
import {Setting, TextAreaComponent} from "obsidian";
4+
import {SearchComponent, Setting, TextAreaComponent, TextComponent, ToggleComponent} from "obsidian";
55
import {FormatSyntaxSuggester} from "../formatSyntaxSuggester";
6-
import {FORMAT_SYNTAX} from "../../constants";
6+
import {FILE_NAME_FORMAT_SYNTAX, FORMAT_SYNTAX} from "../../constants";
77
import {FormatDisplayFormatter} from "../../formatters/formatDisplayFormatter";
88
import type QuickAdd from "../../main";
9+
import {FileNameDisplayFormatter} from "../../formatters/fileNameDisplayFormatter";
10+
import {GenericTextSuggester} from "../genericTextSuggester";
911

1012
export class CaptureChoiceBuilder extends ChoiceBuilder {
1113
choice: ICaptureChoice;
@@ -18,23 +20,62 @@ export class CaptureChoiceBuilder extends ChoiceBuilder {
1820
}
1921

2022
protected display() {
23+
this.contentEl.empty();
24+
2125
this.addCenteredHeader(this.choice.name);
2226
this.addCapturedToSetting();
23-
this.addPrependSetting();
2427
this.addTaskSetting();
25-
this.addAppendLinkSetting();
26-
this.addInsertAfterSetting();
28+
29+
if (!this.choice.captureToActiveFile) {
30+
this.addPrependSetting();
31+
this.addAppendLinkSetting();
32+
this.addInsertAfterSetting();
33+
}
34+
2735
this.addFormatSetting();
2836
}
2937

3038
private addCapturedToSetting() {
39+
let textField: TextComponent;
3140
const captureToSetting: Setting = new Setting(this.contentEl)
3241
.setName('Capture To')
3342
.setDesc('File to capture to. Supports some format syntax.');
3443

35-
this.addFileSearchInputToSetting(captureToSetting, this.choice.captureTo, value => {
36-
this.choice.captureTo = value;
44+
const captureToContainer: HTMLDivElement = this.contentEl.createDiv('captureToContainer');
45+
46+
const captureToActiveFileContainer: HTMLDivElement = captureToContainer.createDiv('captureToActiveFileContainer');
47+
const captureToActiveFileText: HTMLSpanElement = captureToActiveFileContainer.createEl('span');
48+
captureToActiveFileText.textContent = "Capture to active file";
49+
const captureToActiveFileToggle: ToggleComponent = new ToggleComponent(captureToActiveFileContainer);
50+
captureToActiveFileToggle.setValue(this.choice?.captureToActiveFile);
51+
captureToActiveFileToggle.onChange(value => {
52+
this.choice.captureToActiveFile = value;
53+
54+
this.display();
3755
});
56+
57+
if (!this.choice?.captureToActiveFile) {
58+
const captureToFileContainer: HTMLDivElement = captureToContainer.createDiv('captureToFileContainer');
59+
60+
const formatDisplay: HTMLSpanElement = captureToFileContainer.createEl('span');
61+
const displayFormatter: FileNameDisplayFormatter = new FileNameDisplayFormatter(this.app);
62+
(async () => formatDisplay.textContent = await displayFormatter.format(this.choice.captureTo))();
63+
64+
const formatInput = new TextComponent(captureToFileContainer);
65+
formatInput.setPlaceholder("File name format");
66+
textField = formatInput;
67+
formatInput.inputEl.style.width = "100%";
68+
formatInput.inputEl.style.marginBottom = "8px";
69+
formatInput.setValue(this.choice.captureTo)
70+
.setDisabled(this.choice?.captureToActiveFile)
71+
.onChange(async value => {
72+
this.choice.captureTo = value;
73+
formatDisplay.textContent = await displayFormatter.format(value);
74+
});
75+
76+
const markdownFilesAndFormatSyntax = [...this.app.vault.getMarkdownFiles().map(f => f.path), ...FILE_NAME_FORMAT_SYNTAX];
77+
new GenericTextSuggester(this.app, textField.inputEl, markdownFilesAndFormatSyntax);
78+
}
3879
}
3980

4081
private addPrependSetting() {

src/gui/ChoiceBuilder/choiceBuilder.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {App, Modal, Setting} from "obsidian";
1+
import {App, Modal, SearchComponent, Setting} from "obsidian";
22
import type IChoice from "../../types/choices/IChoice";
33
import type {SvelteComponent} from "svelte";
44
import {GenericTextSuggester} from "../genericTextSuggester";
@@ -32,8 +32,11 @@ export abstract class ChoiceBuilder extends Modal {
3232
this.display();
3333
}
3434

35-
protected addFileSearchInputToSetting(setting: Setting, value: string, onChangeCallback: (value: string) => void) {
35+
protected addFileSearchInputToSetting(setting: Setting, value: string, onChangeCallback: (value: string) => void): SearchComponent {
36+
let component: SearchComponent;
37+
3638
setting.addSearch(searchComponent => {
39+
component = searchComponent;
3740
searchComponent.setValue(value);
3841
searchComponent.setPlaceholder("File path");
3942

@@ -42,6 +45,8 @@ export abstract class ChoiceBuilder extends Modal {
4245

4346
searchComponent.onChange(onChangeCallback);
4447
});
48+
49+
return component;
4550
}
4651

4752
protected addCenteredHeader(header: string): void {

src/gui/choiceSuggester.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ export default class ChoiceSuggester extends FuzzySuggestModal<IChoice> {
7171
}
7272

7373
private async onChooseCaptureType(captureChoice: ICaptureChoice) {
74-
if (!captureChoice.captureTo) {
75-
log.logError(`please provide a template path for ${captureChoice.name}`);
74+
if (!captureChoice.captureTo && !captureChoice?.captureToActiveFile) {
75+
log.logError(`please provide a capture path for ${captureChoice.name}`);
7676
return;
7777
}
7878

src/types/choices/CaptureChoice.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type ICaptureChoice from "./ICaptureChoice";
55
export class CaptureChoice extends Choice implements ICaptureChoice {
66
appendLink: boolean;
77
captureTo: string;
8+
captureToActiveFile: boolean;
89
format: { enabled: boolean; format: string };
910
insertAfter: { enabled: boolean; after: string };
1011
prepend: boolean;
@@ -15,6 +16,7 @@ export class CaptureChoice extends Choice implements ICaptureChoice {
1516

1617
this.appendLink = false;
1718
this.captureTo = "";
19+
this.captureToActiveFile = false;
1820
this.format = {enabled: false, format: ""};
1921
this.insertAfter = {enabled: false, after: ""};
2022
this.prepend = false;

src/types/choices/ICaptureChoice.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type IChoice from "./IChoice";
22

33
export default interface ICaptureChoice extends IChoice {
44
captureTo: string;
5+
captureToActiveFile: boolean;
56
format: { enabled: boolean, format: string };
67
prepend: boolean;
78
appendLink: boolean;

styles.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,11 @@
4141
align-content: center;
4242
justify-content: space-around;
4343
margin-top: 20px;
44+
}
45+
46+
.captureToActiveFileContainer {
47+
display: flex;
48+
align-content: center;
49+
justify-content: space-between;
50+
margin-bottom: 10px;
4451
}

versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"0.1.5": "0.12.4"
2+
"0.1.6": "0.12.4"
33
}

0 commit comments

Comments
 (0)