浏览代码

Filter cells to check when splitting table.

Maciej Gołaszewski 5 年之前
父节点
当前提交
94f582b8e3
共有 2 个文件被更改,包括 41 次插入13 次删除
  1. 4 4
      packages/ckeditor5-table/src/tableclipboard.js
  2. 37 9
      packages/ckeditor5-table/src/utils.js

+ 4 - 4
packages/ckeditor5-table/src/tableclipboard.js

@@ -309,14 +309,14 @@ function prepareLandingPlace( selectedTableCells, writer ) {
 	const { first: firstColumn, last: lastColumn } = getColumnIndexes( selectedTableCells );
 
 	if ( firstRow > 0 ) {
-		cutCellsHorizontallyAt( table, firstRow, 0, writer );
+		cutCellsHorizontallyAt( table, firstRow, 0, writer, { firstRow: 0, firstColumn, lastRow: 1000, lastColumn } );
 	}
 
-	cutCellsHorizontallyAt( table, lastRow + 1, firstRow, writer );
+	cutCellsHorizontallyAt( table, lastRow + 1, firstRow, writer, { firstRow: 0, firstColumn, lastRow: 1000, lastColumn } );
 
 	if ( firstColumn > 0 ) {
-		cutCellsVerticallyAt( table, firstColumn, 0, writer );
+		cutCellsVerticallyAt( table, firstColumn, 0, writer, { firstRow, firstColumn: 0, lastRow, lastColumn: 1000 } );
 	}
 
-	cutCellsVerticallyAt( table, lastColumn + 1, firstColumn, writer );
+	cutCellsVerticallyAt( table, lastColumn + 1, firstColumn, writer, { firstRow, firstColumn: 0, lastRow, lastColumn: 1000 } );
 }

+ 37 - 9
packages/ckeditor5-table/src/utils.js

@@ -248,23 +248,48 @@ export function isSelectionRectangular( selectedTableCells, tableUtils ) {
 }
 
 // TODO: refactor it to a better, general util.
-export function cutCellsHorizontallyAt( table, headingRowsToSet, currentHeadingRows, writer ) {
-	const cellsToSplit = getHorizontallyOverlappingCells( table, headingRowsToSet, currentHeadingRows );
+export function cutCellsHorizontallyAt( table, headingRowsToSet, currentHeadingRows, writer, boundingBox ) {
+	const overlappingCells = getHorizontallyOverlappingCells( table, headingRowsToSet, currentHeadingRows );
 
-	for ( const cell of cellsToSplit ) {
+	let cellsToSplit;
+
+	if ( boundingBox === undefined ) {
+		cellsToSplit = overlappingCells;
+	} else {
+		cellsToSplit = overlappingCells.filter( filterToBoundingBox( boundingBox ) );
+	}
+
+	for ( const { cell } of cellsToSplit ) {
 		splitHorizontally( cell, headingRowsToSet, writer );
 	}
 }
 
 // TODO: refactor it to a better, general util.
-export function cutCellsVerticallyAt( table, headingColumnsToSet, currentHeadingColumns, writer ) {
-	const cellsToSplit = getVerticallyOverlappingCells( table, headingColumnsToSet, currentHeadingColumns );
+export function cutCellsVerticallyAt( table, headingColumnsToSet, currentHeadingColumns, writer, boundingBox ) {
+	const overlappingCells = getVerticallyOverlappingCells( table, headingColumnsToSet, currentHeadingColumns );
+
+	let cellsToSplit;
+
+	if ( boundingBox === undefined ) {
+		cellsToSplit = overlappingCells;
+	} else {
+		cellsToSplit = overlappingCells.filter( filterToBoundingBox( boundingBox ) );
+	}
 
 	for ( const { cell, column } of cellsToSplit ) {
 		splitVertically( cell, column, headingColumnsToSet, writer );
 	}
 }
 
+// TODO: better fit to bounding box to match criteria.. should check also spans because sometimes we need to split them.
+function filterToBoundingBox( boundingBox ) {
+	const { firstRow, firstColumn, lastRow, lastColumn } = boundingBox;
+
+	return ( { row, column } ) => {
+		return ( ( firstRow <= row ) && ( row <= lastRow ) ) && ( firstColumn <= column && column <= lastColumn );
+	};
+}
+
 // Returns cells that span beyond the new heading section.
 //
 // @param {module:engine/model/element~Element} table The table to check.
@@ -280,9 +305,11 @@ function getHorizontallyOverlappingCells( table, headingRowsToSet, currentHeadin
 
 	const tableWalker = new TableWalker( table, { startRow: startAnalysisRow, endRow: endAnalysisRow } );
 
-	for ( const { row, rowspan, cell } of tableWalker ) {
+	for ( const twv of tableWalker ) {
+		const { row, rowspan } = twv;
+
 		if ( rowspan > 1 && row + rowspan > headingRowsToSet ) {
-			cellsToSplit.push( cell );
+			cellsToSplit.push( twv );
 		}
 	}
 
@@ -355,14 +382,15 @@ function getVerticallyOverlappingCells( table, headingColumnsToSet, currentHeadi
 	// todo: end/start column
 	const tableWalker = new TableWalker( table );
 
-	for ( const { column, colspan, cell } of tableWalker ) {
+	for ( const twv of tableWalker ) {
+		const { column, colspan } = twv;
 		// Skip slots outside the cropped area.
 		// Could use startColumn, endColumn. See: https://github.com/ckeditor/ckeditor5/issues/6785.
 		if ( startAnalysisColumn > column || column > endAnalysisColumn ) {
 			continue;
 		}
 		if ( colspan > 1 && column + colspan > headingColumnsToSet ) {
-			cellsToSplit.push( { cell, column } );
+			cellsToSplit.push( twv );
 		}
 	}