Make WordPress Core

Ticket #31412: 31412.14.patch

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

     
    163163                /**
    164164                 * Get a view instance.
    165165                 *
    166                  * @param {String} text The textual representation of the view.
     166                 * @param {(String|HTMLElement)} object The textual representation of the view or the view node.
    167167                 */
    168                 getInstance: function( text ) {
    169                         return instances[ encodeURIComponent( text ) ];
     168                getInstance: function( object ) {
     169                        if ( typeof object === 'string' ) {
     170                                return instances[ encodeURIComponent( object ) ];
     171                        }
     172
     173                        return instances[ $( object ).data( 'wpview-text' ) ];
     174                },
     175
     176                /**
     177                 * Given a view node, get the view's text.
     178                 *
     179                 * @param {HTMLElement} node The view node.
     180                 */
     181                getText: function( node ) {
     182                        return decodeURIComponent( $( node ).data( 'wpview-text' ) || '' );
    170183                },
    171184
    172185                /**
     
    188201                 * @param {HTMLElement}    node   The view node to update.
    189202                 */
    190203                update: function( text, editor, node ) {
    191                         var oldText = decodeURIComponent( $( node ).data( 'wpview-text' ) ),
    192                                 instance = this.getInstance( oldText );
     204                        var instance = this.getInstance( node );
    193205
    194206                        if ( instance ) {
    195207                                instance.update( text, editor, node );
     
    203215                 * @param {HTMLElement}    node   The view node to edit.
    204216                 */
    205217                edit: function( editor, node ) {
    206                         var text = decodeURIComponent( $( node ).data( 'wpview-text' ) ),
    207                                 instance = this.getInstance( text );
     218                        var instance = this.getInstance( node );
    208219
    209220                        if ( instance && instance.edit ) {
    210                                 instance.edit( text, function( text ) {
     221                                instance.edit( instance.text, function( text ) {
    211222                                        instance.update( text, editor, node );
    212223                                } );
    213224                        }
     225                },
     226
     227                /**
     228                 * Remove a given view node from the DOM.
     229                 *
     230                 * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in.
     231                 * @param {HTMLElement}    node   The view node to remove.
     232                 */
     233                remove: function( editor, node ) {
     234                        var instance = this.getInstance( node );
     235
     236                        if ( instance ) {
     237                                instance.remove( editor, node );
     238                        }
    214239                }
    215240        };
    216241
     
    218243         * A Backbone-like View constructor intended for use when rendering a TinyMCE View.
    219244         * The main difference is that the TinyMCE View is not tied to a particular DOM node.
    220245         *
    221          * @param {Object} Options.
     246         * @param {Object} options Options.
    222247         */
    223248        wp.mce.View = function( options ) {
    224249                _.extend( this, options );
     
    277302                        if ( this.getContent() ) {
    278303                                this.setContent( this.getContent(), function( editor, node ) {
    279304                                        $( node ).data( 'rendered', true );
    280                                         this.bindNodes.apply( this, arguments );
     305                                        this.bindNode.apply( this, arguments );
    281306                                }, force ? null : false );
    282307                        } else {
    283308                                this.setLoader();
     
    292317                 * @param {HTMLElement}    node        The view node.
    293318                 * @param {HTMLElement}    contentNode The view's content node.
    294319                 */
    295                 bindNodes: function( /* editor, node, contentNode */ ) {},
     320                bindNode: function( /* editor, node, contentNode */ ) {},
    296321
    297322                /**
    298323                 * Unbinds all view nodes tied to this view instance.
     
    300325                 */
    301326                unbind: function() {
    302327                        this.getNodes( function() {
    303                                 this.unbindNodes.apply( this, arguments );
     328                                this.unbindNode.apply( this, arguments );
    304329                        }, true );
    305330                },
    306331
     
    312337                 * @param {HTMLElement}    node        The view node.
    313338                 * @param {HTMLElement}    contentNode The view's content node.
    314339                 */
    315                 unbindNodes: function( /* editor, node, contentNode */ ) {},
     340                unbindNode: function( /* editor, node, contentNode */ ) {},
    316341
    317342                /**
    318343                 * Gets all the TinyMCE editor instances that support views.
     
    631656                        $( node ).data( 'rendered', false );
    632657                        editor.dom.setAttrib( node, 'data-wpview-text', encodeURIComponent( text ) );
    633658                        wp.mce.views.createInstance( this.type, text, this.match( text ).options ).render();
     659                },
     660
     661                /**
     662                 * Remove a given view node from the DOM.
     663                 *
     664                 * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in.
     665                 * @param {HTMLElement}    node   The view node to remove.
     666                 */
     667                remove: function( editor, node ) {
     668                        this.unbindNode( editor, node, $( node ).find( '.wpview-content' ).get( 0 ) );
     669                        editor.dom.remove( node );
    634670                }
    635671        } );
    636672} )( window, window.wp, window.jQuery );
  • src/wp-includes/js/tinymce/plugins/wpview/plugin.js

     
    7272        }
    7373
    7474        function removeView( view ) {
    75                 // TODO: trigger an event to run a clean up function.
    76                 // Maybe `jQuery( view ).trigger( 'remove' );`?
    7775                editor.undoManager.transact( function() {
    7876                        handleEnter( view );
    79                         editor.dom.remove( view );
     77                        wp.mce.views.remove( editor, view );
    8078                });
    8179        }
    8280
     
    107105                clipboard = dom.create( 'div', {
    108106                        'class': 'wpview-clipboard',
    109107                        'contenteditable': 'true'
    110                 }, decodeURIComponent( editor.dom.getAttrib( viewNode, 'data-wpview-text' ) ) );
     108                }, wp.mce.views.getText( viewNode ) );
    111109
    112110                editor.dom.select( '.wpview-body', viewNode )[0].appendChild( clipboard );
    113111
     
    348346        });
    349347
    350348        editor.on( 'PreProcess', function( event ) {
     349                wp.mce.views.unbind();
     350
    351351                // Empty the wpview wrap nodes
    352352                tinymce.each( editor.dom.select( 'div[data-wpview-text]', event.node ), function( node ) {
    353353                        node.textContent = node.innerText = '\u00a0';