Edit image modal:

- Make the calculation of the aspect ratio more robust.
- Better getting of the image height and width.
Props gcorne, see #27366
Built from https://develop.svn.wordpress.org/trunk@27942


git-svn-id: http://core.svn.wordpress.org/trunk@27772 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2014-04-04 01:49:15 +00:00
parent 2d0ce517e8
commit a3650b68cd
7 changed files with 52 additions and 25 deletions

View File

@ -369,13 +369,14 @@ window.wp = window.wp || {};
this.on( 'change:size', this.updateSize, this ); this.on( 'change:size', this.updateSize, this );
this.setLinkTypeFromUrl(); this.setLinkTypeFromUrl();
this.setAspectRatio();
this.set( 'aspectRatio', attributes.customWidth / attributes.customHeight );
this.set( 'originalUrl', attributes.url ); this.set( 'originalUrl', attributes.url );
}, },
bindAttachmentListeners: function() { bindAttachmentListeners: function() {
this.listenTo( this.attachment, 'sync', this.setLinkTypeFromUrl ); this.listenTo( this.attachment, 'sync', this.setLinkTypeFromUrl );
this.listenTo( this.attachment, 'sync', this.setAspectRatio );
this.listenTo( this.attachment, 'change', this.updateSize ); this.listenTo( this.attachment, 'change', this.updateSize );
}, },
@ -466,6 +467,21 @@ window.wp = window.wp || {};
this.set( 'url', size.url ); this.set( 'url', size.url );
this.set( 'width', size.width ); this.set( 'width', size.width );
this.set( 'height', size.height ); this.set( 'height', size.height );
},
setAspectRatio: function() {
var full;
if ( this.attachment ) {
full = this.attachment.get( 'sizes' ).full;
if ( full ) {
this.set( 'aspectRatio', full.width / full.height );
return;
}
}
this.set( 'aspectRatio', this.get( 'customWidth' ) / this.get( 'customHeight' ) );
} }
}); });

File diff suppressed because one or more lines are too long

View File

@ -6060,10 +6060,10 @@
'click .edit-attachment': 'editAttachment', 'click .edit-attachment': 'editAttachment',
'click .replace-attachment': 'replaceAttachment', 'click .replace-attachment': 'replaceAttachment',
'click .advanced-toggle': 'toggleAdvanced', 'click .advanced-toggle': 'toggleAdvanced',
'change [data-setting="customWidth"]': 'syncCustomSize', 'change [data-setting="customWidth"]': 'onCustomSize',
'change [data-setting="customHeight"]': 'syncCustomSize', 'change [data-setting="customHeight"]': 'onCustomSize',
'keyup [data-setting="customWidth"]': 'syncCustomSize', 'keyup [data-setting="customWidth"]': 'onCustomSize',
'keyup [data-setting="customHeight"]': 'syncCustomSize' 'keyup [data-setting="customHeight"]': 'onCustomSize'
} ), } ),
initialize: function() { initialize: function() {
// used in AttachmentDisplay.prototype.updateLinkTo // used in AttachmentDisplay.prototype.updateLinkTo
@ -6136,18 +6136,26 @@
} }
}, },
syncCustomSize: function( event ) { onCustomSize: function( event ) {
var dimension = $( event.target ).data('setting'), var dimension = $( event.target ).data('setting'),
num = $( event.target ).val(),
value; value;
// Ignore bogus input
if ( ! /^\d+/.test( num ) || parseInt( num, 10 ) < 1 ) {
event.preventDefault();
return;
}
if ( dimension === 'customWidth' ) { if ( dimension === 'customWidth' ) {
value = Math.round( 1 / this.model.get( 'aspectRatio' ) * $( event.target ).val() ); value = Math.round( 1 / this.model.get( 'aspectRatio' ) * num );
this.model.set( 'customHeight', value, { silent: true } ); this.model.set( 'customHeight', value, { silent: true } );
this.$( '[data-setting="customHeight"]' ).val( value ); this.$( '[data-setting="customHeight"]' ).val( value );
} else { } else {
value = Math.round( this.model.get( 'aspectRatio' ) * $( event.target ).val() ); value = Math.round( this.model.get( 'aspectRatio' ) * num );
this.model.set( 'customWidth', value, { silent: true } );
this.$( '[data-setting="customWidth"]' ).val( value ); this.$( '[data-setting="customWidth"]' ).val( value );
this.model.set( 'customWidth', value, { silent: true } );
} }
}, },

File diff suppressed because one or more lines are too long

View File

@ -115,19 +115,14 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
function extractImageData( imageNode ) { function extractImageData( imageNode ) {
var classes, extraClasses, metadata, captionBlock, caption, link, width, height, var classes, extraClasses, metadata, captionBlock, caption, link, width, height,
dom = editor.dom; dom = editor.dom,
isIntRegExp = /^\d+$/;
// default attributes // default attributes
metadata = { metadata = {
attachment_id: false, attachment_id: false,
url: false,
height: '',
width: '',
customWidth: '',
customHeight: '',
size: 'custom', size: 'custom',
caption: '', caption: '',
alt: '',
align: 'none', align: 'none',
extraClasses: '', extraClasses: '',
link: false, link: false,
@ -141,12 +136,20 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
metadata.url = dom.getAttrib( imageNode, 'src' ); metadata.url = dom.getAttrib( imageNode, 'src' );
metadata.alt = dom.getAttrib( imageNode, 'alt' ); metadata.alt = dom.getAttrib( imageNode, 'alt' );
metadata.title = dom.getAttrib( imageNode, 'title' ); metadata.title = dom.getAttrib( imageNode, 'title' );
width = dom.getAttrib( imageNode, 'width' ) || imageNode.width;
height = dom.getAttrib( imageNode, 'height' ) || imageNode.height; width = dom.getAttrib( imageNode, 'width' );
metadata.width = parseInt( width, 10 ); height = dom.getAttrib( imageNode, 'height' );
metadata.height = parseInt( height, 10 );
metadata.customWidth = metadata.width; if ( ! isIntRegExp.test( width ) || parseInt( width, 10 ) < 1 ) {
metadata.customHeight = metadata.height; width = imageNode.naturalWidth || imageNode.width;
}
if ( ! isIntRegExp.test( height ) || parseInt( height, 10 ) < 1 ) {
height = imageNode.naturalHeight || imageNode.height;
}
metadata.customWidth = metadata.width = width;
metadata.customHeight = metadata.height = height;
classes = tinymce.explode( imageNode.className, ' ' ); classes = tinymce.explode( imageNode.className, ' ' );
extraClasses = []; extraClasses = [];

File diff suppressed because one or more lines are too long