Skip to content

Commit

Permalink
Other (table): Reuse Rect#getBoundingRect() method to calculate ballo…
Browse files Browse the repository at this point in the history
…on toolbar position.
  • Loading branch information
mlewand committed Aug 17, 2020
1 parent 4ce955f commit d9fe03f
Showing 1 changed file with 13 additions and 28 deletions.
41 changes: 13 additions & 28 deletions packages/ckeditor5-table/src/utils/ui/contextualballoon.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ export function getBalloonCellPositionData( editor ) {

if ( selection.rangeCount > 1 ) {
return {
target: () => createBoundingRect( selection.getRanges(), modelRange => {
const modelTableCell = getTableCellAtPosition( modelRange.start );
const viewTableCell = mapper.toViewElement( modelTableCell );
return new Rect( domConverter.viewToDom( viewTableCell ) );
} ),
target: () => createBoundingRect( selection.getRanges(), editor ),
positions: BALLOON_POSITIONS
};
}
Expand All @@ -115,30 +111,19 @@ function getTableCellAtPosition( position ) {
return isTableCellSelected ? position.nodeAfter : position.findAncestor( 'tableCell' );
}

// Returns bounding rect for list of rects.
// Returns bounding rectangle for given model ranges.
//
// @param {Array.<module:utils/dom/rect~Rect>|Array.<*>} list List of `Rect`s or any list to map by `mapFn`.
// @param {Function} mapFn Mapping function for list elements.
// @param {Iterable.<module:engine/model/range~Range>} ranges Model ranges that the bounding rect should be returned for.
// @param {module:core/editor/editor~Editor} editor The editor instance.
// @returns {module:utils/dom/rect~Rect}
function createBoundingRect( list, mapFn ) {
const rectData = {
left: Number.POSITIVE_INFINITY,
top: Number.POSITIVE_INFINITY,
right: Number.NEGATIVE_INFINITY,
bottom: Number.NEGATIVE_INFINITY
};

for ( const item of list ) {
const rect = mapFn( item );

rectData.left = Math.min( rectData.left, rect.left );
rectData.top = Math.min( rectData.top, rect.top );
rectData.right = Math.max( rectData.right, rect.right );
rectData.bottom = Math.max( rectData.bottom, rect.bottom );
}

rectData.width = rectData.right - rectData.left;
rectData.height = rectData.bottom - rectData.top;
function createBoundingRect( ranges, editor ) {
const mapper = editor.editing.mapper;
const domConverter = editor.editing.view.domConverter;
const rects = Array.from( ranges ).map( range => {
const modelTableCell = getTableCellAtPosition( range.start );
const viewTableCell = mapper.toViewElement( modelTableCell );
return new Rect( domConverter.viewToDom( viewTableCell ) );
} );

return new Rect( rectData );
return Rect.getBoundingRect( rects );
}

0 comments on commit d9fe03f

Please sign in to comment.