Skip to content

Commit d009174

Browse files
committed
fix: fix update message implementation
Fix update message logic avoiding creation and removal of internal elements depending on the message and just focusing on updating the contents of the internal state, and moving the creation of the internal elements to the constructor of the component. Closes #47
1 parent c2ba082 commit d009174

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/main/java/com/flowingcode/vaadin/addons/chatassistant/ChatMessage.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package com.flowingcode.vaadin.addons.chatassistant;
2121

2222
import com.flowingcode.vaadin.addons.chatassistant.model.Message;
23-
import com.flowingcode.vaadin.addons.markdown.BaseMarkdownComponent.DataColorMode;
2423
import com.flowingcode.vaadin.addons.markdown.MarkdownViewer;
2524
import com.vaadin.flow.component.Component;
2625
import com.vaadin.flow.component.HasComponents;
@@ -46,6 +45,7 @@ public class ChatMessage<T extends Message> extends Component implements HasComp
4645
private T message;
4746
private boolean markdownEnabled;
4847
private Div loader;
48+
private MarkdownViewer markdownViewer;
4949

5050
/**
5151
* Creates a new ChatMessage based on the supplied message without markdown support.
@@ -64,6 +64,14 @@ public ChatMessage(T message) {
6464
*/
6565
public ChatMessage(T message, boolean markdownEnabled) {
6666
this.markdownEnabled = markdownEnabled;
67+
loader = new Div(new Div(),new Div(), new Div(), new Div());
68+
loader.setClassName("lds-ellipsis");
69+
loader.setVisible(false);
70+
this.add(loader);
71+
if (markdownEnabled) {
72+
markdownViewer = new MarkdownViewer(message.getContent());
73+
this.add(markdownViewer);
74+
}
6775
setMessage(message);
6876
}
6977

@@ -87,21 +95,17 @@ public void setMessage(T message) {
8795
}
8896
}
8997

98+
/**
99+
* Updates the displayed message content and loading state.
100+
* @param message
101+
*/
90102
private void updateMessage(T message) {
91-
if (message.isLoading()) {
92-
loader = new Div(new Div(),new Div(), new Div(), new Div());
93-
loader.setClassName("lds-ellipsis");
94-
this.add(loader);
95-
} else {
96-
if (loader!=null) {
97-
this.remove(loader);
98-
loader = null;
99-
}
103+
loader.setVisible(message.isLoading());
104+
if (!message.isLoading()) {
100105
if (markdownEnabled) {
101-
MarkdownViewer mdv = new MarkdownViewer(message.getContent());
102-
mdv.setDataColorMode(DataColorMode.LIGHT);
103-
this.add(mdv);
106+
markdownViewer.setContent(message.getContent());
104107
} else {
108+
this.getElement().executeJs("[...this.childNodes].forEach(node => node.nodeType === 3 && this.removeChild(node));");
105109
this.getElement().executeJs("this.appendChild(document.createTextNode($0));", message.getContent());
106110
}
107111
}

0 commit comments

Comments
 (0)