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.getWin(), 'scroll', function(e) {
41 ed.plugins.wpeditimage.hideButtons();
45 ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
46 ed.plugins.wpeditimage.hideButtons();
49 ed.onSaveContent.add(function(ed, o) {
50 ed.plugins.wpeditimage.hideButtons();
53 ed.onMouseUp.add(function(ed, e) {
56 if ( tinymce.isOpera ) {
57 if ( e.target.nodeName == 'IMG' )
58 ed.plugins.wpeditimage.showButtons(e.target);
59 } else if ( ! tinymce.isWebKit ) {
60 n = ed.selection.getNode();
62 if ( n.nodeName == 'IMG' && (DL = ed.dom.getParent(n, 'DL')) ) {
63 window.setTimeout(function(){
64 var ed = tinyMCE.activeEditor, n = ed.selection.getNode(), DL = ed.dom.getParent(n, 'DL');
66 if ( n.width != (parseInt(ed.dom.getStyle(DL, 'width')) - 10) ) {
67 ed.dom.setStyle(DL, 'width', parseInt(n.width)+10);
68 ed.execCommand('mceRepaint');
75 ed.onMouseDown.add(function(ed, e) {
76 if ( tinymce.isOpera || e.target.nodeName != 'IMG' ) {
80 ed.plugins.wpeditimage.showButtons(e.target);
83 ed.onKeyPress.add(function(ed, e) {
86 if ( e.keyCode == 13 && (DL = ed.dom.getParent(ed.selection.getNode(), 'DL')) ) {
87 P = ed.dom.create('p', {}, ' ');
88 if ( (DIV = DL.parentNode) && DIV.nodeName == 'DIV' )
89 ed.dom.insertAfter( P, DIV );
90 else ed.dom.insertAfter( P, DL );
92 tinymce.dom.Event.cancel(e);
93 ed.selection.select(P);
98 ed.onBeforeSetContent.add(function(ed, o) {
99 o.content = t._do_shcode(o.content);
102 ed.onPostProcess.add(function(ed, o) {
104 o.content = t._get_shcode(o.content);
108 _do_shcode : function(co) {
109 return co.replace(/\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\][\s\u00a0]*/g, function(a,b,c){
110 var id, cls, w, cap, div_cls;
112 b = b.replace(/\\'|\\'|\\'/g, ''').replace(/\\"|\\"/g, '"');
113 c = c.replace(/\\'|\\'/g, ''').replace(/\\"/g, '"');
114 id = b.match(/id=['"]([^'"]+)/i);
115 cls = b.match(/align=['"]([^'"]+)/i);
116 w = b.match(/width=['"]([0-9]+)/);
117 cap = b.match(/caption=['"]([^'"]+)/i);
119 id = ( id && id[1] ) ? id[1] : '';
120 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
121 w = ( w && w[1] ) ? w[1] : '';
122 cap = ( cap && cap[1] ) ? cap[1] : '';
123 if ( ! w || ! cap ) return c;
125 div_cls = (cls == 'aligncenter') ? 'mceTemp mceIEcenter' : 'mceTemp';
127 return '<div class="'+div_cls+'"><dl id="'+id+'" class="wp-caption '+cls+'" style="width: '+(10+parseInt(w))+
128 'px"><dt class="wp-caption-dt">'+c+'</dt><dd class="wp-caption-dd">'+cap+'</dd></dl></div>';
132 _get_shcode : function(co) {
133 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){
136 id = b.match(/id=['"]([^'"]+)/i);
137 cls = b.match(/class=['"]([^'"]+)/i);
138 w = c.match(/width=['"]([0-9]+)/);
140 id = ( id && id[1] ) ? id[1] : '';
141 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
142 w = ( w && w[1] ) ? w[1] : '';
144 if ( ! w || ! cap ) return c;
145 cls = cls.match(/align[^ '"]+/) || 'alignnone';
146 cap = cap.replace(/<\S[^<>]*>/gi, '').replace(/'/g, ''').replace(/"/g, '"');
148 return '[caption id="'+id+'" align="'+cls+'" width="'+w+'" caption="'+cap+'"]'+c+'[/caption]';
152 showButtons : function(n) {
153 var t = this, ed = tinyMCE.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y, cls = ed.dom.getAttrib(n, 'class');
155 if ( cls.indexOf('mceItem') != -1 || cls.indexOf('wpGallery') != -1 )
158 vp = ed.dom.getViewPort(ed.getWin());
159 p1 = DOM.getPos(ed.getContentAreaContainer());
160 p2 = ed.dom.getPos(n);
162 X = Math.max(p2.x - vp.x, 0) + p1.x;
163 Y = Math.max(p2.y - vp.y, 0) + p1.y;
165 DOM.setStyles('wp_editbtns', {
171 t.btnsTout = window.setTimeout( function(){ed.plugins.wpeditimage.hideButtons();}, 5000 );
174 hideButtons : function() {
175 if ( tinymce.DOM.isHidden('wp_editbtns') ) return;
177 tinymce.DOM.hide('wp_editbtns');
178 window.clearTimeout(this.btnsTout);
181 _createButtons : function() {
182 var t = this, ed = tinyMCE.activeEditor, DOM = tinymce.DOM, wp_editbtns, wp_editimgbtn, wp_delimgbtn;
184 DOM.remove('wp_editbtns');
186 wp_editbtns = DOM.add(document.body, 'div', {
188 style : 'display:none;'
191 wp_editimgbtn = DOM.add('wp_editbtns', 'img', {
192 src : t.url+'/img/image.png',
193 id : 'wp_editimgbtn',
196 title : ed.getLang('wpeditimage.edit_img')
199 wp_editimgbtn.onmousedown = function(e) {
200 var ed = tinyMCE.activeEditor;
201 ed.windowManager.bookmark = ed.selection.getBookmark('simple');
202 ed.execCommand("WP_EditImage");
203 this.parentNode.style.display = 'none';
206 wp_delimgbtn = DOM.add('wp_editbtns', 'img', {
207 src : t.url+'/img/delete.png',
211 title : ed.getLang('wpeditimage.del_img')
214 wp_delimgbtn.onmousedown = function(e) {
215 var ed = tinyMCE.activeEditor, el = ed.selection.getNode(), p;
217 if ( el.nodeName == 'IMG' && ed.dom.getAttrib(el, 'class').indexOf('mceItem') == -1 ) {
218 if ( (p = ed.dom.getParent(el, 'div')) && ed.dom.hasClass(p, 'mceTemp') )
220 else if ( (p = ed.dom.getParent(el, 'A')) && p.childNodes.length == 1 )
222 else ed.dom.remove(el);
224 this.parentNode.style.display = 'none';
225 ed.execCommand('mceRepaint');
231 getInfo : function() {
233 longname : 'Edit Image',
234 author : 'WordPress',
235 authorurl : 'http://wordpress.org',
242 tinymce.PluginManager.add('wpeditimage', tinymce.plugins.wpEditImage);