Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mode change on reattach and save on close and reload #36

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading