aboutsummaryrefslogtreecommitdiff
path: root/editor/forms/selector.ml
blob: 1354dcc13527175bbfe1fc45f757223a4deddffb (plain)
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
open Brr

type file =
  { file : File.t
  ; content : Jstr.t
  }

type t = file option

(** Read the content from the file *)
let file_loader
  : file option Note.E.send -> File.t -> unit
  = fun event file ->
    let blob = File.as_blob file in
    Fut.await
      (Blob.text blob)
      (Result.iter
         (fun content ->
            event (Some {file; content}) ))
let create
  : unit -> t Note.signal * Brr.El.t
  = fun () ->

    let add_file_event, add_file_sender = Note.E.create () in

    let i = El.input ()
        ~at:[ At.type' (Jstr.v "file")
            ; (At.v (Jstr.v "accept")) (Jstr.v ".json")
            ] in

    (* The event return a list of files.

       We are only interested by a single on, and keep only the first from the
       list. *)
    let on_change files =
      file_loader add_file_sender (List.hd files) in

    Ev.listen
      Ev.change
      (fun _e -> on_change (El.Input.files i)) (El.as_target i);

    let state = Note.S.hold
        None
        add_file_event
    in
    ( state
    , i )