Make WordPress Core

Ticket #28195: 28195.9.patch

File 28195.9.patch, 12.0 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,
     
    2729         */
    2830        wp.mce.View = function( options ) {
    2931                options = options || {};
     32                this.type = options.type;
    3033                _.extend( this, _.pick( options, viewOptions ) );
    3134                this.initialize.apply( this, arguments );
    3235        };
     
    3538                initialize: function() {},
    3639                getHtml: function() {},
    3740                render: function() {
    38                         var html = this.getHtml();
     41                        var html =
     42                                '<div class="toolbar">' +
     43                                        '<div class="dashicons dashicons-no-alt remove"></div>' +
     44                                        ( _.isFunction( views[ this.type ].edit ) ? '<div class="dashicons dashicons-edit edit"></div>' : '' ) +
     45                                '</div>' +
     46                                '<div class="wpview-content">' +
     47                                        this.getHtml() +
     48                                '</div>' +
     49                                ( this.overlay ? '<div class="wpview-overlay"></div>' : '' ) +
     50                                // The <ins> is used to mark the end of the wrapper div.
     51                                // Needed when comparing the content as string for preventing extra undo levels.
     52                                '<ins data-wpview-end="1"></ins>';
     53
    3954                        // Search all tinymce editor instances and update the placeholders
    4055                        _.each( tinymce.editors, function( editor ) {
    4156                                var self = this;
    4257                                if ( editor.plugins.wpview ) {
    43                                         $( editor.getDoc() ).find( '[data-wpview-text="' + this.encodedText + '"]' ).each( function ( i, element ) {
    44                                                 var node = $( element );
    45                                                 // The <ins> is used to mark the end of the wrapper div. Needed when comparing
    46                                                 // the content as string for preventing extra undo levels.
    47                                                 node.html( html ).append( '<ins data-wpview-end="1"></ins>' );
     58                                        $( editor.getBody() ).find( '[data-wpview-text="' + this.encodedText + '"]' ).each( function ( i, element ) {
     59                                                editor.dom.setHTML( element, html );
    4860                                                $( self ).trigger( 'ready', element );
    4961                                        });
    5062                                }
    5163                        }, this );
    5264                },
    53                 unbind: function() {}
     65                unbind: function() {},
     66                setContent: function( node, html ) {
     67                        var dom = tinymce.activeEditor.dom,
     68                                parent = dom.select( '.wpview-content', node )[0];
     69
     70                        if ( _.isString( html ) ) {
     71                                dom.setHTML( parent, html );
     72                        } else {
     73                                parent.appendChild( html );
     74                        }
     75                }
    5476        } );
    5577
    5678        // take advantage of the Backbone extend method
     
    209231
    210232                        if ( ! wp.mce.views.getInstance( encodedText ) ) {
    211233                                viewOptions = options;
     234                                viewOptions.type = viewType;
    212235                                viewOptions.encodedText = encodedText;
    213236                                instance = new view.View( viewOptions );
    214237                                instances[ encodedText ] = instance;
     
    244267                        if ( ! instance ) {
    245268                                result = view.toView( text );
    246269                                viewOptions = result.options;
     270                                viewOptions.type = view.type;
    247271                                viewOptions.encodedText = encodedText;
    248272                                instance = new view.View( viewOptions );
    249273                                instances[ encodedText ] = instance;
     
    361385                loaded: false,
    362386
    363387                View: _.extend( {}, wp.media.mixin, {
     388                        overlay: true,
     389
    364390                        initialize: function( options ) {
    365391                                this.players = [];
    366392                                this.shortcode = options.shortcode;
     
    508534                state: ['playlist-edit', 'video-playlist-edit'],
    509535                View: _.extend( {}, wp.media.mixin, {
    510536                        template:  media.template( 'editor-playlist' ),
     537                        overlay: true,
    511538
    512539                        initialize: function( options ) {
    513540                                this.players = [];
     
    634661        /**
    635662         * TinyMCE handler for the embed shortcode
    636663         */
    637         wp.mce.views.register( 'embed', {
    638                 View: _.extend( {}, wp.media.mixin, {
    639                         template: media.template( 'editor-embed' ),
    640                         initialize: function( options ) {
    641                                 this.players = [];
    642                                 this.content = options.content;
    643                                 this.parsed = false;
    644                                 this.shortcode = options.shortcode;
    645                                 _.bindAll( this, 'setHtml', 'setNode', 'fetch' );
    646                                 $( this ).on( 'ready', this.setNode );
    647                         },
    648                         unbind: function() {
    649                                 var self = this;
    650                                 _.each( this.players, function ( player ) {
    651                                         player.pause();
    652                                         self.removePlayer( player );
    653                                 } );
    654                                 this.players = [];
    655                         },
    656                         setNode: function ( e, node ) {
    657                                 this.node = node;
    658                                 if ( this.parsed ) {
    659                                         this.parseMediaShortcodes();
    660                                 } else {
    661                                         this.fetch();
    662                                 }
    663                         },
    664                         fetch: function () {
    665                                 wp.ajax.send( 'parse-embed', {
    666                                         data: {
    667                                                 post_ID: $( '#post_ID' ).val(),
    668                                                 content: this.shortcode.string()
    669                                         }
    670                                 } ).done( this.setHtml );
    671                         },
    672                         setHtml: function ( content ) {
    673                                 this.parsed = content;
    674                                 $( this.node ).html( this.getHtml() );
     664        wp.mce.embedView = _.extend( {}, wp.media.mixin, {
     665                overlay: true,
     666                initialize: function( options ) {
     667                        this.players = [];
     668                        this.content = options.content;
     669                        this.parsed = false;
     670                        this.original = options.url || options.shortcode.string();
     671                        if ( options.url ) {
     672                                this.shortcode = '[embed]' + options.url + '[/embed]';
     673                        } else {
     674                                this.shortcode = options.shortcode.string();
     675                        }
     676                        _.bindAll( this, 'setHtml', 'setNode', 'fetch' );
     677                        $( this ).on( 'ready', this.setNode );
     678                },
     679                unbind: function() {
     680                        var self = this;
     681                        _.each( this.players, function ( player ) {
     682                                player.pause();
     683                                self.removePlayer( player );
     684                        } );
     685                        this.players = [];
     686                },
     687                setNode: function ( e, node ) {
     688                        this.node = node;
     689                        if ( this.parsed ) {
     690                                this.setHtml( this.parsed );
    675691                                this.parseMediaShortcodes();
    676                         },
    677                         parseMediaShortcodes: function () {
    678                                 var self = this;
    679                                 $( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) {
    680                                         self.players.push( new MediaElementPlayer( element, self.mejsSettings ) );
    681                                 } );
    682                         },
    683                         getHtml: function() {
    684                                 if ( ! this.parsed ) {
    685                                         return '';
     692                        } else {
     693                                this.fetch();
     694                        }
     695                },
     696                fetch: function () {
     697                        var self = this;
     698                        wp.ajax.send( 'parse-embed', {
     699                                data: {
     700                                        post_ID: $( '#post_ID' ).val(),
     701                                        content: this.shortcode
     702                                        }
     703                        } ).done( function( content ) {
     704                                self.parsed = content;
     705                                self.setHtml( content );
     706                        } );
     707                },
     708                /* jshint scripturl: true */
     709                setHtml: function ( content ) {
     710                        var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,
     711                                $content, scripts, iframe, iframeWin, iframeDoc,
     712                                test = '<a href',
     713                                dom = tinymce.activeEditor.dom;
     714
     715                        if ( content.substring( 0, test.length ) === test ) {
     716                                dom.replace( dom.create( 'P', null, this.original ), this.node );
     717                        } else {
     718                                $content = $( $.trim( content ) );
     719                                scripts = $content.find( 'script' ).add( $content.filter( 'script' ) );
     720
     721                                if ( scripts.length ) {
     722                                        iframe = dom.create( 'IFRAME', {
     723                                                src: 'javascript:""',
     724                                                frameBorder: '0',
     725                                                allowTransparency: 'true',
     726                                                style: {
     727                                                        width: '100%',
     728                                                        display: 'block'
     729                                                }
     730                                        } );
     731
     732                                        this.setContent( this.node, iframe );
     733
     734                                        iframeWin = iframe.contentWindow;
     735                                        iframeDoc = iframeWin.document;
     736
     737                                        iframeWin.resize = function() {
     738                                                $( iframe ).height( $( iframeDoc ).height() );
     739                                        };
     740
     741                                        iframeDoc.open();
     742                                        iframeDoc.write(
     743                                                '<!DOCTYPE html>' +
     744                                                '<html>' +
     745                                                        '<head>' +
     746                                                                '<meta charset="utf-8">' +
     747                                                        '</head>' +
     748                                                '<body onload="setTimeout( resize, 1000 );">' +
     749                                                        content +
     750                                                '</body>' +
     751                                                '</html>'
     752                                        );
     753                                        iframeDoc.close();
     754
     755                                        if ( MutationObserver ) {
     756                                                new MutationObserver( _.debounce( function() {
     757                                                        iframeWin.resize();
     758                                                }, 100 ) ).observe( iframeDoc.body, {
     759                                                        attributes: true,
     760                                                        childList: true,
     761                                                        subtree: true
     762                                                } );
     763                                        }
     764                                } else {
     765                                        this.setContent( this.node, content );
    686766                                }
    687                                 return this.template({ content: this.parsed });
    688767                        }
    689                 } ),
    690                 edit: function() {}
     768
     769                        this.parseMediaShortcodes();
     770                },
     771                parseMediaShortcodes: function () {
     772                        var self = this;
     773                        $( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) {
     774                                self.players.push( new MediaElementPlayer( element, self.mejsSettings ) );
     775                        } );
     776                },
     777                getHtml: function() {
     778                        return '';
     779                }
     780        } );
     781
     782        wp.mce.views.register( 'embed', {
     783                View: wp.mce.embedView
     784        } );
     785
     786        wp.mce.views.register( 'embedURL', {
     787                toView: function( content ) {
     788                        var re = /^<p>\s*(https?:\/\/[^\s"]+)\s*<\/p>$/gim,
     789                                match = re.exec( content );
     790
     791                        if ( ! match ) {
     792                                return;
     793                        }
     794
     795                        return {
     796                                index: match.index,
     797                                content: match[0],
     798                                options: {
     799                                        url: match[1]
     800                                }
     801                        };
     802                },
     803                View: wp.mce.embedView
    691804        } );
    692805
    693806}(jQuery));
  • 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.
  • src/wp-includes/media-template.php

     
    994994        </script>
    995995
    996996        <script type="text/html" id="tmpl-editor-gallery">
    997                 <div class="toolbar">
    998                         <div class="dashicons dashicons-edit edit"></div><div class="dashicons dashicons-no-alt remove"></div>
    999                 </div>
    1000997                <# if ( data.attachments ) { #>
    1001998                        <div class="gallery gallery-columns-{{ data.columns }}">
    1002999                                <# _.each( data.attachments, function( attachment, index ) { #>
     
    10271024        </script>
    10281025
    10291026        <script type="text/html" id="tmpl-editor-audio">
    1030                 <div class="toolbar">
    1031                         <div class="dashicons dashicons-edit edit"></div>
    1032                         <div class="dashicons dashicons-no-alt remove"></div>
    1033                 </div>
    10341027                <?php wp_underscore_audio_template() ?>
    1035                 <div class="wpview-overlay"></div>
    10361028        </script>
    10371029
    10381030        <script type="text/html" id="tmpl-editor-video">
    1039                 <div class="toolbar">
    1040                         <div class="dashicons dashicons-edit edit"></div>
    1041                         <div class="dashicons dashicons-no-alt remove"></div>
    1042                 </div>
    10431031                <?php wp_underscore_video_template() ?>
    1044                 <div class="wpview-overlay"></div>
    10451032        </script>
    10461033
    10471034        <?php wp_underscore_playlist_templates() ?>
    10481035
    10491036        <script type="text/html" id="tmpl-editor-playlist">
    1050                 <div class="toolbar">
    1051                         <div class="dashicons dashicons-edit edit"></div>
    1052                         <div class="dashicons dashicons-no-alt remove"></div>
    1053                 </div>
    10541037                <# if ( data.tracks ) { #>
    10551038                        <div class="wp-playlist wp-{{ data.type }}-playlist wp-playlist-{{ data.style }}">
    10561039                                <# if ( 'audio' === data.type ){ #>
     
    10621045                                <div class="wp-playlist-next"></div>
    10631046                                <div class="wp-playlist-prev"></div>
    10641047                        </div>
    1065                         <div class="wpview-overlay"></div>
    10661048                <# } else { #>
    10671049                        <div class="wpview-error">
    10681050                                <div class="dashicons dashicons-video-alt3"></div><p><?php _e( 'No items found.' ); ?></p>
     
    10751057                <div class="upload-errors"></div>
    10761058        </script>
    10771059
    1078         <script type="text/html" id="tmpl-editor-embed">
    1079                 <div class="toolbar">
    1080                         <div class="dashicons dashicons-no-alt remove"></div>
    1081                 </div>
    1082                 {{{ data.content }}}
    1083                 <div class="wpview-overlay"></div>
    1084         </script>
    1085 
    10861060        <?php
    10871061
    10881062        /**