1 /* global tinymce, getUserSetting, setUserSetting */
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 DOM = tinymce.DOM, wpAdvButton, modKey, style,
10 function toggleToolbars( state ) {
11 var iframe, initial, toolbars,
14 initial = ( state === 'hide' );
16 if ( editor.theme.panel ) {
17 toolbars = editor.theme.panel.find('.toolbar:not(.menubar)');
20 if ( ! toolbars || toolbars.length < 2 || ( state === 'hide' && ! toolbars[1].visible() ) ) {
24 if ( ! state && toolbars[1].visible() ) {
28 tinymce.each( toolbars, function( toolbar, i ) {
30 if ( state === 'hide' ) {
40 if ( pixels && ! initial ) {
41 iframe = editor.getContentAreaContainer().firstChild;
42 DOM.setStyle( iframe, 'height', iframe.clientHeight + pixels ); // Resize iframe
44 if ( state === 'hide' ) {
45 setUserSetting('hidetb', '0');
46 wpAdvButton && wpAdvButton.active( false );
48 setUserSetting('hidetb', '1');
49 wpAdvButton && wpAdvButton.active( true );
54 // Add the kitchen sink button :)
55 editor.addButton( 'wp_adv', {
56 tooltip: 'Toolbar Toggle',
58 onPostRender: function() {
60 wpAdvButton.active( getUserSetting( 'hidetb' ) === '1' ? true : false );
64 // Hide the toolbars after loading
65 editor.on( 'PostRender', function() {
66 if ( editor.getParam( 'wordpress_adv_hidden', true ) && getUserSetting( 'hidetb', '0' ) === '0' ) {
67 toggleToolbars( 'hide' );
71 editor.addCommand( 'WP_Adv', function() {
75 editor.on( 'focus', function() {
76 window.wpActiveEditor = editor.id;
79 // Replace Read More/Next Page tags with images
80 editor.on( 'BeforeSetContent', function( e ) {
82 if ( e.content.indexOf( '<!--more' ) !== -1 ) {
83 e.content = e.content.replace( /<!--more(.*?)-->/g, function( match, moretext ) {
84 return '<img src="' + tinymce.Env.transparentSrc + '" data-wp-more="' + moretext + '" ' +
85 'class="wp-more-tag mce-wp-more" title="Read More..." data-mce-resize="false" data-mce-placeholder="1" />';
89 if ( e.content.indexOf( '<!--nextpage-->' ) !== -1 ) {
90 e.content = e.content.replace( /<!--nextpage-->/g,
91 '<img src="' + tinymce.Env.transparentSrc + '" class="wp-more-tag mce-wp-nextpage" ' +
92 'title="Page break" data-mce-resize="false" data-mce-placeholder="1" />' );
97 // Replace images with tags
98 editor.on( 'PostProcess', function( e ) {
100 e.content = e.content.replace(/<img[^>]+>/g, function( image ) {
101 var match, moretext = '';
103 if ( image.indexOf('wp-more-tag') !== -1 ) {
104 if ( image.indexOf('mce-wp-more') !== -1 ) {
105 if ( match = image.match( /data-wp-more="([^"]+)"/ ) ) {
109 image = '<!--more' + moretext + '-->';
110 } else if ( image.indexOf('mce-wp-nextpage') !== -1 ) {
111 image = '<!--nextpage-->';
120 // Display the tag name instead of img in element path
121 editor.on( 'ResolveName', function( e ) {
122 var dom = editor.dom,
125 if ( target.nodeName === 'IMG' && dom.hasClass( target, 'wp-more-tag' ) ) {
126 if ( dom.hasClass( target, 'mce-wp-more' ) ) {
128 } else if ( dom.hasClass( target, 'mce-wp-nextpage' ) ) {
135 editor.addCommand( 'WP_More', function( tag ) {
136 var parent, html, title,
137 classname = 'wp-more-tag',
139 node = editor.selection.getNode();
142 classname += ' mce-wp-' + tag;
143 title = tag === 'more' ? 'More...' : 'Next Page';
144 html = '<img src="' + tinymce.Env.transparentSrc + '" title="' + title + '" class="' + classname + '" ' +
145 'data-mce-resize="false" data-mce-placeholder="1" />';
148 if ( node.nodeName === 'BODY' || ( node.nodeName === 'P' && node.parentNode.nodeName === 'BODY' ) ) {
149 editor.insertContent( html );
153 // Get the top level parent node
154 parent = dom.getParent( node, function( found ) {
155 if ( found.parentNode && found.parentNode.nodeName === 'BODY' ) {
160 }, editor.getBody() );
163 if ( parent.nodeName === 'P' ) {
164 parent.appendChild( dom.create( 'p', null, html ).firstChild );
166 dom.insertAfter( dom.create( 'p', null, html ), parent );
169 editor.nodeChanged();
173 editor.addCommand( 'WP_Code', function() {
174 editor.formatter.toggle('code');
177 editor.addCommand( 'WP_Page', function() {
178 editor.execCommand( 'WP_More', 'nextpage' );
181 editor.addCommand( 'WP_Help', function() {
182 editor.windowManager.open({
183 url: tinymce.baseURL + '/wp-mce-help.php',
184 title: 'Keyboard Shortcuts',
192 editor.addCommand( 'WP_Medialib', function() {
193 if ( typeof wp !== 'undefined' && wp.media && wp.media.editor ) {
194 wp.media.editor.open( editor.id );
199 editor.addButton( 'wp_more', {
200 tooltip: 'Insert Read More tag',
201 onclick: function() {
202 editor.execCommand( 'WP_More', 'more' );
206 editor.addButton( 'wp_page', {
207 tooltip: 'Page break',
208 onclick: function() {
209 editor.execCommand( 'WP_More', 'nextpage' );
213 editor.addButton( 'wp_help', {
214 tooltip: 'Keyboard Shortcuts',
218 editor.addButton( 'wp_code', {
221 stateSelector: 'code'
226 if ( typeof wp !== 'undefined' && wp.media && wp.media.editor ) {
227 editor.addMenuItem( 'add_media', {
229 icon: 'wp-media-library',
235 // Insert "Read More..."
236 editor.addMenuItem( 'wp_more', {
237 text: 'Insert Read More tag',
240 onclick: function() {
241 editor.execCommand( 'WP_More', 'more' );
245 // Insert "Next Page"
246 editor.addMenuItem( 'wp_page', {
250 onclick: function() {
251 editor.execCommand( 'WP_More', 'nextpage' );
255 editor.on( 'BeforeExecCommand', function(e) {
256 if ( tinymce.Env.webkit && ( e.command === 'InsertUnorderedList' || e.command === 'InsertOrderedList' ) ) {
258 style = editor.dom.create( 'style', {'type': 'text/css'},
259 '#tinymce,#tinymce span,#tinymce li,#tinymce li>span,#tinymce p,#tinymce p>span{font:medium sans-serif;color:#000;line-height:normal;}');
262 editor.getDoc().head.appendChild( style );
266 editor.on( 'ExecCommand', function( e ) {
267 if ( tinymce.Env.webkit && style &&
268 ( 'InsertUnorderedList' === e.command || 'InsertOrderedList' === e.command ) ) {
270 editor.dom.remove( style );
274 editor.on( 'init', function() {
275 var env = tinymce.Env,
276 bodyClass = ['mceContentBody'], // back-compat for themes that use this in editor-style.css...
277 doc = editor.getDoc(),
280 if ( editor.getParam( 'directionality' ) === 'rtl' ) {
281 bodyClass.push('rtl');
282 dom.setAttrib( doc.documentElement, 'dir', 'rtl' );
286 if ( parseInt( env.ie, 10 ) === 9 ) {
287 bodyClass.push('ie9');
288 } else if ( parseInt( env.ie, 10 ) === 8 ) {
289 bodyClass.push('ie8');
290 } else if ( env.ie < 8 ) {
291 bodyClass.push('ie7');
295 bodyClass.push('wp-editor');
297 tinymce.each( bodyClass, function( cls ) {
299 dom.addClass( doc.body, cls );
303 // Remove invalid parent paragraphs when inserting HTML
304 // TODO: still needed?
305 editor.on( 'BeforeSetContent', function( e ) {
307 e.content = e.content.replace(/<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>');
308 e.content = e.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '</$1>');
312 if ( typeof window.jQuery !== 'undefined' ) {
313 window.jQuery( document ).triggerHandler( 'tinymce-editor-init', [editor] );
316 if ( window.tinyMCEPreInit && window.tinyMCEPreInit.dragDropUpload ) {
317 dom.bind( doc, 'dragstart dragend dragover drop', function( event ) {
318 if ( typeof window.jQuery !== 'undefined' ) {
319 // Trigger the jQuery handlers.
320 window.jQuery( document ).trigger( new window.jQuery.Event( event ) );
327 if ( typeof window.jQuery !== 'undefined' ) {
328 editor.on( 'keyup', function( e ) {
329 var key = e.keyCode || e.charCode;
331 if ( key === last ) {
335 if ( 13 === key || 8 === last || 46 === last ) {
336 window.jQuery( document ).triggerHandler( 'wpcountwords', [ editor.getContent({ format : 'raw' }) ] );
343 editor.on( 'SaveContent', function( e ) {
344 // If editor is hidden, we just want the textarea's value to be saved
345 if ( editor.isHidden() ) {
346 e.content = e.element.value;
350 // Keep empty paragraphs :(
351 e.content = e.content.replace( /<p>(<br ?\/?>|\u00a0|\uFEFF)?<\/p>/g, '<p> </p>' );
353 if ( editor.getParam( 'wpautop', true ) && typeof window.switchEditors !== 'undefined' ) {
354 e.content = window.switchEditors.pre_wpautop( e.content );
358 editor.on( 'preInit', function() {
359 // Don't replace <i> with <em> and <b> with <strong> and don't remove them when empty
360 editor.schema.addValidElements( '@[id|accesskey|class|dir|lang|style|tabindex|title|contenteditable|draggable|dropzone|hidden|spellcheck|translate],i,b' );
363 // Add custom shortcuts
364 modKey = 'alt+shift';
366 editor.addShortcut( modKey + '+c', '', 'JustifyCenter' );
367 editor.addShortcut( modKey + '+r', '', 'JustifyRight' );
368 editor.addShortcut( modKey + '+l', '', 'JustifyLeft' );
369 editor.addShortcut( modKey + '+j', '', 'JustifyFull' );
370 editor.addShortcut( modKey + '+q', '', 'mceBlockQuote' );
371 editor.addShortcut( modKey + '+u', '', 'InsertUnorderedList' );
372 editor.addShortcut( modKey + '+o', '', 'InsertOrderedList' );
373 editor.addShortcut( modKey + '+n', '', 'mceSpellCheck' );
374 editor.addShortcut( modKey + '+s', '', 'unlink' );
375 editor.addShortcut( modKey + '+m', '', 'WP_Medialib' );
376 editor.addShortcut( modKey + '+z', '', 'WP_Adv' );
377 editor.addShortcut( modKey + '+t', '', 'WP_More' );
378 editor.addShortcut( modKey + '+d', '', 'Strikethrough' );
379 editor.addShortcut( modKey + '+h', '', 'WP_Help' );
380 editor.addShortcut( modKey + '+p', '', 'WP_Page' );
381 editor.addShortcut( modKey + '+x', '', 'WP_Code' );
382 editor.addShortcut( 'ctrl+s', '', function() {
383 if ( typeof wp !== 'undefined' && wp.autosave ) {
384 wp.autosave.server.triggerSave();
388 // popup buttons for the gallery, etc.
389 editor.on( 'init', function() {
390 editor.dom.bind( editor.getWin(), 'scroll', function() {
394 editor.dom.bind( editor.getBody(), 'dragstart', function() {
399 editor.on( 'BeforeExecCommand', function() {
403 editor.on( 'SaveContent', function() {
407 editor.on( 'MouseDown', function( e ) {
408 if ( e.target.nodeName !== 'IMG' ) {
413 editor.on( 'keydown', function( e ) {
414 if ( e.which === tinymce.util.VK.DELETE || e.which === tinymce.util.VK.BACKSPACE ) {
419 // Internal functions
420 function _setEmbed( c ) {
421 return c.replace( /\[embed\]([\s\S]+?)\[\/embed\][\s\u00a0]*/g, function( a, b ) {
422 return '<img width="300" height="200" src="' + tinymce.Env.transparentSrc + '" class="wp-oembed" ' +
423 'alt="'+ b +'" title="'+ b +'" data-mce-resize="false" data-mce-placeholder="1" />';
427 function _getEmbed( c ) {
428 return c.replace( /<img[^>]+>/g, function( a ) {
429 if ( a.indexOf('class="wp-oembed') !== -1 ) {
430 var u = a.match( /alt="([^\"]+)"/ );
433 a = '[embed]' + u[1] + '[/embed]';
441 function _showButtons( n, id ) {
442 var p1, p2, vp, X, Y;
444 vp = editor.dom.getViewPort( editor.getWin() );
445 p1 = DOM.getPos( editor.getContentAreaContainer() );
446 p2 = editor.dom.getPos( n );
448 X = Math.max( p2.x - vp.x, 0 ) + p1.x;
449 Y = Math.max( p2.y - vp.y, 0 ) + p1.y;
452 'top' : Y + 5 + 'px',
453 'left' : X + 5 + 'px',
458 function _hideButtons() {
459 DOM.hide( DOM.select( '#wp_editbtns, #wp_gallerybtns' ) );
462 // Expose some functions (back-compat)
464 _showButtons: _showButtons,
465 _hideButtons: _hideButtons,
466 _setEmbed: _setEmbed,