Source file frontend.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
open Brr
open Js_top_worker_rpc
module W = Js_top_worker_client_fut.W

(* let _ = Toploop.getvalue *)
let ocaml = Jv.get Jv.global "__CM__mllike" |> Stream.Language.of_jv
let ocaml = Stream.Language.define ocaml
let _ = Console.debug [ Jv.of_string "Starting" ]

type meta = {
  libs : string list; [@default []]
  html_scripts : string list; [@default []]
}
[@@deriving yojson]

let initialise requires s callback =
  let open Fut.Result_syntax in
  let rpc = Js_top_worker_client_fut.start s 100000 callback in
  let* () =
    W.init rpc
      {
        Toplevel_api_gen.path = "/static/cmis";
        cmas = [];
        cmis = { static_cmis = []; dynamic_cmis = [] };
        findlib_index = "/_opam/findlib_index";
        findlib_requires = requires;
        stdlib_dcs = "/_opam/ocaml/dynamic_cmis.json";
      }
  in
  Fut.return (Ok rpc)

let styled_text tstyle text =
  let lines = Astring.String.cuts ~sep:"\n" text in
  let non_empty = List.filter (fun x -> x <> "") lines in
  List.map
    (fun l -> El.(span ~at:[ At.style (Jstr.v tstyle) ] [ txt' (l ^ "\n") ]))
    non_empty

let mime_els id mime_vals =
  List.map
    (fun mime ->
      let l = Astring.String.cuts ~sep:"/" mime.Toplevel_api_gen.mime_type in
      match (l, mime.encoding) with
      | [ "image"; _ty ], Mime_printer.Base64 ->
          let src = "data:" ^ mime.mime_type ^ ";base64," ^ mime.data in
          [ El.img ~at:[ At.src (Jstr.of_string src) ] () ]
      | [ "image"; "svg" ], Noencoding ->
          let div = El.div [] in
          Jv.set (El.to_jv div) "innerHTML" (Jv.of_string mime.data);
          El.children div
      | [ "text"; "html" ], Noencoding ->
          let div = El.div [] in
          Jv.set (El.to_jv div) "innerHTML" (Jv.of_string mime.data);
          El.children div
      | _ -> [])
    mime_vals
  |> List.flatten

let append_to_output id output _mime_only =
  let el = Brr.Document.find_el_by_id Brr.G.document (Jstr.v id) in
  match el with
  | None -> ()
  | Some el ->
      let stdout =
        List.map Option.to_list
          Toplevel_api_gen.[ output.stdout; output.caml_ppf ]
        |> List.concat |> String.concat "\n" |> styled_text "color:white"
        |> El.pre ~at:[ At.class' (Jstr.v "stdout") ]
      in
      let mime_els = mime_els id output.mime_vals in
      El.set_children el ([ stdout ] @ mime_els)

let basic_setup =
  Jv.get Jv.global "__CM__basic_setup" |> Code_mirror.Extension.of_jv

type block_ty = Deferred | OCaml | Toplevel | Other

let string_of_block_ty = function
  | Deferred -> "Deferred"
  | OCaml -> "OCaml"
  | Toplevel -> "Toplevel"
  | Other -> "Other"

let block_ty_of_string = function
  | "Deferred" -> Deferred
  | "OCaml" -> OCaml
  | "Toplevel" -> Toplevel
  | "Other" -> Other
  | _ -> failwith "Unknown block type"

type ostate = {
  id : string;
  output_id : string;
  hidden : bool;
  solution : string option;
  ty : block_ty;
  mime_only : bool;
  rpc : Js_top_worker_client_fut.rpc;
}

let to_jv : ostate -> Jv.t =
 fun x ->
  let res =
    Jv.obj
      [|
        ("id", Jv.of_string x.id);
        ("rpc", Jv.repr x.rpc);
        ("hidden", Jv.of_bool x.hidden);
        ("output_id", Jv.of_string x.output_id);
        ("ty", Jv.of_string (string_of_block_ty x.ty));
        ("mime_only", Jv.of_bool x.mime_only);
      |]
  in
  Jv.set_if_some res "solution" (Option.map Jv.of_string x.solution);
  res

let of_jv : Jv.t -> ostate =
 fun jv ->
  let id = Jv.get jv "id" |> Jv.to_string in
  let output_id = Jv.get jv "output_id" |> Jv.to_string in
  let (rpc : Js_top_worker_client_fut.rpc) = Jv.get jv "rpc" |> Obj.magic in
  let solution = Jv.find jv "solution" |> Option.map Jv.to_string in
  let ty = Jv.get jv "ty" |> Jv.to_string |> block_ty_of_string in
  let hidden = Jv.get jv "hidden" |> Jv.to_bool in
  let mime_only = Jv.get jv "mime_only" |> Jv.to_bool in
  { id; output_id; rpc; solution; ty; mime_only; hidden }

let oeffect = Code_mirror.State.StateEffect.define to_jv of_jv
let autorun_effect = Code_mirror.State.StateEffect.define Jv.of_bool Jv.to_bool

let autorun_state_field =
  let update cur t =
    let effects = Code_mirror.State.Transaction.effects t in
    List.fold_right
      (fun e cur ->
        if Code_mirror.State.StateEffect.is e autorun_effect then
          match Code_mirror.State.StateEffect.value e autorun_effect with
          | Some b -> b
          | _ -> cur
        else cur)
      effects cur
  in
  Code_mirror.State.StateField.define Jv.of_bool Jv.to_bool
    ~create:(fun _ -> false)
    ~update

let exec_js id _ =
  let fun_name = Printf.sprintf "%s(globalThis)" id in
  (* let output_div = Printf.sprintf "output-%d" id in *)
  let _f = Js_of_ocaml.Js.Unsafe.eval_string fun_name in
  let mime_entries = Mime_printer.get () in
  Console.log
    [
      Jv.of_string (Printf.sprintf "Got %d entries" (List.length mime_entries));
    ];
  List.iter
    (fun e -> Console.log [ Jv.of_string (Mime_printer.to_odoc e) ])
    mime_entries

let run_code st code v =
  (* let code =
        {|module Odoc_notebook_cell_|} ^ st.id ^ {| = struct |} ^ code
        ^ {| end;; |}
      in *)
  let toggle_autorun = Code_mirror.State.StateEffect.of_ autorun_effect false in

  match st.ty with
  | Other -> Fut.return (Ok ())
  | Deferred ->
      Fut.bind (W.compile_js st.rpc None code) (function
        | Ok res ->
            Console.(log [ str ("JS received: " ^ res) ]);
            let _ = Js_of_ocaml.Js.Unsafe.eval_string res in
            (* exec_js st.id (); *)
            let changes =
              Code_mirror.State.Transaction.create ~effects:[ toggle_autorun ]
                ()
            in
            Code_mirror.View.EditorView.dispatch v changes;
            Fut.return (Ok ())
        | Error _e ->
            Console.(log [ str "error" ]);
            let changes =
              Code_mirror.State.Transaction.create ~effects:[ toggle_autorun ]
                ()
            in
            Code_mirror.View.EditorView.dispatch v changes;
            Fut.return (Ok ()))
  | OCaml ->
      Fut.bind (W.exec st.rpc code) (fun res ->
          match res with
          | Ok res ->
              (* Console.(log [ str ("OCaml received: " ^ (Option.value ~default:"" res.stdout)) ]); *)
              (* append_to_output st.output_id res st.mime_only; *)
              (* Console.log [ Option.get (Option.map Jv.of_string res.stdout) ]; *)
              let changes =
                Code_mirror.State.Transaction.create ~effects:[ toggle_autorun ]
                  ()
              in
              Code_mirror.View.EditorView.dispatch v changes;
              let el =
                Brr.Document.find_el_by_id Brr.G.document (Jstr.v st.output_id)
              in
              (match el with
              | None -> Console.log [ Jv.of_string "No output element" ]
              | Some el ->
                  let mime_els = mime_els st.output_id res.mime_vals in
                  El.set_children el mime_els);
              Console.(log [ str "All finished" ]);
              Fut.return (Ok ())
          | Error _e ->
              let changes =
                Code_mirror.State.Transaction.create ~effects:[ toggle_autorun ]
                  ()
              in
              Code_mirror.View.EditorView.dispatch v changes;
              Console.log [ Jv.of_string "error" ];
              Fut.return (Error ()))
  | Toplevel ->
      Fut.bind (W.exec_toplevel st.rpc code) (fun res ->
          match res with
          | Error _ ->
              let changes =
                Code_mirror.State.Transaction.create ~effects:[ toggle_autorun ]
                  ()
              in
              Code_mirror.View.EditorView.dispatch v changes;
              Fut.return (Ok ())
          | Ok s ->
              let cur_length =
                Code_mirror.View.EditorView.state v
                |> Code_mirror.State.EditorState.doc
                |> Code_mirror.State.Text.length
              in
              let changes =
                {
                  Code_mirror.State.Transaction.from = 0;
                  to_ = Some cur_length;
                  insert = Some s.script;
                }
              in
              let changes =
                Code_mirror.State.Transaction.create ~changes
                  ~effects:[ toggle_autorun ] ()
              in
              Code_mirror.View.EditorView.dispatch v changes;
              let el =
                Brr.Document.find_el_by_id Brr.G.document (Jstr.v st.output_id)
              in
              (match el with
              | None -> Console.log [ Jv.of_string "No output element" ]
              | Some el ->
                  let mime_els = mime_els st.output_id s.mime_vals in
                  El.set_children el mime_els);
              Fut.return (Ok ()))

let panel_constructor (st : ostate) (v : Code_mirror.View.EditorView.t) =
  let run_button =
    Brr.El.(button ~at:[ At.class' (Jstr.v "panel") ] [ txt (Jstr.v "Run") ])
  in
  let run = match st.ty with Other -> [] | _ -> [ run_button ] in
  (* let autorun_txt autorun =
    if autorun
    then Jstr.v "Autorun: On"
    else Jstr.v "Autorun: Off"
  in *)
  let autorun =
    Code_mirror.State.EditorState.field
      (Code_mirror.View.EditorView.state v)
      autorun_state_field
  in
  (* let autorun_span =
    Brr.El.(span [ txt (autorun_txt autorun) ])
  in *)
  let show_solution =
    match st.solution with
    | Some s ->
        let ss = Brr.El.(button [ txt (Jstr.v "Show Solution") ]) in
        let listener =
          Brr.Ev.listen Ev.click
            (fun _ ->
              Console.log [ Jv.of_string "clicked" ];
              let solution = Brr.El.find_by_class (Jstr.v s) in
              match solution with
              | [ e ] ->
                  Console.log [ Jv.of_string "Got a solution" ];
                  let soln =
                    El.txt_text
                      (El.children e |> List.hd |> El.children |> List.hd)
                    |> Jstr.to_string
                  in
                  Console.log [ Jv.of_string soln ];
                  let cur_length =
                    Code_mirror.View.EditorView.state v
                    |> Code_mirror.State.EditorState.doc
                    |> Code_mirror.State.Text.length
                  in
                  let changes =
                    {
                      Code_mirror.State.Transaction.from = 0;
                      to_ = Some cur_length;
                      insert = Some soln;
                    }
                  in
                  let changes =
                    Code_mirror.State.Transaction.create ~changes ()
                  in
                  Code_mirror.View.EditorView.dispatch v changes
              | _ -> Console.log [ "No solution found" ])
            (El.as_target ss)
        in
        ignore listener;
        [ ss ]
    | None -> []
  in
  let update v =
    let state = Code_mirror.View.EditorView.Update.state v in
    let _autorun =
      Code_mirror.State.EditorState.field state autorun_state_field
    in
    ()
    (* let text = autorun_txt autorun in *)
    (* El.set_children autorun_span El.[ txt text ] *)
  in

  let clickfn _ =
    let code =
      Code_mirror.(
        View.EditorView.state v |> State.EditorState.doc |> State.Text.to_string)
    in
    ignore @@ run_code st code v
  in
  ignore (Brr.Ev.listen Ev.click clickfn (El.as_target run_button));
  if autorun then ignore @@ clickfn ();
  (* let id = Brr.El.span ~at:[At.class' (Jstr.v "panel_id")] [ Brr.El.txt' st.id ] in *)
  let dom = Brr.El.(div (run @ show_solution)) in
  Code_mirror.View.Panel.create ~top:true ~update dom

let ostate =
  let provide field =
    Code_mirror.State.Facet.from' Code_mirror.View.showPanel field (fun st ->
        Some (panel_constructor st))
  in

  let update cur t =
    let effects = Code_mirror.State.Transaction.effects t in
    List.fold_right
      (fun e cur ->
        if Code_mirror.State.StateEffect.is e oeffect then
          match Code_mirror.State.StateEffect.value e oeffect with
          | Some b -> b
          | _ -> cur
        else cur)
      effects cur
  in

  Code_mirror.State.StateField.define to_jv of_jv
    ~create:(fun _ -> failwith "unset")
    ~provide ~update

let init ?doc ?(exts = []) () =
  let open Code_mirror in
  (* let doc = Option.map Jv.of_string doc in *)
  let config =
    State.EditorStateConfig.create ?doc ~extensions:(basic_setup :: exts) ()
  in
  let state = State.EditorState.create ~config () in
  let config = View.EditorViewConfig.create ~state () in
  let view : View.EditorView.t = View.EditorView.create ~config () in
  (state, view)

let make_editor rpc id elt =
  let parent_exn elt =
    match El.parent elt with Some e -> e | None -> failwith "no parent"
  in
  let parent = parent_exn elt in
  Console.log [ Jstr.v "Got parent" ];
  Console.log [ parent_exn parent ];
  let output =
    match El.children (parent_exn parent) with
    | _ :: o :: _ -> Some o
    | _ -> None
  in
  let output_id = "output-" ^ string_of_int id in
  let _ =
    Option.iter (El.set_at (Jstr.v "id") (Some (Jstr.v output_id))) output
  in
  let doc = El.txt_text (El.children elt |> List.hd) |> Jstr.to_string in
  let classes_iter =
    Jv.call (Jv.get (Jv.repr parent) "classList") "values" [||]
  in
  let str_classes =
    Jv.It.fold Jv.to_string (fun x y -> x :: y) classes_iter []
  in
  let solution =
    List.fold_right
      (fun x y ->
        if Astring.String.is_prefix ~affix:"solution:" x then
          Some
            (String.sub x
               (String.length "solution:")
               (String.length x - String.length "solution:"))
        else y)
      str_classes None
  in
  let mime_only = List.mem "mime-only" str_classes in
  (* let is_deferred = List.mem "deferred-js" str_classes in *)
  let ty =
    if List.mem "language-ocaml" str_classes then Toplevel
    else if List.mem "language-ocamltop" str_classes then Toplevel
    else Other
  in
  let hidden = List.mem "hidden" str_classes in
  Console.log [ Jstr.v ("Classes: " ^ String.concat ", " str_classes) ];
  let autorun = List.mem "autorun" str_classes in
  let merlin_extensions =
    match ty with
    | OCaml | Deferred ->
        Merlin_codemirror.[ autocomplete rpc; linter rpc; tooltip_on_hover rpc ]
    | Toplevel -> []
    | Other -> []
  in
  let theme =
    Jv.get Jv.global "__CM__theme_dracula" |> Code_mirror.Extension.of_jv
  in
  let el = El.(div ~at:[ At.class' (Jstr.v "editor") ] []) in
  let ostate_extension =
    Code_mirror.State.StateField.init ostate (fun _ ->
        {
          id = "id_" ^ string_of_int (id + 1);
          output_id;
          rpc;
          solution;
          ty;
          mime_only;
          hidden;
        })
  in
  if hidden then
    El.set_at (Jstr.v "style")
      (Some (Jstr.v "display: none;"))
      (parent_exn parent);
  let autorun_extension =
    Code_mirror.State.StateField.init autorun_state_field (fun _ -> autorun)
  in
  let editor, view =
    init ~doc
      ~exts:
        (autorun_extension :: ostate_extension :: ocaml :: theme
       :: merlin_extensions)
      ()
  in
  El.append_children el [ Code_mirror.View.EditorView.dom view ];
  El.insert_siblings `After parent [ el ];
  El.remove parent;
  Some (editor, view)

let get_sections () =
  El.fold_find_by_selector
    (fun x y -> x :: y)
    (Jstr.v "header.odoc-preamble")
    []
  @ El.fold_find_by_selector
      (fun x y -> x :: y)
      (Jstr.v "div.odoc-content > section")
      []

let show_section n =
  let sections = get_sections () in
  let set_display el display =
    let style =
      Printf.sprintf "display: %s;" (if display then "block" else "none")
    in
    El.set_at (Jstr.v "style") (Some (Jstr.v style)) el
  in
  List.iteri (fun i s -> set_display s (i = n)) sections

let nav_setup () =
  let nav_elts =
    El.fold_find_by_selector (fun x y -> x :: y) (Jstr.v "nav > ul > li") []
  in
  List.iteri
    (fun i el ->
      let _ =
        Brr.Ev.listen Ev.click (fun _ -> show_section (i + 1)) (El.as_target el)
      in
      ())
    nav_elts

let init_page requires =
  let open Fut.Result_syntax in
  let* rpc =
    initialise requires "/assets/worker.js" (fun _ ->
        Console.(log [ str "Timeout" ]))
  in
  Console.log [ Jv.of_string "Initialised" ];
  let* _o = W.setup rpc () in
  let ocaml_elts =
    El.fold_find_by_selector (fun x y -> x :: y) (Jstr.v "pre code") []
    |> List.rev
  in
  let _editors = List.mapi (make_editor rpc) ocaml_elts in

  (* List.iteri (fun n x -> exec_js (Printf.sprintf "id_%d" (n + 1)) x) elts; *)
  (* let sections = get_sections () in
  let n = List.length sections in
  List.iteri
    (fun i s ->
      El.set_at (Jstr.v "style") (Some (Jstr.v "display: none;")) s;
      let prev = El.(button [ txt (Jstr.v "Prev") ]) in
      let next = El.(button [ txt (Jstr.v "Next") ]) in
      let _ =
        Brr.Ev.listen Ev.click
          (fun _ ->
            let next = (i + 1) mod n in
            show_section next)
          (El.as_target prev)
      in
      let _ =
        Brr.Ev.listen Ev.click
          (fun _ ->
            let prev = (i - 1 + n) mod n in
            show_section prev)
          (El.as_target next)
      in
      El.append_children s [ prev; next ])
    sections;
  show_section 0; *)
  (* nav_setup (); *)
  Fut.return (Ok ())

open Fut.Syntax

let main _requires =
  let meta_elts =
    El.fold_find_by_selector
      (fun elt y ->
        let parent_exn elt =
          match El.parent elt with Some e -> e | None -> failwith "no parent"
        in
        let parent = parent_exn elt in
        let classes_iter =
          Jv.call (Jv.get (Jv.repr parent) "classList") "values" [||]
        in
        let str_classes =
          Jv.It.fold Jv.to_string (fun x y -> x :: y) classes_iter []
        in
        if List.mem "language-meta" str_classes then
          let meta =
            let doc =
              El.txt_text (El.children elt |> List.hd) |> Jstr.to_string
            in
            let y = Yojson.Safe.from_string doc in
            [ y ]
          in
          meta
        else y)
      (Jstr.v "pre code") []
    |> List.rev
  in
  let meta =
    match meta_elts with
    | [] ->
        Console.log [ Jv.of_string "No meta elements found" ];
        None
    | [ x ] -> ( match meta_of_yojson x with Ok m -> Some m | _ -> None)
    | _ -> None
  in
  let libs = match meta with Some m -> m.libs | None -> [] in

  Console.(log [ str "DOM content loaded." ]);
  let* _ev = Ev.next Ev.load (Window.as_target G.window) in
  Console.(log [ str "Resources loaded." ]);
  ignore (init_page libs);

  Fut.return ()

(* let _ =
  let _ = Console.debug [ Jv.of_string "global cmis = " ] in
  let _ = Console.debug [ Jv.get Jv.global "cmis" ] in
  main (Jv.to_string (Jv.get Jv.global "cmis")) *)