YUI Library Examples: Rich Text Editor (beta): Plain Text Switcher

Rich Text Editor (beta): Plain Text Switcher

This example demonstrates how to toggle from a plain text field to the Rich Text Editor with a simple button click.


Setting up the Editors HTML

Setting up the Editor's HTML is done by creating a textarea control on the page.

1<button type="button" id="toggleEditor">Toggle Editor</button> 
2<form method="post" action="#" id="form1"
3<textarea id="editor" name="editor" rows="20" cols="75"
4This is some more test text.<br>This is some more test text.<br>This is some more test text.<br> 
5This is some more test text.<br>This is some more test text.<br>This is some more test text. 
6</textarea> 
7</form> 
view plain | print | ?

Setting up the Editors Javascript

Once the textarea is on the page, then initialize the Editor like this:

1(function() { 
2    //Setup some private variables 
3    var Dom = YAHOO.util.Dom, 
4        Event = YAHOO.util.Event; 
5 
6        //The Editor config 
7        var myConfig = { 
8            height: '300px'
9            width: '530px'
10            animate: true
11            dompath: true
12            focusAtStart: true 
13        }; 
14 
15    //Now let's load the Editor.. 
16    var myEditor = new YAHOO.widget.Editor('editor', myConfig); 
17    myEditor.render(); 
18})(); 
view plain | print | ?

Now handle the Toggle

Now we will create a Button control and attach a click event to it.

From the click event we will determine the state of the editor (either "on" or "off"). Then we will choose to either hide or show it,

Note: It is not recommended to set the editor to display: none. This causes certain browsers to lose the editor, you should use visibility: hidden or top, left (to move it out of the viewable area).

Before showing or hiding the Editor, we need to clean up the HTML we are using.

Switching from the Editor to the textarea, we need to strip the HTML from our output and replace all <br>'s with line breaks. This code snippet will handle this for our example. Your implementation may need a stronger approach.

Note: You "could" add a button to the toolbar and have it execute the toggle. Then remove these few lines from the example and you will have a source editor.

1//From the Editor to the textarea 
2var stripHTML = /<\S[^><]*>/g; 
3myEditor.get('textarea').value = myEditor.get('textarea').value.replace(/<br>/gi, '\n').replace(stripHTML, ''); 
4 
5//From the textarea to the Editor 
6myEditor.setEditorHTML(myEditor.get('textarea').value.replace(/\n/g, '<br>')); 
view plain | print | ?

Going from the Editor to the textarea

First we must call myEditor.saveHTML(). This method will clean up the HTML in the Editor and return it to the textarea.

Once it is in the textarea, we will process it to remove all of the HTML and replace the <br>'s with line breaks.

Now using YAHOO.util.Dom we will set top, left and position: absolute (this will keep the Editor from taking up page space) on the Editor elements containers firstChild (which is the container that holds the Rich Text Editor). Then we will set the textarea to hidden.

1myEditor.saveHTML(); 
2var stripHTML = /<\S[^><]*>/g; 
3myEditor.get('textarea').value = myEditor.get('textarea').value.replace(/<br>/gi, '\n').replace(stripHTML, ''); 
4Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'position''absolute'); 
5Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'top''-9999px'); 
6Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'left''-9999px'); 
7myEditor.get('element_cont').removeClass('yui-editor-container'); 
8Dom.setStyle(myEditor.get('element'), 'visibility''visible'); 
9Dom.setStyle(myEditor.get('element'), 'top'''); 
10Dom.setStyle(myEditor.get('element'), 'left'''); 
11Dom.setStyle(myEditor.get('element'), 'position''static'); 
view plain | print | ?

Going from the textarea to the Editor

Using YAHOO.util.Dom we will set top: 0, left: 0 and position: static (to put the Editor back in the page) on the Editor elements containers firstChild (which is the container that holds the Rich Text Editor). Then we will set the textarea to visible.

Then we call the Editor method _setDesignMode('on') to re-enable designMode since it may have been lost with the visibility change.

Now we call the Editor method setEditorHTML() passing it the value of the textarea with the line breaks all converted back to <br>'s.

1Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'position''static'); 
2Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'top''0'); 
3Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'left''0'); 
4Dom.setStyle(myEditor.get('textarea'), 'display''none'); 
5myEditor._setDesignMode('on'); 
6myEditor.setEditorHTML(myEditor.get('textarea').value.replace(/\n/g, '<br>')); 
7Dom.setStyle(myEditor.get('element'), 'visibility''hidden'); 
8Dom.setStyle(myEditor.get('element'), 'top''-9999px'); 
9Dom.setStyle(myEditor.get('element'), 'left''-9999px'); 
10Dom.setStyle(myEditor.get('element'), 'position''absolute'); 
11myEditor.get('element_cont').addClass('yui-editor-container'); 
view plain | print | ?

Full Example Javascript Source

1(function() { 
2    var Dom = YAHOO.util.Dom, 
3        Event = YAHOO.util.Event; 
4     
5    YAHOO.log('Create Button Control (#toggleEditor)''info''example'); 
6    var _button = new YAHOO.widget.Button('toggleEditor'); 
7    _button.addClass('toggleEditor'); 
8 
9    var myConfig = { 
10        height: '300px'
11        width: '530px'
12        animate: true
13        dompath: true
14        focusAtStart: true 
15    }; 
16 
17    var state = 'on'
18    YAHOO.log('Set state to on..''info''example'); 
19 
20    YAHOO.log('Create the Editor..''info''example'); 
21    var myEditor = new YAHOO.widget.Editor('editor', myConfig); 
22    myEditor.render(); 
23 
24    _button.on('click'function(ev) { 
25        Event.stopEvent(ev); 
26        if (state == 'on') { 
27            YAHOO.log('state is on, so turn off''info''example'); 
28            state = 'off'
29            myEditor.saveHTML(); 
30            YAHOO.log('Save the Editors HTML''info''example'); 
31            var stripHTML = /<\S[^><]*>/g; 
32            myEditor.get('textarea').value = myEditor.get('textarea').value.replace(/<br>/gi, '\n').replace(stripHTML, ''); 
33            YAHOO.log('Strip the HTML markup from the string.''info''example'); 
34            YAHOO.log('Set Editor container to position: absolute, top: -9999px, left: -9999px. Set textarea visible''info''example'); 
35            Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'position''absolute'); 
36            Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'top''-9999px'); 
37            Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'left''-9999px'); 
38            myEditor.get('element_cont').removeClass('yui-editor-container'); 
39            Dom.setStyle(myEditor.get('element'), 'visibility''visible'); 
40            Dom.setStyle(myEditor.get('element'), 'top'''); 
41            Dom.setStyle(myEditor.get('element'), 'left'''); 
42            Dom.setStyle(myEditor.get('element'), 'position''static'); 
43        } else { 
44            YAHOO.log('state is off, so turn on''info''example'); 
45            state = 'on'
46            YAHOO.log('Set Editor container to position: static, top: 0, left: 0. Set textarea to hidden''info''example'); 
47            Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'position''static'); 
48            Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'top''0'); 
49            Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'left''0'); 
50            Dom.setStyle(myEditor.get('element'), 'visibility''hidden'); 
51            Dom.setStyle(myEditor.get('element'), 'top''-9999px'); 
52            Dom.setStyle(myEditor.get('element'), 'left''-9999px'); 
53            Dom.setStyle(myEditor.get('element'), 'position''absolute'); 
54            myEditor.get('element_cont').addClass('yui-editor-container'); 
55            YAHOO.log('Reset designMode on the Editor''info''example'); 
56            myEditor._setDesignMode('on'); 
57            YAHOO.log('Inject the HTML from the textarea into the editor''info''example'); 
58            myEditor.setEditorHTML(myEditor.get('textarea').value.replace(/\n/g, '<br>')); 
59        } 
60    }); 
61})(); 
view plain | print | ?

YUI Logger Output:

Logger Console

INFO367ms (+0) 11:24:36 PM:

example

Create the Editor..

INFO367ms (+3) 11:24:36 PM:

example

Set state to on..

INFO364ms (+364) 11:24:36 PM:

example

Create Button Control (#toggleEditor)

INFO0ms (+0) 11:24:36 PM:

global

Logger initialized

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

More Rich Text Editor (beta) Resources:

Copyright © 2008 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings