浏览代码

Extract clearTableCellsContents() util function.

Maciej Gołaszewski 5 年之前
父节点
当前提交
5b5466111a
共有 2 个文件被更改,包括 33 次插入12 次删除
  1. 2 12
      packages/ckeditor5-table/src/tableselection.js
  2. 31 0
      packages/ckeditor5-table/src/tableselection/utils.js

+ 2 - 12
packages/ckeditor5-table/src/tableselection.js

@@ -14,6 +14,7 @@ import TableUtils from './tableutils';
 import { setupTableSelectionHighlighting } from './tableselection/converters';
 import MouseSelectionHandler from './tableselection/mouseselectionhandler';
 import { findAncestor } from './commands/utils';
+import { clearTableCellsContents } from './tableselection/utils';
 import cropTable from './tableselection/croptable';
 
 import '../theme/tableselection.css';
@@ -107,7 +108,7 @@ export default class TableSelection extends Plugin {
 			if ( this.hasMultiCellSelection ) {
 				evt.stop();
 
-				this.clearSelectedTableCells();
+				clearTableCellsContents( editor.model, this.getSelectedTableCells() );
 			}
 		}, { priority: 'high' } );
 	}
@@ -251,17 +252,6 @@ export default class TableSelection extends Plugin {
 		} );
 	}
 
-	// TODO: helper function?
-	clearSelectedTableCells() {
-		const model = this.editor.model;
-
-		model.change( writer => {
-			for ( const tableCell of this.getSelectedTableCells() ) {
-				model.deleteContent( writer.createSelection( tableCell, 'in' ) );
-			}
-		} );
-	}
-
 	/**
 	 * Synchronizes the model selection with currently selected table cells.
 	 *

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

@@ -0,0 +1,31 @@
+/**
+ * @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/utils
+ */
+
+/**
+ * Clears contents of the passed table cells.
+ *
+ * This is to be used with table selection
+ *
+ *		tableSelection.startSelectingFrom( startCell )
+ *		tableSelection.setSelectingFrom( endCell )
+ *
+ *		clearTableCellsContents( editor.model, tableSelection.getSelectedTableCells() );
+ *
+ * **Note**: This function is used also by {@link module:table/tableselection~TableSelection#getSelectionAsFragment}
+ *
+ * @param {module:engine/model/model~Model} model
+ * @param {Iterable.<module:engine/model/element~Element>} tableCells
+ */
+export function clearTableCellsContents( model, tableCells ) {
+	model.change( writer => {
+		for ( const tableCell of tableCells ) {
+			model.deleteContent( writer.createSelection( tableCell, 'in' ) );
+		}
+	} );
+}