From ce78b55e62489e2885b789e8f05962e34c6df68d Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Fri, 19 Jan 2024 15:38:01 +0200 Subject: [PATCH] Fixed re-attaching in certain cases & using enabled/readOnly flags at the same time related to #27 --- src/main/java/org/vaadin/tinymce/TinyMce.java | 18 ++++++-- .../tinymce/PreserveOnRefreshBug27.java | 44 +++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/vaadin/tinymce/PreserveOnRefreshBug27.java diff --git a/src/main/java/org/vaadin/tinymce/TinyMce.java b/src/main/java/org/vaadin/tinymce/TinyMce.java index 02764d2..832031b 100644 --- a/src/main/java/org/vaadin/tinymce/TinyMce.java +++ b/src/main/java/org/vaadin/tinymce/TinyMce.java @@ -67,6 +67,8 @@ public class TinyMce extends AbstractCompositeField 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 @@ -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 @@ -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 diff --git a/src/test/java/org/vaadin/tinymce/PreserveOnRefreshBug27.java b/src/test/java/org/vaadin/tinymce/PreserveOnRefreshBug27.java new file mode 100644 index 0000000..bf6db77 --- /dev/null +++ b/src/test/java/org/vaadin/tinymce/PreserveOnRefreshBug27.java @@ -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("

Hallo Leute,

"); + 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); + } +}