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();
15 if ( ed.dom.getAttrib(el, 'class').indexOf('mceItem') != -1 || el.nodeName != 'IMG' )
18 tb_show('', url + '/editimage.html?ver=311g&TB_iframe=true');
19 tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
22 ed.onInit.add(function(ed) {
23 tinymce.dom.Event.add(ed.getWin(), 'scroll', function(e) {
24 ed.plugins.wpeditimage.hideButtons();
28 ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
29 ed.plugins.wpeditimage.hideButtons();
32 ed.onSaveContent.add(function(ed, o) {
33 ed.plugins.wpeditimage.hideButtons();
36 ed.onMouseUp.add(function(ed, e) {
37 if ( tinymce.isOpera ) {
38 if ( e.target.nodeName == 'IMG' )
39 ed.plugins.wpeditimage.showButtons(e.target);
40 } else if ( ! tinymce.isWebKit ) {
41 var n = ed.selection.getNode(), DL;
43 if ( n.nodeName == 'IMG' && (DL = ed.dom.getParent(n, 'DL')) ) {
44 window.setTimeout(function(){
45 var ed = tinyMCE.activeEditor, n = ed.selection.getNode(), DL = ed.dom.getParent(n, 'DL');
47 if ( n.width != (parseInt(ed.dom.getStyle(DL, 'width')) - 10) ) {
48 ed.dom.setStyle(DL, 'width', parseInt(n.width)+10);
49 ed.execCommand('mceRepaint');
56 ed.onMouseDown.add(function(ed, e) {
57 if ( tinymce.isOpera || e.target.nodeName != 'IMG' ) {
61 ed.plugins.wpeditimage.showButtons(e.target);
64 ed.onKeyPress.add(function(ed, e) {
67 if ( e.keyCode == 13 && (DL = ed.dom.getParent(ed.selection.getNode(), 'DL')) ) {
68 var P = ed.dom.create('p', {}, ' ');
69 if ( (DIV = DL.parentNode) && DIV.nodeName == 'DIV' )
70 ed.dom.insertAfter( P, DIV );
71 else ed.dom.insertAfter( P, DL );
73 tinymce.dom.Event.cancel(e);
74 ed.selection.select(P);
79 ed.onBeforeSetContent.add(function(ed, o) {
80 o.content = t._do_shcode(o.content);
83 ed.onPostProcess.add(function(ed, o) {
85 o.content = t._get_shcode(o.content);
89 _do_shcode : function(co) {
90 return co.replace(/\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\][\s\u00a0]*/g, function(a,b,c){
91 b = b.replace(/\\'|\\'|\\'/g, ''').replace(/\\"|\\"/g, '"');
92 c = c.replace(/\\'|\\'/g, ''').replace(/\\"/g, '"');
93 var id = b.match(/id=['"]([^'"]+)/i), cls = b.match(/align=['"]([^'"]+)/i);
94 var w = b.match(/width=['"]([0-9]+)/), cap = b.match(/caption=['"]([^'"]+)/i);
96 id = ( id && id[1] ) ? id[1] : '';
97 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
98 w = ( w && w[1] ) ? w[1] : '';
99 cap = ( cap && cap[1] ) ? cap[1] : '';
100 if ( ! w || ! cap ) return c;
102 var div_cls = (cls == 'aligncenter') ? 'mceTemp mceIEcenter' : 'mceTemp';
104 return '<div class="'+div_cls+'"><dl id="'+id+'" class="wp-caption '+cls+'" style="width: '+(10+parseInt(w))+
105 'px"><dt class="wp-caption-dt">'+c+'</dt><dd class="wp-caption-dd">'+cap+'</dd></dl></div>';
109 _get_shcode : function(co) {
110 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){
111 var id = b.match(/id=['"]([^'"]+)/i), cls = b.match(/class=['"]([^'"]+)/i);
112 var w = c.match(/width=['"]([0-9]+)/);
114 id = ( id && id[1] ) ? id[1] : '';
115 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
116 w = ( w && w[1] ) ? w[1] : '';
118 if ( ! w || ! cap ) return c;
119 cls = cls.match(/align[^ '"]+/) || 'alignnone';
120 cap = cap.replace(/<\S[^<>]*>/gi, '').replace(/'/g, ''').replace(/"/g, '"');
122 return '[caption id="'+id+'" align="'+cls+'" width="'+w+'" caption="'+cap+'"]'+c+'[/caption]';
126 showButtons : function(n) {
127 var t = this, ed = tinyMCE.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y;
129 if (ed.dom.getAttrib(n, 'class').indexOf('mceItem') != -1)
132 vp = ed.dom.getViewPort(ed.getWin());
133 p1 = DOM.getPos(ed.getContentAreaContainer());
134 p2 = ed.dom.getPos(n);
136 X = Math.max(p2.x - vp.x, 0) + p1.x;
137 Y = Math.max(p2.y - vp.y, 0) + p1.y;
139 DOM.setStyles('wp_editbtns', {
145 t.btnsTout = window.setTimeout( function(){ed.plugins.wpeditimage.hideButtons();}, 5000 );
148 hideButtons : function() {
149 if ( tinymce.DOM.isHidden('wp_editbtns') ) return;
151 tinymce.DOM.hide('wp_editbtns');
152 window.clearTimeout(this.btnsTout);
155 _createButtons : function() {
156 var t = this, ed = tinyMCE.activeEditor, DOM = tinymce.DOM;
158 DOM.remove('wp_editbtns');
160 var wp_editbtns = DOM.add(document.body, 'div', {
162 style : 'display:none;'
165 var wp_editimgbtn = DOM.add('wp_editbtns', 'img', {
166 src : t.url+'/img/image.png',
167 id : 'wp_editimgbtn',
170 title : ed.getLang('wpeditimage.edit_img')
173 wp_editimgbtn.onmousedown = function(e) {
174 var ed = tinyMCE.activeEditor;
175 ed.windowManager.bookmark = ed.selection.getBookmark('simple');
176 ed.execCommand("WP_EditImage");
177 this.parentNode.style.display = 'none';
180 var wp_delimgbtn = DOM.add('wp_editbtns', 'img', {
181 src : t.url+'/img/delete.png',
185 title : ed.getLang('wpeditimage.del_img')
188 wp_delimgbtn.onmousedown = function(e) {
189 var ed = tinyMCE.activeEditor, el = ed.selection.getNode(), p;
191 if ( el.nodeName == 'IMG' && ed.dom.getAttrib(el, 'class').indexOf('mceItem') == -1 ) {
192 if ( (p = ed.dom.getParent(el, 'div')) && ed.dom.hasClass(p, 'mceTemp') )
194 else if ( (p = ed.dom.getParent(el, 'A')) && p.childNodes.length == 1 )
196 else ed.dom.remove(el);
198 this.parentNode.style.display = 'none';
199 ed.execCommand('mceRepaint');
205 getInfo : function() {
207 longname : 'Edit Image',
208 author : 'WordPress',
209 authorurl : 'http://wordpress.org',
216 tinymce.PluginManager.add('wpeditimage', tinymce.plugins.wpEditImage);