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');
67 ed.addCommand('WP_Medialib', function() {
68 var id = ed.getParam('wp_fullscreen_editor_id') || ed.getParam('fullscreen_editor_id') || ed.id,
69 link = tinymce.DOM.select('#wp-' + id + '-media-buttons a.thickbox');
71 if ( link && link[0] )
76 tb_show('', link.href);
77 tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
81 ed.addButton('wp_more', {
82 title : 'wordpress.wp_more_desc',
86 ed.addButton('wp_page', {
87 title : 'wordpress.wp_page_desc',
88 image : url + '/img/page.gif',
92 ed.addButton('wp_help', {
93 title : 'wordpress.wp_help_desc',
97 ed.addButton('wp_adv', {
98 title : 'wordpress.wp_adv_desc',
103 ed.addButton('add_media', {
104 title : 'wordpress.add_media',
105 image : url + '/img/image.gif',
109 // Add Media buttons to fullscreen and handle align buttons for image captions
110 ed.onBeforeExecCommand.add(function(ed, cmd, ui, val, o) {
111 var DOM = tinymce.DOM, n, DL, DIV, cls, a, align;
112 if ( 'mceFullScreen' == cmd ) {
113 if ( 'mce_fullscreen' != ed.id && DOM.select('a.thickbox').length )
114 ed.settings.theme_advanced_buttons1 += ',|,add_media';
117 if ( 'JustifyLeft' == cmd || 'JustifyRight' == cmd || 'JustifyCenter' == cmd ) {
118 n = ed.selection.getNode();
120 if ( n.nodeName == 'IMG' ) {
121 align = cmd.substr(7).toLowerCase();
123 DL = ed.dom.getParent(n, 'dl.wp-caption');
124 DIV = ed.dom.getParent(n, 'div.mceTemp');
127 cls = ed.dom.hasClass(DL, a) ? 'alignnone' : a;
128 DL.className = DL.className.replace(/align[^ '"]+\s?/g, '');
129 ed.dom.addClass(DL, cls);
131 if (cls == 'aligncenter')
132 ed.dom.addClass(DIV, 'mceIEcenter');
134 ed.dom.removeClass(DIV, 'mceIEcenter');
137 ed.execCommand('mceRepaint');
139 if ( ed.dom.hasClass(n, a) )
140 ed.dom.addClass(n, 'alignnone');
142 ed.dom.removeClass(n, 'alignnone');
148 ed.onInit.add(function(ed) {
149 // make sure these run last
150 ed.onNodeChange.add( function(ed, cm, e) {
153 if ( e.nodeName == 'IMG' ) {
154 DL = ed.dom.getParent(e, 'dl.wp-caption');
155 } else if ( e.nodeName == 'DIV' && ed.dom.hasClass(e, 'mceTemp') ) {
158 if ( ! ed.dom.hasClass(DL, 'wp-caption') )
163 if ( ed.dom.hasClass(DL, 'alignleft') )
164 cm.setActive('justifyleft', 1);
165 else if ( ed.dom.hasClass(DL, 'alignright') )
166 cm.setActive('justifyright', 1);
167 else if ( ed.dom.hasClass(DL, 'aligncenter') )
168 cm.setActive('justifycenter', 1);
172 if ( ed.id != 'wp_mce_fullscreen' && ed.id != 'mce_fullscreen' )
173 ed.dom.addClass(ed.getBody(), 'wp-editor');
174 else if ( ed.id == 'mce_fullscreen' )
175 ed.dom.addClass(ed.getBody(), 'mce-fullscreen');
177 // remove invalid parent paragraphs when pasting HTML and/or switching to the HTML editor and back
178 ed.onBeforeSetContent.add(function(ed, o) {
180 o.content = o.content.replace(/<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>');
181 o.content = o.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '</$1>');
187 if ( 'undefined' != typeof(jQuery) ) {
188 ed.onKeyUp.add(function(ed, e) {
189 var k = e.keyCode || e.charCode;
194 if ( 13 == k || 8 == last || 46 == last )
195 jQuery(document).triggerHandler('wpcountwords', [ ed.getContent({format : 'raw'}) ]);
201 // keep empty paragraphs :(
202 ed.onSaveContent.addToTop(function(ed, o) {
203 o.content = o.content.replace(/<p>(<br ?\/?>|\u00a0|\uFEFF)?<\/p>/g, '<p> </p>');
206 ed.onSaveContent.add(function(ed, o) {
207 if ( ed.getParam('wpautop', true) && typeof(switchEditors) == 'object' ) {
209 o.content = o.element.value;
211 o.content = switchEditors.pre_wpautop(o.content);
216 ed.onBeforeSetContent.add(function(ed, o) {
217 o.content = t._setEmbed(o.content);
220 ed.onPostProcess.add(function(ed, o) {
222 o.content = t._getEmbed(o.content);
226 // Add listeners to handle more break
227 t._handleMoreBreak(ed, url);
229 // Add custom shortcuts
230 ed.addShortcut('alt+shift+c', ed.getLang('justifycenter_desc'), 'JustifyCenter');
231 ed.addShortcut('alt+shift+r', ed.getLang('justifyright_desc'), 'JustifyRight');
232 ed.addShortcut('alt+shift+l', ed.getLang('justifyleft_desc'), 'JustifyLeft');
233 ed.addShortcut('alt+shift+j', ed.getLang('justifyfull_desc'), 'JustifyFull');
234 ed.addShortcut('alt+shift+q', ed.getLang('blockquote_desc'), 'mceBlockQuote');
235 ed.addShortcut('alt+shift+u', ed.getLang('bullist_desc'), 'InsertUnorderedList');
236 ed.addShortcut('alt+shift+o', ed.getLang('numlist_desc'), 'InsertOrderedList');
237 ed.addShortcut('alt+shift+d', ed.getLang('striketrough_desc'), 'Strikethrough');
238 ed.addShortcut('alt+shift+n', ed.getLang('spellchecker.desc'), 'mceSpellCheck');
239 ed.addShortcut('alt+shift+a', ed.getLang('link_desc'), 'mceLink');
240 ed.addShortcut('alt+shift+s', ed.getLang('unlink_desc'), 'unlink');
241 ed.addShortcut('alt+shift+m', ed.getLang('image_desc'), 'WP_Medialib');
242 ed.addShortcut('alt+shift+g', ed.getLang('fullscreen.desc'), 'mceFullScreen');
243 ed.addShortcut('alt+shift+z', ed.getLang('wp_adv_desc'), 'WP_Adv');
244 ed.addShortcut('alt+shift+h', ed.getLang('help_desc'), 'WP_Help');
245 ed.addShortcut('alt+shift+t', ed.getLang('wp_more_desc'), 'WP_More');
246 ed.addShortcut('alt+shift+p', ed.getLang('wp_page_desc'), 'WP_Page');
247 ed.addShortcut('ctrl+s', ed.getLang('save_desc'), function(){if('function'==typeof autosave)autosave();});
249 if ( tinymce.isWebKit ) {
250 ed.addShortcut('alt+shift+b', ed.getLang('bold_desc'), 'Bold');
251 ed.addShortcut('alt+shift+i', ed.getLang('italic_desc'), 'Italic');
254 ed.onInit.add(function(ed) {
255 tinymce.dom.Event.add(ed.getWin(), 'scroll', function(e) {
256 ed.plugins.wordpress._hideButtons();
258 tinymce.dom.Event.add(ed.getBody(), 'dragstart', function(e) {
259 ed.plugins.wordpress._hideButtons();
263 ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
264 ed.plugins.wordpress._hideButtons();
267 ed.onSaveContent.add(function(ed, o) {
268 ed.plugins.wordpress._hideButtons();
271 ed.onMouseDown.add(function(ed, e) {
272 if ( e.target.nodeName != 'IMG' )
273 ed.plugins.wordpress._hideButtons();
277 getInfo : function() {
279 longname : 'WordPress Plugin',
280 author : 'WordPress', // add Moxiecode?
281 authorurl : 'http://wordpress.org',
282 infourl : 'http://wordpress.org',
287 // Internal functions
288 _setEmbed : function(c) {
289 return c.replace(/\[embed\]([\s\S]+?)\[\/embed\][\s\u00a0]*/g, function(a,b){
290 return '<img width="300" height="200" src="' + tinymce.baseURL + '/plugins/wordpress/img/trans.gif" class="wp-oembed mceItemNoResize" alt="'+b+'" title="'+b+'" />';
294 _getEmbed : function(c) {
295 return c.replace(/<img[^>]+>/g, function(a) {
296 if ( a.indexOf('class="wp-oembed') != -1 ) {
297 var u = a.match(/alt="([^\"]+)"/);
299 a = '[embed]' + u[1] + '[/embed]';
305 _showButtons : function(n, id) {
306 var ed = tinyMCE.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y;
308 vp = ed.dom.getViewPort(ed.getWin());
309 p1 = DOM.getPos(ed.getContentAreaContainer());
310 p2 = ed.dom.getPos(n);
312 X = Math.max(p2.x - vp.x, 0) + p1.x;
313 Y = Math.max(p2.y - vp.y, 0) + p1.y;
322 clearTimeout(this.mceTout);
324 this.mceTout = setTimeout( function(){ed.plugins.wordpress._hideButtons();}, 5000 );
327 _hideButtons : function() {
331 if ( document.getElementById('wp_editbtns') )
332 tinymce.DOM.hide('wp_editbtns');
334 if ( document.getElementById('wp_gallerybtns') )
335 tinymce.DOM.hide('wp_gallerybtns');
337 clearTimeout(this.mceTout);
341 // Resizes the iframe by a relative height value
342 _resizeIframe : function(ed, tb_id, dy) {
343 var ifr = ed.getContentAreaContainer().firstChild;
345 DOM.setStyle(ifr, 'height', ifr.clientHeight + dy); // Resize iframe
346 ed.theme.deltaHeight += dy; // For resize cookie
349 _handleMoreBreak : function(ed, url) {
350 var moreHTML, nextpageHTML;
352 moreHTML = '<img src="' + url + '/img/trans.gif" alt="$1" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
353 nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
355 // Load plugin specific CSS into editor
356 ed.onInit.add(function() {
357 ed.dom.loadCSS(url + '/css/content.css');
360 // Display morebreak instead if img in element path
361 ed.onPostRender.add(function() {
362 if (ed.theme.onResolveName) {
363 ed.theme.onResolveName.add(function(th, o) {
364 if (o.node.nodeName == 'IMG') {
365 if ( ed.dom.hasClass(o.node, 'mceWPmore') )
367 if ( ed.dom.hasClass(o.node, 'mceWPnextpage') )
375 // Replace morebreak with images
376 ed.onBeforeSetContent.add(function(ed, o) {
378 o.content = o.content.replace(/<!--more(.*?)-->/g, moreHTML);
379 o.content = o.content.replace(/<!--nextpage-->/g, nextpageHTML);
383 // Replace images with morebreak
384 ed.onPostProcess.add(function(ed, o) {
386 o.content = o.content.replace(/<img[^>]+>/g, function(im) {
387 if (im.indexOf('class="mceWPmore') !== -1) {
388 var m, moretext = (m = im.match(/alt="(.*?)"/)) ? m[1] : '';
389 im = '<!--more'+moretext+'-->';
391 if (im.indexOf('class="mceWPnextpage') !== -1)
392 im = '<!--nextpage-->';
398 // Set active buttons if user selected pagebreak or more break
399 ed.onNodeChange.add(function(ed, cm, n) {
400 cm.setActive('wp_page', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPnextpage'));
401 cm.setActive('wp_more', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPmore'));
407 tinymce.PluginManager.add('wordpress', tinymce.plugins.WordPress);