From 57b51bb51c90d67d70c17d492b5a8defbf2c6100 Mon Sep 17 00:00:00 2001 From: "TatuWorkLenovo\\Tatu" Date: Fri, 29 Dec 2023 11:18:03 +0200 Subject: [PATCH] Add test view --- src/main/java/org/vaadin/tinymce/TinyMce.java | 7 +- .../java/org/vaadin/tinymce/OnAttachTest.java | 67 +++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/vaadin/tinymce/OnAttachTest.java diff --git a/src/main/java/org/vaadin/tinymce/TinyMce.java b/src/main/java/org/vaadin/tinymce/TinyMce.java index e5d6e7d..30ac1be 100644 --- a/src/main/java/org/vaadin/tinymce/TinyMce.java +++ b/src/main/java/org/vaadin/tinymce/TinyMce.java @@ -99,8 +99,6 @@ protected void onAttach(AttachEvent attachEvent) { ta.setAttribute("id", id); ta.setProperty("innerHTML", currentValue); super.onAttach(attachEvent); - // if (attachEvent.isInitialAttach()) - // injectTinyMceScript(); initConnector(); } @@ -118,8 +116,9 @@ private void initConnector() { runBeforeClientResponse(ui -> { ui.getPage().executeJs( "window.Vaadin.Flow.tinymceConnector.initLazy($0, $1, $2, $3)", - rawConfig, getElement(), ta, config) - .then(res -> initialContentSent = true); + rawConfig, getElement(), ta, config).then(res -> { + initialContentSent = true; + }); }); } diff --git a/src/test/java/org/vaadin/tinymce/OnAttachTest.java b/src/test/java/org/vaadin/tinymce/OnAttachTest.java new file mode 100644 index 0000000..1878a82 --- /dev/null +++ b/src/test/java/org/vaadin/tinymce/OnAttachTest.java @@ -0,0 +1,67 @@ +package org.vaadin.tinymce; + +import java.util.Arrays; +import java.util.List; +import java.util.Random; + +import com.vaadin.flow.component.AttachEvent; +import com.vaadin.flow.component.DetachEvent; +import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; +import com.vaadin.flow.router.PreserveOnRefresh; +import com.vaadin.flow.router.Route; + +@Route("tinymce-attach") +@PreserveOnRefresh +public class OnAttachTest extends VerticalLayout { + + int i = 0; + + public OnAttachTest() { + Button button = new Button("Switch"); + List values = Arrays.asList("Value 1", "Value 2", + "Value 3"); + EditorView editor = new EditorView(values.get(i)); + button.addClickListener(e -> { + this.removeAll(); + i++; + if (i > 2) + i = 0; + editor.setValue(values.get(i)); + add(button, editor); + }); + add(button, editor); + } + + public static class EditorView extends Div { + private String value; + private TinyMce tinyMce; + + public EditorView(String value) { + this.value = value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + protected void onAttach(AttachEvent attachEvent) { + super.onAttach(attachEvent); + tinyMce = new TinyMce(); + tinyMce.addValueChangeListener(e -> { + value = e.getValue(); + }); + tinyMce.configurePlugin(true, Plugin.TABLE).configureToolbar(true, + Toolbar.TABLE); + add(tinyMce); + tinyMce.setValue(value); + } + + @Override + protected void onDetach(DetachEvent detachEvent) { + remove(tinyMce); + } + } +}