Make WordPress Core

Ticket #28195: 28195.7.patch

File 28195.7.patch, 7.3 KB (added by iseulde, 11 years ago)
  • src/wp-includes/js/mce-view.js

     
    88// Ensure the global `wp` object exists.
    99window.wp = window.wp || {};
    1010
    11 (function($){
     11( function( $ ) {
     12        'use strict';
     13
    1214        var views = {},
    1315                instances = {},
    1416                media = wp.media,
     
    637639        /**
    638640         * TinyMCE handler for the embed shortcode
    639641         */
    640         wp.mce.views.register( 'embed', {
    641                 shortcode: 'embed',
    642                 View: _.extend( {}, wp.media.mixin, {
    643                         template: media.template( 'editor-embed' ),
    644                         initialize: function( options ) {
    645                                 this.players = [];
    646                                 this.content = options.content;
    647                                 this.parsed = false;
    648                                 this.shortcode = options.shortcode;
    649                                 _.bindAll( this, 'setHtml', 'setNode', 'fetch' );
    650                                 $( this ).on( 'ready', this.setNode );
    651                         },
    652                         unbind: function() {
    653                                 var self = this;
    654                                 _.each( this.players, function ( player ) {
    655                                         player.pause();
    656                                         self.removePlayer( player );
    657                                 } );
    658                                 this.players = [];
    659                         },
    660                         setNode: function ( e, node ) {
    661                                 this.node = node;
    662                                 if ( this.parsed ) {
    663                                         this.parseMediaShortcodes();
    664                                 } else {
    665                                         this.fetch();
     642        wp.mce.embedView = _.extend( {}, wp.media.mixin, {
     643                template: media.template( 'editor-embed' ),
     644                initialize: function( options ) {
     645                        this.players = [];
     646                        this.content = options.content;
     647                        this.parsed = false;
     648                        this.original = options.url || options.shortcode.string();
     649                        if ( options.url ) {
     650                                this.shortcode = '[embed]' + options.url + '[/embed]';
     651                        } else {
     652                                this.shortcode = options.shortcode.string();
     653                        }
     654                        _.bindAll( this, 'setHtml', 'setNode', 'fetch' );
     655                        $( this ).on( 'ready', this.setNode );
     656                },
     657                unbind: function() {
     658                        var self = this;
     659                        _.each( this.players, function ( player ) {
     660                                player.pause();
     661                                self.removePlayer( player );
     662                        } );
     663                        this.players = [];
     664                },
     665                setNode: function ( e, node ) {
     666                        this.node = node;
     667                        if ( this.parsed ) {
     668                                this.setHtml( this.parsed );
     669                                this.parseMediaShortcodes();
     670                        } else {
     671                                this.fetch();
     672                        }
     673                },
     674                fetch: function () {
     675                        var self = this;
     676                        wp.ajax.send( 'parse-embed', {
     677                                data: {
     678                                        post_ID: $( '#post_ID' ).val(),
     679                                        content: this.shortcode
    666680                                }
    667                         },
    668                         fetch: function () {
    669                                 wp.ajax.send( 'parse-embed', {
    670                                         data: {
    671                                                 post_ID: $( '#post_ID' ).val(),
    672                                                 content: this.shortcode.string()
     681                        } ).done( function( content ) {
     682                                self.parsed = content;
     683                                self.setHtml( content );
     684                        } );
     685                },
     686                /* jshint scripturl: true */
     687                setHtml: function ( content ) {
     688                        var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,
     689                                html, $html, scripts, observer, iframe, iframeWin, iframeDoc,
     690                                test = '<a href',
     691                                dom = tinymce.activeEditor.dom;
     692
     693                        if ( content.substring( 0, test.length ) === test ) {
     694                                dom.replace( dom.create( 'P', null, this.original ), this.node );
     695                        } else {
     696                                html = $.trim( this.template( { content: content } ) );
     697                                $html = $( html );
     698                                scripts = $html.find( 'script' ).add( $html.filter( 'script' ) );
     699
     700                                if ( scripts.length ) {
     701
     702                                        iframe = dom.create( 'IFRAME', {
     703                                                src: 'javascript:""',
     704                                                frameBorder: '0',
     705                                                allowTransparency: 'true',
     706                                                style: {
     707                                                        width: '100%',
     708                                                        display: 'block'
     709                                                }
     710                                        } );
     711
     712                                        this.node.insertBefore( dom.createFragment(
     713                                                '<div class="toolbar">' +
     714                                                        '<div class="dashicons dashicons-no-alt remove"></div>' +
     715                                                '</div>' +
     716                                                '<div class="wpview-overlay"></div>'
     717                                        ), this.node.lastChild );
     718                                        this.node.insertBefore( iframe, this.node.lastChild );
     719
     720                                        iframeWin = iframe.contentWindow;
     721                                        iframeDoc = iframeWin.document;
     722
     723                                        iframeWin.resize = function() {
     724                                                window.parent.postMessage( {
     725                                                        height: $( iframeDoc ).height()
     726                                                }, '*' );
     727                                        };
     728
     729                                        window.addEventListener( 'message', function ( event ) {
     730                                                if ( event.data.height ) {
     731                                                        $( iframe ).height( event.data.height );
     732                                                }
     733                                        }, false );
     734
     735                                        iframeDoc.open();
     736                                        iframeDoc.write( '<!DOCTYPE html><html><head><meta charset="utf-8"></head><body onload="resize();">' + html + '</body></html>' );
     737                                        iframeDoc.close();
     738
     739                                        if ( MutationObserver ) {
     740                                                observer = new MutationObserver( _.debounce( function() {
     741                                                        iframeWin.resize();
     742                                                }, 500 ) ).observe( iframeDoc.body, {
     743                                                        attributes: true,
     744                                                        childList: true,
     745                                                        subtree: true
     746                                                } );
    673747                                        }
    674                                 } ).done( this.setHtml );
    675                         },
    676                         setHtml: function ( content ) {
    677                                 this.parsed = content;
    678                                 $( this.node ).html( this.getHtml() );
    679                                 this.parseMediaShortcodes();
    680                         },
    681                         parseMediaShortcodes: function () {
    682                                 var self = this;
    683                                 $( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) {
    684                                         self.players.push( new MediaElementPlayer( element, self.mejsSettings ) );
    685                                 } );
    686                         },
    687                         getHtml: function() {
    688                                 if ( ! this.parsed ) {
    689                                         return '';
     748                                } else {
     749                                        this.node.insertBefore( dom.createFragment( html ), this.node.lastChild );
    690750                                }
    691                                 return this.template({ content: this.parsed });
    692751                        }
    693                 } ),
     752
     753                        this.parseMediaShortcodes();
     754                },
     755                parseMediaShortcodes: function () {
     756                        var self = this;
     757                        $( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) {
     758                                self.players.push( new MediaElementPlayer( element, self.mejsSettings ) );
     759                        } );
     760                },
     761                getHtml: function() {
     762                        return '';
     763                }
     764        } );
     765
     766        wp.mce.views.register( 'embed', {
     767                shortcode: 'embed',
     768                View: wp.mce.embedView,
     769                edit: function() {}
     770        } );
     771
     772        wp.mce.views.register( 'embedURL', {
     773                shortcode: 'embed',
     774                toView: function( content ) {
     775                        var re = /^<p>\s*(https?:\/\/[^\s"]+)\s*<\/p>$/gim,
     776                                match = re.exec( content );
     777
     778                        if ( ! match ) {
     779                                return;
     780                        }
     781
     782                        return {
     783                                index: match.index,
     784                                content: match[0],
     785                                options: {
     786                                        url: match[1]
     787                                }
     788                        };
     789                },
     790                View: wp.mce.embedView,
    694791                edit: function() {}
    695792        } );
    696793
  • src/wp-includes/js/tinymce/plugins/wpview/plugin.js

     
    158158        // matching view patterns, and transform the matches into
    159159        // view wrappers.
    160160        editor.on( 'BeforeSetContent', function( event ) {
     161                var node;
     162
    161163                if ( ! event.content ) {
    162164                        return;
    163165                }
     
    166168                        wp.mce.views.unbind( editor );
    167169                }
    168170
    169                 event.content = wp.mce.views.toViews( event.content );
    170         });
     171                node = editor.selection.getNode();
    171172
    172         editor.on( 'PastePreProcess', function( event ) {
    173                 if ( event.content.match( /^\s*(https?:\/\/[^\s"]+)\s*$/im ) ) {
    174                         event.content = '[embed]' + event.content + '[/embed]';
     173                // If a url is inserted in an empty paragraph, wrap the url in p tags so it's detected by wp.mce.views.
     174                if ( node.nodeName === 'P' && editor.dom.isEmpty( node ) && event.content.match( /^\s*(https?:\/\/[^\s"]+)\s*$/im ) ) {
     175                        event.content = '<p>' + event.content + '</p>';
    175176                }
    176         } );
     177
     178                event.content = wp.mce.views.toViews( event.content );
     179        });
    177180
    178181        // When the editor's content has been updated and the DOM has been
    179182        // processed, render the views in the document.