Skip to content

Commit c863f14

Browse files
committed
feat: remove setDataColorMode from API to rely on Vaadin theme
Close #10
1 parent 655adb3 commit c863f14

File tree

6 files changed

+112
-88
lines changed

6 files changed

+112
-88
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.flowingcode.vaadin.addons</groupId>
66
<artifactId>markdown-editor-addon</artifactId>
7-
<version>1.1.1-SNAPSHOT</version>
7+
<version>1.2.0-SNAPSHOT</version>
88
<name>Markdown Editor Add-on</name>
99
<description>Markdown Editor for Vaadin Flow</description>
1010
<url>https://www.flowingcode.com/en/open-source/</url>

src/main/java/com/flowingcode/vaadin/addons/markdown/BaseMarkdownComponent.java

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.markdown;
2121

22+
import com.vaadin.flow.component.AttachEvent;
23+
import com.vaadin.flow.component.DetachEvent;
2224
import com.vaadin.flow.component.HasSize;
25+
import com.vaadin.flow.component.UI;
2326
import com.vaadin.flow.component.dependency.CssImport;
27+
import com.vaadin.flow.component.dependency.JavaScript;
2428
import com.vaadin.flow.component.dependency.NpmPackage;
2529
import com.vaadin.flow.component.react.ReactAdapterComponent;
2630
import com.vaadin.flow.function.SerializableConsumer;
@@ -33,29 +37,9 @@
3337
@NpmPackage(value = "mermaid", version = "11.2.1")
3438
@NpmPackage(value = "@uiw/react-md-editor", version = "4.0.4")
3539
@NpmPackage(value = "dompurify", version = "3.1.6")
40+
@JavaScript("./connector.js")
3641
public class BaseMarkdownComponent extends ReactAdapterComponent implements HasSize {
3742

38-
/**
39-
* Defines the color schemes for the Markdown component.
40-
*
41-
* The color mode can be set using the {@link #setDataColorMode(DataColorMode)} method.
42-
*
43-
* <ul>
44-
* <li>{@link #DARK}: Dark color scheme.
45-
* <li>{@link #LIGHT}: Light color scheme.
46-
* <li>{@link #AUTO}: Automatically detects the color scheme based on the user's system settings.
47-
* </ul>
48-
*/
49-
public enum DataColorMode {
50-
DARK,
51-
LIGHT,
52-
/**
53-
* @deprecated Use LIGHT instead
54-
*/
55-
@Deprecated
56-
LIGTH,
57-
AUTO};
58-
5943
private String content;
6044

6145
/**
@@ -96,24 +80,27 @@ public void addContentChangeListener(SerializableConsumer<String> listener) {
9680
addStateChangeListener("content", String.class, listener);
9781
}
9882

99-
/**
100-
* Sets the color mode of the Markdown component.
101-
*
102-
* @param mode the color mode of the component
103-
*/
104-
public void setDataColorMode(DataColorMode mode) {
105-
switch (mode) {
106-
case DARK:
107-
getElement().setAttribute("data-color-mode", "dark");
108-
break;
109-
case LIGTH:
110-
case LIGHT:
111-
getElement().setAttribute("data-color-mode", "light");
112-
break;
113-
case AUTO:
114-
getElement().removeAttribute("data-color-mode");
115-
break;
116-
}
83+
private void runBeforeClientResponse(SerializableConsumer<UI> command) {
84+
getElement().getNode().runWhenAttached(ui -> ui
85+
.beforeClientResponse(this, context -> command.accept(ui)));
11786
}
118-
87+
88+
@Override
89+
protected void onAttach(AttachEvent attachEvent) {
90+
super.onAttach(attachEvent);
91+
92+
runBeforeClientResponse(ui -> ui.getPage().executeJs(
93+
"window.Vaadin.Flow.fcMarkdownEditorConnector.observeThemeChange($0)",
94+
getElement()));
95+
}
96+
97+
@Override
98+
protected void onDetach(DetachEvent detachEvent) {
99+
super.onDetach(detachEvent);
100+
101+
getUI().ifPresent(ui -> ui.getPage().executeJs(
102+
"window.Vaadin.Flow.fcMarkdownEditorConnector.unobserveThemeChange($0)",
103+
this.getElement()));
104+
}
105+
119106
}

src/main/java/com/flowingcode/vaadin/addons/markdown/MarkdownViewer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
import com.vaadin.flow.component.Tag;
2424
import com.vaadin.flow.component.dependency.JsModule;
25-
import com.vaadin.flow.component.dependency.NpmPackage;
26-
import com.vaadin.flow.component.react.ReactAdapterComponent;
2725

2826
/**
2927
* Component for displaying Markdown text
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*-
2+
* #%L
3+
* Markdown Editor Add-on
4+
* %%
5+
* Copyright (C) 2024 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
(function() {
21+
22+
window.Vaadin.Flow.fcMarkdownEditorConnector = {
23+
observeThemeChange: markDownEditor => {
24+
// Check whether the connector was already initialized for markDownEditor
25+
if (markDownEditor.$connector) {
26+
return;
27+
}
28+
29+
markDownEditor.$connector = {};
30+
31+
const supportedTheme = theme => ['light', 'dark'].includes(theme);
32+
33+
const setDataColorMode = theme => {
34+
if (supportedTheme(theme)) {
35+
markDownEditor.setAttribute('data-color-mode', theme);
36+
} else {
37+
markDownEditor.removeAttribute('data-color-mode');
38+
}
39+
};
40+
41+
// Get theme from html element when using Vaadin's @Theme annotation
42+
const mainTheme = document.documentElement.getAttribute('theme');
43+
if (supportedTheme(mainTheme)) {
44+
setDataColorMode(mainTheme);
45+
} else {
46+
// Listen for theme changes in body element otherwise
47+
48+
// options for the observer (which mutations to observe)
49+
const config = { attributes: true };
50+
51+
// callback function to execute when mutations are observed
52+
const callback = (mutationList, observer) => {
53+
for (const mutation of mutationList) {
54+
if (mutation.type === 'attributes' && mutation.attributeName === 'theme') {
55+
const themeName = mutation.target.getAttribute(mutation.attributeName);
56+
setDataColorMode(themeName);
57+
}
58+
}
59+
};
60+
61+
// create an observer instance linked to the callback function
62+
markDownEditor.$connector.themeChangeObserver = new MutationObserver(callback);
63+
64+
// start observing html tag for configured mutations
65+
markDownEditor.$connector.themeChangeObserver.observe(document.documentElement, config);
66+
67+
// start observing body tag for configured mutations
68+
markDownEditor.$connector.themeChangeObserver.observe(document.body, config);
69+
}
70+
},
71+
unobserveThemeChange: markDownEditor => {
72+
// stop observing the target node for configured mutations
73+
if (markDownEditor.$connector.themeChangeObserver) {
74+
markDownEditor.$connector.themeChangeObserver.disconnect();
75+
markDownEditor.$connector.themeChangeObserver = null;
76+
}
77+
}
78+
}
79+
})();

src/test/java/com/flowingcode/vaadin/addons/markdown/MarkdownEditorDemo.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020
package com.flowingcode.vaadin.addons.markdown;
2121

2222
import com.flowingcode.vaadin.addons.demo.DemoSource;
23-
import com.flowingcode.vaadin.addons.markdown.BaseMarkdownComponent.DataColorMode;
23+
import com.vaadin.flow.component.UI;
2424
import com.vaadin.flow.component.button.Button;
2525
import com.vaadin.flow.component.combobox.ComboBox;
2626
import com.vaadin.flow.component.notification.Notification;
2727
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
2828
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
2929
import com.vaadin.flow.router.PageTitle;
3030
import com.vaadin.flow.router.Route;
31+
import com.vaadin.flow.theme.lumo.Lumo;
32+
import java.util.List;
3133

3234
@DemoSource
3335
@PageTitle("Markdown Editor Demo")
@@ -41,26 +43,6 @@ public MarkdownEditorDemo() {
4143
mde.setSizeFull();
4244
mde.setPlaceholder("Enter Markdown here");
4345
mde.setMaxLength(500);
44-
mde.setDataColorMode(DataColorMode.LIGTH);
45-
ComboBox<String> cb = new ComboBox<String>();
46-
cb.setItems("Dark","Light","Automatic");
47-
cb.setLabel("Color mode");
48-
cb.setValue("Light");
49-
cb.addValueChangeListener(ev->{
50-
switch(ev.getValue()) {
51-
case "Dark":
52-
mde.setDataColorMode(DataColorMode.DARK);
53-
break;
54-
case "Light":
55-
mde.setDataColorMode(DataColorMode.LIGHT);
56-
break;
57-
case "Automatic":
58-
mde.setDataColorMode(DataColorMode.AUTO);
59-
break;
60-
default:
61-
break;
62-
}
63-
});
6446
Button getContentButton = new Button("Show content",ev->Notification.show(mde.getContent()));
6547
Button setSampleContent = new Button("Set sample content",ev->{
6648
mde.setContent("""
@@ -90,6 +72,6 @@ public static void main(String[] args) {
9072
~~This is strikethrough text~~
9173
""");
9274
});
93-
add(mde,cb,new HorizontalLayout(getContentButton,setSampleContent));
75+
add(mde,new HorizontalLayout(getContentButton,setSampleContent));
9476
}
9577
}

src/test/java/com/flowingcode/vaadin/addons/markdown/MarkdownViewerDemo.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
package com.flowingcode.vaadin.addons.markdown;
2121

2222
import com.flowingcode.vaadin.addons.demo.DemoSource;
23-
import com.flowingcode.vaadin.addons.markdown.BaseMarkdownComponent.DataColorMode;
24-
import com.vaadin.flow.component.combobox.ComboBox;
2523
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
2624
import com.vaadin.flow.router.PageTitle;
2725
import com.vaadin.flow.router.Route;
@@ -36,7 +34,6 @@ public MarkdownViewerDemo() {
3634
setSizeFull();
3735
MarkdownViewer mdv = new MarkdownViewer();
3836
mdv.setSizeFull();
39-
mdv.setDataColorMode(DataColorMode.LIGTH);
4037
mdv.setContent("""
4138
4239
# h1 Heading
@@ -202,25 +199,6 @@ public MarkdownViewerDemo() {
202199
[^second]: Footnote text.
203200
204201
""");
205-
ComboBox<String> cb = new ComboBox<String>();
206-
cb.setItems("Dark","Light","Automatic");
207-
cb.setLabel("Color mode");
208-
cb.setValue("Light");
209-
cb.addValueChangeListener(ev->{
210-
switch(ev.getValue()) {
211-
case "Dark":
212-
mdv.setDataColorMode(DataColorMode.DARK);
213-
break;
214-
case "Light":
215-
mdv.setDataColorMode(DataColorMode.LIGHT);
216-
break;
217-
case "Automatic":
218-
mdv.setDataColorMode(DataColorMode.AUTO);
219-
break;
220-
default:
221-
break;
222-
}
223-
});
224-
add(mdv,cb);
202+
add(mdv);
225203
}
226204
}

0 commit comments

Comments
 (0)