Applying tinymce to dynamically created textarea works exactly once - tinymce

I have a page with three horizontal tabs. Clicking on a tab dynamically populates a div with a form. I am using BackboneJS to manage views' rendering (not sure if relevant). When I call render() on the view, after I populate the element's html, I make a call to apply tinyMCE to a text area:
$('#text_' + this.model.id, this.$el).tinymce({
script_url : '/lib/tinymce/tinymce.min.js',
theme : "modern",
content_css: "/css/bootstrap.min.css",
menubar: false,
toolbar: "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | " +
"bullist numlist outdent indent | link image | forecolor backcolor emoticons",
setup: function(editor) {
editor.on('change', function(e) {
var change = {};
change["content"] = e.level.content;
self.model.set(change);
});
}
});
This works perfectly fine on the first go. When view is removed, there is an event called that applies .tinymce().remove(); to the field to properly remove editor before displaying the next view. The next view renders - it's the same view but different model. On this, and all subsequent calls, tinyMCE hides the textarea, and doesn't show the editor. I know my views work - commenting out .tinymce() call in render() makes everything work as intended. TinyMCE stumbles somewhere. Any insight will be appreciated.

I'm not 100% clear on what .tinymce().remove(); is doing but have you tried this:
tinymce.triggerSave();
tinymce.execCommand('mceRemoveEditor', true, tinymce.activeEditor.id);
and when you switch to the next view try re-attaching the editor via:
tinymce.execCommand('mceAddEditor', false, selector);
If that doesn't work or doesn't apply to what you're doing. Have you considered having more than 1 tinyMCE editor? It sounds like you have a set number of tabs, so you could create a separate tinyMCE for each one and simply hide/show the editor on each as needed with:
tinymce.execCommand('mceToggleEditor', false, selector);
This method is considered much faster than adding/removing the editor.

Related

Configure the user's default choice on tinyMCE toolbar

I am using v5 of TinyMCE. By default, the style selected is 'Paragraph', as shown in this image :
[tinyMCE toolbar, as the user sees before he mades any format configuration]
But I know my users will all prefer to use 'Div' style. So I would like 'Div' to be selected by default. The toolbar should therefore appear like in this image :
[tinyMCE toolbar, as I want it to be configured by default]
Is it possible ?
I haven't find my answer in tinyMCE documentation.
Same question if you want for instead "bold" button to be selected by default, etc.
Thank you !
To replace the default <p> blocks with <div>, use forced_root_block: https://www.tiny.cloud/docs-3x/reference/Configuration3x/Configuration3x#forced_root_block/
tinymce.init({
// ...
forced_root_block : 'div'
});
To select the bold button by default, you could use execCommand: https://www.tiny.cloud/docs/api/tinymce/tinymce.editor/#execcommand
tinymce.init({
// ...
setup: function(editor) {
editor.on('init', function() {
this.execCommand('Bold');
});
}
});
Example fiddle combining both: https://fiddle.tiny.cloud/YShaab/1

How to disable and enable TINYMCE with a button and Jquery

I have a site, that when it loads, it initializes the tinyMCE, then, when you click a button, it opens a modal and while in that process, it brings some HTML into the modal.
then inside that modal, there is another button that should let you edit the content of the modal. and inside the DIV that holds the loaded HTML, there are divs with a class to make them editable by the tinyMCE.
the thing is, when you build the content of the modal and you save. everything is fine and it stores it with the format, because when you refresh the site, and load the content, everything is still with the correct format (bold,colors,etc). but then when you want to update the content, and you click the button to edit, I have an option to initialize again the tinyMCE and the contents editable zones loses their format.
now I suppose that the problem is when I initialize again the tinyMCE that causes the problem but how can I enable and disable the edition inside the modal when I click the button to make the edits?
I saw that there is an .on() and .off() functions, but I donĀ“t understand well how to use them, is not clear enough in the documentation.
this is a sample of what I have.
<script>
var pathBotones = "<?=$pathBotones?>";
var modalEditor = {
mode: 'exact',
selector: '.zonaEditable',
menubar: false,
inline: true,
plugins: [
'link',
'autolink',
'lists',
'save'
],
toolbar: [
'undo redo | bold italic underline | fontselect fontsizeselect | link',
'forecolor backcolor | alignleft aligncenter alignright alignfull | numlist bullist'
],
valid_elements: 'strong,em,span[style],a[href]',
valid_styles: {
'*': 'font-size,font-family,color,text-decoration,text-align'
},
powerpaste_word_import: 'clean',
powerpaste_html_import: 'clean',
content_css: [
'//fonts.googleapis.com/css?family=Lato:300,300i,400,400i'
]
};
tinymce.init(modalEditor);
$(".edit-content").click(function(e) {
e.preventDefault();
//console.log($('.editOptions').css('display'))
if ($('.editOptions').css('display') === 'none') {
//this is supossed to enable the edition
tinymce.init(modalEditor);
} else {
console.log('NOOO se ve')
}
//this is supossed to disable the edition
$(".editOptions").slideToggle();
$(".deleteRowInfoBox").fadeToggle();;
});
</script>
You can use TinyMCE's APIs to set the mode on the editor to readonly.
https://www.tiny.cloud/docs/api/tinymce/tinymce.editormode/#set
The code would look something like:
tinymce.activeEditor.setMode('readonly');
...or...
tinymce.get('theEditor').setMode('readonly');

TinyMCE in impresspages plugin trouble II

I replaced ipTinyMceConfig() already. It's OK. In the plugin field editor to the richedit is displaying the menus and so on. However when I click in source code menu the source code editor open but disabled for edition. No typing is possible, nothing... Any Idea to solve it? My code replace is like this:
var originalMce = ipTinyMceConfig;
var ipTinyMceConfig = function(){
var customized = originalMce();
customized.plugins = [
"advlist autolink lists link image charmap print preview hr anchor pagebreak",
"searchreplace wordcount visualblocks visualchars code fullscreen",
"insertdatetime media nonbreaking save table contextmenu directionality",
"emoticons template paste textcolor"
];
console.log('Entered tiny newsletter - New');
customized.toolbar1 = "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | link image";
customized.toolbar2 = "print preview media | forecolor backcolor emoticons | bullist numlist outdent indent";
customized.image_advtab = true;
customized.menubar = true;
customized.style_formats_merge = true;
return customized;
}
So thanks!
Haven't tried your code. Are you sure it is disabled? Maybe there is just some kind of CSS overlay that's stands in your way?

TinyMCE issue: textarea not updated even with TriggerSave()

I'm using TinyMCE in a form where I use ajax to submit the form. I've been struggling for the last 4 hours to figure out what is going wrong but I have reached a dead end.
I initialize TinyMCE with the following code:
tinymce.init({
selector: "textarea.editme",
plugins: ["advlist autolink autosave link image lists charmap print preview hr anchor
pagebreak spellchecker","searchreplace wordcount visualblocks visualchars code
fullscreen insertdatetime media nonbreaking","table contextmenu directionality
emoticons template textcolor paste fullpage textcolor imageplugin"],
toolbar1: "image_list undo redo | bold italic underline strikethrough | alignleft
aligncenter alignright alignjustify | fontselect fontsizeselect forecolor backcolor",
toolbar2: "cut copy paste | searchreplace | bullist numlist | outdent indent
blockquote | preview | table",
menubar: false,});
I don't have problems loading it so the code should be all right but I copied it here just in case.
I have a textarea with the right class "editme" to display TinyMCE. This text area is embedded in a form ()
I submit the form with the following code:
$("#submitform").submit(function(e)
{
tinyMCE.triggerSave(true,true);
var postData=$(this).serializeArray();
var url = 'php/forms_post.php';
$.ajax(
{
url : url,
type: "POST",
data : postData,
success:function(data, textStatus, jqXHR)
{
// DISPLAY SUCCESS
},
error: function(jqXHR, textStatus, errorThrown)
{
// DISPLAY FAILURE
}
});
e.preventDefault();
e.unbind();
});
The code works fine, except for one thing. If I write "aaaaaa" in the textarea the first time, it gets submitted correctly and everything is fine. But if I then change it to "bbbbbb", the data that is submitted is still "aaaaaa". It looks like the triggersave keeps the data in memory and refuses any changes. I already restarted the browser, the server, cleaned cache, cleaned cookies,... but still the problem is there.
If I remove TinyMCE and keep a standard textarea, I have no problem.
Do you have any idea of what's going wrong?
Thanks a lot
Laurent

tiny mce can't be inited when load js dynamically

i am having trouble with tinyMCE, when i put <script type="text/javascript" src="/scripts/tiny_mce/tiny_mce.js"> to <head>, and put init code before the <textarea class="tinyMceEditor">, it works fine.
the init code is like this:
tinyMCE.init({
mode : "specific_textareas",
editor_selector : "tinyMceEditor",
plugins : "inlinepopups,advlink",
convert_urls : false,
theme : "advanced",
theme_advanced_buttons1 : "link,unlink",
width: "602",
height: "175",
theme_advanced_statusbar_location : "none"});
But now, i want to defer the loading of tiny_mce.js, when user click a button on the first time, the tiny_mce.js will be loaded, and then append the <textarea class="tinyMceEditor"> to <body>, then do the init work with the previous code, but this time, it won't init the tinyMCE editor, it only shows the <textarea class="tinyMceEditor">
googled, but find nothing related to this, anyone has met this problem?
Any suggestion will be appreciated.
i looked into chrome web developer tools and found that if i dynamically load the tinymce.js, other js needed like en.js, editor_template.js, editor_plugin.js etc won't be loaded. even when i add these js files to dynamically loading, the tinymce still can't be inited.
thank you for your help, i watched in firebug, and i do get the tinymce.js loaded before append <textarea to <body>, then i append the <textarea>, and do the tinymce init(), i am using LazyLoad(jQuery plugin) to dynamically load the js files.
here is what i did
if(typeof TinyMCE == "undefined"){
//dynamically load the tinymce.js
LazyLoad(['tinymce.js'],function(){
//callback function, called after tinymce is loaded
$('body').append('<textarea class="TinyMceEditor"/>');
tinyMCE.init({init settings});
});
}
if i don't load tinymce.js dynamically, just put a <script> tag in <head>, the tinyMCE can be inited , but when i load tinymce.js dynamically, it doesn't work. Any idea with this?
after a day's work, finally found the solution, just put
window.tinymce.dom.Event.domLoaded = true;
before
tinymce.init();
then the tinymce can be inited correctly.
I resolved this issue by creating a separate coffee script file. Then I declared below function with window scope to access in views.
window.initialize_tiny_mce = () ->
if (typeof tinymce != 'undefined' && tinymce != null)
tinymce.remove();
tinymce.init
height: '180'
menubar: false
statusbar: false
cleanup: true
selector: '.new-tinymce-printable-html'
plugins: [ 'autolink link image code lists advlist' ]
toolbar: 'styleselect | bold underline italic | bullist numlist outdent indent | link image | code'
browser_spellcheck: true
setup: (editor) ->
editor.on 'keyup', ->
tinymce.triggerSave()
editor.on 'change', ->
console.log editor.getContent()
return
return
Then in view partial, I called this function:
:coffeescript
initialize_tiny_mce()
Now dynamically created element is assigned a tinymce editor.