May 04

Too many CKeditor fields in jquery tabs makes IE freeze.



IE seems to freeze or take a long time to render a page if there are more than 2 or 3 CKeditor textareas in the page. This problem is specific to IE as Firefox, Chrome and Safari do not seem to have this problem.

The basic reason for this is that IE gets stuck in instantiating the ckeditor javascript object; maybe something in the ckeditor script creates this problem.

We had this particular problem we used a JQuery tab plugin with about 8 or more tabs and each tab had a textarea field wrapped in a ckeditor object.

The final solution that was used is simple: Dont instantiate all the ckeditor objects at page load time. Instantiate for each textarea when required. In other words, let the ckeditor object get created at page load for the default tab. For all the other tabs, convert the textarea in the target tab into a ckeditor when the tab is clicked. This was a simple solution which stopped IE from freezing and also sped up the page load time.

The code snippets are given below. It is assumed that jquery libraries and the ckeditor scripts are included in the page.

The HTML part is given below. The fields are using PHP code to obtain values, but that part can be ignored. Note that the default Homepage tab is already set to class=ckeditor so it becomes a ckeditor object at page load. The other textareas are set to a dummy class called “xckeditor”:

	<tr valign="top">
				<td colspan="2">
					<div id="tabs">
					<ul>
						<li><a href="#tabs-1">Homepage</a></li>
						<li><a href="#tabs-2">About Us</a></li>
						<li><a href="#tabs-3">Privacy</a></li>
						<li><a href="#tabs-4">Terms</a></li>
						<li><a href="#tabs-5">Advertise</a></li>
						<li><a href="#tabs-6">Partner</a></li>
						<li><a href="#tabs-7">Competition</a></li>
						<li><a href="#tabs-8">Bookmark</a></li>
						<li><a href="#tabs-9">Daily Deals Newsletter</a></li>
						<li><a href="#tabs-10">FAQ</a></li>
					</ul>

					<div id="tabs-1">
						<div class="editor_adjust">
							<textarea name="txtHomepageContent" id="txtHomepageContent"  rows="10" cols="40" class="ckeditor"><?php echo($sitecountry->getValue("homepage_content"));?>

The javascript is given below. Note that there is no code to make a textarea into a plain textarea by removing the ckeditor class. This was not required since once a textarea was made into a ckeditor object it was not affecting the page speed:

jQuery(document).ready(function($) {
	$("#tabs").tabs({
		event: 'click'
	}),
	$( "#tabs" ).tabs({
	   select: function(event, ui) {
		   var i = ui.index;

		   if (i == 1) {
			   $('#txtAboutUsContent').removeClass("xckeditor");
			   $('#txtAboutUsContent').ckeditor();
		   }
		   else if (i == 2) {
			   $('#txtPrivacyContent').removeClass("xckeditor");
			   $('#txtPrivacyContent').ckeditor();
		   }
		   else if (i == 3) {
			   $('#txtTermsContent').removeClass("xckeditor");
			   $('#txtTermsContent').ckeditor();
		   }
		   else if (i == 4) {
			   $('#txtAdvertiseContent').removeClass("xckeditor");
			   $('#txtAdvertiseContent').ckeditor();
		   }
		   else if (i == 5) {
			   $('#txtPartnerContent').removeClass("xckeditor");
			   $('#txtPartnerContent').ckeditor();
		   }
		   else if (i == 6) {
			   $('#txtCompetitionContent').removeClass("xckeditor");
			   $('#txtCompetitionContent').ckeditor();
		   }
		   else if (i == 7) {
			   $('#txtBookmarkContent').removeClass("xckeditor");
			   $('#txtBookmarkContent').ckeditor();
		   }
		   else if (i == 8) {
			   $('#txtNewsletterContent').removeClass("xckeditor");
			   $('#txtNewsletterContent').ckeditor();
		   }
		   else if (i == 9) {
			   $('#txtFaqContent').removeClass("xckeditor");
			   $('#txtFaqContent').ckeditor();
		   }
	   }
	});	

});

A good enhancement would have been to place a check if a textarea was already assigned the ckeditor class before running the

Mar 17

Alter/change the width, height, skin and color of CKEditor



Well to change the color or height or width or even change the whole look (skin) of CKEditor , we just need to keep in mind 1 file, and that is the ” config.js ” file in the ckeditor folder.

Any changes made to the main config.js file will reflect on all the pages where the CKEditor has been used.
But if you don’t want the changes to show on all the pages then you only need to add some code in the respective pages where you want the changes to happen.

This code below will give you a nice idea about how to change the properties of your CKEditor using the config.js file
( code inside the config.js file is given below )


CKEDITOR.editorConfig = function( config )
{

// config.language = 'fr';                                                         
// config.customConfig = '/ckeditor/config.js';     
// config.toolbar = 'Basic';
// config.uiColor = '#dfe8f6';

config.height = 200;
config.width = 250;
config.skin = 'v2';      //config.skin = 'office2003'; 
                             //config.skin = 'kama';

CKEDITOR.config.toolbar_Basic = [
['Source','-','Templates'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
['Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
'/',
['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
['Link','Unlink','Anchor'],
['Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
'/',
['Styles','Format','Font','FontSize'],
['TextColor','BGColor'],
['Maximize', 'ShowBlocks','-','About']
]
config.toolbar = 'Basic';
config.extraPlugins = "insert_image";
};

Below is the preview of the toolbar after making the above changes in the config file

After changes made in the config.js

Now let’s try changing only the color of the toolbar, for that you just have to write something like this in the config.js


// config.language = 'fr';
// config.customConfig = '/ckeditor/config.js';
   config.toolbar = 'Basic'; 
   config.uiColor = '#800040'; 
// config.height = 200;
// config.width = 250;
// config.skin = 'v2'; 

Now the toolbar will look something like this

Changed the color of the toolbar from the config.js

But if you don’t want the changes to be shown in every single page where you have used CKEditor then you have to write a few lines of code in the individual pages where you want to change the properties of your CKEditor.

In this case, I want the changes to show up only on one page called “test-editor.html”.

Case 1: changing the skin only

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 strict//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Truelogic.org::TestEditor::CKEditor example &lt;/title&gt;

&lt;script type="text/javascript" src="ckeditor/ckeditor.js"&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;

  &lt;form name="" action="" method=""&gt;
   &lt;textarea id="txtDescrip" name="txtDescrip" cols="20" rows="5" class="ckeditor"&gt;&lt;/textarea&gt;

  &lt;script type="text/javascript"&gt;
    var editor = CKEDITOR.replace( 'txtDescrip',
    {
      toolbar : [
            ['Source','-','Templates'],
            ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
            ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
            ['Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
            '/',
            ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
            ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
            ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
            ['Link','Unlink','Anchor'],
            ['Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
            '/',
            ['Styles','Format','Font','FontSize'],
            ['TextColor','BGColor'],
            ['Maximize', 'ShowBlocks','-','About'],['insert_image']
          ],
      skin : 'office2003',  // ALTERED THE SKIN HERE 
    });
  &lt;/script&gt;

  &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Case 2: changing the height, width and the color of the toolbar

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 strict//EN"&gt; 
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Truelogic.org::TestEditor::CKEditor example &lt;/title&gt;

&lt;script type="text/javascript" src="ckeditor/ckeditor.js"&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;

  &lt;form name="" action="" method=""&gt;
   &lt;textarea id="txtDescrip" name="txtDescrip" cols="20" rows="5" class="ckeditor"&gt;&lt;/textarea&gt;

  &lt;script type="text/javascript"&gt;
    var editor = CKEDITOR.replace( 'txtDescrip',
    {
      toolbar : [
            ['Source','-','Templates'],
            ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
            ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
            ['Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
            '/',
            ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
            ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
            ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
            ['Link','Unlink','Anchor'],
            ['Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
            '/',
            ['Styles','Format','Font','FontSize'],
            ['TextColor','BGColor'],
            ['Maximize', 'ShowBlocks','-','About'],['insert_image']
          ],
      uiColor: 'red', 
      height: 200,
      width: 700,
    });
  &lt;/script&gt;

  &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Mar 17

How to create a new toolbar plugin in CKEditor



Hey there, here is something which I learned the hard way while I was trying out my hands on customizing CKEdior for a website, in which I needed to add a new button which enabled the admin to add a particular kind of text or HTML tag in the textarea.

So therefore I thought I would share my experience through a simple step by step process of how I managed to achieve the customized feature of adding a new plugin (new button) in the CKEditor toolbar.

Well I just made a button which when clicked will add an image (in this case Truelogic.org Logo) in the textarea content.
Step 1: The basic directory structure of the CKEditor 3.3

( DO KEEP IN MIND THE VERSION OF THE CKEditor BEING USED HERE, DIFFERENT VERSIONS MAY HAVE DIFFERENT FOLDER/DIRECTORY STRUCTURE )

  ckeditor/
        config.js
        plugins/
             insert_image/
                    insert_image_button.png
                    plugin.js

Step 2: Now, as you may have an idea after seeing the folder structure that you have to create a new folder called “insert_image”, inside the plugins folder.

( IMPORTANT: Folder name is very important because the config.js recognizes the function of the plugin based on the folder name given )

Now the code which you have to paste after creating the “plugin.js” inside the “insert_image” folder is given below , this plugin code allows you to insert a predefined image inside the textarea (in this case Truelogic.org Logo)

(function(){
     //The Code is written here, which will be executed when the button is clicked
     var a= {
     exec:function(editor){
             CKEDITOR.instances.txtDescrip.insertHtml("&lt;img src='http://truelogic.org/simple/images/true.png' border=0 /&gt;");  
     //CKEDITOR.instances.ID OF THE TEXTAREA.insertHtml("IMAGE URL");
     //above line inserts an IMG tag with the predefined url of the image which points to the truelogic logo
              }
},

//The actual button is defined/created here and the associated code is linked with it
b='insert_image';
CKEDITOR.plugins.add(b,{
    init:function(editor){
    editor.addCommand(b,a); // associating the executable code with the button
    editor.ui.addButton('insert_image',{
                                     label:'Insert image',
                                     icon: this.path + 'insert_image_button.png',
                          //defining the path of the icon image which will be displayed on the toolbar for this button
                          //easiest would be to have the icon image in the same folder as the plugin.js

                                     command:b
                             });
   }
});
})();

Step 3:

Here is the “insert_image_button.png” image to make things easier for you guys

This image has to be put inside the “plugins/insert_image/” folder, next to the plugin.js

Step 4:
Here is the code which is going in the config.js:

CKEDITOR.editorConfig = function( config )
{
     CKEDITOR.config.toolbar_Basic = [
                  ['Source','-','Templates'],
                  ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
                  ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
                  ['Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
                  '/',
                  ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
                  ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
                  ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
                  ['Link','Unlink','Anchor'],
                  ['Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
                  '/',
                  ['Styles','Format','Font','FontSize'],
                  ['TextColor','BGColor'],
                  ['Maximize', 'ShowBlocks','-','About'],
                  ['insert_image']                                //Button added to the toolbar
                ]
 config.toolbar = 'Basic';
 config.extraPlugins = "insert_image”;   //registering the plugin
};

Tips:

If you don’t want to alter the default setup of the CKEditor toolbar or if you want to show or hide specific plugins on particular pages or just one page, you can do it by writing a few lines of code in the page where you want to make this happen.

For you to understand more clearly, let me give you an example, say I only want around 10 buttons to be displayed in the toolbar for a specific page called “test-editor.html”

&lt;script type="text/javascript"&gt;

var editor = CKEDITOR.replace( 'txtDescrip',    //var editor = CKEDITOR.replace( 'ID OF THE TEXTAREA', 
{
                toolbar : [
                         ['Source','-','Templates'],
                         ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
                         '/',
                         ['insert_image']
                            ]
 });
&lt;/script&gt;

Using the above code and pasting it in the page where you want to change the look of the toolbar (hide or display a few of the buttons) will allow the page to only show those buttons or plugins which are mentioned in the code

Just to make it even more easier , here is the full code of the “test-editor.html”

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 strict//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Truelogic.org::TestEditor::CKEditor example &lt;/title&gt;

&lt;script type="text/javascript" src="ckeditor/ckeditor.js"&gt;&lt;/script&gt;
 // do keep in mind the include path of the CKEditor folder on the web server or the local server

&lt;/head&gt;

&lt;body&gt;

 &lt;form name="" action="" method=""&gt;
 &lt;textarea id="txtDescrip" cols="20" rows="5"&gt; &lt;/textarea&gt;

&lt;script type="text/javascript"&gt;

 var editor = CKEDITOR.replace( 'txtDescrip',   //var editor = CKEDITOR.replace( 'ID OF THE TEXTAREA', 
 {
 toolbar : [
 ['Source','-','Templates'],
 ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
 ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
 ['Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton','HiddenField'],
 '/',
 ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
 ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
 ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
 ['Link','Unlink','Anchor'],
 ['Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
 '/',
 ['Styles','Format','Font','FontSize'],
 ['TextColor','BGColor'],
 ['Maximize', 'ShowBlocks','-','About'],['insert_image'] ]
 });
&lt;/script&gt;

&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Small Hints:

‘/’,

- this allows you to add a line break in the middle of the toolbar so that whatever plugin is written after this is displayed in the next line of the toolbar

‘-’

- this allows you to add a small gap between the buttons.
Like for example: the gap between ‘strike-through’ button and the ‘SubScript’ button

Preview of the CKEditor (default) Before adding the new plugin

CKEditor - default toolbar

Preview of the CKEditor (customized) After adding the new plugin

CKEditor - customized toolbar with new plugin

Preview of the textarea when the new plugin button has been clicked

CKEditor - when insert_image button has been clicked