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

89.69% Statements 87/97
84.62% Branches 55/65
100% Functions 5/5
89.69% Lines 87/97
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 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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267                                                                25×                     25×   25×       24× 24×       23×       23×   23×       22×         22×   22× 22×       22×   22×                           20×   19×                                                     20× 20× 48× 18×   30×           30× 30×   28×     20×     22×       18×   18× 18×   18×                                                                 10×         22× 18× 18×       22× 21×     22×                 22×     22×   22× 22×     22×          
/*!
 * VisualEditor ContentEditable linear enter key down handler
 *
 * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/* istanbul ignore next */
/**
 * Enter key down handler for linear selections.
 *
 * @class
 * @extends ve.ce.KeyDownHandler
 *
 * @constructor
 */
ve.ce.LinearEnterKeyDownHandler = function VeCeLinearEnterKeyDownHandler() {
	// Parent constructor - never called because class is fully static
	// ve.ui.LinearEnterKeyDownHandler.super.apply( this, arguments );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ce.LinearEnterKeyDownHandler, ve.ce.KeyDownHandler );
 
/* Static properties */
 
ve.ce.LinearEnterKeyDownHandler.static.name = 'linearEnter';
 
ve.ce.LinearEnterKeyDownHandler.static.keys = [ OO.ui.Keys.ENTER ];
 
ve.ce.LinearEnterKeyDownHandler.static.supportedSelections = [ 'linear' ];
 
/* Static methods */
 
/**
 * @inheritdoc
 */
ve.ce.LinearEnterKeyDownHandler.static.execute = function ( surface, e ) {
	var txRemove, txInsert, outerParent, outerChildrenCount, container, prevContentOffset,
		insertEmptyParagraph, node, focusedNode, splitData,
		range = surface.model.getSelection().getRange(),
		cursor = range.from,
		documentModel = surface.model.getDocument(),
		emptyParagraph = [ { type: 'paragraph' }, { type: '/paragraph' } ],
		advanceCursor = true,
		outermostNode = null,
		nodeModel = null,
		nodeModelRange = null;
 
	e.preventDefault();
 
	if ( e.ctrlKey || e.metaKey ) {
		// CTRL+Enter triggers the submit command
		return false;
	}
 
	focusedNode = surface.getFocusedNode();
	if ( focusedNode ) {
		Eif ( focusedNode.getModel().isEditable() ) {
			focusedNode.executeCommand();
		}
		return true;
	}
 
	Iif ( surface.isReadOnly() ) {
		return true;
	}
 
	node = surface.getDocument().getBranchNodeFromOffset( range.from );
 
	if ( !node.isMultiline() ) {
		return true;
	}
 
	// Handle removal first
	if ( !range.isCollapsed() ) {
		txRemove = ve.dm.TransactionBuilder.static.newFromRemoval( documentModel, range );
		range = txRemove.translateRange( range );
		// We do want this to propagate to the surface
		surface.model.change( txRemove, new ve.dm.LinearSelection( range ) );
		// Remove may have changed node at range.from
		node = surface.getDocument().getBranchNodeFromOffset( range.from );
	}
 
	Eif ( node !== null ) {
		// Assertion: node is certainly a contentBranchNode
		nodeModel = node.getModel();
		nodeModelRange = nodeModel.getRange();
	}
 
	// Handle insertion
	Iif ( node === null ) {
		throw new Error( 'node === null' );
	} else if (
		nodeModel.getType() !== 'paragraph' &&
		(
			cursor === nodeModelRange.from ||
			cursor === nodeModelRange.to
		)
	) {
		// If we're at the start/end of something that's not a paragraph, insert a paragraph
		// before/after. Insert after for empty nodes (from === to).
		if ( cursor === nodeModelRange.to ) {
			txInsert = ve.dm.TransactionBuilder.static.newFromInsertion(
				documentModel, nodeModel.getOuterRange().to, emptyParagraph
			);
		} else Eif ( cursor === nodeModelRange.from ) {
			txInsert = ve.dm.TransactionBuilder.static.newFromInsertion(
				documentModel, nodeModel.getOuterRange().from, emptyParagraph
			);
			advanceCursor = false;
		}
	} else if ( e.shiftKey && nodeModel.hasSignificantWhitespace() ) {
		// Insert newline
		txInsert = ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, range.from, '\n' );
	} else if ( !node.splitOnEnter() ) {
		// Cannot split, so insert some appropriate node
 
		insertEmptyParagraph = false;
		Eif ( documentModel.hasSlugAtOffset( range.from ) ) {
			insertEmptyParagraph = true;
		} else {
			prevContentOffset = documentModel.data.getNearestContentOffset(
				cursor,
				-1
			);
			if ( prevContentOffset === -1 ) {
				insertEmptyParagraph = true;
			}
		}
 
		Eif ( insertEmptyParagraph ) {
			txInsert = ve.dm.TransactionBuilder.static.newFromInsertion(
				documentModel, cursor, emptyParagraph
			);
		} else {
			// Act as if cursor were at previous content offset
			cursor = prevContentOffset;
			node = surface.documentView.getBranchNodeFromOffset( cursor );
			txInsert = undefined;
			// Continue to traverseUpstream below. That will succeed because all
			// ContentBranchNodes have splitOnEnter === true.
		}
		insertEmptyParagraph = undefined;
	}
 
	// Assertion: if txInsert === undefined then node.splitOnEnter() === true
 
	function getSplitData( node ) {
		var stack = [];
		node.traverseUpstream( function ( node ) {
			if ( !node.splitOnEnter() ) {
				return false;
			}
			stack.splice(
				stack.length / 2,
				0,
				{ type: '/' + node.type },
				node.getModel().getClonedElement()
			);
			outermostNode = node;
			if ( e.shiftKey ) {
				return false;
			} else {
				return true;
			}
		} );
		return stack;
	}
 
	if ( txInsert === undefined ) {
		// This node has splitOnEnter = true. Traverse upstream until the first node
		// that has splitOnEnter = false, splitting each node as it is reached. Set
		// outermostNode to the last splittable node.
		splitData = getSplitData( node );
 
		outerParent = outermostNode.getParent();
		outerChildrenCount = outerParent.getChildren().length;
 
		if (
			// Parent removes empty last children
			outerParent.removeEmptyLastChildOnEnter() &&
			// This is the last child
			outerParent.getChildren()[ outerChildrenCount - 1 ] === outermostNode && (
				// Contains one empty ContentBranchNode
				( outermostNode.children.length === 1 && node.getModel().length === 0 ) ||
				// ..or is an empty ContentBranchNode
				( outermostNode.canContainContent() && outermostNode.getModel().length === 0 )
			)
		) {
			// Enter was pressed in an empty last child
			container = outerParent.getParent();
			advanceCursor = false;
			if ( outerChildrenCount === 1 ) {
				// The item we're about to remove is the only child
				// Remove the ouerParent
				txInsert = ve.dm.TransactionBuilder.static.newFromRemoval(
					documentModel, outerParent.getOuterRange()
				);
			} else {
				// Remove the item
				txInsert = ve.dm.TransactionBuilder.static.newFromRemoval(
					documentModel, outermostNode.getOuterRange()
				);
			}
 
			surface.model.change( txInsert );
			range = txInsert.translateRange( range );
 
			// The removed item was in a splitOnEnter node, split it
			if ( container.splitOnEnter() ) {
				splitData = getSplitData( container ).concat( emptyParagraph );
				txInsert = ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, container.getOuterRange().to - 1, splitData );
			} else if ( outerParent.getChildren().length ) {
				// Otherwise just insert a paragraph
				txInsert = ve.dm.TransactionBuilder.static.newFromInsertion(
					documentModel, outerParent.getOuterRange().to, emptyParagraph
				);
			} else {
				// Parent was emptied, nothing more to do
				txInsert = null;
			}
			// Advance the cursor into the new paragraph
			advanceCursor = true;
		} else {
			// We must process the transaction first because getRelativeContentOffset can't help us yet
			txInsert = ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, range.from, splitData );
		}
	}
 
	// Commit the transaction
	if ( txInsert ) {
		surface.model.change( txInsert );
		range = txInsert.translateRange( range );
	}
 
	// Now we can move the cursor forward
	if ( advanceCursor ) {
		cursor = documentModel.data.getRelativeContentOffset( range.from, 1 );
	} else {
		cursor = documentModel.data.getNearestContentOffset( range.from );
	}
	Iif ( cursor === -1 ) {
		// Cursor couldn't be placed in a nearby content node, so create an empty paragraph
		surface.model.change(
			ve.dm.TransactionBuilder.static.newFromInsertion(
				documentModel, range.from, emptyParagraph
			)
		);
		surface.model.setLinearSelection( new ve.Range( range.from + 1 ) );
	} else {
		surface.model.setLinearSelection( new ve.Range( cursor ) );
	}
	// Reset and resume polling
	surface.surfaceObserver.clear();
	// TODO: This setTimeout appears to be unnecessary (we're not render-locked)
	setTimeout( function () {
		surface.checkSequences();
	} );
 
	return true;
};
 
/* Registration */
 
ve.ce.keyDownHandlerFactory.register( ve.ce.LinearEnterKeyDownHandler );