Monday, May 21, 2007

Toggling tinyMCE

Downloaded tinyMCE (V.2.0.8) and went straight to the docs to see if or better still how I can initiate the editor instance such that it toggles on and off with the clicking of a button or another event for that matter.

After going through the docs that came with the download, learnt that the editor instance is initiated with the following codes:

mode : "textareas"

And the mode determines the method I have for calling the editor instance. So I looked further to see the different options/modes that I have. According to the docs I have just three options which are:

  • textarea: which converts all textarea elements to editors when the page loads.

  • specific_textareas: Converts all textarea elements with the ‘textarea_trigger’ attribute set to "true".

  • Exact: Converts only explicit elements, these are listed in the elements option. These elements can be any kind for example textareas or divs.

Which, apparently does not include any option for what I want to achieve; a toggle-able text editor instance. After further ransacking through the docs I realized that there was no provision for what I wanted, I decided I was left with one option; which is to tweak the codes to get what I wanted.

So I looked at the initiating code and since it’s just Javascript, I thought if I wrap it in a function and attach the function to an ‘onclick’ event then I will have achieved my aim. I was so wrong. Instead of it working I got myself a blank and frozen page each time I clicked the button.

Since this post is to show how to toggle tinyMCE with a click, and not to lament about my travails, I will save you all the unpleasant details of what I went through trying to tweak the initiation and just go straight to the point.

So how do you do it?

After my futile attempts I decided to ask around for a solution. This is when I found out that there is a fourth 'mode option' apart from the three specified in the docs. This is:

mode : "none"

It is my finding of this mode option and two other methods that solved the problem.

This is how to use the two methods in creating a toggle-able editor:

First method adds the editor instance.

tinyMCE.addMCEControl(element to be toggled, id given to the editor instance);

Where element to be toggled is the element that will be converted to the tinyMCE instance and the other parameter is the id to be given to the editor instance. This parameter is useful because it will be used in getting a handle on the editor when it is to be removed.

Second method removes the editor instance

tinyMCE.removeMCEControl(id of the editor)

Where id of the editor is used in determining the editor instance to remove and it can be returned by this method: tinyMCE.getEditorId(EditorsID) where EditorsID is the second parameter that was specified for the tinyMCE.addMCEControl() method. I.e. it is the id given to the editor.

Having seen the two methods, here is how the final offering will look like:

var tinyMCEmode = false;

function toggle(editorsId)
//check if there is already an instance of the editor
if (tinyMCEmode)
//if yes then remove it
//indicate that editor has been removed
tinyMCEmode = false;
//add the editor instance
tinyMCE.addMCEControl(document.getElementById('geekabyte'), editorsId);
//indicate that editor has been added
tinyMCEmode = true;

Where ‘geekabyte’ is the id given to the html element that will take on the editor instance. The HTML attached to the following script may look like this:

<div id='geekabyte'>
<input type='button' value='toggle' onclick='toggle("newgeekabyte")'/>


Ikezi Kamanu said...

Nice blog, my first comment..

Curious, had you heard of fckeditor WYSIWYG editor before turning to tinymice? (
I ask because fck has been around for quite some time, and It's quite powerful. We (Adobe) are actually making it part of Scropio (CF8), so web devs can add it with a single tag: <cftextarea richtext="true">

dade said...

Thanx ikezi.
Yeah I've heard about fckeditor but i was never motivated to use it. Guess it was due to the fact i thought its too clunky compared to tinyMCE and then again what i needed was a javascript based editor which fckeditor isn't.
Might get around using it one of these days since a lot of 'big boys' are adopting it; oracle inclusive.