Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve state management #6

Merged
merged 4 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.flowingcode.vaadin.addons</groupId>
<artifactId>markdown-editor-addon</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<name>Markdown Editor Add-on</name>
<description>Markdown Editor for Vaadin Flow</description>
<url>https://www.flowingcode.com/en/open-source/</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -47,13 +48,16 @@ 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.
*
* @param content content to be used in the Markdown component
*/
public BaseMarkdownComponent(String content) {
setContent(content);
this.addContentChangeListener(c->this.content = c);
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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<String> listener) {
addStateChangeListener("content", String.class, listener);
}

/**
* Sets the color mode of the Markdown component.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class MarkdownEditor extends BaseMarkdownComponent {
* Constructor with empty content.
*/
public MarkdownEditor() {
super(null);
super("");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
Loading