Skip to content

Commit

Permalink
ability to change html messages using the changeMessage method
Browse files Browse the repository at this point in the history
  • Loading branch information
OvidijusParsiunas committed Dec 1, 2024
1 parent cd516fa commit 79ed937
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
10 changes: 7 additions & 3 deletions component/src/views/chat/messages/html/htmlMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ export class HTMLMessages {
return messageElements;
}

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

// prettier-ignore
private static overwrite(messages: MessagesBase, html: string, role: string, messagesEls: MessageElements[]) {
const {messageToElements: msgToEls} = messages;
const overwrittenElements = MessageUtils.overwriteMessage(
msgToEls, messagesEls, html, role, 'html', HTMLMessages.HTML_BUBBLE_CLASS);
if (overwrittenElements) {
overwrittenElements.bubbleElement.innerHTML = html;
HTMLUtils.apply(messages, overwrittenElements.outerContainer);
Legacy.flagHTMLUpdateClass(overwrittenElements.bubbleElement);
HTMLMessages.overwriteElements(messages, html, overwrittenElements);
}
return overwrittenElements;
}
Expand Down
22 changes: 18 additions & 4 deletions component/src/views/chat/messages/messageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,16 @@ export class MessageUtils {
});
}

private static removeText(msg: MessagesBase, messageToEls: MessageToElements[0]) {
private static changeHTMLMessage(msg: MessagesBase, messageToEls: MessageToElements[0], newHTML: string) {
if (messageToEls[1].html) {
HTMLMessages.overwriteElements(msg, newHTML, messageToEls[1].html);
} else {
// add new
}
messageToEls[0].html = newHTML;
}

private static removeTextMessage(msg: MessagesBase, messageToEls: MessageToElements[0]) {
const elemsToRemove = messageToEls[1].text;
const removalElsIndex = msg.messageElementRefs.findIndex((messageElements) => messageElements === elemsToRemove);
msg.messageElementRefs.splice(removalElsIndex, 1);
Expand All @@ -205,7 +214,7 @@ export class MessageUtils {
delete messageToEls[1].text;
}

private static changeText(msg: MessagesBase, messageToEls: MessageToElements[0], newText: string) {
private static changeTextMessage(msg: MessagesBase, messageToEls: MessageToElements[0], newText: string) {
if (messageToEls[1].text) {
msg.renderText(messageToEls[1].text.bubbleElement, newText);
} else {
Expand All @@ -222,9 +231,14 @@ export class MessageUtils {
public static changeMessage(msg: MessagesBase, messageToEls: MessageToElements[0], messageBody: MessageBody) {
if (messageToEls) {
if (messageBody.text) {
MessageUtils.changeText(msg, messageToEls, messageBody.text);
MessageUtils.changeTextMessage(msg, messageToEls, messageBody.text);
} else if (messageToEls[1].text) {
MessageUtils.removeTextMessage(msg, messageToEls);
}
if (messageBody.html) {
MessageUtils.changeHTMLMessage(msg, messageToEls, messageBody.html);
} else if (messageToEls[1].text) {
MessageUtils.removeText(msg, messageToEls);
// MessageUtils.removeTextMessage(msg, messageToEls);
}
} else {
console.error('Message index not found. Please use the `getMessages` method to find the correct index');
Expand Down

0 comments on commit 79ed937

Please sign in to comment.