8
0
Prechádzať zdrojové kódy

Internal: Implemented adding column headers.

Toggling is not yet supported.
Marek Lewandowski 5 rokov pred
rodič
commit
027ad87cb9

+ 6 - 7
packages/ckeditor5-table/src/commands/setheadercolumncommand.js

@@ -9,7 +9,7 @@
 
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
 
-import { findAncestor, updateNumericAttribute } from './utils';
+import { updateNumericAttribute } from './utils';
 import { getTableCellsInSelection } from '../tableselection/utils';
 import { getTableCellsInSelection } from '../tableselection/utils';
 
 
 /**
 /**
@@ -63,16 +63,15 @@ export default class SetHeaderColumnCommand extends Command {
 	 */
 	 */
 	execute( options = {} ) {
 	execute( options = {} ) {
 		const model = this.editor.model;
 		const model = this.editor.model;
-		const doc = model.document;
-		const selection = doc.selection;
 		const tableUtils = this.editor.plugins.get( 'TableUtils' );
 		const tableUtils = this.editor.plugins.get( 'TableUtils' );
 
 
-		const position = selection.getFirstPosition();
-		const tableCell = findAncestor( 'tableCell', position );
-		const tableRow = tableCell.parent;
+		const selectedCells = getTableCellsInSelection( model.document.selection, true );
+		const firstCell = selectedCells[ 0 ];
+		const lastCell = selectedCells[ selectedCells.length - 1 ];
+		const tableRow = firstCell.parent;
 		const table = tableRow.parent;
 		const table = tableRow.parent;
 
 
-		const { column: selectionColumn } = tableUtils.getCellLocation( tableCell );
+		const selectionColumn = Math.max( tableUtils.getCellLocation( firstCell ).column, tableUtils.getCellLocation( lastCell ).column );
 
 
 		if ( options.forceValue === this.value ) {
 		if ( options.forceValue === this.value ) {
 			return;
 			return;

+ 82 - 2
packages/ckeditor5-table/tests/commands/setheadercolumncommand.js

@@ -7,7 +7,7 @@ import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltestedit
 import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 
 import SetHeaderColumnCommand from '../../src/commands/setheadercolumncommand';
 import SetHeaderColumnCommand from '../../src/commands/setheadercolumncommand';
-import { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
+import { assertSelectedCells, defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
 import TableSelection from '../../src/tableselection';
 import TableSelection from '../../src/tableselection';
 import TableUtils from '../../src/tableutils';
 import TableUtils from '../../src/tableutils';
 import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
@@ -151,7 +151,7 @@ describe( 'SetHeaderColumnCommand', () => {
 	} );
 	} );
 
 
 	describe( 'execute()', () => {
 	describe( 'execute()', () => {
-		it( 'should set heading columns attribute that cover column in which is selection', () => {
+		it( 'should set heading columns attribute that cover column with collapsed selection', () => {
 			setData( model, modelTable( [
 			setData( model, modelTable( [
 				[ '00', '01[]', '02', '03' ]
 				[ '00', '01[]', '02', '03' ]
 			] ) );
 			] ) );
@@ -163,6 +163,29 @@ describe( 'SetHeaderColumnCommand', () => {
 			], { headingColumns: 2 } ) );
 			], { headingColumns: 2 } ) );
 		} );
 		} );
 
 
+		it( 'should set heading columns attribute that cover column with entire cell selected', () => {
+			setData( model, modelTable( [
+				[ '00', '01', '02', '03' ]
+			] ) );
+
+			const tableSelection = editor.plugins.get( TableSelection );
+			const modelRoot = model.document.getRoot();
+			tableSelection._setCellSelection(
+				modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
+				modelRoot.getNodeByPath( [ 0, 0, 1 ] )
+			);
+
+			command.execute();
+
+			assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
+				[ '00', '01', '02', '03' ]
+			], { headingColumns: 2 } ) );
+
+			assertSelectedCells( model, [
+				[ 0, 1, 0, 0 ]
+			] );
+		} );
+
 		it( 'should set heading columns attribute below current selection column', () => {
 		it( 'should set heading columns attribute below current selection column', () => {
 			setData( model, modelTable( [
 			setData( model, modelTable( [
 				[ '00', '01[]', '02', '03' ]
 				[ '00', '01[]', '02', '03' ]
@@ -175,6 +198,63 @@ describe( 'SetHeaderColumnCommand', () => {
 			], { headingColumns: 1 } ) );
 			], { headingColumns: 1 } ) );
 		} );
 		} );
 
 
+		describe( 'multi-cell selection', () => {
+			describe( 'setting header', () => {
+				it( 'should set it correctly in a middle of single-row, multiple cell selection ', () => {
+					setData( model, modelTable( [
+						[ '00', '01', '02', '03' ]
+					] ) );
+
+					const tableSelection = editor.plugins.get( TableSelection );
+					const modelRoot = model.document.getRoot();
+					tableSelection._setCellSelection(
+						modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
+						modelRoot.getNodeByPath( [ 0, 0, 2 ] )
+					);
+
+					command.execute();
+
+					assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
+						[ '00', '01', '02', '03' ]
+					], {
+						headingColumns: 3
+					} ) );
+
+					assertSelectedCells( model, [
+						[ 0, 1, 1, 0 ]
+					] );
+				} );
+
+				it( 'should set it correctly in a middle of multi-row, multiple cell selection ', () => {
+					setData( model, modelTable( [
+						[ '00', '01', '02', '03' ],
+						[ '10', '11', '12', '13' ]
+					] ) );
+
+					const tableSelection = editor.plugins.get( TableSelection );
+					const modelRoot = model.document.getRoot();
+					tableSelection._setCellSelection(
+						modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
+						modelRoot.getNodeByPath( [ 0, 1, 1 ] )
+					);
+
+					command.execute();
+
+					assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
+						[ '00', '01', '02', '03' ],
+						[ '10', '11', '12', '13' ]
+					], {
+						headingColumns: 2
+					} ) );
+
+					assertSelectedCells( model, [
+						[ 0, 1, 0, 0 ],
+						[ 0, 1, 0, 0 ]
+					] );
+				} );
+			} );
+		} );
+
 		it( 'should toggle of selected column', () => {
 		it( 'should toggle of selected column', () => {
 			setData( model, modelTable( [
 			setData( model, modelTable( [
 				[ '00', '01[]', '02', '03' ]
 				[ '00', '01[]', '02', '03' ]