Skip to content

Commit

Permalink
fix: Debounce set enabled function
Browse files Browse the repository at this point in the history
  • Loading branch information
TatuLund committed Jan 8, 2024
1 parent 8aab0e5 commit 818fe0f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/vaadin/tinymce/TinyMce.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

};

}
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/org/vaadin/tinymce/OnAttachTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}
}

0 comments on commit 818fe0f

Please sign in to comment.