diff --git a/common/src/app/common/files/variant.cljc b/common/src/app/common/files/variant.cljc index 989a80a58b..941bdf1f04 100644 --- a/common/src/app/common/files/variant.cljc +++ b/common/src/app/common/files/variant.cljc @@ -47,6 +47,19 @@ :value (->> v (map :value) distinct)} mdata)))))) +(defn new-property-value + "A \"Value n\" no component of the variant `variant-id` has at the + property position `pos`, n counting up from one more than the values + they have there." + [data objects variant-id pos] + (let [values (->> (find-variant-components data objects variant-id) + (keep #(dm/get-in % [:variant-properties pos :value])) + distinct)] + (->> (iterate inc (inc (count values))) + (map #(str ctv/value-prefix %)) + (remove (set values)) + first))) + (defn get-variant-mains [component data] (assert (ctv/valid-variant-component? component) "expected valid component variant") diff --git a/common/src/app/common/logic/duplicate.cljc b/common/src/app/common/logic/duplicate.cljc index c8839ecc3d..837bc4c73f 100644 --- a/common/src/app/common/logic/duplicate.cljc +++ b/common/src/app/common/logic/duplicate.cljc @@ -218,12 +218,8 @@ :page-id page-id :new-component-file new-component-file}) value (when into-new-variant? - (str ctv/value-prefix - (-> (cfv/extract-properties-values (:data library) objects (:id parent)) - last - :value - count - inc)))] + (cfv/new-property-value (:data library) objects (:id parent) + (-> component :variant-properties count dec)))] [shape (cond-> changes diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index a302b044d1..474e525800 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -68,12 +68,7 @@ (let [data (pcb/get-library-data changes) objects (pcb/get-objects changes) component-id (:component-id shape) - values (-> (cfv/extract-properties-values data objects variant-id) last :value) - ;; the first "Value n" from the number of values up that no member has - value (->> (iterate inc (inc (count values))) - (map #(str ctv/value-prefix %)) - (remove (set values)) - first) + value (cfv/new-property-value data objects variant-id prop-num) [new-shape changes] (-> changes (cld/generate-duplicate-component diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 0dadd8cfda..ae62a505f9 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1708,6 +1708,15 @@ [:text 26 3 0 0] [:add-variant 2] [:switch 0] [:variant-property 0 0 0 0] [:switch 20]]})))) +(t/deftest adding-a-variant-to-a-set-of-many-properties-takes-a-new-last-value + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout nil :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children false :grid-empty false :layout-copies true :text true}] + :ops [[:variant-property 0 0 0 0] [:switch 0] [:variant-property 0 0 0 0] [:switch 0] + [:variant-property 0 0 0 0] [:variant-property 0 0 0 0] [:switch 0] + [:variant-property 0 0 0 0] [:switch 0] [:variant-property 0 0 0 0] [:switch 0] + [:add-variant 0] [:switch 0] [:add-variant 0] [:switch 0]]})))) + (t/deftest random-variant-compositions (let [coverage (atom {}) failure (atom nil)] diff --git a/common/test/common_tests/logic/variants_test.cljc b/common/test/common_tests/logic/variants_test.cljc index bea6b11449..989c937ad2 100644 --- a/common/test/common_tests/logic/variants_test.cljc +++ b/common/test/common_tests/logic/variants_test.cljc @@ -516,6 +516,52 @@ (t/is (= "Value 4" (get-in file' [:data :components new-id :variant-properties 0 :value]))) (t/is (apply distinct? values)))) +(t/deftest test-add-new-variant-counts-the-values-of-the-last-of-many-properties + (let [;; ==== Setup + props (fn [size last-value] + (into [{:name "Size" :value size} {:name "State" :value "Idle"} {:name "Theme" :value "Light"}] + (concat (map #(hash-map :name (str "Property " %) :value "") (range 4 9)) + [{:name "Property 9" :value last-value}]))) + file (-> (thf/sample-file :file1) + (thv/add-variant :v01 :c01 :m01 :c02 :m02) + (thc/update-component :c01 {:variant-properties (props "Small" "Value 2")}) + (ths/update-shape :m01 :variant-name "Small, Idle, Light, Value 2") + (thc/update-component :c02 {:variant-properties (props "Large" "Value 1")}) + (ths/update-shape :m02 :variant-name "Large, Idle, Light, Value 1")) + page (thf/current-page file) + new-id (uuid/next) + + ;; ==== Action + changes (-> (pcb/empty-changes nil (:id page)) + (pcb/with-library-data (:data file)) + (pcb/with-objects (:objects page)) + (clv/generate-add-new-variant (ths/get-shape file :m01) (thi/id :v01) new-id (uuid/next) 8)) + file' (thf/apply-changes file changes)] + + ;; ==== Check + ;; nine property names group into a map whose last entry is not the last property + (t/is (= "Value 3" (get-in file' [:data :components new-id :variant-properties 8 :value]))))) + +(t/deftest test-duplicate-variant-main-takes-a-value-no-member-has + (let [;; ==== Setup + file (-> (thf/sample-file :file1) + (thv/add-variant :v01 :c01 :m01 :c02 :m02) + (thc/update-component :c02 {:variant-properties [{:name "Property 1" :value "Value 3"}]}) + (ths/update-shape :m02 :variant-name "Value 3")) + + ;; ==== Action + file' (tho/duplicate-shape file :m01) + + ;; ==== Get + values (->> (vals (get-in file' [:data :components])) + (remove :deleted) + (map #(-> % :variant-properties first :value)))] + + ;; ==== Check + ;; two values make the next one "Value 3", which a member already has + (t/is (= 3 (count values))) + (t/is (apply distinct? values)))) + (t/deftest test-delete-variant ;; When a variant container becomes empty, it id automatically deleted (let [;; ==== Setup