Skip to content

Commit

Permalink
Insert row above/below refactored to use utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
niegowski committed May 7, 2020
1 parent 81694cd commit 542c77b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
14 changes: 5 additions & 9 deletions packages/ckeditor5-table/src/commands/insertcolumncommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import Command from '@ckeditor/ckeditor5-core/src/command';
import { findAncestor } from './utils';
import { getColumnIndexes, getSelectionAffectedTableCells } from '../utils';

/**
* The insert column command.
Expand Down Expand Up @@ -72,16 +73,11 @@ export default class InsertColumnCommand extends Command {
const tableUtils = editor.plugins.get( 'TableUtils' );
const insertBefore = this.order === 'left';

const referencePosition = insertBefore ? selection.getFirstPosition() : selection.getLastPosition();
const referenceRange = insertBefore ? selection.getFirstRange() : selection.getLastRange();
const affectedTableCells = getSelectionAffectedTableCells( selection );
const columnIndexes = getColumnIndexes( affectedTableCells );

const containedElement = referenceRange.getContainedElement();
const isTableCell = containedElement && containedElement.is( 'tableCell' );

const tableCell = isTableCell ? containedElement : findAncestor( 'tableCell', referencePosition );
const table = findAncestor( 'table', tableCell );

const { column } = tableUtils.getCellLocation( tableCell );
const column = insertBefore ? columnIndexes.first : columnIndexes.last;
const table = findAncestor( 'table', affectedTableCells[ 0 ] );

tableUtils.insertColumns( table, { columns: 1, at: insertBefore ? column : column + 1 } );
}
Expand Down
17 changes: 6 additions & 11 deletions packages/ckeditor5-table/src/commands/insertrowcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import Command from '@ckeditor/ckeditor5-core/src/command';
import { findAncestor } from './utils';
import { getRowIndexes, getSelectionAffectedTableCells } from '../utils';

/**
* The insert row command.
Expand Down Expand Up @@ -71,18 +72,12 @@ export default class InsertRowCommand extends Command {
const tableUtils = editor.plugins.get( 'TableUtils' );
const insertAbove = this.order === 'above';

const referencePosition = insertAbove ? selection.getFirstPosition() : selection.getLastPosition();
const referenceRange = insertAbove ? selection.getFirstRange() : selection.getLastRange();
const affectedTableCells = getSelectionAffectedTableCells( selection );
const rowIndexes = getRowIndexes( affectedTableCells );

const containedElement = referenceRange.getContainedElement();
const isTableCell = containedElement && containedElement.is( 'tableCell' );
const tableCell = isTableCell ? containedElement : findAncestor( 'tableCell', referencePosition );
const row = insertAbove ? rowIndexes.first : rowIndexes.last;
const table = findAncestor( 'table', affectedTableCells[ 0 ] );

const tableRow = tableCell.parent;
const table = tableRow.parent;

const row = table.getChildIndex( tableRow );

tableUtils.insertRows( table, { rows: 1, at: this.order === 'below' ? row + 1 : row } );
tableUtils.insertRows( table, { rows: 1, at: insertAbove ? row : row + 1 } );
}
}

0 comments on commit 542c77b

Please sign in to comment.