@@ -11,7 +11,7 @@ import Command from '@ckeditor/ckeditor5-core/src/command';
1111import Position from '@ckeditor/ckeditor5-engine/src/model/position' ;
1212import Range from '@ckeditor/ckeditor5-engine/src/model/range' ;
1313import TableWalker from '../tablewalker' ;
14- import { updateNumericAttribute } from './utils' ;
14+ import { findAncestor , updateNumericAttribute } from './utils' ;
1515import TableUtils from '../tableutils' ;
1616
1717/**
@@ -83,7 +83,7 @@ export default class MergeCellCommand extends Command {
8383 execute ( ) {
8484 const model = this . editor . model ;
8585 const doc = model . document ;
86- const tableCell = doc . selection . getFirstPosition ( ) . parent ;
86+ const tableCell = findAncestor ( 'tableCell' , doc . selection . getFirstPosition ( ) ) ;
8787 const cellToMerge = this . value ;
8888 const direction = this . direction ;
8989
@@ -97,9 +97,7 @@ export default class MergeCellCommand extends Command {
9797 // Cache the parent of cell to remove for later check.
9898 const removedTableCellRow = cellToRemove . parent ;
9999
100- // Remove table cell and merge it contents with merged cell.
101- writer . move ( Range . createIn ( cellToRemove ) , Position . createAt ( cellToExpand , 'end' ) ) ;
102- writer . remove ( cellToRemove ) ;
100+ mergeTableCells ( cellToRemove , cellToExpand , writer ) ;
103101
104102 const spanAttribute = this . isHorizontal ? 'colspan' : 'rowspan' ;
105103 const cellSpan = parseInt ( tableCell . getAttribute ( spanAttribute ) || 1 ) ;
@@ -125,26 +123,26 @@ export default class MergeCellCommand extends Command {
125123 _getMergeableCell ( ) {
126124 const model = this . editor . model ;
127125 const doc = model . document ;
128- const element = doc . selection . getFirstPosition ( ) . parent ;
126+ const tableCell = findAncestor ( 'tableCell' , doc . selection . getFirstPosition ( ) ) ;
129127
130- if ( ! element . is ( ' tableCell' ) ) {
128+ if ( ! tableCell ) {
131129 return ;
132130 }
133131
134132 const tableUtils = this . editor . plugins . get ( TableUtils ) ;
135133
136134 // First get the cell on proper direction.
137135 const cellToMerge = this . isHorizontal ?
138- getHorizontalCell ( element , this . direction , tableUtils ) :
139- getVerticalCell ( element , this . direction ) ;
136+ getHorizontalCell ( tableCell , this . direction , tableUtils ) :
137+ getVerticalCell ( tableCell , this . direction ) ;
140138
141139 if ( ! cellToMerge ) {
142140 return ;
143141 }
144142
145143 // If found check if the span perpendicular to merge direction is equal on both cells.
146144 const spanAttribute = this . isHorizontal ? 'rowspan' : 'colspan' ;
147- const span = parseInt ( element . getAttribute ( spanAttribute ) || 1 ) ;
145+ const span = parseInt ( tableCell . getAttribute ( spanAttribute ) || 1 ) ;
148146
149147 const cellToMergeSpan = parseInt ( cellToMerge . getAttribute ( spanAttribute ) || 1 ) ;
150148
@@ -254,3 +252,31 @@ function removeEmptyRow( removedTableCellRow, writer ) {
254252
255253 writer . remove ( removedTableCellRow ) ;
256254}
255+
256+ // Merges two table cells - will ensure that after merging cells with empty paragraph the result table cell will only have one paragraph.
257+ // If one of the merged table cell is empty the merged table cell will have contents of the non-empty table cell.
258+ // If both are empty the merged table cell will have only one empty paragraph.
259+ //
260+ // @param {module:engine/model/element~Element } cellToRemove
261+ // @param {module:engine/model/element~Element } cellToExpand
262+ // @param {module:engine/model/writer~Writer } writer
263+ function mergeTableCells ( cellToRemove , cellToExpand , writer ) {
264+ if ( ! isEmpty ( cellToRemove ) ) {
265+ if ( isEmpty ( cellToExpand ) ) {
266+ writer . remove ( Range . createIn ( cellToExpand ) ) ;
267+ }
268+
269+ writer . move ( Range . createIn ( cellToRemove ) , Position . createAt ( cellToExpand , 'end' ) ) ;
270+ }
271+
272+ // Remove merged table cell.
273+ writer . remove ( cellToRemove ) ;
274+ }
275+
276+ // Checks if passed table cell contains empty paragraph.
277+ //
278+ // @param {module:engine/model/element~Element } tableCell
279+ // @returns {Boolean }
280+ function isEmpty ( tableCell ) {
281+ return tableCell . childCount == 1 && tableCell . getChild ( 0 ) . is ( 'paragraph' ) && tableCell . getChild ( 0 ) . isEmpty ;
282+ }
0 commit comments