Selaa lähdekoodia

Fixed insert table row/column commands when a widget is selected inside the table cell.

Kuba Niegowski 5 vuotta sitten
vanhempi
commit
81694cd1d8

+ 5 - 2
packages/ckeditor5-table/src/commands/insertcolumncommand.js

@@ -75,8 +75,11 @@ export default class InsertColumnCommand extends Command {
 		const referencePosition = insertBefore ? selection.getFirstPosition() : selection.getLastPosition();
 		const referenceRange = insertBefore ? selection.getFirstRange() : selection.getLastRange();
 
-		const tableCell = referenceRange.getContainedElement() || findAncestor( 'tableCell', referencePosition );
-		const table = tableCell.parent.parent;
+		const containedElement = referenceRange.getContainedElement();
+		const isTableCell = containedElement && containedElement.is( 'tableCell' );
+
+		const tableCell = isTableCell ? containedElement : findAncestor( 'tableCell', referencePosition );
+		const table = findAncestor( 'table', tableCell );
 
 		const { column } = tableUtils.getCellLocation( tableCell );
 

+ 4 - 1
packages/ckeditor5-table/src/commands/insertrowcommand.js

@@ -74,7 +74,10 @@ export default class InsertRowCommand extends Command {
 		const referencePosition = insertAbove ? selection.getFirstPosition() : selection.getLastPosition();
 		const referenceRange = insertAbove ? selection.getFirstRange() : selection.getLastRange();
 
-		const tableCell = referenceRange.getContainedElement() || findAncestor( 'tableCell', referencePosition );
+		const containedElement = referenceRange.getContainedElement();
+		const isTableCell = containedElement && containedElement.is( 'tableCell' );
+		const tableCell = isTableCell ? containedElement : findAncestor( 'tableCell', referencePosition );
+
 		const tableRow = tableCell.parent;
 		const table = tableRow.parent;
 

+ 18 - 1
packages/ckeditor5-table/tests/commands/insertcolumncommand.js

@@ -4,6 +4,7 @@
  */
 
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
+import HorizontalLineEditing from '@ckeditor/ckeditor5-horizontal-line/src/horizontallineediting';
 import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 import InsertColumnCommand from '../../src/commands/insertcolumncommand';
@@ -18,7 +19,7 @@ describe( 'InsertColumnCommand', () => {
 	beforeEach( () => {
 		return ModelTestEditor
 			.create( {
-				plugins: [ TableUtils, TableSelection ]
+				plugins: [ TableUtils, TableSelection, HorizontalLineEditing ]
 			} )
 			.then( newEditor => {
 				editor = newEditor;
@@ -181,6 +182,22 @@ describe( 'InsertColumnCommand', () => {
 					[ { colspan: 5, contents: '31' }, { colspan: 2, contents: '34' } ]
 				], { headingColumns: 5 } ) );
 			} );
+
+			it( 'should insert a column when a widget in the table cell is selected', () => {
+				setData( model, modelTable( [
+					[ '11', '12' ],
+					[ '21', '22' ],
+					[ '31', '[<horizontalLine></horizontalLine>]' ]
+				] ) );
+
+				command.execute();
+
+				assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
+					[ '11', '12', '' ],
+					[ '21', '22', '' ],
+					[ '31', '<horizontalLine></horizontalLine>', '' ]
+				] ) );
+			} );
 		} );
 	} );
 

+ 19 - 1
packages/ckeditor5-table/tests/commands/insertrowcommand.js

@@ -4,6 +4,7 @@
  */
 
 import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
+import HorizontalLineEditing from '@ckeditor/ckeditor5-horizontal-line/src/horizontallineediting';
 import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 import InsertRowCommand from '../../src/commands/insertrowcommand';
@@ -18,7 +19,7 @@ describe( 'InsertRowCommand', () => {
 	beforeEach( () => {
 		return ModelTestEditor
 			.create( {
-				plugins: [ TableUtils, TableSelection ]
+				plugins: [ TableUtils, TableSelection, HorizontalLineEditing ]
 			} )
 			.then( newEditor => {
 				editor = newEditor;
@@ -214,6 +215,23 @@ describe( 'InsertRowCommand', () => {
 					[ 0, 0 ]
 				] );
 			} );
+
+			it( 'should insert a row when a widget in the table cell is selected', () => {
+				setData( model, modelTable( [
+					[ '11', '12' ],
+					[ '21', '22' ],
+					[ '31', '[<horizontalLine></horizontalLine>]' ]
+				] ) );
+
+				command.execute();
+
+				assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
+					[ '11', '12' ],
+					[ '21', '22' ],
+					[ '31', '<horizontalLine></horizontalLine>' ],
+					[ '', '' ]
+				] ) );
+			} );
 		} );
 	} );