Skip to content

Commit

Permalink
Fixed re-attaching in certain cases & using enabled/readOnly flags at…
Browse files Browse the repository at this point in the history
… the same time

related to #27
  • Loading branch information
mstahv committed Jan 19, 2024
1 parent eef5a28 commit ce78b55
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/main/java/org/vaadin/tinymce/TinyMce.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class TinyMce extends AbstractCompositeField<Div, TinyMce, String>

private int debounceTimeout = 5000;
private boolean basicTinyMCECreated;
private boolean enabled = true;
private boolean readOnly = false;

/**
* Creates a new TinyMce editor with shadowroot set or disabled. The shadow
Expand Down Expand Up @@ -144,6 +146,9 @@ protected void onAttach(AttachEvent attachEvent) {

@Override
protected void onDetach(DetachEvent detachEvent) {
detachEvent.getUI().getPage().executeJs("""
tinymce.get($0).remove();
""", id);
super.onDetach(detachEvent);
initialContentSent = false;
// save the current value to the dom element in case the component gets
Expand Down Expand Up @@ -252,15 +257,22 @@ public void blur() {

@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
this.enabled = enabled;
adjustEnabledState();
}

private void adjustEnabledState() {
boolean reallyEnabled = this.enabled && !this.readOnly;
super.setEnabled(reallyEnabled);
runBeforeClientResponse(ui -> getElement()
.callJsFunction("$connector.setEnabled", enabled));
.callJsFunction("$connector.setEnabled", reallyEnabled));
}

@Override
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
super.setReadOnly(readOnly);
setEnabled(!readOnly);
adjustEnabledState();
}

@Override
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/org/vaadin/tinymce/PreserveOnRefreshBug27.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.vaadin.tinymce;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.dialog.Dialog;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PreserveOnRefresh;
import com.vaadin.flow.router.Route;

@Route
@PreserveOnRefresh
public class PreserveOnRefreshBug27 extends VerticalLayout {

public PreserveOnRefreshBug27() {
Dialog dialog = new Dialog();
TinyMce tinyMce = new TinyMce();
tinyMce.configure("branding", false);
tinyMce.configure("statusbar", false);
tinyMce.setValue("<h2>Hallo Leute,</h2>");
dialog.add(tinyMce);
dialog.add(new Button("Cancel", e -> dialog.close()));
Button open = new Button("Open", e -> dialog.open());
Button enable = new Button("Disable");
enable.addClickListener(e -> {
if ("Disable".equals(enable.getText())) {
tinyMce.setEnabled(false);
enable.setText("Enable");
} else {
tinyMce.setEnabled(true);
enable.setText("Disable");
}
});
Button readOnly = new Button("ReadOnly");
readOnly.addClickListener(e -> {
if ("ReadOnly".equals(readOnly.getText())) {
tinyMce.setReadOnly(true);
readOnly.setText("Writable");
} else {
tinyMce.setReadOnly(false);
readOnly.setText("ReadOnly");
}
});
add(open, enable, readOnly);
}
}

0 comments on commit ce78b55

Please sign in to comment.