From 40cf35d4aca34d0ccfdd3fcf52b5bdc217da7f0d Mon Sep 17 00:00:00 2001 From: Manuel CABARCOS BAULINA Date: Fri, 19 Jul 2019 14:00:36 +0200 Subject: [PATCH] feat (import all): finish Editor_io --- src/editor/editor_lib.ml | 10 ++++-- static/js/jszip/learnocaml_jszip_wrapper.js | 38 ++++++++++----------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/editor/editor_lib.ml b/src/editor/editor_lib.ml index 0a8a917e1..7c599b086 100644 --- a/src/editor/editor_lib.ml +++ b/src/editor/editor_lib.ml @@ -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 @@ -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 diff --git a/static/js/jszip/learnocaml_jszip_wrapper.js b/static/js/jszip/learnocaml_jszip_wrapper.js index 4e9a743da..3ccdee131 100644 --- a/static/js/jszip/learnocaml_jszip_wrapper.js +++ b/static/js/jszip/learnocaml_jszip_wrapper.js @@ -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"); @@ -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, " "); @@ -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)); - }); }); -}*/ \ No newline at end of file +} \ No newline at end of file