4
|
1 'use strict';
|
|
2
|
|
3 function openRenameChat(name) {
|
|
4 let dialog = document.querySelector('dialog[rename]');
|
|
5 dialog.querySelector('input[name=name]').value = name;
|
|
6 dialog.showModal();
|
|
7 }
|
|
8
|
|
9 function renameChat() {
|
|
10 ajax(`rename_chat.js?chat=${chatId}`);
|
|
11 }
|
|
12
|
|
13 function saveRenameChat() {
|
|
14 let dialog = document.querySelector('dialog[rename]');
|
|
15 let name = dialog.querySelector('input[name=name]').value;
|
|
16 ajax(`save_rename_chat.js?chat=${chatId}&name=${encodeURIComponent(name)}`);
|
|
17 dialog.close();
|
|
18 }
|
|
19
|
|
20 function deleteChat() {
|
|
21 let dialog = document.querySelector('dialog[delete]');
|
|
22 dialog.showModal();
|
|
23 }
|
|
24
|
|
25 function doDeleteChat(el) {
|
|
26 closeModal(el);
|
|
27 ajax(`delete_chat.js?chat=${chatId}`);
|
|
28 }
|
|
29
|
|
30 function showWaitingAiIcon() {
|
|
31 document.querySelector('[waiting-ai-icon]').style.display = 'block';
|
|
32 }
|
|
33
|
5
|
34 function hideWaitingAiIcon() {
|
|
35 document.querySelector('[waiting-ai-icon]').style.display = 'none';
|
|
36 }
|
|
37
|
6
|
38 // requires markdown-it
|
|
39 function handleMarkdown() {
|
|
40 let converter = window.markdownit();
|
|
41 let divs = document.querySelectorAll('[role]');
|
|
42 for( let div of divs ) {
|
|
43 div.innerHTML = converter.render(div.textContent);
|
|
44 }
|
|
45 }
|
|
46
|
5
|
47 function updateAi(html) {
|
|
48 hideWaitingAiIcon();
|
|
49 document.querySelector('div[messages]').innerHTML = html;
|
6
|
50 handleMarkdown();
|
5
|
51 document.querySelector('textarea').focus();
|
6
|
52 window.scrollTo(0, document.body.scrollHeight);
|
5
|
53 /*
|
|
54 let scroll = aiDialog.querySelector('[scroll]');
|
|
55 setTimeout(function(){
|
|
56 scroll.scrollTo(0,scroll.scrollHeight);
|
|
57 });
|
|
58 */
|
|
59 }
|
|
60
|
4
|
61 const isMobile = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
|
|
62
|
|
63 function textareaKey(event) {
|
|
64 if( event.keyCode===13 && !event.shiftKey && !event.ctrlKey && !isMobile ) {
|
|
65 event.preventDefault();
|
|
66 askAi();
|
|
67 }
|
|
68 }
|
|
69
|
|
70 function fixTextarea(event) {
|
|
71 let textarea = event.target;
|
|
72 textarea.style.height = 'initial';
|
|
73 textarea.style.height = (textarea.scrollHeight+2) + 'px';
|
|
74 textarea.scrollIntoViewIfNeeded(false);
|
|
75 }
|
|
76
|
|
77 function askAi() {
|
|
78 let input = document.querySelector('textarea');
|
5
|
79 let url = `ai_ask.js?chat=${chatId}&input=${encodeURIComponent(input.value)}`;
|
4
|
80 ajax(url);
|
|
81 input.value = '';
|
|
82 showWaitingAiIcon();
|
|
83 }
|