Skip to content

Commit

Permalink
Merge pull request #7615 from ckeditor/i/7486
Browse files Browse the repository at this point in the history
Fix (table): Pasting table into a table should not set multi-cell selection if TableSelection plugin is disabled. Closes #7486.
  • Loading branch information
jodator authored Jul 14, 2020
2 parents ca82788 + 2469807 commit e50a4e1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/ckeditor5-table/src/tableclipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,18 @@ export default class TableClipboard extends Plugin {
// Content table to which we insert a pasted table.
const selectedTable = selectedTableCells[ 0 ].findAncestor( 'table' );

replaceSelectedCellsWithPasted( pastedTable, pastedDimensions, selectedTable, selection, writer );
const cellsToSelect = replaceSelectedCellsWithPasted( pastedTable, pastedDimensions, selectedTable, selection, writer );

if ( this.editor.plugins.get( 'TableSelection' ).isEnabled ) {
// Selection ranges must be sorted because the first and last selection ranges are considered
// as anchor/focus cell ranges for multi-cell selection.
const selectionRanges = sortRanges( cellsToSelect.map( cell => writer.createRangeOn( cell ) ) );

writer.setSelection( selectionRanges );
} else {
// Set selection inside first cell if multi-cell selection is disabled.
writer.setSelection( cellsToSelect[ 0 ], 0 );
}
} );
}
}
Expand Down Expand Up @@ -248,6 +259,7 @@ function prepareTableForPasting( selectedTableCells, pastedDimensions, writer, t
// @param {Number} selection.lastColumn
// @param {Number} selection.lastRow
// @param {module:engine/model/writer~Writer} writer
// @returns {Array.<module:engine/model/element~Element>}
function replaceSelectedCellsWithPasted( pastedTable, pastedDimensions, selectedTable, selection, writer ) {
const { width: pastedWidth, height: pastedHeight } = pastedDimensions;

Expand Down Expand Up @@ -334,11 +346,7 @@ function replaceSelectedCellsWithPasted( pastedTable, pastedDimensions, selected
cellsToSelect.push( ...newCells );
}

// Selection ranges must be sorted because the first and last selection ranges are considered
// as anchor/focus cell ranges for multi-cell selection.
const selectionRanges = sortRanges( cellsToSelect.map( cell => writer.createRangeOn( cell ) ) );

writer.setSelection( selectionRanges );
return cellsToSelect;
}

// Expand table (in place) to expected size.
Expand Down
15 changes: 15 additions & 0 deletions packages/ckeditor5-table/tests/tableclipboard-paste.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,21 @@ describe( 'table clipboard', () => {
[ '', '', 'ba', 'bb' ]
] ) );
} );

it( 'should not set multi-cell selection if TableSelection plugin is disabled', () => {
editor.plugins.get( 'TableSelection' ).forceDisabled();

pasteTable( [
[ 'aa', 'ab' ],
[ 'ba', 'bb' ]
] );

assertEqualMarkup( getModelData( model ), modelTable( [
[ '[]aa', 'ab', '02' ],
[ 'ba', 'bb', '12' ],
[ '20', '21', '22' ]
] ) );
} );
} );

describe( 'with spanned cells', () => {
Expand Down

0 comments on commit e50a4e1

Please sign in to comment.