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

95.12% Statements 78/82
88.23% Branches 60/68
100% Functions 5/5
95.12% Lines 78/82

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228                                            1x       1x   1x         1x             1x 45x     45x 45x 45x     45x   45x     22x                               25x 10x     15x 9x 6x 4x   2x       15x 8x     7x       8x   8x 8x 8x     45x 4x 4x         41x 15x 15x 4x 4x       37x 17x   17x             7x 3x   7x   7x   4x 4x   7x   10x   10x 10x 10x   4x 4x               33x     1x 1x 32x     2x 2x       33x 3x 3x 3x 3x 3x     33x 33x     33x     33x 33x   4x       29x                                             29x 29x       29x 13x 13x   29x 29x     33x         1x  
/*!
 * VisualEditor ContentEditable linear arrow key down handler
 *
 * @copyright See AUTHORS.txt
 */
 
/* istanbul ignore next */
/**
 * Arrow key down handler for linear selections.
 *
 * @class
 * @extends ve.ce.KeyDownHandler
 *
 * @constructor
 */
ve.ce.LinearArrowKeyDownHandler = function VeCeLinearArrowKeyDownHandler() {
	// Parent constructor - never called because class is fully static
	// ve.ui.LinearArrowKeyDownHandler.super.apply( this, arguments );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ce.LinearArrowKeyDownHandler, ve.ce.KeyDownHandler );
 
/* Static properties */
 
ve.ce.LinearArrowKeyDownHandler.static.name = 'linearArrow';
 
ve.ce.LinearArrowKeyDownHandler.static.keys = [
	OO.ui.Keys.UP, OO.ui.Keys.DOWN, OO.ui.Keys.LEFT, OO.ui.Keys.RIGHT,
	OO.ui.Keys.HOME, OO.ui.Keys.END, OO.ui.Keys.PAGEUP, OO.ui.Keys.PAGEDOWN
];
 
ve.ce.LinearArrowKeyDownHandler.static.supportedSelections = [ 'linear' ];
 
/* Static methods */
 
/**
 * @inheritdoc
 */
ve.ce.LinearArrowKeyDownHandler.static.execute = function ( surface, e ) {
	const isBlockMove = e.keyCode === OO.ui.Keys.UP || e.keyCode === OO.ui.Keys.DOWN ||
			e.keyCode === OO.ui.Keys.PAGEUP || e.keyCode === OO.ui.Keys.PAGEDOWN ||
			e.keyCode === OO.ui.Keys.HOME || e.keyCode === OO.ui.Keys.END,
		keyBlockDirection = e.keyCode === OO.ui.Keys.DOWN || e.keyCode === OO.ui.Keys.PAGEDOWN || e.keyCode === OO.ui.Keys.END ? 1 : -1,
		activeNode = surface.getActiveNode();
	let range = surface.model.getSelection().getRange();
 
	// TODO: onDocumentKeyDown did this already
	surface.surfaceObserver.stopTimerLoop();
	// TODO: onDocumentKeyDown did this already
	surface.surfaceObserver.pollOnce();
 
	function moveOffFocusableNode( focusableRange, dir ) {
		return surface.model.getDocument().getRelativeRange(
			focusableRange,
			dir,
			'character',
			e.shiftKey,
			activeNode && ( e.shiftKey || activeNode.trapsCursor() ) ? activeNode.getRange() : null
		);
	}
 
	/**
	 * Determine the direction to move based on the key pressed and text directionality.
	 *
	 * @param {ve.ce.Node|HTMLElement} nodeForDirection
	 * @return {number} -1 for backwards, 1 for forwards
	 */
	function getDirection( nodeForDirection ) {
		if ( isBlockMove ) {
			return keyBlockDirection;
		}
		let directionality;
		if ( nodeForDirection === surface.focusedNode ) {
			directionality = surface.getFocusedNodeDirectionality();
		} else if ( nodeForDirection instanceof ve.ce.Node ) {
			directionality = $( nodeForDirection.$element[ 0 ] ).css( 'direction' );
		} else {
			directionality = $( nodeForDirection ).css( 'direction' );
		}
		// Left arrow in ltr, or right arrow in rtl
		// eslint-disable-next-line no-bitwise
		if ( e.keyCode === OO.ui.Keys.LEFT ^ directionality === 'rtl' ) {
			return -1;
		}
		// Left arrow in rtl, or right arrow in ltr
		return 1;
	}
 
	function handleMove( nodeForDirection ) {
		const direction = getDirection( nodeForDirection );
		// Block level selection, so directionality is just css directionality
		range = moveOffFocusableNode( range, direction );
		surface.model.setLinearSelection( range );
		e.preventDefault();
	}
 
	if ( surface.focusedBlockSlug ) {
		handleMove( surface.focusedBlockSlug );
		return true;
	}
 
	// Empty paragraphs have inline slugs which cause the cursor to get
	// stuck, so move off them immediately (T162916)
	if ( range.isCollapsed() && !isBlockMove ) {
		const node = surface.getDocument().getBranchNodeFromOffset( range.from );
		if ( node.canContainContent() && node.getModel().length === 0 ) {
			handleMove( node );
			return true;
		}
	}
 
	if ( surface.focusedNode ) {
		const direction = getDirection( surface.focusedNode );
 
		if ( e.shiftKey ) {
			// There is no DOM range to expand (because the selection is faked), so
			// use "collapse to focus - observe - expand". Define "focus" to be the
			// edge of the focusedNode in the direction of motion (so the selection
			// always grows). This means that clicking on the focusableNode then
			// modifying the selection will always include the node.
			// eslint-disable-next-line no-bitwise
			if ( direction === -1 ^ range.isBackwards() ) {
				range = range.flip();
			}
			let observationRange = new ve.Range( range.to );
 
			if ( !surface.focusedNode.isContent() ) {
				// Block focusable node: move the observation range to an adjacent content offset
				observationRange = moveOffFocusableNode( observationRange, direction );
				observationRange = new ve.Range( observationRange.to );
			}
			surface.model.setLinearSelection( observationRange );
		} else {
			range = moveOffFocusableNode( range, direction );
			// Move to start/end of node in the model in DM (and DOM)
			range = new ve.Range( direction === 1 ? range.end : range.start );
			surface.model.setLinearSelection( range );
			if ( !isBlockMove ) {
				// Un-shifted left/right: we've already moved so preventDefault
				e.preventDefault();
				return true;
			}
			// Else keep going with the cursor in the new place
		}
	}
	// Else keep DM range and DOM selection as-is
 
	let collapseNode, collapseOffset;
	if ( e.shiftKey && !ve.supportsSelectionExtend && range.isBackwards() ) {
		// If the browser doesn't support backwards selections, but the dm range
		// is backwards, then use "collapse to anchor - observe - expand".
		collapseNode = surface.nativeSelection.anchorNode;
		collapseOffset = surface.nativeSelection.anchorOffset;
	} else if ( e.shiftKey && !range.isCollapsed() && isBlockMove ) {
		// If selection is expanded and cursoring is up/down, use
		// "collapse to focus - observe - expand" to work round quirks.
		collapseNode = surface.nativeSelection.focusNode;
		collapseOffset = surface.nativeSelection.focusOffset;
	}
	// Else don't collapse the selection
 
	if ( collapseNode ) {
		const nativeRange = surface.getElementDocument().createRange();
		nativeRange.setStart( collapseNode, collapseOffset );
		nativeRange.setEnd( collapseNode, collapseOffset );
		surface.nativeSelection.removeAllRanges();
		surface.nativeSelection.addRange( nativeRange );
	}
 
	const startFocusNode = surface.nativeSelection.focusNode;
	const startFocusOffset = surface.nativeSelection.focusOffset;
 
	// Re-expand (or fixup) the selection after the native action, if necessary
	surface.eventSequencer.afterOne( { keydown: function () {
		// Support: Chrome
		// Chrome bug lets you cursor into a multi-line contentEditable=false with up/down…
		const viewNode = $( surface.nativeSelection.focusNode ).closest( '.ve-ce-leafNode,.ve-ce-branchNode' ).data( 'view' );
		if ( !viewNode ) {
			// Irrelevant selection (or none)
			return;
		}
 
		let newRange;
		Iif ( viewNode.isFocusable() ) {
			let afterDirection;
			// We've landed in a focusable node; fixup the range
			if ( isBlockMove ) {
				// The intended direction is clear, even if the cursor did not move
				// or did something completely preposterous
				afterDirection = keyBlockDirection;
			} else {
				// Observe which way the cursor moved
				afterDirection = ve.compareDocumentOrder(
					surface.nativeSelection.focusNode,
					surface.nativeSelection.focusOffset,
					startFocusNode,
					startFocusOffset
				);
			}
			newRange = (
				afterDirection > 0 ?
					viewNode.getOuterRange() :
					viewNode.getOuterRange().flip()
			);
		} else {
			// Check where the range has moved to
			surface.surfaceObserver.pollOnceNoCallback();
			newRange = new ve.Range( surface.surfaceObserver.getRange().to );
		}
 
		// Adjust range to use old anchor, if necessary
		if ( e.shiftKey ) {
			newRange = new ve.Range( range.from, newRange.to );
			surface.getModel().setLinearSelection( newRange );
		}
		surface.updateActiveAnnotations();
		surface.surfaceObserver.pollOnce();
	} } );
 
	return true;
};
 
/* Registration */
 
ve.ce.keyDownHandlerFactory.register( ve.ce.LinearArrowKeyDownHandler );