Make WordPress Core

Ticket #31669: 31669.5.patch

File 31669.5.patch, 7.4 KB (added by iseulde, 10 years ago)
  • src/wp-includes/js/mce-view.js

     
    7777                 */
    7878                unbind: function() {
    7979                        _.each( instances, function( instance ) {
    80                                 instance.unbind();
     80                                instance.unbindAll();
    8181                        } );
    8282                },
    8383
     
    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 } ],
     
    140142                },
    141143
    142144                /**
     145                 * The opposite of `setMarkers`.
     146                 * Replaces all view markup in a given string with the view text.
     147                 *
     148                 * @param {String} content The string to scan.
     149                 *
     150                 * @return {String} The string with view text.
     151                 */
     152                reset: function( content ) {
     153                        function callback() {
     154                                return '<p>' + decodeURIComponent( arguments[1] ) + '</p>';
     155                        }
     156
     157                        content = content.replace( /<[^>]+data-wpview-text="([^"]+)"[^>]*>[\s\S]+?wpview-selection-after[^>]+>[^>]+>[^>]+>/g, callback );
     158                        content = content.replace( /<[^>]+data-wpview-marker="([^"]+)"[^>]*>[^>]+>/g, callback );
     159
     160                        return content;
     161                },
     162
     163                /**
    143164                 * Create a view instance.
    144165                 *
    145166                 * @param {String} type    The view type.
     
    298319                /**
    299320                 * Renders all view nodes tied to this view instance that are not yet rendered.
    300321                 *
     322                 * @param {String} content The content to render. Optional.
    301323                 * @param {Boolean} force Rerender all view nodes tied to this view instance.
    302324                 */
    303                 render: function( force ) {
     325                render: function( content, force ) {
     326                        if ( content != null ) {
     327                                this.content = content;
     328                        }
     329
     330                        content = this.getContent();
     331
    304332                        // If there's nothing to render an no loader needs to be shown, stop.
    305                         if ( ! this.loader && ! this.getContent() ) {
     333                        if ( ! this.loader && ! content ) {
    306334                                return;
    307335                        }
    308336
    309337                        // We're about to rerender all views of this instance, so unbind rendered views.
    310                         force && this.unbind();
     338                        force && this.unbindAll();
    311339
    312340                        // Replace any left over markers.
    313341                        this.replaceMarkers();
    314342
    315                         if ( this.getContent() ) {
    316                                 this.setContent( this.getContent(), function( editor, node ) {
    317                                         $( node ).data( 'rendered', true ).trigger( 'wp-mce-view-bind' );
     343                        if ( content ) {
     344                                this.setContent( content, function( editor, node ) {
     345                                        $( node ).data( 'rendered', true );
     346                                        this.bind.apply( this, arguments );
    318347                                }, force ? null : false );
    319348                        } else {
    320349                                this.setLoader();
     
    322351                },
    323352
    324353                /**
     354                 * Binds a given node after its content is added to the DOM.
     355                 */
     356                bind: function() {},
     357
     358                /**
     359                 * Unbinds a given node before its content is removed from the DOM.
     360                 */
     361                unbind: function() {},
     362
     363                /**
    325364                 * Unbinds all view nodes tied to this view instance.
    326365                 * Runs before their content is removed from the DOM.
    327366                 */
    328                 unbind: function() {
     367                unbindAll: function() {
    329368                        this.getNodes( function( editor, node ) {
     369                                this.unbind.apply( this, arguments );
    330370                                $( node ).trigger( 'wp-mce-view-unbind' );
    331371                        }, true );
    332372                },
     
    461501                 * @param {Boolean}  rendered Only set for (un)rendered nodes. Optional.
    462502                 */
    463503                setIframes: function( head, body, callback, rendered ) {
    464                         var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
     504                        var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,
     505                                self = this;
    465506
    466507                        this.getNodes( function( editor, node, content ) {
    467508                                // Seems Firefox needs a bit of time to insert/set the view nodes,
     
    582623                                                editor.off( 'wp-body-class-change', classChange );
    583624                                        } );
    584625
    585                                         callback && callback.apply( this, arguments );
     626                                        callback && callback.apply( self, arguments );
    586627                                }, 50 );
    587628                        }, rendered );
    588629                },
     
    663704                 * @param {HTMLElement}    node   The view node to remove.
    664705                 */
    665706                remove: function( editor, node ) {
     707                        this.unbind.call( this, editor, node, $( node ).find( '.wpview-content' ).get( 0 ) );
    666708                        $( node ).trigger( 'wp-mce-view-unbind' );
    667709                        editor.dom.remove( node );
    668710                }
     
    726768                                        }
    727769                                } );
    728770
    729                                 self.content = self.template( {
     771                                self.render( self.template( {
    730772                                        attachments: attachments,
    731773                                        columns: attrs.columns ? parseInt( attrs.columns, 10 ) : wp.media.galleryDefaults.columns
    732                                 } );
    733 
    734                                 self.render();
     774                                } ) );
    735775                        } )
    736776                        .fail( function( jqXHR, textStatus ) {
    737777                                self.setError( textStatus );
     
    752792                                } );
    753793                        }
    754794
    755                         wp.ajax.send( this.action, {
    756                                 data: {
    757                                         post_ID: postID,
    758                                         type: this.shortcode.tag,
    759                                         shortcode: this.shortcode.string()
    760                                 }
     795                        wp.ajax.post( this.action, {
     796                                post_ID: postID,
     797                                type: this.shortcode.tag,
     798                                shortcode: this.shortcode.string()
    761799                        } )
    762800                        .done( function( response ) {
    763                                 self.content = response;
    764                                 self.render();
     801                                self.render( response );
    765802                        } )
    766803                        .fail( function( response ) {
    767804                                if ( self.url ) {
  • src/wp-includes/js/tinymce/plugins/wpview/plugin.js

     
    151151                };
    152152        }
    153153
    154         // Remove the content of view wrappers from HTML string
    155         function emptyViews( content ) {
    156                 return content.replace(/<div[^>]+data-wpview-text=\"([^"]+)"[^>]*>[\s\S]+?wpview-selection-after[^>]+>(?:&nbsp;|\u00a0)*<\/p><\/div>/g, '$1' );
    157         }
    158 
    159154        // Prevent adding undo levels on changes inside a view wrapper
    160155        editor.on( 'BeforeAddUndo', function( event ) {
    161                 if ( event.lastLevel && emptyViews( event.level.content ) === emptyViews( event.lastLevel.content ) ) {
    162                         event.preventDefault();
     156                if ( event.level.content ) {
     157                        event.level.content = wp.mce.views.reset( event.level.content );
    163158                }
    164         });
     159        } );
    165160
    166161        // When the editor's content changes, scan the new content for
    167162        // matching view patterns, and transform the matches into
     
    169164        editor.on( 'BeforeSetContent', function( event ) {
    170165                var node;
    171166
    172                 if ( ! event.content ) {
     167                if ( ! event.selection ) {
     168                        wp.mce.views.unbind();
     169                }
     170
     171                if ( ! event.content || event.wphide ) {
    173172                        return;
    174173                }
    175174
     
    330329                }
    331330        });
    332331
    333         editor.on( 'PreProcess', function( event ) {
    334                 // Empty the wpview wrap nodes
    335                 tinymce.each( editor.dom.select( 'div[data-wpview-text]', event.node ), function( node ) {
    336                         node.textContent = node.innerText = '\u00a0';
    337                 });
    338     });
     332        editor.on( 'PostProcess', function( event ) {
     333                event.content = wp.mce.views.reset( event.content );
     334        } );
    339335
    340     editor.on( 'PostProcess', function( event ) {
    341                 if ( event.content ) {
    342                         event.content = event.content.replace( /<div [^>]*?data-wpview-text="([^"]*)"[^>]*>[\s\S]*?<\/div>/g, function( match, shortcode ) {
    343                                 if ( shortcode ) {
    344                                         return '<p>' + window.decodeURIComponent( shortcode ) + '</p>';
    345                                 }
    346                                 return ''; // If error, remove the view wrapper
    347                         });
    348                 }
    349         });
     336        editor.on( 'hide', function() {
     337                var data = editor.undoManager.data;
     338
     339                editor.setContent( data[ data.length - 1 ].content, { format: 'raw', wphide: true } );
     340        } );
    350341
    351342        // Excludes arrow keys, delete, backspace, enter, space bar.
    352343        // Ref: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent.keyCode