Dojo and TinyMCE – Creating a Widget
Friday, May 8th, 2009I’m working on a project using Dojo which requires a wysiwyg editor that is more robust than Dojo’s Dijit.Editor. After doing a bit of research, I decided to use TinyMCE. This wysiwyg editor is very robust, has great documentation and is the editor that WordPress uses. Integrating TinyMCE did not appear to be difficult, but I also wanted to create a TinyMCE Dojo widget, so that I could easily reuse TinyMCE in other Dojo projects without having to remember how to set it up.
The Dojo widget that I created is based upon an implementation that I found on Disgruntleddevs.wordpress.com, but I changed the widget to make it more flexible – and I also don’t use a template (templatePath , templateString) with the widget because I did understand why it was needed. Also, while researching how to create a Dojo widget, I found a great article by Peter Higgins entitled, “What is a _Widget“.
Widget Location
When working on a project, I generally place all dojo files in a js/ or lib/ folder. I also have a folder named “mywidgets” which is a sibling to the dojo/ folder. All of the widgets that I create are placed inside this folder. In this case, I created a widget named RichEditor.js and placed it in the “mywidgets” folder.
Loading Dojo and TinyMCE
When loading dojo, I have to set modulePaths for the widget in the djConfig attribute. The path of widget is relative to the location of the dojo.js file.
<script type="text/javascript" src="js/dojo/dojo.js" djConfig="parseOnLoad: true, modulePaths:{'im':'../mywidgets'}" </script><!-- script -->
<script type="text/javascript" src="js/tiny_mce/tiny_mce.js" </script><!-- script --> |
Now I load the widget:
<script type="text/javascript">
dojo.require("im.imRichEditor");
</script> |
In the body of the html file, I created a textarea that is to be used for the tinyMCE editor:
<div id="tEditor"> <textarea id="tAreas" cols="80" rows="20" name="tAreas" /><!-- div --> </div> |
The Editor Widget
The imRichEditor widget is instantiated in the dojo.addOnLoad function. The widget is set up by default to display tinyMCE’s “Advanced” theme (all the bells and whistles), so to use it, I can simply:
dojo.addOnLoad(function() { var ta = new im.imRichEditor({editorNode: "tAreas"}, 'tEditor'); }); |
The “editorNode” is the id of the textarea and the ‘tEditor’ is the container. All of the options of the Advanced theme can be changed. For instance, the skin_variant option is set to “silver” by default. If I want to use the TinyMCE’s default “blue” skin:
var ta = new im.imRichEditor({editorNode: "tAreas", skin_variant:""}, 'tEditor'); |
Or Black:
var ta = new im.imRichEditor({editorNode: "tAreas", skin_variant:"black"}, 'tEditor'); |
If I want the editor to use a specific CSS file:
var ta = new im.imRichEditor({editorNode: "tAreas", skin_variant:"", content_css:"squeeze/styles.css"}, 'tEditor'); |
I can also change which buttons display in the editor and where. For instance, if I only want to display save, new document, bold, italic, underline, and strike through on the first row of buttons (advanced_buttons1):
var b1 = "save,newdocument,|,bold,italic,underline,strikethrough"; var ta = new im.imRichEditor({editorNode: "tAreas", advanced_buttons1: b1}, 'tEditor'); |
I can also change the toolbar location. The default value is “top”:
var ta = new im.imRichEditor({editorNode: "tAreas", toolbar_location: "bottom"}, 'tEditor'); |
If I want to use the TinyMCE “simple” theme:
var ta = new im.imRichEditor({editorNode: "tAreas", theme: "simple"}, 'tEditor'); |
The default toolbar location for the “simple” theme is on the bottom (set by TinyMCE, not sure why). To create the “simple” editor with the toolbar located on the top:
var ta = new im.RichEditor({editorNode: "tAreas", theme: "simple", toolbar_location:"top"}, 'tEditor'); |
I can also set an initial value for the editor:
var txt = '<h2 id="headerText">This is my Header Text</h2>'; var ta = new im.imRichEditor({editorNode: "tAreas", skin_variant:"", content_css:"squeeze/styles.css", value: txt}, 'tEditor'); |
To retrieve the content of the editor, I use the getValue() method:
console.log(ta.getValue()); |
As you can see, the imRichEditor widget is quite flexible and much more robust than Dojo’s text editor. You can download the widget from Google Code. Click here to view the demo.



March 7th, 2010 at 5:08 pm
Hey, thanks for this. It got me up and running with TinyMCE pretty fast.
Just a couple notes: in your textboxes, the & are being converted to the code version, which causes errors.
Also, the reason you need dijit._Templated and a templatePath or templateString is so that the widget puts a node on the page. That way you don’t have to add the textArea with an id that needs to match the one you used inside your widget–one less thing to get it up and running.
After those two changes, I had a widget-based tinyMCE editor running.
Thanks!
March 17th, 2010 at 10:09 am
Actually, I did not need dijit._Templated for my implementation. I have used it in other widgets that I have created, but in this case, I decided not to use it. I finally created a demo of this widget. You can view it here. I made a minor change to the widget because it was not displaying the toolbar on top when in simple mode. I now store the widget on Google Code (and updated the post with a link to the demo and Google Code).
April 17th, 2011 at 4:29 pm
Hi cool widget. I have got this up and runnig but im unable to get any data that i type into the tinymce textarea that is created out by using dojo to query the id of the textarea… ? Any idea what could be wrong?