Skip to content

Commit 15780b5

Browse files
committed
feat : add beforeunload management
1 parent d82eb94 commit 15780b5

File tree

5 files changed

+31
-32
lines changed

5 files changed

+31
-32
lines changed

src/ace-lib/ace.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ let set_custom_data { editor } data =
9191
let set_mode {editor} name =
9292
editor##getSession##(setMode (Js.string name))
9393

94+
let on {editor} event callback =
95+
editor##getSession##(on (Js.string event) (Js.Unsafe.meth_callback callback))
96+
9497
type mark_type = Error | Warning | Message
9598

9699
let string_of_make_type: mark_type -> string = function

src/ace-lib/ace.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type loc = {
1818
val create_editor: Dom_html.divElement Js.t -> 'a editor
1919

2020
val set_mode: 'a editor -> string -> unit
21+
val on: 'b editor -> string -> (Dom_html.event Js.t -> unit) -> unit
2122

2223
val read_range: Ace_types.range Js.t -> (int * int) * (int * int)
2324
val create_range:

src/ace-lib/ace_types.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class type editSession = object
5858
method getTokenAt : int -> int -> token Js.t Js.meth
5959
method replace : range Js.t -> Js.js_string Js.t -> unit Js.meth
6060
method setMode : Js.js_string Js.t -> unit Js.meth
61+
method on : Js.js_string Js.t ->
62+
((Dom_html.event Js.t , unit) Js.meth_callback)->
63+
unit Js.meth
6164
method setAnnotations : annotation Js.t Js.js_array Js.t -> unit Js.meth
6265
method getAnnotations : annotation Js.t Js.js_array Js.t Js.meth
6366
method clearAnnotations : unit Js.meth

src/editor/editor.ml

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ open Js_of_ocaml
2424
open Editor_lib
2525
open Dom_html
2626
open Test_spec
27+
28+
2729
(*----------------------------------------------------------------------*)
2830

2931
let init_tabs, select_tab =
@@ -65,6 +67,22 @@ let set_string_translations () =
6567
Manip.setInnerHtml (find_component id) text)
6668
translations
6769

70+
let activate_before_unload () :unit =
71+
Js.Unsafe.js_expr
72+
"window.onbeforeunload = function() {return 'You have unsaved changes!';}"
73+
74+
let unable_before_unload () :unit =
75+
Js.Unsafe.js_expr "window.onbeforeunload = null"
76+
77+
let onchange ace_list =
78+
let add_change_listener ace =
79+
Ace.on
80+
ace
81+
"change"
82+
(fun _ -> activate_before_unload ();) in
83+
List.iter (fun ace -> add_change_listener ace) ace_list
84+
85+
6886
let () =
6987
run_async_with_log @@ fun () ->
7088
(*set_string_translations ();*)
@@ -157,7 +175,6 @@ let () =
157175
(Tyxml_js.To_dom.of_div editor_prelude) in
158176
let ace_prel = Ocaml_mode.get_editor editor_prel in
159177
let contents= get_prelude id in
160-
161178
Ace.set_contents ace_prel contents ;
162179
Ace.set_font_size ace_prel 18;
163180

@@ -384,6 +401,7 @@ let () =
384401
end;
385402

386403
let recovering () =
404+
unable_before_unload ();
387405
let solution = Ace.get_contents ace in
388406
let descr = Ace.get_contents ace_quest in
389407
let template = Ace.get_contents ace_temp in
@@ -435,41 +453,14 @@ let () =
435453
(*let toolbar_button2 = button2 ~container: exo_toolbar ~theme: "light" in*)
436454
begin toolbar_button
437455
~icon: "left" [%i"Metadata"] @@ fun () ->
438-
recovering ();
439456
Dom_html.window##.location##assign
440457
(Js.string ("new_exercise.html#id=" ^ id ^ "&action=open"));
441458
Lwt.return ()
442459
end;
443460
begin toolbar_button
444461
~icon: "list" [%i"Exercises"] @@ fun () ->
445-
let _aborted, abort_message =
446-
let t, _u = Lwt.task () in
447-
let btn_cancel = Tyxml_js.Html5.(button [ pcdata [%i"Cancel"] ]) in
448-
Manip.Ev.onclick btn_cancel ( fun _ ->
449-
hide_loading ~id:"learnocaml-exo-loading" () ; true) ;
450-
let btn_yes = Tyxml_js.Html5.(button [ pcdata [%i"Yes"] ]) in
451-
Manip.Ev.onclick btn_yes (fun _ ->
452-
recovering ();
453462
Dom_html.window##.location##assign
454-
(Js.string "index.html#activity=editor") ; true) ;
455-
let btn_no = Tyxml_js.Html5.(button [ pcdata [%i"No"] ]) in
456-
Manip.Ev.onclick btn_no (fun _ ->
457-
Dom_html.window##.location##assign
458-
(Js.string "index.html#activity=editor") ; true);
459-
let div =
460-
Tyxml_js.Html5.(div ~a: [ a_class [ "dialog" ] ]
461-
[ pcdata [%i"Do you want to save before closing?\n"] ;
462-
btn_yes ;
463-
pcdata " " ;
464-
btn_no ;
465-
pcdata " " ;
466-
btn_cancel ]) in
467-
Manip.SetCss.opacity div (Some "0") ;
468-
t, div in
469-
Manip.replaceChildren messages
470-
Tyxml_js.Html5.[ li [ pcdata "" ] ] ;
471-
show_load "learnocaml-exo-loading" [ abort_message ] ;
472-
Manip.SetCss.opacity abort_message (Some "1") ;
463+
(Js.string "index.html#activity=editor");
473464
Lwt.return ()
474465
end ;
475466

@@ -544,7 +535,7 @@ let () =
544535
recovering ();
545536
grade ()
546537
end ;
547-
Window.onunload (fun _ev -> recovering (); true);
538+
onchange [ace_temp; ace_t; ace_prep; ace_prel; ace_quest; ace ];
548539
(* ---- return -------------------------------------------------------- *)
549540
(* toplevel_launch >>= fun _ -> should be unnecessary? *)
550541
(* typecheck false >>= fun () -> *)

src/editor/editor_lib.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,14 +491,15 @@ module Editor_io = struct
491491
(fun () ->
492492
upload_file () >>=
493493
fun file ->
494-
Firebug.console##(log file);
495494
let (f:Js.js_string Js.t ->(Js.js_string Js.t -> unit)->unit) = Js.Unsafe.eval_string "editor_import" in
496495
let callback =
497496
(fun text ->
497+
498498
SMap.iter
499499
(fun id editor_state ->
500500
if not (upload_new_exercise id editor_state) then
501-
Learnocaml_common.alert [%i"Identifier and/or title not unique\n"])
501+
alert ([%i"Identifier and/or title not unique\n"] ^
502+
"id:" ^ id ^ [%i" title:"] ^ editor_state.metadata.title))
502503
(Json_repr_browser.Json_encoding.destruct
503504
(SMap.enc editor_state_enc)
504505
(Js._JSON##(parse text)));

0 commit comments

Comments
 (0)