Make WordPress Core

Ticket #31669: 31669.4.patch

File 31669.4.patch, 5.2 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
     
    139139                        return _.pluck( pieces, 'content' ).join( '' );
    140140                },
    141141
     142                reset: function( content ) {
     143                        function callback() {
     144                                return '<p>' + decodeURIComponent( arguments[1] ) + '</p>';
     145                        }
     146
     147                        content = content.replace( /<[^>]+data-wpview-text="([^"]+)"[^>]*>[\s\S]+?wpview-selection-after[^>]+>[^>]+>[^>]+>/g, callback );
     148                        content = content.replace( /<[^>]+data-wpview-marker="([^"]+)"[^>]*>[^>]+>/g, callback );
     149
     150                        return content;
     151                },
     152
    142153                /**
    143154                 * Create a view instance.
    144155                 *
     
    307318                        }
    308319
    309320                        // We're about to rerender all views of this instance, so unbind rendered views.
    310                         force && this.unbind();
     321                        force && this.unbindAll();
    311322
    312323                        // Replace any left over markers.
    313324                        this.replaceMarkers();
    314325
    315326                        if ( this.getContent() ) {
    316327                                this.setContent( this.getContent(), function( editor, node ) {
    317                                         $( node ).data( 'rendered', true ).trigger( 'wp-mce-view-bind' );
     328                                        $( node ).data( 'rendered', true );
     329                                        this.bind.apply( this, arguments );
    318330                                }, force ? null : false );
    319331                        } else {
    320332                                this.setLoader();
     
    322334                },
    323335
    324336                /**
     337                 * Binds a given node after its content is added to the DOM.
     338                 */
     339                bind: function() {},
     340
     341                /**
     342                 * Unbinds a given node before its content is removed from the DOM.
     343                 */
     344                unbind: function() {},
     345
     346                /**
    325347                 * Unbinds all view nodes tied to this view instance.
    326348                 * Runs before their content is removed from the DOM.
    327349                 */
    328                 unbind: function() {
     350                unbindAll: function() {
    329351                        this.getNodes( function( editor, node ) {
     352                                this.unbind.apply( this, arguments );
    330353                                $( node ).trigger( 'wp-mce-view-unbind' );
    331354                        }, true );
    332355                },
     
    461484                 * @param {Boolean}  rendered Only set for (un)rendered nodes. Optional.
    462485                 */
    463486                setIframes: function( head, body, callback, rendered ) {
    464                         var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
     487                        var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,
     488                                self = this;
    465489
    466490                        this.getNodes( function( editor, node, content ) {
    467491                                // Seems Firefox needs a bit of time to insert/set the view nodes,
     
    582606                                                editor.off( 'wp-body-class-change', classChange );
    583607                                        } );
    584608
    585                                         callback && callback.apply( this, arguments );
     609                                        callback && callback.apply( self, arguments );
    586610                                }, 50 );
    587611                        }, rendered );
    588612                },
     
    663687                 * @param {HTMLElement}    node   The view node to remove.
    664688                 */
    665689                remove: function( editor, node ) {
     690                        this.unbind.call( this, editor, node, $( node ).find( '.wpview-content' ).get( 0 ) );
    666691                        $( node ).trigger( 'wp-mce-view-unbind' );
    667692                        editor.dom.remove( node );
    668693                }
  • 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 ) ) {
     156                if ( event.lastLevel && wp.mce.views.reset( event.level.content ) === wp.mce.views.reset( event.lastLevel.content ) ) {
    162157                        event.preventDefault();
    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
     167                if ( ! event.selection ) {
     168                        wp.mce.views.unbind();
     169                }
     170
    172171                if ( ! event.content ) {
    173172                        return;
    174173                }
     
    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                editor.setContent( '' );
     338        } );
    350339
    351340        // Excludes arrow keys, delete, backspace, enter, space bar.
    352341        // Ref: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent.keyCode