From e546ed80feb4fba1f974c9eea7e2b9371a642902 Mon Sep 17 00:00:00 2001 From: orakili Date: Mon, 8 Jul 2024 12:04:51 +0000 Subject: [PATCH] feat: submit question when pressing enter in the question textarea Refs: RW-1006 --- .../ocha_ai_chat/components/chat-form/chat-form.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/modules/ocha_ai_chat/components/chat-form/chat-form.js b/modules/ocha_ai_chat/components/chat-form/chat-form.js index ff510d0..eefe578 100644 --- a/modules/ocha_ai_chat/components/chat-form/chat-form.js +++ b/modules/ocha_ai_chat/components/chat-form/chat-form.js @@ -15,6 +15,7 @@ once('ocha-ai-chat-form', '[data-drupal-selector="edit-chat"]', context).forEach(element => { var chatContainer = document.querySelector('[data-drupal-selector="edit-chat"] .fieldset-wrapper'); var submitButton = document.querySelector('[data-drupal-selector="edit-submit"]'); + var questionTextarea = document.querySelector('[data-drupal-selector="edit-question"]'); var chatHeight = this.padChatWindow(); // Do some calculations to decide where to start our smooth scroll. @@ -111,6 +112,17 @@ } }); + // Initialize the event so that pressing enter in the input textarea + // submits the form. + questionTextarea.addEventListener('keydown', function (event) { + if (event.keyCode == 13 && !event.shiftKey) { + event.preventDefault(); + // We don't call chatSend directly because this will not trigger + // the ajax event attached to the submit button. + submitButton.dispatchEvent(new Event('mousedown')); + } + }); + // Set up feedback observers. this.feedbackObservers();