(type, e)
| 93 | } |
| 94 | |
| 95 | const insertMarkdown = (type, e) => { |
| 96 | // Stop event propagation to prevent drag-and-drop |
| 97 | e.preventDefault(); |
| 98 | e.stopPropagation(); |
| 99 | |
| 100 | if (!textareaRef.current) return; |
| 101 | |
| 102 | const textarea = textareaRef.current; |
| 103 | const start = textarea.selectionStart; |
| 104 | const end = textarea.selectionEnd; |
| 105 | const text = content; |
| 106 | const selectedText = text.substring(start, end); |
| 107 | |
| 108 | let insertion = ""; |
| 109 | let newCursorPosition = start; |
| 110 | |
| 111 | switch (type) { |
| 112 | case "heading": |
| 113 | insertion = `### ${selectedText || "Heading"}`; |
| 114 | newCursorPosition = start + 4; // Position cursor after `### ` |
| 115 | break; |
| 116 | case "bold": |
| 117 | insertion = `**${selectedText || "bold text"}**`; |
| 118 | newCursorPosition = start + 2; // Position inside the `**` |
| 119 | break; |
| 120 | case "italic": |
| 121 | insertion = `*${selectedText || "italic text"}*`; |
| 122 | newCursorPosition = start + 1; // Position inside the `*` |
| 123 | break; |
| 124 | case "quote": |
| 125 | insertion = `> ${selectedText || "quote"}`; |
| 126 | newCursorPosition = start + 2; // Position after `> ` |
| 127 | break; |
| 128 | case "code": |
| 129 | insertion = `\`${selectedText || "code"}\``; |
| 130 | newCursorPosition = start + 1; // Position inside the backticks |
| 131 | break; |
| 132 | case "image": |
| 133 | insertion = ""; |
| 134 | newCursorPosition = start + 2; // Move cursor inside `[alt text]` |
| 135 | break; |
| 136 | case "link": |
| 137 | insertion = `[${selectedText || "link text"}](url)`; |
| 138 | newCursorPosition = start + 1; // Move cursor inside `[ ]` |
| 139 | break; |
| 140 | case "numbered": |
| 141 | insertion = `1. ${selectedText || "First item"}\n2. Second item`; |
| 142 | newCursorPosition = start + 3; // Position after `1. ` |
| 143 | break; |
| 144 | case "unordered": |
| 145 | insertion = `- ${selectedText || "List item"}\n- Another item`; |
| 146 | newCursorPosition = start + 2; // Position after `- ` |
| 147 | break; |
| 148 | case "task": |
| 149 | insertion = `- [ ] ${selectedText || "Task item"}\n- [ ] Another task`; |
| 150 | newCursorPosition = start + 6; // Position inside `[ ]` |
| 151 | break; |
| 152 | default: |
no test coverage detected