浏览代码

Changed: Removed commands/utils~getParentTable() method and renamed commands/utils~getParentElement() to findAncestor().

Maciej Gołaszewski 7 年之前
父节点
当前提交
de1c301ab9

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

@@ -8,7 +8,7 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import { getParentElement, getParentTable } from './utils';
+import { findAncestor } from './utils';
 import TableUtils from '../tableutils';
 
 /**
@@ -54,7 +54,7 @@ export default class InsertColumnCommand extends Command {
 	refresh() {
 		const selection = this.editor.model.document.selection;
 
-		const tableParent = getParentTable( selection.getFirstPosition() );
+		const tableParent = findAncestor( 'table', selection.getFirstPosition() );
 
 		this.isEnabled = !!tableParent;
 	}
@@ -74,7 +74,7 @@ export default class InsertColumnCommand extends Command {
 
 		const firstPosition = selection.getFirstPosition();
 
-		const tableCell = getParentElement( 'tableCell', firstPosition );
+		const tableCell = findAncestor( 'tableCell', firstPosition );
 		const table = tableCell.parent.parent;
 
 		const { column } = tableUtils.getCellLocation( tableCell );

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

@@ -8,7 +8,7 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import { getParentElement, getParentTable } from './utils';
+import { findAncestor } from './utils';
 import TableUtils from '../tableutils';
 
 /**
@@ -54,7 +54,7 @@ export default class InsertRowCommand extends Command {
 	refresh() {
 		const selection = this.editor.model.document.selection;
 
-		const tableParent = getParentTable( selection.getFirstPosition() );
+		const tableParent = findAncestor( 'table', selection.getFirstPosition() );
 
 		this.isEnabled = !!tableParent;
 	}
@@ -71,7 +71,7 @@ export default class InsertRowCommand extends Command {
 		const selection = editor.model.document.selection;
 		const tableUtils = editor.plugins.get( TableUtils );
 
-		const tableCell = getParentElement( 'tableCell', selection.getFirstPosition() );
+		const tableCell = findAncestor( 'tableCell', selection.getFirstPosition() );
 		const tableRow = tableCell.parent;
 		const table = tableRow.parent;
 

+ 3 - 3
packages/ckeditor5-table/src/commands/mergecellcommand.js

@@ -11,7 +11,7 @@ import Command from '@ckeditor/ckeditor5-core/src/command';
 import Position from '@ckeditor/ckeditor5-engine/src/model/position';
 import Range from '@ckeditor/ckeditor5-engine/src/model/range';
 import TableWalker from '../tablewalker';
-import { getParentElement, updateNumericAttribute } from './utils';
+import { findAncestor, updateNumericAttribute } from './utils';
 import TableUtils from '../tableutils';
 
 /**
@@ -83,7 +83,7 @@ export default class MergeCellCommand extends Command {
 	execute() {
 		const model = this.editor.model;
 		const doc = model.document;
-		const tableCell = getParentElement( 'tableCell', doc.selection.getFirstPosition() );
+		const tableCell = findAncestor( 'tableCell', doc.selection.getFirstPosition() );
 		const cellToMerge = this.value;
 		const direction = this.direction;
 
@@ -123,7 +123,7 @@ export default class MergeCellCommand extends Command {
 	_getMergeableCell() {
 		const model = this.editor.model;
 		const doc = model.document;
-		const tableCell = getParentElement( 'tableCell', doc.selection.getFirstPosition() );
+		const tableCell = findAncestor( 'tableCell', doc.selection.getFirstPosition() );
 
 		if ( !tableCell ) {
 			return;

+ 3 - 3
packages/ckeditor5-table/src/commands/removecolumncommand.js

@@ -11,7 +11,7 @@ import Command from '@ckeditor/ckeditor5-core/src/command';
 
 import TableWalker from '../tablewalker';
 import TableUtils from '../tableutils';
-import { getParentElement, updateNumericAttribute } from './utils';
+import { findAncestor, updateNumericAttribute } from './utils';
 
 /**
  * The remove column command.
@@ -33,7 +33,7 @@ export default class RemoveColumnCommand extends Command {
 		const selection = editor.model.document.selection;
 		const tableUtils = editor.plugins.get( TableUtils );
 
-		const tableCell = getParentElement( 'tableCell', selection.getFirstPosition() );
+		const tableCell = findAncestor( 'tableCell', selection.getFirstPosition() );
 
 		this.isEnabled = !!tableCell && tableUtils.getColumns( tableCell.parent.parent ) > 1;
 	}
@@ -47,7 +47,7 @@ export default class RemoveColumnCommand extends Command {
 
 		const firstPosition = selection.getFirstPosition();
 
-		const tableCell = getParentElement( 'tableCell', firstPosition );
+		const tableCell = findAncestor( 'tableCell', firstPosition );
 		const tableRow = tableCell.parent;
 		const table = tableRow.parent;
 

+ 3 - 3
packages/ckeditor5-table/src/commands/removerowcommand.js

@@ -12,7 +12,7 @@ import Position from '@ckeditor/ckeditor5-engine/src/model/position';
 import Range from '@ckeditor/ckeditor5-engine/src/model/range';
 
 import TableWalker from '../tablewalker';
-import { getParentElement, updateNumericAttribute } from './utils';
+import { findAncestor, updateNumericAttribute } from './utils';
 
 /**
  * The remove row command.
@@ -33,7 +33,7 @@ export default class RemoveRowCommand extends Command {
 		const model = this.editor.model;
 		const doc = model.document;
 
-		const tableCell = getParentElement( 'tableCell', doc.selection.getFirstPosition() );
+		const tableCell = findAncestor( 'tableCell', doc.selection.getFirstPosition() );
 
 		this.isEnabled = !!tableCell && tableCell.parent.parent.childCount > 1;
 	}
@@ -46,7 +46,7 @@ export default class RemoveRowCommand extends Command {
 		const selection = model.document.selection;
 
 		const firstPosition = selection.getFirstPosition();
-		const tableCell = getParentElement( 'tableCell', firstPosition );
+		const tableCell = findAncestor( 'tableCell', firstPosition );
 		const tableRow = tableCell.parent;
 		const table = tableRow.parent;
 

+ 3 - 3
packages/ckeditor5-table/src/commands/setheadercolumncommand.js

@@ -9,7 +9,7 @@
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
-import { getParentElement, updateNumericAttribute } from './utils';
+import { findAncestor, updateNumericAttribute } from './utils';
 
 /**
  * The header column command.
@@ -36,7 +36,7 @@ export default class SetHeaderColumnCommand extends Command {
 		const selection = doc.selection;
 
 		const position = selection.getFirstPosition();
-		const tableCell = getParentElement( 'tableCell', position );
+		const tableCell = findAncestor( 'tableCell', position );
 
 		const isInTable = !!tableCell;
 
@@ -69,7 +69,7 @@ export default class SetHeaderColumnCommand extends Command {
 		const tableUtils = this.editor.plugins.get( 'TableUtils' );
 
 		const position = selection.getFirstPosition();
-		const tableCell = getParentElement( 'tableCell', position.parent );
+		const tableCell = findAncestor( 'tableCell', position.parent );
 		const tableRow = tableCell.parent;
 		const table = tableRow.parent;
 

+ 3 - 3
packages/ckeditor5-table/src/commands/setheaderrowcommand.js

@@ -10,7 +10,7 @@
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import Position from '@ckeditor/ckeditor5-engine/src/model/position';
 
-import { createEmptyTableCell, getParentElement, updateNumericAttribute } from './utils';
+import { createEmptyTableCell, findAncestor, updateNumericAttribute } from './utils';
 import TableWalker from '../tablewalker';
 
 /**
@@ -37,7 +37,7 @@ export default class SetHeaderRowCommand extends Command {
 		const selection = doc.selection;
 
 		const position = selection.getFirstPosition();
-		const tableCell = getParentElement( 'tableCell', position );
+		const tableCell = findAncestor( 'tableCell', position );
 		const isInTable = !!tableCell;
 
 		this.isEnabled = isInTable;
@@ -68,7 +68,7 @@ export default class SetHeaderRowCommand extends Command {
 		const selection = doc.selection;
 
 		const position = selection.getFirstPosition();
-		const tableCell = getParentElement( 'tableCell', position );
+		const tableCell = findAncestor( 'tableCell', position );
 		const tableRow = tableCell.parent;
 		const table = tableRow.parent;
 

+ 3 - 3
packages/ckeditor5-table/src/commands/splitcellcommand.js

@@ -9,7 +9,7 @@
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import TableUtils from '../tableutils';
-import { getParentElement } from './utils';
+import { findAncestor } from './utils';
 
 /**
  * The split cell command.
@@ -50,7 +50,7 @@ export default class SplitCellCommand extends Command {
 		const model = this.editor.model;
 		const doc = model.document;
 
-		const tableCell = getParentElement( 'tableCell', doc.selection.getFirstPosition() );
+		const tableCell = findAncestor( 'tableCell', doc.selection.getFirstPosition() );
 
 		this.isEnabled = !!tableCell;
 	}
@@ -64,7 +64,7 @@ export default class SplitCellCommand extends Command {
 		const selection = document.selection;
 
 		const firstPosition = selection.getFirstPosition();
-		const tableCell = getParentElement( 'tableCell', firstPosition );
+		const tableCell = findAncestor( 'tableCell', firstPosition );
 
 		const isHorizontally = this.direction === 'horizontally';
 

+ 2 - 12
packages/ckeditor5-table/src/commands/utils.js

@@ -8,23 +8,13 @@
  */
 
 /**
- * Returns the parent table. Returns undefined if position is not inside table.
- *
- * @param {module:engine/model/position~Position} position
- * @returns {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment}
- */
-export function getParentTable( position ) {
-	return getParentElement( 'table', position );
-}
-
-/**
  * Returns the parent element of given name. Returns undefined if position is not inside desired parent.
  *
  * @param {String} parentName Name of parent element to find.
- * @param {module:engine/model/position~Position} position
+ * @param {module:engine/model/position~Position|module:engine/model/position~Position} position Position to start searching.
  * @returns {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment}
  */
-export function getParentElement( parentName, position ) {
+export function findAncestor( parentName, position ) {
 	let parent = position.parent;
 
 	while ( parent ) {

+ 3 - 3
packages/ckeditor5-table/src/converters/table-post-fixer.js

@@ -8,7 +8,7 @@
  */
 
 import Position from '@ckeditor/ckeditor5-engine/src/model/position';
-import { createEmptyTableCell, getParentTable, updateNumericAttribute } from './../commands/utils';
+import { createEmptyTableCell, findAncestor, updateNumericAttribute } from './../commands/utils';
 import TableWalker from './../tablewalker';
 
 /**
@@ -240,12 +240,12 @@ function tablePostFixer( writer, model ) {
 
 		// Fix table on adding/removing table cells and rows.
 		if ( entry.name == 'tableRow' || entry.name == 'tableCell' ) {
-			table = getParentTable( entry.position );
+			table = findAncestor( 'table', entry.position );
 		}
 
 		// Fix table on any table's attribute change - including attributes of table cells.
 		if ( isTableAttributeEntry( entry ) ) {
-			table = getParentTable( entry.range.start );
+			table = findAncestor( 'table', entry.range.start );
 		}
 
 		if ( table && !analyzedTables.has( table ) ) {

+ 4 - 3
packages/ckeditor5-table/src/tableediting.js

@@ -30,12 +30,13 @@ import RemoveRowCommand from './commands/removerowcommand';
 import RemoveColumnCommand from './commands/removecolumncommand';
 import SetHeaderRowCommand from './commands/setheaderrowcommand';
 import SetHeaderColumnCommand from './commands/setheadercolumncommand';
-import { getParentElement } from './commands/utils';
+import { findAncestor } from './commands/utils';
 import TableUtils from '../src/tableutils';
+
 import injectTablePostFixer from './converters/table-post-fixer';
+import injectTableCellPostFixer from './converters/tablecell-post-fixer';
 
 import '../theme/tableediting.css';
-import injectTableCellPostFixer from './converters/tablecell-post-fixer';
 
 /**
  * The table editing feature.
@@ -190,7 +191,7 @@ export default class TableEditing extends Plugin {
 
 			const firstPosition = selection.getFirstPosition();
 
-			const tableCell = getParentElement( 'tableCell', firstPosition );
+			const tableCell = findAncestor( 'tableCell', firstPosition );
 
 			if ( !tableCell ) {
 				return;

+ 6 - 4
packages/ckeditor5-table/src/tableutils.js

@@ -11,7 +11,7 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import Position from '@ckeditor/ckeditor5-engine/src/model/position';
 
 import TableWalker from './tablewalker';
-import { createEmptyTableCell, getParentTable, updateNumericAttribute } from './commands/utils';
+import { createEmptyTableCell, updateNumericAttribute } from './commands/utils';
 
 /**
  * The table utilities plugin.
@@ -294,7 +294,8 @@ export default class TableUtils extends Plugin {
 	 */
 	splitCellVertically( tableCell, numberOfCells = 2 ) {
 		const model = this.editor.model;
-		const table = getParentTable( tableCell );
+		const tableRow = tableCell.parent;
+		const table = tableRow.parent;
 
 		const rowspan = parseInt( tableCell.getAttribute( 'rowspan' ) || 1 );
 		const colspan = parseInt( tableCell.getAttribute( 'colspan' ) || 1 );
@@ -430,8 +431,9 @@ export default class TableUtils extends Plugin {
 	splitCellHorizontally( tableCell, numberOfCells = 2 ) {
 		const model = this.editor.model;
 
-		const table = getParentTable( tableCell );
-		const splitCellRow = table.getChildIndex( tableCell.parent );
+		const tableRow = tableCell.parent;
+		const table = tableRow.parent;
+		const splitCellRow = table.getChildIndex( tableRow );
 
 		const rowspan = parseInt( tableCell.getAttribute( 'rowspan' ) || 1 );
 		const colspan = parseInt( tableCell.getAttribute( 'colspan' ) || 1 );

+ 2 - 2
packages/ckeditor5-table/src/ui/utils.js

@@ -8,7 +8,7 @@
  */
 
 import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
-import { getParentTable } from '../commands/utils';
+import { findAncestor } from '../commands/utils';
 
 /**
  * A helper utility that positions the
@@ -36,7 +36,7 @@ export function getBalloonPositionData( editor ) {
 	const defaultPositions = BalloonPanelView.defaultPositions;
 	const viewSelection = editingView.document.selection;
 
-	const parentTable = getParentTable( viewSelection.getFirstPosition() );
+	const parentTable = findAncestor( 'table', viewSelection.getFirstPosition() );
 
 	return {
 		target: editingView.domConverter.viewToDom( parentTable ),

+ 3 - 3
packages/ckeditor5-table/src/utils.js

@@ -7,8 +7,8 @@
  * @module table/utils
  */
 
-import { toWidget, isWidget } from '@ckeditor/ckeditor5-widget/src/utils';
-import { getParentTable } from './commands/utils';
+import { isWidget, toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
+import { findAncestor } from './commands/utils';
 
 const tableSymbol = Symbol( 'isTable' );
 
@@ -57,7 +57,7 @@ export function isTableWidgetSelected( selection ) {
  * @returns {Boolean}
  */
 export function isTableContentSelected( selection ) {
-	const parentTable = getParentTable( selection.getFirstPosition() );
+	const parentTable = findAncestor( 'table', selection.getFirstPosition() );
 
 	return !!( parentTable && isTableWidget( parentTable.parent ) );
 }

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

@@ -8,7 +8,7 @@ import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 import { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
 
-import { getParentTable } from '../../src/commands/utils';
+import { findAncestor } from '../../src/commands/utils';
 
 describe( 'commands utils', () => {
 	let editor, model;
@@ -32,13 +32,13 @@ describe( 'commands utils', () => {
 		it( 'should return undefined if not in table', () => {
 			setData( model, '<paragraph>foo[]</paragraph>' );
 
-			expect( getParentTable( model.document.selection.focus ) ).to.be.undefined;
+			expect( findAncestor( 'table', model.document.selection.focus ) ).to.be.undefined;
 		} );
 
 		it( 'should return table if position is in tableCell', () => {
 			setData( model, modelTable( [ [ '[]' ] ] ) );
 
-			const parentTable = getParentTable( model.document.selection.focus );
+			const parentTable = findAncestor( 'table', model.document.selection.focus );
 
 			expect( parentTable ).to.not.be.undefined;
 			expect( parentTable.is( 'table' ) ).to.be.true;