3 tinymce.create('tinymce.plugins.wpEditImage', {
5 init : function(ed, url) {
6 var t = this, mouse = {};
11 // Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('...');
12 ed.addCommand('WP_EditImage', function() {
13 var el = ed.selection.getNode(), vp = tinymce.DOM.getViewPort(), H = vp.h, W = ( 720 < vp.w ) ? 720 : vp.w, cls = ed.dom.getAttrib(el, 'class');
15 if ( cls.indexOf('mceItem') != -1 || cls.indexOf('wpGallery') != -1 || el.nodeName != 'IMG' )
18 tb_show('', url + '/editimage.html?ver=321&TB_iframe=true');
19 tinymce.DOM.setStyles('TB_window', {
20 'width':( W - 50 )+'px',
21 'height':( H - 45 )+'px',
22 'margin-left':'-'+parseInt((( W - 50 ) / 2),10) + 'px'
25 if ( ! tinymce.isIE6 ) {
26 tinymce.DOM.setStyles('TB_window', {
32 tinymce.DOM.setStyles('TB_iframeContent', {
33 'width':( W - 50 )+'px',
34 'height':( H - 75 )+'px'
36 tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
39 ed.onInit.add(function(ed) {
40 tinymce.dom.Event.add(ed.getBody(), 'dragstart', function(e) {
41 if ( !tinymce.isGecko && e.target.nodeName == 'IMG' && ed.dom.getParent(e.target, 'dl.wp-caption') )
42 return tinymce.dom.Event.cancel(e);
46 // resize the caption <dl> when the image is soft-resized by the user (only possible in Firefox and IE)
47 ed.onMouseUp.add(function(ed, e) {
48 if ( tinymce.isWebKit || tinymce.isOpera )
51 if ( mouse.x && (e.clientX != mouse.x || e.clientY != mouse.y) ) {
52 var n = ed.selection.getNode();
54 if ( 'IMG' == n.nodeName ) {
55 window.setTimeout(function(){
58 if ( n.width != mouse.img_w || n.height != mouse.img_h )
59 n.className = n.className.replace(/size-[^ "']+/, '');
61 if ( ed.dom.getParent(n, 'div.mceTemp') ) {
62 DL = ed.dom.getParent(n, 'dl.wp-caption');
65 width = ed.dom.getAttrib(n, 'width') || n.width;
66 width = parseInt(width, 10);
67 ed.dom.setStyle(DL, 'width', 10 + width);
68 ed.execCommand('mceRepaint');
78 // show editimage buttons
79 ed.onMouseDown.add(function(ed, e) {
81 if ( e.target && ( e.target.nodeName == 'IMG' || (e.target.firstChild && e.target.firstChild.nodeName == 'IMG') ) ) {
85 img_w: e.target.clientWidth,
86 img_h: e.target.clientHeight
89 if ( ed.dom.getAttrib(e.target, 'class').indexOf('mceItem') == -1 )
90 ed.plugins.wordpress._showButtons(e.target, 'wp_editbtns');
94 // when pressing Return inside a caption move the cursor to a new parapraph under it
95 ed.onKeyPress.add(function(ed, e) {
98 if ( e.keyCode == 13 ) {
99 n = ed.selection.getNode();
100 DL = ed.dom.getParent(n, 'dl.wp-caption');
101 DIV = ed.dom.getParent(DL, 'div.mceTemp');
104 P = ed.dom.create('p', {}, ' ');
105 ed.dom.insertAfter( P, DIV );
108 ed.selection.select(P.firstChild);
110 ed.selection.select(P);
112 tinymce.dom.Event.cancel(e);
118 ed.onBeforeSetContent.add(function(ed, o) {
119 o.content = t._do_shcode(o.content);
122 ed.onPostProcess.add(function(ed, o) {
124 o.content = t._get_shcode(o.content);
128 _do_shcode : function(co) {
129 return co.replace(/(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?[\s\u00a0]*/g, function(a,b,c){
130 var id, cls, w, cap, div_cls;
132 b = b.replace(/\\'|\\'|\\'/g, ''').replace(/\\"|\\"/g, '"');
133 c = c.replace(/\\'|\\'/g, ''').replace(/\\"/g, '"');
134 id = b.match(/id=['"]([^'"]+)/i);
135 cls = b.match(/align=['"]([^'"]+)/i);
136 w = b.match(/width=['"]([0-9]+)/);
137 cap = b.match(/caption=['"]([^'"]+)/i);
139 id = ( id && id[1] ) ? id[1] : '';
140 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
141 w = ( w && w[1] ) ? w[1] : '';
142 cap = ( cap && cap[1] ) ? cap[1] : '';
143 if ( ! w || ! cap ) return c;
145 div_cls = (cls == 'aligncenter') ? 'mceTemp mceIEcenter' : 'mceTemp';
147 return '<div class="'+div_cls+'" draggable><dl id="'+id+'" class="wp-caption '+cls+'" style="width: '+(10+parseInt(w))+
148 'px"><dt class="wp-caption-dt">'+c+'</dt><dd class="wp-caption-dd">'+cap+'</dd></dl></div>';
152 _get_shcode : function(co) {
153 return co.replace(/<div class="mceTemp[^"]*">\s*<dl([^>]+)>\s*<dt[^>]+>([\s\S]+?)<\/dt>\s*<dd[^>]+>(.+?)<\/dd>\s*<\/dl>\s*<\/div>\s*/gi, function(a,b,c,cap){
156 id = b.match(/id=['"]([^'"]+)/i);
157 cls = b.match(/class=['"]([^'"]+)/i);
158 w = c.match(/width=['"]([0-9]+)/);
160 id = ( id && id[1] ) ? id[1] : '';
161 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
162 w = ( w && w[1] ) ? w[1] : '';
164 if ( ! w || ! cap ) return c;
165 cls = cls.match(/align[^ '"]+/) || 'alignnone';
166 cap = cap.replace(/<\S[^<>]*>/gi, '').replace(/'/g, ''').replace(/"/g, '"');
168 return '[caption id="'+id+'" align="'+cls+'" width="'+w+'" caption="'+cap+'"]'+c+'[/caption]';
172 _createButtons : function() {
173 var t = this, ed = tinyMCE.activeEditor, DOM = tinymce.DOM, editButton, dellButton;
175 DOM.remove('wp_editbtns');
177 DOM.add(document.body, 'div', {
179 style : 'display:none;'
182 editButton = DOM.add('wp_editbtns', 'img', {
183 src : t.url+'/img/image.png',
184 id : 'wp_editimgbtn',
187 title : ed.getLang('wpeditimage.edit_img')
190 tinymce.dom.Event.add(editButton, 'mousedown', function(e) {
191 var ed = tinyMCE.activeEditor;
192 ed.windowManager.bookmark = ed.selection.getBookmark('simple');
193 ed.execCommand("WP_EditImage");
196 dellButton = DOM.add('wp_editbtns', 'img', {
197 src : t.url+'/img/delete.png',
201 title : ed.getLang('wpeditimage.del_img')
204 tinymce.dom.Event.add(dellButton, 'mousedown', function(e) {
205 var ed = tinyMCE.activeEditor, el = ed.selection.getNode(), p;
207 if ( el.nodeName == 'IMG' && ed.dom.getAttrib(el, 'class').indexOf('mceItem') == -1 ) {
208 if ( (p = ed.dom.getParent(el, 'div')) && ed.dom.hasClass(p, 'mceTemp') )
210 else if ( (p = ed.dom.getParent(el, 'A')) && p.childNodes.length == 1 )
215 ed.execCommand('mceRepaint');
221 getInfo : function() {
223 longname : 'Edit Image',
224 author : 'WordPress',
225 authorurl : 'http://wordpress.org',
232 tinymce.PluginManager.add('wpeditimage', tinymce.plugins.wpEditImage);