Browse Source

Merge branch 'master' into i/6126

~ Conflicts:
~	src/commands/removecolumncommand.js
~	src/commands/removerowcommand.js
Marek Lewandowski 5 years ago
parent
commit
dd8d41981f

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

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

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

@@ -9,7 +9,8 @@
 
 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.
@@ -33,10 +34,7 @@ export default class SetHeaderColumnCommand extends Command {
 	refresh() {
 		const model = this.editor.model;
 		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;
 
@@ -71,8 +69,7 @@ export default class SetHeaderColumnCommand extends Command {
 		const selection = doc.selection;
 		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 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 { createEmptyTableCell, findAncestor, updateNumericAttribute } from './utils';
+import {
+	createEmptyTableCell,
+	updateNumericAttribute
+} from './utils';
+import { getTableCellsContainingSelection } from '../utils';
 import TableWalker from '../tablewalker';
 
 /**
@@ -35,8 +39,7 @@ export default class SetHeaderRowCommand extends Command {
 		const doc = model.document;
 		const selection = doc.selection;
 
-		const position = selection.getFirstPosition();
-		const tableCell = findAncestor( 'tableCell', position );
+		const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
 		const isInTable = !!tableCell;
 
 		this.isEnabled = isInTable;
@@ -69,8 +72,7 @@ export default class SetHeaderRowCommand extends Command {
 		const doc = model.document;
 		const selection = doc.selection;
 
-		const position = selection.getFirstPosition();
-		const tableCell = findAncestor( 'tableCell', position );
+		const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
 		const tableRow = tableCell.parent;
 		const table = tableRow.parent;
 

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

@@ -28,7 +28,7 @@ import RemoveRowCommand from './commands/removerowcommand';
 import RemoveColumnCommand from './commands/removecolumncommand';
 import SetHeaderRowCommand from './commands/setheaderrowcommand';
 import SetHeaderColumnCommand from './commands/setheadercolumncommand';
-import { findAncestor } from './commands/utils';
+import { getTableCellsContainingSelection } from './utils';
 import TableUtils from '../src/tableutils';
 
 import injectTableLayoutPostFixer from './converters/table-layout-post-fixer';
@@ -195,10 +195,7 @@ export default class TableEditing extends Plugin {
 
 		return ( domEventData, cancel ) => {
 			const selection = editor.model.document.selection;
-
-			const firstPosition = selection.getFirstPosition();
-
-			const tableCell = findAncestor( 'tableCell', firstPosition );
+			const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
 
 			if ( !tableCell ) {
 				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 TableUtils from './tableutils';
 import MouseEventsObserver from './tableselection/mouseeventsobserver';
-import { getSelectedTableCells } from './utils';
+import {
+	getSelectedTableCells,
+	getTableCellsContainingSelection
+} from './utils';
 import { findAncestor } from './commands/utils';
 import cropTable from './tableselection/croptable';
 
@@ -161,7 +164,7 @@ export default class TableSelection extends Plugin {
 				return;
 			}
 
-			const anchorCell = findAncestor( 'tableCell', editor.model.document.selection.anchor.parent );
+			const anchorCell = getTableCellsContainingSelection( editor.model.document.selection )[ 0 ];
 
 			if ( !anchorCell ) {
 				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 TableCellProperties from '../../src/tablecellproperties';
 import { findAncestor } from '../../src/commands/utils';
+import { getTableCellsContainingSelection } from '../../src/utils';
 import global from '@ckeditor/ckeditor5-utils/src/dom/global';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import View from '@ckeditor/ckeditor5-ui/src/view';
@@ -75,7 +76,7 @@ describe( 'UI Utils', () => {
 					'</tableRow></table>' );
 				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 );
 
 				sinon.assert.calledWithExactly( spy, {
@@ -160,7 +161,7 @@ describe( 'UI Utils', () => {
 			'</tableRow></table>' );
 
 			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 );
 
 			expect( data ).to.deep.equal( {