CODEX
Implementing a TinyMCE URL Dialog
URL dialogs are TinyMCE UI components used to display external web pages. These dialogs are used in scenarios where standard TinyMCE plugins and dialogs do not suffice. The external page displayed in the URL dialog may be hosted on a completely different domain than the TinyMCE parent.
Communication
Messaging between the TinyMCE editor and the external page is done with JavaScript through the window.postMessage() method which allows cross-origin communication between the window objects.
To follow along with the rest of this article you may wish to open the demo application and download the source code from GitHub.
Cloning demo application from GitHub
$ git clone https://github.com/jeffholst/tinymce-url-dialog
$ cd tinymce-url-dialog
The tinymce-url-dialog folder should now contain the following files:
- README.md
- main.js
- index.html
- dialog.html
Demo Walkthrough
- open the index.html file in a web browser
- click the Open URL Dialog button in the TinyMCE toolbar to open a new dialog window
- in the URL Dialog click the Get TinyMCE Content button to retrieve the “Hello, World!” content of the parent TinyMCE editor
- in the URL Dialog update the TextArea to say “Hello, Galaxy!” instead of “Hello, World!”
- in the URL Dialog click the Set TinyMCE Content button to update the parent TinyMCE with the new message
- close the URL Dialog by clicking the X in the top right corner
- The TinyMCE content should now say “Hello, Galaxy!”
Overview
main.js
To simplify this demo all JavaScript used by both index.html and dialog.html is contained in the main.js file.
index.html
This is the starting page for the project and contains the TinyMCE editor. Once the page is loaded in the browser the main.js/initializeParent() function is called.
The main.js/initializeParent() function initializes the TinyMCE with these key features:
- an Open URL Dialog button is added to the TinyMCE toolbar
- clicking the Open URL Dialog button fires the onAction() function and opens the external dialog.html file in a modal window
- the onMessage() function is added to listen for messages from the external page
- the sendMessage() API call is used to send messages from the TinyMCE to the external page.
dialog.html
When the Open URL Dialog button is clicked in the TinyMCE editor the external dialog.html is displayed as the URL dialog. Once the page is loaded in the browser the main.js/initializeDialog() JavaScript function is called.
The key feature of the main.js/initializeDialog() function is to add a window.addEventListener() function to the page to listen for messages from the TinyMCE parent window.
The window.addEventListener() function is responsible for checking the received event.origin to verify it should be accepted and then needs to examine the event.data to determine what to do.
The external dialog.html page sends messages to the parent TinyMCE with the window.parent.postMessage call in the main.js/sendMessage() function.
Syntax: window.parent.postMessage(message, targetOrigin)message: data sent to parent windowtargetOrigin: specifies what the origin of the parent window must be to dispatch the event. Wildcard(*) indicates no preference otherwise the scheme, hostname, and port of the specified origin must match for the event to be dispatched
Communicate Flow
Conclusion
For additional examples and to see all the configuration options for a TinyMCE URL dialog, be sure to check out the official documentation.