Make WordPress Core

Ticket #31669: 31669.10.patch

File 31669.10.patch, 7.0 KB (added by iseulde, 9 years ago)
  • src/wp-includes/js/mce-view.js

     
    8787                 * and creates a new instance for every match.
    8888                 *
    8989                 * @param {String} content The string to scan.
     90                 *
     91                 * @return {String} The string with markers.
    9092                 */
    9193                setMarkers: function( content ) {
    9294                        var pieces = [ { content: content } ],
     
    298300                /**
    299301                 * Renders all view nodes tied to this view instance that are not yet rendered.
    300302                 *
     303                 * @param {String} content The content to render. Optional.
    301304                 * @param {Boolean} force Rerender all view nodes tied to this view instance.
    302305                 */
    303                 render: function( force ) {
     306                render: function( content, force ) {
     307                        if ( content != null ) {
     308                                this.content = content;
     309                        }
     310
     311                        content = this.getContent();
     312
    304313                        // If there's nothing to render an no loader needs to be shown, stop.
    305                         if ( ! this.loader && ! this.getContent() ) {
     314                        if ( ! this.loader && ! content ) {
    306315                                return;
    307316                        }
    308317
     
    312321                        // Replace any left over markers.
    313322                        this.replaceMarkers();
    314323
    315                         if ( this.getContent() ) {
    316                                 this.setContent( this.getContent(), function( editor, node ) {
    317                                         $( node ).data( 'rendered', true ).trigger( 'wp-mce-view-bind' );
     324                        if ( content ) {
     325                                this.setContent( content, function( editor, node ) {
     326                                        $( node ).data( 'rendered', true );
     327                                        this.bindNode.apply( this, arguments );
    318328                                }, force ? null : false );
    319329                        } else {
    320330                                this.setLoader();
     
    322332                },
    323333
    324334                /**
     335                 * Binds a given node after its content is added to the DOM.
     336                 */
     337                bindNode: function() {},
     338
     339                /**
     340                 * Unbinds a given node before its content is removed from the DOM.
     341                 */
     342                unbindNode: function() {},
     343
     344                /**
    325345                 * Unbinds all view nodes tied to this view instance.
    326346                 * Runs before their content is removed from the DOM.
    327347                 */
    328348                unbind: function() {
    329349                        this.getNodes( function( editor, node ) {
     350                                this.unbindNode.apply( this, arguments );
    330351                                $( node ).trigger( 'wp-mce-view-unbind' );
    331352                        }, true );
    332353                },
     
    461482                 * @param {Boolean}  rendered Only set for (un)rendered nodes. Optional.
    462483                 */
    463484                setIframes: function( head, body, callback, rendered ) {
    464                         var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
     485                        var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,
     486                                self = this;
    465487
    466488                        this.getNodes( function( editor, node, content ) {
    467489                                // Seems Firefox needs a bit of time to insert/set the view nodes,
     
    582604                                                editor.off( 'wp-body-class-change', classChange );
    583605                                        } );
    584606
    585                                         callback && callback.apply( this, arguments );
     607                                        callback && callback.apply( self, arguments );
    586608                                }, 50 );
    587609                        }, rendered );
    588610                },
     
    663685                 * @param {HTMLElement}    node   The view node to remove.
    664686                 */
    665687                remove: function( editor, node ) {
     688                        this.unbindNode.call( this, editor, node, $( node ).find( '.wpview-content' ).get( 0 ) );
    666689                        $( node ).trigger( 'wp-mce-view-unbind' );
    667690                        editor.dom.remove( node );
    668691                }
     
    726749                                        }
    727750                                } );
    728751
    729                                 self.content = self.template( {
     752                                self.render( self.template( {
    730753                                        attachments: attachments,
    731754                                        columns: attrs.columns ? parseInt( attrs.columns, 10 ) : wp.media.galleryDefaults.columns
    732                                 } );
    733 
    734                                 self.render();
     755                                } ) );
    735756                        } )
    736757                        .fail( function( jqXHR, textStatus ) {
    737758                                self.setError( textStatus );
     
    752773                                } );
    753774                        }
    754775
    755                         wp.ajax.send( this.action, {
    756                                 data: {
    757                                         post_ID: postID,
    758                                         type: this.shortcode.tag,
    759                                         shortcode: this.shortcode.string()
    760                                 }
     776                        wp.ajax.post( this.action, {
     777                                post_ID: postID,
     778                                type: this.shortcode.tag,
     779                                shortcode: this.shortcode.string()
    761780                        } )
    762781                        .done( function( response ) {
    763                                 self.content = response;
    764                                 self.render();
     782                                self.render( response );
    765783                        } )
    766784                        .fail( function( response ) {
    767785                                if ( self.url ) {
  • src/wp-includes/js/tinymce/plugins/wpview/plugin.js

     
    44 * WordPress View plugin.
    55 */
    66tinymce.PluginManager.add( 'wpview', function( editor ) {
    7         var selected,
     7        var $ = editor.$,
     8                selected,
    89                Env = tinymce.Env,
    910                VK = tinymce.util.VK,
    1011                TreeWalker = tinymce.dom.TreeWalker,
     
    153154
    154155        // Remove the content of view wrappers from HTML string
    155156        function emptyViews( content ) {
    156                 return content.replace(/<div[^>]+data-wpview-text=\"([^"]+)"[^>]*>[\s\S]+?wpview-selection-after[^>]+>(?:&nbsp;|\u00a0)*<\/p><\/div>/g, '$1' );
     157                function callback() {
     158                        return '<p>' + decodeURIComponent( arguments[1] ) + '</p>';
     159                }
     160
     161                content = content.replace( /<div[^>]+data-wpview-text="([^"]+)"[^>]*>[\s\S]+?wpview-selection-after[^>]+>[^<]+<\/p><\/div>/g, callback );
     162                content = content.replace( /<p[^>]+data-wpview-marker="([^"]+)"[^>]*>[^<]+<\/p>/g, callback );
     163
     164                return content;
    157165        }
    158166
    159167        // Prevent adding undo levels on changes inside a view wrapper
    160168        editor.on( 'BeforeAddUndo', function( event ) {
    161                 if ( event.lastLevel && emptyViews( event.level.content ) === emptyViews( event.lastLevel.content ) ) {
    162                         event.preventDefault();
     169                if ( event.level.content ) {
     170                        event.level.content = emptyViews( event.level.content );
    163171                }
    164172        });
    165173
     
    169177        editor.on( 'BeforeSetContent', function( event ) {
    170178                var node;
    171179
     180                if ( ! event.selection ) {
     181                        wp.mce.views.unbind();
     182                }
     183
    172184                if ( ! event.content ) {
    173185                        return;
    174186                }
     
    335347                }
    336348        });
    337349
    338         editor.on( 'PreProcess', function( event ) {
    339                 // Empty the wpview wrap nodes
    340                 tinymce.each( editor.dom.select( 'div[data-wpview-text]', event.node ), function( node ) {
    341                         node.textContent = node.innerText = '\u00a0';
    342                 });
    343     });
     350        function resetViews( context ) {
     351                tinymce.each( [
     352                        'data-wpview-text',
     353                        'data-wpview-marker'
     354                ], function( attr ) {
     355                        $( '[' + attr + ']', context ).each( function() {
     356                                var $this = $( this );
     357
     358                                $this.replaceWith(
     359                                        $( document.createElement( 'P' ) ).text(
     360                                                decodeURIComponent( $this.attr( attr ) )
     361                                        )
     362                                );
     363                        } );
     364                } );
     365        }
    344366
    345     editor.on( 'PostProcess', function( event ) {
    346                 if ( event.content ) {
    347                         event.content = event.content.replace( /<div [^>]*?data-wpview-text="([^"]*)"[^>]*>[\s\S]*?<\/div>/g, function( match, shortcode ) {
    348                                 if ( shortcode ) {
    349                                         return '<p>' + window.decodeURIComponent( shortcode ) + '</p>';
    350                                 }
    351                                 return ''; // If error, remove the view wrapper
    352                         });
    353                 }
     367        editor.on( 'PreProcess', function( event ) {
     368                // Replace the view nodes with their text in the DOM clone.
     369                resetViews( event.node );
     370    }, true );
     371
     372        editor.on( 'hide', function() {
     373                // Replace the view nodes with their text directly in the editor body.
     374                wp.mce.views.unbind();
     375                resetViews();
    354376        });
    355377
    356378        // Excludes arrow keys, delete, backspace, enter, space bar.