mirror of
https://github.com/penpot/penpot.git
synced 2025-05-09 23:36:38 +02:00
✨ Allows paste plain text into worskpace
This commit is contained in:
parent
bd31e5742f
commit
a227a0fe21
2 changed files with 78 additions and 12 deletions
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
(ns app.main.data.workspace
|
(ns app.main.data.workspace
|
||||||
(:require
|
(:require
|
||||||
|
[cuerdas.core :as str]
|
||||||
[app.common.data :as d]
|
[app.common.data :as d]
|
||||||
[app.common.exceptions :as ex]
|
[app.common.exceptions :as ex]
|
||||||
[app.common.geom.matrix :as gmt]
|
[app.common.geom.matrix :as gmt]
|
||||||
|
@ -1236,22 +1237,80 @@
|
||||||
(with-meta params
|
(with-meta params
|
||||||
{:on-success image-uploaded})))))))
|
{:on-success image-uploaded})))))))
|
||||||
|
|
||||||
|
(declare paste-text)
|
||||||
|
|
||||||
(def paste
|
(def paste
|
||||||
(ptk/reify ::paste
|
(ptk/reify ::paste
|
||||||
ptk/WatchEvent
|
ptk/WatchEvent
|
||||||
(watch [_ state stream]
|
(watch [_ state stream]
|
||||||
(->> (wapi/read-from-clipboard)
|
(try
|
||||||
(rx/map t/decode)
|
(let [clipboard-str (wapi/read-from-clipboard)
|
||||||
(rx/filter #(= :copied-shapes (:type %)))
|
|
||||||
(rx/map #(select-keys % [:selected :objects]))
|
paste-transit-str
|
||||||
(rx/map paste-impl)
|
(->> clipboard-str
|
||||||
(rx/catch (partial instance? js/SyntaxError)
|
(rx/filter t/transit?)
|
||||||
(fn [_]
|
(rx/map t/decode)
|
||||||
(->> (wapi/read-image-from-clipboard)
|
(rx/filter #(= :copied-shapes (:type %)))
|
||||||
(rx/map paste-image-impl))))
|
(rx/map #(select-keys % [:selected :objects]))
|
||||||
(rx/catch (fn [err]
|
(rx/map paste-impl))
|
||||||
(js/console.error "Clipboard error:" err)
|
|
||||||
(rx/empty)))))))
|
paste-plain-text-str
|
||||||
|
(->> clipboard-str
|
||||||
|
(rx/filter (comp not empty?))
|
||||||
|
(rx/map paste-text))
|
||||||
|
|
||||||
|
paste-image-str
|
||||||
|
(->> (wapi/read-image-from-clipboard)
|
||||||
|
(rx/map paste-image-impl))]
|
||||||
|
|
||||||
|
(->> (rx/concat paste-transit-str
|
||||||
|
paste-plain-text-str
|
||||||
|
paste-image-str)
|
||||||
|
(rx/first)
|
||||||
|
(rx/catch
|
||||||
|
(fn [err]
|
||||||
|
(js/console.error "Clipboard error:" err)
|
||||||
|
(rx/empty)))))
|
||||||
|
(catch :default e
|
||||||
|
(.error js/console "ERROR" e))))))
|
||||||
|
|
||||||
|
(defn as-content [text]
|
||||||
|
(let [paragraphs (->> (str/split text "\n")
|
||||||
|
(map str/trim)
|
||||||
|
(mapv #(hash-map :type "paragraph"
|
||||||
|
:children [{:text %}])))]
|
||||||
|
{:type "root"
|
||||||
|
:children [{:type "paragraph-set" :children paragraphs}]}))
|
||||||
|
|
||||||
|
(defn paste-text [text]
|
||||||
|
(s/assert string? text)
|
||||||
|
(ptk/reify ::paste-text
|
||||||
|
ptk/WatchEvent
|
||||||
|
(watch [_ state stream]
|
||||||
|
(let [id (uuid/next)
|
||||||
|
{:keys [x y]} @ms/mouse-position
|
||||||
|
width (* 7 (count text))
|
||||||
|
height 16
|
||||||
|
shape {:id id
|
||||||
|
:type :text
|
||||||
|
:name "Text"
|
||||||
|
:x x
|
||||||
|
:y y
|
||||||
|
:selrect {:x1 x :y1 y
|
||||||
|
:x2 (+ x width)
|
||||||
|
:y2 (+ y height)
|
||||||
|
:x x :y y
|
||||||
|
:width width
|
||||||
|
:height height}
|
||||||
|
:width width
|
||||||
|
:height height
|
||||||
|
:grow-type :auto-width
|
||||||
|
:content (as-content text)}]
|
||||||
|
(rx/of dwc/start-undo-transaction
|
||||||
|
dws/deselect-all
|
||||||
|
(add-shape shape)
|
||||||
|
(dwc/rehash-shape-frame-relationship [id])
|
||||||
|
dwc/commit-undo-transaction)))))
|
||||||
|
|
||||||
(defn update-shape-flags
|
(defn update-shape-flags
|
||||||
[id {:keys [blocked hidden] :as flags}]
|
[id {:keys [blocked hidden] :as flags}]
|
||||||
|
|
|
@ -94,3 +94,10 @@
|
||||||
(t/write w data))
|
(t/write w data))
|
||||||
(catch :default e
|
(catch :default e
|
||||||
(throw e))))
|
(throw e))))
|
||||||
|
|
||||||
|
(defn transit?
|
||||||
|
"Checks if a string can be decoded with transit"
|
||||||
|
[str]
|
||||||
|
(try
|
||||||
|
(-> str decode nil? not)
|
||||||
|
(catch js/SyntaxError e false)))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue