Skip to content

Commit

Permalink
{{Value}} now gets selected text if any text is selected. (#5)
Browse files Browse the repository at this point in the history
* {{Value}} now gets selected text if any text is selected.

For #4.

* Bump versions
  • Loading branch information
chhoumann authored Jun 16, 2021
1 parent 9dd96c7 commit 8a67cdf
Show file tree
Hide file tree
Showing 9 changed files with 33 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.1.7",
"version": "0.1.8",
"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.7",
"version": "0.1.8",
"description": "Quickly add new pages or content to your vault.",
"main": "main.js",
"scripts": {
Expand Down
8 changes: 5 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
export const VALUE_SYNTAX: string = "{{VALUE}}";

export const FORMAT_SYNTAX: string[] = [
"{{DATE}}", "{{DATE:<DATEFORMAT>}}", "{{VDATE:<VARIABLE NAME>, <DATE FORMAT>}}",
"{{VALUE}}", "{{NAME}}", "{{VALUE:<VARIABLE NAME>}}", "{{LINKCURRENT}}", "{{MACRO:<MACRONAME>}}",
VALUE_SYNTAX, "{{NAME}}", "{{VALUE:<VARIABLE NAME>}}", "{{LINKCURRENT}}", "{{MACRO:<MACRONAME>}}",
"{{TEMPLATE:<TEMPLATEPATH>}}"
];

export const FILE_NAME_FORMAT_SYNTAX: string[] = [
"{{DATE}}", "{{DATE:<DATEFORMAT>}}", "{{VDATE:<VARIABLE NAME>, <DATE FORMAT>}}",
"{{VALUE}}", "{{NAME}}", "{{VALUE:<VARIABLE NAME>}}",
VALUE_SYNTAX, "{{NAME}}", "{{VALUE:<VARIABLE NAME>}}",
]

export const FILE_NUMBER_REGEX: RegExp = new RegExp(/([0-9]*)\.md$/);
Expand All @@ -19,4 +21,4 @@ export const LINK_TO_CURRENT_FILE_REGEX: RegExp = new RegExp(/{{LINKCURRENT}}/);
export const MARKDOWN_FILE_EXTENSION_REGEX: RegExp = new RegExp(/\.md$/);
export const JAVASCRIPT_FILE_EXTENSION_REGEX: RegExp = new RegExp(/\.js$/);
export const MACRO_REGEX: RegExp = new RegExp(/{{MACRO:([^\n\r}]*)}}/);
export const TEMPLATE_REGEX: RegExp = new RegExp(/{{TEMPLATE:([^\n\r}]*.md)}}/);
export const TEMPLATE_REGEX: RegExp = new RegExp(/{{TEMPLATE:([^\n\r}]*.md)}}/);
4 changes: 2 additions & 2 deletions src/engine/CaptureChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {log} from "../logger/logManager";
import GenericInputPrompt from "../gui/GenericInputPrompt/genericInputPrompt";
import {CaptureChoiceFormatter} from "../formatters/captureChoiceFormatter";
import {appendToCurrentLine} from "../utility";
import {MARKDOWN_FILE_EXTENSION_REGEX} from "../constants";
import {MARKDOWN_FILE_EXTENSION_REGEX, VALUE_SYNTAX} from "../constants";
import type QuickAdd from "../main";
import {QuickAddChoiceEngine} from "./QuickAddChoiceEngine";

Expand Down Expand Up @@ -66,7 +66,7 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
let content: string;

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

Expand Down
15 changes: 13 additions & 2 deletions src/formatters/completeFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import GenericSuggester from "../gui/GenericSuggester/genericSuggester";
import type QuickAdd from "../main";
import {SingleMacroEngine} from "../engine/SingleMacroEngine";
import {SingleTemplateEngine} from "../engine/SingleTemplateEngine";
import {MarkdownView} from "obsidian";

export class CompleteFormatter extends Formatter {
private valueHeader: string;
Expand Down Expand Up @@ -58,8 +59,11 @@ export class CompleteFormatter extends Formatter {
}

protected async promptForValue(header?: string): Promise<string> {
if (!this.value)
this.value = await GenericInputPrompt.Prompt(this.app, this.valueHeader ?? `Enter value`)
if (!this.value) {
const selectedText: string = await this.getSelectedText();
this.value = selectedText ? selectedText :
await GenericInputPrompt.Prompt(this.app, this.valueHeader ?? `Enter value`)
}

return this.value;
}
Expand All @@ -80,4 +84,11 @@ export class CompleteFormatter extends Formatter {
protected async getTemplateContent(templatePath: string): Promise<string> {
return await new SingleTemplateEngine(this.app, this.plugin, templatePath).run();
}

protected async getSelectedText(): Promise<string> {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeView) return;

return activeView.editor.getSelection();
}
}
4 changes: 4 additions & 0 deletions src/formatters/fileNameDisplayFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ export class FileNameDisplayFormatter extends Formatter {
protected async getTemplateContent(templatePath: string): Promise<string> {
return `/${templatePath}/`;
}

protected async getSelectedText(): Promise<string> {
return "_selected_";
}
}
4 changes: 4 additions & 0 deletions src/formatters/formatDisplayFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ export class FormatDisplayFormatter extends Formatter {
return `Template (not found): ${templatePath}`;
}
}

protected async getSelectedText(): Promise<string> {
return "_selected_";
}
}
2 changes: 2 additions & 0 deletions src/formatters/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,6 @@ export abstract class Formatter {
protected abstract promptForVariable(variableName: string): Promise<string>;

protected abstract getTemplateContent(templatePath: string): Promise<string>;

protected abstract getSelectedText(): Promise<string>;
}
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"0.1.7": "0.12.4"
"0.1.8": "0.12.4"
}

0 comments on commit 8a67cdf

Please sign in to comment.