Skip to content

Commit

Permalink
Add test view
Browse files Browse the repository at this point in the history
  • Loading branch information
TatuLund committed Dec 29, 2023
1 parent 53ea8ff commit 57b51bb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/main/java/org/vaadin/tinymce/TinyMce.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

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

Expand Down
67 changes: 67 additions & 0 deletions src/test/java/org/vaadin/tinymce/OnAttachTest.java
Original file line number Diff line number Diff line change
@@ -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<String> values = Arrays.asList("<b>Value 1</b>", "<i>Value 2</i>",
"<span style='text-decoration: underline;'>Value 3</span>");
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);
}
}
}

0 comments on commit 57b51bb

Please sign in to comment.