I discovered Xinha a couple of days ago after coming across this blog post and I was quite impressed with it. After playing around with it a little, I noticed it does not have the same issues with pre tags that both TinyMCE and FCKeditor exhibit variations of. This makes it a good choice if you are displaying a lot of code. The issues deal with replacing new line characters with br tags within pre tags and leads to some display and formatting problems. I wrote about my attempt to wrangle TinyMCE in a previous post. Xinha has not exhibited these issues and the supplied skins look great too.

After my initial testing of Xinha, I decided it would be a good time to finally create a plugin. I started with the TinyMCE plugin and adapted the code to work with Xinha. Since this is my first plugin and some cargo culting was involved, let me quickly remind you that there will most likely be bugs or errors at this point and that not everything is working correctly. That being said, the basics are working and unless you want to use some of the plugins mentioned below, you should not run into too many problems (key words being ’should not’).

To get started, install the plugin and run the rake task with

script/plugin install \
svn://rubyforge.org/var/svn/xinha/trunk
rake xinha:scripts:install

Next thing to do is let your controller know you want to use Xinha. Xinha has some initial setup options and the defaults are set to ‘/javascripts/xinha/’, ‘en’ and ‘blue-look’. If you need to change any of them, they are changed through the setup hash and not through the options hash. The javascript requires _editor_ at the beginning of those variables, but you should not use :_editor_skin to change the skin, just use :skin.

The options hash is where you can change all the other configuration options. Except for the editors and plugins options, the name (hash key) of the option should be the same as the xinha_config.option name but with ruby naming conventions (lowercase and an underscore instead of a space). The value of both the editors and plugins options are an array of strings. Although not technically required, if you don’t specify anything for editors, nothing will happen. Here is an example of what the code in your controller might look like.

uses_xinha(:setup => {
              :skin => 'silva' },
           :options => {
              :editors => ['post_body'],
              :plugins => ['Equation','FindReplace','GenericPlugin','GetHtml','HorizontalRule','InsertAnchor','InsertSmiley','SaveSubmit'],
              :height => '500px' },
           :only => [:new, :edit])

From here, the last thing to do is add the appropriate code to your layout. It couldn’t be simpler, just add

<%= include_xinha_if_used %>

inside the head element. The above snippets load up the editor only for the new and edit actions of my example imaginary post scaffolding. Nothing happens with any of the other actions. All of the javascript, images and css are only loaded when necessary.

I have not had the time to test all of the Xinha plugins. I did uncover a few bugs so I want to mention them briefly. If you load all of the plugins, including the legacy htmlArea plugins, one of them, not sure exactly which one, is nice enough to wrap your text with <html> <head> </head> <body spellcheck=”false” > … </body> </html>. It would appear to be related to spellChecker, but removing that plugin did not appear to fix the problem. Removing all the legacy plugins prevents this from happening and some other random issues. Because of this, I am going to recommend you only use the Xinha specific plugins. If you need to use one of the legacy plugins and get it to work, play nicely and let us know what you did to make it work.

Some of the plugins also utilize php code. The good ole webrick server that loads with script/server does not do so well with those plugins and they are lacking functionality. These plugins are

  1. ExtendedFileManager
  2. FormOperations
  3. HtmlTidy
  4. ImageManager
  5. InsertPicture
  6. InsertSnippet
  7. Linker
  8. SuperClean
  9. SpellChecker

That list includes some useful plugins and I hope to get at least a few of them working using ruby instead of php. Please don’t hold your breath though, I have no idea how long it will be until I can get any or all of them working. If you would like to help, it would be much appreciated. I set up a rubyforge project for this plugin here. If you find any bugs or have any suggestions, feel free to leave a comment here or use rubyforge.

Another thing to keep in mind, the initial release is at best considered an alpha release and I know there are several things that need to be added before this plugin creates a fully functional Xinha editor for Rails. Unfortunately, if I waited until I figured everything out, this may never have been released period and which is why it is available now for you to try out. This also is my first attempt at a plugin, so if a rookie mistake has been made somewhere along the line, let me know so I can fix it and not look like a noob. Either way, creating a Rails plugin has been a good learning experience and I hope you find it useful, even if not all of it works right away.

Tags: , , ,