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',
70 image : url + '/img/more.gif',
74 ed.addButton('wp_page', {
75 title : 'wordpress.wp_page_desc',
76 image : url + '/img/page.gif',
80 ed.addButton('wp_help', {
81 title : 'wordpress.wp_help_desc',
82 image : url + '/img/help.gif',
86 ed.addButton('wp_adv', {
87 title : 'wordpress.wp_adv_desc',
88 image : url + '/img/toolbars.gif',
93 ed.addButton('add_media', {
94 title : 'wordpress.add_media',
95 image : url + '/img/media.gif',
96 onclick : function() {
97 tb_show('', tinymce.DOM.get('add_media').href);
98 tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
102 ed.addButton('add_image', {
103 title : 'wordpress.add_image',
104 image : url + '/img/image.gif',
105 onclick : function() {
106 tb_show('', tinymce.DOM.get('add_image').href);
107 tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
111 ed.addButton('add_video', {
112 title : 'wordpress.add_video',
113 image : url + '/img/video.gif',
114 onclick : function() {
115 tb_show('', tinymce.DOM.get('add_video').href);
116 tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
120 ed.addButton('add_audio', {
121 title : 'wordpress.add_audio',
122 image : url + '/img/audio.gif',
123 onclick : function() {
124 tb_show('', tinymce.DOM.get('add_audio').href);
125 tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
129 // Add Media buttons to fullscreen and handle align buttons for image captions
130 ed.onBeforeExecCommand.add(function(ed, cmd, ui, val, o) {
131 var DOM = tinymce.DOM, n, DL, DIV, cls, a, align;
132 if ( 'mceFullScreen' == cmd ) {
133 if ( 'mce_fullscreen' != ed.id && DOM.get('add_audio') && DOM.get('add_video') && DOM.get('add_image') && DOM.get('add_media') )
134 ed.settings.theme_advanced_buttons1 += ',|,add_image,add_video,add_audio,add_media';
137 if ( 'JustifyLeft' == cmd || 'JustifyRight' == cmd || 'JustifyCenter' == cmd ) {
138 n = ed.selection.getNode();
140 if ( n.nodeName == 'IMG' ) {
141 align = cmd.substr(7).toLowerCase();
143 DL = ed.dom.getParent(n, 'dl.wp-caption');
144 DIV = ed.dom.getParent(n, 'div.mceTemp');
147 cls = ed.dom.hasClass(DL, a) ? 'alignnone' : a;
148 DL.className = DL.className.replace(/align[^ '"]+\s?/g, '');
149 ed.dom.addClass(DL, cls);
151 if (cls == 'aligncenter')
152 ed.dom.addClass(DIV, 'mceIEcenter');
154 ed.dom.removeClass(DIV, 'mceIEcenter');
157 ed.execCommand('mceRepaint');
159 if ( ed.dom.hasClass(n, a) )
160 ed.dom.addClass(n, 'alignnone');
162 ed.dom.removeClass(n, 'alignnone');
168 ed.onInit.add(function(ed) {
169 // make sure these run last
170 ed.onNodeChange.add( function(ed, cm, e) {
173 if ( e.nodeName == 'IMG' ) {
174 DL = ed.dom.getParent(e, 'dl.wp-caption');
175 } else if ( e.nodeName == 'DIV' && ed.dom.hasClass(e, 'mceTemp') ) {
178 if ( ! ed.dom.hasClass(DL, 'wp-caption') )
183 if ( ed.dom.hasClass(DL, 'alignleft') )
184 cm.setActive('justifyleft', 1);
185 else if ( ed.dom.hasClass(DL, 'alignright') )
186 cm.setActive('justifyright', 1);
187 else if ( ed.dom.hasClass(DL, 'aligncenter') )
188 cm.setActive('justifycenter', 1);
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>');
201 // Word count if script is loaded
202 if ( 'undefined' != typeof wpWordCount ) {
203 ed.onKeyUp.add(function(ed, e) {
204 if ( e.keyCode == last ) return;
205 if ( 13 == e.keyCode || 8 == last || 46 == last ) wpWordCount.wc( ed.getContent({format : 'raw'}) );
210 ed.onSaveContent.add(function(ed, o) {
211 if ( typeof(switchEditors) == 'object' ) {
213 o.content = o.element.value;
215 o.content = switchEditors.pre_wpautop(o.content);
220 ed.onBeforeSetContent.add(function(ed, o) {
221 o.content = t._setEmbed(o.content);
224 ed.onPostProcess.add(function(ed, o) {
226 o.content = t._getEmbed(o.content);
230 // Add listeners to handle more break
231 t._handleMoreBreak(ed, url);
233 // Add custom shortcuts
234 ed.addShortcut('alt+shift+c', ed.getLang('justifycenter_desc'), 'JustifyCenter');
235 ed.addShortcut('alt+shift+r', ed.getLang('justifyright_desc'), 'JustifyRight');
236 ed.addShortcut('alt+shift+l', ed.getLang('justifyleft_desc'), 'JustifyLeft');
237 ed.addShortcut('alt+shift+j', ed.getLang('justifyfull_desc'), 'JustifyFull');
238 ed.addShortcut('alt+shift+q', ed.getLang('blockquote_desc'), 'mceBlockQuote');
239 ed.addShortcut('alt+shift+u', ed.getLang('bullist_desc'), 'InsertUnorderedList');
240 ed.addShortcut('alt+shift+o', ed.getLang('numlist_desc'), 'InsertOrderedList');
241 ed.addShortcut('alt+shift+d', ed.getLang('striketrough_desc'), 'Strikethrough');
242 ed.addShortcut('alt+shift+n', ed.getLang('spellchecker.desc'), 'mceSpellCheck');
243 ed.addShortcut('alt+shift+a', ed.getLang('link_desc'), 'mceLink');
244 ed.addShortcut('alt+shift+s', ed.getLang('unlink_desc'), 'unlink');
245 ed.addShortcut('alt+shift+m', ed.getLang('image_desc'), 'mceImage');
246 ed.addShortcut('alt+shift+g', ed.getLang('fullscreen.desc'), 'mceFullScreen');
247 ed.addShortcut('alt+shift+z', ed.getLang('wp_adv_desc'), 'WP_Adv');
248 ed.addShortcut('alt+shift+h', ed.getLang('help_desc'), 'WP_Help');
249 ed.addShortcut('alt+shift+t', ed.getLang('wp_more_desc'), 'WP_More');
250 ed.addShortcut('alt+shift+p', ed.getLang('wp_page_desc'), 'WP_Page');
251 ed.addShortcut('ctrl+s', ed.getLang('save_desc'), function(){if('function'==typeof autosave)autosave();});
253 if ( tinymce.isWebKit ) {
254 ed.addShortcut('alt+shift+b', ed.getLang('bold_desc'), 'Bold');
255 ed.addShortcut('alt+shift+i', ed.getLang('italic_desc'), 'Italic');
258 ed.onInit.add(function(ed) {
259 tinymce.dom.Event.add(ed.getWin(), 'scroll', function(e) {
260 ed.plugins.wordpress._hideButtons();
262 tinymce.dom.Event.add(ed.getBody(), 'dragstart', function(e) {
263 ed.plugins.wordpress._hideButtons();
267 ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
268 ed.plugins.wordpress._hideButtons();
271 ed.onSaveContent.add(function(ed, o) {
272 ed.plugins.wordpress._hideButtons();
275 ed.onMouseDown.add(function(ed, e) {
276 if ( e.target.nodeName != 'IMG' )
277 ed.plugins.wordpress._hideButtons();
281 getInfo : function() {
283 longname : 'WordPress Plugin',
284 author : 'WordPress', // add Moxiecode?
285 authorurl : 'http://wordpress.org',
286 infourl : 'http://wordpress.org',
291 // Internal functions
292 _setEmbed : function(c) {
293 return c.replace(/\[embed\]([\s\S]+?)\[\/embed\][\s\u00a0]*/g, function(a,b){
294 return '<img width="300" height="200" src="' + tinymce.baseURL + '/plugins/wordpress/img/trans.gif" class="wp-oembed mceItemNoResize" alt="'+b+'" title="'+b+'" />';
298 _getEmbed : function(c) {
299 return c.replace(/<img[^>]+>/g, function(a) {
300 if ( a.indexOf('class="wp-oembed') != -1 ) {
301 var u = a.match(/alt="([^\"]+)"/);
303 a = '[embed]' + u[1] + '[/embed]';
309 _showButtons : function(n, id) {
310 var ed = tinyMCE.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y;
312 vp = ed.dom.getViewPort(ed.getWin());
313 p1 = DOM.getPos(ed.getContentAreaContainer());
314 p2 = ed.dom.getPos(n);
316 X = Math.max(p2.x - vp.x, 0) + p1.x;
317 Y = Math.max(p2.y - vp.y, 0) + p1.y;
326 clearTimeout(this.mceTout);
328 this.mceTout = setTimeout( function(){ed.plugins.wordpress._hideButtons();}, 5000 );
331 _hideButtons : function() {
335 if ( document.getElementById('wp_editbtns') )
336 tinymce.DOM.hide('wp_editbtns');
338 if ( document.getElementById('wp_gallerybtns') )
339 tinymce.DOM.hide('wp_gallerybtns');
341 clearTimeout(this.mceTout);
345 // Resizes the iframe by a relative height value
346 _resizeIframe : function(ed, tb_id, dy) {
347 var ifr = ed.getContentAreaContainer().firstChild;
349 DOM.setStyle(ifr, 'height', ifr.clientHeight + dy); // Resize iframe
350 ed.theme.deltaHeight += dy; // For resize cookie
353 _handleMoreBreak : function(ed, url) {
354 var moreHTML, nextpageHTML;
356 moreHTML = '<img src="' + url + '/img/trans.gif" alt="$1" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
357 nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
359 // Load plugin specific CSS into editor
360 ed.onInit.add(function() {
361 ed.dom.loadCSS(url + '/css/content.css');
364 // Display morebreak instead if img in element path
365 ed.onPostRender.add(function() {
366 if (ed.theme.onResolveName) {
367 ed.theme.onResolveName.add(function(th, o) {
368 if (o.node.nodeName == 'IMG') {
369 if ( ed.dom.hasClass(o.node, 'mceWPmore') )
371 if ( ed.dom.hasClass(o.node, 'mceWPnextpage') )
379 // Replace morebreak with images
380 ed.onBeforeSetContent.add(function(ed, o) {
382 o.content = o.content.replace(/<!--more(.*?)-->/g, moreHTML);
383 o.content = o.content.replace(/<!--nextpage-->/g, nextpageHTML);
387 // Replace images with morebreak
388 ed.onPostProcess.add(function(ed, o) {
390 o.content = o.content.replace(/<img[^>]+>/g, function(im) {
391 if (im.indexOf('class="mceWPmore') !== -1) {
392 var m, moretext = (m = im.match(/alt="(.*?)"/)) ? m[1] : '';
393 im = '<!--more'+moretext+'-->';
395 if (im.indexOf('class="mceWPnextpage') !== -1)
396 im = '<!--nextpage-->';
402 // Set active buttons if user selected pagebreak or more break
403 ed.onNodeChange.add(function(ed, cm, n) {
404 cm.setActive('wp_page', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPnextpage'));
405 cm.setActive('wp_more', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPmore'));
411 tinymce.PluginManager.add('wordpress', tinymce.plugins.WordPress);