♻️ Change paste implementation to work with more browsers

This commit is contained in:
Andrés Moya 2020-12-02 08:44:39 +01:00 committed by Andrey Antukh
parent 264811c5ee
commit 6186d82151
4 changed files with 149 additions and 83 deletions

View file

@ -79,12 +79,15 @@
(let [cboard (unchecked-get js/navigator "clipboard")]
(.writeText ^js cboard data)))
(defn- read-from-clipboard
(defn read-from-clipboard
[]
(let [cboard (unchecked-get js/navigator "clipboard")]
(rx/from (.readText ^js cboard))))
(if (.-readText ^js cboard)
(rx/from (.readText ^js cboard))
(throw (ex-info "This browser does not implement read from clipboard protocol"
{:not-implemented true})))))
(defn- read-image-from-clipboard
(defn read-image-from-clipboard
[]
(let [cboard (unchecked-get js/navigator "clipboard")
read-item (fn [item]
@ -97,6 +100,28 @@
(rx/mapcat identity) ;; Convert each item into an emission
(rx/switch-map read-item))))
(defn read-from-paste-event
[event]
(let [target (.-target ^js event)]
(when (and (not (.-isContentEditable target)) ;; ignore when pasting into
(not= (.-tagName target) "INPUT")) ;; an editable control
(-> ^js event
(.getBrowserEvent)
(.-clipboardData)))))
(defn extract-text
[clipboard-data]
(when clipboard-data
(.getData clipboard-data "text")))
(defn extract-images
[clipboard-data]
(when clipboard-data
(let [file-list (-> (.-files ^js clipboard-data))]
(->> (range (.-length file-list))
(map #(.item file-list %))
(filter #(str/starts-with? (.-type %) "image/"))))))
(defn request-fullscreen
[el]
(cond