Control.TextArea

Cross browser textarea manipulation for Prototype.js

Introduction

Decent, flexible WYSIWYG editing in the browser is still an inconsistent affair at best in terms of user experience, integration effort, and generated markup. In the meantime a crop of text formatting languages such as Markdown, Textile, BBCode and wiki markup have emerged as ways for users to enter rich content in a controlled enviornment.

Control.TextArea provides a toolbar for textareas where users are entering text in one of these markup languages. In addition to a toolbar it provides access to a cross browser text selection and manipulation API so that creating new functionality, or integration with your custom application features is vastly easier.

Example

The example below demonstrates both components of Control.TextArea as it is used in other areas of the site. This implementation is specific to the Markdown formatting language.

Sample Code

  1. var toolbar = new Control.TextArea.Toolbar(new Control.TextArea('markdown_example'));  
  2. //all button callbacks are automatically bound to the Control.TextArea object  
  3. toolbar.addButton('Bold',function(){  
  4.     this.wrapSelection('**','**');  
  5. });  
  6. toolbar.addButton('Link',function(){  
  7.     var selection = this.getSelection();  
  8.     var response = prompt('Enter Link URL','');  
  9.     if(response == null)  
  10.         return;  
  11.     this.replaceSelection('[' + (selection == '' ? 'Link Text' : selection) + '](' + (response == '' ? 'http://link_url/' : response) + ')');  
  12. });  
  13. toolbar.addButton('Code Block',function(){  
  14.     this.injectEachSelectedLine(function(lines,line){  
  15.         lines.push((event.shiftKey ? line.replace(/    /,'') : '    ' + line));  
  16.         return lines;  
  17.     });  
  18. });