Make WordPress Core

Ticket #36211: 36211.patch

File 36211.patch, 2.9 KB (added by azaozz, 10 years ago)
  • src/wp-includes/js/tinymce/plugins/wpeditimage/plugin.js

     
    11/* global tinymce */
    22tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
    3         var toolbar, serializer, touchOnImage,
     3        var toolbar, serializer, touchOnImage, letPasteInCaption,
    44                each = tinymce.each,
    55                trim = tinymce.trim,
    66                iOS = tinymce.Env.iOS;
     
    853853                                }
    854854                        });
    855855                }
    856     });
     856        });
    857857
     858        editor.on( 'pastePostProcess', function( event ) {
     859                var element, captionNode = editor.dom.getParent( editor.selection.getNode(), 'dd.wp-caption-dd' );
     860
     861                if ( captionNode ) {
     862                        var walker = new tinymce.dom.TreeWalker( event.node, event.node );
     863
     864                        while ( element = walker.next() ) {
     865                                if ( editor.dom.isBlock( element ) ) {
     866                                        // Mark nodes for removal. Can't remove them now as that stops the walker.
     867                                        editor.dom.setAttrib( element, 'data-wp-removeme', 'true' );
     868                                } else if ( element.nodeName === 'IMG' || element.nodeName === 'AUDIO' || element.nodeName === 'VIDEO' || element.nodeName === 'svg' ) {
     869                                        // Can't be pasted in the image caption. Will be inserted in a new paragraph under it.
     870                                        return;
     871                                }
     872                        }
     873
     874                        editor.$( '[data-wp-removeme]', event.node ).each( function( i, node ) {
     875                                // Insert <br> where the blocks used to be. Otherwise longer text runs together and looks bad.
     876                                editor.dom.insertAfter( editor.dom.create( 'br' ), node );
     877                                editor.dom.remove( node, true );
     878                        });
     879
     880                        // Pasted HTML is cleaned up for inserting in the caption.
     881                        letPasteInCaption = true;
     882                }
     883        });
     884
    858885        editor.on( 'BeforeExecCommand', function( event ) {
    859                 var node, p, DL, align, replacement,
     886                var node, p, DL, align, replacement, captionParent,
    860887                        cmd = event.command,
    861888                        dom = editor.dom;
    862889
    863890                if ( cmd === 'mceInsertContent' ) {
    864                         // When inserting content, if the caret is inside a caption create new paragraph under
    865                         // and move the caret there
    866                         if ( node = dom.getParent( editor.selection.getNode(), 'div.mceTemp' ) ) {
     891                        node = editor.selection.getNode();
     892                        captionParent = dom.getParent( node, 'div.mceTemp' );
     893
     894                        if ( captionParent ) {
     895                                if ( letPasteInCaption && dom.getParent( node, 'dd.wp-caption-dd' ) && event.value.data && event.value.data.paste ) {
     896                                        letPasteInCaption = false;
     897                                        // We are in the caption element, and in 'paste' context,
     898                                        // and the pasted HTML was cleaned up on 'pastePostProcess' above.
     899                                        // Let it be pasted!
     900                                        return;
     901                                }
     902
     903                                // Make new paragraph under the caption parent element and move the caret there.
    867904                                p = dom.create( 'p' );
    868                                 dom.insertAfter( p, node );
     905                                dom.insertAfter( p, captionParent );
    869906                                editor.selection.setCursorLocation( p, 0 );
    870907                                editor.nodeChanged();
    871908                        }