🐛 Fix paste elements from outside penpot respect hierarchy

This commit is contained in:
Alejandro Alonso 2022-11-25 11:30:27 +01:00
parent 0fece05cc9
commit a2b70f227c
3 changed files with 46 additions and 24 deletions

View file

@ -1210,7 +1210,6 @@
(watch [_ _ _] (watch [_ _ _]
(try (try
(let [clipboard-str (wapi/read-from-clipboard) (let [clipboard-str (wapi/read-from-clipboard)
paste-transit-str paste-transit-str
(->> clipboard-str (->> clipboard-str
(rx/filter t/transit?) (rx/filter t/transit?)
@ -1469,6 +1468,19 @@
{:type "root" {:type "root"
:children [{:type "paragraph-set" :children paragraphs}]})) :children [{:type "paragraph-set" :children paragraphs}]}))
(defn calculate-paste-position [state]
(cond
;; Pasting inside a frame
(selected-frame? state)
(let [page-selected (wsh/lookup-selected state)
page-objects (wsh/lookup-page-objects state)
frame-id (first page-selected)
frame-object (get page-objects frame-id)]
(gsh/center-shape frame-object))
:else
(deref ms/mouse-position)))
(defn paste-text (defn paste-text
[text] [text]
(us/assert string? text) (us/assert string? text)
@ -1476,26 +1488,22 @@
ptk/WatchEvent ptk/WatchEvent
(watch [_ state _] (watch [_ state _]
(let [id (uuid/next) (let [id (uuid/next)
{:keys [x y]} @ms/mouse-position
width (max 8 (min (* 7 (count text)) 700)) width (max 8 (min (* 7 (count text)) 700))
height 16 height 16
page-id (:current-page-id state) {:keys [x y]} (calculate-paste-position state)
frame-id (-> (wsh/lookup-page-objects state page-id)
(ctst/top-nested-frame @ms/mouse-position)) shape {:id id
shape (cts/setup-rect-selrect :type :text
{:id id :name "Text"
:type :text :x x
:name "Text" :y y
:x x :width width
:y y :height height
:frame-id frame-id :grow-type (if (> (count text) 100) :auto-height :auto-width)
:width width :content (as-content text)}]
:height height
:grow-type (if (> (count text) 100) :auto-height :auto-width)
:content (as-content text)})]
(rx/of (dwu/start-undo-transaction) (rx/of (dwu/start-undo-transaction)
(dws/deselect-all) (dwsh/create-and-add-shape :text x y shape)
(dwsh/add-shape shape)
(dwu/commit-undo-transaction)))))) (dwu/commit-undo-transaction))))))
;; TODO: why not implement it in terms of upload-media-workspace? ;; TODO: why not implement it in terms of upload-media-workspace?
@ -1505,7 +1513,7 @@
(ptk/reify ::paste-svg (ptk/reify ::paste-svg
ptk/WatchEvent ptk/WatchEvent
(watch [_ state _] (watch [_ state _]
(let [position (deref ms/mouse-position) (let [position (calculate-paste-position state)
file-id (:current-file-id state)] file-id (:current-file-id state)]
(->> (dwm/svg->clj ["svg" text]) (->> (dwm/svg->clj ["svg" text])
(rx/map #(dwm/svg-uploaded % file-id position))))))) (rx/map #(dwm/svg-uploaded % file-id position)))))))
@ -1516,9 +1524,10 @@
ptk/WatchEvent ptk/WatchEvent
(watch [_ state _] (watch [_ state _]
(let [file-id (get-in state [:workspace-file :id]) (let [file-id (get-in state [:workspace-file :id])
position (calculate-paste-position state)
params {:file-id file-id params {:file-id file-id
:blobs [image] :blobs [image]
:position @ms/mouse-position}] :position position}]
(rx/of (dwm/upload-media-workspace params)))))) (rx/of (dwm/upload-media-workspace params))))))
(defn toggle-distances-display [value] (defn toggle-distances-display [value]

View file

@ -285,9 +285,14 @@
page-id (:current-page-id state) page-id (:current-page-id state)
frame-id (-> (wsh/lookup-page-objects state page-id) frame-id (-> (wsh/lookup-page-objects state page-id)
(ctst/top-nested-frame {:x frame-x :y frame-y})) (ctst/top-nested-frame {:x frame-x :y frame-y}))
page-objects (wsh/lookup-page-objects state)
page-selected (wsh/lookup-selected state)
base (cph/get-base-shape page-objects page-selected)
parent-id (:parent-id base)
shape (-> (cts/make-minimal-shape type) shape (-> (cts/make-minimal-shape type)
(merge data) (merge data)
(merge {:x x :y y}) (merge {:x x :y y})
(assoc :frame-id frame-id) (assoc :frame-id frame-id :parent-id parent-id)
(cts/setup-rect-selrect))] (cts/setup-rect-selrect))]
(rx/of (add-shape shape)))))) (rx/of (add-shape shape))))))

View file

@ -12,6 +12,7 @@
[app.common.geom.point :as gpt] [app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh] [app.common.geom.shapes :as gsh]
[app.common.pages.changes-builder :as pcb] [app.common.pages.changes-builder :as pcb]
[app.common.pages.helpers :as cph]
[app.common.spec :refer [max-safe-int min-safe-int]] [app.common.spec :refer [max-safe-int min-safe-int]]
[app.common.types.shape :as cts] [app.common.types.shape :as cts]
[app.common.types.shape-tree :as ctst] [app.common.types.shape-tree :as ctst]
@ -184,12 +185,13 @@
(assoc :x offset-x :y offset-y))) (assoc :x offset-x :y offset-y)))
(cts/setup-rect-selrect)))) (cts/setup-rect-selrect))))
(defn create-svg-root [frame-id svg-data] (defn create-svg-root [frame-id parent-id svg-data]
(let [{:keys [name x y width height offset-x offset-y]} svg-data] (let [{:keys [name x y width height offset-x offset-y]} svg-data]
(-> {:id (uuid/next) (-> {:id (uuid/next)
:type :group :type :group
:name name :name name
:frame-id frame-id :frame-id frame-id
:parent-id parent-id
:width width :width width
:height height :height height
:x (+ x offset-x) :x (+ x offset-x)
@ -439,6 +441,11 @@
frame-id (ctst/top-nested-frame objects position) frame-id (ctst/top-nested-frame objects position)
selected (wsh/lookup-selected state) selected (wsh/lookup-selected state)
page-objects (wsh/lookup-page-objects state)
page-selected (wsh/lookup-selected state)
base (cph/get-base-shape page-objects page-selected)
parent-id (:parent-id base)
[vb-x vb-y vb-width vb-height] (svg-dimensions svg-data) [vb-x vb-y vb-width vb-height] (svg-dimensions svg-data)
x (- x vb-x (/ vb-width 2)) x (- x vb-x (/ vb-width 2))
y (- y vb-y (/ vb-height 2)) y (- y vb-y (/ vb-height 2))
@ -464,7 +471,7 @@
svg-data (assoc svg-data :defs def-nodes) svg-data (assoc svg-data :defs def-nodes)
root-shape (create-svg-root frame-id svg-data) root-shape (create-svg-root frame-id parent-id svg-data)
root-id (:id root-shape) root-id (:id root-shape)
;; In penpot groups have the size of their children. To respect the imported svg size and empty space let's create a transparent shape as background to respect the imported size ;; In penpot groups have the size of their children. To respect the imported svg size and empty space let's create a transparent shape as background to respect the imported size
@ -483,7 +490,8 @@
(assoc :content (into [base-background-shape] (:content svg-data)))) (assoc :content (into [base-background-shape] (:content svg-data))))
;; Creates the root shape ;; Creates the root shape
new-shape (dwsh/make-new-shape root-shape objects selected) new-shape (-> (dwsh/make-new-shape root-shape objects selected)
(assoc :parent-id parent-id))
changes (-> (pcb/empty-changes it page-id) changes (-> (pcb/empty-changes it page-id)
(pcb/with-objects objects) (pcb/with-objects objects)