Skip to content

Commit d23c987

Browse files
committed
feat(export) : add a folder in the zip when exporting one exercise
1 parent bac422c commit d23c987

File tree

2 files changed

+56
-37
lines changed

2 files changed

+56
-37
lines changed

src/editor/editor_lib.ml

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -477,32 +477,31 @@ module Editor_io = struct
477477
ignore (Js.Unsafe.meth_call input_files_load "click" [||]) ;
478478
result_t
479479

480-
let upload_new_exercise id text =
481-
let save_file =
482-
Json_repr_browser.Json_encoding.destruct
483-
editor_state_enc
484-
(Js._JSON##(parse text))
485-
|> put_exercise_id id
480+
let upload_new_exercise id to_save =
481+
let to_save = put_exercise_id id to_save
486482
in
487483
let open Exercise.Meta in
488-
let result= idUnique id && titleUnique save_file.metadata.title in
484+
let result= idUnique id && titleUnique to_save.metadata.title in
489485
if result then
490-
update_index save_file;
486+
update_index to_save;
491487
result
492488

493489
let upload () =
494490
run_async_with_log
495491
(fun () ->
496492
upload_file () >>=
497493
fun file ->
498-
let id = Filename.chop_extension (Js.to_string file##.name) in
499494
let f = Js.Unsafe.eval_string "editor_import" in
500495
let callback =
501496
(fun text ->
502-
if upload_new_exercise id text then
503-
Dom_html.window##.location##reload
504-
else
505-
Learnocaml_common.alert [%i"Identifier and/or title not unique\n"]);
497+
SMap.iter
498+
(fun id editor_state ->
499+
if not (upload_new_exercise id editor_state) then
500+
Learnocaml_common.alert [%i"Identifier and/or title not unique\n"])
501+
(Json_repr_browser.Json_encoding.destruct
502+
(SMap.enc editor_state_enc)
503+
(Js._JSON##(parse text)));
504+
Dom_html.window##.location##reload)
506505
in
507506
Js.Unsafe.fun_call f [| Js.Unsafe.inject file ;
508507
Js.Unsafe.inject callback|])

static/js/jszip/learnocaml_jszip_wrapper.js

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ function editor_create_exercise(zip, data, prefix) {
1212
function editor_download(brut_data, callback) {
1313
var zip = new JSZip();
1414
var data = JSON.parse(brut_data);
15-
editor_create_exercise(zip, data, "")
15+
var dirname = data.exercise.id
16+
zip.folder(dirname);
17+
editor_create_exercise(zip, data, dirname + "/");
1618
zip.generateAsync({
1719
type: "blob",
1820
compression: "DEFLATE",
@@ -41,35 +43,53 @@ function editor_download_all(brut_exercises, brut_index, callback) {
4143
}).then(function(blob) { callback(blob) });
4244
}
4345

46+
function editor_read_exercise(loaded_zip, path) {
47+
return new Promise(function(resolve, reject) {
48+
var descr = loaded_zip.file(path + "descr.md").async("string");
49+
var meta = loaded_zip.file(path + "meta.json").async("string");
50+
var prelude = loaded_zip.file(path + "prelude.ml").async("string");
51+
var prepare = loaded_zip.file(path + "prepare.ml").async("string");
52+
var template = loaded_zip.file(path + "template.ml").async("string");
53+
var test = loaded_zip.file(path + "test.ml").async("string");
54+
var solution = loaded_zip.file(path + "solution.ml").async("string");
55+
Promise.all([descr, meta, prelude, prepare, template, test, solution])
56+
.then(function(values) {
57+
result.exercise.max_score = 0;
58+
result.exercise.id = "";
59+
result.exercise.descr = values[0];
60+
var brut_meta = values[1];
61+
var meta = brut_meta.replace(/\r?\n|\r/g, " ");
62+
result.metadata = JSON.parse(meta);
63+
result.exercise.prelude = values[2];
64+
result.exercise.prepare = values[3];
65+
result.exercise.template = values[4];
66+
result.exercise.test = values[5];
67+
result.exercise.solution = values[6];
68+
resolve(result);
69+
})
70+
})
71+
}
72+
/*
4473
//also to keep in sync
4574
function editor_import(brut_data, callback) {
4675
var zip = new JSZip();
4776
zip.loadAsync(brut_data)
4877
.then(function(loaded_zip) {
49-
var result = { exercise: {}, metadata: {} };
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: {} };
5091
51-
var descr = loaded_zip.file("descr.md").async("string");
52-
var meta = loaded_zip.file("meta.json").async("string");
53-
var prelude = loaded_zip.file("prelude.ml").async("string");
54-
var prepare = loaded_zip.file("prepare.ml").async("string");
55-
var template = loaded_zip.file("template.ml").async("string");
56-
var test = loaded_zip.file("test.ml").async("string");
57-
var solution = loaded_zip.file("solution.ml").async("string");
58-
Promise.all([descr, meta, prelude, prepare, template, test, solution])
59-
.then(function(values) {
60-
result.exercise.max_score = 0;
61-
result.exercise.id = "";
62-
result.exercise.descr = values[0];
63-
var brut_meta = values[1];
64-
var meta = brut_meta.replace(/\r?\n|\r/g, " ");
65-
result.metadata = JSON.parse(meta);
66-
result.exercise.prelude = values[2];
67-
result.exercise.prepare = values[3];
68-
result.exercise.template = values[4];
69-
result.exercise.test = values[5];
70-
result.exercise.solution = values[6];
71-
console.log(brut_data);
7292
callback(JSON.stringify(result));
7393
});
7494
});
75-
}
95+
}*/

0 commit comments

Comments
 (0)