8
0
Quellcode durchsuchen

Merge branch 'master' into i/6127

~ Conflicts:
~	src/commands/setheadercolumncommand.js
~	src/commands/setheaderrowcommand.js
Marek Lewandowski vor 5 Jahren
Ursprung
Commit
6b7afe779f

+ 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 - 6
packages/ckeditor5-table/src/commands/removecolumncommand.js

@@ -10,7 +10,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 remove column command.
@@ -31,8 +32,7 @@ export default class RemoveColumnCommand extends Command {
 		const editor = this.editor;
 		const selection = editor.model.document.selection;
 		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;
 	}
@@ -44,9 +44,7 @@ export default class RemoveColumnCommand extends Command {
 		const model = this.editor.model;
 		const selection = model.document.selection;
 
-		const firstPosition = selection.getFirstPosition();
-
-		const tableCell = findAncestor( 'tableCell', firstPosition );
+		const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
 		const tableRow = tableCell.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 TableWalker from '../tablewalker';
-import { findAncestor, updateNumericAttribute } from './utils';
+import { updateNumericAttribute } from './utils';
+import { getTableCellsContainingSelection } from '../utils';
 
 /**
  * The remove row command.
@@ -30,8 +31,7 @@ export default class RemoveRowCommand extends Command {
 	refresh() {
 		const model = this.editor.model;
 		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;
 	}
@@ -42,9 +42,7 @@ export default class RemoveRowCommand extends Command {
 	execute() {
 		const model = this.editor.model;
 		const selection = model.document.selection;
-
-		const firstPosition = selection.getFirstPosition();
-		const tableCell = findAncestor( 'tableCell', firstPosition );
+		const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
 		const tableRow = tableCell.parent;
 		const table = tableRow.parent;
 

+ 6 - 16
packages/ckeditor5-table/src/commands/splitcellcommand.js

@@ -8,7 +8,7 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import { findAncestor } from './utils';
+import { getSelectionAffectedTableCells } from '../utils';
 
 /**
  * The split cell command.
@@ -46,30 +46,20 @@ export default class SplitCellCommand extends Command {
 	 * @inheritDoc
 	 */
 	refresh() {
-		const model = this.editor.model;
-		const doc = model.document;
+		const selectedCells = getSelectionAffectedTableCells( this.editor.model.document.selection );
 
-		const tableCell = findAncestor( 'tableCell', doc.selection.getFirstPosition() );
-
-		this.isEnabled = !!tableCell;
+		this.isEnabled = selectedCells.length === 1;
 	}
 
 	/**
 	 * @inheritDoc
 	 */
 	execute() {
-		const model = this.editor.model;
-		const document = model.document;
-		const selection = document.selection;
-
-		const firstPosition = selection.getFirstPosition();
-		const tableCell = findAncestor( 'tableCell', firstPosition );
-
-		const isHorizontally = this.direction === 'horizontally';
-
+		const tableCell = getSelectionAffectedTableCells( this.editor.model.document.selection )[ 0 ];
+		const isHorizontal = this.direction === 'horizontally';
 		const tableUtils = this.editor.plugins.get( 'TableUtils' );
 
-		if ( isHorizontally ) {
+		if ( isHorizontal ) {
 			tableUtils.splitCellHorizontally( tableCell, 2 );
 		} else {
 			tableUtils.splitCellVertically( tableCell, 2 );

+ 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;

+ 32 - 1
packages/ckeditor5-table/tests/commands/splitcellcommand.js

@@ -8,6 +8,7 @@ import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model
 
 import SplitCellCommand from '../../src/commands/splitcellcommand';
 import { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
+import TableSelection from '../../src/tableselection';
 import TableUtils from '../../src/tableutils';
 import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
@@ -17,7 +18,7 @@ describe( 'SplitCellCommand', () => {
 	beforeEach( () => {
 		return ModelTestEditor
 			.create( {
-				plugins: [ TableUtils ]
+				plugins: [ TableUtils, TableSelection ]
 			} )
 			.then( newEditor => {
 				editor = newEditor;
@@ -47,6 +48,36 @@ describe( 'SplitCellCommand', () => {
 				expect( command.isEnabled ).to.be.true;
 			} );
 
+			it( 'should be true if in an entire cell is selected', () => {
+				setData( model, modelTable( [
+					[ '00', '01' ]
+				] ) );
+
+				const tableSelection = editor.plugins.get( TableSelection );
+				const modelRoot = model.document.getRoot();
+				tableSelection._setCellSelection(
+					modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
+					modelRoot.getNodeByPath( [ 0, 0, 0 ] )
+				);
+
+				expect( command.isEnabled ).to.be.true;
+			} );
+
+			it( 'should be false if multiple cells are selected', () => {
+				setData( model, modelTable( [
+					[ '00', '01' ]
+				] ) );
+
+				const tableSelection = editor.plugins.get( TableSelection );
+				const modelRoot = model.document.getRoot();
+				tableSelection._setCellSelection(
+					modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
+					modelRoot.getNodeByPath( [ 0, 0, 1 ] )
+				);
+
+				expect( command.isEnabled ).to.be.false;
+			} );
+
 			it( 'should be false if not in cell', () => {
 				setData( model, '<paragraph>11[]</paragraph>' );
 

+ 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( {