Browse Source

Extract getFirstSelectedTableCell() helper function.

Maciej Gołaszewski 5 years ago
parent
commit
8e48b43087

+ 15 - 0
packages/ckeditor5-table/src/commands/utils.js

@@ -30,6 +30,21 @@ 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 );
+ *
+ * @param {module:engine/model/documentselection~DocumentSelection} selection Document selection.
+ * @returns {module:engine/model/element~Element}
+ */
+export function getFirstSelectedTableCell( selection ) {
+	const firstPosition = selection.getFirstPosition();
+	const isTableCellSelected = firstPosition.nodeAfter && firstPosition.nodeAfter.is( 'tableCell' );
+
+	return isTableCellSelected ? firstPosition.nodeAfter : findAncestor( 'tableCell', firstPosition );
+}
+
+/**
  * A common method to update the numeric value. If a value is the default one, it will be unset.
  *
  * @param {String} key An attribute key.

+ 2 - 6
packages/ckeditor5-table/src/tablecellproperties/commands/tablecellpropertycommand.js

@@ -9,7 +9,7 @@
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
-import { findAncestor } from '../../commands/utils';
+import { findAncestor, getFirstSelectedTableCell } from '../../commands/utils';
 
 /**
  * The table cell attribute command.
@@ -36,11 +36,7 @@ export default class TableCellPropertyCommand extends Command {
 	 */
 	refresh() {
 		const editor = this.editor;
-		const selection = editor.model.document.selection;
-
-		const firstPosition = selection.getFirstPosition();
-		const isTableCellSelected = firstPosition.nodeAfter && firstPosition.nodeAfter.is( 'tableCell' );
-		const tableCell = isTableCellSelected ? firstPosition.nodeAfter : findAncestor( 'tableCell', firstPosition );
+		const tableCell = getFirstSelectedTableCell( editor.model.document.selection );
 
 		this.isEnabled = !!tableCell;
 		this.value = this._getAttribute( tableCell );

+ 2 - 6
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 } from '../commands/utils';
+import { findAncestor, getFirstSelectedTableCell } from '../commands/utils';
 
 const DEFAULT_BALLOON_POSITIONS = BalloonPanelView.defaultPositions;
 const BALLOON_POSITIONS = [
@@ -81,11 +81,7 @@ export function getBalloonTablePositionData( editor ) {
  * @returns {module:utils/dom/position~Options}
  */
 export function getBalloonCellPositionData( editor ) {
-	const firstPosition = editor.model.document.selection.getFirstPosition();
-	const nodeAfter = firstPosition.nodeAfter;
-
-	const isTableCellSelected = nodeAfter && nodeAfter.is( 'tableCell' );
-	const modelTableCell = isTableCellSelected ? nodeAfter : findAncestor( 'tableCell', firstPosition );
+	const modelTableCell = getFirstSelectedTableCell( editor.model.document.selection );
 	const viewTableCell = editor.editing.mapper.toViewElement( modelTableCell );
 
 	return {