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;
14 var cls = ed.dom.getAttrib(el, 'class');
16 if ( cls.indexOf('mceItem') != -1 || cls.indexOf('wpGallery') != -1 || el.nodeName != 'IMG' )
19 tb_show('', url + '/editimage.html?ver=321&TB_iframe=true');
20 tinymce.DOM.setStyles('TB_window', {
21 'width':( W - 50 )+'px',
22 'height':( H - 45 )+'px',
23 'margin-left':'-'+parseInt((( W - 50 ) / 2),10) + 'px'
26 if ( ! tinymce.isIE6 ) {
27 tinymce.DOM.setStyles('TB_window', {
33 tinymce.DOM.setStyles('TB_iframeContent', {
34 'width':( W - 50 )+'px',
35 'height':( H - 75 )+'px'
37 tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
40 ed.onInit.add(function(ed) {
41 tinymce.dom.Event.add(ed.getWin(), 'scroll', function(e) {
42 ed.plugins.wpeditimage.hideButtons();
46 ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
47 ed.plugins.wpeditimage.hideButtons();
50 ed.onSaveContent.add(function(ed, o) {
51 ed.plugins.wpeditimage.hideButtons();
54 ed.onMouseUp.add(function(ed, e) {
55 if ( tinymce.isOpera ) {
56 if ( e.target.nodeName == 'IMG' )
57 ed.plugins.wpeditimage.showButtons(e.target);
58 } else if ( ! tinymce.isWebKit ) {
59 var n = ed.selection.getNode(), DL;
61 if ( n.nodeName == 'IMG' && (DL = ed.dom.getParent(n, 'DL')) ) {
62 window.setTimeout(function(){
63 var ed = tinyMCE.activeEditor, n = ed.selection.getNode(), DL = ed.dom.getParent(n, 'DL');
65 if ( n.width != (parseInt(ed.dom.getStyle(DL, 'width')) - 10) ) {
66 ed.dom.setStyle(DL, 'width', parseInt(n.width)+10);
67 ed.execCommand('mceRepaint');
74 ed.onMouseDown.add(function(ed, e) {
75 if ( tinymce.isOpera || e.target.nodeName != 'IMG' ) {
79 ed.plugins.wpeditimage.showButtons(e.target);
82 ed.onKeyPress.add(function(ed, e) {
85 if ( e.keyCode == 13 && (DL = ed.dom.getParent(ed.selection.getNode(), 'DL')) ) {
86 var P = ed.dom.create('p', {}, ' ');
87 if ( (DIV = DL.parentNode) && DIV.nodeName == 'DIV' )
88 ed.dom.insertAfter( P, DIV );
89 else ed.dom.insertAfter( P, DL );
91 tinymce.dom.Event.cancel(e);
92 ed.selection.select(P);
97 ed.onBeforeSetContent.add(function(ed, o) {
98 o.content = t._do_shcode(o.content);
101 ed.onPostProcess.add(function(ed, o) {
103 o.content = t._get_shcode(o.content);
107 _do_shcode : function(co) {
108 return co.replace(/\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\][\s\u00a0]*/g, function(a,b,c){
109 b = b.replace(/\\'|\\'|\\'/g, ''').replace(/\\"|\\"/g, '"');
110 c = c.replace(/\\'|\\'/g, ''').replace(/\\"/g, '"');
111 var id = b.match(/id=['"]([^'"]+)/i), cls = b.match(/align=['"]([^'"]+)/i);
112 var w = b.match(/width=['"]([0-9]+)/), cap = b.match(/caption=['"]([^'"]+)/i);
114 id = ( id && id[1] ) ? id[1] : '';
115 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
116 w = ( w && w[1] ) ? w[1] : '';
117 cap = ( cap && cap[1] ) ? cap[1] : '';
118 if ( ! w || ! cap ) return c;
120 var div_cls = (cls == 'aligncenter') ? 'mceTemp mceIEcenter' : 'mceTemp';
122 return '<div class="'+div_cls+'"><dl id="'+id+'" class="wp-caption '+cls+'" style="width: '+(10+parseInt(w))+
123 'px"><dt class="wp-caption-dt">'+c+'</dt><dd class="wp-caption-dd">'+cap+'</dd></dl></div>';
127 _get_shcode : function(co) {
128 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){
129 var id = b.match(/id=['"]([^'"]+)/i), cls = b.match(/class=['"]([^'"]+)/i);
130 var w = c.match(/width=['"]([0-9]+)/);
132 id = ( id && id[1] ) ? id[1] : '';
133 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
134 w = ( w && w[1] ) ? w[1] : '';
136 if ( ! w || ! cap ) return c;
137 cls = cls.match(/align[^ '"]+/) || 'alignnone';
138 cap = cap.replace(/<\S[^<>]*>/gi, '').replace(/'/g, ''').replace(/"/g, '"');
140 return '[caption id="'+id+'" align="'+cls+'" width="'+w+'" caption="'+cap+'"]'+c+'[/caption]';
144 showButtons : function(n) {
145 var t = this, ed = tinyMCE.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y;
146 var cls = ed.dom.getAttrib(n, 'class');
148 if ( cls.indexOf('mceItem') != -1 || cls.indexOf('wpGallery') != -1 )
151 vp = ed.dom.getViewPort(ed.getWin());
152 p1 = DOM.getPos(ed.getContentAreaContainer());
153 p2 = ed.dom.getPos(n);
155 X = Math.max(p2.x - vp.x, 0) + p1.x;
156 Y = Math.max(p2.y - vp.y, 0) + p1.y;
158 DOM.setStyles('wp_editbtns', {
164 t.btnsTout = window.setTimeout( function(){ed.plugins.wpeditimage.hideButtons();}, 5000 );
167 hideButtons : function() {
168 if ( tinymce.DOM.isHidden('wp_editbtns') ) return;
170 tinymce.DOM.hide('wp_editbtns');
171 window.clearTimeout(this.btnsTout);
174 _createButtons : function() {
175 var t = this, ed = tinyMCE.activeEditor, DOM = tinymce.DOM;
177 DOM.remove('wp_editbtns');
179 var wp_editbtns = DOM.add(document.body, 'div', {
181 style : 'display:none;'
184 var wp_editimgbtn = DOM.add('wp_editbtns', 'img', {
185 src : t.url+'/img/image.png',
186 id : 'wp_editimgbtn',
189 title : ed.getLang('wpeditimage.edit_img')
192 wp_editimgbtn.onmousedown = function(e) {
193 var ed = tinyMCE.activeEditor;
194 ed.windowManager.bookmark = ed.selection.getBookmark('simple');
195 ed.execCommand("WP_EditImage");
196 this.parentNode.style.display = 'none';
199 var wp_delimgbtn = DOM.add('wp_editbtns', 'img', {
200 src : t.url+'/img/delete.png',
204 title : ed.getLang('wpeditimage.del_img')
207 wp_delimgbtn.onmousedown = function(e) {
208 var ed = tinyMCE.activeEditor, el = ed.selection.getNode(), p;
210 if ( el.nodeName == 'IMG' && ed.dom.getAttrib(el, 'class').indexOf('mceItem') == -1 ) {
211 if ( (p = ed.dom.getParent(el, 'div')) && ed.dom.hasClass(p, 'mceTemp') )
213 else if ( (p = ed.dom.getParent(el, 'A')) && p.childNodes.length == 1 )
215 else ed.dom.remove(el);
217 this.parentNode.style.display = 'none';
218 ed.execCommand('mceRepaint');
224 getInfo : function() {
226 longname : 'Edit Image',
227 author : 'WordPress',
228 authorurl : 'http://wordpress.org',
235 tinymce.PluginManager.add('wpeditimage', tinymce.plugins.wpEditImage);