Skip to content

Commit d1ef47d

Browse files
Merge pull request #36 from parttio/fix-mode-change-on-reattach
Fix mode change on reattach and save on close and reload
2 parents de5633d + b692121 commit d1ef47d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/main/java/org/vaadin/tinymce/TinyMce.java

+6
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ protected void onAttach(AttachEvent attachEvent) {
194194
if (attachEvent.isInitialAttach())
195195
injectTinyMceScript();
196196
initConnector();
197+
saveOnClose();
197198
}
198199

199200
@Override
@@ -221,6 +222,11 @@ rawConfig, getElement(), ta, config, currentValue,
221222
.then(res -> initialContentSent = true);
222223
});
223224
}
225+
226+
private void saveOnClose(){
227+
runBeforeClientResponse(ui -> {
228+
getElement().callJsFunction("$connector.saveOnClose");});
229+
}
224230

225231
void runBeforeClientResponse(SerializableConsumer<UI> command) {
226232
getElement().getNode().runWhenAttached(ui -> ui

src/main/resources/META-INF/resources/frontend/tinymceConnector.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@ window.Vaadin.Flow.tinymceConnector = {
22
initLazy: function (customConfig, c, ta, options, initialContent, enabled) {
33
var currentValue = ta.innerHTML;
44
var readonlyTimeout;
5-
var changeMode = 'change';
5+
var changeMode;
6+
7+
const beforeUnloadHandler = (event) => {
8+
const blurEvent = new Event("tblur");
9+
c.dispatchEvent(blurEvent);
10+
const changeEvent = new Event("tchange");
11+
changeEvent.htmlString = c.$connector.editor.getContent();
12+
c.dispatchEvent(changeEvent);
13+
};
14+
15+
window.removeEventListener("beforeunload", beforeUnloadHandler);
616

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

1627
setEditorContent : function(html) {
@@ -52,6 +63,10 @@ window.Vaadin.Flow.tinymceConnector = {
5263
parent = parent.parentElement;
5364
}
5465
return inDialog;
66+
},
67+
68+
saveOnClose : function() {
69+
window.addEventListener("beforeunload", beforeUnloadHandler);
5570
}
5671
};
5772
}

0 commit comments

Comments
 (0)