3 tinymce.create('tinymce.plugins.wpEditImage', {
5 init : function(ed, url) {
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 ( ed.dom.getParent(e.target, 'div.mceTemp') || ed.dom.is(e.target, 'div.mceTemp') ) {
52 window.setTimeout(function(){
53 var ed = tinyMCE.activeEditor, n = ed.selection.getNode(), DL, width;
55 if ( 'IMG' == n.nodeName ) {
56 DL = ed.dom.getParent(n, 'dl.wp-caption');
57 width = ed.dom.getAttrib(n, 'width') || n.width;
58 width = parseInt(width, 10);
60 if ( DL && width != ( parseInt(ed.dom.getStyle(DL, 'width'), 10) - 10 ) ) {
61 ed.dom.setStyle(DL, 'width', 10 + width);
62 ed.execCommand('mceRepaint');
69 // show editimage buttons
70 ed.onMouseDown.add(function(ed, e) {
73 if ( e.target.nodeName == 'IMG' && ed.dom.getAttrib(e.target, 'class').indexOf('mceItem') == -1 ) {
74 ed.plugins.wordpress._showButtons(e.target, 'wp_editbtns');
75 if ( tinymce.isGecko && (p = ed.dom.getParent(e.target, 'dl.wp-caption')) && ed.dom.hasClass(p.parentNode, 'mceTemp') )
76 ed.selection.select(p.parentNode);
80 // when pressing Return inside a caption move the cursor to a new parapraph under it
81 ed.onKeyPress.add(function(ed, e) {
84 if ( e.keyCode == 13 ) {
85 n = ed.selection.getNode();
86 DL = ed.dom.getParent(n, 'dl.wp-caption');
87 DIV = ed.dom.getParent(DL, 'div.mceTemp');
90 P = ed.dom.create('p', {}, ' ');
91 ed.dom.insertAfter( P, DIV );
94 ed.selection.select(P.firstChild);
96 ed.selection.select(P);
98 tinymce.dom.Event.cancel(e);
104 ed.onBeforeSetContent.add(function(ed, o) {
105 o.content = t._do_shcode(o.content);
108 ed.onPostProcess.add(function(ed, o) {
110 o.content = t._get_shcode(o.content);
114 _do_shcode : function(co) {
115 return co.replace(/(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?[\s\u00a0]*/g, function(a,b,c){
116 var id, cls, w, cap, div_cls;
118 b = b.replace(/\\'|\\'|\\'/g, ''').replace(/\\"|\\"/g, '"');
119 c = c.replace(/\\'|\\'/g, ''').replace(/\\"/g, '"');
120 id = b.match(/id=['"]([^'"]+)/i);
121 cls = b.match(/align=['"]([^'"]+)/i);
122 w = b.match(/width=['"]([0-9]+)/);
123 cap = b.match(/caption=['"]([^'"]+)/i);
125 id = ( id && id[1] ) ? id[1] : '';
126 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
127 w = ( w && w[1] ) ? w[1] : '';
128 cap = ( cap && cap[1] ) ? cap[1] : '';
129 if ( ! w || ! cap ) return c;
131 div_cls = (cls == 'aligncenter') ? 'mceTemp mceIEcenter' : 'mceTemp';
133 return '<div class="'+div_cls+'" draggable><dl id="'+id+'" class="wp-caption '+cls+'" style="width: '+(10+parseInt(w))+
134 'px"><dt class="wp-caption-dt">'+c+'</dt><dd class="wp-caption-dd">'+cap+'</dd></dl></div>';
138 _get_shcode : function(co) {
139 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){
142 id = b.match(/id=['"]([^'"]+)/i);
143 cls = b.match(/class=['"]([^'"]+)/i);
144 w = c.match(/width=['"]([0-9]+)/);
146 id = ( id && id[1] ) ? id[1] : '';
147 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
148 w = ( w && w[1] ) ? w[1] : '';
150 if ( ! w || ! cap ) return c;
151 cls = cls.match(/align[^ '"]+/) || 'alignnone';
152 cap = cap.replace(/<\S[^<>]*>/gi, '').replace(/'/g, ''').replace(/"/g, '"');
154 return '[caption id="'+id+'" align="'+cls+'" width="'+w+'" caption="'+cap+'"]'+c+'[/caption]';
158 _createButtons : function() {
159 var t = this, ed = tinyMCE.activeEditor, DOM = tinymce.DOM, editButton, dellButton;
161 DOM.remove('wp_editbtns');
163 DOM.add(document.body, 'div', {
165 style : 'display:none;'
168 editButton = DOM.add('wp_editbtns', 'img', {
169 src : t.url+'/img/image.png',
170 id : 'wp_editimgbtn',
173 title : ed.getLang('wpeditimage.edit_img')
176 tinymce.dom.Event.add(editButton, 'mousedown', function(e) {
177 var ed = tinyMCE.activeEditor;
178 ed.windowManager.bookmark = ed.selection.getBookmark('simple');
179 ed.execCommand("WP_EditImage");
182 dellButton = DOM.add('wp_editbtns', 'img', {
183 src : t.url+'/img/delete.png',
187 title : ed.getLang('wpeditimage.del_img')
190 tinymce.dom.Event.add(dellButton, 'mousedown', function(e) {
191 var ed = tinyMCE.activeEditor, el = ed.selection.getNode(), p;
193 if ( el.nodeName == 'IMG' && ed.dom.getAttrib(el, 'class').indexOf('mceItem') == -1 ) {
194 if ( (p = ed.dom.getParent(el, 'div')) && ed.dom.hasClass(p, 'mceTemp') )
196 else if ( (p = ed.dom.getParent(el, 'A')) && p.childNodes.length == 1 )
201 ed.execCommand('mceRepaint');
207 getInfo : function() {
209 longname : 'Edit Image',
210 author : 'WordPress',
211 authorurl : 'http://wordpress.org',
218 tinymce.PluginManager.add('wpeditimage', tinymce.plugins.wpEditImage);