]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/js/tinymce/plugins/wordpress/plugin.js
WordPress 4.2-scripts
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wordpress / plugin.js
index f3af6490ced16185f755fd0f26901c0bd61382ec..b0632aa923016ff9a62d8decaa9055ab5ef071e5 100644 (file)
@@ -4,7 +4,10 @@
 tinymce.ui.FloatPanel.zIndex = 100100;
 
 tinymce.PluginManager.add( 'wordpress', function( editor ) {
-       var DOM = tinymce.DOM, wpAdvButton, modKey, style,
+       var DOM = tinymce.DOM,
+               each = tinymce.each,
+               __ = editor.editorManager.i18n.translate,
+               wpAdvButton, style,
                last = 0;
 
        if ( typeof window.jQuery !== 'undefined' ) {
@@ -29,7 +32,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) {
                        state = 'hide';
                }
 
-               tinymce.each( toolbars, function( toolbar, i ) {
+               each( toolbars, function( toolbar, i ) {
                        if ( i > 0 ) {
                                if ( state === 'hide' ) {
                                        toolbar.hide();
@@ -91,7 +94,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) {
 
                if ( e.content ) {
                        if ( e.content.indexOf( '<!--more' ) !== -1 ) {
-                               title = editor.editorManager.i18n.translate( 'Read more...' );
+                               title = __( 'Read more...' );
 
                                e.content = e.content.replace( /<!--more(.*?)-->/g, function( match, moretext ) {
                                        return '<img src="' + tinymce.Env.transparentSrc + '" data-wp-more="more" data-wp-more-text="' + moretext + '" ' +
@@ -100,7 +103,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) {
                        }
 
                        if ( e.content.indexOf( '<!--nextpage-->' ) !== -1 ) {
-                               title = editor.editorManager.i18n.translate( 'Page break' );
+                               title = __( 'Page break' );
 
                                e.content = e.content.replace( /<!--nextpage-->/g,
                                        '<img src="' + tinymce.Env.transparentSrc + '" data-wp-more="nextpage" class="wp-more-tag mce-wp-nextpage" ' +
@@ -149,7 +152,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) {
                tag = tag || 'more';
                classname += ' mce-wp-' + tag;
                title = tag === 'more' ? 'Read more...' : 'Next page';
-               title = editor.editorManager.i18n.translate( title );
+               title = __( title );
                html = '<img src="' + tinymce.Env.transparentSrc + '" title="' + title + '" class="' + classname + '" ' +
                        'data-wp-more="' + tag + '" data-mce-resize="false" data-mce-placeholder="1" />';
 
@@ -309,7 +312,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) {
 
                bodyClass.push('wp-editor');
 
-               tinymce.each( bodyClass, function( cls ) {
+               each( bodyClass, function( cls ) {
                        if ( cls ) {
                                dom.addClass( doc.body, cls );
                        }
@@ -351,7 +354,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) {
 
                        editor.on( 'PastePostProcess', function( event ) {
                                // Remove empty paragraphs
-                               tinymce.each( dom.select( 'p', event.node ), function( node ) {
+                               each( dom.select( 'p', event.node ), function( node ) {
                                        if ( dom.isEmpty( node ) ) {
                                                dom.remove( node );
                                        }
@@ -394,8 +397,10 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) {
 
        // Remove spaces from empty paragraphs.
        editor.on( 'BeforeSetContent', function( event ) {
+               var paragraph = tinymce.Env.webkit ? '<p><br /></p>' : '<p></p>';
+
                if ( event.content ) {
-                       event.content = event.content.replace( /<p>(?:&nbsp;|\u00a0|\uFEFF| )+<\/p>/gi, '<p></p>' );
+                       event.content = event.content.replace( /<p>(?:&nbsp;|\u00a0|\uFEFF|\s)+<\/p>/gi, paragraph );
                }
        });
 
@@ -406,112 +411,344 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) {
                if ( tinymce.Env.iOS ) {
                        editor.settings.height = 300;
                }
-       });
 
-       // Add custom shortcuts
-       modKey = 'alt+shift';
-
-       editor.addShortcut( modKey + '+c', '', 'JustifyCenter' );
-       editor.addShortcut( modKey + '+r', '', 'JustifyRight' );
-       editor.addShortcut( modKey + '+l', '', 'JustifyLeft' );
-       editor.addShortcut( modKey + '+j', '', 'JustifyFull' );
-       editor.addShortcut( modKey + '+q', '', 'mceBlockQuote' );
-       editor.addShortcut( modKey + '+u', '', 'InsertUnorderedList' );
-       editor.addShortcut( modKey + '+o', '', 'InsertOrderedList' );
-       editor.addShortcut( modKey + '+n', '', 'mceSpellCheck' );
-       editor.addShortcut( modKey + '+s', '', 'unlink' );
-       editor.addShortcut( modKey + '+m', '', 'WP_Medialib' );
-       editor.addShortcut( modKey + '+z', '', 'WP_Adv' );
-       editor.addShortcut( modKey + '+t', '', 'WP_More' );
-       editor.addShortcut( modKey + '+d', '', 'Strikethrough' );
-       editor.addShortcut( modKey + '+h', '', 'WP_Help' );
-       editor.addShortcut( modKey + '+p', '', 'WP_Page' );
-       editor.addShortcut( modKey + '+x', '', 'WP_Code' );
-       editor.addShortcut( 'ctrl+s', '', function() {
-               if ( typeof wp !== 'undefined' && wp.autosave ) {
-                       wp.autosave.server.triggerSave();
-               }
-       });
+               each( {
+                       c: 'JustifyCenter',
+                       r: 'JustifyRight',
+                       l: 'JustifyLeft',
+                       j: 'JustifyFull',
+                       q: 'mceBlockQuote',
+                       u: 'InsertUnorderedList',
+                       o: 'InsertOrderedList',
+                       s: 'unlink',
+                       m: 'WP_Medialib',
+                       z: 'WP_Adv',
+                       t: 'WP_More',
+                       d: 'Strikethrough',
+                       h: 'WP_Help',
+                       p: 'WP_Page',
+                       x: 'WP_Code'
+               }, function( command, key ) {
+                       editor.shortcuts.add( 'access+' + key, '', command );
+               } );
+
+               editor.addShortcut( 'ctrl+s', '', function() {
+                       if ( typeof wp !== 'undefined' && wp.autosave ) {
+                               wp.autosave.server.triggerSave();
+                       }
+               } );
+       } );
+
+       /**
+        * Experimental: create a floating toolbar.
+        * This functionality will change in the next releases. Not recommended for use by plugins.
+        */
+       ( function() {
+               var Factory = tinymce.ui.Factory,
+                       settings = editor.settings,
+                       currentToolbar,
+                       currentSelection;
+
+               function create( buttons ) {
+                       var toolbar,
+                               toolbarItems = [],
+                               buttonGroup;
+
+                       each( buttons, function( item ) {
+                               var itemName;
+
+                               function bindSelectorChanged() {
+                                       var selection = editor.selection;
+
+                                       if ( itemName === 'bullist' ) {
+                                               selection.selectorChanged( 'ul > li', function( state, args ) {
+                                                       var i = args.parents.length,
+                                                               nodeName;
+
+                                                       while ( i-- ) {
+                                                               nodeName = args.parents[ i ].nodeName;
+
+                                                               if ( nodeName === 'OL' || nodeName == 'UL' ) {
+                                                                       break;
+                                                               }
+                                                       }
+
+                                                       item.active( state && nodeName === 'UL' );
+                                               } );
+                                       }
 
-       // popup buttons for the gallery, etc.
-       editor.on( 'init', function() {
-               editor.dom.bind( editor.getWin(), 'scroll', function() {
-                       _hideButtons();
-               });
+                                       if ( itemName === 'numlist' ) {
+                                               selection.selectorChanged( 'ol > li', function( state, args ) {
+                                                       var i = args.parents.length,
+                                                               nodeName;
 
-               editor.dom.bind( editor.getBody(), 'dragstart', function() {
-                       _hideButtons();
-               });
-       });
+                                                       while ( i-- ) {
+                                                               nodeName = args.parents[ i ].nodeName;
 
-       editor.on( 'BeforeExecCommand', function() {
-               _hideButtons();
-       });
+                                                               if ( nodeName === 'OL' || nodeName === 'UL' ) {
+                                                                       break;
+                                                               }
+                                                       }
 
-       editor.on( 'SaveContent', function() {
-               _hideButtons();
-       });
+                                                       item.active( state && nodeName === 'OL' );
+                                               } );
+                                       }
 
-       editor.on( 'MouseDown', function( e ) {
-               if ( e.target.nodeName !== 'IMG' ) {
-                       _hideButtons();
-               }
-       });
+                                       if ( item.settings.stateSelector ) {
+                                               selection.selectorChanged( item.settings.stateSelector, function( state ) {
+                                                       item.active( state );
+                                               }, true );
+                                       }
 
-       editor.on( 'keydown', function( e ) {
-               if ( e.which === tinymce.util.VK.DELETE || e.which === tinymce.util.VK.BACKSPACE ) {
-                       _hideButtons();
-               }
-       });
+                                       if ( item.settings.disabledStateSelector ) {
+                                               selection.selectorChanged( item.settings.disabledStateSelector, function( state ) {
+                                                       item.disabled( state );
+                                               } );
+                                       }
+                               }
 
-       // Internal functions
-       function _setEmbed( c ) {
-               return c.replace( /\[embed\]([\s\S]+?)\[\/embed\][\s\u00a0]*/g, function( a, b ) {
-                       return '<img width="300" height="200" src="' + tinymce.Env.transparentSrc + '" class="wp-oembed" ' +
-                               'alt="'+ b +'" title="'+ b +'" data-mce-resize="false" data-mce-placeholder="1" />';
-               });
-       }
+                               if ( item === '|' ) {
+                                       buttonGroup = null;
+                               } else {
+                                       if ( Factory.has( item ) ) {
+                                               item = {
+                                                       type: item
+                                               };
+
+                                               if ( settings.toolbar_items_size ) {
+                                                       item.size = settings.toolbar_items_size;
+                                               }
+
+                                               toolbarItems.push( item );
+
+                                               buttonGroup = null;
+                                       } else {
+                                               if ( ! buttonGroup ) {
+                                                       buttonGroup = {
+                                                               type: 'buttongroup',
+                                                               items: []
+                                                       };
+
+                                                       toolbarItems.push( buttonGroup );
+                                               }
+
+                                               if ( editor.buttons[ item ] ) {
+                                                       itemName = item;
+                                                       item = editor.buttons[ itemName ];
 
-       function _getEmbed( c ) {
-               return c.replace( /<img[^>]+>/g, function( a ) {
-                       if ( a.indexOf('class="wp-oembed') !== -1 ) {
-                               var u = a.match( /alt="([^\"]+)"/ );
+                                                       if ( typeof item === 'function' ) {
+                                                               item = item();
+                                                       }
 
-                               if ( u[1] ) {
-                                       a = '[embed]' + u[1] + '[/embed]';
+                                                       item.type = item.type || 'button';
+
+                                                       if ( settings.toolbar_items_size ) {
+                                                               item.size = settings.toolbar_items_size;
+                                                       }
+
+                                                       item = Factory.create( item );
+
+                                                       buttonGroup.items.push( item );
+
+                                                       if ( editor.initialized ) {
+                                                               bindSelectorChanged();
+                                                       } else {
+                                                               editor.on( 'init', bindSelectorChanged );
+                                                       }
+                                               }
+                                       }
                                }
+                       } );
+
+                       toolbar = Factory.create( {
+                               type: 'panel',
+                               layout: 'stack',
+                               classes: 'toolbar-grp inline-toolbar-grp',
+                               ariaRoot: true,
+                               ariaRemember: true,
+                               items: [ {
+                                       type: 'toolbar',
+                                       layout: 'flow',
+                                       items: toolbarItems
+                               } ]
+                       } );
+
+                       function hide() {
+                               toolbar.hide();
                        }
 
-                       return a;
-               });
-       }
+                       function reposition() {
+                               var top, left, minTop, className,
+                                       windowPos, adminbar, mceToolbar, boundary,
+                                       boundaryMiddle, boundaryVerticalMiddle, spaceTop,
+                                       spaceBottom, windowWidth, toolbarWidth, toolbarHalf,
+                                       iframe, iframePos, iframeWidth, iframeHeigth,
+                                       toolbarNodeHeight, verticalSpaceNeeded,
+                                       toolbarNode = this.getEl(),
+                                       buffer = 5,
+                                       margin = 8,
+                                       adminbarHeight = 0;
+
+                               if ( ! currentSelection ) {
+                                       return;
+                               }
+
+                               windowPos = window.pageYOffset || document.documentElement.scrollTop;
+                               adminbar = tinymce.$( '#wpadminbar' )[0];
+                               mceToolbar = tinymce.$( '.mce-toolbar-grp', editor.getContainer() )[0];
+                               boundary = currentSelection.getBoundingClientRect();
+                               boundaryMiddle = ( boundary.left + boundary.right ) / 2;
+                               boundaryVerticalMiddle = ( boundary.top + boundary.bottom ) / 2;
+                               spaceTop = boundary.top;
+                               spaceBottom = iframeHeigth - boundary.bottom;
+                               windowWidth = window.innerWidth;
+                               toolbarWidth = toolbarNode.offsetWidth;
+                               toolbarHalf = toolbarWidth / 2;
+                               iframe = document.getElementById( editor.id + '_ifr' );
+                               iframePos = DOM.getPos( iframe );
+                               iframeWidth = iframe.offsetWidth;
+                               iframeHeigth = iframe.offsetHeight;
+                               toolbarNodeHeight = toolbarNode.offsetHeight;
+                               verticalSpaceNeeded = toolbarNodeHeight + margin + buffer;
+
+                               if ( spaceTop >= verticalSpaceNeeded ) {
+                                       className = ' mce-arrow-down';
+                                       top = boundary.top + iframePos.y - toolbarNodeHeight - margin;
+                               } else if ( spaceBottom >= verticalSpaceNeeded ) {
+                                       className = ' mce-arrow-up';
+                                       top = boundary.bottom + iframePos.y;
+                               } else {
+                                       top = buffer;
 
-       function _showButtons( n, id ) {
-               var p1, p2, vp, X, Y;
+                                       if ( boundaryVerticalMiddle >= verticalSpaceNeeded ) {
+                                               className = ' mce-arrow-down';
+                                       } else {
+                                               className = ' mce-arrow-up';
+                                       }
+                               }
 
-               vp = editor.dom.getViewPort( editor.getWin() );
-               p1 = DOM.getPos( editor.getContentAreaContainer() );
-               p2 = editor.dom.getPos( n );
+                               // Make sure the image toolbar is below the main toolbar.
+                               if ( mceToolbar ) {
+                                       minTop = DOM.getPos( mceToolbar ).y + mceToolbar.clientHeight;
+                               } else {
+                                       minTop = iframePos.y;
+                               }
 
-               X = Math.max( p2.x - vp.x, 0 ) + p1.x;
-               Y = Math.max( p2.y - vp.y, 0 ) + p1.y;
+                               // Make sure the image toolbar is below the adminbar (if visible) or below the top of the window.
+                               if ( windowPos ) {
+                                       if ( adminbar && adminbar.getBoundingClientRect().top === 0 ) {
+                                               adminbarHeight = adminbar.clientHeight;
+                                       }
 
-               DOM.setStyles( id, {
-                       'top' : Y + 5 + 'px',
-                       'left' : X + 5 + 'px',
-                       'display': 'block'
-               });
-       }
+                                       if ( windowPos + adminbarHeight > minTop ) {
+                                               minTop = windowPos + adminbarHeight;
+                                       }
+                               }
 
-       function _hideButtons() {
-               DOM.hide( DOM.select( '#wp_editbtns, #wp_gallerybtns' ) );
-       }
+                               if ( top && minTop && ( minTop + buffer > top ) ) {
+                                       top = minTop + buffer;
+                                       className = '';
+                               }
+
+                               left = boundaryMiddle - toolbarHalf;
+                               left += iframePos.x;
+
+                               if ( boundary.left < 0 || boundary.right > iframeWidth ) {
+                                       left = iframePos.x + ( iframeWidth - toolbarWidth ) / 2;
+                               } else if ( toolbarWidth >= windowWidth ) {
+                                       className += ' mce-arrow-full';
+                                       left = 0;
+                               } else if ( ( left < 0 && boundary.left + toolbarWidth > windowWidth ) ||
+                                       ( left + toolbarWidth > windowWidth && boundary.right - toolbarWidth < 0 ) ) {
+
+                                       left = ( windowWidth - toolbarWidth ) / 2;
+                               } else if ( left < iframePos.x ) {
+                                       className += ' mce-arrow-left';
+                                       left = boundary.left + iframePos.x;
+                               } else if ( left + toolbarWidth > iframeWidth + iframePos.x ) {
+                                       className += ' mce-arrow-right';
+                                       left = boundary.right - toolbarWidth + iframePos.x;
+                               }
+
+                               toolbarNode.className = toolbarNode.className.replace( / ?mce-arrow-[\w]+/g, '' );
+                               toolbarNode.className += className;
+
+                               DOM.setStyles( toolbarNode, { 'left': left, 'top': top } );
+
+                               return this;
+                       }
+
+                       toolbar.on( 'show', function() {
+                               currentToolbar = this;
+                               this.reposition();
+                       } );
+
+                       toolbar.on( 'hide', function() {
+                               currentToolbar = false;
+                       } );
+
+                       toolbar.on( 'keydown', function( event ) {
+                               if ( event.keyCode === 27 ) {
+                                       this.hide();
+                                       editor.focus();
+                               }
+                       } );
+
+                       toolbar.on( 'remove', function() {
+                               DOM.unbind( window, 'resize scroll', hide );
+                               editor.dom.unbind( editor.getWin(), 'resize scroll', hide );
+                               editor.off( 'blur hide', hide );
+                       } );
+
+                       editor.once( 'init', function() {
+                               DOM.bind( window, 'resize scroll', hide );
+                               editor.dom.bind( editor.getWin(), 'resize scroll', hide );
+                               editor.on( 'blur hide', hide );
+                       } );
+
+                       toolbar.reposition = reposition;
+                       toolbar.hide().renderTo( document.body );
+
+                       return toolbar;
+               }
+
+               editor.shortcuts.add( 'alt+119', '', function() {
+                       var node;
+
+                       if ( currentToolbar ) {
+                               node = currentToolbar.find( 'toolbar' )[0];
+                               node && node.focus( true );
+                       }
+               } );
+
+               editor.on( 'nodechange', function( event ) {
+                       var collapsed = editor.selection.isCollapsed();
+
+                       var args = {
+                               element: event.element,
+                               parents: event.parents,
+                               collapsed: collapsed
+                       };
+
+                       editor.fire( 'wptoolbar', args );
+
+                       currentSelection = args.selection || args.element;
+
+                       currentToolbar && currentToolbar.hide();
+                       args.toolbar && args.toolbar.show();
+               } );
+
+               editor.wp = editor.wp || {};
+               editor.wp._createToolbar = create;
+       }());
+
+       function noop() {}
 
        // Expose some functions (back-compat)
        return {
-               _showButtons: _showButtons,
-               _hideButtons: _hideButtons,
-               _setEmbed: _setEmbed,
-               _getEmbed: _getEmbed
+               _showButtons: noop,
+               _hideButtons: noop,
+               _setEmbed: noop,
+               _getEmbed: noop
        };
 });