aboutsummaryrefslogtreecommitdiff
path: root/editor/forms
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2022-02-07 15:27:05 +0100
committerSébastien Dailly <sebastien@dailly.me>2022-02-07 16:43:33 +0100
commit28417d795a6922c3df3c2c0aea78a51648b0428e (patch)
tree28af6bd86e2127003855574b4c777efa1cd07207 /editor/forms
parent72e3b16bbd258e63f047392c973ba5e8f0a823c8 (diff)
Added import button in editor
Diffstat (limited to 'editor/forms')
-rwxr-xr-xeditor/forms/selector.ml47
1 files changed, 47 insertions, 0 deletions
diff --git a/editor/forms/selector.ml b/editor/forms/selector.ml
new file mode 100755
index 0000000..1354dcc
--- /dev/null
+++ b/editor/forms/selector.ml
@@ -0,0 +1,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 )