enable chat tab completion
This commit is contained in:
65
code/chat.js
65
code/chat.js
@ -1,5 +1,44 @@
|
||||
window.chat = function() {};
|
||||
|
||||
window.chat._lastNicksForAutocomplete = [[], []];
|
||||
window.chat.addNickForAutocomplete = function(nick, isFaction) {
|
||||
var r = chat._lastNicksForAutocomplete[isFaction ? 0 : 1];
|
||||
if(r.indexOf(nick) !== -1) return;
|
||||
r.push(nick);
|
||||
if(r.length >= 15)
|
||||
r.shift();
|
||||
}
|
||||
|
||||
window.chat.handleTabCompletion = function() {
|
||||
var el = $('#chatinput input');
|
||||
var curPos = el.get(0).selectionStart;
|
||||
var text = el.val();
|
||||
var word = text.slice(0, curPos).replace(/.*\b([a-z0-9-_])/, '$1').toLowerCase();
|
||||
|
||||
var list = window.chat._lastNicksForAutocomplete;
|
||||
list = list[1].concat(list[0]);
|
||||
|
||||
var nick = null;
|
||||
for(var i = 0; i < list.length; i++) {
|
||||
if(!list[i].toLowerCase().startsWith(word)) continue;
|
||||
if(nick && nick !== list[i]) {
|
||||
console.log('More than one nick matches, aborting. ('+list[i]+' vs '+nick+')');
|
||||
return;
|
||||
}
|
||||
nick = list[i];
|
||||
}
|
||||
if(!nick) {
|
||||
console.log('No matches for ' + word);
|
||||
return;
|
||||
}
|
||||
|
||||
var posStart = curPos - word.length;
|
||||
var newText = text.substring(0, posStart);
|
||||
newText += nick + (posStart === 0 ? ': ' : ' ');
|
||||
newText += text.substring(curPos);
|
||||
el.val(newText);
|
||||
}
|
||||
|
||||
//
|
||||
// timestamp and clear management
|
||||
//
|
||||
@ -363,6 +402,7 @@ window.chat.renderPlayerMsgsTo = function(isFaction, data, isOldMsgs, dupCheckAr
|
||||
nick = markup[1].plain.slice(0, -2); // cut “: ” at end
|
||||
pguid = markup[1].guid;
|
||||
window.setPlayerName(pguid, nick); // free nick name resolves
|
||||
if(!isOldMsgs) window.chat.addNickForAutocomplete(nick, isFaction);
|
||||
}
|
||||
|
||||
if(markup[0] === 'TEXT') {
|
||||
@ -591,15 +631,28 @@ window.chat.setupTime = function() {
|
||||
|
||||
|
||||
window.chat.setupPosting = function() {
|
||||
$('#chatinput input').keypress(function(e) {
|
||||
if((e.keyCode ? e.keyCode : e.which) != 13) return;
|
||||
chat.postMsg();
|
||||
e.preventDefault();
|
||||
$('#chatinput input').keypress(function(event) {
|
||||
try{
|
||||
|
||||
var kc = (event.keyCode ? event.keyCode : event.which);
|
||||
if(kc === 13) { // enter
|
||||
chat.postMsg();
|
||||
event.preventDefault();
|
||||
} else if (kc === 9) { // tab
|
||||
event.preventDefault();
|
||||
window.chat.handleTabCompletion();
|
||||
}
|
||||
|
||||
|
||||
} catch(error) {
|
||||
console.log(error);
|
||||
debug.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
$('#chatinput').submit(function(e) {
|
||||
$('#chatinput').submit(function(event) {
|
||||
chat.postMsg();
|
||||
e.preventDefault();
|
||||
event.preventDefault();
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user