Skip to content

Commit e55da92

Browse files
committed
Merge pull request #136 from juanvillegas/master
imilar" tags clash when @ is already in textarea
2 parents a548008 + 3f57757 commit e55da92

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

jquery.mentionsInput.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,25 @@
184184
//Adds mention to mentions collections
185185
function addMention(mention) {
186186

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;
188191

189192
// 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")
192205

193-
var startCaretPosition = regex.lastIndex - currentDataQuery.length - 1; //Set the star caret position
194-
var currentCaretPosition = regex.lastIndex; //Set the current caret position
195206

196207
var start = currentMessage.substr(0, startCaretPosition);
197208
var end = currentMessage.substr(currentCaretPosition, currentMessage.length);

0 commit comments

Comments
 (0)