diff --git a/src/main/java/org/vaadin/tinymce/TinyMce.java b/src/main/java/org/vaadin/tinymce/TinyMce.java index 9ced4a5..4394b62 100644 --- a/src/main/java/org/vaadin/tinymce/TinyMce.java +++ b/src/main/java/org/vaadin/tinymce/TinyMce.java @@ -193,8 +193,9 @@ public void focus() { @Override public void setEnabled(boolean enabled) { super.setEnabled(enabled); - runBeforeClientResponse(ui -> getElement() - .callJsFunction("$connector.setEnabled", enabled)); + runBeforeClientResponse(ui -> { + getElement().callJsFunction("$connector.setEnabled", enabled); + }); } @Override diff --git a/src/main/resources/META-INF/resources/frontend/tinymceConnector.js b/src/main/resources/META-INF/resources/frontend/tinymceConnector.js index c79b683..bc713c4 100644 --- a/src/main/resources/META-INF/resources/frontend/tinymceConnector.js +++ b/src/main/resources/META-INF/resources/frontend/tinymceConnector.js @@ -2,6 +2,7 @@ window.Vaadin.Flow.tinymceConnector = { initLazy: function (customConfig, c, ta, options) { // Check whether the connector was already initialized for the editor var currentValue = ta.innerHTML; + var readonlyTimeout; if (c.$connector) { // If connector was already set, this is re-attach, remove editor @@ -27,9 +28,14 @@ window.Vaadin.Flow.tinymceConnector = { }, setEnabled : function(enabled) { - this.editor.mode.set(enabled ? "design" : "readonly"); + // Debounce is needed if mode is attempted to be changed more than once + // during the attach + readonlyTimeout.clear(); + readonlyTimeout = setTimeout(() => { + this.editor.mode.set(enabled ? 'design' : 'readonly'); + }, 20); } - + }; } diff --git a/src/test/java/org/vaadin/tinymce/OnAttachTest.java b/src/test/java/org/vaadin/tinymce/OnAttachTest.java index 6481e29..e9ed908 100644 --- a/src/test/java/org/vaadin/tinymce/OnAttachTest.java +++ b/src/test/java/org/vaadin/tinymce/OnAttachTest.java @@ -54,6 +54,7 @@ public EditorView(String value) { getContent().addValueChangeListener(e -> { this.value = e.getValue(); }); + getContent().setEnabled(false); } public void setValue(String value, boolean immediate) { @@ -67,11 +68,13 @@ public void setValue(String value, boolean immediate) { @Override protected void onAttach(AttachEvent attachEvent) { super.onAttach(attachEvent); + getContent().setEnabled(true); getContent().setValue(value); } @Override protected void onDetach(DetachEvent detachEvent) { + getContent().setEnabled(false); } } }