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

94.44% Statements 17/18
50% Branches 3/6
100% Functions 2/2
94.44% Lines 17/18
1 function Ignored     
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 78                                                                                                                         
/*!
 * VisualEditor ContentEditable table delete key down handler
 *
 * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/* 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 */
 
/**
 * @inheritdoc
 *
 * 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.
 */
ve.ce.TableDeleteKeyDownHandler.static.execute = function ( surface, e ) {
	var i, l,
		surfaceModel = surface.getModel(),
		documentModel = surfaceModel.getDocument(),
		fragments = [],
		cells = surfaceModel.getSelection().getMatrixCells( documentModel );
 
	Eif ( e ) {
		e.preventDefault();
	}
 
	Iif ( surface.isReadOnly() ) {
		return true;
	}
 
	for ( 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 ( 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 );