Tuesday 19 May 2009

TinyMCE ListBox Plugin for Merge Tags

Well, today I finally managed to cobble together the following solution for a problem I had whereby I wanted a plugin for TinyMCE whereby I could select a value from a drop down / listbox, and have it inserted into TinyMCE.

So here's how I did it:

First define a javascript array BEFORE initialising the TinyMCE editor. This allows you to create this array server side if you want (by writing a response stream declaring it if necessary), or allows you to control it per page. E.g:

var arrUserTags = new Array('{{Name}}','{{Address}}','{{Company}}');


I created a new plugin in tiny_mce/plugins/usertags/editor_plugin.js as follows:


(function() {
tinymce.create('tinymce.plugins.usertags', {
init : function(ed, url) {
},
createControl: function(n, cm) {
switch (n) {
case 'usertags':
var mlb = cm.createListBox('usertags', {
title : 'Insert details ',
onselect : function(v) {
tinyMCE.activeEditor.selection.setContent(v);
}
});

//The array arrUserTags, must be defined before the editor is added!!! Or if you prefer, just ignore the array idea, and just do multiple mlb.add() statements.
for(i=0;i

This you can then register as a plugin in tinymce.

2 comments:

  1. tinyMCE.activeEditor.selection.setContent(v); -- this is not good, because it can insert into wrong editor if there're more than one editor on a page

    ReplyDelete
  2. Thanks for your comment, although this is not my experience. The editor in question is the only that receives the content, probably because the editor receives focus when you are selecting from the drop down box whilst using it. This I have working on a form that has some 19 editors on it (it's a large data entry app).

    ReplyDelete