Skip to content

Commit

Permalink
added new method to save on browser close or reload
Browse files Browse the repository at this point in the history
  • Loading branch information
mukherjeesudebi committed Jun 25, 2024
1 parent 5c98950 commit 582437d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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 @@ -192,6 +192,7 @@ protected void onAttach(AttachEvent attachEvent) {
ta.setProperty("innerHTML", currentValue);
super.onAttach(attachEvent);
initConnector();
saveOnClose();
}

@Override
Expand Down Expand Up @@ -219,6 +220,11 @@ rawConfig, getElement(), ta, config, currentValue,
});
}

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

void runBeforeClientResponse(SerializableConsumer<UI> command) {
getElement().getNode().runWhenAttached(ui -> ui
.beforeClientResponse(this, context -> command.accept(ui)));
Expand Down
15 changes: 15 additions & 0 deletions src/main/resources/META-INF/resources/frontend/tinymceConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ window.Vaadin.Flow.tinymceConnector = {
var changeMode;

var readonlyTimeout;

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);

if (c.$connector) {
// If connector was already set, this is re-attach, remove editor
// and re-init
Expand Down Expand Up @@ -54,6 +65,10 @@ window.Vaadin.Flow.tinymceConnector = {
parent = parent.parentElement;
}
return inDialog;
},

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

Expand Down

0 comments on commit 582437d

Please sign in to comment.