/* global tinymce */ tinymce.PluginManager.add( 'wpeditimage', function( editor ) { function parseShortcode( content ) { return content.replace( /(?:
)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function( a, b, c ) {
var id, cls, w, cap, img, width,
trim = tinymce.trim;
id = b.match( /id=['"]([^'"]*)['"] ?/ );
if ( id ) {
b = b.replace( id[0], '' );
}
cls = b.match( /align=['"]([^'"]*)['"] ?/ );
if ( cls ) {
b = b.replace( cls[0], '' );
}
w = b.match( /width=['"]([0-9]*)['"] ?/ );
if ( w ) {
b = b.replace( w[0], '' );
}
c = trim( c );
img = c.match( /((?:]+>)?]+>(?:<\/a>)?)([\s\S]*)/i );
if ( img && img[2] ) {
cap = trim( img[2] );
img = trim( img[1] );
} else {
// old captions shortcode style
cap = trim( b ).replace( /caption=['"]/, '' ).replace( /['"]$/, '' );
img = c;
}
id = ( id && id[1] ) ? id[1] : '';
cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
w = ( w && w[1] ) ? w[1] : '';
if ( ! w || ! cap ) {
return c;
}
width = parseInt( w, 10 ) + 10;
return ' ' + out[1] + ' with the caption text.
out = b.replace( /[\s\S]*?((?:]+>)?]+>(?:<\/a>)?)( [\s\S]*<\/p>)?[\s\S]*/gi, ' $1 /, '' ).replace( /<\/p>$/, '' );
}
}
// extract linkTo
if ( imageNode.parentNode.nodeName === 'A' ) {
metadata.linkUrl = editor.dom.getAttrib( imageNode.parentNode, 'href' );
}
return metadata;
}
function updateImage( imageNode, imageData ) {
var className, width, node, html, captionNode, nodeToReplace, uid;
if ( imageData.caption ) {
html = createImageAndLink( imageData, 'html' );
width = imageData.width + 10;
className = 'align' + imageData.align;
//TODO: shouldn't add the id attribute if it isn't an attachment
// should create a new function for genrating the caption markup
html = ' /, '' ).replace( /<\/p>$/, '' );
}
}
});
// Prevent dragging images out of the caption elements
dom.bind( editor.getDoc(), 'dragstart', function( event ) {
var node = editor.selection.getNode();
if ( node.nodeName === 'IMG' && dom.getParent( node, '.wp-caption' ) ) {
event.preventDefault();
}
});
});
editor.on( 'ObjectResized', function( event ) {
var parent,
node = event.target;
if ( node.nodeName === 'IMG' && ( parent = editor.dom.getParent( node, '.wp-caption' ) ) ) {
editor.dom.setStyle( parent, 'width', 10 + event.width + 'px' );
}
});
editor.on( 'BeforeExecCommand', function( e ) {
var node, p, DL, align,
cmd = e.command,
dom = editor.dom;
if ( cmd === 'mceInsertContent' ) {
// When inserting content, if the caret is inside a caption create new paragraph under
// and move the caret there
if ( node = dom.getParent( editor.selection.getNode(), 'div.mceTemp' ) ) {
p = dom.create( 'p' );
dom.insertAfter( p, node );
editor.selection.setCursorLocation( p, 0 );
editor.nodeChanged();
if ( tinymce.Env.ie > 8 ) {
setTimeout( function() {
editor.selection.setCursorLocation( p, 0 );
editor.selection.setContent( e.value );
}, 500 );
return false;
}
}
} else if ( cmd === 'JustifyLeft' || cmd === 'JustifyRight' || cmd === 'JustifyCenter' ) {
node = editor.selection.getNode();
align = cmd.substr(7).toLowerCase();
align = 'align' + align;
if ( dom.is( node, 'dl.wp-caption' ) ) {
DL = node;
} else {
DL = dom.getParent( node, 'dl.wp-caption' );
}
if ( DL ) {
// When inside an image caption, set the align* class on dl.wp-caption
if ( dom.hasClass( DL, align ) ) {
dom.removeClass( DL, align );
dom.addClass( DL, 'alignnone' );
} else {
DL.className = DL.className.replace( /align[^ ]+/g, '' );
dom.addClass( DL, align );
}
return false;
}
if ( node.nodeName === 'IMG' ) {
if ( dom.hasClass( node, align ) ) {
// The align class is being removed
dom.addClass( node, 'alignnone' );
} else {
dom.removeClass( node, 'alignnone' );
}
}
}
});
editor.on( 'keydown', function( e ) {
var node, wrap, P, spacer,
selection = editor.selection,
dom = editor.dom;
if ( e.keyCode === tinymce.util.VK.ENTER ) {
// When pressing Enter inside a caption move the caret to a new parapraph under it
wrap = dom.getParent( editor.selection.getNode(), 'div.mceTemp' );
if ( wrap ) {
dom.events.cancel(e); // Doesn't cancel all :(
// Remove any extra dt and dd cleated on pressing Enter...
tinymce.each( dom.select( 'dt, dd', wrap ), function( element ) {
if ( dom.isEmpty( element ) ) {
dom.remove( element );
}
});
spacer = tinymce.Env.ie ? '' : ']+)>\s*
cap = cap.replace( /\s*\n\s*/g, '
' );
return '[caption id="'+ id +'" align="'+ cls +'" width="'+ w +'"]'+ c +' '+ cap +'[/caption]';
});
if ( out.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
]*>/g, '$&\n' ).replace( /^
caption = caption.replace( /(
]*>)\s*\n\s*/g, '$1' ).replace( /\s*\n\s*/g, '
' );
}
if ( ! imgNode ) {
// New image inserted
html = dom.createHTML( 'img', data );
if ( caption ) {
node = editor.selection.getNode();
if ( data.width ) {
captionWidth = parseInt( data.width, 10 ) + 10;
captionWidth = ' style="width: '+ captionWidth +'px"';
}
html = ' ';
if ( node.nodeName === 'P' ) {
parent = node;
} else {
parent = dom.getParent( node, 'p' );
}
if ( parent && parent.nodeName === 'P' ) {
wrap = dom.create( 'div', { 'class': 'mceTemp', 'draggable': 'true' }, html );
dom.insertAfter( wrap, parent );
editor.selection.select( wrap );
editor.nodeChanged();
if ( dom.isEmpty( parent ) ) {
dom.remove( parent );
}
} else {
editor.selection.setContent( '' +
'
';
if ( parent = dom.getParent( imgNode, 'p' ) ) {
wrap = dom.create( 'div', { 'class': 'mceTemp', 'draggable': 'true' }, html );
dom.insertAfter( wrap, parent );
editor.selection.select( wrap );
editor.nodeChanged();
// Delete the old image node
dom.remove( node );
if ( dom.isEmpty( parent ) ) {
dom.remove( parent );
}
} else {
editor.selection.setContent( '
]*>/g, '$&\n' ).replace( /^
';
P = dom.create( 'p', null, spacer );
dom.insertAfter( P, wrap );
selection.setCursorLocation( P, 0 );
editor.nodeChanged();
}
} else if ( e.keyCode === tinymce.util.VK.DELETE || e.keyCode === tinymce.util.VK.BACKSPACE ) {
node = selection.getNode();
if ( node.nodeName === 'DIV' && dom.hasClass( node, 'mceTemp' ) ) {
wrap = node;
} else if ( node.nodeName === 'IMG' || node.nodeName === 'DT' || node.nodeName === 'A' ) {
wrap = dom.getParent( node, 'div.mceTemp' );
}
if ( wrap ) {
dom.events.cancel(e);
if ( wrap.nextSibling ) {
selection.select( wrap.nextSibling );
} else if ( wrap.previousSibling ) {
selection.select( wrap.previousSibling );
} else {
selection.select( wrap.parentNode );
}
selection.collapse( true );
editor.nodeChanged();
dom.remove( wrap );
wrap = null;
return false;
}
}
});
editor.on( 'mousedown', function( e ) {
var imageNode, frame, callback;
if ( e.target.nodeName === 'IMG' && editor.selection.getNode() === e.target ) {
// Don't trigger on right-click
if ( e.button !== 2 ) {
// Don't attempt to edit placeholders
if ( editor.dom.hasClass( e.target, 'mceItem' ) || '1' === editor.dom.getAttrib( e.target, 'data-mce-placeholder' ) ) {
return;
}
imageNode = e.target;
frame = wp.media({
frame: 'image',
state: 'image-details',
metadata: extractImageData( imageNode )
} );
callback = function( imageData ) {
updateImage( imageNode, imageData );
editor.focus();
};
frame.state('image-details').on( 'update', callback );
frame.state('replace-image').on( 'replace', callback );
frame.open();
}
}
} );
editor.wpSetImgCaption = function( content ) {
return parseShortcode( content );
};
editor.wpGetImgCaption = function( content ) {
return getShortcode( content );
};
editor.on( 'BeforeSetContent', function( e ) {
e.content = editor.wpSetImgCaption( e.content );
});
editor.on( 'PostProcess', function( e ) {
if ( e.get ) {
e.content = editor.wpGetImgCaption( e.content );
}
});
return {
_do_shcode: parseShortcode,
_get_shcode: getShortcode
};
});