From 8a67cdf01a21b63d629172f259d57cda6ded4a1d Mon Sep 17 00:00:00 2001 From: Christian Bager Bach Houmann Date: Wed, 16 Jun 2021 15:45:40 +0100 Subject: [PATCH] {{Value}} now gets selected text if any text is selected. (#5) * {{Value}} now gets selected text if any text is selected. For #4. * Bump versions --- manifest.json | 2 +- package.json | 2 +- src/constants.ts | 8 +++++--- src/engine/CaptureChoiceEngine.ts | 4 ++-- src/formatters/completeFormatter.ts | 15 +++++++++++++-- src/formatters/fileNameDisplayFormatter.ts | 4 ++++ src/formatters/formatDisplayFormatter.ts | 4 ++++ src/formatters/formatter.ts | 2 ++ versions.json | 2 +- 9 files changed, 33 insertions(+), 10 deletions(-) diff --git a/manifest.json b/manifest.json index 6ca0f61..0abd47b 100644 --- a/manifest.json +++ b/manifest.json @@ -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", diff --git a/package.json b/package.json index d1509cc..39cf800 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/constants.ts b/src/constants.ts index 7331af0..4de1d7f 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,12 +1,14 @@ +export const VALUE_SYNTAX: string = "{{VALUE}}"; + export const FORMAT_SYNTAX: string[] = [ "{{DATE}}", "{{DATE:}}", "{{VDATE:, }}", - "{{VALUE}}", "{{NAME}}", "{{VALUE:}}", "{{LINKCURRENT}}", "{{MACRO:}}", + VALUE_SYNTAX, "{{NAME}}", "{{VALUE:}}", "{{LINKCURRENT}}", "{{MACRO:}}", "{{TEMPLATE:}}" ]; export const FILE_NAME_FORMAT_SYNTAX: string[] = [ "{{DATE}}", "{{DATE:}}", "{{VDATE:, }}", - "{{VALUE}}", "{{NAME}}", "{{VALUE:}}", + VALUE_SYNTAX, "{{NAME}}", "{{VALUE:}}", ] export const FILE_NUMBER_REGEX: RegExp = new RegExp(/([0-9]*)\.md$/); @@ -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)}}/); \ No newline at end of file diff --git a/src/engine/CaptureChoiceEngine.ts b/src/engine/CaptureChoiceEngine.ts index a3b733d..060d8a5 100644 --- a/src/engine/CaptureChoiceEngine.ts +++ b/src/engine/CaptureChoiceEngine.ts @@ -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"; @@ -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; diff --git a/src/formatters/completeFormatter.ts b/src/formatters/completeFormatter.ts index 862f220..e7eb732 100644 --- a/src/formatters/completeFormatter.ts +++ b/src/formatters/completeFormatter.ts @@ -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; @@ -58,8 +59,11 @@ export class CompleteFormatter extends Formatter { } protected async promptForValue(header?: string): Promise { - 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; } @@ -80,4 +84,11 @@ export class CompleteFormatter extends Formatter { protected async getTemplateContent(templatePath: string): Promise { return await new SingleTemplateEngine(this.app, this.plugin, templatePath).run(); } + + protected async getSelectedText(): Promise { + const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); + if (!activeView) return; + + return activeView.editor.getSelection(); + } } \ No newline at end of file diff --git a/src/formatters/fileNameDisplayFormatter.ts b/src/formatters/fileNameDisplayFormatter.ts index 0f1a983..644b5bb 100644 --- a/src/formatters/fileNameDisplayFormatter.ts +++ b/src/formatters/fileNameDisplayFormatter.ts @@ -48,4 +48,8 @@ export class FileNameDisplayFormatter extends Formatter { protected async getTemplateContent(templatePath: string): Promise { return `/${templatePath}/`; } + + protected async getSelectedText(): Promise { + return "_selected_"; + } } \ No newline at end of file diff --git a/src/formatters/formatDisplayFormatter.ts b/src/formatters/formatDisplayFormatter.ts index 13e23c3..71c7613 100644 --- a/src/formatters/formatDisplayFormatter.ts +++ b/src/formatters/formatDisplayFormatter.ts @@ -58,4 +58,8 @@ export class FormatDisplayFormatter extends Formatter { return `Template (not found): ${templatePath}`; } } + + protected async getSelectedText(): Promise { + return "_selected_"; + } } diff --git a/src/formatters/formatter.ts b/src/formatters/formatter.ts index 0d2dc5b..69f5073 100644 --- a/src/formatters/formatter.ts +++ b/src/formatters/formatter.ts @@ -159,4 +159,6 @@ export abstract class Formatter { protected abstract promptForVariable(variableName: string): Promise; protected abstract getTemplateContent(templatePath: string): Promise; + + protected abstract getSelectedText(): Promise; } \ No newline at end of file diff --git a/versions.json b/versions.json index be94d26..5fe4a13 100644 --- a/versions.json +++ b/versions.json @@ -1,3 +1,3 @@ { - "0.1.7": "0.12.4" + "0.1.8": "0.12.4" }