Skip to content

Commit 40cf35d

Browse files
committed
feat (import all): finish Editor_io
1 parent d23c987 commit 40cf35d

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

src/editor/editor_lib.ml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,8 @@ module Editor_io = struct
491491
(fun () ->
492492
upload_file () >>=
493493
fun file ->
494-
let f = Js.Unsafe.eval_string "editor_import" in
494+
Firebug.console##(log file);
495+
let (f:Js.js_string Js.t ->(Js.js_string Js.t -> unit)->unit) = Js.Unsafe.eval_string "editor_import" in
495496
let callback =
496497
(fun text ->
497498
SMap.iter
@@ -503,7 +504,10 @@ module Editor_io = struct
503504
(Js._JSON##(parse text)));
504505
Dom_html.window##.location##reload)
505506
in
506-
Js.Unsafe.fun_call f [| Js.Unsafe.inject file ;
507-
Js.Unsafe.inject callback|])
507+
let _ =
508+
Js.Unsafe.fun_call f
509+
[| Js.Unsafe.inject file ;
510+
Js.Unsafe.inject callback|]
511+
in Lwt.return_unit)
508512

509513
end

static/js/jszip/learnocaml_jszip_wrapper.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function editor_download_all(brut_exercises, brut_index, callback) {
4343
}).then(function(blob) { callback(blob) });
4444
}
4545

46-
function editor_read_exercise(loaded_zip, path) {
46+
function editor_read_exercise(loaded_zip, path, id) {
4747
return new Promise(function(resolve, reject) {
4848
var descr = loaded_zip.file(path + "descr.md").async("string");
4949
var meta = loaded_zip.file(path + "meta.json").async("string");
@@ -54,8 +54,9 @@ function editor_read_exercise(loaded_zip, path) {
5454
var solution = loaded_zip.file(path + "solution.ml").async("string");
5555
Promise.all([descr, meta, prelude, prepare, template, test, solution])
5656
.then(function(values) {
57+
var result = { exercise: {}, metadata: {} };
5758
result.exercise.max_score = 0;
58-
result.exercise.id = "";
59+
result.exercise.id = id;
5960
result.exercise.descr = values[0];
6061
var brut_meta = values[1];
6162
var meta = brut_meta.replace(/\r?\n|\r/g, " ");
@@ -69,27 +70,26 @@ function editor_read_exercise(loaded_zip, path) {
6970
})
7071
})
7172
}
72-
/*
73+
7374
//also to keep in sync
7475
function editor_import(brut_data, callback) {
7576
var zip = new JSZip();
7677
zip.loadAsync(brut_data)
7778
.then(function(loaded_zip) {
78-
if (loaded_zip.file("index.json")) {
79-
loaded_zip.forEach(function(relative_path, file) {
80-
if (file.dir) {
81-
new Promise(function(resolve, reject) {
82-
editor_read_exercise(loaded_zip, relative_path)
83-
.then(function(result) {
84-
85-
})
86-
})
87-
}
88-
}
89-
}
90-
var result = { exercise: {}, metadata: {} };
79+
var promises = [];
80+
loaded_zip.forEach(function(relative_path, file) {
81+
if (file.dir) {
82+
var promise = editor_read_exercise(loaded_zip, relative_path, file.name.replace(/\//, ""));
83+
promises.push(promise);
84+
}
85+
})
86+
Promise.all(promises).then(function(values) {
87+
var result = values.reduce(function(acc, elt) {
88+
acc[elt.exercise.id] = elt;
89+
return acc;
90+
}, {})
91+
callback(JSON.stringify(result));
92+
})
9193

92-
callback(JSON.stringify(result));
93-
});
9494
});
95-
}*/
95+
}

0 commit comments

Comments
 (0)