Make WordPress Core

Ticket #30147: 30147.2.patch

File 30147.2.patch, 17.4 KB (added by azaozz, 11 years ago)
  • src/wp-includes/css/editor.css

     
    154154        position: relative;
    155155}
    156156
     157div.mce-inline-toolbar-grp {
     158        border: 1px solid #aaa;
     159        border-radius: 2px;
     160        -webkit-box-shadow: 0 1px 4px rgba( 0, 0, 0, 0.2 )
     161        box-shadow: 0 1px 4px rgba( 0, 0, 0, 0.2 )
     162        -webkit-box-sizing: border-box;
     163        -moz-box-sizing: border-box;
     164        box-sizing: border-box;
     165        margin-bottom: 8px;
     166        position: absolute;
     167        opacity: 0;
     168        -moz-user-select: none;
     169        -webkit-user-select: none;
     170        -ms-user-select: none;
     171        user-select: none;
     172}
     173
     174div.mce-wp-image-toolbar > div.mce-stack-layout {
     175        padding: 1px;
     176}
     177
     178div.mce-inline-toolbar-grp.mce-arrow-up {
     179        margin-bottom: 0;
     180        margin-top: 8px;
     181}
     182
     183div.mce-inline-toolbar-grp.mce-inline-toolbar-grp-active {
     184        transition:
     185                top 0.1s ease-out,
     186                left 0.1s ease-out,
     187                opacity 0.1s ease-in-out;
     188        opacity: 1;
     189}
     190
     191div.mce-inline-toolbar-grp:before,
     192div.mce-inline-toolbar-grp:after {
     193        position: absolute;
     194        left: 50%;
     195        display: block;
     196        width: 0;
     197        height: 0;
     198        border-style: solid;
     199        border-color: transparent;
     200        content: '';
     201}
     202
     203div.mce-inline-toolbar-grp.mce-arrow-up:before {
     204        top: -18px;
     205        border-bottom-color: #aaa;
     206        border-width: 9px;
     207        margin-left: -9px;
     208}
     209
     210div.mce-inline-toolbar-grp.mce-arrow-down:before {
     211        bottom: -18px;
     212        border-top-color: #aaa;
     213        border-width: 9px;
     214        margin-left: -9px;
     215}
     216
     217div.mce-inline-toolbar-grp.mce-arrow-up:after {
     218        top: -16px;
     219        border-bottom-color: #f5f5f5;
     220        border-width: 8px;
     221        margin-left: -8px;
     222}
     223
     224div.mce-inline-toolbar-grp.mce-arrow-down:after {
     225        bottom: -16px;
     226        border-top-color: #f5f5f5;
     227        border-width: 8px;
     228        margin-left: -8px;
     229}
     230
     231div.mce-inline-toolbar-grp.mce-arrow-left:before,
     232div.mce-inline-toolbar-grp.mce-arrow-left:after {
     233        left: 20px;
     234}
     235
     236div.mce-inline-toolbar-grp.mce-arrow-right:before,
     237div.mce-inline-toolbar-grp.mce-arrow-right:after {
     238        right: 11px;
     239        left: auto;
     240}
     241
     242div.mce-inline-toolbar-grp.mce-arrow-full {
     243        right: 0;
     244}
     245
     246div.mce-inline-toolbar-grp.mce-arrow-full > div {
     247        width: 100%;
     248        overflow-x: auto;
     249}
     250
    157251div.mce-toolbar-grp > div {
    158252        padding: 3px;
    159253}
     
    621715i.mce-i-ltr,
    622716i.mce-i-wp_page,
    623717i.mce-i-hr,
     718i.mce-i-dashicon,
    624719.mce-close {
    625720        font: normal 20px/1 'dashicons';
    626721        padding: 0;
  • src/wp-includes/js/tinymce/plugins/wpeditimage/plugin.js

     
    11/* global tinymce */
    22tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
    3         var toolbarActive = false,
    4                 editingImage = false;
     3        var DOM = tinymce.DOM,
     4                settings = editor.settings,
     5                Factory = tinymce.ui.Factory,
     6                each = tinymce.each,
     7                postDivRich = DOM.get( 'postdivrich' ),
     8                tb;
    59
     10        editor.addButton( 'wp_img_remove', {
     11                tooltip: 'Remove',
     12                icon: 'dashicon dashicons-no',
     13                onclick: function() {
     14                        removeImage( editor.selection.getNode() );
     15                }
     16        } );
     17
     18        editor.addButton( 'wp_img_edit', {
     19                tooltip: 'Edit',
     20                icon: 'dashicon dashicons-edit',
     21                onclick: function() {
     22                        editImage( editor.selection.getNode() );
     23                }
     24        } );
     25
     26        each( {
     27                alignleft: 'Align Left',
     28                aligncenter: 'Align Center',
     29                alignright: 'Align Right',
     30                alignnone: 'Remove Alignment'
     31        }, function( tooltip, name ) {
     32                var direction = name.slice( 5 );
     33
     34                editor.addButton( 'wp_img_' + name, {
     35                        tooltip: tooltip,
     36                        icon: 'dashicon dashicons-align-' + direction,
     37                        cmd: 'alignnone' === name ? 'wpAlignNone' : 'Justify' + direction.slice( 0, 1 ).toUpperCase() + direction.slice( 1 ),
     38                        onPostRender: function() {
     39                                var self = this;
     40
     41                                editor.on( 'NodeChange', function( event ) {
     42                                        var node;
     43
     44                                        // Don't bother.
     45                                        if ( event.element.nodeName !== 'IMG' ) {
     46                                                return;
     47                                        }
     48
     49                                        node = editor.dom.getParent( event.element, '.wp-caption' ) || event.element;
     50
     51                                        if ( 'alignnone' === name ) {
     52                                                self.active( ! /\balign(left|center|right)\b/.test( node.className ) );
     53                                        } else {
     54                                                self.active( editor.dom.hasClass( node, name ) );
     55                                        }
     56                                } );
     57                        }
     58                } );
     59        } );
     60
     61        function toolbarConfig() {
     62                var toolbarItems = [],
     63                        buttonGroup;
     64
     65                each( [ 'wp_img_alignleft', 'wp_img_aligncenter', 'wp_img_alignright', 'wp_img_alignnone', 'wp_img_edit', 'wp_img_remove' ], function( item ) {
     66                        var itemName;
     67
     68                        function bindSelectorChanged() {
     69                                var selection = editor.selection;
     70
     71                                if ( item.settings.stateSelector ) {
     72                                        selection.selectorChanged( item.settings.stateSelector, function( state ) {
     73                                                item.active( state );
     74                                        }, true );
     75                                }
     76
     77                                if ( item.settings.disabledStateSelector ) {
     78                                        selection.selectorChanged( item.settings.disabledStateSelector, function( state ) {
     79                                                item.disabled( state );
     80                                        } );
     81                                }
     82                        }
     83
     84                        if ( item === '|' ) {
     85                                buttonGroup = null;
     86                        } else {
     87                                if ( Factory.has( item ) ) {
     88                                        item = {
     89                                                type: item
     90                                        };
     91
     92                                        if ( settings.toolbar_items_size ) {
     93                                                item.size = settings.toolbar_items_size;
     94                                        }
     95
     96                                        toolbarItems.push( item );
     97
     98                                        buttonGroup = null;
     99                                } else {
     100                                        if ( ! buttonGroup ) {
     101                                                buttonGroup = {
     102                                                        type: 'buttongroup',
     103                                                        items: []
     104                                                };
     105
     106                                                toolbarItems.push( buttonGroup );
     107                                        }
     108
     109                                        if ( editor.buttons[ item ] ) {
     110                                                itemName = item;
     111                                                item = editor.buttons[ itemName ];
     112
     113                                                if ( typeof item === 'function' ) {
     114                                                        item = item();
     115                                                }
     116
     117                                                item.type = item.type || 'button';
     118
     119                                                if ( settings.toolbar_items_size ) {
     120                                                        item.size = settings.toolbar_items_size;
     121                                                }
     122
     123                                                item = Factory.create( item );
     124                                                buttonGroup.items.push( item );
     125
     126                                                if ( editor.initialized ) {
     127                                                        bindSelectorChanged();
     128                                                } else {
     129                                                        editor.on( 'init', bindSelectorChanged );
     130                                                }
     131                                        }
     132                                }
     133                        }
     134                } );
     135
     136                return {
     137                        type: 'panel',
     138                        layout: 'stack',
     139                        classes: 'toolbar-grp inline-toolbar-grp wp-image-toolbar',
     140                        ariaRoot: true,
     141                        ariaRemember: true,
     142                        items: [
     143                                {
     144                                        type: 'toolbar',
     145                                        layout: 'flow',
     146                                        items: toolbarItems
     147                                }
     148                        ]
     149                };
     150        }
     151
     152        tb = Factory.create( toolbarConfig() ).renderTo( document.body ).hide();
     153
     154        tb.reposition = function() {
     155                var top, left, minTop, className,
     156                        toolbarEl = this.getEl(),
     157                        buffer = 5,
     158                        margin = 8,
     159                        boundary = editor.selection.getRng().getBoundingClientRect(),
     160                        boundaryMiddle = ( boundary.left + boundary.right ) / 2,
     161                        boundaryVerticalMiddle = ( boundary.top + boundary.bottom ) / 2,
     162                        spaceTop = boundary.top,
     163                        spaceBottom = iframeHeigth - boundary.bottom,
     164                        windowWidth = window.innerWidth,
     165                        toolbarWidth = toolbarEl.offsetWidth,
     166                        toolbarHalf = toolbarWidth / 2,
     167                        iframe = editor.getContentAreaContainer().firstChild,
     168                        iframePos = DOM.getPos( iframe ),
     169                        iframeWidth = iframe.offsetWidth,
     170                        iframeHeigth = iframe.offsetHeight,
     171                        toolbarElHeight = toolbarEl.offsetHeight,
     172                        verticalSpaceNeeded = toolbarElHeight + margin + buffer;
     173
     174                if ( spaceTop >= verticalSpaceNeeded ) {
     175                        className = ' mce-arrow-down';
     176                        top = boundary.top + iframePos.y - toolbarElHeight - margin;
     177                } else if ( spaceBottom >= verticalSpaceNeeded ) {
     178                        className = ' mce-arrow-up';
     179                        top = boundary.bottom + iframePos.y;
     180                } else {
     181                        top = buffer;
     182
     183                        if ( boundaryVerticalMiddle >= verticalSpaceNeeded ) {
     184                                className = ' mce-arrow-down';
     185                        } else {
     186                                className = ' mce-arrow-up';
     187                        }
     188                }
     189
     190                minTop = tinymce.$( '.mce-tinymce .mce-toolbar-grp' )[0];
     191
     192                if ( minTop ) {
     193                        minTop = DOM.getPos( minTop ).y + minTop.clientHeight;
     194                } else {
     195                        minTop = iframePos.top;
     196                }
     197
     198                if ( top && minTop && ( minTop + buffer > top ) ) {
     199                        top = minTop + buffer;
     200                        className = '';
     201                }
     202
     203                left = boundaryMiddle - toolbarHalf;
     204                left += iframePos.x;
     205
     206                if ( toolbarWidth >= windowWidth ) {
     207                        className += ' mce-arrow-full';
     208                        left = 0;
     209                } else if ( ( left < 0 && boundary.left + toolbarWidth > windowWidth ) || ( left + toolbarWidth > windowWidth && boundary.right - toolbarWidth < 0 ) ) {
     210                        left = ( windowWidth - toolbarWidth ) / 2;
     211                } else if ( left < iframePos.x ) {
     212                        className += ' mce-arrow-left';
     213                        left = boundary.left + iframePos.x;
     214                } else if ( left + toolbarWidth > iframeWidth + iframePos.x ) {
     215                        className += ' mce-arrow-right';
     216                        left = boundary.right - toolbarWidth + iframePos.x;
     217                }
     218
     219                toolbarEl.className = toolbarEl.className.replace( / ?mce-arrow-[\w]+/g, '' );
     220                toolbarEl.className += className;
     221
     222                DOM.setStyles( toolbarEl, { 'left': left, 'top': top } );
     223
     224                return this;
     225        };
     226
     227        editor.on( 'nodechange', function( event ) {
     228                if ( event.element.nodeName !== 'IMG' ) {
     229                        tb.hide();
     230                        return;
     231                }
     232
     233                setTimeout( function() {
     234                        var element = editor.selection.getNode();
     235
     236                        if ( element.nodeName === 'IMG' ) {
     237                                if ( tb._visible ) {
     238                                        tb.reposition();
     239                                } else {
     240                                        tb.show();
     241                                }
     242                        } else {
     243                                tb.hide();
     244                        }
     245                }, 50 );
     246        } );
     247
     248        tb.on( 'show', function() {
     249                var self = this;
     250
     251                setTimeout( function() {
     252                        self._visible && DOM.addClass( self.getEl(), 'mce-inline-toolbar-grp-active' );
     253                }, 100 );
     254        } );
     255
     256        tb.on( 'hide', function() {
     257                DOM.removeClass( this.getEl(), 'mce-inline-toolbar-active' );
     258        } );
     259
     260        function hide() {
     261                tb.hide();
     262        }
     263
     264        DOM.bind( window, 'resize scroll', hide );
     265
     266        editor.on( 'init', function() {
     267                DOM.bind( editor.getWin(), 'resize scroll', hide );
     268        } );
     269
     270        editor.on( 'blur hide', hide );
     271
     272        // 119 = F8
     273        editor.shortcuts.add( 'Alt+119', '', function() {
     274                var item = tb.find( 'toolbar' )[0];
     275
     276                item && item.focus( true );
     277        } );
     278
    6279        function parseShortcode( content ) {
    7280                return content.replace( /(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function( a, b, c ) {
    8281                        var id, align, classes, caption, img, width,
     
    372645                }
    373646
    374647                editor.nodeChanged();
    375                 // Refresh the toolbar
    376                 addToolbar( imageNode );
    377648        }
    378649
    379650        function editImage( img ) {
     
    414685                frame.on( 'close', function() {
    415686                        editor.focus();
    416687                        frame.detach();
    417                         editingImage = false;
    418688                });
    419689
    420690                frame.open();
     
    444714                        editor.dom.remove( node );
    445715                }
    446716
    447                 removeToolbar();
    448717                editor.nodeChanged();
    449718                editor.undoManager.add();
    450719        }
    451720
    452         function addToolbar( node ) {
    453                 var rectangle, toolbarHtml, toolbar, left,
    454                         dom = editor.dom;
    455 
    456                 removeToolbar();
    457 
    458                 // Don't add to placeholders
    459                 if ( ! node || node.nodeName !== 'IMG' || isPlaceholder( node ) ) {
    460                         return;
    461                 }
    462 
    463                 dom.setAttrib( node, 'data-wp-imgselect', 1 );
    464                 rectangle = dom.getRect( node );
    465 
    466                 toolbarHtml = '<i class="dashicons dashicons-edit edit" data-mce-bogus="all"></i>' +
    467                         '<i class="dashicons dashicons-no-alt remove" data-mce-bogus="all"></i>';
    468 
    469                 toolbar = dom.create( 'p', {
    470                         'id': 'wp-image-toolbar',
    471                         'data-mce-bogus': 'all',
    472                         'contenteditable': false
    473                 }, toolbarHtml );
    474 
    475                 if ( editor.rtl ) {
    476                         left = rectangle.x + rectangle.w - 82;
    477                 } else {
    478                         left = rectangle.x;
    479                 }
    480 
    481                 editor.getBody().appendChild( toolbar );
    482                 dom.setStyles( toolbar, {
    483                         top: rectangle.y,
    484                         left: left
    485                 });
    486 
    487                 toolbarActive = true;
    488         }
    489 
    490         function removeToolbar() {
    491                 var toolbar = editor.dom.get( 'wp-image-toolbar' );
    492 
    493                 if ( toolbar ) {
    494                         editor.dom.remove( toolbar );
    495                 }
    496 
    497                 editor.dom.setAttrib( editor.dom.select( 'img[data-wp-imgselect]' ), 'data-wp-imgselect', null );
    498 
    499                 editingImage = false;
    500                 toolbarActive = false;
    501         }
    502 
    503         function isPlaceholder( node ) {
    504                 var dom = editor.dom;
    505 
    506                 if ( dom.hasClass( node, 'mceItem' ) || dom.getAttrib( node, 'data-mce-placeholder' ) ||
    507                         dom.getAttrib( node, 'data-mce-object' ) ) {
    508 
    509                         return true;
    510                 }
    511 
    512                 return false;
    513         }
    514 
    515         function isToolbarButton( node ) {
    516                 return ( node && node.nodeName === 'I' && node.parentNode.id === 'wp-image-toolbar' );
    517         }
    518 
    519         function edit( event ) {
    520                 var image,
    521                         node = event.target,
    522                         dom = editor.dom;
    523 
    524                 // Don't trigger on right-click
    525                 if ( event.button && event.button > 1 ) {
    526                         return;
    527                 }
    528 
    529                 if ( isToolbarButton( node ) ) {
    530                         image = dom.select( 'img[data-wp-imgselect]' )[0];
    531 
    532                         if ( image ) {
    533                                 editor.selection.select( image );
    534 
    535                                 if ( dom.hasClass( node, 'remove' ) ) {
    536                                         removeImage( image );
    537                                 } else if ( dom.hasClass( node, 'edit' ) ) {
    538                                         if ( ! editingImage ) {
    539                                                 editImage( image );
    540                                                 editingImage = true;
    541                                         }
    542                                 }
    543                         }
    544 
    545                         event.preventDefault();
    546                 } else if ( node.nodeName === 'IMG' && ! editor.dom.getAttrib( node, 'data-wp-imgselect' ) && ! isPlaceholder( node ) ) {
    547                         addToolbar( node );
    548                 } else if ( node.nodeName !== 'IMG' ) {
    549                         removeToolbar();
    550                 }
    551         }
    552 
    553         if ( 'ontouchend' in document ) {
    554                 editor.on( 'click', function( event ) {
    555                         var target = event.target;
    556 
    557                         if ( editingImage && target.nodeName === 'IMG' ) {
    558                                 event.preventDefault();
    559                         }
    560 
    561                         if ( isToolbarButton( target ) ) {
    562                                 event.preventDefault();
    563                                 event.stopPropagation();
    564                         }
    565                 });
    566         }
    567 
    568         editor.on( 'mouseup touchend', edit );
    569 
    570721        editor.on( 'init', function() {
    571722                var dom = editor.dom,
    572723                        captionClass = editor.getParam( 'wpeditimage_html5_captions' ) ? 'html5-captions' : 'html4-captions';
     
    803954                        if ( node.nodeName === 'IMG' && dom.getParent( node, '.wp-caption' ) ) {
    804955                                event.preventDefault();
    805956                        }
    806 
    807                         // Remove toolbar to avoid an orphaned toolbar when dragging an image to a new location
    808                         removeToolbar();
    809957                });
    810958
    811959                // Prevent IE11 from making dl.wp-caption resizable
     
    821969                                        event.target.focus();
    822970                                }
    823971                        });
    824 
    825                         editor.on( 'click', function( event ) {
    826                                 if ( event.target.nodeName === 'IMG' && dom.getAttrib( event.target, 'data-wp-imgselect' ) &&
    827                                         dom.getParent( event.target, 'dl.wp-caption' ) ) {
    828 
    829                                         editor.getBody().focus();
    830                                 }
    831                         });
    832972                }
    833973        });
    834974
     
    855995                                                dom.setStyle( parent, 'width', width + 'px' );
    856996                                        }
    857997                                }
    858                                 // refresh toolbar
    859                                 addToolbar( node );
    860998                        });
    861999                }
    8621000    });
    8631001
    8641002        editor.on( 'BeforeExecCommand', function( event ) {
    865                 var node, p, DL, align,
     1003                var node, p, DL, align, replacement,
    8661004                        cmd = event.command,
    8671005                        dom = editor.dom;
    8681006
     
    8751013                                editor.selection.setCursorLocation( p, 0 );
    8761014                                editor.nodeChanged();
    8771015                        }
    878                 } else if ( cmd === 'JustifyLeft' || cmd === 'JustifyRight' || cmd === 'JustifyCenter' ) {
     1016                } else if ( cmd === 'JustifyLeft' || cmd === 'JustifyRight' || cmd === 'JustifyCenter' || cmd === 'wpAlignNone' ) {
    8791017                        node = editor.selection.getNode();
    880                         align = cmd.substr(7).toLowerCase();
    881                         align = 'align' + align;
    882                         DL = dom.getParent( node, 'dl.wp-caption' );
     1018                        align = 'align' + cmd.slice( 7 ).toLowerCase();
     1019                        DL = editor.dom.getParent( node, '.wp-caption' );
    8831020
    884                         removeToolbar();
     1021                        if ( node.nodeName !== 'IMG' && ! DL ) {
     1022                                return;
     1023                        }
    8851024
    886                         if ( DL ) {
    887                                 // When inside an image caption, set the align* class on dl.wp-caption
    888                                 if ( dom.hasClass( DL, align ) ) {
    889                                         dom.removeClass( DL, align );
    890                                         dom.addClass( DL, 'alignnone' );
    891                                 } else {
    892                                         DL.className = DL.className.replace( /align[^ ]+/g, '' );
    893                                         dom.addClass( DL, align );
    894                                 }
     1025                        node = DL || node;
    8951026
    896                                 if ( node.nodeName === 'IMG' ) {
    897                                         // Re-select the image to update resize handles, etc.
    898                                         editor.nodeChanged();
    899                                 }
    900 
    901                                 event.preventDefault();
     1027                        if ( editor.dom.hasClass( node, align ) ) {
     1028                                replacement = ' alignnone';
     1029                        } else {
     1030                                replacement = ' ' + align;
    9021031                        }
    9031032
    904                         if ( node.nodeName === 'IMG' ) {
    905                                 if ( dom.hasClass( node, align ) ) {
    906                                         // The align class is being removed
    907                                         dom.addClass( node, 'alignnone' );
    908                                 } else {
    909                                         dom.removeClass( node, 'alignnone' );
    910                                 }
     1033                        node.className = node.className.replace( / ?align(left|center|right|none)/g, '' ) + replacement;
     1034
     1035                        editor.nodeChanged();
     1036                        event.preventDefault();
     1037
     1038                        if ( tb ) {
     1039                                tb.reposition();
    9111040                        }
    9121041                }
    9131042        });
     
    9601089                                removeImage( node );
    9611090                                return false;
    9621091                        }
    963 
    964                         removeToolbar();
    9651092                }
    966 
    967                 // Most key presses will replace the image so we need to remove the toolbar
    968                 if ( toolbarActive ) {
    969                         if ( event.ctrlKey || event.metaKey || event.altKey || ( keyCode < 48 && keyCode !== VK.SPACEBAR ) ) {
    970                                 return;
    971                         }
    972 
    973                         removeToolbar();
    974                 }
    9751093        });
    9761094
    977         editor.on( 'mousedown', function( event ) {
    978                 if ( isToolbarButton( event.target ) ) {
    979                         if ( tinymce.Env.ie ) {
    980                                 // Stop IE > 8 from making the wrapper resizable on mousedown
    981                                 event.preventDefault();
    982                         }
    983                 } else if ( event.target.nodeName !== 'IMG' ) {
    984                         removeToolbar();
    985                 }
    986         });
    987 
    988         // Remove from undo levels
    989         editor.on( 'BeforeAddUndo', function( event ) {
    990                 event.level.content = event.level.content.replace( / data-wp-imgselect="1"/g, '' );
    991         });
    992 
    9931095        // After undo/redo FF seems to set the image height very slowly when it is set to 'auto' in the CSS.
    9941096        // This causes image.getBoundingClientRect() to return wrong values and the resize handles are shown in wrong places.
    9951097        // Collapse the selection to remove the resize handles.
     
    10011103                });
    10021104        }
    10031105
    1004         editor.on( 'cut wpview-selected', function() {
    1005                 removeToolbar();
    1006         });
    1007 
    10081106        editor.wpSetImgCaption = function( content ) {
    10091107                return parseShortcode( content );
    10101108        };
     
    10221120        editor.on( 'PostProcess', function( event ) {
    10231121                if ( event.get ) {
    10241122                        event.content = editor.wpGetImgCaption( event.content );
    1025                         event.content = event.content.replace( / data-wp-imgselect="1"/g, '' );
    10261123                }
    10271124        });
    10281125