From 35715096f44e1eb6d074f4feeb06549c00948e7f Mon Sep 17 00:00:00 2001 From: Raghav Tirumale <62105787+MythicalCow@users.noreply.github.com> Date: Fri, 14 Jun 2024 03:15:09 -0400 Subject: [PATCH] UX Improvement: Keyboard Shortcuts for Recent Messages (#804) * added keyboard shortcuts to access old queries --- .../0044_conversation_file_filters.py | 2 +- src/khoj/interface/web/chat.html | 66 ++++++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/src/khoj/database/migrations/0044_conversation_file_filters.py b/src/khoj/database/migrations/0044_conversation_file_filters.py index cf8669f8..eecf6658 100644 --- a/src/khoj/database/migrations/0044_conversation_file_filters.py +++ b/src/khoj/database/migrations/0044_conversation_file_filters.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.10 on 2024-05-29 19:56 +# Generated by Django 4.2.11 on 2024-06-14 04:36 from django.db import migrations, models diff --git a/src/khoj/interface/web/chat.html b/src/khoj/interface/web/chat.html index 559cce13..1232bf82 100644 --- a/src/khoj/interface/web/chat.html +++ b/src/khoj/interface/web/chat.html @@ -541,6 +541,13 @@ To get started, just start typing below. You can also type / to see a list of co if (query.length === 0) return; + // if the query is not empty then update userMessages array. keep the size of the array to 10 + if (userMessages.length >= 10) { + userMessages.shift(); + } + userMessages.push(query); + resetUserMessageIndex(); + // Add message by user to chat body renderMessage(query, "you"); document.getElementById("chat-input").value = ""; @@ -1114,6 +1121,12 @@ To get started, just start typing below. You can also type / to see a list of co var query = document.getElementById("chat-input").value.trim(); console.log(`Query: ${query}`); + if (userMessages.length >= 10) { + userMessages.shift(); + } + userMessages.push(query); + resetUserMessageIndex(); + // Add message by user to chat body renderMessage(query, "you"); document.getElementById("chat-input").value = ""; @@ -1155,7 +1168,8 @@ To get started, just start typing below. You can also type / to see a list of co rawQuery: query, } } - + var userMessages = []; + var userMessageIndex = -1; function loadChat() { let chatBody = document.getElementById("chat-body"); chatBody.innerHTML = ""; @@ -1262,9 +1276,15 @@ To get started, just start typing below. You can also type / to see a list of co }, {rootMargin: '0px 0px 0px 0px'}); const fullChatLog = response.chat || []; + userMessages = []; + userMessageIndex = 0; fullChatLog.forEach((chat_log, index) => { // Render the last 10 messages immediately + // also cache user messages into array for shortcut access if (chat_log.message != null) { + if(chat_log.by !== "khoj") { + userMessages.push(chat_log.message); + } let messageElement = renderMessageWithReference( chat_log.message, chat_log.by, @@ -1284,6 +1304,7 @@ To get started, just start typing below. You can also type / to see a list of co } loadingScreen.style.height = chatBody.scrollHeight + 'px'; }); + userMessageIndex = userMessages.length; // Scroll to bottom of chat-body element chatBody.scrollTop = chatBody.scrollHeight; @@ -2068,11 +2089,35 @@ To get started, just start typing below. You can also type / to see a list of co console.error("Error loading file filters:", error); }); } + + function inputAutoFiller(key){ + var chatInput = document.getElementById("chat-input"); + console.log(key, userMessageIndex) + if (key === "up") { + if (userMessageIndex > 0) { + userMessageIndex -= 1; + chatInput.value = userMessages[userMessageIndex]; + } else { + userMessageIndex = -1; + chatInput.value = ""; + } + } else if (key === "down") { + if (userMessageIndex < userMessages.length - 1) { + userMessageIndex += 1; + chatInput.value = userMessages[userMessageIndex]; + } else if (userMessageIndex === userMessages.length - 1) { + userMessageIndex += 1; + chatInput.value = ""; + } + } + } + function resetUserMessageIndex(){ + userMessageIndex = userMessages.length; + }
- {% import 'utils.html' as utils %} {{ utils.heading_pane(user_photo, username, is_active, has_documents) }} @@ -2143,6 +2188,7 @@ To get started, just start typing below. You can also type / to see a list of co fileList.style.display = 'none'; selectedFileList.style.display = 'block'; } + }); fileInput.addEventListener('click', function(event) { @@ -2199,6 +2245,22 @@ To get started, just start typing below. You can also type / to see a list of co + +