From 311ca18a40f0ae1fb33cee09bd26beb528d3dcc6 Mon Sep 17 00:00:00 2001 From: web-padawan Date: Fri, 10 May 2024 15:32:13 +0300 Subject: [PATCH 1/4] feat: add API to set color options to RichTextEditor --- .../richtexteditor/RichTextEditor.java | 28 +++++++++++++++++++ .../RichTextEditorSanitizationTest.java | 18 ++++++++++++ .../richtexteditor/RichTextEditorTest.java | 22 +++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/main/java/com/vaadin/flow/component/richtexteditor/RichTextEditor.java b/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/main/java/com/vaadin/flow/component/richtexteditor/RichTextEditor.java index eb2aeffb517..e984ff8dbc0 100644 --- a/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/main/java/com/vaadin/flow/component/richtexteditor/RichTextEditor.java +++ b/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/main/java/com/vaadin/flow/component/richtexteditor/RichTextEditor.java @@ -9,6 +9,7 @@ package com.vaadin.flow.component.richtexteditor; import java.io.Serializable; +import java.util.List; import java.util.Objects; import com.vaadin.flow.component.AbstractSinglePropertyField; @@ -35,6 +36,7 @@ import com.vaadin.flow.internal.JsonSerializer; import com.vaadin.flow.shared.Registration; +import elemental.json.JsonArray; import elemental.json.JsonObject; /** @@ -268,6 +270,32 @@ private String getDeltaValue() { return getElement().getProperty("value"); } + /** + * Gets the list of colors used by the text color picker and background + * color picker controls of the text editor. + * + * @since 24.5 + * @return the list of colors options + */ + public List getColorOptions() { + return JsonSerializer.toObjects(String.class, + (JsonArray) getElement().getPropertyRaw("colorOptions")); + } + + /** + * Sets the list of colors used by the text color picker and background + * color picker controls of the text editor. + * + * @since 24.5 + * @param colorOptions + * the list of colors to set, not null + */ + public void setColorOptions(List colorOptions) { + Objects.requireNonNull(colorOptions, "Color options must not be null"); + getElement().setPropertyJson("colorOptions", + JsonSerializer.toJson(colorOptions)); + } + static String sanitize(String html) { var settings = new org.jsoup.nodes.Document.OutputSettings(); settings.prettyPrint(false); diff --git a/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/test/java/com/vaadin/flow/component/richtexteditor/RichTextEditorSanitizationTest.java b/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/test/java/com/vaadin/flow/component/richtexteditor/RichTextEditorSanitizationTest.java index 74aa1cf065e..55c2fd2c902 100644 --- a/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/test/java/com/vaadin/flow/component/richtexteditor/RichTextEditorSanitizationTest.java +++ b/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/test/java/com/vaadin/flow/component/richtexteditor/RichTextEditorSanitizationTest.java @@ -65,6 +65,24 @@ public void sanitizeH3Tag_H3TagPersist() { RichTextEditor.sanitize("

Foo

")); } + // Style group sanitization + + @Test + public void sanitizeStyleColor_StyleColorPersist() { + Assert.assertEquals( + "

Foo

", + RichTextEditor.sanitize( + "

Foo

")); + } + + @Test + public void sanitizeStyleBackgroundColor_StyleBackgroundColorPersist() { + Assert.assertEquals( + "

Foo

", + RichTextEditor.sanitize( + "

Foo

")); + } + // Super - / Sub - scripts group sanitization @Test diff --git a/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/test/java/com/vaadin/flow/component/richtexteditor/RichTextEditorTest.java b/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/test/java/com/vaadin/flow/component/richtexteditor/RichTextEditorTest.java index 1872cffa4f2..41079637c06 100644 --- a/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/test/java/com/vaadin/flow/component/richtexteditor/RichTextEditorTest.java +++ b/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/test/java/com/vaadin/flow/component/richtexteditor/RichTextEditorTest.java @@ -10,6 +10,9 @@ import static org.junit.Assert.assertEquals; +import java.util.List; +import elemental.json.JsonArray; + import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -230,4 +233,23 @@ public void elementHasValue_wrapIntoField_propertyIsNotSetToInitialValue() { RichTextEditor field = Component.from(element, RichTextEditor.class); Assert.assertEquals("foo", field.getElement().getPropertyRaw("value")); } + + @Test + public void setColorOptions_propertyIsUpdated() { + RichTextEditor rte = new RichTextEditor(); + rte.setColorOptions( + List.of("#000000", "#0066cc", "#008a00", "#e60000")); + JsonArray jsonArray = (JsonArray) rte.getElement() + .getPropertyRaw("colorOptions"); + Assert.assertEquals(4, jsonArray.length()); + } + + @Test + public void setColorOptions_getColorOptions() { + RichTextEditor rte = new RichTextEditor(); + rte.setColorOptions( + List.of("#000000", "#0066cc", "#008a00", "#e60000")); + List options = rte.getColorOptions(); + Assert.assertEquals(4, options.size()); + } } From 30e1d58044a99807a60f770f20615ead326ab889 Mon Sep 17 00:00:00 2001 From: web-padawan Date: Mon, 13 May 2024 10:20:01 +0300 Subject: [PATCH 2/4] docs: mention that colors should be in HEX format --- .../flow/component/richtexteditor/RichTextEditor.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/main/java/com/vaadin/flow/component/richtexteditor/RichTextEditor.java b/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/main/java/com/vaadin/flow/component/richtexteditor/RichTextEditor.java index e984ff8dbc0..a99b5ed52f1 100644 --- a/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/main/java/com/vaadin/flow/component/richtexteditor/RichTextEditor.java +++ b/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/main/java/com/vaadin/flow/component/richtexteditor/RichTextEditor.java @@ -271,8 +271,8 @@ private String getDeltaValue() { } /** - * Gets the list of colors used by the text color picker and background - * color picker controls of the text editor. + * Gets the list of colors in HEX format used by the text color picker and + * background color picker controls of the text editor. * * @since 24.5 * @return the list of colors options @@ -283,8 +283,8 @@ public List getColorOptions() { } /** - * Sets the list of colors used by the text color picker and background - * color picker controls of the text editor. + * Sets the list of colors in HEX format to use by the text color picker and + * background color picker controls of the text editor. * * @since 24.5 * @param colorOptions From 3631f69a80e64c5fe80a697f6e6d02201155a1fd Mon Sep 17 00:00:00 2001 From: web-padawan Date: Wed, 28 Aug 2024 12:06:06 +0300 Subject: [PATCH 3/4] chore: run formatter --- .../flow/component/richtexteditor/RichTextEditorTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/test/java/com/vaadin/flow/component/richtexteditor/RichTextEditorTest.java b/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/test/java/com/vaadin/flow/component/richtexteditor/RichTextEditorTest.java index 41079637c06..c12b2892be0 100644 --- a/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/test/java/com/vaadin/flow/component/richtexteditor/RichTextEditorTest.java +++ b/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/test/java/com/vaadin/flow/component/richtexteditor/RichTextEditorTest.java @@ -11,7 +11,6 @@ import static org.junit.Assert.assertEquals; import java.util.List; -import elemental.json.JsonArray; import org.junit.Assert; import org.junit.Rule; @@ -28,6 +27,8 @@ import com.vaadin.flow.server.VaadinService; import com.vaadin.flow.server.VaadinSession; +import elemental.json.JsonArray; + /** * Tests for the {@link RichTextEditor}. */ From 6377a17dbbe1022040ea1a4d186307b9395cab3b Mon Sep 17 00:00:00 2001 From: web-padawan Date: Wed, 28 Aug 2024 13:33:35 +0300 Subject: [PATCH 4/4] refactor: return unmodifiable list, update JavaDoc --- .../component/richtexteditor/RichTextEditor.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/main/java/com/vaadin/flow/component/richtexteditor/RichTextEditor.java b/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/main/java/com/vaadin/flow/component/richtexteditor/RichTextEditor.java index a99b5ed52f1..d3ed9ab3777 100644 --- a/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/main/java/com/vaadin/flow/component/richtexteditor/RichTextEditor.java +++ b/vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/main/java/com/vaadin/flow/component/richtexteditor/RichTextEditor.java @@ -9,6 +9,7 @@ package com.vaadin.flow.component.richtexteditor; import java.io.Serializable; +import java.util.Collections; import java.util.List; import java.util.Objects; @@ -271,15 +272,19 @@ private String getDeltaValue() { } /** - * Gets the list of colors in HEX format used by the text color picker and - * background color picker controls of the text editor. + * Gets an unmodifiable list of colors in HEX format used by the text color + * picker and background color picker controls of the text editor. + *

+ * Returns {@code null} by default, which means the web component shows a + * default color palette. * * @since 24.5 - * @return the list of colors options + * @return an unmodifiable list of colors options */ public List getColorOptions() { - return JsonSerializer.toObjects(String.class, + List options = JsonSerializer.toObjects(String.class, (JsonArray) getElement().getPropertyRaw("colorOptions")); + return Collections.unmodifiableList(options); } /**