Skip to content

Commit c500941

Browse files
vaadin-botCopilotsissbruecker
authored
fix: render markdown content on re-attachment (#7808) (#7810)
* Initial plan * Fix re-attaching Markdown component content disappearing issue * run formatter --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: sissbruecker <[email protected]> Co-authored-by: Sascha Ißbrücker <[email protected]>
1 parent 0abbcfb commit c500941

File tree

2 files changed

+25
-0
lines changed
  • vaadin-markdown-flow-parent/vaadin-markdown-flow/src

2 files changed

+25
-0
lines changed

vaadin-markdown-flow-parent/vaadin-markdown-flow/src/main/java/com/vaadin/flow/component/markdown/Markdown.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Objects;
1919

20+
import com.vaadin.flow.component.AttachEvent;
2021
import com.vaadin.flow.component.Component;
2122
import com.vaadin.flow.component.HasSize;
2223
import com.vaadin.flow.component.Tag;
@@ -89,6 +90,15 @@ public String getContent() {
8990
return serverContent;
9091
}
9192

93+
@Override
94+
protected void onAttach(AttachEvent attachEvent) {
95+
super.onAttach(attachEvent);
96+
if (serverContent != null) {
97+
clientContent = null;
98+
scheduleContentUpdate();
99+
}
100+
}
101+
92102
private void scheduleContentUpdate() {
93103
getElement().getNode()
94104
.runWhenAttached(ui -> ui.beforeClientResponse(this, ctx -> {

vaadin-markdown-flow-parent/vaadin-markdown-flow/src/test/java/com/vaadin/flow/component/markdown/tests/MarkdownTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ public void testDetach_setContent_attach() {
138138
Assert.assertEquals("**Hello** _World_", markdown.getContent());
139139
}
140140

141+
@Test
142+
public void testReAttachment_contentPreserved() {
143+
// Set content while attached
144+
markdown.setContent("**Hello** _World_");
145+
assertUpdateMarkdownCall(markdown, "**Hello** _World_", false);
146+
147+
// Detach the component
148+
markdown.removeFromParent();
149+
150+
// Re-attach the component - content should be sent to client again
151+
ui.add(markdown);
152+
assertUpdateMarkdownCall(markdown, "**Hello** _World_", false);
153+
Assert.assertEquals("**Hello** _World_", markdown.getContent());
154+
}
155+
141156
private void assertUpdateMarkdownCall(Component component, String content,
142157
boolean isAppend) {
143158
var pendingJavaScriptInvocations = getPendingJavaScriptInvocations();

0 commit comments

Comments
 (0)