Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS

Customizing TinyMCE: Adding Setup Event


Reply

7 Posts   4071 Views

Avatar
Deklin

14 July 2010 at 1:04pm Community Member, 16 Posts

TinyMCE allows you to create a JavaScript function to be executed when the editor first loads. Here is an example of how to do this with TinyMCE:

tinyMCE.init({
mode : "textareas",
theme : "simple",
setup : function() { alert('This function runs when TinyMCE has fully loaded.') );
}
});

In older versions of TinyMCE I could accomplish this by hacking cms/javascript/tinymce.template.js but now that file seems to be replaced with a dynamic alternative.

How can I add a setup : function() to TinyMCE in SilverStripe version 2.4+?

Avatar
TotalNet

14 July 2010 at 2:04pm (Last edited: 14 July 2010 2:09pm), Community Member, 181 Posts

I've had mixed results configuring TinyMCE in the CMS but you should be able to add options via HtmlConfigEditor in your _config.php e.g.

HtmlEditorConfig::get('cms')->setOption('setup', 'yourSetupFunction');

and include your javascript file with the setup function using LeftAndMain::require_javascript()

The main issues I've had are mostly to do with the version of TinyMCE but that should work. take a look at _config.php in cms for the setup of the default editor profile.

Avatar
Deklin

14 July 2010 at 11:54pm Community Member, 16 Posts

This does not work. It gives a JavaScript error: "F is undefined"

HtmlEditorConfig::get('cms')->setOption('setup', "window.onload = function() {alert('Editor Loaded')}");

Am I doing anything wrong? Is this a TinyMCE bug?

Thanks!

Avatar
TotalNet

15 July 2010 at 9:33am Community Member, 181 Posts

Well, it sounds like the option is being set at least.

At this point I think you'll need to look at the javascript with firebug or similar. It's possible that there's already a window.onload already being called and you can't have more than one AFAIK or some other syntax conflict, try something else so you can test it's being called correctly. There's a LOT of javascript in the CMS.

Beyond that, I'm afraid I won't be of much more help.

Rich

Avatar
TotalNet

15 July 2010 at 10:00am Community Member, 181 Posts

Okay, so I've tried it quickly myself.

"setup" is definitely being set, however, you're correct that as soon as you have any value in there it produces two errors in the CMS;
f is undefined - tiny_mce_src.js Line 9365
tree.setCurrentByIdx is not a function - LeftAndMain_left.js Line 26

So, it could be a conflict between TinyMCE and one of the many other scripts, and that's way beyond me, sorry.

Perhaps there's a better place to fire a setup event?

Avatar
Deklin

15 July 2010 at 10:54am Community Member, 16 Posts

Thanks for the help. Yes, the JavaScript is definitely being set however it seems to destroy TinyMCE in the process.

Does anyone else have any experience with this?

Avatar
pingu

17 January 2011 at 4:56pm Community Member, 75 Posts

I also need to do this - I need to add a character count to TinyMCE, and I was hoping to do it using this code snippet:
http://tinymce.moxiecode.com/forum/viewtopic.php?pid=42558#p42558

Does anyone have any tips on how I can create a setup event, or if there are better ways of adding a character count to TinyMCE?