Exemplo n.º 1
0
			setURL: function( url ) {
				let index, lastIndex;

				if ( this.url !== url ) {
					this.url = url;

					url = window.decodeURIComponent( url );

					url = url.replace( /^(?:https?:)?\/\/(?:www\.)?/, '' );

					if ( ( index = url.indexOf( '?' ) ) !== -1 ) {
						url = url.slice( 0, index );
					}

					if ( ( index = url.indexOf( '#' ) ) !== -1 ) {
						url = url.slice( 0, index );
					}

					url = url.replace( /(?:index)?\.html$/, '' );

					if ( url.charAt( url.length - 1 ) === '/' ) {
						url = url.slice( 0, -1 );
					}

					// If the URL is longer that 40 chars, concatenate the beginning (after the domain) and ending with ...
					if (
						url.length > 40 &&
						( index = url.indexOf( '/' ) ) !== -1 &&
						( lastIndex = url.lastIndexOf( '/' ) ) !== -1 &&
						lastIndex !== index
					) {
						// If the beginning + ending are shorter that 40 chars, show more of the ending
						if ( index + url.length - lastIndex < 40 ) {
							lastIndex = -( 40 - ( index + 1 ) );
						}

						url = url.slice( 0, index + 1 ) + '\u2026' + url.slice( lastIndex );
					}

					tinymce
						.$( this.getEl().firstChild )
						.attr( 'href', this.url )
						.text( url );
				}
			},
Exemplo n.º 2
0
		function() {
			var Factory = tinymce.ui.Factory,
				settings = editor.settings,
				activeToolbar,
				currentSelection,
				timeout,
				container = editor.getContainer(),
				wpAdminbar = document.getElementById( 'wpadminbar' ),
				mceIframe = document.getElementById( editor.id + '_ifr' ),
				mceToolbar,
				mceStatusbar,
				wpStatusbar,
				isChromeRtl =
					editor.getParam( 'directionality' ) === 'rtl' && /Chrome/.test( navigator.userAgent );

			if ( container ) {
				mceToolbar = tinymce.$( '.mce-toolbar-grp', container )[ 0 ];
				mceStatusbar = tinymce.$( '.mce-statusbar', container )[ 0 ];
			}

			if ( editor.id === 'content' ) {
				wpStatusbar = document.getElementById( 'post-status-info' );
			}

			function create( buttons, bottom ) {
				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' );
							} );
						}

						if ( itemName === 'numlist' ) {
							selection.selectorChanged( 'ol > 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 === 'OL' );
							} );
						}

						if ( item.settings.stateSelector ) {
							selection.selectorChanged(
								item.settings.stateSelector,
								function( state ) {
									item.active( state );
								},
								true
							);
						}

						if ( item.settings.disabledStateSelector ) {
							selection.selectorChanged( item.settings.disabledStateSelector, function( state ) {
								item.disabled( state );
							} );
						}
					}

					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 ];

								if ( typeof item === 'function' ) {
									item = item();
								}

								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,
						},
					],
				} );

				toolbar.bottom = bottom;

				function reposition() {
					if ( ! currentSelection ) {
						return this;
					}

					var scrollX = window.pageXOffset || document.documentElement.scrollLeft,
						scrollY = window.pageYOffset || document.documentElement.scrollTop,
						windowWidth = window.innerWidth,
						windowHeight = window.innerHeight,
						iframeRect = mceIframe
							? mceIframe.getBoundingClientRect()
							: {
									top: 0,
									right: windowWidth,
									bottom: windowHeight,
									left: 0,
									width: windowWidth,
									height: windowHeight,
								},
						toolbar = this.getEl(),
						toolbarWidth = toolbar.offsetWidth,
						toolbarHeight = toolbar.offsetHeight,
						selection = currentSelection.getBoundingClientRect(),
						selectionMiddle = ( selection.left + selection.right ) / 2,
						buffer = 5,
						margin = 8,
						spaceNeeded = toolbarHeight + margin + buffer,
						wpAdminbarBottom = wpAdminbar ? wpAdminbar.getBoundingClientRect().bottom : 0,
						mceToolbarBottom = mceToolbar ? mceToolbar.getBoundingClientRect().bottom : 0,
						mceStatusbarTop = mceStatusbar
							? windowHeight - mceStatusbar.getBoundingClientRect().top
							: 0,
						wpStatusbarTop = wpStatusbar
							? windowHeight - wpStatusbar.getBoundingClientRect().top
							: 0,
						blockedTop = Math.max( 0, wpAdminbarBottom, mceToolbarBottom, iframeRect.top ),
						blockedBottom = Math.max(
							0,
							mceStatusbarTop,
							wpStatusbarTop,
							windowHeight - iframeRect.bottom
						),
						spaceTop = selection.top + iframeRect.top - blockedTop,
						spaceBottom = windowHeight - iframeRect.top - selection.bottom - blockedBottom,
						editorHeight = windowHeight - blockedTop - blockedBottom,
						className = '',
						top,
						left;

					if ( spaceTop >= editorHeight || spaceBottom >= editorHeight ) {
						return this.hide();
					}

					if ( this.bottom ) {
						if ( spaceBottom >= spaceNeeded ) {
							className = ' mce-arrow-up';
							top = selection.bottom + iframeRect.top + scrollY;
						} else if ( spaceTop >= spaceNeeded ) {
							className = ' mce-arrow-down';
							top = selection.top + iframeRect.top + scrollY - toolbarHeight - margin;
						}
					} else {
						if ( spaceTop >= spaceNeeded ) {
							className = ' mce-arrow-down';
							top = selection.top + iframeRect.top + scrollY - toolbarHeight - margin;
						} else if (
							spaceBottom >= spaceNeeded &&
							editorHeight / 2 > selection.bottom + iframeRect.top - blockedTop
						) {
							className = ' mce-arrow-up';
							top = selection.bottom + iframeRect.top + scrollY;
						}
					}

					if ( typeof top === 'undefined' ) {
						top = scrollY + blockedTop + buffer;
					}

					left = selectionMiddle - toolbarWidth / 2 + iframeRect.left + scrollX;

					if ( selection.left < 0 || selection.right > iframeRect.width ) {
						left = iframeRect.left + scrollX + ( iframeRect.width - toolbarWidth ) / 2;
					} else if ( toolbarWidth >= windowWidth ) {
						className += ' mce-arrow-full';
						left = 0;
					} else if (
						( left < 0 && selection.left + toolbarWidth > windowWidth ) ||
						( left + toolbarWidth > windowWidth && selection.right - toolbarWidth < 0 )
					) {
						left = ( windowWidth - toolbarWidth ) / 2;
					} else if ( left < iframeRect.left + scrollX ) {
						className += ' mce-arrow-left';
						left = selection.left + iframeRect.left + scrollX;
					} else if ( left + toolbarWidth > iframeRect.width + iframeRect.left + scrollX ) {
						className += ' mce-arrow-right';
						left = selection.right - toolbarWidth + iframeRect.left + scrollX;
					}

					toolbar.className = toolbar.className.replace( / ?mce-arrow-[\w]+/g, '' ) + className;

					DOM.setStyles( toolbar, {
						left: left,
						top: top,
					} );

					return this;
				}

				toolbar.on( 'show', function() {
					this.reposition();
					if ( isChromeRtl ) {
						editor.$( '.mce-widget.mce-tooltip', document.body ).addClass( 'wp-hide-mce-tooltip' );
					}
				} );

				toolbar.on( 'keydown', function( event ) {
					if ( event.keyCode === 27 ) {
						this.hide();
						editor.focus();
					}
				} );

				editor.on( 'remove', function() {
					toolbar.remove();
				} );

				toolbar.reposition = reposition;
				toolbar.hide().renderTo( document.body );

				return toolbar;
			}

			editor.shortcuts.add( 'alt+119', '', function() {
				var node;

				if ( activeToolbar ) {
					node = activeToolbar.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;

				if ( activeToolbar ) {
					activeToolbar.hide();
				}

				if ( args.toolbar ) {
					activeToolbar = args.toolbar;
					activeToolbar.show();
				} else {
					activeToolbar = false;
				}
			} );

			editor.on( 'focus', function() {
				if ( activeToolbar ) {
					activeToolbar.show();
				}
			} );

			function hide( event ) {
				if ( activeToolbar ) {
					activeToolbar.hide();

					if ( event.type === 'hide' || event.type === 'blur' ) {
						activeToolbar = false;
					} else if ( event.type === 'resizewindow' || event.type === 'scrollwindow' ) {
						clearTimeout( timeout );

						timeout = setTimeout( function() {
							if ( activeToolbar && typeof activeToolbar.show === 'function' ) {
								activeToolbar.show();
							}
						}, 250 );
					}
				}
			}

			DOM.bind( window, 'resize scroll', hide );

			editor.on( 'remove', function() {
				DOM.unbind( window, 'resize scroll', hide );
			} );

			editor.on( 'blur hide', hide );

			editor.wp = editor.wp || {};
			editor.wp._createToolbar = create;
		},