YUI Library Home

YUI Library Examples: Resize Utility: Custom resizing for the Rich Text Editor

Resize Utility: Custom resizing for the Rich Text Editor

This example shows how to customize the Resize Utility for things like the Rich Text Editor Control.

This text field can contain stylized text and graphics. To cycle through all formatting options, use the keyboard shortcut Shift + Escape to place focus on the toolbar and navigate between options with your arrow keys. To exit this text editor use the Escape key and continue tabbing.

Common formatting keyboard shortcuts:

  • Control Shift B sets text to bold
  • Control Shift I sets text to italic
  • Control Shift U underlines text
  • Control Shift L adds an HTML link
body

You have left the Rich Text Editor.

Setting up the Editors HTML

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

1<form method="post" action="#" id="form1"
2<textarea id="editor" name="editor" rows="20" cols="75"
3Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse justo nibh, 
4pharetra at, adipiscing ullamcorper, rutrum ac, enim. Nullam pretium interdum metus. 
5Ut in neque. Vivamus ut lorem vitae turpis porttitor tempor. Nam consectetuer est quis lacus. 
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    Event.onDOMReady(function() { 
7        editor = new YAHOO.widget.SimpleEditor('editor', { 
8            height: '300px'
9            width: '522px'
10            dompath: true
11            animate: true 
12        }); 
13        editor.render(); 
14    }); 
15 
16})(); 
view plain | print | ?

Setting up the Resize Utility

Now we listen for the editorContentLoaded event and setup our Resize instance.

1editor.on('editorContentLoaded'function() { 
2    resize = new YAHOO.util.Resize(editor.get('element_cont').get('element'), { 
3        handles: ['br'], 
4        autoRatio: true
5        status: true
6        proxy: true 
7    }); 
8}); 
view plain | print | ?

Making the Resize Utility do what we want

Now we have a resizable Editor instance, but it doesn't resize the way we want. It only resizes the outer element. But we want the Editor to resize the content area as well.

This is where the config option setSize comes in. When you set the setSize option to false (only when using the proxy config), the Resize Utility will not resize the element. It will return the data needed to resize it in the resize Event.

So now we listen for the startResize Event to set the disabled option on the Editor instance.

Then we listen for the resize Event to get the new height and width. Once we have that we can do a little math and tell the Editor instance to resize itself.

1resize = new YAHOO.util.Resize(editor.get('element_cont').get('element'), { 
2    handles: ['br'], 
3    autoRatio: true
4    status: true
5    proxy: true
6    setSize: false //This is where the magic happens 
7}); 
8resize.on('startResize'function() { 
9    this.hide(); 
10    this.set('disabled'true); 
11}, editor, true); 
12resize.on('resize'function(args) { 
13    var h = args.height; 
14    var th = (this.toolbar.get('element').clientHeight + 2); //It has a 1px border.. 
15    var dh = (this.dompath.clientHeight + 1); //It has a 1px top border.. 
16    var newH = (h - th - dh); 
17    this.set('width', args.width + 'px'); 
18    this.set('height', newH + 'px'); 
19    this.set('disabled'false); 
20    this.show(); 
21}, editor, true); 
view plain | print | ?

Styling the resize handle

Next we will override the default Resize CSS and make the handle a little bigger.

1/* The ID of the editor's container and the bottom right resize handle. */ 
2#editor_container .yui-resize-handle-br { 
3    /* Make the handle a little bigger than the default */ 
4    height11px
5    width11px
6    /* Resposition the image */ 
7    background-position-20px -60px
8    /* Kill the hover on the handle */ 
9    background-colortransparent
10} 
view plain | print | ?

Full Example Source

1(function() { 
2    var Dom = YAHOO.util.Dom, 
3        Event = YAHOO.util.Event, 
4        editor = null
5        resize = null
6 
7    Event.onDOMReady(function() { 
8        editor = new YAHOO.widget.SimpleEditor('editor', { 
9            height: '300px'
10            width: '522px'
11            dompath: true
12            animate: true 
13        }); 
14        editor.on('editorContentLoaded'function() { 
15            resize = new YAHOO.util.Resize(editor.get('element_cont').get('element'), { 
16                handles: ['br'], 
17                autoRatio: true
18                status: true
19                proxy: true
20                setSize: false //This is where the magic happens 
21            }); 
22            resize.on('startResize'function() { 
23                this.hide(); 
24                this.set('disabled'true); 
25            }, editor, true); 
26            resize.on('resize'function(args) { 
27                var h = args.height; 
28                var th = (this.toolbar.get('element').clientHeight + 2); //It has a 1px border.. 
29                var dh = (this.dompath.clientHeight + 1); //It has a 1px top border.. 
30                var newH = (h - th - dh); 
31                this.set('width', args.width + 'px'); 
32                this.set('height', newH + 'px'); 
33                this.set('disabled'false); 
34                this.show(); 
35            }, editor, true); 
36        }); 
37        editor.render(); 
38    }); 
39 
40})(); 
view plain | print | ?

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

Copyright © 2009 Yahoo! Inc. All rights reserved.

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