Skip to content

Commit

Permalink
Merge pull request #36 from parttio/fix-mode-change-on-reattach
Browse files Browse the repository at this point in the history
Fix mode change on reattach and save on close and reload
  • Loading branch information
mukherjeesudebi authored Jul 4, 2024
2 parents de5633d + b692121 commit d1ef47d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/main/java/org/vaadin/tinymce/TinyMce.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ protected void onAttach(AttachEvent attachEvent) {
if (attachEvent.isInitialAttach())
injectTinyMceScript();
initConnector();
saveOnClose();
}

@Override
Expand Down Expand Up @@ -221,6 +222,11 @@ rawConfig, getElement(), ta, config, currentValue,
.then(res -> initialContentSent = true);
});
}

private void saveOnClose(){
runBeforeClientResponse(ui -> {
getElement().callJsFunction("$connector.saveOnClose");});
}

void runBeforeClientResponse(SerializableConsumer<UI> command) {
getElement().getNode().runWhenAttached(ui -> ui
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ window.Vaadin.Flow.tinymceConnector = {
initLazy: function (customConfig, c, ta, options, initialContent, enabled) {
var currentValue = ta.innerHTML;
var readonlyTimeout;
var changeMode = 'change';
var changeMode;

const beforeUnloadHandler = (event) => {
const blurEvent = new Event("tblur");
c.dispatchEvent(blurEvent);
const changeEvent = new Event("tchange");
changeEvent.htmlString = c.$connector.editor.getContent();
c.dispatchEvent(changeEvent);
};

window.removeEventListener("beforeunload", beforeUnloadHandler);

// Check whether the connector was already initialized
if (c.$connector) {
Expand All @@ -11,6 +21,7 @@ window.Vaadin.Flow.tinymceConnector = {
c.$connector.editor.remove();
} else {
// Init connector at first visit
changeMode = 'change';
c.$connector = {

setEditorContent : function(html) {
Expand Down Expand Up @@ -52,6 +63,10 @@ window.Vaadin.Flow.tinymceConnector = {
parent = parent.parentElement;
}
return inDialog;
},

saveOnClose : function() {
window.addEventListener("beforeunload", beforeUnloadHandler);
}
};
}
Expand Down

0 comments on commit d1ef47d

Please sign in to comment.