8 tinymce.create('tinymce.plugins.WordPress', {
11 init : function(ed, url) {
12 var t = this, tbId = ed.getParam('wordpress_adv_toolbar', 'toolbar2'), last = 0, moreHTML, nextpageHTML;
13 moreHTML = '<img src="' + url + '/img/trans.gif" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
14 nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
16 if ( getUserSetting('hidetb', '0') == '1' )
17 ed.settings.wordpress_adv_hidden = 0;
19 // Hides the specified toolbar and resizes the iframe
20 ed.onPostRender.add(function() {
21 var adv_toolbar = ed.controlManager.get(tbId);
22 if ( ed.getParam('wordpress_adv_hidden', 1) && adv_toolbar ) {
23 DOM.hide(adv_toolbar.id);
24 t._resizeIframe(ed, tbId, 28);
29 ed.addCommand('WP_More', function() {
30 ed.execCommand('mceInsertContent', 0, moreHTML);
33 ed.addCommand('WP_Page', function() {
34 ed.execCommand('mceInsertContent', 0, nextpageHTML);
37 ed.addCommand('WP_Help', function() {
38 ed.windowManager.open({
39 url : tinymce.baseURL + '/wp-mce-help.php',
46 ed.addCommand('WP_Adv', function() {
47 var cm = ed.controlManager, id = cm.get(tbId).id;
49 if ( 'undefined' == id )
52 if ( DOM.isHidden(id) ) {
53 cm.setActive('wp_adv', 1);
55 t._resizeIframe(ed, tbId, -28);
56 ed.settings.wordpress_adv_hidden = 0;
57 setUserSetting('hidetb', '1');
59 cm.setActive('wp_adv', 0);
61 t._resizeIframe(ed, tbId, 28);
62 ed.settings.wordpress_adv_hidden = 1;
63 setUserSetting('hidetb', '0');
68 ed.addButton('wp_more', {
69 title : 'wordpress.wp_more_desc',
73 ed.addButton('wp_page', {
74 title : 'wordpress.wp_page_desc',
75 image : url + '/img/page.gif',
79 ed.addButton('wp_help', {
80 title : 'wordpress.wp_help_desc',
84 ed.addButton('wp_adv', {
85 title : 'wordpress.wp_adv_desc',
90 ed.addButton('add_media', {
91 title : 'wordpress.add_media',
92 image : url + '/img/media.gif',
93 onclick : function() {
94 tb_show('', tinymce.DOM.get('add_media').href);
95 tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
99 ed.addButton('add_image', {
100 title : 'wordpress.add_image',
101 image : url + '/img/image.gif',
102 onclick : function() {
103 tb_show('', tinymce.DOM.get('add_image').href);
104 tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
108 ed.addButton('add_video', {
109 title : 'wordpress.add_video',
110 image : url + '/img/video.gif',
111 onclick : function() {
112 tb_show('', tinymce.DOM.get('add_video').href);
113 tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
117 ed.addButton('add_audio', {
118 title : 'wordpress.add_audio',
119 image : url + '/img/audio.gif',
120 onclick : function() {
121 tb_show('', tinymce.DOM.get('add_audio').href);
122 tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
126 // Add Media buttons to fullscreen and handle align buttons for image captions
127 ed.onBeforeExecCommand.add(function(ed, cmd, ui, val, o) {
128 var DOM = tinymce.DOM, n, DL, DIV, cls, a, align;
129 if ( 'mceFullScreen' == cmd ) {
130 if ( 'mce_fullscreen' != ed.id && DOM.get('add_audio') && DOM.get('add_video') && DOM.get('add_image') && DOM.get('add_media') )
131 ed.settings.theme_advanced_buttons1 += ',|,add_image,add_video,add_audio,add_media';
134 if ( 'JustifyLeft' == cmd || 'JustifyRight' == cmd || 'JustifyCenter' == cmd ) {
135 n = ed.selection.getNode();
137 if ( n.nodeName == 'IMG' ) {
138 align = cmd.substr(7).toLowerCase();
140 DL = ed.dom.getParent(n, 'dl.wp-caption');
141 DIV = ed.dom.getParent(n, 'div.mceTemp');
144 cls = ed.dom.hasClass(DL, a) ? 'alignnone' : a;
145 DL.className = DL.className.replace(/align[^ '"]+\s?/g, '');
146 ed.dom.addClass(DL, cls);
148 if (cls == 'aligncenter')
149 ed.dom.addClass(DIV, 'mceIEcenter');
151 ed.dom.removeClass(DIV, 'mceIEcenter');
154 ed.execCommand('mceRepaint');
156 if ( ed.dom.hasClass(n, a) )
157 ed.dom.addClass(n, 'alignnone');
159 ed.dom.removeClass(n, 'alignnone');
165 ed.onInit.add(function(ed) {
166 // make sure these run last
167 ed.onNodeChange.add( function(ed, cm, e) {
170 if ( e.nodeName == 'IMG' ) {
171 DL = ed.dom.getParent(e, 'dl.wp-caption');
172 } else if ( e.nodeName == 'DIV' && ed.dom.hasClass(e, 'mceTemp') ) {
175 if ( ! ed.dom.hasClass(DL, 'wp-caption') )
180 if ( ed.dom.hasClass(DL, 'alignleft') )
181 cm.setActive('justifyleft', 1);
182 else if ( ed.dom.hasClass(DL, 'alignright') )
183 cm.setActive('justifyright', 1);
184 else if ( ed.dom.hasClass(DL, 'aligncenter') )
185 cm.setActive('justifycenter', 1);
189 if ( ed.id != 'wp_mce_fullscreen' )
190 ed.dom.addClass(ed.getBody(), 'wp-editor');
192 // remove invalid parent paragraphs when pasting HTML and/or switching to the HTML editor and back
193 ed.onBeforeSetContent.add(function(ed, o) {
195 o.content = o.content.replace(/<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>');
196 o.content = o.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '</$1>');
202 if ( 'undefined' != typeof(jQuery) ) {
203 ed.onKeyUp.add(function(ed, e) {
204 var k = e.keyCode || e.charCode;
209 if ( 13 == k || 8 == last || 46 == last )
210 jQuery(document).triggerHandler('wpcountwords', [ ed.getContent({format : 'raw'}) ]);
216 // keep empty paragraphs :(
217 ed.onSaveContent.addToTop(function(ed, o) {
218 o.content = o.content.replace(/<p>(<br ?\/?>|\u00a0|\uFEFF)?<\/p>/g, '<p> </p>');
221 ed.onSaveContent.add(function(ed, o) {
222 if ( typeof(switchEditors) == 'object' ) {
224 o.content = o.element.value;
226 o.content = switchEditors.pre_wpautop(o.content);
231 ed.onBeforeSetContent.add(function(ed, o) {
232 o.content = t._setEmbed(o.content);
235 ed.onPostProcess.add(function(ed, o) {
237 o.content = t._getEmbed(o.content);
241 // Add listeners to handle more break
242 t._handleMoreBreak(ed, url);
244 // Add custom shortcuts
245 ed.addShortcut('alt+shift+c', ed.getLang('justifycenter_desc'), 'JustifyCenter');
246 ed.addShortcut('alt+shift+r', ed.getLang('justifyright_desc'), 'JustifyRight');
247 ed.addShortcut('alt+shift+l', ed.getLang('justifyleft_desc'), 'JustifyLeft');
248 ed.addShortcut('alt+shift+j', ed.getLang('justifyfull_desc'), 'JustifyFull');
249 ed.addShortcut('alt+shift+q', ed.getLang('blockquote_desc'), 'mceBlockQuote');
250 ed.addShortcut('alt+shift+u', ed.getLang('bullist_desc'), 'InsertUnorderedList');
251 ed.addShortcut('alt+shift+o', ed.getLang('numlist_desc'), 'InsertOrderedList');
252 ed.addShortcut('alt+shift+d', ed.getLang('striketrough_desc'), 'Strikethrough');
253 ed.addShortcut('alt+shift+n', ed.getLang('spellchecker.desc'), 'mceSpellCheck');
254 ed.addShortcut('alt+shift+a', ed.getLang('link_desc'), 'mceLink');
255 ed.addShortcut('alt+shift+s', ed.getLang('unlink_desc'), 'unlink');
256 ed.addShortcut('alt+shift+m', ed.getLang('image_desc'), 'mceImage');
257 ed.addShortcut('alt+shift+g', ed.getLang('fullscreen.desc'), 'mceFullScreen');
258 ed.addShortcut('alt+shift+z', ed.getLang('wp_adv_desc'), 'WP_Adv');
259 ed.addShortcut('alt+shift+h', ed.getLang('help_desc'), 'WP_Help');
260 ed.addShortcut('alt+shift+t', ed.getLang('wp_more_desc'), 'WP_More');
261 ed.addShortcut('alt+shift+p', ed.getLang('wp_page_desc'), 'WP_Page');
262 ed.addShortcut('ctrl+s', ed.getLang('save_desc'), function(){if('function'==typeof autosave)autosave();});
264 if ( tinymce.isWebKit ) {
265 ed.addShortcut('alt+shift+b', ed.getLang('bold_desc'), 'Bold');
266 ed.addShortcut('alt+shift+i', ed.getLang('italic_desc'), 'Italic');
269 ed.onInit.add(function(ed) {
270 tinymce.dom.Event.add(ed.getWin(), 'scroll', function(e) {
271 ed.plugins.wordpress._hideButtons();
273 tinymce.dom.Event.add(ed.getBody(), 'dragstart', function(e) {
274 ed.plugins.wordpress._hideButtons();
278 ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
279 ed.plugins.wordpress._hideButtons();
282 ed.onSaveContent.add(function(ed, o) {
283 ed.plugins.wordpress._hideButtons();
286 ed.onMouseDown.add(function(ed, e) {
287 if ( e.target.nodeName != 'IMG' )
288 ed.plugins.wordpress._hideButtons();
292 getInfo : function() {
294 longname : 'WordPress Plugin',
295 author : 'WordPress', // add Moxiecode?
296 authorurl : 'http://wordpress.org',
297 infourl : 'http://wordpress.org',
302 // Internal functions
303 _setEmbed : function(c) {
304 return c.replace(/\[embed\]([\s\S]+?)\[\/embed\][\s\u00a0]*/g, function(a,b){
305 return '<img width="300" height="200" src="' + tinymce.baseURL + '/plugins/wordpress/img/trans.gif" class="wp-oembed mceItemNoResize" alt="'+b+'" title="'+b+'" />';
309 _getEmbed : function(c) {
310 return c.replace(/<img[^>]+>/g, function(a) {
311 if ( a.indexOf('class="wp-oembed') != -1 ) {
312 var u = a.match(/alt="([^\"]+)"/);
314 a = '[embed]' + u[1] + '[/embed]';
320 _showButtons : function(n, id) {
321 var ed = tinyMCE.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y;
323 vp = ed.dom.getViewPort(ed.getWin());
324 p1 = DOM.getPos(ed.getContentAreaContainer());
325 p2 = ed.dom.getPos(n);
327 X = Math.max(p2.x - vp.x, 0) + p1.x;
328 Y = Math.max(p2.y - vp.y, 0) + p1.y;
337 clearTimeout(this.mceTout);
339 this.mceTout = setTimeout( function(){ed.plugins.wordpress._hideButtons();}, 5000 );
342 _hideButtons : function() {
346 if ( document.getElementById('wp_editbtns') )
347 tinymce.DOM.hide('wp_editbtns');
349 if ( document.getElementById('wp_gallerybtns') )
350 tinymce.DOM.hide('wp_gallerybtns');
352 clearTimeout(this.mceTout);
356 // Resizes the iframe by a relative height value
357 _resizeIframe : function(ed, tb_id, dy) {
358 var ifr = ed.getContentAreaContainer().firstChild;
360 DOM.setStyle(ifr, 'height', ifr.clientHeight + dy); // Resize iframe
361 ed.theme.deltaHeight += dy; // For resize cookie
364 _handleMoreBreak : function(ed, url) {
365 var moreHTML, nextpageHTML;
367 moreHTML = '<img src="' + url + '/img/trans.gif" alt="$1" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
368 nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
370 // Load plugin specific CSS into editor
371 ed.onInit.add(function() {
372 ed.dom.loadCSS(url + '/css/content.css');
375 // Display morebreak instead if img in element path
376 ed.onPostRender.add(function() {
377 if (ed.theme.onResolveName) {
378 ed.theme.onResolveName.add(function(th, o) {
379 if (o.node.nodeName == 'IMG') {
380 if ( ed.dom.hasClass(o.node, 'mceWPmore') )
382 if ( ed.dom.hasClass(o.node, 'mceWPnextpage') )
390 // Replace morebreak with images
391 ed.onBeforeSetContent.add(function(ed, o) {
393 o.content = o.content.replace(/<!--more(.*?)-->/g, moreHTML);
394 o.content = o.content.replace(/<!--nextpage-->/g, nextpageHTML);
398 // Replace images with morebreak
399 ed.onPostProcess.add(function(ed, o) {
401 o.content = o.content.replace(/<img[^>]+>/g, function(im) {
402 if (im.indexOf('class="mceWPmore') !== -1) {
403 var m, moretext = (m = im.match(/alt="(.*?)"/)) ? m[1] : '';
404 im = '<!--more'+moretext+'-->';
406 if (im.indexOf('class="mceWPnextpage') !== -1)
407 im = '<!--nextpage-->';
413 // Set active buttons if user selected pagebreak or more break
414 ed.onNodeChange.add(function(ed, cm, n) {
415 cm.setActive('wp_page', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPnextpage'));
416 cm.setActive('wp_more', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPmore'));
422 tinymce.PluginManager.add('wordpress', tinymce.plugins.WordPress);