3 tinymce.create('tinymce.plugins.wpEditImage', {
7 init: function(ed, url) {
8 var t = this, mouse = {};
14 ed.addCommand('WP_EditImage', t._editImage);
16 ed.onInit.add(function(ed) {
17 ed.dom.events.add(ed.getBody(), 'mousedown', function(e) {
20 if ( e.target.nodeName == 'IMG' && ( parent = ed.dom.getParent(e.target, 'div.mceTemp') ) ) {
21 if ( tinymce.isGecko )
22 ed.selection.select(parent);
23 else if ( tinymce.isWebKit )
24 ed.dom.events.prevent(e);
28 // when pressing Return inside a caption move the caret to a new parapraph under it
29 ed.dom.events.add(ed.getBody(), 'keydown', function(e) {
30 var n, DL, DIV, P, content;
32 if ( e.keyCode == 13 ) {
33 n = ed.selection.getNode();
34 DL = ed.dom.getParent(n, 'dl.wp-caption');
37 DIV = ed.dom.getParent(DL, 'div.mceTemp');
40 ed.dom.events.cancel(e);
41 P = ed.dom.create('p', {}, '\uFEFF');
42 ed.dom.insertAfter( P, DIV );
43 ed.selection.setCursorLocation(P, 0);
49 // iOS6 doesn't show the buttons properly on click, show them on 'touchstart'
50 if ( 'ontouchstart' in window ) {
51 ed.dom.events.add(ed.getBody(), 'touchstart', function(e){
57 // resize the caption <dl> when the image is soft-resized by the user
58 ed.onMouseUp.add(function(ed, e) {
59 if ( tinymce.isWebKit || tinymce.isOpera )
62 if ( mouse.x && (e.clientX != mouse.x || e.clientY != mouse.y) ) {
63 var n = ed.selection.getNode();
65 if ( 'IMG' == n.nodeName ) {
66 window.setTimeout(function(){
67 var DL = ed.dom.getParent(n, 'dl.wp-caption'), width;
69 if ( n.width != mouse.img_w || n.height != mouse.img_h )
70 n.className = n.className.replace(/size-[^ "']+/, '');
73 width = ed.dom.getAttrib(n, 'width') || n.width;
74 width = parseInt(width, 10);
75 ed.dom.setStyle(DL, 'width', 10 + width);
76 ed.execCommand('mceRepaint');
84 // show editimage buttons
85 ed.onMouseDown.add(function(ed, e){
89 ed.onBeforeSetContent.add(function(ed, o) {
90 o.content = ed.wpSetImgCaption(o.content);
93 ed.onPostProcess.add(function(ed, o) {
95 o.content = ed.wpGetImgCaption(o.content);
98 ed.wpSetImgCaption = function(content) {
99 return t._do_shcode(content);
102 ed.wpGetImgCaption = function(content) {
103 return t._get_shcode(content);
106 // When inserting content, if the caret is inside a caption create new paragraph under
107 // and move the caret there
108 ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
111 if ( cmd == 'mceInsertContent' ) {
112 node = ed.dom.getParent(ed.selection.getNode(), 'div.mceTemp');
117 p = ed.dom.create('p');
118 ed.dom.insertAfter( p, node );
119 ed.selection.setCursorLocation(p, 0);
124 _do_shcode : function(content) {
125 return content.replace(/(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function(a,b,c){
126 var id, cls, w, cap, div_cls, img, trim = tinymce.trim;
128 id = b.match(/id=['"]([^'"]*)['"] ?/);
130 b = b.replace(id[0], '');
132 cls = b.match(/align=['"]([^'"]*)['"] ?/);
134 b = b.replace(cls[0], '');
136 w = b.match(/width=['"]([0-9]*)['"] ?/);
138 b = b.replace(w[0], '');
141 img = c.match(/((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i);
143 if ( img && img[2] ) {
144 cap = trim( img[2] );
145 img = trim( img[1] );
147 // old captions shortcode style
148 cap = trim(b).replace(/caption=['"]/, '').replace(/['"]$/, '');
152 id = ( id && id[1] ) ? id[1] : '';
153 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
154 w = ( w && w[1] ) ? w[1] : '';
160 if ( cls == 'aligncenter' )
161 div_cls += ' mceIEcenter';
163 return '<div class="'+div_cls+'"><dl id="'+id+'" class="wp-caption '+cls+'" style="width: '+( 10 + parseInt(w) )+
164 'px"><dt class="wp-caption-dt">'+img+'</dt><dd class="wp-caption-dd">'+cap+'</dd></dl></div>';
168 _get_shcode : function(content) {
169 return content.replace(/<div (?:id="attachment_|class="mceTemp)[^>]*>([\s\S]+?)<\/div>/g, function(a, b){
170 var ret = b.replace(/<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>/gi, function(a,b,c,cap){
173 w = c.match(/width="([0-9]*)"/);
174 w = ( w && w[1] ) ? w[1] : '';
179 id = b.match(/id="([^"]*)"/);
180 id = ( id && id[1] ) ? id[1] : '';
182 cls = b.match(/class="([^"]*)"/);
183 cls = ( cls && cls[1] ) ? cls[1] : '';
184 cls = cls.match(/align[a-z]+/) || 'alignnone';
186 cap = cap.replace(/\r\n|\r/g, '\n').replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
187 // no line breaks inside HTML tags
188 return a.replace(/[\r\n\t]+/, ' ');
191 // convert remaining line breaks to <br>
192 cap = cap.replace(/\s*\n\s*/g, '<br />');
194 return '[caption id="'+id+'" align="'+cls+'" width="'+w+'"]'+c+' '+cap+'[/caption]';
197 if ( ret.indexOf('[caption') !== 0 ) {
198 // the caption html seems brocken, try to find the image that may be wrapped in a link
199 // and may be followed by <p> with the caption text.
200 ret = b.replace(/[\s\S]*?((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)(<p>[\s\S]*<\/p>)?[\s\S]*/gi, '<p>$1</p>$2');
207 _createButtons : function() {
208 var t = this, ed = tinymce.activeEditor, DOM = tinymce.DOM, editButton, dellButton, isRetina;
210 if ( DOM.get('wp_editbtns') )
213 isRetina = ( window.devicePixelRatio && window.devicePixelRatio > 1 ) || // WebKit, Opera
214 ( window.matchMedia && window.matchMedia('(min-resolution:130dpi)').matches ); // Firefox, IE10, Opera
216 DOM.add(document.body, 'div', {
218 style : 'display:none;'
221 editButton = DOM.add('wp_editbtns', 'img', {
222 src : isRetina ? t.url+'/img/image-2x.png' : t.url+'/img/image.png',
223 id : 'wp_editimgbtn',
226 title : ed.getLang('wpeditimage.edit_img')
229 tinymce.dom.Event.add(editButton, 'mousedown', function(e) {
231 ed.plugins.wordpress._hideButtons();
234 dellButton = DOM.add('wp_editbtns', 'img', {
235 src : isRetina ? t.url+'/img/delete-2x.png' : t.url+'/img/delete.png',
239 title : ed.getLang('wpeditimage.del_img')
242 tinymce.dom.Event.add(dellButton, 'mousedown', function(e) {
243 var ed = tinymce.activeEditor, el = ed.selection.getNode(), parent;
245 if ( el.nodeName == 'IMG' && ed.dom.getAttrib(el, 'class').indexOf('mceItem') == -1 ) {
246 if ( (parent = ed.dom.getParent(el, 'div')) && ed.dom.hasClass(parent, 'mceTemp') ) {
247 ed.dom.remove(parent);
249 if ( el.parentNode.nodeName == 'A' && el.parentNode.childNodes.length == 1 )
252 if ( el.parentNode.nodeName == 'P' && el.parentNode.childNodes.length == 1 )
258 ed.execCommand('mceRepaint');
261 ed.plugins.wordpress._hideButtons();
265 _editImage : function() {
266 var ed = tinymce.activeEditor, url = this.url, el = ed.selection.getNode(), vp, H, W, cls = el.className;
268 if ( cls.indexOf('mceItem') != -1 || cls.indexOf('wpGallery') != -1 || el.nodeName != 'IMG' )
271 vp = tinymce.DOM.getViewPort();
272 H = 680 < (vp.h - 70) ? 680 : vp.h - 70;
273 W = 650 < vp.w ? 650 : vp.w;
275 ed.windowManager.open({
276 file: url + '/editimage.html',
283 _showButtons : function(e) {
284 var ed = this.editor, target = e.target;
286 if ( target.nodeName != 'IMG' ) {
287 if ( target.firstChild && target.firstChild.nodeName == 'IMG' && target.childNodes.length == 1 ) {
288 target = target.firstChild;
290 ed.plugins.wordpress._hideButtons();
295 if ( ed.dom.getAttrib(target, 'class').indexOf('mceItem') == -1 ) {
299 img_w: target.clientWidth,
300 img_h: target.clientHeight
303 if ( e.type == 'touchstart' ) {
304 ed.selection.select(target);
305 ed.dom.events.cancel(e);
308 ed.plugins.wordpress._hideButtons();
309 ed.plugins.wordpress._showButtons(target, 'wp_editbtns');
313 getInfo : function() {
315 longname : 'Edit Image',
316 author : 'WordPress',
317 authorurl : 'http://wordpress.org',
324 tinymce.PluginManager.add('wpeditimage', tinymce.plugins.wpEditImage);