Browse Source

Fix: InsertRowCommand should work with block content.

Maciej Gołaszewski 7 years ago
parent
commit
432533fd5d

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

@@ -8,7 +8,7 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import { getParentTable } from './utils';
+import { getParentElement, getParentTable } from './utils';
 import TableUtils from '../tableutils';
 
 /**
@@ -71,10 +71,11 @@ export default class InsertRowCommand extends Command {
 		const selection = editor.model.document.selection;
 		const tableUtils = editor.plugins.get( TableUtils );
 
-		const tableCell = selection.getFirstPosition().parent;
-		const table = getParentTable( selection.getFirstPosition() );
+		const tableCell = getParentElement( 'tableCell', selection.getFirstPosition() );
+		const tableRow = tableCell.parent;
+		const table = tableRow.parent;
 
-		const row = table.getChildIndex( tableCell.parent );
+		const row = table.getChildIndex( tableRow );
 		const insertAt = this.order === 'below' ? row + 1 : row;
 
 		tableUtils.insertRows( table, { rows: 1, at: insertAt } );

+ 39 - 3
packages/ckeditor5-table/tests/commands/insertrowcommand.js

@@ -50,7 +50,9 @@ describe( 'InsertRowCommand', () => {
 					isLimit: true
 				} );
 
-				model.schema.register( 'p', { inheritAllFrom: '$block' } );
+				schema.extend( '$block', { allowIn: 'tableCell' } );
+
+				model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
 
 				// Table conversion.
 				conversion.for( 'upcast' ).add( upcastTable() );
@@ -88,7 +90,7 @@ describe( 'InsertRowCommand', () => {
 
 		describe( 'isEnabled', () => {
 			it( 'should be false if wrong node', () => {
-				setData( model, '<p>foo[]</p>' );
+				setData( model, '<paragraph>foo[]</paragraph>' );
 				expect( command.isEnabled ).to.be.false;
 			} );
 
@@ -114,6 +116,23 @@ describe( 'InsertRowCommand', () => {
 				] ) );
 			} );
 
+			it( 'should insert row after current position (selection in block content)', () => {
+				setData( model, modelTable( [
+					[ '00' ],
+					[ '<paragraph>[]10</paragraph>' ],
+					[ '20' ]
+				] ) );
+
+				command.execute();
+
+				expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+					[ '00' ],
+					[ '<paragraph>[]10</paragraph>' ],
+					[ '' ],
+					[ '20' ]
+				] ) );
+			} );
+
 			it( 'should update table heading rows attribute when inserting row in headings section', () => {
 				setData( model, modelTable( [
 					[ '00[]', '01' ],
@@ -223,7 +242,7 @@ describe( 'InsertRowCommand', () => {
 
 		describe( 'isEnabled', () => {
 			it( 'should be false if wrong node', () => {
-				setData( model, '<p>foo[]</p>' );
+				setData( model, '<paragraph>foo[]</paragraph>' );
 				expect( command.isEnabled ).to.be.false;
 			} );
 
@@ -234,6 +253,23 @@ describe( 'InsertRowCommand', () => {
 		} );
 
 		describe( 'execute()', () => {
+			it( 'should insert row before current position (selection in block content)', () => {
+				setData( model, modelTable( [
+					[ '00' ],
+					[ '<paragraph>[]10</paragraph>' ],
+					[ '20' ]
+				] ) );
+
+				command.execute();
+
+				expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
+					[ '00' ],
+					[ '' ],
+					[ '<paragraph>[]10</paragraph>' ],
+					[ '20' ]
+				] ) );
+			} );
+
 			it( 'should insert row at the beginning of a table', () => {
 				setData( model, modelTable( [
 					[ '00[]', '01' ],