Yahoo! Developer Network Home - Help

YDN Site Search

# 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.

View example in new window.

This is some more test text.<br>This is some more test text.<br>This is some more test text.<br>This is some more test text.<br>This is some more test text.<br>This is some more test text.<br>This is some more test text.<br>

## Setting up the Editors HTML

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

<button type="button" id="toggleEditor">Toggle Editor</button> <form method="post" action="#" id="form1"> <textarea id="editor" name="editor" rows="20" cols="75"> This is some more test text.<br>This is some more test text.<br>This is some more test text.<br> This is some more test text.<br>This is some more test text.<br>This is some more test text. </textarea> </form>

## Setting up the Editors Javascript

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

(function() { //Setup some private variables var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event; //The Editor config var myConfig = { height: '300px', width: '530px', animate: true, dompath: true, focusAtStart: true }; //Now let's load the Editor.. var myEditor = new YAHOO.widget.Editor('editor', myConfig); myEditor.render(); })();

## 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.

//From the Editor to the textarea var stripHTML = /<\S[^><]*>/g; myEditor.get('textarea').value = myEditor.get('textarea').value.replace(/<br>/gi, '\n').replace(stripHTML, ''); //From the textarea to the Editor myEditor.setEditorHTML(myEditor.get('textarea').value.replace(/\n/g, '<br>'));

## 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`.

myEditor.saveHTML(); var stripHTML = /<\S[^><]*>/g; myEditor.get('textarea').value = myEditor.get('textarea').value.replace(/<br>/gi, '\n').replace(stripHTML, ''); Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'position', 'absolute'); Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'top', '-9999px'); Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'left', '-9999px'); myEditor.get('element_cont').removeClass('yui-editor-container'); Dom.setStyle(myEditor.get('element'), 'visibility', 'visible'); Dom.setStyle(myEditor.get('element'), 'top', ''); Dom.setStyle(myEditor.get('element'), 'left', ''); Dom.setStyle(myEditor.get('element'), 'position', 'static');

## 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.

Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'position', 'static'); Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'top', '0'); Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'left', '0'); Dom.setStyle(myEditor.get('textarea'), 'display', 'none'); myEditor._setDesignMode('on'); myEditor.setEditorHTML(myEditor.get('textarea').value.replace(/\n/g, '<br>')); Dom.setStyle(myEditor.get('element'), 'visibility', 'hidden'); Dom.setStyle(myEditor.get('element'), 'top', '-9999px'); Dom.setStyle(myEditor.get('element'), 'left', '-9999px'); Dom.setStyle(myEditor.get('element'), 'position', 'absolute'); myEditor.get('element_cont').addClass('yui-editor-container');

## Full Example Javascript Source

(function() { var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event; YAHOO.log('Create Button Control (#toggleEditor)', 'info', 'example'); var _button = new YAHOO.widget.Button('toggleEditor'); _button.addClass('toggleEditor'); var myConfig = { height: '300px', width: '530px', animate: true, dompath: true, focusAtStart: true }; var state = 'on'; YAHOO.log('Set state to on..', 'info', 'example'); YAHOO.log('Create the Editor..', 'info', 'example'); var myEditor = new YAHOO.widget.Editor('editor', myConfig); myEditor.render(); _button.on('click', function(ev) { Event.stopEvent(ev); if (state == 'on') { YAHOO.log('state is on, so turn off', 'info', 'example'); state = 'off'; myEditor.saveHTML(); YAHOO.log('Save the Editors HTML', 'info', 'example'); var stripHTML = /<\S[^><]*>/g; myEditor.get('textarea').value = myEditor.get('textarea').value.replace(/<br>/gi, '\n').replace(stripHTML, ''); YAHOO.log('Strip the HTML markup from the string.', 'info', 'example'); YAHOO.log('Set Editor container to position: absolute, top: -9999px, left: -9999px. Set textarea visible', 'info', 'example'); Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'position', 'absolute'); Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'top', '-9999px'); Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'left', '-9999px'); myEditor.get('element_cont').removeClass('yui-editor-container'); Dom.setStyle(myEditor.get('element'), 'visibility', 'visible'); Dom.setStyle(myEditor.get('element'), 'top', ''); Dom.setStyle(myEditor.get('element'), 'left', ''); Dom.setStyle(myEditor.get('element'), 'position', 'static'); } else { YAHOO.log('state is off, so turn on', 'info', 'example'); state = 'on'; YAHOO.log('Set Editor container to position: static, top: 0, left: 0. Set textarea to hidden', 'info', 'example'); Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'position', 'static'); Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'top', '0'); Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'left', '0'); Dom.setStyle(myEditor.get('element'), 'visibility', 'hidden'); Dom.setStyle(myEditor.get('element'), 'top', '-9999px'); Dom.setStyle(myEditor.get('element'), 'left', '-9999px'); Dom.setStyle(myEditor.get('element'), 'position', 'absolute'); myEditor.get('element_cont').addClass('yui-editor-container'); YAHOO.log('Reset designMode on the Editor', 'info', 'example'); myEditor._setDesignMode('on'); YAHOO.log('Inject the HTML from the textarea into the editor', 'info', 'example'); myEditor.setEditorHTML(myEditor.get('textarea').value.replace(/\n/g, '<br>')); } }); })();

### YUI Logger Output:

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

Reload with logging
and debugging disabled.

### Rich Text Editor (beta) Examples:

- Flickr Image Search
- Calendar Plugin
- Icon Insertion
- Plain Text Switcher
- Editor Data Post with Connection Manager
- Skinning the Editor
- Default Toolbar Config
- Code Editor
- Editor inside a Tabview Control
- One Editor, Multiple Edit Areas
- Editor with Custom Image Browser
- Simple Editor — Basic Buttons
- Simple Editor — Advanced Buttons
- Editor — Basic Buttons
- Editor Auto Adjusting Height
- Custom resizing for the Rich Text Editor (included with examples for Resize Utility (beta))
- Simple Application (included with examples for Layout Manager (beta))
- Complex Application (included with examples for Layout Manager (beta))

### More Rich Text Editor (beta) Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2008 Yahoo! Inc. All rights reserved.

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