diff --git a/src/wp-includes/js/mce-view.js b/src/wp-includes/js/mce-view.js index 4d5396ac5b..fb4afe51d2 100644 --- a/src/wp-includes/js/mce-view.js +++ b/src/wp-includes/js/mce-view.js @@ -8,7 +8,9 @@ // Ensure the global `wp` object exists. window.wp = window.wp || {}; -(function($){ +( function( $ ) { + 'use strict'; + var views = {}, instances = {}, media = wp.media, @@ -27,6 +29,7 @@ window.wp = window.wp || {}; */ wp.mce.View = function( options ) { options = options || {}; + this.type = options.type; _.extend( this, _.pick( options, viewOptions ) ); this.initialize.apply( this, arguments ); }; @@ -35,22 +38,67 @@ window.wp = window.wp || {}; initialize: function() {}, getHtml: function() {}, render: function() { - var html = this.getHtml(); - // Search all tinymce editor instances and update the placeholders + this.setContent( + '
' + + '' + message + '
' + + '' + media.find( 'source' ).eq(0).prop( 'src' ) + '
' ); return; } else { media.closest( '.wpview-wrap' ).removeClass( 'wont-play' ); - if ( firefox ) { + if ( this.ua.is( 'ff' ) ) { media.prop( 'preload', 'metadata' ); } else { media.prop( 'preload', 'none' ); @@ -508,6 +550,7 @@ window.wp = window.wp || {}; state: ['playlist-edit', 'video-playlist-edit'], View: _.extend( {}, wp.media.mixin, { template: media.template( 'editor-playlist' ), + overlay: true, initialize: function( options ) { this.players = []; @@ -531,7 +574,7 @@ window.wp = window.wp || {}; this.dfd = this.attachments.more().done( _.bind( this.render, this ) ); }, - setPlaylist: function( event, element ) { + setPlaylist: function( event, editor, element ) { if ( ! this.data.tracks ) { return; } @@ -634,60 +677,161 @@ window.wp = window.wp || {}; /** * TinyMCE handler for the embed shortcode */ - wp.mce.views.register( 'embed', { - View: _.extend( {}, wp.media.mixin, { - template: media.template( 'editor-embed' ), - initialize: function( options ) { - this.players = []; - this.content = options.content; - this.parsed = false; - this.shortcode = options.shortcode; - _.bindAll( this, 'setHtml', 'setNode', 'fetch' ); - $( this ).on( 'ready', this.setNode ); - }, - unbind: function() { - var self = this; - _.each( this.players, function ( player ) { - player.pause(); - self.removePlayer( player ); - } ); - this.players = []; - }, - setNode: function ( e, node ) { - this.node = node; - if ( this.parsed ) { - this.parseMediaShortcodes(); - } else { - this.fetch(); - } - }, - fetch: function () { - wp.ajax.send( 'parse-embed', { - data: { - post_ID: $( '#post_ID' ).val(), - content: this.shortcode.string() - } - } ).done( this.setHtml ); - }, - setHtml: function ( content ) { - this.parsed = content; - $( this.node ).html( this.getHtml() ); - this.parseMediaShortcodes(); - }, - parseMediaShortcodes: function () { - var self = this; - $( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) { - self.players.push( new MediaElementPlayer( element, self.mejsSettings ) ); - } ); - }, - getHtml: function() { - if ( ! this.parsed ) { - return ''; - } - return this.template({ content: this.parsed }); + wp.mce.embedView = _.extend( {}, wp.media.mixin, { + overlay: true, + initialize: function( options ) { + this.players = []; + this.content = options.content; + this.fetching = false; + this.parsed = false; + this.original = options.url || options.shortcode.string(); + + if ( options.url ) { + this.shortcode = '[embed]' + options.url + '[/embed]'; + } else { + this.shortcode = options.shortcode.string(); } - } ), - edit: function() {} + + _.bindAll( this, 'setHtml', 'setNode', 'fetch' ); + $( this ).on( 'ready', this.setNode ); + }, + unbind: function() { + var self = this; + _.each( this.players, function ( player ) { + player.pause(); + self.removePlayer( player ); + } ); + this.players = []; + }, + setNode: function () { + if ( this.parsed ) { + this.setHtml( this.parsed ); + this.parseMediaShortcodes(); + } else if ( ! this.fetching ) { + this.fetch(); + } + }, + fetch: function () { + var self = this; + + this.fetching = true; + + wp.ajax.send( 'parse-embed', { + data: { + post_ID: $( '#post_ID' ).val(), + content: this.shortcode + } + } ) + .done( function( content ) { + self.fetching = false; + + if ( content.substring( 0, ( '' + + '' + + '' + + '' + + '' + + '' + + content + + '' + + '' + ); + iframeDoc.close(); + + resize = function() { + $( iframe ).height( $( iframeDoc ).height() ); + }; + + if ( MutationObserver ) { + new MutationObserver( _.debounce( function() { + resize(); + }, 100 ) ) + .observe( iframeDoc.body, { + attributes: true, + childList: true, + subtree: true + } ); + } else { + for ( i = 1; i < 6; i++ ) { + setTimeout( resize, i * 700 ); + } + } + } else { + this.setContent( content ); + } + + this.parseMediaShortcodes(); + }, + parseMediaShortcodes: function () { + var self = this; + $( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) { + self.players.push( new MediaElementPlayer( element, self.mejsSettings ) ); + } ); + }, + getHtml: function() { + return ''; + } + } ); + + wp.mce.views.register( 'embed', { + View: wp.mce.embedView + } ); + + wp.mce.views.register( 'embedURL', { + toView: function( content ) { + var re = /(?:^|)(https?:\/\/[^\s"]+?)(?:<\/p>\s*|$)/gi, + match = re.exec( tinymce.trim( content ) ); + + if ( ! match ) { + return; + } + + return { + index: match.index, + content: match[0], + options: { + url: match[1] + } + }; + }, + View: wp.mce.embedView } ); }(jQuery)); diff --git a/src/wp-includes/js/tinymce/plugins/wpview/plugin.js b/src/wp-includes/js/tinymce/plugins/wpview/plugin.js index fba3bfc012..be11d0663c 100644 --- a/src/wp-includes/js/tinymce/plugins/wpview/plugin.js +++ b/src/wp-includes/js/tinymce/plugins/wpview/plugin.js @@ -158,6 +158,8 @@ tinymce.PluginManager.add( 'wpview', function( editor ) { // matching view patterns, and transform the matches into // view wrappers. editor.on( 'BeforeSetContent', function( event ) { + var node; + if ( ! event.content ) { return; } @@ -166,15 +168,17 @@ tinymce.PluginManager.add( 'wpview', function( editor ) { wp.mce.views.unbind( editor ); } + node = editor.selection.getNode(); + + // When a url is pasted, only try to embed it when pasted in an empty paragrapgh. + if ( event.content.match( /^\s*(https?:\/\/[^\s"]+)\s*$/i ) && + ( node.nodeName !== 'P' || node.parentNode !== editor.getBody() || ! editor.dom.isEmpty( node ) ) ) { + return; + } + event.content = wp.mce.views.toViews( event.content ); }); - editor.on( 'PastePreProcess', function( event ) { - if ( event.content.match( /^\s*(https?:\/\/[^\s"]+)\s*$/im ) ) { - event.content = '[embed]' + event.content + '[/embed]'; - } - } ); - // When the editor's content has been updated and the DOM has been // processed, render the views in the document. editor.on( 'SetContent', function( event ) { diff --git a/src/wp-includes/media-template.php b/src/wp-includes/media-template.php index 7ef4f041e3..1fdcb7f0a9 100644 --- a/src/wp-includes/media-template.php +++ b/src/wp-includes/media-template.php @@ -994,9 +994,6 @@ function wp_print_media_templates() { - -