2 tinymce.PluginManager.add('wpgallery', function( editor ) {
4 function replaceGalleryShortcodes( content ) {
5 return content.replace( /\[gallery([^\]]*)\]/g, function( match ) {
6 return html( 'wp-gallery', match );
10 function html( cls, data ) {
11 data = window.encodeURIComponent( data );
12 return '<img src="' + tinymce.Env.transparentSrc + '" class="wp-media mceItem ' + cls + '" ' +
13 'data-wp-media="' + data + '" data-mce-resize="false" data-mce-placeholder="1" />';
16 function restoreMediaShortcodes( content ) {
17 function getAttr( str, name ) {
18 name = new RegExp( name + '=\"([^\"]+)\"' ).exec( str );
19 return name ? window.decodeURIComponent( name[1] ) : '';
22 return content.replace( /(?:<p(?: [^>]+)?>)*(<img [^>]+>)(?:<\/p>)*/g, function( match, image ) {
23 var data = getAttr( image, 'data-wp-media' );
26 return '<p>' + data + '</p>';
33 function editMedia( node ) {
34 var gallery, frame, data;
36 if ( node.nodeName !== 'IMG' ) {
40 // Check if the `wp.media` API exists.
41 if ( typeof wp === 'undefined' || ! wp.media ) {
45 data = window.decodeURIComponent( editor.dom.getAttrib( node, 'data-wp-media' ) );
47 // Make sure we've selected a gallery node.
48 if ( editor.dom.hasClass( node, 'wp-gallery' ) && wp.media.gallery ) {
49 gallery = wp.media.gallery;
50 frame = gallery.edit( data );
52 frame.state('gallery-edit').on( 'update', function( selection ) {
53 var shortcode = gallery.shortcode( selection ).string();
54 editor.dom.setAttrib( node, 'data-wp-media', window.encodeURIComponent( shortcode ) );
60 // Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('...');
61 editor.addCommand( 'WP_Gallery', function() {
62 editMedia( editor.selection.getNode() );
65 editor.on( 'init', function( e ) {
68 // iOS6 doesn't show the buttons properly on click, show them on 'touchstart'
69 if ( 'ontouchstart' in window ) {
70 editor.dom.events.bind( editor.getBody(), 'touchstart', function( e ) {
71 var target = e.target;
73 if ( target.nodeName == 'IMG' && editor.dom.hasClass( target, 'wp-gallery' ) ) {
74 editor.selection.select( target );
75 editor.dom.events.cancel( e );
76 editor.plugins.wordpress._hideButtons();
77 editor.plugins.wordpress._showButtons( target, 'wp_gallerybtns' );
83 editor.on( 'mouseup', function( event ) {
88 dom.removeClass( dom.select( 'img.wp-media-selected' ), 'wp-media-selected' );
91 if ( node.nodeName === 'IMG' && dom.getAttrib( node, 'data-wp-media' ) ) {
92 // Don't trigger on right-click
93 if ( event.button !== 2 ) {
94 if ( dom.hasClass( node, 'wp-media-selected' ) ) {
98 dom.addClass( node, 'wp-media-selected' );
106 // Display gallery, audio or video instead of img in the element path
107 editor.on( 'ResolveName', function( event ) {
108 var dom = editor.dom,
111 if ( node.nodeName === 'IMG' && dom.getAttrib( node, 'data-wp-media' ) ) {
112 if ( dom.hasClass( node, 'wp-gallery' ) ) {
113 event.name = 'gallery';
118 editor.on( 'BeforeSetContent', function( event ) {
119 // 'wpview' handles the gallery shortcode when present
120 if ( ! editor.plugins.wpview ) {
121 event.content = replaceGalleryShortcodes( event.content );
125 editor.on( 'PostProcess', function( event ) {
127 event.content = restoreMediaShortcodes( event.content );