[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/js/tinymce/plugins/wordpress/ -> editor_plugin.dev.js (source)

   1  /**
   2   * WordPress plugin.
   3   */
   4  
   5  (function() {
   6      var DOM = tinymce.DOM;
   7  
   8      tinymce.create('tinymce.plugins.WordPress', {
   9          mceTout : 0,
  10  
  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')+'" />';
  15  
  16              if ( getUserSetting('hidetb', '0') == '1' )
  17                  ed.settings.wordpress_adv_hidden = 0;
  18  
  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);
  25                  }
  26              });
  27  
  28              // Register commands
  29              ed.addCommand('WP_More', function() {
  30                  ed.execCommand('mceInsertContent', 0, moreHTML);
  31              });
  32  
  33              ed.addCommand('WP_Page', function() {
  34                  ed.execCommand('mceInsertContent', 0, nextpageHTML);
  35              });
  36  
  37              ed.addCommand('WP_Help', function() {
  38                  ed.windowManager.open({
  39                      url : tinymce.baseURL + '/wp-mce-help.php',
  40                      width : 450,
  41                      height : 420,
  42                      inline : 1
  43                  });
  44              });
  45  
  46              ed.addCommand('WP_Adv', function() {
  47                  var cm = ed.controlManager, id = cm.get(tbId).id;
  48  
  49                  if ( 'undefined' == id )
  50                      return;
  51  
  52                  if ( DOM.isHidden(id) ) {
  53                      cm.setActive('wp_adv', 1);
  54                      DOM.show(id);
  55                      t._resizeIframe(ed, tbId, -28);
  56                      ed.settings.wordpress_adv_hidden = 0;
  57                      setUserSetting('hidetb', '1');
  58                  } else {
  59                      cm.setActive('wp_adv', 0);
  60                      DOM.hide(id);
  61                      t._resizeIframe(ed, tbId, 28);
  62                      ed.settings.wordpress_adv_hidden = 1;
  63                      setUserSetting('hidetb', '0');
  64                  }
  65              });
  66              
  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');
  70  
  71                  if ( link && link[0] )
  72                      link = link[0];
  73                  else
  74                      return;
  75  
  76                  tb_show('', link.href);
  77                  tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
  78              });
  79  
  80              // Register buttons
  81              ed.addButton('wp_more', {
  82                  title : 'wordpress.wp_more_desc',
  83                  cmd : 'WP_More'
  84              });
  85  
  86              ed.addButton('wp_page', {
  87                  title : 'wordpress.wp_page_desc',
  88                  image : url + '/img/page.gif',
  89                  cmd : 'WP_Page'
  90              });
  91  
  92              ed.addButton('wp_help', {
  93                  title : 'wordpress.wp_help_desc',
  94                  cmd : 'WP_Help'
  95              });
  96  
  97              ed.addButton('wp_adv', {
  98                  title : 'wordpress.wp_adv_desc',
  99                  cmd : 'WP_Adv'
 100              });
 101  
 102              // Add Media button
 103              ed.addButton('add_media', {
 104                  title : 'wordpress.add_media',
 105                  image : url + '/img/image.gif',
 106                  cmd : 'WP_Medialib'
 107              });
 108  
 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';
 115                  }
 116  
 117                  if ( 'JustifyLeft' == cmd || 'JustifyRight' == cmd || 'JustifyCenter' == cmd ) {
 118                      n = ed.selection.getNode();
 119  
 120                      if ( n.nodeName == 'IMG' ) {
 121                          align = cmd.substr(7).toLowerCase();
 122                          a = 'align' + align;
 123                          DL = ed.dom.getParent(n, 'dl.wp-caption');
 124                          DIV = ed.dom.getParent(n, 'div.mceTemp');
 125  
 126                          if ( DL && DIV ) {
 127                              cls = ed.dom.hasClass(DL, a) ? 'alignnone' : a;
 128                              DL.className = DL.className.replace(/align[^ '"]+\s?/g, '');
 129                              ed.dom.addClass(DL, cls);
 130  
 131                              if (cls == 'aligncenter')
 132                                  ed.dom.addClass(DIV, 'mceIEcenter');
 133                              else
 134                                  ed.dom.removeClass(DIV, 'mceIEcenter');
 135  
 136                              o.terminate = true;
 137                              ed.execCommand('mceRepaint');
 138                          } else {
 139                              if ( ed.dom.hasClass(n, a) )
 140                                  ed.dom.addClass(n, 'alignnone');
 141                              else
 142                                  ed.dom.removeClass(n, 'alignnone');
 143                          }
 144                      }
 145                  }
 146              });
 147              
 148              ed.onInit.add(function(ed) {
 149                  var bodyClass = ed.getParam('body_class', '');
 150  
 151                  // make sure these run last
 152                  ed.onNodeChange.add( function(ed, cm, e) {
 153                      var DL;
 154  
 155                      if ( e.nodeName == 'IMG' ) {
 156                          DL = ed.dom.getParent(e, 'dl.wp-caption');
 157                      } else if ( e.nodeName == 'DIV' && ed.dom.hasClass(e, 'mceTemp') ) {
 158                          DL = e.firstChild;
 159  
 160                          if ( ! ed.dom.hasClass(DL, 'wp-caption') )
 161                              DL = false;
 162                      }
 163  
 164                      if ( DL ) {
 165                          if ( ed.dom.hasClass(DL, 'alignleft') )
 166                              cm.setActive('justifyleft', 1);
 167                          else if ( ed.dom.hasClass(DL, 'alignright') )
 168                              cm.setActive('justifyright', 1);
 169                          else if ( ed.dom.hasClass(DL, 'aligncenter') )
 170                              cm.setActive('justifycenter', 1);
 171                      }
 172                  });
 173  
 174                  if ( ed.id != 'wp_mce_fullscreen' && ed.id != 'mce_fullscreen' )
 175                      ed.dom.addClass(ed.getBody(), 'wp-editor');
 176                  else if ( ed.id == 'mce_fullscreen' )
 177                      ed.dom.addClass(ed.getBody(), 'mce-fullscreen');
 178  
 179                  // remove invalid parent paragraphs when pasting HTML and/or switching to the HTML editor and back
 180                  ed.onBeforeSetContent.add(function(ed, o) {
 181                      if ( o.content ) {
 182                          o.content = o.content.replace(/<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>');
 183                          o.content = o.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '</$1>');
 184                      }
 185                  });
 186  
 187                  if ( bodyClass )
 188                      ed.dom.addClass(ed.getBody(), bodyClass);
 189              });
 190  
 191              // Word count
 192              if ( 'undefined' != typeof(jQuery) ) {
 193                  ed.onKeyUp.add(function(ed, e) {
 194                      var k = e.keyCode || e.charCode;
 195  
 196                      if ( k == last )
 197                          return;
 198  
 199                      if ( 13 == k || 8 == last || 46 == last )
 200                          jQuery(document).triggerHandler('wpcountwords', [ ed.getContent({format : 'raw'}) ]);
 201  
 202                      last = k;
 203                  });
 204              };
 205  
 206              // keep empty paragraphs :(
 207              ed.onSaveContent.addToTop(function(ed, o) {
 208                  o.content = o.content.replace(/<p>(<br ?\/?>|\u00a0|\uFEFF)?<\/p>/g, '<p>&nbsp;</p>');
 209              });
 210  
 211              ed.onSaveContent.add(function(ed, o) {
 212                  if ( ed.getParam('wpautop', true) && typeof(switchEditors) == 'object' ) {
 213                      if ( ed.isHidden() )
 214                          o.content = o.element.value;
 215                      else
 216                          o.content = switchEditors.pre_wpautop(o.content);
 217                  }
 218              });
 219  
 220              /* disable for now
 221              ed.onBeforeSetContent.add(function(ed, o) {
 222                  o.content = t._setEmbed(o.content);
 223              });
 224  
 225              ed.onPostProcess.add(function(ed, o) {
 226                  if ( o.get )
 227                      o.content = t._getEmbed(o.content);
 228              });
 229              */
 230  
 231              // Add listeners to handle more break
 232              t._handleMoreBreak(ed, url);
 233  
 234              // Add custom shortcuts
 235              ed.addShortcut('alt+shift+c', ed.getLang('justifycenter_desc'), 'JustifyCenter');
 236              ed.addShortcut('alt+shift+r', ed.getLang('justifyright_desc'), 'JustifyRight');
 237              ed.addShortcut('alt+shift+l', ed.getLang('justifyleft_desc'), 'JustifyLeft');
 238              ed.addShortcut('alt+shift+j', ed.getLang('justifyfull_desc'), 'JustifyFull');
 239              ed.addShortcut('alt+shift+q', ed.getLang('blockquote_desc'), 'mceBlockQuote');
 240              ed.addShortcut('alt+shift+u', ed.getLang('bullist_desc'), 'InsertUnorderedList');
 241              ed.addShortcut('alt+shift+o', ed.getLang('numlist_desc'), 'InsertOrderedList');
 242              ed.addShortcut('alt+shift+d', ed.getLang('striketrough_desc'), 'Strikethrough');
 243              ed.addShortcut('alt+shift+n', ed.getLang('spellchecker.desc'), 'mceSpellCheck');
 244              ed.addShortcut('alt+shift+a', ed.getLang('link_desc'), 'mceLink');
 245              ed.addShortcut('alt+shift+s', ed.getLang('unlink_desc'), 'unlink');
 246              ed.addShortcut('alt+shift+m', ed.getLang('image_desc'), 'WP_Medialib');
 247              ed.addShortcut('alt+shift+g', ed.getLang('fullscreen.desc'), 'mceFullScreen');
 248              ed.addShortcut('alt+shift+z', ed.getLang('wp_adv_desc'), 'WP_Adv');
 249              ed.addShortcut('alt+shift+h', ed.getLang('help_desc'), 'WP_Help');
 250              ed.addShortcut('alt+shift+t', ed.getLang('wp_more_desc'), 'WP_More');
 251              ed.addShortcut('alt+shift+p', ed.getLang('wp_page_desc'), 'WP_Page');
 252              ed.addShortcut('ctrl+s', ed.getLang('save_desc'), function(){if('function'==typeof autosave)autosave();});
 253  
 254              if ( tinymce.isWebKit ) {
 255                  ed.addShortcut('alt+shift+b', ed.getLang('bold_desc'), 'Bold');
 256                  ed.addShortcut('alt+shift+i', ed.getLang('italic_desc'), 'Italic');
 257              }
 258  
 259              ed.onInit.add(function(ed) {
 260                  tinymce.dom.Event.add(ed.getWin(), 'scroll', function(e) {
 261                      ed.plugins.wordpress._hideButtons();
 262                  });
 263                  tinymce.dom.Event.add(ed.getBody(), 'dragstart', function(e) {
 264                      ed.plugins.wordpress._hideButtons();
 265                  });
 266              });
 267  
 268              ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
 269                  ed.plugins.wordpress._hideButtons();
 270              });
 271  
 272              ed.onSaveContent.add(function(ed, o) {
 273                  ed.plugins.wordpress._hideButtons();
 274              });
 275  
 276              ed.onMouseDown.add(function(ed, e) {
 277                  if ( e.target.nodeName != 'IMG' )
 278                      ed.plugins.wordpress._hideButtons();
 279              });
 280          },
 281  
 282          getInfo : function() {
 283              return {
 284                  longname : 'WordPress Plugin',
 285                  author : 'WordPress', // add Moxiecode?
 286                  authorurl : 'http://wordpress.org',
 287                  infourl : 'http://wordpress.org',
 288                  version : '3.0'
 289              };
 290          },
 291  
 292          // Internal functions
 293          _setEmbed : function(c) {
 294              return c.replace(/\[embed\]([\s\S]+?)\[\/embed\][\s\u00a0]*/g, function(a,b){
 295                  return '<img width="300" height="200" src="' + tinymce.baseURL + '/plugins/wordpress/img/trans.gif" class="wp-oembed mceItemNoResize" alt="'+b+'" title="'+b+'" />';
 296              });
 297          },
 298  
 299          _getEmbed : function(c) {
 300              return c.replace(/<img[^>]+>/g, function(a) {
 301                  if ( a.indexOf('class="wp-oembed') != -1 ) {
 302                      var u = a.match(/alt="([^\"]+)"/);
 303                      if ( u[1] )
 304                          a = '[embed]' + u[1] + '[/embed]';
 305                  }
 306                  return a;
 307              });
 308          },
 309  
 310          _showButtons : function(n, id) {
 311              var ed = tinyMCE.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y;
 312  
 313              vp = ed.dom.getViewPort(ed.getWin());
 314              p1 = DOM.getPos(ed.getContentAreaContainer());
 315              p2 = ed.dom.getPos(n);
 316  
 317              X = Math.max(p2.x - vp.x, 0) + p1.x;
 318              Y = Math.max(p2.y - vp.y, 0) + p1.y;
 319  
 320              DOM.setStyles(id, {
 321                  'top' : Y+5+'px',
 322                  'left' : X+5+'px',
 323                  'display' : 'block'
 324              });
 325  
 326              if ( this.mceTout )
 327                  clearTimeout(this.mceTout);
 328  
 329              this.mceTout = setTimeout( function(){ed.plugins.wordpress._hideButtons();}, 5000 );
 330          },
 331  
 332          _hideButtons : function() {
 333              if ( !this.mceTout )
 334                  return;
 335  
 336              if ( document.getElementById('wp_editbtns') )
 337                  tinymce.DOM.hide('wp_editbtns');
 338  
 339              if ( document.getElementById('wp_gallerybtns') )
 340                  tinymce.DOM.hide('wp_gallerybtns');
 341  
 342              clearTimeout(this.mceTout);
 343              this.mceTout = 0;
 344          },
 345  
 346          // Resizes the iframe by a relative height value
 347          _resizeIframe : function(ed, tb_id, dy) {
 348              var ifr = ed.getContentAreaContainer().firstChild;
 349  
 350              DOM.setStyle(ifr, 'height', ifr.clientHeight + dy); // Resize iframe
 351              ed.theme.deltaHeight += dy; // For resize cookie
 352          },
 353  
 354          _handleMoreBreak : function(ed, url) {
 355              var moreHTML, nextpageHTML;
 356  
 357              moreHTML = '<img src="' + url + '/img/trans.gif" alt="$1" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
 358              nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
 359  
 360              // Load plugin specific CSS into editor
 361              ed.onInit.add(function() {
 362                  ed.dom.loadCSS(url + '/css/content.css');
 363              });
 364  
 365              // Display morebreak instead if img in element path
 366              ed.onPostRender.add(function() {
 367                  if (ed.theme.onResolveName) {
 368                      ed.theme.onResolveName.add(function(th, o) {
 369                          if (o.node.nodeName == 'IMG') {
 370                              if ( ed.dom.hasClass(o.node, 'mceWPmore') )
 371                                  o.name = 'wpmore';
 372                              if ( ed.dom.hasClass(o.node, 'mceWPnextpage') )
 373                                  o.name = 'wppage';
 374                          }
 375  
 376                      });
 377                  }
 378              });
 379  
 380              // Replace morebreak with images
 381              ed.onBeforeSetContent.add(function(ed, o) {
 382                  if ( o.content ) {
 383                      o.content = o.content.replace(/<!--more(.*?)-->/g, moreHTML);
 384                      o.content = o.content.replace(/<!--nextpage-->/g, nextpageHTML);
 385                  }
 386              });
 387  
 388              // Replace images with morebreak
 389              ed.onPostProcess.add(function(ed, o) {
 390                  if (o.get)
 391                      o.content = o.content.replace(/<img[^>]+>/g, function(im) {
 392                          if (im.indexOf('class="mceWPmore') !== -1) {
 393                              var m, moretext = (m = im.match(/alt="(.*?)"/)) ? m[1] : '';
 394                              im = '<!--more'+moretext+'-->';
 395                          }
 396                          if (im.indexOf('class="mceWPnextpage') !== -1)
 397                              im = '<!--nextpage-->';
 398  
 399                          return im;
 400                      });
 401              });
 402  
 403              // Set active buttons if user selected pagebreak or more break
 404              ed.onNodeChange.add(function(ed, cm, n) {
 405                  cm.setActive('wp_page', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPnextpage'));
 406                  cm.setActive('wp_more', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPmore'));
 407              });
 408          }
 409      });
 410  
 411      // Register plugin
 412      tinymce.PluginManager.add('wordpress', tinymce.plugins.WordPress);
 413  })();


Generated: Sat Feb 18 03:55:58 2012 Hosted by follow the white rabbit.