Przeglądaj źródła

Refactor InsertRowCommand.

Maciej Gołaszewski 7 lat temu
rodzic
commit
1cd12b9674

+ 16 - 15
packages/ckeditor5-table/src/commands/insertrowcommand.js

@@ -22,22 +22,28 @@ export default class InsertRowCommand extends Command {
 	 *
 	 * @param {module:core/editor/editor~Editor} editor Editor on which this command will be used.
 	 * @param {Object} options
-	 * @param {String} [options.location="below"] Where to insert new row - relative to current row. Possible values: "above", "below".
+	 * @param {String} [options.order="below"] The order of insertion relative to a row in which caret is located.
+	 * Possible values: "above" and "below".
 	 */
 	constructor( editor, options = {} ) {
 		super( editor );
 
-		this.direction = options.location || 'below';
+		/**
+		 * The order of insertion relative to a row in which caret is located.
+		 *
+		 * @readonly
+		 * @member {String} module:table/commands/insertrowcommand~InsertRowCommand#order
+		 */
+		this.order = options.order || 'below';
 	}
 
 	/**
 	 * @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;
 	}
@@ -49,19 +55,14 @@ export default class InsertRowCommand extends Command {
 	 */
 	execute() {
 		const editor = this.editor;
-		const model = editor.model;
-		const doc = model.document;
-		const selection = doc.selection;
-
-		const element = doc.selection.getFirstPosition().parent;
-
-		const table = getParentTable( selection.getFirstPosition() );
-
+		const selection = editor.model.document.selection;
 		const tableUtils = editor.plugins.get( TableUtils );
 
-		const rowIndex = table.getChildIndex( element.parent );
+		const tableCell = selection.getFirstPosition().parent;
+		const table = getParentTable( selection.getFirstPosition() );
 
-		const insertAt = this.direction === 'below' ? rowIndex + 1 : rowIndex;
+		const row = table.getChildIndex( tableCell.parent );
+		const insertAt = this.order === 'below' ? row + 1 : row;
 
 		tableUtils.insertRows( table, { rows: 1, at: insertAt } );
 	}

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

@@ -70,7 +70,7 @@ describe( 'InsertRowCommand', () => {
 		return editor.destroy();
 	} );
 
-	describe( 'below', () => {
+	describe( 'order=below', () => {
 		beforeEach( () => {
 			command = new InsertRowCommand( editor );
 		} );
@@ -205,9 +205,9 @@ describe( 'InsertRowCommand', () => {
 		} );
 	} );
 
-	describe( 'location=above', () => {
+	describe( 'order=above', () => {
 		beforeEach( () => {
-			command = new InsertRowCommand( editor, { location: 'above' } );
+			command = new InsertRowCommand( editor, { order: 'above' } );
 		} );
 
 		describe( 'isEnabled', () => {