Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6768: Added option to set heading rows/columns for insertTable command and TableUtils#createTable() #7597

Merged
merged 3 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1511,4 +1511,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 } ) );
} );
} );
} );