Przeglądaj źródła

Used sortRanges() helper from utils. Some code readability tuning.

Kuba Niegowski 5 lat temu
rodzic
commit
b4eaa69b1a

+ 9 - 6
packages/ckeditor5-table/src/tableclipboard.js

@@ -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,
@@ -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 );
 }

+ 10 - 4
packages/ckeditor5-table/src/utils/selection.js

@@ -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 );
@@ -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;