208 lines
7.9 KiB
JavaScript
208 lines
7.9 KiB
JavaScript
/**
|
|
* ===============================================================================
|
|
* FILE: modules/web_api/static/js/chat/input.js
|
|
* ROLE: Управление полем ввода (textarea), автовысота, история команд и Drag-and-Drop.
|
|
* ===============================================================================
|
|
*/
|
|
|
|
window.ChatInput = (function() {
|
|
let currentAttachedFile = null;
|
|
const CHAT_INPUT_STORAGE_KEY = "scud_chat_input_history";
|
|
let chatInputHistory = JSON.parse(localStorage.getItem(CHAT_INPUT_STORAGE_KEY) || "[]");
|
|
let chatHistoryIndex = -1;
|
|
let temporaryCurrentInput = "";
|
|
|
|
function saveCommandToHistory(commandText) {
|
|
if (!commandText || !commandText.trim()) return;
|
|
const cleanCmd = commandText.trim();
|
|
if (cleanCmd.startsWith("action:save_draft_")) return;
|
|
|
|
chatInputHistory = chatInputHistory.filter(item => item !== cleanCmd);
|
|
chatInputHistory.push(cleanCmd);
|
|
if (chatInputHistory.length > 50) chatInputHistory.shift();
|
|
localStorage.setItem(CHAT_INPUT_STORAGE_KEY, JSON.stringify(chatInputHistory));
|
|
chatHistoryIndex = -1;
|
|
}
|
|
|
|
function updateInputHeightAndFade(textarea) {
|
|
if (!textarea) return;
|
|
|
|
if (!textarea.value || textarea.value.trim() === '') {
|
|
textarea.style.height = '32px';
|
|
textarea.style.overflowY = 'hidden';
|
|
textarea.style.maskImage = 'none';
|
|
textarea.style.webkitMaskImage = 'none';
|
|
return;
|
|
}
|
|
|
|
textarea.style.height = 'auto';
|
|
const minHeight = 32;
|
|
const maxHeight = 140;
|
|
const currentScrollHeight = textarea.scrollHeight;
|
|
|
|
if (currentScrollHeight <= minHeight + 2) {
|
|
textarea.style.height = minHeight + 'px';
|
|
textarea.style.overflowY = 'hidden';
|
|
textarea.style.maskImage = 'none';
|
|
textarea.style.webkitMaskImage = 'none';
|
|
} else if (currentScrollHeight > maxHeight) {
|
|
textarea.style.height = maxHeight + 'px';
|
|
textarea.style.overflowY = 'auto';
|
|
textarea.style.maskImage = 'linear-gradient(to bottom, transparent 0%, black 14px, black 100%)';
|
|
textarea.style.webkitMaskImage = 'linear-gradient(to bottom, transparent 0%, black 14px, black 100%)';
|
|
} else {
|
|
textarea.style.height = currentScrollHeight + 'px';
|
|
textarea.style.overflowY = 'hidden';
|
|
textarea.style.maskImage = 'none';
|
|
textarea.style.webkitMaskImage = 'none';
|
|
}
|
|
}
|
|
|
|
function clearAttachedFile() {
|
|
currentAttachedFile = null;
|
|
const preview = document.getElementById("file-attachment-preview");
|
|
const nameEl = document.getElementById("file-attachment-name");
|
|
const fileInput = document.getElementById("file-upload-input");
|
|
|
|
if (preview) preview.classList.add("hidden");
|
|
if (nameEl) nameEl.innerText = "";
|
|
if (fileInput) fileInput.value = "";
|
|
}
|
|
|
|
function handleFileSelected(file) {
|
|
if (!file) return;
|
|
currentAttachedFile = file;
|
|
const preview = document.getElementById("file-attachment-preview");
|
|
const nameEl = document.getElementById("file-attachment-name");
|
|
|
|
if (preview && nameEl) {
|
|
nameEl.innerText = file.name;
|
|
preview.classList.remove("hidden");
|
|
}
|
|
}
|
|
|
|
function getAttachedFile() {
|
|
return currentAttachedFile;
|
|
}
|
|
|
|
function resetInput(input) {
|
|
if (!input) return;
|
|
input.value = "";
|
|
input.style.height = '32px';
|
|
input.style.overflowY = 'hidden';
|
|
input.style.maskImage = 'none';
|
|
input.style.webkitMaskImage = 'none';
|
|
clearAttachedFile();
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
const input = document.getElementById("user-input");
|
|
const fileInput = document.getElementById("file-upload-input");
|
|
const overlay = document.getElementById("global-drag-overlay");
|
|
|
|
if (input) {
|
|
input.style.lineHeight = '24px';
|
|
input.style.height = '32px';
|
|
|
|
input.addEventListener("keydown", (e) => {
|
|
if (e.key === "Enter") {
|
|
if (e.shiftKey) return;
|
|
e.preventDefault();
|
|
window.sendMessage();
|
|
return;
|
|
}
|
|
|
|
if (e.key === "ArrowUp") {
|
|
const isSingleLine = !input.value.includes("\n");
|
|
const isAtBeginning = input.selectionStart === 0 && input.selectionEnd === 0;
|
|
|
|
if (isSingleLine || isAtBeginning) {
|
|
if (chatInputHistory.length > 0) {
|
|
e.preventDefault();
|
|
if (chatHistoryIndex === -1) {
|
|
temporaryCurrentInput = input.value;
|
|
chatHistoryIndex = chatInputHistory.length - 1;
|
|
} else if (chatHistoryIndex > 0) {
|
|
chatHistoryIndex--;
|
|
}
|
|
input.value = chatInputHistory[chatHistoryIndex];
|
|
updateInputHeightAndFade(input);
|
|
input.setSelectionRange(input.value.length, input.value.length);
|
|
}
|
|
}
|
|
} else if (e.key === "ArrowDown") {
|
|
const isSingleLine = !input.value.includes("\n");
|
|
const isAtEnd = input.selectionStart === input.value.length;
|
|
|
|
if (isSingleLine || isAtEnd) {
|
|
if (chatHistoryIndex !== -1) {
|
|
e.preventDefault();
|
|
if (chatHistoryIndex < chatInputHistory.length - 1) {
|
|
chatHistoryIndex++;
|
|
input.value = chatInputHistory[chatHistoryIndex];
|
|
} else {
|
|
chatHistoryIndex = -1;
|
|
input.value = temporaryCurrentInput;
|
|
}
|
|
updateInputHeightAndFade(input);
|
|
input.setSelectionRange(input.value.length, input.value.length);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
input.addEventListener("input", function() {
|
|
updateInputHeightAndFade(this);
|
|
chatHistoryIndex = -1;
|
|
});
|
|
}
|
|
|
|
if (fileInput) {
|
|
fileInput.addEventListener("change", (e) => {
|
|
if (e.target.files && e.target.files[0]) {
|
|
handleFileSelected(e.target.files[0]);
|
|
}
|
|
});
|
|
}
|
|
|
|
let dragCounter = 0;
|
|
window.addEventListener('dragenter', (e) => {
|
|
e.preventDefault();
|
|
dragCounter++;
|
|
if (overlay) overlay.classList.remove('hidden');
|
|
}, false);
|
|
|
|
window.addEventListener('dragleave', (e) => {
|
|
e.preventDefault();
|
|
dragCounter--;
|
|
if (dragCounter <= 0) {
|
|
dragCounter = 0;
|
|
if (overlay) overlay.classList.add('hidden');
|
|
}
|
|
}, false);
|
|
|
|
window.addEventListener('dragover', (e) => { e.preventDefault(); }, false);
|
|
|
|
window.addEventListener('drop', (e) => {
|
|
e.preventDefault();
|
|
dragCounter = 0;
|
|
if (overlay) overlay.classList.add('hidden');
|
|
const dt = e.dataTransfer;
|
|
if (dt && dt.files && dt.files[0]) {
|
|
handleFileSelected(dt.files[0]);
|
|
}
|
|
}, false);
|
|
});
|
|
|
|
return {
|
|
saveCommandToHistory,
|
|
updateInputHeightAndFade,
|
|
clearAttachedFile,
|
|
getAttachedFile,
|
|
resetInput
|
|
};
|
|
})();
|
|
|
|
// Для совместимости с вызовами из HTML
|
|
window.clearAttachedFile = window.ChatInput.clearAttachedFile;
|