/Web/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin_src.js

https://bitbucket.org/jimjenkins5/blog · JavaScript · 284 lines · 267 code · 15 blank · 2 comment · 24 complexity · 23acb7d19b52e8fafaa84c3a342cfe7c MD5 · raw file

  1. (function() {
  2. tinymce.create('tinymce.plugins.wpEditImage', {
  3. url: '',
  4. editor: {},
  5. init: function(ed, url) {
  6. var t = this, mouse = {};
  7. t.url = url;
  8. t.editor = ed;
  9. t._createButtons();
  10. // Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('...');
  11. ed.addCommand('WP_EditImage', function() {
  12. var el = ed.selection.getNode(), vp, H, W, cls = ed.dom.getAttrib(el, 'class');
  13. if ( cls.indexOf('mceItem') != -1 || cls.indexOf('wpGallery') != -1 || el.nodeName != 'IMG' )
  14. return;
  15. vp = tinymce.DOM.getViewPort();
  16. H = 680 < (vp.h - 70) ? 680 : vp.h - 70;
  17. W = 650 < vp.w ? 650 : vp.w;
  18. ed.windowManager.open({
  19. file: url + '/editimage.html',
  20. width: W+'px',
  21. height: H+'px',
  22. inline: true
  23. });
  24. });
  25. ed.onInit.add(function(ed) {
  26. ed.dom.events.add(ed.getBody(), 'dragstart', function(e) {
  27. var parent;
  28. if ( e.target.nodeName == 'IMG' && ( parent = ed.dom.getParent(e.target, 'div.mceTemp') ) ) {
  29. ed.selection.select(parent);
  30. }
  31. });
  32. });
  33. // resize the caption <dl> when the image is soft-resized by the user (only possible in Firefox and IE)
  34. ed.onMouseUp.add(function(ed, e) {
  35. if ( tinymce.isWebKit || tinymce.isOpera )
  36. return;
  37. if ( mouse.x && (e.clientX != mouse.x || e.clientY != mouse.y) ) {
  38. var n = ed.selection.getNode();
  39. if ( 'IMG' == n.nodeName ) {
  40. window.setTimeout(function(){
  41. var DL = ed.dom.getParent(n, 'dl.wp-caption'), width;
  42. if ( n.width != mouse.img_w || n.height != mouse.img_h )
  43. n.className = n.className.replace(/size-[^ "']+/, '');
  44. if ( DL ) {
  45. width = ed.dom.getAttrib(n, 'width') || n.width;
  46. width = parseInt(width, 10);
  47. ed.dom.setStyle(DL, 'width', 10 + width);
  48. ed.execCommand('mceRepaint');
  49. }
  50. }, 100);
  51. }
  52. }
  53. mouse = {};
  54. });
  55. // show editimage buttons
  56. ed.onMouseDown.add(function(ed, e) {
  57. var target = e.target;
  58. if ( target.nodeName != 'IMG' ) {
  59. if ( target.firstChild && target.firstChild.nodeName == 'IMG' && target.childNodes.length == 1 )
  60. target = target.firstChild;
  61. else
  62. return;
  63. }
  64. if ( ed.dom.getAttrib(target, 'class').indexOf('mceItem') == -1 ) {
  65. mouse = {
  66. x: e.clientX,
  67. y: e.clientY,
  68. img_w: target.clientWidth,
  69. img_h: target.clientHeight
  70. };
  71. ed.plugins.wordpress._showButtons(target, 'wp_editbtns');
  72. }
  73. });
  74. // when pressing Return inside a caption move the caret to a new parapraph under it
  75. ed.onKeyPress.add(function(ed, e) {
  76. var n, DL, DIV, P;
  77. if ( e.keyCode == 13 ) {
  78. n = ed.selection.getNode();
  79. DL = ed.dom.getParent(n, 'dl.wp-caption');
  80. if ( DL )
  81. DIV = ed.dom.getParent(DL, 'div.mceTemp');
  82. if ( DIV ) {
  83. P = ed.dom.create('p', {}, '<br>');
  84. ed.dom.insertAfter( P, DIV );
  85. ed.selection.select(P.firstChild);
  86. if ( tinymce.isIE ) {
  87. ed.selection.setContent('');
  88. } else {
  89. ed.selection.setContent('<br _moz_dirty="">');
  90. ed.selection.setCursorLocation(P, 0);
  91. }
  92. ed.dom.events.cancel(e);
  93. return false;
  94. }
  95. }
  96. });
  97. ed.onBeforeSetContent.add(function(ed, o) {
  98. o.content = ed.wpSetImgCaption(o.content);
  99. });
  100. ed.onPostProcess.add(function(ed, o) {
  101. if (o.get)
  102. o.content = ed.wpGetImgCaption(o.content);
  103. });
  104. ed.wpSetImgCaption = function(content) {
  105. return t._do_shcode(content);
  106. };
  107. ed.wpGetImgCaption = function(content) {
  108. return t._get_shcode(content);
  109. };
  110. },
  111. _do_shcode : function(content) {
  112. return content.replace(/(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function(a,b,c){
  113. var id, cls, w, cap, div_cls, img, trim = tinymce.trim;
  114. id = b.match(/id=['"]([^'"]*)['"] ?/);
  115. if ( id )
  116. b = b.replace(id[0], '');
  117. cls = b.match(/align=['"]([^'"]*)['"] ?/);
  118. if ( cls )
  119. b = b.replace(cls[0], '');
  120. w = b.match(/width=['"]([0-9]*)['"] ?/);
  121. if ( w )
  122. b = b.replace(w[0], '');
  123. c = trim(c);
  124. img = c.match(/((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i);
  125. if ( img && img[2] ) {
  126. cap = trim( img[2] );
  127. img = trim( img[1] );
  128. } else {
  129. // old captions shortcode style
  130. cap = trim(b).replace(/caption=['"]/, '').replace(/['"]$/, '');
  131. img = c;
  132. }
  133. id = ( id && id[1] ) ? id[1] : '';
  134. cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
  135. w = ( w && w[1] ) ? w[1] : '';
  136. if ( !w || !cap )
  137. return c;
  138. div_cls = 'mceTemp';
  139. if ( cls == 'aligncenter' )
  140. div_cls += ' mceIEcenter';
  141. return '<div class="'+div_cls+'"><dl id="'+id+'" class="wp-caption '+cls+'" style="width: '+( 10 + parseInt(w) )+
  142. 'px"><dt class="wp-caption-dt">'+img+'</dt><dd class="wp-caption-dd">'+cap+'</dd></dl></div>';
  143. });
  144. },
  145. _get_shcode : function(content) {
  146. return content.replace(/<div (?:id="attachment_|class="mceTemp)[^>]*>([\s\S]+?)<\/div>/g, function(a, b){
  147. var ret = b.replace(/<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>/gi, function(a,b,c,cap){
  148. var id, cls, w;
  149. w = c.match(/width="([0-9]*)"/);
  150. w = ( w && w[1] ) ? w[1] : '';
  151. if ( !w || !cap )
  152. return c;
  153. id = b.match(/id="([^"]*)"/);
  154. id = ( id && id[1] ) ? id[1] : '';
  155. cls = b.match(/class="([^"]*)"/);
  156. cls = ( cls && cls[1] ) ? cls[1] : '';
  157. cls = cls.match(/align[a-z]+/) || 'alignnone';
  158. cap = cap.replace(/\r\n|\r/g, '\n').replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
  159. // no line breaks inside HTML tags
  160. return a.replace(/[\r\n\t]+/, ' ');
  161. });
  162. // convert remaining line breaks to <br>
  163. cap = cap.replace(/\s*\n\s*/g, '<br />');
  164. return '[caption id="'+id+'" align="'+cls+'" width="'+w+'"]'+c+' '+cap+'[/caption]';
  165. });
  166. if ( ret.indexOf('[caption') !== 0 ) {
  167. // the caption html seems brocken, try to find the image that may be wrapped in a link
  168. // and may be followed by <p> with the caption text.
  169. ret = b.replace(/[\s\S]*?((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)(<p>[\s\S]*<\/p>)?[\s\S]*/gi, '<p>$1</p>$2');
  170. }
  171. return ret;
  172. });
  173. },
  174. _createButtons : function() {
  175. var t = this, ed = tinyMCE.activeEditor, DOM = tinymce.DOM, editButton, dellButton;
  176. DOM.remove('wp_editbtns');
  177. DOM.add(document.body, 'div', {
  178. id : 'wp_editbtns',
  179. style : 'display:none;'
  180. });
  181. editButton = DOM.add('wp_editbtns', 'img', {
  182. src : t.url+'/img/image.png',
  183. id : 'wp_editimgbtn',
  184. width : '24',
  185. height : '24',
  186. title : ed.getLang('wpeditimage.edit_img')
  187. });
  188. tinymce.dom.Event.add(editButton, 'mousedown', function(e) {
  189. var ed = tinyMCE.activeEditor;
  190. ed.windowManager.bookmark = ed.selection.getBookmark('simple');
  191. ed.execCommand("WP_EditImage");
  192. });
  193. dellButton = DOM.add('wp_editbtns', 'img', {
  194. src : t.url+'/img/delete.png',
  195. id : 'wp_delimgbtn',
  196. width : '24',
  197. height : '24',
  198. title : ed.getLang('wpeditimage.del_img')
  199. });
  200. tinymce.dom.Event.add(dellButton, 'mousedown', function(e) {
  201. var ed = tinyMCE.activeEditor, el = ed.selection.getNode(), p;
  202. if ( el.nodeName == 'IMG' && ed.dom.getAttrib(el, 'class').indexOf('mceItem') == -1 ) {
  203. if ( (p = ed.dom.getParent(el, 'div')) && ed.dom.hasClass(p, 'mceTemp') )
  204. ed.dom.remove(p);
  205. else if ( (p = ed.dom.getParent(el, 'A')) && p.childNodes.length == 1 )
  206. ed.dom.remove(p);
  207. else
  208. ed.dom.remove(el);
  209. ed.execCommand('mceRepaint');
  210. return false;
  211. }
  212. });
  213. },
  214. getInfo : function() {
  215. return {
  216. longname : 'Edit Image',
  217. author : 'WordPress',
  218. authorurl : 'http://wordpress.org',
  219. infourl : '',
  220. version : "1.0"
  221. };
  222. }
  223. });
  224. tinymce.PluginManager.add('wpeditimage', tinymce.plugins.wpEditImage);
  225. })();