From: Laurent Mazet Date: Sat, 4 Jul 2020 12:53:35 +0000 (+0200) Subject: correct highlighting when one puts a tile and create same word horizontally and verti... X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=aa2d1776eea4255c6f2ebfc1a80978d616ebbe23;p=trivabble.git correct highlighting when one puts a tile and create same word horizontally and vertically --- diff --git a/public/trivabble.js b/public/trivabble.js index 4bc9557..92c59c0 100644 --- a/public/trivabble.js +++ b/public/trivabble.js @@ -1529,6 +1529,22 @@ return (word.letters === initLetter) ? null : word; } + function isNewWord(newWord, words) { + if (newWord === null) { + return false; + } + for (const k of Object.keys(words)) { + if ((words[k].letters === newWord.letters) && + (words[k].row === newWord.row) && + (words[k].col === newWord.col) && + (words[k].incRow === newWord.incRow) && + (words[k].incCol === newWord.incCol)) { + return false; + } + } + return true; + } + function searchNewWords() { const words = []; @@ -1540,13 +1556,13 @@ /* Look for word in column */ const newWordInCol = searchWordInLine(row, col, 1, 0); - if ((newWordInCol !== null) && (!words.find(element => element.letters === newWordInCol.letters))) { + if (isNewWord(newWordInCol, words)) { words.push(newWordInCol); } /* Look for word in row */ const newWordInRow = searchWordInLine(row, col, 0, 1); - if ((newWordInRow !== null) && (!words.find(element => element.letters === newWordInRow.letters))) { + if (isNewWord(newWordInRow, words)) { words.push(newWordInRow); } @@ -1598,21 +1614,32 @@ function checkSpelling() { let newWords = searchNewWords(); + if (newWords.length === 0) { + myAlert(_("No new word found")); + return; + } /* Check if words are in dictionary */ let words = []; let badWords = []; for (const k of Object.keys(newWords)) { - words.push(newWords[k].letters.replace(/ /g, "?")); + let currentWord = newWords[k].letters.replace(/ /g, "?"); + if (words.indexOf(currentWord) === -1) { + words.push(currentWord); + } modifyTileClass(newWords[k], "tile-spelling-ok", true); - setTimeout(function () {modifyTileClass(newWords[k], "tile-spelling-ok", false);}, 5000); const pattern = "^" + newWords[k].letters.replace(/ /g, ".") + "$"; if (!dictionary.find(element => element.match(pattern))) { - badWords.push(newWords[k].letters.replace(/ /g, "?")); + if (badWords.indexOf(currentWord) === -1) { + badWords.push(currentWord); + } modifyTileClass(newWords[k], "tile-spelling-nok", true); - setTimeout(function () {modifyTileClass(newWords[k], "tile-spelling-nok", false);}, 5000); } } + setTimeout(function () { + modifyTileClass(null, "tile-spelling-ok", false); + modifyTileClass(null, "tile-spelling-nok", false); + }, 5000); words = words.join(", ").trim(); badWords = badWords.join(", ").trim(); @@ -1620,10 +1647,8 @@ if (badWords.length > 0) { const msg = chatMessage("", format(_("Incorrect spelling for {0} from {1}"), badWords, words)); msg.classList.add("error"); - } else if (words.length > 0) { - chatMessage("", format(_("All words are correct: {0}"), words)); } else { - myAlert(_("No new word found")); + chatMessage("", format(_("All words are correct: {0}"), words)); } }