Browse Source

Expose getSelectedTableCell() util function and remove getFirstSelectedTableCell().

Maciej Gołaszewski 5 years ago
parent
commit
c0e2ac8fe1

+ 5 - 14
packages/ckeditor5-table/src/commands/utils.js

@@ -32,15 +32,15 @@ export function findAncestor( parentName, positionOrElement ) {
 /**
  * Returns a first selected table cell from a multi-cell or in-cell selection.
  *
- *		const tableCell = getFirstSelectedTableCell( editor.model.selection );
+ *		const tableCell = getSelectedTableCell( editor.model.document.selection.getFirstPosition() );
  *
- * @param {module:engine/model/documentselection~DocumentSelection} selection Document selection.
+ * @param {module:engine/model/position~Position} position Document position.
  * @returns {module:engine/model/element~Element}
  */
-export function getFirstSelectedTableCell( selection ) {
-	const firstPosition = selection.getFirstPosition();
+export function getSelectedTableCell( position ) {
+	const isTableCellSelected = position.nodeAfter && position.nodeAfter.is( 'tableCell' );
 
-	return getSelectedTableCell( firstPosition );
+	return isTableCellSelected ? position.nodeAfter : findAncestor( 'tableCell', position );
 }
 
 export function getSelectedTableCells( model ) {
@@ -136,12 +136,3 @@ export function addDefaultUnitToNumericValue( value, defaultUnit ) {
 
 	return `${ numericValue }${ defaultUnit }`;
 }
-
-// Returns a table cell for two scenarios:
-// 1. Selection is on table cell (position is before table cell).
-// 2. Selection is inside a table cell (position is inside as well).
-function getSelectedTableCell( position ) {
-	const isTableCellSelected = position.nodeAfter && position.nodeAfter.is( 'tableCell' );
-
-	return isTableCellSelected ? position.nodeAfter : findAncestor( 'tableCell', position );
-}

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

@@ -14,7 +14,7 @@ import Model from '@ckeditor/ckeditor5-ui/src/model';
 import ColorInputView from './colorinputview';
 import { isColor, isLength, isPercentage } from '@ckeditor/ckeditor5-engine/src/view/styles/utils';
 import { getTableWidgetAncestor } from '../utils';
-import { findAncestor, getFirstSelectedTableCell } from '../commands/utils';
+import { findAncestor, getSelectedTableCell } from '../commands/utils';
 
 const DEFAULT_BALLOON_POSITIONS = BalloonPanelView.defaultPositions;
 const BALLOON_POSITIONS = [
@@ -81,7 +81,7 @@ export function getBalloonTablePositionData( editor ) {
  * @returns {module:utils/dom/position~Options}
  */
 export function getBalloonCellPositionData( editor ) {
-	const modelTableCell = getFirstSelectedTableCell( editor.model.document.selection );
+	const modelTableCell = getSelectedTableCell( editor.model.document.selection.getFirstPosition() );
 	const viewTableCell = editor.editing.mapper.toViewElement( modelTableCell );
 
 	return {