Opera10 commited on
Commit
fb4532e
·
verified ·
1 Parent(s): 7eb8b38

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +104 -5
templates/index.html CHANGED
@@ -1221,13 +1221,26 @@
1221
  });
1222
  }
1223
 
1224
- async function saveMessage(role, text, audioUrl = null, imageBase64 = null, timestamp = null) {
1225
  let displayTitle = text ? text.substring(0, 20) + "..." : "تصویر ارسال شد";
 
 
 
1226
  if (!currentSessionId) {
1227
  await createNewSession(displayTitle);
1228
  }
1229
  const msgTime = timestamp || Date.now();
1230
- const message = { sessionId: currentSessionId, role: role, text: text, audioUrl: audioUrl, imageBase64: imageBase64, timestamp: msgTime };
 
 
 
 
 
 
 
 
 
 
1231
  const tx = db.transaction('messages', 'readwrite');
1232
  tx.objectStore('messages').add(message);
1233
  if (role === 'user') {
@@ -1303,8 +1316,15 @@
1303
  } else {
1304
  document.getElementById('emptyState').style.display = 'none';
1305
  messages.forEach(msg => {
1306
- if(msg.role === 'user') appendUserMessageUI(msg.text, true, msg.imageBase64, msg.timestamp);
1307
- else appendBotMessageUI(msg.text, msg.audioUrl, true);
 
 
 
 
 
 
 
1308
  });
1309
  setTimeout(() => {
1310
  const rows = chatArea.querySelectorAll('.message-row');
@@ -1461,6 +1481,85 @@
1461
  chatArea.insertBefore(row, document.getElementById('dynamicSpacer'));
1462
  }
1463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1464
  let currentBotRow = null;
1465
  let currentBotBubble = null;
1466
  let currentBotText = "";
@@ -1649,7 +1748,7 @@
1649
  `;
1650
  }
1651
 
1652
- saveMessage('bot', "مقاله شما ایجاد شد.\n" + fullText, null, null, Date.now());
1653
  }
1654
  else if (data.type === 'error') {
1655
  contentBox.innerHTML += `<br><span style="color:red;">خطا: ${data.message}</span>`;
 
1221
  });
1222
  }
1223
 
1224
+ async function saveMessage(role, text, audioUrl = null, imageBase64 = null, timestamp = null, pdfUrl = null, docxUrl = null, isDocument = false) {
1225
  let displayTitle = text ? text.substring(0, 20) + "..." : "تصویر ارسال شد";
1226
+ if (isDocument) {
1227
+ displayTitle = "📄 مقاله: " + displayTitle;
1228
+ }
1229
  if (!currentSessionId) {
1230
  await createNewSession(displayTitle);
1231
  }
1232
  const msgTime = timestamp || Date.now();
1233
+ const message = {
1234
+ sessionId: currentSessionId,
1235
+ role: role,
1236
+ text: text,
1237
+ audioUrl: audioUrl,
1238
+ imageBase64: imageBase64,
1239
+ timestamp: msgTime,
1240
+ pdfUrl: pdfUrl,
1241
+ docxUrl: docxUrl,
1242
+ isDocument: isDocument
1243
+ };
1244
  const tx = db.transaction('messages', 'readwrite');
1245
  tx.objectStore('messages').add(message);
1246
  if (role === 'user') {
 
1316
  } else {
1317
  document.getElementById('emptyState').style.display = 'none';
1318
  messages.forEach(msg => {
1319
+ if(msg.role === 'user') {
1320
+ appendUserMessageUI(msg.text, true, msg.imageBase64, msg.timestamp);
1321
+ } else {
1322
+ if (msg.isDocument) {
1323
+ appendBotDocumentUI(msg.text, msg.pdfUrl, msg.docxUrl, true);
1324
+ } else {
1325
+ appendBotMessageUI(msg.text, msg.audioUrl, true);
1326
+ }
1327
+ }
1328
  });
1329
  setTimeout(() => {
1330
  const rows = chatArea.querySelectorAll('.message-row');
 
1481
  chatArea.insertBefore(row, document.getElementById('dynamicSpacer'));
1482
  }
1483
 
1484
+ /* --- قرارگیری فایل ها داخل کارت و دکمه های دانلود هنگام لود تاریخچه --- */
1485
+ function appendBotDocumentUI(text, pdfUrl, docxUrl, isRestore = false) {
1486
+ document.getElementById('emptyState').style.display = 'none';
1487
+ const row = document.createElement('div');
1488
+ row.className = 'message-row bot';
1489
+
1490
+ const cardId = 'doc-' + Date.now() + '-' + Math.random().toString(36).substr(2, 5);
1491
+ const contentId = 'content-' + cardId;
1492
+
1493
+ let pdfHtml = '';
1494
+ if (pdfUrl) {
1495
+ pdfHtml = `
1496
+ <div class="document-card" style="width:100%; border-right: 4px solid #ef4444;">
1497
+ <div class="doc-header" style="border-bottom:none; padding-bottom:0;">
1498
+ <div class="doc-download-actions visible">
1499
+ <a href="${pdfUrl}" class="doc-dl-btn" title="دانلود فایل PDF" download>
1500
+ <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="#ef4444" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
1501
+ <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
1502
+ <polyline points="7 10 12 15 17 10"></polyline>
1503
+ <line x1="12" y1="15" x2="12" y2="3"></line>
1504
+ </svg>
1505
+ </a>
1506
+ </div>
1507
+ <div class="doc-title-container">
1508
+ <span>فایل PDF مقاله</span>
1509
+ <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#ef4444" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line></svg>
1510
+ </div>
1511
+ </div>
1512
+ </div>
1513
+ `;
1514
+ }
1515
+
1516
+ let docxHtml = '';
1517
+ if (docxUrl) {
1518
+ docxHtml = `
1519
+ <div class="document-card" style="width:100%; border-right: 4px solid #2563eb;">
1520
+ <div class="doc-header" style="border-bottom:none; padding-bottom:0;">
1521
+ <div class="doc-download-actions visible">
1522
+ <a href="${docxUrl}" class="doc-dl-btn" title="دانلود فایل Word" download>
1523
+ <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="#2563eb" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
1524
+ <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
1525
+ <polyline points="7 10 12 15 17 10"></polyline>
1526
+ <line x1="12" y1="15" x2="12" y2="3"></line>
1527
+ </svg>
1528
+ </a>
1529
+ </div>
1530
+ <div class="doc-title-container">
1531
+ <span>فایل Word مقاله</span>
1532
+ <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#2563eb" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line></svg>
1533
+ </div>
1534
+ </div>
1535
+ </div>
1536
+ `;
1537
+ }
1538
+
1539
+ row.innerHTML = `
1540
+ <div style="display:flex; flex-direction:column; gap:12px; width:100%; max-width:320px;">
1541
+ <div class="document-card" style="width:100%;">
1542
+ <div class="doc-header">
1543
+ <div class="doc-title-container" style="width: 100%; justify-content: flex-start;">
1544
+ <span>متن مقاله ساخته شده</span>
1545
+ <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line></svg>
1546
+ </div>
1547
+ </div>
1548
+ <div class="doc-content-scroll" id="${contentId}">
1549
+ ${formatMessageText(text)}
1550
+ </div>
1551
+ </div>
1552
+ ${pdfHtml}
1553
+ ${docxHtml}
1554
+ </div>
1555
+ <div class="action-buttons">
1556
+ <button class="action-btn" title="کپی" onclick="copyMessageText(this)">${COPY_ICON_SVG}</button>
1557
+ </div>
1558
+ `;
1559
+
1560
+ chatArea.insertBefore(row, document.getElementById('dynamicSpacer'));
1561
+ }
1562
+
1563
  let currentBotRow = null;
1564
  let currentBotBubble = null;
1565
  let currentBotText = "";
 
1748
  `;
1749
  }
1750
 
1751
+ saveMessage('bot', fullText, null, null, Date.now(), data.pdf_url, data.docx_url, true);
1752
  }
1753
  else if (data.type === 'error') {
1754
  contentBox.innerHTML += `<br><span style="color:red;">خطا: ${data.message}</span>`;