85 lines
3.3 KiB
JavaScript
85 lines
3.3 KiB
JavaScript
const AUTH_TOKEN_KEY = "scud_api_auth_token";
|
|
const SESSION_ID = "web_session_main";
|
|
const STORAGE_KEY = "scud_chat_input_history";
|
|
|
|
let API_TOKEN = localStorage.getItem(AUTH_TOKEN_KEY) || "";
|
|
let CURRENT_USERNAME = localStorage.getItem("scud_username") || "";
|
|
let IS_ADMIN = localStorage.getItem("scud_is_admin") === "true";
|
|
let IS_GUEST = localStorage.getItem("scud_is_guest") === "true";
|
|
|
|
let inputHistory = JSON.parse(localStorage.getItem(STORAGE_KEY) || "[]");
|
|
let historyIndex = -1;
|
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
const userInputEl = document.getElementById("user-input");
|
|
|
|
if (userInputEl) {
|
|
userInputEl.addEventListener("input", function() {
|
|
this.style.height = "auto";
|
|
this.style.height = Math.min(this.scrollHeight, 80) + "px";
|
|
});
|
|
|
|
userInputEl.addEventListener("keydown", function(e) {
|
|
// Отправка по Enter
|
|
if (e.key === "Enter" && !e.shiftKey) {
|
|
e.preventDefault();
|
|
|
|
const val = this.value.trim();
|
|
if (val) {
|
|
// Сохраняем команду в историю
|
|
if (inputHistory.length === 0 || inputHistory[inputHistory.length - 1] !== val) {
|
|
inputHistory.push(val);
|
|
if (inputHistory.length > 50) inputHistory.shift();
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(inputHistory));
|
|
}
|
|
historyIndex = -1;
|
|
}
|
|
|
|
if (typeof sendMessage === "function") {
|
|
sendMessage(e);
|
|
}
|
|
}
|
|
// История: стрелка ВВЕРХ
|
|
else if (e.key === "ArrowUp") {
|
|
if (inputHistory.length > 0 && historyIndex < inputHistory.length - 1) {
|
|
e.preventDefault();
|
|
if (historyIndex === -1) {
|
|
this.dataset.draft = this.value;
|
|
}
|
|
historyIndex++;
|
|
this.value = inputHistory[inputHistory.length - 1 - historyIndex];
|
|
this.dispatchEvent(new Event("input"));
|
|
setTimeout(() => this.setSelectionRange(this.value.length, this.value.length), 0);
|
|
}
|
|
}
|
|
// История: стрелка ВНИЗ
|
|
else if (e.key === "ArrowDown") {
|
|
if (historyIndex !== -1) {
|
|
e.preventDefault();
|
|
if (historyIndex > 0) {
|
|
historyIndex--;
|
|
this.value = inputHistory[inputHistory.length - 1 - historyIndex];
|
|
} else {
|
|
historyIndex = -1;
|
|
this.value = this.dataset.draft || "";
|
|
}
|
|
this.dispatchEvent(new Event("input"));
|
|
setTimeout(() => this.setSelectionRange(this.value.length, this.value.length), 0);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
if (IS_GUEST) {
|
|
hideAuthModal();
|
|
updateUIState();
|
|
} else if (API_TOKEN) {
|
|
hideAuthModal();
|
|
updateUIState();
|
|
if (typeof loadTasks === "function") {
|
|
loadTasks();
|
|
}
|
|
} else {
|
|
showAuthModal();
|
|
}
|
|
}); |