From 4c103b467b5d20364728158aa68382ec8d3fdbe7 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sun, 9 Sep 2018 15:23:12 +0300 Subject: [PATCH] Implement input history per channel --- client/components/ChatInput.vue | 30 ++++++++++++++++++++++++++++++ client/js/vue.js | 3 +++ 2 files changed, 33 insertions(+) diff --git a/client/components/ChatInput.vue b/client/components/ChatInput.vue index b523f3df..874cbe73 100644 --- a/client/components/ChatInput.vue +++ b/client/components/ChatInput.vue @@ -115,6 +115,29 @@ export default { } }); + inputTrap.bind(["up", "down"], (e, key) => { + if (e.target.selectionStart !== e.target.selectionEnd) { + return; + } + + if (this.channel.inputHistoryPosition === 0) { + this.channel.inputHistory[this.channel.inputHistoryPosition] = this.channel.pendingMessage; + } + + if (key === "up") { + if (this.channel.inputHistoryPosition < this.channel.inputHistory.length - 1) { + this.channel.inputHistoryPosition++; + } + } else if (this.channel.inputHistoryPosition > 0) { + this.channel.inputHistoryPosition--; + } + + this.channel.pendingMessage = this.$refs.input.value = this.channel.inputHistory[this.channel.inputHistoryPosition]; + this.setInputSize(); + + return false; + }); + if (this.$root.fileUploadEnabled) { upload.initialize(); } @@ -125,6 +148,7 @@ export default { methods: { setPendingMessage(e) { this.channel.pendingMessage = e.target.value; + this.channel.inputHistoryPosition = 0; this.setInputSize(); }, setInputSize() { @@ -166,10 +190,16 @@ export default { return false; } + this.channel.inputHistoryPosition = 0; this.channel.pendingMessage = ""; this.$refs.input.value = ""; this.setInputSize(); + // Store new message in history if last message isn't already equal + if (this.channel.inputHistory[1] !== text) { + this.channel.inputHistory.splice(1, 0, text); + } + if (text[0] === "/") { const args = text.substr(1).split(" "); const cmd = args.shift().toLowerCase(); diff --git a/client/js/vue.js b/client/js/vue.js index 2f843a71..9107760c 100644 --- a/client/js/vue.js +++ b/client/js/vue.js @@ -71,6 +71,9 @@ function findChannel(id) { } function initChannel(channel) { + channel.pendingMessage = ""; + channel.inputHistoryPosition = 0; + channel.inputHistory = [""]; channel.historyLoading = false; channel.scrolledToBottom = true;