save at will (not automatically)

This commit is contained in:
adrien
2020-11-22 23:53:57 +01:00
parent 019ce058cd
commit cfc535b772
3 changed files with 61 additions and 7 deletions

24
sudoku.js Normal file → Executable file
View File

@@ -10,6 +10,7 @@ let suggestionTimer = null
let valueToInsert = ""
let history = []
let accessKeyModifiers = "AccessKey+"
let changesToSave = false
function shuffle(iterable) {
array = Array.from(iterable)
@@ -142,6 +143,7 @@ function onclick() {
function oninput() {
history.push({ box: this, value: this.previousValue, placeholder: this.previousPlaceholder })
undoButton.disabled = false
changesToSave = true
if (pencilRadio.checked) {
this.value = Array.from(new Set(this.value)).sort().join("")
this.previousValue = ""
@@ -157,17 +159,10 @@ function oninput() {
}
function refreshBox(box) {
saveGame()
checkBox(box)
refreshUI()
}
function saveGame() {
let saveGame = boxes.map(box => box.value || UNKNOWN).join("")
localStorage[location.pathname] = saveGame
fixGridLink.href = saveGame
}
function checkBox(box) {
box.neighbourhood.concat([box]).forEach(neighbour => {
searchCandidatesOf(neighbour)
@@ -200,6 +195,7 @@ function checkBox(box) {
} else { // Errors on grid
box.form.reportValidity()
}
}
function refreshUI() {
enableRadio()
@@ -316,6 +312,20 @@ function restart() {
}
}
function save() {
let saveGame = boxes.map(box => box.value || UNKNOWN).join("")
localStorage[location.pathname] = saveGame
fixGridLink.href = saveGame
changesToSave = false
}
window.onbeforeunload = function(event) {
if (changesToSave) {
event.preventDefault()
event.returnValue = ""
}
}
function showSuggestion() {
const easyBoxes = boxes.filter(box => box.value == "" && box.candidates.size == 1)
if (easyBoxes.length) {