浏览代码

Make table paste work only for document selection.

Maciej Gołaszewski 5 年之前
父节点
当前提交
4926e94d86
共有 2 个文件被更改,包括 65 次插入1 次删除
  1. 11 1
      packages/ckeditor5-table/src/tableclipboard.js
  2. 54 0
      packages/ckeditor5-table/tests/tableclipboard-paste.js

+ 11 - 1
packages/ckeditor5-table/src/tableclipboard.js

@@ -93,8 +93,14 @@ export default class TableClipboard extends Plugin {
 	 * @private
 	 * @param evt
 	 * @param {module:engine/model/documentfragment~DocumentFragment|module:engine/model/item~Item} content The content to insert.
+	 * @param {module:engine/model/selection~Selectable} [selectable=model.document.selection]
+	 * The selection into which the content should be inserted. If not provided the current model document selection will be used.
 	 */
-	_onInsertContent( evt, content ) {
+	_onInsertContent( evt, content, selectable ) {
+		if ( selectable && !selectable.is( 'documentSelection' ) ) {
+			return;
+		}
+
 		const tableSelection = this.editor.plugins.get( TableSelection );
 		const selectedTableCells = tableSelection.getSelectedTableCells();
 
@@ -226,6 +232,10 @@ export default class TableClipboard extends Plugin {
 }
 
 function getTableFromContent( content ) {
+	if ( content.is( 'table' ) ) {
+		return content;
+	}
+
 	for ( const child of Array.from( content ) ) {
 		if ( child.is( 'table' ) ) {
 			return child;

+ 54 - 0
packages/ckeditor5-table/tests/tableclipboard-paste.js

@@ -20,6 +20,8 @@ import { assertSelectedCells, modelTable, viewTable } from './_utils/utils';
 
 import TableEditing from '../src/tableediting';
 import TableCellPropertiesEditing from '../src/tablecellproperties/tablecellpropertiesediting';
+import TableWalker from '../src/tablewalker';
+
 import TableClipboard from '../src/tableclipboard';
 
 describe( 'table clipboard', () => {
@@ -94,6 +96,58 @@ describe( 'table clipboard', () => {
 			] ) );
 		} );
 
+		it( 'should not alter model.insertContent if selectable is different from document selection', () => {
+			model.change( writer => {
+				writer.setSelection( modelRoot.getNodeByPath( [ 0, 0, 0 ] ), 0 );
+
+				const selectedTableCells = model.createSelection( [
+					model.createRangeOn( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) ),
+					model.createRangeOn( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) ),
+					model.createRangeOn( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) ),
+					model.createRangeOn( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) )
+				] );
+
+				const tableToInsert = editor.plugins.get( 'TableUtils' ).createTable( writer, 2, 2 );
+
+				for ( const { cell } of new TableWalker( tableToInsert ) ) {
+					writer.insertText( 'foo', cell.getChild( 0 ), 0 );
+				}
+
+				model.insertContent( tableToInsert, selectedTableCells );
+			} );
+
+			assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
+				[ '', '', '02', '03' ],
+				[ '', '', '12', '13' ],
+				[ '20', '21', '22', '23' ],
+				[ '30', '31', '32', '33' ]
+			] ) );
+		} );
+
+		it( 'should alter model.insertContent if selectable is  document selection', () => {
+			tableSelection.setCellSelection(
+				modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
+				modelRoot.getNodeByPath( [ 0, 1, 1 ] )
+			);
+
+			model.change( writer => {
+				const tableToInsert = editor.plugins.get( 'TableUtils' ).createTable( writer, 2, 2 );
+
+				for ( const { cell } of new TableWalker( tableToInsert ) ) {
+					writer.insertText( 'foo', cell.getChild( 0 ), 0 );
+				}
+
+				model.insertContent( tableToInsert, editor.model.document.selection );
+			} );
+
+			assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
+				[ 'foo', 'foo', '02', '03' ],
+				[ 'foo', 'foo', '12', '13' ],
+				[ '20', '21', '22', '23' ],
+				[ '30', '31', '32', '33' ]
+			] ) );
+		} );
+
 		it( 'should block non-rectangular selection', () => {
 			setModelData( model, modelTable( [
 				[ { contents: '00', colspan: 3 } ],