Parcourir la source

Internal: Added a helper to get number of rows in the table.

Marek Lewandowski il y a 5 ans
Parent
commit
c1dbd37106

+ 16 - 0
packages/ckeditor5-table/src/tableutils.js

@@ -550,6 +550,22 @@ export default class TableUtils extends Plugin {
 			return columns + columnWidth;
 		}, 0 );
 	}
+
+	/**
+	 * Returns the number of rows for a given table.
+	 *
+	 *		editor.plugins.get( 'TableUtils' ).getRows( table );
+	 *
+	 * @param {module:engine/model/element~Element} table The table to analyze.
+	 * @returns {Number}
+	 */
+	getRows( table ) {
+		return [ ...table.getChildren() ].reduce( ( rows, row ) => {
+			const currentRowCount = parseInt( row.getChild( 0 ).getAttribute( 'rowspan' ) || 1 );
+
+			return rows + currentRowCount;
+		}, 0 );
+	}
 }
 
 // Creates empty rows at the given index in an existing table.

+ 30 - 0
packages/ckeditor5-table/tests/tableutils.js

@@ -699,4 +699,34 @@ describe( 'TableUtils', () => {
 			expect( tableUtils.getColumns( root.getNodeByPath( [ 0 ] ) ) ).to.equal( 5 );
 		} );
 	} );
+
+	describe( 'getRows()', () => {
+		it( 'should return proper number of columns for simple table', () => {
+			setData( model, modelTable( [
+				[ '00', '01' ],
+				[ '10', '11' ]
+			] ) );
+
+			expect( tableUtils.getRows( root.getNodeByPath( [ 0 ] ) ) ).to.equal( 2 );
+		} );
+
+		it( 'should return proper number of columns for a table with header', () => {
+			setData( model, modelTable( [
+				[ '00', '01' ],
+				[ '10', '11' ]
+			], { headingRows: 1 } ) );
+
+			expect( tableUtils.getRows( root.getNodeByPath( [ 0 ] ) ) ).to.equal( 2 );
+		} );
+
+		it( 'should return proper number of columns for rowspan table', () => {
+			setData( model, modelTable( [
+				[ '00', '01' ],
+				[ { rowspan: 2, contents: '10' }, '11' ],
+				[ '21' ]
+			] ) );
+
+			expect( tableUtils.getRows( root.getNodeByPath( [ 0 ] ) ) ).to.equal( 4 );
+		} );
+	} );
 } );