Skip to content

Commit

Permalink
fix(web-app): Remove automatic dialog (cf. Mechanism-2 of PR ocaml-sf…
Browse files Browse the repository at this point in the history
…#372)

This fixes "bugs 1, 2" of issue ocaml-sf#505.
  • Loading branch information
erikmd committed Dec 16, 2022
1 parent 1c068dd commit 7ea03f1
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 78 deletions.
4 changes: 1 addition & 3 deletions src/ace-lib/ace.ml
Expand Up @@ -88,7 +88,7 @@ let set_synchronized_status editor status =

let focus { editor } = editor##focus

let create_editor editor_div check_valid_state =
let create_editor editor_div =
let editor = edit editor_div in
Js.Unsafe.set editor "$blockScrolling" (Js.Unsafe.variable "Infinity");
let data =
Expand All @@ -102,8 +102,6 @@ let create_editor editor_div check_valid_state =
editor##.customData := (data, None);
editor##setOption (Js.string "displayIndentGuides") (Js.bool false);
editor##on (Js.string "change") (fun () ->
check_valid_state (set_contents data) (fun () -> focus data)
(fun () -> set_synchronized_status data true);
set_synchronized_status data false);
data

Expand Down
3 changes: 1 addition & 2 deletions src/ace-lib/ace.mli
Expand Up @@ -17,8 +17,7 @@ type loc = {
loc_end: int * int;
}

val create_editor: Dom_html.divElement Js.t
-> ((string -> unit) -> (unit -> unit) -> (unit -> unit) -> unit) -> 'a editor
val create_editor: Dom_html.divElement Js.t -> 'a editor

val is_synchronized : 'a editor -> bool

Expand Down
4 changes: 2 additions & 2 deletions src/ace-lib/ocaml_mode.ml
Expand Up @@ -514,8 +514,8 @@ let do_delete ace_editor =
Ace.remove ace_editor "left"
end

let create_ocaml_editor div check_valid_state =
let ace = Ace.create_editor div check_valid_state in
let create_ocaml_editor div =
let ace = Ace.create_editor div in
Ace.set_mode ace "ace/mode/ocaml.ocp";
Ace.set_tab_size ace !config.indent.IndentConfig.i_base;
let editor = { ace; current_error = None; current_warnings = [] } in
Expand Down
3 changes: 1 addition & 2 deletions src/ace-lib/ocaml_mode.mli
Expand Up @@ -24,8 +24,7 @@ type error = msg list

type warning = error

val create_ocaml_editor:
Dom_html.divElement Js.t -> ((string -> unit) -> (unit -> unit) -> (unit -> unit) -> unit) -> editor
val create_ocaml_editor: Dom_html.divElement Js.t -> editor
val get_editor: editor -> editor Ace.editor

val report_error: editor -> ?set_class: bool -> error option -> warning list -> unit Lwt.t
Expand Down
64 changes: 1 addition & 63 deletions src/app/learnocaml_common.ml
Expand Up @@ -712,72 +712,11 @@ let mouseover_toggle_signal elt sigvalue setter =
in
Manip.Ev.onmouseover elt hdl
(*
If a user has made no change to a solution for the exercise [id]
for 180 seconds, [check_valid_editor_state id] ensures that there is
no more recent version of this solution in the server. If this is
the case, the user is asked if we should download this solution
from the server.
This function reduces the risk of an involuntary overwriting of a
student solution when the solution is open in several clients.
*)
let is_synchronized_with_server_callback = ref (fun () -> false)
let is_synchronized_with_server () = !is_synchronized_with_server_callback ()
let check_valid_editor_state id =
let last_changed = ref (Unix.gettimeofday ()) in
fun update_content focus_back on_sync ->
let update_local_copy checking_time () =
let get_solution () =
Learnocaml_local_storage.(retrieve (exercise_state id)).Answer.solution in
try let mtime =
Learnocaml_local_storage.(retrieve (exercise_state id)).Answer.mtime in
if mtime > checking_time then begin
let buttons =
if is_synchronized_with_server () then
[
[%i "Fetch from server"],
(fun () -> let solution = get_solution () in
Lwt.return (focus_back (); update_content solution; on_sync ()));
[%i "Ignore & keep editing"],
(fun () -> Lwt.return (focus_back ()));
]
else
[
[%i "Ignore & keep editing"],
(fun () -> Lwt.return (focus_back ()));
[%i "Fetch from server & overwrite"],
(fun () -> let solution = get_solution () in
Lwt.return (focus_back (); update_content solution; on_sync ()));
]
in
lwt_alert ~title:"Question"
~buttons
[ H.p [H.txt [%i "A more recent answer exists on the server. \
Do you want to fetch the new version?"] ] ]
end else Lwt.return_unit
with
| Not_found -> Lwt.return ()
in
let now = Unix.gettimeofday () in
if now -. !last_changed > 180. then (
let checking_time = !last_changed in
last_changed := now;
Lwt.async (update_local_copy checking_time)
) else
last_changed := now
let ace_display tab =
let ace = lazy (
let answer =
Ocaml_mode.create_ocaml_editor
(Tyxml_js.To_dom.of_div tab)
(fun _ _ _ -> ())
in
let ace = Ocaml_mode.get_editor answer in
Ace.set_font_size ace 16;
Expand Down Expand Up @@ -983,12 +922,11 @@ module Editor_button (E : Editor_info) = struct
end
let setup_editor id solution =
let setup_editor solution =
let editor_pane = find_component "learnocaml-exo-editor-pane" in
let editor =
Ocaml_mode.create_ocaml_editor
(Tyxml_js.To_dom.of_div editor_pane)
(check_valid_editor_state id)
in
let ace = Ocaml_mode.get_editor editor in
Ace.set_contents ace ~reset_undo:true solution;
Expand Down
4 changes: 1 addition & 3 deletions src/app/learnocaml_common.mli
Expand Up @@ -218,9 +218,7 @@ module Editor_button (_ : Editor_info) : sig
val sync : Token.t option Lwt.t -> Learnocaml_data.SMap.key -> (unit -> unit) -> unit
end

val setup_editor : string -> string -> Ocaml_mode.editor * Ocaml_mode.editor Ace.editor

val is_synchronized_with_server_callback : (unit -> bool) ref
val setup_editor : string -> Ocaml_mode.editor * Ocaml_mode.editor Ace.editor

val typecheck :
Learnocaml_toplevel.t ->
Expand Down
3 changes: 1 addition & 2 deletions src/app/learnocaml_exercise_main.ml
Expand Up @@ -179,8 +179,7 @@ let () =
Tyxml_js.Html5.[ h1 [ txt ex_meta.Exercise.Meta.title ] ;
Tyxml_js.Of_dom.of_iFrame text_iframe ] ;
(* ---- editor pane --------------------------------------------------- *)
let editor, ace = setup_editor id solution in
is_synchronized_with_server_callback := (fun () -> Ace.is_synchronized ace);
let editor, ace = setup_editor solution in
let module EB = Editor_button (struct let ace = ace let buttons_container = editor_toolbar end) in
EB.cleanup (Learnocaml_exercise.(access File.template exo));
EB.sync token id (fun () -> Ace.focus ace; Ace.set_synchronized ace) ;
Expand Down
2 changes: 1 addition & 1 deletion src/app/learnocaml_playground_main.ml
Expand Up @@ -64,7 +64,7 @@ let main () =
(* ---- toplevel pane ------------------------------------------------- *)
init_toplevel_pane toplevel_launch top toplevel_buttons_group toplevel_button ;
(* ---- editor pane --------------------------------------------------- *)
let editor, ace = setup_editor id solution in
let editor, ace = setup_editor solution in
let module EB = Editor_button (struct let ace = ace let buttons_container = editor_toolbar end) in
EB.cleanup playground.Playground.template;
EB.download id;
Expand Down

0 comments on commit 7ea03f1

Please sign in to comment.