Ticket #28195: 28195.12.patch
File 28195.12.patch, 14.0 KB (added by , 10 years ago) |
---|
-
src/wp-includes/js/mce-view.js
8 8 // Ensure the global `wp` object exists. 9 9 window.wp = window.wp || {}; 10 10 11 (function($){ 11 ( function( $ ) { 12 'use strict'; 13 12 14 var views = {}, 13 15 instances = {}, 14 16 media = wp.media, … … 27 29 */ 28 30 wp.mce.View = function( options ) { 29 31 options = options || {}; 32 this.type = options.type; 30 33 _.extend( this, _.pick( options, viewOptions ) ); 31 34 this.initialize.apply( this, arguments ); 32 35 }; … … 35 38 initialize: function() {}, 36 39 getHtml: function() {}, 37 40 render: function() { 38 var html = this.getHtml(); 39 // Search all tinymce editor instances and update the placeholders 41 this.setContent( 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 (has to be the last child node). 51 // Needed when comparing the content as string for preventing extra undo levels. 52 '<ins data-wpview-end="1"></ins>', 53 function( self, editor, node ) { 54 $( self ).trigger( 'ready', [ editor, node ] ); 55 } 56 ); 57 }, 58 unbind: function() {}, 59 setContent: function( html, callback, replace ) { 40 60 _.each( tinymce.editors, function( editor ) { 41 61 var self = this; 42 62 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>' ); 48 $( self ).trigger( 'ready', element ); 49 }); 63 $( editor.getBody() ) 64 .find( '[data-wpview-text="' + this.encodedText + '"]' ) 65 .each( function ( i, element ) { 66 var contentWrap = $( element ).children( '.wpview-content' ), 67 wrap = element; 68 69 if ( contentWrap.length ) { 70 element = contentWrap = contentWrap[0]; 71 } 72 73 if ( _.isString( html ) ) { 74 if ( replace ) { 75 element = editor.dom.replace( editor.dom.createFragment( html ), wrap ); 76 } else { 77 editor.dom.setHTML( element, html ); 78 } 79 } else { 80 if ( replace ) { 81 element = editor.dom.replace( html, wrap ); 82 } else { 83 element.appendChild( html ); 84 } 85 } 86 87 if ( _.isFunction( callback ) ) { 88 callback( self, editor, $( element ).children( '.wpview-content' )[0] ); 89 } 90 } ); 50 91 } 51 92 }, this ); 52 93 }, 53 unbind: function() {} 94 setError: function( message, dashicon ) { 95 this.setContent( 96 '<div class="wpview-error">' + 97 '<div class="dashicons dashicons-' + ( dashicon ? dashicon : 'no' ) + '"></div>' + 98 '<p>' + message + '</p>' + 99 '</div>' 100 ); 101 } 54 102 } ); 55 103 56 104 // take advantage of the Backbone extend method … … 209 257 210 258 if ( ! wp.mce.views.getInstance( encodedText ) ) { 211 259 viewOptions = options; 260 viewOptions.type = viewType; 212 261 viewOptions.encodedText = encodedText; 213 262 instance = new view.View( viewOptions ); 214 263 instances[ encodedText ] = instance; … … 244 293 if ( ! instance ) { 245 294 result = view.toView( text ); 246 295 viewOptions = result.options; 296 viewOptions.type = view.type; 247 297 viewOptions.encodedText = encodedText; 248 298 instance = new view.View( viewOptions ); 249 299 instances[ encodedText ] = instance; … … 331 381 }; 332 382 333 383 return this.template( options ); 334 335 384 } 336 385 }, 337 386 … … 361 410 loaded: false, 362 411 363 412 View: _.extend( {}, wp.media.mixin, { 413 overlay: true, 414 364 415 initialize: function( options ) { 365 416 this.players = []; 366 417 this.shortcode = options.shortcode; … … 375 426 * 376 427 * @global MediaElementPlayer 377 428 * 378 * @param {Event} e 429 * @param {Event} event 430 * @param {Object} editor 379 431 * @param {HTMLElement} node 380 432 */ 381 setPlayer: function(e, node) { 382 // if the ready event fires on an empty node 383 if ( ! node ) { 384 return; 385 } 386 433 setPlayer: function( event, editor, node ) { 387 434 var self = this, 388 media, 389 firefox = this.ua.is( 'ff' ), 390 className = '.wp-' + this.shortcode.tag + '-shortcode'; 435 media; 391 436 392 media = $( node ).find( className);437 media = $( node ).find( '.wp-' + this.shortcode.tag + '-shortcode' ); 393 438 394 439 if ( ! this.isCompatible( media ) ) { 395 440 media.closest( '.wpview-wrap' ).addClass( 'wont-play' ); 396 if ( ! media.parent().hasClass( 'wpview-wrap' ) ) {397 media.parent().replaceWith( media );398 }399 441 media.replaceWith( '<p>' + media.find( 'source' ).eq(0).prop( 'src' ) + '</p>' ); 400 442 return; 401 443 } else { 402 444 media.closest( '.wpview-wrap' ).removeClass( 'wont-play' ); 403 if ( firefox) {445 if ( this.ua.is( 'ff' ) ) { 404 446 media.prop( 'preload', 'metadata' ); 405 447 } else { 406 448 media.prop( 'preload', 'none' ); … … 508 550 state: ['playlist-edit', 'video-playlist-edit'], 509 551 View: _.extend( {}, wp.media.mixin, { 510 552 template: media.template( 'editor-playlist' ), 553 overlay: true, 511 554 512 555 initialize: function( options ) { 513 556 this.players = []; … … 531 574 this.dfd = this.attachments.more().done( _.bind( this.render, this ) ); 532 575 }, 533 576 534 setPlaylist: function( event, e lement ) {577 setPlaylist: function( event, editor, element ) { 535 578 if ( ! this.data.tracks ) { 536 579 return; 537 580 } … … 634 677 /** 635 678 * TinyMCE handler for the embed shortcode 636 679 */ 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(); 680 wp.mce.embedView = _.extend( {}, wp.media.mixin, { 681 overlay: true, 682 initialize: function( options ) { 683 this.players = []; 684 this.content = options.content; 685 this.fetching = false; 686 this.parsed = false; 687 this.original = options.url || options.shortcode.string(); 688 if ( options.url ) { 689 this.shortcode = '[embed]' + options.url + '[/embed]'; 690 } else { 691 this.shortcode = options.shortcode.string(); 692 } 693 _.bindAll( this, 'setHtml', 'setNode', 'fetch' ); 694 $( this ).on( 'ready', this.setNode ); 695 }, 696 unbind: function() { 697 var self = this; 698 _.each( this.players, function ( player ) { 699 player.pause(); 700 self.removePlayer( player ); 701 } ); 702 this.players = []; 703 }, 704 setNode: function () { 705 if ( this.parsed ) { 706 this.setHtml( this.parsed ); 707 this.parseMediaShortcodes(); 708 } else if ( ! this.fetching ) { 709 this.fetch(); 710 } 711 }, 712 fetch: function () { 713 var self = this; 714 715 this.fetching = true; 716 717 wp.ajax.send( 'parse-embed', { 718 data: { 719 post_ID: $( '#post_ID' ).val(), 720 content: this.shortcode 721 } 722 } ) 723 .done( function( content ) { 724 self.fetching = false; 725 726 if ( content.substring( 0, ( '<a href' ).length ) === '<a href' ) { 727 if ( self.type === 'embed' ) { 728 self.setError( self.original + ' failed to embed.', 'admin-media' ); 729 } else { 730 self.setContent( self.original, null, true ); 731 } 660 732 } else { 661 this.fetch(); 733 self.parsed = content; 734 self.setHtml( content ); 662 735 } 663 }, 664 fetch: function () { 665 wp.ajax.send( 'parse-embed', { 666 data: { 667 post_ID: $( '#post_ID' ).val(), 668 content: this.shortcode.string() 736 } ) 737 .fail( function() { 738 self.fetching = false; 739 self.setError( self.original + ' failed to embed due to a server error.', 'admin-media' ); 740 } ); 741 }, 742 /* jshint scripturl: true */ 743 setHtml: function ( content ) { 744 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver, 745 iframe, iframeWin, iframeDoc, 746 dom = tinymce.DOM; 747 748 if ( content.indexOf( '<script' ) !== -1 ) { 749 iframe = dom.create( 'iframe', { 750 src: 'javascript:""', 751 frameBorder: '0', 752 allowTransparency: 'true', 753 style: { 754 width: '100%', 755 display: 'block' 669 756 } 670 } ).done( this.setHtml );671 },672 setHtml: function ( content ) {673 this.parsed = content;674 $( this.node ).html( this.getHtml() );675 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 757 } ); 682 }, 683 getHtml: function() { 684 if ( ! this.parsed ) { 685 return ''; 758 759 this.setContent( iframe ); 760 761 iframeWin = iframe.contentWindow; 762 iframeDoc = iframeWin.document; 763 764 iframeWin.resize = function() { 765 $( iframe ).height( $( iframeDoc ).height() ); 766 }; 767 768 iframeDoc.open(); 769 iframeDoc.write( 770 '<!DOCTYPE html>' + 771 '<html>' + 772 '<head>' + 773 '<meta charset="utf-8">' + 774 '</head>' + 775 '<body onload="setTimeout( resize, 2000 );">' + 776 content + 777 '</body>' + 778 '</html>' 779 ); 780 iframeDoc.close(); 781 782 if ( MutationObserver ) { 783 new MutationObserver( _.debounce( function() { 784 iframeWin.resize(); 785 }, 100 ) ) 786 .observe( iframeDoc.body, { 787 attributes: true, 788 childList: true, 789 subtree: true 790 } ); 686 791 } 687 return this.template({ content: this.parsed }); 792 } else { 793 this.setContent( content ); 688 794 } 689 } ), 690 edit: function() {} 795 796 this.parseMediaShortcodes(); 797 }, 798 parseMediaShortcodes: function () { 799 var self = this; 800 $( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) { 801 self.players.push( new MediaElementPlayer( element, self.mejsSettings ) ); 802 } ); 803 }, 804 getHtml: function() { 805 return ''; 806 } 691 807 } ); 692 808 809 wp.mce.views.register( 'embed', { 810 View: wp.mce.embedView 811 } ); 812 813 wp.mce.views.register( 'embedURL', { 814 toView: function( content ) { 815 var re = /(?:^|<p>)(https?:\/\/[^\s"]+?)(?:<\/p>\s*|$)/gi, 816 match = re.exec( tinymce.trim( content ) ); 817 818 if ( ! match ) { 819 return; 820 } 821 822 return { 823 index: match.index, 824 content: match[0], 825 options: { 826 url: match[1] 827 } 828 }; 829 }, 830 View: wp.mce.embedView 831 } ); 832 693 833 }(jQuery)); -
src/wp-includes/js/tinymce/plugins/wpview/plugin.js
169 169 event.content = wp.mce.views.toViews( event.content ); 170 170 }); 171 171 172 editor.on( 'PastePreProcess', function( event ) {173 if ( event.content.match( /^\s*(https?:\/\/[^\s"]+)\s*$/im ) ) {174 event.content = '[embed]' + event.content + '[/embed]';175 }176 } );177 178 172 // When the editor's content has been updated and the DOM has been 179 173 // processed, render the views in the document. 180 174 editor.on( 'SetContent', function( event ) { -
src/wp-includes/media-template.php
994 994 </script> 995 995 996 996 <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>1000 997 <# if ( data.attachments ) { #> 1001 998 <div class="gallery gallery-columns-{{ data.columns }}"> 1002 999 <# _.each( data.attachments, function( attachment, index ) { #> … … 1027 1024 </script> 1028 1025 1029 1026 <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>1034 1027 <?php wp_underscore_audio_template() ?> 1035 <div class="wpview-overlay"></div>1036 1028 </script> 1037 1029 1038 1030 <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>1043 1031 <?php wp_underscore_video_template() ?> 1044 <div class="wpview-overlay"></div>1045 1032 </script> 1046 1033 1047 1034 <?php wp_underscore_playlist_templates() ?> 1048 1035 1049 1036 <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>1054 1037 <# if ( data.tracks ) { #> 1055 1038 <div class="wp-playlist wp-{{ data.type }}-playlist wp-playlist-{{ data.style }}"> 1056 1039 <# if ( 'audio' === data.type ){ #> … … 1062 1045 <div class="wp-playlist-next"></div> 1063 1046 <div class="wp-playlist-prev"></div> 1064 1047 </div> 1065 <div class="wpview-overlay"></div>1066 1048 <# } else { #> 1067 1049 <div class="wpview-error"> 1068 1050 <div class="dashicons dashicons-video-alt3"></div><p><?php _e( 'No items found.' ); ?></p> … … 1075 1057 <div class="upload-errors"></div> 1076 1058 </script> 1077 1059 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 1086 1060 <?php 1087 1061 1088 1062 /**