Ticket #29841: 29841.5.patch
File 29841.5.patch, 37.4 KB (added by , 10 years ago) |
---|
-
src/wp-includes/js/mce-view.js
1 1 /* global tinymce */ 2 /** 2 3 /* 4 * The TinyMCE view API. 5 * 3 6 * Note: this API is "experimental" meaning that it will probably change 4 7 * in the next few releases based on feedback from 3.9.0. 5 8 * If you decide to use it, please follow the development closely. 6 9 */ 7 8 // Ensure the global `wp` object exists.9 window.wp = window.wp || {};10 11 10 ( function( $ ) { 12 11 'use strict'; 13 12 14 13 var views = {}, 15 14 instances = {}, 16 media = wp.media, 17 mediaWindows = [], 18 windowIdx = 0, 19 waitInterval = 50, 20 viewOptions = ['encodedText']; 15 viewOptions = [ 'encodedText' ]; 21 16 22 // Create the `wp.mce` object if necessary.17 window.wp = window.wp || {}; 23 18 wp.mce = wp.mce || {}; 24 19 25 20 /** 26 * wp.mce.View27 *28 * A Backbone-like View constructor intended for use when rendering a TinyMCE View. The main difference is29 * that the TinyMCE View is not tied to a particular DOM node.30 *31 * @param {Object} [options={}]32 */33 wp.mce.View = function( options ) {34 options = options || {};35 this.type = options.type;36 _.extend( this, _.pick( options, viewOptions ) );37 this.initialize.apply( this, arguments );38 };39 40 _.extend( wp.mce.View.prototype, {41 initialize: function() {},42 getHtml: function() {43 return '';44 },45 loadingPlaceholder: function() {46 return '' +47 '<div class="loading-placeholder">' +48 '<div class="dashicons dashicons-admin-media"></div>' +49 '<div class="wpview-loading"><ins></ins></div>' +50 '</div>';51 },52 render: function( force ) {53 if ( force || ! this.rendered() ) {54 this.unbind();55 56 this.setContent(57 '<p class="wpview-selection-before">\u00a0</p>' +58 '<div class="wpview-body" contenteditable="false">' +59 '<div class="toolbar mce-arrow-down">' +60 ( _.isFunction( views[ this.type ].edit ) ? '<div class="dashicons dashicons-edit edit"></div>' : '' ) +61 '<div class="dashicons dashicons-no remove"></div>' +62 '</div>' +63 '<div class="wpview-content wpview-type-' + this.type + '">' +64 ( this.getHtml() || this.loadingPlaceholder() ) +65 '</div>' +66 ( this.overlay ? '<div class="wpview-overlay"></div>' : '' ) +67 '</div>' +68 '<p class="wpview-selection-after">\u00a0</p>',69 'wrap'70 );71 72 $( this ).trigger( 'ready' );73 74 this.rendered( true );75 }76 },77 unbind: function() {},78 getEditors: function( callback ) {79 var editors = [];80 81 _.each( tinymce.editors, function( editor ) {82 if ( editor.plugins.wpview ) {83 if ( callback ) {84 callback( editor );85 }86 87 editors.push( editor );88 }89 }, this );90 91 return editors;92 },93 getNodes: function( callback ) {94 var nodes = [],95 self = this;96 97 this.getEditors( function( editor ) {98 $( editor.getBody() )99 .find( '[data-wpview-text="' + self.encodedText + '"]' )100 .each( function ( i, node ) {101 if ( callback ) {102 callback( editor, node, $( node ).find( '.wpview-content' ).get( 0 ) );103 }104 105 nodes.push( node );106 } );107 } );108 109 return nodes;110 },111 setContent: function( html, option ) {112 this.getNodes( function ( editor, node, content ) {113 var el = ( option === 'wrap' || option === 'replace' ) ? node : content,114 insert = html;115 116 if ( _.isString( insert ) ) {117 insert = editor.dom.createFragment( insert );118 }119 120 if ( option === 'replace' ) {121 editor.dom.replace( insert, el );122 } else {123 el.innerHTML = '';124 el.appendChild( insert );125 }126 } );127 },128 /* jshint scripturl: true */129 setIframes: function ( head, body ) {130 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,131 importStyles = this.type === 'video' || this.type === 'audio' || this.type === 'playlist';132 133 if ( head || body.indexOf( '<script' ) !== -1 ) {134 this.getNodes( function ( editor, node, content ) {135 var dom = editor.dom,136 styles = '',137 bodyClasses = editor.getBody().className || '',138 iframe, iframeDoc, i, resize;139 140 content.innerHTML = '';141 head = head || '';142 143 if ( importStyles ) {144 if ( ! wp.mce.views.sandboxStyles ) {145 tinymce.each( dom.$( 'link[rel="stylesheet"]', editor.getDoc().head ), function( link ) {146 if ( link.href && link.href.indexOf( 'skins/lightgray/content.min.css' ) === -1 &&147 link.href.indexOf( 'skins/wordpress/wp-content.css' ) === -1 ) {148 149 styles += dom.getOuterHTML( link ) + '\n';150 }151 });152 153 wp.mce.views.sandboxStyles = styles;154 } else {155 styles = wp.mce.views.sandboxStyles;156 }157 }158 159 // Seems Firefox needs a bit of time to insert/set the view nodes, or the iframe will fail160 // especially when switching Text => Visual.161 setTimeout( function() {162 iframe = dom.add( content, 'iframe', {163 src: tinymce.Env.ie ? 'javascript:""' : '',164 frameBorder: '0',165 allowTransparency: 'true',166 scrolling: 'no',167 'class': 'wpview-sandbox',168 style: {169 width: '100%',170 display: 'block'171 }172 } );173 174 iframeDoc = iframe.contentWindow.document;175 176 iframeDoc.open();177 iframeDoc.write(178 '<!DOCTYPE html>' +179 '<html>' +180 '<head>' +181 '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' +182 head +183 styles +184 '<style>' +185 'html {' +186 'background: transparent;' +187 'padding: 0;' +188 'margin: 0;' +189 '}' +190 'body#wpview-iframe-sandbox {' +191 'background: transparent;' +192 'padding: 1px 0 !important;' +193 'margin: -1px 0 0 !important;' +194 '}' +195 'body#wpview-iframe-sandbox:before,' +196 'body#wpview-iframe-sandbox:after {' +197 'display: none;' +198 'content: "";' +199 '}' +200 '</style>' +201 '</head>' +202 '<body id="wpview-iframe-sandbox" class="' + bodyClasses + '">' +203 body +204 '</body>' +205 '</html>'206 );207 iframeDoc.close();208 209 resize = function() {210 var $iframe, iframeDocHeight;211 212 // Make sure the iframe still exists.213 if ( iframe.contentWindow ) {214 $iframe = $( iframe );215 iframeDocHeight = $( iframeDoc.body ).height();216 217 if ( $iframe.height() !== iframeDocHeight ) {218 $iframe.height( iframeDocHeight );219 editor.nodeChanged();220 }221 }222 };223 224 if ( MutationObserver ) {225 new MutationObserver( _.debounce( function() {226 resize();227 }, 100 ) )228 .observe( iframeDoc.body, {229 attributes: true,230 childList: true,231 subtree: true232 } );233 } else {234 for ( i = 1; i < 6; i++ ) {235 setTimeout( resize, i * 700 );236 }237 }238 239 if ( importStyles ) {240 editor.on( 'wp-body-class-change', function() {241 iframeDoc.body.className = editor.getBody().className;242 });243 }244 }, waitInterval );245 });246 } else {247 this.setContent( body );248 }249 },250 setError: function( message, dashicon ) {251 this.setContent(252 '<div class="wpview-error">' +253 '<div class="dashicons dashicons-' + ( dashicon ? dashicon : 'no' ) + '"></div>' +254 '<p>' + message + '</p>' +255 '</div>'256 );257 },258 rendered: function( value ) {259 var notRendered;260 261 this.getNodes( function( editor, node ) {262 if ( value != null ) {263 $( node ).data( 'rendered', value === true );264 } else {265 notRendered = notRendered || ! $( node ).data( 'rendered' );266 }267 } );268 269 return ! notRendered;270 }271 } );272 273 // take advantage of the Backbone extend method274 wp.mce.View.extend = Backbone.View.extend;275 276 /**277 21 * wp.mce.views 278 22 * 279 23 * A set of utilities that simplifies adding custom UI within a TinyMCE editor. … … 283 27 wp.mce.views = { 284 28 285 29 /** 286 * wp.mce.views.register( type, view )287 *288 30 * Registers a new TinyMCE view. 289 31 * 290 * @param type 291 * @param constructor 292 * 32 * @param {String} The view type. 33 * @param {Object} A constructor. 293 34 */ 294 35 register: function( type, constructor ) { 295 36 var defaultConstructor = { … … 319 60 }, 320 61 321 62 /** 322 * wp.mce.views.get( id ) 323 * 324 * Returns a TinyMCE view constructor. 63 * Unregisters a TinyMCE view. 325 64 * 326 * @param type65 * @param {String} The view type. 327 66 */ 328 get: function( type ) {329 returnviews[ type ];67 unregister: function( type ) { 68 delete views[ type ]; 330 69 }, 331 70 332 71 /** 333 * wp.mce.views.unregister( type ) 334 * 335 * Unregisters a TinyMCE view. 72 * Returns a TinyMCE view constructor. 336 73 * 337 * @param type74 * @param {String} The view type. 338 75 */ 339 unregister: function( type ) {340 deleteviews[ type ];76 get: function( type ) { 77 return views[ type ]; 341 78 }, 342 79 343 80 /** 344 * wp.mce.views.unbind( editor )345 *346 81 * The editor DOM is being rebuilt, run cleanup. 347 82 */ 348 83 unbind: function() { … … 352 87 }, 353 88 354 89 /** 355 * toViews( content )356 * Scans a `content` string for each view's pattern, replacing any357 * matches with wrapper elements, and creates a new instance for358 * every match,which triggers the related data to be fetched.90 * Scans a given string for each view's pattern, 91 * replacing any matches with markers, 92 * and creates a new instance for every match, 93 * which triggers the related data to be fetched. 359 94 * 360 * @param content95 * @param {String} String to scan. 361 96 */ 362 97 toViews: function( content ) { 363 98 var pieces = [ { content: content } ], 364 99 current; 365 100 366 _.each( views, function( view, viewType ) {101 _.each( views, function( view, type ) { 367 102 current = pieces.slice(); 368 103 pieces = []; 369 104 … … 379 114 380 115 // Iterate through the string progressively matching views 381 116 // and slicing the string as we go. 382 while ( remaining && ( result = view.toView( remaining )) ) {117 while ( remaining && ( result = view.toView( remaining ) ) ) { 383 118 // Any text before the match becomes an unprocessed piece. 384 119 if ( result.index ) { 385 pieces.push( { content: remaining.substring( 0, result.index ) });120 pieces.push( { content: remaining.substring( 0, result.index ) } ); 386 121 } 387 122 388 123 // Add the processed piece for the match. 389 pieces.push( {390 content: wp.mce.views.toView( viewType, result.content, result.options ),124 pieces.push( { 125 content: wp.mce.views.toView( type, result.content, result.options ), 391 126 processed: true 392 } );127 } ); 393 128 394 129 // Update the remaining content. 395 130 remaining = remaining.slice( result.index + result.content.length ); … … 398 133 // There are no additional matches. If any content remains, 399 134 // add it as an unprocessed piece. 400 135 if ( remaining ) { 401 pieces.push( { content: remaining });136 pieces.push( { content: remaining } ); 402 137 } 403 } );404 } );138 } ); 139 } ); 405 140 406 return _.pluck( pieces, 'content' ).join( '');141 return _.pluck( pieces, 'content' ).join( '' ); 407 142 }, 408 143 409 144 /** 410 * Create a placeholder for a particular view type 411 * 412 * @param viewType 413 * @param text 414 * @param options 145 * Returns the marked text for a particular view type, 146 * and creates a new instance for that view type if it does not exist. 147 * The text will be returned unmarked if no view of that type is registered. 148 * 149 * @param {String} The view type. 150 * @param {String} The textual representation of the view. 151 * @param {Object} Options. 415 152 * 153 * @return {String} The markered text. 416 154 */ 417 toView: function( viewType, text, options ) { 418 var view = wp.mce.views.get( viewType ), 419 encodedText = window.encodeURIComponent( text ), 420 instance, viewOptions; 421 155 toView: function( type, text, options ) { 156 var view = this.get( type ), 157 encodedText = window.encodeURIComponent( text ); 422 158 423 159 if ( ! view ) { 424 160 return text; 425 161 } 426 162 427 if ( ! wp.mce.views.getInstance( encodedText ) ) { 428 viewOptions = options; 429 viewOptions.type = viewType; 430 viewOptions.encodedText = encodedText; 431 instance = new view.View( viewOptions ); 432 instances[ encodedText ] = instance; 163 if ( ! this.getInstance( encodedText ) ) { 164 instances[ encodedText ] = new view.View( _.extend( options, { 165 encodedText: encodedText, 166 type: type 167 } ) ); 433 168 } 434 169 435 return wp.html.string({ 436 tag: 'div', 437 438 attrs: { 439 'class': 'wpview-wrap', 440 'data-wpview-text': encodedText, 441 'data-wpview-type': viewType 442 }, 443 444 content: '\u00a0' 445 }); 170 return '<p data-wpview-marker="' + encodedText + '">' + text + '</p>'; 446 171 }, 447 172 448 173 /** 449 * Refresh views after an update is made 174 * Refresh views after an update is made. 450 175 * 451 * @param view {object} being refreshed452 * @param text {string} textual representation of the view453 * @param force {Boolean} whether to force rendering176 * @param {Object} The view to refresh. 177 * @param {String} The textual representation of the view. 178 * @param {Boolean} Whether or not to force rendering. 454 179 */ 455 180 refreshView: function( view, text, force ) { 456 var encodedText = window.encodeURIComponent( text ), 457 viewOptions, 458 result, instance; 459 460 instance = wp.mce.views.getInstance( encodedText ); 461 462 if ( ! instance ) { 463 result = view.toView( text ); 464 viewOptions = result.options; 465 viewOptions.type = view.type; 466 viewOptions.encodedText = encodedText; 467 instance = new view.View( viewOptions ); 468 instances[ encodedText ] = instance; 181 var encodedText = encodeURIComponent( text ); 182 183 if ( ! this.getInstance( encodedText ) ) { 184 instances[ encodedText ] = new view.View( _.extend( view.toView( text ).options, { 185 encodedText: encodedText, 186 type: view.type 187 } ) ); 469 188 } 470 189 471 instance .render( force );190 instances[ encodedText ].render( force ); 472 191 }, 473 192 193 /** 194 * @param {String} The textual representation of the view, encoded. 195 */ 474 196 getInstance: function( encodedText ) { 475 197 return instances[ encodedText ]; 476 198 }, 477 199 200 getInstances: function() { 201 return instances; 202 }, 203 478 204 /** 479 * render( scope )205 * Renders any view instances. 480 206 * 481 * Renders any view instances inside a DOM node `scope`. 207 * View instances are detected by the presence of markers. 208 * To generate markers, pass your content through `toViews`. 482 209 * 483 * View instances are detected by the presence of wrapper elements. 484 * To generate wrapper elements, pass your content through 485 * `wp.mce.view.toViews( content )`. 210 * @param {Boolean} Whether or not to force rendering. 486 211 */ 487 212 render: function( force ) { 488 213 _.each( instances, function( instance ) { … … 490 215 } ); 491 216 }, 492 217 493 edit: function( node ) { 494 var viewType = $( node ).data('wpview-type'), 495 view = wp.mce.views.get( viewType ); 218 /** 219 * @param {HTMLElement} A view node. 220 */ 221 edit: function( editor, node ) { 222 var instance = this.getInstance( $( node ).data( 'wpview-text' ) ), 223 view = wp.mce.views.get( instance.type ), 224 self = this; 496 225 497 if ( view ) {226 if ( view && view.edit ) { 498 227 view.edit( node ); 228 } else if ( instance && instance.edit ) { 229 instance.edit( decodeURIComponent( instance.encodedText ), function( text ) { 230 $( node ).data( 'rendered', false ); 231 editor.dom.setAttrib( node, 'data-wpview-text', encodeURIComponent( text ) ); 232 self.refreshView( view, text ); 233 } ); 499 234 } 500 235 } 501 236 }; 502 237 503 wp.mce.views.register( 'gallery', { 238 /** 239 * A Backbone-like View constructor intended for use when rendering a TinyMCE View. The main difference is 240 * that the TinyMCE View is not tied to a particular DOM node. 241 * 242 * @param {Object} Options. 243 */ 244 wp.mce.View = function( options ) { 245 options = options || {}; 246 this.type = options.type; 247 _.extend( this, _.pick( options, viewOptions ) ); 248 this.initialize.apply( this, arguments ); 249 }; 250 251 _.extend( wp.mce.View.prototype, { 252 253 /** 254 * Whether or not to display a loader. 255 * 256 * @type {Boolean} 257 */ 258 loader: true, 259 260 /** 261 * Runs after the view instance is created. 262 */ 263 initialize: function() {}, 264 265 /** 266 * Retuns the HTML string to render in the view. 267 * 268 * @return {*} 269 */ 270 getHtml: function() {}, 271 272 /** 273 * Retuns the HTML string to render in the view. 274 * 275 * @param {Boolean} Whether or not to rerender. 276 */ 277 render: function( rerender ) { 278 var i = 0; 279 280 // If there's nothing to render an no loader needs to be shown, stop. 281 if ( ! this.loader && ! this.getHtml() ) { 282 return; 283 } 284 285 // We're about to rerender all views of this instance, so unbind rendered views. 286 rerender && this.unbind(); 287 288 // Replace any left over markers. 289 this.replaceMarkers(); 290 291 if ( this.getHtml() ) { 292 this.setContent( this.getHtml(), function( editor, node ) { 293 $( node ).data( 'rendered', true ); 294 this.bindNodes.apply( this, arguments ); 295 i++; 296 }, rerender ? null : false ); 297 298 // Use `bindNodes` if possible. 299 i && $( this ).trigger( 'ready' ); 300 } else { 301 this.setLoader(); 302 } 303 }, 304 305 /** 306 * Runs after a view is added to the DOM. 307 * 308 * @param {tinymce.Editor} The editor instance the view node is in. 309 * @param {HTMLElement} The view node. 310 * @param {HTMLElement} The view's content node. 311 */ 312 bindNodes: function() {}, 313 314 /** 315 * Runs before a view is removed from the DOM. 316 */ 317 unbind: function() { 318 this.getNodes( function() { 319 this.unbindNodes.apply( this, arguments ); 320 }, true ); 321 }, 322 323 /** 324 * Runs before a view is removed from the DOM. 325 * 326 * @param {tinymce.Editor} The editor instance the view node is in. 327 * @param {HTMLElement} The view node. 328 * @param {HTMLElement} The view's content node. 329 */ 330 unbindNodes: function() {}, 331 332 /** 333 * Gets all the TinyMCE editors that support views. 334 * 335 * @param {Function} A callback (optional). 336 * 337 * @return {tinymce.Editor[]} An array of TinyMCE editor instances. 338 */ 339 getEditors: function( callback ) { 340 var editors = []; 341 342 _.each( tinymce.editors, function( editor ) { 343 if ( editor.plugins.wpview ) { 344 editors.push( editor ); 345 callback && callback.call( this, editor ); 346 } 347 }, this ); 348 349 return editors; 350 }, 351 352 /** 353 * Gets all the view nodes in each editor. 354 * 355 * @param {Function} A callback (optional). 356 * @param {Boolean} Get (un)rendered nodes (optional). 357 * 358 * @return {HTMLElement[]} An array of view nodes. 359 */ 360 getNodes: function( callback, rendered ) { 361 var nodes = []; 362 363 this.getEditors( function( editor ) { 364 var self = this; 365 366 $( editor.getBody() ) 367 .find( '[data-wpview-text="' + self.encodedText + '"]' ) 368 .filter( function() { 369 var data; 370 371 if ( rendered == null ) { 372 return true; 373 } 374 375 data = $( this ).data( 'rendered' ) === true; 376 377 return rendered ? data : ! data; 378 } ) 379 .each( function ( i, node ) { 380 nodes.push( node ); 381 callback && callback.call( self, editor, node, $( node ).find( '.wpview-content' ).get( 0 ) ); 382 } ); 383 } ); 384 385 return nodes; 386 }, 387 388 /** 389 * Gets all the markers in each editor. 390 * 391 * @param {Function} A callback (optional). 392 * 393 * @return {HTMLElement[]} An array of marker nodes. 394 */ 395 getMarkers: function( callback ) { 396 var markers = []; 397 398 this.getEditors( function( editor ) { 399 var self = this; 400 401 $( editor.getBody() ) 402 .find( '[data-wpview-marker="' + this.encodedText + '"]' ) 403 .each( function( i, node ) { 404 markers.push( node ); 405 callback && callback.call( self, editor, node ); 406 } ); 407 } ); 408 409 return markers; 410 }, 411 412 /** 413 * Replaces all markers with view nodes. 414 */ 415 replaceMarkers: function() { 416 this.getMarkers( function( editor, node ) { 417 if ( $( node ).text() !== decodeURIComponent( this.encodedText ) ) { 418 editor.dom.setAttrib( node, 'data-wpview-marker', null ); 419 return; 420 } 421 422 editor.dom.replace( 423 editor.dom.createFragment( 424 '<div class="wpview-wrap" data-wpview-text="' + this.encodedText + '" data-wpview-type="' + this.type + '">' + 425 '<p class="wpview-selection-before">\u00a0</p>' + 426 '<div class="wpview-body" contenteditable="false">' + 427 '<div class="toolbar mce-arrow-down">' + 428 ( this.edit || views[ this.type ].edit ? '<div class="dashicons dashicons-edit edit"></div>' : '' ) + 429 '<div class="dashicons dashicons-no remove"></div>' + 430 '</div>' + 431 '<div class="wpview-content wpview-type-' + this.type + '"></div>' + 432 ( this.overlay ? '<div class="wpview-overlay"></div>' : '' ) + 433 '</div>' + 434 '<p class="wpview-selection-after">\u00a0</p>' + 435 '</div>' 436 ), 437 node 438 ); 439 } ); 440 }, 441 442 /** 443 * Removes all markers. 444 */ 445 removeMarkers: function() { 446 this.getMarkers( function( editor, node ) { 447 editor.dom.setAttrib( node, 'data-wpview-marker', null ); 448 } ); 449 }, 450 451 /** 452 * Gets all the markers in each editor. 453 * 454 * @param {(String|HTMLElement)} The content to set. 455 * @param {Function} A callback (optional). 456 * @param {Boolean} Only set for (un)rendered nodes (optional). 457 */ 458 setContent: function( content, callback, rendered ) { 459 if ( _.isObject( content ) && content.body.indexOf( '<script' ) !== -1 ) { 460 this.setIframes( content.head, content.body, callback, rendered ); 461 } else if ( _.isString( content ) && content.indexOf( '<script' ) !== -1 ) { 462 this.setIframes( null, content, callback, rendered ); 463 } else { 464 this.getNodes( function( editor, node, contentNode ) { 465 content = content.body || content; 466 467 contentNode.innerHTML = ''; 468 contentNode.appendChild( _.isString( content ) ? editor.dom.createFragment( content ) : content ); 469 470 callback && callback.apply( this, arguments ); 471 }, rendered ); 472 } 473 }, 474 475 /** 476 * Set an iframe as the view content. 477 */ 478 setIframes: function( head, body, callback, rendered ) { 479 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver, 480 importStyles = this.type === 'video' || this.type === 'audio' || this.type === 'playlist'; 481 482 this.getNodes( function( editor, node, content ) { 483 var dom = editor.dom, 484 styles = '', 485 bodyClasses = editor.getBody().className || '', 486 iframe, iframeDoc, i, resize; 487 488 content.innerHTML = ''; 489 head = head || ''; 490 491 if ( importStyles ) { 492 if ( ! wp.mce.views.sandboxStyles ) { 493 tinymce.each( dom.$( 'link[rel="stylesheet"]', editor.getDoc().head ), function( link ) { 494 if ( link.href && link.href.indexOf( 'skins/lightgray/content.min.css' ) === -1 && 495 link.href.indexOf( 'skins/wordpress/wp-content.css' ) === -1 ) { 496 497 styles += dom.getOuterHTML( link ) + '\n'; 498 } 499 }); 500 501 wp.mce.views.sandboxStyles = styles; 502 } else { 503 styles = wp.mce.views.sandboxStyles; 504 } 505 } 506 507 // Seems Firefox needs a bit of time to insert/set the view nodes, 508 // or the iframe will fail especially when switching Text => Visual. 509 setTimeout( function() { 510 iframe = dom.add( content, 'iframe', { 511 /* jshint scripturl: true */ 512 src: tinymce.Env.ie ? 'javascript:""' : '', 513 frameBorder: '0', 514 allowTransparency: 'true', 515 scrolling: 'no', 516 'class': 'wpview-sandbox', 517 style: { 518 width: '100%', 519 display: 'block' 520 } 521 } ); 522 523 iframeDoc = iframe.contentWindow.document; 524 525 iframeDoc.open(); 526 527 iframeDoc.write( 528 '<!DOCTYPE html>' + 529 '<html>' + 530 '<head>' + 531 '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' + 532 head + 533 styles + 534 '<style>' + 535 'html {' + 536 'background: transparent;' + 537 'padding: 0;' + 538 'margin: 0;' + 539 '}' + 540 'body#wpview-iframe-sandbox {' + 541 'background: transparent;' + 542 'padding: 1px 0 !important;' + 543 'margin: -1px 0 0 !important;' + 544 '}' + 545 'body#wpview-iframe-sandbox:before,' + 546 'body#wpview-iframe-sandbox:after {' + 547 'display: none;' + 548 'content: "";' + 549 '}' + 550 '</style>' + 551 '</head>' + 552 '<body id="wpview-iframe-sandbox" class="' + bodyClasses + '">' + 553 body + 554 '</body>' + 555 '</html>' 556 ); 557 558 iframeDoc.close(); 559 560 resize = function() { 561 var $iframe, iframeDocHeight; 562 563 // Make sure the iframe still exists. 564 if ( iframe.contentWindow ) { 565 $iframe = $( iframe ); 566 iframeDocHeight = $( iframeDoc.body ).height(); 567 568 if ( $iframe.height() !== iframeDocHeight ) { 569 $iframe.height( iframeDocHeight ); 570 editor.nodeChanged(); 571 } 572 } 573 }; 574 575 if ( MutationObserver ) { 576 new MutationObserver( _.debounce( function() { 577 resize(); 578 }, 100 ) ) 579 .observe( iframeDoc.body, { 580 attributes: true, 581 childList: true, 582 subtree: true 583 } ); 584 } else { 585 for ( i = 1; i < 6; i++ ) { 586 setTimeout( resize, i * 700 ); 587 } 588 } 589 590 if ( importStyles ) { 591 editor.on( 'wp-body-class-change', function() { 592 iframeDoc.body.className = editor.getBody().className; 593 } ); 594 } 595 }, 50 ); 596 597 callback && callback.apply( this, arguments ); 598 }, rendered ); 599 }, 600 601 /** 602 * Set a loader. 603 */ 604 setLoader: function() { 605 this.setContent( 606 '<div class="loading-placeholder">' + 607 '<div class="dashicons dashicons-admin-media"></div>' + 608 '<div class="wpview-loading"><ins></ins></div>' + 609 '</div>' 610 ); 611 }, 612 613 /** 614 * Set an error. 615 * 616 * @param {String} The error message to set. 617 * @param {String} A dashicon ID (optional). {@link https://developer.wordpress.org/resource/dashicons/} 618 */ 619 setError: function( message, dashicon ) { 620 this.setContent( 621 '<div class="wpview-error">' + 622 '<div class="dashicons dashicons-' + ( dashicon || 'no' ) + '"></div>' + 623 '<p>' + message + '</p>' + 624 '</div>' 625 ); 626 } 627 } ); 628 629 // Take advantage of the Backbone extend method. 630 wp.mce.View.extend = Backbone.View.extend; 631 } )( jQuery ); 632 633 /* 634 * The WordPress core TinyMCE views. 635 * Views for the gallery, audio, video, playlist and embed shortcodes, 636 * and a view for embeddable URLs. 637 */ 638 ( function( $, views ) { 639 views.register( 'gallery', { 504 640 View: { 505 template: media.template( 'editor-gallery' ),506 641 507 // The fallback post ID to use as a parent for galleries that don't 508 // specify the `ids` or `include` parameters. 509 // 510 // Uses the hidden input on the edit posts page by default. 511 postID: $('#post_ID').val(), 642 template: wp.media.template( 'editor-gallery' ), 643 644 postID: $( '#post_ID' ).val(), 512 645 513 646 initialize: function( options ) { 514 647 this.shortcode = options.shortcode; … … 520 653 521 654 this.attachments = wp.media.gallery.attachments( this.shortcode, this.postID ); 522 655 this.dfd = this.attachments.more().done( function() { 523 self.render( true);656 self.render(); 524 657 } ); 525 658 }, 526 659 … … 556 689 }; 557 690 558 691 return this.template( options ); 559 } 560 }, 561 562 edit: function( node ) { 563 var gallery = wp.media.gallery, 564 self = this, 565 frame, data; 566 567 data = window.decodeURIComponent( $( node ).attr('data-wpview-text') ); 568 frame = gallery.edit( data ); 692 }, 569 693 570 frame.state('gallery-edit').on( 'update', function( selection ) { 571 var shortcode = gallery.shortcode( selection ).string(); 572 $( node ).attr( 'data-wpview-text', window.encodeURIComponent( shortcode ) ); 573 wp.mce.views.refreshView( self, shortcode, true ); 574 }); 694 edit: function( text, callback ) { 695 var gallery = wp.media.gallery, 696 frame = gallery.edit( text ); 697 698 frame.state( 'gallery-edit' ).on( 'update', function( selection ) { 699 // var shortcode = gallery.shortcode( selection ).string(); 700 // $( node ).attr( 'data-wpview-text', encodeURIComponent( shortcode ) ); 701 // wp.mce.views.refreshView( self, shortcode, true ); 702 callback( gallery.shortcode( selection ).string() ); 703 } ); 575 704 576 frame.on( 'close', function() { 577 frame.detach(); 578 }); 705 frame.on( 'close', function() { 706 frame.detach(); 707 } ); 708 } 579 709 } 580 710 } ); 581 711 … … 593 723 initialize: function( options ) { 594 724 var self = this; 595 725 596 this.shortcode = options.shortcode; 597 598 _.bindAll( this, 'setIframes', 'setNodes', 'fetch', 'stopPlayers' ); 599 $( this ).on( 'ready', this.setNodes ); 600 601 $( document ).on( 'media:edit', this.stopPlayers ); 726 if ( options.url ) { 727 this.loader = false; 728 this.shortcode = wp.media.embed.shortcode( { 729 url: options.url 730 } ); 731 } else { 732 this.shortcode = options.shortcode; 733 } 602 734 603 735 this.fetch(); 604 736 605 737 this.getEditors( function( editor ) { 606 editor.on( 'hide', function () { 607 mediaWindows = []; 608 windowIdx = 0; 738 editor.on( 'wpview-selected', function() { 609 739 self.stopPlayers(); 610 740 } ); 611 });612 },613 614 pauseOtherWindows: function ( win ) {615 _.each( mediaWindows, function ( mediaWindow ) {616 if ( mediaWindow.sandboxId !== win.sandboxId ) {617 _.each( mediaWindow.mejs.players, function ( player ) {618 player.pause();619 } );620 }621 741 } ); 622 742 }, 623 743 624 iframeLoaded: function (win) {625 return _.bind( function () {626 var callback;627 if ( ! win.mejs || _.isEmpty( win.mejs.players ) ) {628 return;629 }630 631 win.sandboxId = windowIdx;632 windowIdx++;633 mediaWindows.push( win );634 635 callback = _.bind( function () {636 this.pauseOtherWindows( win );637 }, this );638 639 if ( ! _.isEmpty( win.mejs.MediaPluginBridge.pluginMediaElements ) ) {640 _.each( win.mejs.MediaPluginBridge.pluginMediaElements, function ( mediaElement ) {641 mediaElement.addEventListener( 'play', callback );642 } );643 }644 645 _.each( win.mejs.players, function ( player ) {646 $( player.node ).on( 'play', callback );647 }, this );648 }, this );649 },650 651 listenToSandboxes: function () {652 _.each( this.getNodes(), function ( node ) {653 var win, iframe = $( '.wpview-sandbox', node ).get( 0 );654 if ( iframe && ( win = iframe.contentWindow ) ) {655 $( win ).load( _.bind( this.iframeLoaded( win ), this ) );656 }657 }, this );658 },659 660 deferredListen: function () {661 window.setTimeout( _.bind( this.listenToSandboxes, this ), this.getNodes().length * waitInterval );662 },663 664 setNodes: function () {665 if ( this.parsed ) {666 this.setIframes( this.parsed.head, this.parsed.body );667 this.deferredListen();668 } else {669 this.fail();670 }671 },672 673 744 fetch: function () { 674 745 var self = this; 675 746 … … 681 752 } 682 753 } ) 683 754 .done( function( response ) { 684 if ( response ) { 685 self.parsed = response; 686 self.setIframes( response.head, response.body ); 687 self.deferredListen(); 688 } else { 689 self.fail( true ); 690 } 755 self.parsed = response; 756 self.render(); 691 757 } ) 692 758 .fail( function( response ) { 693 self.fail( response || true ); 694 } ); 695 }, 696 697 fail: function( error ) { 698 if ( ! this.error ) { 699 if ( error ) { 700 this.error = error; 701 } else { 702 return; 703 } 704 } 705 706 if ( this.error.message ) { 707 if ( ( this.error.type === 'not-embeddable' && this.type === 'embed' ) || this.error.type === 'not-ssl' || 708 this.error.type === 'no-items' ) { 709 710 this.setError( this.error.message, 'admin-media' ); 759 if ( self.type === 'embedURL' ) { 760 self.removeMarkers(); 711 761 } else { 712 this.setContent( '<p>' + this.original + '</p>', 'replace' );762 self.setError( response.message || response.statusText, 'admin-media' ); 713 763 } 714 } else if ( this.error.statusText ) { 715 this.setError( this.error.statusText, 'admin-media' ); 716 } else if ( this.original ) { 717 this.setContent( '<p>' + this.original + '</p>', 'replace' ); 718 } 764 } ); 719 765 }, 720 766 721 767 stopPlayers: function( remove ) { 722 var rem = remove === 'remove';723 724 768 this.getNodes( function( editor, node, content ) { 725 var p, win, 726 iframe = $( 'iframe.wpview-sandbox', content ).get(0); 769 var win = $( 'iframe.wpview-sandbox', content ).get( 0 ); 727 770 728 if ( iframe && ( win = iframe.contentWindow ) && win.mejs ) { 729 // Sometimes ME.js may show a "Download File" placeholder and player.remove() doesn't exist there. 730 try { 731 for ( p in win.mejs.players ) { 732 win.mejs.players[p].pause(); 733 734 if ( rem ) { 735 win.mejs.players[p].remove(); 736 } 737 } 738 } catch( er ) {} 771 if ( win && ( win = win.contentWindow ) && win.mejs ) { 772 _.each( win.mejs.players, function( player ) { 773 try { 774 player[ remove ? 'remove' : 'pause' ](); 775 } catch ( e ) {} 776 } ); 739 777 } 740 } );778 } ); 741 779 }, 742 780 743 unbind: function() {744 this.stopPlayers( 'remove' );781 getHtml: function() { 782 return this.parsed; 745 783 } 746 784 }, 747 785 … … 760 798 self = this, 761 799 frame, data, callback; 762 800 763 $( document ).trigger( 'media:edit' ); 801 _.each( views.getInstances(), function( instance ) { 802 instance.stopPlayers && instance.stopPlayers(); 803 } ); 764 804 765 data = window.decodeURIComponent( $( node ).attr('data-wpview-text') );805 data = decodeURIComponent( $( node ).attr('data-wpview-text') ); 766 806 frame = media.edit( data ); 807 767 808 frame.on( 'close', function() { 768 809 frame.detach(); 769 810 } ); 770 811 771 812 callback = function( selection ) { 772 813 var shortcode = wp.media[ self.type ].shortcode( selection ).string(); 773 $( node ).attr( 'data-wpview-text', window.encodeURIComponent( shortcode ) );814 $( node ).attr( 'data-wpview-text', encodeURIComponent( shortcode ) ); 774 815 wp.mce.views.refreshView( self, shortcode ); 775 816 frame.detach(); 776 817 }; 818 777 819 if ( _.isArray( self.state ) ) { 778 820 _.each( self.state, function (state) { 779 821 frame.state( state ).on( 'update', callback ); … … 781 823 } else { 782 824 frame.state( self.state ).on( 'update', callback ); 783 825 } 826 784 827 frame.open(); 785 828 } 786 829 }; … … 790 833 * 791 834 * @mixes wp.mce.av 792 835 */ 793 wp.mce.views.register( 'video', _.extend( {}, wp.mce.av, {836 views.register( 'video', _.extend( {}, wp.mce.av, { 794 837 state: 'video-details' 795 838 } ) ); 796 839 … … 799 842 * 800 843 * @mixes wp.mce.av 801 844 */ 802 wp.mce.views.register( 'audio', _.extend( {}, wp.mce.av, {845 views.register( 'audio', _.extend( {}, wp.mce.av, { 803 846 state: 'audio-details' 804 847 } ) ); 805 848 … … 808 851 * 809 852 * @mixes wp.mce.av 810 853 */ 811 wp.mce.views.register( 'playlist', _.extend( {}, wp.mce.av, {854 views.register( 'playlist', _.extend( {}, wp.mce.av, { 812 855 state: [ 'playlist-edit', 'video-playlist-edit' ] 813 856 } ) ); 814 857 … … 818 861 wp.mce.embedMixin = { 819 862 View: _.extend( {}, wp.mce.av.View, { 820 863 overlay: true, 821 action: 'parse-embed', 822 initialize: function( options ) { 823 this.content = options.content; 824 this.original = options.url || options.shortcode.string(); 825 826 if ( options.url ) { 827 this.shortcode = media.embed.shortcode( { 828 url: options.url 829 } ); 830 } else { 831 this.shortcode = options.shortcode; 832 } 833 834 _.bindAll( this, 'setIframes', 'setNodes', 'fetch' ); 835 $( this ).on( 'ready', this.setNodes ); 836 837 this.fetch(); 838 } 864 action: 'parse-embed' 839 865 } ), 840 866 edit: function( node ) { 841 var embed = media.embed,867 var embed = wp.media.embed, 842 868 self = this, 843 869 frame, 844 870 data, 845 871 isURL = 'embedURL' === this.type; 846 872 847 $( document ).trigger( 'media:edit' ); 848 849 data = window.decodeURIComponent( $( node ).attr('data-wpview-text') ); 873 data = decodeURIComponent( $( node ).attr('data-wpview-text') ); 850 874 frame = embed.edit( data, isURL ); 875 851 876 frame.on( 'close', function() { 852 877 frame.detach(); 853 878 } ); 879 854 880 frame.state( 'embed' ).props.on( 'change:url', function (model, url) { 855 881 if ( ! url ) { 856 882 return; 857 883 } 858 884 frame.state( 'embed' ).metadata = model.toJSON(); 859 885 } ); 886 860 887 frame.state( 'embed' ).on( 'select', function() { 861 888 var shortcode; 862 889 … … 865 892 } else { 866 893 shortcode = embed.shortcode( frame.state( 'embed' ).metadata ).string(); 867 894 } 868 $( node ).attr( 'data-wpview-text', window.encodeURIComponent( shortcode ) );895 $( node ).attr( 'data-wpview-text', encodeURIComponent( shortcode ) ); 869 896 wp.mce.views.refreshView( self, shortcode ); 870 897 frame.detach(); 871 898 } ); 899 872 900 frame.open(); 873 901 } 874 902 }; 875 903 876 wp.mce.views.register( 'embed', _.extend( {}, wp.mce.embedMixin ) );904 views.register( 'embed', _.extend( {}, wp.mce.embedMixin ) ); 877 905 878 wp.mce.views.register( 'embedURL', _.extend( {}, wp.mce.embedMixin, {906 views.register( 'embedURL', _.extend( {}, wp.mce.embedMixin, { 879 907 toView: function( content ) { 880 908 var re = /(^|<p>)(https?:\/\/[^\s"]+?)(<\/p>\s*|$)/gi, 881 909 match = re.exec( tinymce.trim( content ) ); … … 893 921 }; 894 922 } 895 923 } ) ); 896 897 }(jQuery)); 924 } )( jQuery, wp.mce.views ); -
src/wp-includes/js/tinymce/plugins/wpview/plugin.js
341 341 editor.focus(); 342 342 } 343 343 344 wp.mce.views.edit( view );344 wp.mce.views.edit( editor, view ); 345 345 return false; 346 346 } else if ( editor.dom.hasClass( event.target, 'remove' ) ) { 347 347 removeView( view );