浏览代码

Changed: Remove unsplitVertically() method from commands/utils.

Maciej Gołaszewski 7 年之前
父节点
当前提交
43a6e6dae5

+ 64 - 5
packages/ckeditor5-table/src/commands/settableheaderscommand.js

@@ -8,8 +8,9 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import { getParentTable, unsplitVertically } from './utils';
+import { getParentTable } from './utils';
 import TableWalker from '../tablewalker';
+import Position from '../../../ckeditor5-engine/src/model/position';
 
 /**
  * The set table headers command.
@@ -62,13 +63,12 @@ export default class SetTableHeadersCommand extends Command {
 					const row = tableWalkerValue.row;
 
 					if ( rowspan > 1 && row + rowspan > rows ) {
-						cellsToSplit.push( tableWalkerValue.cell );
+						cellsToSplit.push( tableWalkerValue );
 					}
 				}
 
-				for ( const tableCell of cellsToSplit ) {
-					unsplitVertically( tableCell, writer );
-					writer.removeAttribute( 'rowspan', tableCell );
+				for ( const tableWalkerValue of cellsToSplit ) {
+					splitVertically( tableWalkerValue.cell, rows, writer );
 				}
 			}
 
@@ -90,3 +90,62 @@ function updateTableAttribute( table, attributeName, newValue, writer ) {
 		}
 	}
 }
+
+/**
+ * Splits table cell vertically.
+ *
+ * @param {module:engine/model/element} tableCell
+ * @param writer
+ */
+function splitVertically( tableCell, headingRows, writer ) {
+	const tableRow = tableCell.parent;
+	const table = tableRow.parent;
+	const rowIndex = tableRow.index;
+
+	const rowspan = parseInt( tableCell.getAttribute( 'rowspan' ) );
+
+	const newRowspan = headingRows - rowIndex;
+	const spanToSet = rowspan - newRowspan;
+
+	if ( newRowspan > 1 ) {
+		writer.setAttribute( 'rowspan', newRowspan, tableCell );
+	} else {
+		writer.removeAttribute( 'rowspan', tableCell );
+	}
+
+	const startRow = table.getChildIndex( tableRow );
+	const endRow = startRow + newRowspan;
+
+	const tableWalker = new TableWalker( table, { startRow, endRow, includeSpanned: true } );
+
+	let columnIndex;
+	let previousCell;
+
+	const attributes = {};
+
+	if ( spanToSet > 1 ) {
+		attributes.rowspan = spanToSet;
+	}
+
+	for ( const tableWalkerInfo of [ ...tableWalker ] ) {
+		if ( tableWalkerInfo.cell ) {
+			previousCell = tableWalkerInfo.cell;
+		}
+
+		if ( tableWalkerInfo.cell === tableCell ) {
+			columnIndex = tableWalkerInfo.column;
+
+			if ( tableWalkerInfo.colspan > 1 ) {
+				attributes.colspan = tableWalkerInfo.colspan;
+			}
+		}
+
+		if ( columnIndex !== undefined && columnIndex === tableWalkerInfo.column && tableWalkerInfo.row === endRow ) {
+			const insertRow = table.getChild( tableWalkerInfo.row );
+
+			const position = previousCell.parent === insertRow ? Position.createAt( insertRow ) : Position.createAfter( previousCell );
+
+			writer.insertElement( 'tableCell', attributes, position );
+		}
+	}
+}

+ 0 - 59
packages/ckeditor5-table/src/commands/utils.js

@@ -7,9 +7,6 @@
  * @module table/commands/utils
  */
 
-import Position from '@ckeditor/ckeditor5-engine/src/model/position';
-import TableWalker from '../tablewalker';
-
 /**
  * Returns parent table.
  *
@@ -43,59 +40,3 @@ export function getColumns( table ) {
 		return columns + ( columnWidth );
 	}, 0 );
 }
-
-/**
- * Splits table cell vertically.
- *
- * @param {module:engine/model/element} tableCell
- * @param writer
- * @param {Object} [options]
- * @param {Boolean} [options.breakHorizontally=false]
- */
-export function unsplitVertically( tableCell, writer, options = {} ) {
-	const tableRow = tableCell.parent;
-	const table = tableRow.parent;
-
-	const rowspan = parseInt( tableCell.getAttribute( 'rowspan' ) );
-
-	const startRow = table.getChildIndex( tableRow );
-	const endRow = startRow + rowspan - 1;
-
-	const tableWalker = new TableWalker( table, { startRow, endRow, includeSpanned: true } );
-
-	let columnIndex;
-	let previousCell;
-	let cellsToInsert;
-
-	const breakHorizontally = !!options.breakHorizontally;
-	const attributes = {};
-
-	for ( const tableWalkerInfo of tableWalker ) {
-		if ( tableWalkerInfo.cell ) {
-			previousCell = tableWalkerInfo.cell;
-		}
-
-		if ( tableWalkerInfo.cell === tableCell ) {
-			columnIndex = tableWalkerInfo.column;
-			cellsToInsert = breakHorizontally ? tableWalkerInfo.colspan : 1;
-
-			if ( !breakHorizontally && tableWalkerInfo.colspan > 1 ) {
-				attributes.colspan = tableWalkerInfo.colspan;
-			}
-		}
-
-		if ( columnIndex !== undefined && columnIndex === tableWalkerInfo.column && tableWalkerInfo.row > startRow ) {
-			const insertRow = table.getChild( tableWalkerInfo.row );
-
-			if ( previousCell.parent === insertRow ) {
-				for ( let i = 0; i < cellsToInsert; i++ ) {
-					writer.insertElement( 'tableCell', attributes, Position.createAfter( previousCell ) );
-				}
-			} else {
-				for ( let i = 0; i < cellsToInsert; i++ ) {
-					writer.insertElement( 'tableCell', attributes, Position.createAt( insertRow ) );
-				}
-			}
-		}
-	}
-}