Skip to content

Commit

Permalink
Removed findAncestor() table helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
niegowski committed Jul 9, 2020
1 parent 9a10205 commit aa535c1
Show file tree
Hide file tree
Showing 23 changed files with 48 additions and 97 deletions.
5 changes: 2 additions & 3 deletions packages/ckeditor5-table/src/commands/insertcolumncommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

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

/**
* The insert column command.
Expand Down Expand Up @@ -54,7 +53,7 @@ export default class InsertColumnCommand extends Command {
refresh() {
const selection = this.editor.model.document.selection;

const tableParent = findAncestor( 'table', selection.getFirstPosition() );
const tableParent = selection.getFirstPosition().findAncestor( 'table' );

this.isEnabled = !!tableParent;
}
Expand All @@ -77,7 +76,7 @@ export default class InsertColumnCommand extends Command {
const columnIndexes = getColumnIndexes( affectedTableCells );

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

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

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

/**
* The insert row command.
Expand Down Expand Up @@ -54,7 +53,7 @@ export default class InsertRowCommand extends Command {
refresh() {
const selection = this.editor.model.document.selection;

const tableParent = findAncestor( 'table', selection.getFirstPosition() );
const tableParent = selection.getFirstPosition().findAncestor( 'table' );

this.isEnabled = !!tableParent;
}
Expand All @@ -76,7 +75,7 @@ export default class InsertRowCommand extends Command {
const rowIndexes = getRowIndexes( affectedTableCells );

const row = insertAbove ? rowIndexes.first : rowIndexes.last;
const table = findAncestor( 'table', affectedTableCells[ 0 ] );
const table = affectedTableCells[ 0 ].findAncestor( 'table' );

tableUtils.insertRows( table, { at: insertAbove ? row : row + 1, copyStructureFromAbove: !insertAbove } );
}
Expand Down
4 changes: 2 additions & 2 deletions packages/ckeditor5-table/src/commands/mergecellcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import Command from '@ckeditor/ckeditor5-core/src/command';
import TableWalker from '../tablewalker';
import { getTableCellsContainingSelection } from '../utils/selection';
import { findAncestor, isHeadingColumnCell } from '../utils/common';
import { isHeadingColumnCell } from '../utils/common';
import { removeEmptyRowsColumns } from '../utils/structure';

/**
Expand Down Expand Up @@ -106,7 +106,7 @@ export default class MergeCellCommand extends Command {
writer.setSelection( writer.createRangeIn( cellToExpand ) );

const tableUtils = this.editor.plugins.get( 'TableUtils' );
const table = findAncestor( 'table', removedTableCellRow );
const table = removedTableCellRow.findAncestor( 'table' );

// Remove empty rows and columns after merging.
removeEmptyRowsColumns( table, tableUtils, writer.batch );
Expand Down
4 changes: 2 additions & 2 deletions packages/ckeditor5-table/src/commands/mergecellscommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import Command from '@ckeditor/ckeditor5-core/src/command';
import TableUtils from '../tableutils';
import { getSelectedTableCells, isSelectionRectangular } from '../utils/selection';
import { findAncestor, updateNumericAttribute } from '../utils/common';
import { updateNumericAttribute } from '../utils/common';
import { removeEmptyRowsColumns } from '../utils/structure';

/**
Expand Down Expand Up @@ -57,7 +57,7 @@ export default class MergeCellsCommand extends Command {
mergeTableCells( tableCell, firstTableCell, writer );
}

const table = findAncestor( 'table', firstTableCell );
const table = firstTableCell.findAncestor( 'table' );

// Remove rows and columns that become empty (have no anchored cells).
removeEmptyRowsColumns( table, tableUtils, writer.batch );
Expand Down
3 changes: 1 addition & 2 deletions packages/ckeditor5-table/src/commands/removecolumncommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import Command from '@ckeditor/ckeditor5-core/src/command';

import TableWalker from '../tablewalker';
import { getColumnIndexes, getSelectionAffectedTableCells } from '../utils/selection';
import { findAncestor } from '../utils/common';

/**
* The remove column command.
Expand All @@ -33,7 +32,7 @@ export default class RemoveColumnCommand extends Command {
const firstCell = selectedCells[ 0 ];

if ( firstCell ) {
const table = findAncestor( 'table', firstCell );
const table = firstCell.findAncestor( 'table' );
const tableColumnCount = this.editor.plugins.get( 'TableUtils' ).getColumns( table );

const { first, last } = getColumnIndexes( selectedCells );
Expand Down
5 changes: 2 additions & 3 deletions packages/ckeditor5-table/src/commands/removerowcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import Command from '@ckeditor/ckeditor5-core/src/command';

import { getRowIndexes, getSelectionAffectedTableCells } from '../utils/selection';
import { findAncestor } from '../utils/common';

/**
* The remove row command.
Expand All @@ -32,7 +31,7 @@ export default class RemoveRowCommand extends Command {
const firstCell = selectedCells[ 0 ];

if ( firstCell ) {
const table = findAncestor( 'table', firstCell );
const table = firstCell.findAncestor( 'table' );
const tableRowCount = this.editor.plugins.get( 'TableUtils' ).getRows( table );
const lastRowIndex = tableRowCount - 1;

Expand All @@ -56,7 +55,7 @@ export default class RemoveRowCommand extends Command {
const removedRowIndexes = getRowIndexes( referenceCells );

const firstCell = referenceCells[ 0 ];
const table = findAncestor( 'table', firstCell );
const table = firstCell.findAncestor( 'table' );

const columnIndexToFocus = this.editor.plugins.get( 'TableUtils' ).getCellLocation( firstCell ).column;

Expand Down
3 changes: 1 addition & 2 deletions packages/ckeditor5-table/src/commands/selectcolumncommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import Command from '@ckeditor/ckeditor5-core/src/command';

import TableWalker from '../tablewalker';
import { getSelectionAffectedTableCells } from '../utils/selection';
import { findAncestor } from '../utils/common';

/**
* The select column command.
Expand Down Expand Up @@ -42,7 +41,7 @@ export default class SelectColumnCommand extends Command {
const referenceCells = getSelectionAffectedTableCells( model.document.selection );
const firstCell = referenceCells[ 0 ];
const lastCell = referenceCells.pop();
const table = findAncestor( 'table', firstCell );
const table = firstCell.findAncestor( 'table' );

const tableUtils = this.editor.plugins.get( 'TableUtils' );
const startLocation = tableUtils.getCellLocation( firstCell );
Expand Down
3 changes: 1 addition & 2 deletions packages/ckeditor5-table/src/commands/selectrowcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import Command from '@ckeditor/ckeditor5-core/src/command';

import { getRowIndexes, getSelectionAffectedTableCells } from '../utils/selection';
import { findAncestor } from '../utils/common';

/**
* The select row command.
Expand Down Expand Up @@ -41,7 +40,7 @@ export default class SelectRowCommand extends Command {
const referenceCells = getSelectionAffectedTableCells( model.document.selection );
const rowIndexes = getRowIndexes( referenceCells );

const table = findAncestor( 'table', referenceCells[ 0 ] );
const table = referenceCells[ 0 ].findAncestor( 'table' );
const rangesToSelect = [];

for ( let rowIndex = rowIndexes.first; rowIndex <= rowIndexes.last; rowIndex++ ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import Command from '@ckeditor/ckeditor5-core/src/command';

import {
findAncestor,
isHeadingColumnCell,
updateNumericAttribute
} from '../utils/common';
Expand Down Expand Up @@ -74,7 +73,7 @@ export default class SetHeaderColumnCommand extends Command {

const model = this.editor.model;
const selectedCells = getSelectionAffectedTableCells( model.document.selection );
const table = findAncestor( 'table', selectedCells[ 0 ] );
const table = selectedCells[ 0 ].findAncestor( 'table' );

const { first, last } = getColumnIndexes( selectedCells );
const headingColumnsToSet = this.value ? first : last + 1;
Expand Down
4 changes: 2 additions & 2 deletions packages/ckeditor5-table/src/commands/setheaderrowcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import Command from '@ckeditor/ckeditor5-core/src/command';

import { findAncestor, updateNumericAttribute } from '../utils/common';
import { updateNumericAttribute } from '../utils/common';
import { getRowIndexes, getSelectionAffectedTableCells } from '../utils/selection';
import { getVerticallyOverlappingCells, splitHorizontally } from '../utils/structure';

Expand Down Expand Up @@ -67,7 +67,7 @@ export default class SetHeaderRowCommand extends Command {
}
const model = this.editor.model;
const selectedCells = getSelectionAffectedTableCells( model.document.selection );
const table = findAncestor( 'table', selectedCells[ 0 ] );
const table = selectedCells[ 0 ].findAncestor( 'table' );

const { first, last } = getRowIndexes( selectedCells );
const headingRowsToSet = this.value ? first : last + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import TableWalker from './../tablewalker';
import { createEmptyTableCell, findAncestor, updateNumericAttribute } from '../utils/common';
import { createEmptyTableCell, updateNumericAttribute } from '../utils/common';

/**
* Injects a table layout post-fixer into the model.
Expand Down Expand Up @@ -238,12 +238,12 @@ function tableLayoutPostFixer( writer, model ) {

// Fix table on adding/removing table cells and rows.
if ( entry.name == 'tableRow' || entry.name == 'tableCell' ) {
table = findAncestor( 'table', entry.position );
table = entry.position.findAncestor( 'table' );
}

// Fix table on any table's attribute change - including attributes of table cells.
if ( isTableAttributeEntry( entry ) ) {
table = findAncestor( 'table', entry.range.start );
table = entry.range.start.findAncestor( 'table' );
}

if ( table && !analyzedTables.has( table ) ) {
Expand Down
7 changes: 2 additions & 5 deletions packages/ckeditor5-table/src/tableclipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';

import TableSelection from './tableselection';
import TableWalker from './tablewalker';
import {
findAncestor
} from './utils/common';
import TableUtils from './tableutils';
import { getColumnIndexes, getRowIndexes, getSelectionAffectedTableCells, isSelectionRectangular } from './utils/selection';
import {
Expand Down Expand Up @@ -165,7 +162,7 @@ export default class TableClipboard extends Plugin {
pastedTable = cropTableToDimensions( pastedTable, cropDimensions, writer );

// Content table to which we insert a pasted table.
const selectedTable = findAncestor( 'table', selectedTableCells[ 0 ] );
const selectedTable = selectedTableCells[ 0 ].findAncestor( 'table' );

replaceSelectedCellsWithPasted( pastedTable, pastedDimensions, selectedTable, selection, writer );
} );
Expand All @@ -186,7 +183,7 @@ export default class TableClipboard extends Plugin {
// @returns {Number} selection.lastColumn
// @returns {Number} selection.lastRow
function prepareTableForPasting( selectedTableCells, pastedDimensions, writer, tableUtils ) {
const selectedTable = findAncestor( 'table', selectedTableCells[ 0 ] );
const selectedTable = selectedTableCells[ 0 ].findAncestor( 'table' );

const columnIndexes = getColumnIndexes( selectedTableCells );
const rowIndexes = getRowIndexes( selectedTableCells );
Expand Down
5 changes: 2 additions & 3 deletions packages/ckeditor5-table/src/tablekeyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
getLocalizedArrowKeyCodeDirection
} from '@ckeditor/ckeditor5-utils/src/keyboard';
import { getSelectedTableCells, getTableCellsContainingSelection } from './utils/selection';
import { findAncestor } from './utils/common';

/**
* This plugin enables keyboard navigation for tables.
Expand Down Expand Up @@ -214,7 +213,7 @@ export default class TableKeyboard extends Plugin {
}

// Abort if we're not in a table cell.
const tableCell = findAncestor( 'tableCell', selection.focus );
const tableCell = selection.focus.findAncestor( 'tableCell' );

if ( !tableCell ) {
return false;
Expand Down Expand Up @@ -418,7 +417,7 @@ export default class TableKeyboard extends Plugin {
_navigateFromCellInDirection( focusCell, direction, expandSelection = false ) {
const model = this.editor.model;

const table = findAncestor( 'table', focusCell );
const table = focusCell.findAncestor( 'table' );
const tableMap = [ ...new TableWalker( table, { includeAllSlots: true } ) ];
const { row: lastRow, column: lastColumn } = tableMap[ tableMap.length - 1 ];

Expand Down
7 changes: 1 addition & 6 deletions packages/ckeditor5-table/src/tablemouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import TableSelection from './tableselection';
import MouseEventsObserver from './tablemouse/mouseeventsobserver';

import { findAncestor } from './utils/common';
import { getTableCellsContainingSelection } from './utils/selection';

/**
Expand Down Expand Up @@ -210,11 +209,7 @@ export default class TableMouse extends Plugin {
const modelPosition = this.editor.editing.mapper.toModelPosition( viewPosition );
const modelElement = modelPosition.parent;

if ( modelElement.is( 'tableCell' ) ) {
return modelElement;
}

return findAncestor( 'tableCell', modelElement );
return modelElement.findAncestor( 'tableCell', { includeSelf: true } );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import Command from '@ckeditor/ckeditor5-core/src/command';

import { findAncestor } from '../../utils/common';

/**
* The table cell attribute command.
*
Expand Down Expand Up @@ -38,7 +36,7 @@ export default class TablePropertyCommand extends Command {
const editor = this.editor;
const selection = editor.model.document.selection;

const table = findAncestor( 'table', selection.getFirstPosition() );
const table = selection.getFirstPosition().findAncestor( 'table' );

this.isEnabled = !!table;
this.value = this._getValue( table );
Expand All @@ -60,7 +58,7 @@ export default class TablePropertyCommand extends Command {

const { value, batch } = options;

const table = findAncestor( 'table', selection.getFirstPosition() );
const table = selection.getFirstPosition().findAncestor( 'table' );
const valueToSet = this._getValueToSet( value );

model.enqueueChange( batch || 'default', writer => {
Expand Down
5 changes: 2 additions & 3 deletions packages/ckeditor5-table/src/tableselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import first from '@ckeditor/ckeditor5-utils/src/first';
import TableWalker from './tablewalker';
import TableUtils from './tableutils';

import { findAncestor } from './utils/common';
import { cropTableToDimensions, adjustLastRowIndex, adjustLastColumnIndex } from './utils/structure';
import { getColumnIndexes, getRowIndexes, getSelectedTableCells, isSelectionRectangular } from './utils/selection';

Expand Down Expand Up @@ -95,7 +94,7 @@ export default class TableSelection extends Plugin {
const { first: firstColumn, last: lastColumn } = getColumnIndexes( selectedCells );
const { first: firstRow, last: lastRow } = getRowIndexes( selectedCells );

const sourceTable = findAncestor( 'table', selectedCells[ 0 ] );
const sourceTable = selectedCells[ 0 ].findAncestor( 'table' );

let adjustedLastRow = lastRow;
let adjustedLastColumn = lastColumn;
Expand Down Expand Up @@ -335,7 +334,7 @@ export default class TableSelection extends Plugin {
endColumn
};

for ( const { row, cell } of new TableWalker( findAncestor( 'table', anchorCell ), walkerOptions ) ) {
for ( const { row, cell } of new TableWalker( anchorCell.findAncestor( 'table' ), walkerOptions ) ) {
selectionMap[ row - startRow ].push( cell );
}

Expand Down
20 changes: 0 additions & 20 deletions packages/ckeditor5-table/src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,6 @@
* @module table/utils/common
*/

/**
* Returns the parent element of the given name. Returns undefined if the position or the element is not inside the desired parent.
*
* @param {String} parentName The name of the parent element to find.
* @param {module:engine/model/position~Position|module:engine/model/position~Position} positionOrElement The position or
* the parentElement to start searching.
* @returns {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment}
*/
export function findAncestor( parentName, positionOrElement ) {
let parent = positionOrElement.parent;

while ( parent ) {
if ( parent.name === parentName ) {
return parent;
}

parent = parent.parent;
}
}

/**
* A common method to update the numeric value. If a value is the default one, it will be unset.
*
Expand Down
Loading

0 comments on commit aa535c1

Please sign in to comment.