0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-23 23:18:48 -05:00

Merge pull request #2601 from penpot/superalex-fix-paste-elements-from-outside-penpot-respect-hierarchy

🐛 Fix paste elements from outside penpot respect hierarchy
This commit is contained in:
Pablo Alba 2022-11-30 16:40:30 +01:00 committed by GitHub
commit c1a1120137
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 24 deletions

View file

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

View file

@ -285,9 +285,14 @@
page-id (:current-page-id state)
frame-id (-> (wsh/lookup-page-objects state page-id)
(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)
(merge data)
(merge {:x x :y y})
(assoc :frame-id frame-id)
(assoc :frame-id frame-id :parent-id parent-id)
(cts/setup-rect-selrect))]
(rx/of (add-shape shape))))))

View file

@ -12,6 +12,7 @@
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh]
[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.types.shape :as cts]
[app.common.types.shape-tree :as ctst]
@ -184,12 +185,13 @@
(assoc :x offset-x :y offset-y)))
(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]
(-> {:id (uuid/next)
:type :group
:name name
:frame-id frame-id
:parent-id parent-id
:width width
:height height
:x (+ x offset-x)
@ -439,6 +441,11 @@
frame-id (ctst/top-nested-frame objects position)
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)
x (- x vb-x (/ vb-width 2))
y (- y vb-y (/ vb-height 2))
@ -464,7 +471,7 @@
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)
;; 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))))
;; 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)
(pcb/with-objects objects)