How to detect when a menu is rendered in tinymce 4?


Remi Becheras

In tinymce 4, the menu bar is rendered, but each menu is only rendered when clicked.

To illustrate this, notice that every menu in the menu bar has mce-menuthis class.

At any time, if no menu is open, the attempt to get the menu set fails because the menu has not yet been rendered:

var menuSet = $('.mce-menu');
// menuSet.length : 0

However, if you click on the menu bar title, lets say insertmenu, it will be rendered and opened. Now, keep it open, go to the console and try again:

var menuSet = $('.mce-menu');
// menuSet.length : 1

You will get the menu that opens.

Now, if you close it, click anywhere in the menu that opens and try again:

var menuSet = $('.mce-menu');
// menuSet.length : 1

...the menu is not removed from the DOM. Here's the good news: since the menu is rendered one at a time, we can get and manipulate it.

I need to do some DOM manipulation on each .mce-menuelement , but now it has to be used the first time each menu is opened.

But how to handle such an event?

I can't find any clues from official documentation, forums or anywhere.

Remi Becheras

I solved this problem by writing a handy `Tinymce 4 plugin that focuses on this purpose.

Of course, this plugin is open sourced under the GNU GPL v2 license following the original Tinymce license policy.

Tinymce plugin menu controller:

https://github.com/sirap-group/tinymce-plugin-menuscontroller

Sorry, I haven't written the documentation yet.

However, here's how to use it:

Install the plugin

Download the latest version tarball from github, or even better install it from bower:

bower install tinymce-plugin-menuscontroller

If you don't know , find itbower here: https://bower.io ( ) .npm i -g bower; bower --help

The npm package is not available yet, I'll make it available soon (but any Pull Request on github is welcome...).

By default, the location of the plugins folder will be deselected and placed in ./bower_components. If you installed tinymce the same way, you must also install ./bower_components/tinymceor ./bower_components/tinymce-dist.

You don't need to add the script to the file index.html, because tinymce loads it automatically if you set it up correctly .

Therefore, you need:

  1. Symlink it to the tinymce plugins folder:

    $ cd ./bower_components/tinymce/plugins
    $ ln -s ../../tinymce-plugin-menuscontroller menuscontroller
    
  2. Load it into tinymce init. E.g:

    tinymce.init({selector: 'textarea', //[...] plugins: 'menuscontroller'})

Get the plugin instance:

var editor = window.tinymce.activeEditor
var menusCtl = editor.plugins.menuscontroller
// at this point, if menusCtl is undefined, something gone wrong in the setup step: please check the previous steps.

Plugin API (v0.2.1)

instance method

Get the menu bar:

menusCtl.getMenubar()

Get each menu by its registered name:

menusCtl.getMenuByName(String: name)

get toolbar

menusCtl.getToolbars

Memorabilia

event: menusController:mceMenuRenderedevent

when presenting any tinymce menu

$('body').on('menusController:mceMenuRendered', function (evt, menuDomNode) {
  console.log(menuDomNode)
})

menusController:mceMenuRenderedWhen the event is rendered, each menu of the active editor menu bar refers to the event as an event , so when the user clicks the drop-down menu ( Filelink for the "File" menu, link for the Insert"Insert" menu, etc. ) is called an event .

event:menusController:mceMenuItemRendered:<menuDomID>

when any menu item is rendered. Suppose we have created a menu item with an my-custom-menu-itemidentifier . So tinymce sets its DOM ID to my-custom-menu-item. So the MenuController plugin will create and bind the following events to the body:

menusController:mceMenuItemRendered:my-custom-menu-item

So you can handle render events that your custom menu item listens to:

$('body').on('menusController:mceMenuItemRendered:my-custom-menu-item',
  function (evt, menuItemDomNode) {
    console.log(menuItemDomNode)
  }
)

MenusController API(v0.3.0 +)

At the time of this writing (Monday, March 13, 2017), the last released version is v0.2.1. But it v0.3.0is planned to be released soon and will provide a new event that is more useful than the previous one.

event:menusController:mceMenuItemRendered

When you need to know the menu item ID to handle the event menusController:mceMenuItemRendered:<menuDomID>and get the menu item DOM Node as a callback parameter, the event menusController:mceMenuItemRendereddoesn't need it, but instead provides it as a callback parameter for each newly rendered menu item:

$('body').on('menusController:mceMenuItemRendered',
  function (evt, menuItemID) {
    console.log(menuItemID) // 'my-custom-menu-item'

    // So you can hanlde all menu item even if you don't know its ID
    // And you can also handle the DOM Node with the selector by ID
    var selector = '#' + menuItemID
    var menuItem = $(selector)
    console.log(menuItem) // jQuery object (the menu item)
  }
)

Related


How to detect when a menu is rendered in tinymce 4?

Remi Becheras In tinymce 4, the menu bar is rendered, but each menu is only rendered when clicked. To illustrate this, notice that every menu in the menu bar has mce-menuthis class. At any time, if no menu is open, the attempt to get the menu set fails because

How to detect when a menu is rendered in tinymce 4?

Remi Becheras In tinymce 4, the menu bar is rendered, but each menu is only rendered when clicked. To illustrate this, notice that every menu in the menu bar has mce-menuthis class. At any time, if no menu is open, the attempt to get the menu set fails because

How to detect when a menu is rendered in TinyMce 4?

Remi Becheras In tinymce 4, the menu bar is rendered, but each menu is only rendered when clicked. To illustrate this, notice that every menu in the menu bar has mce-menuthis class. At any point, if no menu is open, trying to get the menu set will fail because

How to detect when a menu is rendered in TinyMce 4?

Remi Becheras In tinymce 4, the menu bar is rendered, but each menu is only rendered when clicked. To illustrate this, notice that every menu in the menu bar has mce-menuthis class. At any point, if no menu is open, trying to get the menu set will fail because

How to detect when a menu is rendered in tinymce 4?

Remi Becheras In tinymce 4, the menu bar is rendered, but each menu is only rendered when clicked. To illustrate this, notice that every menu in the menu bar has mce-menuthis class. At any time, if no menu is open, the attempt to get the menu set fails because

How to detect when a view element is rendered in Angular?

Alex Savitsky My setup is an Angular Material datatable with clickable rows. When a row is clicked, its content is displayed inline for textareaediting. The only problem I have is that I try to move the input focus to textarea. I tried to do this with @ViewChi

How to detect when a view element is rendered in Angular?

Alex Savitsky My setup is an Angular Material datatable with clickable rows. When a row is clicked, its content is displayed inline for textareaediting. The only problem I have is that I try to move the input focus to textarea. I tried to do this with @ViewChi

How to detect when a view element is rendered in Angular?

Alex Savitsky My setup is an Angular Material datatable with clickable rows. When a row is clicked, its content is displayed inline for textareaediting. The only problem I have is that I try to move the input focus to textarea. I tried to do this with @ViewChi

How to detect when a view element is rendered in Angular?

Alex Savitsky My setup is an Angular Material datatable with clickable rows. When a row is clicked, its content is displayed inline for textareaediting. The only problem I have is that I try to move the input focus to textarea. I tried to do this with @ViewChi

How to detect when Windows start menu/start screen opens?

black How can I set an event handler or callback for the Windows start menu (or Windows 8 start screen) to open? Or, at least, how to check if the start menu is currently open? I prefer a C# solution to this problem, but a C++ solution would be greatly appreci

Remove menu and status bar in TinyMCE 4

Tom Hagrid I'm trying to remove the menu and status bar from TinyMCE 4 because I want to setup a very basic editor. is it possible? The documentation for TinyMCE 3 doesn't seem relevant and I can't find anything for version 4. Tom Hagrid I looked at the docume

TinyMCE 4 add dropdown to menu bar

Tomalz I need to add another dropdown menu next to the "Tools" item in TinyMCE 4: The closest solution I found is: // Adds a custom menu item to the editor that inserts contents when clicked // The context option allows you to add the menu item to an existing

TinyMCE 4 add dropdown to menu bar

Tomalz I need to add another dropdown menu next to the "Tools" item in TinyMCE 4: The closest solution I found is: // Adds a custom menu item to the editor that inserts contents when clicked // The context option allows you to add the menu item to an existing

Remove menu and status bar in TinyMCE 4

Tom Hagrid I'm trying to remove the menu and status bar from TinyMCE 4 because I want to setup a very basic editor. is it possible? The documentation for TinyMCE 3 doesn't seem relevant and I can't find anything for version 4. Tom Hagrid I looked at the docume

TinyMCE 4 add dropdown to menu bar

Tomalz I need to add another dropdown menu next to the "Tools" item in TinyMCE 4: The closest solution I found is: // Adds a custom menu item to the editor that inserts contents when clicked // The context option allows you to add the menu item to an existing

TinyMCE 4 add dropdown to menu bar

Tomalz I need to add another dropdown menu next to the "Tools" item in TinyMCE 4: The closest solution I found is: // Adds a custom menu item to the editor that inserts contents when clicked // The context option allows you to add the menu item to an existing

Remove menu and status bar in TinyMCE 4

Tom Hagrid I'm trying to remove the menu and status bar from TinyMCE 4 because I want to setup a very basic editor. is it possible? The documentation for TinyMCE 3 doesn't seem relevant and I can't find anything for version 4. Tom Hagrid I looked at the docume

TinyMCE 4 add dropdown to menu bar

Tomalz I need to add another dropdown menu next to the "Tools" item in TinyMCE 4: The closest solution I found is: // Adds a custom menu item to the editor that inserts contents when clicked // The context option allows you to add the menu item to an existing

How can I detect if the view is fully rendered?

Commonwealth I have several GWT widgets that are displayed in a view. The view contains the following: FlowPanel mainPanel = new FlowPanel(); RootPanel.get().add(mainPanel); Label label = new Label("test"); mainPanel.add(label); FlowPanel otherPanel = new Flow

How can I detect if the view is fully rendered?

Commonwealth I have several GWT widgets that are displayed in a view. The view contains the following: FlowPanel mainPanel = new FlowPanel(); RootPanel.get().add(mainPanel); Label label = new Label("test"); mainPanel.add(label); FlowPanel otherPanel = new Flow

How to detect if rendered HTML content is blank/blank?

Jordan Ritter Consider the following code: <p>&nbsp;</p><!-- comment --> <span></span><br /> <div><span class="foo"></span></div> Effectively renders blank on browsers. I'm wondering if given or similar tokens, is there a straightforward, programmatic way to

How to detect if rendered HTML content is blank/blank?

Jordan Ritter Consider the following code: <p>&nbsp;</p><!-- comment --> <span></span><br /> <div><span class="foo"></span></div> Effectively renders blank on browsers. I was wondering, given or similar tokens, if there is a straightforward, programmatic way

How to detect if rendered HTML content is blank/blank?

Jordan Ritter Consider the following code: <p>&nbsp;</p><!-- comment --> <span></span><br /> <div><span class="foo"></span></div> Effectively renders blank on browsers. I'm wondering if given or similar tokens, is there a straightforward, programmatic way to

ThreeJS: How to detect if an object is rendered/visible

Markus Siebeneicher How can I detect as soon as possible if the camera eye can see the threejs object? The obj.visible property is setter, so useless. Also, frustumCullum is not enough as it only indicates if the object is outside the camera viewport. I need t

How to detect which view file is rendered

Sumit Shrestha I have to know the view files rendered by grails. One way is to do the afterView action in the filter. Here, I can't find a way to know which view file to render. So, is there any way for me to know which view file has been rendered by the rende