0

In TinyMCE 4 init I want to use the paste_preprocess function to clean up and edit content that is pasted into TinyMCE editor. What I would like to do is have paste_preprocess make an ajax call to a PHP script that does the cleanup / editing. At the moment I'm not really getting it to work. My code at the moment:

paste_preprocess: function(plugin, args) {
    $.ajax({
        type: 'POST',
        url: '/site/ajax/preprocess.php',
        data: {content: args.content},
        dataType: 'json',
        cache: false,
        success: function(result){
            if(result.success){         
                text = result.message;
            }
        }
    })      
    args.content = text;
},

Does anyone know of a method on how to approach this or pointing me into the right direction? Thanks!

tvgemert
  • 1,436
  • 3
  • 25
  • 50
  • Obviously `args.content = text;` needs to be inside the `success` callback. That's AJAX 101 (http://stackoverflow.com/questions/6009206/what-is-ajax-and-how-does-it-work). What I don't understand is how you expect a user to accept that copy-pasting in a textbox can potentially take seconds. – Sergiu Paraschiv May 27 '14 at 12:07
  • Allright, good points but apart from that. When args.content is inside the success handler it isn't used in the paste_preprocess function. I need some way to get paste_preprocess to use the result of the AJAX call. – tvgemert May 27 '14 at 12:35
  • 2
    TinyMCE probably uses `args` immediately after calling the `paste_preprocess` function, expecting you to also populate it immediately. By nature AJAX implies out-of-order execution (the `success` callback is called some time after `paste_preprocess` returns). So you won't be able to make this work unless TinyMCE has some built-in mechanism to help you with this (promises or yet another callback maybe). And as far as I know it does not. – Sergiu Paraschiv May 27 '14 at 12:42

0 Answers0