YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Rich Text Editor: One Editor, Multiple Edit Areas

# Rich Text Editor: One Editor, Multiple Edit Areas

This example demonstrates how to have one Editor instance control different editable areas on a page.

View example in new window.

## Setting up the HTML

First we place a textarea on the page and set it to visibility hidden, then create the editable areas and assign them a class of `editable`

By using the `.yui-editor-container` class, we are setting the Editor to an absolute position of -9999px top and -9999px left to render it off of the screen.

.yui-editor-container { position: absolute; top: -9999px; left: -9999px; z-index: 999; /* So Safari behaves */ } #editor { visibility: hidden; position: absolute; } .editable { border: 1px solid black; margin: .25em; float: left; width: 350px; height: 100px; } <textarea id="editor"></textarea> <div id="editable_cont"> <div class="editable">#1. Double click me to edit the contents</div> <div class="editable">#2. Double click me to edit the contents</div> <div class="editable">#3. Double click me to edit the contents</div> <div class="editable">#4. Double click me to edit the contents</div> <div class="editable">#5. Double click me to edit the contents</div> <div class="editable">#6. Double click me to edit the contents</div> </div>

## Prep the Editor

First we are going to setup the editor with a smaller toolbar.

(function() { var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event, editing = null; YAHOO.log('Setup a stripped down config for the editor', 'info', 'example'); var myConfig = { height: '150px', width: '380px', animate: true, limitCommands: true, toolbar: { titlebar: 'My Editor', collapse: true, buttons: [ { group: 'textstyle', label: 'Font Style', buttons: [ { type: 'push', label: 'Bold', value: 'bold' }, { type: 'push', label: 'Italic', value: 'italic' }, { type: 'push', label: 'Underline', value: 'underline' }, { type: 'separator' }, { type: 'select', label: 'Arial', value: 'fontname', disabled: true, menu: [ { text: 'Arial', checked: true }, { text: 'Arial Black' }, { text: 'Comic Sans MS' }, { text: 'Courier New' }, { text: 'Lucida Console' }, { text: 'Tahoma' }, { text: 'Times New Roman' }, { text: 'Trebuchet MS' }, { text: 'Verdana' } ] }, { type: 'spin', label: '13', value: 'fontsize', range: [ 9, 75 ], disabled: true }, { type: 'separator' }, { type: 'color', label: 'Font Color', value: 'forecolor', disabled: true }, { type: 'color', label: 'Background Color', value: 'backcolor', disabled: true } ] } ] } }; YAHOO.log('Create the Editor..', 'info', 'example'); myEditor = new YAHOO.widget.Editor('editor', myConfig); myEditor.render(); })();

## Showing the Editor

Now that we have the Editor rendered and positioned off of the screen, we need to be able to show it when you double click on an editable area.

We add a `dblclick` listener to the container `editable_cont`.

Inside of this listener, we grab the target of the event and check to see if it has the `editable` class.

Note: This logic may need to be refined for your use case as it relies on the user clicking the DIV with the editable class, so clicking on an element inside the div will fail. Most of the time, you would probably want the user to click on some sort of Edit button to exec this action.

Once we have the DIV, we set the Editor's HTML with the innerHTML of the DIV.

Next we position the Editor with the XY coordinates of the DIV.

Event.on('editable_cont', 'dblclick', function(ev) { var tar = Event.getTarget(ev); while(tar.id != 'editable_cont') { if (Dom.hasClass(tar, 'editable')) { YAHOO.log('An element with the classname of editable was double clicked on.', 'info', 'example'); if (editing !== null) { YAHOO.log('There is an editor open, save its data before continuing..', 'info', 'example'); myEditor.saveHTML(); editing.innerHTML = myEditor.get('element').value; } YAHOO.log('Get the XY position of the Element that was clicked', 'info', 'example'); var xy = Dom.getXY(tar); YAHOO.log('Set the Editors HTML with the elements innerHTML', 'info', 'example'); myEditor.setEditorHTML(tar.innerHTML); YAHOO.log('Reposition the editor with the elements xy', 'info', 'example'); Dom.setXY(myEditor.get('element_cont').get('element'), xy); editing = tar; } tar = tar.parentNode; } });

## Saving the Editor with the toolbar

Now we need to setup a way to save the Editor's data, we are going to override the toolbar's collapse button and use it as a save button.

First we need to override the text on the collapse button, we do that by overriding the var `STR_COLLAPSE` on the `YAHOO.widget.Toolbar`'s prototype.

Now we listen for the `toolbarLoaded` event so we can attach our handler to the `toolbarCollapsed` event.

Inside of the `toolbarCollapsed` event, we will save the Editor's data back to the editable area. Then we will set the position of the editor back to -9999px left and -9999px top.

Note: You could also do this using a Save button in the toolbar.

YAHOO.log('Override the prototype of the toolbar to use a different string for the collapse button', 'info', 'example'); YAHOO.widget.Toolbar.prototype.STR_COLLAPSE = 'Click to close the editor.'; YAHOO.log('Create the Editor..', 'info', 'example'); myEditor = new YAHOO.widget.Editor('editor', myConfig); myEditor.on('toolbarLoaded', function() { YAHOO.log('Toolbar is loaded, add a listener for the toolbarCollapsed event', 'info', 'example'); this.toolbar.on('toolbarCollapsed', function() { YAHOO.log('Toolbar was collapsed, reposition and save the editors data', 'info', 'example'); Dom.setXY(this.get('element_cont').get('element'), [-99999, -99999]); Dom.removeClass(this.toolbar.get('cont').parentNode, 'yui-toolbar-container-collapsed'); myEditor.saveHTML(); editing.innerHTML = myEditor.get('element').value; editing = null; }, myEditor, true); }, myEditor, true); myEditor.render();

## Full Example Javascript Source

(function() { var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event, editing = null; YAHOO.log('Setup a stripped down config for the editor', 'info', 'example'); var myConfig = { height: '150px', width: '380px', animate: true, limitCommands: true, toolbar: { titlebar: 'My Editor', collapse: true, buttons: [ { group: 'textstyle', label: 'Font Style', buttons: [ { type: 'push', label: 'Bold', value: 'bold' }, { type: 'push', label: 'Italic', value: 'italic' }, { type: 'push', label: 'Underline', value: 'underline' }, { type: 'separator' }, { type: 'select', label: 'Arial', value: 'fontname', disabled: true, menu: [ { text: 'Arial', checked: true }, { text: 'Arial Black' }, { text: 'Comic Sans MS' }, { text: 'Courier New' }, { text: 'Lucida Console' }, { text: 'Tahoma' }, { text: 'Times New Roman' }, { text: 'Trebuchet MS' }, { text: 'Verdana' } ] }, { type: 'spin', label: '13', value: 'fontsize', range: [ 9, 75 ], disabled: true }, { type: 'separator' }, { type: 'color', label: 'Font Color', value: 'forecolor', disabled: true }, { type: 'color', label: 'Background Color', value: 'backcolor', disabled: true } ] } ] } }; YAHOO.log('Override the prototype of the toolbar to use a different string for the collapse button', 'info', 'example'); YAHOO.widget.Toolbar.prototype.STR_COLLAPSE = 'Click to close the editor.'; YAHOO.log('Create the Editor..', 'info', 'example'); myEditor = new YAHOO.widget.Editor('editor', myConfig); myEditor.on('toolbarLoaded', function() { YAHOO.log('Toolbar is loaded, add a listener for the toolbarCollapsed event', 'info', 'example'); this.toolbar.on('toolbarCollapsed', function() { YAHOO.log('Toolbar was collapsed, reposition and save the editors data', 'info', 'example'); Dom.setXY(this.get('element_cont').get('element'), [-99999, -99999]); Dom.removeClass(this.toolbar.get('cont').parentNode, 'yui-toolbar-container-collapsed'); myEditor.saveHTML(); editing.innerHTML = myEditor.get('element').value; editing = null; }, myEditor, true); }, myEditor, true); myEditor.render(); Event.on('editable_cont', 'dblclick', function(ev) { var tar = Event.getTarget(ev); while(tar.id != 'editable_cont') { if (Dom.hasClass(tar, 'editable')) { YAHOO.log('An element with the classname of editable was double clicked on.', 'info', 'example'); if (editing !== null) { YAHOO.log('There is an editor open, save its data before continuing..', 'info', 'example'); myEditor.saveHTML(); editing.innerHTML = myEditor.get('element').value; } YAHOO.log('Get the XY position of the Element that was clicked', 'info', 'example'); var xy = Dom.getXY(tar); YAHOO.log('Set the Editors HTML with the elements innerHTML', 'info', 'example'); myEditor.setEditorHTML(tar.innerHTML); YAHOO.log('Reposition the editor with the elements xy', 'info', 'example'); Dom.setXY(myEditor.get('element_cont').get('element'), xy); editing = tar; } tar = tar.parentNode; } }); })();

### Rich Text Editor 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)
- Simple Application (included with examples for Layout Manager)
- Complex Application (included with examples for Layout Manager)

### More Rich Text Editor 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