Skip to content

Commit afb7984

Browse files
committed
Sync html content right after insertion has happened
Also added disclaimers for the functionality. Fixes #40
1 parent 61d2353 commit afb7984

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ public TinyMce configureLanguage(Language language) {
272272

273273
/**
274274
* Replaces text in the editors selection (can be just a caret position).
275+
* <p>
276+
* Note, that this updates the value on the client-side on the next round-trip,
277+
* so the value on the server side is not necessarily up-to-date, right after this
278+
* call, but will be synced soon and a value change event will be fired after a
279+
* small timeout.
280+
* </p>
275281
*
276282
* @param htmlString
277283
* the html snippet to be inserted

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

+11-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ window.Vaadin.Flow.tinymceConnector = {
1313
};
1414

1515
window.removeEventListener("beforeunload", beforeUnloadHandler);
16-
16+
17+
const syncValue = () => {
18+
const event = new Event("tchange");
19+
event.htmlString = c.$connector.editor.getContent();
20+
c.dispatchEvent(event);
21+
}
22+
1723
// Check whether the connector was already initialized
1824
if (c.$connector) {
1925
// If connector was already set, this is re-attach, remove editor
@@ -33,6 +39,7 @@ window.Vaadin.Flow.tinymceConnector = {
3339

3440
replaceSelectionContent : function(html) {
3541
this.editor.selection.setContent(html);
42+
syncValue();
3643
},
3744

3845
focus : function() {
@@ -122,17 +129,13 @@ window.Vaadin.Flow.tinymceConnector = {
122129

123130
ed.on('change', function(e) {
124131
if (changeMode === 'timeout') {
125-
const event = new Event("tchange");
126-
event.htmlString = ed.getContent();
127-
c.dispatchEvent(event);
132+
syncValue();
128133
}
129134
});
130135
ed.on('blur', function(e) {
131136
const blurEvent = new Event("tblur");
132137
c.dispatchEvent(blurEvent);
133-
const changeEvent = new Event("tchange");
134-
changeEvent.htmlString = ed.getContent();
135-
c.dispatchEvent(changeEvent);
138+
syncValue();
136139
});
137140
ed.on('focus', function(e) {
138141
const event = new Event("tfocus");
@@ -141,9 +144,7 @@ window.Vaadin.Flow.tinymceConnector = {
141144

142145
ed.on('input', function(e) {
143146
if (changeMode === 'timeout') {
144-
const event = new Event("tchange");
145-
event.htmlString = ed.getContent();
146-
c.dispatchEvent(event);
147+
syncValue();
147148
}
148149
});
149150

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package org.vaadin.tinymce;
22

3-
import java.time.LocalDateTime;
4-
5-
import org.vaadin.tinymce.TinyMce;
6-
73
import com.vaadin.flow.component.button.Button;
4+
import com.vaadin.flow.component.notification.Notification;
85
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
96
import com.vaadin.flow.router.Route;
107

8+
import java.time.LocalDateTime;
9+
1110
@Route
1211
public class ReplaceSelectionContent extends VerticalLayout {
1312

@@ -22,11 +21,21 @@ public ReplaceSelectionContent() {
2221
String now = LocalDateTime.now().toString();
2322

2423
editor.replaceSelectionContent(now);
25-
26-
});
24+
25+
// Note, this is not up to date yet as the replacement happens on the client side
26+
String value = editor.getValue();
27+
});
28+
29+
editor.addValueChangeListener(e -> {
30+
Notification.show("ValueChange event: " + e.getValue());
31+
System.out.println(e.getValue());
32+
});
2733

2834
add(editor, insertCurrentTime);
29-
35+
36+
add(new Button("Show value", e -> {
37+
Notification.show(editor.getValue());
38+
}));
3039

3140
}
3241
}

0 commit comments

Comments
 (0)