3 tinymce.create('tinymce.plugins.wpEditImage', {
7 init: function(ed, url) {
8 var t = this, mouse = {};
14 // Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('...');
15 ed.addCommand('WP_EditImage', function() {
16 var el = ed.selection.getNode(), vp, H, W, cls = ed.dom.getAttrib(el, 'class');
18 if ( cls.indexOf('mceItem') != -1 || cls.indexOf('wpGallery') != -1 || el.nodeName != 'IMG' )
21 vp = tinymce.DOM.getViewPort();
22 H = 680 < (vp.h - 70) ? 680 : vp.h - 70;
23 W = 650 < vp.w ? 650 : vp.w;
25 ed.windowManager.open({
26 file: url + '/editimage.html',
33 ed.onInit.add(function(ed) {
34 ed.dom.events.add(ed.getBody(), 'dragstart', function(e) {
37 if ( e.target.nodeName == 'IMG' && ( parent = ed.dom.getParent(e.target, 'div.mceTemp') ) ) {
38 ed.selection.select(parent);
43 // resize the caption <dl> when the image is soft-resized by the user (only possible in Firefox and IE)
44 ed.onMouseUp.add(function(ed, e) {
45 if ( tinymce.isWebKit || tinymce.isOpera )
48 if ( mouse.x && (e.clientX != mouse.x || e.clientY != mouse.y) ) {
49 var n = ed.selection.getNode();
51 if ( 'IMG' == n.nodeName ) {
52 window.setTimeout(function(){
53 var DL = ed.dom.getParent(n, 'dl.wp-caption'), width;
55 if ( n.width != mouse.img_w || n.height != mouse.img_h )
56 n.className = n.className.replace(/size-[^ "']+/, '');
59 width = ed.dom.getAttrib(n, 'width') || n.width;
60 width = parseInt(width, 10);
61 ed.dom.setStyle(DL, 'width', 10 + width);
62 ed.execCommand('mceRepaint');
70 // show editimage buttons
71 ed.onMouseDown.add(function(ed, e) {
72 var target = e.target;
74 if ( target.nodeName != 'IMG' ) {
75 if ( target.firstChild && target.firstChild.nodeName == 'IMG' && target.childNodes.length == 1 )
76 target = target.firstChild;
81 if ( ed.dom.getAttrib(target, 'class').indexOf('mceItem') == -1 ) {
85 img_w: target.clientWidth,
86 img_h: target.clientHeight
89 ed.plugins.wordpress._showButtons(target, 'wp_editbtns');
93 // when pressing Return inside a caption move the caret to a new parapraph under it
94 ed.onKeyPress.add(function(ed, e) {
97 if ( e.keyCode == 13 ) {
98 n = ed.selection.getNode();
99 DL = ed.dom.getParent(n, 'dl.wp-caption');
102 DIV = ed.dom.getParent(DL, 'div.mceTemp');
105 P = ed.dom.create('p', {}, '<br>');
106 ed.dom.insertAfter( P, DIV );
107 ed.selection.select(P.firstChild);
109 if ( tinymce.isIE ) {
110 ed.selection.setContent('');
112 ed.selection.setContent('<br _moz_dirty="">');
113 ed.selection.setCursorLocation(P, 0);
116 ed.dom.events.cancel(e);
122 ed.onBeforeSetContent.add(function(ed, o) {
123 o.content = ed.wpSetImgCaption(o.content);
126 ed.onPostProcess.add(function(ed, o) {
128 o.content = ed.wpGetImgCaption(o.content);
131 ed.wpSetImgCaption = function(content) {
132 return t._do_shcode(content);
135 ed.wpGetImgCaption = function(content) {
136 return t._get_shcode(content);
140 _do_shcode : function(content) {
141 return content.replace(/(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function(a,b,c){
142 var id, cls, w, cap, div_cls, img, trim = tinymce.trim;
144 id = b.match(/id=['"]([^'"]*)['"] ?/);
146 b = b.replace(id[0], '');
148 cls = b.match(/align=['"]([^'"]*)['"] ?/);
150 b = b.replace(cls[0], '');
152 w = b.match(/width=['"]([0-9]*)['"] ?/);
154 b = b.replace(w[0], '');
157 img = c.match(/((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i);
159 if ( img && img[2] ) {
160 cap = trim( img[2] );
161 img = trim( img[1] );
163 // old captions shortcode style
164 cap = trim(b).replace(/caption=['"]/, '').replace(/['"]$/, '');
168 id = ( id && id[1] ) ? id[1] : '';
169 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
170 w = ( w && w[1] ) ? w[1] : '';
176 if ( cls == 'aligncenter' )
177 div_cls += ' mceIEcenter';
179 return '<div class="'+div_cls+'"><dl id="'+id+'" class="wp-caption '+cls+'" style="width: '+( 10 + parseInt(w) )+
180 'px"><dt class="wp-caption-dt">'+img+'</dt><dd class="wp-caption-dd">'+cap+'</dd></dl></div>';
184 _get_shcode : function(content) {
185 return content.replace(/<div (?:id="attachment_|class="mceTemp)[^>]*>([\s\S]+?)<\/div>/g, function(a, b){
186 var ret = b.replace(/<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>/gi, function(a,b,c,cap){
189 w = c.match(/width="([0-9]*)"/);
190 w = ( w && w[1] ) ? w[1] : '';
195 id = b.match(/id="([^"]*)"/);
196 id = ( id && id[1] ) ? id[1] : '';
198 cls = b.match(/class="([^"]*)"/);
199 cls = ( cls && cls[1] ) ? cls[1] : '';
200 cls = cls.match(/align[a-z]+/) || 'alignnone';
202 cap = cap.replace(/\r\n|\r/g, '\n').replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
203 // no line breaks inside HTML tags
204 return a.replace(/[\r\n\t]+/, ' ');
207 // convert remaining line breaks to <br>
208 cap = cap.replace(/\s*\n\s*/g, '<br />');
210 return '[caption id="'+id+'" align="'+cls+'" width="'+w+'"]'+c+' '+cap+'[/caption]';
213 if ( ret.indexOf('[caption') !== 0 ) {
214 // the caption html seems brocken, try to find the image that may be wrapped in a link
215 // and may be followed by <p> with the caption text.
216 ret = b.replace(/[\s\S]*?((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)(<p>[\s\S]*<\/p>)?[\s\S]*/gi, '<p>$1</p>$2');
223 _createButtons : function() {
224 var t = this, ed = tinyMCE.activeEditor, DOM = tinymce.DOM, editButton, dellButton;
226 DOM.remove('wp_editbtns');
228 DOM.add(document.body, 'div', {
230 style : 'display:none;'
233 editButton = DOM.add('wp_editbtns', 'img', {
234 src : t.url+'/img/image.png',
235 id : 'wp_editimgbtn',
238 title : ed.getLang('wpeditimage.edit_img')
241 tinymce.dom.Event.add(editButton, 'mousedown', function(e) {
242 var ed = tinyMCE.activeEditor;
243 ed.windowManager.bookmark = ed.selection.getBookmark('simple');
244 ed.execCommand("WP_EditImage");
247 dellButton = DOM.add('wp_editbtns', 'img', {
248 src : t.url+'/img/delete.png',
252 title : ed.getLang('wpeditimage.del_img')
255 tinymce.dom.Event.add(dellButton, 'mousedown', function(e) {
256 var ed = tinyMCE.activeEditor, el = ed.selection.getNode(), p;
258 if ( el.nodeName == 'IMG' && ed.dom.getAttrib(el, 'class').indexOf('mceItem') == -1 ) {
259 if ( (p = ed.dom.getParent(el, 'div')) && ed.dom.hasClass(p, 'mceTemp') )
261 else if ( (p = ed.dom.getParent(el, 'A')) && p.childNodes.length == 1 )
266 ed.execCommand('mceRepaint');
272 getInfo : function() {
274 longname : 'Edit Image',
275 author : 'WordPress',
276 authorurl : 'http://wordpress.org',
283 tinymce.PluginManager.add('wpeditimage', tinymce.plugins.wpEditImage);