Make WordPress Core

Ticket #31669: 31669.diff

File 31669.diff, 9.1 KB (added by mattheu, 10 years ago)
  • src/wp-includes/js/mce-view.js

    diff --git a/src/wp-includes/js/mce-view.js b/src/wp-includes/js/mce-view.js
    index 9817454..1c939ec 100644
    a b window.wp = window.wp || {}; 
    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 } ],
    window.wp = window.wp || {}; 
    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
    window.wp = window.wp || {}; 
    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();
    window.wp = window.wp || {}; 
    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                },
    window.wp = window.wp || {}; 
    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
     486                        var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,
     487                                self = this;
    465488
    466489                        this.getNodes( function( editor, node, content ) {
    467                                 // Seems Firefox needs a bit of time to insert/set the view nodes,
    468                                 // or the iframe will fail especially when switching Text => Visual.
    469                                 setTimeout( function() {
    470                                         var dom = editor.dom,
    471                                                 styles = '',
    472                                                 bodyClasses = editor.getBody().className || '',
    473                                                 iframe, iframeDoc, observer, i;
    474 
    475                                         tinymce.each( dom.$(
    476                                                 'link[rel="stylesheet"]',
    477                                                 editor.getDoc().getElementsByTagName( 'head' )[0]
    478                                         ), function( link ) {
    479                                                 if (
    480                                                         link.href &&
    481                                                         link.href.indexOf( 'skins/lightgray/content.min.css' ) === -1 &&
    482                                                         link.href.indexOf( 'skins/wordpress/wp-content.css' ) === -1
    483                                                 ) {
    484                                                         styles += dom.getOuterHTML( link );
    485                                                 }
    486                                         } );
    487490
    488                                         content.innerHTML = '';
     491                                console.log( head );
     492
     493                                var dom = editor.dom,
     494                                        styles = '',
     495                                        bodyClasses = editor.getBody().className || '',
     496                                        iframe, iframeDoc, observer, i;
     497
     498                                if ( ! head ) {
     499                                        head = editor.getDoc().getElementsByTagName( 'head' )[0].innerHTML
     500                                }
     501
     502                                tinymce.each( dom.$( 'link[rel="stylesheet"]', head ), function( link ) {
     503                                        if ( link.href && link.href.indexOf( 'skins/lightgray/content.min.css' ) === -1 &&
     504                                                link.href.indexOf( 'skins/wordpress/wp-content.css' ) === -1 ) {
     505
     506                                                styles += dom.getOuterHTML( link );
     507                                        }
     508                                } );
    489509
     510                                content.innerHTML = '';
     511
     512                                // Seems the browsers need a bit of time to insert/set the view nodes,
     513                                // or the iframe will fail especially when switching Text => Visual.
     514                                setTimeout( function() {
    490515                                        iframe = dom.add( content, 'iframe', {
    491516                                                /* jshint scripturl: true */
    492517                                                src: tinymce.Env.ie ? 'javascript:""' : '',
    window.wp = window.wp || {}; 
    582607                                                editor.off( 'wp-body-class-change', classChange );
    583608                                        } );
    584609
    585                                         callback && callback.apply( this, arguments );
     610                                        callback && callback.apply( self, arguments );
    586611                                }, 50 );
    587612                        }, rendered );
    588613                },
    window.wp = window.wp || {}; 
    663688                 * @param {HTMLElement}    node   The view node to remove.
    664689                 */
    665690                remove: function( editor, node ) {
     691                        this.unbindNode.call( this, editor, node, $( node ).find( '.wpview-content' ).get( 0 ) );
    666692                        $( node ).trigger( 'wp-mce-view-unbind' );
    667693                        editor.dom.remove( node );
    668694                }
    window.wp = window.wp || {}; 
    726752                                        }
    727753                                } );
    728754
    729                                 self.content = self.template( {
     755                                self.render( self.template( {
    730756                                        attachments: attachments,
    731757                                        columns: attrs.columns ? parseInt( attrs.columns, 10 ) : wp.media.galleryDefaults.columns
    732                                 } );
    733 
    734                                 self.render();
     758                                } ) );
    735759                        } )
    736760                        .fail( function( jqXHR, textStatus ) {
    737761                                self.setError( textStatus );
    window.wp = window.wp || {}; 
    752776                                } );
    753777                        }
    754778
    755                         wp.ajax.send( this.action, {
    756                                 data: {
    757                                         post_ID: postID,
    758                                         type: this.shortcode.tag,
    759                                         shortcode: this.shortcode.string()
    760                                 }
     779                        wp.ajax.post( this.action, {
     780                                post_ID: postID,
     781                                type: this.shortcode.tag,
     782                                shortcode: this.shortcode.string()
    761783                        } )
    762784                        .done( function( response ) {
    763                                 self.content = response;
    764                                 self.render();
     785                                self.render( response );
    765786                        } )
    766787                        .fail( function( response ) {
    767788                                if ( self.url ) {
  • src/wp-includes/js/tinymce/plugins/wpview/plugin.js

    diff --git a/src/wp-includes/js/tinymce/plugins/wpview/plugin.js b/src/wp-includes/js/tinymce/plugins/wpview/plugin.js
    index 9a8b650..657317e 100644
    a b  
    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,
    tinymce.PluginManager.add( 'wpview', function( editor ) { 
    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                content = content.replace( /<div[^>]+data-wpview-text="([^"]+)"[^>]*>[\s\S]+?wpview-selection-after[^>]+>[^<>]*<\/p>\s*<\/div>/g, function( all, match ) {
     158                        return '<p>' + window.decodeURIComponent( match ) + '</p>';
     159                });
     160
     161                return content.replace( / data-wpview-marker="[^"]+"/g, '' );
    157162        }
    158163
    159164        // Prevent adding undo levels on changes inside a view wrapper
    160165        editor.on( 'BeforeAddUndo', function( event ) {
    161                 if ( event.lastLevel && emptyViews( event.level.content ) === emptyViews( event.lastLevel.content ) ) {
    162                         event.preventDefault();
     166                if ( event.level.content ) {
     167                        event.level.content = emptyViews( event.level.content );
    163168                }
    164169        });
    165170
    tinymce.PluginManager.add( 'wpview', function( editor ) { 
    169174        editor.on( 'BeforeSetContent', function( event ) {
    170175                var node;
    171176
     177                if ( ! event.selection ) {
     178                        wp.mce.views.unbind();
     179                }
     180
    172181                if ( ! event.content ) {
    173182                        return;
    174183                }
    tinymce.PluginManager.add( 'wpview', function( editor ) { 
    339348                }
    340349        });
    341350
    342         editor.on( 'PreProcess', function( event ) {
    343                 // Empty the wpview wrap nodes
    344                 tinymce.each( editor.dom.select( 'div[data-wpview-text]', event.node ), function( node ) {
    345                         node.textContent = node.innerText = '\u00a0';
     351        function resetViews( rootNode ) {
     352                // Replace view nodes
     353                $( 'div[data-wpview-text]', rootNode ).each( function( i, node ) {
     354                        var $node = $( node ),
     355                                text = window.decodeURIComponent( $node.attr( 'data-wpview-text' ) || '' );
     356
     357                        if ( text && node.parentNode ) {
     358                                $node.replaceWith( $( editor.dom.create('p') ).text( text ) );
     359                        }
    346360                });
    347     });
    348361
    349     editor.on( 'PostProcess', function( event ) {
    350                 if ( event.content ) {
    351                         event.content = event.content.replace( /<div [^>]*?data-wpview-text="([^"]*)"[^>]*>[\s\S]*?<\/div>/g, function( match, shortcode ) {
    352                                 if ( shortcode ) {
    353                                         return '<p>' + window.decodeURIComponent( shortcode ) + '</p>';
    354                                 }
    355                                 return ''; // If error, remove the view wrapper
    356                         });
    357                 }
     362                // Remove marker attributes
     363                $( 'p[data-wpview-marker]', rootNode ).attr( 'data-wpview-marker', null );
     364        }
     365
     366        editor.on( 'PreProcess', function( event ) {
     367                // Replace the view nodes with their text in the DOM clone.
     368                resetViews( event.node );
     369    }, true );
     370
     371        editor.on( 'hide', function() {
     372                // Replace the view nodes with their text directly in the editor body.
     373                wp.mce.views.unbind();
     374                resetViews( editor.getBody() );
    358375        });
    359376
    360377        // Excludes arrow keys, delete, backspace, enter, space bar.