🐛 Fix problem with copy/paste shapes

This commit is contained in:
alonso.torres 2023-03-28 17:18:56 +02:00
parent d54e152a3d
commit 43fe2390c8
2 changed files with 31 additions and 18 deletions

View file

@ -595,3 +595,25 @@
(d/enumerate) (d/enumerate)
(sort comparator-layout-z-index) (sort comparator-layout-z-index)
(mapv second))) (mapv second)))
(defn common-parent-frame
"Search for the common frame for the selected shapes. Otherwise returns the root frame"
[objects selected]
(loop [frame-id (get-in objects [(first selected) :frame-id])
frame-parents (get-parent-ids objects frame-id)
selected (rest selected)]
(if (empty? selected)
frame-id
(let [current (first selected)
parent? (into #{} (get-parent-ids objects current))
[frame-id frame-parents]
(if (parent? frame-id)
[frame-id frame-parents]
(let [frame-id (d/seek parent? frame-parents)]
[frame-id (get-parent-ids objects frame-id)]))]
(recur frame-id frame-parents (rest selected))))))

View file

@ -1304,8 +1304,8 @@
;; Prepare the shape object. Mainly needed for image shapes ;; Prepare the shape object. Mainly needed for image shapes
;; for retrieve the image data and convert it to the ;; for retrieve the image data and convert it to the
;; data-url. ;; data-url.
(prepare-object [objects selected+children {:keys [type] :as obj}] (prepare-object [objects parent-frame-id {:keys [type] :as obj}]
(let [obj (maybe-translate obj objects selected+children)] (let [obj (maybe-translate obj objects parent-frame-id)]
(if (= type :image) (if (= type :image)
(let [url (cf/resolve-file-media (:metadata obj))] (let [url (cf/resolve-file-media (:metadata obj))]
(->> (http/send! {:method :get (->> (http/send! {:method :get
@ -1328,20 +1328,11 @@
(update res :images conj img-part)) (update res :images conj img-part))
res))) res)))
(maybe-translate [shape objects selected+children] (maybe-translate [shape objects parent-frame-id]
(let [frame-id (:frame-id shape) (if (= parent-frame-id uuid/zero)
root-frame-id (cph/get-shape-id-root-frame objects (:id shape))] shape
(cond (let [frame (get objects parent-frame-id)]
(cph/root-frame? shape) shape (gsh/translate-to-frame shape frame))))
(contains? selected+children root-frame-id) shape
(cph/frame-shape? shape)
(let [frame (get objects root-frame-id)]
(gsh/translate-to-frame shape frame))
:else
(let [frame (get objects frame-id)]
(gsh/translate-to-frame shape frame)))))
(on-copy-error [error] (on-copy-error [error]
(js/console.error "Clipboard blocked:" error) (js/console.error "Clipboard blocked:" error)
@ -1354,7 +1345,7 @@
selected (->> (wsh/lookup-selected state) selected (->> (wsh/lookup-selected state)
(cph/clean-loops objects)) (cph/clean-loops objects))
selected+children (cph/selected-with-children objects selected) parent-frame-id (cph/common-parent-frame objects selected)
pdata (reduce (partial collect-object-ids objects) {} selected) pdata (reduce (partial collect-object-ids objects) {} selected)
initial {:type :copied-shapes initial {:type :copied-shapes
:file-id (:current-file-id state) :file-id (:current-file-id state)
@ -1369,7 +1360,7 @@
(catch :default e (catch :default e
(on-copy-error e))) (on-copy-error e)))
(->> (rx/from (seq (vals pdata))) (->> (rx/from (seq (vals pdata)))
(rx/merge-map (partial prepare-object objects selected+children)) (rx/merge-map (partial prepare-object objects parent-frame-id))
(rx/reduce collect-data initial) (rx/reduce collect-data initial)
(rx/map (partial sort-selected state)) (rx/map (partial sort-selected state))
(rx/map t/encode-str) (rx/map t/encode-str)