Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Minor code refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinmar committed May 4, 2017
1 parent d3b198e commit 273513d
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/dom/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import global from './global';
import isRange from './isrange';
import isElement from '../lib/lodash/isElement';

const rectProperties = [ 'top', 'right', 'bottom', 'left', 'width', 'height' ];

/**
* A helper class representing a `ClientRect` object, e.g. value returned by
* the native `object.getBoundingClientRect()` method. Provides a set of methods
Expand Down Expand Up @@ -54,38 +52,30 @@ export default class Rect {
enumerable: false
} );

// Acquires all the rect properties from the passed source.
//
// @private
// @param {ClientRect|module:utils/dom/rect~Rect|Object} source}
const setProperties = source => {
rectProperties.forEach( p => this[ p ] = source[ p ] );
};

if ( isElement( source ) ) {
setProperties( source.getBoundingClientRect() );
copyRectProperties( this, source.getBoundingClientRect() );
} else if ( isRange( source ) ) {
// Use getClientRects() when the range is collapsed
// Use getClientRects() when the range is collapsed.
// https://github.com/ckeditor/ckeditor5-utils/issues/153
if ( source.collapsed ) {
const rects = source.getClientRects();

if ( rects.length ) {
setProperties( rects[ 0 ] );
copyRectProperties( this, rects[ 0 ] );
}
// If there's no client rects for the Range, use parent container's bounding
// rect instead and adjust rect's width to simulate the actual geometry of such
// range.
// https://github.com/ckeditor/ckeditor5-utils/issues/153
else {
setProperties( source.startContainer.getBoundingClientRect() );
copyRectProperties( this, source.startContainer.getBoundingClientRect() );
this.width = 0;
}
} else {
setProperties( source.getBoundingClientRect() );
copyRectProperties( this, source.getBoundingClientRect() );
}
} else {
setProperties( source );
copyRectProperties( this, source );
}

/**
Expand Down Expand Up @@ -279,3 +269,16 @@ export default class Rect {
} );
}
}

const rectProperties = [ 'top', 'right', 'bottom', 'left', 'width', 'height' ];

// Acquires all the rect properties from the passed source.
//
// @private
// @param {module:utils/dom/rect~Rect} rect
// @param {ClientRect|module:utils/dom/rect~Rect|Object} source
function copyRectProperties( rect, source ) {
for ( const p of rectProperties ) {
rect[ p ] = source[ p ];
}
}

0 comments on commit 273513d

Please sign in to comment.