From 026f1db5bd1d977c57de28fea1b09e22f419033c Mon Sep 17 00:00:00 2001 From: Martin Lopez Date: Fri, 18 Oct 2024 18:49:47 -0300 Subject: [PATCH] feat: improve content state management Improve content state management by adding a state change listener and storing the state as an internal variable Fixes #4 --- .../addons/markdown/BaseMarkdownComponent.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/flowingcode/vaadin/addons/markdown/BaseMarkdownComponent.java b/src/main/java/com/flowingcode/vaadin/addons/markdown/BaseMarkdownComponent.java index 97c8d9b..6f7b54e 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/markdown/BaseMarkdownComponent.java +++ b/src/main/java/com/flowingcode/vaadin/addons/markdown/BaseMarkdownComponent.java @@ -23,6 +23,7 @@ import com.vaadin.flow.component.dependency.CssImport; import com.vaadin.flow.component.dependency.NpmPackage; import com.vaadin.flow.component.react.ReactAdapterComponent; +import com.vaadin.flow.function.SerializableConsumer; /** * Base class for Markdown based Components. @@ -47,6 +48,8 @@ public class BaseMarkdownComponent extends ReactAdapterComponent implements HasS */ public enum DataColorMode {DARK,LIGTH,AUTO}; + private String content; + /** * Base constructor that receives the content of the markdown component. * @@ -54,6 +57,7 @@ public enum DataColorMode {DARK,LIGTH,AUTO}; */ public BaseMarkdownComponent(String content) { setContent(content); + this.addContentChangeListener(c->this.content = c); } /** @@ -62,7 +66,7 @@ public BaseMarkdownComponent(String content) { * @return the content of the Markdown component */ public String getContent() { - return getState("content", String.class); + return content; } /** @@ -71,9 +75,19 @@ public String getContent() { * @param content retrieved from the state of the component */ public void setContent(String content) { + this.content = content; setState("content", content); } + /** + * Adds the specified listener for the content change event. + * + * @param listener the listener callback for receiving content changes + */ + public void addContentChangeListener(SerializableConsumer listener) { + addStateChangeListener("content", String.class, listener); + } + /** * Sets the color mode of the Markdown component. *