diff --git a/src/main/java/org/vaadin/tinymce/TinyMce.java b/src/main/java/org/vaadin/tinymce/TinyMce.java index ffd4365..38f0e2b 100644 --- a/src/main/java/org/vaadin/tinymce/TinyMce.java +++ b/src/main/java/org/vaadin/tinymce/TinyMce.java @@ -192,6 +192,7 @@ protected void onAttach(AttachEvent attachEvent) { ta.setProperty("innerHTML", currentValue); super.onAttach(attachEvent); initConnector(); + saveOnClose(); } @Override @@ -219,6 +220,11 @@ rawConfig, getElement(), ta, config, currentValue, }); } + private void saveOnClose(){ + runBeforeClientResponse(ui -> { + getElement().callJsFunction("$connector.saveOnClose");}); + } + void runBeforeClientResponse(SerializableConsumer command) { getElement().getNode().runWhenAttached(ui -> ui .beforeClientResponse(this, context -> command.accept(ui))); diff --git a/src/main/resources/META-INF/resources/frontend/tinymceConnector.js b/src/main/resources/META-INF/resources/frontend/tinymceConnector.js index 2f809ae..db5bf76 100644 --- a/src/main/resources/META-INF/resources/frontend/tinymceConnector.js +++ b/src/main/resources/META-INF/resources/frontend/tinymceConnector.js @@ -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 @@ -54,6 +65,10 @@ window.Vaadin.Flow.tinymceConnector = { parent = parent.parentElement; } return inDialog; + }, + + saveOnClose : function() { + window.addEventListener("beforeunload", beforeUnloadHandler); } };