Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement TinyMCE with Require.js?

I am currently passing in the TinyMCE source as a dependency, and then calling tinyMCE.init({}); but it is not initializing TinyMCE. When I console.log TinyMCE, it returns a TinyMCE Object. Code sample below:

define([
'jQuery',
'Underscore',
'Backbone',
'TinyMCE'
], function($, _, Backbone, tinyMCE) {

        tinyMCE.init({
            mode: "exact",
            elements: $('textarea'),
            theme: "advanced",
            theme_advanced_toolbar_location: 'top',
            theme_advanced_buttons1: 'bold,italic,underline,bullist,numlist,link,unlink',
            theme_advanced_buttons2: '',
            theme_advanced_buttons3: '',
            theme_advanced_toolbar_align: 'left',
            plugins: 'paste,inlinepopups',
            width: '100%',
            height: textarea.attr('data-height'),
            oninit: function () {
                console.log('TargetTD :');
                console.log(targetTD);

            }
        });
   }
});
like image 970
user784756 Avatar asked Jul 05 '12 17:07

user784756


People also ask

Where do I put TinyMCE init?

Initialization of TinyMCE In order to initialize the TinyMCE the following code must be placed within HEAD element of a document.


2 Answers

You can use 'shim' for requirejs 2.1.0 or higher, see example of main script below:

requirejs.config({
    baseUrl: "js",
    paths: {
        tinyMCE: 'libs/tinymce/tiny_mce'
    },
    shim: {
        tinyMCE: {
            exports: 'tinyMCE',
            init: function () {
                this.tinyMCE.DOM.events.domLoaded = true;
                return this.tinyMCE;
            }
        }
    }
});

requirejs([
    'tinyMCE'
], function (tinyMCE) {
    console.log(tinyMCE);

    // your code here
});

Edit: I added iimuhin’s snippet from below in the comments. It doesn’t seem to work without it; I added it because future searchers will appreciate avoiding the added IE headache.

like image 68
ebaranov Avatar answered Nov 06 '22 21:11

ebaranov


Had the same problem. My solution was to use TinyMCE jQuery plugin instead of TinyMCE directly. This way it works fine.

define(['jquery', 'tiny_mce/jquery.tinymce'], function ($) {
    $('textarea').tinymce({
        script_url : 'js/tiny_mce/tiny_mce.js',
        theme : 'advanced',
        theme_advanced_buttons1 : 'fontselect,fontsizeselect,forecolor,bold,italic,underline,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,removeformat,indent,outdent,numlist,bullist,copy,paste,link',
        theme_advanced_buttons2 : '',
        theme_advanced_buttons3 : '',
        theme_advanced_toolbar_location : 'top',
        theme_advanced_toolbar_align : 'left'
   });
});
like image 26
Alexander Rysenko Avatar answered Nov 06 '22 21:11

Alexander Rysenko