8
0
Piotrek Koszuliński 5 лет назад
Родитель
Сommit
27318f7146

+ 3 - 12
packages/ckeditor5-table/src/tableclipboard.js

@@ -39,15 +39,6 @@ export default class TableClipboard extends Plugin {
 		const editor = this.editor;
 		const viewDocument = editor.editing.view.document;
 
-		/**
-		 * A table selection plugin instance.
-		 *
-		 * @private
-		 * @readonly
-		 * @member {module:table/tableselection~TableSelection} module:tableclipboard~TableClipboard#_tableSelection
-		 */
-		this._tableSelection = editor.plugins.get( 'TableSelection' );
-
 		this.listenTo( viewDocument, 'copy', ( evt, data ) => this._onCopyCut( evt, data ) );
 		this.listenTo( viewDocument, 'cut', ( evt, data ) => this._onCopyCut( evt, data ) );
 	}
@@ -60,9 +51,9 @@ export default class TableClipboard extends Plugin {
 	 * @private
 	 */
 	_onCopyCut( evt, data ) {
-		const tableSelection = this._tableSelection;
+		const tableSelection = this.editor.plugins.get( 'TableSelection' );
 
-		if ( !tableSelection.hasMultiCellSelection ) {
+		if ( !tableSelection.getSelectedTableCells() ) {
 			return;
 		}
 
@@ -72,7 +63,7 @@ export default class TableClipboard extends Plugin {
 		const dataController = this.editor.data;
 		const viewDocument = this.editor.editing.view.document;
 
-		const content = dataController.toView( this._tableSelection.getSelectionAsFragment() );
+		const content = dataController.toView( tableSelection.getSelectionAsFragment() );
 
 		viewDocument.fire( 'clipboardOutput', {
 			dataTransfer: data.dataTransfer,

+ 17 - 0
packages/ckeditor5-table/src/tableselection.js

@@ -16,6 +16,7 @@ import TableUtils from './tableutils';
 import MouseEventsObserver from './tableselection/mouseeventsobserver';
 import { getTableCellsInSelection, clearTableCellsContents } from './tableselection/utils';
 import { findAncestor } from './commands/utils';
+import cropTable from './tableselection/croptable';
 
 import '../theme/tableselection.css';
 
@@ -79,6 +80,22 @@ export default class TableSelection extends Plugin {
 		return selectedCells;
 	}
 
+	/**
+	 * Returns a selected table fragment as a document fragment.
+	 *
+	 * @returns {module:engine/model/documentfragment~DocumentFragment}
+	 */
+	getSelectionAsFragment() {
+		return this.editor.model.change( writer => {
+			const documentFragment = writer.createDocumentFragment();
+			const table = cropTable( this.getSelectedTableCells(), this.editor.plugins.get( 'TableUtils' ), writer );
+
+			writer.insert( table, documentFragment, 0 );
+
+			return documentFragment;
+		} );
+	}
+
 	/**
 	 * Defines a selection converter which marks selected cells with a specific class.
 	 *

+ 0 - 165
packages/ckeditor5-table/src/tableselection/mouseselectionhandler.js

@@ -1,165 +0,0 @@
-/**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
-
-/**
- * @module table/tableselection/mouseselectionhandler
- */
-
-import mix from '@ckeditor/ckeditor5-utils/src/mix';
-import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
-
-import { findAncestor } from '../commands/utils';
-import MouseEventsObserver from './mouseeventsobserver';
-
-/**
- * A mouse selection handler class for the table selection.
- *
- * It registers the {@link module:table/tableselection/mouseeventsobserver~MouseEventsObserver} to observe view document mouse events
- * and invoke proper {@link module:table/tableselection~TableSelection} actions.
- */
-export default class MouseSelectionHandler {
-	/**
-	 * Creates an instance of the `MouseSelectionHandler`.
-	 *
-	 * @param {module:table/tableselection~TableSelection} tableSelection
-	 * @param {module:engine/controller/editingcontroller~EditingController} editing
-	 */
-	constructor( tableSelection, editing ) {
-		/**
-		 * The table selection plugin instance.
-		 *
-		 * @private
-		 * @readonly
-		 * @member {module:table/tableselection~TableSelection}
-		 */
-		this._tableSelection = tableSelection;
-
-		/**
-		 * A flag indicating that the mouse selection is "active". A selection is "active" if it was started and not yet finished.
-		 * A selection can be "active", for instance, if a user moves a mouse over a table while holding a mouse button down.
-		 *
-		 * @private
-		 * @readonly
-		 * @member {Boolean}
-		 */
-		this._isSelecting = false;
-
-		/**
-		 * Editing mapper.
-		 *
-		 * @private
-		 * @readonly
-		 * @member {module:engine/conversion/mapper~Mapper}
-		 */
-		this._mapper = editing.mapper;
-
-		const view = editing.view;
-
-		// Currently the MouseObserver only handles `mouseup` events.
-		view.addObserver( MouseEventsObserver );
-
-		this.listenTo( view.document, 'mousedown', ( event, domEventData ) => this._handleMouseDown( domEventData ) );
-		this.listenTo( view.document, 'mousemove', ( event, domEventData ) => this._handleMouseMove( domEventData ) );
-		this.listenTo( view.document, 'mouseup', ( event, domEventData ) => this._handleMouseUp( domEventData ) );
-	}
-
-	/**
-	 * Handles starting a selection when "mousedown" event has table cell as a DOM target.
-	 *
-	 * If there is no table cell in the event target, it will clear the previous selection.
-	 *
-	 * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
-	 * @private
-	 */
-	_handleMouseDown( domEventData ) {
-		const tableCell = this._getModelTableCellFromDomEvent( domEventData );
-
-		if ( !tableCell ) {
-			this._tableSelection.clearSelection();
-
-			return;
-		}
-
-		this._isSelecting = true;
-		this._tableSelection.startSelectingFrom( tableCell );
-	}
-
-	/**
-	 * Handles updating the table selection when the "mousemove" event has a table cell as a DOM target.
-	 *
-	 * Does nothing if there is no table cell in event target or the selection has not been started yet.
-	 *
-	 * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
-	 * @private
-	 */
-	_handleMouseMove( domEventData ) {
-		if ( !this._isSelecting || !isButtonPressed( domEventData ) ) {
-			this._tableSelection.stopSelection();
-
-			return;
-		}
-
-		// https://github.com/ckeditor/ckeditor5/issues/6114
-		domEventData.preventDefault();
-
-		const tableCell = this._getModelTableCellFromDomEvent( domEventData );
-
-		if ( !tableCell ) {
-			return;
-		}
-
-		this._tableSelection.setSelectingTo( tableCell );
-	}
-
-	/**
-	 * Handles ending (not clearing) the table selection on the "mouseup" event.
-	 *
-	 * Does nothing if the selection has not been started yet.
-	 *
-	 * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
-	 * @private
-	 */
-	_handleMouseUp( domEventData ) {
-		if ( !this._isSelecting ) {
-			return;
-		}
-
-		this._isSelecting = false;
-
-		const tableCell = this._getModelTableCellFromDomEvent( domEventData );
-
-		// Selection can be stopped if table cell is undefined.
-		this._tableSelection.stopSelection( tableCell );
-	}
-
-	/**
-	 * Finds a model table cell for a given DOM event.
-	 *
-	 * @private
-	 * @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
-	 * @returns {module:engine/model/element~Element|undefined} Returns model table cell or undefined event target is not
-	 * a mapped table cell.
-	 */
-	_getModelTableCellFromDomEvent( domEventData ) {
-		const viewTargetElement = domEventData.target;
-		const modelElement = this._mapper.toModelElement( viewTargetElement );
-
-		if ( !modelElement ) {
-			return;
-		}
-
-		if ( modelElement.is( 'tableCell' ) ) {
-			return modelElement;
-		}
-
-		return findAncestor( 'tableCell', modelElement );
-	}
-}
-
-mix( MouseSelectionHandler, ObservableMixin );
-
-function isButtonPressed( domEventData ) {
-	return !!domEventData.domEvent.buttons;
-}