Ticket #28195: 28195.10.patch
File 28195.10.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. 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 }, 53 unbind: function() {} 92 } 54 93 } ); 55 94 56 95 // take advantage of the Backbone extend method … … 209 248 210 249 if ( ! wp.mce.views.getInstance( encodedText ) ) { 211 250 viewOptions = options; 251 viewOptions.type = viewType; 212 252 viewOptions.encodedText = encodedText; 213 253 instance = new view.View( viewOptions ); 214 254 instances[ encodedText ] = instance; … … 244 284 if ( ! instance ) { 245 285 result = view.toView( text ); 246 286 viewOptions = result.options; 287 viewOptions.type = view.type; 247 288 viewOptions.encodedText = encodedText; 248 289 instance = new view.View( viewOptions ); 249 290 instances[ encodedText ] = instance; … … 331 372 }; 332 373 333 374 return this.template( options ); 334 335 375 } 336 376 }, 337 377 … … 361 401 loaded: false, 362 402 363 403 View: _.extend( {}, wp.media.mixin, { 404 overlay: true, 405 364 406 initialize: function( options ) { 365 407 this.players = []; 366 408 this.shortcode = options.shortcode; … … 375 417 * 376 418 * @global MediaElementPlayer 377 419 * 378 * @param {Event} e 420 * @param {Event} event 421 * @param {Object} editor 379 422 * @param {HTMLElement} node 380 423 */ 381 setPlayer: function(e, node) { 382 // if the ready event fires on an empty node 383 if ( ! node ) { 384 return; 385 } 386 424 setPlayer: function( event, editor, node ) { 387 425 var self = this, 388 media, 389 firefox = this.ua.is( 'ff' ), 390 className = '.wp-' + this.shortcode.tag + '-shortcode'; 426 media; 391 427 392 media = $( node ).find( className);428 media = $( node ).find( '.wp-' + this.shortcode.tag + '-shortcode' ); 393 429 394 430 if ( ! this.isCompatible( media ) ) { 395 431 media.closest( '.wpview-wrap' ).addClass( 'wont-play' ); 396 if ( ! media.parent().hasClass( 'wpview-wrap' ) ) {397 media.parent().replaceWith( media );398 }399 432 media.replaceWith( '<p>' + media.find( 'source' ).eq(0).prop( 'src' ) + '</p>' ); 400 433 return; 401 434 } else { 402 435 media.closest( '.wpview-wrap' ).removeClass( 'wont-play' ); 403 if ( firefox) {436 if ( this.ua.is( 'ff' ) ) { 404 437 media.prop( 'preload', 'metadata' ); 405 438 } else { 406 439 media.prop( 'preload', 'none' ); … … 508 541 state: ['playlist-edit', 'video-playlist-edit'], 509 542 View: _.extend( {}, wp.media.mixin, { 510 543 template: media.template( 'editor-playlist' ), 544 overlay: true, 511 545 512 546 initialize: function( options ) { 513 547 this.players = []; … … 531 565 this.dfd = this.attachments.more().done( _.bind( this.render, this ) ); 532 566 }, 533 567 534 setPlaylist: function( event, e lement ) {568 setPlaylist: function( event, editor, element ) { 535 569 if ( ! this.data.tracks ) { 536 570 return; 537 571 } … … 634 668 /** 635 669 * TinyMCE handler for the embed shortcode 636 670 */ 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(); 671 wp.mce.embedView = _.extend( {}, wp.media.mixin, { 672 overlay: true, 673 initialize: function( options ) { 674 this.players = []; 675 this.content = options.content; 676 this.fetching = false; 677 this.parsed = false; 678 this.original = options.url || options.shortcode.string(); 679 if ( options.url ) { 680 this.shortcode = '[embed]' + options.url + '[/embed]'; 681 } else { 682 this.shortcode = options.shortcode.string(); 683 } 684 _.bindAll( this, 'setHtml', 'setNode', 'fetch' ); 685 $( this ).on( 'ready', this.setNode ); 686 }, 687 unbind: function() { 688 var self = this; 689 _.each( this.players, function ( player ) { 690 player.pause(); 691 self.removePlayer( player ); 692 } ); 693 this.players = []; 694 }, 695 setNode: function () { 696 if ( this.parsed ) { 697 this.setHtml( this.parsed ); 698 this.parseMediaShortcodes(); 699 } else if ( ! this.fetching ) { 700 this.fetch(); 701 } 702 }, 703 fetch: function () { 704 var self = this; 705 706 this.fetching = true; 707 708 wp.ajax.send( 'parse-embed', { 709 data: { 710 post_ID: $( '#post_ID' ).val(), 711 content: this.shortcode 662 712 } 663 }, 664 fetch: function () { 665 wp.ajax.send( 'parse-embed', { 666 data: { 667 post_ID: $( '#post_ID' ).val(), 668 content: this.shortcode.string() 713 } ).done( function( content ) { 714 self.parsed = content; 715 self.setHtml( content ); 716 } ); 717 }, 718 /* jshint scripturl: true */ 719 setHtml: function ( content ) { 720 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver, 721 iframe, iframeWin, iframeDoc, 722 test = '<a href', 723 dom = tinymce.DOM; 724 725 if ( content.substring( 0, test.length ) === test ) { 726 this.setContent( '<p>' + this.original + '</p>', null, true ); 727 } else { 728 if ( content.indexOf( '<script' ) !== -1 ) { 729 iframe = dom.create( 'IFRAME', { 730 src: 'javascript:""', 731 frameBorder: '0', 732 allowTransparency: 'true', 733 style: { 734 width: '100%', 735 display: 'block' 736 } 737 } ); 738 739 this.setContent( iframe ); 740 741 iframeWin = iframe.contentWindow; 742 iframeDoc = iframeWin.document; 743 744 iframeWin.resize = function() { 745 $( iframe ).height( $( iframeDoc ).height() ); 746 }; 747 748 iframeDoc.open(); 749 iframeDoc.write( 750 '<!DOCTYPE html>' + 751 '<html>' + 752 '<head>' + 753 '<meta charset="utf-8">' + 754 '</head>' + 755 '<body onload="setTimeout( resize, 1000 );">' + 756 content + 757 '</body>' + 758 '</html>' 759 ); 760 iframeDoc.close(); 761 762 if ( MutationObserver ) { 763 new MutationObserver( _.debounce( function() { 764 iframeWin.resize(); 765 }, 100 ) ) 766 .observe( iframeDoc.body, { 767 attributes: true, 768 childList: true, 769 subtree: true 770 } ); 669 771 } 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 } ); 682 }, 683 getHtml: function() { 684 if ( ! this.parsed ) { 685 return ''; 772 } else { 773 this.setContent( content ); 686 774 } 687 return this.template({ content: this.parsed });688 775 } 689 } ), 690 edit: function() {} 776 777 this.parseMediaShortcodes(); 778 }, 779 parseMediaShortcodes: function () { 780 var self = this; 781 $( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) { 782 self.players.push( new MediaElementPlayer( element, self.mejsSettings ) ); 783 } ); 784 }, 785 getHtml: function() { 786 return ''; 787 } 788 } ); 789 790 wp.mce.views.register( 'embed', { 791 View: wp.mce.embedView 792 } ); 793 794 wp.mce.views.register( 'embedURL', { 795 toView: function( content ) { 796 var re = /^<p>\s*(https?:\/\/[^\s"]+)\s*<\/p>$/gim, 797 match = re.exec( content ); 798 799 if ( ! match ) { 800 return; 801 } 802 803 return { 804 index: match.index, 805 content: match[0], 806 options: { 807 url: match[1] 808 } 809 }; 810 }, 811 View: wp.mce.embedView 691 812 } ); 692 813 693 814 }(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 /**