8
0
Просмотр исходного кода

Refactor InsertColumnCommand and create the `TableUtils#getCellLocation()` method.

Maciej Gołaszewski 7 лет назад
Родитель
Сommit
43624e47ca

+ 17 - 26
packages/ckeditor5-table/src/commands/insertcolumncommand.js

@@ -8,7 +8,6 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import TableWalker from '../tablewalker';
 import { getParentTable } from './utils';
 import TableUtils from '../tableutils';
 
@@ -23,53 +22,45 @@ export default class InsertColumnCommand extends Command {
 	 *
 	 * @param {module:core/editor/editor~Editor} editor Editor on which this command will be used.
 	 * @param {Object} options
-	 * @param {String} [options.location="after"] Where to insert new row - relative to current row. Possible values: "after", "before".
+	 * @param {String} [options.order="after"] The order of insertion relative to a column in which caret is located.
+	 * Possible values: "after" and "before".
 	 */
 	constructor( editor, options = {} ) {
 		super( editor );
 
-		this.direction = options.location || 'after';
+		/**
+		 * The order of insertion relative to a column in which caret is located.
+		 *
+		 * @readonly
+		 * @member {String} module:table/commands/insertcolumncommand~InsertColumnCommand#order
+		 */
+		this.order = options.order || 'after';
 	}
 
 	/**
 	 * @inheritDoc
 	 */
 	refresh() {
-		const model = this.editor.model;
-		const doc = model.document;
+		const selection = this.editor.model.document.selection;
 
-		const tableParent = getParentTable( doc.selection.getFirstPosition() );
+		const tableParent = getParentTable( selection.getFirstPosition() );
 
 		this.isEnabled = !!tableParent;
 	}
 
 	/**
-	 * Executes the command.
-	 *
-	 * @fires execute
+	 * @inheritDoc
 	 */
 	execute() {
 		const editor = this.editor;
-		const model = editor.model;
-		const doc = model.document;
-		const selection = doc.selection;
+		const selection = editor.model.document.selection;
+		const tableUtils = editor.plugins.get( TableUtils );
 
 		const table = getParentTable( selection.getFirstPosition() );
+		const tableCell = selection.getFirstPosition().parent;
 
-		const element = doc.selection.getFirstPosition().parent;
-		const rowIndex = table.getChildIndex( element.parent );
-
-		let columnIndex;
-
-		for ( const tableWalkerValue of new TableWalker( table, { startRow: rowIndex, endRow: rowIndex } ) ) {
-			if ( tableWalkerValue.cell === element ) {
-				columnIndex = tableWalkerValue.column;
-			}
-		}
-
-		const insertAt = this.direction === 'after' ? columnIndex + 1 : columnIndex;
-
-		const tableUtils = editor.plugins.get( TableUtils );
+		const { column } = tableUtils.getCellLocation( tableCell );
+		const insertAt = this.order === 'after' ? column + 1 : column;
 
 		tableUtils.insertColumns( table, { columns: 1, at: insertAt } );
 	}

+ 21 - 0
packages/ckeditor5-table/src/tableutils.js

@@ -18,6 +18,27 @@ import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  * @extends module:core/command~Command
  */
 export default class TableUtils extends Plugin {
+	/**
+	 * Returns table cell location in table.
+	 *
+	 * @param tableCell
+	 * @returns {Object}
+	 */
+	getCellLocation( tableCell ) {
+		const tableRow = tableCell.parent;
+		const table = tableRow.parent;
+
+		const rowIndex = table.getChildIndex( tableRow );
+
+		const tableWalker = new TableWalker( table, { startRow: rowIndex, endRow: rowIndex } );
+
+		for ( const { cell, row, column } of tableWalker ) {
+			if ( cell === tableCell ) {
+				return { row, column };
+			}
+		}
+	}
+
 	insertRows( table, options = {} ) {
 		const model = this.editor.model;
 

+ 3 - 3
packages/ckeditor5-table/tests/commands/insertcolumncommand.js

@@ -70,7 +70,7 @@ describe( 'InsertColumnCommand', () => {
 		return editor.destroy();
 	} );
 
-	describe( 'location=after', () => {
+	describe( 'order=after', () => {
 		beforeEach( () => {
 			command = new InsertColumnCommand( editor );
 		} );
@@ -182,9 +182,9 @@ describe( 'InsertColumnCommand', () => {
 		} );
 	} );
 
-	describe( 'location=before', () => {
+	describe( 'order=before', () => {
 		beforeEach( () => {
-			command = new InsertColumnCommand( editor, { location: 'before' } );
+			command = new InsertColumnCommand( editor, { order: 'before' } );
 		} );
 
 		describe( 'isEnabled', () => {

+ 13 - 0
packages/ckeditor5-table/tests/tableutils.js

@@ -71,6 +71,19 @@ describe( 'TableUtils', () => {
 		return editor.destroy();
 	} );
 
+	describe( 'getCellLocation()', () => {
+		it( 'should return proper table cell location', () => {
+			setData( model, modelTable( [
+				[ { rowspan: 2, colspan: 2, contents: '00[]' }, '02' ],
+				[ '12' ]
+			] ) );
+
+			expect( tableUtils.getCellLocation( root.getNodeByPath( [ 0, 0, 0 ] ) ) ).to.deep.equal( { row: 0, column: 0 } );
+			expect( tableUtils.getCellLocation( root.getNodeByPath( [ 0, 0, 1 ] ) ) ).to.deep.equal( { row: 0, column: 2 } );
+			expect( tableUtils.getCellLocation( root.getNodeByPath( [ 0, 1, 0 ] ) ) ).to.deep.equal( { row: 1, column: 2 } );
+		} );
+	} );
+
 	describe( 'insertRows()', () => {
 		it( 'should insert row in given table at given index', () => {
 			setData( model, modelTable( [