Wednesday, July 14, 2010 | 11:05 AM
One of the many UI widgets included in the Closure Library is a full-featured rich text editor.
All modern browsers have rich text editing functionality built in — unfortunately this functionality is at best inconsistent and, at worst, broken. Closure Library’s editor is a sub-project within Closure. It provides a common, powerful, and extensible interface that abstracts the cross-browser inconsistencies and shortcomings of rich text editing.
We use the Closure editor in Gmail, Google Sites, Google Groups, and many other Google products. We’re very happy to be able to share this editor with you as part of the Closure Library.
Using the editor in your project
A demo of the basic editor functionality is included in the source distribution, and can be tried here.
The most important code is as follows. First, we create an editable field and attach it to an element with the id
// Create an editable field.
var myField = new goog.editor.Field('editMe');
Next, we register editing plugins. The rich text editor uses a plugin-based architecture in order to support many products with different feature sets. For example, in Gmail, the tab key moves the cursor to the send button, while in Google Sites it either adds 4 spaces or indents the current bulleted list. Plugins allow us to create modular features for the Editor and let each application pick and choose which ones they need or want. This ensures each application only brings in the code it needs, helping to minimize code size and latency.
// Create and register all of the editing plugins you want to use.
new goog.editor.plugins.LoremIpsum('Click here to edit'));
In a future post, we’ll discuss how you can use the plugin system to add your own custom functionality to your editor.
Next we set up our toolbar:
// Specify the buttons to add to the toolbar, using built in default buttons.
var buttons = [
var myToolbar = goog.ui.editor.DefaultToolbar.makeToolbar(buttons,
// Hook the toolbar into the field.
var myToolbarController =
new goog.ui.editor.ToolbarController(myField, myToolbar);
And finally, we turn on editing:
When we open the file in the browser, we get an HTML editor:
Let us know in the comments if you’ve used or plan to use the Closure Library rich text editor in your project. If you have questions, please post them to the discussion group.