diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index 032e9588d..fce96410d 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -9,21 +9,6 @@ [app.common.types.file :as ctf] [app.common.types.variant :as ctv])) - -(defn- generate-path - [path objects base-id shape] - (let [get-type #(case % - :frame :container - :group :container - :rect :shape - :circle :shape - :bool :shape - :path :shape - %)] - (if (= base-id (:id shape)) - path - (generate-path (str path " " (:name shape) (get-type (:type shape))) objects base-id (get objects (:parent-id shape)))))) - (defn generate-add-new-variant [changes shape variant-id new-component-id new-shape-id prop-num] (let [data (pcb/get-library-data changes) @@ -46,20 +31,56 @@ (clvp/generate-update-property-value new-component-id prop-num value) (pcb/change-parent (:parent-id shape) [new-shape] 0)))) +(defn- generate-path + [path objects base-id shape] + (let [get-type #(case % + :frame :container + :group :container + :rect :shape + :circle :shape + :bool :shape + :path :shape + %)] + (if (= base-id (:id shape)) + path + (generate-path (str path " " (:name shape) (get-type (:type shape))) objects base-id (get objects (:parent-id shape)))))) + +(defn- add-unique-path + "Adds a new property :shape-path to the shape, with the path of the shape. + Suffixes like -1, -2, etc. are added to ensure uniqueness." + [shapes objects base-id] + (letfn [(unique-path [shape counts] + (let [path (generate-path "" objects base-id shape) + num (get counts path 1)] + [(str path "-" num) (update counts path (fnil inc 1))]))] + (first + (reduce + (fn [[result counts] shape] + (let [[shape-path counts'] (unique-path shape counts)] + [(conj result (assoc shape :shape-path shape-path)) counts'])) + [[] {}] + shapes)))) + + (defn generate-keep-touched [changes new-shape original-shape original-shapes page libraries] - (let [objects (pcb/get-objects changes) - new-path-map (into {} - (map (fn [shape] {(generate-path "" objects (:id new-shape) shape) shape})) - (cfh/get-children-with-self objects (:id new-shape))) + (let [objects (pcb/get-objects changes) + orig-objects (into {} (map (juxt :id identity) original-shapes)) + orig-shapes-w-path (add-unique-path + (reverse original-shapes) + orig-objects + (:id original-shape)) + new-shapes-w-path (add-unique-path + (reverse (cfh/get-children-with-self objects (:id new-shape))) + objects + (:id new-shape)) + new-shapes-map (into {} (map (juxt :shape-path identity) new-shapes-w-path)) + orig-touched (filter (comp seq :touched) orig-shapes-w-path) - orig-touched (filter (comp seq :touched) original-shapes) - orig-objects (into {} (map (juxt :id identity) original-shapes)) - container (ctn/make-container page :page)] + container (ctn/make-container page :page)] (reduce (fn [changes touched-shape] - (let [path (generate-path "" orig-objects (:id original-shape) touched-shape) - related-shape (get new-path-map path) + (let [related-shape (get new-shapes-map (:shape-path touched-shape)) orig-ref-shape (ctf/find-ref-shape nil container libraries touched-shape)] (if related-shape (cll/update-attrs-on-switch