Procházet zdrojové kódy

Internal: Reused recently added `getSelectionAffectedTableCells()` function.

Marek Lewandowski před 5 roky
rodič
revize
c708b41475

+ 5 - 28
packages/ckeditor5-table/src/commands/removecolumncommand.js

@@ -10,7 +10,8 @@
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
 import TableWalker from '../tablewalker';
-import { findAncestor, updateNumericAttribute } from './utils';
+import { updateNumericAttribute } from './utils';
+import { getSelectionAffectedTableCells } from '../utils';
 
 /**
  * The remove column command.
@@ -28,7 +29,8 @@ export default class RemoveColumnCommand extends Command {
 	 * @inheritDoc
 	 */
 	refresh() {
-		const firstCell = this._getReferenceCells().next().value;
+		const selectedCells = getSelectionAffectedTableCells( this.editor.model.document.selection );
+		const firstCell = selectedCells[ 0 ];
 
 		if ( firstCell ) {
 			const table = firstCell.parent.parent;
@@ -36,7 +38,6 @@ export default class RemoveColumnCommand extends Command {
 			const tableColumnCount = table && tableUtils.getColumns( table );
 
 			const tableMap = [ ...new TableWalker( table ) ];
-			const selectedCells = Array.from( this._getReferenceCells() );
 			const columnIndexes = tableMap.filter( entry => selectedCells.includes( entry.cell ) ).map( el => el.column );
 
 			this.isEnabled = Math.max.apply( null, columnIndexes ) - Math.min.apply( null, columnIndexes ) < ( tableColumnCount - 1 );
@@ -51,7 +52,7 @@ export default class RemoveColumnCommand extends Command {
 	execute() {
 		const model = this.editor.model;
 
-		const referenceCells = Array.from( this._getReferenceCells() );
+		const referenceCells = getSelectionAffectedTableCells( this.editor.model.document.selection );
 		let firstCell = referenceCells[ 0 ];
 		let lastCell = referenceCells[ referenceCells.length - 1 ];
 		const tableRow = firstCell.parent;
@@ -99,30 +100,6 @@ export default class RemoveColumnCommand extends Command {
 			writer.setSelection( writer.createPositionAt( cellsToFocus.reverse().filter( item => item != null )[ 0 ], 0 ) );
 		} );
 	}
-
-	/**
-	 * Returns cells that are selected and are a reference to removing rows.
-	 *
-	 * @private
-	 * @returns {Iterable.<module:engine/model/element~Element>} Generates `tableCell` elements.
-	 */
-	* _getReferenceCells() {
-		const plugins = this.editor.plugins;
-		if ( plugins.has( 'TableSelection' ) ) {
-			const selectedCells = plugins.get( 'TableSelection' ).getSelectedTableCells();
-
-			if ( selectedCells ) {
-				for ( const cell of selectedCells ) {
-					yield cell;
-				}
-
-				return;
-			}
-		}
-
-		const selection = this.editor.model.document.selection;
-		yield findAncestor( 'tableCell', selection.getFirstPosition() );
-	}
 }
 
 // Updates heading columns attribute if removing a row from head section.

+ 5 - 28
packages/ckeditor5-table/src/commands/removerowcommand.js

@@ -10,7 +10,8 @@
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
 import TableWalker from '../tablewalker';
-import { findAncestor, updateNumericAttribute } from './utils';
+import { updateNumericAttribute } from './utils';
+import { getSelectionAffectedTableCells } from '../utils';
 
 /**
  * The remove row command.
@@ -28,7 +29,8 @@ export default class RemoveRowCommand extends Command {
 	 * @inheritDoc
 	 */
 	refresh() {
-		const firstCell = this._getReferenceCells().next().value;
+		const selectedCells = getSelectionAffectedTableCells( this.editor.model.document.selection );
+		const firstCell = selectedCells[ 0 ];
 
 		if ( firstCell ) {
 			const table = firstCell.parent.parent;
@@ -36,7 +38,6 @@ export default class RemoveRowCommand extends Command {
 			const tableRowCount = table && tableUtils.getRows( table );
 
 			const tableMap = [ ...new TableWalker( table ) ];
-			const selectedCells = Array.from( this._getReferenceCells() );
 			const rowIndexes = tableMap.filter( entry => selectedCells.includes( entry.cell ) ).map( el => el.row );
 
 			this.isEnabled = Math.max.apply( null, rowIndexes ) - Math.min.apply( null, rowIndexes ) < ( tableRowCount - 1 );
@@ -49,7 +50,7 @@ export default class RemoveRowCommand extends Command {
 	 * @inheritDoc
 	 */
 	execute() {
-		const referenceCells = Array.from( this._getReferenceCells() );
+		const referenceCells = getSelectionAffectedTableCells( this.editor.model.document.selection );
 		const removedRowIndexes = {
 			first: referenceCells[ 0 ].parent.index,
 			last: referenceCells[ referenceCells.length - 1 ].parent.index
@@ -140,30 +141,6 @@ export default class RemoveRowCommand extends Command {
 
 		writer.remove( tableRow );
 	}
-
-	/**
-	 * Returns cells that are selected and are a reference to removing rows.
-	 *
-	 * @private
-	 * @returns {Iterable.<module:engine/model/element~Element>} Generates `tableCell` elements.
-	 */
-	* _getReferenceCells() {
-		const plugins = this.editor.plugins;
-		if ( plugins.has( 'TableSelection' ) ) {
-			const selectedCells = plugins.get( 'TableSelection' ).getSelectedTableCells();
-
-			if ( selectedCells ) {
-				for ( const cell of selectedCells ) {
-					yield cell;
-				}
-
-				return;
-			}
-		}
-
-		const selection = this.editor.model.document.selection;
-		yield findAncestor( 'tableCell', selection.getFirstPosition() );
-	}
 }
 
 // Returns a cell that should be focused before removing the row, belonging to the same column as the currently focused cell.