-/* global tinymce, getUserSetting, setUserSetting */
-
+/* global getUserSetting, setUserSetting */
+( function( tinymce ) {
// Set the minimum value for the modals z-index higher than #wpadminbar (100000)
tinymce.ui.FloatPanel.zIndex = 100100;
tinymce.PluginManager.add( 'wordpress', function( editor ) {
- var DOM = tinymce.DOM, wpAdvButton, modKey, style,
- last = 0;
-
- if ( typeof window.jQuery !== 'undefined' ) {
- window.jQuery( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] );
+ var wpAdvButton, style,
+ DOM = tinymce.DOM,
+ each = tinymce.each,
+ __ = editor.editorManager.i18n.translate,
+ $ = window.jQuery,
+ wp = window.wp,
+ hasWpautop = ( wp && wp.editor && wp.editor.autop && editor.getParam( 'wpautop', true ) );
+
+ if ( $ ) {
+ $( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] );
}
function toggleToolbars( state ) {
state = 'hide';
}
- tinymce.each( toolbars, function( toolbar, i ) {
+ each( toolbars, function( toolbar, i ) {
if ( i > 0 ) {
if ( state === 'hide' ) {
toolbar.hide();
});
// Replace Read More/Next Page tags with images
- editor.on( 'BeforeSetContent', function( e ) {
+ editor.on( 'BeforeSetContent', function( event ) {
var title;
- if ( e.content ) {
- if ( e.content.indexOf( '<!--more' ) !== -1 ) {
- title = editor.editorManager.i18n.translate( 'Read more...' );
+ if ( event.content ) {
+ if ( event.content.indexOf( '<!--more' ) !== -1 ) {
+ title = __( 'Read more...' );
- e.content = e.content.replace( /<!--more(.*?)-->/g, function( match, moretext ) {
+ event.content = event.content.replace( /<!--more(.*?)-->/g, function( match, moretext ) {
return '<img src="' + tinymce.Env.transparentSrc + '" data-wp-more="more" data-wp-more-text="' + moretext + '" ' +
'class="wp-more-tag mce-wp-more" title="' + title + '" data-mce-resize="false" data-mce-placeholder="1" />';
});
}
- if ( e.content.indexOf( '<!--nextpage-->' ) !== -1 ) {
- title = editor.editorManager.i18n.translate( 'Page break' );
+ if ( event.content.indexOf( '<!--nextpage-->' ) !== -1 ) {
+ title = __( 'Page break' );
- e.content = e.content.replace( /<!--nextpage-->/g,
+ event.content = event.content.replace( /<!--nextpage-->/g,
'<img src="' + tinymce.Env.transparentSrc + '" data-wp-more="nextpage" class="wp-more-tag mce-wp-nextpage" ' +
'title="' + title + '" data-mce-resize="false" data-mce-placeholder="1" />' );
}
+
+ if ( event.load && event.format !== 'raw' && hasWpautop ) {
+ event.content = wp.editor.autop( event.content );
+ }
+
+ // Remove spaces from empty paragraphs.
+ event.content = event.content.replace( /<p>(?: |\u00a0|\uFEFF|\s)+<\/p>/gi, '<p><br /></p>' );
}
});
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" />';
});
editor.addCommand( 'WP_Help', function() {
- editor.windowManager.open({
- url: tinymce.baseURL + '/wp-mce-help.php',
+ var access = tinymce.Env.mac ? __( 'Ctrl + Alt + letter:' ) : __( 'Shift + Alt + letter:' ),
+ meta = tinymce.Env.mac ? __( 'Cmd + letter:' ) : __( 'Ctrl + letter:' ),
+ table1 = [],
+ table2 = [],
+ header, html, dialog, $wrap;
+
+ each( [
+ { c: 'Copy', x: 'Cut' },
+ { v: 'Paste', a: 'Select all' },
+ { z: 'Undo', y: 'Redo' },
+ { b: 'Bold', i: 'Italic' },
+ { u: 'Underline', k: 'Insert/edit link' }
+ ], function( row ) {
+ table1.push( tr( row ) );
+ } );
+
+ each( [
+ { 1: 'Heading 1', 2: 'Heading 2' },
+ { 3: 'Heading 3', 4: 'Heading 4' },
+ { 5: 'Heading 5', 6: 'Heading 6' },
+ { l: 'Align left', c: 'Align center' },
+ { r: 'Align right', j: 'Justify' },
+ { d: 'Strikethrough', q: 'Blockquote' },
+ { u: 'Bullet list', o: 'Numbered list' },
+ { a: 'Insert/edit link', s: 'Remove link' },
+ { m: 'Insert/edit image', t: 'Insert Read More tag' },
+ { h: 'Keyboard Shortcuts', x: 'Code' },
+ { p: 'Insert Page Break tag', w: 'Distraction-free writing mode' }
+ ], function( row ) {
+ table2.push( tr( row ) );
+ } );
+
+ function tr( row ) {
+ var out = '<tr>';
+
+ each( row, function( text, key ) {
+ if ( ! text ) {
+ out += '<td></td><td></td>';
+ } else {
+ out += '<td><kbd>' + key + '</kbd></td><td>' + __( text ) + '</td>';
+ }
+ });
+
+ return out + '</tr>';
+ }
+
+ header = [ __( 'Letter' ), __( 'Action' ), __( 'Letter' ), __( 'Action' ) ];
+ header = '<tr><th>' + header.join( '</th><th>' ) + '</th></tr>';
+
+ html = '<div class="wp-editor-help">';
+
+ // Main section, default and additional shortcuts
+ html = html +
+ '<h2>' + __( 'Default shortcuts,' ) + ' ' + meta + '</h2>' +
+ '<table class="wp-help-th-center">' +
+ header +
+ table1.join('') +
+ '</table>' +
+ '<h2>' + __( 'Additional shortcuts,' ) + ' ' + access + '</h2>' +
+ '<table class="wp-help-th-center">' +
+ header +
+ table2.join('') +
+ '</table>';
+
+ if ( editor.plugins.wptextpattern ) {
+ // Text pattern section
+ html = html +
+ '<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>' +
+ '<table>' +
+ tr({ '*': 'Bullet list' }) +
+ tr({ '-': 'Bullet list' }) +
+ tr({ '1.': 'Numbered list' }) +
+ tr({ '1)': 'Numbered list' }) +
+ '</table>';
+
+ html = html +
+ '<h2>' + __( 'The following formatting shortcuts are replaced when pressing Enter. Press Escape or the Undo button to undo.' ) + '</h2>' +
+ '<table>' +
+ tr({ '>': 'Blockquote' }) +
+ tr({ '##': 'Heading 2' }) +
+ tr({ '###': 'Heading 3' }) +
+ tr({ '####': 'Heading 4' }) +
+ tr({ '#####': 'Heading 5' }) +
+ tr({ '######': 'Heading 6' }) +
+ '</table>';
+ }
+
+ // Focus management section
+ html = html +
+ '<h2>' + __( 'Focus shortcuts:' ) + '</h2>' +
+ '<table>' +
+ tr({ 'Alt + F8': 'Inline toolbar (when an image, link or preview is selected)' }) +
+ tr({ 'Alt + F9': 'Editor menu (when enabled)' }) +
+ tr({ 'Alt + F10': 'Editor toolbar' }) +
+ tr({ 'Alt + F11': 'Elements path' }) +
+ '</table>' +
+ '<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>';
+
+ html += '</div>';
+
+ dialog = editor.windowManager.open( {
title: 'Keyboard Shortcuts',
- width: 450,
- height: 420,
- classes: 'wp-help',
- buttons: { text: 'Close', onclick: 'close' }
- });
- });
+ items: {
+ type: 'container',
+ classes: 'wp-help',
+ html: html
+ },
+ buttons: {
+ text: 'Close',
+ onclick: 'close'
+ }
+ } );
+
+ if ( dialog.$el ) {
+ dialog.$el.find( 'div[role="application"]' ).attr( 'role', 'document' );
+ $wrap = dialog.$el.find( '.mce-wp-help' );
+
+ if ( $wrap[0] ) {
+ $wrap.attr( 'tabindex', '0' );
+ $wrap[0].focus();
+ $wrap.on( 'keydown', function( event ) {
+ // Prevent use of: page up, page down, end, home, left arrow, up arrow, right arrow, down arrow
+ // in the dialog keydown handler.
+ if ( event.keyCode >= 33 && event.keyCode <= 40 ) {
+ event.stopPropagation();
+ }
+ });
+ }
+ }
+ } );
editor.addCommand( 'WP_Medialib', function() {
- if ( typeof wp !== 'undefined' && wp.media && wp.media.editor ) {
+ if ( wp && wp.media && wp.media.editor ) {
wp.media.editor.open( editor.id );
}
});
// Menubar
// Insert->Add Media
- if ( typeof wp !== 'undefined' && wp.media && wp.media.editor ) {
+ if ( wp && wp.media && wp.media.editor ) {
editor.addMenuItem( 'add_media', {
text: 'Add Media',
icon: 'wp-media-library',
doc = editor.getDoc(),
dom = editor.dom;
- if ( tinymce.Env.iOS ) {
+ if ( env.iOS ) {
dom.addClass( doc.documentElement, 'ios' );
}
bodyClass.push('wp-editor');
- tinymce.each( bodyClass, function( cls ) {
+ each( bodyClass, function( cls ) {
if ( cls ) {
dom.addClass( doc.body, cls );
}
});
// Remove invalid parent paragraphs when inserting HTML
- // TODO: still needed?
- editor.on( 'BeforeSetContent', function( e ) {
- if ( e.content ) {
- e.content = e.content.replace(/<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>');
- e.content = e.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '</$1>');
+ editor.on( 'BeforeSetContent', function( event ) {
+ if ( event.content ) {
+ event.content = event.content.replace( /<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)( [^>]*)?>/gi, '<$1$2>' )
+ .replace( /<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)>\s*<\/p>/gi, '</$1>' );
}
});
- if ( typeof window.jQuery !== 'undefined' ) {
- window.jQuery( document ).triggerHandler( 'tinymce-editor-init', [editor] );
+ if ( $ ) {
+ $( document ).triggerHandler( 'tinymce-editor-init', [editor] );
}
if ( window.tinyMCEPreInit && window.tinyMCEPreInit.dragDropUpload ) {
dom.bind( doc, 'dragstart dragend dragover drop', function( event ) {
- if ( typeof window.jQuery !== 'undefined' ) {
+ if ( $ ) {
// Trigger the jQuery handlers.
- window.jQuery( document ).trigger( new window.jQuery.Event( event ) );
+ $( document ).trigger( new $.Event( event ) );
}
});
}
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 );
}
}
});
- // Word count
- if ( typeof window.jQuery !== 'undefined' ) {
- editor.on( 'keyup', function( e ) {
- var key = e.keyCode || e.charCode;
-
- if ( key === last ) {
- return;
- }
-
- if ( 13 === key || 8 === last || 46 === last ) {
- window.jQuery( document ).triggerHandler( 'wpcountwords', [ editor.getContent({ format : 'raw' }) ] );
- }
-
- last = key;
- });
- }
-
- editor.on( 'SaveContent', function( e ) {
+ editor.on( 'SaveContent', function( event ) {
// If editor is hidden, we just want the textarea's value to be saved
if ( ! editor.inline && editor.isHidden() ) {
- e.content = e.element.value;
+ event.content = event.element.value;
return;
}
// Keep empty paragraphs :(
- e.content = e.content.replace( /<p>(?:<br ?\/?>|\u00a0|\uFEFF| )*<\/p>/g, '<p> </p>' );
-
- if ( editor.getParam( 'wpautop', true ) && typeof window.switchEditors !== 'undefined' ) {
- e.content = window.switchEditors.pre_wpautop( e.content );
- }
- });
+ event.content = event.content.replace( /<p>(?:<br ?\/?>|\u00a0|\uFEFF| )*<\/p>/g, '<p> </p>' );
- // Remove spaces from empty paragraphs.
- editor.on( 'BeforeSetContent', function( event ) {
- if ( event.content ) {
- event.content = event.content.replace( /<p>(?: |\u00a0|\uFEFF| )+<\/p>/gi, '<p></p>' );
+ if ( hasWpautop ) {
+ event.content = wp.editor.removep( event.content );
}
});
editor.on( 'preInit', function() {
- // Don't replace <i> with <em> and <b> with <strong> and don't remove them when empty
- editor.schema.addValidElements( '@[id|accesskey|class|dir|lang|style|tabindex|title|contenteditable|draggable|dropzone|hidden|spellcheck|translate],i,b' );
+ var validElementsSetting = '@[id|accesskey|class|dir|lang|style|tabindex|' +
+ 'title|contenteditable|draggable|dropzone|hidden|spellcheck|translate],' + // Global attributes.
+ 'i,' + // Don't replace <i> with <em> and <b> with <strong> and don't remove them when empty.
+ 'b,' +
+ 'script[src|async|defer|type|charset|crossorigin|integrity]'; // Add support for <script>.
+
+ editor.schema.addValidElements( validElementsSetting );
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( 'meta+s', '', function() {
+ if ( wp && 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.
+ */
+ editor.on( 'preinit', 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;
+
+ if ( container ) {
+ mceToolbar = tinymce.$( '.mce-toolbar-grp', container )[0];
+ mceStatusbar = tinymce.$( '.mce-statusbar', container )[0];
+ }
- // popup buttons for the gallery, etc.
- editor.on( 'init', function() {
- editor.dom.bind( editor.getWin(), 'scroll', function() {
- _hideButtons();
- });
+ if ( editor.id === 'content' ) {
+ wpStatusbar = document.getElementById( 'post-status-info' );
+ }
- editor.dom.bind( editor.getBody(), 'dragstart', function() {
- _hideButtons();
- });
- });
+ function create( buttons, bottom ) {
+ var toolbar,
+ toolbarItems = [],
+ buttonGroup;
- editor.on( 'BeforeExecCommand', function() {
- _hideButtons();
- });
+ each( buttons, function( item ) {
+ var itemName;
- editor.on( 'SaveContent', function() {
- _hideButtons();
- });
+ function bindSelectorChanged() {
+ var selection = editor.selection;
- editor.on( 'MouseDown', function( e ) {
- if ( e.target.nodeName !== 'IMG' ) {
- _hideButtons();
- }
- });
+ if ( itemName === 'bullist' ) {
+ selection.selectorChanged( 'ul > li', function( state, args ) {
+ var i = args.parents.length,
+ nodeName;
- editor.on( 'keydown', function( e ) {
- if ( e.which === tinymce.util.VK.DELETE || e.which === tinymce.util.VK.BACKSPACE ) {
- _hideButtons();
- }
- });
+ while ( i-- ) {
+ nodeName = args.parents[ i ].nodeName;
- // 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 ( 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;
- function _getEmbed( c ) {
- return c.replace( /<img[^>]+>/g, function( a ) {
- if ( a.indexOf('class="wp-oembed') !== -1 ) {
- var u = a.match( /alt="([^\"]+)"/ );
+ while ( i-- ) {
+ nodeName = args.parents[ i ].nodeName;
+
+ if ( nodeName === 'OL' || nodeName === 'UL' ) {
+ break;
+ }
+ }
+
+ item.active( state && nodeName === 'OL' );
+ } );
+ }
- if ( u[1] ) {
- a = '[embed]' + u[1] + '[/embed]';
+ 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();
+ } );
+
+ 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 );
}
+ } );
- return a;
- });
- }
+ editor.on( 'nodechange', function( event ) {
+ var collapsed = editor.selection.isCollapsed();
- function _showButtons( n, id ) {
- var p1, p2, vp, X, Y;
+ var args = {
+ element: event.element,
+ parents: event.parents,
+ collapsed: collapsed
+ };
- vp = editor.dom.getViewPort( editor.getWin() );
- p1 = DOM.getPos( editor.getContentAreaContainer() );
- p2 = editor.dom.getPos( n );
+ editor.fire( 'wptoolbar', args );
- X = Math.max( p2.x - vp.x, 0 ) + p1.x;
- Y = Math.max( p2.y - vp.y, 0 ) + p1.y;
+ currentSelection = args.selection || args.element;
- DOM.setStyles( id, {
- 'top' : Y + 5 + 'px',
- 'left' : X + 5 + 'px',
- 'display': 'block'
- });
- }
+ if ( activeToolbar ) {
+ activeToolbar.hide();
+ }
- function _hideButtons() {
- DOM.hide( DOM.select( '#wp_editbtns, #wp_gallerybtns' ) );
- }
+ 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' ) {
+ activeToolbar = false;
+ } else if ( event.type === 'resize' || event.type === 'scroll' ) {
+ clearTimeout( timeout );
+
+ timeout = setTimeout( function() {
+ if ( activeToolbar && typeof activeToolbar.show === 'function' ) {
+ activeToolbar.show();
+ }
+ }, 250 );
+ }
+ }
+ }
+
+ DOM.bind( window, 'resize scroll', hide );
+ editor.dom.bind( editor.getWin(), 'resize scroll', hide );
+
+ editor.on( 'remove', function() {
+ DOM.unbind( window, 'resize scroll', hide );
+ editor.dom.unbind( editor.getWin(), 'resize scroll', hide );
+ } );
+
+ editor.on( 'blur hide', hide );
+
+ editor.wp = editor.wp || {};
+ editor.wp._createToolbar = create;
+ }, true );
+
+ function noop() {}
// Expose some functions (back-compat)
return {
- _showButtons: _showButtons,
- _hideButtons: _hideButtons,
- _setEmbed: _setEmbed,
- _getEmbed: _getEmbed
+ _showButtons: noop,
+ _hideButtons: noop,
+ _setEmbed: noop,
+ _getEmbed: noop
};
});
+
+}( window.tinymce ));