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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Customizing TinyMCE: Adding Setup Event


Go to End


7 Posts   6853 Views

Avatar
Deklin

Community Member, 16 Posts

14 July 2010 at 1:04pm

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

Community Member, 181 Posts

14 July 2010 at 2:04pm

Edited: 14/07/2010 2:09pm

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

Community Member, 16 Posts

14 July 2010 at 11:54pm

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

Community Member, 181 Posts

15 July 2010 at 9:33am

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

Community Member, 181 Posts

15 July 2010 at 10:00am

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

Community Member, 16 Posts

15 July 2010 at 10:54am

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

Community Member, 75 Posts

17 January 2011 at 4:56pm

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?