diff --git a/pom.xml b/pom.xml index a15cb9d..a9e080e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.flowingcode.vaadin.addons markdown-editor-addon - 1.0.1-SNAPSHOT + 1.1.0-SNAPSHOT Markdown Editor Add-on Markdown Editor for Vaadin Flow https://www.flowingcode.com/en/open-source/ 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. * diff --git a/src/main/java/com/flowingcode/vaadin/addons/markdown/MarkdownEditor.java b/src/main/java/com/flowingcode/vaadin/addons/markdown/MarkdownEditor.java index 0491822..c4a4f85 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/markdown/MarkdownEditor.java +++ b/src/main/java/com/flowingcode/vaadin/addons/markdown/MarkdownEditor.java @@ -37,7 +37,7 @@ public class MarkdownEditor extends BaseMarkdownComponent { * Constructor with empty content. */ public MarkdownEditor() { - super(null); + super(""); } /** diff --git a/src/test/java/com/flowingcode/vaadin/addons/markdown/MarkdownEditorDemo.java b/src/test/java/com/flowingcode/vaadin/addons/markdown/MarkdownEditorDemo.java index 61ad7cd..44870cd 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/markdown/MarkdownEditorDemo.java +++ b/src/test/java/com/flowingcode/vaadin/addons/markdown/MarkdownEditorDemo.java @@ -21,7 +21,10 @@ import com.flowingcode.vaadin.addons.demo.DemoSource; import com.flowingcode.vaadin.addons.markdown.BaseMarkdownComponent.DataColorMode; +import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.combobox.ComboBox; +import com.vaadin.flow.component.notification.Notification; +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; @@ -58,6 +61,35 @@ public MarkdownEditorDemo() { break; } }); - add(mde,cb); + Button getContentButton = new Button("Show content",ev->Notification.show(mde.getContent())); + Button setSampleContent = new Button("Set sample content",ev->{ + mde.setContent(""" +# Markdown Editor Demo + +## This is a heading + +*This is a list item* + +[Link to Vaadin](https://vaadin.com) + +```java +// Sample Java code +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} +``` + +> This is a blockquote + +**This is bold text** + +_This is italic text_ + +~~This is strikethrough text~~ + """); + }); + add(mde,cb,new HorizontalLayout(getContentButton,setSampleContent)); } }