diff --git a/static/chat.html b/static/chat.html index 0dd3604..4e45036 100644 --- a/static/chat.html +++ b/static/chat.html @@ -42,7 +42,7 @@ body { font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Robo .header-actions button { padding: .35rem .75rem; background: transparent; border: 1.5px solid var(--border); border-radius: var(--radius); cursor: pointer; font-size: .78rem; color: var(--text-muted); font-weight: 500; transition: all .15s; } .header-actions button:hover { background: var(--bg); border-color: var(--accent); color: var(--accent); } -#messages { flex: 1; overflow-y: auto; padding: 1.2rem 1rem; display: flex; flex-direction: column; gap: .7rem; max-width: 900px; width: 100%; margin: 0 auto; } +#messages { flex: 1; overflow-y: auto; padding: 1.2rem 1.6rem; display: flex; flex-direction: column; gap: .7rem; width: 100%; } .msg { max-width: 85%; padding: .8rem 1.1rem; border-radius: var(--radius-lg); line-height: 1.6; white-space: pre-wrap; word-break: break-word; font-size: .9rem; box-shadow: var(--shadow-sm); position: relative; } .msg-time { display: block; font-size: .65rem; color: var(--text-muted, #999); margin-bottom: .3rem; opacity: .7; } .msg-user { align-self: flex-end; background: var(--user-bg); color: var(--user-text); border-bottom-right-radius: 4px; } @@ -63,7 +63,7 @@ body { font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Robo .typing { align-self: flex-start; color: var(--text-light); font-style: italic; font-size: .85rem; padding: .4rem 0; } /* Markdown rendered content */ -.msg-ai { white-space: normal; } +.msg-ai { white-space: normal; max-width: 96%; } .msg-ai h1, .msg-ai h2, .msg-ai h3, .msg-ai h4 { margin: .6rem 0 .3rem; font-size: .95rem; font-weight: 700; color: var(--text); } .msg-ai h2 { font-size: 1rem; } .msg-ai h3 { font-size: .95rem; } @@ -73,9 +73,15 @@ body { font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Robo .msg-ai li { margin: .15rem 0; } .msg-ai strong { font-weight: 600; } .msg-ai hr { border: none; border-top: 1px solid var(--border-light); margin: .6rem 0; } -.msg-ai table { border-collapse: collapse; margin: .4rem 0; font-size: .85rem; width: 100%; } -.msg-ai th, .msg-ai td { border: 1px solid var(--border); padding: .3rem .5rem; text-align: left; } -.msg-ai th { background: var(--bg); font-weight: 600; } +.msg-ai .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: .5rem 0; border: 1px solid var(--border); border-radius: 8px; } +.msg-ai table { border-collapse: collapse; margin: 0; font-size: .82rem; width: auto; min-width: 100%; } +.msg-ai th, .msg-ai td { border: 1px solid var(--border-light); padding: .35rem .6rem; text-align: left; vertical-align: top; white-space: normal; max-width: 360px; } +.msg-ai th { background: var(--bg); font-weight: 600; position: sticky; top: 0; } +.msg-ai td.num, .msg-ai th.num { white-space: nowrap; text-align: right; font-variant-numeric: tabular-nums; } +.msg-ai tbody tr:nth-child(even) { background: rgba(0,0,0,.022); } +.msg-table-bar { display: flex; justify-content: flex-end; margin: .5rem 0 -.2rem; } +.msg-table-bar button { background: #1f6f43; color: #fff; border: none; border-radius: 6px; cursor: pointer; font-size: .72rem; padding: .28rem .7rem; font-weight: 600; transition: background .15s; } +.msg-table-bar button:hover { background: #18603a; } .msg-ai code { background: #f5f0eb; padding: .1rem .3rem; border-radius: 3px; font-size: .85em; font-family: 'SF Mono', Menlo, monospace; } .msg-ai pre { background: #2c2c2c; color: #f5f0eb; padding: .6rem; border-radius: 6px; overflow-x: auto; margin: .4rem 0; } .msg-ai pre code { background: none; padding: 0; color: inherit; } @@ -83,7 +89,7 @@ body { font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Robo /* Input */ #input-area { background: var(--bg-card); border-top: 1px solid var(--border); padding: .8rem 1rem; box-shadow: 0 -2px 8px rgba(0,0,0,.03); } -#input-area > div { max-width: 900px; margin: 0 auto; } +#input-area > div { width: 100%; } #drop-zone { border: 2px dashed transparent; border-radius: var(--radius); transition: all .2s; padding: .3rem; } #drop-zone.dragover { border-color: var(--accent); background: var(--accent-light); } #file-list { display: flex; gap: .3rem; flex-wrap: wrap; margin-bottom: .3rem; } @@ -204,6 +210,68 @@ function renderMarkdown(text) { return text.replace(/'); } +// Числовая ячейка сметы: «1 234,56», «0,02», «110%», «—», «-». Учитываем +// неразрывные/узкие пробелы как разделители тысяч. +function isNumericCell(s) { + const t = (s || '').replace(/[\s ]/g, '').trim(); + if (!t || t === '—' || t === '-') return true; // пустые/прочерки не ломают колонку + return /\d/.test(t) && /^[\d.,%+\-–]+$/.test(t); +} + +// Пост-обработка таблиц в AI-ответе: горизонтальный скролл, числовые колонки +// (числа в одну строку, выравнивание вправо) и кнопка выгрузки в Excel. +// Идемпотентна — повторный вызов на уже обработанной таблице ничего не делает. +function enhanceTables(container) { + const tables = container.querySelectorAll('table'); + tables.forEach(table => { + if (table.closest('.table-wrap')) return; // уже обёрнута + // классификация колонок: колонка числовая, если все её data-ячейки числовые + const rows = [...table.rows]; + const bodyRows = rows.slice(1); // первая строка — шапка + const colCount = Math.max(...rows.map(r => r.cells.length)); + for (let c = 0; c < colCount; c++) { + const cells = bodyRows.map(r => r.cells[c]).filter(Boolean); + if (cells.length && cells.every(td => isNumericCell(td.textContent))) { + cells.forEach(td => td.classList.add('num')); + const th = rows[0] && rows[0].cells[c]; + if (th) th.classList.add('num'); + } + } + const wrap = document.createElement('div'); + wrap.className = 'table-wrap'; + table.parentNode.insertBefore(wrap, table); + wrap.appendChild(table); + // панель с кнопкой выгрузки + const bar = document.createElement('div'); + bar.className = 'msg-table-bar'; + const btn = document.createElement('button'); + btn.textContent = '⤓ Скачать в Excel'; + btn.onclick = () => tableToExcel(table, 'смета.xls'); + bar.appendChild(btn); + wrap.parentNode.insertBefore(bar, wrap); + }); +} + +// Выгрузка HTML-таблицы в файл, открываемый Excel/LibreOffice как лист. +// Без внешних библиотек: Excel понимает HTML-таблицу в .xls. UTF-8 BOM — +// чтобы кириллица не побилась. +function tableToExcel(table, filename) { + const html = + '
' + + '' + + '' + + '' + + '' + table.outerHTML + ''; + const blob = new Blob(['', html], {type: 'application/vnd.ms-excel'}); + const url = URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; a.download = filename; + document.body.appendChild(a); a.click(); a.remove(); + setTimeout(() => URL.revokeObjectURL(url), 1000); +} + const API = ''; let token = localStorage.getItem('token'); let pendingFiles = []; @@ -329,6 +397,7 @@ function addMsg(type, text, ts) { if (type === 'ai') { div.innerHTML = renderMarkdown(text); div.prepend(time); + enhanceTables(div); } else { div.textContent = text; div.prepend(time); @@ -600,6 +669,7 @@ async function sendMessage() { } } if (!aiDiv && !aiText) addMsg('ai', '(пустой ответ)'); + if (aiDiv) enhanceTables(aiDiv); // streaming re-renders innerHTML each chunk → enhance once at the end } catch (err) { removeTyping();