diff --git a/common/src/app/common/logic/variant_properties.cljc b/common/src/app/common/logic/variant_properties.cljc index 02490a3c81..f24b576a18 100644 --- a/common/src/app/common/logic/variant_properties.cljc +++ b/common/src/app/common/logic/variant_properties.cljc @@ -14,6 +14,16 @@ [app.common.types.variant :as ctv] [cuerdas.core :as str])) +(defn- set-variant-properties + "Set the variant properties of `component` to `props`, and the variant name + of its main to match them." + [changes component props] + (-> changes + (pcb/update-component (:id component) #(assoc % :variant-properties props) + {:apply-changes-local-library? true}) + (pcb/update-shapes [(:main-instance-id component)] + #(assoc % :variant-name (ctv/properties-to-name props))))) + (defn generate-update-property-name [changes variant-id pos new-name] (if-let [new-name (ctv/prepare-property-name new-name)] @@ -44,14 +54,8 @@ props (-> related-components first :variant-properties)] (if (and (seq props) (<= 0 pos) (< pos (count props))) (reduce (fn [changes component] - (let [props (:variant-properties component) - props (d/remove-at-index props pos) - main-id (:main-instance-id component) - name (ctv/properties-to-name props)] - (-> changes - (pcb/update-component (:id component) #(assoc % :variant-properties props) - {:apply-changes-local-library? true}) - (pcb/update-shapes [main-id] #(assoc % :variant-name name))))) + (set-variant-properties changes component + (d/remove-at-index (:variant-properties component) pos))) changes related-components) changes))) @@ -60,18 +64,11 @@ (defn generate-update-property-value [changes component-id pos value] (if-let [value (ctv/prepare-property-value value)] - (let [data (pcb/get-library-data changes) - objects (pcb/get-objects changes) - component (ctcl/get-component data component-id true) - main-id (:main-instance-id component)] - (if (contains? objects main-id) - (let [name (-> (:variant-properties component) - (update pos assoc :value value) - ctv/properties-to-name)] - (-> changes - (pcb/update-component component-id #(assoc-in % [:variant-properties pos :value] value) - {:apply-changes-local-library? true}) - (pcb/update-shapes [main-id] #(assoc % :variant-name name)))) + (let [component (ctcl/get-component (pcb/get-library-data changes) component-id true)] + ;; The main must be in the objects of the changes + (if (contains? (pcb/get-objects changes) (:main-instance-id component)) + (set-variant-properties changes component + (update (:variant-properties component) pos assoc :value value)) changes)) changes)) @@ -87,22 +84,15 @@ #(assoc % :variant-error value)))))) -(defn generate-reorder-variant-poperties +(defn generate-reorder-variant-properties [changes variant-id from-pos to-space-between-pos] (let [data (pcb/get-library-data changes) objects (pcb/get-objects changes) related-components (cfv/find-variant-components data objects variant-id)] (reduce (fn [changes component] - (let [props (:variant-properties component) - props (d/reorder props from-pos to-space-between-pos) - main-id (:main-instance-id component) - name (ctv/properties-to-name props)] - (-> changes - (pcb/update-component (:id component) - #(assoc % :variant-properties props) - {:apply-changes-local-library? true}) - (pcb/update-shapes [main-id] - #(assoc % :variant-name name))))) + (set-variant-properties changes component + (d/reorder (:variant-properties component) + from-pos to-space-between-pos))) changes related-components))) diff --git a/common/src/app/common/test_helpers/composable/comp/nodes.cljc b/common/src/app/common/test_helpers/composable/comp/nodes.cljc index 3f06d7ef41..3bf1572b54 100644 --- a/common/src/app/common/test_helpers/composable/comp/nodes.cljc +++ b/common/src/app/common/test_helpers/composable/comp/nodes.cljc @@ -1499,7 +1499,7 @@ (clvp/generate-add-new-property changes variant-id opts)) :remove (apply clvp/generate-remove-property changes args) :rename (apply clvp/generate-update-property-name changes args) - :reorder (apply clvp/generate-reorder-variant-poperties changes args) + :reorder (apply clvp/generate-reorder-variant-properties changes args) :set-value (apply clvp/generate-update-property-value changes args))) (defrecord EditVariantProperty [set-name edit params] diff --git a/common/src/app/common/types/variant.cljc b/common/src/app/common/types/variant.cljc index f91075a170..eda46110e6 100644 --- a/common/src/app/common/types/variant.cljc +++ b/common/src/app/common/types/variant.cljc @@ -270,14 +270,6 @@ (filter some?))] (into #{} xform props1))) -(defn- find-index-by-name - "Returns the index of the first item in props with the given name, or nil if not found." - [name props] - (some (fn [[idx item]] - (when (= (:name item) name) - idx)) - (map-indexed vector props))) - (defn- next-valid-position "Returns the first non-negative integer not present in the used-pos set." [used-pos] @@ -290,7 +282,7 @@ "Returns the index of the property with the given name in `props`, or the next available index not in `used-pos` if not found." [name props used-pos] - (or (find-index-by-name name props) + (or (find-index-for-property-name props name) (next-valid-position used-pos))) (defn merge-properties diff --git a/frontend/src/app/main/data/workspace/variants.cljs b/frontend/src/app/main/data/workspace/variants.cljs index 2ffc023b9b..c4ad7fe971 100644 --- a/frontend/src/app/main/data/workspace/variants.cljs +++ b/frontend/src/app/main/data/workspace/variants.cljs @@ -66,6 +66,23 @@ (f page-id objects) (rx/empty))) +(defn- variant-changes + "The empty changes of the page that holds the mains `opts` names (see + `get-variant-mains-page`), with its objects and the data of the file, or + nil when the file has no such page. Without `opts`, of the current page." + ([it state] + (let [page-id (:current-page-id state) + data (dsh/lookup-file-data state)] + (-> (pcb/empty-changes it page-id) + (pcb/with-library-data data) + (pcb/with-objects (:objects (dsh/get-page data page-id)))))) + ([it state opts] + (let [data (dsh/lookup-file-data state)] + (when-let [{:keys [page-id objects]} (get-variant-mains-page data opts)] + (-> (pcb/empty-changes it page-id) + (pcb/with-library-data data) + (pcb/with-objects objects)))))) + (defn update-properties-names-and-values "Compares the previous properties with the updated ones and executes the correspondent action for each one depending on if it needs to be removed, updated or added" @@ -77,51 +94,41 @@ ptk/WatchEvent (watch [it state _] - (let [data (dsh/lookup-file-data state)] - (with-main-page data {:component-id component-id} - (fn [page-id objects] - (let [updated-properties (ctv/update-number-in-repeated-prop-names updated-properties) + (when-let [changes (variant-changes it state {:component-id component-id})] + (let [updated-properties (ctv/update-number-in-repeated-prop-names updated-properties) - properties-to-remove (ctv/find-properties-to-remove previous-properties updated-properties) - properties-to-add (ctv/find-properties-to-add previous-properties updated-properties) - properties-to-update (ctv/find-properties-to-update previous-properties updated-properties) + properties-to-remove (ctv/find-properties-to-remove previous-properties updated-properties) + properties-to-add (ctv/find-properties-to-add previous-properties updated-properties) + properties-to-update (ctv/find-properties-to-update previous-properties updated-properties) - changes (-> (pcb/empty-changes it page-id) - (pcb/with-objects objects) - (pcb/with-library-data data)) + changes (reduce + (fn [changes {:keys [name]}] + (-> changes + (clvp/generate-update-property-value component-id (ctv/find-index-for-property-name previous-properties name) ""))) + changes + properties-to-remove) - changes (reduce - (fn [changes {:keys [name]}] - (-> changes - (clvp/generate-update-property-value component-id (ctv/find-index-for-property-name previous-properties name) ""))) - changes - properties-to-remove) + changes (reduce + (fn [changes {:keys [name value]}] + (-> changes + (clvp/generate-update-property-value component-id (ctv/find-index-for-property-name previous-properties name) value))) + changes + properties-to-update) - changes (reduce - (fn [changes {:keys [name value]}] - (-> changes - (clvp/generate-update-property-value component-id (ctv/find-index-for-property-name previous-properties name) value))) - changes - properties-to-update) + changes (reduce + (fn [changes [idx {:keys [name value]}]] + (-> changes + (clvp/generate-add-new-property variant-id {:property-name name}) + (clvp/generate-update-property-value component-id (+ idx (count previous-properties)) value))) + changes + (map-indexed vector properties-to-add))] - changes (reduce - (fn [changes [idx {:keys [name value]}]] - (-> changes - (clvp/generate-add-new-property variant-id {:property-name name}) - (clvp/generate-update-property-value component-id (+ idx (count previous-properties)) value))) - changes - (map-indexed vector properties-to-add)) - - undo-id (js/Symbol)] - - (rx/of - (when (or (seq properties-to-remove) (seq properties-to-update)) - (ev/event {::ev/name "variant-edit-property-value" ::ev/origin "workspace:rename-in-layers"})) - (when (seq properties-to-add) - (ev/event {::ev/name "variant-add-property" ::ev/origin "workspace:rename-in-layers"})) - (dwu/start-undo-transaction undo-id) - (dch/commit-changes changes) - (dwu/commit-undo-transaction undo-id))))))))) + (rx/of + (when (or (seq properties-to-remove) (seq properties-to-update)) + (ev/event {::ev/name "variant-edit-property-value" ::ev/origin "workspace:rename-in-layers"})) + (when (seq properties-to-add) + (ev/event {::ev/name "variant-add-property" ::ev/origin "workspace:rename-in-layers"})) + (dch/commit-changes changes))))))) (defn update-property-name "Update the variant property name on the position pos @@ -145,25 +152,17 @@ ptk/WatchEvent (watch [it state _] - (let [data (dsh/lookup-file-data state)] - (with-main-page data {:variant-id variant-id} - (fn [page-id objects] - (let [related-components (cfv/find-variant-components data objects variant-id) - props (-> related-components last :variant-properties) - valid-pos? (> (count props) pos) - prop-name (when valid-pos? (-> props (nth pos) :name)) - changes (when valid-pos? - (-> (pcb/empty-changes it page-id) - (pcb/with-objects objects) - (pcb/with-library-data data) - (clvp/generate-update-property-name variant-id pos new-name))) - undo-id (js/Symbol)] - (when (and valid-pos? (not= prop-name new-name)) - (rx/of - (dwu/start-undo-transaction undo-id) - (dch/commit-changes changes) - (dwu/commit-undo-transaction undo-id) - (ev/event {::ev/name "variant-edit-property-name" ::ev/origin trigger})))))))))) + (when-let [changes (variant-changes it state {:variant-id variant-id})] + (let [data (dsh/lookup-file-data state) + props (-> (cfv/find-variant-components data (pcb/get-objects changes) variant-id) + last + :variant-properties) + valid-pos? (> (count props) pos) + prop-name (when valid-pos? (-> props (nth pos) :name))] + (when (and valid-pos? (not= prop-name new-name)) + (rx/of + (dch/commit-changes (clvp/generate-update-property-name changes variant-id pos new-name)) + (ev/event {::ev/name "variant-edit-property-name" ::ev/origin trigger})))))))) (defn update-property-value "Updates the variant property value on the position pos in a component" @@ -171,18 +170,8 @@ (ptk/reify ::update-property-value ptk/WatchEvent (watch [it state _] - (let [data (dsh/lookup-file-data state)] - (with-main-page data {:component-id component-id} - (fn [page-id objects] - (let [changes (-> (pcb/empty-changes it page-id) - (pcb/with-library-data data) - (pcb/with-objects objects) - (clvp/generate-update-property-value component-id pos value)) - undo-id (js/Symbol)] - (rx/of - (dwu/start-undo-transaction undo-id) - (dch/commit-changes changes) - (dwu/commit-undo-transaction undo-id))))))))) + (when-let [changes (variant-changes it state {:component-id component-id})] + (rx/of (dch/commit-changes (clvp/generate-update-property-value changes component-id pos value))))))) (defn update-error "Sets or unsets an error for a component" @@ -192,18 +181,8 @@ (ptk/reify ::update-error ptk/WatchEvent (watch [it state _] - (let [data (dsh/lookup-file-data state)] - (with-main-page data {:component-id component-id} - (fn [page-id objects] - (let [changes (-> (pcb/empty-changes it page-id) - (pcb/with-library-data data) - (pcb/with-objects objects) - (clvp/generate-set-variant-error component-id value)) - undo-id (js/Symbol)] - (rx/of - (dwu/start-undo-transaction undo-id) - (dch/commit-changes changes) - (dwu/commit-undo-transaction undo-id)))))))))) + (when-let [changes (variant-changes it state {:component-id component-id})] + (rx/of (dch/commit-changes (clvp/generate-set-variant-error changes component-id value)))))))) (defn remove-property "Remove the variant property on the position pos @@ -212,18 +191,8 @@ (ptk/reify ::remove-property ptk/WatchEvent (watch [it state _] - (let [data (dsh/lookup-file-data state)] - (with-main-page data {:variant-id variant-id} - (fn [page-id objects] - (let [changes (-> (pcb/empty-changes it page-id) - (pcb/with-library-data data) - (pcb/with-objects objects) - (clvp/generate-remove-property variant-id pos)) - undo-id (js/Symbol)] - (rx/of - (dwu/start-undo-transaction undo-id) - (dch/commit-changes changes) - (dwu/commit-undo-transaction undo-id))))))))) + (when-let [changes (variant-changes it state {:variant-id variant-id})] + (rx/of (dch/commit-changes (clvp/generate-remove-property changes variant-id pos))))))) (defn remove-empty-properties "Remove every empty property for all components when their respective values are empty @@ -232,39 +201,35 @@ (ptk/reify ::remove-empty-properties ptk/WatchEvent (watch [it state _] - (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) - (map-indexed - (fn [i [_ v]] - [i (->> v - (map :value) - (remove empty?) - empty?)])) - (reverse)) - changes (-> (pcb/empty-changes it page-id) - (pcb/with-library-data data) - (pcb/with-objects objects)) - changes (reduce - (fn [changes [pos property-empty?]] - (if property-empty? - (-> changes - (clvp/generate-remove-property variant-id pos)) - changes)) - changes - properties-empty-pos) - undo-id (js/Symbol)] + (when-let [changes (variant-changes it state {:variant-id variant-id})] + (let [data (dsh/lookup-file-data state) - (when (seq (:redo-changes changes)) - (rx/of - (ev/event {::ev/name "variant-remove-property" ::ev/origin "workspace:rename-in-layers"}) - (dwu/start-undo-transaction undo-id) - (dch/commit-changes changes) - (dwu/commit-undo-transaction undo-id)))))))))) + 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)))))))) (defn add-new-property "Add a new variant property to all the components with this variant-id" @@ -272,37 +237,17 @@ (ptk/reify ::add-new-property ptk/WatchEvent (watch [it state _] - (let [data (dsh/lookup-file-data state)] - (with-main-page data {:variant-id variant-id} - (fn [page-id objects] - (let [changes (-> (pcb/empty-changes it page-id) - (pcb/with-library-data data) - (pcb/with-objects objects) - (clvp/generate-add-new-property variant-id options)) - undo-id (js/Symbol)] - (rx/of - (dwu/start-undo-transaction undo-id) - (dch/commit-changes changes) - (dwu/commit-undo-transaction undo-id))))))))) + (when-let [changes (variant-changes it state {:variant-id variant-id})] + (rx/of (dch/commit-changes (clvp/generate-add-new-property changes variant-id options))))))) -(defn reorder-variant-poperties +(defn reorder-variant-properties "Reorder properties by moving a property from some position to some space between positions" [variant-id from-pos to-space-between-pos] (ptk/reify ::reorder-variant-properties ptk/WatchEvent (watch [it state _] - (let [data (dsh/lookup-file-data state)] - (with-main-page data {:variant-id variant-id} - (fn [page-id objects] - (let [changes (-> (pcb/empty-changes it page-id) - (pcb/with-library-data data) - (pcb/with-objects objects) - (clvp/generate-reorder-variant-poperties variant-id from-pos to-space-between-pos)) - undo-id (js/Symbol)] - (rx/of - (dwu/start-undo-transaction undo-id) - (dch/commit-changes changes) - (dwu/commit-undo-transaction undo-id))))))))) + (when-let [changes (variant-changes it state {:variant-id variant-id})] + (rx/of (dch/commit-changes (clvp/generate-reorder-variant-properties changes variant-id from-pos to-space-between-pos))))))) (defn- define-variant-container "Read page objects after the artboard exists, then commit the defining @@ -311,15 +256,9 @@ (ptk/reify ::define-variant-container ptk/WatchEvent (watch [it state _] - (let [page-id (:current-page-id state) - data (dsh/lookup-file-data state) - objects (dsh/lookup-page-objects state page-id) - changes (-> (pcb/empty-changes it page-id) - (pcb/with-objects objects) - (pcb/with-library-data data) - (clv/generate-define-variant-container - variant-id main-id component-id name))] - (rx/of (dch/commit-changes changes)))))) + (rx/of (dch/commit-changes (-> (variant-changes it state) + (clv/generate-define-variant-container + variant-id main-id component-id name))))))) (defn- focus-property [variant-id] diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/component.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/menus/component.cljs index 54bdaa2393..8d204a268c 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/options/menus/component.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/component.cljs @@ -420,7 +420,7 @@ (mf/use-fn (mf/deps variant-id) (fn [from-pos to-space-between-pos] - (st/emit! (dwv/reorder-variant-poperties variant-id from-pos to-space-between-pos))))] + (st/emit! (dwv/reorder-variant-properties variant-id from-pos to-space-between-pos))))] [:* [:> h/sortable-container* {} @@ -1271,7 +1271,7 @@ (mf/use-fn (mf/deps variant-id) (fn [from-pos to-space-between-pos] - (st/emit! (dwv/reorder-variant-poperties variant-id from-pos to-space-between-pos)))) + (st/emit! (dwv/reorder-variant-properties variant-id from-pos to-space-between-pos)))) select-shapes-with-malformed (mf/use-fn diff --git a/frontend/test/frontend_tests/composable_tests/interpreter.cljs b/frontend/test/frontend_tests/composable_tests/interpreter.cljs index d33f9f95c2..f98a90fffb 100644 --- a/frontend/test/frontend_tests/composable_tests/interpreter.cljs +++ b/frontend/test/frontend_tests/composable_tests/interpreter.cljs @@ -251,7 +251,7 @@ :remove (apply dwv/remove-property args) :rename (let [[variant-id pos name] args] (dwv/update-property-name variant-id pos name {:trigger "workspace:design-tab"})) - :reorder (apply dwv/reorder-variant-poperties args) + :reorder (apply dwv/reorder-variant-properties args) :set-value (apply dwv/update-property-value args))]) (instance? n/Detach op)