8 tinymce.create('tinymce.plugins.WordPress', {
9 init : function(ed, url) {
10 var t = this, tbId = ed.getParam('wordpress_adv_toolbar', 'toolbar2'), last = 0, moreHTML, nextpageHTML, closeOnClick, mod_key, style;
11 moreHTML = '<img src="' + url + '/img/trans.gif" class="mce-wp-more mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
12 nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mce-wp-nextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
14 if ( getUserSetting('hidetb', '0') == '1' )
15 ed.settings.wordpress_adv_hidden = 0;
17 // Hides the specified toolbar and resizes the iframe
18 ed.onPostRender.add(function() {
19 var adv_toolbar = ed.controlManager.get(tbId);
20 if ( ed.getParam('wordpress_adv_hidden', 1) && adv_toolbar ) {
21 DOM.hide(adv_toolbar.id);
22 t._resizeIframe(ed, tbId, 28);
27 ed.addCommand('WP_More', function() {
28 ed.execCommand('mceInsertContent', 0, moreHTML);
31 ed.addCommand('WP_Page', function() {
32 ed.execCommand('mceInsertContent', 0, nextpageHTML);
35 ed.addCommand('WP_Help', function() {
36 ed.windowManager.open({
37 url : tinymce.baseURL + '/wp-mce-help.php',
44 ed.addCommand('WP_Adv', function() {
45 var cm = ed.controlManager, id = cm.get(tbId).id;
47 if ( 'undefined' == id )
50 if ( DOM.isHidden(id) ) {
51 cm.setActive('wp_adv', 1);
53 t._resizeIframe(ed, tbId, -28);
54 ed.settings.wordpress_adv_hidden = 0;
55 setUserSetting('hidetb', '1');
57 cm.setActive('wp_adv', 0);
59 t._resizeIframe(ed, tbId, 28);
60 ed.settings.wordpress_adv_hidden = 1;
61 setUserSetting('hidetb', '0');
65 ed.addCommand('WP_Medialib', function() {
66 if ( typeof wp !== 'undefined' && wp.media && wp.media.editor )
67 wp.media.editor.open( ed.id );
71 ed.addButton('wp_more', {
72 title : 'wordpress.wp_more_desc',
76 ed.addButton('wp_page', {
77 title : 'wordpress.wp_page_desc',
78 image : url + '/img/page.gif',
82 ed.addButton('wp_help', {
83 title : 'wordpress.wp_help_desc',
87 ed.addButton('wp_adv', {
88 title : 'wordpress.wp_adv_desc',
93 ed.addButton('add_media', {
94 title : 'wordpress.add_media',
95 image : url + '/img/image.gif',
99 // Add Media buttons to fullscreen and handle align buttons for image captions
100 ed.onBeforeExecCommand.add(function(ed, cmd, ui, val, o) {
101 var DOM = tinymce.DOM, n, DL, DIV, cls, a, align;
102 if ( 'mceFullScreen' == cmd ) {
103 if ( 'mce_fullscreen' != ed.id && DOM.select('a.thickbox').length )
104 ed.settings.theme_advanced_buttons1 += ',|,add_media';
107 if ( 'JustifyLeft' == cmd || 'JustifyRight' == cmd || 'JustifyCenter' == cmd ) {
108 n = ed.selection.getNode();
110 if ( n.nodeName == 'IMG' ) {
111 align = cmd.substr(7).toLowerCase();
113 DL = ed.dom.getParent(n, 'dl.wp-caption');
114 DIV = ed.dom.getParent(n, 'div.mceTemp');
117 cls = ed.dom.hasClass(DL, a) ? 'alignnone' : a;
118 DL.className = DL.className.replace(/align[^ '"]+\s?/g, '');
119 ed.dom.addClass(DL, cls);
121 if (cls == 'aligncenter')
122 ed.dom.addClass(DIV, 'mceIEcenter');
124 ed.dom.removeClass(DIV, 'mceIEcenter');
127 ed.execCommand('mceRepaint');
129 if ( ed.dom.hasClass(n, a) )
130 ed.dom.addClass(n, 'alignnone');
132 ed.dom.removeClass(n, 'alignnone');
137 if ( tinymce.isWebKit && ( 'InsertUnorderedList' == cmd || 'InsertOrderedList' == cmd ) ) {
139 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;}');
141 ed.getDoc().head.appendChild( style );
145 ed.onExecCommand.add( function( ed, cmd, ui, val ) {
146 if ( tinymce.isWebKit && style && ( 'InsertUnorderedList' == cmd || 'InsertOrderedList' == cmd ) )
147 ed.dom.remove( style );
150 ed.onInit.add(function(ed) {
151 var bodyClass = ed.getParam('body_class', ''), body = ed.getBody();
155 bodyClass = bodyClass.split(' ');
159 if ( ed.getParam('directionality', '') == 'rtl' )
160 bodyClass.push('rtl');
163 bodyClass.push('ie9');
164 else if ( tinymce.isIE8 )
165 bodyClass.push('ie8');
166 else if ( tinymce.isIE7 )
167 bodyClass.push('ie7');
169 if ( ed.id != 'wp_mce_fullscreen' && ed.id != 'mce_fullscreen' )
170 bodyClass.push('wp-editor');
171 else if ( ed.id == 'mce_fullscreen' )
172 bodyClass.push('mce-fullscreen');
174 tinymce.each( bodyClass, function(cls){
176 ed.dom.addClass(body, cls);
179 // make sure these run last
180 ed.onNodeChange.add( function(ed, cm, e) {
183 if ( e.nodeName == 'IMG' ) {
184 DL = ed.dom.getParent(e, 'dl.wp-caption');
185 } else if ( e.nodeName == 'DIV' && ed.dom.hasClass(e, 'mceTemp') ) {
188 if ( ! ed.dom.hasClass(DL, 'wp-caption') )
193 if ( ed.dom.hasClass(DL, 'alignleft') )
194 cm.setActive('justifyleft', 1);
195 else if ( ed.dom.hasClass(DL, 'alignright') )
196 cm.setActive('justifyright', 1);
197 else if ( ed.dom.hasClass(DL, 'aligncenter') )
198 cm.setActive('justifycenter', 1);
202 // remove invalid parent paragraphs when pasting HTML and/or switching to the HTML editor and back
203 ed.onBeforeSetContent.add(function(ed, o) {
205 o.content = o.content.replace(/<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>');
206 o.content = o.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '</$1>');
212 if ( 'undefined' != typeof(jQuery) ) {
213 ed.onKeyUp.add(function(ed, e) {
214 var k = e.keyCode || e.charCode;
219 if ( 13 == k || 8 == last || 46 == last )
220 jQuery(document).triggerHandler('wpcountwords', [ ed.getContent({format : 'raw'}) ]);
226 // keep empty paragraphs :(
227 ed.onSaveContent.addToTop(function(ed, o) {
228 o.content = o.content.replace(/<p>(<br ?\/?>|\u00a0|\uFEFF)?<\/p>/g, '<p> </p>');
231 // Fix bug in iOS Safari where it's impossible to type after a touchstart event on the parent document.
232 // Happens after zooming in or out while the keyboard is open. See #25131.
233 if ( tinymce.isIOS5 ) {
234 ed.onKeyDown.add( function() {
235 if ( document.activeElement == document.body ) {
241 ed.onSaveContent.add(function(ed, o) {
242 // If editor is hidden, we just want the textarea's value to be saved
244 o.content = o.element.value;
245 else if ( ed.getParam('wpautop', true) && typeof(switchEditors) == 'object' )
246 o.content = switchEditors.pre_wpautop(o.content);
250 ed.onBeforeSetContent.add(function(ed, o) {
251 o.content = t._setEmbed(o.content);
254 ed.onPostProcess.add(function(ed, o) {
256 o.content = t._getEmbed(o.content);
260 // Add listeners to handle more break
261 t._handleMoreBreak(ed, url);
263 // Add custom shortcuts
264 mod_key = 'alt+shift';
266 // if ( tinymce.isGecko ) // disable for mow, too many shortcuts conflicts
267 // mod_key = 'ctrl+alt';
269 ed.addShortcut(mod_key + '+c', 'justifycenter_desc', 'JustifyCenter');
270 ed.addShortcut(mod_key + '+r', 'justifyright_desc', 'JustifyRight');
271 ed.addShortcut(mod_key + '+l', 'justifyleft_desc', 'JustifyLeft');
272 ed.addShortcut(mod_key + '+j', 'justifyfull_desc', 'JustifyFull');
273 ed.addShortcut(mod_key + '+q', 'blockquote_desc', 'mceBlockQuote');
274 ed.addShortcut(mod_key + '+u', 'bullist_desc', 'InsertUnorderedList');
275 ed.addShortcut(mod_key + '+o', 'numlist_desc', 'InsertOrderedList');
276 ed.addShortcut(mod_key + '+n', 'spellchecker.desc', 'mceSpellCheck');
277 ed.addShortcut(mod_key + '+a', 'link_desc', 'WP_Link');
278 ed.addShortcut(mod_key + '+s', 'unlink_desc', 'unlink');
279 ed.addShortcut(mod_key + '+m', 'image_desc', 'WP_Medialib');
280 ed.addShortcut(mod_key + '+z', 'wordpress.wp_adv_desc', 'WP_Adv');
281 ed.addShortcut(mod_key + '+t', 'wordpress.wp_more_desc', 'WP_More');
282 ed.addShortcut(mod_key + '+d', 'striketrough_desc', 'Strikethrough');
283 ed.addShortcut(mod_key + '+h', 'help_desc', 'WP_Help');
284 ed.addShortcut(mod_key + '+p', 'wordpress.wp_page_desc', 'WP_Page');
285 ed.addShortcut('ctrl+s', 'save_desc', function(){if('function'==typeof autosave)autosave();});
287 if ( /\bwpfullscreen\b/.test(ed.settings.plugins) )
288 ed.addShortcut(mod_key + '+w', 'wordpress.wp_fullscreen_desc', 'wpFullScreen');
289 else if ( /\bfullscreen\b/.test(ed.settings.plugins) )
290 ed.addShortcut(mod_key + '+g', 'fullscreen.desc', 'mceFullScreen');
292 // popup buttons for images and the gallery
293 ed.onInit.add(function(ed) {
294 tinymce.dom.Event.add(ed.getWin(), 'scroll', function(e) {
295 ed.plugins.wordpress._hideButtons();
297 tinymce.dom.Event.add(ed.getBody(), 'dragstart', function(e) {
298 ed.plugins.wordpress._hideButtons();
302 ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
303 ed.plugins.wordpress._hideButtons();
306 ed.onSaveContent.add(function(ed, o) {
307 ed.plugins.wordpress._hideButtons();
310 ed.onMouseDown.add(function(ed, e) {
311 if ( e.target.nodeName != 'IMG' )
312 ed.plugins.wordpress._hideButtons();
315 ed.onKeyDown.add(function(ed, e){
316 if ( e.which == tinymce.VK.DELETE || e.which == tinymce.VK.BACKSPACE )
317 ed.plugins.wordpress._hideButtons();
320 closeOnClick = function(e){
323 if ( e.target.id == 'mceModalBlocker' || e.target.className == 'ui-widget-overlay' ) {
324 for ( id in ed.windowManager.windows ) {
325 ed.windowManager.close(null, id);
330 // close popups when clicking on the background
331 tinymce.dom.Event.remove(document.body, 'click', closeOnClick);
332 tinymce.dom.Event.add(document.body, 'click', closeOnClick);
335 getInfo : function() {
337 longname : 'WordPress Plugin',
338 author : 'WordPress', // add Moxiecode?
339 authorurl : 'http://wordpress.org',
340 infourl : 'http://wordpress.org',
345 // Internal functions
346 _setEmbed : function(c) {
347 return c.replace(/\[embed\]([\s\S]+?)\[\/embed\][\s\u00a0]*/g, function(a,b){
348 return '<img width="300" height="200" src="' + tinymce.baseURL + '/plugins/wordpress/img/trans.gif" class="wp-oembed mceItemNoResize" alt="'+b+'" title="'+b+'" />';
352 _getEmbed : function(c) {
353 return c.replace(/<img[^>]+>/g, function(a) {
354 if ( a.indexOf('class="wp-oembed') != -1 ) {
355 var u = a.match(/alt="([^\"]+)"/);
357 a = '[embed]' + u[1] + '[/embed]';
363 _showButtons : function(n, id) {
364 var ed = tinyMCE.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y;
366 vp = ed.dom.getViewPort(ed.getWin());
367 p1 = DOM.getPos(ed.getContentAreaContainer());
368 p2 = ed.dom.getPos(n);
370 X = Math.max(p2.x - vp.x, 0) + p1.x;
371 Y = Math.max(p2.y - vp.y, 0) + p1.y;
380 _hideButtons : function() {
381 var DOM = tinymce.DOM;
382 DOM.hide( DOM.select('#wp_editbtns, #wp_gallerybtns') );
385 // Resizes the iframe by a relative height value
386 _resizeIframe : function(ed, tb_id, dy) {
387 var ifr = ed.getContentAreaContainer().firstChild;
389 DOM.setStyle(ifr, 'height', ifr.clientHeight + dy); // Resize iframe
390 ed.theme.deltaHeight += dy; // For resize cookie
393 _handleMoreBreak : function(ed, url) {
394 var moreHTML, nextpageHTML;
396 moreHTML = '<img src="' + url + '/img/trans.gif" alt="$1" class="mce-wp-more mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
397 nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mce-wp-nextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
399 // Display morebreak instead if img in element path
400 ed.onPostRender.add(function() {
401 if (ed.theme.onResolveName) {
402 ed.theme.onResolveName.add(function(th, o) {
403 if (o.node.nodeName == 'IMG') {
404 if ( ed.dom.hasClass(o.node, 'mce-wp-more') )
406 if ( ed.dom.hasClass(o.node, 'mce-wp-nextpage') )
414 // Replace morebreak with images
415 ed.onBeforeSetContent.add(function(ed, o) {
417 o.content = o.content.replace(/<!--more(.*?)-->/g, moreHTML);
418 o.content = o.content.replace(/<!--nextpage-->/g, nextpageHTML);
422 // Replace images with morebreak
423 ed.onPostProcess.add(function(ed, o) {
425 o.content = o.content.replace(/<img[^>]+>/g, function(im) {
426 if (im.indexOf('class="mce-wp-more') !== -1) {
427 var m, moretext = (m = im.match(/alt="(.*?)"/)) ? m[1] : '';
428 im = '<!--more'+moretext+'-->';
430 if (im.indexOf('class="mce-wp-nextpage') !== -1)
431 im = '<!--nextpage-->';
437 // Set active buttons if user selected pagebreak or more break
438 ed.onNodeChange.add(function(ed, cm, n) {
439 cm.setActive('wp_page', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mce-wp-nextpage'));
440 cm.setActive('wp_more', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mce-wp-more'));
446 tinymce.PluginManager.add('wordpress', tinymce.plugins.WordPress);