Kaynağa Gözat

Code refactoring.

Aleksander Nowodzinski 5 yıl önce
ebeveyn
işleme
4ffd96cafb

+ 21 - 27
packages/ckeditor5-table/src/tableselection.js

@@ -293,35 +293,29 @@ export default class TableSelection extends Plugin {
 	 */
 	_handleDeleteContent( event, args ) {
 		const [ selection, options ] = args;
+		const model = this.editor.model;
+		const isBackward = !options || options.direction == 'backward';
+		const selectedTableCells = getTableCellsInSelection( selection );
 
-		// This Model#deleteContent() hook supports multiple-cell selections only.
-		// **Note**: It executes Model#deleteContent() itself for each individual cell within
-		// the selection, so this also avoids infinite loops.
-		if ( selection.rangeCount > 1 ) {
-			const model = this.editor.model;
-			const isBackward = !options || options.direction == 'backward';
-			const selectedTableCells = getTableCellsInSelection( selection );
-
-			// There are possible multiple-range selection that have nothing to do with tables.
-			// Let's filter them out and focus on table cells only.
-			if ( selectedTableCells.length ) {
-				event.stop();
-
-				model.change( writer => {
-					const tableCellToSelect = selectedTableCells[ isBackward ? selectedTableCells.length - 1 : 0 ];
-
-					clearTableCellsContents( model, selectedTableCells );
-
-					// The insertContent() helper passes the actual DocumentSelection,
-					// while the deleteContent() helper always operates on the abstract clones.
-					if ( selection.is( 'documentSelection' ) ) {
-						writer.setSelection( tableCellToSelect, 'in' );
-					} else {
-						selection.setTo( tableCellToSelect, 'in' );
-					}
-				} );
-			}
+		if ( !selectedTableCells.length ) {
+			return;
 		}
+
+		event.stop();
+
+		model.change( writer => {
+			const tableCellToSelect = selectedTableCells[ isBackward ? selectedTableCells.length - 1 : 0 ];
+
+			clearTableCellsContents( model, selectedTableCells );
+
+			// The insertContent() helper passes the actual DocumentSelection,
+			// while the deleteContent() helper always operates on the abstract clones.
+			if ( selection.is( 'documentSelection' ) ) {
+				writer.setSelection( tableCellToSelect, 'in' );
+			} else {
+				selection.setTo( tableCellToSelect, 'in' );
+			}
+		} );
 	}
 }
 

+ 6 - 4
packages/ckeditor5-table/src/tableselection/utils.js

@@ -7,6 +7,8 @@
  * @module table/tableselection/utils
  */
 
+import { getRangeContainedElement } from '../commands/utils';
+
 /**
  * Clears contents of the passed table cells.
  *
@@ -38,10 +40,10 @@ export function getTableCellsInSelection( selection ) {
 	const cells = [];
 
 	for ( const range of selection.getRanges() ) {
-		for ( const item of range.getItems() ) {
-			if ( item.is( 'tableCell' ) ) {
-				cells.push( item );
-			}
+		const element = getRangeContainedElement( range );
+
+		if ( element.is( 'tableCell' ) ) {
+			cells.push( element );
 		}
 	}