From 9dd840c18bcd2ebdcec3ddbe0409778895c67435 Mon Sep 17 00:00:00 2001 From: Chase Date: Sat, 9 Nov 2024 17:22:22 -0600 Subject: [PATCH] Allows user to right-click on revealed clue to automatically paste answer and clue into search engine. Implements Add Shortcut to Copy on Grid #313 --- src/components/Grid/Grid.tsx | 29 ++++++++++++++++++++++++++++- src/components/Player/Player.js | 3 ++- src/lib/wrappers/GridWrapper.js | 30 +++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/components/Grid/Grid.tsx b/src/components/Grid/Grid.tsx index 7aef3135..7efcb72d 100644 --- a/src/components/Grid/Grid.tsx +++ b/src/components/Grid/Grid.tsx @@ -36,6 +36,9 @@ export interface GridProps { editMode: boolean; frozen: boolean; + // Clue lookup + clues: {across: string[]; down: string[]}; //figure out type + // callbacks onChangeDirection(): void; onSetSelected(cellCoords: CellCoords): void; @@ -119,7 +122,31 @@ export default class Grid extends React.PureComponent { }; handleRightClick = (r: number, c: number) => { - this.props.onPing && this.props.onPing(r, c); + const {direction} = this.props; + const clueNumber = this.grid.getParent(r, c, direction); + const answer = this.grid.isWordRevealed(direction, clueNumber); + const clue = this.props.clues[direction][clueNumber]; + if (answer !== '') { + window.open(`https://www.google.com/search?q=${answer}+${clue}`, '_blank'); + window.focus(); + } else { + this.props.onPing && this.props.onPing(r, c); + } + }; + + lookUpWordMeaning = (r: number, c: number) => { + const {direction} = this.props; + const clueNumber = this.grid.getParent(r, c, direction); + const answer = this.grid.isWordRevealed(direction); + const clue = ''; + + if (answer !== '') { + window.open( + `https://www.google.com/search?q=${answer}, + '_blank` + ); + window.focus(); + } }; getAllSquares() { diff --git a/src/components/Player/Player.js b/src/components/Player/Player.js index 5dd1127d..509224db 100644 --- a/src/components/Player/Player.js +++ b/src/components/Player/Player.js @@ -413,6 +413,7 @@ export default class Player extends Component { onChangeDirection: this._changeDirection, pickups, frozen, + clues, }; const clueProps = { @@ -566,7 +567,7 @@ export default class Player extends Component {
- +
diff --git a/src/lib/wrappers/GridWrapper.js b/src/lib/wrappers/GridWrapper.js index e40a22a9..3db2a64b 100644 --- a/src/lib/wrappers/GridWrapper.js +++ b/src/lib/wrappers/GridWrapper.js @@ -148,7 +148,6 @@ export default class GridWrapper { const _r = r; const _c = c; let {noWraparound = false, skipFirst = false} = options; - while (this.isWriteable(r, c)) { if (!this.isFilled(r, c)) { if (!skipFirst) { @@ -185,6 +184,31 @@ export default class GridWrapper { return !this.hasEmptyCells(clueRoot.r, clueRoot.c, direction); } + getRevealedWord(r, c, direction, answer = '') { + while (this.isWriteable(r, c)) { + if (!this.isFilled(r, c)) { + return ''; + } + if (this.isRevealed(r, c)) { + answer += this.grid[r][c].value; + } else { + return ''; + } + if (direction === 'across') { + c += 1; + } else { + r += 1; + } + } + return answer; + } + + isWordRevealed(direction, number) { + if (number === 0) return ''; + const clueRoot = this.getCellByNumber(number); + return this.getRevealedWord(clueRoot.r, clueRoot.c, direction); + } + getNextClue(clueNumber, direction, clues, backwards, parallel) { clueNumber = parallel ? this.parallelMap[direction][clueNumber] : clueNumber; const add = backwards ? -1 : 1; @@ -280,6 +304,10 @@ export default class GridWrapper { return !this.grid[r][c].black; } + isRevealed(r, c) { + return this.grid[r][c].revealed; + } + isWriteable(r, c) { return this.isInBounds(r, c) && this.isWhite(r, c); }