Ticket #28195: 28195.11.patch
File 28195.11.patch, 14.5 KB (added by , 11 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. 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 unbind: function() {}, 58 setContent: function( html, callback, replace ) { 40 59 _.each( tinymce.editors, function( editor ) { 41 60 var self = this; 42 61 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 }); 62 $( editor.getBody() ) 63 .find( '[data-wpview-text="' + this.encodedText + '"]' ) 64 .each( function ( i, element ) { 65 var contentWrap = $( element ).children( '.wpview-content' ), 66 wrap = element; 67 68 if ( contentWrap.length ) { 69 element = contentWrap = contentWrap[0]; 70 } 71 72 if ( _.isString( html ) ) { 73 if ( replace ) { 74 element = editor.dom.replace( editor.dom.createFragment( html ), wrap ); 75 } else { 76 editor.dom.setHTML( element, html ); 77 } 78 } else { 79 if ( replace ) { 80 element = editor.dom.replace( html, wrap ); 81 } else { 82 element.appendChild( html ); 83 } 84 } 85 86 if ( _.isFunction( callback ) ) { 87 callback( self, editor, $( element ).children( '.wpview-content' )[0] ); 88 } 89 } ); 50 90 } 51 91 }, this ); 52 92 }, 53 unbind: function() {} 93 setError: function( message, dashicon ) { 94 this.setContent( 95 '<div class="wpview-error">' + 96 '<div class="dashicons dashicons-' + ( dashicon ? dashicon : 'no' ) + '"></div>' + 97 '<p>' + message + '</p>' + 98 '</div>' 99 ); 100 } 54 101 } ); 55 102 56 103 // take advantage of the Backbone extend method … … 209 256 210 257 if ( ! wp.mce.views.getInstance( encodedText ) ) { 211 258 viewOptions = options; 259 viewOptions.type = viewType; 212 260 viewOptions.encodedText = encodedText; 213 261 instance = new view.View( viewOptions ); 214 262 instances[ encodedText ] = instance; … … 244 292 if ( ! instance ) { 245 293 result = view.toView( text ); 246 294 viewOptions = result.options; 295 viewOptions.type = view.type; 247 296 viewOptions.encodedText = encodedText; 248 297 instance = new view.View( viewOptions ); 249 298 instances[ encodedText ] = instance; … … 331 380 }; 332 381 333 382 return this.template( options ); 334 335 383 } 336 384 }, 337 385 … … 361 409 loaded: false, 362 410 363 411 View: _.extend( {}, wp.media.mixin, { 412 overlay: true, 413 364 414 initialize: function( options ) { 365 415 this.players = []; 366 416 this.shortcode = options.shortcode; … … 375 425 * 376 426 * @global MediaElementPlayer 377 427 * 378 * @param {Event} e 428 * @param {Event} event 429 * @param {Object} editor 379 430 * @param {HTMLElement} node 380 431 */ 381 setPlayer: function(e, node) { 382 // if the ready event fires on an empty node 383 if ( ! node ) { 384 return; 385 } 386 432 setPlayer: function( event, editor, node ) { 387 433 var self = this, 388 media, 389 firefox = this.ua.is( 'ff' ), 390 className = '.wp-' + this.shortcode.tag + '-shortcode'; 434 media; 391 435 392 media = $( node ).find( className);436 media = $( node ).find( '.wp-' + this.shortcode.tag + '-shortcode' ); 393 437 394 438 if ( ! this.isCompatible( media ) ) { 395 439 media.closest( '.wpview-wrap' ).addClass( 'wont-play' ); 396 if ( ! media.parent().hasClass( 'wpview-wrap' ) ) {397 media.parent().replaceWith( media );398 }399 440 media.replaceWith( '<p>' + media.find( 'source' ).eq(0).prop( 'src' ) + '</p>' ); 400 441 return; 401 442 } else { 402 443 media.closest( '.wpview-wrap' ).removeClass( 'wont-play' ); 403 if ( firefox) {444 if ( this.ua.is( 'ff' ) ) { 404 445 media.prop( 'preload', 'metadata' ); 405 446 } else { 406 447 media.prop( 'preload', 'none' ); … … 508 549 state: ['playlist-edit', 'video-playlist-edit'], 509 550 View: _.extend( {}, wp.media.mixin, { 510 551 template: media.template( 'editor-playlist' ), 552 overlay: true, 511 553 512 554 initialize: function( options ) { 513 555 this.players = []; … … 531 573 this.dfd = this.attachments.more().done( _.bind( this.render, this ) ); 532 574 }, 533 575 534 setPlaylist: function( event, e lement ) {576 setPlaylist: function( event, editor, element ) { 535 577 if ( ! this.data.tracks ) { 536 578 return; 537 579 } … … 634 676 /** 635 677 * TinyMCE handler for the embed shortcode 636 678 */ 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(); 679 wp.mce.embedView = _.extend( {}, wp.media.mixin, { 680 overlay: true, 681 initialize: function( options ) { 682 this.players = []; 683 this.content = options.content; 684 this.fetching = false; 685 this.parsed = false; 686 this.original = options.url || options.shortcode.string(); 687 if ( options.url ) { 688 this.shortcode = '[embed]' + options.url + '[/embed]'; 689 } else { 690 this.shortcode = options.shortcode.string(); 691 } 692 _.bindAll( this, 'setHtml', 'setNode', 'fetch' ); 693 $( this ).on( 'ready', this.setNode ); 694 }, 695 unbind: function() { 696 var self = this; 697 _.each( this.players, function ( player ) { 698 player.pause(); 699 self.removePlayer( player ); 700 } ); 701 this.players = []; 702 }, 703 setNode: function () { 704 if ( this.parsed ) { 705 this.setHtml( this.parsed ); 706 this.parseMediaShortcodes(); 707 } else if ( ! this.fetching ) { 708 this.fetch(); 709 } 710 }, 711 fetch: function () { 712 var self = this; 713 714 this.fetching = true; 715 716 wp.ajax.send( 'parse-embed', { 717 data: { 718 post_ID: $( '#post_ID' ).val(), 719 content: this.shortcode 720 } 721 } ) 722 .done( function( content ) { 723 self.fetching = false; 724 725 if ( content.substring( 0, ( '<a href' ).length ) === '<a href' ) { 726 if ( self.type === 'embed' ) { 727 self.setError( self.original + ' failed to embed.', 'admin-media' ); 728 } else { 729 self.setContent( '<p>' + self.original + '</p>', null, true ); 730 } 660 731 } else { 661 this.fetch(); 732 self.parsed = content; 733 self.setHtml( content ); 662 734 } 663 }, 664 fetch: function () { 665 wp.ajax.send( 'parse-embed', { 666 data: { 667 post_ID: $( '#post_ID' ).val(), 668 content: this.shortcode.string() 735 } ) 736 .fail( function() { 737 self.fetching = false; 738 self.setError( self.original + ' failed to embed due to a server error.', 'admin-media' ); 739 } ); 740 }, 741 /* jshint scripturl: true */ 742 setHtml: function ( content ) { 743 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver, 744 iframe, iframeWin, iframeDoc, 745 dom = tinymce.DOM; 746 747 if ( content.indexOf( '<script' ) !== -1 ) { 748 iframe = dom.create( 'IFRAME', { 749 src: 'javascript:""', 750 frameBorder: '0', 751 allowTransparency: 'true', 752 style: { 753 width: '100%', 754 display: 'block' 669 755 } 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 756 } ); 682 }, 683 getHtml: function() { 684 if ( ! this.parsed ) { 685 return ''; 757 758 this.setContent( iframe ); 759 760 iframeWin = iframe.contentWindow; 761 iframeDoc = iframeWin.document; 762 763 iframeWin.resize = function() { 764 $( iframe ).height( $( iframeDoc ).height() ); 765 }; 766 767 iframeDoc.open(); 768 iframeDoc.write( 769 '<!DOCTYPE html>' + 770 '<html>' + 771 '<head>' + 772 '<meta charset="utf-8">' + 773 '</head>' + 774 '<body onload="setTimeout( resize, 1000 );">' + 775 content + 776 '</body>' + 777 '</html>' 778 ); 779 iframeDoc.close(); 780 781 if ( MutationObserver ) { 782 new MutationObserver( _.debounce( function() { 783 iframeWin.resize(); 784 }, 100 ) ) 785 .observe( iframeDoc.body, { 786 attributes: true, 787 childList: true, 788 subtree: true 789 } ); 686 790 } 687 return this.template({ content: this.parsed }); 791 } else { 792 this.setContent( content ); 688 793 } 689 } ), 690 edit: function() {} 794 795 this.parseMediaShortcodes(); 796 }, 797 parseMediaShortcodes: function () { 798 var self = this; 799 $( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) { 800 self.players.push( new MediaElementPlayer( element, self.mejsSettings ) ); 801 } ); 802 }, 803 getHtml: function() { 804 return ''; 805 } 806 } ); 807 808 wp.mce.views.register( 'embed', { 809 View: wp.mce.embedView 810 } ); 811 812 wp.mce.views.register( 'embedURL', { 813 toView: function( content ) { 814 var re = /^<p>\s*(https?:\/\/[^\s"]+)\s*<\/p>$/gim, 815 match = re.exec( content ); 816 817 if ( ! match ) { 818 return; 819 } 820 821 return { 822 index: match.index, 823 content: match[0], 824 options: { 825 url: match[1] 826 } 827 }; 828 }, 829 View: wp.mce.embedView 691 830 } ); 692 831 693 832 }(jQuery)); -
src/wp-includes/js/tinymce/plugins/wpview/plugin.js
158 158 // matching view patterns, and transform the matches into 159 159 // view wrappers. 160 160 editor.on( 'BeforeSetContent', function( event ) { 161 var node; 162 161 163 if ( ! event.content ) { 162 164 return; 163 165 } … … 166 168 wp.mce.views.unbind( editor ); 167 169 } 168 170 169 event.content = wp.mce.views.toViews( event.content ); 170 }); 171 node = editor.selection.getNode(); 171 172 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>'; 175 176 } 176 } ); 177 178 event.content = wp.mce.views.toViews( event.content ); 179 }); 177 180 178 181 // When the editor's content has been updated and the DOM has been 179 182 // processed, render the views in the document. -
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 /**