Skip to content

Commit d3fc9c5

Browse files
YoanwMerikmd
andcommitted
feat: Make the prelude available in description page (ocaml-sf#393)
* Make the prelude available in description page * fix(description.html): Load learnocaml_tryocaml.css & Comment-out unneeded file * fix(description): Initially show the Prelude * fix(description): Simplify/Fix HTML+CSS+Js_of_ocaml for the prelude Co-authored-by: Erik Martin-Dorel <[email protected]>
1 parent 5f16d4d commit d3fc9c5

File tree

5 files changed

+140
-15
lines changed

5 files changed

+140
-15
lines changed

src/app/learnocaml_common.ml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,42 @@ let set_nickname_div () =
952952
| nickname -> Manip.setInnerText nickname_div nickname
953953
| exception Not_found -> ()
954954
955+
(* setup for the prelude in the description page (in description_main.ml) *)
956+
let setup_tab_text_prelude_pane prelude =
957+
if prelude = "" then () else
958+
let iframe_pane = find_component "learnocaml-exo-tab-text-descr" in
959+
let prelude_pane = find_component "learnocaml-exo-tab-text-prelude" in
960+
let open Tyxml_js.Html5 in
961+
let state =
962+
ref (match arg "tab_text_prelude" with
963+
| exception Not_found -> true
964+
| "shown" -> true
965+
| "hidden" -> false
966+
| _ -> failwith "Bad format for argument prelude.") in
967+
let prelude_btn = button [] in
968+
let prelude_title = h1 [ txt [%i"OCaml prelude"] ;
969+
prelude_btn ] in
970+
let prelude_container =
971+
pre ~a: [ a_class [ "toplevel-code" ] ]
972+
(Learnocaml_toplevel_output.format_ocaml_code prelude) in
973+
let update () =
974+
if !state then begin
975+
Manip.replaceChildren prelude_btn [ txt (""^[%i"Hide"]) ] ;
976+
Manip.SetCss.display prelude_container "" ;
977+
Manip.SetCss.top iframe_pane "241px";
978+
set_arg "tab_text_prelude" "shown"
979+
end else begin
980+
Manip.replaceChildren prelude_btn [ txt (""^[%i"Show"]) ] ;
981+
Manip.SetCss.display prelude_container "none" ;
982+
Manip.SetCss.top iframe_pane "90px";
983+
set_arg "tab_text_prelude" "hidden"
984+
end in
985+
update () ;
986+
Manip.Ev.onclick prelude_btn
987+
(fun _ -> state := not !state ; update () ; true) ;
988+
Manip.appendChildren prelude_pane
989+
[ prelude_title ; prelude_container ]
990+
955991
let setup_prelude_pane ace prelude =
956992
if prelude = "" then () else
957993
let editor_pane = find_component "learnocaml-exo-editor-pane" in

src/app/learnocaml_common.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ val typecheck :
229229

230230
val set_nickname_div : unit -> unit
231231

232+
val setup_tab_text_prelude_pane : string -> unit
233+
232234
val setup_prelude_pane : 'a Ace.editor -> string -> unit
233235

234236
val get_token : ?has_server:bool -> unit -> Learnocaml_data.student Learnocaml_data.token option Lwt.t

src/app/learnocaml_description_main.ml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,33 @@ module Exercise_link =
1515
a_class cl ]
1616
content)
1717
end
18-
19-
module Display = Display_exercise(Exercise_link)
18+
19+
module Display = Display_exercise(Exercise_link)
2020
open Display
21-
21+
2222
let () =
2323
run_async_with_log @@ fun () ->
2424
let id = match Url.Current.path with
2525
| "" :: "description" :: p | "description" :: p ->
2626
String.concat "/" (List.map Url.urldecode (List.filter ((<>) "") p))
2727
| _ -> arg "id" in
2828
Learnocaml_local_storage.init () ;
29-
let text_container = find_component "learnocaml-exo-tab-text" in
29+
let title_container = find_component "learnocaml-exo-tab-text-title" in
30+
let text_container = find_component "learnocaml-exo-tab-text-descr" in
3031
try begin
3132
let token = Learnocaml_data.Token.parse (arg "token") in
3233
let exercise_fetch =
3334
retrieve (Learnocaml_api.Exercise (Some token, id))
3435
in
3536
init_tabs ();
3637
exercise_fetch >>= fun (ex_meta, exo, _deadline) ->
37-
(* display exercise questions *)
38+
(* display exercise questions and prelude *)
39+
setup_tab_text_prelude_pane Learnocaml_exercise.(decipher File.prelude exo);
3840
let text_iframe = Dom_html.createIframe Dom_html.document in
41+
Manip.replaceChildren title_container
42+
Tyxml_js.Html5.[ h1 [ txt ex_meta.title] ];
3943
Manip.replaceChildren text_container
40-
Tyxml_js.Html5.[ h1 [ txt ex_meta.title ] ;
41-
Tyxml_js.Of_dom.of_iFrame text_iframe ] ;
44+
[ Tyxml_js.Of_dom.of_iFrame text_iframe ];
4245
Js.Opt.case
4346
(text_iframe##.contentDocument)
4447
(fun () -> failwith "cannot edit iframe document")

static/css/learnocaml_description.css

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ body {
3131
display: flex;
3232
flex-direction: column;
3333
overflow: hidden;
34+
border: none;
3435
}
35-
#learnocaml-exo-tabs > * > h1 {
36+
#learnocaml-exo-tabs h1 {
3637
flex: 0 0 auto;
3738
background: #222;
3839
color: #eee;
@@ -43,23 +44,101 @@ body {
4344
display: block;
4445
font-weight: normal;
4546
position: relative;
47+
border-bottom: thin solid #eee;
4648
}
47-
#learnocaml-exo-tabs > * > h1:first-child {
49+
#learnocaml-exo-tabs h1:first-child {
4850
margin-top: 5px;
4951
}
50-
#learnocaml-exo-tabs > * > iframe {
52+
#learnocaml-exo-tabs iframe {
5153
border: none;
5254
overflow: auto;
5355
flex: 1 3 auto ;
5456
}
55-
#learnocaml-exo-tab-meta > h1::after {
57+
#learnocaml-exo-tab-meta h1::after {
58+
position: absolute;
59+
left: 0px; bottom: -5px; width: 100%;
60+
content:"";
61+
height:5px; background: pink;
62+
background: linear-gradient(to bottom, rgba(0,0,0,0.3) 0, transparent 100%)
63+
}
64+
65+
/* ----------------------- iframe --------------------------------*/
66+
67+
#learnocaml-exo-tab-text-descr {
68+
position : absolute;
69+
top: 48px;
70+
right : 0px;
71+
left : 0px;
72+
bottom : 0px;
73+
display : flex ;
74+
flex-direction : column;
75+
}
76+
77+
/* -------------------- Prelude -------------------------------- */
78+
79+
#learnocaml-exo-tab-text-prelude {
80+
overflow: hidden;
81+
left: 0;
82+
top: 61px;
83+
z-index: 1002;
84+
border-bottom: thin solid #eee;
85+
width: 100%;
86+
}
87+
#learnocaml-exo-tab-text-prelude > h1 {
88+
position: relative;
89+
flex: 0 0 auto;
90+
background: #222;
91+
color: #eee;
92+
font-size: 20px;
93+
line-height: 22px;
94+
margin: 0;
95+
padding-top: 10px;
96+
padding-bottom: 10px;
97+
display: block;
98+
font-weight: normal;
99+
z-index: 1005;
100+
width: 100%;
101+
text-align: center;
102+
}
103+
#learnocaml-exo-tab-text-prelude > h1:first-child {
104+
margin-top: 0;
105+
}
106+
#learnocaml-exo-tab-text-prelude > h1 > button {
107+
float: right;
108+
border: none;
109+
border-left: 1px #eee solid;
110+
color: #eee;
111+
padding-top: 10px;
112+
padding-bottom: 10px;
113+
margin-top: -10px;
114+
margin-bottom: -10px;
115+
font-size: 20px;
116+
line-height: 22px;
117+
z-index: 1006;
118+
width: 20%;
119+
background: #222;
120+
}
121+
122+
#learnocaml-exo-tab-text-prelude > pre.toplevel-code {
123+
flex: 0 1 auto;
124+
height: 150px;
125+
max-height: 150px;
126+
background: #666;
127+
margin: 0;
128+
overflow: auto;
129+
width: 100%;
130+
position: absolute;
131+
}
132+
#learnocaml-exo-tab-text-prelude > h1::after {
56133
position: absolute;
57134
left: 0px; bottom: -5px; width: 100%;
58135
content:"";
59136
height:5px; background: pink;
60137
background: linear-gradient(to bottom, rgba(0,0,0,0.3) 0, transparent 100%)
61138
}
62139

140+
141+
63142
/* BEGIN excerpt from learnocaml_exercise.css */
64143

65144
.learnocaml-exo-meta-category ~ .exercise + .exercise {
@@ -191,9 +270,9 @@ body {
191270
#learnocaml-exo-tab-text {
192271
position: relative;
193272
flex-grow: 4;
194-
border-right: #222 2px solid;
273+
border-right: none;
195274
overflow-y: auto;
196-
}
275+
}
197276
#learnocaml-exo-tab-meta {
198277
position: relative;
199278
flex-grow: 1;

static/description.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1">
77
<title>Learn OCaml by OCamlPro - Description</title>
88
<link rel="stylesheet" href="/css/learnocaml_common.css">
9-
<link rel="stylesheet" href="/css/learnocaml_standalone_description.css">
9+
<link rel="stylesheet" href="/css/learnocaml_tryocaml.css">
1010
<link rel="stylesheet" href="/css/learnocaml_description.css">
11+
<!-- <link rel="stylesheet" href="/css/learnocaml_standalone_description.css"> -->
1112

1213
<script src="/js/ace/ace.js" type="text/javascript" charset="utf-8" defer></script>
1314
<!-- var editor = ace.edit("learnocaml-exo-editor-pane"); -->
@@ -34,7 +35,11 @@
3435
<div id="learnocaml-exo-tabs">
3536
<div id="learnocaml-exo-tab-meta" class="front-tab"></div>
3637
<div id="learnocaml-exo-tab-meta-sep"></div>
37-
<div id="learnocaml-exo-tab-text"></div>
38+
<div id="learnocaml-exo-tab-text">
39+
<div id="learnocaml-exo-tab-text-title"></div>
40+
<div id="learnocaml-exo-tab-text-prelude"></div>
41+
<div id="learnocaml-exo-tab-text-descr"></div>
42+
</div>
3843
</div>
3944
</body>
4045

0 commit comments

Comments
 (0)