It is not possible to recycle a TinyMCE editor that is embedded in a popup

Having a frustrating issue with the TinyMCE Editor plugin embedded within a Fancybox pop-up window.

I have a list of objects with Edit links that trigger an AJAX call to retrieve content from the server and place it in a <textarea>. A TinyMCE editor is then initialized on this <textarea>.

The problem arises when closing the pop-up, as a clean-up function is triggered. The first time this process occurs, everything works smoothly. However, subsequent attempts fail, requiring a page reload for TinyMCE to function properly again.

The result is the TinyMCE editor and textarea object hidden on the page, even if visibility/display properties are overridden in FireBug. The editor remains non-functional, with no content displayed or editable.

This issue occurs with TinyMCE version 4.2.4 for jQuery.

Below is the initialization and cleanup code for the TinyMCE editor:

hideEditor = function(){
    tinyMCE.remove('#r_description');
};
loadEditor = function(){
    // Initialization code here
}

Here's the related code snippet for the "Edit" link:

$("a.edit").on( "click", function(){
    // Edit link functionality
});

Answer №1

Attempting to address your issue, it seems you are looking to open a fancybox with a TinyMCE editor when clicking on links from a list.

I have provided an example in this JSFIDDLE. However, a workaround was necessary:

HTML

<a href='javascript:void(0);' class="editor" data-descr='test1'>editor 1</div> <br/>
<a href='javascript:void(0);' class="editor" data-descr='test2'>editor 2</div> <br/>
<a href='javascript:void(0);' class="editor" data-descr='test3'>editor 3</div> <br/>

<div id="TheFancybox" style="display: none;">  
    <textarea id="r_description"></textarea>
</div>

JS

$("a.editor").on( "click", function(){
var self = $(this);                   
   $.fancybox( "#TheFancybox", {
       afterLoad: function () { 
           /* loadEditor();
            tinyMCE.get("r_description").execCommand('mceInsertContent', false, self.data('descr')); */
            //tinyMCE initialized here not working correctly   
        },
        afterClose: function () { hideEditor(); }
    });
    //but if you init it here - it will

    $('#r_description').val(self.data('descr')); 
    loadEditor();

    return false;
});

function hideEditor(){
    tinyMCE.remove('#r_description');
};

function loadEditor(){
    tinymce.init({
        selector: "#r_description",
        mode: "none",
        setup :  function(ed) {
            var tinymce_placeholder = $('#'+ed.id);
            var attr = tinymce_placeholder.attr('placeholder');

            if (typeof attr !== 'undefined' && attr !== false) {
                var is_default = false;

                ed.on('init' , function(ed) {
                    var cont = ed.target.getContent().replace(/<p>|<\/p>/g, '');
                    if(cont.length == 0){
                        ed.target.setContent(tinymce_placeholder.attr("placeholder"));
                        cont = tinymce_placeholder.attr("placeholder");
                    }

                    is_default = (cont == tinymce_placeholder.attr("placeholder"));
                    if (!is_default){ return; }
                });

                ed.on('focus', function(ed,e) {
                    var cont = ed.target.getContent().replace(/<p>|<\/p>/g, '');
                    is_default = (cont == tinymce_placeholder.attr("placeholder"));
                    if (is_default){
                        ed.target.setContent('');
                    }
                });

                ed.on('blur', function(ed,e) {
                    var cont = ed.target.getContent();
                    if ( cont == '' ){
                        ed.target.setContent( tinymce_placeholder.attr("placeholder") );
                    }
                });
            }
        },

        // General options
        theme : "modern",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_resizing : true,
        toolbar: "link unlink | undo redo",
        relative_urls : false,
        remove_script_host : false,
        content_css: "/css/wysiwyg.css",
        menubar: false,
        statusbar: false
    });
}

Hoping this solution proves beneficial for resolving your concern.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it necessary to include async/await in a method if there is already an await keyword where it is invoked?

Here are the two methods I have written in Typescript: async getCertURL(pol: string): Promise<string> { return await Api.getData(this.apiUrl + pol + this.certEndpoint, {timeout: 60000}).then( (response) => { return response.data.certUR ...

Navigate to items within a content block with a set height

I have a <div> that has a fixed height and overflow-y: scroll. Inside this div, there is mostly a <p> tag containing a long text with some highlighting (spans with background-color and a numbered id attribute). Just to note, this is an HTML5 a ...

Exploring the Integration of Callbacks and Promises in Express.js

I'm currently developing an Express.js application where I am utilizing x-ray as a scraping tool to extract information. For each individual website I scrape, I intend to establish a unique model due to the distinct data and procedures involved in th ...

The touch events are not detected on Pixi.js when buttons are placed on a stage that has been both scaled and

Currently, I am developing a game using pixi js. In order to ensure that the game appears consistent on all screens, I have implemented the following scaling logic: this.scale = Math.max(this.viewWidth, this.viewHeight) / (2048 * this.ratio); Additionall ...

The 'palette' property is not found on the Type 'Theme' within the MUI Property

Having some trouble with MUI and TypeScript. I keep encountering this error message: Property 'palette' does not exist on type 'Theme'.ts(2339) Check out the code snippet below: const StyledTextField = styled(TextField)(({ theme }) = ...

Create a separate server session specifically for handling an ajax request?

Currently, I am working with a collection of PHP server-side scripts that manage user session state by utilizing PHP sessions extensively for authenticated users. For the client side within a mobile application and using Jquery ajax, I am striving to esta ...

Exploring ways to navigate through a JSON object/array using recursive iteration

Attempting to recursively iterate through JSON data with an unknown number of nesting levels is proving challenging. While I've managed to go one level deep, adding additional levels requires complex conditional checks that don't feel quite right ...

Is employing setTimeout a legitimate technique for circumventing a stack overflow issue when implementing callbacks?

Let's imagine a scenario where I deliberately create a complex sequence of callbacks: function handleInput(callback) { ... } function fetchData(url, callback) { ... } function processResponse(callback) { .... } function updateDatabase ...

Include personalized headers to the 'request'

I have configured my express server to proxy my API using the following setup: // Proxy api calls app.use('/api', function (req, res) { let url = config.API_HOST + req.url req.pipe(request(url)).pipe(res) }) In this instance, confi ...

I'm struggling to understand how to interpret this. The v-tab function seems to be generating a button with various properties, but I'm unsure which specific property is related to

The V-tab below generates the button known as the right one on the v-app-bar: https://i.stack.imgur.com/DzNmq.png <v-tab :to="'/demo'" active-class="text--primary" class=&quo ...

Is there a more efficient alternative to the sluggish scroll event?

Currently, I have set up a scroll event that tracks the user's position on the page and updates the navigation styling based on which section they are viewing. However, the calculation I'm using during scrolling is quite resource-intensive and ca ...

Why is my jQuery $.ajax success function not providing any results?

When checking the Network tab in Chrome, I noticed that the correct data (action, username, password) is being sent, but the message is not returning to $('#return_login'). Can anyone spot what might be wrong with my code? Below is the jQuery co ...

What is the method to alter the color of an SVG source within an image tag?

I am currently working on a component that involves changing the color of an SVG icon from black to white when a color prop is given. export class CategoryIconComponent extends React.Component { static displayName = 'CategoryIcon'; state = ...

What can possibly be the reason why the HttpClient in Angular 5 is not functioning properly

I am attempting to retrieve data from the server and display it in a dropdown menu, but I am encountering an error while fetching the data. Here is my code: https://stackblitz.com/edit/angular-xsydti ngOnInit(){ console.log('init') this ...

Struggling with making react-hook-form correctly validate an email address

I've been struggling for a long time to make this validation work correctly, but it seems impossible. I even added some text at the bottom to display an error message related to the email, but it always shows no error, regardless of the situation. Ed ...

Encountering Challenges when Implementing Next.js with Jest

When attempting to run a test in next.js using jest, an error keeps appearing: The issue may be due to the wrong test environment being used. Refer to https://jestjs.io/docs/configuration#testenvironment-string for more information. Consider utilizing the ...

Is there a way to link dynamic server fields with VueJS?

How can I bind data posted by the server with Vue.js in order to display the data instead of field names? <script> module.exports = { data: function () { return { filedNameFromServer: ['{{filed1}}' ...

The radio button fails to trigger the setState function in React

In my React program, I have implemented a method to update the state and modify a specific value for a Radio button: The state : class App extends Component { constructor(props) { super(props); this.state = { checked: [], expanded: [], ...

What could be the reason for receiving an undefined user ID when trying to pass it through my URL?

Currently, I am in the process of constructing a profile page and aiming to display authenticated user data on it. The API call functions correctly with the user's ID, and manually entering the ID into the URL on the front end also works. However, wh ...

Triggering a system context menu through a longpress gesture on the MobileFirst iOS app is a

After experimenting with our MobileFirst iOS app, I noticed that if you hold down on any anchor links for more than 2 or 3 seconds, iOS will bring up a built-in menu displaying the internal path of the current HTML page. I'm unsure whether this behav ...