From e752853a5870e51dab3185d03388066f8d94fe17 Mon Sep 17 00:00:00 2001 From: Bauke Date: Fri, 24 Nov 2023 19:53:19 +0100 Subject: [PATCH] Put the cursor at the end of the snippet when no marker is used. --- source/content-scripts/features/markdown-toolbar.tsx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/source/content-scripts/features/markdown-toolbar.tsx b/source/content-scripts/features/markdown-toolbar.tsx index a41c44c..e2d82d3 100644 --- a/source/content-scripts/features/markdown-toolbar.tsx +++ b/source/content-scripts/features/markdown-toolbar.tsx @@ -157,7 +157,12 @@ function insertSnippet(props: Required) { cursorIndex -= MarkdownSnippetMarker.SelectedCursor.length; } + if (cursorIndex === -1) { + cursorIndex = 0; + } + let cursorPosition = cursorIndex; + const snippetLength = markdown.length; // If any text has been selected, include it. if (selectionStart !== selectionEnd) { @@ -175,5 +180,11 @@ function insertSnippet(props: Required) { markdown + textarea.value.slice(selectionEnd); + if (cursorPosition === 0) { + // If no marker was used in the snippet, then put the cursor at the + // end of the snippet. + cursorPosition = snippetLength; + } + textarea.selectionEnd = selectionEnd + cursorPosition; }