-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipboard_fx.cljs
34 lines (30 loc) · 1.13 KB
/
clipboard_fx.cljs
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
(ns re-frame.clipboard-fx
(:require
[oops.core :refer [oget oset! ocall oapply ocall! oapply!
oget+ oset!+ ocall+ oapply+ ocall!+ oapply!+]]
[re-frame.core :refer [reg-fx dispatch]]))
;; Utilities
;; =============================================================================
(defn create-invisible-textarea
[text]
(doto (ocall js/document :createElement "textarea")
(oset! :value text)
(ocall :setAttribute "readonly" "")
(oset! :style.position "absolute")
(oset! :style.left "-9999px")))
;; Effects and Handlers
;; =============================================================================
(reg-fx
::copy
(fn [{:keys [text on-success on-failure]
:or {on-success [:clipboard-no-on-success]
on-failure [:clipboard-no-on-failure]}}]
(try
(let [el (create-invisible-textarea text)]
(ocall js/document :body.appendChild el)
(ocall el :select)
(ocall js/document :execCommand "copy")
(ocall js/document :body.removeChild el)
(dispatch on-success))
(catch js/Object error
(dispatch (conj on-failure error))))))