Skip to content

Commit

Permalink
removing the use of textElementsToText variable in favour of using me…
Browse files Browse the repository at this point in the history
…ssageToElements
  • Loading branch information
OvidijusParsiunas committed Nov 18, 2024
1 parent fbe854c commit 99928da
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 22 deletions.
12 changes: 3 additions & 9 deletions component/src/views/chat/messages/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,13 @@ export class Messages extends MessagesBase {
// WORK - update all message classes to use deep-chat prefix
private clearMessages(serviceIO: ServiceIO, isReset?: boolean) {
const retainedElements: MessageElements[] = [];
const retainedTextElements: [MessageElements, string][] = [];
this.messageElementRefs.forEach((message) => {
if (Messages.isActiveElement(message.bubbleElement.classList)) {
retainedElements.push(message);
} else {
message.outerContainer.remove();
}
});
this.textElementsToText.forEach((textElementToText) => {
const bubbleClasslist = textElementToText[0].bubbleElement.classList;
if (Messages.isActiveElement(bubbleClasslist)) retainedTextElements.push(textElementToText);
});
// this is a form of cleanup as this.messageElementRefs does not contain error messages
// and can only be deleted by direct search
Array.from(this.elementRef.children).forEach((messageElement) => {
Expand All @@ -342,10 +337,10 @@ export class Messages extends MessagesBase {
}
});
this.messageElementRefs = retainedElements;
const retainedMessageToElements = this.messageToElements.filter((elToMessage) => {
if (elToMessage[0].text !== undefined || elToMessage[0].html !== undefined) {
const retainedMessageToElements = this.messageToElements.filter((msgToEls) => {
if (msgToEls[0].text !== undefined || msgToEls[0].html !== undefined) {
// safe because streamed messages can't contain multiple props (text, html)
return elToMessage[1].find((els) => Messages.isActiveElement(els.bubbleElement.classList));
return msgToEls[1].find((els) => Messages.isActiveElement(els.bubbleElement.classList));
}
return false;
});
Expand All @@ -354,7 +349,6 @@ export class Messages extends MessagesBase {
if (this._introPanel?._elementRef) this._introPanel.display();
this.addIntroductoryMessages();
}
this.textElementsToText = retainedTextElements;
this._onClearMessages?.();
delete serviceIO.sessionId;
}
Expand Down
14 changes: 4 additions & 10 deletions 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 messageStyles?: MessageStyles;
readonly htmlClassUtilities: HTMLClassUtilities = {};
readonly messageToElements: MessageToElements = [];
textElementsToText: [MessageElements, string][] = [];
protected _introPanel?: IntroPanel;
protected readonly _avatars?: Avatars;
protected readonly _names?: Names;
Expand Down Expand Up @@ -65,18 +64,12 @@ export class MessagesBase {
messageElements.bubbleElement.classList.add('text-message');
this.applyCustomStyles(messageElements, role, false);
MessageUtils.fillEmptyMessageElement(messageElements.bubbleElement, text);
const textElements: [MessageElements, string] = [messageElements, text];
MessageUtils.updateRefArr(this.textElementsToText, textElements, isTop);
return messageElements;
}

private overwriteText(role: string, text: string, elementRefs: MessageElements[]) {
const elems = MessageUtils.overwriteMessage(this.messageToElements, elementRefs, text, role, 'text', 'text-message');
if (elems) {
this.renderText(elems.bubbleElement, text);
const elementToText = MessageUtils.getLastTextToElement(this.textElementsToText, elems);
if (elementToText) elementToText[1] = text;
}
if (elems) this.renderText(elems.bubbleElement, text);
return elems;
}

Expand Down Expand Up @@ -224,8 +217,9 @@ export class MessagesBase {
// this is mostly used for enabling highlight.js to highlight code if it downloads later
protected refreshTextMessages() {
this._remarkable = RemarkableConfig.createNew();
this.textElementsToText.forEach((elementToText) => {
this.renderText(elementToText[0].bubbleElement, elementToText[1]);
this.messageToElements.forEach((msgToEls) => {
// important for text bubble to be first if multiple message content properties are used
if (msgToEls[0].text) this.renderText(msgToEls[1][0].bubbleElement, msgToEls[0].text);
});
}
}
3 changes: 0 additions & 3 deletions component/src/views/chat/messages/stream/messageStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export class MessageStream {
private updateText(text: string, bubbleElement: HTMLElement, isOverwrite: boolean) {
if (!this._message) return;
this._message.text = isOverwrite ? text : this._message.text + text;
this._messages.textElementsToText[this._messages.textElementsToText.length - 1][1] = this._message.text;
this._messages.renderText(bubbleElement, this._message.text);
}

Expand All @@ -85,13 +84,11 @@ export class MessageStream {
}

public finaliseStreamedMessage() {
const {textElementsToText} = this._messages;
if (this._endStreamAfterOperation || !this._message) return;
if (this._fileAdded && !this._elements) return;
if (!this._elements) throw Error(ErrorMessages.NO_VALID_STREAM_EVENTS_SENT);
if (!this._elements.bubbleElement?.classList.contains(MessageStream.MESSAGE_CLASS)) return;
if (this._streamType === 'text') {
textElementsToText[textElementsToText.length - 1][1] = this._message.text || '';
if (this._messages.textToSpeech) TextToSpeech.speak(this._message.text || '', this._messages.textToSpeech);
} else if (this._streamType === 'html') {
if (this._elements) HTMLUtils.apply(this._messages, this._elements.outerContainer);
Expand Down

0 comments on commit 99928da

Please sign in to comment.