PageRenderTime 717ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/js/tinymce/plugins/wordpress/editor_plugin_src.js

https://bitbucket.org/lordmuffin/origin
JavaScript | 437 lines | 387 code | 36 blank | 14 comment | 38 complexity | 065e5a8b0d8875768f0b75fc1bf7b6f0 MD5 | raw file
  1. /**
  2. * WordPress plugin.
  3. */
  4. (function() {
  5. var DOM = tinymce.DOM;
  6. tinymce.create('tinymce.plugins.WordPress', {
  7. init : function(ed, url) {
  8. var t = this, tbId = ed.getParam('wordpress_adv_toolbar', 'toolbar2'), last = 0, moreHTML, nextpageHTML, closeOnClick, mod_key, style;
  9. moreHTML = '<img src="' + url + '/img/trans.gif" class="mce-wp-more mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
  10. nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mce-wp-nextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
  11. if ( getUserSetting('hidetb', '0') == '1' )
  12. ed.settings.wordpress_adv_hidden = 0;
  13. // Hides the specified toolbar and resizes the iframe
  14. ed.onPostRender.add(function() {
  15. var adv_toolbar = ed.controlManager.get(tbId);
  16. if ( ed.getParam('wordpress_adv_hidden', 1) && adv_toolbar ) {
  17. DOM.hide(adv_toolbar.id);
  18. t._resizeIframe(ed, tbId, 28);
  19. }
  20. });
  21. // Register commands
  22. ed.addCommand('WP_More', function() {
  23. ed.execCommand('mceInsertContent', 0, moreHTML);
  24. });
  25. ed.addCommand('WP_Page', function() {
  26. ed.execCommand('mceInsertContent', 0, nextpageHTML);
  27. });
  28. ed.addCommand('WP_Help', function() {
  29. ed.windowManager.open({
  30. url : tinymce.baseURL + '/wp-mce-help.php',
  31. width : 450,
  32. height : 420,
  33. inline : 1
  34. });
  35. });
  36. ed.addCommand('WP_Adv', function() {
  37. var cm = ed.controlManager, id = cm.get(tbId).id;
  38. if ( 'undefined' == id )
  39. return;
  40. if ( DOM.isHidden(id) ) {
  41. cm.setActive('wp_adv', 1);
  42. DOM.show(id);
  43. t._resizeIframe(ed, tbId, -28);
  44. ed.settings.wordpress_adv_hidden = 0;
  45. setUserSetting('hidetb', '1');
  46. } else {
  47. cm.setActive('wp_adv', 0);
  48. DOM.hide(id);
  49. t._resizeIframe(ed, tbId, 28);
  50. ed.settings.wordpress_adv_hidden = 1;
  51. setUserSetting('hidetb', '0');
  52. }
  53. });
  54. ed.addCommand('WP_Medialib', function() {
  55. if ( typeof wp !== 'undefined' && wp.media && wp.media.editor )
  56. wp.media.editor.open( ed.id );
  57. });
  58. // Register buttons
  59. ed.addButton('wp_more', {
  60. title : 'wordpress.wp_more_desc',
  61. cmd : 'WP_More'
  62. });
  63. ed.addButton('wp_page', {
  64. title : 'wordpress.wp_page_desc',
  65. image : url + '/img/page.gif',
  66. cmd : 'WP_Page'
  67. });
  68. ed.addButton('wp_help', {
  69. title : 'wordpress.wp_help_desc',
  70. cmd : 'WP_Help'
  71. });
  72. ed.addButton('wp_adv', {
  73. title : 'wordpress.wp_adv_desc',
  74. cmd : 'WP_Adv'
  75. });
  76. // Add Media button
  77. ed.addButton('add_media', {
  78. title : 'wordpress.add_media',
  79. image : url + '/img/image.gif',
  80. cmd : 'WP_Medialib'
  81. });
  82. // Add Media buttons to fullscreen and handle align buttons for image captions
  83. ed.onBeforeExecCommand.add(function(ed, cmd, ui, val, o) {
  84. var DOM = tinymce.DOM, n, DL, DIV, cls, a, align;
  85. if ( 'mceFullScreen' == cmd ) {
  86. if ( 'mce_fullscreen' != ed.id && DOM.select('a.thickbox').length )
  87. ed.settings.theme_advanced_buttons1 += ',|,add_media';
  88. }
  89. if ( 'JustifyLeft' == cmd || 'JustifyRight' == cmd || 'JustifyCenter' == cmd ) {
  90. n = ed.selection.getNode();
  91. if ( n.nodeName == 'IMG' ) {
  92. align = cmd.substr(7).toLowerCase();
  93. a = 'align' + align;
  94. DL = ed.dom.getParent(n, 'dl.wp-caption');
  95. DIV = ed.dom.getParent(n, 'div.mceTemp');
  96. if ( DL && DIV ) {
  97. cls = ed.dom.hasClass(DL, a) ? 'alignnone' : a;
  98. DL.className = DL.className.replace(/align[^ '"]+\s?/g, '');
  99. ed.dom.addClass(DL, cls);
  100. if (cls == 'aligncenter')
  101. ed.dom.addClass(DIV, 'mceIEcenter');
  102. else
  103. ed.dom.removeClass(DIV, 'mceIEcenter');
  104. o.terminate = true;
  105. ed.execCommand('mceRepaint');
  106. } else {
  107. if ( ed.dom.hasClass(n, a) )
  108. ed.dom.addClass(n, 'alignnone');
  109. else
  110. ed.dom.removeClass(n, 'alignnone');
  111. }
  112. }
  113. }
  114. if ( tinymce.isWebKit && ( 'InsertUnorderedList' == cmd || 'InsertOrderedList' == cmd ) ) {
  115. if ( !style )
  116. 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;}');
  117. ed.getDoc().head.appendChild( style );
  118. }
  119. });
  120. ed.onExecCommand.add( function( ed, cmd, ui, val ) {
  121. if ( tinymce.isWebKit && style && ( 'InsertUnorderedList' == cmd || 'InsertOrderedList' == cmd ) )
  122. ed.dom.remove( style );
  123. });
  124. ed.onInit.add(function(ed) {
  125. var bodyClass = ed.getParam('body_class', ''), body = ed.getBody();
  126. // add body classes
  127. if ( bodyClass )
  128. bodyClass = bodyClass.split(' ');
  129. else
  130. bodyClass = [];
  131. if ( ed.getParam('directionality', '') == 'rtl' )
  132. bodyClass.push('rtl');
  133. if ( tinymce.isIE9 )
  134. bodyClass.push('ie9');
  135. else if ( tinymce.isIE8 )
  136. bodyClass.push('ie8');
  137. else if ( tinymce.isIE7 )
  138. bodyClass.push('ie7');
  139. if ( ed.id != 'wp_mce_fullscreen' && ed.id != 'mce_fullscreen' )
  140. bodyClass.push('wp-editor');
  141. else if ( ed.id == 'mce_fullscreen' )
  142. bodyClass.push('mce-fullscreen');
  143. tinymce.each( bodyClass, function(cls){
  144. if ( cls )
  145. ed.dom.addClass(body, cls);
  146. });
  147. // make sure these run last
  148. ed.onNodeChange.add( function(ed, cm, e) {
  149. var DL;
  150. if ( e.nodeName == 'IMG' ) {
  151. DL = ed.dom.getParent(e, 'dl.wp-caption');
  152. } else if ( e.nodeName == 'DIV' && ed.dom.hasClass(e, 'mceTemp') ) {
  153. DL = e.firstChild;
  154. if ( ! ed.dom.hasClass(DL, 'wp-caption') )
  155. DL = false;
  156. }
  157. if ( DL ) {
  158. if ( ed.dom.hasClass(DL, 'alignleft') )
  159. cm.setActive('justifyleft', 1);
  160. else if ( ed.dom.hasClass(DL, 'alignright') )
  161. cm.setActive('justifyright', 1);
  162. else if ( ed.dom.hasClass(DL, 'aligncenter') )
  163. cm.setActive('justifycenter', 1);
  164. }
  165. });
  166. // remove invalid parent paragraphs when pasting HTML and/or switching to the HTML editor and back
  167. ed.onBeforeSetContent.add(function(ed, o) {
  168. if ( o.content ) {
  169. o.content = o.content.replace(/<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>');
  170. o.content = o.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '</$1>');
  171. }
  172. });
  173. });
  174. // Word count
  175. if ( 'undefined' != typeof(jQuery) ) {
  176. ed.onKeyUp.add(function(ed, e) {
  177. var k = e.keyCode || e.charCode;
  178. if ( k == last )
  179. return;
  180. if ( 13 == k || 8 == last || 46 == last )
  181. jQuery(document).triggerHandler('wpcountwords', [ ed.getContent({format : 'raw'}) ]);
  182. last = k;
  183. });
  184. };
  185. // keep empty paragraphs :(
  186. ed.onSaveContent.addToTop(function(ed, o) {
  187. o.content = o.content.replace(/<p>(<br ?\/?>|\u00a0|\uFEFF)?<\/p>/g, '<p>&nbsp;</p>');
  188. });
  189. ed.onSaveContent.add(function(ed, o) {
  190. // If editor is hidden, we just want the textarea's value to be saved
  191. if ( ed.isHidden() )
  192. o.content = o.element.value;
  193. else if ( ed.getParam('wpautop', true) && typeof(switchEditors) == 'object' )
  194. o.content = switchEditors.pre_wpautop(o.content);
  195. });
  196. /* disable for now
  197. ed.onBeforeSetContent.add(function(ed, o) {
  198. o.content = t._setEmbed(o.content);
  199. });
  200. ed.onPostProcess.add(function(ed, o) {
  201. if ( o.get )
  202. o.content = t._getEmbed(o.content);
  203. });
  204. */
  205. // Add listeners to handle more break
  206. t._handleMoreBreak(ed, url);
  207. // Add custom shortcuts
  208. mod_key = 'alt+shift';
  209. // if ( tinymce.isGecko ) // disable for mow, too many shortcuts conflicts
  210. // mod_key = 'ctrl+alt';
  211. ed.addShortcut(mod_key + '+c', 'justifycenter_desc', 'JustifyCenter');
  212. ed.addShortcut(mod_key + '+r', 'justifyright_desc', 'JustifyRight');
  213. ed.addShortcut(mod_key + '+l', 'justifyleft_desc', 'JustifyLeft');
  214. ed.addShortcut(mod_key + '+j', 'justifyfull_desc', 'JustifyFull');
  215. ed.addShortcut(mod_key + '+q', 'blockquote_desc', 'mceBlockQuote');
  216. ed.addShortcut(mod_key + '+u', 'bullist_desc', 'InsertUnorderedList');
  217. ed.addShortcut(mod_key + '+o', 'numlist_desc', 'InsertOrderedList');
  218. ed.addShortcut(mod_key + '+n', 'spellchecker.desc', 'mceSpellCheck');
  219. ed.addShortcut(mod_key + '+a', 'link_desc', 'WP_Link');
  220. ed.addShortcut(mod_key + '+s', 'unlink_desc', 'unlink');
  221. ed.addShortcut(mod_key + '+m', 'image_desc', 'WP_Medialib');
  222. ed.addShortcut(mod_key + '+z', 'wordpress.wp_adv_desc', 'WP_Adv');
  223. ed.addShortcut(mod_key + '+t', 'wordpress.wp_more_desc', 'WP_More');
  224. ed.addShortcut(mod_key + '+d', 'striketrough_desc', 'Strikethrough');
  225. ed.addShortcut(mod_key + '+h', 'help_desc', 'WP_Help');
  226. ed.addShortcut(mod_key + '+p', 'wordpress.wp_page_desc', 'WP_Page');
  227. ed.addShortcut('ctrl+s', 'save_desc', function(){if('function'==typeof autosave)autosave();});
  228. if ( /\bwpfullscreen\b/.test(ed.settings.plugins) )
  229. ed.addShortcut(mod_key + '+w', 'wordpress.wp_fullscreen_desc', 'wpFullScreen');
  230. else if ( /\bfullscreen\b/.test(ed.settings.plugins) )
  231. ed.addShortcut(mod_key + '+g', 'fullscreen.desc', 'mceFullScreen');
  232. // popup buttons for images and the gallery
  233. ed.onInit.add(function(ed) {
  234. tinymce.dom.Event.add(ed.getWin(), 'scroll', function(e) {
  235. ed.plugins.wordpress._hideButtons();
  236. });
  237. tinymce.dom.Event.add(ed.getBody(), 'dragstart', function(e) {
  238. ed.plugins.wordpress._hideButtons();
  239. });
  240. });
  241. ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
  242. ed.plugins.wordpress._hideButtons();
  243. });
  244. ed.onSaveContent.add(function(ed, o) {
  245. ed.plugins.wordpress._hideButtons();
  246. });
  247. ed.onMouseDown.add(function(ed, e) {
  248. if ( e.target.nodeName != 'IMG' )
  249. ed.plugins.wordpress._hideButtons();
  250. });
  251. ed.onKeyDown.add(function(ed, e){
  252. if ( e.which == tinymce.VK.DELETE || e.which == tinymce.VK.BACKSPACE )
  253. ed.plugins.wordpress._hideButtons();
  254. });
  255. closeOnClick = function(e){
  256. var id;
  257. if ( e.target.id == 'mceModalBlocker' || e.target.className == 'ui-widget-overlay' ) {
  258. for ( id in ed.windowManager.windows ) {
  259. ed.windowManager.close(null, id);
  260. }
  261. }
  262. }
  263. // close popups when clicking on the background
  264. tinymce.dom.Event.remove(document.body, 'click', closeOnClick);
  265. tinymce.dom.Event.add(document.body, 'click', closeOnClick);
  266. },
  267. getInfo : function() {
  268. return {
  269. longname : 'WordPress Plugin',
  270. author : 'WordPress', // add Moxiecode?
  271. authorurl : 'http://wordpress.org',
  272. infourl : 'http://wordpress.org',
  273. version : '3.0'
  274. };
  275. },
  276. // Internal functions
  277. _setEmbed : function(c) {
  278. return c.replace(/\[embed\]([\s\S]+?)\[\/embed\][\s\u00a0]*/g, function(a,b){
  279. return '<img width="300" height="200" src="' + tinymce.baseURL + '/plugins/wordpress/img/trans.gif" class="wp-oembed mceItemNoResize" alt="'+b+'" title="'+b+'" />';
  280. });
  281. },
  282. _getEmbed : function(c) {
  283. return c.replace(/<img[^>]+>/g, function(a) {
  284. if ( a.indexOf('class="wp-oembed') != -1 ) {
  285. var u = a.match(/alt="([^\"]+)"/);
  286. if ( u[1] )
  287. a = '[embed]' + u[1] + '[/embed]';
  288. }
  289. return a;
  290. });
  291. },
  292. _showButtons : function(n, id) {
  293. var ed = tinyMCE.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y;
  294. vp = ed.dom.getViewPort(ed.getWin());
  295. p1 = DOM.getPos(ed.getContentAreaContainer());
  296. p2 = ed.dom.getPos(n);
  297. X = Math.max(p2.x - vp.x, 0) + p1.x;
  298. Y = Math.max(p2.y - vp.y, 0) + p1.y;
  299. DOM.setStyles(id, {
  300. 'top' : Y+5+'px',
  301. 'left' : X+5+'px',
  302. 'display' : 'block'
  303. });
  304. },
  305. _hideButtons : function() {
  306. var DOM = tinymce.DOM;
  307. DOM.hide( DOM.select('#wp_editbtns, #wp_gallerybtns') );
  308. },
  309. // Resizes the iframe by a relative height value
  310. _resizeIframe : function(ed, tb_id, dy) {
  311. var ifr = ed.getContentAreaContainer().firstChild;
  312. DOM.setStyle(ifr, 'height', ifr.clientHeight + dy); // Resize iframe
  313. ed.theme.deltaHeight += dy; // For resize cookie
  314. },
  315. _handleMoreBreak : function(ed, url) {
  316. var moreHTML, nextpageHTML;
  317. moreHTML = '<img src="' + url + '/img/trans.gif" alt="$1" class="mce-wp-more mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
  318. nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mce-wp-nextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
  319. // Display morebreak instead if img in element path
  320. ed.onPostRender.add(function() {
  321. if (ed.theme.onResolveName) {
  322. ed.theme.onResolveName.add(function(th, o) {
  323. if (o.node.nodeName == 'IMG') {
  324. if ( ed.dom.hasClass(o.node, 'mce-wp-more') )
  325. o.name = 'wpmore';
  326. if ( ed.dom.hasClass(o.node, 'mce-wp-nextpage') )
  327. o.name = 'wppage';
  328. }
  329. });
  330. }
  331. });
  332. // Replace morebreak with images
  333. ed.onBeforeSetContent.add(function(ed, o) {
  334. if ( o.content ) {
  335. o.content = o.content.replace(/<!--more(.*?)-->/g, moreHTML);
  336. o.content = o.content.replace(/<!--nextpage-->/g, nextpageHTML);
  337. }
  338. });
  339. // Replace images with morebreak
  340. ed.onPostProcess.add(function(ed, o) {
  341. if (o.get)
  342. o.content = o.content.replace(/<img[^>]+>/g, function(im) {
  343. if (im.indexOf('class="mce-wp-more') !== -1) {
  344. var m, moretext = (m = im.match(/alt="(.*?)"/)) ? m[1] : '';
  345. im = '<!--more'+moretext+'-->';
  346. }
  347. if (im.indexOf('class="mce-wp-nextpage') !== -1)
  348. im = '<!--nextpage-->';
  349. return im;
  350. });
  351. });
  352. // Set active buttons if user selected pagebreak or more break
  353. ed.onNodeChange.add(function(ed, cm, n) {
  354. cm.setActive('wp_page', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mce-wp-nextpage'));
  355. cm.setActive('wp_more', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mce-wp-more'));
  356. });
  357. }
  358. });
  359. // Register plugin
  360. tinymce.PluginManager.add('wordpress', tinymce.plugins.WordPress);
  361. })();