diff --git a/common/src/app/common/logic/variant_properties.cljc b/common/src/app/common/logic/variant_properties.cljc index f24b576a18..2768c136fc 100644 --- a/common/src/app/common/logic/variant_properties.cljc +++ b/common/src/app/common/logic/variant_properties.cljc @@ -61,6 +61,19 @@ changes))) +(defn generate-remove-empty-properties + "Remove each property of the variant `variant-id` whose value is empty in + all its components." + [changes variant-id] + (let [data (pcb/get-library-data changes) + objects (pcb/get-objects changes) + components (cfv/find-variant-components data objects variant-id) + empty-pos (filter (fn [pos] + (every? #(empty? (get-in % [:variant-properties pos :value])) components)) + (range (count (-> components first :variant-properties))))] + ;; from the last one, so the positions of the others stay + (reduce #(generate-remove-property %1 variant-id %2) changes (reverse empty-pos)))) + (defn generate-update-property-value [changes component-id pos value] (if-let [value (ctv/prepare-property-value value)] diff --git a/frontend/src/app/main/data/workspace/variants.cljs b/frontend/src/app/main/data/workspace/variants.cljs index 5471b3fdd7..19508655db 100644 --- a/frontend/src/app/main/data/workspace/variants.cljs +++ b/frontend/src/app/main/data/workspace/variants.cljs @@ -200,35 +200,12 @@ (ptk/reify ::remove-empty-properties ptk/WatchEvent (watch [it state _] - (when-let [changes (variant-changes it state {:variant-id variant-id})] - (let [data (dsh/lookup-file-data state) - - variant-components (cfv/find-variant-components data (pcb/get-objects changes) variant-id) - - properties-empty-pos (->> variant-components - (mapcat :variant-properties) - (group-by :name) - (map-indexed - (fn [i [_ v]] - [i (->> v - (map :value) - (remove empty?) - empty?)])) - (reverse)) - - changes (reduce - (fn [changes [pos property-empty?]] - (if property-empty? - (-> changes - (clvp/generate-remove-property variant-id pos)) - changes)) - changes - properties-empty-pos)] - - (when (seq (:redo-changes changes)) - (rx/of - (ev/event {::ev/name "variant-remove-property" ::ev/origin "workspace:rename-in-layers"}) - (dch/commit-changes changes)))))))) + (when-let [changes (some-> (variant-changes it state {:variant-id variant-id}) + (clvp/generate-remove-empty-properties variant-id))] + (when (seq (:redo-changes changes)) + (rx/of + (ev/event {::ev/name "variant-remove-property" ::ev/origin "workspace:rename-in-layers"}) + (dch/commit-changes changes))))))) (defn add-new-property "Add a new variant property to all the components with this variant-id" diff --git a/frontend/test/frontend_tests/data/workspace_variants_test.cljs b/frontend/test/frontend_tests/data/workspace_variants_test.cljs index bca5869c06..2014da825b 100644 --- a/frontend/test/frontend_tests/data/workspace_variants_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_variants_test.cljs @@ -793,3 +793,23 @@ (let [file' (ths/get-file-from-state state)] (t/is (= "Renamed" (:name (ctkl/get-component (:data file') (cthi/id :plain1))))) (t/is (= "Renamed" (:name (cths/get-shape file' :plain1-root)))))))))) + +(t/deftest remove-empty-properties-of-a-set-with-many-properties + (t/async + done + (let [props (fn [value] + (mapv (fn [i] + {:name (str "Property " (inc i)) + :value (if (= i 5) "" value)}) + (range 10))) + file (-> (cthf/sample-file :file1) + (cthv/add-variant :v01 :c01 :m01 :c02 :m02) + (cthc/update-component :c01 {:variant-properties (props "a")}) + (cthc/update-component :c02 {:variant-properties (props "b")}))] + (ths/run-store + (ths/setup-store file) done + [(dwv/remove-empty-properties (cthi/id :v01))] + (fn [state] + (let [data (:data (ths/get-file-from-state state))] + (t/is (= (remove #{"Property 6"} (map :name (props "a"))) + (map :name (:variant-properties (ctkl/get-component data (cthi/id :c01))))))))))))