diff --git a/common/src/app/common/files/variant.cljc b/common/src/app/common/files/variant.cljc index 802c90fd1c..80dde18d72 100644 --- a/common/src/app/common/files/variant.cljc +++ b/common/src/app/common/files/variant.cljc @@ -59,7 +59,7 @@ [data objects variant-id] (->> (find-variant-components data objects variant-id) (mapcat :variant-properties) - (group-by :name) + (ctv/group-properties-by-name) (mapv (fn [[k v]] (let [mdata (reduce merge {} (map meta v))] (with-meta {:name k diff --git a/common/src/app/common/types/variant.cljc b/common/src/app/common/types/variant.cljc index f91075a170..0469c3c674 100644 --- a/common/src/app/common/types/variant.cljc +++ b/common/src/app/common/types/variant.cljc @@ -315,6 +315,16 @@ props2) :props))) +(defn group-properties-by-name + "Group properties by :name, preserving first-seen name order. + clojure.core/group-by loses order once maps grow past ArrayMap's + 8-key threshold." + [props] + (reduce (fn [m p] + (update m (:name p) (fnil conj []) p)) + (d/ordered-map) + props)) + (defn compare-properties "Compares vectors of properties keeping the value if it is the same for all or setting a custom value where their values do not coincide" @@ -322,7 +332,7 @@ (compare-properties props-list nil)) ([props-list distinct-mark] - (let [grouped (group-by :name (apply concat props-list)) + (let [grouped (group-properties-by-name (apply concat props-list)) check-values (fn [values] (let [vals (map :value values)] (if (apply = vals) diff --git a/common/test/common_tests/logic/variants_test.cljc b/common/test/common_tests/logic/variants_test.cljc index 8162cf5598..a2ba965283 100644 --- a/common/test/common_tests/logic/variants_test.cljc +++ b/common/test/common_tests/logic/variants_test.cljc @@ -7,6 +7,7 @@ (ns common-tests.logic.variants-test (:require [app.common.files.changes-builder :as pcb] + [app.common.files.variant :as cfv] [app.common.geom.point :as gpt] [app.common.logic.libraries :as cll] [app.common.logic.shapes :as cls] @@ -722,3 +723,16 @@ (cpn/merge-path-item (:path comp2') (:name comp2')))) (t/is (some? (:variant-id comp1'))) (t/is (some? (:variant-id comp2'))))) + +(t/deftest test-extract-properties-values-preserves-order-past-eight + (let [names (mapv #(str "Property " %) (range 1 10)) + props1 (mapv (fn [n] {:name n :value (str n "-a")}) names) + props2 (mapv (fn [n] {:name n :value (str n "-b")}) names) + file (-> (thf/sample-file :file1) + (thv/add-variant :v01 :c01 :m01 :c02 :m02) + (thc/update-component :c01 {:variant-properties props1}) + (thc/update-component :c02 {:variant-properties props2})) + page (thf/current-page file) + v-id (:id (ths/get-shape file :v01)) + result (cfv/extract-properties-values (:data file) (:objects page) v-id)] + (t/is (= names (mapv :name result))))) diff --git a/common/test/common_tests/variant_test.cljc b/common/test/common_tests/variant_test.cljc index 0dea2aa947..bcccea9f1c 100644 --- a/common/test/common_tests/variant_test.cljc +++ b/common/test/common_tests/variant_test.cljc @@ -119,6 +119,12 @@ (t/is (= (ctv/compare-properties [props-1 props-2 props-3] "&") [{:name "border" :value "&"} {:name "color" :value "&"}]))))) +(t/deftest compare-properties-preserves-order-past-eight + (let [names (mapv #(str "Property " %) (range 1 10)) + props1 (mapv (fn [n] {:name n :value "a"}) names) + props2 (mapv (fn [n] {:name n :value "b"}) names)] + (t/is (= names (mapv :name (ctv/compare-properties [props1 props2])))))) + (t/deftest check-belong-same-variant (let [components-1 [{:variant-id "a variant"} {:variant-id "a variant"}] diff --git a/frontend/src/app/main/data/workspace/variants.cljs b/frontend/src/app/main/data/workspace/variants.cljs index 49eb435f34..8c7cdbe34c 100644 --- a/frontend/src/app/main/data/workspace/variants.cljs +++ b/frontend/src/app/main/data/workspace/variants.cljs @@ -235,14 +235,10 @@ (let [data (dsh/lookup-file-data state)] (with-main-page data {:variant-id variant-id} (fn [page-id objects] - (let [variant-components (cfv/find-variant-components data objects variant-id) - properties-empty-pos (->> variant-components - (mapcat :variant-properties) - (group-by :name) + (let [properties-empty-pos (->> (cfv/extract-properties-values data objects variant-id) (map-indexed - (fn [i [_ v]] - [i (->> v - (map :value) + (fn [i {:keys [value]}] + [i (->> value (remove empty?) empty?)])) (reverse)) diff --git a/frontend/test/frontend_tests/data/workspace_variants_test.cljs b/frontend/test/frontend_tests/data/workspace_variants_test.cljs index d596add6c2..b293ba2560 100644 --- a/frontend/test/frontend_tests/data/workspace_variants_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_variants_test.cljs @@ -35,6 +35,26 @@ (thv/add-variant-two-properties :v01 :c01 :m01 :c02 :m02) (cthf/add-sample-page :page2))) +(defn- nine-property-file-with-empty-middle + "Nine properties; Property 5 is empty on every variant." + [] + (let [names (mapv #(str "Property " %) (range 1 10)) + props1 (mapv (fn [n i] + {:name n :value (if (= i 4) "" (str "a" (inc i)))}) + names + (range)) + props2 (mapv (fn [n i] + {:name n :value (if (= i 4) "" (str "b" (inc i)))}) + names + (range))] + (-> (cthf/sample-file :file1 :page-label :page1) + (thv/add-variant :v01 :c01 :m01 :c02 :m02) + (cthc/update-component :c01 {:variant-properties props1}) + (cthc/update-component :c02 {:variant-properties props2}) + (cths/update-shape :m01 :variant-name (ctv/properties-to-name props1)) + (cths/update-shape :m02 :variant-name (ctv/properties-to-name props2)) + (cthf/add-sample-page :page2)))) + (defn- run [file events done assert-fn] (ths/run-store @@ -124,6 +144,24 @@ (t/is (= "crítica" (-> props second :value))) (t/is (= (ctv/properties-to-name props) (variant-name file :m01))))))))) +(t/deftest remove-empty-properties-preserves-order-past-eight + (t/async + done + (let [file (nine-property-file-with-empty-middle) + variant-id (cthi/id :v01) + expected ["Property 1" "Property 2" "Property 3" "Property 4" + "Property 6" "Property 7" "Property 8" "Property 9"]] + (run file + [(dwv/remove-empty-properties variant-id)] + done + (fn [state] + (let [file (ths/get-file-from-state state) + names1 (mapv :name (:variant-properties (cthc/get-component file :c01))) + names2 (mapv :name (:variant-properties (cthc/get-component file :c02)))] + (t/is (= expected names1)) + (t/is (= expected names2)) + (t/is (not-any? #(= "Property 5" %) names1)))))))) + (t/deftest update-property-name-from-another-page (t/async done