Skip to content

Commit

Permalink
Add 'Insert at the end of section' feature to captures.
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Jun 28, 2021
1 parent ecac016 commit f092448
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 10 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.13",
"version": "0.2.14",
"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.13",
"version": "0.2.14",
"description": "Quickly add new pages or content to your vault.",
"main": "main.js",
"scripts": {
Expand Down
30 changes: 28 additions & 2 deletions src/formatters/captureChoiceFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {App, TFile} from "obsidian";
import {log} from "../logger/logManager";
import type QuickAdd from "../main";
import type {IChoiceExecutor} from "../IChoiceExecutor";
import {templaterParseTemplate} from "../utility";
import {getLinesInString, templaterParseTemplate} from "../utility";

export class CaptureChoiceFormatter extends CompleteFormatter {
private choice: ICaptureChoice;
Expand Down Expand Up @@ -47,11 +47,37 @@ export class CaptureChoiceFormatter extends CompleteFormatter {

if (this.choice.insertAfter.enabled) {
const targetRegex = new RegExp(`\s*${this.choice.insertAfter.after}\s*`)
const targetPosition = this.fileContent.split("\n").findIndex(line => targetRegex.test(line));
let fileContentLines: string[] = getLinesInString(this.fileContent);

const targetPosition = fileContentLines.findIndex(line => targetRegex.test(line));
if (targetPosition === -1) {
log.logError("unable to find insert after line in file.")
}

if (this.choice.insertAfter?.insertAtEnd) {
const nextHeaderPositionAfterTargetPosition = fileContentLines.slice(targetPosition + 1).findIndex(line => (/^#+ |---/).test(line))
const foundNextHeader = nextHeaderPositionAfterTargetPosition !== -1;

if (foundNextHeader) {
let endOfSectionIndex: number;

for (let i = nextHeaderPositionAfterTargetPosition + targetPosition; i > targetPosition; i--) {
const lineIsNewline: boolean = (/^[\s\n ]*$/).test(fileContentLines[i]);

if (!lineIsNewline) {
endOfSectionIndex = i;
break;
}
}

if (!endOfSectionIndex) endOfSectionIndex = targetPosition;

return this.insertTextAfterPositionInBody(formatted, this.fileContent, endOfSectionIndex);
} else {
return this.insertTextAfterPositionInBody(formatted, this.fileContent, fileContentLines.length - 1);
}
}

return this.insertTextAfterPositionInBody(formatted, this.fileContent, targetPosition);
}

Expand Down
15 changes: 14 additions & 1 deletion src/gui/ChoiceBuilder/captureChoiceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,27 @@ export class CaptureChoiceBuilder extends ChoiceBuilder {
.setDesc("Insert capture after specified line.")
.addToggle(toggle => {
toggle.setValue(this.choice.insertAfter.enabled);
toggle.onChange(value => this.choice.insertAfter.enabled = value);
toggle.onChange(value => {
this.choice.insertAfter.enabled = value;
this.reload();
});
})
.addText(textEl => {
textEl.setPlaceholder("Line text");
textEl.inputEl.style.marginLeft = "10px";
textEl.setValue(this.choice.insertAfter.after);
textEl.onChange(value => this.choice.insertAfter.after = value);
});

if (this.choice.insertAfter.enabled) {
const insertAtEndSetting: Setting = new Setting(this.contentEl);
insertAtEndSetting.setName("Insert at end of section")
.setDesc("Insert the text at the end of the section, rather than at the top.")
.addToggle(toggle => toggle
.setValue(this.choice.insertAfter?.insertAtEnd)
.onChange(value => this.choice.insertAfter.insertAtEnd = value)
);
}
}

private addFormatSetting() {
Expand Down
6 changes: 3 additions & 3 deletions src/types/choices/CaptureChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export class CaptureChoice extends Choice implements ICaptureChoice {
appendLink: boolean;
captureTo: string;
captureToActiveFile: boolean;
createFileIfItDoesntExist: {enabled: boolean, createWithTemplate: boolean, template: string};
createFileIfItDoesntExist: { enabled: boolean, createWithTemplate: boolean, template: string };
format: { enabled: boolean; format: string };
insertAfter: { enabled: boolean; after: string };
insertAfter: { enabled: boolean; after: string, insertAtEnd: boolean };
prepend: boolean;
task: boolean;

Expand All @@ -20,7 +20,7 @@ export class CaptureChoice extends Choice implements ICaptureChoice {
this.captureToActiveFile = false;
this.createFileIfItDoesntExist = {enabled: false, createWithTemplate: false, template: ""};
this.format = {enabled: false, format: ""};
this.insertAfter = {enabled: false, after: ""};
this.insertAfter = {enabled: false, after: "", insertAtEnd: false};
this.prepend = false;
this.task = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/choices/ICaptureChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export default interface ICaptureChoice extends IChoice {
prepend: boolean;
appendLink: boolean;
task: boolean;
insertAfter: { enabled: boolean, after: string };
insertAfter: { enabled: boolean, after: string, insertAtEnd: boolean };
}

15 changes: 15 additions & 0 deletions src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,19 @@ export function getUserScriptMemberAccess(fullMemberPath: string): {basename: st

export function waitFor(ms: number): Promise<unknown> {
return new Promise(res => setTimeout(res, ms));
}

export function getLinesInString(input: string) {
let lines: string[] = [];
let tempString = input;

while (tempString.contains("\n")) {
const lineEndIndex = tempString.indexOf("\n");
lines.push(tempString.slice(0, lineEndIndex));
tempString = tempString.slice(lineEndIndex + 1);
}

lines.push(tempString);

return lines;
}
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"0.2.13": "0.12.4"
"0.2.14": "0.12.4"
}

0 comments on commit f092448

Please sign in to comment.