onSelect()

in static/src/javascripts/projects/common/modules/crosswords/crossword.js [164:217]


	onSelect(x, y) {
		const cellInFocus = this.state.cellInFocus;
		const clue = cluesFor(this.clueMap, x, y);
		const focussedClue = this.clueInFocus();
		let newDirection;

		const isInsideFocussedClue = () =>
			focussedClue ? entryHasCell(focussedClue, x, y) : false;

		if (
			cellInFocus &&
			cellInFocus.x === x &&
			cellInFocus.y === y &&
			this.state.directionOfEntry
		) {
			/** User has clicked again on the highlighted cell, meaning we ought to swap direction */
			newDirection = otherDirection(this.state.directionOfEntry);

			if (clue[newDirection]) {
				this.focusClue(x, y, newDirection);
			}
		} else if (isInsideFocussedClue() && this.state.directionOfEntry) {
			/**
			 * If we've clicked inside the currently highlighted clue, then we ought to just shift the cursor
			 * to the new cell, not change direction or anything funny.
			 */

			this.focusClue(x, y, this.state.directionOfEntry);
		} else {
			this.state.cellInFocus = {
				x,
				y,
			};

			const isStartOfClue = (sourceClue) =>
				!!sourceClue &&
				sourceClue.position.x === x &&
				sourceClue.position.y === y;

			/**
			 * If the user clicks on the start of a down clue midway through an across clue, we should
			 * prefer to highlight the down clue.
			 */
			if (!isStartOfClue(clue.across) && isStartOfClue(clue.down)) {
				newDirection = 'down';
			} else if (clue.across) {
				/** Across is the default focus otherwise */
				newDirection = 'across';
			} else {
				newDirection = 'down';
			}
			this.focusClue(x, y, newDirection);
		}
	}