Explorar el Código

Changed: Support heading rows as table attribute.

Maciej Gołaszewski hace 7 años
padre
commit
fdc157c775

+ 26 - 33
packages/ckeditor5-table/src/converters.js

@@ -19,18 +19,19 @@ export function createTableCell( viewElement, modelWriter ) {
 	return modelWriter.createElement( 'tableCell', attributes );
 }
 
-export function createTableRow( viewElement, modelWriter ) {
-	const attributes = {};
+export function createTable( viewElement, modelWriter ) {
+	const attributes = {
+		headingRows: 0,
+		headingColumns: 0
+	};
 
-	if ( viewElement.parent.name === 'tfoot' ) {
-		attributes.isFooter = true;
-	}
+	const header = _getChildHeader( viewElement );
 
-	if ( viewElement.parent.name === 'thead' ) {
-		attributes.isHeading = true;
+	if ( header ) {
+		attributes.headingRows = header.childCount;
 	}
 
-	return modelWriter.createElement( 'tableRow', attributes );
+	return modelWriter.createElement( 'table', attributes );
 }
 
 export function downcastTableCell( dispatcher ) {
@@ -61,49 +62,41 @@ export function downcastTable( dispatcher ) {
 			return;
 		}
 
-		if ( !conversionApi.consumable.consume( data.item, 'insert' ) ) {
+		const table = data.item;
+
+		if ( !conversionApi.consumable.consume( table, 'insert' ) ) {
 			return;
 		}
 
-		const { headings, footers, rows } = _extractSectionsRows( data.item );
+		const headingRows = table.getAttribute( 'headingRows' );
 
-		if ( headings.length ) {
-			_createTableSection( 'thead', tableElement, headings, conversionApi );
-		}
+		const tableRows = [ ...table.getChildren() ];
+		const headings = tableRows.slice( 0, headingRows );
+		const bodyRows = tableRows.slice( headingRows );
 
-		if ( rows.length ) {
-			_createTableSection( 'tbody', tableElement, rows, conversionApi );
+		if ( headingRows ) {
+			_createTableSection( 'thead', tableElement, headings, conversionApi );
 		}
 
-		if ( footers.length ) {
-			_createTableSection( 'tfoot', tableElement, footers, conversionApi );
+		if ( bodyRows.length ) {
+			_createTableSection( 'tbody', tableElement, bodyRows, conversionApi );
 		}
 
 		const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
 
-		conversionApi.mapper.bindElements( data.item, tableElement );
+		conversionApi.mapper.bindElements( table, tableElement );
 		conversionApi.writer.insert( viewPosition, tableElement );
 	}, { priority: 'normal' } );
 }
 
-function _extractSectionsRows( table ) {
-	const tableRows = [ ...table.getChildren() ];
-
-	const headings = [];
-	const footers = [];
-	const rows = [];
-
-	for ( const tableRow of tableRows ) {
-		if ( tableRow.getAttribute( 'isHeading' ) ) {
-			headings.push( tableRow );
-		} else if ( tableRow.getAttribute( 'isFooter' ) ) {
-			footers.push( tableRow );
-		} else {
-			rows.push( tableRow );
+function _getChildHeader( table ) {
+	for ( const child of Array.from( table.getChildren() ) ) {
+		if ( child.name === 'thead' ) {
+			return child;
 		}
 	}
 
-	return { headings, footers, rows };
+	return false;
 }
 
 function _createTableSection( elementName, tableElement, rows, conversionApi ) {

+ 6 - 6
packages/ckeditor5-table/src/tableediting.js

@@ -9,7 +9,7 @@
 
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
-import { createTableCell, createTableRow, downcastTableCell, downcastTable } from './converters';
+import { createTableCell, createTable, downcastTableCell, downcastTable } from './converters';
 import InsertTableCommand from './inserttablecommand';
 
 export default class TablesEditing extends Plugin {
@@ -23,14 +23,14 @@ export default class TablesEditing extends Plugin {
 
 		schema.register( 'table', {
 			allowWhere: '$block',
-			allowAttributes: [],
+			allowAttributes: [ 'headingRows' ],
 			isBlock: true,
 			isObject: true
 		} );
 
 		schema.register( 'tableRow', {
 			allowIn: 'table',
-			allowAttributes: [ 'isHeading', 'isFooter' ],
+			allowAttributes: [],
 			isBlock: true,
 			isLimit: true
 		} );
@@ -38,17 +38,17 @@ export default class TablesEditing extends Plugin {
 		schema.register( 'tableCell', {
 			allowIn: 'tableRow',
 			allowContentOf: '$block',
-			allowAttributes: [ 'isHeading', 'colspan', 'rowspan' ],
+			allowAttributes: [ 'colspan', 'rowspan' ],
 			isBlock: true,
 			isLimit: true
 		} );
 
 		// Table conversion.
-		conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'table', view: 'table' } ) );
+		conversion.for( 'upcast' ).add( upcastElementToElement( { model: createTable, view: 'table' } ) );
 		conversion.for( 'downcast' ).add( downcastTable );
 
 		// Table row upcast only since downcast conversion is done in `downcastTable()`.
-		conversion.for( 'upcast' ).add( upcastElementToElement( { model: createTableRow, view: 'tr' } ) );
+		conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
 
 		// Table cell conversion.
 		conversion.for( 'upcast' ).add( upcastElementToElement( { model: createTableCell, view: 'td' } ) );

+ 9 - 22
packages/ckeditor5-table/tests/tableediting.js

@@ -40,40 +40,27 @@ describe( 'TableEditing', () => {
 				expect( editor.getData() ).to.equal( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
 			} );
 
-			it( 'should create tfoot section', () => {
-				setModelData( model, '<table><tableRow isFooter="true"><tableCell>foo[]</tableCell></tableRow></table>' );
-
-				expect( editor.getData() ).to.equal( '<table><tfoot><tr><td>foo</td></tr></tfoot></table>' );
-			} );
-
 			it( 'should create thead section', () => {
-				setModelData( model, '<table><tableRow isHeading="true"><tableCell>foo[]</tableCell></tableRow></table>' );
+				setModelData( model, '<table headingRows="1"><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
 
 				expect( editor.getData() ).to.equal( '<table><thead><tr><td>foo</td></tr></thead></table>' );
 			} );
 
-			it( 'should create thead, tbody and tfoot sections in proper order', () => {
-				setModelData( model, '<table>' +
-					'<tableRow><tableCell>foo[]</tableCell></tableRow>' +
-					'<tableRow isFooter="true"><tableCell>foo[]</tableCell></tableRow>' +
-					'<tableRow isHeading="true"><tableCell>foo[]</tableCell></tableRow>' +
+			it( 'should create thead and tbody sections in proper order', () => {
+				setModelData( model, '<table headingRows="1">' +
+					'<tableRow><tableCell>foo</tableCell></tableRow>' +
+					'<tableRow><tableCell>bar</tableCell></tableRow>' +
+					'<tableRow><tableCell>baz[]</tableCell></tableRow>' +
 					'</table>'
 				);
 
 				expect( editor.getData() ).to.equal( '<table>' +
 					'<thead><tr><td>foo</td></tr></thead>' +
-					'<tbody><tr><td>foo</td></tr></tbody>' +
-					'<tfoot><tr><td>foo</td></tr></tfoot>' +
+					'<tbody><tr><td>bar</td></tr><tr><td>baz</td></tr></tbody>' +
 					'</table>'
 				);
 			} );
 
-			it( 'should create th element for tableCell with attribute isHeading=true', () => {
-				setModelData( model, '<table><tableRow isHeading="true"><tableCell isHeading="true">foo[]</tableCell></tableRow></table>' );
-
-				expect( editor.getData() ).to.equal( '<table><thead><tr><th>foo</th></tr></thead></table>' );
-			} );
-
 			it( 'should convert rowspan on tableCell', () => {
 				setModelData( model, '<table><tableRow><tableCell rowspan="2">foo[]</tableCell></tableRow></table>' );
 
@@ -88,11 +75,11 @@ describe( 'TableEditing', () => {
 		} );
 
 		describe( 'view to model', () => {
-			it( 'should convert image figure', () => {
+			it( 'should convert table', () => {
 				editor.setData( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
 
 				expect( getModelData( model, { withoutSelection: true } ) )
-					.to.equal( '<table><tableRow><tableCell>foo</tableCell></tableRow></table>' );
+					.to.equal( '<table headingColumns="0" headingRows="0"><tableRow><tableCell>foo</tableCell></tableRow></table>' );
 			} );
 		} );
 	} );