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

100% Statements 29/29
90.47% Branches 19/21
100% Functions 1/1
100% Lines 29/29

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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                                            1x       1x   1x   1x                 1x 6x 6x 6x   6x 5x       1x     4x   2x 2x 2x     2x 2x               2x   2x   2x 2x   2x 2x 2x   1x     1x   2x     1x         1x  
/*!
 * VisualEditor ContentEditable linear escape key down handler
 *
 * @copyright See AUTHORS.txt
 */
 
/* istanbul ignore next */
/**
 * Tab key down handler for linear selections.
 *
 * @class
 * @extends ve.ce.KeyDownHandler
 *
 * @constructor
 */
ve.ce.LinearTabKeyDownHandler = function VeCeLinearTabKeyDownHandler() {
	// Parent constructor - never called because class is fully static
	// ve.ui.LinearTabKeyDownHandler.super.apply( this, arguments );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ce.LinearTabKeyDownHandler, ve.ce.KeyDownHandler );
 
/* Static properties */
 
ve.ce.LinearTabKeyDownHandler.static.name = 'linearTab';
 
ve.ce.LinearTabKeyDownHandler.static.keys = [ OO.ui.Keys.TAB ];
 
ve.ce.LinearTabKeyDownHandler.static.supportedSelections = [ 'linear' ];
 
/* Static methods */
 
/**
 * Handle tab key down events with a linear selection while table editing.
 *
 * @inheritdoc
 */
ve.ce.LinearTabKeyDownHandler.static.execute = function ( surface, e ) {
	const activeTableNode = surface.getActiveNode() && surface.getActiveNode().findParent( ve.ce.TableNode ),
		activeTableCaptionNode = surface.getActiveNode() && surface.getActiveNode().findParent( ve.ce.TableCaptionNode ),
		documentModel = surface.getModel().getDocument();
	// Check we have an active table node
	if ( activeTableNode ) {
		if ( e.ctrlKey || e.altKey || e.metaKey ) {
			// Support: Firefox
			// In Firefox, ctrl-tab to switch browser-tabs still triggers the
			// keydown event.
			return;
		}
 
		if ( activeTableNode.editingFragment ) {
			// we are inside a cell (editingFragment), and not just a caption
			e.preventDefault();
			e.stopPropagation();
			activeTableNode.setEditing( false );
			// If this was a merged cell, we're going to have unexpected behavior when the selection moves,
			// so preemptively collapse to the top-left point of the merged cell.
			surface.getModel().setSelection( surface.getModel().getSelection().collapseToStart() );
			ve.ce.TableArrowKeyDownHandler.static.moveTableSelection(
				surface,
				0, // Rows
				e.shiftKey ? -1 : 1, // Columns
				false, // Logical direction, not visual
				false, // Don't expand the current selection,
				true // Wrap to next/previous row
			);
			Eif ( surface.getModel().getSelection() instanceof ve.dm.TableSelection ) {
				// Might have moved off the table and into the caption
				activeTableNode.setEditing( true );
			}
			return true;
		} else Eif ( activeTableCaptionNode ) {
			// We're in a table caption
			e.preventDefault();
			e.stopPropagation();
			if ( e.shiftKey ) {
				// back out of the table
				surface.getModel().setSelection( new ve.dm.LinearSelection( documentModel.getRelativeRange( activeTableNode.getRange(), -1 ) ) );
			} else {
				// move to the first cell
				surface.getModel().setSelection( new ve.dm.TableSelection( activeTableNode.getOuterRange(), 0, 0 ) );
			}
			return true;
		}
	}
	return false;
};
 
/* Registration */
 
ve.ce.keyDownHandlerFactory.register( ve.ce.LinearTabKeyDownHandler );