1 /* global getUserSetting, setUserSetting */
2 ( function( tinymce ) {
3 // Set the minimum value for the modals z-index higher than #wpadminbar (100000)
4 tinymce.ui.FloatPanel.zIndex = 100100;
6 tinymce.PluginManager.add( 'wordpress', function( editor ) {
7 var wpAdvButton, style,
10 __ = editor.editorManager.i18n.translate,
13 hasWpautop = ( wp && wp.editor && wp.editor.autop && editor.getParam( 'wpautop', true ) );
16 $( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] );
19 function toggleToolbars( state ) {
20 var iframe, initial, toolbars,
23 initial = ( state === 'hide' );
25 if ( editor.theme.panel ) {
26 toolbars = editor.theme.panel.find('.toolbar:not(.menubar)');
29 if ( ! toolbars || toolbars.length < 2 || ( state === 'hide' && ! toolbars[1].visible() ) ) {
33 if ( ! state && toolbars[1].visible() ) {
37 each( toolbars, function( toolbar, i ) {
39 if ( state === 'hide' ) {
49 if ( pixels && ! initial ) {
50 // Resize iframe, not needed in iOS
51 if ( ! tinymce.Env.iOS ) {
52 iframe = editor.getContentAreaContainer().firstChild;
53 DOM.setStyle( iframe, 'height', iframe.clientHeight + pixels );
56 if ( state === 'hide' ) {
57 setUserSetting('hidetb', '0');
58 wpAdvButton && wpAdvButton.active( false );
60 setUserSetting('hidetb', '1');
61 wpAdvButton && wpAdvButton.active( true );
65 editor.fire( 'wp-toolbar-toggle' );
68 // Add the kitchen sink button :)
69 editor.addButton( 'wp_adv', {
70 tooltip: 'Toolbar Toggle',
72 onPostRender: function() {
74 wpAdvButton.active( getUserSetting( 'hidetb' ) === '1' ? true : false );
78 // Hide the toolbars after loading
79 editor.on( 'PostRender', function() {
80 if ( editor.getParam( 'wordpress_adv_hidden', true ) && getUserSetting( 'hidetb', '0' ) === '0' ) {
81 toggleToolbars( 'hide' );
85 editor.addCommand( 'WP_Adv', function() {
89 editor.on( 'focus', function() {
90 window.wpActiveEditor = editor.id;
93 editor.on( 'BeforeSetContent', function( event ) {
96 if ( event.content ) {
97 if ( event.content.indexOf( '<!--more' ) !== -1 ) {
98 title = __( 'Read more...' );
100 event.content = event.content.replace( /<!--more(.*?)-->/g, function( match, moretext ) {
101 return '<img src="' + tinymce.Env.transparentSrc + '" data-wp-more="more" data-wp-more-text="' + moretext + '" ' +
102 'class="wp-more-tag mce-wp-more" alt="" title="' + title + '" data-mce-resize="false" data-mce-placeholder="1" />';
106 if ( event.content.indexOf( '<!--nextpage-->' ) !== -1 ) {
107 title = __( 'Page break' );
109 event.content = event.content.replace( /<!--nextpage-->/g,
110 '<img src="' + tinymce.Env.transparentSrc + '" data-wp-more="nextpage" class="wp-more-tag mce-wp-nextpage" ' +
111 'alt="" title="' + title + '" data-mce-resize="false" data-mce-placeholder="1" />' );
114 if ( event.load && event.format !== 'raw' && hasWpautop ) {
115 event.content = wp.editor.autop( event.content );
118 if ( event.content.indexOf( '<script' ) !== -1 || event.content.indexOf( '<style' ) !== -1 ) {
119 event.content = event.content.replace( /<(script|style)[^>]*>[\s\S]*?<\/\1>/g, function( match, tag ) {
121 'src="' + tinymce.Env.transparentSrc + '" ' +
122 'data-wp-preserve="' + encodeURIComponent( match ) + '" ' +
123 'data-mce-resize="false" ' +
124 'data-mce-placeholder="1" '+
125 'class="mce-object" ' +
126 'width="20" height="20" '+
127 'alt="<' + tag + '>" ' +
128 'title="<' + tag + '>" ' +
133 // Remove spaces from empty paragraphs.
134 // Try to avoid a lot of backtracking, can freeze the editor. See #35890 and #38294.
135 event.content = event.content.replace( /<p>([^<>]+)<\/p>/gi, function( tag, text ) {
136 if ( text === ' ' || ! /\S/.test( text ) ) {
137 return '<p><br /></p>';
145 editor.on( 'PostProcess', function( event ) {
147 event.content = event.content.replace(/<img[^>]+>/g, function( image ) {
152 if ( image.indexOf( 'data-wp-more="more"' ) !== -1 ) {
153 if ( match = image.match( /data-wp-more-text="([^"]+)"/ ) ) {
157 string = '<!--more' + moretext + '-->';
158 } else if ( image.indexOf( 'data-wp-more="nextpage"' ) !== -1 ) {
159 string = '<!--nextpage-->';
160 } else if ( image.indexOf( 'data-wp-preserve' ) !== -1 ) {
161 if ( match = image.match( / data-wp-preserve="([^"]+)"/ ) ) {
162 string = decodeURIComponent( match[1] );
166 return string || image;
171 // Display the tag name instead of img in element path
172 editor.on( 'ResolveName', function( event ) {
175 if ( event.target.nodeName === 'IMG' && ( attr = editor.dom.getAttrib( event.target, 'data-wp-more' ) ) ) {
181 editor.addCommand( 'WP_More', function( tag ) {
182 var parent, html, title,
183 classname = 'wp-more-tag',
185 node = editor.selection.getNode();
188 classname += ' mce-wp-' + tag;
189 title = tag === 'more' ? 'Read more...' : 'Next page';
191 html = '<img src="' + tinymce.Env.transparentSrc + '" alt="" title="' + title + '" class="' + classname + '" ' +
192 'data-wp-more="' + tag + '" data-mce-resize="false" data-mce-placeholder="1" />';
195 if ( node.nodeName === 'BODY' || ( node.nodeName === 'P' && node.parentNode.nodeName === 'BODY' ) ) {
196 editor.insertContent( html );
200 // Get the top level parent node
201 parent = dom.getParent( node, function( found ) {
202 if ( found.parentNode && found.parentNode.nodeName === 'BODY' ) {
207 }, editor.getBody() );
210 if ( parent.nodeName === 'P' ) {
211 parent.appendChild( dom.create( 'p', null, html ).firstChild );
213 dom.insertAfter( dom.create( 'p', null, html ), parent );
216 editor.nodeChanged();
220 editor.addCommand( 'WP_Code', function() {
221 editor.formatter.toggle('code');
224 editor.addCommand( 'WP_Page', function() {
225 editor.execCommand( 'WP_More', 'nextpage' );
228 editor.addCommand( 'WP_Help', function() {
229 var access = tinymce.Env.mac ? __( 'Ctrl + Alt + letter:' ) : __( 'Shift + Alt + letter:' ),
230 meta = tinymce.Env.mac ? __( 'Cmd + letter:' ) : __( 'Ctrl + letter:' ),
237 labels = editor.settings.wp_shortcut_labels,
238 header, html, dialog, $wrap;
244 function tr( row, columns ) {
248 columns = columns || 1;
250 each( row, function( text, key ) {
251 out += '<td><kbd>' + key + '</kbd></td><td>' + __( text ) + '</td>';
255 while ( i < columns ) {
256 out += '<td></td><td></td>';
260 return out + '</tr>';
263 each ( labels, function( label, name ) {
266 if ( label.indexOf( 'meta' ) !== -1 ) {
268 letter = label.replace( 'meta', '' ).toLowerCase();
271 row1[ letter ] = name;
273 if ( i1 % 2 === 0 ) {
274 table1.push( tr( row1, 2 ) );
278 } else if ( label.indexOf( 'access' ) !== -1 ) {
280 letter = label.replace( 'access', '' ).toLowerCase();
283 row2[ letter ] = name;
285 if ( i2 % 2 === 0 ) {
286 table2.push( tr( row2, 2 ) );
293 // Add remaining single entries.
295 table1.push( tr( row1, 2 ) );
299 table2.push( tr( row2, 2 ) );
302 header = [ __( 'Letter' ), __( 'Action' ), __( 'Letter' ), __( 'Action' ) ];
303 header = '<tr><th>' + header.join( '</th><th>' ) + '</th></tr>';
305 html = '<div class="wp-editor-help">';
307 // Main section, default and additional shortcuts
309 '<h2>' + __( 'Default shortcuts,' ) + ' ' + meta + '</h2>' +
310 '<table class="wp-help-th-center fixed">' +
314 '<h2>' + __( 'Additional shortcuts,' ) + ' ' + access + '</h2>' +
315 '<table class="wp-help-th-center fixed">' +
320 if ( editor.plugins.wptextpattern && ( ! tinymce.Env.ie || tinymce.Env.ie > 8 ) ) {
321 // Text pattern section
323 '<h2>' + __( 'When starting a new paragraph with one of these formatting shortcuts followed by a space, the formatting will be applied automatically. Press Backspace or Escape to undo.' ) + '</h2>' +
324 '<table class="wp-help-th-center fixed">' +
325 tr({ '*': 'Bullet list', '1.': 'Numbered list' }) +
326 tr({ '-': 'Bullet list', '1)': 'Numbered list' }) +
330 '<h2>' + __( 'The following formatting shortcuts are replaced when pressing Enter. Press Escape or the Undo button to undo.' ) + '</h2>' +
331 '<table class="wp-help-single">' +
332 tr({ '>': 'Blockquote' }) +
333 tr({ '##': 'Heading 2' }) +
334 tr({ '###': 'Heading 3' }) +
335 tr({ '####': 'Heading 4' }) +
336 tr({ '#####': 'Heading 5' }) +
337 tr({ '######': 'Heading 6' }) +
338 tr({ '---': 'Horizontal line' }) +
342 // Focus management section
344 '<h2>' + __( 'Focus shortcuts:' ) + '</h2>' +
345 '<table class="wp-help-single">' +
346 tr({ 'Alt + F8': 'Inline toolbar (when an image, link or preview is selected)' }) +
347 tr({ 'Alt + F9': 'Editor menu (when enabled)' }) +
348 tr({ 'Alt + F10': 'Editor toolbar' }) +
349 tr({ 'Alt + F11': 'Elements path' }) +
351 '<p>' + __( 'To move focus to other buttons use Tab or the arrow keys. To return focus to the editor press Escape or use one of the buttons.' ) + '</p>';
355 dialog = editor.windowManager.open( {
356 title: 'Keyboard Shortcuts',
369 dialog.$el.find( 'div[role="application"]' ).attr( 'role', 'document' );
370 $wrap = dialog.$el.find( '.mce-wp-help' );
373 $wrap.attr( 'tabindex', '0' );
375 $wrap.on( 'keydown', function( event ) {
376 // Prevent use of: page up, page down, end, home, left arrow, up arrow, right arrow, down arrow
377 // in the dialog keydown handler.
378 if ( event.keyCode >= 33 && event.keyCode <= 40 ) {
379 event.stopPropagation();
386 editor.addCommand( 'WP_Medialib', function() {
387 if ( wp && wp.media && wp.media.editor ) {
388 wp.media.editor.open( editor.id );
393 editor.addButton( 'wp_more', {
394 tooltip: 'Insert Read More tag',
395 onclick: function() {
396 editor.execCommand( 'WP_More', 'more' );
400 editor.addButton( 'wp_page', {
401 tooltip: 'Page break',
402 onclick: function() {
403 editor.execCommand( 'WP_More', 'nextpage' );
407 editor.addButton( 'wp_help', {
408 tooltip: 'Keyboard Shortcuts',
412 editor.addButton( 'wp_code', {
415 stateSelector: 'code'
420 if ( wp && wp.media && wp.media.editor ) {
421 editor.addMenuItem( 'add_media', {
423 icon: 'wp-media-library',
429 // Insert "Read More..."
430 editor.addMenuItem( 'wp_more', {
431 text: 'Insert Read More tag',
434 onclick: function() {
435 editor.execCommand( 'WP_More', 'more' );
439 // Insert "Next Page"
440 editor.addMenuItem( 'wp_page', {
444 onclick: function() {
445 editor.execCommand( 'WP_More', 'nextpage' );
449 editor.on( 'BeforeExecCommand', function(e) {
450 if ( tinymce.Env.webkit && ( e.command === 'InsertUnorderedList' || e.command === 'InsertOrderedList' ) ) {
452 style = editor.dom.create( 'style', {'type': 'text/css'},
453 '#tinymce,#tinymce span,#tinymce li,#tinymce li>span,#tinymce p,#tinymce p>span{font:medium sans-serif;color:#000;line-height:normal;}');
456 editor.getDoc().head.appendChild( style );
460 editor.on( 'ExecCommand', function( e ) {
461 if ( tinymce.Env.webkit && style &&
462 ( 'InsertUnorderedList' === e.command || 'InsertOrderedList' === e.command ) ) {
464 editor.dom.remove( style );
468 editor.on( 'init', function() {
469 var env = tinymce.Env,
470 bodyClass = ['mceContentBody'], // back-compat for themes that use this in editor-style.css...
471 doc = editor.getDoc(),
475 dom.addClass( doc.documentElement, 'ios' );
478 if ( editor.getParam( 'directionality' ) === 'rtl' ) {
479 bodyClass.push('rtl');
480 dom.setAttrib( doc.documentElement, 'dir', 'rtl' );
483 dom.setAttrib( doc.documentElement, 'lang', editor.getParam( 'wp_lang_attr' ) );
486 if ( parseInt( env.ie, 10 ) === 9 ) {
487 bodyClass.push('ie9');
488 } else if ( parseInt( env.ie, 10 ) === 8 ) {
489 bodyClass.push('ie8');
490 } else if ( env.ie < 8 ) {
491 bodyClass.push('ie7');
493 } else if ( env.webkit ) {
494 bodyClass.push('webkit');
497 bodyClass.push('wp-editor');
499 each( bodyClass, function( cls ) {
501 dom.addClass( doc.body, cls );
505 // Remove invalid parent paragraphs when inserting HTML
506 editor.on( 'BeforeSetContent', function( event ) {
507 if ( event.content ) {
508 event.content = event.content.replace( /<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)( [^>]*)?>/gi, '<$1$2>' )
509 .replace( /<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)>\s*<\/p>/gi, '</$1>' );
514 $( document ).triggerHandler( 'tinymce-editor-init', [editor] );
517 if ( window.tinyMCEPreInit && window.tinyMCEPreInit.dragDropUpload ) {
518 dom.bind( doc, 'dragstart dragend dragover drop', function( event ) {
520 // Trigger the jQuery handlers.
521 $( document ).trigger( new $.Event( event ) );
526 if ( editor.getParam( 'wp_paste_filters', true ) ) {
527 editor.on( 'PastePreProcess', function( event ) {
528 // Remove trailing <br> added by WebKit browsers to the clipboard
529 event.content = event.content.replace( /<br class="?Apple-interchange-newline"?>/gi, '' );
531 // In WebKit this is handled by removeWebKitStyles()
532 if ( ! tinymce.Env.webkit ) {
533 // Remove all inline styles
534 event.content = event.content.replace( /(<[^>]+) style="[^"]*"([^>]*>)/gi, '$1$2' );
536 // Put back the internal styles
537 event.content = event.content.replace(/(<[^>]+) data-mce-style=([^>]+>)/gi, '$1 style=$2' );
541 editor.on( 'PastePostProcess', function( event ) {
542 // Remove empty paragraphs
543 each( dom.select( 'p', event.node ), function( node ) {
544 if ( dom.isEmpty( node ) ) {
551 if ( editor.settings.wp_shortcut_labels && editor.theme.panel ) {
553 var access = 'Shift+Alt+';
556 // For Mac: ctrl = \u2303, cmd = \u2318, alt = \u2325
558 if ( tinymce.Env.mac ) {
559 access = '\u2303\u2325';
563 each( editor.settings.wp_shortcut_labels, function( value, name ) {
564 labels[ name ] = value.replace( 'access', access ).replace( 'meta', meta );
567 each( editor.theme.panel.find('button'), function( button ) {
568 if ( button && button.settings.tooltip && labels.hasOwnProperty( button.settings.tooltip ) ) {
569 // Need to translate now. We are changing the string so it won't match and cannot be translated later.
570 button.settings.tooltip = editor.translate( button.settings.tooltip ) + ' (' + labels[ button.settings.tooltip ] + ')';
574 // listbox for the "blocks" drop-down
575 each( editor.theme.panel.find('listbox'), function( listbox ) {
576 if ( listbox && listbox.settings.text === 'Paragraph' ) {
577 each( listbox.settings.values, function( item ) {
578 if ( item.text && labels.hasOwnProperty( item.text ) ) {
579 item.shortcut = '(' + labels[ item.text ] + ')';
587 editor.on( 'SaveContent', function( event ) {
588 // If editor is hidden, we just want the textarea's value to be saved
589 if ( ! editor.inline && editor.isHidden() ) {
590 event.content = event.element.value;
594 // Keep empty paragraphs :(
595 event.content = event.content.replace( /<p>(?:<br ?\/?>|\u00a0|\uFEFF| )*<\/p>/g, '<p> </p>' );
598 event.content = wp.editor.removep( event.content );
602 editor.on( 'preInit', function() {
603 var validElementsSetting = '@[id|accesskey|class|dir|lang|style|tabindex|' +
604 'title|contenteditable|draggable|dropzone|hidden|spellcheck|translate],' + // Global attributes.
605 'i,' + // Don't replace <i> with <em> and <b> with <strong> and don't remove them when empty.
607 'script[src|async|defer|type|charset|crossorigin|integrity]'; // Add support for <script>.
609 editor.schema.addValidElements( validElementsSetting );
611 if ( tinymce.Env.iOS ) {
612 editor.settings.height = 300;
621 u: 'InsertUnorderedList',
622 o: 'InsertOrderedList',
630 }, function( command, key ) {
631 editor.shortcuts.add( 'access+' + key, '', command );
634 editor.addShortcut( 'meta+s', '', function() {
635 if ( wp && wp.autosave ) {
636 wp.autosave.server.triggerSave();
640 if ( window.getUserSetting( 'editor_plain_text_paste_warning' ) > 1 ) {
641 editor.settings.paste_plaintext_inform = false;
644 // Change the editor iframe title on MacOS, add the correct help shortcut.
645 if ( tinymce.Env.mac ) {
646 tinymce.$( editor.iframeElement ).attr( 'title', __( 'Rich Text Area. Press Control-Option-H for help.' ) );
650 editor.on( 'PastePlainTextToggle', function( event ) {
651 // Warn twice, then stop.
652 if ( event.state === true ) {
653 var times = parseInt( window.getUserSetting( 'editor_plain_text_paste_warning' ), 10 ) || 0;
656 window.setUserSetting( 'editor_plain_text_paste_warning', ++times );
662 * Experimental: create a floating toolbar.
663 * This functionality will change in the next releases. Not recommended for use by plugins.
665 editor.on( 'preinit', function() {
666 var Factory = tinymce.ui.Factory,
667 settings = editor.settings,
671 container = editor.getContainer(),
672 wpAdminbar = document.getElementById( 'wpadminbar' ),
673 mceIframe = document.getElementById( editor.id + '_ifr' ),
679 mceToolbar = tinymce.$( '.mce-toolbar-grp', container )[0];
680 mceStatusbar = tinymce.$( '.mce-statusbar', container )[0];
683 if ( editor.id === 'content' ) {
684 wpStatusbar = document.getElementById( 'post-status-info' );
687 function create( buttons, bottom ) {
692 each( buttons, function( item ) {
695 function bindSelectorChanged() {
696 var selection = editor.selection;
698 if ( itemName === 'bullist' ) {
699 selection.selectorChanged( 'ul > li', function( state, args ) {
700 var i = args.parents.length,
704 nodeName = args.parents[ i ].nodeName;
706 if ( nodeName === 'OL' || nodeName == 'UL' ) {
711 item.active( state && nodeName === 'UL' );
715 if ( itemName === 'numlist' ) {
716 selection.selectorChanged( 'ol > li', function( state, args ) {
717 var i = args.parents.length,
721 nodeName = args.parents[ i ].nodeName;
723 if ( nodeName === 'OL' || nodeName === 'UL' ) {
728 item.active( state && nodeName === 'OL' );
732 if ( item.settings.stateSelector ) {
733 selection.selectorChanged( item.settings.stateSelector, function( state ) {
734 item.active( state );
738 if ( item.settings.disabledStateSelector ) {
739 selection.selectorChanged( item.settings.disabledStateSelector, function( state ) {
740 item.disabled( state );
745 if ( item === '|' ) {
748 if ( Factory.has( item ) ) {
753 if ( settings.toolbar_items_size ) {
754 item.size = settings.toolbar_items_size;
757 toolbarItems.push( item );
761 if ( ! buttonGroup ) {
767 toolbarItems.push( buttonGroup );
770 if ( editor.buttons[ item ] ) {
772 item = editor.buttons[ itemName ];
774 if ( typeof item === 'function' ) {
778 item.type = item.type || 'button';
780 if ( settings.toolbar_items_size ) {
781 item.size = settings.toolbar_items_size;
784 item = Factory.create( item );
786 buttonGroup.items.push( item );
788 if ( editor.initialized ) {
789 bindSelectorChanged();
791 editor.on( 'init', bindSelectorChanged );
798 toolbar = Factory.create( {
801 classes: 'toolbar-grp inline-toolbar-grp',
811 toolbar.bottom = bottom;
813 function reposition() {
814 if ( ! currentSelection ) {
818 var scrollX = window.pageXOffset || document.documentElement.scrollLeft,
819 scrollY = window.pageYOffset || document.documentElement.scrollTop,
820 windowWidth = window.innerWidth,
821 windowHeight = window.innerHeight,
822 iframeRect = mceIframe ? mceIframe.getBoundingClientRect() : {
825 bottom: windowHeight,
830 toolbar = this.getEl(),
831 toolbarWidth = toolbar.offsetWidth,
832 toolbarHeight = toolbar.clientHeight,
833 selection = currentSelection.getBoundingClientRect(),
834 selectionMiddle = ( selection.left + selection.right ) / 2,
836 spaceNeeded = toolbarHeight + buffer,
837 wpAdminbarBottom = wpAdminbar ? wpAdminbar.getBoundingClientRect().bottom : 0,
838 mceToolbarBottom = mceToolbar ? mceToolbar.getBoundingClientRect().bottom : 0,
839 mceStatusbarTop = mceStatusbar ? windowHeight - mceStatusbar.getBoundingClientRect().top : 0,
840 wpStatusbarTop = wpStatusbar ? windowHeight - wpStatusbar.getBoundingClientRect().top : 0,
841 blockedTop = Math.max( 0, wpAdminbarBottom, mceToolbarBottom, iframeRect.top ),
842 blockedBottom = Math.max( 0, mceStatusbarTop, wpStatusbarTop, windowHeight - iframeRect.bottom ),
843 spaceTop = selection.top + iframeRect.top - blockedTop,
844 spaceBottom = windowHeight - iframeRect.top - selection.bottom - blockedBottom,
845 editorHeight = windowHeight - blockedTop - blockedBottom,
851 if ( spaceTop >= editorHeight || spaceBottom >= editorHeight ) {
852 this.scrolling = true;
854 this.scrolling = false;
858 // Add offset in iOS to move the menu over the image, out of the way of the default iOS menu.
859 if ( tinymce.Env.iOS && currentSelection.nodeName === 'IMG' ) {
861 iosOffsetBottom = 46;
865 if ( spaceBottom >= spaceNeeded ) {
866 className = ' mce-arrow-up';
867 top = selection.bottom + iframeRect.top + scrollY - iosOffsetBottom;
868 } else if ( spaceTop >= spaceNeeded ) {
869 className = ' mce-arrow-down';
870 top = selection.top + iframeRect.top + scrollY - toolbarHeight + iosOffsetTop;
873 if ( spaceTop >= spaceNeeded ) {
874 className = ' mce-arrow-down';
875 top = selection.top + iframeRect.top + scrollY - toolbarHeight + iosOffsetTop;
876 } else if ( spaceBottom >= spaceNeeded && editorHeight / 2 > selection.bottom + iframeRect.top - blockedTop ) {
877 className = ' mce-arrow-up';
878 top = selection.bottom + iframeRect.top + scrollY - iosOffsetBottom;
882 if ( typeof top === 'undefined' ) {
883 top = scrollY + blockedTop + buffer + iosOffsetBottom;
886 left = selectionMiddle - toolbarWidth / 2 + iframeRect.left + scrollX;
888 if ( selection.left < 0 || selection.right > iframeRect.width ) {
889 left = iframeRect.left + scrollX + ( iframeRect.width - toolbarWidth ) / 2;
890 } else if ( toolbarWidth >= windowWidth ) {
891 className += ' mce-arrow-full';
893 } else if ( ( left < 0 && selection.left + toolbarWidth > windowWidth ) || ( left + toolbarWidth > windowWidth && selection.right - toolbarWidth < 0 ) ) {
894 left = ( windowWidth - toolbarWidth ) / 2;
895 } else if ( left < iframeRect.left + scrollX ) {
896 className += ' mce-arrow-left';
897 left = selection.left + iframeRect.left + scrollX;
898 } else if ( left + toolbarWidth > iframeRect.width + iframeRect.left + scrollX ) {
899 className += ' mce-arrow-right';
900 left = selection.right - toolbarWidth + iframeRect.left + scrollX;
903 // No up/down arrows on the menu over images in iOS.
904 if ( tinymce.Env.iOS && currentSelection.nodeName === 'IMG' ) {
905 className = className.replace( / ?mce-arrow-(up|down)/g, '' );
908 toolbar.className = toolbar.className.replace( / ?mce-arrow-[\w]+/g, '' ) + className;
910 DOM.setStyles( toolbar, {
918 toolbar.on( 'show', function() {
922 toolbar.on( 'keydown', function( event ) {
923 if ( event.keyCode === 27 ) {
929 editor.on( 'remove', function() {
933 toolbar.reposition = reposition;
934 toolbar.hide().renderTo( document.body );
939 editor.shortcuts.add( 'alt+119', '', function() {
942 if ( activeToolbar ) {
943 node = activeToolbar.find( 'toolbar' )[0];
944 node && node.focus( true );
948 editor.on( 'nodechange', function( event ) {
949 var collapsed = editor.selection.isCollapsed();
952 element: event.element,
953 parents: event.parents,
957 editor.fire( 'wptoolbar', args );
959 currentSelection = args.selection || args.element;
961 if ( activeToolbar && activeToolbar !== args.toolbar ) {
962 activeToolbar.hide();
965 if ( args.toolbar ) {
966 if ( activeToolbar !== args.toolbar ) {
967 activeToolbar = args.toolbar;
968 activeToolbar.show();
970 activeToolbar.reposition();
973 activeToolbar = false;
977 editor.on( 'focus', function() {
978 if ( activeToolbar ) {
979 activeToolbar.show();
983 function hide( event ) {
984 if ( activeToolbar ) {
985 if ( activeToolbar.tempHide || event.type === 'hide' ) {
986 activeToolbar.hide();
987 activeToolbar = false;
989 event.type === 'resizewindow' ||
990 event.type === 'scrollwindow' ||
991 event.type === 'resize' ||
992 event.type === 'scroll'
993 ) && ! activeToolbar.blockHide ) {
994 clearTimeout( timeout );
996 timeout = setTimeout( function() {
997 if ( activeToolbar && typeof activeToolbar.show === 'function' ) {
998 activeToolbar.scrolling = false;
999 activeToolbar.show();
1003 activeToolbar.scrolling = true;
1004 activeToolbar.hide();
1009 // For full height editor.
1010 editor.on( 'resizewindow scrollwindow', hide );
1011 // For scrollable editor.
1012 editor.dom.bind( editor.getWin(), 'resize scroll', hide );
1014 editor.on( 'remove', function() {
1015 editor.off( 'resizewindow scrollwindow', hide );
1016 editor.dom.unbind( editor.getWin(), 'resize scroll', hide );
1019 editor.on( 'blur hide', hide );
1021 editor.wp = editor.wp || {};
1022 editor.wp._createToolbar = create;
1027 // Expose some functions (back-compat)
1036 }( window.tinymce ));