Skip to content

Commit

Permalink
Merge pull request #7597 from ckeditor/i/6768
Browse files Browse the repository at this point in the history
Feature (table): Added option to set heading rows and columns for `insertTable` command and `TableUtils#createTable()`. Closes #6768.

MINOR BREAKING CHANGE (table): The parameters of `TableUtils#createTable()` has changed. Use `options` object to pass number of `rows` and `columns`.
  • Loading branch information
jodator authored Jul 13, 2020
2 parents ef4b1f9 + c8608e8 commit 392f61f
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 10 deletions.
7 changes: 3 additions & 4 deletions packages/ckeditor5-table/src/commands/inserttablecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,19 @@ 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 = {} ) {
const model = this.editor.model;
const selection = model.document.selection;
const tableUtils = this.editor.plugins.get( 'TableUtils' );

const rows = parseInt( options.rows ) || 2;
const columns = parseInt( options.columns ) || 2;

const insertPosition = findOptimalInsertionPosition( selection, model );

model.change( writer => {
const table = tableUtils.createTable( writer, rows, columns );
const table = tableUtils.createTable( writer, options );

model.insertContent( table, insertPosition );

Expand Down
22 changes: 18 additions & 4 deletions packages/ckeditor5-table/src/tableutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,36 @@ export default class TableUtils extends Plugin {
*
* model.change( ( writer ) => {
* // Create a table of 2 rows and 7 columns:
* const table = tableUtils.createTable( writer, 2, 7);
* const table = tableUtils.createTable( writer, { rows: 2, columns: 7 } );
*
* // Insert a table to the model at the best position taking the current selection:
* model.insertContent( table );
* }
*
* @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.rows=2] The number of rows to create.
* @param {Number} [options.columns=2] The number of columns to create.
* @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, options ) {
const table = writer.createElement( 'table' );

const rows = parseInt( options.rows ) || 2;
const columns = parseInt( options.columns ) || 2;

createEmptyRows( writer, table, 0, rows, columns );

if ( options.headingRows ) {
updateNumericAttribute( 'headingRows', options.headingRows, table, writer, 0 );
}

if ( options.headingColumns ) {
updateNumericAttribute( 'headingColumns', options.headingColumns, table, writer, 0 );
}

return table;
}

Expand Down
15 changes: 15 additions & 0 deletions packages/ckeditor5-table/tests/commands/inserttablecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>' );

Expand Down
4 changes: 2 additions & 2 deletions packages/ckeditor5-table/tests/tableclipboard-paste.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe( 'table clipboard', () => {
model.createRangeOn( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) )
] );

const tableToInsert = editor.plugins.get( 'TableUtils' ).createTable( writer, 2, 2 );
const tableToInsert = editor.plugins.get( 'TableUtils' ).createTable( writer, { rows: 2, columns: 2 } );

for ( const { cell } of new TableWalker( tableToInsert ) ) {
writer.insertText( 'foo', cell.getChild( 0 ), 0 );
Expand Down Expand Up @@ -461,7 +461,7 @@ describe( 'table clipboard', () => {
);

model.change( writer => {
const tableToInsert = editor.plugins.get( 'TableUtils' ).createTable( writer, 2, 2 );
const tableToInsert = editor.plugins.get( 'TableUtils' ).createTable( writer, { rows: 2, columns: 2 } );

for ( const { cell } of new TableWalker( tableToInsert ) ) {
writer.insertText( 'foo', cell.getChild( 0 ), 0 );
Expand Down
66 changes: 66 additions & 0 deletions packages/ckeditor5-table/tests/tableutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1526,4 +1526,70 @@ describe( 'TableUtils', () => {
} );
} );
} );

describe( 'createTable()', () => {
it( 'should create table', () => {
setData( model, '[]' );

model.change( writer => {
const table = tableUtils.createTable( writer, { rows: 3, columns: 2 } );

model.insertContent( table, model.document.selection.focus );
} );

assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
[ '', '' ],
[ '', '' ],
[ '', '' ]
] ) );
} );

it( 'should create table with heading rows', () => {
setData( model, '[]' );

model.change( writer => {
const table = tableUtils.createTable( writer, { rows: 3, columns: 2, headingRows: 1 } );

model.insertContent( table, model.document.selection.focus );
} );

assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
[ '', '' ],
[ '', '' ],
[ '', '' ]
], { headingRows: 1 } ) );
} );

it( 'should create table with heading columns', () => {
setData( model, '[]' );

model.change( writer => {
const table = tableUtils.createTable( writer, { rows: 3, columns: 2, headingColumns: 1 } );

model.insertContent( table, model.document.selection.focus );
} );

assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
[ '', '' ],
[ '', '' ],
[ '', '' ]
], { headingColumns: 1 } ) );
} );

it( 'should create table with heading rows and columns', () => {
setData( model, '[]' );

model.change( writer => {
const table = tableUtils.createTable( writer, { rows: 3, columns: 2, headingRows: 2, headingColumns: 1 } );

model.insertContent( table, model.document.selection.focus );
} );

assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
[ '', '' ],
[ '', '' ],
[ '', '' ]
], { headingRows: 2, headingColumns: 1 } ) );
} );
} );
} );

0 comments on commit 392f61f

Please sign in to comment.