summaryrefslogtreecommitdiff
path: root/editor/actions/export.ml
blob: a97eeac23cba03ffdbf7514c475d627914c7b0c4 (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
module Js = Js_of_ocaml.Js

module M = struct

  type t = unit

  let update
    : t -> State.t -> State.t
    = fun _ state ->

      (* Save this as a json element. The text may contains UTF-16 characters,
         which will raise an error in the btoa function.

         As an easy solution, we convert them into UTF-8 through the native
         OCaml representation of string.
      *)
      let json = State.Storage.to_json ()
                 |> Jstr.to_string (* Encode into UTF-8 *)
                 |> Obj.magic      (* Then type the element again as a string. *)
      in
      Elements.Transfert.send
        ~mime_type:(Jstr.v "application/json")
        ~filename:(Jstr.v "export.json")
        json;

      (* The function does not actually update the state, and return it
         unchanged *)
      state

end

(** Create a new element *)
let create
  : unit -> State.event Note.event
  = fun () ->
    let title = Jstr.v "Confirmation"
    and message = Jstr.v "Exporter les notes" in
    let ev = Elements.Popup.create
        ~title
        ~form:(Some (Forms.Validation.create message ))
    in
    Note.E.map
      (fun v -> State.E
          ( v
          , (module M : State.Event with type t = M.t )))
      (Note.E.Option.on_some ev)