2009-01-09 13:02:59 +01:00
|
|
|
|
|
|
|
(function() {
|
|
|
|
tinymce.create('tinymce.plugins.wpEditImage', {
|
2012-02-24 02:58:18 +01:00
|
|
|
url: '',
|
|
|
|
editor: {},
|
2009-01-09 13:02:59 +01:00
|
|
|
|
2012-02-24 02:58:18 +01:00
|
|
|
init: function(ed, url) {
|
2011-09-18 05:00:13 +02:00
|
|
|
var t = this, mouse = {};
|
2009-01-09 13:02:59 +01:00
|
|
|
|
|
|
|
t.url = url;
|
2012-02-24 02:58:18 +01:00
|
|
|
t.editor = ed;
|
2009-01-09 13:02:59 +01:00
|
|
|
t._createButtons();
|
|
|
|
|
|
|
|
// Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('...');
|
|
|
|
ed.addCommand('WP_EditImage', function() {
|
2012-02-22 04:05:25 +01:00
|
|
|
var el = ed.selection.getNode(), vp, H, W, cls = ed.dom.getAttrib(el, 'class');
|
2009-01-09 13:02:59 +01:00
|
|
|
|
|
|
|
if ( cls.indexOf('mceItem') != -1 || cls.indexOf('wpGallery') != -1 || el.nodeName != 'IMG' )
|
|
|
|
return;
|
|
|
|
|
2012-02-22 04:05:25 +01:00
|
|
|
vp = tinymce.DOM.getViewPort();
|
|
|
|
H = 680 < (vp.h - 70) ? 680 : vp.h - 70;
|
|
|
|
W = 650 < vp.w ? 650 : vp.w;
|
2009-01-09 13:02:59 +01:00
|
|
|
|
2012-02-22 04:05:25 +01:00
|
|
|
ed.windowManager.open({
|
|
|
|
file: url + '/editimage.html',
|
|
|
|
width: W+'px',
|
|
|
|
height: H+'px',
|
|
|
|
inline: true
|
2009-01-09 13:02:59 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
ed.onInit.add(function(ed) {
|
2012-02-24 02:58:18 +01:00
|
|
|
ed.dom.events.add(ed.getBody(), 'dragstart', function(e) {
|
|
|
|
var parent;
|
|
|
|
|
|
|
|
if ( e.target.nodeName == 'IMG' && ( parent = ed.dom.getParent(e.target, 'div.mceTemp') ) ) {
|
|
|
|
ed.selection.select(parent);
|
|
|
|
}
|
2009-10-22 07:30:29 +02:00
|
|
|
});
|
2009-01-09 13:02:59 +01:00
|
|
|
});
|
|
|
|
|
2010-10-29 22:06:00 +02:00
|
|
|
// resize the caption <dl> when the image is soft-resized by the user (only possible in Firefox and IE)
|
2009-01-09 13:02:59 +01:00
|
|
|
ed.onMouseUp.add(function(ed, e) {
|
2009-10-22 07:30:29 +02:00
|
|
|
if ( tinymce.isWebKit || tinymce.isOpera )
|
|
|
|
return;
|
|
|
|
|
2011-09-18 05:00:13 +02:00
|
|
|
if ( mouse.x && (e.clientX != mouse.x || e.clientY != mouse.y) ) {
|
|
|
|
var n = ed.selection.getNode();
|
2009-10-22 07:30:29 +02:00
|
|
|
|
2011-09-18 05:00:13 +02:00
|
|
|
if ( 'IMG' == n.nodeName ) {
|
|
|
|
window.setTimeout(function(){
|
2012-02-24 02:58:18 +01:00
|
|
|
var DL = ed.dom.getParent(n, 'dl.wp-caption'), width;
|
2011-09-18 05:00:13 +02:00
|
|
|
|
|
|
|
if ( n.width != mouse.img_w || n.height != mouse.img_h )
|
|
|
|
n.className = n.className.replace(/size-[^ "']+/, '');
|
|
|
|
|
2012-02-24 02:58:18 +01:00
|
|
|
if ( DL ) {
|
|
|
|
width = ed.dom.getAttrib(n, 'width') || n.width;
|
|
|
|
width = parseInt(width, 10);
|
|
|
|
ed.dom.setStyle(DL, 'width', 10 + width);
|
|
|
|
ed.execCommand('mceRepaint');
|
2010-10-29 22:06:00 +02:00
|
|
|
}
|
2011-09-18 05:00:13 +02:00
|
|
|
}, 100);
|
|
|
|
}
|
2009-01-09 13:02:59 +01:00
|
|
|
}
|
2011-09-18 05:00:13 +02:00
|
|
|
mouse = {};
|
2009-01-09 13:02:59 +01:00
|
|
|
});
|
|
|
|
|
2010-10-29 22:06:00 +02:00
|
|
|
// show editimage buttons
|
2009-01-09 13:02:59 +01:00
|
|
|
ed.onMouseDown.add(function(ed, e) {
|
2012-02-24 20:18:59 +01:00
|
|
|
var target = e.target;
|
2009-10-22 07:30:29 +02:00
|
|
|
|
2012-02-24 20:18:59 +01:00
|
|
|
if ( target.nodeName != 'IMG' ) {
|
|
|
|
if ( target.firstChild && target.firstChild.nodeName == 'IMG' && target.childNodes.length == 1 )
|
|
|
|
target = target.firstChild;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ed.dom.getAttrib(target, 'class').indexOf('mceItem') == -1 ) {
|
2011-09-18 05:00:13 +02:00
|
|
|
mouse = {
|
|
|
|
x: e.clientX,
|
|
|
|
y: e.clientY,
|
2012-02-24 20:18:59 +01:00
|
|
|
img_w: target.clientWidth,
|
|
|
|
img_h: target.clientHeight
|
2011-09-18 05:00:13 +02:00
|
|
|
};
|
|
|
|
|
2012-02-24 20:18:59 +01:00
|
|
|
ed.plugins.wordpress._showButtons(target, 'wp_editbtns');
|
2009-01-09 13:02:59 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-02-24 02:58:18 +01:00
|
|
|
// when pressing Return inside a caption move the caret to a new parapraph under it
|
2009-01-09 13:02:59 +01:00
|
|
|
ed.onKeyPress.add(function(ed, e) {
|
2010-10-29 22:06:00 +02:00
|
|
|
var n, DL, DIV, P;
|
2009-10-24 13:43:42 +02:00
|
|
|
|
2010-10-29 22:06:00 +02:00
|
|
|
if ( e.keyCode == 13 ) {
|
|
|
|
n = ed.selection.getNode();
|
|
|
|
DL = ed.dom.getParent(n, 'dl.wp-caption');
|
2009-01-09 13:02:59 +01:00
|
|
|
|
2012-02-24 02:58:18 +01:00
|
|
|
if ( DL )
|
|
|
|
DIV = ed.dom.getParent(DL, 'div.mceTemp');
|
|
|
|
|
|
|
|
if ( DIV ) {
|
|
|
|
P = ed.dom.create('p', {}, '<br>');
|
2010-10-29 22:06:00 +02:00
|
|
|
ed.dom.insertAfter( P, DIV );
|
2012-02-24 02:58:18 +01:00
|
|
|
ed.selection.select(P.firstChild);
|
|
|
|
|
|
|
|
if ( tinymce.isIE ) {
|
|
|
|
ed.selection.setContent('');
|
|
|
|
} else {
|
|
|
|
ed.selection.setContent('<br _moz_dirty="">');
|
|
|
|
ed.selection.setCursorLocation(P, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ed.dom.events.cancel(e);
|
2010-10-29 22:06:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2009-01-09 13:02:59 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ed.onBeforeSetContent.add(function(ed, o) {
|
2012-02-24 02:58:18 +01:00
|
|
|
o.content = ed.wpSetImgCaption(o.content);
|
2009-01-09 13:02:59 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
ed.onPostProcess.add(function(ed, o) {
|
|
|
|
if (o.get)
|
2012-02-24 02:58:18 +01:00
|
|
|
o.content = ed.wpGetImgCaption(o.content);
|
2009-01-09 13:02:59 +01:00
|
|
|
});
|
2012-02-24 02:58:18 +01:00
|
|
|
|
|
|
|
ed.wpSetImgCaption = function(content) {
|
|
|
|
return t._do_shcode(content);
|
|
|
|
};
|
|
|
|
|
|
|
|
ed.wpGetImgCaption = function(content) {
|
|
|
|
return t._get_shcode(content);
|
|
|
|
};
|
2009-01-09 13:02:59 +01:00
|
|
|
},
|
|
|
|
|
2012-02-24 02:58:18 +01:00
|
|
|
_do_shcode : function(content) {
|
|
|
|
return content.replace(/(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function(a,b,c){
|
2009-01-09 13:02:59 +01:00
|
|
|
var id, cls, w, cap, div_cls;
|
2012-02-24 02:58:18 +01:00
|
|
|
|
2012-02-28 09:03:40 +01:00
|
|
|
id = b.match(/id=['"]([^'"]*)['"] ?/);
|
2012-02-24 02:58:18 +01:00
|
|
|
b = b.replace(id[0], '');
|
|
|
|
|
2012-02-28 09:03:40 +01:00
|
|
|
cls = b.match(/align=['"]([^'"]*)['"] ?/);
|
2012-02-24 02:58:18 +01:00
|
|
|
b = b.replace(cls[0], '');
|
|
|
|
|
2012-02-28 09:03:40 +01:00
|
|
|
w = b.match(/width=['"]([0-9]*)['"] ?/);
|
2012-02-24 02:58:18 +01:00
|
|
|
b = b.replace(w[0], '');
|
|
|
|
|
|
|
|
cap = tinymce.trim(b).replace(/caption=['"]/, '').replace(/['"]$/, '');
|
2009-01-09 13:02:59 +01:00
|
|
|
|
|
|
|
id = ( id && id[1] ) ? id[1] : '';
|
|
|
|
cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
|
|
|
|
w = ( w && w[1] ) ? w[1] : '';
|
2012-02-24 02:58:18 +01:00
|
|
|
|
|
|
|
if ( !w || !cap )
|
|
|
|
return c;
|
|
|
|
|
2012-02-28 09:03:40 +01:00
|
|
|
div_cls = 'mceTemp';
|
|
|
|
if ( cls == 'aligncenter' )
|
|
|
|
div_cls += ' mceIEcenter';
|
2009-01-09 13:02:59 +01:00
|
|
|
|
2012-02-28 09:03:40 +01:00
|
|
|
return '<div class="'+div_cls+'"><dl id="'+id+'" class="wp-caption '+cls+'" style="width: '+( 10 + parseInt(w) )+
|
2009-01-09 13:02:59 +01:00
|
|
|
'px"><dt class="wp-caption-dt">'+c+'</dt><dd class="wp-caption-dd">'+cap+'</dd></dl></div>';
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-02-24 02:58:18 +01:00
|
|
|
_get_shcode : function(content) {
|
2012-02-28 09:03:40 +01:00
|
|
|
return content.replace(/<div (?:id="attachment_|class="mceTemp)[^>]*>([\s\S]+?)<\/div>/g, function(a, b){
|
|
|
|
var ret = b.replace(/<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>/gi, function(a,b,c,cap){
|
|
|
|
var id, cls, w;
|
2009-01-09 13:02:59 +01:00
|
|
|
|
2012-02-28 09:03:40 +01:00
|
|
|
w = c.match(/width="([0-9]*)"/);
|
|
|
|
w = ( w && w[1] ) ? w[1] : '';
|
2009-01-09 13:02:59 +01:00
|
|
|
|
2012-02-28 09:03:40 +01:00
|
|
|
if ( !w || !cap )
|
|
|
|
return c;
|
2012-02-24 02:58:18 +01:00
|
|
|
|
2012-02-28 09:03:40 +01:00
|
|
|
id = b.match(/id="([^"]*)"/);
|
|
|
|
id = ( id && id[1] ) ? id[1] : '';
|
2012-02-24 02:58:18 +01:00
|
|
|
|
2012-02-28 09:03:40 +01:00
|
|
|
cls = b.match(/class="([^"]*)"/);
|
|
|
|
cls = ( cls && cls[1] ) ? cls[1] : '';
|
|
|
|
cls = cls.match(/align[a-z]+/) || 'alignnone';
|
2012-02-24 02:58:18 +01:00
|
|
|
|
2012-03-05 08:31:31 +01:00
|
|
|
cap = cap.replace(/<[a-zA-Z0-9]+ [^<>]+>/g, function(a){ // look only for html tags with attributes
|
2012-02-28 09:03:40 +01:00
|
|
|
a = a.replace(/="[^"]+"/, function(b){
|
|
|
|
return b.replace(/'/g, ''');
|
|
|
|
});
|
|
|
|
return a.replace(/"/g, "'");
|
|
|
|
});
|
|
|
|
|
|
|
|
cap = cap.replace(/"/g, '"');
|
|
|
|
|
|
|
|
return '[caption id="'+id+'" align="'+cls+'" width="'+w+'" caption="'+cap+'"]'+c+'[/caption]';
|
2012-02-24 02:58:18 +01:00
|
|
|
});
|
|
|
|
|
2012-02-28 09:03:40 +01:00
|
|
|
if ( ret.indexOf('[caption') !== 0 ) {
|
|
|
|
// the caption html seems brocken, try to find the image that may be wrapped in a link
|
|
|
|
// and may be followed by <p> with the caption text.
|
|
|
|
ret = b.replace(/[\s\S]*?((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)(<p>[\s\S]*<\/p>)?[\s\S]*/gi, '<p>$1</p>$2');
|
|
|
|
}
|
2009-01-09 13:02:59 +01:00
|
|
|
|
2012-02-28 09:03:40 +01:00
|
|
|
return ret;
|
2009-01-09 13:02:59 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_createButtons : function() {
|
2009-10-24 13:43:42 +02:00
|
|
|
var t = this, ed = tinyMCE.activeEditor, DOM = tinymce.DOM, editButton, dellButton;
|
2009-01-09 13:02:59 +01:00
|
|
|
|
|
|
|
DOM.remove('wp_editbtns');
|
|
|
|
|
2009-10-24 13:43:42 +02:00
|
|
|
DOM.add(document.body, 'div', {
|
2009-01-09 13:02:59 +01:00
|
|
|
id : 'wp_editbtns',
|
|
|
|
style : 'display:none;'
|
|
|
|
});
|
|
|
|
|
2009-10-24 13:43:42 +02:00
|
|
|
editButton = DOM.add('wp_editbtns', 'img', {
|
2009-01-09 13:02:59 +01:00
|
|
|
src : t.url+'/img/image.png',
|
|
|
|
id : 'wp_editimgbtn',
|
|
|
|
width : '24',
|
|
|
|
height : '24',
|
|
|
|
title : ed.getLang('wpeditimage.edit_img')
|
|
|
|
});
|
|
|
|
|
2009-10-24 13:43:42 +02:00
|
|
|
tinymce.dom.Event.add(editButton, 'mousedown', function(e) {
|
2009-10-27 01:23:58 +01:00
|
|
|
var ed = tinyMCE.activeEditor;
|
2009-01-09 13:02:59 +01:00
|
|
|
ed.windowManager.bookmark = ed.selection.getBookmark('simple');
|
|
|
|
ed.execCommand("WP_EditImage");
|
2009-10-22 07:30:29 +02:00
|
|
|
});
|
2009-01-09 13:02:59 +01:00
|
|
|
|
2009-10-24 13:43:42 +02:00
|
|
|
dellButton = DOM.add('wp_editbtns', 'img', {
|
2009-01-09 13:02:59 +01:00
|
|
|
src : t.url+'/img/delete.png',
|
|
|
|
id : 'wp_delimgbtn',
|
|
|
|
width : '24',
|
|
|
|
height : '24',
|
|
|
|
title : ed.getLang('wpeditimage.del_img')
|
|
|
|
});
|
|
|
|
|
2009-10-24 13:43:42 +02:00
|
|
|
tinymce.dom.Event.add(dellButton, 'mousedown', function(e) {
|
2009-01-09 13:02:59 +01:00
|
|
|
var ed = tinyMCE.activeEditor, el = ed.selection.getNode(), p;
|
|
|
|
|
|
|
|
if ( el.nodeName == 'IMG' && ed.dom.getAttrib(el, 'class').indexOf('mceItem') == -1 ) {
|
|
|
|
if ( (p = ed.dom.getParent(el, 'div')) && ed.dom.hasClass(p, 'mceTemp') )
|
|
|
|
ed.dom.remove(p);
|
|
|
|
else if ( (p = ed.dom.getParent(el, 'A')) && p.childNodes.length == 1 )
|
|
|
|
ed.dom.remove(p);
|
2009-10-22 07:30:29 +02:00
|
|
|
else
|
|
|
|
ed.dom.remove(el);
|
2009-01-09 13:02:59 +01:00
|
|
|
|
|
|
|
ed.execCommand('mceRepaint');
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-22 07:30:29 +02:00
|
|
|
});
|
2009-01-09 13:02:59 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getInfo : function() {
|
|
|
|
return {
|
|
|
|
longname : 'Edit Image',
|
|
|
|
author : 'WordPress',
|
|
|
|
authorurl : 'http://wordpress.org',
|
|
|
|
infourl : '',
|
|
|
|
version : "1.0"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
tinymce.PluginManager.add('wpeditimage', tinymce.plugins.wpEditImage);
|
|
|
|
})();
|