-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChatArea.js
206 lines (192 loc) · 6.73 KB
/
ChatArea.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { UINotification } from './UINotification.js';
import { AppController } from './AppController.js';
import { ExportChat } from './ExportChat.js';
import { Event } from './Event.js';
import { Hoverable } from './Hoverable.js';
import { ChatTitle } from './ChatTitle.js';
import { ChatForm } from './ChatForm.js';
export class ChatArea {
constructor() {
this.chatTitle = new ChatTitle();
this.chatForm = new ChatForm();
this.chatHistory = document.getElementById('chat-history');
this.messageInput = document.getElementById('message-input');
this.editChatButton = document.getElementById('edit-chat-button');
this.scrollToTopButton = document.getElementById('scroll-to-top-button');
this.scrollToEndButton = document.getElementById('scroll-to-end-button');
this.deleteChatButton = document.getElementById('delete-chat-button');
this.exportChatButton = document.getElementById('export-chat-button');
AppController.getCurrentChat().then((chat) => {
this.chat = chat;
this.render();
});
this.bindEventListeners();
}
render() {
// Clear history view
this.chatHistory.innerText = '';
if (this.chat) {
this.chat // Render chat history
.getMessages()
.then((messages) => {
messages.forEach((message) => {
this.createMessageDiv(message);
});
});
this.scrollToEnd();
}
this.messageInput.focus();
}
bindEventListeners() {
Event.listen('chatSelected', this.handleChatSelected.bind(this));
Event.listen('chatDeleted', this.handleChatDeleted.bind(this));
this.scrollToTopButton.addEventListener(
'click',
this.scrollToTop.bind(this),
);
this.scrollToEndButton.addEventListener(
'click',
this.scrollToEnd.bind(this),
);
this.editChatButton.addEventListener(
'click',
this.handleEditChat.bind(this),
);
this.deleteChatButton.addEventListener(
'click',
this.handleDeleteChat.bind(this),
);
this.exportChatButton.addEventListener('click', () => {
ExportChat.exportChat(this.chat, `chat-${this.chat.id}.json`);
});
this.currentMessage = this.chatHistory.querySelector('.selected');
// Select chat message with arrow up and arrow down keys
document.addEventListener('keydown', (event) => {
let next, previous;
if (event.key === 'ArrowDown') {
next = this.currentMessage
? this.currentMessage.nextElementSibling
: this.chatHistory.firstElementChild;
if (next) {
if (this.currentMessage)
this.currentMessage.classList.remove('hover');
next.classList.add('hover');
this.currentMessage = next; // Update currentMessage
this.currentMessage.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
}); // Scroll into view
}
} else if (event.key === 'ArrowUp') {
previous = this.currentMessage
? this.currentMessage.previousElementSibling
: this.chatHistory.lastElementChild;
if (previous) {
if (this.currentMessage)
this.currentMessage.classList.remove('hover');
previous.classList.add('hover');
}
this.currentMessage = previous; // Update currentMessage
this.currentMessage.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
}); // Scroll into view
}
});
}
createMessageDiv(message) {
const domId = `chat-message-${message.id}`;
const role = message.role;
const content = message.content;
// Get the template and its content
const template = document.getElementById('chat-message-template');
const messageClone = template.content.cloneNode(true);
// Find the div and span elements within the cloned template
const messageDiv = messageClone.querySelector('.chat-message');
const textSpan = messageClone.querySelector('.chat-message-text');
const deleteButton = messageClone.querySelector(
'.delete-chat-message-button',
);
const copyButton = messageClone.querySelector(
'.copy-chat-message-button .copy-button',
);
const goodButton = messageClone.querySelector('.good-chat-message-button');
const badButton = messageClone.querySelector('.bad-chat-message-button');
const flagButton = messageClone.querySelector('.flag-chat-message-button');
const flagButtons = [goodButton, badButton, flagButton];
const setFlagSelected = function (flagButton) {
flagButtons.forEach((btn) => {
btn.classList.remove('selected');
});
flagButton.classList.add('selected');
};
if (message.quality == 'bad') {
badButton.classList.add('selected');
} else if (message.quality == 'good') {
goodButton.classList.add('selected');
} else if (message.quality == 'flagged') {
flagButton.classList.add('selected');
}
// Set the class for role and text content
messageDiv.classList.add(`${role}-chat-message`);
messageDiv.id = domId;
textSpan.textContent = content;
messageDiv.spellcheck = false;
// Append to chatHistory and adjust scroll
this.chatHistory.appendChild(messageDiv);
messageDiv.dataset['id'] = message.id;
new Hoverable(messageDiv);
deleteButton.addEventListener('click', async () => {
await AppController.deleteChatMessage(message.id);
messageDiv.remove();
});
copyButton.dataset['target'] = domId;
flagButton.addEventListener('click', async () => {
//UINotification.show('Flagged message').autoDismiss();
message.quality = 'flagged';
setFlagSelected(flagButton);
await message.save();
});
goodButton.addEventListener('click', async () => {
//UINotification.show('Marked message as good').autoDismiss();
message.quality = 'good';
setFlagSelected(goodButton);
await message.save();
});
badButton.addEventListener('click', async () => {
//UINotification.show('Marked message as bad').autoDismiss();
message.quality = 'bad';
setFlagSelected(badButton);
await message.save();
});
return { element: messageDiv, textElement: textSpan };
}
handleChatDeleted(chat) {
if (chat.id === this.chat?.id) {
this.chat = null;
} else {
this.chat = chat;
}
this.render();
}
handleChatSelected(chat) {
this.chat = chat;
this.render();
}
handleEditChat() {
this.chatTitle.focus();
event.stopPropagation();
}
async handleDeleteChat() {
const chat = await AppController.getCurrentChat();
if (chat) {
await AppController.deleteChat(chat);
}
}
scrollToTop() {
this.chatHistory.scrollTop = 0;
}
scrollToEnd() {
this.chatHistory.scrollTop = this.chatHistory.scrollHeight;
}
}