mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-12 00:45:47 +01:00
fixed auto complete for insertion in the middle, 6am commits FTW
This commit is contained in:
@@ -149,9 +149,13 @@ define(['taskbar'], function(taskbar) {
|
|||||||
$(bodyEl).autocomplete({
|
$(bodyEl).autocomplete({
|
||||||
source: function(request, response) {
|
source: function(request, response) {
|
||||||
var term = request.term;
|
var term = request.term;
|
||||||
var lastMention = request.term.lastIndexOf('@');
|
|
||||||
|
var cursorPosition = $(bodyEl).getCursorPosition();
|
||||||
|
term = term.substr(0, cursorPosition);
|
||||||
|
|
||||||
|
var lastMention = term.lastIndexOf('@');
|
||||||
if(lastMention !== -1) {
|
if(lastMention !== -1) {
|
||||||
term = request.term.substr(lastMention);
|
term = term.substr(lastMention);
|
||||||
}
|
}
|
||||||
|
|
||||||
var userslugs = getUniqueUserslugs();
|
var userslugs = getUniqueUserslugs();
|
||||||
@@ -162,18 +166,22 @@ define(['taskbar'], function(taskbar) {
|
|||||||
response(userslugs);
|
response(userslugs);
|
||||||
$('.ui-autocomplete a').attr('href', '#');
|
$('.ui-autocomplete a').attr('href', '#');
|
||||||
},
|
},
|
||||||
response: function(event, ui) {
|
focus: function(event, ui) {
|
||||||
var content = $(bodyEl).val();
|
return false;
|
||||||
var lastIndex = content.lastIndexOf('@');
|
},
|
||||||
if(content === '@') {
|
select: function(event, ui) {
|
||||||
content = '';
|
var cursorPosition = $(bodyEl).getCursorPosition();
|
||||||
} else if(lastIndex !== -1) {
|
var upToCursor = $(bodyEl).val().substr(0, cursorPosition);
|
||||||
content = content.substr(0, lastIndex);
|
var index = upToCursor.lastIndexOf('@');
|
||||||
}
|
|
||||||
|
|
||||||
for(var i=0; i<ui.content.length; ++i) {
|
if(index !== -1) {
|
||||||
ui.content[i].value = content + ui.content[i].value;
|
var firstPart = $(bodyEl).val().substr(0, index);
|
||||||
|
var lastPart = $(bodyEl).val().substr(cursorPosition);
|
||||||
|
|
||||||
|
$(bodyEl).val(firstPart + ui.item.value + lastPart);
|
||||||
|
$(bodyEl).selectRange(index + ui.item.value.length);
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
},
|
},
|
||||||
position: { my : "left bottom", at: "left bottom" }
|
position: { my : "left bottom", at: "left bottom" }
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -187,6 +187,40 @@
|
|||||||
|
|
||||||
if ('undefined' !== typeof window) {
|
if ('undefined' !== typeof window) {
|
||||||
window.utils = module.exports;
|
window.utils = module.exports;
|
||||||
|
|
||||||
|
(function ($, undefined) {
|
||||||
|
$.fn.getCursorPosition = function() {
|
||||||
|
var el = $(this).get(0);
|
||||||
|
var pos = 0;
|
||||||
|
if('selectionStart' in el) {
|
||||||
|
pos = el.selectionStart;
|
||||||
|
} else if('selection' in document) {
|
||||||
|
el.focus();
|
||||||
|
var Sel = document.selection.createRange();
|
||||||
|
var SelLength = document.selection.createRange().text.length;
|
||||||
|
Sel.moveStart('character', -el.value.length);
|
||||||
|
pos = Sel.text.length - SelLength;
|
||||||
|
}
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
$.fn.selectRange = function(start, end) {
|
||||||
|
if(!end) end = start;
|
||||||
|
return this.each(function() {
|
||||||
|
if (this.setSelectionRange) {
|
||||||
|
this.focus();
|
||||||
|
this.setSelectionRange(start, end);
|
||||||
|
} else if (this.createTextRange) {
|
||||||
|
var range = this.createTextRange();
|
||||||
|
range.collapse(true);
|
||||||
|
range.moveEnd('character', end);
|
||||||
|
range.moveStart('character', start);
|
||||||
|
range.select();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
})(jQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
})('undefined' === typeof module ? {
|
})('undefined' === typeof module ? {
|
||||||
|
|||||||
Reference in New Issue
Block a user