Explorar el Código

Made the Model#deleteContent() hook in the TableSelection plugin... TableSelection-agnostic.

Aleksander Nowodzinski hace 5 años
padre
commit
17e4dd5537

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

@@ -14,7 +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 { getTableCellsInSelection, clearTableCellsContents } from './tableselection/utils';
 import cropTable from './tableselection/croptable';
 
 import '../theme/tableselection.css';
@@ -300,7 +300,7 @@ export default class TableSelection extends Plugin {
 		if ( selection.rangeCount > 1 ) {
 			const model = this.editor.model;
 			const isBackward = !options || options.direction == 'backward';
-			const selectedTableCells = [ ...this.getSelectedTableCells() ];
+			const selectedTableCells = getTableCellsInSelection( selection );
 
 			// There are possible multiple-range selection that have nothing to do with tables.
 			// Let's filter them out and focus on table cells only.
@@ -308,19 +308,20 @@ export default class TableSelection extends Plugin {
 				event.stop();
 
 				model.change( writer => {
-					clearTableCellsContents( model, selectedTableCells );
+					const tableCellToSelect = selectedTableCells[ isBackward ? selectedTableCells.length - 1 : 0 ];
 
-					const tableCell = isBackward ? this._endElement : this._startElement;
+					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( tableCell, 'in' );
+						writer.setSelection( tableCellToSelect, 'in' );
 					} else {
-						selection.setTo( tableCell, 'in' );
+						selection.setTo( tableCellToSelect, 'in' );
 					}
 				} );
 			}
 		}
 	}
 }
+

+ 20 - 2
packages/ckeditor5-table/src/tableselection/utils.js

@@ -17,8 +17,6 @@
  *
  *		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
  */
@@ -29,3 +27,23 @@ export function clearTableCellsContents( model, tableCells ) {
 		}
 	} );
 }
+
+/**
+ * Returns all model cells within the provided model selection.
+ *
+ * @param {Iterable.<module:engine/model/selection~Selection>} selection
+ * @returns {Array.<module:engine/model/element~Element>}
+ */
+export function getTableCellsInSelection( selection ) {
+	const cells = [];
+
+	for ( const range of selection.getRanges() ) {
+		for ( const item of range.getItems() ) {
+			if ( item.is( 'tableCell' ) ) {
+				cells.push( item );
+			}
+		}
+	}
+
+	return cells;
+}

+ 50 - 0
packages/ckeditor5-table/tests/tableselection-integration.js

@@ -95,6 +95,56 @@ describe( 'table selection', () => {
 					[ '31', '32', '33' ]
 				] ) );
 			} );
+
+			it( 'should work with any arbitrary selection passed to Model#deleteContent() (delete backwards)', () => {
+				const selection = model.createSelection( [
+					model.createRange(
+						model.createPositionFromPath( modelRoot, [ 0, 0, 0 ] ),
+						model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] )
+					),
+					model.createRange(
+						model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] ),
+						model.createPositionFromPath( modelRoot, [ 0, 0, 2 ] )
+					)
+				] );
+
+				model.change( writer => {
+					model.deleteContent( selection );
+					writer.setSelection( selection );
+				} );
+
+				assertEqualMarkup( getModelData( model ), modelTable( [
+					[ '', '[]', '13' ],
+					[ '21', '22', '23' ],
+					[ '31', '32', '33' ]
+				] ) );
+			} );
+
+			it( 'should work with any arbitrary selection passed to Model#deleteContent() (delete forwards)', () => {
+				const selection = model.createSelection( [
+					model.createRange(
+						model.createPositionFromPath( modelRoot, [ 0, 0, 0 ] ),
+						model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] )
+					),
+					model.createRange(
+						model.createPositionFromPath( modelRoot, [ 0, 0, 1 ] ),
+						model.createPositionFromPath( modelRoot, [ 0, 0, 2 ] )
+					)
+				] );
+
+				model.change( writer => {
+					model.deleteContent( selection, {
+						direction: 'forward'
+					} );
+					writer.setSelection( selection );
+				} );
+
+				assertEqualMarkup( getModelData( model ), modelTable( [
+					[ '[]', '', '13' ],
+					[ '21', '22', '23' ],
+					[ '31', '32', '33' ]
+				] ) );
+			} );
 		} );
 
 		describe( 'on user input', () => {