8
0
Просмотр исходного кода

Added option to set heading rows/columns for `insertTable` command and `TableUtils#createTable()`.

Kuba Niegowski 5 лет назад
Родитель
Сommit
7a969ab6f8

+ 3 - 1
packages/ckeditor5-table/src/commands/inserttablecommand.js

@@ -43,6 +43,8 @@ export default class InsertTableCommand extends Command {
 	 * @param {Object} options
 	 * @param {Number} [options.rows=2] The number of rows to create in the inserted table.
 	 * @param {Number} [options.columns=2] The number of columns to create in the inserted table.
+	 * @param {Number} [options.headingRows=0] The number of heading rows.
+	 * @param {Number} [options.headingColumns=0] The number of heading columns.
 	 * @fires execute
 	 */
 	execute( options = {} ) {
@@ -56,7 +58,7 @@ export default class InsertTableCommand extends Command {
 		const insertPosition = findOptimalInsertionPosition( selection, model );
 
 		model.change( writer => {
-			const table = tableUtils.createTable( writer, rows, columns );
+			const table = tableUtils.createTable( writer, rows, columns, options );
 
 			model.insertContent( table, insertPosition );
 

+ 15 - 1
packages/ckeditor5-table/src/tableutils.js

@@ -83,13 +83,27 @@ export default class TableUtils extends Plugin {
 	 * @param {module:engine/model/writer~Writer} writer The model writer.
 	 * @param {Number} rows The number of rows to create.
 	 * @param {Number} columns The number of columns to create.
+	 * @param {Object} [options]
+	 * @param {Number} [options.headingRows=0] The number of heading rows.
+	 * @param {Number} [options.headingColumns=0] The number of heading columns.
 	 * @returns {module:engine/model/element~Element} The created table element.
 	 */
-	createTable( writer, rows, columns ) {
+	createTable( writer, rows, columns, options ) {
 		const table = writer.createElement( 'table' );
 
 		createEmptyRows( writer, table, 0, rows, columns );
 
+		const headingRows = options && parseInt( options.headingRows ) || 0;
+		const headingColumns = options && parseInt( options.headingColumns ) || 0;
+
+		if ( headingRows ) {
+			updateNumericAttribute( 'headingRows', headingRows, table, writer, 0 );
+		}
+
+		if ( headingColumns ) {
+			updateNumericAttribute( 'headingColumns', headingColumns, table, writer, 0 );
+		}
+
 		return table;
 	}
 

+ 15 - 0
packages/ckeditor5-table/tests/commands/inserttablecommand.js

@@ -100,6 +100,21 @@ describe( 'InsertTableCommand', () => {
 				);
 			} );
 
+			it( 'should insert table with given heading rows and heading columns after non-empty paragraph', () => {
+				setData( model, '<paragraph>foo[]</paragraph>' );
+
+				command.execute( { rows: 3, columns: 4, headingRows: 1, headingColumns: 2 } );
+
+				assertEqualMarkup( getData( model ),
+					'<paragraph>foo</paragraph>' +
+					modelTable( [
+						[ '[]', '', '', '' ],
+						[ '', '', '', '' ],
+						[ '', '', '', '' ]
+					], { headingRows: 1, headingColumns: 2 } )
+				);
+			} );
+
 			it( 'should insert table before after non-empty paragraph if selection is inside', () => {
 				setData( model, '<paragraph>f[]oo</paragraph>' );