I have a card-making web project. I have a tinyMCE textfield and a visual of the card. Upon changing the content of the tinyMCE editor I want to update the visual of the card to reflect the new text/changes.
TinyMCE comes with an IsDirty method:
if (tinyMCE.activeEditor.isDirty())
alert("You must save your contents.");
What I don't understand is WHERE I would place this if statement to regularly check for it. I understand that JS is event driven and so it needs to be "called", do I call it every keypress?
Is TinyMCE free? Yes. The TinyMCE core editor is free to use for commercial and noncommercial purposes.
Use tinymce. remove() method to remove TinyMCE editor from the HTML element and again call tinymce. init() on the selector to reinitialize.
TinyMCE is an online rich-text editor released as open-source software under the MIT License. It has the ability to convert HTML text area fields or other HTML elements to editor instances. TinyMCE is designed to easily integrate with JavaScript libraries such as React, Vue.
You can do this using the getContent() API method. Let's say you have initialized the editor on a textarea with id=”myTextarea”. This will return the content in the editor marked up as HTML.
You could add a global timeout function that every second or so (the interval is up to you) checks:
function updateCardIfDirty() {
if (tinyMCE.isDirty()) {
// rerender the card
}
}
setInterval(updateCardIfDirty, 2000); // check every 2 seconds.
A cleaner solution might be to check every time they make a change in the tinyMCE editor. This can be made possible by the onChange()
event tinyMCE provides, as follows:
tinyMCE.init({
...
setup : function(ed) {
ed.onChange.add(function(ed, l) {
// rerender the card
});
}
});
The downside of the first approach is that it will execute every 2 seconds, even if they dont edit the card for an hour. The downside of the second approach is that if they perform 10 edits in 1 second, it will rerender the card 10 times in that second.
So finally, let's try a third approach which gets the best of both worlds, and loses both disadvantages we mentioned:
tinyMCE.init({
...
setup : function(ed) {
ed.onChange.add(function(ed, l) {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(function(){ timeout=null; rerenderCard(); }, 1000);
});
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With