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

Internal: Split command is now disabled when multiple cells are selected.

Currently the logic uses yet another getSelectedCells() method. In next commit I'll propose changes to the sibling getTableCellsInSelection() method, so that it works for this use case.
Marek Lewandowski 5 лет назад
Родитель
Сommit
14cf1944bf

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

@@ -9,6 +9,7 @@
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import { findAncestor } from './utils';
+import { getSelectedCells } from '../tableselection/utils';
 
 /**
  * The split cell command.
@@ -46,12 +47,12 @@ export default class SplitCellCommand extends Command {
 	 * @inheritDoc
 	 */
 	refresh() {
-		const model = this.editor.model;
-		const doc = model.document;
+		const selectedCellsGenerator = getSelectedCells( this.editor, true );
 
-		const tableCell = findAncestor( 'tableCell', doc.selection.getFirstPosition() );
+		const tableCell = selectedCellsGenerator.next().value;
+		const hasMoreCells = selectedCellsGenerator.next().done == false;
 
-		this.isEnabled = !!tableCell;
+		this.isEnabled = !!tableCell && !hasMoreCells;
 	}
 
 	/**

+ 31 - 1
packages/ckeditor5-table/src/tableselection/utils.js

@@ -7,7 +7,7 @@
  * @module table/tableselection/utils
  */
 
-import { getRangeContainedElement } from '../commands/utils';
+import { getRangeContainedElement, findAncestor } from '../commands/utils';
 
 /**
  * Clears contents of the passed table cells.
@@ -49,3 +49,33 @@ export function getTableCellsInSelection( selection ) {
 
 	return cells;
 }
+
+/**
+ * Yields selected table cells.
+ *
+ * @private
+ * @param {module:core/editor/editor~Editor} editor
+ * @param {Boolean} [expandSelection=false] If set to `true` expands the selection to entire cell (if possible).
+ * @returns {Iterable.<module:engine/model/element~Element>}
+ */
+export function* getSelectedCells( editor, expandSelection = false ) {
+	const plugins = editor.plugins;
+	if ( plugins.has( 'TableSelection' ) ) {
+		const selectedCells = plugins.get( 'TableSelection' ).getSelectedTableCells();
+
+		if ( selectedCells ) {
+			for ( const cell of selectedCells ) {
+				yield cell;
+			}
+
+			return;
+		}
+	}
+
+	if ( expandSelection ) {
+		const cellAncestor = findAncestor( 'tableCell', editor.model.document.selection.getFirstPosition() );
+		if ( cellAncestor ) {
+			yield cellAncestor;
+		}
+	}
+}