Skip to content

Commit d792be6

Browse files
committed
Merge branch 't/10833' into major
2 parents ea3a4dc + 2d17d5e commit d792be6

File tree

3 files changed

+87
-50
lines changed

3 files changed

+87
-50
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Fixed Issues:
4141
* [#10823](http://dev.ckeditor.com/ticket/10823): Fixed: Link plugin does not work with non-editable content.
4242
* [#10828](http://dev.ckeditor.com/ticket/10828): Magicline integration with widgets system.
4343
* [#10865](http://dev.ckeditor.com/ticket/10865): Improved hiding copybin, so copying widgets works smoothly.
44+
* [#10833](http://dev.ckeditor.com/ticket/10833): Fixed: *Lock ratio* option should be on by default in Image2 dialog.
4445

4546
## CKEditor 4.3 Beta
4647

plugins/image2/dialogs/image2.js

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
3636
resetButtonId: resetButtonId
3737
} ),
3838

39+
helpers = CKEDITOR.plugins.image2,
40+
41+
// Functions inherited from image2 plugin.
42+
checkHasNaturalRatio = helpers.checkHasNaturalRatio,
43+
getNatural = helpers.getNatural,
44+
3945
// Global variables referring to the dialog's context.
4046
doc, widget, image,
4147

@@ -52,7 +58,9 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
5258
lockRatio, userDefinedLock,
5359

5460
// Global variables referring to dialog fields and elements.
55-
lockButton, resetButton, widthField, heightField;
61+
lockButton, resetButton, widthField, heightField,
62+
63+
natural;
5664

5765
// Validates dimension. Allowed values are:
5866
// "123px", "123", "" (empty string)
@@ -124,7 +132,7 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
124132

125133
// There was problem loading the image. Unlock ratio.
126134
if ( !image )
127-
return toggleLockDimensions( false );
135+
return toggleLockRatio( false );
128136

129137
// Fill width field with the width of the new image.
130138
widthField.setValue( width );
@@ -139,7 +147,7 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
139147
preLoadedHeight = height;
140148

141149
// Check for new lock value if image exist.
142-
toggleLockDimensions( 'check' );
150+
toggleLockRatio( helpers.checkHasNaturalRatio( image ) );
143151
} );
144152

145153
srcChanged = true;
@@ -183,7 +191,7 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
183191

184192
// If the value of the field is invalid (e.g. with %), unlock ratio.
185193
if ( !value.match( regexGetSizeOrEmpty ) )
186-
toggleLockDimensions( false );
194+
toggleLockRatio( false );
187195

188196
// No automatic re-scale when dimension is '0'.
189197
if ( value === '0' )
@@ -235,7 +243,7 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
235243
dialog.addFocusable( lockButton, 4 );
236244

237245
lockButton.on( 'click', function( evt ) {
238-
toggleLockDimensions();
246+
toggleLockRatio();
239247
evt.data && evt.data.preventDefault();
240248
}, this.getDialog() );
241249

@@ -270,46 +278,28 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
270278
}
271279
}
272280

273-
function toggleLockDimensions( enable ) {
281+
function toggleLockRatio( enable ) {
274282
// No locking if there's no radio (i.e. due to ACF).
275283
if ( !lockButton )
276284
return;
277285

278-
var width, height;
279-
280-
// Check image ratio and original image ratio, but respecting user's
281-
// preference. This is performed when a new image is pre-loaded
282-
// but not if user already manually locked the ratio.
283-
if ( enable == 'check' && !userDefinedLock ) {
284-
width = widthField.getValue();
285-
height = heightField.getValue();
286-
287-
var domRatio = preLoadedWidth * 1000 / preLoadedHeight,
288-
ratio = width * 1000 / height;
286+
if ( typeof enable == 'boolean' ) {
287+
// If user explicitly wants to decide whether
288+
// to lock or not, don't do anything.
289+
if ( userDefinedLock )
290+
return;
289291

290-
lockRatio = false;
291-
292-
// Lock ratio, if there is no width and no height specified.
293-
if ( !width && !height )
294-
lockRatio = true;
295-
296-
// Lock ratio if there is at least width or height specified,
297-
// and the old ratio that matches the new one.
298-
else if ( !isNaN( domRatio + ratio ) && Math.round( domRatio ) == Math.round( ratio ) )
299-
lockRatio = true;
300-
}
301-
302-
// True or false.
303-
else if ( typeof enable == 'boolean' )
304292
lockRatio = enable;
293+
}
305294

306295
// Undefined. User changed lock value.
307296
else {
297+
var width = widthField.getValue(),
298+
height;
299+
308300
userDefinedLock = true;
309301
lockRatio = !lockRatio;
310302

311-
width = widthField.getValue();
312-
313303
// Automatically adjust height to width to match
314304
// the original ratio (based on dom- dimensions).
315305
if ( lockRatio && width ) {
@@ -356,22 +346,16 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
356346
image = widget.parts.image;
357347

358348
// Reset global variables.
359-
preLoadedWidth = preLoadedHeight = srcChanged =
360-
userDefinedLock = lockRatio = false;
349+
srcChanged = userDefinedLock = lockRatio = false;
350+
351+
// Natural dimensions of the image.
352+
natural = getNatural( image );
361353

362-
// TODO: IE8
363354
// Get the natural width of the image.
364-
domWidth = image.$.naturalWidth;
355+
preLoadedWidth = domWidth = natural.width;
365356

366-
// TODO: IE8
367357
// Get the natural height of the image.
368-
domHeight = image.$.naturalHeight;
369-
370-
// Determine image ratio lock on startup. Delayed, waiting for
371-
// fields to be filled with setup functions.
372-
setTimeout( function() {
373-
toggleLockDimensions( 'check' );
374-
} );
358+
preLoadedHeight = domHeight = natural.height;
375359
},
376360
contents: [
377361
{
@@ -473,6 +457,12 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
473457
type: 'html',
474458
style: lockResetStyle,
475459
onLoad: onLoadLockReset,
460+
setup: function( widget ) {
461+
toggleLockRatio( widget.data.lock );
462+
},
463+
commit: function( widget ) {
464+
widget.setData( 'lock', lockRatio );
465+
},
476466
html: lockResetHtml
477467
}
478468
]

plugins/image2/plugin.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,17 @@
210210
},
211211

212212
init: function() {
213-
var image = this.parts.image,
213+
var helpers = CKEDITOR.plugins.image2,
214+
image = this.parts.image,
214215
data = {
215216
hasCaption: !!this.parts.caption,
216217
src: image.getAttribute( 'src' ),
217218
alt: image.getAttribute( 'alt' ) || '',
218219
width: image.getAttribute( 'width' ) || '',
219-
height: image.getAttribute( 'height' ) || ''
220+
height: image.getAttribute( 'height' ) || '',
221+
222+
// Lock ratio is on by default (#10833).
223+
lock: this.ready ? helpers.checkHasNaturalRatio( image ) : true
220224
};
221225

222226
// Read initial float style from figure/image and
@@ -237,7 +241,7 @@
237241
// Setup dynamic image resizing with mouse.
238242
setupResizer( this );
239243

240-
this.shiftState = CKEDITOR.plugins.image2.stateShifter( this.editor );
244+
this.shiftState = helpers.stateShifter( this.editor );
241245

242246
// Add widget editing option to its context menu.
243247
this.on( 'contextMenu', function( evt ) {
@@ -413,6 +417,48 @@
413417

414418
data.init( data.element );
415419
};
420+
},
421+
422+
// Checks whether current ratio of the image match the natural one.
423+
// by comparing dimensions.
424+
// @param {CKEDITOR.dom.element} image
425+
// @returns {Boolean}
426+
checkHasNaturalRatio: function( image ) {
427+
var $ = image.$,
428+
natural = this.getNatural( image );
429+
430+
// The reason for two alternative comparisons is that the rounding can come from
431+
// both dimensions, e.g. there are two cases:
432+
// 1. height is computed as a rounded relation of the real height and the value of width,
433+
// 2. width is computed as a rounded relation of the real width and the value of heigh.
434+
return Math.round( $.clientWidth / natural.width * natural.height ) == $.clientHeight ||
435+
Math.round( $.clientHeight / natural.height * natural.width ) == $.clientWidth;
436+
},
437+
438+
// Returns natural dimensions of the image. For modern browsers
439+
// it uses natural(Width|Height) for old ones (IE8), creates
440+
// a new image and reads dimensions.
441+
// @param {CKEDITOR.dom.element} image
442+
// @returns {Object}
443+
getNatural: function( image ) {
444+
var dimensions;
445+
446+
if ( image.$.naturalWidth ) {
447+
dimensions = {
448+
width: image.$.naturalWidth,
449+
height: image.$.naturalHeight
450+
};
451+
} else {
452+
var img = new Image();
453+
img.src = image.getAttribute( 'src' );
454+
455+
dimensions = {
456+
width: img.width,
457+
height: img.height
458+
};
459+
}
460+
461+
return dimensions;
416462
}
417463
};
418464

@@ -663,13 +709,13 @@
663709
// Calculate with first, and then adjust height, preserving ratio.
664710
function adjustToX() {
665711
newWidth = startWidth + factor * moveDiffX;
666-
newHeight = 0 | newWidth / ratio;
712+
newHeight = Math.round( newWidth / ratio );
667713
}
668714

669715
// Calculate height first, and then adjust width, preserving ratio.
670716
function adjustToY() {
671717
newHeight = startHeight - moveDiffY;
672-
newWidth = 0 | newHeight * ratio;
718+
newWidth = Math.round( newHeight * ratio );
673719
}
674720

675721
// This is how variables refer to the geometry.

0 commit comments

Comments
 (0)