Skip to content

Commit

Permalink
removing htmlElementsToMessage in favour of using messageToElements
Browse files Browse the repository at this point in the history
  • Loading branch information
OvidijusParsiunas committed Nov 12, 2024
1 parent 3084d7b commit 6ec5e28
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 38 deletions.
2 changes: 0 additions & 2 deletions component/src/deepChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,6 @@ export class DeepChat extends InternalHTML {

setPlaceholderText: (text: string) => void = () => {};

updateHTMLMessage: (html: string, index: number) => void = () => {};

@Property('function')
onMessage?: OnMessage;

Expand Down
16 changes: 7 additions & 9 deletions component/src/views/chat/messages/html/htmlMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ export class HTMLMessages {
return messageElements;
}

public static overwrite(messages: MessagesBase, html: string, elements: MessageElements) {
elements.bubbleElement.innerHTML = html;
HTMLUtils.apply(messages, elements.outerContainer);
Legacy.flagHTMLUpdateClass(elements.bubbleElement);
}

private static overwriteLast(messages: MessagesBase, html: string, role: string, messagesEls: MessageElements[]) {
private static overwrite(messages: MessagesBase, html: string, role: string, messagesEls: MessageElements[]) {
const {messageToElements: msgToEls} = messages;
const overwrittenElements = MessageUtils.overwriteMessage(msgToEls, messagesEls, html, role, 'html', 'html-message');
if (overwrittenElements) HTMLMessages.overwrite(messages, html, overwrittenElements);
if (overwrittenElements) {
overwrittenElements.bubbleElement.innerHTML = html;
HTMLUtils.apply(messages, overwrittenElements.outerContainer);
Legacy.flagHTMLUpdateClass(overwrittenElements.bubbleElement);
}
return overwrittenElements;
}

Expand All @@ -36,7 +34,7 @@ export class HTMLMessages {
messages: MessagesBase, html: string, role: string,
messagesEls: MessageElements[], overwrite?: Overwrite, isTop = false) {
if (overwrite?.status) {
const overwrittenElements = this.overwriteLast(messages, html, role, messagesEls);
const overwrittenElements = this.overwrite(messages, html, role, messagesEls);
if (overwrittenElements) return overwrittenElements;
overwrite.status = false;
}
Expand Down
26 changes: 0 additions & 26 deletions component/src/views/chat/messages/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export class Messages extends MessagesBase {
deepChat.addMessage = (message: ResponseI, isUpdate?: boolean) => {
this.addAnyMessage({...message, sendUpdate: !!isUpdate}, !isUpdate);
};
deepChat.updateHTMLMessage = this.updateHTMLMessage.bind(this);
// interface - setUpMessagesForService
if (serviceIO.isWebModel()) (serviceIO as WebModel).setUpMessages(this);
if (demo) this.prepareDemo(demo);
Expand Down Expand Up @@ -167,10 +166,6 @@ export class Messages extends MessagesBase {
if (message.html !== undefined && message.html !== null) {
const elements = HTMLMessages.add(this, message.html, message.role, this.messageElementRefs, overwrite, isTop);
if (HTMLDeepChatElements.isElementTemporary(elements)) delete message.html;
if (message.html) {
const htmlElements: [MessageElements, MessageContentI] = [elements, message];
MessageUtils.updateRefArr(this.htmlElementsToMessage, htmlElements, isTop);
}
}
if (this.isValidMessageContent(message) && !isTop) {
this.updateStateOnMessage(message, data.overwrite, data.sendUpdate, isHistory);
Expand Down Expand Up @@ -363,25 +358,4 @@ export class Messages extends MessagesBase {
this._onClearMessages?.();
delete serviceIO.sessionId;
}

private updateHTMLMessage(html: string, index: number) {
if (html === undefined || typeof html !== 'string') {
return console.error('The first argument of updateHTMLMessage must be of type String');
}
if (index === undefined || typeof index !== 'number') {
return console.error('The second argument of updateHTMLMessage must be of type Number');
}
const processedIndex = Math.floor(index);
const message = this.messageToElements[processedIndex][0];
if (!message?.html) {
return console.error(`The message at index ${processedIndex} does not contain a 'html' message`);
}
const elToMessage = this.htmlElementsToMessage.find((htmlElementToMessage) => htmlElementToMessage[1] === message);
if (elToMessage) {
HTMLMessages.overwrite(this, html, elToMessage[0]);
elToMessage[1].html = html;
} else {
return console.error('Deep Chat error - HTML message was not found');
}
}
}
1 change: 0 additions & 1 deletion component/src/views/chat/messages/messagesBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export class MessagesBase {
readonly htmlClassUtilities: HTMLClassUtilities = {};
readonly messageToElements: MessageToElements = [];
textElementsToText: [MessageElements, string][] = [];
htmlElementsToMessage: [MessageElements, MessageContentI][] = [];
protected _introPanel?: IntroPanel;
protected readonly _avatars?: Avatars;
protected readonly _names?: Names;
Expand Down

0 comments on commit 6ec5e28

Please sign in to comment.