Changeset 28748
- Timestamp:
- 06/12/2014 02:48:17 AM (11 years ago)
- Location:
- trunk/src/wp-includes
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/js/mce-view.js
r28689 r28748 9 9 window.wp = window.wp || {}; 10 10 11 (function($){ 11 ( function( $ ) { 12 'use strict'; 13 12 14 var views = {}, 13 15 instances = {}, … … 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 ); … … 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 ( _.isFunction( views[ this.type ].edit ) ? '<div class="dashicons dashicons-edit edit"></div>' : '' ) + 44 '<div class="dashicons dashicons-no-alt remove"></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 … … 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 ); … … 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 ); … … 332 382 333 383 return this.template( options ); 334 335 384 } 336 385 }, … … 362 411 363 412 View: _.extend( {}, wp.media.mixin, { 413 overlay: true, 414 364 415 initialize: function( options ) { 365 416 this.players = []; … … 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'; 391 392 media = $( node ).find( className ); 435 media; 436 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 { … … 509 551 View: _.extend( {}, wp.media.mixin, { 510 552 template: media.template( 'editor-playlist' ), 553 overlay: true, 511 554 512 555 initialize: function( options ) { … … 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; … … 635 678 * TinyMCE handler for the embed shortcode 636 679 */ 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 689 if ( options.url ) { 690 this.shortcode = '[embed]' + options.url + '[/embed]'; 691 } else { 692 this.shortcode = options.shortcode.string(); 693 } 694 695 _.bindAll( this, 'setHtml', 'setNode', 'fetch' ); 696 $( this ).on( 'ready', this.setNode ); 697 }, 698 unbind: function() { 699 var self = this; 700 _.each( this.players, function ( player ) { 701 player.pause(); 702 self.removePlayer( player ); 703 } ); 704 this.players = []; 705 }, 706 setNode: function () { 707 if ( this.parsed ) { 708 this.setHtml( this.parsed ); 709 this.parseMediaShortcodes(); 710 } else if ( ! this.fetching ) { 711 this.fetch(); 712 } 713 }, 714 fetch: function () { 715 var self = this; 716 717 this.fetching = true; 718 719 wp.ajax.send( 'parse-embed', { 720 data: { 721 post_ID: $( '#post_ID' ).val(), 722 content: this.shortcode 723 } 724 } ) 725 .done( function( content ) { 726 self.fetching = false; 727 728 if ( content.substring( 0, ( '<a href' ).length ) === '<a href' ) { 729 if ( self.type === 'embed' ) { 730 self.setError( self.original + ' failed to embed.', 'admin-media' ); 731 } else { 732 self.setContent( self.original, null, true ); 733 } 734 } else { 735 self.parsed = content; 736 self.setHtml( content ); 737 } 738 } ) 739 .fail( function() { 740 self.fetching = false; 741 self.setError( self.original + ' failed to embed due to a server error.', 'admin-media' ); 742 } ); 743 }, 744 /* jshint scripturl: true */ 745 setHtml: function ( content ) { 746 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver, 747 iframe, iframeDoc, i, resize, 748 dom = tinymce.DOM; 749 750 if ( content.indexOf( '<script' ) !== -1 ) { 751 iframe = dom.create( 'iframe', { 752 src: tinymce.Env.ie ? 'javascript:""' : '', 753 frameBorder: '0', 754 allowTransparency: 'true', 755 style: { 756 width: '100%', 757 display: 'block' 758 } 759 } ); 760 761 this.setContent( iframe ); 762 iframeDoc = iframe.contentWindow.document; 763 764 iframeDoc.open(); 765 iframeDoc.write( 766 '<!DOCTYPE html>' + 767 '<html>' + 768 '<head>' + 769 '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' + 770 '</head>' + 771 '<body>' + 772 content + 773 '</body>' + 774 '</html>' 775 ); 776 iframeDoc.close(); 777 778 resize = function() { 779 $( iframe ).height( $( iframeDoc ).height() ); 780 }; 781 782 if ( MutationObserver ) { 783 new MutationObserver( _.debounce( function() { 784 resize(); 785 }, 100 ) ) 786 .observe( iframeDoc.body, { 787 attributes: true, 788 childList: true, 789 subtree: true 790 } ); 791 } else { 792 for ( i = 1; i < 6; i++ ) { 793 setTimeout( resize, i * 700 ); 794 } 795 } 796 } else { 797 this.setContent( content ); 798 } 799 800 this.parseMediaShortcodes(); 801 }, 802 parseMediaShortcodes: function () { 803 var self = this; 804 $( '.wp-audio-shortcode, .wp-video-shortcode', this.node ).each( function ( i, element ) { 805 self.players.push( new MediaElementPlayer( element, self.mejsSettings ) ); 806 } ); 807 }, 808 getHtml: function() { 809 return ''; 810 } 811 } ); 812 637 813 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() ); 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 ''; 686 } 687 return this.template({ content: this.parsed }); 688 } 689 } ), 690 edit: function() {} 814 View: wp.mce.embedView 691 815 } ); 692 816 817 wp.mce.views.register( 'embedURL', { 818 toView: function( content ) { 819 var re = /(?:^|<p>)(https?:\/\/[^\s"]+?)(?:<\/p>\s*|$)/gi, 820 match = re.exec( tinymce.trim( content ) ); 821 822 if ( ! match ) { 823 return; 824 } 825 826 return { 827 index: match.index, 828 content: match[0], 829 options: { 830 url: match[1] 831 } 832 }; 833 }, 834 View: wp.mce.embedView 835 } ); 836 693 837 }(jQuery)); -
trunk/src/wp-includes/js/tinymce/plugins/wpview/plugin.js
r28687 r28748 159 159 // view wrappers. 160 160 editor.on( 'BeforeSetContent', function( event ) { 161 var node; 162 161 163 if ( ! event.content ) { 162 164 return; … … 167 169 } 168 170 171 node = editor.selection.getNode(); 172 173 // When a url is pasted, only try to embed it when pasted in an empty paragrapgh. 174 if ( event.content.match( /^\s*(https?:\/\/[^\s"]+)\s*$/i ) && 175 ( node.nodeName !== 'P' || node.parentNode !== editor.getBody() || ! editor.dom.isEmpty( node ) ) ) { 176 return; 177 } 178 169 179 event.content = wp.mce.views.toViews( event.content ); 170 180 }); 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 181 178 182 // When the editor's content has been updated and the DOM has been -
trunk/src/wp-includes/media-template.php
r28683 r28748 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 }}"> … … 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 … … 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 }}"> … … 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"> … … 1074 1056 <img class="crop-image" src="{{ data.url }}"> 1075 1057 <div class="upload-errors"></div> 1076 </script>1077 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 1058 </script> 1085 1059
Note: See TracChangeset
for help on using the changeset viewer.