Skip to content

Commit

Permalink
Used sortRanges() helper from utils. Some code readability tuning.
Browse files Browse the repository at this point in the history
  • Loading branch information
niegowski committed Jul 10, 2020
1 parent f5a982a commit b4eaa69
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
15 changes: 9 additions & 6 deletions packages/ckeditor5-table/src/tableclipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
findAncestor
} from './utils/common';
import TableUtils from './tableutils';
import { getColumnIndexes, getRowIndexes, getSelectionAffectedTableCells, isSelectionRectangular } from './utils/selection';
import { getColumnIndexes, getRowIndexes, getSelectionAffectedTableCells, isSelectionRectangular, sortRanges } from './utils/selection';
import {
cropTableToDimensions,
getHorizontallyOverlappingCells,
Expand Down Expand Up @@ -320,23 +320,26 @@ function replaceSelectedCellsWithPasted( pastedTable, pastedDimensions, selected
const headingRows = parseInt( selectedTable.getAttribute( 'headingRows' ) || 0 );
const headingColumns = parseInt( selectedTable.getAttribute( 'headingColumns' ) || 0 );

if ( selection.firstRow < headingRows && headingRows <= selection.lastRow ) {
const areHeadingRowsIntersectingSelection = selection.firstRow < headingRows && headingRows <= selection.lastRow;
const areHeadingColumnsIntersectingSelection = selection.firstColumn < headingColumns && headingColumns <= selection.lastColumn;

if ( areHeadingRowsIntersectingSelection ) {
const columnsLimit = { first: selection.firstColumn, last: selection.lastColumn };
const newCells = doHorizontalSplit( selectedTable, headingRows, columnsLimit, writer, selection.firstRow );

cellsToSelect.push( ...newCells );
}

if ( selection.firstColumn < headingColumns && headingColumns <= selection.lastColumn ) {
if ( areHeadingColumnsIntersectingSelection ) {
const rowsLimit = { first: selection.firstRow, last: selection.lastRow };
const newCells = doVerticalSplit( selectedTable, headingColumns, rowsLimit, writer );

cellsToSelect.push( ...newCells );
}

const selectionRanges = cellsToSelect
.map( cell => writer.createRangeOn( cell ) )
.sort( ( a, b ) => a.start.isBefore( b.start ) ? -1 : 1 );
// Selection ranges must be sorted because the first and last selection ranges are considered
// as anchor/focus cell ranges for multi-cell selection.
const selectionRanges = sortRanges( cellsToSelect.map( cell => writer.createRangeOn( cell ) ) );

writer.setSelection( selectionRanges );
}
Expand Down
14 changes: 10 additions & 4 deletions packages/ckeditor5-table/src/utils/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ export function isSelectionRectangular( selectedTableCells, tableUtils ) {
return areaOfValidSelection == areaOfSelectedCells;
}

/**
* Returns array of sorted ranges.
*
* @param {Iterable.<module:engine/model/range~Range>} ranges
* @return {Array.<module:engine/model/range~Range>}
*/
export function sortRanges( ranges ) {
return Array.from( ranges ).sort( compareRangeOrder );
}

// Helper method to get an object with `first` and `last` indexes from an unsorted array of indexes.
function getFirstLastIndexesObject( indexes ) {
const allIndexesSorted = indexes.sort( ( indexA, indexB ) => indexA - indexB );
Expand All @@ -198,10 +208,6 @@ function getFirstLastIndexesObject( indexes ) {
return { first, last };
}

function sortRanges( rangesIterator ) {
return Array.from( rangesIterator ).sort( compareRangeOrder );
}

function compareRangeOrder( rangeA, rangeB ) {
// Since table cell ranges are disjoint, it's enough to check their start positions.
const posA = rangeA.start;
Expand Down

0 comments on commit b4eaa69

Please sign in to comment.