1 /* global tinymce, getUserSetting, setUserSetting, switchEditors, autosave */
9 tinymce.create('tinymce.plugins.WordPress', {
10 init : function(ed, url) {
11 var t = this, tbId = ed.getParam('wordpress_adv_toolbar', 'toolbar2'), last = 0, moreHTML, nextpageHTML, closeOnClick, mod_key, style;
12 moreHTML = '<img src="' + url + '/img/trans.gif" class="mce-wp-more mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
13 nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mce-wp-nextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
15 if ( getUserSetting('hidetb', '0') == '1' )
16 ed.settings.wordpress_adv_hidden = 0;
18 // Hides the specified toolbar and resizes the iframe
19 ed.onPostRender.add(function() {
20 var adv_toolbar = ed.controlManager.get(tbId);
21 if ( ed.getParam('wordpress_adv_hidden', 1) && adv_toolbar ) {
22 DOM.hide(adv_toolbar.id);
23 t._resizeIframe(ed, tbId, 28);
28 ed.addCommand('WP_More', function() {
29 ed.execCommand('mceInsertContent', 0, moreHTML);
32 ed.addCommand('WP_Page', function() {
33 ed.execCommand('mceInsertContent', 0, nextpageHTML);
36 ed.addCommand('WP_Help', function() {
37 ed.windowManager.open({
38 url : tinymce.baseURL + '/wp-mce-help.php',
45 ed.addCommand('WP_Adv', function() {
46 var cm = ed.controlManager, id = cm.get(tbId).id;
48 if ( 'undefined' == id )
51 if ( DOM.isHidden(id) ) {
52 cm.setActive('wp_adv', 1);
54 t._resizeIframe(ed, tbId, -28);
55 ed.settings.wordpress_adv_hidden = 0;
56 setUserSetting('hidetb', '1');
58 cm.setActive('wp_adv', 0);
60 t._resizeIframe(ed, tbId, 28);
61 ed.settings.wordpress_adv_hidden = 1;
62 setUserSetting('hidetb', '0');
66 ed.addCommand('WP_Medialib', function() {
67 if ( typeof wp !== 'undefined' && wp.media && wp.media.editor )
68 wp.media.editor.open( ed.id );
72 ed.addButton('wp_more', {
73 title : 'wordpress.wp_more_desc',
77 ed.addButton('wp_page', {
78 title : 'wordpress.wp_page_desc',
79 image : url + '/img/page.gif',
83 ed.addButton('wp_help', {
84 title : 'wordpress.wp_help_desc',
88 ed.addButton('wp_adv', {
89 title : 'wordpress.wp_adv_desc',
94 ed.addButton('add_media', {
95 title : 'wordpress.add_media',
96 image : url + '/img/image.gif',
100 // Add Media buttons to fullscreen and handle align buttons for image captions
101 ed.onBeforeExecCommand.add(function(ed, cmd, ui, val, o) {
102 var DOM = tinymce.DOM, n, DL, DIV, cls, a, align;
103 if ( 'mceFullScreen' == cmd ) {
104 if ( 'mce_fullscreen' != ed.id && DOM.select('a.thickbox').length )
105 ed.settings.theme_advanced_buttons1 += ',|,add_media';
108 if ( 'JustifyLeft' == cmd || 'JustifyRight' == cmd || 'JustifyCenter' == cmd ) {
109 n = ed.selection.getNode();
111 if ( n.nodeName == 'IMG' ) {
112 align = cmd.substr(7).toLowerCase();
114 DL = ed.dom.getParent(n, 'dl.wp-caption');
115 DIV = ed.dom.getParent(n, 'div.mceTemp');
118 cls = ed.dom.hasClass(DL, a) ? 'alignnone' : a;
119 DL.className = DL.className.replace(/align[^ '"]+\s?/g, '');
120 ed.dom.addClass(DL, cls);
122 if (cls == 'aligncenter')
123 ed.dom.addClass(DIV, 'mceIEcenter');
125 ed.dom.removeClass(DIV, 'mceIEcenter');
128 ed.execCommand('mceRepaint');
130 if ( ed.dom.hasClass(n, a) )
131 ed.dom.addClass(n, 'alignnone');
133 ed.dom.removeClass(n, 'alignnone');
138 if ( tinymce.isWebKit && ( 'InsertUnorderedList' == cmd || 'InsertOrderedList' == cmd ) ) {
140 style = ed.dom.create('style', {'type': 'text/css'}, '#tinymce,#tinymce span,#tinymce li,#tinymce li>span,#tinymce p,#tinymce p>span{font:medium sans-serif;color:#000;line-height:normal;}');
142 ed.getDoc().head.appendChild( style );
146 ed.onExecCommand.add( function( ed, cmd ) {
147 if ( tinymce.isWebKit && style && ( 'InsertUnorderedList' == cmd || 'InsertOrderedList' == cmd ) )
148 ed.dom.remove( style );
151 ed.onInit.add(function(ed) {
152 var bodyClass = ed.getParam('body_class', ''), body = ed.getBody();
156 bodyClass = bodyClass.split(' ');
160 if ( ed.getParam('directionality', '') == 'rtl' )
161 bodyClass.push('rtl');
164 bodyClass.push('ie9');
165 else if ( tinymce.isIE8 )
166 bodyClass.push('ie8');
167 else if ( tinymce.isIE7 )
168 bodyClass.push('ie7');
170 if ( ed.id != 'wp_mce_fullscreen' && ed.id != 'mce_fullscreen' )
171 bodyClass.push('wp-editor');
172 else if ( ed.id == 'mce_fullscreen' )
173 bodyClass.push('mce-fullscreen');
175 tinymce.each( bodyClass, function(cls){
177 ed.dom.addClass(body, cls);
180 // make sure these run last
181 ed.onNodeChange.add( function(ed, cm, e) {
184 if ( e.nodeName == 'IMG' ) {
185 DL = ed.dom.getParent(e, 'dl.wp-caption');
186 } else if ( e.nodeName == 'DIV' && ed.dom.hasClass(e, 'mceTemp') ) {
189 if ( ! ed.dom.hasClass(DL, 'wp-caption') )
194 if ( ed.dom.hasClass(DL, 'alignleft') )
195 cm.setActive('justifyleft', 1);
196 else if ( ed.dom.hasClass(DL, 'alignright') )
197 cm.setActive('justifyright', 1);
198 else if ( ed.dom.hasClass(DL, 'aligncenter') )
199 cm.setActive('justifycenter', 1);
203 // remove invalid parent paragraphs when pasting HTML and/or switching to the HTML editor and back
204 ed.onBeforeSetContent.add(function(ed, o) {
206 o.content = o.content.replace(/<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>');
207 o.content = o.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '</$1>');
213 if ( 'undefined' != typeof(jQuery) ) {
214 ed.onKeyUp.add(function(ed, e) {
215 var k = e.keyCode || e.charCode;
220 if ( 13 == k || 8 == last || 46 == last )
221 jQuery(document).triggerHandler('wpcountwords', [ ed.getContent({format : 'raw'}) ]);
227 // keep empty paragraphs :(
228 ed.onSaveContent.addToTop(function(ed, o) {
229 o.content = o.content.replace(/<p>(<br ?\/?>|\u00a0|\uFEFF)?<\/p>/g, '<p> </p>');
232 // Fix bug in iOS Safari where it's impossible to type after a touchstart event on the parent document.
233 // Happens after zooming in or out while the keyboard is open. See #25131.
234 if ( tinymce.isIOS5 ) {
235 ed.onKeyDown.add( function() {
236 if ( document.activeElement == document.body ) {
242 ed.onSaveContent.add(function(ed, o) {
243 // If editor is hidden, we just want the textarea's value to be saved
245 o.content = o.element.value;
246 else if ( ed.getParam('wpautop', true) && typeof(switchEditors) == 'object' )
247 o.content = switchEditors.pre_wpautop(o.content);
251 ed.onBeforeSetContent.add(function(ed, o) {
252 o.content = t._setEmbed(o.content);
255 ed.onPostProcess.add(function(ed, o) {
257 o.content = t._getEmbed(o.content);
261 // Add listeners to handle more break
262 t._handleMoreBreak(ed, url);
264 // Add custom shortcuts
265 mod_key = 'alt+shift';
267 // if ( tinymce.isGecko ) // disable for mow, too many shortcuts conflicts
268 // mod_key = 'ctrl+alt';
270 ed.addShortcut(mod_key + '+c', 'justifycenter_desc', 'JustifyCenter');
271 ed.addShortcut(mod_key + '+r', 'justifyright_desc', 'JustifyRight');
272 ed.addShortcut(mod_key + '+l', 'justifyleft_desc', 'JustifyLeft');
273 ed.addShortcut(mod_key + '+j', 'justifyfull_desc', 'JustifyFull');
274 ed.addShortcut(mod_key + '+q', 'blockquote_desc', 'mceBlockQuote');
275 ed.addShortcut(mod_key + '+u', 'bullist_desc', 'InsertUnorderedList');
276 ed.addShortcut(mod_key + '+o', 'numlist_desc', 'InsertOrderedList');
277 ed.addShortcut(mod_key + '+n', 'spellchecker.desc', 'mceSpellCheck');
278 ed.addShortcut(mod_key + '+a', 'link_desc', 'WP_Link');
279 ed.addShortcut(mod_key + '+s', 'unlink_desc', 'unlink');
280 ed.addShortcut(mod_key + '+m', 'image_desc', 'WP_Medialib');
281 ed.addShortcut(mod_key + '+z', 'wordpress.wp_adv_desc', 'WP_Adv');
282 ed.addShortcut(mod_key + '+t', 'wordpress.wp_more_desc', 'WP_More');
283 ed.addShortcut(mod_key + '+d', 'striketrough_desc', 'Strikethrough');
284 ed.addShortcut(mod_key + '+h', 'help_desc', 'WP_Help');
285 ed.addShortcut(mod_key + '+p', 'wordpress.wp_page_desc', 'WP_Page');
286 ed.addShortcut('ctrl+s', 'save_desc', function(){if('function'==typeof autosave)autosave();});
288 if ( /\bwpfullscreen\b/.test(ed.settings.plugins) )
289 ed.addShortcut(mod_key + '+w', 'wordpress.wp_fullscreen_desc', 'wpFullScreen');
290 else if ( /\bfullscreen\b/.test(ed.settings.plugins) )
291 ed.addShortcut(mod_key + '+g', 'fullscreen.desc', 'mceFullScreen');
293 // popup buttons for images and the gallery
294 ed.onInit.add(function(ed) {
295 tinymce.dom.Event.add(ed.getWin(), 'scroll', function() {
296 ed.plugins.wordpress._hideButtons();
298 tinymce.dom.Event.add(ed.getBody(), 'dragstart', function() {
299 ed.plugins.wordpress._hideButtons();
303 ed.onBeforeExecCommand.add( function( ed ) {
304 ed.plugins.wordpress._hideButtons();
307 ed.onSaveContent.add( function( ed ) {
308 ed.plugins.wordpress._hideButtons();
311 ed.onMouseDown.add(function(ed, e) {
312 if ( e.target.nodeName != 'IMG' )
313 ed.plugins.wordpress._hideButtons();
316 ed.onKeyDown.add(function(ed, e){
317 if ( e.which == tinymce.VK.DELETE || e.which == tinymce.VK.BACKSPACE )
318 ed.plugins.wordpress._hideButtons();
321 closeOnClick = function(e){
324 if ( e.target.id == 'mceModalBlocker' || e.target.className == 'ui-widget-overlay' ) {
325 for ( id in ed.windowManager.windows ) {
326 ed.windowManager.close(null, id);
331 // close popups when clicking on the background
332 tinymce.dom.Event.remove(document.body, 'click', closeOnClick);
333 tinymce.dom.Event.add(document.body, 'click', closeOnClick);
336 getInfo : function() {
338 longname : 'WordPress Plugin',
339 author : 'WordPress', // add Moxiecode?
340 authorurl : 'http://wordpress.org',
341 infourl : 'http://wordpress.org',
346 // Internal functions
347 _setEmbed : function(c) {
348 return c.replace(/\[embed\]([\s\S]+?)\[\/embed\][\s\u00a0]*/g, function(a,b){
349 return '<img width="300" height="200" src="' + tinymce.baseURL + '/plugins/wordpress/img/trans.gif" class="wp-oembed mceItemNoResize" alt="'+b+'" title="'+b+'" />';
353 _getEmbed : function(c) {
354 return c.replace(/<img[^>]+>/g, function(a) {
355 if ( a.indexOf('class="wp-oembed') != -1 ) {
356 var u = a.match(/alt="([^\"]+)"/);
358 a = '[embed]' + u[1] + '[/embed]';
364 _showButtons : function(n, id) {
365 var ed = tinymce.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y;
367 vp = ed.dom.getViewPort(ed.getWin());
368 p1 = DOM.getPos(ed.getContentAreaContainer());
369 p2 = ed.dom.getPos(n);
371 X = Math.max(p2.x - vp.x, 0) + p1.x;
372 Y = Math.max(p2.y - vp.y, 0) + p1.y;
381 _hideButtons : function() {
382 var DOM = tinymce.DOM;
383 DOM.hide( DOM.select('#wp_editbtns, #wp_gallerybtns') );
386 // Resizes the iframe by a relative height value
387 _resizeIframe : function(ed, tb_id, dy) {
388 var ifr = ed.getContentAreaContainer().firstChild;
390 DOM.setStyle(ifr, 'height', ifr.clientHeight + dy); // Resize iframe
391 ed.theme.deltaHeight += dy; // For resize cookie
394 _handleMoreBreak : function(ed, url) {
395 var moreHTML, nextpageHTML;
397 moreHTML = '<img src="' + url + '/img/trans.gif" alt="$1" class="mce-wp-more mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
398 nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mce-wp-nextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
400 // Display morebreak instead if img in element path
401 ed.onPostRender.add(function() {
402 if (ed.theme.onResolveName) {
403 ed.theme.onResolveName.add(function(th, o) {
404 if (o.node.nodeName == 'IMG') {
405 if ( ed.dom.hasClass(o.node, 'mce-wp-more') )
407 if ( ed.dom.hasClass(o.node, 'mce-wp-nextpage') )
415 // Replace morebreak with images
416 ed.onBeforeSetContent.add(function(ed, o) {
418 o.content = o.content.replace(/<!--more(.*?)-->/g, moreHTML);
419 o.content = o.content.replace(/<!--nextpage-->/g, nextpageHTML);
423 // Replace images with morebreak
424 ed.onPostProcess.add(function(ed, o) {
426 o.content = o.content.replace(/<img[^>]+>/g, function(im) {
427 if (im.indexOf('class="mce-wp-more') !== -1) {
428 var m, moretext = (m = im.match(/alt="(.*?)"/)) ? m[1] : '';
429 im = '<!--more'+moretext+'-->';
431 if (im.indexOf('class="mce-wp-nextpage') !== -1)
432 im = '<!--nextpage-->';
438 // Set active buttons if user selected pagebreak or more break
439 ed.onNodeChange.add(function(ed, cm, n) {
440 cm.setActive('wp_page', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mce-wp-nextpage'));
441 cm.setActive('wp_more', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mce-wp-more'));
447 tinymce.PluginManager.add('wordpress', tinymce.plugins.WordPress);