8
0
Просмотр исходного кода

Used the getTableCellsContainingSelection() helper across the package.

Aleksander Nowodzinski 5 лет назад
Родитель
Сommit
7dc643238d

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

@@ -9,7 +9,8 @@
 
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import TableWalker from '../tablewalker';
 import TableWalker from '../tablewalker';
-import { findAncestor, updateNumericAttribute } from './utils';
+import { updateNumericAttribute } from './utils';
+import { getTableCellsContainingSelection } from '../utils';
 
 
 /**
 /**
  * The merge cell command.
  * The merge cell command.
@@ -78,7 +79,7 @@ export default class MergeCellCommand extends Command {
 	execute() {
 	execute() {
 		const model = this.editor.model;
 		const model = this.editor.model;
 		const doc = model.document;
 		const doc = model.document;
-		const tableCell = findAncestor( 'tableCell', doc.selection.getFirstPosition() );
+		const tableCell = getTableCellsContainingSelection( doc.selection )[ 0 ];
 		const cellToMerge = this.value;
 		const cellToMerge = this.value;
 		const direction = this.direction;
 		const direction = this.direction;
 
 
@@ -118,7 +119,7 @@ export default class MergeCellCommand extends Command {
 	_getMergeableCell() {
 	_getMergeableCell() {
 		const model = this.editor.model;
 		const model = this.editor.model;
 		const doc = model.document;
 		const doc = model.document;
-		const tableCell = findAncestor( 'tableCell', doc.selection.getFirstPosition() );
+		const tableCell = getTableCellsContainingSelection( doc.selection )[ 0 ];
 
 
 		if ( !tableCell ) {
 		if ( !tableCell ) {
 			return;
 			return;

+ 4 - 6
packages/ckeditor5-table/src/commands/removecolumncommand.js

@@ -10,7 +10,8 @@
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
 
 import TableWalker from '../tablewalker';
 import TableWalker from '../tablewalker';
-import { findAncestor, updateNumericAttribute } from './utils';
+import { updateNumericAttribute } from './utils';
+import { getTableCellsContainingSelection } from '../utils';
 
 
 /**
 /**
  * The remove column command.
  * The remove column command.
@@ -31,8 +32,7 @@ export default class RemoveColumnCommand extends Command {
 		const editor = this.editor;
 		const editor = this.editor;
 		const selection = editor.model.document.selection;
 		const selection = editor.model.document.selection;
 		const tableUtils = editor.plugins.get( 'TableUtils' );
 		const tableUtils = editor.plugins.get( 'TableUtils' );
-
-		const tableCell = findAncestor( 'tableCell', selection.getFirstPosition() );
+		const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
 
 
 		this.isEnabled = !!tableCell && tableUtils.getColumns( tableCell.parent.parent ) > 1;
 		this.isEnabled = !!tableCell && tableUtils.getColumns( tableCell.parent.parent ) > 1;
 	}
 	}
@@ -44,9 +44,7 @@ export default class RemoveColumnCommand extends Command {
 		const model = this.editor.model;
 		const model = this.editor.model;
 		const selection = model.document.selection;
 		const selection = model.document.selection;
 
 
-		const firstPosition = selection.getFirstPosition();
-
-		const tableCell = findAncestor( 'tableCell', firstPosition );
+		const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
 		const tableRow = tableCell.parent;
 		const tableRow = tableCell.parent;
 		const table = tableRow.parent;
 		const table = tableRow.parent;
 
 

+ 4 - 6
packages/ckeditor5-table/src/commands/removerowcommand.js

@@ -10,7 +10,8 @@
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
 
 import TableWalker from '../tablewalker';
 import TableWalker from '../tablewalker';
-import { findAncestor, updateNumericAttribute } from './utils';
+import { updateNumericAttribute } from './utils';
+import { getTableCellsContainingSelection } from '../utils';
 
 
 /**
 /**
  * The remove row command.
  * The remove row command.
@@ -30,8 +31,7 @@ export default class RemoveRowCommand extends Command {
 	refresh() {
 	refresh() {
 		const model = this.editor.model;
 		const model = this.editor.model;
 		const doc = model.document;
 		const doc = model.document;
-
-		const tableCell = findAncestor( 'tableCell', doc.selection.getFirstPosition() );
+		const tableCell = getTableCellsContainingSelection( doc.selection )[ 0 ];
 
 
 		this.isEnabled = !!tableCell && tableCell.parent.parent.childCount > 1;
 		this.isEnabled = !!tableCell && tableCell.parent.parent.childCount > 1;
 	}
 	}
@@ -42,9 +42,7 @@ export default class RemoveRowCommand extends Command {
 	execute() {
 	execute() {
 		const model = this.editor.model;
 		const model = this.editor.model;
 		const selection = model.document.selection;
 		const selection = model.document.selection;
-
-		const firstPosition = selection.getFirstPosition();
-		const tableCell = findAncestor( 'tableCell', firstPosition );
+		const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
 		const tableRow = tableCell.parent;
 		const tableRow = tableCell.parent;
 		const table = tableRow.parent;
 		const table = tableRow.parent;
 
 

+ 4 - 7
packages/ckeditor5-table/src/commands/setheadercolumncommand.js

@@ -9,7 +9,8 @@
 
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
 
-import { findAncestor, updateNumericAttribute } from './utils';
+import { updateNumericAttribute } from './utils';
+import { getTableCellsContainingSelection } from '../utils';
 
 
 /**
 /**
  * The header column command.
  * The header column command.
@@ -33,10 +34,7 @@ export default class SetHeaderColumnCommand extends Command {
 	refresh() {
 	refresh() {
 		const model = this.editor.model;
 		const model = this.editor.model;
 		const doc = model.document;
 		const doc = model.document;
-		const selection = doc.selection;
-
-		const position = selection.getFirstPosition();
-		const tableCell = findAncestor( 'tableCell', position );
+		const tableCell = getTableCellsContainingSelection( doc.selection )[ 0 ];
 
 
 		const isInTable = !!tableCell;
 		const isInTable = !!tableCell;
 
 
@@ -71,8 +69,7 @@ export default class SetHeaderColumnCommand extends Command {
 		const selection = doc.selection;
 		const selection = doc.selection;
 		const tableUtils = this.editor.plugins.get( 'TableUtils' );
 		const tableUtils = this.editor.plugins.get( 'TableUtils' );
 
 
-		const position = selection.getFirstPosition();
-		const tableCell = findAncestor( 'tableCell', position );
+		const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
 		const tableRow = tableCell.parent;
 		const tableRow = tableCell.parent;
 		const table = tableRow.parent;
 		const table = tableRow.parent;
 
 

+ 7 - 5
packages/ckeditor5-table/src/commands/setheaderrowcommand.js

@@ -9,7 +9,11 @@
 
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
 
-import { createEmptyTableCell, findAncestor, updateNumericAttribute } from './utils';
+import {
+	createEmptyTableCell,
+	updateNumericAttribute
+} from './utils';
+import { getTableCellsContainingSelection } from '../utils';
 import TableWalker from '../tablewalker';
 import TableWalker from '../tablewalker';
 
 
 /**
 /**
@@ -35,8 +39,7 @@ export default class SetHeaderRowCommand extends Command {
 		const doc = model.document;
 		const doc = model.document;
 		const selection = doc.selection;
 		const selection = doc.selection;
 
 
-		const position = selection.getFirstPosition();
-		const tableCell = findAncestor( 'tableCell', position );
+		const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
 		const isInTable = !!tableCell;
 		const isInTable = !!tableCell;
 
 
 		this.isEnabled = isInTable;
 		this.isEnabled = isInTable;
@@ -69,8 +72,7 @@ export default class SetHeaderRowCommand extends Command {
 		const doc = model.document;
 		const doc = model.document;
 		const selection = doc.selection;
 		const selection = doc.selection;
 
 
-		const position = selection.getFirstPosition();
-		const tableCell = findAncestor( 'tableCell', position );
+		const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
 		const tableRow = tableCell.parent;
 		const tableRow = tableCell.parent;
 		const table = tableRow.parent;
 		const table = tableRow.parent;
 
 

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

@@ -8,7 +8,7 @@
  */
  */
 
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import { findAncestor } from './utils';
+import { getTableCellsContainingSelection } from '../utils';
 
 
 /**
 /**
  * The split cell command.
  * The split cell command.
@@ -49,7 +49,7 @@ export default class SplitCellCommand extends Command {
 		const model = this.editor.model;
 		const model = this.editor.model;
 		const doc = model.document;
 		const doc = model.document;
 
 
-		const tableCell = findAncestor( 'tableCell', doc.selection.getFirstPosition() );
+		const tableCell = getTableCellsContainingSelection( doc.selection )[ 0 ];
 
 
 		this.isEnabled = !!tableCell;
 		this.isEnabled = !!tableCell;
 	}
 	}
@@ -62,8 +62,7 @@ export default class SplitCellCommand extends Command {
 		const document = model.document;
 		const document = model.document;
 		const selection = document.selection;
 		const selection = document.selection;
 
 
-		const firstPosition = selection.getFirstPosition();
-		const tableCell = findAncestor( 'tableCell', firstPosition );
+		const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
 
 
 		const isHorizontally = this.direction === 'horizontally';
 		const isHorizontally = this.direction === 'horizontally';
 
 

+ 2 - 5
packages/ckeditor5-table/src/tableediting.js

@@ -28,7 +28,7 @@ import RemoveRowCommand from './commands/removerowcommand';
 import RemoveColumnCommand from './commands/removecolumncommand';
 import RemoveColumnCommand from './commands/removecolumncommand';
 import SetHeaderRowCommand from './commands/setheaderrowcommand';
 import SetHeaderRowCommand from './commands/setheaderrowcommand';
 import SetHeaderColumnCommand from './commands/setheadercolumncommand';
 import SetHeaderColumnCommand from './commands/setheadercolumncommand';
-import { findAncestor } from './commands/utils';
+import { getTableCellsContainingSelection } from './utils';
 import TableUtils from '../src/tableutils';
 import TableUtils from '../src/tableutils';
 
 
 import injectTableLayoutPostFixer from './converters/table-layout-post-fixer';
 import injectTableLayoutPostFixer from './converters/table-layout-post-fixer';
@@ -195,10 +195,7 @@ export default class TableEditing extends Plugin {
 
 
 		return ( domEventData, cancel ) => {
 		return ( domEventData, cancel ) => {
 			const selection = editor.model.document.selection;
 			const selection = editor.model.document.selection;
-
-			const firstPosition = selection.getFirstPosition();
-
-			const tableCell = findAncestor( 'tableCell', firstPosition );
+			const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
 
 
 			if ( !tableCell ) {
 			if ( !tableCell ) {
 				return;
 				return;

+ 5 - 2
packages/ckeditor5-table/src/tableselection.js

@@ -12,7 +12,10 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import TableWalker from './tablewalker';
 import TableWalker from './tablewalker';
 import TableUtils from './tableutils';
 import TableUtils from './tableutils';
 import MouseEventsObserver from './tableselection/mouseeventsobserver';
 import MouseEventsObserver from './tableselection/mouseeventsobserver';
-import { getSelectedTableCells } from './utils';
+import {
+	getSelectedTableCells,
+	getTableCellsContainingSelection
+} from './utils';
 import { findAncestor } from './commands/utils';
 import { findAncestor } from './commands/utils';
 import cropTable from './tableselection/croptable';
 import cropTable from './tableselection/croptable';
 
 
@@ -161,7 +164,7 @@ export default class TableSelection extends Plugin {
 				return;
 				return;
 			}
 			}
 
 
-			const anchorCell = findAncestor( 'tableCell', editor.model.document.selection.anchor.parent );
+			const anchorCell = getTableCellsContainingSelection( editor.model.document.selection )[ 0 ];
 
 
 			if ( !anchorCell ) {
 			if ( !anchorCell ) {
 				return;
 				return;

+ 3 - 2
packages/ckeditor5-table/tests/ui/utils.js

@@ -7,6 +7,7 @@ import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor'
 import Table from '../../src/table';
 import Table from '../../src/table';
 import TableCellProperties from '../../src/tablecellproperties';
 import TableCellProperties from '../../src/tablecellproperties';
 import { findAncestor } from '../../src/commands/utils';
 import { findAncestor } from '../../src/commands/utils';
+import { getTableCellsContainingSelection } from '../../src/utils';
 import global from '@ckeditor/ckeditor5-utils/src/dom/global';
 import global from '@ckeditor/ckeditor5-utils/src/dom/global';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import View from '@ckeditor/ckeditor5-ui/src/view';
 import View from '@ckeditor/ckeditor5-ui/src/view';
@@ -75,7 +76,7 @@ describe( 'UI Utils', () => {
 					'</tableRow></table>' );
 					'</tableRow></table>' );
 				repositionContextualBalloon( editor, 'cell' );
 				repositionContextualBalloon( editor, 'cell' );
 
 
-				const modelCell = findAncestor( 'tableCell', editor.model.document.selection.getFirstPosition() );
+				const modelCell = getTableCellsContainingSelection( editor.model.document.selection )[ 0 ];
 				const viewCell = editor.editing.mapper.toViewElement( modelCell );
 				const viewCell = editor.editing.mapper.toViewElement( modelCell );
 
 
 				sinon.assert.calledWithExactly( spy, {
 				sinon.assert.calledWithExactly( spy, {
@@ -160,7 +161,7 @@ describe( 'UI Utils', () => {
 			'</tableRow></table>' );
 			'</tableRow></table>' );
 
 
 			const data = getBalloonCellPositionData( editor );
 			const data = getBalloonCellPositionData( editor );
-			const modelCell = findAncestor( 'tableCell', editor.model.document.selection.getFirstPosition() );
+			const modelCell = getTableCellsContainingSelection( editor.model.document.selection )[ 0 ];
 			const viewCell = editor.editing.mapper.toViewElement( modelCell );
 			const viewCell = editor.editing.mapper.toViewElement( modelCell );
 
 
 			expect( data ).to.deep.equal( {
 			expect( data ).to.deep.equal( {