From ad6abcd5fc5e4e66062c8a01b511a1bd4bda2e94 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 9 Aug 2020 08:37:18 +0200 Subject: Export as CSV --- src/Lib/Dom/Element.ml | 3 +++ src/Lib/Dom/HA.ml | 2 ++ src/Lib/File.ml | 12 ++++++++++++ src/Lib/Fun.ml | 2 ++ src/Lib/URI.ml | 2 ++ 5 files changed, 21 insertions(+) create mode 100644 src/Lib/File.ml create mode 100644 src/Lib/Fun.ml create mode 100644 src/Lib/URI.ml (limited to 'src/Lib') diff --git a/src/Lib/Dom/Element.ml b/src/Lib/Dom/Element.ml index 3c63ef4..90c0321 100644 --- a/src/Lib/Dom/Element.ml +++ b/src/Lib/Dom/Element.ml @@ -27,6 +27,9 @@ external first_child : Dom.element -> Dom.element Js.Nullable.t = "firstChild" external remove_child : Dom.element -> Dom.element -> unit = "removeChild" [@@bs.send] +external click : Dom.element -> unit = "click" + [@@bs.send] + let remove_first_child element = match Js.toOption (first_child element) with | Some child -> diff --git a/src/Lib/Dom/HA.ml b/src/Lib/Dom/HA.ml index 53fb84d..ce02f2a 100644 --- a/src/Lib/Dom/HA.ml +++ b/src/Lib/Dom/HA.ml @@ -39,3 +39,5 @@ let style v = H.TextAttr ("style", v) let href v = H.TextAttr ("href", v) let autocomplete v = H.TextAttr ("autocomplete", v) + +let download v = H.TextAttr ("download", v) diff --git a/src/Lib/File.ml b/src/Lib/File.ml new file mode 100644 index 0000000..0089001 --- /dev/null +++ b/src/Lib/File.ml @@ -0,0 +1,12 @@ +let download filename content = + let a = + H.a + [| HA.href ("data:text/plain;charset=utf-8," ^ URI.encode content) + ; HA.download filename + ; HA.style "display:none" + |] + [| |] + in + let () = Element.append_child Document.body a in + let () = Element.click a in + Element.remove_child Document.body a diff --git a/src/Lib/Fun.ml b/src/Lib/Fun.ml new file mode 100644 index 0000000..bf1eb38 --- /dev/null +++ b/src/Lib/Fun.ml @@ -0,0 +1,2 @@ +let flip f b a = + f a b diff --git a/src/Lib/URI.ml b/src/Lib/URI.ml new file mode 100644 index 0000000..705bc7b --- /dev/null +++ b/src/Lib/URI.ml @@ -0,0 +1,2 @@ +external encode : string -> string = "encodeURIComponent" + [@@bs.val] -- cgit v1.2.3