Skip to content

Commit

Permalink
Capture to current page. Fix tasks not automatically creating linebre…
Browse files Browse the repository at this point in the history
…aks.
  • Loading branch information
chhoumann committed Jun 14, 2021
1 parent 23b1c02 commit f7dd5de
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 23 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.1.5",
"version": "0.1.6",
"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.1.5",
"version": "0.1.6",
"description": "Quickly add new pages or content to your vault.",
"main": "main.js",
"scripts": {
Expand Down
38 changes: 29 additions & 9 deletions src/engine/CaptureChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,19 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {

async run(): Promise<void> {
try {
if (this.choice?.captureToActiveFile) {
await this.captureToActiveFile();
return;
}

const captureTo = this.choice.captureTo;
if (!captureTo) {
log.logError(`Invalid capture to for ${this.choice.name}`);
return;
}

const filePath = await this.getFilePath(captureTo);
let content: string;

if (!this.choice.format.enabled)
content = await GenericInputPrompt.Prompt(this.app, this.choice.name);
else
content = this.choice.format.format;

if (this.choice.task)
content = `- [ ] ${content}`;
let content = await this.getCaptureContent();

if (await this.fileExists(filePath)) {
const file: TFile = await this.getFileByPath(filePath);
Expand Down Expand Up @@ -63,9 +60,32 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
}
}

private async getCaptureContent(): Promise<string> {
let content: string;

if (!this.choice.format.enabled)
content = await GenericInputPrompt.Prompt(this.app, this.choice.name);
else
content = this.choice.format.format;

if (this.choice.task)
content = `- [ ] ${content}\n`;

return content;
}

private async getFilePath(captureTo: string) {
const formattedCaptureTo: string = await this.formatter.formatFileName(captureTo, this.choice.name);
return this.formatFilePath("", formattedCaptureTo);
}

private async captureToActiveFile() {
let content: string = await this.getCaptureContent();

if (this.choice.format.enabled) {
content = await this.formatter.formatContent(content, this.choice);
}

appendToCurrentLine(content, this.app);
}
}
55 changes: 48 additions & 7 deletions src/gui/ChoiceBuilder/captureChoiceBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {ChoiceBuilder} from "./choiceBuilder";
import type ICaptureChoice from "../../types/choices/ICaptureChoice";
import type {App} from "obsidian";
import {Setting, TextAreaComponent} from "obsidian";
import {SearchComponent, Setting, TextAreaComponent, TextComponent, ToggleComponent} from "obsidian";
import {FormatSyntaxSuggester} from "../formatSyntaxSuggester";
import {FORMAT_SYNTAX} from "../../constants";
import {FILE_NAME_FORMAT_SYNTAX, FORMAT_SYNTAX} from "../../constants";
import {FormatDisplayFormatter} from "../../formatters/formatDisplayFormatter";
import type QuickAdd from "../../main";
import {FileNameDisplayFormatter} from "../../formatters/fileNameDisplayFormatter";
import {GenericTextSuggester} from "../genericTextSuggester";

export class CaptureChoiceBuilder extends ChoiceBuilder {
choice: ICaptureChoice;
Expand All @@ -18,23 +20,62 @@ export class CaptureChoiceBuilder extends ChoiceBuilder {
}

protected display() {
this.contentEl.empty();

this.addCenteredHeader(this.choice.name);
this.addCapturedToSetting();
this.addPrependSetting();
this.addTaskSetting();
this.addAppendLinkSetting();
this.addInsertAfterSetting();

if (!this.choice.captureToActiveFile) {
this.addPrependSetting();
this.addAppendLinkSetting();
this.addInsertAfterSetting();
}

this.addFormatSetting();
}

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

this.addFileSearchInputToSetting(captureToSetting, this.choice.captureTo, value => {
this.choice.captureTo = value;
const captureToContainer: HTMLDivElement = this.contentEl.createDiv('captureToContainer');

const captureToActiveFileContainer: HTMLDivElement = captureToContainer.createDiv('captureToActiveFileContainer');
const captureToActiveFileText: HTMLSpanElement = captureToActiveFileContainer.createEl('span');
captureToActiveFileText.textContent = "Capture to active file";
const captureToActiveFileToggle: ToggleComponent = new ToggleComponent(captureToActiveFileContainer);
captureToActiveFileToggle.setValue(this.choice?.captureToActiveFile);
captureToActiveFileToggle.onChange(value => {
this.choice.captureToActiveFile = value;

this.display();
});

if (!this.choice?.captureToActiveFile) {
const captureToFileContainer: HTMLDivElement = captureToContainer.createDiv('captureToFileContainer');

const formatDisplay: HTMLSpanElement = captureToFileContainer.createEl('span');
const displayFormatter: FileNameDisplayFormatter = new FileNameDisplayFormatter(this.app);
(async () => formatDisplay.textContent = await displayFormatter.format(this.choice.captureTo))();

const formatInput = new TextComponent(captureToFileContainer);
formatInput.setPlaceholder("File name format");
textField = formatInput;
formatInput.inputEl.style.width = "100%";
formatInput.inputEl.style.marginBottom = "8px";
formatInput.setValue(this.choice.captureTo)
.setDisabled(this.choice?.captureToActiveFile)
.onChange(async value => {
this.choice.captureTo = value;
formatDisplay.textContent = await displayFormatter.format(value);
});

const markdownFilesAndFormatSyntax = [...this.app.vault.getMarkdownFiles().map(f => f.path), ...FILE_NAME_FORMAT_SYNTAX];
new GenericTextSuggester(this.app, textField.inputEl, markdownFilesAndFormatSyntax);
}
}

private addPrependSetting() {
Expand Down
9 changes: 7 additions & 2 deletions src/gui/ChoiceBuilder/choiceBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {App, Modal, Setting} from "obsidian";
import {App, Modal, SearchComponent, Setting} from "obsidian";
import type IChoice from "../../types/choices/IChoice";
import type {SvelteComponent} from "svelte";
import {GenericTextSuggester} from "../genericTextSuggester";
Expand Down Expand Up @@ -32,8 +32,11 @@ export abstract class ChoiceBuilder extends Modal {
this.display();
}

protected addFileSearchInputToSetting(setting: Setting, value: string, onChangeCallback: (value: string) => void) {
protected addFileSearchInputToSetting(setting: Setting, value: string, onChangeCallback: (value: string) => void): SearchComponent {
let component: SearchComponent;

setting.addSearch(searchComponent => {
component = searchComponent;
searchComponent.setValue(value);
searchComponent.setPlaceholder("File path");

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

searchComponent.onChange(onChangeCallback);
});

return component;
}

protected addCenteredHeader(header: string): void {
Expand Down
4 changes: 2 additions & 2 deletions src/gui/choiceSuggester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export default class ChoiceSuggester extends FuzzySuggestModal<IChoice> {
}

private async onChooseCaptureType(captureChoice: ICaptureChoice) {
if (!captureChoice.captureTo) {
log.logError(`please provide a template path for ${captureChoice.name}`);
if (!captureChoice.captureTo && !captureChoice?.captureToActiveFile) {
log.logError(`please provide a capture path for ${captureChoice.name}`);
return;
}

Expand Down
2 changes: 2 additions & 0 deletions src/types/choices/CaptureChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type ICaptureChoice from "./ICaptureChoice";
export class CaptureChoice extends Choice implements ICaptureChoice {
appendLink: boolean;
captureTo: string;
captureToActiveFile: boolean;
format: { enabled: boolean; format: string };
insertAfter: { enabled: boolean; after: string };
prepend: boolean;
Expand All @@ -15,6 +16,7 @@ export class CaptureChoice extends Choice implements ICaptureChoice {

this.appendLink = false;
this.captureTo = "";
this.captureToActiveFile = false;
this.format = {enabled: false, format: ""};
this.insertAfter = {enabled: false, after: ""};
this.prepend = false;
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 @@ -2,6 +2,7 @@ import type IChoice from "./IChoice";

export default interface ICaptureChoice extends IChoice {
captureTo: string;
captureToActiveFile: boolean;
format: { enabled: boolean, format: string };
prepend: boolean;
appendLink: boolean;
Expand Down
7 changes: 7 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@
align-content: center;
justify-content: space-around;
margin-top: 20px;
}

.captureToActiveFileContainer {
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.1.5": "0.12.4"
"0.1.6": "0.12.4"
}

0 comments on commit f7dd5de

Please sign in to comment.