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

Added: Basic table heading columns calculation.

Maciej Gołaszewski 7 лет назад
Родитель
Сommit
ee788e7204

+ 29 - 8
packages/ckeditor5-table/src/converters.js

@@ -131,12 +131,12 @@ function _downcastTableRow( tableRow, conversionApi, parent ) {
 	conversionApi.writer.insert( Position.createAt( parent, 'end' ), tableRowElement );
 }
 
-function _sortRows( table, conversionApi ) {
-	const rows = { header: [], body: [] };
+function _sortRows( viewTable, conversionApi ) {
+	const rows = { header: [], body: [], maxHeadings: 0 };
 
 	let firstThead;
 
-	for ( const viewChild of Array.from( table.getChildren() ) ) {
+	for ( const viewChild of Array.from( viewTable.getChildren() ) ) {
 		if ( viewChild.name === 'tbody' || viewChild.name === 'thead' || viewChild.name === 'tfoot' ) {
 			if ( viewChild.name === 'thead' && !firstThead ) {
 				firstThead = viewChild;
@@ -151,12 +151,12 @@ function _sortRows( table, conversionApi ) {
 	return rows;
 }
 
-function _upcastTableRows( table, modelTable, modelCursor, conversionApi ) {
+function _upcastTableRows( viewTable, modelTable, modelCursor, conversionApi ) {
 	const modelRange = new ModelRange( modelCursor );
 
-	const rows = _sortRows( table, conversionApi, modelCursor );
+	const tableMeta = _sortRows( viewTable, conversionApi, modelCursor );
 
-	const allRows = [ ...rows.header, ...rows.body ];
+	const allRows = [ ...tableMeta.header, ...tableMeta.body ];
 
 	for ( const rowDef of allRows ) {
 		const rowPosition = ModelPosition.createAt( modelTable, 'end' );
@@ -168,8 +168,12 @@ function _upcastTableRows( table, modelTable, modelCursor, conversionApi ) {
 		conversionApi.convertChildren( rowDef.view, childrenCursor );
 	}
 
-	if ( rows.header.length ) {
-		conversionApi.writer.setAttribute( 'headingRows', rows.header.length, modelTable );
+	if ( tableMeta.header.length ) {
+		conversionApi.writer.setAttribute( 'headingRows', tableMeta.header.length, modelTable );
+	}
+
+	if ( tableMeta.maxHeadings ) {
+		conversionApi.writer.setAttribute( 'headingColumns', tableMeta.maxHeadings, modelTable );
 	}
 
 	if ( !allRows.length ) {
@@ -195,6 +199,23 @@ function _createModelRow( row, rows, conversionApi, firstThead ) {
 	} else {
 		rows.body.push( { model: modelRow, view: row } );
 	}
+
+	let headingCols = 0;
+
+	const tableCells = Array.from( row.getChildren() );
+
+	for ( const tableCell of tableCells ) {
+		const name = tableCell.name;
+		const cellIndex = row.getChildIndex( tableCell );
+
+		if ( name === 'th' && ( cellIndex === 0 || tableCells[ cellIndex - 1 ].name === 'th' ) ) {
+			headingCols = cellIndex + 1;
+		}
+	}
+
+	if ( headingCols > rows.maxHeadings ) {
+		rows.maxHeadings = headingCols;
+	}
 }
 
 function isHead( tableCell ) {

+ 0 - 3
packages/ckeditor5-table/src/tableediting.js

@@ -47,9 +47,6 @@ export default class TablesEditing extends Plugin {
 		conversion.for( 'upcast' ).add( upcastTable() );
 		conversion.for( 'downcast' ).add( downcastTable() );
 
-		// Table row upcast only since downcast conversion is done in `downcastTable()`.
-		conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
-
 		// Table cell conversion.
 		conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
 		conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );

+ 18 - 0
packages/ckeditor5-table/tests/converters.js

@@ -244,6 +244,24 @@ describe( 'Table converters', () => {
 				'<fooTable></fooTable>'
 			);
 		} );
+
+		describe( 'headingColumns', () => {
+			it( 'should properly calculate heading columns', () => {
+				editor.setData(
+					'<table>' +
+					'<tbody>' +
+					'<tr><th>11</th><th>12</th><td>13</td></tr>' +
+					'</tbody>' +
+					'</table>'
+				);
+
+				expectModel(
+					'<table headingColumns="2">' +
+					'<tableRow><tableCell>11</tableCell><tableCell>12</tableCell><tableCell>13</tableCell></tableRow>' +
+					'</table>'
+				);
+			} );
+		} );
 	} );
 
 	describe( 'downcastTable()', () => {