|
184 | 184 | //Adds mention to mentions collections |
185 | 185 | function addMention(mention) { |
186 | 186 |
|
187 | | - var currentMessage = getInputBoxValue(); //Get the actual value of the text area |
| 187 | + var currentMessage = getInputBoxValue(), |
| 188 | + caretStart = elmInputBox[0].selectionStart, |
| 189 | + shortestDistance = false, |
| 190 | + bestLastIndex = false; |
188 | 191 |
|
189 | 192 | // Using a regex to figure out positions |
190 | | - var regex = new RegExp("\\" + settings.triggerChar + currentDataQuery, "gi"); |
191 | | - regex.exec(currentMessage); //Executes a search for a match in a specified string. Returns a result array, or null |
| 193 | + var regex = new RegExp("\\" + settings.triggerChar + currentDataQuery, "gi"), |
| 194 | + regexMatch; |
| 195 | + |
| 196 | + while(regexMatch = regex.exec(currentMessage)) { |
| 197 | + if (shortestDistance === false || Math.abs(regex.lastIndex - caretStart) < shortestDistance) { |
| 198 | + shortestDistance = Math.abs(regex.lastIndex - caretStart); |
| 199 | + bestLastIndex = regex.lastIndex; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + var startCaretPosition = bestLastIndex - currentDataQuery.length - 1; //Set the start caret position (right before the @) |
| 204 | + var currentCaretPosition = bestLastIndex; //Set the current caret position (right after the end of the "mention") |
192 | 205 |
|
193 | | - var startCaretPosition = regex.lastIndex - currentDataQuery.length - 1; //Set the star caret position |
194 | | - var currentCaretPosition = regex.lastIndex; //Set the current caret position |
195 | 206 |
|
196 | 207 | var start = currentMessage.substr(0, startCaretPosition); |
197 | 208 | var end = currentMessage.substr(currentCaretPosition, currentMessage.length); |
|
0 commit comments