Make WordPress Core

Ticket #31412: 31412.15.patch

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

     
    6464                 * Returns the settings of a view type.
    6565                 *
    6666                 * @param {String} type The view type.
     67                 *
     68                 * @return {Function} The view constructor.
    6769                 */
    6870                get: function( type ) {
    6971                        return views[ type ];
     
    142144                 * @param {String} type    The view type.
    143145                 * @param {String} text    The textual representation of the view.
    144146                 * @param {Object} options Options.
     147                 *
     148                 * @return {wp.mce.View} The view instance.
    145149                 */
    146150                createInstance: function( type, text, options ) {
    147151                        var View = this.get( type ),
     
    163167                /**
    164168                 * Get a view instance.
    165169                 *
    166                  * @param {String} text The textual representation of the view.
     170                 * @param {(String|HTMLElement)} object The textual representation of the view or the view node.
     171                 *
     172                 * @return {wp.mce.View} The view instance.
    167173                 */
    168                 getInstance: function( text ) {
    169                         return instances[ encodeURIComponent( text ) ];
     174                getInstance: function( object ) {
     175                        if ( typeof object === 'string' ) {
     176                                return instances[ encodeURIComponent( object ) ];
     177                        }
     178
     179                        return instances[ $( object ).data( 'wpview-text' ) ];
     180                },
     181
     182                /**
     183                 * Given a view node, get the view's text.
     184                 *
     185                 * @param {HTMLElement} node The view node.
     186                 *
     187                 * @return {String} The textual representation of the view.
     188                 */
     189                getText: function( node ) {
     190                        return decodeURIComponent( $( node ).data( 'wpview-text' ) || '' );
    170191                },
    171192
    172193                /**
     
    188209                 * @param {HTMLElement}    node   The view node to update.
    189210                 */
    190211                update: function( text, editor, node ) {
    191                         var oldText = decodeURIComponent( $( node ).data( 'wpview-text' ) ),
    192                                 instance = this.getInstance( oldText );
     212                        var instance = this.getInstance( node );
    193213
    194214                        if ( instance ) {
    195215                                instance.update( text, editor, node );
     
    203223                 * @param {HTMLElement}    node   The view node to edit.
    204224                 */
    205225                edit: function( editor, node ) {
    206                         var text = decodeURIComponent( $( node ).data( 'wpview-text' ) ),
    207                                 instance = this.getInstance( text );
     226                        var instance = this.getInstance( node );
    208227
    209228                        if ( instance && instance.edit ) {
    210                                 instance.edit( text, function( text ) {
     229                                instance.edit( instance.text, function( text ) {
    211230                                        instance.update( text, editor, node );
    212231                                } );
    213232                        }
     233                },
     234
     235                /**
     236                 * Remove a given view node from the DOM.
     237                 *
     238                 * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in.
     239                 * @param {HTMLElement}    node   The view node to remove.
     240                 */
     241                remove: function( editor, node ) {
     242                        var instance = this.getInstance( node );
     243
     244                        if ( instance ) {
     245                                instance.remove( editor, node );
     246                        }
    214247                }
    215248        };
    216249
     
    218251         * A Backbone-like View constructor intended for use when rendering a TinyMCE View.
    219252         * The main difference is that the TinyMCE View is not tied to a particular DOM node.
    220253         *
    221          * @param {Object} Options.
     254         * @param {Object} options Options.
    222255         */
    223256        wp.mce.View = function( options ) {
    224257                _.extend( this, options );
     
    277310                        if ( this.getContent() ) {
    278311                                this.setContent( this.getContent(), function( editor, node ) {
    279312                                        $( node ).data( 'rendered', true );
    280                                         this.bindNodes.apply( this, arguments );
     313                                        this.bindNode.apply( this, arguments );
    281314                                }, force ? null : false );
    282315                        } else {
    283316                                this.setLoader();
     
    292325                 * @param {HTMLElement}    node        The view node.
    293326                 * @param {HTMLElement}    contentNode The view's content node.
    294327                 */
    295                 bindNodes: function( /* editor, node, contentNode */ ) {},
     328                bindNode: function( /* editor, node, contentNode */ ) {},
    296329
    297330                /**
    298331                 * Unbinds all view nodes tied to this view instance.
     
    300333                 */
    301334                unbind: function() {
    302335                        this.getNodes( function() {
    303                                 this.unbindNodes.apply( this, arguments );
     336                                this.unbindNode.apply( this, arguments );
    304337                        }, true );
    305338                },
    306339
     
    312345                 * @param {HTMLElement}    node        The view node.
    313346                 * @param {HTMLElement}    contentNode The view's content node.
    314347                 */
    315                 unbindNodes: function( /* editor, node, contentNode */ ) {},
     348                unbindNode: function( /* editor, node, contentNode */ ) {},
    316349
    317350                /**
    318351                 * Gets all the TinyMCE editor instances that support views.
     
    631664                        $( node ).data( 'rendered', false );
    632665                        editor.dom.setAttrib( node, 'data-wpview-text', encodeURIComponent( text ) );
    633666                        wp.mce.views.createInstance( this.type, text, this.match( text ).options ).render();
     667                },
     668
     669                /**
     670                 * Remove a given view node from the DOM.
     671                 *
     672                 * @param {tinymce.Editor} editor The TinyMCE editor instance the view node is in.
     673                 * @param {HTMLElement}    node   The view node to remove.
     674                 */
     675                remove: function( editor, node ) {
     676                        this.unbindNode( editor, node, $( node ).find( '.wpview-content' ).get( 0 ) );
     677                        editor.dom.remove( node );
    634678                }
    635679        } );
    636680} )( 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';