UNPKG

@adamson/react-crossword

Version:

A flexible, responsive, and easy-to-use crossword component for React apps

235 lines (196 loc) 6.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isAcross = isAcross; exports.otherDirection = otherDirection; exports.createEmptyGuessState = createEmptyGuessState; exports.createGridData = createGridData; exports.byNumber = byNumber; exports.clearGuesses = clearGuesses; exports.saveGuesses = saveGuesses; exports.serializeGuesses = serializeGuesses; exports.loadGuesses = loadGuesses; exports.deserializeGuesses = deserializeGuesses; exports.findCorrectAnswers = findCorrectAnswers; exports.MarkStatus = exports.bothDirections = void 0; var _lib = require("puz-reader/lib"); // import createSuper from "@babel/runtime/helpers/esm/createSuper"; var bothDirections = Object.values(_lib.Direction); exports.bothDirections = bothDirections; function isAcross(direction) { return direction === _lib.Direction.Across; } function otherDirection(direction) { return isAcross(direction) ? _lib.Direction.Down : _lib.Direction.Across; } var MarkStatus = { CORRECT: 1, INCORRECT: 2 }; exports.MarkStatus = MarkStatus; Object.freeze(MarkStatus); // const emptyCellData = { // used: false, // number: null, // answer: '', // guess: '', // locked: false, // // row: r, // // col: c, // across: null, // down: null, // }; // function createEmptyGuessState(crossword) { var guessState = Array(crossword.height); // Rather than [x][y] in column-major order, the cells are indexed as // [row][col] in row-major order. for (var i = 0; i < crossword.height; i++) { guessState[i] = Array(crossword.width); for (var j = 0; j < crossword.width; j++) { var square = crossword.squares[i][j]; if (square) { guessState[i][j] = { value: '', marked: false }; } else { guessState[i][j] = null; } } } return guessState; } // export function fillClues(gridData, clues, data, direction) { // const dir = directionInfo[direction]; // // Object.entries(data.cluesForDirection(direction)).forEach((clue) => { // const { row: rowStart, col: colStart, text, answer, number } = clue; // for (let i = 0; i < answer.length; i++) { // const row = rowStart + (dir.primary === 'row' ? i : 0); // const col = colStart + (dir.primary === 'col' ? i : 0); // const square = data.squares[row][col]; // // // TODO?: check to ensure the answer is the same if it's already set? // cellData.used = true; // cellData.answer = answer[i]; // cellData[direction] = number; // // if (i === 0) { // // TODO?: check to ensure the number is the same if it's already set? // cellData.number = number; // } // } // // clues[direction].push({ number, clue }); // }); // // clues[direction].sort(byNumber); // } // Given the "nice format" for a crossword, generate the usable data optimized // for rendering and our interactivity. function createGridData(crossword) { var _clueCorrectness; var size = Math.max(crossword.height, crossword.width); var guessState = createEmptyGuessState(crossword); console.log("In createGridData, crossword size is " + crossword.height + "," + crossword.width); console.log("In createGridData, crossword.squares is " + crossword.squares); console.log("In createGridData, guessState is " + guessState); // Now fill with answers... and also collect the clues var clueCorrectness = (_clueCorrectness = {}, _clueCorrectness[_lib.Direction.Across] = crossword.across.reduce(function (correctness, clue) { correctness[clue.number] = false; return correctness; }, {}), _clueCorrectness[_lib.Direction.Down] = crossword.down.reduce(function (correctness, clue) { correctness[clue.number] = false; return correctness; }, {}), _clueCorrectness); // fillClues(gridData, clues, data, 'across'); // fillClues(gridData, clues, data, 'down'); return { size: size, guessState: guessState, clueCorrectness: clueCorrectness }; } // sort helper for clues... function byNumber(a, b) { var aNum = Number.parseInt(a.number, 10); var bNum = Number.parseInt(b.number, 10); return aNum - bNum; } function clearGuesses(storageKey) { if (!window.localStorage) { return; } window.localStorage.removeItem(storageKey); } function saveGuesses(crossword, guessState, storageKey) { var _window = window, localStorage = _window.localStorage; if (!localStorage) { return; } var guesses = serializeGuesses(crossword, guessState); var saveData = { date: Date.now(), guesses: guesses }; localStorage.setItem(storageKey, JSON.stringify(saveData)); } function serializeGuesses(crossword, guessState) { var guesses = guessState.reduce(function (memo, row, i) { return row.reduce(function (memoInner, guess, j) { if (guess) { var value = guess.value; if (value !== '') { memoInner[i + "_" + j] = value; } } return memoInner; }, memo); }, {}); return guesses; } function loadGuesses(crossword, guessState, storageKey) { var _window2 = window, localStorage = _window2.localStorage; if (!localStorage) { return; } var saveRaw = localStorage.getItem(storageKey); if (!saveRaw) { return; } var saveData = JSON.parse(saveRaw); // TODO: check date for expiration? deserializeGuesses(crossword, guessState, saveData.guesses); } function deserializeGuesses(crossword, guessState, guesses) { Object.entries(guesses).forEach(function (_ref) { var key = _ref[0], val = _ref[1]; var _key$split = key.split('_'), i = _key$split[0], j = _key$split[1]; // ignore any out-of-bounds guesses! if (i < crossword.height && j < crossword.width) { guessState[i][j].value = val; } }); } function findCorrectAnswers(crossword, guessState) { var correctAnswers = []; console.log("guessState is " + guessState); bothDirections.forEach(function (direction) { Object.entries(crossword.cluesForDirection(direction)).forEach(function (_ref2) { var number = _ref2[0], clue = _ref2[1]; var correct = true; clue.squares.forEach(function (square) { console.log("Guess " + square.row + "," + square.column); var squareGuess = guessState[square.row][square.column]; if (squareGuess.value !== square.answer) { correct = false; } }); if (correct) { // same args as notifyCorrect: direction, number, answer correctAnswers.push([direction, clue.number, clue.answer]); } }); }); return correctAnswers; }