Reimplement input history

This commit is contained in:
Pavel Djundik 2018-03-23 18:23:44 +02:00
parent 2cfd0e1fe0
commit 3aea9d34e9
3 changed files with 55 additions and 93 deletions

View File

@ -4,6 +4,7 @@ const $ = require("jquery");
const Mousetrap = require("mousetrap"); const Mousetrap = require("mousetrap");
const wrapCursor = require("undate").wrapCursor; const wrapCursor = require("undate").wrapCursor;
const utils = require("./utils"); const utils = require("./utils");
const form = $("#form");
const input = $("#input"); const input = $("#input");
const sidebar = $("#sidebar"); const sidebar = $("#sidebar");
const windows = $("#windows"); const windows = $("#windows");
@ -103,6 +104,60 @@ Mousetrap.bind([
}); });
const inputTrap = Mousetrap(input.get(0)); const inputTrap = Mousetrap(input.get(0));
function enableHistory() {
const history = [""];
let position = 0;
input.on("input", () => {
position = 0;
});
inputTrap.bind("enter", function() {
position = 0;
const text = input.val();
if (text.length === 0) {
return false;
}
// Submit the form when pressing enter instead of inserting a new line
form.trigger("submit");
// Store new message in history if last message isn't already equal
if (history[1] !== text) {
history.splice(1, 0, text);
}
return false;
});
inputTrap.bind(["up", "down"], function(e, key) {
if (e.target.selectionStart !== e.target.selectionEnd || input.data("autocompleting")) {
return;
}
if (position === 0) {
history[position] = input.val();
}
if (key === "up") {
if (position < history.length - 1) {
position++;
}
} else if (position > 0) {
position--;
}
input.val(history[position]);
return false;
});
}
enableHistory();
const colorsHotkeys = { const colorsHotkeys = {
k: "\x03", k: "\x03",
b: "\x02", b: "\x02",

View File

@ -1,91 +0,0 @@
import jQuery from "jquery";
/*!
* inputhistory
* https://github.com/erming/inputhistory
* v0.3.1
*/
(function($) {
$.inputhistory = {};
$.inputhistory.defaultOptions = {
history: [],
preventSubmit: false
};
$.fn.history = // Alias
$.fn.inputhistory = function(options) {
options = $.extend(
$.inputhistory.defaultOptions,
options
);
var self = this;
if (self.length > 1) {
return self.each(function() {
$(this).history(options);
});
}
var history = options.history;
history.push("");
var i = 0;
self.on("keydown", function(e) {
var key = e.which;
switch (key) {
case 13: // Enter
if (e.shiftKey || self.data("autocompleting")) {
return; // multiline input
}
if (self.val() != "") {
i = history.length;
history[i - 1] = self.val();
history.push("");
if (history[i - 1] == history[i - 2]) {
history.splice(-2, 1);
i--;
}
}
if (!options.preventSubmit) {
self.parents("form").eq(0).submit();
}
self.val("");
break;
case 38: // Up
case 40: // Down
// NOTICE: This is specific to The Lounge.
if (e.ctrlKey || e.altKey || e.metaKey || self.data("autocompleting")) {
break;
}
if (
this.value.indexOf("\n") >= 0
&&
(key === 38 && this.selectionStart > 0)
||
(key === 40 && this.selectionStart < this.value.length))
{
return; // don't prevent default
}
history[i] = self.val();
if (key == 38 && i != 0) {
i--;
} else if (key == 40 && i < history.length - 1) {
i++;
}
self.val(history[i]);
break;
default:
return;
}
e.preventDefault();
});
return this;
}
})(jQuery);

View File

@ -7,7 +7,6 @@ const moment = require("moment");
const URI = require("urijs"); const URI = require("urijs");
// our libraries // our libraries
require("./libs/jquery/inputhistory");
require("./libs/jquery/stickyscroll"); require("./libs/jquery/stickyscroll");
const slideoutMenu = require("./slideout"); const slideoutMenu = require("./slideout");
const templates = require("../views"); const templates = require("../views");
@ -212,7 +211,6 @@ $(function() {
} }
const input = $("#input") const input = $("#input")
.history()
.on("input", function() { .on("input", function() {
const style = window.getComputedStyle(this); const style = window.getComputedStyle(this);