how to disable tinymce editor - tinymce

I want to disable tinymce editor using Javascript. Actually, I have two radio buttons: 1) On & 2) Off.
When the user selects the Off radio button, my tinymce editor should be readonly/disabled & when the user selects the On radio button, my tinymce editor should be enabled.
How can I achieve that?
EDITED:- (As it is not working in IE8)
tinyMCE.init({
force_p_newlines : false,
force_br_newlines : false,
forced_root_block : false,
convert_newlines_to_brs: false,
// Not to add br elements.
remove_linebreaks : true,
mode : "textareas",
theme : "advanced",
plugins : "safari",
convert_urls : false,
width : "560",
height : "15",
theme_advanced_buttons1 : "fontselect,fontsizeselect, separator, bold,italic,underline,separator,forecolor,backcolor,justifyleft,justifycenter,justifyright,justifyfull",
theme_advanced_buttons2 : "",
theme_advanced_buttons3 : "",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left", extended_valid_elements : "a[name|href|target|title|onclick],img[class|src| border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name], hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"
});
This code is used to disable:
function tinymce_state(id, disable){
var state = (disable==true)? 'Off' : 'On'
tinymce.get(id).getDoc().designMode = state;
tinymce.get(id).controlManager.get('fontselect').setDisabled(disable);
tinymce.get(id).controlManager.get('fontsizeselect').setDisabled(disable);
tinymce.get(id).controlManager.get('bold').setDisabled(disable);
tinymce.get(id).controlManager.get('italic').setDisabled(disable);
tinymce.get(id).controlManager.get('underline').setDisabled(disable);
tinymce.get(id).controlManager.get('forecolor').setDisabled(disable);
tinymce.get(id).controlManager.get('backcolor').setDisabled(disable);
tinymce.get(id).controlManager.get('justifyleft').setDisabled(disable);
tinymce.get(id).controlManager.get('justifycenter').setDisabled(disable);
tinymce.get(id).controlManager.get('justifyright').setDisabled(disable);
tinymce.get(id).controlManager.get('justifyfull').setDisabled(disable);
}

You may use the following to block input in the editor:
// blockeditor input
tinymce.get('editor_id').getDoc().designMode = 'Off'; // switches editable off
// turn it on again
tinymce.get('editor_id').getDoc().designMode = 'On'; // switches editable on
You still need to find a way to block the tinymce UI. You could deactivate EACH control you have loaded (in the init function) using a line for EACH one of them
// example control bold
tinymce.get('editor_id').controlManager.get('bold').setDisabled(true);
// turn it on again
tinymce.get('editor_id').controlManager.get('bold').setDisabled(false);
EDIT:
You could change the contenteditable property of your rtes iframe body.
Downside will be that you will have to disable the tinymce UI (buttons) seperatly
// disable contenteditable
tinymce.get('editor_id').getBody().setAttribute('contenteditable', 'false');
// enable contenteditable
tinymce.get('editor_id').getBody().setAttribute('contenteditable', 'true');

For some reason the collection of editors has two type of ID, the numeric ID (0,1, ... n) and an alpha ID (Testing1, testing2, ... xyx)
the commands in the code snippet only work with the aplha-based ID e.g. "Testing1"
I have twelve tinyMCE version 4.1.5 editors in my project and can disable all of them with this code:
for (editor_id in tinyMCE.editors) {
if (editor_id.length > 2) { //there are twelve editors in my project so ignore two-digit IDs
tinyMCE.editors[editor_id].getBody().setAttribute('readonly', '1');
tinymce.EditorManager.execCommand('mceRemoveControl', true, editor_id);
tinymce.EditorManager.execCommand('mceRemoveEditor', true, editor_id);
tinymce.EditorManager.execCommand('mceAddControl', true, editor_id);
tinymce.EditorManager.execCommand('mceAddEditor', true, editor_id);
}
}
This site helped me figure it out: http://jeromejaglale.com/doc/javascript/tinymce_jquery_ajax_form

To disable the editor use:
tinymce.activeEditor.mode.set('readonly');
To enable the editor use:
tinymce.activeEditor.mode.set('design');
Tested on version 5.x

You can cover with a white div opacity .7 and higher z-index.

You can use in 3.x the option
editor_deselector : "no_mce",
to disabled (so give the textarea the css class no_mce). You can toggle the CSS Class with jQuery for example.
In 4.x you can use the option
selector:'textarea.not(.no_mce)'
Hope that helps.
(Oviously you can change the CSS Class to whatever you want)

For old 3 ver you can use:
tinyMCE.getInstanceById(tinyMCE.activeEditor.id).getBody().classList.add("mceNonEditable");

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

Perform action after TinyMCE rendered in DOM

I'm using TinyMCE 4 and setting it up as follows:
tinyMCE.init({
mode : "specific_textareas",
editor_selector : "basicTinyMCE",
theme : "modern",
readonly : false,
...});
I want to call a function after it has been rendered in the DOM.
I came across this and tried:
tinyMCE.init({
mode : "specific_textareas",
editor_selector : "basicTinyMCE",
theme : "modern",
readonly : false,
setup : function(ed) {
ed.onPostRender.add(function(ed,cm) {
console.log('After render: ' + ed.id);
});
}
});
I get the following error:
SCRIPT5007: Unable to get property 'add' of undefined or null reference
Any ideas if this is the correct way to achieve what I want?
And if so, why is the error appearing?
You've got two options:
Use configuration init_instance_callback http://www.tinymce.com/wiki.php/Configuration:init_instance_callback
Use new way to add callbacks
ed.on('postRender', function(e) {
console.log('postRender');
});
I found the "Dirty" event was what I wanted, only triggered after the user makes a change to the contents of the editor
https://www.tiny.cloud/docs/advanced/events/

Tinymce - reloading data with html tags

I am trying to reload data saved by tinymce. When i do so all of the html tags are then viewable inside the control.
How do you load html back into the control the same as it was previously saved(without the
tags viewable)?
Here is my initialization:
$('textarea.tinymce').tinymce({
// Location of TinyMCE script
script_url : '/Scripts/tinymce/tiny_mce.js',
encoding: "xml",
// General options
theme : "advanced",
plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
// Theme options
theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect,|,forecolor,backcolor",
theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,undo,redo,|,image,",
theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,ltr,rtl,|,fullscreen",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : false,
// Example content CSS (should be your site CSS)
//content_css : "/Content/content.css",
// Drop lists for link/image/media/template dialogs
template_external_list_url: "/Scripts/tinymce/lists/template_list.js",
external_link_list_url: "/Scripts/tinymce/lists/link_list.js",
external_image_list_url: "/Scripts/tinymce/lists/image_list.js",
media_external_list_url: "/Scripts/tinymce/lists/media_list.js"
});//end tinymce
and my textarea:
<%: Html.TextArea("Blog", Model.Blog, new { #class = "tinymce", rows = "25", cols = "40" })%>
Thank you for your time.
Billy
Using MVC 2 -
The resolution to this issue for me was to store my data decoded using HttpUtility.HtmlDecode.
Then when pulling it back up and assigning to a display(string) property of my model i encoded the string using HttpUtility.HtmlEncode.
Finally on the view page i decoded that same property for display using HttpUtility.HtmlDecode once more.
Hope this helps someone else with a similar issue.
See http://www.tinymce.com/wiki.php/Configuration:encoding
Seems like you need to set encoding: 'xml' on your configuration.

Adjusting the width for tinymce buttons on header?

i have used below code snippet to show the different features like forecolor,backcolor,bulllist etc on tinymce
$(function() {
appendTinyMCE();
function appendTinyMCE(){
tinyMCE.init({
// General options
mode : "textareas",
theme : "advanced",
plugins : "preview",
// Theme options
theme_advanced_buttons1 : "forecolor,backcolor,|,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,|,formatselect,fontselect,fontsizeselect,sub,sup,|,bold,italic,underline,strikethrough",
theme_advanced_buttons2 : "",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true
});}
});
But when i see the tinymce on browser, around 25 percent of tinymce editor header width is taken by just two buttons i.e forecolor,backcolor
which looks very odd , though i have mentioned theme_advanced_toolbar_align : "left".
here is the image.
Can we adjust the tinymce buttons so
that each button takes uniform width?
You can adjust the css of the tinymce toolbar using the tinymce init configuration paramter editor_css.

TinyMCE is removing elements like <title>, <script> from the document loaded onto it

A similar problem was reported here:
http://www.tinymce.com/forum/viewtopic.php?id=78
My TinyMCE initialization code is as follows:
tinyMCE.init({
// General options
mode : "specific_textareas",
editor_selector : "mceEditor",
width : "94%%",
height : "449px",
theme :"advanced",
plugins : "autolink,lists,pagebreak,style,layer,table,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups",
// Theme options
theme_advanced_buttons1 : "bold,italic,underline,fontselect,fontsizeselect,forecolor,backcolor,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,blockquote,|,link,unlink,|,undo,redo",
theme_advanced_buttons2 : "",
theme_advanced_buttons3 : "",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_resizing : false,
//content_css : "css/word.css",
convert_urls : false,
extended_valid_elements: 'script[src|title|type],title[dir<ltr?rtl|lang]',
});
By default TinyMCE only allows editing of the body part of a HTML page, as this is the most common use case. If you want to use it to edit a full HTML page I'd suggest using the fullpage plugin: http://www.tinymce.com/wiki.php/Plugin:fullpage
Add plugin to TinyMCE plugin option list example: plugins : "fullpage".
You had an error in your tinymce config (height and width need to get set as a value without 'px'). Check this fiddle out. The element stay there and do not get stripped out (you may verify this using the html/code button).