Merge pull request #1096 from penpot/fix-duplicate-names

🐛 Fix repeated names when duplicating object trees.
This commit is contained in:
Andrey Antukh 2021-07-16 16:26:56 +02:00 committed by GitHub
commit 2758b6ffd9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 32 deletions

View file

@ -7,6 +7,7 @@
### :bug: Bugs fixed ### :bug: Bugs fixed
- Add scroll bar to Teams menu [Taiga #1894](https://tree.taiga.io/project/penpot/issue/1894). - Add scroll bar to Teams menu [Taiga #1894](https://tree.taiga.io/project/penpot/issue/1894).
- Fix repeated names when duplicating artboards or groups [Taiga #1892](https://tree.taiga.io/project/penpot/issue/1892).
### :arrow_up: Deps updates ### :arrow_up: Deps updates
### :boom: Breaking changes ### :boom: Breaking changes

View file

@ -1474,8 +1474,8 @@
(= :frame (get-in objects [(first selected) :type]))))) (= :frame (get-in objects [(first selected) :type])))))
(defn- paste-shape (defn- paste-shape
[{:keys [selected objects images] :as data} in-viewport?] [{:keys [selected objects images] :as data} in-viewport?] ;; TODO: perhaps rename 'objects' to 'shapes', because it contains only
(letfn [;; Given a file-id and img (part generated by the (letfn [;; Given a file-id and img (part generated by the ;; the shapes to paste, not the whole page tree of shapes
;; copy-selected event), uploads the new media. ;; copy-selected event), uploads the new media.
(upload-media [file-id imgpart] (upload-media [file-id imgpart]
(->> (http/send! {:uri (:file-data imgpart) (->> (http/send! {:uri (:file-data imgpart)
@ -1583,7 +1583,7 @@
page-id (:current-page-id state) page-id (:current-page-id state)
unames (-> (wsh/lookup-page-objects state page-id) unames (-> (wsh/lookup-page-objects state page-id)
(dwc/retrieve-used-names)) (dwc/retrieve-used-names)) ;; TODO: move this calculation inside prepare-duplcate-changes?
rchanges (->> (dws/prepare-duplicate-changes objects page-id unames selected delta) rchanges (->> (dws/prepare-duplicate-changes objects page-id unames selected delta)
(mapv (partial process-rchange media-idx)) (mapv (partial process-rchange media-idx))

View file

@ -382,7 +382,7 @@
page-id (:current-page-id state) page-id (:current-page-id state)
objects (wsh/lookup-page-objects state page-id) objects (wsh/lookup-page-objects state page-id)
unames (atom (dwc/retrieve-used-names objects)) unames (volatile! (dwc/retrieve-used-names objects))
frame-id (cp/frame-id-by-position objects (gpt/add orig-pos delta)) frame-id (cp/frame-id-by-position objects (gpt/add orig-pos delta))
@ -391,7 +391,7 @@
(let [new-name (dwc/generate-unique-name @unames (:name new-shape))] (let [new-name (dwc/generate-unique-name @unames (:name new-shape))]
(when (nil? (:parent-id original-shape)) (when (nil? (:parent-id original-shape))
(swap! unames conj new-name)) (vswap! unames conj new-name))
(cond-> new-shape (cond-> new-shape
true true

View file

@ -257,8 +257,6 @@
(declare prepare-duplicate-frame-change) (declare prepare-duplicate-frame-change)
(declare prepare-duplicate-shape-change) (declare prepare-duplicate-shape-change)
(def ^:private change->name #(get-in % [:obj :name]))
(defn update-indices (defn update-indices
"Fixes the indices for a set of changes after a duplication. We need to "Fixes the indices for a set of changes after a duplication. We need to
fix the indices to take into the account the movement of indices. fix the indices to take into the account the movement of indices.
@ -290,19 +288,19 @@
"Prepare objects to paste: generate new id, give them unique names, "Prepare objects to paste: generate new id, give them unique names,
move to the position of mouse pointer, and find in what frame they move to the position of mouse pointer, and find in what frame they
fit." fit."
[objects page-id names ids delta] [objects page-id unames ids delta]
(loop [names names (let [unames (volatile! unames)
ids (seq ids) update-unames! (fn [new-name] (vswap! unames conj new-name))]
chgs []] (loop [ids (seq ids)
(if ids chgs []]
(let [id (first ids) (if ids
result (prepare-duplicate-change objects page-id names id delta) (let [id (first ids)
result (if (vector? result) result [result])] result (prepare-duplicate-change objects page-id unames update-unames! id delta)
(recur result (if (vector? result) result [result])]
(into names (map change->name) result) (recur
(next ids) (next ids)
(into chgs result))) (into chgs result)))
chgs))) chgs))))
(defn duplicate-changes-update-indices (defn duplicate-changes-update-indices
"Parses the change set when duplicating to set-up the appropiate indices" "Parses the change set when duplicating to set-up the appropiate indices"
@ -317,32 +315,32 @@
(-> changes (update-indices index-map)))) (-> changes (update-indices index-map))))
(defn- prepare-duplicate-change (defn- prepare-duplicate-change
[objects page-id names id delta] [objects page-id unames update-unames! id delta]
(let [obj (get objects id)] (let [obj (get objects id)]
(if (= :frame (:type obj)) (if (= :frame (:type obj))
(prepare-duplicate-frame-change objects page-id names obj delta) (prepare-duplicate-frame-change objects page-id unames update-unames! obj delta)
(prepare-duplicate-shape-change objects page-id names obj delta (:frame-id obj) (:parent-id obj))))) (prepare-duplicate-shape-change objects page-id unames update-unames! obj delta (:frame-id obj) (:parent-id obj)))))
(defn- prepare-duplicate-shape-change (defn- prepare-duplicate-shape-change
[objects page-id names obj delta frame-id parent-id] [objects page-id unames update-unames! obj delta frame-id parent-id]
(when (some? obj) (when (some? obj)
(let [id (uuid/next) (let [id (uuid/next)
name (dwc/generate-unique-name names (:name obj)) name (dwc/generate-unique-name @unames (:name obj))
_ (update-unames! name)
renamed-obj (assoc obj :id id :name name) renamed-obj (assoc obj :id id :name name)
moved-obj (geom/move renamed-obj delta) moved-obj (geom/move renamed-obj delta)
parent-id (or parent-id frame-id) parent-id (or parent-id frame-id)
children-changes children-changes
(loop [names names (loop [result []
result []
cid (first (:shapes obj)) cid (first (:shapes obj))
cids (rest (:shapes obj))] cids (rest (:shapes obj))]
(if (nil? cid) (if (nil? cid)
result result
(let [obj (get objects cid) (let [obj (get objects cid)
changes (prepare-duplicate-shape-change objects page-id names obj delta frame-id id)] changes (prepare-duplicate-shape-change objects page-id unames update-unames! obj delta frame-id id)]
(recur (recur
(into names (map change->name changes))
(into result changes) (into result changes)
(first cids) (first cids)
(rest cids))))) (rest cids)))))
@ -361,11 +359,13 @@
children-changes)))) children-changes))))
(defn- prepare-duplicate-frame-change (defn- prepare-duplicate-frame-change
[objects page-id names obj delta] [objects page-id unames update-unames! obj delta]
(let [frame-id (uuid/next) (let [frame-id (uuid/next)
frame-name (dwc/generate-unique-name names (:name obj)) frame-name (dwc/generate-unique-name @unames (:name obj))
_ (update-unames! frame-name)
sch (->> (map #(get objects %) (:shapes obj)) sch (->> (map #(get objects %) (:shapes obj))
(mapcat #(prepare-duplicate-shape-change objects page-id names % delta frame-id frame-id))) (mapcat #(prepare-duplicate-shape-change objects page-id unames update-unames! % delta frame-id frame-id)))
frame (-> obj frame (-> obj
(assoc :id frame-id) (assoc :id frame-id)