diff --git a/img/save.svg b/img/save.svg new file mode 100755 index 0000000..7d81658 --- /dev/null +++ b/img/save.svg @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sudoku.js b/sudoku.js old mode 100644 new mode 100755 index c190432..b556e2e --- a/sudoku.js +++ b/sudoku.js @@ -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) { diff --git a/sudoku.php b/sudoku.php old mode 100644 new mode 100755 index 52eb670..42fc230 --- a/sudoku.php +++ b/sudoku.php @@ -101,6 +101,7 @@ +