diff --git a/takhta.html b/takhta.html index cfa117a..fa5b634 100644 --- a/takhta.html +++ b/takhta.html @@ -177,6 +177,7 @@
+

@@ -437,6 +438,56 @@ // Replace the old board with the new one document.getElementById('board').replaceChild(newBoard, oldBoard); } + + function download() { + let actions = document.getElementById("actions"); + let moves = document.getElementById("movelist"); + let link = document.createElement("a"); + + link.setAttribute("download", "moves.text"); + link.style.display = "none"; + actions.appendChild(link); + + var pos = 0; + var game = []; + var longest = 0; + + for(let move = 0; move < moves.children.length; move += 1) { + let record = moves.children[move]; + + if(move % 2 === 0) { + if(record.innerHTML.length > longest) { + longest = record.innerHTML.length; + } + + game[pos] = [record.innerHTML]; + } else { + game[pos].push(record.innerHTML); + pos += 1; + } + } + + let file = []; + let padding = Math.floor(Math.log10(game.length)); + + for(let entry = 0; entry < game.length; entry += 1) { + var line = `${entry + 1}`.padStart(padding + 1, " ") + ": "; + + if(game[entry][1] !== undefined) { + line += `${game[entry][0].padEnd(longest, " ")} ${game[entry][1]}`; + } else { + line += game[entry][0]; + } + + file.push(line); + } + + let contents = encodeURIComponent(file.join("\n")); + + link.setAttribute("href", "data:text/plain;charset=utf-8," + contents); + link.click(); + actions.removeChild(link); + }