Implement grid loading and saving

This commit is contained in:
Tissevert 2022-07-26 19:25:56 +02:00
parent ccc48a3c8d
commit 6071977123
4 changed files with 42 additions and 5 deletions

View file

@ -1,4 +1,4 @@
LIB = unitJS
LIB = unitJS WTK
WEBAPP = index.html style.css main.js
ROOT = $(PREFIX)/var/www

View file

@ -7,7 +7,8 @@
(let
((%source-dir (dirname (current-filename)))
(SJW (load "/home/Bureau/sjw/SJW.scm"))
(UnitJS (load "/home/Bureau/unitJS/guix.scm")))
(UnitJS (load "/home/Bureau/unitJS/guix.scm"))
(WTK (load "/home/Bureau/WTK/guix.scm")))
(package
(name "etoiles")
(version "devel")
@ -19,7 +20,8 @@
(native-inputs
(list
SJW
UnitJS))
UnitJS
WTK))
(arguments
`(#:make-flags
,#~(list (string-append "PREFIX=" #$output))

View file

@ -8,7 +8,7 @@
</head>
<body>
<ul id="mode">
<li id="edit">Édition</li>
<li id="edit">Edit</li>
</ul>
<div id="playground">
<ul id="toolbox">
@ -24,7 +24,9 @@
<select id="colors" class="color0"></select>
</li>
</ul>
<table id="grid"/>
<button id="load">Load</button>
<table id="grid"></table>
<button id="save">Save</button>
</div>
</body>
</html>

View file

@ -1,6 +1,10 @@
import * as File from WTK.File;
import * as Async from UnitJS.Async;
import compose from UnitJS.Fun;
import * as Dom from UnitJS.Dom;
import {color, tool} from Toolbox;
var grid = {
element: null,
data: null
@ -22,6 +26,8 @@ function init(size, elementId) {
grid.element.addEventListener('mouseleave', function() {
down = false;
});
document.getElementById('load').addEventListener('click', load);
document.getElementById('save').addEventListener('click', save);
}
function emptyGrid(size) {
@ -88,3 +94,30 @@ function paint(i0, j0) {
queue.shift();
}
}
function load() {
Async.run(
Async.bind(
File.pick({accept: 'text/json,.json'}),
function(input) {
return File.load(input.files[0]);
},
Async.map(compose(setGridData, JSON.parse))
)
);
}
function setGridData(data) {
if(data != undefined) {
for(var row = 0; row < grid.element.children.length; row++) {
for(var column = 0; column < grid.element.children[row].children.length; column++) {
grid.data[row][column] = data[row][column];
grid.element.children[row].children[column].className = data[row][column];
}
}
}
}
function save() {
File.save('data:text/json,' + JSON.stringify(grid.data), "grid.json");
}