client: chat scrollback with ctrl+up / ctrl+down

This commit is contained in:
mappu 2017-02-05 17:48:25 +13:00
parent a927add8c5
commit afd190b7cc

View File

@ -4,6 +4,7 @@
"use strict"; "use strict";
var SENTINEL_PASSWORD = "************"; var SENTINEL_PASSWORD = "************";
var CHAT_SCROLLBACK_LIMIT = 50; // Once over 2x $limit, the first $limit will be trimmed off the list
var $ = (document.querySelectorAll ? var $ = (document.querySelectorAll ?
function(s) { function(s) {
@ -69,6 +70,15 @@ var textContent = function($el) {
return ""; return "";
}; };
var negmod = function(l, r) {
var ret = l % r;
if (l < 0) {
return ret + r;
} else {
return ret;
}
};
// @ref https://developer.mozilla.org/en/docs/Web/API/WindowBase64/Base64_encoding_and_decoding // @ref https://developer.mozilla.org/en/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
var b64 = function(str) { var b64 = function(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
@ -323,6 +333,12 @@ var submit = function() {
sock.emit('pub', {'message' : str}); sock.emit('pub', {'message' : str});
} }
chat_scrollback.push( str );
chat_scrollback_index = -1;
if (chat_scrollback.length > (2*CHAT_SCROLLBACK_LIMIT)) {
chat_scrollback = chat_scrollback.slice(CHAT_SCROLLBACK_LIMIT);
}
} else { } else {
write("tab-main").system("Invalid internal state."); write("tab-main").system("Invalid internal state.");
} }
@ -673,6 +689,23 @@ var STATE_READY_FOR_LOGIN = 0;
var STATE_CONNECTING = 1; var STATE_CONNECTING = 1;
var STATE_ACTIVE = 2; var STATE_ACTIVE = 2;
var chat_scrollback = [];
var chat_scrollback_index = -1;
var scrollback_move = function(delta) {
if (chat_scrollback.length === 0) {
return; // no effect
}
if (chat_scrollback_index === -1) {
chat_scrollback_index = (delta === 1) ? 0 : chat_scrollback.length - 1;
} else {
chat_scrollback_index = negmod(chat_scrollback_index + delta, chat_scrollback.length);
}
$("#chatbox").value = chat_scrollback[chat_scrollback_index];
};
/* */ /* */
var persistence_set = function(key, value) { var persistence_set = function(key, value) {
@ -708,7 +741,6 @@ var transition = function(new_state) {
case STATE_CONNECTING: { case STATE_CONNECTING: {
$("#chatbox").disabled = true; $("#chatbox").disabled = true;
} break; } break;
case STATE_ACTIVE: { case STATE_ACTIVE: {
@ -736,14 +768,24 @@ window.onload = function() {
}; };
$("#chatbox").onkeydown = function(ev) { $("#chatbox").onkeydown = function(ev) {
if (ev.keyCode === 9) { if (ev.keyCode === 9 /* Tab */) {
// tab
tabcompletion_start(); tabcompletion_start();
ev.preventDefault(); ev.preventDefault();
return false; return false;
} else if (ev.keyCode == 38 /* ArrowUp */ && ev.ctrlKey) {
scrollback_move(-1);
ev.preventDefault();
return false;
} else if (ev.keyCode == 40 /* ArrowDown */ && ev.ctrlKey) {
scrollback_move(1);
ev.preventDefault();
return false;
} else { } else {
tabcompletion_inactive(); tabcompletion_inactive();
chat_scrollback_index = -1; // clear
} }
}; };