Skip to content

Commit

Permalink
add toolabar buttons
Browse files Browse the repository at this point in the history
also, allow single-character words
  • Loading branch information
chrisrude committed Jul 19, 2023
1 parent 4691585 commit 0c5fc28
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 32 deletions.
104 changes: 80 additions & 24 deletions src/lib/components/ClueGrid.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,55 @@
}
};
function findSquareToGroup(): [number, number] | null {
// are we using the current square?
let startRow,
startCol = -1;
if (
puzzle.grid[selectedRow][selectedCol].text === ' ' ||
isInWord(puzzle, selectedRow, selectedCol, isUsingBand())
) {
// is the previous square in the row/band filled in?
[startRow, startCol] = nextCell(true);
} else {
startRow = selectedRow;
startCol = selectedCol;
}
if (startRow === -1 || startCol === -1) {
// nothing to do
return null;
}
if (puzzle.grid[startRow][startCol].text === ' ') {
return null;
}
if (isInWord(puzzle, startRow, startCol, isUsingBand())) {
return null;
}
return [startRow, startCol];
}
export const canGroupIntoAnswer = () => {
if (selectedRow === -1 || selectedCol === -1) {
return false;
}
if (selectedRow === center && selectedCol === center) {
return false;
}
return null != findSquareToGroup();
};
export const groupIntoAnswer = () => {
const result = findSquareToGroup();
if (result === null) {
return;
}
const [startRow, startCol] = result;
createWordAtLocation(puzzle, startRow, startCol, isUsingBand());
// do an assignment to trigger a re-render
mouseDragging = true;
mouseDragging = false;
};
function clearWordAtSelection(): boolean {
if (!isInWord(puzzle, selectedRow, selectedCol, isUsingBand())) {
return false;
Expand Down Expand Up @@ -170,27 +219,7 @@
case '.':
// find adjacent letters and mark them as a word
//
// are we using the current square?
let startRow,
startCol = -1;
if (
puzzle.grid[selectedRow][selectedCol].text === ' ' ||
isInWord(puzzle, selectedRow, selectedCol, isUsingBand())
) {
// is the previous square in the row/band filled in?
[startRow, startCol] = nextCell(true);
} else {
startRow = selectedRow;
startCol = selectedCol;
}
if (startRow === -1 || startCol === -1) {
// nothing to do
return;
}
createWordAtLocation(puzzle, startRow, startCol, isUsingBand());
// do an assignment to trigger a re-render
mouseDragging = true;
mouseDragging = false;
groupIntoAnswer();
event.preventDefault();
return;
Expand Down Expand Up @@ -508,9 +537,29 @@

<svelte:window on:keydown={onKeyDown} />

{#if currentClue}
<div class="grid-current-clue">{currentClue}</div>
{/if}
<div class="grid-current-clue">{currentClue}</div>

<div class="button-bar">
<button
on:click={toggleSelection}
title="Switch to row mode [space]"
disabled={selectedRow === -1 || selectedCol === -1 || highlightRow !== -1}>𐃘</button
>
<button
on:click={toggleSelection}
title="Switch to band mode [space]"
disabled={selectedRow === -1 || selectedCol === -1 || highlightBand !== -1}>⤵</button
>

<button
on:click={groupIntoAnswer}
disabled={selectedRow === -1 ||
selectedCol === -1 ||
(mouseDragging && !mouseDragging) ||
!canGroupIntoAnswer()}
title="Group these letters into answer [period]">⛶</button
>
</div>

<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
Expand Down Expand Up @@ -742,4 +791,11 @@
max-height: 3rem;
min-height: 3rem;
}
.button-bar {
display: flex;
justify-content: flex-end;
align-items: center;
margin-bottom: 1rem;
}
</style>
8 changes: 0 additions & 8 deletions src/lib/puzzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,6 @@ function createWordAtLocation(puzzle: Puzzle, i: number, j: number, useBand: boo
}
endIdx = k;
}
if (startIdx == endIdx) {
// don't add a single letter word
return;
}
addPuzzleWordBand(puzzle, bandNumber, startIdx, endIdx);
} else {
// look backwards for a square that has a letter in it and is not
Expand All @@ -316,10 +312,6 @@ function createWordAtLocation(puzzle: Puzzle, i: number, j: number, useBand: boo
}
endIdx = k;
}
// don't add a single letter word
if (startIdx == endIdx) {
return;
}
addPuzzleWordRow(puzzle, i, startIdx, endIdx);
}
}
Expand Down

0 comments on commit 0c5fc28

Please sign in to comment.