All files / src/ce/keydownhandlers ve.ce.TableDeleteKeyDownHandler.js

95.83% Statements 23/24
50% Branches 3/6
100% Functions 1/1
95% Lines 19/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                            1x       1x   1x   1x                     1x 1x 1x 1x 1x   1x 1x     1x       1x 5x   5x       1x   5x           1x         1x  
/*!
 * VisualEditor ContentEditable table delete key down handler
 *
 * @copyright See AUTHORS.txt
 */
 
/* istanbul ignore next */
/**
 * Delete key down handler for table selections.
 *
 * @class
 * @extends ve.ce.KeyDownHandler
 *
 * @constructor
 */
ve.ce.TableDeleteKeyDownHandler = function VeCeTableDeleteKeyDownHandler() {
	// Parent constructor - never called because class is fully static
	// ve.ui.TableDeleteKeyDownHandler.super.apply( this, arguments );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ce.TableDeleteKeyDownHandler, ve.ce.KeyDownHandler );
 
/* Static properties */
 
ve.ce.TableDeleteKeyDownHandler.static.name = 'tableDelete';
 
ve.ce.TableDeleteKeyDownHandler.static.keys = [ OO.ui.Keys.BACKSPACE, OO.ui.Keys.DELETE ];
 
ve.ce.TableDeleteKeyDownHandler.static.supportedSelections = [ 'table' ];
 
/* Static methods */
 
/**
 * Handle delete and backspace key down events with a table selection.
 *
 * Performs a strip-delete removing all the cell contents but not altering the structure.
 *
 * @inheritdoc
 */
ve.ce.TableDeleteKeyDownHandler.static.execute = function ( surface, e ) {
	const surfaceModel = surface.getModel(),
		documentModel = surfaceModel.getDocument(),
		fragments = [],
		cells = surfaceModel.getSelection().getMatrixCells( documentModel );
 
	Eif ( e ) {
		e.preventDefault();
	}
 
	Iif ( surface.isReadOnly() ) {
		return true;
	}
 
	for ( let i = 0, l = cells.length; i < l; i++ ) {
		Eif ( cells[ i ].node.isCellEditable() ) {
			// Create auto-updating fragments from ranges
			fragments.push( surfaceModel.getLinearFragment( cells[ i ].node.getRange(), true ) );
		}
	}
 
	for ( let i = 0, l = fragments.length; i < l; i++ ) {
		// Replace contents with empty wrapper paragraphs
		fragments[ i ].insertContent( [
			{ type: 'paragraph', internal: { generated: 'wrapper' } },
			{ type: '/paragraph' }
		] );
	}
 
	return true;
};
 
/* Registration */
 
ve.ce.keyDownHandlerFactory.register( ve.ce.TableDeleteKeyDownHandler );