);
}
function SnippetDropdown(props: Props) {
- const options = snippets.map((snippet) => (
-
- ));
+ const snippets = props.allSnippets;
+ const options = snippets
+ ?.filter((snippet) => snippet.value.inDropdown)
+ .map((snippet) => (
+
+ ));
+
+ if (options.length === 0) {
+ return null;
+ }
const change = (event: Event) => {
event.preventDefault();
const snippet = snippets.find(
- (value) => value.name === (event.target as HTMLSelectElement).value,
+ (value) => value.value.name === (event.target as HTMLSelectElement).value,
)!;
insertSnippet({
@@ -175,20 +131,41 @@ function insertSnippet(props: Required) {
// snippet, the textarea won't be focused anymore. So focus it again.
textarea.focus();
- let {index, markdown} = snippet;
+ let {markdown} = snippet.value;
+
+ // Get the marker positions and remove them from the snippet.
+ let cursorIndex = markdown.indexOf(MarkdownSnippetMarker.Cursor);
+ markdown = markdown.replace(MarkdownSnippetMarker.Cursor, "");
+ const selectedCursorIndex = markdown.indexOf(
+ MarkdownSnippetMarker.SelectedCursor,
+ );
+ markdown = markdown.replace(MarkdownSnippetMarker.SelectedCursor, "");
+
+ // If we have a Cursor and SelectedCursor in the snippet, and the Cursor is
+ // placed after the SelectedCursor we have to account for the marker string
+ // length.
+ // We don't have to do it in reverse because the Cursor index is taken first
+ // and the marker string for that is removed before the SelectedCursor index
+ // is taken.
+ if (
+ cursorIndex !== -1 &&
+ selectedCursorIndex !== -1 &&
+ cursorIndex > selectedCursorIndex
+ ) {
+ cursorIndex -= MarkdownSnippetMarker.SelectedCursor.length;
+ }
+
+ let cursorPosition = cursorIndex;
// If any text has been selected, include it.
if (selectionStart !== selectionEnd) {
markdown =
- markdown.slice(0, index) +
+ markdown.slice(0, cursorIndex) +
textarea.value.slice(selectionStart, selectionEnd) +
- markdown.slice(index);
+ markdown.slice(cursorIndex);
- // Change the index when the Link snippet is used so the cursor ends up
- // in the URL part of the Markdown: "[existing text](cursor here)".
- if (snippet.name === "Link") {
- index += 2;
- }
+ cursorPosition =
+ selectedCursorIndex === -1 ? cursorIndex : selectedCursorIndex;
}
textarea.value =
@@ -196,5 +173,5 @@ function insertSnippet(props: Required) {
markdown +
textarea.value.slice(selectionEnd);
- textarea.selectionEnd = selectionEnd + index;
+ textarea.selectionEnd = selectionEnd + cursorPosition;
}
diff --git a/source/content-scripts/setup.tsx b/source/content-scripts/setup.tsx
index 295fba4..94d0909 100644
--- a/source/content-scripts/setup.tsx
+++ b/source/content-scripts/setup.tsx
@@ -9,6 +9,7 @@ import {
Data,
Feature,
MiscellaneousFeature,
+ collectMarkdownSnippets,
fromStorage,
} from "../storage/exports.js";
import {
@@ -106,8 +107,9 @@ async function initialize() {
}
if (enabledFeatures.value.has(Feature.MarkdownToolbar) && isLoggedIn) {
- observerFeatures.push(() => {
- runMarkdownToolbarFeature();
+ observerFeatures.push(async () => {
+ const snippets = await collectMarkdownSnippets();
+ runMarkdownToolbarFeature(snippets);
});
}