5 let compute_result text
=
7 let a,b
= Scanf.sscanf text
"%d %d" (fun x y
-> x
,y
) in
8 string_of_int
(EuclideanAlgorithm63.euclid
a b
)
15 module Html
= Js_of_ocaml.Dom_html
16 module Js
= Js_of_ocaml.Js
17 module Dom
= Js_of_ocaml.Dom
19 let node x
= (x
: #
Dom.node Js.t
:> Dom.node Js.t
)
21 let (<|) e l
= List.iter
(fun c
-> Dom.appendChild e c
) l
; node e
23 let html_of_string (d
: Html.document
Js.t
) (s
:string) =
24 d##createElement
(Js.string "p") <|
25 [node (d##createTextNode
(Js.string s
))]
27 let replace_child p n
=
28 Js.Opt.iter
(p##
.firstChild
) (fun c
-> Dom.removeChild p c
);
31 let onload (_event
: #
Html.event
Js.t
) : bool Js.t
=
32 let d = Html.document
in
34 Js.Opt.get
(d##getElementById
(Js.string "test"))
35 (fun () -> assert false) in
36 let textbox = Html.createTextarea
d in
37 textbox##
.rows
:= 20; textbox##
.cols
:= 100;
38 let preview = Html.createDiv
d in
39 preview##
.style##
.border
:= Js.string "1px black";
40 preview##
.style##
.padding
:= Js.string "5px";
41 Dom.appendChild
body textbox;
42 Dom.appendChild
body (Html.createBr
d);
43 Dom.appendChild
body preview;
44 let rec dyn_preview old_text n
=
45 let text = Js.to_string
(textbox##
.value) in
47 if text <> old_text
then
51 html_of_string d (compute_result text)
53 replace_child preview rendered
62 (Js_of_ocaml_lwt.Lwt_js.sleep
(if n = 0 then 0.5 else 0.1))
63 (fun () -> dyn_preview text n)
65 let (_
: '
a Lwt.t
) = dyn_preview "" 0 in Js._false
67 let (_
: unit) = Html.window##
.onload := Html.handler
onload