Skip to content

Commit

Permalink
feat (import all): finish Editor_io
Browse files Browse the repository at this point in the history
  • Loading branch information
leunam217 committed Jul 19, 2019
1 parent d23c987 commit 40cf35d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
10 changes: 7 additions & 3 deletions src/editor/editor_lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,8 @@ module Editor_io = struct
(fun () ->
upload_file () >>=
fun file ->
let f = Js.Unsafe.eval_string "editor_import" in
Firebug.console##(log file);
let (f:Js.js_string Js.t ->(Js.js_string Js.t -> unit)->unit) = Js.Unsafe.eval_string "editor_import" in
let callback =
(fun text ->
SMap.iter
Expand All @@ -503,7 +504,10 @@ module Editor_io = struct
(Js._JSON##(parse text)));
Dom_html.window##.location##reload)
in
Js.Unsafe.fun_call f [| Js.Unsafe.inject file ;
Js.Unsafe.inject callback|])
let _ =
Js.Unsafe.fun_call f
[| Js.Unsafe.inject file ;
Js.Unsafe.inject callback|]
in Lwt.return_unit)

end
38 changes: 19 additions & 19 deletions static/js/jszip/learnocaml_jszip_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function editor_download_all(brut_exercises, brut_index, callback) {
}).then(function(blob) { callback(blob) });
}

function editor_read_exercise(loaded_zip, path) {
function editor_read_exercise(loaded_zip, path, id) {
return new Promise(function(resolve, reject) {
var descr = loaded_zip.file(path + "descr.md").async("string");
var meta = loaded_zip.file(path + "meta.json").async("string");
Expand All @@ -54,8 +54,9 @@ function editor_read_exercise(loaded_zip, path) {
var solution = loaded_zip.file(path + "solution.ml").async("string");
Promise.all([descr, meta, prelude, prepare, template, test, solution])
.then(function(values) {
var result = { exercise: {}, metadata: {} };
result.exercise.max_score = 0;
result.exercise.id = "";
result.exercise.id = id;
result.exercise.descr = values[0];
var brut_meta = values[1];
var meta = brut_meta.replace(/\r?\n|\r/g, " ");
Expand All @@ -69,27 +70,26 @@ function editor_read_exercise(loaded_zip, path) {
})
})
}
/*

//also to keep in sync
function editor_import(brut_data, callback) {
var zip = new JSZip();
zip.loadAsync(brut_data)
.then(function(loaded_zip) {
if (loaded_zip.file("index.json")) {
loaded_zip.forEach(function(relative_path, file) {
if (file.dir) {
new Promise(function(resolve, reject) {
editor_read_exercise(loaded_zip, relative_path)
.then(function(result) {
})
})
}
}
}
var result = { exercise: {}, metadata: {} };
var promises = [];
loaded_zip.forEach(function(relative_path, file) {
if (file.dir) {
var promise = editor_read_exercise(loaded_zip, relative_path, file.name.replace(/\//, ""));
promises.push(promise);
}
})
Promise.all(promises).then(function(values) {
var result = values.reduce(function(acc, elt) {
acc[elt.exercise.id] = elt;
return acc;
}, {})
callback(JSON.stringify(result));
})

callback(JSON.stringify(result));
});
});
}*/
}

0 comments on commit 40cf35d

Please sign in to comment.