feat: add support for sending messages with Ctrl+Enter

This commit modifies the `initInputContainer` function to add support for sending messages with Ctrl+Enter. If the user presses Ctrl+Enter, the message is sent without adding a new line. If the user presses Enter without Ctrl or Shift, a new line is added to the input field. The commit also removes unnecessary whitespace.
This commit is contained in:
Rankin Zheng 2023-05-03 22:36:59 +08:00
parent 99e75d8f46
commit 0445ae7931

View File

@ -5,11 +5,16 @@ function initInputContainer() {
const sendButton = document.getElementById('send-button');
messageInput.addEventListener('keypress', function (e) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
const message = messageInput.value.trim();
if (message !== '') {
sendButton.click();
if (e.key === 'Enter') {
if (e.ctrlKey) {
e.preventDefault();
const message = messageInput.value.trim();
if (message !== '') {
sendButton.click();
}
} else if (!e.shiftKey) {
e.preventDefault();
messageInput.setRangeText('\n', messageInput.selectionStart, messageInput.selectionEnd, 'end');
}
}
});
@ -19,10 +24,10 @@ function initInputContainer() {
if (message) {
// Add the user's message to the chat UI
addMessageToUI('user', message);
// Clear the input field
messageInput.value = '';
// Process and send the message to the extension
messageUtil.sendMessage({
command: 'sendMessage',