浏览代码

Fix: InsertRowCommand should work with block content (#value only).

Maciej Gołaszewski 7 年之前
父节点
当前提交
c7ba2a3c5d

+ 7 - 7
packages/ckeditor5-table/src/commands/mergecellcommand.js

@@ -11,7 +11,7 @@ import Command from '@ckeditor/ckeditor5-core/src/command';
 import Position from '@ckeditor/ckeditor5-engine/src/model/position';
 import Range from '@ckeditor/ckeditor5-engine/src/model/range';
 import TableWalker from '../tablewalker';
-import { updateNumericAttribute } from './utils';
+import { getParentElement, updateNumericAttribute } from './utils';
 import TableUtils from '../tableutils';
 
 /**
@@ -83,7 +83,7 @@ export default class MergeCellCommand extends Command {
 	execute() {
 		const model = this.editor.model;
 		const doc = model.document;
-		const tableCell = doc.selection.getFirstPosition().parent;
+		const tableCell = getParentElement( 'tableCell', doc.selection.getFirstPosition() );
 		const cellToMerge = this.value;
 		const direction = this.direction;
 
@@ -125,9 +125,9 @@ export default class MergeCellCommand extends Command {
 	_getMergeableCell() {
 		const model = this.editor.model;
 		const doc = model.document;
-		const element = doc.selection.getFirstPosition().parent;
+		const tableCell = getParentElement( 'tableCell', doc.selection.getFirstPosition() );
 
-		if ( !element.is( 'tableCell' ) ) {
+		if ( !tableCell ) {
 			return;
 		}
 
@@ -135,8 +135,8 @@ export default class MergeCellCommand extends Command {
 
 		// First get the cell on proper direction.
 		const cellToMerge = this.isHorizontal ?
-			getHorizontalCell( element, this.direction, tableUtils ) :
-			getVerticalCell( element, this.direction );
+			getHorizontalCell( tableCell, this.direction, tableUtils ) :
+			getVerticalCell( tableCell, this.direction );
 
 		if ( !cellToMerge ) {
 			return;
@@ -144,7 +144,7 @@ export default class MergeCellCommand extends Command {
 
 		// If found check if the span perpendicular to merge direction is equal on both cells.
 		const spanAttribute = this.isHorizontal ? 'rowspan' : 'colspan';
-		const span = parseInt( element.getAttribute( spanAttribute ) || 1 );
+		const span = parseInt( tableCell.getAttribute( spanAttribute ) || 1 );
 
 		const cellToMergeSpan = parseInt( cellToMerge.getAttribute( spanAttribute ) || 1 );
 

+ 50 - 12
packages/ckeditor5-table/tests/commands/mergecellcommand.js

@@ -51,7 +51,9 @@ describe( 'MergeCellCommand', () => {
 					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() );
@@ -139,7 +141,7 @@ describe( 'MergeCellCommand', () => {
 			} );
 
 			it( 'should be false if not in a cell', () => {
-				setData( model, '<p>11[]</p>' );
+				setData( model, '<paragraph>11[]</paragraph>' );
 
 				expect( command.isEnabled ).to.be.false;
 			} );
@@ -154,6 +156,14 @@ describe( 'MergeCellCommand', () => {
 				expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
 			} );
 
+			it( 'should be set to mergeable sibling if in cell that has sibling on the right (selection in block content)', () => {
+				setData( model, modelTable( [
+					[ '00', '<paragraph>[]01</paragraph>', '02' ]
+				] ) );
+
+				expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 2 ] ) );
+			} );
+
 			it( 'should be undefined if last cell of a row', () => {
 				setData( model, modelTable( [
 					[ '00', '01[]' ]
@@ -179,14 +189,14 @@ describe( 'MergeCellCommand', () => {
 			} );
 
 			it( 'should be undefined if not in a cell', () => {
-				setData( model, '<p>11[]</p>' );
+				setData( model, '<paragraph>11[]</paragraph>' );
 
 				expect( command.value ).to.be.undefined;
 			} );
 		} );
 
 		describe( 'execute()', () => {
-			it( 'should merge table cells ', () => {
+			it( 'should merge table cells', () => {
 				setData( model, modelTable( [
 					[ '[]00', '01' ]
 				] ) );
@@ -257,7 +267,7 @@ describe( 'MergeCellCommand', () => {
 			} );
 
 			it( 'should be false if not in a cell', () => {
-				setData( model, '<p>11[]</p>' );
+				setData( model, '<paragraph>11[]</paragraph>' );
 
 				expect( command.isEnabled ).to.be.false;
 			} );
@@ -272,6 +282,14 @@ describe( 'MergeCellCommand', () => {
 				expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
 			} );
 
+			it( 'should be set to mergeable sibling if in cell that has sibling on the left (selection in block content)', () => {
+				setData( model, modelTable( [
+					[ '00', '<paragraph>01[]</paragraph>', '02' ]
+				] ) );
+
+				expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
+			} );
+
 			it( 'should be undefined if first cell of a row', () => {
 				setData( model, modelTable( [
 					[ '00[]', '01' ]
@@ -297,14 +315,14 @@ describe( 'MergeCellCommand', () => {
 			} );
 
 			it( 'should be undefined if not in a cell', () => {
-				setData( model, '<p>11[]</p>' );
+				setData( model, '<paragraph>11[]</paragraph>' );
 
 				expect( command.value ).to.be.undefined;
 			} );
 		} );
 
 		describe( 'execute()', () => {
-			it( 'should merge table cells ', () => {
+			it( 'should merge table cells', () => {
 				setData( model, modelTable( [
 					[ '00', '[]01' ]
 				] ) );
@@ -361,7 +379,7 @@ describe( 'MergeCellCommand', () => {
 			} );
 
 			it( 'should be false if not in a cell', () => {
-				setData( model, '<p>11[]</p>' );
+				setData( model, '<paragraph>11[]</paragraph>' );
 
 				expect( command.isEnabled ).to.be.false;
 			} );
@@ -386,6 +404,16 @@ describe( 'MergeCellCommand', () => {
 				expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 1 ] ) );
 			} );
 
+			it( 'should be set to mergeable cell (selection in block content)', () => {
+				setData( model, modelTable( [
+					[ '00' ],
+					[ '<paragraph>10[]</paragraph>' ],
+					[ '20' ]
+				] ) );
+
+				expect( command.value ).to.equal( root.getNodeByPath( [ 0, 2, 0 ] ) );
+			} );
+
 			it( 'should be undefined if in last row', () => {
 				setData( model, modelTable( [
 					[ '00', '01' ],
@@ -414,14 +442,14 @@ describe( 'MergeCellCommand', () => {
 			} );
 
 			it( 'should be undefined if not in a cell', () => {
-				setData( model, '<p>11[]</p>' );
+				setData( model, '<paragraph>11[]</paragraph>' );
 
 				expect( command.value ).to.be.undefined;
 			} );
 		} );
 
 		describe( 'execute()', () => {
-			it( 'should merge table cells ', () => {
+			it( 'should merge table cells', () => {
 				setData( model, modelTable( [
 					[ '00', '01[]' ],
 					[ '10', '11' ]
@@ -514,7 +542,7 @@ describe( 'MergeCellCommand', () => {
 			} );
 
 			it( 'should be false if not in a cell', () => {
-				setData( model, '<p>11[]</p>' );
+				setData( model, '<paragraph>11[]</paragraph>' );
 
 				expect( command.isEnabled ).to.be.false;
 			} );
@@ -539,6 +567,16 @@ describe( 'MergeCellCommand', () => {
 				expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
 			} );
 
+			it( 'should be set to mergeable cell (selection in block content)', () => {
+				setData( model, modelTable( [
+					[ '00' ],
+					[ '<paragraph>10[]</paragraph>' ],
+					[ '20' ]
+				] ) );
+
+				expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
+			} );
+
 			it( 'should be undefined if in first row', () => {
 				setData( model, modelTable( [
 					[ '00[]', '01' ],
@@ -578,7 +616,7 @@ describe( 'MergeCellCommand', () => {
 			} );
 
 			it( 'should be undefined if not in a cell', () => {
-				setData( model, '<p>11[]</p>' );
+				setData( model, '<paragraph>11[]</paragraph>' );
 
 				expect( command.value ).to.be.undefined;
 			} );