Răsfoiți Sursa

Completed integration with other features and added some tests.

Piotrek Koszuliński 5 ani în urmă
părinte
comite
ea42d8e6cc

+ 1 - 0
packages/ckeditor5-table/package.json

@@ -21,6 +21,7 @@
     "@ckeditor/ckeditor5-clipboard": "^17.0.0",
     "@ckeditor/ckeditor5-editor-classic": "^17.0.0",
     "@ckeditor/ckeditor5-engine": "^17.0.0",
+    "@ckeditor/ckeditor5-horizontal-line": "^17.0.0",
     "@ckeditor/ckeditor5-image": "^17.0.0",
     "@ckeditor/ckeditor5-indent": "^17.0.0",
     "@ckeditor/ckeditor5-list": "^17.0.0",

+ 8 - 6
packages/ckeditor5-table/src/tableselection.js

@@ -304,12 +304,14 @@ export default class TableSelection extends Plugin {
 
 			clearTableCellsContents( model, selectedTableCells );
 
-			// The insertContent() helper passes the actual DocumentSelection,
-			// while the deleteContent() helper always operates on the abstract clones.
-			if ( selection.is( 'documentSelection' ) ) {
-				writer.setSelection( tableCellToSelect, 'in' );
-			} else {
-				selection.setTo( tableCellToSelect, 'in' );
+			const rangeToSelect = model.schema.getNearestSelectionRange( writer.createPositionAt( tableCellToSelect, 0 ) );
+
+			if ( rangeToSelect ) {
+				if ( selection.is( 'documentSelection' ) ) {
+					writer.setSelection( rangeToSelect );
+				} else {
+					selection.setTo( rangeToSelect );
+				}
 			}
 		} );
 	}

+ 68 - 4
packages/ckeditor5-table/tests/tableselection-integration.js

@@ -5,16 +5,22 @@
 
 /* globals document */
 
+import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
+
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
-import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import Delete from '@ckeditor/ckeditor5-typing/src/delete';
+import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
+import HorizontalLine from '@ckeditor/ckeditor5-horizontal-line/src/horizontalline';
 
 import TableEditing from '../src/tableediting';
 import TableSelection from '../src/tableselection';
+import TableClipboard from '../src/tableclipboard';
+
+import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+
 import { modelTable } from './_utils/utils';
 import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
-import Delete from '@ckeditor/ckeditor5-typing/src/delete';
-import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
 import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import Input from '@ckeditor/ckeditor5-typing/src/input';
 import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
@@ -207,12 +213,70 @@ describe( 'TableSelection - integration', () => {
 		} );
 	} );
 
+	describe( 'with other features', () => {
+		beforeEach( async () => {
+			await setupEditor( [ Clipboard, HorizontalLine ] );
+		} );
+
+		it( 'allows pasting over multi-cell selection', () => {
+			tableSelection._setCellSelection(
+				modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
+				modelRoot.getNodeByPath( [ 0, 1, 1 ] )
+			);
+
+			const dataTransferMock = {
+				getData: sinon.stub().withArgs( 'text/plain' ).returns( 'foo' )
+			};
+
+			editor.editing.view.document.fire( 'clipboardInput', {
+				dataTransfer: dataTransferMock,
+				stop: sinon.spy()
+			} );
+
+			assertEqualMarkup( getModelData( model ), modelTable( [
+				[ 'foo[]', '', '13' ],
+				[ '', '', '23' ],
+				[ '31', '32', '33' ]
+			] ) );
+		} );
+
+		it( 'allows inserting a horizontal line over a multi-range selection', () => {
+			tableSelection._setCellSelection(
+				modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
+				modelRoot.getNodeByPath( [ 0, 1, 1 ] )
+			);
+
+			editor.execute( 'horizontalLine' );
+
+			assertEqualMarkup(
+				getModelData( model ),
+				'<table>' +
+					'<tableRow>' +
+						'<tableCell><horizontalLine></horizontalLine><paragraph>[]</paragraph></tableCell>' +
+						'<tableCell><paragraph></paragraph></tableCell>' +
+						'<tableCell><paragraph>13</paragraph></tableCell>' +
+					'</tableRow>' +
+					'<tableRow>' +
+						'<tableCell><paragraph></paragraph></tableCell>' +
+						'<tableCell><paragraph></paragraph></tableCell>' +
+						'<tableCell><paragraph>23</paragraph></tableCell>' +
+					'</tableRow>' +
+					'<tableRow>' +
+						'<tableCell><paragraph>31</paragraph></tableCell>' +
+						'<tableCell><paragraph>32</paragraph></tableCell>' +
+						'<tableCell><paragraph>33</paragraph></tableCell>' +
+					'</tableRow>' +
+				'</table>'
+			);
+		} );
+	} );
+
 	async function setupEditor( plugins ) {
 		element = document.createElement( 'div' );
 		document.body.appendChild( element );
 
 		editor = await ClassicTestEditor.create( element, {
-			plugins: [ TableEditing, TableSelection, Paragraph, ...plugins ]
+			plugins: [ TableEditing, TableSelection, TableClipboard, Paragraph, ...plugins ]
 		} );
 
 		model = editor.model;